Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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 #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 // Modified to Add new methods Thu Sep 14 14:35:18 2006 
83 // Contribution of Samtech www.samcef.com BEGIN
84 //===========================================================================
85 //function : FillMap
86 //purpose  : 
87 //===========================================================================
88   void NMTDS_ShapesDataStructure::FillMap
89   (const TopoDS_Shape& aS,
90    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
91    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
92 {
93   Standard_Integer iX, i, j, aIndex, aNbSc, aNbS;
94   BooleanOperations_AncestorsSeqAndSuccessorsSeq aAS;
95   //
96   aMSA.Add(aS, aAS);
97   aMS.Add(aS, aAS);
98   FillSubshapes(aS, aMSA, aMS);
99   //
100   aNbS=aMS.Extent();
101   for(i=1; i<=aNbS; ++i) {
102     TColStd_MapOfInteger aMFence;
103     //
104     const TopoDS_Shape& aSX=aMS.FindKey(i);
105     iX=aMSA.FindIndex(aSX);
106     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS1=aMSA(iX);
107     //
108     aNbSc=aAS1.NumberOfSuccessors();
109     for(j=1; j<=aNbSc; ++j) {
110       aIndex=aAS1.GetSuccessor(j);
111       if(aMFence.Add(aIndex)) {
112         BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS2=aMSA.ChangeFromIndex(aIndex);
113         aAS2.SetNewAncestor(iX);
114       }
115     }
116   }
117 }
118 //===========================================================================
119 //function : FillSubshapes
120 //purpose  : 
121 //===========================================================================
122   void NMTDS_ShapesDataStructure::FillSubshapes
123   (const TopoDS_Shape& aS,
124    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSA,
125    BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMS) const
126 {
127   Standard_Boolean bIsNewSubShape;
128   Standard_Integer aIndexSubShape, aIndex;
129   BooleanOperations_AncestorsSeqAndSuccessorsSeq aASx;
130   //
131   aIndex=aMSA.FindIndex(aS);
132   BooleanOperations_AncestorsSeqAndSuccessorsSeq& aAS=aMSA.ChangeFromIndex(aIndex);
133   //
134   TopoDS_Iterator anIt(aS, Standard_True);
135   for(; anIt.More(); anIt.Next()) {
136     const TopoDS_Shape& aSubShape = anIt.Value();
137     bIsNewSubShape = Standard_False;
138     if(!aMSA.Contains(aSubShape)) {
139       bIsNewSubShape=!bIsNewSubShape;
140       aIndexSubShape=aMSA.Add(aSubShape, aASx);
141       aMS.Add(aSubShape, aASx);
142     }
143     else {
144       aIndexSubShape=aMSA.FindIndex(aSubShape);
145     }
146     aAS.SetNewSuccessor(aIndexSubShape);
147     aAS.SetNewOrientation(aSubShape.Orientation());
148     //
149     if(bIsNewSubShape && (aSubShape.ShapeType() != TopAbs_VERTEX)) {
150       FillSubshapes(aSubShape, aMSA, aMS);
151     }
152   }
153 }
154 // Contribution of Samtech www.samcef.com END 
155 //===========================================================================
156 //function : Init
157 //purpose  : 
158 //===========================================================================
159   void NMTDS_ShapesDataStructure::Init()
160 {
161   Standard_Integer i, j, aNbSx, aNbS, aShift, aNbRanges;
162   Standard_Integer iFirst, iLast;
163   NMTDS_ListOfIndexedDataMapOfShapeAncestorsSuccessors aLx;
164   NMTDS_ListIteratorOfListOfIndexedDataMapOfShapeAncestorsSuccessors aLit;
165   TopoDS_Iterator anIt;
166   BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMSA;
167   //
168   anIt.Initialize(myCompositeShape);
169   for (; anIt.More(); anIt.Next()) {
170     const TopoDS_Shape& aSx=anIt.Value(); 
171     BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors aMS;
172     //modified by NIZNHY-PKV Tue Feb 27 17:05:47 2007f
173     //FillMap(aSx, aMSA, aMS);
174     //aLx.Append(aMS);
175     //
176     if (!aMSA.Contains(aSx)) {
177       FillMap(aSx, aMSA, aMS);
178       aLx.Append(aMS);
179     }
180     //modified by NIZNHY-PKV Tue Feb 27 17:06:03 2007t
181   }
182   aNbS=aMSA.Extent(); 
183   //
184   // Fill myRanges
185   //modified by NIZNHY-PKV Tue Feb 27 17:10:07 2007f
186   i=aLx.Extent();
187   //modified by NIZNHY-PKV Tue Feb 27 17:10:10 2007t
188   myRanges.Resize(i);
189   aLit.Initialize(aLx);
190   for (i=1; aLit.More(); aLit.Next(), ++i) {
191     // Modified  Thu Sep 14 14:35:18 2006 
192     // Contribution of Samtech www.samcef.com BEGIN
193     const BooleanOperations_IndexedDataMapOfShapeAncestorsSuccessors& aMSx=aLit.Value();
194     aNbSx=aMSx.Extent();
195     // Contribution of Samtech www.samcef.com END
196     if (i==1) {
197       iFirst=1;
198       iLast=aNbSx;
199       myRanges(i).SetFirst(iFirst);
200       myRanges(i).SetLast(iLast);
201       continue;
202     }
203     iFirst=myRanges(i-1).Last()+1;
204     iLast=iFirst+aNbSx-1;
205     myRanges(i).SetFirst(iFirst);
206     myRanges(i).SetLast(iLast);
207   }
208   //
209   myNumberOfShapesOfTheObject=aNbS;
210   // Modified  Thu Sep 14 14:35:18 2006 
211   // Contribution of Samtech www.samcef.com BEGIN
212   //myNumberOfShapesOfTheTool=aNbS;
213   //myLength=3*aNbS;
214   myNumberOfShapesOfTheTool=0;
215   myLength=2*aNbS;
216   // Contribution of Samtech www.samcef.com END
217   //
218   // Allocate the whole Table
219   myListOfShapeAndInterferences = (BooleanOperations_PShapeAndInterferences)
220     Standard::Allocate(myLength*sizeof(BooleanOperations_ShapeAndInterferences));
221   // Modified  Thu Sep 14 14:35:18 2006 
222   // Contribution of Samtech www.samcef.com BEGIN
223   //
224   // Fill the table
225     
226   //aShift=0;
227   //for (i=0; i<2; ++i) {
228   //  if (i) {
229   //    aShift=aNbS;
230   //  }
231   //  for (j=1; j<=aNbS; ++j) {
232   //    const TopoDS_Shape& aSx=aMSA.FindKey(j);
233   //    const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=
234   //      aMSA.FindFromIndex(j);
235   //    InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
236   //  }
237   //}
238   
239   aShift=0;
240   for (j=1; j<=aNbS; ++j) {
241     const TopoDS_Shape& aSx=aMSA.FindKey(j);
242     const BooleanOperations_AncestorsSeqAndSuccessorsSeq& aASx=aMSA.FindFromIndex(j);
243     InsertShapeAndAncestorsSuccessors(aSx, aASx, aShift);
244   }
245   // Contribution of Samtech www.samcef.com END
246   //
247   // myShapeIndexMap
248   myShapeIndexMap.Clear();
249   //
250   aNbRanges=myRanges.Extent();
251   for (i=1; i<=aNbRanges; ++i){
252     BooleanOperations_IndexedDataMapOfShapeInteger aSIM;
253     //
254     const NMTDS_IndexRange& aR=myRanges(i);
255     iFirst=aR.First();
256     iLast =aR.Last();
257     for (j=iFirst; j<=iLast; ++j) { 
258       const TopoDS_Shape& aS=Shape(j);
259       aSIM.Add(aS, j);
260     }
261     myShapeIndexMap.Add(i, aSIM);
262   }
263   //
264   // myRefEdges
265   iLast=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
266   myRefEdges.Resize(iLast);
267
268   for (i=1; i<=iLast; ++i) {
269     const TopoDS_Shape& aS=Shape(i);
270     myRefEdges(i)=0;
271     if (aS.ShapeType()==TopAbs_EDGE) {
272       myNbEdges++;
273       myRefEdges(i)=myNbEdges;
274     }
275   }
276 }
277 //===========================================================================
278 //function : ShapeRangeIndex
279 //purpose  : 
280 //===========================================================================
281   Standard_Integer NMTDS_ShapesDataStructure::ShapeRangeIndex(const Standard_Integer aId)const
282 {
283   Standard_Boolean bFound;
284   Standard_Integer i, aNbR, aIdx, aNbS, aZero=0;
285   //
286   aNbS=myNumberOfShapesOfTheObject+myNumberOfShapesOfTheTool;
287   
288   aIdx=aId;
289   if (aIdx > aNbS || aIdx < 1){
290     return aZero;
291   }
292   //  
293   if (aIdx > myNumberOfShapesOfTheObject) {
294     aIdx-=myNumberOfShapesOfTheObject;
295   }
296   //
297   aNbR=myRanges.Extent();
298   for (i=1; i<=aNbR; ++i) {
299     const NMTDS_IndexRange& aRange=myRanges(i);
300     bFound=aRange.IsInRange(aIdx);
301     if (bFound) {
302      return i;
303     }
304   }
305   return aZero;
306 }
307 //===========================================================================
308 //function : Rank
309 //purpose  : 
310 //===========================================================================
311   Standard_Integer NMTDS_ShapesDataStructure::Rank(const Standard_Integer aId)const
312 {
313   Standard_Boolean bFound;
314   Standard_Integer i, aNbR, aNbS, aZero=0;
315   //
316   aNbS=myNumberOfShapesOfTheObject;
317   
318   if (aId > aNbS || aId < 1){
319     return aZero;
320   }
321   //  
322   aNbR=myRanges.Extent();
323   for (i=1; i<=aNbR; ++i) {
324     const NMTDS_IndexRange& aRange=myRanges(i);
325     bFound=aRange.IsInRange(aId);
326     if (bFound) {
327      return i;
328     }
329   }
330   return aZero;
331 }
332 //===========================================================================
333 //function : ShapeIndex
334 //purpose  : 
335 //===========================================================================
336   Standard_Integer NMTDS_ShapesDataStructure::ShapeIndex(const TopoDS_Shape& aS,
337                                                          const Standard_Integer aRank)const
338 {
339   Standard_Boolean bFound;
340   Standard_Integer aIndex=0;
341   //
342   bFound=myShapeIndexMap.Contains(aRank);
343   if (!bFound) {
344     return aIndex;
345   }
346   // Modified Thu Sep 14 14:35:18 2006 
347   // Contribution of Samtech www.samcef.com BEGIN
348   //
349   //const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=
350   //  myShapeIndexMap.FindFromKey(aRank);
351   //
352   //bFound=aSIM.Contains(aS);
353   //if (!bFound) {
354   //  return aIndex;
355   //}
356   //
357   //aIndex=aSIM.FindFromKey(aS);
358   //
359   Standard_Integer i, aNbRanks;
360   //
361   aNbRanks=myShapeIndexMap.Extent();
362   for (i=1; i<=aNbRanks; ++i){
363     const BooleanOperations_IndexedDataMapOfShapeInteger& aSIM=myShapeIndexMap.FindFromKey(i);
364     bFound=aSIM.Contains(aS);
365     if (bFound) {
366       aIndex=aSIM.FindFromKey(aS);
367       return aIndex;
368     }
369   }
370   // Contribution of Samtech www.samcef.com END
371   return aIndex;
372 }
373
374 //=======================================================================
375 //function : ComputeBoxEx
376 //purpose  : 
377 //=======================================================================
378   void NMTDS_ShapesDataStructure::ComputeBoxEx (const Standard_Integer aIx,
379                                                 Bnd_Box& aBoxEx)const
380 {
381   ComputeBoxExS(aIx, this, aBoxEx);
382 }
383 //=======================================================================
384 //function : GetAllSuccessors
385 //purpose  : 
386 //=======================================================================
387   void NMTDS_ShapesDataStructure::GetAllSuccessors(const Standard_Integer nS,
388                                                    TColStd_IndexedMapOfInteger& aMA)const
389 {
390   GetAllSuccessorsS(nS, this, aMA);
391 }
392 //=======================================================================
393 //function : GetAllSuccessorsS
394 //purpose  : 
395 //=======================================================================
396 void GetAllSuccessorsS(const Standard_Integer nS,
397                        const NMTDS_ShapesDataStructure* myDS,
398                        TColStd_IndexedMapOfInteger& aMA)
399 {
400   TopAbs_ShapeEnum aT;
401   Standard_Integer i, nSx, aNbSuccessors, *pSuccessors;
402   Standard_Address xSuccessors;
403   //
404   const TopoDS_Shape& aS=myDS->Shape(nS);
405   aT=aS.ShapeType();
406   if(NMTDS_Tools::HasBRep(aT)) {
407     aMA.Add(nS);
408     //
409     if (aT==TopAbs_VERTEX) {
410       return;
411     }
412   }
413   //
414   myDS->GetSuccessors(nS, xSuccessors, aNbSuccessors);
415   pSuccessors=(Standard_Integer*)xSuccessors;
416   for (i=0; i<aNbSuccessors; ++i) {
417     nSx=pSuccessors[i];
418     GetAllSuccessorsS(nSx, myDS, aMA);
419   }
420 }
421 //=======================================================================
422 // function: ComputeBoxExS
423 // purpose: 
424 //=======================================================================
425   void ComputeBoxExS(const Standard_Integer aIx,
426                      const NMTDS_ShapesDataStructure* pDS,
427                      Bnd_Box& aBoxEx)
428 {
429   Standard_Integer i, aNbS, iS;
430   //
431   const Bnd_Box& aBox=pDS->GetBoundingBox(aIx);
432   aBoxEx.Add(aBox);
433   //
434   aNbS=pDS->NumberOfSuccessors(aIx);
435   for (i=1; i<=aNbS; ++i) {
436     Bnd_Box aBoxS;
437     iS=pDS->GetSuccessor(aIx, i);
438     ComputeBoxExS(iS, pDS, aBoxS);
439     aBoxEx.Add(aBoxS);
440   }
441 }