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