]> SALOME platform Git repositories - modules/geom.git/blob - src/NMTTools/NMTTools_PaveFiller_7.cxx
Salome HOME
New version of PartitionAlgo.
[modules/geom.git] / src / NMTTools / NMTTools_PaveFiller_7.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:        NMTTools_PaveFiller_7.cxx
21 // Created:     Thu Dec 18 15:14:55 2003
22 // Author:      Peter KURNEV
23 //              <pkv@irinox>
24
25
26 #include <NMTTools_PaveFiller.ixx>
27
28 #include <TopoDS_Vertex.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <TopoDS.hxx>
31
32 #include <NMTDS_ShapesDataStructure.hxx>
33 #include <TopAbs_Orientation.hxx>
34 #include <BOPTools_ListOfPaveBlock.hxx>
35 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
36 #include <BOPTools_PaveBlock.hxx>
37 #include <BOPTools_Pave.hxx>
38 #include <BOPTools_Tools.hxx>
39 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
40 #include <BRep_Tool.hxx>
41 #include <NMTTools_ListIteratorOfListOfCommonBlock.hxx>
42 #include <BOPTools_SSInterference.hxx>
43 #include <BOPTools_CArray1OfSSInterference.hxx>
44 #include <TopExp_Explorer.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopExp.hxx>
47 #include <TColStd_IndexedMapOfInteger.hxx>
48
49 //
50 #include <IntTools_SequenceOfPntOn2Faces.hxx>
51 #include <IntTools_PntOnFace.hxx>
52 #include <IntTools_PntOn2Faces.hxx>
53 #include <BOPTools_Tools.hxx>
54 #include <TopTools_DataMapOfShapeListOfInteger.hxx>
55 #include <TColStd_ListOfInteger.hxx>
56 #include <TopoDS_Compound.hxx>
57 #include <BRep_Builder.hxx>
58 #include <BOPTools_CArray1OfVVInterference.hxx>
59 #include <BOPTools_VVInterference.hxx>
60 #include <TopTools_DataMapOfShapeShape.hxx>
61 #include <TopTools_DataMapOfShapeListOfInteger.hxx>
62 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfInteger.hxx>
63 #include <TColStd_MapOfInteger.hxx>
64 #include <TColStd_ListIteratorOfListOfInteger.hxx>
65 #include <TopTools_DataMapOfIntegerShape.hxx>
66 #include <Bnd_HArray1OfBox.hxx>
67 #include <Bnd_BoundSortBox.hxx>
68 #include <Bnd_Box.hxx>
69 #include <BRepBndLib.hxx>
70 #include <TopTools_DataMapIteratorOfDataMapOfIntegerShape.hxx>
71 #include <TopTools_DataMapOfShapeInteger.hxx>
72 #include <TopTools_DataMapIteratorOfDataMapOfShapeInteger.hxx>
73 #include <TopTools_DataMapOfShapeInteger.hxx>
74 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
75
76 // Modified  Thu Sep 14 14:35:18 2006 
77 // Contribution of Samtech www.samcef.com BEGIN
78 static 
79   void FuseVertices(const TopoDS_Shape& aCompound,
80                     TopTools_DataMapOfShapeShape& aDMVV);
81 // Contribution of Samtech www.samcef.com END
82
83 //=======================================================================
84 // function: MakeSplitEdges
85 // purpose: 
86 //=======================================================================
87   void NMTTools_PaveFiller::MakeSplitEdges()
88 {
89   myIsDone=Standard_False;
90   //
91   Standard_Boolean bIsNewVertex1, bIsNewVertex2;
92   Standard_Integer i, aNbS, nV1, nV2, aNbPaveBlocks, aNewShapeIndex;
93   Standard_Real    t1, t2;
94   TopAbs_Orientation anOri;
95   TopoDS_Edge aE, aESplit;
96   TopoDS_Vertex aV1, aV2;
97   //
98   aNbS=myDS->NumberOfShapesOfTheObject();
99   for (i=1; i<=aNbS; ++i) {
100     if (myDS->GetShapeType(i) != TopAbs_EDGE)
101       continue;
102     //
103     // Original Edge
104     aE=TopoDS::Edge(myDS->Shape(i));
105     if (BRep_Tool::Degenerated(aE)){
106       continue;
107     }
108     //
109     anOri=aE.Orientation(); 
110     aE.Orientation(TopAbs_FORWARD);
111     //
112     // Making Split Edges
113     //
114     // Split Set for the Original Edge i
115     BOPTools_ListOfPaveBlock& aSplitEdges=mySplitShapesPool(myDS->RefEdge(i));
116     BOPTools_ListIteratorOfListOfPaveBlock aPBIt(aSplitEdges);
117     //
118     aNbPaveBlocks=aSplitEdges.Extent();
119   
120     for (; aPBIt.More(); aPBIt.Next()) {
121       BOPTools_PaveBlock& aPB=aPBIt.Value();
122       // aPave1
123       const BOPTools_Pave& aPave1=aPB.Pave1();
124       nV1=aPave1.Index();
125       t1=aPave1.Param();
126       aV1=TopoDS::Vertex(myDS->GetShape(nV1));
127       aV1.Orientation(TopAbs_FORWARD);
128       // aPave2
129       const BOPTools_Pave& aPave2=aPB.Pave2();
130       nV2=aPave2.Index();
131       t2=aPave2.Param();
132       aV2=TopoDS::Vertex(myDS->GetShape(nV2));
133       aV2.Orientation(TopAbs_REVERSED);
134       //xx
135       if (aNbPaveBlocks==1) {
136         bIsNewVertex1=myDS->IsNewShape (nV1);
137         bIsNewVertex2=myDS->IsNewShape (nV2);
138         if (!bIsNewVertex1 && !bIsNewVertex2) {
139           aPB.SetEdge(i);
140           continue;
141         }
142       }
143       //xx
144       BOPTools_Tools::MakeSplitEdge(aE, aV1, t1, aV2, t2, aESplit);  
145       //
146       // Add Split Part of the Original Edge to the DS
147       BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
148
149       anASSeq.SetNewSuccessor(nV1);
150       anASSeq.SetNewOrientation(aV1.Orientation());
151
152       anASSeq.SetNewSuccessor(nV2);
153       anASSeq.SetNewOrientation(aV2.Orientation());
154       //
155       if (anOri==TopAbs_INTERNAL) {
156         anASSeq.SetNewAncestor(i);
157         aESplit.Orientation(anOri);
158       }
159       //
160       myDS->InsertShapeAndAncestorsSuccessors(aESplit, anASSeq);
161       aNewShapeIndex=myDS->NumberOfInsertedShapes();
162       myDS->SetState(aNewShapeIndex, BooleanOperations_UNKNOWN);
163       //
164       // Fill Split Set for the Original Edge
165       aPB.SetEdge(aNewShapeIndex);
166       //
167     }
168   } 
169   myIsDone=Standard_True;
170 }
171 //=======================================================================
172 // function: UpdateCommonBlocks
173 // purpose: 
174 //=======================================================================
175   void NMTTools_PaveFiller::UpdateCommonBlocks()
176 {
177   myIsDone=Standard_False;
178   //
179   Standard_Integer nE, aNbS,  nSp, nEx, nSpx;
180   NMTTools_ListIteratorOfListOfCommonBlock aCBIt;
181   BOPTools_ListIteratorOfListOfPaveBlock aPBIt;
182   //
183   aNbS=myDS->NumberOfShapesOfTheObject();
184   //
185   for (nE=1; nE<=aNbS; ++nE) {
186     if (myDS->GetShapeType(nE)!=TopAbs_EDGE){
187       continue;
188     }
189     if (BRep_Tool::Degenerated(TopoDS::Edge(myDS->Shape(nE)))){
190       continue;
191     }
192     //
193     NMTTools_ListOfCommonBlock& aLCB=myCommonBlockPool(myDS->RefEdge(nE));
194     BOPTools_ListOfPaveBlock& aLPB=mySplitShapesPool  (myDS->RefEdge(nE));
195     //
196     aCBIt.Initialize(aLCB);
197     for (; aCBIt.More(); aCBIt.Next()) {
198       NMTTools_CommonBlock& aCB=aCBIt.Value();
199       BOPTools_PaveBlock& aPB=aCB.PaveBlock1(nE);
200       nSp=SplitIndex(aPB);
201       aPB.SetEdge(nSp);
202       //
203       const BOPTools_ListOfPaveBlock& aCBLPB=aCB.PaveBlocks();
204       aPBIt.Initialize(aCBLPB);
205       for (; aPBIt.More(); aPBIt.Next()) {
206         BOPTools_PaveBlock& aPBx=aPBIt.Value();
207         nEx=aPBx.OriginalEdge();
208         if (nEx==nE) {
209           continue;
210         }
211         //
212         nSpx=SplitIndex(aPBx);
213         aPBx.SetEdge(nSpx);
214       }
215       //
216     }
217   }
218 }
219 //=======================================================================
220 // function: SplitIndex
221 // purpose: 
222 //=======================================================================
223   Standard_Integer NMTTools_PaveFiller::SplitIndex(const BOPTools_PaveBlock& aPBx)const 
224 {
225   Standard_Integer anOriginalEdge, anEdgeIndex=0;
226
227   anOriginalEdge=aPBx.OriginalEdge();
228
229   const BOPTools_ListOfPaveBlock& aLPB=mySplitShapesPool(myDS->RefEdge(anOriginalEdge));
230   //
231   BOPTools_ListIteratorOfListOfPaveBlock anIt(aLPB);
232   for (; anIt.More(); anIt.Next()) {
233     BOPTools_PaveBlock& aPB=anIt.Value();
234     if (aPB.IsEqual(aPBx)) {
235       anEdgeIndex=aPB.Edge();
236       return anEdgeIndex;
237     }
238   }
239   return anEdgeIndex;
240
241 //=======================================================================
242 // function: UpdatePaveBlocks
243 // purpose: 
244 //=======================================================================
245   void NMTTools_PaveFiller::UpdatePaveBlocks()
246 {
247   myIsDone=Standard_False;
248   //
249   
250   Standard_Integer i, aNbFFs, nF1, nF2, aNbF, nF, iRankF, nE, nV1, nV2, aNbPB;
251   Standard_Real aT1, aT2;
252   TColStd_IndexedMapOfInteger aMF, aME;
253   TopExp_Explorer aExp;
254   TopoDS_Vertex aV1, aV2;
255   TopoDS_Edge aE;
256   BOPTools_Pave aPave1, aPave2;
257   BOPTools_PaveBlock aPB;
258   //
259   BOPTools_CArray1OfSSInterference& aFFs=myIntrPool->SSInterferences();
260   //
261   aNbFFs=aFFs.Extent();
262   for (i=1; i<=aNbFFs; ++i) {
263     BOPTools_SSInterference& aFFi=aFFs(i);
264     aFFi.Indices(nF1, nF2);
265     aMF.Add(nF1);
266     aMF.Add(nF2);
267   }
268   //
269   aNbF=aMF.Extent();
270   for(i=1; i<=aNbF; ++i) {
271     nF=aMF(i);
272     iRankF=myDS->Rank(nF);
273     const TopoDS_Shape aF=myDS->Shape(nF);//mpv
274     aExp.Init(aF, TopAbs_EDGE);
275     for(; aExp.More();  aExp.Next()) {
276       aE=TopoDS::Edge(aExp.Current());
277       //
278       if (BRep_Tool::Degenerated(aE)) {
279         continue;
280       }
281       //
282       nE=myDS->ShapeIndex(aE, iRankF);
283       //
284       if (aME.Contains(nE)) {
285         continue;
286       }
287       aME.Add(nE);
288       //
289       BOPTools_ListOfPaveBlock& aLPB=mySplitShapesPool(myDS->RefEdge(nE));
290       aNbPB=aLPB.Extent();
291       if (aNbPB) {
292         continue;
293       }
294       TopExp::Vertices(aE, aV1, aV2);
295       //
296       nV1=myDS->ShapeIndex(aV1, iRankF);
297       aT1=BRep_Tool::Parameter(aV1, aE);
298       aPave1.SetIndex(nV1);
299       aPave1.SetParam(aT1);
300       //
301       nV2=myDS->ShapeIndex(aV2, iRankF);
302       aT2=BRep_Tool::Parameter(aV2, aE);
303       aPave2.SetIndex(nV2);
304       aPave2.SetParam(aT2);
305       //
306       aPB.SetEdge(nE);
307       aPB.SetOriginalEdge(nE);
308       aPB.SetPave1(aPave1);
309       aPB.SetPave2(aPave2);
310       //
311       aLPB.Append(aPB);
312     }
313   }
314
315 // Modified 
316 // to treat Alone Vertices between faces
317 // Thu Sep 14 14:35:18 2006 
318 // Contribution of Samtech www.samcef.com BEGIN
319 //=======================================================================
320 // function: MakeAloneVertices
321 // purpose: 
322 //=======================================================================
323   void NMTTools_PaveFiller::MakeAloneVertices()
324 {
325   Standard_Integer i, aNbFFs, nF1, nF2, j, aNbPnts, nFx, aNbV;
326   Standard_Real aTolF1, aTolF2, aTolSum, aTolV;
327   TColStd_ListIteratorOfListOfInteger aIt;
328   TColStd_ListOfInteger aLI;
329   TopoDS_Vertex aV;
330   TopoDS_Compound aCompound;
331   BRep_Builder aBB;
332   TopTools_DataMapOfShapeListOfInteger aDMVFF, aDMVFF1;
333   TopTools_DataMapIteratorOfDataMapOfShapeListOfInteger aItDMVFF;
334   TopTools_DataMapOfShapeShape aDMVV;
335   TopTools_DataMapOfIntegerShape aDMIV;
336   TopTools_DataMapOfShapeInteger aDMVI;
337   TopTools_DataMapIteratorOfDataMapOfShapeInteger aItDMVI;
338   TopTools_DataMapIteratorOfDataMapOfIntegerShape aItDMIV;
339   //
340   aBB.MakeCompound(aCompound);
341   //
342   myAloneVertices.Clear();
343   //
344   BOPTools_CArray1OfSSInterference& aFFs=myIntrPool->SSInterferences();
345   //
346   // 1. Collect alone vertices from FFs
347   aNbV=0;
348   aNbFFs=aFFs.Extent();
349   for (i=1; i<=aNbFFs; ++i) {
350     BOPTools_SSInterference& aFFi=aFFs(i);
351     aFFi.Indices(nF1, nF2);
352     //
353     const TopoDS_Face aF1=TopoDS::Face(myDS->Shape(nF1));//mpv
354     const TopoDS_Face aF2=TopoDS::Face(myDS->Shape(nF2));//mpv
355     //
356     aTolF1=BRep_Tool::Tolerance(aF1);
357     aTolF2=BRep_Tool::Tolerance(aF2);
358     aTolSum=aTolF1+aTolF2;
359     //
360     aLI.Clear();
361     aLI.Append(nF1);
362     aLI.Append(nF2);
363     //
364     const IntTools_SequenceOfPntOn2Faces& aSeqAlonePnts=aFFi.AlonePnts();
365     aNbPnts=aSeqAlonePnts.Length();
366     for (j=1; j<=aNbPnts; ++j) {
367       const gp_Pnt& aP=aSeqAlonePnts(j).P1().Pnt();
368       BOPTools_Tools::MakeNewVertex(aP, aTolSum, aV);
369       aDMVFF.Bind(aV, aLI);
370       aBB.Add(aCompound, aV);
371       ++aNbV;
372     }
373   }
374   if (!aNbV) {
375     return;
376   }
377   //
378   // 2. Try to fuse alone vertices themselves;
379   FuseVertices(aCompound, aDMVV);
380   //
381   // if some are fused, replace them by new ones 
382   aItDMVFF.Initialize(aDMVFF);
383   for (;  aItDMVFF.More(); aItDMVFF.Next()) {
384     const TopoDS_Shape& aVx=aItDMVFF.Key();
385     const TColStd_ListOfInteger& aLIx=aItDMVFF.Value();
386     //
387     if (!aDMVV.IsBound(aVx)) {
388       aDMVFF1.Bind(aVx, aLIx);
389     }
390     else {
391       const TopoDS_Shape& aVy=aDMVV.Find(aVx);
392       
393       if (aDMVFF1.IsBound(aVy)) {
394         TColStd_ListOfInteger& aLIy=aDMVFF1.ChangeFind(aVy);
395         aIt.Initialize(aLIx);
396         for(; aIt.More(); aIt.Next()) {
397           nFx=aIt.Value();
398           aLIy.Append(nFx);
399         }
400       }
401       else { 
402         aDMVFF1.Bind(aVy, aLIx);
403       }
404     }
405   }
406   aDMVFF.Clear();
407   //
408   // refine lists of faces in aDMVFF1; 
409   aItDMVFF.Initialize(aDMVFF1);
410   for (;  aItDMVFF.More(); aItDMVFF.Next()) {
411     TColStd_MapOfInteger aMIy;
412     TColStd_ListOfInteger aLIy;
413     //
414     const TopoDS_Shape& aVx=aItDMVFF.Key();
415     TColStd_ListOfInteger& aLIx=aDMVFF1.ChangeFind(aVx);
416     aIt.Initialize(aLIx);
417     for(; aIt.More(); aIt.Next()) {
418       nFx=aIt.Value();
419       if (aMIy.Add(nFx)) {
420         aLIy.Append(nFx);
421       }
422     }
423     aLIx.Clear();
424     aLIx.Append(aLIy);
425   }
426   //==================================
427   //
428   // 3. Collect vertices from DS
429   Standard_Integer aNbS, nV, nVSD, aNbVDS, i1, i2, aNbVSD;
430   //
431   aNbS=myDS->NumberOfShapesOfTheObject();
432   // old shapes
433   for (i=1; i<=aNbS; ++i) {
434     const TopoDS_Shape& aS=myDS->Shape(i);
435     if (aS.ShapeType() != TopAbs_VERTEX){
436       continue;
437     }
438     //
439     nVSD=FindSDVertex(i); 
440     nV=(nVSD) ? nVSD : i;
441     const TopoDS_Shape& aVx=myDS->Shape(nV);
442     if (!aDMVI.IsBound(aVx)) {
443       aDMVI.Bind(aVx, nV);
444     }
445   }
446   // new shapes
447   i1=myDS->NumberOfSourceShapes()+1;
448   i2=myDS->NumberOfInsertedShapes();
449   for (i=i1; i<=i2; ++i) {
450     const TopoDS_Shape aS=myDS->Shape(i);//mpv
451     if (aS.ShapeType() != TopAbs_VERTEX){
452       continue;
453     }
454     if (!aDMVI.IsBound(aS)) {
455       aDMVI.Bind(aS, i);
456     }
457   }
458   // 
459   // 4. Initialize BoundSortBox on aDMVI
460   //
461   Handle(Bnd_HArray1OfBox) aHAB;
462   Bnd_BoundSortBox aBSB;
463   //
464   aNbVDS=aDMVI.Extent();
465   aHAB=new Bnd_HArray1OfBox(1, aNbVDS);
466   //
467   aItDMVI.Initialize(aDMVI);
468   for (i=1; aItDMVI.More(); aItDMVI.Next(), ++i) {
469     Bnd_Box aBox;
470     //
471     nV=aItDMVI.Value();
472     aV=TopoDS::Vertex(aItDMVI.Key());
473     aTolV=BRep_Tool::Tolerance(aV);
474     aBox.SetGap(aTolV);
475     BRepBndLib::Add(aV, aBox);
476     aHAB->SetValue(i, aBox);
477     //
478     aDMIV.Bind(i, aV);
479   }
480   aBSB.Initialize(aHAB);
481   //
482   // 5. Compare 
483   aItDMVFF.Initialize(aDMVFF1);
484   for (;  aItDMVFF.More(); aItDMVFF.Next()) {
485     Bnd_Box aBoxV;
486     //
487     const TColStd_ListOfInteger& aLIFF=aItDMVFF.Value();
488     aV=TopoDS::Vertex(aItDMVFF.Key());
489     //
490     aTolV=BRep_Tool::Tolerance(aV);
491     aBoxV.SetGap(aTolV);
492     BRepBndLib::Add(aV, aBoxV);
493     //
494     const TColStd_ListOfInteger& aLIVSD=aBSB.Compare(aBoxV);
495     aNbVSD=aLIVSD.Extent();
496     if (aNbVSD==0) {
497       // add new vertex in DS and update map myAloneVertices
498       BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
499       //
500       myDS->InsertShapeAndAncestorsSuccessors(aV, anASSeq);
501       nV=myDS->NumberOfInsertedShapes();
502       //
503       aIt.Initialize(aLIFF);
504       for (; aIt.More(); aIt.Next()) {
505         nFx=aIt.Value();
506         if (myAloneVertices.Contains(nFx)) {
507           TColStd_IndexedMapOfInteger& aMVx=myAloneVertices.ChangeFromKey(nFx);
508           aMVx.Add(nV);
509         }
510         else {
511           TColStd_IndexedMapOfInteger aMVx;
512           aMVx.Add(nV);
513           myAloneVertices.Add(nFx, aMVx);
514         }
515       }
516     }
517   }
518 }
519 //=======================================================================
520 // function: AloneVertices
521 // purpose: 
522 //=======================================================================
523   const NMTTools_IndexedDataMapOfIndexedMapOfInteger& NMTTools_PaveFiller::AloneVertices()const
524 {
525   return myAloneVertices;
526 }
527 //=======================================================================
528 // function: FuseVertices
529 // purpose: 
530 //=======================================================================
531 void FuseVertices(const TopoDS_Shape& aCompound,
532                   TopTools_DataMapOfShapeShape& aDMVV)
533 {
534   Standard_Integer i, aNbVV, n1, n2, nX;
535   NMTDS_ShapesDataStructure tDS;
536   //
537   tDS.SetCompositeShape(aCompound);
538   tDS.Init();
539   //
540   BOPTools_InterferencePool tInterfPool(tDS);
541   NMTTools_PaveFiller tPaveFiller(tInterfPool);
542   //
543   tPaveFiller.Init();
544   //
545   tPaveFiller.PerformVV();
546   tPaveFiller.PerformNewVertices();
547   //
548   const BOPTools_CArray1OfVVInterference& aVVt=tInterfPool.VVInterfs();
549   //
550   aNbVV=aVVt.Extent();
551   for (i=1; i<=aNbVV; ++i) {
552     const BOPTools_VVInterference& aVV=aVVt(i);
553     aVV.Indices(n1, n2);
554     nX=aVV.NewShape();
555     if (nX) {
556       const TopoDS_Shape& aV1=tDS.Shape(n1);
557       const TopoDS_Shape& aV2=tDS.Shape(n2);
558       const TopoDS_Shape& aVx=tDS.Shape(nX);
559       aDMVV.Bind(aV1, aVx);
560       aDMVV.Bind(aV2, aVx);
561     }
562   }
563 }
564 // Contribution of Samtech www.samcef.com END