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

http://pythonocc.googlecode.com/ · C++ · 463 lines · 300 code · 9 blank · 154 comment · 44 complexity · 4cbe09d31f5452982cfab53460f37627 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_GlueDetector.cxx
  23. // Created: Wed Dec 15 11:08:09 2004
  24. // Author: Peter KURNEV
  25. // <pkv@irinox>
  26. //
  27. #include <GEOMAlgo_GlueAnalyser.ixx>
  28. #include <TopoDS.hxx>
  29. #include <TopoDS_Shape.hxx>
  30. #include <TopoDS_Face.hxx>
  31. #include <TopoDS_Edge.hxx>
  32. #include <TopoDS_Compound.hxx>
  33. #include <BRep_Builder.hxx>
  34. #include <TopExp.hxx>
  35. #include <TopTools_IndexedMapOfShape.hxx>
  36. #include <TopTools_ListOfShape.hxx>
  37. #include <TopTools_ListIteratorOfListOfShape.hxx>
  38. #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
  39. #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
  40. #include <GEOMAlgo_PassKeyShape.hxx>
  41. #include <GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape.hxx>
  42. #include <GEOMAlgo_Tools.hxx>
  43. #include <GEOMAlgo_CoupleOfShapes.hxx>
  44. #include <GEOMAlgo_Gluer.hxx>
  45. #include <Bnd_HArray1OfBox.hxx>
  46. #include <Bnd_BoundSortBox.hxx>
  47. #include <TopoDS_Vertex.hxx>
  48. #include <GEOMAlgo_IndexedDataMapOfIntegerShape.hxx>
  49. #include <GEOMAlgo_IndexedDataMapOfShapeBox.hxx>
  50. #include <Bnd_Box.hxx>
  51. #include <TColStd_ListOfInteger.hxx>
  52. #include <TopTools_MapOfShape.hxx>
  53. #include <TColStd_ListIteratorOfListOfInteger.hxx>
  54. #include <BRepBndLib.hxx>
  55. //=======================================================================
  56. //function :
  57. //purpose :
  58. //=======================================================================
  59. GEOMAlgo_GlueAnalyser::GEOMAlgo_GlueAnalyser()
  60. :
  61. GEOMAlgo_Gluer()
  62. {}
  63. //=======================================================================
  64. //function : ~
  65. //purpose :
  66. //=======================================================================
  67. GEOMAlgo_GlueAnalyser::~GEOMAlgo_GlueAnalyser()
  68. {}
  69. //=======================================================================
  70. //function : HasSolidsToGlue
  71. //purpose :
  72. //=======================================================================
  73. Standard_Boolean GEOMAlgo_GlueAnalyser::HasSolidsToGlue()const
  74. {
  75. return !mySolidsToGlue.IsEmpty();
  76. }
  77. //=======================================================================
  78. //function : HasSolidsAlone
  79. //purpose :
  80. //=======================================================================
  81. Standard_Boolean GEOMAlgo_GlueAnalyser::HasSolidsAlone()const
  82. {
  83. return !mySolidsAlone.IsEmpty();
  84. }
  85. //=======================================================================
  86. //function : SolidsToGlue
  87. //purpose :
  88. //=======================================================================
  89. const GEOMAlgo_ListOfCoupleOfShapes& GEOMAlgo_GlueAnalyser::SolidsToGlue()const
  90. {
  91. return mySolidsToGlue;
  92. }
  93. //=======================================================================
  94. //function : SolidsAlone
  95. //purpose :
  96. //=======================================================================
  97. const TopTools_ListOfShape& GEOMAlgo_GlueAnalyser::SolidsAlone()const
  98. {
  99. return mySolidsAlone;
  100. }
  101. //=======================================================================
  102. //function : Perform
  103. //purpose :
  104. //=======================================================================
  105. void GEOMAlgo_GlueAnalyser::Perform()
  106. {
  107. myErrorStatus=0;
  108. myWarningStatus=0;
  109. //
  110. mySolidsToGlue.Clear();
  111. mySolidsAlone.Clear();
  112. //
  113. CheckData();
  114. if (myErrorStatus) {
  115. return;
  116. }
  117. //
  118. InnerTolerance();
  119. if (myErrorStatus) {
  120. return;
  121. }
  122. //
  123. DetectVertices();
  124. if (myErrorStatus) {
  125. return;
  126. }
  127. //
  128. DetectEdges();
  129. if (myErrorStatus) {
  130. return;
  131. }
  132. //
  133. DetectFaces();
  134. if (myErrorStatus) {
  135. return;
  136. }
  137. //
  138. DetectSolids();
  139. if (myErrorStatus) {
  140. return;
  141. }
  142. }
  143. //=======================================================================
  144. //function : DetectVertices
  145. //purpose :
  146. //=======================================================================
  147. void GEOMAlgo_GlueAnalyser::DetectVertices()
  148. {
  149. myErrorStatus=0;
  150. //
  151. Standard_Integer j, i, aNbV, aIndex, aNbVSD;
  152. TColStd_ListIteratorOfListOfInteger aIt;
  153. Handle(Bnd_HArray1OfBox) aHAB;
  154. Bnd_BoundSortBox aBSB;
  155. TopoDS_Shape aSTmp, aVF;
  156. TopoDS_Vertex aVnew;
  157. TopTools_IndexedMapOfShape aMV, aMVProcessed;
  158. TopTools_ListIteratorOfListOfShape aItS;
  159. TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
  160. GEOMAlgo_IndexedDataMapOfIntegerShape aMIS;
  161. GEOMAlgo_IndexedDataMapOfShapeBox aMSB;
  162. //
  163. TopExp::MapShapes(myShape, TopAbs_VERTEX, aMV);
  164. aNbV=aMV.Extent();
  165. if (!aNbV) {
  166. myErrorStatus=2; // no vertices in source shape
  167. return;
  168. }
  169. //
  170. aHAB=new Bnd_HArray1OfBox(1, aNbV);
  171. //
  172. for (i=1; i<=aNbV; ++i) {
  173. const TopoDS_Shape& aV=aMV(i);
  174. Bnd_Box aBox;
  175. //
  176. aBox.SetGap(myTol);
  177. BRepBndLib::Add(aV, aBox);
  178. aHAB->SetValue(i, aBox);
  179. aMIS.Add(i, aV);
  180. aMSB.Add(aV, aBox);
  181. }
  182. //
  183. aBSB.Initialize(aHAB);
  184. //
  185. for (i=1; i<=aNbV; ++i) {
  186. const TopoDS_Shape& aV=aMV(i);
  187. //
  188. if (aMVProcessed.Contains(aV)) {
  189. continue;
  190. }
  191. //
  192. const Bnd_Box& aBoxV=aMSB.FindFromKey(aV);
  193. const TColStd_ListOfInteger& aLI=aBSB.Compare(aBoxV);
  194. aNbVSD=aLI.Extent();
  195. if (!aNbVSD) {
  196. myErrorStatus=3; // it must not be
  197. return;
  198. }
  199. //
  200. // Images
  201. TopTools_ListOfShape aLVSD;
  202. //
  203. aIt.Initialize(aLI);
  204. for (j=0; aIt.More(); aIt.Next(), ++j) {
  205. aIndex=aIt.Value();
  206. const TopoDS_Shape& aVx=aMIS.FindFromKey(aIndex);
  207. if(!j) {
  208. aVF=aVx;
  209. }
  210. aLVSD.Append(aVx);
  211. aMVProcessed.Add(aVx);
  212. }
  213. myImages.Bind(aVF, aLVSD);
  214. }
  215. // Origins
  216. aItIm.Initialize(myImages);
  217. for (; aItIm.More(); aItIm.Next()) {
  218. const TopoDS_Shape& aV=aItIm.Key();
  219. const TopTools_ListOfShape& aLVSD=aItIm.Value();
  220. //
  221. aItS.Initialize(aLVSD);
  222. for (; aItS.More(); aItS.Next()) {
  223. const TopoDS_Shape& aVSD=aItS.Value();
  224. if (!myOrigins.IsBound(aVSD)) {
  225. myOrigins.Bind(aVSD, aV);
  226. }
  227. }
  228. }
  229. }
  230. //=======================================================================
  231. //function : DetectFaces
  232. //purpose :
  233. //=======================================================================
  234. void GEOMAlgo_GlueAnalyser::DetectFaces()
  235. {
  236. DetectShapes(TopAbs_FACE);
  237. }
  238. //=======================================================================
  239. //function : DetectEdges
  240. //purpose :
  241. //=======================================================================
  242. void GEOMAlgo_GlueAnalyser::DetectEdges()
  243. {
  244. DetectShapes(TopAbs_EDGE);
  245. }
  246. //=======================================================================
  247. //function : DetectShapes
  248. //purpose :
  249. //=======================================================================
  250. void GEOMAlgo_GlueAnalyser::DetectShapes(const TopAbs_ShapeEnum aType)
  251. {
  252. myErrorStatus=0;
  253. //
  254. Standard_Integer i, aNbF, aNbSDF, iErr;
  255. TopoDS_Shape aNewShape;
  256. TopTools_IndexedMapOfShape aMF;
  257. TopTools_ListIteratorOfListOfShape aItS;
  258. GEOMAlgo_PassKeyShape aPKF;
  259. GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLF;
  260. //
  261. TopExp::MapShapes(myShape, aType, aMF);
  262. //
  263. aNbF=aMF.Extent();
  264. for (i=1; i<=aNbF; ++i) {
  265. const TopoDS_Shape& aS=aMF(i);
  266. //
  267. //aPKF.Clear();//qft
  268. if (aType==TopAbs_FACE) {
  269. const TopoDS_Face& aF=TopoDS::Face(aS);
  270. FacePassKey(aF, aPKF);
  271. }
  272. else if (aType==TopAbs_EDGE) {
  273. const TopoDS_Edge& aE=TopoDS::Edge(aS);
  274. EdgePassKey(aE, aPKF);
  275. }
  276. //
  277. if (myErrorStatus) {
  278. return;
  279. }
  280. //
  281. if (aMPKLF.Contains(aPKF)) {
  282. TopTools_ListOfShape& aLSDF=aMPKLF.ChangeFromKey(aPKF);
  283. aLSDF.Append(aS);
  284. }
  285. else {
  286. TopTools_ListOfShape aLSDF;
  287. //
  288. aLSDF.Append(aS);
  289. aMPKLF.Add(aPKF, aLSDF);
  290. }
  291. }
  292. // check geometric coincidence
  293. if (myCheckGeometry) {
  294. iErr=GEOMAlgo_Tools::RefineSDShapes(aMPKLF, myTol, myContext); //XX
  295. if (iErr) {
  296. myErrorStatus=200;
  297. return;
  298. }
  299. }
  300. //
  301. // Images/Origins
  302. aNbF=aMPKLF.Extent();
  303. for (i=1; i<=aNbF; ++i) {
  304. const TopTools_ListOfShape& aLSDF=aMPKLF(i);
  305. aNbSDF=aLSDF.Extent();
  306. if (!aNbSDF) {
  307. myErrorStatus=4; // it must not be
  308. }
  309. //
  310. const TopoDS_Shape& aS1=aLSDF.First();
  311. aNewShape=aS1;
  312. //
  313. myImages.Bind(aNewShape, aLSDF);
  314. // origins
  315. aItS.Initialize(aLSDF);
  316. for (; aItS.More(); aItS.Next()) {
  317. const TopoDS_Shape& aFSD=aItS.Value();
  318. if (!myOrigins.IsBound(aFSD)) {
  319. myOrigins.Bind(aFSD, aNewShape);
  320. }
  321. }
  322. }
  323. }
  324. //=======================================================================
  325. //function : DetectSolids
  326. //purpose :
  327. //=======================================================================
  328. void GEOMAlgo_GlueAnalyser::DetectSolids()
  329. {
  330. myErrorStatus=0;
  331. //
  332. Standard_Integer i, aNbF, aNbS, aNbC, aNbX;
  333. TopoDS_Compound aCmp;
  334. BRep_Builder aBB;
  335. TopTools_IndexedDataMapOfShapeListOfShape aMFS;
  336. TopTools_IndexedMapOfShape aMx, aMS;
  337. TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItIm;
  338. GEOMAlgo_CoupleOfShapes aCS;
  339. //
  340. GEOMAlgo_IndexedDataMapOfPassKeyShapeListOfShape aMPKLS;
  341. GEOMAlgo_PassKeyShape aPKSx;
  342. //
  343. aBB.MakeCompound(aCmp);
  344. //
  345. TopExp::MapShapesAndAncestors(myShape, TopAbs_FACE, TopAbs_SOLID, aMFS);
  346. //
  347. aItIm.Initialize(myImages);
  348. for (; aItIm.More(); aItIm.Next()) {
  349. const TopoDS_Shape& aIm=aItIm.Key();
  350. if (aIm.ShapeType()!=TopAbs_FACE) {
  351. continue;
  352. }
  353. //
  354. const TopTools_ListOfShape& aLF=aItIm.Value();
  355. aNbF=aLF.Extent();
  356. if (aNbF!=2) {
  357. continue;
  358. }
  359. //
  360. TopoDS_Shape aSx[2], aFx[2];
  361. //
  362. aFx[0]=aLF.First();
  363. aFx[1]=aLF.Last();
  364. for (i=0; i<2; ++i) {
  365. if (!aMFS.Contains(aFx[i])) {
  366. continue;// it must not be so
  367. }
  368. //
  369. const TopTools_ListOfShape& aLS=aMFS.FindFromKey(aFx[i]);
  370. aNbS=aLS.Extent();
  371. if (aNbS!=1) {
  372. continue;
  373. }
  374. aSx[i]=aLS.First();
  375. }
  376. //
  377. if (aSx[0].IsNull() || aSx[1].IsNull()) {
  378. continue;
  379. }
  380. //
  381. //aPKSx.Clear();//qft
  382. //qf
  383. //aPKSx.SetIds(aSx[0], aSx[1]);
  384. aPKSx.SetShapes(aSx[0], aSx[1]);
  385. //qt
  386. //
  387. if (!aMPKLS.Contains(aPKSx)) {
  388. TopTools_ListOfShape aLSx;
  389. //
  390. aLSx.Append(aSx[0]);
  391. aLSx.Append(aSx[1]);
  392. //
  393. aMPKLS.Add(aPKSx, aLSx);
  394. }
  395. }
  396. //
  397. mySolidsToGlue.Clear();
  398. mySolidsAlone.Clear();
  399. //
  400. aNbC=aMPKLS.Extent();
  401. if (!aNbC) {
  402. return;
  403. }
  404. //
  405. for (i=1; i<=aNbC; ++i) {
  406. const TopTools_ListOfShape& aLSx=aMPKLS(i);
  407. const TopoDS_Shape& aSx1=aLSx.First();
  408. const TopoDS_Shape& aSx2=aLSx.Last();
  409. aCS.SetShape1(aSx1);
  410. aCS.SetShape2(aSx2);
  411. mySolidsToGlue.Append(aCS);
  412. //
  413. if (!aMx.Contains(aSx1)) {
  414. aBB.Add(aCmp, aSx1);
  415. aMx.Add(aSx1);
  416. }
  417. if (!aMx.Contains(aSx2)) {
  418. aBB.Add(aCmp, aSx2);
  419. aMx.Add(aSx2);
  420. }
  421. }
  422. myResult=aCmp;
  423. //
  424. // check alone solids
  425. TopExp::MapShapes(myShape, TopAbs_SOLID, aMS);
  426. //
  427. aNbX=aMx.Extent();
  428. for (i=1; i<=aNbX; ++i) {
  429. const TopoDS_Shape& aSx=aMx(i);
  430. if (!aMS.Contains(aSx)) {
  431. mySolidsAlone.Append(aSx);
  432. }
  433. }
  434. }
  435. /*
  436. // A
  437. // Make vertices
  438. aMV.Clear();
  439. aItIm.Initialize(myImages);
  440. for (; aItIm.More(); aItIm.Next()) {
  441. const TopoDS_Shape& aV=aItIm.Key();
  442. aMV.Add(aV);
  443. const TopTools_ListOfShape& aLVSD=aItIm.Value();
  444. MakeVertex(aLVSD, aVnew);//ZZ
  445. myImages.Bind(aVnew, aLVSD);
  446. }
  447. //
  448. aNbV=aMV.Extent();
  449. for (i=1; i<=aNbV; ++i) {
  450. const TopoDS_Shape& aV=aMV(i);
  451. myImages.UnBind(aV);
  452. }
  453. //
  454. */