1 // File: IntTools_Context.cxx
2 // Created: Wed Apr 3 16:57:54 2002
3 // Author: Peter KURNEV
7 #include <IntTools_Context.ixx>
9 #include <Precision.hxx>
11 #include <Geom_Curve.hxx>
12 #include <Geom_BoundedCurve.hxx>
13 #include <GeomAPI_ProjectPointOnCurve.hxx>
14 #include <GeomAPI_ProjectPointOnSurf.hxx>
15 #include <GeomAdaptor_Curve.hxx>
17 #include <TopAbs_State.hxx>
19 #include <TopExp_Explorer.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepAdaptor_Surface.hxx>
24 #include <IntTools_Tools.hxx>
25 #include <IntTools_FClass2d.hxx>
27 #include <Extrema_LocateExtPC.hxx>
29 #include <Geom2d_Curve.hxx>
31 //=======================================================================
34 //=======================================================================
35 IntTools_Context::IntTools_Context()
38 //=======================================================================
41 //=======================================================================
42 IntTools_Context::~IntTools_Context()
44 Standard_Address anAdr;
45 Standard_Integer i, aNb;
47 IntTools_FClass2d* pFClass2d;
48 aNb=myFClass2dMap.Extent();
49 for (i=1; i<=aNb; ++i) {
50 anAdr=myFClass2dMap(i);
51 pFClass2d=(IntTools_FClass2d*)anAdr;
54 myFClass2dMap.Clear();
56 GeomAPI_ProjectPointOnSurf* pProjPS;
57 aNb=myProjPSMap.Extent();
58 for (i=1; i<=aNb; ++i) {
60 pProjPS=(GeomAPI_ProjectPointOnSurf*)anAdr;
65 GeomAPI_ProjectPointOnCurve* pProjPC;
66 aNb=myProjPCMap.Extent();
67 for (i=1; i<=aNb; ++i) {
69 pProjPC=(GeomAPI_ProjectPointOnCurve*)anAdr;
74 GeomAPI_ProjectPointOnCurve* pProjPT;
75 aNb=myProjPTMap.Extent();
76 for (i=1; i<=aNb; ++i) {
78 pProjPT=(GeomAPI_ProjectPointOnCurve*)anAdr;
83 BRepClass3d_SolidClassifier* pSC;
84 aNb=mySClassMap.Extent();
85 for (i=1; i<=aNb; ++i) {
87 pSC=(BRepClass3d_SolidClassifier*)anAdr;
92 IntTools_SurfaceRangeLocalizeData* pSData = NULL;
93 aNb = myProjSDataMap.Extent();
94 for (i=1; i<=aNb; ++i) {
95 anAdr=myProjSDataMap(i);
96 pSData = (IntTools_SurfaceRangeLocalizeData*)anAdr;
101 myProjSDataMap.Clear();
103 //=======================================================================
104 //function : FClass2d
106 //=======================================================================
107 IntTools_FClass2d& IntTools_Context::FClass2d(const TopoDS_Face& aF)
109 Standard_Address anAdr;
110 IntTools_FClass2d* pFClass2d;
112 anAdr=myFClass2dMap.FindFromKey1(aF);
116 aFF.Orientation(TopAbs_FORWARD);
117 aTolF=BRep_Tool::Tolerance(aFF);
119 pFClass2d=new IntTools_FClass2d(aFF, aTolF);
121 anAdr=(Standard_Address)pFClass2d;
122 myFClass2dMap.Add(aFF, anAdr);
126 Standard_Address *pAdr=(Standard_Address *)anAdr;
127 pFClass2d=(IntTools_FClass2d*)*pAdr;
132 //=======================================================================
135 //=======================================================================
136 GeomAPI_ProjectPointOnSurf& IntTools_Context::ProjPS(const TopoDS_Face& aF)
138 Standard_Address anAdr;
139 GeomAPI_ProjectPointOnSurf* pProjPS;
141 anAdr=myProjPSMap.FindFromKey1(aF);
143 Standard_Real Umin, Usup, Vmin, Vsup, anEpsT=1.e-12 ;
144 BRepAdaptor_Surface aBAS;
146 const Handle(Geom_Surface)& aS=BRep_Tool::Surface(aF);
147 aBAS.Initialize (aF, Standard_True);
149 Umin=aBAS.FirstUParameter();
150 Usup=aBAS.LastUParameter ();
151 Vmin=aBAS.FirstVParameter();
152 Vsup=aBAS.LastVParameter ();
154 pProjPS=new GeomAPI_ProjectPointOnSurf;
155 pProjPS->Init(aS ,Umin, Usup, Vmin, Vsup, anEpsT);
157 anAdr=(Standard_Address)pProjPS;
158 myProjPSMap.Add(aF, anAdr);
162 Standard_Address *pAdr=(Standard_Address *)anAdr;
163 pProjPS=(GeomAPI_ProjectPointOnSurf*)*pAdr;
167 //=======================================================================
170 //=======================================================================
171 GeomAPI_ProjectPointOnCurve& IntTools_Context::ProjPC(const TopoDS_Edge& aE)
173 Standard_Address anAdr;
174 GeomAPI_ProjectPointOnCurve* pProjPC;
176 anAdr=myProjPCMap.FindFromKey1(aE);
180 Handle(Geom_Curve)aC3D=BRep_Tool::Curve (aE, f, l);
182 pProjPC=new GeomAPI_ProjectPointOnCurve;
183 pProjPC->Init(aC3D, f, l);
185 anAdr=(Standard_Address)pProjPC;
186 myProjPCMap.Add(aE, anAdr);
189 Standard_Address *pAdr=(Standard_Address *)anAdr;
190 pProjPC=(GeomAPI_ProjectPointOnCurve*)*pAdr;
194 //=======================================================================
197 //=======================================================================
198 GeomAPI_ProjectPointOnCurve& IntTools_Context::ProjPT(const Handle(Geom_Curve)& aC3D)
201 Standard_Address anAdr;
202 GeomAPI_ProjectPointOnCurve* pProjPT;
204 anAdr=myProjPTMap.FindFromKey1(aC3D);
207 f=aC3D->FirstParameter();
208 l=aC3D->LastParameter();
210 pProjPT=new GeomAPI_ProjectPointOnCurve;
211 pProjPT->Init(aC3D, f, l);
213 anAdr=(Standard_Address)pProjPT;
214 myProjPTMap.Add(aC3D, anAdr);
218 Standard_Address *pAdr=(Standard_Address *)anAdr;
219 pProjPT=(GeomAPI_ProjectPointOnCurve*)*pAdr;
223 //=======================================================================
224 //function : SurfaceData
226 //=======================================================================
227 IntTools_SurfaceRangeLocalizeData& IntTools_Context::SurfaceData(const TopoDS_Face& aF)
229 Standard_Address anAdr;
230 IntTools_SurfaceRangeLocalizeData* pSData;
232 anAdr=myProjSDataMap.FindFromKey1(aF);
235 pSData=new IntTools_SurfaceRangeLocalizeData(3,
237 10. * Precision::PConfusion(),
238 10. * Precision::PConfusion());
240 anAdr=(Standard_Address)pSData;
241 myProjSDataMap.Add(aF, anAdr);
245 Standard_Address *pAdr=(Standard_Address *)anAdr;
246 pSData=(IntTools_SurfaceRangeLocalizeData*)*pAdr;
251 //=======================================================================
252 //function : SolidClassifier
254 //=======================================================================
255 BRepClass3d_SolidClassifier& IntTools_Context::SolidClassifier(const TopoDS_Solid& aSolid)
257 Standard_Address anAdr;
258 BRepClass3d_SolidClassifier* pSC;
260 anAdr=mySClassMap.FindFromKey1(aSolid);
263 pSC=new BRepClass3d_SolidClassifier(aSolid);
265 anAdr=(Standard_Address)pSC;
266 mySClassMap.Add(aSolid, anAdr);
269 Standard_Address *pAdr=(Standard_Address *)anAdr;
270 pSC =(BRepClass3d_SolidClassifier*)*pAdr;
274 //=======================================================================
275 //function : ComputeVE
277 //=======================================================================
278 Standard_Integer IntTools_Context::ComputeVE(const TopoDS_Vertex& aV1,
279 const TopoDS_Edge& aE2,
282 Standard_Boolean bToUpdate;
283 Standard_Integer iFlag;
286 iFlag= IntTools_Context::ComputeVE(aV1, aE2, aT, bToUpdate, aDist);
290 //=======================================================================
291 //function : ComputeVE
293 //=======================================================================
294 Standard_Integer IntTools_Context::ComputeVE(const TopoDS_Vertex& aV1,
295 const TopoDS_Edge& aE2,
297 Standard_Boolean& bToUpdateVertex,
298 Standard_Real& aDist)
300 bToUpdateVertex=Standard_False;
303 if (BRep_Tool::Degenerated(aE2)) {
307 //modified by NIZNHY-PKV Wed Jul 13 08:30:08 2011f
308 //if (!BRep_Tool::IsGeometric(aE2)) {
311 //modified by NIZNHY-PKV Wed Jul 13 08:30:13 2011t
313 Standard_Real aTolV1, aTolE2, aTolSum, aTolVx;
314 Standard_Integer aNbProj;
317 aP=BRep_Tool::Pnt(aV1);
319 GeomAPI_ProjectPointOnCurve& aProjector=ProjPC(aE2);
320 aProjector.Perform(aP);
321 aNbProj=aProjector.NbPoints();
326 aDist=aProjector.LowerDistance();
327 aTolV1=BRep_Tool::Tolerance(aV1);
328 aTolE2=BRep_Tool::Tolerance(aE2);
329 aTolSum=aTolV1+aTolE2;
331 aT=aProjector.LowerDistanceParameter();
332 if (aDist > aTolSum) {
338 bToUpdateVertex=!bToUpdateVertex;
344 //=======================================================================
345 //function : ComputeVS
347 //=======================================================================
348 Standard_Integer IntTools_Context::ComputeVS(const TopoDS_Vertex& aV1,
349 const TopoDS_Face& aF2,
353 Standard_Real aTolV1, aTolF2, aTolSum, aDist;
356 aP=BRep_Tool::Pnt(aV1);
358 // 1. Check if the point is projectable on the surface
359 GeomAPI_ProjectPointOnSurf& aProjector=ProjPS(aF2);
360 aProjector.Perform(aP);
362 if (!aProjector.IsDone()) {
363 // the point is not projectable on the surface
367 // 2. Check the distance between the projection point and
368 // the original point
369 aDist=aProjector.LowerDistance();
371 aTolV1=BRep_Tool::Tolerance(aV1);
372 aTolF2=BRep_Tool::Tolerance(aF2);
373 aTolSum=aTolV1+aTolF2;
374 if (aDist > aTolSum) {
375 // the distance is too large
378 aProjector.LowerDistanceParameters(U, V);
381 Standard_Boolean pri=IsPointInFace (aF2, aP2d);
383 // the point lays on the surface but out of the face
388 //=======================================================================
389 //function : StatePointFace
391 //=======================================================================
392 TopAbs_State IntTools_Context::StatePointFace(const TopoDS_Face& aF,
393 const gp_Pnt2d& aP2d)
396 IntTools_FClass2d& aClass2d=FClass2d(aF);
397 aState=aClass2d.Perform(aP2d);
400 //=======================================================================
401 //function : IsPointInFace
403 //=======================================================================
404 Standard_Boolean IntTools_Context::IsPointInFace(const TopoDS_Face& aF,
405 const gp_Pnt2d& aP2d)
407 TopAbs_State aState=StatePointFace(aF, aP2d);
408 if (aState==TopAbs_OUT || aState==TopAbs_ON) {
409 return Standard_False;
411 return Standard_True;
413 //=======================================================================
414 //function : IsPointInOnFace
416 //=======================================================================
417 Standard_Boolean IntTools_Context::IsPointInOnFace(const TopoDS_Face& aF,
418 const gp_Pnt2d& aP2d)
420 TopAbs_State aState=StatePointFace(aF, aP2d);
421 if (aState==TopAbs_OUT) {
422 return Standard_False;
424 return Standard_True;
426 //=======================================================================
427 //function : IsValidPointForFace
429 //=======================================================================
430 Standard_Boolean IntTools_Context::IsValidPointForFace(const gp_Pnt& aP,
431 const TopoDS_Face& aF,
432 const Standard_Real aTol)
434 Standard_Boolean bFlag;
435 Standard_Real Umin, myEpsT, U, V;
438 GeomAPI_ProjectPointOnSurf& aProjector=ProjPS(aF);
439 aProjector.Perform(aP);
441 bFlag=aProjector.IsDone();
444 Umin=aProjector.LowerDistance();
445 //if (Umin > 1.e-3) { // it was
450 aProjector.LowerDistanceParameters(U, V);
452 bFlag=IsPointInOnFace (aF, aP2D);
456 //=======================================================================
457 //function : IsValidPointForFaces
459 //=======================================================================
460 Standard_Boolean IntTools_Context::IsValidPointForFaces (const gp_Pnt& aP,
461 const TopoDS_Face& aF1,
462 const TopoDS_Face& aF2,
463 const Standard_Real aTol)
465 Standard_Boolean bFlag1, bFlag2;
467 bFlag1=IsValidPointForFace(aP, aF1, aTol);
471 bFlag2=IsValidPointForFace(aP, aF2, aTol);
474 //=======================================================================
475 //function : IsValidBlockForFace
477 //=======================================================================
478 Standard_Boolean IntTools_Context::IsValidBlockForFace (const Standard_Real aT1,
479 const Standard_Real aT2,
480 const IntTools_Curve& aC,
481 const TopoDS_Face& aF,
482 const Standard_Real aTol)
484 Standard_Boolean bFlag;
485 Standard_Real aTInterm, aFirst, aLast;
488 aTInterm=IntTools_Tools::IntermediatePoint(aT1, aT2);
490 Handle(Geom_Curve) aC3D=aC.Curve();
491 aFirst=aC3D->FirstParameter();
492 aLast =aC3D->LastParameter();
494 aC3D->D0(aTInterm, aPInterm);
496 bFlag=IsValidPointForFace (aPInterm, aF, aTol);
499 //=======================================================================
500 //function : IsValidBlockForFaces
502 //=======================================================================
503 Standard_Boolean IntTools_Context::IsValidBlockForFaces (const Standard_Real aT1,
504 const Standard_Real aT2,
505 const IntTools_Curve& aC,
506 const TopoDS_Face& aF1,
507 const TopoDS_Face& aF2,
508 const Standard_Real aTol)
510 Standard_Boolean bFlag1, bFlag2;
512 Handle(Geom2d_Curve) aPC1 = aC.FirstCurve2d();
513 Handle(Geom2d_Curve) aPC2 = aC.SecondCurve2d();
514 if( !aPC1.IsNull() && !aPC2.IsNull() ) {
515 Standard_Real aMidPar = IntTools_Tools::IntermediatePoint(aT1, aT2);
519 aPC1->D0(aMidPar, aPnt2D);
520 bFlag1 = IsPointInOnFace(aF1, aPnt2D);
525 aPC2->D0(aMidPar, aPnt2D);
526 bFlag2 = IsPointInOnFace(aF2, aPnt2D);
531 bFlag1=IsValidBlockForFace (aT1, aT2, aC, aF1, aTol);
535 bFlag2=IsValidBlockForFace (aT1, aT2, aC, aF2, aTol);
538 //=======================================================================
539 //function : IsVertexOnLine
541 //=======================================================================
542 Standard_Boolean IntTools_Context::IsVertexOnLine (const TopoDS_Vertex& aV,
543 const IntTools_Curve& aC,
544 const Standard_Real aTolC,
547 Standard_Boolean bRet;
550 aTolV=BRep_Tool::Tolerance(aV);
551 bRet=IntTools_Context::IsVertexOnLine(aV, aTolV, aC, aTolC , aT);
555 //=======================================================================
556 //function : IsVertexOnLine
558 //=======================================================================
559 Standard_Boolean IntTools_Context::IsVertexOnLine (const TopoDS_Vertex& aV,
560 const Standard_Real aTolV,
561 const IntTools_Curve& aC,
562 const Standard_Real aTolC,
565 Standard_Real aFirst, aLast, aDist, aTolSum;
566 Standard_Integer aNbProj;
569 aPv=BRep_Tool::Pnt(aV);
571 Handle(Geom_Curve) aC3D=aC.Curve();
576 GeomAdaptor_Curve aGAC(aC3D);
577 GeomAbs_CurveType aType=aGAC.GetType();
578 if (aType==GeomAbs_BSplineCurve ||
579 aType==GeomAbs_BezierCurve) {
586 aTolSum=2.*aTolSum;//xft
591 aFirst=aC3D->FirstParameter();
592 aLast =aC3D->LastParameter();
594 //Checking extermities first
595 if (!Precision::IsInfinite(aFirst)) {
596 gp_Pnt aPCFirst=aC3D->Value(aFirst);
597 aDist=aPv.Distance(aPCFirst);
598 if (aDist < aTolSum) {
602 Extrema_LocateExtPC anExt(aPv, aGAC, aFirst, 1.e-10);
605 Extrema_POnCurv aPOncurve = anExt.Point();
606 aT = aPOncurve.Parameter();
608 if((aT > (aLast + aFirst) * 0.5) ||
609 (aPv.Distance(aPOncurve.Value()) > aTolSum) ||
610 (aPCFirst.Distance(aPOncurve.Value()) < Precision::Confusion()))
615 return Standard_True;
619 if (!Precision::IsInfinite(aLast)) {
620 gp_Pnt aPCLast=aC3D->Value(aLast);
621 aDist=aPv.Distance(aPCLast);
622 if (aDist < aTolSum) {
626 Extrema_LocateExtPC anExt(aPv, aGAC, aLast, 1.e-10);
629 Extrema_POnCurv aPOncurve = anExt.Point();
630 aT = aPOncurve.Parameter();
632 if((aT < (aLast + aFirst) * 0.5) ||
633 (aPv.Distance(aPOncurve.Value()) > aTolSum) ||
634 (aPCLast.Distance(aPOncurve.Value()) < Precision::Confusion()))
639 return Standard_True;
643 GeomAPI_ProjectPointOnCurve& aProjector=ProjPT(aC3D);
644 aProjector.Perform(aPv);
646 aNbProj=aProjector.NbPoints();
648 Handle(Geom_BoundedCurve) aBC=
649 Handle(Geom_BoundedCurve)::DownCast(aC3D);
651 gp_Pnt aPStart=aBC->StartPoint();
652 gp_Pnt aPEnd =aBC->EndPoint();
654 aDist=aPv.Distance(aPStart);
655 if (aDist < aTolSum) {
657 return Standard_True;
660 aDist=aPv.Distance(aPEnd);
661 if (aDist < aTolSum) {
663 return Standard_True;
667 return Standard_False;
670 aDist=aProjector.LowerDistance();
672 if (aDist > aTolSum) {
673 return Standard_False;
676 aT=aProjector.LowerDistanceParameter();
678 return Standard_True;
680 //=======================================================================
681 //function : ProjectPointOnEdge
683 //=======================================================================
684 Standard_Boolean IntTools_Context::ProjectPointOnEdge(const gp_Pnt& aP,
685 const TopoDS_Edge& anEdge,
688 Standard_Integer aNbPoints;
690 GeomAPI_ProjectPointOnCurve& aProjector=ProjPC(anEdge);
691 aProjector.Perform(aP);
693 aNbPoints=aProjector.NbPoints();
695 aT=aProjector.LowerDistanceParameter();
696 return Standard_True;
698 return Standard_False;