Salome HOME
6388a1655a1fc5187ee1adf998f806969f0a3f98
[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 //===========================================================================
25 //function : SetCompositeShape
26 //purpose  : 
27 //===========================================================================
28   void NMTDS_ShapesDataStructure::SetCompositeShape(const TopoDS_Shape& aS)
29 {
30   myCompositeShape=aS;
31 }
32 //===========================================================================
33 //function : CompositeShape
34 //purpose  : 
35 //===========================================================================
36   const TopoDS_Shape& NMTDS_ShapesDataStructure::CompositeShape()const
37 {
38   return myCompositeShape;
39 }
40 //===========================================================================
41 //function : Ranges
42 //purpose  : 
43 //===========================================================================
44   const NMTDS_CArray1OfIndexRange& NMTDS_ShapesDataStructure::Ranges()const
45 {
46   return myRanges;
47 }
48 //===========================================================================
49 //function : Init
50 //purpose  : 
51 //===========================================================================
52   void NMTDS_ShapesDataStructure::Init()
53 {
54   Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
55   Standard_Integer iFirst, iLast;
56   NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
57   NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
58   TopoDS_Iterator anIt;
59   //
60   aNbS=0;
61   anIt.Initialize(myCompositeShape);
62   for (i=0; anIt.More(); anIt.Next(), ++i) {
63     const TopoDS_Shape& aSx=anIt.Value();
64     BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aIndDatMapShape;
65     FillIndexedMapOfShapesAncestorsAndSuccessors(aSx, aIndDatMapShape);
66     aNbSx=aIndDatMapShape.Extent();
67     aNbS+=aNbSx;
68     aLx.Append(aIndDatMapShape);
69   }
70   //
71   // Fill myRanges
72   myRanges.Resize(i);
73   aLit.Initialize(aLx);
74   for (i=1; aLit.More(); aLit.Next(), ++i) {
75     const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aM=aLit.Value();
76     aNbSx=aM.Extent();
77     if (i==1) {
78       iFirst=1;
79       iLast=aNbSx;
80       myRanges(i).SetFirst(iFirst);
81       myRanges(i).SetLast(iLast);
82       continue;
83     }
84     iFirst=myRanges(i-1).Last()+1;
85     iLast=iFirst+aNbSx-1;
86     myRanges(i).SetFirst(iFirst);
87     myRanges(i).SetLast(iLast);
88   }
89   //
90   myNumberOfShapesOfTheObject=aNbS;
91   myNumberOfShapesOfTheTool=aNbS;
92   myLength=3*aNbS;
93   //
94   // Allocate the whole Table
95   myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
96     Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
97   //
98   // Fill the table
99   for (i=0; i<2; ++i) {
100     aShift=0;
101     if (i) {
102       aShift=myNumberOfShapesOfTheObject;
103     }
104     aLit.Initialize(aLx);
105     for (; aLit.More(); aLit.Next()) {
106       const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aM=aLit.Value();
107       aNbSx=aM.Extent();
108       for (j=1; j<=aNbSx; ++j) {
109         const TopoDS_Shape& aSx=aM.FindKey(j);
110         const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aM.FindFromIndex(j);
111         InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
112       }
113       aShift+=aNbSx;
114     }
115   }
116   //
117   // myShapeIndexMap
118   myShapeIndexMap.Clear();
119   //
120   aNbRanges=myRanges.Extent();
121   for (i=1; i<=aNbRanges; ++i){
122     BooleanOperations_IndexedDataMapOfShapeInteger aSIM;
123     //
124     const NMTDS_IndexRange& aR=myRanges(i);
125     iFirst=aR.First();
126     iLast =aR.Last();
127     for (j=iFirst; j<=iLast; ++j) { 
128       const TopoDS_Shape& aS=GetShape(j);
129       aSIM.Add(aS, j);
130     }
131     myShapeIndexMap.Add(i, aSIM);
132   }
133   //
134   // myRefEdges
135   iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
136   myRefEdges.Resize(iLast);
137
138   for (i=1; i<=iLast; ++i) {
139     const TopoDS_Shape& aS=Shape(i);
140     myRefEdges(i)=0;
141     if (aS.ShapeType()==TopAbs_EDGE) {
142       myNbEdges++;
143       myRefEdges(i)=myNbEdges;
144     }
145   }
146 }
147 //===========================================================================
148 //function : ShapeRangeIndex
149 //purpose  : 
150 //===========================================================================
151   Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex(const Standard_Integer aId)const
152 {
153   Standard_Boolean bFound;
154   Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
155   //
156   aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
157   
158   aIdx=aId;
159   if (aIdx > aNbS || aIdx < 1){
160     return aZero;
161   }
162   //  
163   if (aIdx > myNumberOfShapesOfTheObject) {
164     aIdx-=myNumberOfShapesOfTheObject;
165   }
166   //
167   aNbR=myRanges.Extent();
168   for (i=1; i<=aNbR; ++i) {
169     const NMTDS_IndexRange& aRange=myRanges(i);
170     bFound=aRange.IsInRange(aIdx);
171     if (bFound) {
172      return i;
173     }
174   }
175   return aZero;
176 }
177 //===========================================================================
178 //function : Rank
179 //purpose  : 
180 //===========================================================================
181   Standard_Integer NMTDS_ShapesDataStructure::Rank(const Standard_Integer aId)const
182 {
183   Standard_Boolean bFound;
184   Standard_Integer i, aNbR, aNbS, aZero=0;
185   //
186   aNbS=myNumberOfShapesOfTheObject;
187   
188   if (aId > aNbS || aId < 1){
189     return aZero;
190   }
191   //  
192   aNbR=myRanges.Extent();
193   for (i=1; i<=aNbR; ++i) {
194     const NMTDS_IndexRange& aRange=myRanges(i);
195     bFound=aRange.IsInRange(aId);
196     if (bFound) {
197      return i;
198     }
199   }
200   return aZero;
201 }
202 //===========================================================================
203 //function : ShapeIndex
204 //purpose  : 
205 //===========================================================================
206   Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex(const TopoDS_Shape& aS,
207                                                          const Standard_Integer aRank)const
208 {
209   Standard_Boolean bFound;
210   Standard_Integer aIndex=0;
211   //
212   bFound=myShapeIndexMap.Contains(aRank);
213   if (!bFound) {
214     return aIndex;
215   }
216   //
217   const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=myShapeIndexMap.FindFromKey(aRank);
218   //
219   bFound=aSIM.Contains(aS);
220   if (!bFound) {
221     return aIndex;
222   }
223   //
224   aIndex=aSIM.FindFromKey(aS);
225   return aIndex;
226 }