Salome HOME
e0e06fff3dab7fe3f855aea8e2b1c4bd54984ee3
[modules/geom.git] / src / NMTDS / NMTDS_ShapesDataStructure.cxx
1 // File:        NMTDS_ShapesDataStructure.cxx
2 // Created:     Mon Dec  1 10:21:04 2003
3 // Author:      Peter KURNEV
4 //              <pkv@irinox>
5
6
7 #include <NMTDS_ShapesDataStructure.ixx>
8 #include <TopoDS_Iterator.hxx>
9 #include <TopoDS_Shape.hxx>
10 #include <BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors.hxx>
11 #include <NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
12 #include <NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
13 #include <BooleanOperations_ShapeAndInterferences.hxx>
14 #include <NMTDS_IndexRange.hxx>
15
16 //===========================================================================
17 //function : NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure
18 //purpose  : 
19 //===========================================================================
20   NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure()
21 :
22   BooleanOperations_ShapesDataStructure()
23 {
24   //printf(" NMTDS_ShapesDataStructure CREATE:%x\n", (int)this);
25 }
26 //modified by NIZNHY-PKV Wed Feb  2 11:45:04 2005f
27 //===========================================================================
28 //function : ~
29 //purpose  : 
30 //===========================================================================
31   NMTDS_ShapesDataStructure::~NMTDS_ShapesDataStructure()
32 {
33   //printf(" NMTDS_ShapesDataStructure DELETE:%x\n", (int)this);
34 }
35 //modified by NIZNHY-PKV Wed Feb  2 11:45:06 2005t
36 //===========================================================================
37 //function : SetCompositeShape
38 //purpose  : 
39 //===========================================================================
40   void NMTDS_ShapesDataStructure::SetCompositeShape(const TopoDS_Shape& aS)
41 {
42   myCompositeShape=aS;
43 }
44 //===========================================================================
45 //function : CompositeShape
46 //purpose  : 
47 //===========================================================================
48   const TopoDS_Shape& NMTDS_ShapesDataStructure::CompositeShape()const
49 {
50   return myCompositeShape;
51 }
52 //===========================================================================
53 //function : Ranges
54 //purpose  : 
55 //===========================================================================
56   const NMTDS_CArray1OfIndexRange& NMTDS_ShapesDataStructure::Ranges()const
57 {
58   return myRanges;
59 }
60 //===========================================================================
61 //function : Init
62 //purpose  : 
63 //===========================================================================
64   void NMTDS_ShapesDataStructure::Init()
65 {
66   Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
67   Standard_Integer iFirst, iLast;
68   NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
69   NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
70   TopoDS_Iterator anIt;
71   //
72   aNbS=0;
73   anIt.Initialize(myCompositeShape);
74   for (i=0; anIt.More(); anIt.Next(), ++i) {
75     const TopoDS_Shape& aSx=anIt.Value();
76     BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aIndDatMapShape;
77     FillIndexedMapOfShapesAncestorsAndSuccessors(aSx, aIndDatMapShape);
78     aNbSx=aIndDatMapShape.Extent();
79     aNbS+=aNbSx;
80     aLx.Append(aIndDatMapShape);
81   }
82   //
83   // Fill myRanges
84   myRanges.Resize(i);
85   aLit.Initialize(aLx);
86   for (i=1; aLit.More(); aLit.Next(), ++i) {
87     const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aM=aLit.Value();
88     aNbSx=aM.Extent();
89     if (i==1) {
90       iFirst=1;
91       iLast=aNbSx;
92       myRanges(i).SetFirst(iFirst);
93       myRanges(i).SetLast(iLast);
94       continue;
95     }
96     iFirst=myRanges(i-1).Last()+1;
97     iLast=iFirst+aNbSx-1;
98     myRanges(i).SetFirst(iFirst);
99     myRanges(i).SetLast(iLast);
100   }
101   //
102   myNumberOfShapesOfTheObject=aNbS;
103   myNumberOfShapesOfTheTool=aNbS;
104   myLength=3*aNbS;
105   //
106   // Allocate the whole Table
107   myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
108     Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
109   //
110   // Fill the table
111   for (i=0; i<2; ++i) {
112     aShift=0;
113     if (i) {
114       aShift=myNumberOfShapesOfTheObject;
115     }
116     aLit.Initialize(aLx);
117     for (; aLit.More(); aLit.Next()) {
118       const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aM=aLit.Value();
119       aNbSx=aM.Extent();
120       for (j=1; j<=aNbSx; ++j) {
121         const TopoDS_Shape& aSx=aM.FindKey(j);
122         const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aM.FindFromIndex(j);
123         InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
124       }
125       aShift+=aNbSx;
126     }
127   }
128   //
129   // myShapeIndexMap
130   myShapeIndexMap.Clear();
131   //
132   aNbRanges=myRanges.Extent();
133   for (i=1; i<=aNbRanges; ++i){
134     BooleanOperations_IndexedDataMapOfShapeInteger aSIM;
135     //
136     const NMTDS_IndexRange& aR=myRanges(i);
137     iFirst=aR.First();
138     iLast =aR.Last();
139     for (j=iFirst; j<=iLast; ++j) { 
140       const TopoDS_Shape& aS=GetShape(j);
141       aSIM.Add(aS, j);
142     }
143     myShapeIndexMap.Add(i, aSIM);
144   }
145   //
146   // myRefEdges
147   iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
148   myRefEdges.Resize(iLast);
149
150   for (i=1; i<=iLast; ++i) {
151     const TopoDS_Shape& aS=Shape(i);
152     myRefEdges(i)=0;
153     if (aS.ShapeType()==TopAbs_EDGE) {
154       myNbEdges++;
155       myRefEdges(i)=myNbEdges;
156     }
157   }
158 }
159 //===========================================================================
160 //function : ShapeRangeIndex
161 //purpose  : 
162 //===========================================================================
163   Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex(const Standard_Integer aId)const
164 {
165   Standard_Boolean bFound;
166   Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
167   //
168   aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
169   
170   aIdx=aId;
171   if (aIdx > aNbS || aIdx < 1){
172     return aZero;
173   }
174   //  
175   if (aIdx > myNumberOfShapesOfTheObject) {
176     aIdx-=myNumberOfShapesOfTheObject;
177   }
178   //
179   aNbR=myRanges.Extent();
180   for (i=1; i<=aNbR; ++i) {
181     const NMTDS_IndexRange& aRange=myRanges(i);
182     bFound=aRange.IsInRange(aIdx);
183     if (bFound) {
184      return i;
185     }
186   }
187   return aZero;
188 }
189 //===========================================================================
190 //function : Rank
191 //purpose  : 
192 //===========================================================================
193   Standard_Integer NMTDS_ShapesDataStructure::Rank(const Standard_Integer aId)const
194 {
195   Standard_Boolean bFound;
196   Standard_Integer i, aNbR, aNbS, aZero=0;
197   //
198   aNbS=myNumberOfShapesOfTheObject;
199   
200   if (aId > aNbS || aId < 1){
201     return aZero;
202   }
203   //  
204   aNbR=myRanges.Extent();
205   for (i=1; i<=aNbR; ++i) {
206     const NMTDS_IndexRange& aRange=myRanges(i);
207     bFound=aRange.IsInRange(aId);
208     if (bFound) {
209      return i;
210     }
211   }
212   return aZero;
213 }
214 //===========================================================================
215 //function : ShapeIndex
216 //purpose  : 
217 //===========================================================================
218   Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex(const TopoDS_Shape& aS,
219                                                          const Standard_Integer aRank)const
220 {
221   Standard_Boolean bFound;
222   Standard_Integer aIndex=0;
223   //
224   bFound=myShapeIndexMap.Contains(aRank);
225   if (!bFound) {
226     return aIndex;
227   }
228   //
229   const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=myShapeIndexMap.FindFromKey(aRank);
230   //
231   bFound=aSIM.Contains(aS);
232   if (!bFound) {
233     return aIndex;
234   }
235   //
236   aIndex=aSIM.FindFromKey(aS);
237   return aIndex;
238 }