Salome HOME
0022177: [CEA 799] RemoveExtraEdges produces non valid faces
[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   Standard_Boolean bRet=Standard_False;
144   Standard_Real aSM;
145   //
146   gp_Vec aV12(aP1, aP2);
147   gp_Dir aD12(aV12);
148   //
149   const gp_Ax1& aAxis=aCyl.Axis();
150   const gp_Dir& aDAxis=aAxis.Direction();
151   //
152   aSM=fabs(aD12*aDAxis);
153   if (fabs(1.-aSM) > aTol) {
154     return bRet;
155   }
156   //
157   return !bRet;
158 }
159 //=======================================================================
160 //function : IsAnalytic
161 //purpose  :
162 //=======================================================================
163 Standard_Boolean GEOMAlgo_SurfaceTools::IsAnalytic(const Handle(Geom_Surface)& aSurf)
164 {
165   Standard_Boolean bRet;
166   GeomAbs_SurfaceType aType;
167   GeomAdaptor_Surface aGAS;
168   //
169   aGAS.Load(aSurf);
170   aType=aGAS.GetType();
171   bRet=(aType==GeomAbs_Plane ||
172         aType==GeomAbs_Cylinder ||
173         aType==GeomAbs_Sphere);
174   return bRet;
175 }
176 //=======================================================================
177 //function : IsConformState
178 //purpose  :
179 //=======================================================================
180 Standard_Boolean GEOMAlgo_SurfaceTools::IsConformState(const TopAbs_State aST1,
181                                                        const GEOMAlgo_State aST2)
182 {
183   Standard_Boolean bRet=Standard_False;
184   //
185   switch (aST2) {
186     case GEOMAlgo_ST_IN:
187       if (aST1==TopAbs_IN) {
188         bRet=!bRet;
189       }
190       break;
191     case GEOMAlgo_ST_OUT:
192       if (aST1==TopAbs_OUT) {
193         bRet=!bRet;
194       }
195       break;
196     case GEOMAlgo_ST_ON:
197       if (aST1==TopAbs_ON) {
198         bRet=!bRet;
199       }
200       break;
201     case GEOMAlgo_ST_ONIN:
202       if (aST1==TopAbs_ON || aST1==TopAbs_IN) {
203         bRet=!bRet;
204       }
205       break;
206     case GEOMAlgo_ST_ONOUT:
207       if (aST1==TopAbs_ON || aST1==TopAbs_OUT) {
208         bRet=!bRet;
209       }
210       break;
211     default:
212       break;
213   }
214   return bRet;
215 }