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