/src/contrib/geom-5.1.2.7/src/GEOMAlgo/GEOMAlgo_SurfaceTools.cpp

http://pythonocc.googlecode.com/ · C++ · 269 lines · 178 code · 8 blank · 83 comment · 16 complexity · 7a5cb14edc19baaa92e05beb2f5efdab MD5 · raw file

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