Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_SurfaceTools.cxx
1 //  Copyright (C) 2007-2008  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.
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 // File:        GEOMAlgo_SurfaceTools.cxx
23 // Created:     Thu Jan 27 11:05:16 2005
24 // Author:      Peter KURNEV
25 //              <pkv@irinox>
26 //
27 #include <GEOMAlgo_SurfaceTools.ixx>
28
29 #include <math.h>
30
31 #include <gp_Pln.hxx>
32 #include <gp_Cylinder.hxx>
33 #include <gp_Sphere.hxx>
34 #include <gp_Ax1.hxx>
35 #include <gp_Lin.hxx>
36 #include <gp_Ax3.hxx>
37 #include <gp_Dir.hxx>
38 #include <gp_Ax1.hxx>
39 #include <gp_Vec.hxx>
40
41 #include <GeomAbs_SurfaceType.hxx>
42 #include <GeomAdaptor_Surface.hxx>
43
44
45 //=======================================================================
46 //function : GetState
47 //purpose  : 
48 //=======================================================================
49  Standard_Integer GEOMAlgo_SurfaceTools::GetState(const gp_Pnt& aP,
50                                                   const GeomAdaptor_Surface& aGAS,
51                                                   const Standard_Real aTol,
52                                                   TopAbs_State& aState)
53 {
54   Standard_Integer iErr;
55   Standard_Real aDp, aR;
56   GeomAbs_SurfaceType aType;
57   gp_Sphere aSph;
58   gp_Cylinder aCyl;
59   gp_Pln aPln;
60   //
61   iErr=0;
62   aState=TopAbs_UNKNOWN;
63   //
64   aType=aGAS.GetType();
65   switch (aType) {
66   case GeomAbs_Plane:
67     aPln=aGAS.Plane();
68     aR=0.;
69     aDp=GEOMAlgo_SurfaceTools::Distance(aP, aPln);
70     break;
71   
72   case GeomAbs_Cylinder: 
73     aCyl=aGAS.Cylinder();
74     aR=aCyl.Radius();
75     aDp=GEOMAlgo_SurfaceTools::Distance(aP, aCyl);
76     break; 
77
78   case GeomAbs_Sphere: 
79     aSph=aGAS.Sphere();
80     aR=aSph.Radius();
81     aDp=GEOMAlgo_SurfaceTools::Distance(aP, aSph);
82     break;
83     
84   default:
85     iErr=1; // unprocessed surface type
86     break;
87   }
88   //
89   if (!iErr) {
90     aState=TopAbs_ON;
91     if (aDp>aR+aTol) {
92       aState=TopAbs_OUT;
93     }
94     else if (aDp<aR-aTol) {
95       aState=TopAbs_IN;
96     }
97   }
98   //
99   return iErr;
100 }
101 //=======================================================================
102 //function : GetState
103 //purpose  : 
104 //=======================================================================
105  Standard_Integer GEOMAlgo_SurfaceTools::GetState(const gp_Pnt& aP,
106                                                   const Handle(Geom_Surface)& aSurf,
107                                                   const Standard_Real aTol,
108                                                   TopAbs_State& aState)
109 {
110   Standard_Integer iErr;
111   GeomAdaptor_Surface aGAS;
112   //
113   aState=TopAbs_UNKNOWN;
114   aGAS.Load(aSurf);
115   //
116   iErr=GEOMAlgo_SurfaceTools::GetState(aP, aGAS, aTol, aState);
117   //
118   return iErr;
119 }
120 //=======================================================================
121 //function : ReverseState
122 //purpose  : 
123 //=======================================================================
124  TopAbs_State GEOMAlgo_SurfaceTools::ReverseState(const TopAbs_State aState)
125 {
126   TopAbs_State aRSt=aState;
127   //
128   switch (aState) {
129     case TopAbs_IN:
130      aRSt=TopAbs_OUT;
131      break;
132    case TopAbs_OUT:
133      aRSt=TopAbs_IN;
134      break;
135    default:
136      break;
137   }
138   //
139   return aRSt;
140 }
141 //=======================================================================
142 //function : Distance
143 //purpose  : 
144 //=======================================================================
145 Standard_Real GEOMAlgo_SurfaceTools::Distance(const gp_Pnt& aP, 
146                                               const gp_Sphere& aSph)
147 {
148   Standard_Real aD;
149   //
150   const gp_Pnt& aLoc=aSph.Location();
151   aD=aLoc.Distance(aP);
152   //
153   return aD;
154 }
155 //=======================================================================
156 //function : Distance
157 //purpose  : 
158 //=======================================================================
159 Standard_Real GEOMAlgo_SurfaceTools::Distance(const gp_Pnt& aP, 
160                                               const gp_Cylinder& aCyl)
161 {
162   Standard_Real aD;
163   //
164   const gp_Ax1& aAxis=aCyl.Axis();
165   gp_Lin aLin(aAxis);
166   aD=aLin.Distance(aP);
167   //
168   return aD;
169 }
170 //=======================================================================
171 //function : Distance
172 //purpose  : 
173 //=======================================================================
174 Standard_Real GEOMAlgo_SurfaceTools::Distance(const gp_Pnt& aP, 
175                                               const gp_Pln& aPL)
176 {
177   Standard_Real aD;
178   //
179   const gp_Ax3& aPos=aPL.Position();
180   const gp_Pnt& aLoc=aPos.Location ();
181   const gp_Dir& aDir=aPos.Direction();
182   //
183   aD= (aDir.X() * (aP.X() - aLoc.X()) +
184        aDir.Y() * (aP.Y() - aLoc.Y()) +
185        aDir.Z() * (aP.Z() - aLoc.Z()));
186   return aD;
187 }
188 //=======================================================================
189 //function : IsCoaxial
190 //purpose  : 
191 //=======================================================================
192 Standard_Boolean GEOMAlgo_SurfaceTools::IsCoaxial(const gp_Pnt& aP1,
193                                                   const gp_Pnt& aP2,
194                                                   const gp_Cylinder& aCyl,
195                                                   const Standard_Real aTol)
196 {
197   Standard_Boolean bRet=Standard_False;
198   Standard_Real aSM;
199   //
200   gp_Vec aV12(aP1, aP2);
201   gp_Dir aD12(aV12);
202   //
203   const gp_Ax1& aAxis=aCyl.Axis();
204   const gp_Dir& aDAxis=aAxis.Direction();
205   //
206   aSM=fabs(aD12*aDAxis);
207   if (fabs(1.-aSM) > aTol) {
208     return bRet;
209   }
210   //
211   return !bRet;
212 }
213 //=======================================================================
214 //function : IsAnalytic
215 //purpose  : 
216 //=======================================================================
217 Standard_Boolean GEOMAlgo_SurfaceTools::IsAnalytic(const Handle(Geom_Surface)& aSurf)
218 {
219   Standard_Boolean bRet;
220   GeomAbs_SurfaceType aType;
221   GeomAdaptor_Surface aGAS;
222   //
223   aGAS.Load(aSurf);
224   aType=aGAS.GetType();
225   bRet=(aType==GeomAbs_Plane || 
226         aType==GeomAbs_Cylinder ||
227         aType==GeomAbs_Sphere);
228   return bRet;
229 }
230 //=======================================================================
231 //function : IsConformState
232 //purpose  : 
233 //=======================================================================
234 Standard_Boolean GEOMAlgo_SurfaceTools::IsConformState(const TopAbs_State aST1,
235                                                        const GEOMAlgo_State aST2)
236 {
237   Standard_Boolean bRet=Standard_False;
238   //
239   switch (aST2) {
240     case GEOMAlgo_ST_IN:
241       if (aST1==TopAbs_IN) {
242         bRet=!bRet;
243       }
244       break;
245     case GEOMAlgo_ST_OUT:
246       if (aST1==TopAbs_OUT) {
247         bRet=!bRet;
248       }
249       break;
250     case GEOMAlgo_ST_ON:
251       if (aST1==TopAbs_ON) {
252         bRet=!bRet;
253       }
254       break;
255     case GEOMAlgo_ST_ONIN:
256       if (aST1==TopAbs_ON || aST1==TopAbs_IN) {
257         bRet=!bRet;
258       }
259       break;
260     case GEOMAlgo_ST_ONOUT:
261       if (aST1==TopAbs_ON || aST1==TopAbs_OUT) {
262         bRet=!bRet;
263       }
264       break;
265     default:
266       break;
267   }
268   return bRet;
269 }