/src/contrib/geom-5.1.2.7/src/NMTDS/NMTDS_ShapesDataStructure.cpp

http://pythonocc.googlecode.com/ · C++ · 442 lines · 270 code · 9 blank · 163 comment · 34 complexity · 1ad749f852dfead26fff65da7f14b848 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: NMTDS_ShapesDataStructure.cxx
  23. // Created: Mon Dec 1 10:21:04 2003
  24. // Author: Peter KURNEV
  25. // <pkv@irinox>
  26. //
  27. #include <NMTDS_ShapesDataStructure.ixx>
  28. #include <TopoDS_Iterator.hxx>
  29. #include <TopoDS_Shape.hxx>
  30. #include <BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors.hxx>
  31. #include <NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
  32. #include <NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
  33. #include <BooleanOperations_ShapeAndInterferences.hxx>
  34. #include <NMTDS_IndexRange.hxx>
  35. //
  36. #include <BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors.hxx>
  37. #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
  38. #include <TColStd_MapOfInteger.hxx>
  39. #include <NMTDS_Tools.hxx>
  40. static
  41. void ComputeBoxExS(const Standard_Integer aIx,
  42. const NMTDS_ShapesDataStructure* pDS,
  43. Bnd_Box& aBoxEx);
  44. static
  45. void GetAllSuccessorsS(const Standard_Integer nS,
  46. const NMTDS_ShapesDataStructure* myDS,
  47. TColStd_IndexedMapOfInteger& aMA);
  48. //===========================================================================
  49. //function : NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure
  50. //purpose :
  51. //===========================================================================
  52. NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure()
  53. :
  54. BooleanOperations_ShapesDataStructure()
  55. {}
  56. //===========================================================================
  57. //function : SetCompositeShape
  58. //purpose :
  59. //===========================================================================
  60. void NMTDS_ShapesDataStructure::SetCompositeShape(const TopoDS_Shape& aS)
  61. {
  62. myCompositeShape=aS;
  63. }
  64. //===========================================================================
  65. //function : CompositeShape
  66. //purpose :
  67. //===========================================================================
  68. const TopoDS_Shape& NMTDS_ShapesDataStructure::CompositeShape()const
  69. {
  70. return myCompositeShape;
  71. }
  72. //===========================================================================
  73. //function : Ranges
  74. //purpose :
  75. //===========================================================================
  76. const NMTDS_CArray1OfIndexRange& NMTDS_ShapesDataStructure::Ranges()const
  77. {
  78. return myRanges;
  79. }
  80. // Modified to Add new methods Thu Sep 14 14:35:18 2006
  81. // Contribution of Samtech www.samcef.com BEGIN
  82. //===========================================================================
  83. //function : FillMap
  84. //purpose :
  85. //===========================================================================
  86. void NMTDS_ShapesDataStructure::FillMap
  87. (const TopoDS_Shape& aS,
  88. BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
  89. BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
  90. {
  91. Standard_Integer iX, i, j, aIndex, aNbSc, aNbS;
  92. BooleanOperations_AncestorsSeqAndSuccessorsSeq aAS;
  93. //
  94. aMSA.Add(aS, aAS);
  95. aMS.Add(aS, aAS);
  96. FillSubshapes(aS, aMSA, aMS);
  97. //
  98. aNbS=aMS.Extent();
  99. for(i=1; i<=aNbS; ++i) {
  100. TColStd_MapOfInteger aMFence;
  101. //
  102. const TopoDS_Shape& aSX=aMS.FindKey(i);
  103. iX=aMSA.FindIndex(aSX);
  104. const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS1=aMSA(iX);
  105. //
  106. aNbSc=aAS1.NumberOfSuccessors();
  107. for(j=1; j<=aNbSc; ++j) {
  108. aIndex=aAS1.GetSuccessor(j);
  109. if(aMFence.Add(aIndex)) {
  110. BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS2=aMSA.ChangeFromIndex(aIndex);
  111. aAS2.SetNewAncestor(iX);
  112. }
  113. }
  114. }
  115. }
  116. //===========================================================================
  117. //function : FillSubshapes
  118. //purpose :
  119. //===========================================================================
  120. void NMTDS_ShapesDataStructure::FillSubshapes
  121. (const TopoDS_Shape& aS,
  122. BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
  123. BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
  124. {
  125. Standard_Boolean bIsNewSubShape;
  126. Standard_Integer aIndexSubShape, aIndex;
  127. BooleanOperations_AncestorsSeqAndSuccessorsSeq aASx;
  128. //
  129. aIndex=aMSA.FindIndex(aS);
  130. BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS=aMSA.ChangeFromIndex(aIndex);
  131. //
  132. TopoDS_Iterator anIt(aS, Standard_True);
  133. for(; anIt.More(); anIt.Next()) {
  134. const TopoDS_Shape& aSubShape = anIt.Value();
  135. bIsNewSubShape = Standard_False;
  136. if(!aMSA.Contains(aSubShape)) {
  137. bIsNewSubShape=!bIsNewSubShape;
  138. aIndexSubShape=aMSA.Add(aSubShape, aASx);
  139. aMS.Add(aSubShape, aASx);
  140. }
  141. else {
  142. aIndexSubShape=aMSA.FindIndex(aSubShape);
  143. }
  144. aAS.SetNewSuccessor(aIndexSubShape);
  145. aAS.SetNewOrientation(aSubShape.Orientation());
  146. //
  147. if(bIsNewSubShape && (aSubShape.ShapeType() != TopAbs_VERTEX)) {
  148. FillSubshapes(aSubShape, aMSA, aMS);
  149. }
  150. }
  151. }
  152. // Contribution of Samtech www.samcef.com END
  153. //===========================================================================
  154. //function : Init
  155. //purpose :
  156. //===========================================================================
  157. void NMTDS_ShapesDataStructure::Init()
  158. {
  159. Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
  160. Standard_Integer iFirst, iLast;
  161. NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
  162. NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
  163. TopoDS_Iterator anIt;
  164. BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMSA;
  165. //
  166. anIt.Initialize(myCompositeShape);
  167. for (; anIt.More(); anIt.Next()) {
  168. const TopoDS_Shape& aSx=anIt.Value();
  169. BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMS;
  170. //modified by NIZNHY-PKV Tue Feb 27 17:05:47 2007f
  171. //FillMap(aSx, aMSA, aMS);
  172. //aLx.Append(aMS);
  173. //
  174. if (!aMSA.Contains(aSx)) {
  175. FillMap(aSx, aMSA, aMS);
  176. aLx.Append(aMS);
  177. }
  178. //modified by NIZNHY-PKV Tue Feb 27 17:06:03 2007t
  179. }
  180. aNbS=aMSA.Extent();
  181. //
  182. // Fill myRanges
  183. //modified by NIZNHY-PKV Tue Feb 27 17:10:07 2007f
  184. i=aLx.Extent();
  185. //modified by NIZNHY-PKV Tue Feb 27 17:10:10 2007t
  186. myRanges.Resize(i);
  187. aLit.Initialize(aLx);
  188. for (i=1; aLit.More(); aLit.Next(), ++i) {
  189. // Modified Thu Sep 14 14:35:18 2006
  190. // Contribution of Samtech www.samcef.com BEGIN
  191. const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSx=aLit.Value();
  192. aNbSx=aMSx.Extent();
  193. // Contribution of Samtech www.samcef.com END
  194. if (i==1) {
  195. iFirst=1;
  196. iLast=aNbSx;
  197. myRanges(i).SetFirst(iFirst);
  198. myRanges(i).SetLast(iLast);
  199. continue;
  200. }
  201. iFirst=myRanges(i-1).Last()+1;
  202. iLast=iFirst+aNbSx-1;
  203. myRanges(i).SetFirst(iFirst);
  204. myRanges(i).SetLast(iLast);
  205. }
  206. //
  207. myNumberOfShapesOfTheObject=aNbS;
  208. // Modified Thu Sep 14 14:35:18 2006
  209. // Contribution of Samtech www.samcef.com BEGIN
  210. //myNumberOfShapesOfTheTool=aNbS;
  211. //myLength=3*aNbS;
  212. myNumberOfShapesOfTheTool=0;
  213. myLength=2*aNbS;
  214. // Contribution of Samtech www.samcef.com END
  215. //
  216. // Allocate the whole Table
  217. myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
  218. Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
  219. // Modified Thu Sep 14 14:35:18 2006
  220. // Contribution of Samtech www.samcef.com BEGIN
  221. //
  222. // Fill the table
  223. //aShift=0;
  224. //for (i=0; i<2; ++i) {
  225. // if (i) {
  226. // aShift=aNbS;
  227. // }
  228. // for (j=1; j<=aNbS; ++j) {
  229. // const TopoDS_Shape& aSx=aMSA.FindKey(j);
  230. // const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=
  231. // aMSA.FindFromIndex(j);
  232. // InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
  233. // }
  234. //}
  235. aShift=0;
  236. for (j=1; j<=aNbS; ++j) {
  237. const TopoDS_Shape& aSx=aMSA.FindKey(j);
  238. const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aMSA.FindFromIndex(j);
  239. InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
  240. }
  241. // Contribution of Samtech www.samcef.com END
  242. //
  243. // myShapeIndexMap
  244. myShapeIndexMap.Clear();
  245. //
  246. aNbRanges=myRanges.Extent();
  247. for (i=1; i<=aNbRanges; ++i){
  248. BooleanOperations_IndexedDataMapOfShapeInteger aSIM;
  249. //
  250. const NMTDS_IndexRange& aR=myRanges(i);
  251. iFirst=aR.First();
  252. iLast =aR.Last();
  253. for (j=iFirst; j<=iLast; ++j) {
  254. const TopoDS_Shape& aS=Shape(j);
  255. aSIM.Add(aS, j);
  256. }
  257. myShapeIndexMap.Add(i, aSIM);
  258. }
  259. //
  260. // myRefEdges
  261. iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
  262. myRefEdges.Resize(iLast);
  263. for (i=1; i<=iLast; ++i) {
  264. const TopoDS_Shape& aS=Shape(i);
  265. myRefEdges(i)=0;
  266. if (aS.ShapeType()==TopAbs_EDGE) {
  267. myNbEdges++;
  268. myRefEdges(i)=myNbEdges;
  269. }
  270. }
  271. }
  272. //===========================================================================
  273. //function : ShapeRangeIndex
  274. //purpose :
  275. //===========================================================================
  276. Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex(const Standard_Integer aId)const
  277. {
  278. Standard_Boolean bFound;
  279. Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
  280. //
  281. aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
  282. aIdx=aId;
  283. if (aIdx > aNbS || aIdx < 1){
  284. return aZero;
  285. }
  286. //
  287. if (aIdx > myNumberOfShapesOfTheObject) {
  288. aIdx-=myNumberOfShapesOfTheObject;
  289. }
  290. //
  291. aNbR=myRanges.Extent();
  292. for (i=1; i<=aNbR; ++i) {
  293. const NMTDS_IndexRange& aRange=myRanges(i);
  294. bFound=aRange.IsInRange(aIdx);
  295. if (bFound) {
  296. return i;
  297. }
  298. }
  299. return aZero;
  300. }
  301. //===========================================================================
  302. //function : Rank
  303. //purpose :
  304. //===========================================================================
  305. Standard_Integer NMTDS_ShapesDataStructure::Rank(const Standard_Integer aId)const
  306. {
  307. Standard_Boolean bFound;
  308. Standard_Integer i, aNbR, aNbS, aZero=0;
  309. //
  310. aNbS=myNumberOfShapesOfTheObject;
  311. if (aId > aNbS || aId < 1){
  312. return aZero;
  313. }
  314. //
  315. aNbR=myRanges.Extent();
  316. for (i=1; i<=aNbR; ++i) {
  317. const NMTDS_IndexRange& aRange=myRanges(i);
  318. bFound=aRange.IsInRange(aId);
  319. if (bFound) {
  320. return i;
  321. }
  322. }
  323. return aZero;
  324. }
  325. //===========================================================================
  326. //function : ShapeIndex
  327. //purpose :
  328. //===========================================================================
  329. Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex(const TopoDS_Shape& aS,
  330. const Standard_Integer aRank)const
  331. {
  332. Standard_Boolean bFound;
  333. Standard_Integer aIndex=0;
  334. //
  335. bFound=myShapeIndexMap.Contains(aRank);
  336. if (!bFound) {
  337. return aIndex;
  338. }
  339. // Modified Thu Sep 14 14:35:18 2006
  340. // Contribution of Samtech www.samcef.com BEGIN
  341. //
  342. //const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=
  343. // myShapeIndexMap.FindFromKey(aRank);
  344. //
  345. //bFound=aSIM.Contains(aS);
  346. //if (!bFound) {
  347. // return aIndex;
  348. //}
  349. //
  350. //aIndex=aSIM.FindFromKey(aS);
  351. //
  352. Standard_Integer i, aNbRanks;
  353. //
  354. aNbRanks=myShapeIndexMap.Extent();
  355. for (i=1; i<=aNbRanks; ++i){
  356. const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=myShapeIndexMap.FindFromKey(i);
  357. bFound=aSIM.Contains(aS);
  358. if (bFound) {
  359. aIndex=aSIM.FindFromKey(aS);
  360. return aIndex;
  361. }
  362. }
  363. // Contribution of Samtech www.samcef.com END
  364. return aIndex;
  365. }
  366. //=======================================================================
  367. //function : ComputeBoxEx
  368. //purpose :
  369. //=======================================================================
  370. void NMTDS_ShapesDataStructure::ComputeBoxEx (const Standard_Integer aIx,
  371. Bnd_Box& aBoxEx)const
  372. {
  373. ComputeBoxExS(aIx, this, aBoxEx);
  374. }
  375. //=======================================================================
  376. //function : GetAllSuccessors
  377. //purpose :
  378. //=======================================================================
  379. void NMTDS_ShapesDataStructure::GetAllSuccessors(const Standard_Integer nS,
  380. TColStd_IndexedMapOfInteger& aMA)const
  381. {
  382. GetAllSuccessorsS(nS, this, aMA);
  383. }
  384. //=======================================================================
  385. //function : GetAllSuccessorsS
  386. //purpose :
  387. //=======================================================================
  388. void GetAllSuccessorsS(const Standard_Integer nS,
  389. const NMTDS_ShapesDataStructure* myDS,
  390. TColStd_IndexedMapOfInteger& aMA)
  391. {
  392. TopAbs_ShapeEnum aT;
  393. Standard_Integer i, nSx, aNbSuccessors, *pSuccessors;
  394. Standard_Address xSuccessors;
  395. //
  396. const TopoDS_Shape& aS=myDS->Shape(nS);
  397. aT=aS.ShapeType();
  398. if(NMTDS_Tools::HasBRep(aT)) {
  399. aMA.Add(nS);
  400. //
  401. if (aT==TopAbs_VERTEX) {
  402. return;
  403. }
  404. }
  405. //
  406. myDS->GetSuccessors(nS, xSuccessors, aNbSuccessors);
  407. pSuccessors=(Standard_Integer*)xSuccessors;
  408. for (i=0; i<aNbSuccessors; ++i) {
  409. nSx=pSuccessors[i];
  410. GetAllSuccessorsS(nSx, myDS, aMA);
  411. }
  412. }
  413. //=======================================================================
  414. // function: ComputeBoxExS
  415. // purpose:
  416. //=======================================================================
  417. void ComputeBoxExS(const Standard_Integer aIx,
  418. const NMTDS_ShapesDataStructure* pDS,
  419. Bnd_Box& aBoxEx)
  420. {
  421. Standard_Integer i, aNbS, iS;
  422. //
  423. const Bnd_Box& aBox=pDS->GetBoundingBox(aIx);
  424. aBoxEx.Add(aBox);
  425. //
  426. aNbS=pDS->NumberOfSuccessors(aIx);
  427. for (i=1; i<=aNbS; ++i) {
  428. Bnd_Box aBoxS;
  429. iS=pDS->GetSuccessor(aIx, i);
  430. ComputeBoxExS(iS, pDS, aBoxS);
  431. aBoxEx.Add(aBoxS);
  432. }
  433. }