b311480e |
1 | // Created on: 1993-03-08 |
2 | // Created by: Didier PIFFAULT |
3 | // Copyright (c) 1993-1999 Matra Datavision |
973c2be1 |
4 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
5 | // |
973c2be1 |
6 | // This file is part of Open CASCADE Technology software library. |
b311480e |
7 | // |
d5f74e42 |
8 | // This library is free software; you can redistribute it and/or modify it under |
9 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
10 | // by the Free Software Foundation, with special exception defined in the file |
11 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
12 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
13 | // |
973c2be1 |
14 | // Alternatively, this file may be used under the terms of Open CASCADE |
15 | // commercial license or contractual agreement. |
7fd59977 |
16 | |
17 | #include <Bnd_BoundSortBox2d.ixx> |
18 | #include <Standard_NullValue.hxx> |
19 | #include <Bnd_Array1OfBox.hxx> |
20 | #include <TColStd_ListIteratorOfListOfInteger.hxx> |
21 | #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx> |
22 | #include <TColStd_Array1OfListOfInteger.hxx> |
23 | |
24 | |
25 | //======================================================================= |
26 | //function : Bnd_BoundSortBox2d |
27 | //purpose : |
28 | //======================================================================= |
29 | |
30 | Bnd_BoundSortBox2d::Bnd_BoundSortBox2d() |
31 | : discrX(0), discrY(0) |
32 | {} |
33 | |
34 | |
35 | //======================================================================= |
36 | //function : Initialize |
37 | //purpose : |
38 | //======================================================================= |
39 | |
40 | void Bnd_BoundSortBox2d::Initialize(const Bnd_Box2d& CompleteBox, |
41 | const Handle(Bnd_HArray1OfBox2d)& SetOfBox) |
42 | { |
43 | myBox=CompleteBox; |
44 | myBndComponents=SetOfBox; |
45 | discrX=SetOfBox->Length(); |
46 | discrY=discrX; |
47 | |
48 | Standard_Real xmin, ymin, xmax, ymax; |
49 | Standard_Real middleX=0.; |
50 | Standard_Real middleY=0.; |
51 | |
52 | const Bnd_Array1OfBox2d & taBox=myBndComponents->Array1(); |
53 | |
54 | Standard_Integer labox; |
55 | for (labox=taBox.Lower(); labox<=taBox.Upper(); labox++) { |
56 | if (!taBox(labox).IsVoid()) { |
57 | taBox.Value(labox).Get(xmin, ymin, xmax, ymax); |
58 | middleX+=xmax-xmin; |
59 | middleY+=ymax-ymin; |
60 | } |
61 | } |
62 | middleX=middleX/taBox.Length(); |
63 | middleY=middleY/taBox.Length(); |
64 | |
65 | Standard_Real Xmax, Ymax; |
66 | CompleteBox.Get(Xmin, Ymin, Xmax, Ymax); |
67 | deltaX=(Xmax-Xmin)/(Standard_Real)discrX; |
68 | deltaY=(Ymax-Ymin)/(Standard_Real)discrY; |
69 | |
70 | if (middleX < Epsilon(100.)) { |
71 | discrX=1; |
72 | deltaX=Xmax-Xmin; |
73 | } |
74 | else if (middleX > deltaX) { |
75 | discrX=(Standard_Integer)((Xmax-Xmin)/middleX); |
76 | deltaX=middleX; |
77 | discrX++; |
78 | } |
79 | |
80 | if (middleY < Epsilon(100.)) { |
81 | discrY=1; |
82 | deltaY=Ymax-Ymin; |
83 | } |
84 | else if (middleY > deltaY) { |
85 | discrY=(Standard_Integer)((Ymax-Ymin)/middleY + 0.1); |
86 | deltaY=middleY; |
87 | discrY++; |
88 | } |
89 | |
90 | SortBoxes(); |
91 | } |
92 | |
93 | //======================================================================= |
94 | //function : Initialize |
95 | //purpose : |
96 | //======================================================================= |
97 | |
857ffd5e |
98 | void Bnd_BoundSortBox2d::Initialize(const Handle(Bnd_HArray1OfBox2d)& SetOfBox) |
7fd59977 |
99 | { |
100 | myBndComponents=SetOfBox; |
101 | discrX=SetOfBox->Length(); |
102 | discrY=discrX; |
103 | |
104 | Standard_Real xmin, ymin, xmax, ymax; |
105 | Standard_Real middleX=0.; |
106 | Standard_Real middleY=0.; |
107 | |
108 | const Bnd_Array1OfBox2d & taBox=myBndComponents->Array1(); |
109 | |
110 | Standard_Integer labox; |
111 | for (labox=taBox.Lower(); labox<=taBox.Upper(); labox++) { |
112 | if (!taBox(labox).IsVoid()) { |
113 | myBox.Add(taBox(labox)); |
114 | taBox.Value(labox).Get(xmin, ymin, xmax, ymax); |
115 | middleX+=xmax-xmin; |
116 | middleY+=ymax-ymin; |
117 | } |
118 | } |
119 | middleX=middleX/taBox.Length(); |
120 | middleY=middleY/taBox.Length(); |
121 | |
122 | Standard_Real Xmax, Ymax; |
123 | myBox.Get(Xmin, Ymin, Xmax, Ymax); |
124 | deltaX=(Xmax-Xmin)/(Standard_Real)discrX; |
125 | deltaY=(Ymax-Ymin)/(Standard_Real)discrY; |
126 | |
127 | if (middleX < Epsilon(100.)) { |
128 | discrX=1; |
129 | deltaX=Xmax-Xmin; |
130 | } |
131 | else if (middleX > deltaX) { |
132 | discrX=(Standard_Integer)((Xmax-Xmin)/middleX); |
133 | deltaX=middleX; |
134 | discrX++; |
135 | } |
136 | |
137 | if (middleY < Epsilon(100.)) { |
138 | discrY=1; |
139 | deltaY=Ymax-Ymin; |
140 | } |
141 | else if (middleY > deltaY) { |
142 | discrY=(Standard_Integer)((Ymax-Ymin)/middleY + 0.1); |
143 | deltaY=middleY; |
144 | discrY++; |
145 | } |
146 | |
147 | SortBoxes(); |
148 | } |
149 | |
150 | |
151 | //======================================================================= |
152 | //function : SortBoxes |
153 | //purpose : |
154 | //======================================================================= |
155 | |
156 | void Bnd_BoundSortBox2d::SortBoxes() |
157 | { |
158 | Standard_NullValue_Raise_if (discrX+discrY <=0, "BoundSortBox2d nul!"); |
159 | |
160 | Standard_Integer labox, lacase, firstcase, lastcase; |
161 | Standard_Real xmin, ymin, xmax, ymax; |
162 | const Bnd_Array1OfBox2d & taBox=myBndComponents->Array1(); |
163 | |
164 | axisX=new TColStd_HArray1OfListOfInteger(1, discrX); |
165 | TColStd_Array1OfListOfInteger & tabListX=axisX->ChangeArray1(); |
166 | |
167 | axisY=new TColStd_HArray1OfListOfInteger(1, discrY); |
168 | TColStd_Array1OfListOfInteger & tabListY=axisY->ChangeArray1(); |
169 | |
170 | for (labox=taBox.Lower(); labox<=taBox.Upper(); labox++) { |
171 | if (!taBox(labox).IsVoid()) { |
172 | taBox(labox).Get(xmin, ymin, xmax, ymax); |
173 | |
174 | if (discrX>1) { |
175 | firstcase=(Standard_Integer ) Max(1.0, (xmin-Xmin)/deltaX); |
176 | lastcase=(Standard_Integer ) Min((Standard_Real)discrX, ((xmax-Xmin)/deltaX)+1); |
177 | for (lacase=firstcase; lacase<=lastcase; lacase++) { |
178 | tabListX(lacase).Append(labox); |
179 | } |
180 | } |
181 | |
182 | if (discrY >1) { |
183 | firstcase=(Standard_Integer ) Max(1.0, (ymin-Ymin)/deltaY); |
184 | lastcase=(Standard_Integer ) Min((Standard_Real)discrY, ((ymax-Ymin)/deltaY)+1); |
185 | for (lacase=firstcase; lacase<=lastcase; lacase++) { |
186 | tabListY(lacase).Append(labox); |
187 | } |
188 | } |
189 | } |
190 | } |
191 | } |
192 | |
193 | //======================================================================= |
194 | //function : Initialize |
195 | //purpose : |
196 | //======================================================================= |
197 | |
198 | void Bnd_BoundSortBox2d::Initialize(const Bnd_Box2d& CompleteBox, |
199 | const Standard_Integer nbComponents) |
200 | { |
201 | Standard_NullValue_Raise_if (nbComponents <=0, "BoundSortBox nul!"); |
202 | |
203 | myBox=CompleteBox; |
204 | myBndComponents=new Bnd_HArray1OfBox2d(1, nbComponents); |
205 | |
206 | Bnd_Box2d emptyBox; |
207 | myBndComponents->Init( emptyBox ); |
208 | |
209 | discrX=nbComponents; |
210 | discrY=nbComponents; |
211 | |
212 | Standard_Real Xmax, Ymax; |
213 | CompleteBox.Get(Xmin, Ymin, Xmax, Ymax); |
214 | |
215 | deltaX=(Xmax-Xmin)/(Standard_Real)discrX; |
216 | deltaY=(Ymax-Ymin)/(Standard_Real)discrY; |
217 | |
218 | if (deltaX < Epsilon(100.)) { |
219 | discrX=1; |
220 | deltaX=Xmax-Xmin; |
221 | } |
222 | else axisX=new TColStd_HArray1OfListOfInteger(1, discrX); |
223 | |
224 | if (deltaY < Epsilon(100.)) { |
225 | discrY=1; |
226 | deltaY=Ymax-Ymin; |
227 | } |
228 | else axisY=new TColStd_HArray1OfListOfInteger(1, discrY); |
229 | } |
230 | |
231 | //======================================================================= |
232 | //function : Add |
233 | //purpose : |
234 | //======================================================================= |
235 | |
236 | void Bnd_BoundSortBox2d::Add(const Bnd_Box2d& theBox, |
237 | const Standard_Integer boxIndex) |
238 | { |
239 | Standard_MultiplyDefined_Raise_if |
240 | (!(myBndComponents->Value(boxIndex).IsVoid()), |
241 | " This box is already defined !"); |
242 | |
243 | if (!theBox.IsVoid()) { |
244 | Bnd_Array1OfBox2d & taBox=myBndComponents->ChangeArray1(); |
245 | Standard_Integer theGap, firstGap , lastGap; |
246 | Standard_Real xmin, ymin, xmax, ymax; |
247 | theBox.Get(xmin, ymin, xmax, ymax); |
248 | |
249 | if (taBox.Lower()<=boxIndex && boxIndex<=taBox.Upper()) |
250 | taBox(boxIndex).Update(xmin, ymin, xmax, ymax); |
251 | |
252 | TColStd_Array1OfListOfInteger & tabListX=axisX->ChangeArray1(); |
253 | |
254 | if (discrX>1) { |
255 | firstGap=(Standard_Integer ) Max(1.0, ((xmin-Xmin)/deltaX)+1); |
256 | lastGap=(Standard_Integer ) Min((Standard_Real)discrX, ((xmax-Xmin)/deltaX)+1); |
257 | for (theGap=firstGap; theGap<=lastGap; theGap++) { |
258 | tabListX(theGap).Append(boxIndex); |
259 | } |
260 | } |
261 | |
262 | TColStd_Array1OfListOfInteger & tabListY=axisY->ChangeArray1(); |
263 | |
264 | if (discrY >1) { |
265 | firstGap=(Standard_Integer ) Max(1.0, ((ymin-Ymin)/deltaY)+1); |
266 | lastGap=(Standard_Integer ) Min((Standard_Real)discrY, ((ymax-Ymin)/deltaY)+1); |
267 | for (theGap=firstGap; theGap<=lastGap; theGap++) { |
268 | tabListY(theGap).Append(boxIndex); |
269 | } |
270 | } |
271 | } |
272 | } |
273 | |
274 | |
275 | //======================================================================= |
276 | //function : Compare |
277 | //purpose : |
278 | //======================================================================= |
279 | |
280 | |
281 | const TColStd_ListOfInteger& Bnd_BoundSortBox2d::Compare |
282 | (const Bnd_Box2d& theBox) |
283 | { |
284 | Standard_NullValue_Raise_if (discrX+discrY <=0, |
285 | "Compare sur 1 BoundSortBox2d nul!"); |
286 | |
287 | lastResult.Clear(); |
288 | if (theBox.IsVoid()) return lastResult; |
289 | if (theBox.IsOut(myBox)) return lastResult; |
290 | |
291 | Standard_Integer lacase, firstcase, lastcase; |
292 | Standard_Real xmin, ymin, xmax, ymax; |
293 | theBox.Get(xmin, ymin, xmax, ymax); |
294 | |
295 | const Bnd_Array1OfBox2d & taBox=myBndComponents->Array1(); |
296 | Crible.Clear(); |
297 | theFound=2; |
298 | |
299 | Standard_Integer cardY=0; |
300 | if (discrY>1 && (!theBox.IsOpenYmin() || !theBox.IsOpenYmax())) { |
301 | const TColStd_Array1OfListOfInteger & tabList=axisY->Array1(); |
302 | firstcase=(Standard_Integer ) Max(1.0, (ymin-Ymin)/deltaY); |
303 | lastcase=(Standard_Integer ) Min((Standard_Real)discrY, ((ymax-Ymin)/deltaY)+1); |
304 | for (lacase=firstcase; lacase<=lastcase; lacase++) { |
305 | TColStd_ListIteratorOfListOfInteger theList(tabList(lacase)); |
306 | for (; theList.More(); theList.Next()) { |
307 | cardY++; |
308 | Crible.Bind(theList.Value(), 2); |
309 | } |
310 | } |
311 | if (cardY==0) return lastResult; |
312 | } |
313 | else { |
314 | if (ymin > Ymin+deltaY || ymax < Ymin) |
315 | return lastResult; |
316 | theFound-=2; |
317 | } |
318 | |
319 | if (discrX>1 && (!theBox.IsOpenXmin() || !theBox.IsOpenXmax())) { |
320 | const TColStd_Array1OfListOfInteger & tabList=axisX->Array1(); |
321 | firstcase=(Standard_Integer ) Max(1.0, (xmin-Xmin)/deltaX); |
322 | lastcase=(Standard_Integer ) Min((Standard_Real)discrX, ((xmax-Xmin)/deltaX)+1); |
323 | for (lacase=firstcase; lacase<=lastcase; lacase++) { |
324 | TColStd_ListIteratorOfListOfInteger theList(tabList(lacase)); |
325 | for (; theList.More(); theList.Next()) { |
326 | if (Crible.IsBound(theList.Value())) { |
327 | if (Crible(theList.Value())==theFound) { |
328 | if (!taBox.Value(theList.Value()).IsOut(theBox)){ |
329 | lastResult.Append(theList.Value()); |
330 | Crible(theList.Value())=0; |
331 | } |
332 | } |
333 | } |
334 | } |
335 | } |
336 | return lastResult; |
337 | } |
338 | |
339 | else { |
340 | if (xmin > Xmin+deltaX || xmax < Xmin) return lastResult; |
341 | else if (discrY==1) |
342 | { |
343 | lacase=1; |
344 | for(Standard_Integer i=taBox.Lower();i<=taBox.Upper();i++) |
345 | { |
346 | lastResult.Append(i); |
347 | } |
348 | } |
349 | else{ |
350 | |
351 | TColStd_DataMapIteratorOfDataMapOfIntegerInteger itDM(Crible); |
352 | for (; itDM.More(); itDM.Next()) { |
353 | if (itDM.Value()==theFound) { |
354 | if (taBox.Lower()<=itDM.Key() && itDM.Key()<=taBox.Upper()) { |
355 | if (!taBox(itDM.Key()).IsOut(theBox)) |
356 | lastResult.Append(itDM.Key()); |
357 | } |
358 | else { |
359 | lastResult.Append(itDM.Key()); |
360 | } |
361 | } |
362 | } |
363 | } |
364 | } |
365 | return lastResult; |
366 | } |
367 | |
368 | |
369 | //======================================================================= |
370 | //function : Dump |
371 | //purpose : |
372 | //======================================================================= |
373 | |
374 | void Bnd_BoundSortBox2d::Dump() const |
375 | { |
376 | Standard_Integer lacase; |
377 | |
378 | cout << "axis X : " << discrX << " intervalles de " << deltaX << endl; |
379 | if (discrX>1) { |
380 | const TColStd_Array1OfListOfInteger & tabList=axisX->Array1(); |
381 | for (lacase=1; lacase<=discrX; lacase++) { |
382 | cout << " X " << lacase << " : " ; |
383 | TColStd_ListIteratorOfListOfInteger theList(tabList(lacase)); |
384 | for (; theList.More(); theList.Next()) { |
385 | cout << theList.Value() << " "; |
386 | } |
387 | cout << "\n"; |
388 | } |
389 | } |
390 | |
391 | cout << "axis Y : " << discrY << " intervalles de " << deltaY << endl; |
392 | if (discrY>1) { |
393 | const TColStd_Array1OfListOfInteger & tabList=axisY->Array1(); |
394 | for (lacase=1; lacase<=discrY; lacase++) { |
395 | cout << " Y " << lacase << " : " ; |
396 | TColStd_ListIteratorOfListOfInteger theList(tabList(lacase)); |
397 | for (; theList.More(); theList.Next()) { |
398 | cout << theList.Value() << " "; |
399 | } |
400 | cout << "\n"; |
401 | } |
402 | } |
403 | } |