Salome HOME
*** empty log message ***
[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
28 #include <TColStd_MapOfInteger.hxx>
29
30 #include <TopoDS_Iterator.hxx>
31 #include <TopoDS_Shape.hxx>
32
33 #include <BooleanOperations_ShapeAndInterferences.hxx>
34 #include <BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors.hxx>
35 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
36
37 #include <NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
38 #include <NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors.hxx>
39 #include <NMTDS_IndexRange.hxx>
40
41 //===========================================================================
42 //function : NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure
43 //purpose  : 
44 //===========================================================================
45   NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure()
46 :
47   BooleanOperations_ShapesDataStructure()
48 {}
49 //===========================================================================
50 //function : SetCompositeShape
51 //purpose  : 
52 //===========================================================================
53   void NMTDS_ShapesDataStructure::SetCompositeShape(const TopoDS_Shape& aS)
54 {
55   myCompositeShape=aS;
56 }
57 //===========================================================================
58 //function : CompositeShape
59 //purpose  : 
60 //===========================================================================
61   const TopoDS_Shape& NMTDS_ShapesDataStructure::CompositeShape()const
62 {
63   return myCompositeShape;
64 }
65 //===========================================================================
66 //function : Ranges
67 //purpose  : 
68 //===========================================================================
69   const NMTDS_CArray1OfIndexRange& NMTDS_ShapesDataStructure::Ranges()const
70 {
71   return myRanges;
72 }
73 // Modified to Add new methods Thu Sep 14 14:35:18 2006 
74 // Contribution of Samtech www.samcef.com BEGIN
75 //===========================================================================
76 //function : FillMap
77 //purpose  : 
78 //===========================================================================
79   void NMTDS_ShapesDataStructure::FillMap
80   (const TopoDS_Shape& aS,
81    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
82    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
83 {
84   Standard_Integer iX, i, j, aIndex, aNbSc, aNbS;
85   BooleanOperations_AncestorsSeqAndSuccessorsSeq aAS;
86   //
87   aMSA.Add(aS, aAS);
88   aMS.Add(aS, aAS);
89   FillSubshapes(aS, aMSA, aMS);
90   //
91   aNbS=aMS.Extent();
92   for(i=1; i<=aNbS; ++i) {
93     TColStd_MapOfInteger aMFence;
94     //
95     const TopoDS_Shape& aSX=aMS.FindKey(i);
96     iX=aMSA.FindIndex(aSX);
97     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS1=aMSA(iX);
98     //
99     aNbSc=aAS1.NumberOfSuccessors();
100     for(j=1; j<=aNbSc; ++j) {
101       aIndex=aAS1.GetSuccessor(j);
102       if(aMFence.Add(aIndex)) {
103         BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS2=aMSA.ChangeFromIndex(aIndex);
104         aAS2.SetNewAncestor(iX);
105       }
106     }
107   }
108 }
109 //===========================================================================
110 //function : FillSubshapes
111 //purpose  : 
112 //===========================================================================
113   void NMTDS_ShapesDataStructure::FillSubshapes
114   (const TopoDS_Shape& aS,
115    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
116    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
117 {
118   Standard_Boolean bIsNewSubShape;
119   Standard_Integer aIndexSubShape, aIndex;
120   BooleanOperations_AncestorsSeqAndSuccessorsSeq aASx;
121   //
122   aIndex=aMSA.FindIndex(aS);
123   BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS=aMSA.ChangeFromIndex(aIndex);
124   //
125   TopoDS_Iterator anIt(aS, Standard_True);
126   for(; anIt.More(); anIt.Next()) {
127     const TopoDS_Shape& aSubShape = anIt.Value();
128     bIsNewSubShape = Standard_False;
129     if(!aMSA.Contains(aSubShape)) {
130       bIsNewSubShape=!bIsNewSubShape;
131       aIndexSubShape=aMSA.Add(aSubShape, aASx);
132       aMS.Add(aSubShape, aASx);
133     }
134     else {
135       aIndexSubShape=aMSA.FindIndex(aSubShape);
136     }
137     aAS.SetNewSuccessor(aIndexSubShape);
138     aAS.SetNewOrientation(aSubShape.Orientation());
139     //
140     if(bIsNewSubShape && (aSubShape.ShapeType() != TopAbs_VERTEX)) {
141       FillSubshapes(aSubShape, aMSA, aMS);
142     }
143   }
144 }
145 // Contribution of Samtech www.samcef.com END 
146 //===========================================================================
147 //function : Init
148 //purpose  : 
149 //===========================================================================
150   void NMTDS_ShapesDataStructure::Init()
151 {
152   Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
153   Standard_Integer iFirst, iLast;
154   NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
155   NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
156   TopoDS_Iterator anIt;
157   BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMSA;
158   //
159   anIt.Initialize(myCompositeShape);
160   for (; anIt.More(); anIt.Next()) {
161     const TopoDS_Shape& aSx=anIt.Value(); 
162     BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMS;
163     //modified by NIZNHY-PKV Tue Feb 27 17:05:47 2007f
164     //FillMap(aSx, aMSA, aMS);
165     //aLx.Append(aMS);
166     //
167     if (!aMSA.Contains(aSx)) {
168       FillMap(aSx, aMSA, aMS);
169       aLx.Append(aMS);
170     }
171     //modified by NIZNHY-PKV Tue Feb 27 17:06:03 2007t
172   }
173   //
174   aNbS=aMSA.Extent(); 
175   //
176   // Fill myRanges
177   //modified by NIZNHY-PKV Tue Feb 27 17:10:07 2007f
178   i=aLx.Extent();
179   //modified by NIZNHY-PKV Tue Feb 27 17:10:10 2007t
180   myRanges.Resize(i);
181   aLit.Initialize(aLx);
182   for (i=1; aLit.More(); aLit.Next(), ++i) {
183     // Modified  Thu Sep 14 14:35:18 2006 
184     // Contribution of Samtech www.samcef.com BEGIN
185     const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSx=aLit.Value();
186     aNbSx=aMSx.Extent();
187     // Contribution of Samtech www.samcef.com END
188     if (i==1) {
189       iFirst=1;
190       iLast=aNbSx;
191       myRanges(i).SetFirst(iFirst);
192       myRanges(i).SetLast(iLast);
193       continue;
194     }
195     iFirst=myRanges(i-1).Last()+1;
196     iLast=iFirst+aNbSx-1;
197     myRanges(i).SetFirst(iFirst);
198     myRanges(i).SetLast(iLast);
199   }
200   //
201   myNumberOfShapesOfTheObject=aNbS;
202   // Modified  Thu Sep 14 14:35:18 2006 
203   // Contribution of Samtech www.samcef.com BEGIN
204   //myNumberOfShapesOfTheTool=aNbS;
205   //myLength=3*aNbS;
206   myNumberOfShapesOfTheTool=0;
207   myLength=2*aNbS;
208   // Contribution of Samtech www.samcef.com END
209   //
210   // Allocate the whole Table
211   myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
212     Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
213   // Modified  Thu Sep 14 14:35:18 2006 
214   // Contribution of Samtech www.samcef.com BEGIN
215   //
216   // Fill the table
217   //aShift=0;
218   //for (i=0; i<2; ++i) {
219   //  if (i) {
220   //    aShift=aNbS;
221   //  }
222   //  for (j=1; j<=aNbS; ++j) {
223   //    const TopoDS_Shape& aSx=aMSA.FindKey(j);
224   //    const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=
225   //      aMSA.FindFromIndex(j);
226   //    InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
227   //  }
228   //}
229
230   aShift=0;
231   for (j=1; j<=aNbS; ++j) {
232     const TopoDS_Shape& aSx=aMSA.FindKey(j);
233     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aMSA.FindFromIndex(j);
234     InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
235   }
236   // Contribution of Samtech www.samcef.com END
237   //
238   // myShapeIndexMap
239   myShapeIndexMap.Clear();
240   //
241   aNbRanges=myRanges.Extent();
242   for (i=1; i<=aNbRanges; ++i){
243     BooleanOperations_IndexedDataMapOfShapeInteger aSIM;
244     //
245     const NMTDS_IndexRange& aR=myRanges(i);
246     iFirst=aR.First();
247     iLast =aR.Last();
248     for (j=iFirst; j<=iLast; ++j) { 
249       const TopoDS_Shape& aS=Shape(j);
250       aSIM.Add(aS, j);
251     }
252     myShapeIndexMap.Add(i, aSIM);
253   }
254   //
255   // myRefEdges
256   iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
257   myRefEdges.Resize(iLast);
258
259   for (i=1; i<=iLast; ++i) {
260     const TopoDS_Shape& aS=Shape(i);
261     myRefEdges(i)=0;
262     if (aS.ShapeType()==TopAbs_EDGE) {
263       myNbEdges++;
264       myRefEdges(i)=myNbEdges;
265     }
266   }
267 }
268 //===========================================================================
269 //function : ShapeRangeIndex
270 //purpose  : 
271 //===========================================================================
272   Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex(const Standard_Integer aId)const
273 {
274   Standard_Boolean bFound;
275   Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
276   //
277   aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
278   
279   aIdx=aId;
280   if (aIdx > aNbS || aIdx < 1){
281     return aZero;
282   }
283   //  
284   if (aIdx > myNumberOfShapesOfTheObject) {
285     aIdx-=myNumberOfShapesOfTheObject;
286   }
287   //
288   aNbR=myRanges.Extent();
289   for (i=1; i<=aNbR; ++i) {
290     const NMTDS_IndexRange& aRange=myRanges(i);
291     bFound=aRange.IsInRange(aIdx);
292     if (bFound) {
293      return i;
294     }
295   }
296   return aZero;
297 }
298 //===========================================================================
299 //function : Rank
300 //purpose  : 
301 //===========================================================================
302   Standard_Integer NMTDS_ShapesDataStructure::Rank(const Standard_Integer aId)const
303 {
304   Standard_Boolean bFound;
305   Standard_Integer i, aNbR, aNbS, aZero=0;
306   //
307   aNbS=myNumberOfShapesOfTheObject;
308   
309   if (aId > aNbS || aId < 1){
310     return aZero;
311   }
312   //  
313   aNbR=myRanges.Extent();
314   for (i=1; i<=aNbR; ++i) {
315     const NMTDS_IndexRange& aRange=myRanges(i);
316     bFound=aRange.IsInRange(aId);
317     if (bFound) {
318      return i;
319     }
320   }
321   return aZero;
322 }
323 //===========================================================================
324 //function : ShapeIndex
325 //purpose  : 
326 //===========================================================================
327   Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex(const TopoDS_Shape& aS,
328                                                          const Standard_Integer aRank)const
329 {
330   Standard_Boolean bFound;
331   Standard_Integer aIndex=0;
332   //
333   bFound=myShapeIndexMap.Contains(aRank);
334   if (!bFound) {
335     return aIndex;
336   }
337   // Modified Thu Sep 14 14:35:18 2006 
338   // Contribution of Samtech www.samcef.com BEGIN
339   //
340   //const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=
341   //  myShapeIndexMap.FindFromKey(aRank);
342   //
343   //bFound=aSIM.Contains(aS);
344   //if (!bFound) {
345   //  return aIndex;
346   //}
347   //
348   //aIndex=aSIM.FindFromKey(aS);
349   //
350   Standard_Integer i, aNbRanks;
351   //
352   aNbRanks=myShapeIndexMap.Extent();
353   for (i=1; i<=aNbRanks; ++i){
354     const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=myShapeIndexMap.FindFromKey(i);
355     bFound=aSIM.Contains(aS);
356     if (bFound) {
357       aIndex=aSIM.FindFromKey(aS);
358       return aIndex;
359     }
360   }
361   // Contribution of Samtech www.samcef.com END
362   return aIndex;
363 }