973c2be1 |
1 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
2 | // |
973c2be1 |
3 | // This file is part of Open CASCADE Technology software library. |
b311480e |
4 | // |
d5f74e42 |
5 | // This library is free software; you can redistribute it and/or modify it under |
6 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
7 | // by the Free Software Foundation, with special exception defined in the file |
8 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
9 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
10 | // |
973c2be1 |
11 | // Alternatively, this file may be used under the terms of Open CASCADE |
12 | // commercial license or contractual agreement. |
b311480e |
13 | |
42cf5bc1 |
14 | |
15 | #include <IntTools_CArray1OfReal.hxx> |
16 | #include <IntTools_MarkedRangeSet.hxx> |
17 | #include <IntTools_Range.hxx> |
7fd59977 |
18 | |
19 | IntTools_MarkedRangeSet::IntTools_MarkedRangeSet() : |
20 | myRangeNumber(0) |
21 | { |
22 | } |
23 | |
24 | IntTools_MarkedRangeSet::IntTools_MarkedRangeSet(const Standard_Real theFirstBoundary, |
25 | const Standard_Real theLastBoundary, |
26 | const Standard_Integer theInitFlag) |
27 | { |
28 | SetBoundaries(theFirstBoundary, theLastBoundary, theInitFlag); |
29 | } |
30 | |
31 | IntTools_MarkedRangeSet::IntTools_MarkedRangeSet(const IntTools_CArray1OfReal& theSortedArray, |
32 | const Standard_Integer theInitFlag) |
33 | |
34 | { |
35 | SetRanges(theSortedArray, theInitFlag); |
36 | } |
37 | |
38 | void IntTools_MarkedRangeSet::SetBoundaries(const Standard_Real theFirstBoundary, |
39 | const Standard_Real theLastBoundary, |
40 | const Standard_Integer theInitFlag) |
41 | { |
42 | myRangeSetStorer.Clear(); |
43 | myRangeSetStorer.Append(theFirstBoundary); |
44 | myRangeSetStorer.Append(theLastBoundary); |
45 | myRangeNumber = 1; |
46 | myFlags.Clear(); |
47 | myFlags.Append(theInitFlag); |
48 | } |
49 | |
50 | void IntTools_MarkedRangeSet::SetRanges(const IntTools_CArray1OfReal& theSortedArray, |
51 | const Standard_Integer theInitFlag) |
52 | { |
53 | myRangeSetStorer.Clear(); |
54 | myFlags.Clear(); |
55 | Standard_Integer i = 0; |
56 | |
57 | for(i = 0; i < theSortedArray.Length(); i++) { |
58 | myRangeSetStorer.Append(theSortedArray(i)); |
59 | } |
60 | myRangeNumber = myRangeSetStorer.Length() - 1; |
61 | |
62 | for(i = 1; i <= myRangeNumber; i++) { |
63 | myFlags.Append(theInitFlag); |
64 | } |
65 | } |
66 | |
67 | Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary, |
68 | const Standard_Real theLastBoundary, |
69 | const Standard_Integer theFlag) |
70 | { |
71 | Standard_Integer anIndex1 = GetIndex(theFirstBoundary, Standard_True); |
72 | |
73 | if(!anIndex1) |
74 | return Standard_False; |
75 | Standard_Integer anIndex2 = GetIndex(theLastBoundary, Standard_False); |
76 | |
77 | if(!anIndex2) |
78 | return Standard_False; |
79 | |
80 | if(anIndex2 < anIndex1) { // it can be if theLastBoundary==theFirstBoundary==boundary_of_a_range or theFirstBoundary > theLastBoundary |
81 | Standard_Integer atmpindex = anIndex1; |
82 | anIndex1 = anIndex2; |
83 | anIndex2 = atmpindex; |
84 | |
85 | if(theLastBoundary < theFirstBoundary) |
86 | return Standard_False; |
87 | } |
88 | |
89 | Standard_Boolean areEqualIndices = (anIndex1 == anIndex2); |
90 | Standard_Integer aPrevFlag = myFlags(anIndex1); |
91 | |
92 | myRangeSetStorer.InsertAfter(anIndex1, theFirstBoundary); |
93 | anIndex2++; |
94 | myFlags.InsertAfter(anIndex1, theFlag); |
95 | myRangeNumber = myRangeSetStorer.Length() - 1; |
96 | |
97 | myRangeSetStorer.InsertAfter(anIndex2, theLastBoundary); |
98 | |
99 | if(areEqualIndices) { |
100 | myFlags.InsertAfter(anIndex2, aPrevFlag); |
101 | } |
102 | else { |
103 | myFlags.InsertBefore(anIndex2, theFlag); |
104 | } |
105 | |
106 | if(!areEqualIndices) { |
107 | anIndex1++; |
108 | anIndex2++; |
109 | |
110 | for(Standard_Integer i = anIndex1; i < anIndex2; i++) { |
111 | myFlags.SetValue(i, theFlag); |
112 | } |
113 | } |
114 | |
115 | myRangeNumber = myRangeSetStorer.Length() - 1; |
116 | |
117 | return Standard_True; |
118 | } |
119 | |
120 | Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange, |
121 | const Standard_Integer theFlag) |
122 | { |
123 | return InsertRange(theRange.First(), theRange.Last(), theFlag); |
124 | } |
125 | |
126 | Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const Standard_Real theFirstBoundary, |
127 | const Standard_Real theLastBoundary, |
128 | const Standard_Integer theFlag, |
129 | const Standard_Integer theIndex) |
130 | { |
131 | Standard_Real aTolerance = 1.e-15; |
132 | Standard_Integer anIndex = theIndex; |
133 | |
134 | if((theIndex <= 0) || (theIndex > myRangeNumber)) |
135 | return Standard_False; |
136 | |
137 | if((theFirstBoundary < myRangeSetStorer(theIndex)) || |
138 | (theLastBoundary > myRangeSetStorer(theIndex+1)) || |
139 | (Abs(theFirstBoundary - theLastBoundary) < aTolerance)) { |
140 | return InsertRange(theFirstBoundary, theLastBoundary, theFlag); |
141 | } |
142 | else { |
143 | Standard_Integer aPrevFlag = myFlags(anIndex); |
144 | |
145 | if((Abs(theFirstBoundary - myRangeSetStorer(anIndex)) > aTolerance) && |
146 | (Abs(theFirstBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) { |
147 | myRangeSetStorer.InsertAfter(anIndex, theFirstBoundary); |
148 | myFlags.InsertAfter(anIndex, theFlag); |
149 | anIndex++; |
150 | myRangeNumber = myRangeSetStorer.Length() - 1; |
151 | } |
152 | else { |
153 | myFlags.SetValue(anIndex, theFlag); |
154 | } |
155 | |
156 | if((Abs(theLastBoundary - myRangeSetStorer(anIndex)) > aTolerance) && |
157 | (Abs(theLastBoundary - myRangeSetStorer(anIndex+1)) > aTolerance)) { |
158 | myRangeSetStorer.InsertAfter(anIndex, theLastBoundary); |
159 | myRangeNumber = myRangeSetStorer.Length() - 1; |
160 | myFlags.InsertAfter(anIndex, aPrevFlag); |
161 | } |
162 | } |
163 | |
164 | return Standard_True; |
165 | } |
166 | |
167 | Standard_Boolean IntTools_MarkedRangeSet::InsertRange(const IntTools_Range& theRange, |
168 | const Standard_Integer theFlag, |
169 | const Standard_Integer theIndex) |
170 | { |
171 | return InsertRange(theRange.First(), theRange.Last(), theFlag, theIndex); |
172 | } |
173 | |
174 | void IntTools_MarkedRangeSet::SetFlag(const Standard_Integer theIndex, |
175 | const Standard_Integer theFlag) |
176 | { |
177 | myFlags.SetValue(theIndex, theFlag); |
178 | } |
179 | |
180 | Standard_Integer IntTools_MarkedRangeSet::Flag(const Standard_Integer theIndex) const |
181 | { |
182 | return myFlags(theIndex); |
183 | } |
184 | |
185 | const TColStd_SequenceOfInteger& IntTools_MarkedRangeSet::GetIndices(const Standard_Real theValue) |
186 | { |
187 | myFoundIndices.Clear(); |
188 | |
189 | if(theValue < myRangeSetStorer(1)) |
190 | return myFoundIndices; |
191 | else { |
192 | Standard_Boolean found = Standard_False; |
193 | |
194 | for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) { |
195 | if(found) { |
196 | if(theValue >= myRangeSetStorer(i-1)) { |
197 | myFoundIndices.Append(i-1); |
198 | } |
199 | else { |
200 | break; |
201 | } |
202 | } |
203 | else { |
204 | if(theValue <= myRangeSetStorer(i)) { |
205 | myFoundIndices.Append(i-1); |
206 | found = Standard_True; |
207 | } |
208 | else { |
209 | if(found) { |
210 | break; |
211 | } |
212 | } |
213 | } |
214 | } |
215 | } |
216 | return myFoundIndices; |
217 | } |
218 | |
219 | |
220 | Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue) const |
221 | { |
222 | Standard_Integer anIndex = 0; |
223 | |
224 | if(theValue < myRangeSetStorer(1)) |
225 | anIndex = 0; |
226 | else { |
227 | for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) { |
228 | if(theValue <= myRangeSetStorer(i)) { |
229 | anIndex = i-1; |
230 | break; |
231 | } |
232 | } |
233 | } |
234 | |
235 | return anIndex; |
236 | } |
237 | |
238 | Standard_Integer IntTools_MarkedRangeSet::GetIndex(const Standard_Real theValue, |
239 | const Standard_Boolean UseLower) const |
240 | { |
241 | Standard_Integer anIndex = 0; |
242 | |
0ebaa4db |
243 | if((UseLower && (theValue < myRangeSetStorer(1))) || |
7fd59977 |
244 | (!UseLower && (theValue <= myRangeSetStorer(1)))) |
245 | anIndex = 0; |
246 | else { |
247 | for(Standard_Integer i = 2; i <= myRangeSetStorer.Length(); i++) { |
248 | if((UseLower && theValue < myRangeSetStorer(i)) || |
249 | (!UseLower && theValue <= myRangeSetStorer(i))) { |
250 | anIndex = i-1; |
251 | break; |
252 | } |
253 | } |
254 | } |
255 | return anIndex; |
256 | } |
257 | |
258 | IntTools_Range IntTools_MarkedRangeSet::Range(const Standard_Integer theIndex) const |
259 | { |
260 | IntTools_Range aRange(myRangeSetStorer(theIndex), myRangeSetStorer(theIndex+1)); |
261 | return aRange; |
262 | } |