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