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

http://pythonocc.googlecode.com/ · C++ · 476 lines · 327 code · 14 blank · 135 comment · 38 complexity · 3cbd645a1aa249e259c351ac6ff49e28 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_Tools.cxx
  23. // Created: Mon Dec 6 11:35:29 2004
  24. // Author: Peter KURNEV
  25. // <pkv@irinox>
  26. //
  27. #include <GEOMAlgo_Tools.ixx>
  28. #include <gp_Pnt.hxx>
  29. #include <gp_Pnt2d.hxx>
  30. #include <Geom_Surface.hxx>
  31. #include <Geom_Curve.hxx>
  32. #include <Geom2d_Curve.hxx>
  33. #include <GeomAdaptor_Surface.hxx>
  34. #include <GeomAPI_ProjectPointOnSurf.hxx>
  35. #include <TopAbs_ShapeEnum.hxx>
  36. #include <TopoDS.hxx>
  37. #include <TopoDS_Shape.hxx>
  38. #include <TopoDS_Edge.hxx>
  39. #include <TopoDS_Iterator.hxx>
  40. #include <TopTools_ListOfShape.hxx>
  41. #include <TopTools_ListIteratorOfListOfShape.hxx>
  42. #include <TopTools_IndexedMapOfShape.hxx>
  43. #include <BRep_Tool.hxx>
  44. #include <BRep_Builder.hxx>
  45. #include <BRepTools.hxx>
  46. #include <BOPTools_Tools2D.hxx>
  47. #include <IntTools_Context.hxx>
  48. #include <GEOMAlgo_PassKeyShape.hxx>//qft
  49. #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>//qft
  50. static
  51. void GetCount(const TopoDS_Shape& aS,
  52. Standard_Integer& iCnt);
  53. //=======================================================================
  54. //function : IsCompositeShape
  55. //purpose :
  56. //=======================================================================
  57. Standard_Boolean GEOMAlgo_Tools::IsCompositeShape(const TopoDS_Shape& aS)
  58. {
  59. Standard_Boolean bRet;
  60. Standard_Integer iCnt;
  61. TopoDS_Iterator aIt;
  62. //
  63. iCnt=0;
  64. GetCount(aS, iCnt);
  65. bRet=(iCnt>1);
  66. //
  67. return bRet;
  68. }
  69. //=======================================================================
  70. //function : GetCount
  71. //purpose :
  72. //=======================================================================
  73. void GetCount(const TopoDS_Shape& aS,
  74. Standard_Integer& iCnt)
  75. {
  76. TopoDS_Iterator aIt;
  77. TopAbs_ShapeEnum aTS;
  78. //
  79. aTS=aS.ShapeType();
  80. //
  81. if (aTS==TopAbs_SHAPE) {
  82. return;
  83. }
  84. if (aTS!=TopAbs_COMPOUND) {
  85. ++iCnt;
  86. return;
  87. }
  88. //
  89. aIt.Initialize(aS);
  90. for (; aIt.More(); aIt.Next()) {
  91. const TopoDS_Shape& aSx=aIt.Value();
  92. GetCount(aSx, iCnt);
  93. }
  94. }
  95. //=======================================================================
  96. //function : RefineSDShapes
  97. //purpose :
  98. //=======================================================================
  99. Standard_Integer GEOMAlgo_Tools::RefineSDShapes(GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape& aMPKLE,
  100. const Standard_Real aTol,
  101. IntTools_Context& aCtx)
  102. {
  103. Standard_Integer i, aNbE, iErr, j, aNbEE, aNbToAdd;
  104. TopTools_IndexedDataMapOfShapeListOfShape aMEE, aMSDE, aMEToAdd;
  105. //
  106. iErr=1;
  107. //
  108. aNbE=aMPKLE.Extent();
  109. for (i=1; i<=aNbE; ++i) {
  110. TopTools_ListOfShape& aLSDE=aMPKLE.ChangeFromIndex(i);
  111. //
  112. aMEE.Clear();
  113. iErr=GEOMAlgo_Tools::FindSDShapes(aLSDE, aTol, aMEE, aCtx);
  114. if (iErr) {
  115. return iErr;
  116. }
  117. //
  118. aNbEE=aMEE.Extent();
  119. if (aNbEE==1) {
  120. continue; // nothing to do
  121. }
  122. //
  123. for (j=1; j<=aNbEE; ++j) {
  124. TopTools_ListOfShape& aLEE=aMEE.ChangeFromIndex(j);
  125. //
  126. if (j==1) {
  127. aLSDE.Clear();
  128. aLSDE.Append(aLEE);
  129. }
  130. else {
  131. const TopoDS_Shape& aE1=aLEE.First();
  132. aMEToAdd.Add(aE1, aLEE);
  133. }
  134. }
  135. }
  136. //
  137. aNbToAdd=aMEToAdd.Extent();
  138. if (!aNbToAdd) {
  139. return aNbToAdd;
  140. }
  141. //
  142. for (i=1; i<=aNbToAdd; ++i) {
  143. GEOMAlgo_PassKeyShape aPKE1;
  144. //
  145. const TopoDS_Shape& aE1=aMEToAdd.FindKey(i);
  146. const TopTools_ListOfShape& aLE=aMEToAdd(i);
  147. //
  148. //qf
  149. //aPKE1.SetIds(aE1);
  150. aPKE1.SetShapes(aE1);
  151. //qt
  152. aMPKLE.Add(aPKE1, aLE);
  153. }
  154. //
  155. return 0;
  156. }
  157. //=======================================================================
  158. //function : FindSDShapes
  159. //purpose :
  160. //=======================================================================
  161. Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopTools_ListOfShape& aLE,
  162. const Standard_Real aTol,
  163. TopTools_IndexedDataMapOfShapeListOfShape& aMEE,
  164. IntTools_Context& aCtx)
  165. {
  166. Standard_Integer aNbE, aNbEProcessed, aNbESD, iErr;
  167. TopTools_ListOfShape aLESD;
  168. TopTools_ListIteratorOfListOfShape aIt, aIt1;
  169. TopTools_IndexedMapOfShape aMProcessed;
  170. TopAbs_ShapeEnum aType;
  171. //
  172. aNbE=aLE.Extent();
  173. if (!aNbE) {
  174. return 3; // Err
  175. }
  176. //modified by NIZNHY-PKV Thu Dec 30 10:56:52 2004 f
  177. if (aNbE==1) {
  178. return 0; // Nothing to do
  179. }
  180. //modified by NIZNHY-PKV Thu Dec 30 10:56:56 2004 t
  181. //
  182. while(1) {
  183. aNbEProcessed=aMProcessed.Extent();
  184. if (aNbEProcessed==aNbE) {
  185. break;
  186. }
  187. //
  188. aIt.Initialize(aLE);
  189. for (; aIt.More(); aIt.Next()) {
  190. const TopoDS_Shape& aS=aIt.Value();
  191. //
  192. if (aMProcessed.Contains(aS)) {
  193. continue;
  194. }
  195. //
  196. //modified by NIZNHY-PKV Thu Dec 30 10:57:01 2004 f
  197. aType=aS.ShapeType();
  198. if (aType==TopAbs_EDGE) {
  199. const TopoDS_Edge& aE=TopoDS::Edge(aS);
  200. if (BRep_Tool::Degenerated(aE)) {
  201. aMProcessed.Add(aE);
  202. continue;
  203. }
  204. }
  205. //modified by NIZNHY-PKV Thu Dec 30 10:57:03 2004 t
  206. //
  207. aLESD.Clear();
  208. iErr=GEOMAlgo_Tools::FindSDShapes(aS, aLE, aTol, aLESD, aCtx);
  209. if (iErr) {
  210. return 2; // Err
  211. }
  212. //
  213. aNbESD=aLESD.Extent();
  214. if (!aNbESD) {
  215. return 1; // Err
  216. }
  217. //
  218. aMEE.Add(aS, aLESD);
  219. //
  220. aIt1.Initialize(aLESD);
  221. for (; aIt1.More(); aIt1.Next()) {
  222. const TopoDS_Shape& aE1=aIt1.Value();
  223. aMProcessed.Add(aE1);
  224. }
  225. }
  226. }
  227. return 0;
  228. }
  229. //=======================================================================
  230. //function : FindSDShapes
  231. //purpose :
  232. //=======================================================================
  233. Standard_Integer GEOMAlgo_Tools::FindSDShapes(const TopoDS_Shape& aE1,
  234. const TopTools_ListOfShape& aLE,
  235. const Standard_Real aTol,
  236. TopTools_ListOfShape& aLESD,
  237. IntTools_Context& aCtx)
  238. {
  239. Standard_Boolean bIsDone;
  240. Standard_Real aTol2, aD2;
  241. gp_Pnt aP1, aP2;
  242. TopTools_ListIteratorOfListOfShape aIt;
  243. //
  244. aTol2=aTol*aTol;
  245. GEOMAlgo_Tools::PointOnShape(aE1, aP1);
  246. //
  247. aIt.Initialize(aLE);
  248. for (; aIt.More(); aIt.Next()) {
  249. const TopoDS_Shape& aE2=aIt.Value();
  250. if (aE2.IsSame(aE1)) {
  251. aLESD.Append(aE2);
  252. }
  253. else {
  254. bIsDone=GEOMAlgo_Tools::ProjectPointOnShape(aP1, aE2, aP2, aCtx);
  255. if (!bIsDone) {
  256. //return 1;
  257. continue; // jfa BUG 20361
  258. }
  259. aD2=aP1.SquareDistance(aP2);
  260. if(aD2<aTol2) {
  261. aLESD.Append(aE2);
  262. }
  263. }
  264. }
  265. return 0;
  266. }
  267. //=======================================================================
  268. //function : ProjectPointOnShape
  269. //purpose :
  270. //=======================================================================
  271. Standard_Boolean GEOMAlgo_Tools::ProjectPointOnShape(const gp_Pnt& aP1,
  272. const TopoDS_Shape& aS,
  273. gp_Pnt& aP2,
  274. IntTools_Context& aCtx)
  275. {
  276. Standard_Boolean bIsDone = Standard_False;
  277. Standard_Real aT2;
  278. TopAbs_ShapeEnum aType;
  279. //
  280. aType = aS.ShapeType();
  281. switch (aType)
  282. {
  283. case TopAbs_EDGE:
  284. {
  285. const TopoDS_Edge& aE2 = TopoDS::Edge(aS);
  286. //
  287. if (BRep_Tool::Degenerated(aE2)) { // jfa
  288. return Standard_True;
  289. }
  290. else {
  291. Standard_Real f, l;
  292. Handle(Geom_Curve) aC3D = BRep_Tool::Curve (aE2, f, l);
  293. if (aC3D.IsNull()) {
  294. return Standard_True;
  295. }
  296. bIsDone = aCtx.ProjectPointOnEdge(aP1, aE2, aT2);
  297. }
  298. if (!bIsDone) {
  299. return bIsDone;
  300. }
  301. //
  302. GEOMAlgo_Tools::PointOnEdge(aE2, aT2, aP2);
  303. }
  304. break;
  305. //
  306. case TopAbs_FACE:
  307. {
  308. const TopoDS_Face& aF2 = TopoDS::Face(aS);
  309. GeomAPI_ProjectPointOnSurf& aProj = aCtx.ProjPS(aF2);
  310. //
  311. aProj.Perform(aP1);
  312. bIsDone = aProj.IsDone();
  313. if (!bIsDone) {
  314. return bIsDone;
  315. }
  316. //
  317. aP2 = aProj.NearestPoint();
  318. }
  319. break;
  320. //
  321. default:
  322. break; // Err
  323. }
  324. return bIsDone;
  325. }
  326. //=======================================================================
  327. //function : PointOnShape
  328. //purpose :
  329. //=======================================================================
  330. void GEOMAlgo_Tools::PointOnShape(const TopoDS_Shape& aS,
  331. gp_Pnt& aP3D)
  332. {
  333. TopAbs_ShapeEnum aType;
  334. //
  335. aP3D.SetCoord(99.,99.,99.);
  336. aType=aS.ShapeType();
  337. switch(aType) {
  338. case TopAbs_EDGE: {
  339. const TopoDS_Edge& aE=TopoDS::Edge(aS);
  340. GEOMAlgo_Tools::PointOnEdge(aE, aP3D);
  341. }
  342. break;
  343. //
  344. case TopAbs_FACE: {
  345. const TopoDS_Face& aF=TopoDS::Face(aS);
  346. GEOMAlgo_Tools::PointOnFace(aF, aP3D);
  347. }
  348. break;
  349. //
  350. default:
  351. break; // Err
  352. }
  353. }
  354. //=======================================================================
  355. //function : PointOnFace
  356. //purpose :
  357. //=======================================================================
  358. void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
  359. gp_Pnt& aP3D)
  360. {
  361. Standard_Real aU, aV, aUMin, aUMax, aVMin, aVMax;
  362. //
  363. BRepTools::UVBounds(aF, aUMin, aUMax, aVMin, aVMax);
  364. //
  365. aU=BOPTools_Tools2D::IntermediatePoint(aUMin, aUMax);
  366. aV=BOPTools_Tools2D::IntermediatePoint(aVMin, aVMax);
  367. //
  368. GEOMAlgo_Tools::PointOnFace(aF, aU, aV, aP3D);
  369. }
  370. //=======================================================================
  371. //function : PointOnFace
  372. //purpose :
  373. //=======================================================================
  374. void GEOMAlgo_Tools::PointOnFace(const TopoDS_Face& aF,
  375. const Standard_Real aU,
  376. const Standard_Real aV,
  377. gp_Pnt& aP3D)
  378. {
  379. Handle(Geom_Surface) aS;
  380. //
  381. aS=BRep_Tool::Surface(aF);
  382. aS->D0(aU, aV, aP3D);
  383. }
  384. //=======================================================================
  385. //function : PointOnEdge
  386. //purpose :
  387. //=======================================================================
  388. void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
  389. gp_Pnt& aP3D)
  390. {
  391. Standard_Real aTx, aT1, aT2;
  392. //
  393. BRep_Tool::Curve(aE, aT1, aT2);
  394. aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
  395. GEOMAlgo_Tools::PointOnEdge(aE, aTx, aP3D);
  396. }
  397. //=======================================================================
  398. //function : PointOnEdge
  399. //purpose :
  400. //=======================================================================
  401. void GEOMAlgo_Tools::PointOnEdge(const TopoDS_Edge& aE,
  402. const Standard_Real aT,
  403. gp_Pnt& aP3D)
  404. {
  405. Standard_Real aT1, aT2;
  406. Handle(Geom_Curve) aC3D;
  407. //
  408. aC3D=BRep_Tool::Curve(aE, aT1, aT2);
  409. aC3D->D0(aT, aP3D);
  410. }
  411. //=======================================================================
  412. //function : RefinePCurveForEdgeOnFace
  413. //purpose :
  414. //=======================================================================
  415. void GEOMAlgo_Tools::RefinePCurveForEdgeOnFace(const TopoDS_Edge& aE,
  416. const TopoDS_Face& aF,
  417. const Standard_Real aUMin,
  418. const Standard_Real aUMax)
  419. {
  420. Standard_Real aT1, aT2, aTx, aUx, aTol, aTwoPI;
  421. gp_Pnt2d aP2D;
  422. Handle(Geom_Surface) aS;
  423. Handle(Geom2d_Curve) aC2D;
  424. BRep_Builder aBB;
  425. //
  426. aTwoPI=PI+PI;
  427. //
  428. aC2D=BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
  429. if (!aC2D.IsNull()) {
  430. if (BRep_Tool::IsClosed(aE, aF)) {
  431. return;
  432. }
  433. aTx=BOPTools_Tools2D::IntermediatePoint(aT1, aT2);
  434. aC2D->D0(aTx, aP2D);
  435. aUx=aP2D.X();
  436. if (aUx < aUMin || aUx > aUMax) {
  437. // need to rebuild
  438. Handle(Geom2d_Curve) aC2Dx;
  439. //
  440. aTol=BRep_Tool::Tolerance(aE);
  441. aBB.UpdateEdge(aE, aC2Dx, aF, aTol);
  442. }
  443. }
  444. }
  445. //=======================================================================
  446. //function : IsUPeriodic
  447. //purpose :
  448. //=======================================================================
  449. Standard_Boolean GEOMAlgo_Tools::IsUPeriodic(const Handle(Geom_Surface) &aS)
  450. {
  451. Standard_Boolean bRet;
  452. GeomAbs_SurfaceType aType;
  453. GeomAdaptor_Surface aGAS;
  454. //
  455. aGAS.Load(aS);
  456. aType=aGAS.GetType();
  457. bRet=(aType==GeomAbs_Cylinder||
  458. aType==GeomAbs_Cone ||
  459. aType==GeomAbs_Sphere);
  460. //
  461. return bRet;
  462. }