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