Salome HOME
Merge with version on tag OCC-V2_1_0d
[modules/geom.git] / src / NMTTools / NMTTools_PaveFiller_6.cxx
1 // File:        NMTTools_PaveFiller_6.cxx
2 // Created:     Fri Dec 19 10:27:31 2003
3 // Author:      Peter KURNEV
4 //              <pkv@irinox>
5
6 #include <NMTTools_PaveFiller.ixx>
7
8 #include <Precision.hxx>
9
10 #include <TColStd_IndexedMapOfInteger.hxx>
11 #include <TColStd_MapOfInteger.hxx>
12
13 #include <Geom2d_Curve.hxx>
14 #include <Geom_TrimmedCurve.hxx>
15
16 #include <GeomAdaptor_Curve.hxx>
17 #include <BndLib_Add3dCurve.hxx>
18
19 #include <TopoDS_Face.hxx>
20 #include <TopoDS.hxx>
21 #include <TopoDS_Compound.hxx>
22
23 #include <TopExp.hxx>
24
25 #include <BRep_Builder.hxx>
26 #include <BRep_Tool.hxx>
27 #include <BRepBndLib.hxx>
28
29 #include <TopTools_IndexedMapOfShape.hxx>
30
31 #include <BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger.hxx>
32 #include <BOPTColStd_IndexedDataMapOfIntegerInteger.hxx>
33
34 #include <BooleanOperations_IndexedDataMapOfShapeInteger.hxx>
35 #include <BooleanOperations_AncestorsSeqAndSuccessorsSeq.hxx>
36 #include <BooleanOperations_OnceExplorer.hxx>
37 #include <BooleanOperations_ShapesDataStructure.hxx>
38
39 #include <IntTools_SequenceOfPntOn2Faces.hxx>
40 #include <IntTools_SequenceOfCurves.hxx>
41 #include <IntTools_FaceFace.hxx>
42 #include <IntTools_Tools.hxx>
43
44 #include <BOPTools_CArray1OfSSInterference.hxx>
45 #include <BOPTools_ListIteratorOfListOfInterference.hxx>
46 #include <BOPTools_CArray1OfInterferenceLine.hxx>
47 #include <BOPTools_InterferenceLine.hxx>
48 #include <BOPTools_ListOfInterference.hxx>
49 #include <BOPTools_Interference.hxx>
50 #include <BOPTools_InterferencePool.hxx>
51 #include <BOPTools_SSInterference.hxx>
52 #include <BOPTools_ListOfPaveBlock.hxx>
53 #include <BOPTools_ListIteratorOfListOfPaveBlock.hxx>
54 #include <BOPTools_PaveBlock.hxx>
55 #include <BOPTools_ListIteratorOfListOfPave.hxx>
56 #include <BOPTools_Tools.hxx>
57 #include <BOPTools_PaveBlockIterator.hxx>
58 #include <BOPTools_Tools2D.hxx>
59
60 #include <NMTDS_ShapesDataStructure.hxx>
61 #include <NMTTools_IndexedDataMapOfShapePaveBlock.hxx>
62 #include <NMTTools_CommonBlockAPI.hxx>
63 #include <Geom2d_Curve.hxx>
64 #include <NMTTools_Tools.hxx>
65 #include <BRepLib.hxx>
66 #include <Geom2d_TrimmedCurve.hxx>
67
68 static 
69   Standard_Boolean IsPairFound(const Standard_Integer nF1,
70                                const Standard_Integer nF2,
71                                BOPTools_InterferencePool* myIntrPool,
72                                BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMapWhat,
73                                BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMapWith);
74
75 static
76   void FMapWhat(const Standard_Integer nF,
77                 BOPTools_InterferencePool* myIntrPool,
78                 TColStd_IndexedMapOfInteger& aMapWhat);
79 static
80   void FMapWith(const Standard_Integer nF,
81                 BOPTools_InterferencePool* myIntrPool,
82                 TColStd_IndexedMapOfInteger& aMapWith);
83 static
84   Standard_Boolean IsFound(const TColStd_IndexedMapOfInteger& aMapWhat,
85                            const TColStd_IndexedMapOfInteger& aMapWith);
86
87 //=======================================================================
88 // function: PerformFF
89 // purpose: 
90 //=======================================================================
91   void NMTTools_PaveFiller::PerformFF() 
92 {
93   myIsDone=Standard_False;
94   //
95   Standard_Boolean bIsFound, bJustAdd, bIsComputed;
96   Standard_Integer n1, n2, anIndexIn, nF1, nF2, aBlockLength, aNbFFs;
97   Standard_Boolean bToApproxC3d, bToApproxC2dOnS1, bToApproxC2dOnS2, bIsDone;
98   Standard_Integer aNbCurves, aNbPoints;
99   Standard_Real anApproxTol, aTolR3D, aTolR2D;
100   BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger aMapWhat, aMapWith;
101   IntTools_SequenceOfPntOn2Faces aPnts;
102   IntTools_SequenceOfCurves aCvs;
103   BooleanOperations_KindOfInterference aTypeFF=BooleanOperations_SurfaceSurface;
104   //
105   BOPTools_CArray1OfSSInterference& aFFs=myIntrPool->SSInterferences();
106   //
107   //  F/F Interferences  [BooleanOperations_SurfaceSurface]
108   myDSIt.Initialize(TopAbs_FACE, TopAbs_FACE);
109   //
110   // BlockLength correction
111   aNbFFs=ExpectedPoolLength();
112   aBlockLength=aFFs.BlockLength();
113   if (aNbFFs > aBlockLength) {
114     aFFs.SetBlockLength(aNbFFs);
115   }
116   //
117   for (; myDSIt.More(); myDSIt.Next()) {
118     myDSIt.Current(n1, n2, bJustAdd);
119     //
120     bIsComputed=myIntrPool->IsComputed(n1, n2);
121     if (bIsComputed) {
122       continue;
123     }
124     //
125     nF1 = n2;
126     nF2 = n1;
127     if(n1 < n2) {
128       nF1 = n1;
129       nF2 = n2;
130     }
131     anIndexIn=0;
132     aPnts.Clear();
133     aCvs.Clear();
134     //
135     bIsFound=IsPairFound(nF1, nF2, myIntrPool, aMapWhat, aMapWith);
136     //
137     if (bJustAdd) {
138       if (!bIsFound) {
139         myIntrPool->AddInterference (nF1, nF2, aTypeFF, anIndexIn);
140       }
141       else{
142         BOPTools_SSInterference anInterf (nF1, nF2, 1.e-07, 1.e-07, aCvs, aPnts);
143         anIndexIn=aFFs.Append(anInterf);
144         myIntrPool->AddInterference (nF1, nF2, aTypeFF, anIndexIn);
145       }
146       continue;
147     }
148     //
149     const TopoDS_Face& aF1=TopoDS::Face(myDS->Shape(nF1));
150     const TopoDS_Face& aF2=TopoDS::Face(myDS->Shape(nF2));
151     //
152     // FF
153     bToApproxC3d     = mySectionAttribute.Approximation();
154     bToApproxC2dOnS1 = mySectionAttribute.PCurveOnS1();
155     bToApproxC2dOnS2 = mySectionAttribute.PCurveOnS2();
156     //
157     anApproxTol=1.e-7;
158     //
159     IntTools_FaceFace aFF;
160     //
161     aFF.SetParameters (bToApproxC3d, bToApproxC2dOnS1, 
162                        bToApproxC2dOnS2, anApproxTol);
163     //
164     aFF.Perform(aF1, aF2);
165     //
166     bIsDone=aFF.IsDone();
167     //
168     if (!bIsDone) {
169       if (!bIsFound) {
170         myIntrPool->AddInterference (nF1, nF2, aTypeFF, anIndexIn);
171       }
172       else {
173         BOPTools_SSInterference anInterf (nF1, nF2, 1.e-07, 1.e-07, aCvs, aPnts);
174         anIndexIn=aFFs.Append(anInterf);
175         myIntrPool->AddInterference (nF1, nF2, aTypeFF, anIndexIn);
176       }
177       continue;
178     }
179     //
180     aTolR3D=aFF.TolReached3d();
181     aTolR2D=aFF.TolReached2d();
182     if (aTolR3D < 1.e-7){
183       aTolR3D=1.e-7;
184     } 
185     //
186     aFF.PrepareLines3D();
187     //
188     const IntTools_SequenceOfCurves& aCvsX=aFF.Lines();
189     const IntTools_SequenceOfPntOn2Faces& aPntsX=aFF.Points();
190     //
191     aNbCurves=aCvsX.Length();
192     aNbPoints=aPntsX.Length();
193     //
194     if (!aNbCurves && !aNbPoints) {
195       BOPTools_SSInterference anInterf (nF1, nF2, 1.e-07, 1.e-07, aCvs, aPnts);
196       anIndexIn=aFFs.Append(anInterf);
197       myIntrPool->AddInterference (nF1, nF2, aTypeFF, anIndexIn);
198       continue;
199     }
200     //
201     {
202       BOPTools_SSInterference anInterf (nF1, nF2, aTolR3D, aTolR2D, aCvsX, aPntsX);
203       anIndexIn=aFFs.Append(anInterf);
204       myIntrPool->AddInterference (nF1, nF2, aTypeFF, anIndexIn);
205     }
206     //
207   }// for (; myDSIt.More(); myDSIt.Next()) 
208   //
209   myIsDone=Standard_True;
210 }
211 //=======================================================================
212 // function: MakeBlocks
213 // purpose: 
214 //=======================================================================
215   void NMTTools_PaveFiller::MakeBlocks()
216 {
217   myIsDone=Standard_False;
218   //
219   Standard_Boolean bIsExistingPaveBlock, bIsValidIn2D;
220   Standard_Integer i, aNbFFs, nF1, nF2, aBid=0;
221   Standard_Integer nV1, nV2, j, aNbCurves;
222   Standard_Real aTolR3D, aTol2D, aT1, aT2, aTolPPC=Precision::PConfusion();
223   TColStd_MapOfInteger aMap;
224   NMTTools_IndexedDataMapOfShapePaveBlock aMEPB;
225   BooleanOperations_IndexedDataMapOfShapeInteger aMapEI;
226   //
227   BOPTools_CArray1OfSSInterference& aFFs=myIntrPool->SSInterferences();
228   //
229   // 1. Produce Section Edges from intersection curves
230   //    between each pair of faces
231   //
232   aNbFFs=aFFs.Extent();
233   for (i=1; i<=aNbFFs; ++i) {
234     BOPTools_SSInterference& aFFi=aFFs(i);
235     // 
236     // Faces
237     aFFi.Indices(nF1, nF2);
238     const TopoDS_Face& aF1=TopoDS::Face(myDS->Shape(nF1));
239     const TopoDS_Face& aF2=TopoDS::Face(myDS->Shape(nF2));
240     //
241     // Add blocks that are existing ones for this FF-interference
242     BOPTools_ListOfPaveBlock aLPB;
243     RealSplitsInFace (aBid, nF1, nF2, aLPB);
244     RealSplitsInFace (aBid, nF2, nF1, aLPB);
245     RealSplitsOnFace (aBid, nF1, nF2, aLPB);
246     //
247     aMap.Clear();
248     BOPTools_ListIteratorOfListOfPaveBlock anIt(aLPB);
249     for (; anIt.More(); anIt.Next()) {
250       const BOPTools_PaveBlock& aPB=anIt.Value();
251       aFFi.AppendBlock(aPB);
252       nV1=aPB.Pave1().Index();
253       nV2=aPB.Pave2().Index();
254       aMap.Add(nV1);
255       aMap.Add(nV2);
256     }
257     //
258     BOPTools_SequenceOfCurves& aSCvs=aFFi.Curves();
259     aNbCurves=aSCvs.Length();
260     if (!aNbCurves) {
261       continue;
262     }
263     //
264     aTolR3D=aFFi.TolR3D();
265     aTol2D=(aTolR3D < 1.e-3) ? 1.e-3 : aTolR3D;
266     //
267     BOPTools_PaveSet aPSF;
268     //
269     PrepareSetForFace (nF1, nF2, aPSF);
270     //
271     // Put Paves On Curves
272     for (j=1; j<=aNbCurves; ++j) {
273       BOPTools_Curve& aBC=aSCvs(j);
274       // DEBUG
275       const IntTools_Curve& aC=aBC.Curve();
276       Handle (Geom_Curve) aC3D= aC.Curve();
277       //
278       PutPaveOnCurve (aPSF, aTolR3D, aBC);
279     }
280     //
281     // Put bounding paves on curves
282     for (j=1; j<=aNbCurves; ++j) {
283       BOPTools_Curve& aBC=aSCvs(j);
284       PutBoundPaveOnCurve (aBC, aFFi);
285     }
286     //
287     //  Pave Blocks on Curves
288     for (j=1; j<=aNbCurves; ++j) {
289       BOPTools_Curve& aBC=aSCvs(j);
290       const IntTools_Curve& aIC= aBC.Curve();
291       BOPTools_PaveSet& aPaveSet=aBC.Set();
292       //
293       BOPTools_PaveBlockIterator aPBIter(0, aPaveSet);
294       for (; aPBIter.More(); aPBIter.Next()) {
295         BOPTools_PaveBlock& aPBNew=aPBIter.Value();
296         aPBNew.SetCurve(aIC);
297         aPBNew.SetFace1(nF1);
298         aPBNew.SetFace2(nF2);
299         //
300         nV1=aPBNew.Pave1().Index();
301         nV2=aPBNew.Pave2().Index();
302         aT1=aPBNew.Pave1().Param();
303         aT2=aPBNew.Pave2().Param();
304         // ???
305         if((nV1==nV2) && (Abs(aT2 - aT1) < aTolPPC)) {
306           continue;// mkk ft
307         }
308         //
309         bIsExistingPaveBlock=IsExistingPaveBlock(aPBNew, aFFi);
310         if (bIsExistingPaveBlock) {
311           // aPBNew was (or just lays) boundary PB 
312           continue;
313         }
314         // Checking of validity in 2D
315         //
316         bIsValidIn2D=myContext.IsValidBlockForFaces(aT1, aT2, aIC, aF1, aF2, aTol2D);
317         if (!bIsValidIn2D) {
318           continue;
319         }
320         //
321         //aBC.AppendNewBlock(aPBNew);
322         //
323         // Make Section Edge  
324         TopoDS_Edge aES;
325         //
326         const TopoDS_Vertex& aV1=TopoDS::Vertex(myDS->Shape(nV1));
327         const TopoDS_Vertex& aV2=TopoDS::Vertex(myDS->Shape(nV2));
328         //
329         BOPTools_Tools::MakeSectEdge (aIC, aV1, aT1, aV2, aT2, aES);
330         //
331         {
332           Standard_Real aTolR2D;
333           //
334           aTolR2D=aFFi.TolR2D();
335           Handle(Geom2d_Curve) aC2D1=aIC.FirstCurve2d();
336           Handle(Geom2d_Curve) aC2D2=aIC.SecondCurve2d();
337           //
338           NMTTools_Tools::MakePCurve(aES, aF1, aC2D1, aTolR2D);
339           NMTTools_Tools::MakePCurve(aES, aF2, aC2D2, aTolR2D);
340         }
341         //
342         aMEPB.Add(aES, aPBNew);
343         aMapEI.Add(aES, i);
344       }
345     } // end of for (j=1; j<=aNbCurves; ++j)
346   }// for (i=1; i<=aNbFFs; ++i)
347   //=============================================================
348   //
349   // II. Post treatment 
350   //
351   // Input data: aMEPB, aMapEI
352   // Result    : section edges in myDS
353   //
354   Standard_Integer aNbSE;
355   //
356   aNbSE=aMEPB.Extent();
357   if (!aNbSE) {
358     // there is nothing to do here
359     return;
360   } 
361   //
362   BRep_Builder aBB;
363   TopoDS_Compound aCompound;
364   //
365   // 1. Make compound from SE
366   aBB.MakeCompound(aCompound);
367   for (i=1; i<=aNbSE; ++i) {
368     const TopoDS_Shape& aSE=aMEPB.FindKey(i);
369     aBB.Add(aCompound, aSE);
370   }
371   //
372   //
373   // 2. Intersect SE using auxiliary Filler
374   NMTDS_ShapesDataStructure tDS;
375   //
376   tDS.SetCompositeShape(aCompound);
377   tDS.Init();
378   //
379   BOPTools_InterferencePool tIP(tDS);
380   NMTTools_PaveFiller tPF(tIP);
381   //
382   // 2.1.VV
383   tPF.Init();
384   tPF.PerformVV();
385   tPF.PerformNewVertices();
386   //
387   // 2.2.VE
388   tPF.myPavePool.Resize (tPF.myNbEdges);
389   tPF.PrepareEdges();
390   tPF.PerformVE();
391   //
392   // 2.3.VF
393   tPF.PerformVF();
394   //
395   // 2.4.EE
396   tPF.myCommonBlockPool.Resize (tPF.myNbEdges);
397   tPF.mySplitShapesPool.Resize (tPF.myNbEdges);
398   tPF.myPavePoolNew    .Resize (tPF.myNbEdges);
399   
400   tPF.PreparePaveBlocks(TopAbs_VERTEX, TopAbs_EDGE);
401   tPF.PreparePaveBlocks(TopAbs_EDGE, TopAbs_EDGE);
402   //
403   tPF.PerformEE();
404   //
405   tPF.RefinePavePool ();
406   //
407   tPF.myPavePoolNew.Destroy();
408   //
409   tPF.MakeSplitEdges();
410   tPF.UpdateCommonBlocks();
411   //
412   // 3. Treatment of the result of intersection
413   //
414   Standard_Integer aNbOld, aNbLines, aNbPB, mV1, mV2, nE, mE, iFF;
415   TopAbs_ShapeEnum aType;
416   BOPTools_ListIteratorOfListOfPaveBlock aIt;
417   BOPTColStd_IndexedDataMapOfIntegerInteger aMNewOld;
418   //
419   const BOPTools_SplitShapesPool& aSSP=tPF.mySplitShapesPool;
420   const NMTTools_CommonBlockPool& aCBP=tPF.myCommonBlockPool;
421   //
422   aNbLines=tDS.NumberOfInsertedShapes();
423   aNbOld=tDS.NumberOfShapesOfTheObject();
424   // 
425   // 3.1 Links between indices in tDS and DS (kept in aMNewOld)
426   //
427   // 3.1.1.Old vertices [ links ]
428   for (i=1; i<=aNbOld; ++i) {
429     const TopoDS_Shape& aV=tDS.Shape(i);
430     aType=aV.ShapeType();
431     if (aType!=TopAbs_VERTEX) {
432       continue;
433     }
434     //
435     for (j=1; j<=aNbSE; ++j) {
436       const BOPTools_PaveBlock& aPBSE=aMEPB(j);
437       nV1=aPBSE.Pave1().Index();
438       const TopoDS_Shape& aV1=myDS->Shape(nV1);
439       if (aV1.IsSame(aV)) {
440         aMNewOld.Add(i, nV1);
441         break;
442       }
443       nV2=aPBSE.Pave2().Index();
444       const TopoDS_Shape& aV2=myDS->Shape(nV2);
445       if (aV2.IsSame(aV)) {
446         aMNewOld.Add(i, nV2);
447         break;
448       }
449     }
450   }
451   //
452   // 3.1.2. New vertices [ links ]
453   i=tDS.NumberOfSourceShapes()+1;
454   for (; i<=aNbLines; ++i) {
455     const TopoDS_Shape& aV=tDS.Shape(i);
456     aType=aV.ShapeType();
457     if (aType!=TopAbs_VERTEX) {
458       continue;
459     }
460     //
461     // Insert new vertex in myDS
462     BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
463     myDS->InsertShapeAndAncestorsSuccessors(aV, anASSeq);
464     nV1=myDS->NumberOfInsertedShapes();
465     // link
466     aMNewOld.Add(i, nV1);
467   }
468   //
469   // 3.2. Treatment of section edges (SE)
470   for (i=1; i<=aNbOld; ++i) {
471     const TopoDS_Shape& aE=tDS.Shape(i);
472     aType=aE.ShapeType();
473     if (aType!=TopAbs_EDGE) {
474       continue;
475     }
476     //
477     //  block of section edge that we already have for this SE
478     BOPTools_PaveBlock& aPBSE=aMEPB.ChangeFromKey(aE);
479     //
480     // Corresponding FF-interference
481     iFF=aMapEI.FindFromKey(aE);
482     BOPTools_SSInterference& aFFi=aFFs(iFF);
483     BOPTools_SequenceOfCurves& aSCvs=aFFi.Curves();
484     //
485     BOPTools_Curve& aBC=aSCvs(1); 
486     //
487     const BOPTools_ListOfPaveBlock& aLPB=aSSP(tDS.RefEdge(i));
488     aNbPB=aLPB.Extent();
489     //
490     if (!aNbPB) {
491       // no pave blocks -> use aPBSE and whole edge aE
492       BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
493       //
494       nV1=aPBSE.Pave1().Index();
495       const TopoDS_Shape& aV1=myDS->Shape(nV1);
496       nV2=aPBSE.Pave2().Index();
497       const TopoDS_Shape& aV2=myDS->Shape(nV2);
498       //
499       anASSeq.SetNewSuccessor(nV1);
500       anASSeq.SetNewOrientation(aV1.Orientation());
501       anASSeq.SetNewSuccessor(nV2);
502       anASSeq.SetNewOrientation(aV2.Orientation());
503       //
504       myDS->InsertShapeAndAncestorsSuccessors(aE, anASSeq);
505       nE=myDS->NumberOfInsertedShapes();
506       //
507       aPBSE.SetEdge(nE);
508       aBC.AppendNewBlock(aPBSE);
509       //
510       continue;
511     }
512     //
513     nF1=aPBSE.Face1();
514     nF2=aPBSE.Face2();
515     const TopoDS_Face& aF1=TopoDS::Face(myDS->Shape(nF1));
516     const TopoDS_Face& aF2=TopoDS::Face(myDS->Shape(nF2));
517     //
518     const NMTTools_ListOfCommonBlock& aLCB=aCBP(tDS.RefEdge(i));
519     NMTTools_CommonBlockAPI aCBAPI(aLCB);
520     //
521     aIt.Initialize(aLPB);
522     for (; aIt.More(); aIt.Next()) {
523       BOPTools_PaveBlock aPB=aIt.Value();
524       //
525       if (aCBAPI.IsCommonBlock(aPB)) {
526         // it can be Common Block
527         Standard_Boolean bHasPCOnF, bFound;
528         Standard_Integer nF, k, nEOrx, nF1x, nF2x;
529         Standard_Real aTolEx, aT1x, aT2x;
530         BOPTools_ListIteratorOfListOfPaveBlock aItPBx;
531         //
532         NMTTools_CommonBlock& aCB=aCBAPI.CommonBlock(aPB);
533         const BOPTools_ListOfPaveBlock& aLPBx=aCB.PaveBlocks();
534         //
535         aPB=aCB.PaveBlock1();
536         mE=aPB.Edge(); // index of edge in tDS
537         const TopoDS_Edge& aEx=TopoDS::Edge(tDS.Shape(mE));
538         aTolEx=BRep_Tool::Tolerance(aEx);
539         //
540         for (k=0; k<2; ++k) {   
541           nF=(!k) ? nF1 : nF2;
542           const TopoDS_Face& aF=TopoDS::Face(myDS->Shape(nF));
543           //
544           bHasPCOnF=BOPTools_Tools2D::HasCurveOnSurface(aEx, aF); 
545           if (bHasPCOnF) {
546             continue;
547           }
548           //
549           bFound=Standard_False;
550           aItPBx.Initialize(aLPBx);
551           for (; aItPBx.More(); aItPBx.Next()) {
552             BOPTools_PaveBlock& aPBx=aIt.Value();
553             nEOrx=aPBx.OriginalEdge();
554             const TopoDS_Shape& aEOrx=tDS.Shape(nEOrx);
555             BOPTools_PaveBlock& aPBSEx=aMEPB.ChangeFromKey(aEOrx);
556             aT1x=aPBSEx.Pave1().Param();
557             aT2x=aPBSEx.Pave2().Param();
558             const IntTools_Curve& aICx=aPBSEx.Curve();
559             //
560             nF1x=aPBSEx.Face1();
561             nF2x=aPBSEx.Face2();
562             //
563             if (nF1x==nF) {
564               Handle(Geom2d_Curve) aC2D1x=aICx.FirstCurve2d();
565               Handle(Geom2d_TrimmedCurve)aC2D1xT =new Geom2d_TrimmedCurve(aC2D1x, aT1x, aT2x);
566               aBB.UpdateEdge(aEx, aC2D1xT, aF, aTolEx);
567               bFound=!bFound;
568               break;
569             }
570             //
571             if (nF2x==nF) {
572               Handle(Geom2d_Curve) aC2D2x=aICx.SecondCurve2d();
573               Handle(Geom2d_TrimmedCurve)aC2D2xT =new Geom2d_TrimmedCurve(aC2D2x, aT1x, aT2x);
574               aBB.UpdateEdge(aEx, aC2D2xT, aF, aTolEx);
575               bFound=!bFound;
576               break;
577             }
578           }
579           if (bFound){
580             BRepLib::SameParameter(aEx, aTolEx, Standard_True);
581           }
582         }
583       } //if (aCBAPI.IsCommonBlock(aPB))
584       //
585       // new SE
586       mE=aPB.Edge(); // index of edge in tDS
587       const TopoDS_Shape& aSp=tDS.Shape(mE);
588       //
589       const BOPTools_Pave& aPave1=aPB.Pave1();
590       aT1=aPave1.Param();
591       mV1=aPave1.Index();            // index in tDS
592       nV1=aMNewOld.FindFromKey(mV1); // index in myDS
593       const TopoDS_Shape& aV1=myDS->Shape(nV1);
594       //
595       const BOPTools_Pave& aPave2=aPB.Pave2();
596       aT2=aPave2.Param();
597       mV2=aPave2.Index();
598       nV2=aMNewOld.FindFromKey(mV2);
599       const TopoDS_Shape& aV2=myDS->Shape(nV2);
600       //
601       if (!aMNewOld.Contains(mE)) {
602         // add new SE to the myDS
603         BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
604         //
605         anASSeq.SetNewSuccessor(nV1);
606         anASSeq.SetNewOrientation(aV1.Orientation());
607
608         anASSeq.SetNewSuccessor(nV2);
609         anASSeq.SetNewOrientation(aV2.Orientation());
610         
611         myDS->InsertShapeAndAncestorsSuccessors(aSp, anASSeq);
612         nE=myDS->NumberOfInsertedShapes();
613         //
614         aMNewOld.Add(mE, nE);
615       }
616       else {
617         nE=aMNewOld.FindFromKey(mE);
618       }
619       // Form PaveBlock;
620       BOPTools_PaveBlock aPBx;
621       BOPTools_Pave aP1, aP2;
622       //
623       aPBx.SetFace1(nF1);
624       aPBx.SetFace1(nF2);
625       //
626       aP1.SetIndex(nV1);
627       aP1.SetParam(aT1);
628       //
629       aP2.SetIndex(nV2);
630       aP2.SetParam(aT2);
631       //
632       aPBx.SetPave1(aP1);
633       aPBx.SetPave2(aP2);
634       //
635       aPBx.SetEdge(nE);
636       //
637       aBC.AppendNewBlock(aPBx);
638     }// for (; aIt.More(); aIt.Next()) 
639   }// for (i=1; i<=aNbOld; ++i) 
640   //
641   myIsDone=Standard_True;
642 }
643 //=======================================================================
644 // function: MakePCurves
645 // purpose: 
646 //=======================================================================
647   void NMTTools_PaveFiller::MakePCurves()
648 {
649   Standard_Integer i, aNb,  nF1, nF2, nE;
650   TopoDS_Face aF1FWD, aF2FWD;
651   BOPTools_ListIteratorOfListOfPaveBlock anIt;
652   //
653   BOPTools_CArray1OfSSInterference& aFFs=myIntrPool->SSInterferences();
654   //
655   aNb=aFFs.Extent();
656   for (i=1; i<=aNb; i++) {
657     BOPTools_SSInterference& aFF=aFFs(i);
658     aFF.Indices(nF1, nF2);
659     //
660     const TopoDS_Face& aF1=TopoDS::Face(myDS->Shape(nF1));
661     const TopoDS_Face& aF2=TopoDS::Face(myDS->Shape(nF2));
662     //
663     aF1FWD=aF1;
664     aF1FWD.Orientation(TopAbs_FORWARD);
665     aF2FWD=aF2;
666     aF2FWD.Orientation(TopAbs_FORWARD);
667     //
668     // In, On parts processing
669     const BOPTools_ListOfPaveBlock& aLPBInOn=aFF.PaveBlocks();
670     //
671     anIt.Initialize(aLPBInOn);
672     for (; anIt.More(); anIt.Next()) {
673       const BOPTools_PaveBlock& aPB=anIt.Value();
674       nE=aPB.Edge();
675       const TopoDS_Edge& aE=TopoDS::Edge(myDS->Shape(nE));
676       
677       BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF1FWD);
678       BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF2FWD);
679     }
680     //
681     //  Section Edges processing
682     /*
683     Standard_Integer aNbCurves, k, aNbV; 
684     Standard_Real aTolEdge, aTolR2D, aTolFact, aTolV, aTolVmax; 
685     BRep_Builder aBB; 
686     //
687     BOPTools_SequenceOfCurves& aSC=aFF.Curves();
688     aNbCurves=aSC.Length();
689     if (!aNbCurves) {
690       continue;
691     }
692     //
693     const BOPTools_Curve& aBC=aSC(1);
694     const BOPTools_ListOfPaveBlock& aLPB=aBC.NewPaveBlocks();
695     anIt.Initialize(aLPB);
696     for (; anIt.More(); anIt.Next()) {
697       const BOPTools_PaveBlock& aPB=anIt.Value();
698       nE=aPB.Edge();
699       const TopoDS_Edge& aE=TopoDS::Edge(myDS->Shape(nE));
700       //
701       aTolEdge=BRep_Tool::Tolerance(aE);
702       aTolR2D=aFF.TolR2D();
703       aTolFact=Max(aTolEdge, aTolR2D);
704       //
705       // Check vertices tolerances and correct them if necessary 
706       // to prevent situation when TolE > TolV
707       //
708       TopTools_IndexedMapOfShape aVMap;
709       TopExp::MapShapes(aE, TopAbs_VERTEX, aVMap);
710
711       aTolVmax=-1.;
712       aNbV=aVMap.Extent();
713       for (k=1; k<=aNbV; ++k) {
714         const TopoDS_Vertex& aV=TopoDS::Vertex(aVMap(k));
715         aTolV=BRep_Tool::Tolerance(aV);
716         if (aTolV>aTolVmax) {
717           aTolVmax=aTolV;
718         }
719       }
720       //
721       if (aTolFact>aTolVmax) {
722         aTolFact=aTolVmax;
723       }
724       // 
725       Standard_Real aTFirst, aTLast, aOutFirst, aOutLast, aOutTol;
726       Handle(Geom2d_Curve) aC2D1, aC2D1A, aC2D2, aC2D2A;
727       Handle(Geom_TrimmedCurve)aC3DETrim;
728       //
729       const Handle(Geom_Curve)& aC3DE=BRep_Tool::Curve(aE, aTFirst, aTLast);
730       aC3DETrim=new Geom_TrimmedCurve(aC3DE, aTFirst, aTLast);
731       //
732       // first P-Curve
733       BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF1FWD);
734       BOPTools_Tools2D::CurveOnSurface(aE, aF1FWD, aC2D1, aOutFirst, aOutLast, aOutTol, Standard_True);
735       //
736       if (aC3DE->IsPeriodic()) {
737         BOPTools_Tools2D::AdjustPCurveOnFace(aF1FWD, aTFirst, aTLast,  aC2D1, aC2D1A); 
738       }
739       else {
740         BOPTools_Tools2D::AdjustPCurveOnFace(aF1FWD, aC3DETrim, aC2D1, aC2D1A); 
741       }
742       //
743       aBB.UpdateEdge(aE, aC2D1A, aF1FWD, aTolFact);
744       // 
745       // second P-Curve
746       BOPTools_Tools2D::BuildPCurveForEdgeOnFace(aE, aF2FWD);
747       BOPTools_Tools2D::CurveOnSurface(aE, aF2FWD, aC2D2, aOutFirst, aOutLast, aOutTol, Standard_True);
748       //
749       if (aC3DE->IsPeriodic()) {
750         BOPTools_Tools2D::AdjustPCurveOnFace(aF2FWD, aTFirst, aTLast, aC2D2, aC2D2A); 
751       }
752       else {
753         BOPTools_Tools2D::AdjustPCurveOnFace(aF2FWD, aC3DETrim, aC2D2, aC2D2A); 
754       }
755       //
756       aBB.UpdateEdge(aE, aC2D2A, aF2FWD, aTolFact);
757     }
758     */
759   } 
760   
761 }
762 //=======================================================================
763 // function: IsExistingPaveBlock
764 // purpose: 
765 //=======================================================================
766    Standard_Boolean NMTTools_PaveFiller::IsExistingPaveBlock(const BOPTools_PaveBlock& aPBNew,
767                                                              const BOPTools_SSInterference& aFFi)
768 {
769   Standard_Boolean bFlag=Standard_False;
770   Standard_Integer nVNew1, nVNew2, nV1, nV2, iC;
771   Standard_Real aTolR3D;
772   BOPTools_ListIteratorOfListOfPaveBlock anIt;
773   //
774   aTolR3D=aFFi.TolR3D();
775   //
776   nVNew1=aPBNew.Pave1().Index();
777   nVNew2=aPBNew.Pave2().Index();
778   //
779   const BOPTools_ListOfPaveBlock& aLPBR=aFFi.PaveBlocks();
780   anIt.Initialize(aLPBR);
781   for (; anIt.More(); anIt.Next()) {
782     const BOPTools_PaveBlock& aPBR=anIt.Value();
783     nV1=aPBR.Pave1().Index();
784     nV2=aPBR.Pave2().Index();
785     if (nVNew1==nV1 || nVNew1==nV2 || nVNew2==nV1 || nVNew2==nV2) {
786       //
787       iC=CheckIntermediatePoint(aPBNew, aPBR, aTolR3D);
788       if (!iC) {
789         return !bFlag;
790       }
791     }
792   }
793   return bFlag;
794 }
795 //=======================================================================
796 // function: CheckIntermediatePoint
797 // purpose: 
798 //=======================================================================
799   Standard_Integer NMTTools_PaveFiller::CheckIntermediatePoint(const BOPTools_PaveBlock& aPB,
800                                                                const BOPTools_PaveBlock& aPBR,
801                                                                const Standard_Real aTolC)
802                                                          
803 {
804   Standard_Real aT11, aT12, aTM, aTmp;
805   Standard_Integer iVM, nE2;
806   gp_Pnt aPM;
807   BRep_Builder aBB;
808   TopoDS_Vertex aVM;
809   // 
810   // Vertex
811   const BOPTools_Pave& aPave11=aPB.Pave1();
812   aT11=aPave11.Param();
813   // 
814   const BOPTools_Pave& aPave12=aPB.Pave2();
815   aT12=aPave12.Param();
816   //
817   aTM=IntTools_Tools::IntermediatePoint (aT11, aT12);
818   //
819   const IntTools_Curve& aIC=aPB.Curve();
820   aIC.D0(aTM, aPM);
821   //
822   aBB.MakeVertex (aVM, aPM, aTolC);
823   //
824   //Edge
825   nE2=aPBR.Edge();
826   const TopoDS_Edge& aE2=TopoDS::Edge(myDS->GetShape(nE2));
827   // VE
828   iVM=myContext.ComputeVE(aVM, aE2, aTmp); 
829   //
830   return iVM;
831 }
832 //=======================================================================
833 // function: PutBoundPaveOnCurve
834 // purpose: 
835 //=======================================================================
836   void NMTTools_PaveFiller::PutBoundPaveOnCurve(BOPTools_Curve& aBC,
837                                                 BOPTools_SSInterference& aFFi)
838
839   Standard_Boolean bHasBounds, bVF;
840   Standard_Integer nF1, nF2;
841   Standard_Real aT1, aT2, aTolR3D;
842   gp_Pnt aP1, aP2;
843   //
844   const IntTools_Curve& aIC=aBC.Curve();
845   bHasBounds=aIC.HasBounds ();
846   if (!bHasBounds){
847     return;
848   }
849   //
850   // Bounds
851   aIC.Bounds (aT1, aT2, aP1, aP2);
852   //
853   // Faces
854   aFFi.Indices(nF1, nF2);
855   aTolR3D=aFFi.TolR3D();
856   //
857   const TopoDS_Face& aF1=TopoDS::Face(myDS->GetShape(nF1));
858   const TopoDS_Face& aF2=TopoDS::Face(myDS->GetShape(nF2));
859   //
860   bVF=myContext.IsValidPointForFaces (aP1, aF1, aF2, aTolR3D);
861   if (bVF) {
862     PutBoundPaveOnCurve (aP1, aT1, aBC, aFFi);
863   }
864   //
865   bVF=myContext.IsValidPointForFaces (aP2, aF1, aF2, aTolR3D);
866   if (bVF) {
867     PutBoundPaveOnCurve (aP2, aT2, aBC, aFFi);
868   }
869 }
870 //=======================================================================
871 // function: PutBoundPaveOnCurve
872 // purpose: 
873 //=======================================================================
874   void NMTTools_PaveFiller::PutBoundPaveOnCurve(const gp_Pnt& aP,
875                                                 const Standard_Real aT,
876                                                 BOPTools_Curve& aBC,
877                                                 BOPTools_SSInterference& aFFi)
878
879   Standard_Boolean bFound1, bFound2;
880   Standard_Integer nV;
881   Standard_Real aTolV=aFFi.TolR3D();
882
883   BOPTools_Pave aPave1, aPave2, aPave;
884   BOPTools_PaveSet& aCPS=aBC.Set();
885   BOPTools_PaveSet& aFFiPS=aFFi.NewPaveSet();
886   const IntTools_Curve& aIC=aBC.Curve();
887   //
888   bFound1=FindPave(aP, aTolV, aCPS  , aPave1);
889   bFound2=FindPave(aP, aTolV, aFFiPS, aPave2);
890   //
891   if (!bFound1 && !bFound2) {
892     TopoDS_Vertex aNewVertex;
893     BOPTools_Tools::MakeNewVertex(aP, aTolV, aNewVertex);
894     //
895     BooleanOperations_AncestorsSeqAndSuccessorsSeq anASSeq;
896     myDS->InsertShapeAndAncestorsSuccessors(aNewVertex, anASSeq);
897     nV=myDS->NumberOfInsertedShapes();
898     aPave.SetIndex(nV);
899     aPave.SetParam(aT);
900
901     aCPS.Append(aPave);
902     aFFiPS.Append(aPave);
903     //
904     // Append Techno Vertex to the Curve
905     TColStd_ListOfInteger& aTVs=aBC.TechnoVertices();
906     aTVs.Append(nV);
907   }
908   
909   if (bFound1 && !bFound2) {
910     nV=aPave1.Index();
911     aPave.SetIndex(nV);
912     aPave.SetParam(aT);
913     aFFiPS.Append(aPave);
914     //
915     const TopoDS_Vertex& aV=TopoDS::Vertex(myDS->Shape(nV));
916     BOPTools_Tools::UpdateVertex (aIC, aT, aV);
917   }
918   
919   if (!bFound1 && bFound2) {
920     nV=aPave2.Index();
921     aPave.SetIndex(nV);
922     aPave.SetParam(aT);
923     aCPS.Append(aPave);
924     //
925     const TopoDS_Vertex& aV=TopoDS::Vertex(myDS->Shape(nV));
926     BOPTools_Tools::UpdateVertex (aIC, aT, aV);
927   }
928 }
929 //=======================================================================
930 // function: FindPave
931 // purpose: 
932 //=======================================================================
933   Standard_Boolean NMTTools_PaveFiller::FindPave(const gp_Pnt& aP,
934                                                  const Standard_Real aTolPV, 
935                                                  const BOPTools_PaveSet& aPS,
936                                                  BOPTools_Pave& aPave)
937 {
938   Standard_Integer nV;
939   Standard_Boolean bIsVertex=Standard_False;
940  
941   const BOPTools_ListOfPave& aLP=aPS.Set();
942   BOPTools_ListIteratorOfListOfPave anIt(aLP);
943   for (; anIt.More(); anIt.Next()) {
944     const BOPTools_Pave& aPC=anIt.Value();
945     nV=aPC.Index();
946     const TopoDS_Vertex& aV=TopoDS::Vertex(myDS->Shape(nV));
947     bIsVertex=IntTools_Tools::IsVertex (aP, aTolPV, aV);
948     if (bIsVertex) {
949       aPave=aPC;
950       return bIsVertex;
951     }
952   }
953   return bIsVertex;
954 }
955 //=======================================================================
956 // function: PrepareSetForFace
957 // purpose: 
958 //=======================================================================
959   void NMTTools_PaveFiller::PrepareSetForFace(const Standard_Integer nF1,
960                                               const Standard_Integer nF2,
961                                                BOPTools_PaveSet& aPSF)
962 {
963   Standard_Integer nV1, nV2; 
964   TColStd_MapOfInteger aMap;
965   BOPTools_ListOfPaveBlock aLPB1, aLPB2;
966   BOPTools_ListIteratorOfListOfPaveBlock anIt;
967   //
968   RealSplitsFace(nF1, aLPB1);
969   RealSplitsFace(nF2, aLPB2);
970   //
971   aLPB1.Append(aLPB2);
972   //
973   anIt.Initialize(aLPB1);
974   for (; anIt.More(); anIt.Next()) {
975     const BOPTools_PaveBlock& aPB=anIt.Value();
976     const BOPTools_Pave& aPave1=aPB.Pave1();
977     nV1=aPave1.Index();
978     if (!aMap.Contains(nV1)) {
979       aMap.Add(nV1);
980       aPSF.Append(aPave1);
981     }
982     const BOPTools_Pave& aPave2=aPB.Pave2();
983     nV2=aPave2.Index();
984     if (!aMap.Contains(nV2)) {
985       aMap.Add(nV2);
986       aPSF.Append(aPave2);
987     }
988   }
989 }
990 //=======================================================================
991 // function: PutPaveOnCurve
992 // purpose: 
993 //=======================================================================
994   void NMTTools_PaveFiller::PutPaveOnCurve(const BOPTools_PaveSet& aPaveSet,
995                                            const Standard_Real aTolR3D,
996                                            BOPTools_Curve& aBC)
997
998   Standard_Integer nV;
999   Standard_Boolean bIsVertexOnLine;
1000   Standard_Real aT;
1001   BOPTools_ListIteratorOfListOfPave anIt;
1002   Bnd_Box aBBC;
1003   GeomAdaptor_Curve aGAC;
1004   //
1005   const IntTools_Curve& aC=aBC.Curve();
1006   Handle (Geom_Curve) aC3D= aC.Curve();
1007   aGAC.Load(aC3D);
1008   BndLib_Add3dCurve::Add(aGAC, aTolR3D, aBBC);
1009   //
1010   const BOPTools_ListOfPave& aLP=aPaveSet.Set();
1011   anIt.Initialize(aLP);
1012   for (; anIt.More(); anIt.Next()) {
1013     const BOPTools_Pave& aPave=anIt.Value();
1014     //
1015     nV=aPave.Index();
1016     const TopoDS_Vertex& aV=TopoDS::Vertex(myDS->Shape(nV));
1017     //
1018     Bnd_Box aBBV;
1019     BRepBndLib::Add(aV, aBBV);
1020     if (aBBC.IsOut(aBBV)){
1021       continue; 
1022     }
1023     //
1024     bIsVertexOnLine=myContext.IsVertexOnLine(aV, aC, aTolR3D, aT);
1025     //
1026     if (bIsVertexOnLine) {
1027       BOPTools_Pave aPaveNew(nV, aT, BooleanOperations_SurfaceSurface);
1028       BOPTools_PaveSet& aPS=aBC.Set();
1029       aPS.Append(aPaveNew);
1030       //<-B
1031       BOPTools_Tools::UpdateVertex (aC, aT, aV);
1032     }
1033   }
1034 }
1035 /////////////
1036 //=======================================================================
1037 // function: IsPairFound
1038 // purpose: 
1039 //=======================================================================
1040 Standard_Boolean IsPairFound(const Standard_Integer nF1,
1041                              const Standard_Integer nF2,
1042                              BOPTools_InterferencePool* myIntrPool,
1043                              BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMapWhat,
1044                              BOPTColStd_IndexedDataMapOfIntegerIndexedMapOfInteger& aMapWith)
1045 {
1046   Standard_Boolean bIsFound;
1047   //
1048   if (!aMapWhat.Contains(nF1)) {
1049     TColStd_IndexedMapOfInteger aMWhat;
1050     FMapWhat(nF1, myIntrPool, aMWhat);
1051     aMapWhat.Add(nF1, aMWhat);
1052   }
1053   //
1054   if (!aMapWith.Contains(nF2)) {
1055     TColStd_IndexedMapOfInteger aMWith;
1056     FMapWith(nF2, myIntrPool, aMWith);
1057     aMapWith.Add(nF2, aMWith);
1058   }
1059   //
1060   const TColStd_IndexedMapOfInteger& aMWht=aMapWhat.FindFromKey(nF1);
1061   const TColStd_IndexedMapOfInteger& aMWit=aMapWith.FindFromKey(nF2);
1062   //
1063   bIsFound=IsFound(aMWht, aMWit);
1064   //
1065   return bIsFound;
1066 }
1067 //=======================================================================
1068 // function: FMapWhat
1069 // purpose: 
1070 //=======================================================================
1071 void FMapWhat(const Standard_Integer nF,
1072               BOPTools_InterferencePool* myIntrPool,
1073               TColStd_IndexedMapOfInteger& aMapWhat)
1074                     
1075 {
1076   Standard_Integer nE, nV;
1077  
1078   
1079
1080   BooleanOperations_ShapesDataStructure* myDS=myIntrPool->DS();
1081   BooleanOperations_OnceExplorer aExp(*myDS);
1082   //
1083   //  What
1084   aMapWhat.Add(nF);
1085   aExp.Init(nF, TopAbs_VERTEX);
1086   for (; aExp.More(); aExp.Next()) {
1087     nV=aExp.Current();
1088     aMapWhat.Add(nV);
1089   }
1090   //
1091   aExp.Init(nF, TopAbs_EDGE);
1092   for (; aExp.More(); aExp.Next()) {
1093     nE=aExp.Current();
1094     aMapWhat.Add(nE);
1095   }
1096 }
1097 //=======================================================================
1098 // function: FMapWith
1099 // purpose: 
1100 //=======================================================================
1101 void FMapWith(const Standard_Integer nF,
1102               BOPTools_InterferencePool* myIntrPool,
1103               TColStd_IndexedMapOfInteger& aMapWith)
1104 {
1105   TColStd_IndexedMapOfInteger aMapWhat;
1106   
1107   FMapWhat(nF, myIntrPool, aMapWhat);
1108   //
1109   // With
1110   Standard_Integer i, aNb, anIndex, aWhat, aWith;
1111   BOPTools_ListIteratorOfListOfInterference anIt;
1112   
1113   const BOPTools_CArray1OfInterferenceLine& anArrIL= myIntrPool->InterferenceTable();
1114
1115   aNb=aMapWhat.Extent();
1116   for (i=1; i<=aNb; i++) {
1117     aWhat=aMapWhat(i);
1118     
1119     const BOPTools_InterferenceLine& aWithLine=anArrIL(aWhat);
1120   
1121     const BOPTools_ListOfInterference& aLI=aWithLine.List();
1122     anIt.Initialize(aLI);
1123     for (; anIt.More(); anIt.Next()) {
1124       const BOPTools_Interference& anIntf=anIt.Value();
1125       anIndex=anIntf.Index();
1126       if (anIndex) {
1127         aWith=anIntf.With();
1128         aMapWith.Add(aWith);
1129       }
1130     }
1131   }
1132 }
1133 //=======================================================================
1134 // function: IsFound
1135 // purpose: 
1136 //=======================================================================
1137 Standard_Boolean IsFound(const TColStd_IndexedMapOfInteger& aMapWhat,
1138                          const TColStd_IndexedMapOfInteger& aMapWith)
1139 {
1140   Standard_Boolean bFlag=Standard_False;
1141   Standard_Integer i, aNb, aWhat;
1142
1143   aNb=aMapWhat.Extent();
1144   for (i=1; i<=aNb; i++) {
1145     aWhat=aMapWhat(i);
1146     if (aMapWith.Contains(aWhat)) {
1147       return !bFlag;
1148     }
1149   }
1150   return bFlag;
1151 }