Salome HOME
Fix bug 13341: geompy.SubShapeAllSortedIDs() works wrong.
[modules/geom.git] / src / NMTDS / NMTDS_ShapesDataStructure.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 // File:        NMTDS_ShapesDataStructure.cxx
21 // Created:     Mon Dec  1 10:21:04 2003
22 // Author:      Peter KURNEV
23 //              <pkv@irinox>
24
25
26 #include <NMTDS_ShapesDataStructure.ixx>
27 #include <TopoDS_Iterator.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors.hxx>
30 #include <NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
31 #include <NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
32 #include <BooleanOperations_ShapeAndInterferences.hxx>
33 #include <NMTDS_IndexRange.hxx>
34
35 //===========================================================================
36 //function : NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure
37 //purpose  : 
38 //===========================================================================
39   NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure()
40 :
41   BooleanOperations_ShapesDataStructure()
42 {
43   //printf(" NMTDS_ShapesDataStructure CREATE:%x\n", (int)this);
44 }
45 //modified by NIZNHY-PKV Wed Feb  2 11:45:04 2005f
46 //===========================================================================
47 //function : ~
48 //purpose  : 
49 //===========================================================================
50   NMTDS_ShapesDataStructure::~NMTDS_ShapesDataStructure()
51 {
52   //printf(" NMTDS_ShapesDataStructure DELETE:%x\n", (int)this);
53 }
54 //modified by NIZNHY-PKV Wed Feb  2 11:45:06 2005t
55 //===========================================================================
56 //function : SetCompositeShape
57 //purpose  : 
58 //===========================================================================
59   void NMTDS_ShapesDataStructure::SetCompositeShape(const TopoDS_Shape& aS)
60 {
61   myCompositeShape=aS;
62 }
63 //===========================================================================
64 //function : CompositeShape
65 //purpose  : 
66 //===========================================================================
67   const TopoDS_Shape& NMTDS_ShapesDataStructure::CompositeShape()const
68 {
69   return myCompositeShape;
70 }
71 //===========================================================================
72 //function : Ranges
73 //purpose  : 
74 //===========================================================================
75   const NMTDS_CArray1OfIndexRange& NMTDS_ShapesDataStructure::Ranges()const
76 {
77   return myRanges;
78 }
79 //===========================================================================
80 //function : Init
81 //purpose  : 
82 //===========================================================================
83   void NMTDS_ShapesDataStructure::Init()
84 {
85   Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
86   Standard_Integer iFirst, iLast;
87   NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
88   NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
89   TopoDS_Iterator anIt;
90   //
91   aNbS=0;
92   anIt.Initialize(myCompositeShape);
93   for (i=0; anIt.More(); anIt.Next(), ++i) {
94     const TopoDS_Shape& aSx=anIt.Value();
95     BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aIndDatMapShape;
96     FillIndexedMapOfShapesAncestorsAndSuccessors(aSx, aIndDatMapShape);
97     aNbSx=aIndDatMapShape.Extent();
98     aNbS+=aNbSx;
99     aLx.Append(aIndDatMapShape);
100   }
101   //
102   // Fill myRanges
103   myRanges.Resize(i);
104   aLit.Initialize(aLx);
105   for (i=1; aLit.More(); aLit.Next(), ++i) {
106     const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aM=aLit.Value();
107     aNbSx=aM.Extent();
108     if (i==1) {
109       iFirst=1;
110       iLast=aNbSx;
111       myRanges(i).SetFirst(iFirst);
112       myRanges(i).SetLast(iLast);
113       continue;
114     }
115     iFirst=myRanges(i-1).Last()+1;
116     iLast=iFirst+aNbSx-1;
117     myRanges(i).SetFirst(iFirst);
118     myRanges(i).SetLast(iLast);
119   }
120   //
121   myNumberOfShapesOfTheObject=aNbS;
122   myNumberOfShapesOfTheTool=aNbS;
123   myLength=3*aNbS;
124   //
125   // Allocate the whole Table
126   myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
127     Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
128   //
129   // Fill the table
130   for (i=0; i<2; ++i) {
131     aShift=0;
132     if (i) {
133       aShift=myNumberOfShapesOfTheObject;
134     }
135     aLit.Initialize(aLx);
136     for (; aLit.More(); aLit.Next()) {
137       const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aM=aLit.Value();
138       aNbSx=aM.Extent();
139       for (j=1; j<=aNbSx; ++j) {
140         const TopoDS_Shape& aSx=aM.FindKey(j);
141         const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aM.FindFromIndex(j);
142         InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
143       }
144       aShift+=aNbSx;
145     }
146   }
147   //
148   // myShapeIndexMap
149   myShapeIndexMap.Clear();
150   //
151   aNbRanges=myRanges.Extent();
152   for (i=1; i<=aNbRanges; ++i){
153     BooleanOperations_IndexedDataMapOfShapeInteger aSIM;
154     //
155     const NMTDS_IndexRange& aR=myRanges(i);
156     iFirst=aR.First();
157     iLast =aR.Last();
158     for (j=iFirst; j<=iLast; ++j) { 
159       const TopoDS_Shape& aS=GetShape(j);
160       aSIM.Add(aS, j);
161     }
162     myShapeIndexMap.Add(i, aSIM);
163   }
164   //
165   // myRefEdges
166   iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
167   myRefEdges.Resize(iLast);
168
169   for (i=1; i<=iLast; ++i) {
170     const TopoDS_Shape& aS=Shape(i);
171     myRefEdges(i)=0;
172     if (aS.ShapeType()==TopAbs_EDGE) {
173       myNbEdges++;
174       myRefEdges(i)=myNbEdges;
175     }
176   }
177 }
178 //===========================================================================
179 //function : ShapeRangeIndex
180 //purpose  : 
181 //===========================================================================
182   Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex(const Standard_Integer aId)const
183 {
184   Standard_Boolean bFound;
185   Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
186   //
187   aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
188   
189   aIdx=aId;
190   if (aIdx > aNbS || aIdx < 1){
191     return aZero;
192   }
193   //  
194   if (aIdx > myNumberOfShapesOfTheObject) {
195     aIdx-=myNumberOfShapesOfTheObject;
196   }
197   //
198   aNbR=myRanges.Extent();
199   for (i=1; i<=aNbR; ++i) {
200     const NMTDS_IndexRange& aRange=myRanges(i);
201     bFound=aRange.IsInRange(aIdx);
202     if (bFound) {
203      return i;
204     }
205   }
206   return aZero;
207 }
208 //===========================================================================
209 //function : Rank
210 //purpose  : 
211 //===========================================================================
212   Standard_Integer NMTDS_ShapesDataStructure::Rank(const Standard_Integer aId)const
213 {
214   Standard_Boolean bFound;
215   Standard_Integer i, aNbR, aNbS, aZero=0;
216   //
217   aNbS=myNumberOfShapesOfTheObject;
218   
219   if (aId > aNbS || aId < 1){
220     return aZero;
221   }
222   //  
223   aNbR=myRanges.Extent();
224   for (i=1; i<=aNbR; ++i) {
225     const NMTDS_IndexRange& aRange=myRanges(i);
226     bFound=aRange.IsInRange(aId);
227     if (bFound) {
228      return i;
229     }
230   }
231   return aZero;
232 }
233 //===========================================================================
234 //function : ShapeIndex
235 //purpose  : 
236 //===========================================================================
237   Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex(const TopoDS_Shape& aS,
238                                                          const Standard_Integer aRank)const
239 {
240   Standard_Boolean bFound;
241   Standard_Integer aIndex=0;
242   //
243   bFound=myShapeIndexMap.Contains(aRank);
244   if (!bFound) {
245     return aIndex;
246   }
247   //
248   const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=myShapeIndexMap.FindFromKey(aRank);
249   //
250   bFound=aSIM.Contains(aS);
251   if (!bFound) {
252     return aIndex;
253   }
254   //
255   aIndex=aSIM.FindFromKey(aS);
256   return aIndex;
257 }