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