Salome HOME
0021672: [CEA 565] Dump Study from script
[modules/geom.git] / src / NMTDS / NMTDS_ShapesDataStructure.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        NMTDS_ShapesDataStructure.cxx
24 // Author:      Peter KURNEV
25
26 #include <NMTDS_ShapesDataStructure.hxx>
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 #include <NMTDS_Tools.hxx>
39
40
41 static
42   void ComputeBoxExS(const Standard_Integer aIx,
43                      const NMTDS_ShapesDataStructure* pDS,
44                      Bnd_Box& aBoxEx);
45 static
46   void GetAllSuccessorsS(const Standard_Integer nS,
47                          const NMTDS_ShapesDataStructure* myDS,
48                          TColStd_IndexedMapOfInteger& aMA);
49
50 //===========================================================================
51 //function : NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure
52 //purpose  : 
53 //===========================================================================
54 NMTDS_ShapesDataStructure::NMTDS_ShapesDataStructure()
55 :
56   BooleanOperations_ShapesDataStructure()
57 {}
58 //===========================================================================
59 //function : SetCompositeShape
60 //purpose  : 
61 //===========================================================================
62 void NMTDS_ShapesDataStructure::SetCompositeShape(const TopoDS_Shape& aS)
63 {
64   myCompositeShape=aS;
65 }
66 //===========================================================================
67 //function : CompositeShape
68 //purpose  : 
69 //===========================================================================
70 const TopoDS_Shape& NMTDS_ShapesDataStructure::CompositeShape()const
71 {
72   return myCompositeShape;
73 }
74 //===========================================================================
75 //function : Ranges
76 //purpose  : 
77 //===========================================================================
78 const NMTDS_CArray1OfIndexRange& NMTDS_ShapesDataStructure::Ranges()const
79 {
80   return myRanges;
81 }
82 //===========================================================================
83 //function : FillMap
84 //purpose  : 
85 //===========================================================================
86 void NMTDS_ShapesDataStructure::FillMap
87   (const TopoDS_Shape& aS,
88    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
89    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
90 {
91   Standard_Integer iX, i, j, aIndex, aNbSc, aNbS;
92   BooleanOperations_AncestorsSeqAndSuccessorsSeq aAS;
93   //
94   aMSA.Add(aS, aAS);
95   aMS.Add(aS, aAS);
96   FillSubshapes(aS, aMSA, aMS);
97   //
98   aNbS=aMS.Extent();
99   for(i=1; i<=aNbS; ++i) {
100     TColStd_MapOfInteger aMFence;
101     //
102     const TopoDS_Shape& aSX=aMS.FindKey(i);
103     iX=aMSA.FindIndex(aSX);
104     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS1=aMSA(iX);
105     //
106     aNbSc=aAS1.NumberOfSuccessors();
107     for(j=1; j<=aNbSc; ++j) {
108       aIndex=aAS1.GetSuccessor(j);
109       if(aMFence.Add(aIndex)) {
110         BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS2=aMSA.ChangeFromIndex(aIndex);
111         aAS2.SetNewAncestor(iX);
112       }
113     }
114   }
115 }
116 //===========================================================================
117 //function : FillSubshapes
118 //purpose  : 
119 //===========================================================================
120 void NMTDS_ShapesDataStructure::FillSubshapes
121   (const TopoDS_Shape& aS,
122    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
123    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
124 {
125   Standard_Boolean bIsNewSubShape;
126   Standard_Integer aIndexSubShape, aIndex;
127   BooleanOperations_AncestorsSeqAndSuccessorsSeq aASx;
128   //
129   aIndex=aMSA.FindIndex(aS);
130   BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS=aMSA.ChangeFromIndex(aIndex);
131   //
132   TopoDS_Iterator anIt(aS, Standard_True);
133   for(; anIt.More(); anIt.Next()) {
134     const TopoDS_Shape& aSubShape = anIt.Value();
135     bIsNewSubShape = Standard_False;
136     if(!aMSA.Contains(aSubShape)) {
137       bIsNewSubShape=!bIsNewSubShape;
138       aIndexSubShape=aMSA.Add(aSubShape, aASx);
139       aMS.Add(aSubShape, aASx);
140     }
141     else {
142       aIndexSubShape=aMSA.FindIndex(aSubShape);
143     }
144     aAS.SetNewSuccessor(aIndexSubShape);
145     aAS.SetNewOrientation(aSubShape.Orientation());
146     //
147     if(bIsNewSubShape && (aSubShape.ShapeType() != TopAbs_VERTEX)) {
148       FillSubshapes(aSubShape, aMSA, aMS);
149     }
150   }
151 }
152 //===========================================================================
153 //function : Init
154 //purpose  : 
155 //===========================================================================
156 void NMTDS_ShapesDataStructure::Init()
157 {
158   Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
159   Standard_Integer iFirst, iLast;
160   NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
161   NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
162   TopoDS_Iterator anIt;
163   BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMSA;
164   //
165   anIt.Initialize(myCompositeShape);
166   for (; anIt.More(); anIt.Next()) {
167     const TopoDS_Shape& aSx=anIt.Value(); 
168     BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMS;
169     //
170     if (!aMSA.Contains(aSx)) {
171       FillMap(aSx, aMSA, aMS);
172       aLx.Append(aMS);
173     }
174   }
175   aNbS=aMSA.Extent(); 
176   //
177   // Fill myRanges
178   i=aLx.Extent();
179   myRanges.Resize(i);
180   aLit.Initialize(aLx);
181   for (i=1; aLit.More(); aLit.Next(), ++i) {
182     const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSx=aLit.Value();
183     aNbSx=aMSx.Extent();
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   myNumberOfShapesOfTheTool=0;
199   myLength=2*aNbS;
200   //
201   // Allocate the whole Table
202   myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
203     Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
204   //
205   // Fill the table
206   
207   aShift=0;
208   for (j=1; j<=aNbS; ++j) {
209     const TopoDS_Shape& aSx=aMSA.FindKey(j);
210     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aMSA.FindFromIndex(j);
211     InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
212   }
213   // myShapeIndexMap
214   myShapeIndexMap.Clear();
215   //
216   //modified by NIZNHY-PKV Mon Dec 12 09:01:53 2011f
217   aNbRanges=myRanges.Extent();
218   for (i=1; i<=aNbRanges; ++i){
219     const NMTDS_IndexRange& aR=myRanges(i);
220     iFirst=aR.First();
221     iLast =aR.Last();
222     for (j=iFirst; j<=iLast; ++j) { 
223       const TopoDS_Shape& aS=Shape(j);
224       myShapeIndexMap.Bind(aS, j);
225     }
226   }
227   //modified by NIZNHY-PKV Mon Dec 12 09:02:00 2011t
228   //
229   // myRefEdges
230   iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
231   myRefEdges.Resize(iLast);
232
233   for (i=1; i<=iLast; ++i) {
234     const TopoDS_Shape& aS=Shape(i);
235     myRefEdges(i)=0;
236     if (aS.ShapeType()==TopAbs_EDGE) {
237       myNbEdges++;
238       myRefEdges(i)=myNbEdges;
239     }
240   }
241 }
242 //===========================================================================
243 //function : ShapeRangeIndex
244 //purpose  : 
245 //===========================================================================
246 Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex
247   (const Standard_Integer aId)const
248 {
249   Standard_Boolean bFound;
250   Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
251   //
252   aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
253   
254   aIdx=aId;
255   if (aIdx > aNbS || aIdx < 1){
256     return aZero;
257   }
258   //  
259   if (aIdx > myNumberOfShapesOfTheObject) {
260     aIdx-=myNumberOfShapesOfTheObject;
261   }
262   //
263   aNbR=myRanges.Extent();
264   for (i=1; i<=aNbR; ++i) {
265     const NMTDS_IndexRange& aRange=myRanges(i);
266     bFound=aRange.IsInRange(aIdx);
267     if (bFound) {
268      return i;
269     }
270   }
271   return aZero;
272 }
273 //===========================================================================
274 //function : Rank
275 //purpose  : 
276 //===========================================================================
277 Standard_Integer NMTDS_ShapesDataStructure::Rank
278   (const Standard_Integer aId)const
279 {
280   Standard_Boolean bFound;
281   Standard_Integer i, aNbR, aNbS, aZero=0;
282   //
283   aNbS=myNumberOfShapesOfTheObject;
284   
285   if (aId > aNbS || aId < 1){
286     return aZero;
287   }
288   //  
289   aNbR=myRanges.Extent();
290   for (i=1; i<=aNbR; ++i) {
291     const NMTDS_IndexRange& aRange=myRanges(i);
292     bFound=aRange.IsInRange(aId);
293     if (bFound) {
294      return i;
295     }
296   }
297   return aZero;
298 }
299 //===========================================================================
300 //function : ShapeIndex
301 //purpose  : 
302 //===========================================================================
303 Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex
304   (const TopoDS_Shape& aS,
305    const Standard_Integer aRank)const
306 {
307   Standard_Address pIndex;
308   Standard_Integer aIndex;
309   //
310   aIndex=0;
311   //
312   //modified by NIZNHY-PKV Mon Dec 12 09:02:48 2011f
313   pIndex=myShapeIndexMap.Find1(aS);
314   if (pIndex) {
315     aIndex=*((Standard_Integer*)pIndex);
316   }
317   //modified by NIZNHY-PKV Mon Dec 12 09:02:54 2011t
318   return aIndex;
319 }
320
321 //=======================================================================
322 //function : ComputeBoxEx
323 //purpose  : 
324 //=======================================================================
325 void NMTDS_ShapesDataStructure::ComputeBoxEx 
326   (const Standard_Integer aIx,
327    Bnd_Box& aBoxEx)const
328 {
329   ComputeBoxExS(aIx, this, aBoxEx);
330 }
331 //=======================================================================
332 //function : GetAllSuccessors
333 //purpose  : 
334 //=======================================================================
335 void NMTDS_ShapesDataStructure::GetAllSuccessors
336   (const Standard_Integer nS,
337    TColStd_IndexedMapOfInteger& aMA)const
338 {
339   GetAllSuccessorsS(nS, this, aMA);
340 }
341 //=======================================================================
342 //function : GetAllSuccessorsS
343 //purpose  : 
344 //=======================================================================
345 void GetAllSuccessorsS(const Standard_Integer nS,
346                        const NMTDS_ShapesDataStructure* myDS,
347                        TColStd_IndexedMapOfInteger& aMA)
348 {
349   TopAbs_ShapeEnum aT;
350   Standard_Integer i, nSx, aNbSuccessors, *pSuccessors;
351   Standard_Address xSuccessors;
352   //
353   const TopoDS_Shape& aS=myDS->Shape(nS);
354   aT=aS.ShapeType();
355   if(NMTDS_Tools::HasBRep(aT)) {
356     aMA.Add(nS);
357     //
358     if (aT==TopAbs_VERTEX) {
359       return;
360     }
361   }
362   //
363   myDS->GetSuccessors(nS, xSuccessors, aNbSuccessors);
364   pSuccessors=(Standard_Integer*)xSuccessors;
365   for (i=0; i<aNbSuccessors; ++i) {
366     nSx=pSuccessors[i];
367     GetAllSuccessorsS(nSx, myDS, aMA);
368   }
369 }
370 //=======================================================================
371 // function: ComputeBoxExS
372 // purpose: 
373 //=======================================================================
374 void ComputeBoxExS(const Standard_Integer aIx,
375                    const NMTDS_ShapesDataStructure* pDS,
376                    Bnd_Box& aBoxEx)
377 {
378   Standard_Integer i, aNbS, iS;
379   //
380   const Bnd_Box& aBox=pDS->GetBoundingBox(aIx);
381   aBoxEx.Add(aBox);
382   //
383   aNbS=pDS->NumberOfSuccessors(aIx);
384   for (i=1; i<=aNbS; ++i) {
385     Bnd_Box aBoxS;
386     iS=pDS->GetSuccessor(aIx, i);
387     ComputeBoxExS(iS, pDS, aBoxS);
388     aBoxEx.Add(aBoxS);
389   }
390 }