Salome HOME
76e1779fc676d2bda6c57fa4e24ea4a6e2259067
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_SurfaceTools.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        GEOMAlgo_SurfaceTools.cxx
24 // Created:     Thu Jan 27 11:05:16 2005
25 // Author:      Peter KURNEV
26 //              <pkv@irinox>
27 //
28 #include <GEOMAlgo_SurfaceTools.hxx>
29
30 #include <math.h>
31
32 #include <gp_Pln.hxx>
33 #include <gp_Cylinder.hxx>
34 #include <gp_Sphere.hxx>
35 #include <gp_Ax1.hxx>
36 #include <gp_Lin.hxx>
37 #include <gp_Ax3.hxx>
38 #include <gp_Dir.hxx>
39 #include <gp_Ax1.hxx>
40 #include <gp_Vec.hxx>
41
42 #include <GeomAbs_SurfaceType.hxx>
43 #include <GeomAdaptor_Surface.hxx>
44 #include <IntSurf_Quadric.hxx>
45
46
47 //=======================================================================
48 //function : GetState
49 //purpose  :
50 //=======================================================================
51  Standard_Integer GEOMAlgo_SurfaceTools::GetState
52   (const gp_Pnt& aP,
53    const GeomAdaptor_Surface& aGAS,
54    const Standard_Real aTol,
55    TopAbs_State& aState)
56 {
57   Standard_Integer    iErr  = 0;
58   GeomAbs_SurfaceType aType = aGAS.GetType();
59   IntSurf_Quadric     aQuad;
60   //
61   aState = TopAbs_UNKNOWN;
62   //
63   switch (aType) {
64   case GeomAbs_Plane:
65     aQuad.SetValue(aGAS.Plane());
66     break;
67
68   case GeomAbs_Cylinder:
69     aQuad.SetValue(aGAS.Cylinder());
70     break;
71
72   case GeomAbs_Sphere:
73     aQuad.SetValue(aGAS.Sphere());
74     break;
75
76   default:
77     iErr=1; // unprocessed surface type
78     break;
79   }
80   //
81   if (!iErr) {
82     const Standard_Real aDp = aQuad.Distance(aP);
83     //
84     aState = TopAbs_ON;
85     //
86     if (aDp > aTol) {
87       aState = TopAbs_OUT;
88     } else if (aDp < -aTol) {
89       aState = TopAbs_IN;
90     }
91   }
92   //
93   return iErr;
94 }
95 //=======================================================================
96 //function : GetState
97 //purpose  :
98 //=======================================================================
99  Standard_Integer GEOMAlgo_SurfaceTools::GetState
100   (const gp_Pnt& aP,
101    const Handle(Geom_Surface)& aSurf,
102    const Standard_Real aTol,
103    TopAbs_State& aState)
104 {
105   Standard_Integer iErr;
106   GeomAdaptor_Surface aGAS;
107   //
108   aState=TopAbs_UNKNOWN;
109   aGAS.Load(aSurf);
110   //
111   iErr=GEOMAlgo_SurfaceTools::GetState(aP, aGAS, aTol, aState);
112   //
113   return iErr;
114 }
115 //=======================================================================
116 //function : ReverseState
117 //purpose  :
118 //=======================================================================
119  TopAbs_State GEOMAlgo_SurfaceTools::ReverseState
120   (const TopAbs_State aState)
121 {
122   TopAbs_State aRSt=aState;
123   //
124   switch (aState) {
125     case TopAbs_IN:
126      aRSt=TopAbs_OUT;
127      break;
128    case TopAbs_OUT:
129      aRSt=TopAbs_IN;
130      break;
131    default:
132      break;
133   }
134   //
135   return aRSt;
136 }
137 //=======================================================================
138 //function : IsCoaxial
139 //purpose  :
140 //=======================================================================
141 Standard_Boolean GEOMAlgo_SurfaceTools::IsCoaxial
142   (const gp_Pnt& aP1,
143    const gp_Pnt& aP2,
144    const gp_Cylinder& aCyl,
145    const Standard_Real aTol)
146 {
147   const gp_XYZ &aLoc   = aCyl.Location().XYZ();
148   const gp_Ax1 &aAxis  = aCyl.Axis();
149   const gp_XYZ &aDAxis = aAxis.Direction().XYZ();
150   gp_XYZ        aDP1   = aP1.XYZ().Subtracted(aLoc);
151   gp_XYZ        aDP2   = aP2.XYZ().Subtracted(aLoc);
152   Standard_Real aDot1  = aDP1.Dot(aDAxis);
153   Standard_Real aDot2  = aDP1.Dot(aDAxis);
154   //Standard_Real aTol2  = aTol*aTol;
155
156   // Project P1 and P2 onto a plane with location aLoc and Norm aDAxis.
157   aDP1.Subtract(aDAxis.Multiplied(aDot1));
158   aDP2.Subtract(aDAxis.Multiplied(aDot2));
159
160   Standard_Real    aRadius1 = aDP1.Modulus();
161   Standard_Real    aRadius2 = aDP2.Modulus();
162   Standard_Boolean isOn     = Standard_False;
163
164   if (fabs(aRadius1 - aRadius2) <= aTol) {
165     // Check the deflection of the middle point.
166     //gp_XYZ        aMidP       = 0.5*(aDP1 + aDP2);
167     //Standard_Real aMidRadius1 = aMidP.Modulus();
168
169     if (fabs(aRadius1 - aRadius2) <= aTol) {
170       isOn = Standard_True;
171     }
172   }
173
174   return isOn;
175 }
176 //=======================================================================
177 //function : IsAnalytic
178 //purpose  :
179 //=======================================================================
180 Standard_Boolean GEOMAlgo_SurfaceTools::IsAnalytic
181   (const Handle(Geom_Surface)& aSurf)
182 {
183   Standard_Boolean bRet;
184   GeomAbs_SurfaceType aType;
185   GeomAdaptor_Surface aGAS;
186   //
187   aGAS.Load(aSurf);
188   aType=aGAS.GetType();
189   bRet=(aType==GeomAbs_Plane ||
190         aType==GeomAbs_Cylinder ||
191         aType==GeomAbs_Sphere);
192   return bRet;
193 }
194 //=======================================================================
195 //function : IsConformState
196 //purpose  :
197 //=======================================================================
198 Standard_Boolean GEOMAlgo_SurfaceTools::IsConformState
199   (const TopAbs_State aST1,
200    const GEOMAlgo_State aST2)
201 {
202   Standard_Boolean bRet=Standard_False;
203   //
204   switch (aST2) {
205     case GEOMAlgo_ST_IN:
206       if (aST1==TopAbs_IN) {
207         bRet=!bRet;
208       }
209       break;
210     case GEOMAlgo_ST_OUT:
211       if (aST1==TopAbs_OUT) {
212         bRet=!bRet;
213       }
214       break;
215     case GEOMAlgo_ST_ON:
216       if (aST1==TopAbs_ON) {
217         bRet=!bRet;
218       }
219       break;
220     case GEOMAlgo_ST_ONIN:
221       if (aST1==TopAbs_ON || aST1==TopAbs_IN) {
222         bRet=!bRet;
223       }
224       break;
225     case GEOMAlgo_ST_ONOUT:
226       if (aST1==TopAbs_ON || aST1==TopAbs_OUT) {
227         bRet=!bRet;
228       }
229       break;
230     default:
231       break;
232   }
233   return bRet;
234 }