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