0027300: Boolean operation produces invalid shape in terms of "bopargcheck" command
[occt.git] / src / IntTools / IntTools_Tools.cxx
1 // Created on: 2000-11-16
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <Bnd_Box.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepAdaptor_Curve.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <Geom2d_Curve.hxx>
23 #include <Geom2d_TrimmedCurve.hxx>
24 #include <Geom_BoundedCurve.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom_Geometry.hxx>
27 #include <Geom_Line.hxx>
28 #include <Geom_Surface.hxx>
29 #include <Geom_TrimmedCurve.hxx>
30 #include <GeomAbs_CurveType.hxx>
31 #include <GeomAdaptor_Curve.hxx>
32 #include <GeomAdaptor_Surface.hxx>
33 #include <GeomAPI_ProjectPointOnCurve.hxx>
34 #include <GeomAPI_ProjectPointOnSurf.hxx>
35 #include <gp.hxx>
36 #include <gp_Ax1.hxx>
37 #include <gp_Dir.hxx>
38 #include <gp_Lin.hxx>
39 #include <gp_Pln.hxx>
40 #include <gp_Pnt.hxx>
41 #include <gp_Pnt2d.hxx>
42 #include <IntTools_CommonPrt.hxx>
43 #include <IntTools_Curve.hxx>
44 #include <IntTools_FClass2d.hxx>
45 #include <IntTools_Range.hxx>
46 #include <IntTools_SequenceOfCurves.hxx>
47 #include <IntTools_Tools.hxx>
48 #include <Precision.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopLoc_Location.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopoDS_Vertex.hxx>
56 #include <TopoDS_Wire.hxx>
57 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
58
59 static
60   void ParabolaTolerance(const Handle(Geom_Curve)& ,
61     const Standard_Real ,
62     const Standard_Real ,
63     const Standard_Real ,
64     Standard_Real& ,
65     Standard_Real& );
66
67 //=======================================================================
68 //function : HasInternalEdge
69 //purpose  : 
70 //=======================================================================
71   Standard_Boolean IntTools_Tools::HasInternalEdge(const TopoDS_Wire& aW)
72 {
73   Standard_Boolean bFlag=Standard_True;
74
75   TopExp_Explorer anExp(aW, TopAbs_EDGE);
76   for (; anExp.More(); anExp.Next()) {
77     const TopoDS_Edge& aE=TopoDS::Edge(anExp.Current());
78     TopAbs_Orientation anOr=aE.Orientation();
79     if (anOr==TopAbs_INTERNAL) {
80       return bFlag;
81     }
82   }
83   return !bFlag;
84 }
85
86 //=======================================================================
87 //function : IsClosed
88 //purpose  : 
89 //=======================================================================
90   Standard_Boolean IntTools_Tools::IsClosed (const Handle(Geom_Curve)& aC3D)
91 {
92   Standard_Boolean bRet;
93   Standard_Real aF, aL, aDist, aPC;
94   gp_Pnt aP1, aP2;
95   
96   Handle (Geom_BoundedCurve) aGBC=
97       Handle (Geom_BoundedCurve)::DownCast(aC3D);
98   if (aGBC.IsNull()) {
99     return Standard_False;
100   }
101   
102   aF=aC3D->FirstParameter();
103   aL=aC3D-> LastParameter();
104   
105   aC3D->D0(aF, aP1);
106   aC3D->D0(aL, aP2);
107
108   
109   //
110   aPC=Precision::Confusion();
111   aPC=aPC*aPC;
112   aDist=aP1.SquareDistance(aP2);
113   bRet=aDist<aPC;
114   return bRet;
115 }
116
117 //=======================================================================
118 //function : RejectLines
119 //purpose  : 
120 //=======================================================================
121    void IntTools_Tools::RejectLines(const IntTools_SequenceOfCurves& aSIn,
122         IntTools_SequenceOfCurves& aSOut)
123 {
124   Standard_Integer i, j, aNb;
125   Standard_Boolean bFlag;
126   Handle (Geom_Curve) aC3D;
127
128   gp_Dir aD1, aD2;
129
130   aSOut.Clear();
131
132   aNb=aSIn.Length();
133   for (i=1; i<=aNb; i++) {
134     const IntTools_Curve& IC=aSIn(i);
135     aC3D=IC.Curve();
136     //
137     Handle (Geom_TrimmedCurve) aGTC=
138       Handle (Geom_TrimmedCurve)::DownCast(aC3D);
139     
140     if (!aGTC.IsNull()) {
141       aC3D=aGTC->BasisCurve();
142       IntTools_Curve* pIC=(IntTools_Curve*) &IC;
143       pIC->SetCurve(aC3D);
144     }
145     //
146     Handle (Geom_Line) aGLine=
147       Handle (Geom_Line)::DownCast(aC3D);
148     
149     if (aGLine.IsNull()) {
150       aSOut.Clear();
151       for (j=1; j<=aNb; j++) {
152  aSOut.Append(aSIn(j));
153       }
154       return;
155     }
156     //
157     gp_Lin aLin=aGLine->Lin();
158     aD2=aLin.Direction();
159     if (i==1) {
160       aSOut.Append(IC);
161       aD1=aD2;
162       continue;
163     }
164     
165     bFlag=IntTools_Tools::IsDirsCoinside(aD1, aD2);
166     if (!bFlag) {
167       aSOut.Append(IC);
168       return;
169     }
170   }
171 }
172
173 //=======================================================================
174 //function : IsDirsCoinside
175 //purpose  : 
176 //=======================================================================
177   Standard_Boolean IntTools_Tools::IsDirsCoinside  (const gp_Dir& D1, const gp_Dir& D2)
178 {
179   Standard_Boolean bFlag;
180   gp_Pnt P1(D1.X(), D1.Y(), D1.Z());  
181   gp_Pnt P2(D2.X(), D2.Y(), D2.Z());
182   Standard_Real dLim=0.0002, d;
183   d=P1.Distance (P2); 
184   bFlag= (d<dLim || fabs (2.-d)<dLim); 
185   return bFlag;
186 }
187   
188 //=======================================================================
189 //function : IsDirsCoinside
190 //purpose  : 
191 //=======================================================================
192   Standard_Boolean IntTools_Tools::IsDirsCoinside  (const gp_Dir& D1, 
193           const gp_Dir& D2,
194           const Standard_Real dLim)
195 {
196   Standard_Boolean bFlag;
197   Standard_Real d;
198   //
199   gp_Pnt P1(D1.X(), D1.Y(), D1.Z());  
200   gp_Pnt P2(D2.X(), D2.Y(), D2.Z());
201   d=P1.Distance (P2); 
202   bFlag= (d<dLim || fabs (2.-d)<dLim); 
203   return bFlag;
204 }
205 //=======================================================================
206 //function : SplitCurve
207 //purpose  : 
208 //=======================================================================
209   Standard_Integer IntTools_Tools::SplitCurve(const IntTools_Curve& IC,
210            IntTools_SequenceOfCurves& aCvs)
211 {
212   Handle (Geom_Curve) aC3D =IC.Curve();
213   if(aC3D.IsNull())
214     return 0;
215   //
216   Handle (Geom2d_Curve) aC2D1=IC.FirstCurve2d();
217   Handle (Geom2d_Curve) aC2D2=IC.SecondCurve2d();
218   Standard_Boolean bIsClosed;
219
220   bIsClosed=IntTools_Tools::IsClosed(aC3D);
221   if (!bIsClosed) {
222     return 0;
223   }
224
225   Standard_Real aF, aL, aMid;
226   
227   //
228   aF=aC3D->FirstParameter();
229   aL=aC3D->LastParameter();
230   aMid=0.5*(aF+aL);
231   GeomAdaptor_Curve aGAC(aC3D);
232   GeomAbs_CurveType aCT=aGAC.GetType();
233   if (aCT==GeomAbs_BSplineCurve ||
234       aCT==GeomAbs_BezierCurve) {
235     //aMid=0.5*aMid;
236     aMid=IntTools_Tools::IntermediatePoint(aF, aL);
237   }
238   //
239   Handle(Geom_Curve) aC3DNewF, aC3DNewL;
240   aC3DNewF =new Geom_TrimmedCurve  (aC3D, aF, aMid);
241   aC3DNewL =new Geom_TrimmedCurve  (aC3D, aMid, aL);
242
243   //
244   Handle (Geom2d_Curve) aC2D1F, aC2D1L, aC2D2F, aC2D2L;
245   //
246   if(!aC2D1.IsNull()) {
247     aC2D1F=new Geom2d_TrimmedCurve (aC2D1, aF, aMid);
248     aC2D1L=new Geom2d_TrimmedCurve (aC2D1, aMid, aL);
249   }
250
251   if(!aC2D2.IsNull()) {
252     aC2D2F=new Geom2d_TrimmedCurve (aC2D2, aF, aMid);
253     aC2D2L=new Geom2d_TrimmedCurve (aC2D2, aMid, aL);
254   }
255   // 
256   
257   IntTools_Curve aIC1(aC3DNewF, aC2D1F, aC2D2F);
258   IntTools_Curve aIC2(aC3DNewL, aC2D1L, aC2D2L);
259   //
260   aCvs.Append(aIC1);
261   //
262   aCvs.Append(aIC2);
263   //
264   return 2;
265 }
266
267 //=======================================================================
268 //function : IntermediatePoint
269 //purpose  : 
270 //=======================================================================
271   Standard_Real IntTools_Tools::IntermediatePoint (const Standard_Real aFirst,
272          const Standard_Real aLast)
273 {
274   //define parameter division number as 10*e^(-M_PI) = 0.43213918
275   const Standard_Real PAR_T = 0.43213918;
276   Standard_Real aParm;
277   aParm=(1.-PAR_T)*aFirst + PAR_T*aLast;
278   return aParm;
279 }
280
281 //=======================================================================
282 //function : IsVertex
283 //purpose  : 
284 //=======================================================================
285   Standard_Boolean IntTools_Tools::IsVertex (const gp_Pnt& aP,
286           const Standard_Real aTolPV,
287           const TopoDS_Vertex& aV)
288 {
289   Standard_Boolean bRet;
290   Standard_Real aTolV, aD, dTol;
291   gp_Pnt aPv; 
292   
293   aTolV=BRep_Tool::Tolerance(aV);
294   //
295   dTol=Precision::Confusion();
296   aTolV=aTolV+aTolPV+dTol;
297   //
298   aPv=BRep_Tool::Pnt(aV);
299   //
300   aD=aPv.SquareDistance(aP);
301   aTolV=aTolV*aTolV;
302   bRet=(aD<=aTolV);
303   return bRet;
304 }
305
306
307 //=======================================================================
308 //function : IsVertex
309 //purpose  : 
310 //=======================================================================
311   Standard_Boolean IntTools_Tools::IsVertex (const IntTools_CommonPrt& aCmnPrt)
312 {
313   Standard_Boolean anIsVertex;
314   Standard_Real aParam;
315
316   const TopoDS_Edge&    aE1=aCmnPrt.Edge1();
317   const IntTools_Range& aR1=aCmnPrt.Range1();
318   aParam=0.5*(aR1.First()+aR1.Last());
319   anIsVertex=IntTools_Tools::IsVertex (aE1, aParam);
320   
321   if (anIsVertex) {
322     return Standard_True;
323   }
324
325   const TopoDS_Edge&    aE2=aCmnPrt.Edge2();
326   const IntTools_SequenceOfRanges& aRs2=aCmnPrt.Ranges2();
327   const IntTools_Range& aR2=aRs2(1);
328   aParam=0.5*(aR2.First()+aR2.Last());
329   anIsVertex=IntTools_Tools::IsVertex (aE2, aParam);
330   if (anIsVertex) {
331     return Standard_True;
332   }
333   return Standard_False;
334 }
335
336 //=======================================================================
337 //function : IsVertex
338 //purpose  : 
339 //=======================================================================
340   Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
341           const TopoDS_Vertex& aV,
342           const Standard_Real t)
343 {
344   Standard_Real aTolV, aTolV2, d2;
345   gp_Pnt aPv, aPt; 
346   
347   BRepAdaptor_Curve aBAC(aE);
348   aBAC.D0(t, aPt);
349
350   aTolV=BRep_Tool::Tolerance(aV);
351   aTolV2=aTolV*aTolV;
352   aPv=BRep_Tool::Pnt(aV);
353   d2=aPv.SquareDistance (aPt);
354   if (d2 < aTolV2) {
355     return Standard_True;
356   }
357   return Standard_False;
358 }
359 //=======================================================================
360 //function : IsVertex
361 //purpose  : 
362 //=======================================================================
363   Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
364           const Standard_Real t)
365 {
366   Standard_Real aTolV, aTolV2, d2;
367   TopoDS_Vertex aV;
368   gp_Pnt aPv, aPt; 
369   
370   BRepAdaptor_Curve aBAC(aE);
371   aBAC.D0(t, aPt);
372
373   TopExp_Explorer anExp(aE, TopAbs_VERTEX);
374   for (; anExp.More(); anExp.Next()) {
375     aV=TopoDS::Vertex (anExp.Current());
376     aTolV=BRep_Tool::Tolerance(aV);
377     aTolV2=aTolV*aTolV;
378     aTolV2=1.e-12;
379      aPv=BRep_Tool::Pnt(aV);
380      d2=aPv.SquareDistance (aPt);
381      if (d2 < aTolV2) {
382        return Standard_True;
383      }
384   }
385   return Standard_False;
386 }
387
388
389 //=======================================================================
390 //function : ComputeVV
391 //purpose  : 
392 //=======================================================================
393   Standard_Integer IntTools_Tools::ComputeVV(const TopoDS_Vertex& aV1, 
394           const TopoDS_Vertex& aV2)
395 {
396   Standard_Real aTolV1, aTolV2, aTolSum, d;
397   gp_Pnt aP1, aP2;
398
399   aTolV1=BRep_Tool::Tolerance(aV1);
400   aTolV2=BRep_Tool::Tolerance(aV2);
401   aTolSum=aTolV1+aTolV2;
402   
403   aP1=BRep_Tool::Pnt(aV1);
404   aP2=BRep_Tool::Pnt(aV2);
405   aTolSum=aTolSum*aTolSum;
406   d=aP1.SquareDistance(aP2);
407   if (d<aTolSum) {
408     return 0;
409   }
410   return -1;
411 }
412
413 //=======================================================================
414 //function : MakeFaceFromWireAndFace
415 //purpose  : 
416 //=======================================================================
417   void IntTools_Tools::MakeFaceFromWireAndFace(const TopoDS_Wire& aW,
418             const TopoDS_Face& aF,
419             TopoDS_Face& aFNew)
420 {
421   TopoDS_Face aFF;
422   aFF=aF;
423   aFF.Orientation(TopAbs_FORWARD);
424   aFNew=TopoDS::Face (aFF.EmptyCopied());
425   BRep_Builder BB;
426   BB.Add(aFNew, aW);
427 }
428
429 //=======================================================================
430 //function : ClassifyPointByFace
431 //purpose  : 
432 //=======================================================================
433   TopAbs_State IntTools_Tools::ClassifyPointByFace(const TopoDS_Face& aF,
434          const gp_Pnt2d& aP2d)
435 {
436   Standard_Real aFaceTolerance;
437   TopAbs_State aState;
438   
439   aFaceTolerance=BRep_Tool::Tolerance(aF);
440   IntTools_FClass2d aClass2d(aF, aFaceTolerance);
441   aState=aClass2d.Perform(aP2d);
442   
443   return aState;
444 }
445
446 //=======================================================================
447 //function : IsMiddlePointsEqual
448 //purpose  : 
449 //=======================================================================
450   Standard_Boolean IntTools_Tools::IsMiddlePointsEqual(const TopoDS_Edge& aE1,
451              const TopoDS_Edge& aE2)
452              
453 {
454   Standard_Boolean bRet;
455   Standard_Real f1, l1, m1, f2, l2, m2, aTol1, aTol2, aSumTol, aD2;
456   gp_Pnt aP1, aP2;
457
458   aTol1=BRep_Tool::Tolerance(aE1);
459   Handle(Geom_Curve) C1=BRep_Tool::Curve(aE1, f1, l1);
460   m1=0.5*(f1+l1);
461   C1->D0(m1, aP1);
462
463   aTol2=BRep_Tool::Tolerance(aE2);
464   Handle(Geom_Curve) C2=BRep_Tool::Curve(aE2, f2, l2);
465   m2=0.5*(f2+l2);
466   C2->D0(m2, aP2);
467
468   aSumTol=aTol1+aTol2;
469   aSumTol=aSumTol*aSumTol;
470   aD2=aP1.SquareDistance(aP2);
471   bRet=aD2<aSumTol;
472   return bRet;
473 }
474
475 //=======================================================================
476 //function : CurveTolerance
477 //purpose  : 
478 //=======================================================================
479   Standard_Real IntTools_Tools::CurveTolerance(const Handle(Geom_Curve)& aC3D,
480             const Standard_Real aTolBase)
481 {
482   Standard_Real aTolReached, aTf, aTl, aTolMin, aTolMax;
483
484   aTolReached=aTolBase;
485   //
486   if (aC3D.IsNull()) {
487     return aTolReached;
488   }
489   //
490   Handle(Geom_TrimmedCurve) aCT3D=Handle(Geom_TrimmedCurve)::DownCast(aC3D);
491   if (aCT3D.IsNull()) {
492     return aTolReached;
493   }
494   //
495   aTolMin=aTolBase;
496   aTolMax=aTolBase;
497   //
498   aTf=aCT3D->FirstParameter();
499   aTl=aCT3D->LastParameter();
500   //
501   GeomAdaptor_Curve aGAC(aCT3D);
502   GeomAbs_CurveType aCType=aGAC.GetType();
503   //
504   if (aCType==GeomAbs_Parabola) {
505     Handle(Geom_Curve) aC3DBase=aCT3D->BasisCurve();
506     ParabolaTolerance(aC3DBase, aTf, aTl, aTolBase, aTolMin, aTolMax);
507     aTolReached=aTolMax;
508   }
509   //
510   return aTolReached;
511 }
512
513 #include <Geom_Parabola.hxx>
514 #include <gp_Parab.hxx>
515 #include <BndLib_Add3dCurve.hxx>
516 #include <BRepLib_CheckCurveOnSurface.hxx>
517 //=======================================================================
518 //function : ParabolaTolerance
519 //purpose  : 
520 //=======================================================================
521 void ParabolaTolerance(const Handle(Geom_Curve)& aC3D,
522          const Standard_Real aTf,
523          const Standard_Real aTl,
524          const Standard_Real aTol,
525          Standard_Real& aTolMin,
526          Standard_Real& aTolMax)
527 {
528   
529   aTolMin=aTol;
530   aTolMax=aTol;
531
532   Handle(Geom_Parabola) aGP=Handle(Geom_Parabola)::DownCast(aC3D);
533   if (aGP.IsNull()){
534     return;
535   }
536
537   Standard_Integer aNbPoints;
538   Standard_Real aFocal, aX1, aX2, aTol1, aTol2;
539   gp_Pnt aPf, aPl;
540   gp_Parab aParab=aGP->Parab();
541   gp_Ax1 aXAxis=aParab.XAxis();
542   Handle(Geom_Line) aGAxis=new Geom_Line(aXAxis);
543
544   aFocal=aGP->Focal();
545   if (aFocal==0.) {
546     return;
547   }
548   //
549   // aTol1
550   aTol1=aTol;
551   aX1=0.;
552   aGP->D0(aTf, aPf);
553   GeomAPI_ProjectPointOnCurve aProj1(aPf, aGAxis);
554   aNbPoints=aProj1.NbPoints();
555   if (aNbPoints) {
556     aX1=aProj1.LowerDistanceParameter();
557   }
558   if (aX1>=0.) {
559     aTol1=aTol*sqrt(0.5*aX1/aFocal);
560   }
561   if (aTol1==0.) {
562     aTol1=aTol;
563   }
564   //
565   // aTol2
566   aTol2=aTol;
567   aX2=0.;
568   aGP->D0(aTl, aPl);
569   GeomAPI_ProjectPointOnCurve aProj2(aPl, aGAxis);
570   aNbPoints=aProj2.NbPoints();
571   if (aNbPoints) {
572     aX2=aProj2.LowerDistanceParameter();
573   }
574   
575   if (aX2>=0.) {
576     aTol2=aTol*sqrt(0.5*aX2/aFocal);
577   }
578   if (aTol2==0.) {
579     aTol2=aTol;
580   }
581   //
582   aTolMax=(aTol1>aTol2) ? aTol1 : aTol2;
583   aTolMin=(aTol1<aTol2) ? aTol1 : aTol2;
584 }
585 /////////////////////////////////////////////////////////////////////////
586 //=======================================================================
587 //function : CheckCurve
588 //purpose  : 
589 //=======================================================================
590 Standard_Boolean IntTools_Tools::CheckCurve(const Handle (Geom_Curve)& aC3D,
591                                             const Standard_Real aTolR3D,
592                                             Bnd_Box& aBox)
593 {
594   Standard_Boolean bRet;  
595   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dX, dY, dZ;
596   Standard_Real dS, aTol;
597   GeomAdaptor_Curve aGAC;
598   //
599   aGAC.Load(aC3D);
600   BndLib_Add3dCurve::Add(aGAC, aTolR3D, aBox);
601   // 910/B1
602   aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
603   dX=aXmax-aXmin;
604   dY=aYmax-aYmin;
605   dZ=aZmax-aZmin;
606   dS=1.e-12;
607   aTol=2.*aTolR3D+dS;
608   bRet=(dX>aTol ||  dY>aTol || dZ>aTol);
609   //
610   return bRet;
611 }
612 //=======================================================================
613 //function : IsOnPave
614 //purpose  : 
615 //=======================================================================
616 Standard_Boolean IntTools_Tools::IsOnPave(const Standard_Real aT1,
617                                           const IntTools_Range& aRange,
618                                           const Standard_Real aTolerance)
619 {
620   Standard_Boolean firstisonpave1, firstisonpave2, bIsOnPave;
621   //
622   firstisonpave1  = (Abs(aRange.First() - aT1) < aTolerance);
623   firstisonpave2  = (Abs(aRange.Last()  - aT1) < aTolerance);
624   bIsOnPave=(firstisonpave1 || firstisonpave2);
625   return bIsOnPave;
626 }
627 //=======================================================================
628 // function: VertexParameters
629 // purpose: 
630 //=======================================================================
631 void IntTools_Tools::VertexParameters(const IntTools_CommonPrt& aCPart,
632                                       Standard_Real& aT1, 
633                                       Standard_Real& aT2)
634 {
635   const IntTools_Range& aR1=aCPart.Range1();
636   aT1=0.5*(aR1.First()+aR1.Last());
637   //
638   if((aCPart.VertexParameter1() >= aR1.First()) &&
639      (aCPart.VertexParameter1() <= aR1.Last())) {
640     aT1 = aCPart.VertexParameter1();
641   }
642   //
643   const IntTools_SequenceOfRanges& aRanges2=aCPart.Ranges2();
644   const IntTools_Range& aR2=aRanges2(1);
645   aT2=0.5*(aR2.First()+aR2.Last());
646   //
647   if((aCPart.VertexParameter2() >= aR2.First()) &&
648      (aCPart.VertexParameter2() <= aR2.Last())) {
649     aT2 = aCPart.VertexParameter2();
650   }
651 }
652 //=======================================================================
653 // function: VertexParameter
654 // purpose: 
655 //=======================================================================
656 void IntTools_Tools::VertexParameter(const IntTools_CommonPrt& aCPart,
657                                      Standard_Real& aT)
658 {
659   const IntTools_Range& aR=aCPart.Range1();
660   aT=0.5*(aR.First()+aR.Last());
661   if((aCPart.VertexParameter1() >= aR.First()) &&
662      (aCPart.VertexParameter1() <= aR.Last())) {
663     aT = aCPart.VertexParameter1();
664   }
665 }
666 //=======================================================================
667 // function: IsOnPave1
668 // purpose: 
669 //=======================================================================
670 Standard_Boolean IntTools_Tools::IsOnPave1(const Standard_Real aTR,
671                                            const IntTools_Range& aCPRange,
672                                            const Standard_Real aTolerance)
673 {
674   Standard_Boolean bIsOnPave;
675   Standard_Real aT1, aT2, dT1, dT2;
676   //
677   aT1=aCPRange.First();
678   aT2=aCPRange.Last();
679   bIsOnPave=(aTR>=aT1 && aTR<=aT2);
680   if (bIsOnPave) {
681     return bIsOnPave;
682   }
683   //
684   dT1=Abs(aTR-aT1);  
685   dT2=Abs(aTR-aT2);
686   bIsOnPave=(dT1<=aTolerance || dT2<=aTolerance);
687   return bIsOnPave;
688 }
689 //=======================================================================
690 // function: IsInRange
691 // purpose: 
692 //=======================================================================
693 Standard_Boolean IntTools_Tools::IsInRange(const IntTools_Range& aRRef,
694                                            const IntTools_Range& aR,
695                                            const Standard_Real aTolerance)
696 {
697   Standard_Boolean bIsIn;
698   Standard_Real aT1, aT2, aTRef1, aTRef2;
699   //
700   aR.Range(aT1, aT2);
701   aRRef.Range(aTRef1, aTRef2);
702   //
703   aTRef1-=aTolerance;
704   aTRef2+=aTolerance;
705   //
706   bIsIn = (aT1>=aTRef1 && aT1<=aTRef2) ||
707           (aT2>=aTRef1 && aT2<=aTRef2);
708   //
709   return bIsIn;
710 }
711 //=======================================================================
712 //function : SegPln
713 //purpose  : 
714 //=======================================================================
715 Standard_Integer IntTools_Tools::SegPln(const gp_Lin& theLin,
716                                         const Standard_Real theTLin1,
717                                         const Standard_Real theTLin2,
718                                         const Standard_Real theTolLin,
719                                         const gp_Pln& thePln,
720                                         const Standard_Real theTolPln,
721                                         gp_Pnt& theP,
722                                         Standard_Real& theTP,
723                                         Standard_Real& theTolP,
724                                         Standard_Real& theTPmin,
725                                         Standard_Real& theTPmax)
726 {
727   Standard_Integer iRet;
728   Standard_Real aTol, aA, aB, aC, aD, aE, aH, aTP, aDist1, aDist2;
729   gp_Pnt aP1, aP2;
730   //
731   iRet=0;
732   aTol=theTolLin+theTolPln;
733   //
734   const gp_Ax3& aPosPln=thePln.Position();
735   const gp_Dir& aDirPln=aPosPln.Direction();
736   const gp_Pnt& aLocPln=aPosPln.Location();
737   //
738   const gp_Dir& aDirLin=theLin.Direction();
739   const gp_Pnt& aLocLin=theLin.Location();
740   //
741   aP1.SetXYZ(aLocLin.XYZ()+theTLin1*aDirLin.XYZ());
742   aDist1=aDirPln.X()*(aP1.X()-aLocPln.X())+
743          aDirPln.Y()*(aP1.Y()-aLocPln.Y())+
744          aDirPln.Z()*(aP1.Z()-aLocPln.Z());
745   //
746   aP2.SetXYZ(aLocLin.XYZ()+theTLin2*aDirLin.XYZ());
747   aDist2=aDirPln.X()*(aP2.X()-aLocPln.X())+
748          aDirPln.Y()*(aP2.Y()-aLocPln.Y())+
749          aDirPln.Z()*(aP2.Z()-aLocPln.Z());
750   //
751   if (aDist1<aTol && aDist2<aTol){
752     iRet=1; // common block
753     return iRet;
754   }
755   //
756   if (aDist1*aDist2 > 0.) {
757     iRet=2; // segment lays on one side to the Plane
758     return iRet;
759   } 
760   //
761   thePln.Coefficients(aA, aB, aC, aD);
762   aE=aA*aLocLin.X()+aB*aLocLin.Y()+aC*aLocLin.Z()+aD;
763   aH=aA*aDirLin.X()+aB*aDirLin.Y()+aC*aDirLin.Z();
764   aTP=-aE/aH;
765   if (aTP < theTLin1-aTol || aTP > theTLin2+aTol) {
766     iRet=3; // no intersections due to range of the Line
767     return iRet;
768   }
769   //
770   theTP=aTP;
771   theP.SetXYZ(aLocLin.XYZ()+aTP*aDirLin.XYZ());
772   theTolP=aTol;
773   theTPmin=theTP-theTolPln;
774   theTPmax=theTP+theTolPln;
775   iRet=0; // intersection point
776   return iRet;
777 }
778
779 //=======================================================================
780 // Function : ComputeTolerance
781 // purpose : 
782 //=======================================================================
783 Standard_Boolean IntTools_Tools::ComputeTolerance
784   (const Handle(Geom_Curve)& theCurve3D,
785    const Handle(Geom2d_Curve)& theCurve2D,
786    const Handle(Geom_Surface)& theSurf,
787    const Standard_Real theFirst,
788    const Standard_Real theLast,
789    Standard_Real& theMaxDist,
790    Standard_Real& theMaxPar,
791    const Standard_Real theTolRange)
792 {
793   GeomLib_CheckCurveOnSurface aCS;
794   //
795   aCS.Init(theCurve3D, theSurf, theFirst, theLast, theTolRange);
796   aCS.Perform(theCurve2D);
797   if (!aCS.IsDone()) {
798     return Standard_False;
799   }
800
801   //Obtaining precise result is impossible if we use 
802   //numeric methods for solution. Therefore, we must provide
803   //some margin. Otherwise, in the future
804   //(when geometrical properties of the curve will be changed,
805   //e.g. after trimming) we will be able to come
806   //to the more precise minimum point. As result, this curve with the
807   //tolerance computed earlier will become invalid.
808   const Standard_Real anEps = (1.0+1.0e-9);
809   theMaxDist = anEps*aCS.MaxDistance();
810   theMaxPar  = aCS.MaxParameter();
811   //
812   return Standard_True;
813 }
814
815 //=======================================================================
816 // Function : ComputeIntRange
817 // purpose : 
818 //=======================================================================
819 Standard_Real IntTools_Tools::ComputeIntRange(const Standard_Real theTol1,
820                                               const Standard_Real theTol2,
821                                               const Standard_Real theAngle)
822 {
823   Standard_Real aDt;
824   //
825   if (Abs(M_PI_2 - theAngle) < Precision::Angular()) {
826     aDt = theTol2;
827   }
828   else {
829     Standard_Real a1, a2, anAngle;
830     //
831     anAngle = (theAngle > M_PI_2) ? (M_PI - theAngle) : theAngle;
832     a1 = theTol1 * tan(M_PI_2 - anAngle);
833     a2 = theTol2 / sin(anAngle);
834     aDt = a1 + a2;
835   }
836   //
837   return aDt;
838 }