0b769dd3db88b4671c6673766c080500390aebd2
[occt.git] / src / BOPInt / BOPInt_Tools.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 #include <BOPInt_Tools.ixx>
16
17 #include <gp_Pnt.hxx>
18 #include <BndLib_Add3dCurve.hxx>
19 #include <Bnd_Box.hxx>
20 #include <Geom_Curve.hxx>
21 #include <GeomAdaptor_Curve.hxx>
22
23 //=======================================================================
24 //function : CheckCurve
25 //purpose  : 
26 //=======================================================================
27   Standard_Boolean BOPInt_Tools::CheckCurve(const Handle (Geom_Curve)& aC3D,
28                                             const Standard_Real aTolR3D,
29                                             Bnd_Box& aBox)
30 {
31   Standard_Boolean bRet;  
32   Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dX, dY, dZ;
33   Standard_Real dS, aTol;
34   GeomAdaptor_Curve aGAC;
35   //
36   aGAC.Load(aC3D);
37   BndLib_Add3dCurve::Add(aGAC, aTolR3D, aBox);
38   // 910/B1
39   aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
40   dX=aXmax-aXmin;
41   dY=aYmax-aYmin;
42   dZ=aZmax-aZmin;
43   dS=1.e-12;
44   aTol=2.*aTolR3D+dS;
45   bRet=(dX>aTol ||  dY>aTol || dZ>aTol);
46   //
47   return bRet;
48 }
49
50
51 //=======================================================================
52 //function : IsOnPave
53 //purpose  : 
54 //=======================================================================
55   Standard_Boolean BOPInt_Tools::IsOnPave(const Standard_Real aT1,
56                                           const IntTools_Range& aRange,
57                                           const Standard_Real aTolerance)
58 {
59   Standard_Boolean firstisonpave1, firstisonpave2, bIsOnPave;
60   //
61   firstisonpave1  = (Abs(aRange.First() - aT1) < aTolerance);
62   firstisonpave2  = (Abs(aRange.Last()  - aT1) < aTolerance);
63   bIsOnPave=(firstisonpave1 || firstisonpave2);
64   return bIsOnPave;
65 }
66 //=======================================================================
67 // function: VertexParameters
68 // purpose: 
69 //=======================================================================
70   void BOPInt_Tools::VertexParameters(const IntTools_CommonPrt& aCPart,
71                                       Standard_Real& aT1, 
72                                       Standard_Real& aT2)
73 {
74   const IntTools_Range& aR1=aCPart.Range1();
75   aT1=0.5*(aR1.First()+aR1.Last());
76   //
77   if((aCPart.VertexParameter1() >= aR1.First()) &&
78      (aCPart.VertexParameter1() <= aR1.Last())) {
79     aT1 = aCPart.VertexParameter1();
80   }
81   //
82   const IntTools_SequenceOfRanges& aRanges2=aCPart.Ranges2();
83   const IntTools_Range& aR2=aRanges2(1);
84   aT2=0.5*(aR2.First()+aR2.Last());
85   //
86   if((aCPart.VertexParameter2() >= aR2.First()) &&
87      (aCPart.VertexParameter2() <= aR2.Last())) {
88     aT2 = aCPart.VertexParameter2();
89   }
90 }
91 //=======================================================================
92 // function: VertexParameter
93 // purpose: 
94 //=======================================================================
95   void BOPInt_Tools::VertexParameter(const IntTools_CommonPrt& aCPart,
96                                      Standard_Real& aT)
97 {
98   const IntTools_Range& aR=aCPart.Range1();
99   aT=0.5*(aR.First()+aR.Last());
100   if((aCPart.VertexParameter1() >= aR.First()) &&
101      (aCPart.VertexParameter1() <= aR.Last())) {
102     aT = aCPart.VertexParameter1();
103   }
104 }
105 //=======================================================================
106 // function: IsOnPave1
107 // purpose: 
108 //=======================================================================
109   Standard_Boolean BOPInt_Tools::IsOnPave1(const Standard_Real aTR,
110                                            const IntTools_Range& aCPRange,
111                                            const Standard_Real aTolerance)
112 {
113   Standard_Boolean bIsOnPave;
114   Standard_Real aT1, aT2, dT1, dT2;
115   //
116   aT1=aCPRange.First();
117   aT2=aCPRange.Last();
118   bIsOnPave=(aTR>=aT1 && aTR<=aT1);
119   if (bIsOnPave) {
120     return bIsOnPave;
121   }
122   //
123   dT1=Abs(aTR-aT1);  
124   dT2=Abs(aTR-aT2);
125   bIsOnPave=(dT1<=aTolerance || dT2<=aTolerance);
126   return bIsOnPave;
127 }
128
129 //=======================================================================
130 // function: IsInRange
131 // purpose: 
132 //=======================================================================
133   Standard_Boolean BOPInt_Tools::IsInRange(const IntTools_Range& aRRef,
134                                            const IntTools_Range& aR,
135                                            const Standard_Real aTolerance)
136 {
137   Standard_Boolean bIsIn;
138   Standard_Real aT1, aT2, aTRef1, aTRef2;
139   //
140   aR.Range(aT1, aT2);
141   aRRef.Range(aTRef1, aTRef2);
142   //
143   aTRef1-=aTolerance;
144   aTRef2+=aTolerance;
145   //
146   bIsIn = (aT1>=aTRef1 && aT1<=aTRef2) ||
147           (aT2>=aTRef1 && aT2<=aTRef2);
148   //
149   return bIsIn;
150 }
151
152 //=======================================================================
153 //function : SegPln
154 //purpose  : 
155 //=======================================================================
156   Standard_Integer BOPInt_Tools::SegPln(const gp_Lin& theLin,
157                                         const Standard_Real theTLin1,
158                                         const Standard_Real theTLin2,
159                                         const Standard_Real theTolLin,
160                                         const gp_Pln& thePln,
161                                         const Standard_Real theTolPln,
162                                         gp_Pnt& theP,
163                                         Standard_Real& theTP,
164                                         Standard_Real& theTolP,
165                                         Standard_Real& theTPmin,
166                                         Standard_Real& theTPmax)
167 {
168   Standard_Integer iRet;
169   Standard_Real aTol, aA, aB, aC, aD, aE, aH, aTP, aDist1, aDist2;
170   gp_Pnt aP1, aP2;
171   //
172   iRet=0;
173   aTol=theTolLin+theTolPln;
174   //
175   const gp_Ax3& aPosPln=thePln.Position();
176   const gp_Dir& aDirPln=aPosPln.Direction();
177   const gp_Pnt& aLocPln=aPosPln.Location();
178   //
179   const gp_Dir& aDirLin=theLin.Direction();
180   const gp_Pnt& aLocLin=theLin.Location();
181   //
182   aP1.SetXYZ(aLocLin.XYZ()+theTLin1*aDirLin.XYZ());
183   aDist1=aDirPln.X()*(aP1.X()-aLocPln.X())+
184          aDirPln.Y()*(aP1.Y()-aLocPln.Y())+
185          aDirPln.Z()*(aP1.Z()-aLocPln.Z());
186   //
187   aP2.SetXYZ(aLocLin.XYZ()+theTLin2*aDirLin.XYZ());
188   aDist2=aDirPln.X()*(aP2.X()-aLocPln.X())+
189          aDirPln.Y()*(aP2.Y()-aLocPln.Y())+
190          aDirPln.Z()*(aP2.Z()-aLocPln.Z());
191   //
192   if (aDist1<aTol && aDist2<aTol){
193     iRet=1; // common block
194     return iRet;
195   }
196   //
197   if (aDist1*aDist2 > 0.) {
198     iRet=2; // segment lays on one side to the Plane
199     return iRet;
200   } 
201   //
202   thePln.Coefficients(aA, aB, aC, aD);
203   aE=aA*aLocLin.X()+aB*aLocLin.Y()+aC*aLocLin.Z()+aD;
204   aH=aA*aDirLin.X()+aB*aDirLin.Y()+aC*aDirLin.Z();
205   aTP=-aE/aH;
206   if (aTP < theTLin1-aTol || aTP > theTLin2+aTol) {
207     iRet=3; // no intersections due to range of the Line
208     return iRet;
209   }
210   //
211   theTP=aTP;
212   theP.SetXYZ(aLocLin.XYZ()+aTP*aDirLin.XYZ());
213   theTolP=aTol;
214   theTPmin=theTP-theTolPln;
215   theTPmax=theTP+theTolPln;
216   iRet=0; // intersection point
217   return iRet;
218 }