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