0026515: Exponential memory usage problems with BOPDS_IndexedMapOfPaveBlock and NColl...
[occt.git] / src / IntTools / IntTools_Context.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <Bnd_Box.hxx>
17 #include <BRep_Tool.hxx>
18 #include <BRepAdaptor_Surface.hxx>
19 #include <BRepBndLib.hxx>
20 #include <BRepClass3d_SolidClassifier.hxx>
21 #include <Extrema_LocateExtPC.hxx>
22 #include <Geom2d_Curve.hxx>
23 #include <Geom2d_TrimmedCurve.hxx>
24 #include <Geom2dHatch_Hatcher.hxx>
25 #include <Geom2dHatch_Intersector.hxx>
26 #include <Geom_BoundedCurve.hxx>
27 #include <Geom_Curve.hxx>
28 #include <GeomAdaptor_Curve.hxx>
29 #include <GeomAPI_ProjectPointOnCurve.hxx>
30 #include <GeomAPI_ProjectPointOnSurf.hxx>
31 #include <gp_Pnt.hxx>
32 #include <gp_Pnt2d.hxx>
33 #include <IntTools_Context.hxx>
34 #include <IntTools_Curve.hxx>
35 #include <IntTools_FClass2d.hxx>
36 #include <IntTools_SurfaceRangeLocalizeData.hxx>
37 #include <IntTools_Tools.hxx>
38 #include <Precision.hxx>
39 #include <Standard_Type.hxx>
40 #include <TopAbs_State.hxx>
41 #include <TopExp_Explorer.hxx>
42 #include <TopoDS.hxx>
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Face.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Solid.hxx>
47 #include <TopoDS_Vertex.hxx>
48
49 // 
50 //=======================================================================
51 //function : 
52 //purpose  : 
53 //=======================================================================
54 IntTools_Context::IntTools_Context()
55 :
56   myAllocator(NCollection_BaseAllocator::CommonBaseAllocator()),
57   myFClass2dMap(100, myAllocator),
58   myProjPSMap(100, myAllocator),
59   myProjPCMap(100, myAllocator),
60   mySClassMap(100, myAllocator),
61   myProjPTMap(100, myAllocator),
62   myHatcherMap(100, myAllocator),
63   myProjSDataMap(100, myAllocator),
64   myBndBoxDataMap(100, myAllocator),
65   myCreateFlag(0),
66   myPOnSTolerance(1.e-12)
67 {
68 }
69 //=======================================================================
70 //function : 
71 //purpose  : 
72 //=======================================================================
73 IntTools_Context::IntTools_Context
74   (const Handle(NCollection_BaseAllocator)& theAllocator)
75 :
76   myAllocator(theAllocator),
77   myFClass2dMap(100, myAllocator),
78   myProjPSMap(100, myAllocator),
79   myProjPCMap(100, myAllocator),
80   mySClassMap(100, myAllocator),
81   myProjPTMap(100, myAllocator),
82   myHatcherMap(100, myAllocator),
83   myProjSDataMap(100, myAllocator),
84   myBndBoxDataMap(100, myAllocator),
85   myCreateFlag(1),
86   myPOnSTolerance(1.e-12)
87 {
88 }
89 //=======================================================================
90 //function : ~
91 //purpose  : 
92 //=======================================================================
93 IntTools_Context::~IntTools_Context()
94 {
95   Standard_Address anAdr;
96   BOPCol_DataMapIteratorOfDataMapOfShapeAddress aIt;
97   BOPCol_DataMapIteratorOfDataMapOfTransientAddress aIt1;
98   //
99   IntTools_FClass2d* pFClass2d;
100   //
101   aIt.Initialize(myFClass2dMap);
102   for (; aIt.More(); aIt.Next()) {
103     anAdr=aIt.Value();
104     pFClass2d=(IntTools_FClass2d*)anAdr;
105     (*pFClass2d).~IntTools_FClass2d();
106     myAllocator->Free(anAdr); 
107   }
108   myFClass2dMap.Clear();
109   //
110   clearCachedPOnSProjectors();
111   //
112   GeomAPI_ProjectPointOnCurve* pProjPC;
113   aIt.Initialize(myProjPCMap);
114   for (; aIt.More(); aIt.Next()) {
115     anAdr=aIt.Value();
116     pProjPC=(GeomAPI_ProjectPointOnCurve*)anAdr;
117     (*pProjPC).~GeomAPI_ProjectPointOnCurve();
118     myAllocator->Free(anAdr); 
119   }
120   myProjPCMap.Clear();
121   //
122   //
123   BRepClass3d_SolidClassifier* pSC;
124   aIt.Initialize(mySClassMap);
125   for (; aIt.More(); aIt.Next()) {
126     anAdr=aIt.Value();
127     pSC=(BRepClass3d_SolidClassifier*)anAdr;
128     (*pSC).~BRepClass3d_SolidClassifier();
129     myAllocator->Free(anAdr); 
130   }
131   mySClassMap.Clear();
132   //
133   GeomAPI_ProjectPointOnCurve* pProjPT;
134   aIt1.Initialize(myProjPTMap);
135   for (; aIt1.More(); aIt1.Next()) {
136     anAdr=aIt1.Value();
137     pProjPT=(GeomAPI_ProjectPointOnCurve*)anAdr;
138     (*pProjPT).~GeomAPI_ProjectPointOnCurve();
139     myAllocator->Free(anAdr); 
140   }
141   myProjPTMap.Clear();
142   //
143   Geom2dHatch_Hatcher* pHatcher;
144   aIt.Initialize(myHatcherMap);
145   for (; aIt.More(); aIt.Next()) {
146     anAdr=aIt.Value();
147     pHatcher=(Geom2dHatch_Hatcher*)anAdr;
148     (*pHatcher).~Geom2dHatch_Hatcher();
149     myAllocator->Free(anAdr);
150   }
151   myHatcherMap.Clear();
152   //
153   IntTools_SurfaceRangeLocalizeData* pSData = NULL;
154   aIt.Initialize(myProjSDataMap);
155   for (; aIt.More(); aIt.Next()) {
156     anAdr=aIt.Value();
157     pSData = (IntTools_SurfaceRangeLocalizeData*)anAdr;
158     (*pSData).~IntTools_SurfaceRangeLocalizeData();
159     myAllocator->Free(anAdr);
160   }
161   myProjSDataMap.Clear();
162   //
163   Bnd_Box* pBox;
164   aIt.Initialize(myBndBoxDataMap);
165   for (; aIt.More(); aIt.Next()) {
166     anAdr=aIt.Value();
167     pBox=(Bnd_Box*)anAdr;
168     (*pBox).~Bnd_Box();
169     myAllocator->Free(anAdr); 
170   }
171   myBndBoxDataMap.Clear();
172 }
173 //=======================================================================
174 //function : BndBox
175 //purpose  : 
176 //=======================================================================
177 Bnd_Box& IntTools_Context::BndBox(const TopoDS_Shape& aS)
178 {
179   Standard_Address anAdr;
180   Bnd_Box* pBox;
181   //
182   if (!myBndBoxDataMap.IsBound(aS)) {
183     //
184     pBox=(Bnd_Box*)myAllocator->Allocate(sizeof(Bnd_Box));
185     new (pBox) Bnd_Box();
186     //
187     Bnd_Box &aBox=*pBox;
188     BRepBndLib::Add(aS, aBox);
189     //
190     anAdr=(Standard_Address)pBox;
191     myBndBoxDataMap.Bind(aS, anAdr);
192   }
193   else {
194     anAdr=myBndBoxDataMap.Find(aS);
195     pBox=(Bnd_Box*)anAdr;
196   }
197   return *pBox;
198 }
199 //=======================================================================
200 //function : IsInfiniteFace
201 //purpose  : 
202 //=======================================================================
203 Standard_Boolean IntTools_Context::IsInfiniteFace
204   (const TopoDS_Face& aFace)
205 {
206   Standard_Boolean bRet;
207   Standard_Integer i;
208   Standard_Real aX[6];
209   //
210   bRet=Standard_False;
211   //
212   if (!BRep_Tool::NaturalRestriction(aFace)) {
213     return bRet; 
214   }
215   //
216   Bnd_Box& aBox=BndBox(aFace);
217   //
218   aBox.Get(aX[0], aX[1], aX[2], aX[3], aX[4], aX[5]);
219   //
220   for (i=0; (i<6) && (!bRet); ++i) {
221     bRet=Precision::IsInfinite(aX[i]);
222   }
223   //
224   return bRet; 
225 }
226 //=======================================================================
227 //function : FClass2d
228 //purpose  : 
229 //=======================================================================
230 IntTools_FClass2d& IntTools_Context::FClass2d(const TopoDS_Face& aF)
231 {
232   Standard_Address anAdr;
233   IntTools_FClass2d* pFClass2d;
234   //
235   if (!myFClass2dMap.IsBound(aF)) {
236     Standard_Real aTolF;
237     TopoDS_Face aFF;
238     //
239     aFF=aF;
240     aFF.Orientation(TopAbs_FORWARD);
241     aTolF=BRep_Tool::Tolerance(aFF);
242     //
243     pFClass2d=(IntTools_FClass2d*)myAllocator->Allocate(sizeof(IntTools_FClass2d));
244     new (pFClass2d) IntTools_FClass2d(aFF, aTolF);
245     //
246     anAdr=(Standard_Address)pFClass2d;
247     myFClass2dMap.Bind(aFF, anAdr);
248   }
249   else {
250     anAdr=myFClass2dMap.Find(aF);
251     pFClass2d=(IntTools_FClass2d*)anAdr;
252   }
253   return *pFClass2d;
254 }
255 //=======================================================================
256 //function : ProjPS
257 //purpose  : 
258 //=======================================================================
259 GeomAPI_ProjectPointOnSurf& IntTools_Context::ProjPS(const TopoDS_Face& aF)
260 {
261   Standard_Address anAdr;
262   GeomAPI_ProjectPointOnSurf* pProjPS;
263  
264   if (!myProjPSMap.IsBound(aF)) {
265     Standard_Real Umin, Usup, Vmin, Vsup;
266     BRepAdaptor_Surface aBAS;
267     //
268     const Handle(Geom_Surface)& aS=BRep_Tool::Surface(aF);
269     aBAS.Initialize (aF, Standard_True);
270     //
271     Umin=aBAS.FirstUParameter();
272     Usup=aBAS.LastUParameter ();
273     Vmin=aBAS.FirstVParameter();
274     Vsup=aBAS.LastVParameter ();
275     //
276     pProjPS=(GeomAPI_ProjectPointOnSurf*)myAllocator->Allocate(sizeof(GeomAPI_ProjectPointOnSurf));
277     new (pProjPS) GeomAPI_ProjectPointOnSurf();
278     pProjPS->Init(aS ,Umin, Usup, Vmin, Vsup, myPOnSTolerance/*, Extrema_ExtAlgo_Tree*/);
279     Extrema_ExtPS& anExtAlgo = const_cast<Extrema_ExtPS&>(pProjPS->Extrema());
280     anExtAlgo.SetFlag(Extrema_ExtFlag_MIN);
281     //
282     anAdr=(Standard_Address)pProjPS;
283     myProjPSMap.Bind(aF, anAdr);
284   }
285   
286   else {
287     anAdr=myProjPSMap.Find(aF);
288     pProjPS=(GeomAPI_ProjectPointOnSurf*)anAdr;
289   }
290   return *pProjPS;
291 }
292 //=======================================================================
293 //function : ProjPC
294 //purpose  : 
295 //=======================================================================
296 GeomAPI_ProjectPointOnCurve& IntTools_Context::ProjPC(const TopoDS_Edge& aE)
297 {
298   Standard_Address anAdr;
299   GeomAPI_ProjectPointOnCurve* pProjPC;
300  
301   if (!myProjPCMap.IsBound(aE)) {
302     Standard_Real f, l;
303     //
304     Handle(Geom_Curve)aC3D=BRep_Tool::Curve (aE, f, l);
305     //
306     pProjPC=(GeomAPI_ProjectPointOnCurve*)myAllocator->Allocate(sizeof(GeomAPI_ProjectPointOnCurve));
307     new (pProjPC) GeomAPI_ProjectPointOnCurve();
308     pProjPC->Init(aC3D, f, l);
309     //
310     anAdr=(Standard_Address)pProjPC;
311     myProjPCMap.Bind(aE, anAdr);
312   }
313   
314   else {
315     anAdr=myProjPCMap.Find(aE);
316     pProjPC=(GeomAPI_ProjectPointOnCurve*)anAdr;
317   }
318   return *pProjPC;
319 }
320
321 //=======================================================================
322 //function : ProjPT
323 //purpose  : 
324 //=======================================================================
325 GeomAPI_ProjectPointOnCurve& IntTools_Context::ProjPT
326   (const Handle(Geom_Curve)& aC3D)
327
328 {
329   Standard_Address anAdr;
330   GeomAPI_ProjectPointOnCurve* pProjPT;
331  
332   if (!myProjPTMap.IsBound(aC3D)) {
333     Standard_Real f, l;
334     f=aC3D->FirstParameter();
335     l=aC3D->LastParameter();
336     //
337     pProjPT=(GeomAPI_ProjectPointOnCurve*)myAllocator->Allocate(sizeof(GeomAPI_ProjectPointOnCurve));
338     new (pProjPT) GeomAPI_ProjectPointOnCurve();
339     pProjPT->Init(aC3D, f, l);
340     //
341     anAdr=(Standard_Address)pProjPT;
342     myProjPTMap.Bind(aC3D, anAdr);
343   }
344   
345   else {
346     anAdr=myProjPTMap.Find(aC3D);
347     pProjPT=(GeomAPI_ProjectPointOnCurve*)anAdr;
348   }
349   return *pProjPT;
350 }
351 //=======================================================================
352 //function : SolidClassifier
353 //purpose  : 
354 //=======================================================================
355 BRepClass3d_SolidClassifier& IntTools_Context::SolidClassifier
356   (const TopoDS_Solid& aSolid)
357 {
358   Standard_Address anAdr;
359   BRepClass3d_SolidClassifier* pSC;
360  
361   if (!mySClassMap.IsBound(aSolid)) {
362     //
363     pSC=(BRepClass3d_SolidClassifier*)myAllocator->Allocate(sizeof(BRepClass3d_SolidClassifier));
364     new (pSC) BRepClass3d_SolidClassifier(aSolid);
365     //
366     anAdr=(Standard_Address)pSC;
367     mySClassMap.Bind(aSolid, anAdr);
368   }
369   
370   else {
371     anAdr=mySClassMap.Find(aSolid);
372     pSC =(BRepClass3d_SolidClassifier*)anAdr;
373   }
374   return *pSC;
375 }
376
377 //=======================================================================
378 //function : Hatcher
379 //purpose  : 
380 //=======================================================================
381 Geom2dHatch_Hatcher& IntTools_Context::Hatcher(const TopoDS_Face& aF)
382 {
383   Standard_Address anAdr;
384   Geom2dHatch_Hatcher* pHatcher;
385   //
386   if (!myHatcherMap.IsBound(aF)) {
387     Standard_Real aTolArcIntr, aTolTangfIntr, aTolHatch2D, aTolHatch3D;
388     Standard_Real aU1, aU2, aEpsT;
389     TopAbs_Orientation aOrE;
390     Handle(Geom_Surface) aS;
391     Handle(Geom2d_Curve) aC2D;
392     Handle(Geom2d_TrimmedCurve) aCT2D;
393     TopoDS_Face aFF;
394     TopExp_Explorer aExp;
395     //
396     aTolHatch2D=1.e-8;
397     aTolHatch3D=1.e-8;
398     aTolArcIntr=1.e-10;
399     aTolTangfIntr=1.e-10;
400     aEpsT=Precision::PConfusion();
401     //
402     Geom2dHatch_Intersector aIntr(aTolArcIntr, aTolTangfIntr);
403     pHatcher=new Geom2dHatch_Hatcher(aIntr,
404                                      aTolHatch2D, aTolHatch3D,
405                                      Standard_True, Standard_False);
406     
407     //
408     aFF=aF;
409     aFF.Orientation(TopAbs_FORWARD);
410     aS=BRep_Tool::Surface(aFF);
411
412     aExp.Init (aFF, TopAbs_EDGE);
413     for (; aExp.More() ; aExp.Next()) {
414       const TopoDS_Edge& aE=*((TopoDS_Edge*)&aExp.Current());
415       aOrE=aE.Orientation();
416       //
417       aC2D=BRep_Tool::CurveOnSurface (aE, aFF, aU1, aU2);
418       if (aC2D.IsNull() ) {
419         continue;
420       }
421       if (fabs(aU1-aU2) < aEpsT) {
422         continue;
423       }
424       //
425       aCT2D=new Geom2d_TrimmedCurve(aC2D, aU1, aU2);
426       Geom2dAdaptor_Curve aGAC (aCT2D);
427       pHatcher->AddElement(aGAC, aOrE);
428     }// for (; aExp.More() ; aExp.Next()) {
429     //
430     anAdr=(Standard_Address)pHatcher;
431     myHatcherMap.Bind(aFF, anAdr);
432   }//if (!myHatcherMap.IsBound(aF)) {
433   //
434   else {
435     anAdr=myHatcherMap.Find(aF);
436     pHatcher=(Geom2dHatch_Hatcher*)anAdr;
437   }
438
439   return *pHatcher;
440 }
441
442 //=======================================================================
443 //function : SurfaceData
444 //purpose  : 
445 //=======================================================================
446 IntTools_SurfaceRangeLocalizeData& IntTools_Context::SurfaceData
447   (const TopoDS_Face& aF) 
448 {
449   Standard_Address anAdr;
450   IntTools_SurfaceRangeLocalizeData* pSData;
451   //
452   if (!myProjSDataMap.IsBound(aF)) {
453     pSData=new IntTools_SurfaceRangeLocalizeData
454       (3, 
455        3, 
456        10. * Precision::PConfusion(), 
457        10. * Precision::PConfusion());
458     //
459     anAdr=(Standard_Address)pSData;
460     myProjSDataMap.Bind(aF, anAdr);
461   }
462   
463   else {
464     anAdr=myProjSDataMap.Find(aF);
465     pSData=(IntTools_SurfaceRangeLocalizeData*)anAdr;
466   }
467   return *pSData;
468
469 }
470
471 //=======================================================================
472 //function : ComputePE
473 //purpose  : 
474 //=======================================================================
475 Standard_Integer IntTools_Context::ComputePE
476   (const gp_Pnt& aP1,
477    const Standard_Real aTolP1,
478    const TopoDS_Edge& aE2,
479    Standard_Real& aT)
480 {
481   if (!BRep_Tool::IsGeometric(aE2)) { 
482     return -2;
483   }
484   Standard_Real aDist, aTolE2, aTolSum;
485   Standard_Integer aNbProj;
486   //
487   GeomAPI_ProjectPointOnCurve& aProjector=ProjPC(aE2);
488   aProjector.Perform(aP1);
489
490   aNbProj=aProjector.NbPoints();
491   if (!aNbProj) {
492     return -3;
493   }
494   //
495   aDist=aProjector.LowerDistance();
496   //
497   aTolE2=BRep_Tool::Tolerance(aE2);
498   aTolSum=aTolP1+aTolE2;
499   //
500   aT=aProjector.LowerDistanceParameter();
501   if (aDist > aTolSum) {
502     return -4;
503   }
504   return 0;
505 }
506 //=======================================================================
507 //function : ComputeVE
508 //purpose  : 
509 //=======================================================================
510 Standard_Integer IntTools_Context::ComputeVE
511   (const TopoDS_Vertex& aV1, 
512    const TopoDS_Edge&   aE2,
513    Standard_Real& aT)
514 {
515   if (BRep_Tool::Degenerated(aE2)) {
516     return -1;
517   }
518   if (!BRep_Tool::IsGeometric(aE2)) { 
519     return -2;
520   }
521   Standard_Real aDist, aTolV1, aTolE2, aTolSum;
522   Standard_Integer aNbProj;
523   gp_Pnt aP;
524   //
525   aP=BRep_Tool::Pnt(aV1);
526   //
527   GeomAPI_ProjectPointOnCurve& aProjector=ProjPC(aE2);
528   aProjector.Perform(aP);
529
530   aNbProj=aProjector.NbPoints();
531   if (!aNbProj) {
532     return -3;
533   }
534   //
535   aDist=aProjector.LowerDistance();
536
537   // tolerance of check for coincidence is sum of tolerances of edge and vertex 
538   // extended by additional Precision::Confusion() to allow for interference where
539   // it is very close but not fit to tolerance (see #24108)
540   aTolV1=BRep_Tool::Tolerance(aV1);
541   aTolE2=BRep_Tool::Tolerance(aE2);
542   aTolSum = aTolV1 + aTolE2 + Precision::Confusion();
543   //
544   aT=aProjector.LowerDistanceParameter();
545   if (aDist > aTolSum) {
546     return -4;
547   }
548   return 0;
549 }
550 //=======================================================================
551 //function : ComputeVS
552 //purpose  : 
553 //=======================================================================
554 Standard_Integer IntTools_Context::ComputeVF
555   (const TopoDS_Vertex& aV1, 
556    const TopoDS_Face&   aF2,
557    Standard_Real& U,
558    Standard_Real& V)
559 {
560   Standard_Real aTolV1, aTolF2, aTolSum, aDist;
561   gp_Pnt aP;
562
563   aP=BRep_Tool::Pnt(aV1);
564   //
565   // 1. Check if the point is projectable on the surface
566   GeomAPI_ProjectPointOnSurf& aProjector=ProjPS(aF2);
567   aProjector.Perform(aP);
568   //
569   if (!aProjector.IsDone()) { // the point is not  projectable on the surface
570     return -1;
571   }
572   //
573   // 2. Check the distance between the projection point and 
574   //    the original point
575   aDist=aProjector.LowerDistance();
576
577   aTolV1=BRep_Tool::Tolerance(aV1);
578   aTolF2=BRep_Tool::Tolerance(aF2);
579   aTolSum=aTolV1+aTolF2;
580   if (aDist > aTolSum) {
581     // the distance is too large
582     return -2;
583   }
584   aProjector.LowerDistanceParameters(U, V);
585   //
586   gp_Pnt2d aP2d(U, V);
587   Standard_Boolean pri=IsPointInFace (aF2, aP2d);
588   if (!pri) {//  the point lays on the surface but out of the face 
589     return -3;
590   }
591   return 0;
592 }
593 //=======================================================================
594 //function : StatePointFace
595 //purpose  : 
596 //=======================================================================
597 TopAbs_State IntTools_Context::StatePointFace
598   (const TopoDS_Face& aF,
599    const gp_Pnt2d& aP2d)
600 {
601   TopAbs_State aState;
602   IntTools_FClass2d& aClass2d=FClass2d(aF);
603   aState=aClass2d.Perform(aP2d);
604   return aState;
605 }
606 //=======================================================================
607 //function : IsPointInFace
608 //purpose  : 
609 //=======================================================================
610 Standard_Boolean IntTools_Context::IsPointInFace
611   (const TopoDS_Face& aF,
612    const gp_Pnt2d& aP2d)
613 {
614   TopAbs_State aState=StatePointFace(aF, aP2d);
615   if (aState==TopAbs_OUT || aState==TopAbs_ON) {
616     return Standard_False;
617   }
618   return Standard_True;
619 }
620 //=======================================================================
621 //function : IsPointInFace
622 //purpose  : 
623 //=======================================================================
624 Standard_Boolean IntTools_Context::IsPointInFace
625   (const gp_Pnt& aP,
626    const TopoDS_Face& aF,
627    const Standard_Real aTol) 
628 {
629   Standard_Boolean bIn;
630   Standard_Real aDist;
631   //
632   GeomAPI_ProjectPointOnSurf& aProjector=ProjPS(aF);
633   aProjector.Perform(aP);
634   //
635   bIn = aProjector.IsDone();
636   if (bIn) {
637     aDist = aProjector.LowerDistance();
638     if (aDist < aTol) {
639       Standard_Real U, V;
640       //
641       aProjector.LowerDistanceParameters(U, V);
642       gp_Pnt2d aP2D(U, V);
643       bIn = IsPointInFace(aF, aP2D);
644     }
645   }
646   //
647   return bIn;
648 }
649 //=======================================================================
650 //function : IsPointInOnFace
651 //purpose  : 
652 //=======================================================================
653   Standard_Boolean IntTools_Context::IsPointInOnFace(const TopoDS_Face& aF,
654                                                    const gp_Pnt2d& aP2d)
655
656   TopAbs_State aState=StatePointFace(aF, aP2d);
657   if (aState==TopAbs_OUT) {
658     return Standard_False;
659   }
660   return Standard_True;
661 }
662 //=======================================================================
663 //function : IsValidPointForFace
664 //purpose  : 
665 //=======================================================================
666 Standard_Boolean IntTools_Context::IsValidPointForFace
667   (const gp_Pnt& aP,
668    const TopoDS_Face& aF,
669    const Standard_Real aTol) 
670 {
671   Standard_Boolean bFlag;
672   Standard_Real Umin, U, V;
673
674   GeomAPI_ProjectPointOnSurf& aProjector=ProjPS(aF);
675   aProjector.Perform(aP);
676   
677   bFlag=aProjector.IsDone();
678   if (bFlag) {
679     
680     Umin=aProjector.LowerDistance();
681     //if (Umin > 1.e-3) { // it was 
682     if (Umin > aTol) {
683       return !bFlag; 
684     }
685     //
686     aProjector.LowerDistanceParameters(U, V);
687     gp_Pnt2d aP2D(U, V);
688     bFlag=IsPointInOnFace (aF, aP2D);
689   }
690   return bFlag;
691 }
692 //=======================================================================
693 //function : IsValidPointForFaces
694 //purpose  : 
695 //=======================================================================
696 Standard_Boolean IntTools_Context::IsValidPointForFaces 
697   (const gp_Pnt& aP,
698    const TopoDS_Face& aF1,
699    const TopoDS_Face& aF2,
700    const Standard_Real aTol) 
701 {
702   Standard_Boolean bFlag1, bFlag2;
703
704   bFlag1=IsValidPointForFace(aP, aF1, aTol);
705   if (!bFlag1) {
706     return bFlag1;
707   }
708   bFlag2=IsValidPointForFace(aP, aF2, aTol);  
709   return  bFlag2;
710 }
711 //=======================================================================
712 //function : IsValidBlockForFace
713 //purpose  : 
714 //=======================================================================
715 Standard_Boolean IntTools_Context::IsValidBlockForFace 
716   (const Standard_Real aT1,
717    const Standard_Real aT2,
718    const IntTools_Curve& aC, 
719    const TopoDS_Face& aF,
720    const Standard_Real aTol) 
721 {
722   Standard_Boolean bFlag;
723   Standard_Real aTInterm;
724   gp_Pnt aPInterm;
725
726   aTInterm=IntTools_Tools::IntermediatePoint(aT1, aT2);
727
728   Handle(Geom_Curve) aC3D=aC.Curve();
729   // point 3D
730   aC3D->D0(aTInterm, aPInterm);
731   //
732   bFlag=IsValidPointForFace (aPInterm, aF, aTol);
733   return bFlag;
734 }
735 //=======================================================================
736 //function : IsValidBlockForFaces
737 //purpose  : 
738 //=======================================================================
739 Standard_Boolean IntTools_Context::IsValidBlockForFaces 
740   (const Standard_Real aT1,
741    const Standard_Real aT2,
742    const IntTools_Curve& aC, 
743    const TopoDS_Face& aF1,
744    const TopoDS_Face& aF2,
745    const Standard_Real aTol) 
746 {
747   Standard_Boolean bFlag1, bFlag2;
748   //
749   Handle(Geom2d_Curve) aPC1 = aC.FirstCurve2d();
750   Handle(Geom2d_Curve) aPC2 = aC.SecondCurve2d();
751   if( !aPC1.IsNull() && !aPC2.IsNull() ) {
752     Standard_Real aMidPar = IntTools_Tools::IntermediatePoint(aT1, aT2);
753     gp_Pnt2d aPnt2D;
754
755
756     aPC1->D0(aMidPar, aPnt2D);
757     bFlag1 = IsPointInOnFace(aF1, aPnt2D);
758
759     if( !bFlag1 )
760       return bFlag1;
761
762     aPC2->D0(aMidPar, aPnt2D);
763     bFlag2 = IsPointInOnFace(aF2, aPnt2D);
764     return bFlag2;
765   }
766   //
767
768   bFlag1=IsValidBlockForFace (aT1, aT2, aC, aF1, aTol);
769   if (!bFlag1) {
770     return bFlag1;
771   }
772   bFlag2=IsValidBlockForFace (aT1, aT2, aC, aF2, aTol);
773   return bFlag2;
774 }
775 //=======================================================================
776 //function : IsVertexOnLine
777 //purpose  : 
778 //=======================================================================
779 Standard_Boolean IntTools_Context::IsVertexOnLine 
780   (const TopoDS_Vertex& aV,
781    const IntTools_Curve& aC, 
782    const Standard_Real aTolC,
783    Standard_Real& aT)
784 {
785   Standard_Boolean bRet;
786   Standard_Real aTolV;
787   //
788   aTolV=BRep_Tool::Tolerance(aV);
789   bRet=IntTools_Context::IsVertexOnLine(aV, aTolV, aC, aTolC , aT);
790   //
791   return bRet;
792 }
793 //=======================================================================
794 //function : IsVertexOnLine
795 //purpose  : 
796 //=======================================================================
797 Standard_Boolean IntTools_Context::IsVertexOnLine 
798   (const TopoDS_Vertex& aV,
799    const Standard_Real aTolV,
800    const IntTools_Curve& aC, 
801    const Standard_Real aTolC,
802    Standard_Real& aT)
803 {
804   Standard_Real aFirst, aLast, aDist, aTolSum;
805   Standard_Integer aNbProj;
806   gp_Pnt aPv; 
807   
808   aPv=BRep_Tool::Pnt(aV);
809
810   Handle(Geom_Curve) aC3D=aC.Curve();
811   
812   
813   aTolSum=aTolV+aTolC;
814   //
815   GeomAdaptor_Curve aGAC(aC3D);
816   GeomAbs_CurveType aType=aGAC.GetType();
817   if (aType==GeomAbs_BSplineCurve ||
818       aType==GeomAbs_BezierCurve) {
819     aTolSum=2.*aTolSum;
820     if (aTolSum<1.e-5) {
821       aTolSum=1.e-5;
822     }
823   }
824   else {
825     aTolSum=2.*aTolSum;//xft
826     if(aTolSum < 1.e-6)
827       aTolSum = 1.e-6;
828   }
829   //
830   aFirst=aC3D->FirstParameter();
831   aLast =aC3D->LastParameter();
832   //
833   //Checking extermities first
834   if (!Precision::IsInfinite(aFirst)) {
835     gp_Pnt aPCFirst=aC3D->Value(aFirst);
836     aDist=aPv.Distance(aPCFirst);
837     if (aDist < aTolSum) {
838       aT=aFirst;
839       //
840       if(aDist > aTolV) {
841         Extrema_LocateExtPC anExt(aPv, aGAC, aFirst, 1.e-10);
842         
843         if(anExt.IsDone()) {
844           Extrema_POnCurv aPOncurve = anExt.Point();
845           aT = aPOncurve.Parameter();
846           
847           if((aT > (aLast + aFirst) * 0.5) ||
848              (aPv.Distance(aPOncurve.Value()) > aTolSum) ||
849              (aPCFirst.Distance(aPOncurve.Value()) < Precision::Confusion()))
850             aT = aFirst;
851         }
852       }
853       //
854       return Standard_True;
855     }
856   }
857   //
858   //if (!Precision::IsInfinite(aFirst)) {
859   if (!Precision::IsInfinite(aLast)) {
860     gp_Pnt aPCLast=aC3D->Value(aLast);
861     aDist=aPv.Distance(aPCLast);
862     if (aDist < aTolSum) {
863       aT=aLast;
864       //
865       if(aDist > aTolV) {
866         Extrema_LocateExtPC anExt(aPv, aGAC, aLast, 1.e-10);
867         
868         if(anExt.IsDone()) {
869           Extrema_POnCurv aPOncurve = anExt.Point();
870           aT = aPOncurve.Parameter();
871           
872           if((aT < (aLast + aFirst) * 0.5) ||
873              (aPv.Distance(aPOncurve.Value()) > aTolSum) ||
874              (aPCLast.Distance(aPOncurve.Value()) < Precision::Confusion()))
875             aT = aLast;
876         }
877       }
878       //
879       return Standard_True;
880     }
881   }
882   //
883   GeomAPI_ProjectPointOnCurve& aProjector=ProjPT(aC3D);
884   aProjector.Perform(aPv);
885   
886   aNbProj=aProjector.NbPoints();
887   if (!aNbProj) {
888     Handle(Geom_BoundedCurve) aBC=
889       Handle(Geom_BoundedCurve)::DownCast(aC3D);
890     if (!aBC.IsNull()) {
891       gp_Pnt aPStart=aBC->StartPoint();
892       gp_Pnt aPEnd  =aBC->EndPoint();
893       
894       aDist=aPv.Distance(aPStart);
895       if (aDist < aTolSum) {
896         aT=aFirst;
897         return Standard_True;
898       }
899       
900       aDist=aPv.Distance(aPEnd);
901       if (aDist < aTolSum) {
902         aT=aLast;
903         return Standard_True;
904       }
905     }
906     
907     return Standard_False;
908   }
909   
910   aDist=aProjector.LowerDistance();
911   
912   if (aDist > aTolSum) {
913     return Standard_False;
914   }
915
916   aT=aProjector.LowerDistanceParameter();
917
918   return Standard_True; 
919 }
920 //=======================================================================
921 //function : ProjectPointOnEdge
922 //purpose  : 
923 //=======================================================================
924 Standard_Boolean IntTools_Context::ProjectPointOnEdge
925   (const gp_Pnt& aP,
926    const TopoDS_Edge& anEdge,
927    Standard_Real& aT)
928 {
929   Standard_Integer aNbPoints;
930
931   GeomAPI_ProjectPointOnCurve& aProjector=ProjPC(anEdge);
932   aProjector.Perform(aP);
933
934   aNbPoints=aProjector.NbPoints();
935   if (aNbPoints) {
936     aT=aProjector.LowerDistanceParameter();
937     return Standard_True;
938   }
939   return Standard_False;
940 }
941
942 //=======================================================================
943 //function : SetPOnSProjectionTolerance
944 //purpose  : 
945 //=======================================================================
946 void IntTools_Context::SetPOnSProjectionTolerance(const Standard_Real theValue)
947 {
948   myPOnSTolerance = theValue;
949   clearCachedPOnSProjectors();
950 }
951
952 //=======================================================================
953 //function : clearCachedPOnSProjectors
954 //purpose  : 
955 //=======================================================================
956 void IntTools_Context::clearCachedPOnSProjectors()
957 {
958   GeomAPI_ProjectPointOnSurf* pProjPS;
959   BOPCol_DataMapIteratorOfDataMapOfShapeAddress aIt(myProjPSMap);
960   for (; aIt.More(); aIt.Next()) {
961     Standard_Address anAdr=aIt.Value();
962     pProjPS=(GeomAPI_ProjectPointOnSurf*)anAdr;
963     (*pProjPS).~GeomAPI_ProjectPointOnSurf();
964     myAllocator->Free(anAdr); 
965   }
966   myProjPSMap.Clear();
967 }