Salome HOME
PAL8395: Improve CheckCompoundOfBlocks and RemoveExtraEdges. Improvement done by...
[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 <gp_Pln.hxx>
10 #include <gp_Cylinder.hxx>
11 #include <gp_Sphere.hxx>
12 #include <gp_Ax1.hxx>
13 #include <gp_Lin.hxx>
14 #include <gp_Ax3.hxx>
15 #include <gp_Dir.hxx>
16
17 #include <GeomAbs_SurfaceType.hxx>
18 #include <GeomAdaptor_Surface.hxx>
19
20 //=======================================================================
21 //function : GetState
22 //purpose  : 
23 //=======================================================================
24  Standard_Integer GEOMAlgo_SurfaceTools::GetState(const gp_Pnt& aP,
25                                                   const Handle(Geom_Surface)& aSurf,
26                                                   const Standard_Real aTol,
27                                                   TopAbs_State& aState)
28 {
29   Standard_Integer iErr;
30   Standard_Real aDp, aR;
31   GeomAbs_SurfaceType aType;
32   GeomAdaptor_Surface aGAS;
33   gp_Sphere aSph;
34   gp_Cylinder aCyl;
35   gp_Pln aPln;
36   //
37   iErr=0;
38   aState=TopAbs_UNKNOWN;
39   aGAS.Load(aSurf);
40   //
41   aType=aGAS.GetType();
42   switch (aType) {
43   case GeomAbs_Plane:
44     aPln=aGAS.Plane();
45     aR=0.;
46     aDp=GEOMAlgo_SurfaceTools::Distance(aP, aPln);
47     break;
48   
49   case GeomAbs_Cylinder: 
50     aCyl=aGAS.Cylinder();
51     aR=aCyl.Radius();
52     aDp=GEOMAlgo_SurfaceTools::Distance(aP, aCyl);
53     break; 
54
55   case GeomAbs_Sphere: 
56     aSph=aGAS.Sphere();
57     aR=aSph.Radius();
58     aDp=GEOMAlgo_SurfaceTools::Distance(aP, aSph);
59     break;
60     
61   default:
62     iErr=1; // unprocessed surface type
63     break;
64   }
65   //
66   if (!iErr) {
67     aState=TopAbs_ON;
68     if (aDp>aR+aTol) {
69       aState=TopAbs_OUT;
70     }
71     else if (aDp<aR-aTol) {
72       aState=TopAbs_IN;
73     }
74   }
75   //
76   return iErr;
77 }
78 //=======================================================================
79 //function : ReverseState
80 //purpose  : 
81 //=======================================================================
82  TopAbs_State GEOMAlgo_SurfaceTools::ReverseState(const TopAbs_State aState)
83 {
84   TopAbs_State aRSt=aState;
85   //
86   switch (aState) {
87     case TopAbs_IN:
88      aRSt=TopAbs_OUT;
89      break;
90    case TopAbs_OUT:
91      aRSt=TopAbs_IN;
92      break;
93    default:
94      break;
95   }
96   //
97   return aRSt;
98 }
99 //=======================================================================
100 //function : Distance
101 //purpose  : 
102 //=======================================================================
103 Standard_Real GEOMAlgo_SurfaceTools::Distance(const gp_Pnt& aP, 
104                                               const gp_Sphere& aSph)
105 {
106   Standard_Real aD, aR;
107   //
108   aR=aSph.Radius();
109   const gp_Pnt& aLoc=aSph.Location();
110   aD=aLoc.Distance(aP);
111   //
112   return aD;
113 }
114 //=======================================================================
115 //function : Distance
116 //purpose  : 
117 //=======================================================================
118 Standard_Real GEOMAlgo_SurfaceTools::Distance(const gp_Pnt& aP, 
119                                               const gp_Cylinder& aCyl)
120 {
121   Standard_Real aD, aR;
122   //
123   aR=aCyl.Radius();
124   const gp_Ax1& aAxis=aCyl.Axis();
125   gp_Lin aLin(aAxis);
126   aD=aLin.Distance(aP);
127   //
128   return aD;
129 }
130 //=======================================================================
131 //function : Distance
132 //purpose  : 
133 //=======================================================================
134 Standard_Real GEOMAlgo_SurfaceTools::Distance(const gp_Pnt& aP, 
135                                               const gp_Pln& aPL)
136 {
137   Standard_Real aD;
138   //
139   const gp_Ax3& aPos=aPL.Position();
140   const gp_Pnt& aLoc=aPos.Location ();
141   const gp_Dir& aDir=aPos.Direction();
142   //
143   aD= (aDir.X() * (aP.X() - aLoc.X()) +
144        aDir.Y() * (aP.Y() - aLoc.Y()) +
145        aDir.Z() * (aP.Z() - aLoc.Z()));
146   return aD;
147 }
148 //=======================================================================
149 //function : IsAnalytic
150 //purpose  : 
151 //=======================================================================
152 Standard_Boolean GEOMAlgo_SurfaceTools::IsAnalytic(const Handle(Geom_Surface)& aSurf)
153 {
154   Standard_Boolean bRet;
155   GeomAbs_SurfaceType aType;
156   GeomAdaptor_Surface aGAS;
157   //
158   aGAS.Load(aSurf);
159   aType=aGAS.GetType();
160   bRet=(aType==GeomAbs_Plane || 
161         aType==GeomAbs_Cylinder ||
162         aType==GeomAbs_Sphere);
163   return bRet;
164 }
165 //=======================================================================
166 //function : IsConformState
167 //purpose  : 
168 //=======================================================================
169 Standard_Boolean GEOMAlgo_SurfaceTools::IsConformState(const TopAbs_State aST1,
170                                                        const GEOMAlgo_State aST2)
171 {
172   Standard_Boolean bRet=Standard_False;
173   //
174   switch (aST2) {
175     case GEOMAlgo_ST_IN:
176       if (aST1==TopAbs_IN) {
177         bRet=!bRet;
178       }
179       break;
180     case GEOMAlgo_ST_OUT:
181       if (aST1==TopAbs_OUT) {
182         bRet=!bRet;
183       }
184       break;
185     case GEOMAlgo_ST_ON:
186       if (aST1==TopAbs_ON) {
187         bRet=!bRet;
188       }
189       break;
190     case GEOMAlgo_ST_ONIN:
191       if (aST1==TopAbs_ON || aST1==TopAbs_IN) {
192         bRet=!bRet;
193       }
194       break;
195     case GEOMAlgo_ST_ONOUT:
196       if (aST1==TopAbs_ON || aST1==TopAbs_OUT) {
197         bRet=!bRet;
198       }
199       break;
200     default:
201       break;
202   }
203   return bRet;
204 }