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