Salome HOME
Merge from V7_3_BR branch 18/12/2013
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_SurfaceTools.cxx
1 // Copyright (C) 2007-2013  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.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(const gp_Pnt& aP,
52                                                   const GeomAdaptor_Surface& aGAS,
53                                                   const Standard_Real aTol,
54                                                   TopAbs_State& aState)
55 {
56   Standard_Integer    iErr  = 0;
57   GeomAbs_SurfaceType aType = aGAS.GetType();
58   IntSurf_Quadric     aQuad;
59   //
60   aState = TopAbs_UNKNOWN;
61   //
62   switch (aType) {
63   case GeomAbs_Plane:
64     aQuad.SetValue(aGAS.Plane());
65     break;
66
67   case GeomAbs_Cylinder:
68     aQuad.SetValue(aGAS.Cylinder());
69     break;
70
71   case GeomAbs_Sphere:
72     aQuad.SetValue(aGAS.Sphere());
73     break;
74
75   default:
76     iErr=1; // unprocessed surface type
77     break;
78   }
79   //
80   if (!iErr) {
81     const Standard_Real aDp = aQuad.Distance(aP);
82     //
83     aState = TopAbs_ON;
84     //
85     if (aDp > aTol) {
86       aState = TopAbs_OUT;
87     } else if (aDp < -aTol) {
88       aState = TopAbs_IN;
89     }
90   }
91   //
92   return iErr;
93 }
94 //=======================================================================
95 //function : GetState
96 //purpose  :
97 //=======================================================================
98  Standard_Integer GEOMAlgo_SurfaceTools::GetState(const gp_Pnt& aP,
99                                                   const Handle(Geom_Surface)& aSurf,
100                                                   const Standard_Real aTol,
101                                                   TopAbs_State& aState)
102 {
103   Standard_Integer iErr;
104   GeomAdaptor_Surface aGAS;
105   //
106   aState=TopAbs_UNKNOWN;
107   aGAS.Load(aSurf);
108   //
109   iErr=GEOMAlgo_SurfaceTools::GetState(aP, aGAS, aTol, aState);
110   //
111   return iErr;
112 }
113 //=======================================================================
114 //function : ReverseState
115 //purpose  :
116 //=======================================================================
117  TopAbs_State GEOMAlgo_SurfaceTools::ReverseState(const TopAbs_State aState)
118 {
119   TopAbs_State aRSt=aState;
120   //
121   switch (aState) {
122     case TopAbs_IN:
123      aRSt=TopAbs_OUT;
124      break;
125    case TopAbs_OUT:
126      aRSt=TopAbs_IN;
127      break;
128    default:
129      break;
130   }
131   //
132   return aRSt;
133 }
134 //=======================================================================
135 //function : IsCoaxial
136 //purpose  :
137 //=======================================================================
138 Standard_Boolean GEOMAlgo_SurfaceTools::IsCoaxial(const gp_Pnt& aP1,
139                                                   const gp_Pnt& aP2,
140                                                   const gp_Cylinder& aCyl,
141                                                   const Standard_Real aTol)
142 {
143   const gp_XYZ &aLoc   = aCyl.Location().XYZ();
144   const gp_Ax1 &aAxis  = aCyl.Axis();
145   const gp_XYZ &aDAxis = aAxis.Direction().XYZ();
146   gp_XYZ        aDP1   = aP1.XYZ().Subtracted(aLoc);
147   gp_XYZ        aDP2   = aP2.XYZ().Subtracted(aLoc);
148   Standard_Real aDot1  = aDP1.Dot(aDAxis);
149   Standard_Real aDot2  = aDP1.Dot(aDAxis);
150   Standard_Real aTol2  = aTol*aTol;
151
152   // Project P1 and P2 onto a plane with location aLoc and Norm aDAxis.
153   aDP1.Subtract(aDAxis.Multiplied(aDot1));
154   aDP2.Subtract(aDAxis.Multiplied(aDot2));
155
156   Standard_Real    aRadius1 = aDP1.Modulus();
157   Standard_Real    aRadius2 = aDP2.Modulus();
158   Standard_Boolean isOn     = Standard_False;
159
160   if (fabs(aRadius1 - aRadius2) <= aTol) {
161     // Check the deflection of the middle point.
162     gp_XYZ        aMidP       = 0.5*(aDP1 + aDP2);
163     Standard_Real aMidRadius1 = aMidP.Modulus();
164
165     if (fabs(aRadius1 - aRadius2) <= aTol) {
166       isOn = Standard_True;
167     }
168   }
169
170   return isOn;
171 }
172 //=======================================================================
173 //function : IsAnalytic
174 //purpose  :
175 //=======================================================================
176 Standard_Boolean GEOMAlgo_SurfaceTools::IsAnalytic(const Handle(Geom_Surface)& aSurf)
177 {
178   Standard_Boolean bRet;
179   GeomAbs_SurfaceType aType;
180   GeomAdaptor_Surface aGAS;
181   //
182   aGAS.Load(aSurf);
183   aType=aGAS.GetType();
184   bRet=(aType==GeomAbs_Plane ||
185         aType==GeomAbs_Cylinder ||
186         aType==GeomAbs_Sphere);
187   return bRet;
188 }
189 //=======================================================================
190 //function : IsConformState
191 //purpose  :
192 //=======================================================================
193 Standard_Boolean GEOMAlgo_SurfaceTools::IsConformState(const TopAbs_State aST1,
194                                                        const GEOMAlgo_State aST2)
195 {
196   Standard_Boolean bRet=Standard_False;
197   //
198   switch (aST2) {
199     case GEOMAlgo_ST_IN:
200       if (aST1==TopAbs_IN) {
201         bRet=!bRet;
202       }
203       break;
204     case GEOMAlgo_ST_OUT:
205       if (aST1==TopAbs_OUT) {
206         bRet=!bRet;
207       }
208       break;
209     case GEOMAlgo_ST_ON:
210       if (aST1==TopAbs_ON) {
211         bRet=!bRet;
212       }
213       break;
214     case GEOMAlgo_ST_ONIN:
215       if (aST1==TopAbs_ON || aST1==TopAbs_IN) {
216         bRet=!bRet;
217       }
218       break;
219     case GEOMAlgo_ST_ONOUT:
220       if (aST1==TopAbs_ON || aST1==TopAbs_OUT) {
221         bRet=!bRet;
222       }
223       break;
224     default:
225       break;
226   }
227   return bRet;
228 }