0023132: Suspicious code snippets
[occt.git] / src / TopOpeBRep / TopOpeBRep_ShapeIntersector2d.cxx
1 // Created on: 1993-05-07
2 // Created by: Jean Yves LEBEY
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <TopOpeBRep_ShapeIntersector2d.ixx>
23
24 #include <Standard_ProgramError.hxx>
25 #include <Standard_NotImplemented.hxx>
26 #include <TopAbs.hxx>
27 #include <Bnd_Box.hxx>
28 #include <TopOpeBRepTool_box.hxx>
29
30 #ifdef DEB
31 extern Standard_Boolean TopOpeBRep_GettraceSI(); 
32 extern Standard_Boolean TopOpeBRep_GetcontextFFOR();
33 #endif
34
35 //=======================================================================
36 //function : TopOpeBRep_ShapeIntersector2d
37 //purpose  : 
38 //=======================================================================
39
40 TopOpeBRep_ShapeIntersector2d::TopOpeBRep_ShapeIntersector2d()
41 {
42   Reset();
43   myHBoxTool = FBOX_GetHBoxTool();
44   myFaceScanner.ChangeBoxSort().SetHBoxTool(myHBoxTool);
45   myEdgeScanner.ChangeBoxSort().SetHBoxTool(myHBoxTool);
46 }
47
48 //=======================================================================
49 //function : Reset
50 //purpose  : 
51 //=======================================================================
52
53 void TopOpeBRep_ShapeIntersector2d::Reset()
54 {
55   myIntersectionDone = Standard_False;
56   
57   myFFDone = Standard_False;
58   myEEFFDone = Standard_False;
59   
60   myFFInit = Standard_False;
61   myEEFFInit = Standard_False;
62 }
63
64 //=======================================================================
65 //function : Init
66 //purpose  : 
67 //=======================================================================
68
69 void TopOpeBRep_ShapeIntersector2d::Init
70 (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
71 {
72   Reset();
73   myShape1 = S1;
74   myShape2 = S2;
75   myHBoxTool->Clear();
76 }
77
78 //=======================================================================
79 //function : SetIntersectionDone
80 //purpose  : 
81 //=======================================================================
82
83 void TopOpeBRep_ShapeIntersector2d::SetIntersectionDone()
84 {
85   myIntersectionDone = (myFFDone || 
86                         myEEFFDone);
87 }
88
89
90 //=======================================================================
91 //function : CurrentGeomShape
92 //purpose  : 
93 //=======================================================================
94
95 const TopoDS_Shape& TopOpeBRep_ShapeIntersector2d::CurrentGeomShape
96 (const Standard_Integer Index) const
97 {
98   if ( myIntersectionDone ) {
99     if      (myFFDone) {
100       if      ( Index == 1 ) return myFaceScanner.Current();
101       else if ( Index == 2 ) return myFaceExplorer.Current();
102     }
103     else if (myEEFFDone) {
104       if      ( Index == 1 ) return myEdgeScanner.Current();
105       else if ( Index == 2 ) return myEdgeExplorer.Current();
106     }
107   }
108   
109   Standard_ProgramError::Raise("CurrentGeomShape : no intersection 2d");
110   TopoDS_Shape* bid = new TopoDS_Shape();
111   return *bid;
112 }
113
114
115 //=======================================================================
116 //function : InitIntersection
117 //purpose  : 
118 //=======================================================================
119
120 void TopOpeBRep_ShapeIntersector2d::InitIntersection
121 (const TopoDS_Shape& S1, const TopoDS_Shape& S2)
122 {
123   Init(S1,S2);
124   InitFFIntersection();
125 }
126
127
128 //=======================================================================
129 //function : MoreIntersection
130 //purpose  : 
131 //=======================================================================
132
133 Standard_Boolean TopOpeBRep_ShapeIntersector2d::MoreIntersection() const
134 {
135   Standard_Boolean res = myIntersectionDone;
136   
137 #ifdef DEB
138   if (TopOpeBRep_GettraceSI() && res) {
139     if      ( myFFDone )   cout<<"FF : ";
140     else if ( myEEFFDone ) cout<<"    EE : ";
141     DumpCurrent(1);
142     DumpCurrent(2);
143   }    
144 #endif
145   
146   return res;
147 }
148
149
150 //=======================================================================
151 //function : DumpCurrent
152 //purpose  : 
153 //=======================================================================
154
155 void TopOpeBRep_ShapeIntersector2d::DumpCurrent(const Standard_Integer K) const
156 {
157 #ifdef DEB
158   if      ( myFFDone ) {
159     if      ( K == 1 ) myFaceScanner.DumpCurrent(cout);
160     else if ( K == 2 ) myFaceExplorer.DumpCurrent(cout);
161   }
162   else if ( myEEFFDone ) {
163     if      ( K == 1 ) myEdgeScanner.DumpCurrent(cout);
164     else if ( K == 2 ) myEdgeExplorer.DumpCurrent(cout);
165   }
166 #endif
167 }
168
169 //=======================================================================
170 //function : Index
171 //purpose  : 
172 //=======================================================================
173
174 Standard_Integer TopOpeBRep_ShapeIntersector2d::Index
175 (const Standard_Integer K)const
176 {
177   Standard_Integer i = 0;
178 #ifdef DEB
179   if      ( myFFDone ) {
180     if      ( K == 1 ) i = myFaceScanner.Index();
181     else if ( K == 2 ) i = myFaceExplorer.Index();
182   }
183   else if ( myEEFFDone ) {
184     if      ( K == 1 ) i = myEdgeScanner.Index();
185     else if ( K == 2 ) i = myEdgeExplorer.Index();
186   }
187 #endif
188   return i;
189 }
190
191
192 //=======================================================================
193 //function : NextIntersection
194 //purpose  : 
195 //=======================================================================
196
197 void TopOpeBRep_ShapeIntersector2d::NextIntersection()
198 {
199   myIntersectionDone = Standard_False;
200   
201   if (myFFDone) {
202     // precedant etat du More() : 2 faces
203     myFFDone = Standard_False;
204     InitEEFFIntersection();
205     FindEEFFIntersection();
206     if ( !myIntersectionDone ) {
207       NextFFCouple();
208       FindFFIntersection();
209     }
210   }
211   else if ( myEEFFDone ) {
212     NextEEFFCouple();
213     FindEEFFIntersection();
214     if ( !myIntersectionDone ) {
215       NextFFCouple();
216       FindFFIntersection();
217     }
218   }
219   
220   if ( !myIntersectionDone ) {
221     InitFFIntersection();
222   }
223 }
224
225
226 // ========
227 // FFFFFFFF
228 // ========
229
230
231 //=======================================================================
232 //function : InitFFIntersection
233 //purpose  : 
234 //=======================================================================
235
236 void TopOpeBRep_ShapeIntersector2d::InitFFIntersection()
237 {
238   if ( !myFFInit) { 
239     TopAbs_ShapeEnum tscann = TopAbs_FACE;
240     TopAbs_ShapeEnum texplo = TopAbs_FACE;
241     myFaceScanner.Clear();
242     myFaceScanner.AddBoxesMakeCOB(myShape1,tscann);
243     myFaceExplorer.Init(myShape2,texplo);
244     myFaceScanner.Init(myFaceExplorer);
245     FindFFIntersection();
246   }
247   myFFInit = Standard_True;
248 }
249
250
251 //=======================================================================
252 //function : FindFFIntersection
253 //purpose  : 
254 //=======================================================================
255
256 void TopOpeBRep_ShapeIntersector2d::FindFFIntersection()
257 {
258   myFFDone = Standard_False;
259 //  myFFSameDomain = Standard_False;
260   
261   if ( MoreFFCouple() ) {
262     
263     // The two candidate intersecting GeomShapes GS1,GS2 and their types t1,t2
264     const TopoDS_Shape& GS1 = myFaceScanner.Current();
265     const TopoDS_Shape& GS2 = myFaceExplorer.Current();
266     
267 #ifdef DEB
268     if (TopOpeBRep_GettraceSI()) {
269       cout<<"?? FF : ";
270       myFaceScanner.DumpCurrent(cout); 
271       myFaceExplorer.DumpCurrent(cout);
272       cout<<endl;
273     }    
274 #endif
275
276     const TopOpeBRepTool_BoxSort& BS = myFaceScanner.BoxSort();
277     BS.Box(GS1);
278     BS.Box(GS2);
279     myFFDone = Standard_True;
280   }
281   
282   SetIntersectionDone();
283 }
284
285
286 //=======================================================================
287 //function : MoreFFCouple
288 //purpose  : 
289 //=======================================================================
290
291 Standard_Boolean TopOpeBRep_ShapeIntersector2d::MoreFFCouple() const
292 {
293   Standard_Boolean more1 = myFaceScanner.More();
294   Standard_Boolean more2 = myFaceExplorer.More();
295   return (more1 && more2);
296 }
297
298
299 //=======================================================================
300 //function : NextFFCouple
301 //purpose  : 
302 //=======================================================================
303
304 void TopOpeBRep_ShapeIntersector2d::NextFFCouple()
305 {
306   myFaceScanner.Next();
307   Standard_Boolean b1,b2;
308   
309   b1 = (!myFaceScanner.More());
310   b2 = (myFaceExplorer.More());
311   while ( b1 && b2 ) {
312     myFaceExplorer.Next();
313     myFaceScanner.Init(myFaceExplorer);
314     b1 = (!myFaceScanner.More());
315     b2 = (myFaceExplorer.More());
316   }
317
318 }
319
320
321 // ========
322 // EEFFEEFF
323 // ========
324
325
326 //=======================================================================
327 //function : InitEEFFIntersection
328 //purpose  : 
329 //=======================================================================
330
331 void TopOpeBRep_ShapeIntersector2d::InitEEFFIntersection()
332 {
333   // prepare exploration of the edges of the two current SameDomain faces 
334   TopoDS_Shape face1 = myFaceScanner.Current(); // -26-08-96
335   TopoDS_Shape face2 = myFaceExplorer.Current(); // -26-08-96
336   
337 #ifdef DEB
338   if (TopOpeBRep_GetcontextFFOR()) {
339     face1.Orientation(TopAbs_FORWARD); //-05/07
340     face2.Orientation(TopAbs_FORWARD); //-05/07
341     cout<<"ctx : InitEEFFIntersection : faces FORWARD"<<endl;
342   }
343 #endif
344   
345   myEEIntersector.SetFaces(face1,face2);
346   
347   TopAbs_ShapeEnum tscann = TopAbs_EDGE;
348   TopAbs_ShapeEnum texplo = TopAbs_EDGE;
349   myEdgeScanner.Clear();
350   myEdgeScanner.AddBoxesMakeCOB(face1,tscann);
351   myEdgeExplorer.Init(face2,texplo);
352   myEdgeScanner.Init(myEdgeExplorer);
353   
354   myEEFFInit = Standard_True;
355 }
356
357
358 //=======================================================================
359 //function : FindEEFFIntersection
360 //purpose  : 
361 //=======================================================================
362
363 void TopOpeBRep_ShapeIntersector2d::FindEEFFIntersection()
364 {
365   myEEFFDone = Standard_False;
366   while ( MoreEEFFCouple() ) {
367     const TopoDS_Shape& GS1 = myEdgeScanner.Current();
368     const TopoDS_Shape& GS2 = myEdgeExplorer.Current();
369     myEEIntersector.Perform(GS1,GS2);
370     
371 #ifdef DEB
372     if (TopOpeBRep_GettraceSI() && myEEIntersector.IsEmpty()) {
373       cout<<"    EE : ";
374       myEdgeScanner.DumpCurrent(cout);
375       myEdgeExplorer.DumpCurrent(cout);
376       cout<<"(EE of FF SameDomain)";
377       cout<<" : EMPTY INTERSECTION";
378       cout<<endl;
379     }    
380 #endif
381     
382     myEEFFDone = ! (myEEIntersector.IsEmpty());
383     if (myEEFFDone) break;
384     else NextEEFFCouple();
385   }
386   SetIntersectionDone();
387 }
388
389
390 //=======================================================================
391 //function : MoreEEFFCouple
392 //purpose  : 
393 //=======================================================================
394
395 Standard_Boolean TopOpeBRep_ShapeIntersector2d::MoreEEFFCouple() const
396 {
397   Standard_Boolean more1 = myEdgeScanner.More();
398   Standard_Boolean more2 = myEdgeExplorer.More();
399   return (more1 && more2);
400 }
401
402
403 //=======================================================================
404 //function : NextEEFFCouple
405 //purpose  : 
406 //=======================================================================
407
408 void TopOpeBRep_ShapeIntersector2d::NextEEFFCouple()
409 {
410   myEdgeScanner.Next();
411   while ( ! myEdgeScanner.More() && myEdgeExplorer.More() ) {
412     myEdgeExplorer.Next();
413     myEdgeScanner.Init(myEdgeExplorer);
414   }
415 }
416
417
418 //=======================================================================
419 //function : Shape
420 //purpose  : 
421 //=======================================================================
422
423 const TopoDS_Shape& TopOpeBRep_ShapeIntersector2d::Shape
424 ( const Standard_Integer Index )const
425 {
426   if      ( Index == 1 ) return myShape1;
427   else if ( Index == 2 ) return myShape2;
428   
429   Standard_ProgramError::Raise("ShapeIntersector : no shape");
430   TopoDS_Shape* bid = new TopoDS_Shape();
431   return *bid;
432 }
433
434 //=======================================================================
435 //function : ChangeEdgesIntersector
436 //purpose  : 
437 //=======================================================================
438
439 TopOpeBRep_EdgesIntersector& 
440 TopOpeBRep_ShapeIntersector2d::ChangeEdgesIntersector()
441 { return myEEIntersector; }
442