Salome HOME
New version of PartitionAlgo.
[modules/geom.git] / src / NMTTools / NMTTools_DEProcessor.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_DEProcessor.cxx
21 // Created:     Wed Sep 12 12:10:52 2001
22 // Author:      Peter KURNEV
23 //              <pkv@irinox>
24
25 #include <NMTTools_DEProcessor.ixx>
26
27 #include <Precision.hxx>
28
29 #include <TColStd_ListIteratorOfListOfInteger.hxx>
30 #include <TColStd_ListOfInteger.hxx>
31
32 #include <gp_Pnt2d.hxx>
33 #include <gp_Pnt.hxx>
34 #include <gp_Sphere.hxx>
35
36 #include <Geom2d_Curve.hxx>
37 #include <Geom2d_Line.hxx>
38 #include <Geom2dAdaptor_Curve.hxx>
39 #include <Geom2dInt_GInter.hxx>
40
41 #include <IntRes2d_IntersectionPoint.hxx>
42
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Edge.hxx>
45 #include <TopoDS.hxx>
46 #include <TopoDS_Face.hxx>
47 #include <TopoDS_Vertex.hxx>
48 #include <TopoDS_Solid.hxx>
49
50 #include <TopExp.hxx>
51 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
52 #include <TopTools_ListOfShape.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
54
55 #include <BRep_Tool.hxx>
56 #include <BRep_Builder.hxx>
57
58 #include <BRepAdaptor_Surface.hxx>
59
60 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
61
62 #include <IntTools_Tools.hxx>
63 #include <IntTools_Context.hxx>
64
65 #include <BOPTools_DEInfo.hxx>
66 #include <BOPTools_Pave.hxx>
67 #include <BOPTools_ListOfPave.hxx>
68 #include <BOPTools_ListIteratorOfListOfPave.hxx>
69 #include <BOPTools_PaveBlock.hxx>
70 #include <BOPTools_ListOfPaveBlock.hxx>
71 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
72 #include <BOPTools_PaveBlockIterator.hxx>
73 #include <BOPTools_SSInterference.hxx>
74 #include <BOPTools_PavePool.hxx>
75 #include <BOPTools_PaveSet.hxx>
76 #include <BOPTools_Tools3D.hxx>
77 #include <BOPTools_InterferencePool.hxx>
78 #include <BOPTools_CArray1OfSSInterference.hxx>
79 #include <BOPTools_SplitShapesPool.hxx>
80
81 #include <NMTDS_ShapesDataStructure.hxx>
82
83 #include <NMTTools_PaveFiller.hxx>
84 //
85 #include <BOPTools_SequenceOfCurves.hxx>
86 #include <BOPTools_Curve.hxx>
87 //
88 #include <Geom2d_TrimmedCurve.hxx>
89
90 #include <ElCLib.hxx>
91 #include <gp_Lin2d.hxx>
92
93
94
95 //=======================================================================
96 // function: NMTTools_DEProcessor::NMTTools_DEProcessor
97 // purpose: 
98 //=======================================================================
99   NMTTools_DEProcessor::NMTTools_DEProcessor(NMTTools_PaveFiller& aPaveFiller)
100 :
101   myIsDone(Standard_False)
102 {
103   myFiller=(NMTTools_PaveFiller*) &aPaveFiller;
104   myDS=myFiller->DS();
105 }
106 //=======================================================================
107 // function: IsDone
108 // purpose: 
109 //=======================================================================
110   Standard_Boolean NMTTools_DEProcessor::IsDone() const
111 {
112   return myIsDone;
113 }
114 //=======================================================================
115 // function:  Do
116 // purpose: 
117 //=======================================================================
118   void NMTTools_DEProcessor::Do()
119 {
120   Standard_Integer aNbE;
121   myIsDone=Standard_False;
122
123   FindDegeneratedEdges();
124   aNbE=myDEMap.Extent();
125   
126   if (!aNbE) {
127     myIsDone=Standard_True;
128     return;
129   }
130   DoPaves();
131 }
132 //=======================================================================
133 // function:  FindDegeneratedEdges
134 // purpose: 
135 //=======================================================================
136   void NMTTools_DEProcessor::FindDegeneratedEdges()
137 {
138   Standard_Integer i, aNb, nV, nF, nVx, ip, iRankE;
139   TopTools_IndexedDataMapOfShapeListOfShape aMEF;
140   //
141   aNb=myDS->NumberOfShapesOfTheObject();
142   //
143   for (i=1; i<=aNb; i++) {
144     const TopoDS_Shape aF=myDS->Shape(i);//mpv
145     if (aF.ShapeType()==TopAbs_FACE) {
146       TopExp::MapShapesAndAncestors (aF, TopAbs_EDGE, TopAbs_FACE, aMEF);
147     }
148   }
149   //
150   for (i=1; i<=aNb; i++) {
151     const TopoDS_Shape aS=myDS->Shape(i);//mpv
152     if (aS.ShapeType()==TopAbs_EDGE) {
153       const TopoDS_Edge& aE=TopoDS::Edge(aS);
154       
155       if (BRep_Tool::Degenerated(aE)) {
156         iRankE=myDS->Rank(i);
157
158         TopoDS_Vertex aV=TopExp::FirstVertex(aE);
159
160         nVx=myDS->ShapeIndex(aV, iRankE);
161         //
162         nV=nVx;
163         ip=myFiller->FindSDVertex(nV);
164         if (ip) {
165           nV=ip;
166         }
167         //
168         TColStd_ListOfInteger aLFn;
169         const TopTools_ListOfShape& aLF=aMEF.FindFromKey(aE);
170         TopTools_ListIteratorOfListOfShape anIt(aLF);
171         for (; anIt.More(); anIt.Next()) {
172           const TopoDS_Shape& aF=anIt.Value();
173           nF=myDS->ShapeIndex(aF, iRankE);
174           aLFn.Append(nF);
175         }
176         BOPTools_DEInfo aDEInfo;
177         aDEInfo.SetVertex(nV);
178         aDEInfo.SetFaces(aLFn);
179
180         myDEMap.Add (i, aDEInfo);
181       }
182     }
183   }
184 }
185 //=======================================================================
186 // function:  DoPaves
187 // purpose: 
188 //=======================================================================
189   void NMTTools_DEProcessor::DoPaves()
190 {
191
192   Standard_Integer i, aNbE, nED, nVD, nFD=0;
193   //
194   aNbE=myDEMap.Extent();
195   for (i=1; i<=aNbE; i++) {
196     nED=myDEMap.FindKey(i);
197     
198     const BOPTools_DEInfo& aDEInfo=myDEMap(i);
199     nVD=aDEInfo.Vertex();
200     // Fill PaveSet for the edge nED
201     const TColStd_ListOfInteger& nLF=aDEInfo.Faces();
202     TColStd_ListIteratorOfListOfInteger anIt(nLF);
203     for (; anIt.More(); anIt.Next()) {
204       nFD=anIt.Value();
205       
206       BOPTools_ListOfPaveBlock aLPB;
207       FindPaveBlocks(nED, nVD, nFD, aLPB);
208       FillPaveSet (nED, nVD, nFD, aLPB);
209     }
210     // 
211     // Fill aSplitEdges for the edge nED
212     FillSplitEdgesPool(nED);
213     //
214     // MakeSplitEdges
215     MakeSplitEdges(nED, nFD);
216     //
217   }// next nED
218 }
219 //=======================================================================
220 // function:  FindPaveBlocks
221 // purpose: 
222 //=======================================================================
223   void NMTTools_DEProcessor::FindPaveBlocks(const Standard_Integer ,
224                                             const Standard_Integer nVD,
225                                             const Standard_Integer nFD,
226                                             BOPTools_ListOfPaveBlock& aLPBOut)
227 {
228   BOPTools_ListIteratorOfListOfPaveBlock anIt;
229   Standard_Integer i, aNb, nF2, nV;
230   //
231   BOPTools_CArray1OfSSInterference& aFFs=(myFiller->InterfPool())->SSInterferences();
232   //
233   aNb=aFFs.Extent();
234   for (i=1; i<=aNb; ++i) {
235     BOPTools_SSInterference& aFF=aFFs(i);
236     //
237     nF2=aFF.OppositeIndex(nFD);
238     if (!nF2) {
239       continue;
240     }
241     //
242     // Split Parts 
243     const BOPTools_ListOfPaveBlock& aLPBSplits=aFF.PaveBlocks();
244     anIt.Initialize(aLPBSplits);
245     for (; anIt.More(); anIt.Next()) {
246       const BOPTools_PaveBlock& aPBSp=anIt.Value();
247       //
248       const BOPTools_Pave& aPave1=aPBSp.Pave1();
249       nV=aPave1.Index();
250       if (nV==nVD) {
251         aLPBOut.Append(aPBSp);
252         continue;
253       }
254       //
255       const BOPTools_Pave& aPave2=aPBSp.Pave2();
256       nV=aPave2.Index();
257       if (nV==nVD) {
258         aLPBOut.Append(aPBSp);
259         continue;
260       }
261     }
262     //
263     // Section Parts
264     Standard_Integer j, aNbCurves;   
265     BOPTools_SequenceOfCurves& aSC=aFF.Curves();
266     aNbCurves=aSC.Length();
267     
268     for (j=1; j<=aNbCurves; j++) {
269       const BOPTools_Curve& aBC=aSC(j);
270       const BOPTools_ListOfPaveBlock& aLPBSe=aBC.NewPaveBlocks();
271
272       anIt.Initialize(aLPBSe);
273       for (; anIt.More(); anIt.Next()) {
274         const BOPTools_PaveBlock& aPBSe=anIt.Value();
275         
276         const BOPTools_Pave& aPv1=aPBSe.Pave1();
277         nV=aPv1.Index();
278         if (nV==nVD) {
279           aLPBOut.Append(aPBSe);
280           continue;
281         }
282         
283         const BOPTools_Pave& aPv2=aPBSe.Pave2();
284         nV=aPv2.Index();
285         if (nV==nVD) {
286           aLPBOut.Append(aPBSe);
287           continue;
288         }
289       }
290     }
291   }
292 }
293 //=======================================================================
294 // function:  FillPaveSet
295 // purpose: 
296 //=======================================================================
297   void NMTTools_DEProcessor::FillPaveSet (const Standard_Integer nED,
298                                           const Standard_Integer nVD,
299                                           const Standard_Integer nFD,
300                                           const BOPTools_ListOfPaveBlock& aLPB)
301 {
302   Standard_Boolean bIsDone, bXDir, bRejectFlag;
303   Standard_Integer nE, aNbPoints, j;
304   Standard_Real aTD1, aTD2, aT1, aT2, aTolInter, aX, aDT;
305   //
306   aDT=Precision::PConfusion();
307   //
308   BOPTools_PaveSet& aPaveSet= (myFiller->ChangePavePool()).ChangeValue(myDS->RefEdge(nED));
309   //
310   // Clear aPaveSet, aSplitEdges
311   aPaveSet.ChangeSet().Clear();
312   //
313   const TopoDS_Edge aDE=TopoDS::Edge(myDS->Shape(nED));//mpv
314   const TopoDS_Face aDF=TopoDS::Face(myDS->Shape(nFD));//mpv
315   //
316   // 2D Curve of degenerated edge on the face aDF
317   // Modified  Thu Sep 14 14:35:18 2006 
318   // Contribution of Samtech www.samcef.com BEGIN
319   //Handle(Geom2d_Curve) aC2DDE=BRep_Tool::CurveOnSurface(aDE, aDF, aTD1, aTD2);
320   Handle(Geom2d_Curve) aC2DDE1=BRep_Tool::CurveOnSurface(aDE, aDF, aTD1, aTD2);
321   Handle(Geom2d_TrimmedCurve)aC2DDE=new Geom2d_TrimmedCurve(aC2DDE1, aTD1, aTD2);
322   // Contribution of Samtech www.samcef.com END 
323   //
324   // Choose direction for Degenerated Edge
325   gp_Pnt2d aP2d1, aP2d2;
326   aC2DDE->D0(aTD1, aP2d1);
327   aC2DDE->D0(aTD2, aP2d2);
328
329   bXDir=Standard_False;
330   if (fabs(aP2d1.Y()-aP2d2.Y()) < aDT){
331     bXDir=!bXDir;
332   }
333   //
334   // Prepare bounding Paves
335   BOPTools_Pave aPave1 (nVD, aTD1, BooleanOperations_UnknownInterference);
336   aPaveSet.Append(aPave1);
337   BOPTools_Pave aPave2 (nVD, aTD2, BooleanOperations_UnknownInterference);
338   aPaveSet.Append(aPave2);
339   //
340   // Fill other paves 
341   BOPTools_ListIteratorOfListOfPaveBlock anIt(aLPB);
342   for (; anIt.More(); anIt.Next()) {
343     const BOPTools_PaveBlock& aPB=anIt.Value();
344     nE=aPB.Edge();
345     const TopoDS_Edge aE=TopoDS::Edge(myDS->Shape(nE));//mpv
346     
347     Handle(Geom2d_Curve) aC2D=BRep_Tool::CurveOnSurface(aE, aDF, aT1, aT2);
348     //
349     // Intersection
350     aTolInter=0.001;
351         
352     Geom2dAdaptor_Curve aGAC1, aGAC2;
353     
354     aGAC1.Load(aC2DDE, aTD1, aTD2);
355     Handle(Geom2d_Line) aL2D= Handle(Geom2d_Line)::DownCast(aC2D);
356     if (!aL2D.IsNull()) {
357       aGAC2.Load(aC2D);
358     }
359     else {
360       aGAC2.Load(aC2D, aT1, aT2);
361     }
362     
363     Geom2dInt_GInter aGInter(aGAC1, aGAC2, aTolInter, aTolInter);
364     
365     bIsDone=aGInter.IsDone();
366     if(bIsDone) {
367       aNbPoints=aGInter.NbPoints();
368       if (aNbPoints) { 
369         for (j=1; j<=aNbPoints; ++j) {
370           gp_Pnt2d aP2D=aGInter.Point(j).Value();
371           //
372           // Modified to obtain exact parameter Thu Sep 14 14:35:18 2006 
373           // Contribution of Samtech www.samcef.com BEGIN
374           Handle(Geom2d_Line) aCLDE=Handle(Geom2d_Line)::DownCast(aC2DDE1);
375           if (aCLDE.IsNull()) {
376             continue;
377           }
378           gp_Lin2d aLDE=aCLDE->Lin2d();
379           aX=ElCLib::Parameter(aLDE, aP2D);
380           //
381           //aX=(bXDir) ? aP2D.X(): aP2D.Y();
382           // Contribution of Samtech www.samcef.com END 
383           //
384           if (fabs (aX-aTD1) < aDT || fabs (aX-aTD2) < aDT) {
385             continue; 
386           }
387           if (aX < aTD1 || aX > aTD2) {
388             continue; 
389           }
390           //
391           bRejectFlag=Standard_False;
392           const BOPTools_ListOfPave& aListOfPave=aPaveSet.Set();
393           BOPTools_ListIteratorOfListOfPave aPaveIt(aListOfPave);
394           for (; aPaveIt.More(); aPaveIt.Next()) {
395             const BOPTools_Pave& aPavex=aPaveIt.Value();
396             Standard_Real aXx=aPavex.Param();
397             if (fabs (aX-aXx) < aDT) {
398               bRejectFlag=Standard_True;
399               break;
400             }
401           }
402           if (bRejectFlag) {
403             continue; 
404           }
405           //
406           BOPTools_Pave aPave(nVD, aX, BooleanOperations_UnknownInterference);
407           aPaveSet.Append(aPave);
408         }
409       }
410     }
411   }
412 }
413 //=======================================================================
414 // function:  FillSplitEdgesPool
415 // purpose: 
416 //=======================================================================
417   void NMTTools_DEProcessor::FillSplitEdgesPool (const Standard_Integer nED)
418 {
419   BOPTools_SplitShapesPool& aSplitShapesPool=myFiller->ChangeSplitShapesPool();
420   BOPTools_ListOfPaveBlock& aSplitEdges=aSplitShapesPool.ChangeValue(myDS->RefEdge(nED));
421   //
422   aSplitEdges.Clear();
423   //
424   const BOPTools_PavePool& aPavePool=myFiller->PavePool();
425   BOPTools_PavePool* pPavePool=(BOPTools_PavePool*) &aPavePool;
426   BOPTools_PaveSet& aPaveSet= pPavePool->ChangeValue(myDS->RefEdge(nED));
427   
428   BOPTools_PaveBlockIterator aPBIt(nED, aPaveSet);
429   for (; aPBIt.More(); aPBIt.Next()) {
430     BOPTools_PaveBlock& aPB=aPBIt.Value();
431     aSplitEdges.Append(aPB);
432   }
433 }
434 //=======================================================================
435 // function:  MakeSplitEdges
436 // purpose: 
437 //=======================================================================
438   void NMTTools_DEProcessor::MakeSplitEdges (const Standard_Integer nED,
439                                              const Standard_Integer nFD)
440 {
441   const BOPTools_SplitShapesPool& aSplitShapesPool=myFiller->SplitShapesPool();
442   const BOPTools_ListOfPaveBlock& aSplitEdges=aSplitShapesPool(myDS->RefEdge(nED));
443
444   Standard_Integer nV1, nV2, aNewShapeIndex;
445   Standard_Real    t1, t2;
446   TopoDS_Edge aE, aESplit;
447   TopoDS_Vertex aV1, aV2;
448
449   const TopoDS_Edge aDE=TopoDS::Edge(myDS->Shape(nED));//mpv
450   const TopoDS_Face aDF=TopoDS::Face(myDS->Shape(nFD));//mpv
451
452   BOPTools_ListIteratorOfListOfPaveBlock aPBIt(aSplitEdges);
453
454   for (; aPBIt.More(); aPBIt.Next()) {
455     BOPTools_PaveBlock& aPB=aPBIt.Value();
456     
457     const BOPTools_Pave& aPave1=aPB.Pave1();
458     nV1=aPave1.Index();
459     t1=aPave1.Param();
460     aV1=TopoDS::Vertex(myDS->GetShape(nV1));
461     aV1.Orientation(TopAbs_FORWARD);
462     
463     const BOPTools_Pave& aPave2=aPB.Pave2();
464     nV2=aPave2.Index();
465     t2=aPave2.Param();
466     aV2=TopoDS::Vertex(myDS->GetShape(nV2));
467     aV2.Orientation(TopAbs_REVERSED);
468     
469     MakeSplitEdge(aDE, aDF, aV1, t1, aV2, t2, aESplit); 
470     //
471     // Add Split Part of the Original Edge to the DS
472     BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
473     
474     anASSeq.SetNewSuccessor(nV1);
475     anASSeq.SetNewOrientation(aV1.Orientation());
476     
477     anASSeq.SetNewSuccessor(nV2);
478     anASSeq.SetNewOrientation(aV2.Orientation());
479     
480     myDS->InsertShapeAndAncestorsSuccessors(aESplit, anASSeq);
481     aNewShapeIndex=myDS->NumberOfInsertedShapes();
482     myDS->SetState(aNewShapeIndex, BooleanOperations_UNKNOWN);
483     //
484     // Fill Split Set for the Original Edge
485     aPB.SetEdge(aNewShapeIndex);
486     //
487   }
488 }
489 //=======================================================================
490 // function:  MakeSplitEdge
491 // purpose: 
492 //=======================================================================
493   void NMTTools_DEProcessor::MakeSplitEdge (const TopoDS_Edge&   aE,
494                                             const TopoDS_Face&   aF,
495                                             const TopoDS_Vertex& aV1,
496                                             const Standard_Real  aP1,
497                                             const TopoDS_Vertex& aV2,
498                                             const Standard_Real  aP2,
499                                             TopoDS_Edge& aNewEdge)
500 {
501   Standard_Real aTol=1.e-7;
502
503   TopoDS_Edge E=aE;
504
505   E.EmptyCopy();
506   BRep_Builder BB;
507   BB.Add  (E, aV1);
508   BB.Add  (E, aV2);
509
510   BB.Range(E, aF, aP1, aP2);
511
512   BB.Degenerated(E, Standard_True);
513
514   BB.UpdateEdge(E, aTol);
515   aNewEdge=E;
516 }
517 /*
518 //=======================================================================
519 // function: NMTTools_DEProcessor::NMTTools_DEProcessor
520 // purpose: 
521 //=======================================================================
522   NMTTools_DEProcessor::NMTTools_DEProcessor(NMTTools_PDSFiller& pDSFiller)
523 :
524   myIsDone(Standard_False)
525 {
526   myDSFiller=pDSFiller;
527   myFiller=(NMTTools_PaveFiller*) &(myDSFiller->PaveFiller());
528   myDS=myFiller->DS();
529 }
530 */