bc973bddd340d0cc845a026c959eec49d0e63a5e
[occt.git] / src / BOPAlgo / BOPAlgo_PaveFiller_6.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // This file is part of Open CASCADE Technology software library.
8 //
9 // This library is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU Lesser General Public License version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
14 //
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
17
18 #include <Bnd_Box.hxx>
19 #include <BOPAlgo_PaveFiller.hxx>
20 #include <BOPAlgo_SectionAttribute.hxx>
21 #include <BOPAlgo_Tools.hxx>
22 #include <BOPCol_DataMapOfIntegerReal.hxx>
23 #include <BOPCol_DataMapOfShapeInteger.hxx>
24 #include <BOPCol_ListOfInteger.hxx>
25 #include <BOPCol_ListOfShape.hxx>
26 #include <BOPCol_MapOfInteger.hxx>
27 #include <BOPCol_NCVector.hxx>
28 #include <BOPCol_Parallel.hxx>
29 #include <BOPDS_CommonBlock.hxx>
30 #include <BOPDS_CoupleOfPaveBlocks.hxx>
31 #include <BOPDS_Curve.hxx>
32 #include <BOPDS_DataMapOfPaveBlockListOfPaveBlock.hxx>
33 #include <BOPDS_DS.hxx>
34 #include <BOPDS_FaceInfo.hxx>
35 #include <BOPDS_Interf.hxx>
36 #include <BOPDS_Iterator.hxx>
37 #include <BOPDS_ListOfPave.hxx>
38 #include <BOPDS_ListOfPaveBlock.hxx>
39 #include <BOPDS_MapOfPaveBlock.hxx>
40 #include <BOPDS_PaveBlock.hxx>
41 #include <BOPDS_Point.hxx>
42 #include <BOPDS_ShapeInfo.hxx>
43 #include <BOPDS_VectorOfCurve.hxx>
44 #include <BOPDS_VectorOfPoint.hxx>
45 #include <BOPTools.hxx>
46 #include <BOPTools_AlgoTools.hxx>
47 #include <BOPTools_AlgoTools3D.hxx>
48 #include <BRep_Builder.hxx>
49 #include <BRep_Tool.hxx>
50 #include <BRep_TEdge.hxx>
51 #include <BRepAdaptor_Curve.hxx>
52 #include <BRepAdaptor_Surface.hxx>
53 #include <BRepBndLib.hxx>
54 #include <BRepBuilderAPI_MakeVertex.hxx>
55 #include <BRepTools.hxx>
56 #include <Geom2d_Curve.hxx>
57 #include <Geom_Curve.hxx>
58 #include <GeomAPI_ProjectPointOnCurve.hxx>
59 #include <GeomAPI_ProjectPointOnSurf.hxx>
60 #include <gp_Pnt.hxx>
61 #include <IntSurf_ListOfPntOn2S.hxx>
62 #include <IntSurf_PntOn2S.hxx>
63 #include <IntTools.hxx>
64 #include <IntTools_Context.hxx>
65 #include <IntTools_Curve.hxx>
66 #include <IntTools_EdgeFace.hxx>
67 #include <IntTools_FaceFace.hxx>
68 #include <IntTools_PntOn2Faces.hxx>
69 #include <IntTools_SequenceOfCurves.hxx>
70 #include <IntTools_SequenceOfPntOn2Faces.hxx>
71 #include <IntTools_ShrunkRange.hxx>
72 #include <IntTools_Tools.hxx>
73 #include <Precision.hxx>
74 #include <TopExp.hxx>
75 #include <TopExp_Explorer.hxx>
76 #include <TopoDS.hxx>
77 #include <TopoDS_Compound.hxx>
78 #include <TopoDS_Edge.hxx>
79 #include <TopoDS_Face.hxx>
80 #include <TopoDS_Vertex.hxx>
81
82 //
83 static void ToleranceFF(const BRepAdaptor_Surface& aBAS1,
84                         const BRepAdaptor_Surface& aBAS2,
85                         Standard_Real& aTolFF);
86
87 /////////////////////////////////////////////////////////////////////////
88 //=======================================================================
89 //class    : BOPAlgo_FaceFace
90 //purpose  : 
91 //=======================================================================
92 class BOPAlgo_FaceFace : 
93   public IntTools_FaceFace,
94   public BOPAlgo_Algo {
95
96  public:
97   DEFINE_STANDARD_ALLOC
98
99   BOPAlgo_FaceFace() : 
100     IntTools_FaceFace(),  
101     BOPAlgo_Algo(),
102     myIF1(-1), myIF2(-1), myTolFF(1.e-7) {
103   }
104   //
105   virtual ~BOPAlgo_FaceFace() {
106   }
107   //
108   void SetIndices(const Standard_Integer nF1,
109                   const Standard_Integer nF2) {
110     myIF1=nF1;
111     myIF2=nF2;
112   }
113   //
114   void Indices(Standard_Integer& nF1,
115                Standard_Integer& nF2) const {
116     nF1=myIF1;
117     nF2=myIF2;
118   }
119   //
120   void SetFaces(const TopoDS_Face& aF1,
121                 const TopoDS_Face& aF2) {
122     myF1=aF1;
123     myF2=aF2;
124   }
125   //
126   const TopoDS_Face& Face1()const {
127     return myF1;
128   }
129   //
130   const TopoDS_Face& Face2()const {
131     return myF2;
132   }
133   //
134   void SetTolFF(const Standard_Real aTolFF) {
135     myTolFF=aTolFF;
136   }
137   //
138   Standard_Real TolFF() const{
139     return myTolFF;
140   }
141   //
142   virtual void Perform() {
143     BOPAlgo_Algo::UserBreak();
144     IntTools_FaceFace::Perform(myF1, myF2);
145   }
146   //
147  protected:
148   Standard_Integer myIF1;
149   Standard_Integer myIF2;
150   Standard_Real myTolFF;
151   TopoDS_Face myF1;
152   TopoDS_Face myF2;
153 };
154 //
155 //=======================================================================
156 typedef BOPCol_NCVector
157   <BOPAlgo_FaceFace> BOPAlgo_VectorOfFaceFace; 
158 //
159 typedef BOPCol_Functor 
160   <BOPAlgo_FaceFace,
161   BOPAlgo_VectorOfFaceFace> BOPAlgo_FaceFaceFunctor;
162 //
163 typedef BOPCol_Cnt 
164   <BOPAlgo_FaceFaceFunctor,
165   BOPAlgo_VectorOfFaceFace> BOPAlgo_FaceFaceCnt;
166 /////////////////////////////////////////////////////////////////////////
167 //=======================================================================
168 //function : PerformFF
169 //purpose  : 
170 //=======================================================================
171 void BOPAlgo_PaveFiller::PerformFF()
172 {
173   Standard_Integer iSize;
174   Standard_Boolean bValid;
175   //
176   myErrorStatus=0;
177   //
178   myIterator->Initialize(TopAbs_FACE, TopAbs_FACE);
179   iSize=myIterator->ExpectedLength();
180   if (!iSize) {
181     return; 
182   }
183   //
184   Standard_Boolean bJustAdd, bApp, bCompC2D1, bCompC2D2, bIsDone;
185   Standard_Boolean bToSplit, bTangentFaces;
186   Standard_Integer nF1, nF2, aNbCurves, aNbPoints, i, aNbLP;
187   Standard_Integer aNbFaceFace, k;
188   Standard_Real aApproxTol, aTolR3D, aTolR2D, aTolFF, aTolReal;
189   BRepAdaptor_Surface aBAS1, aBAS2;
190   BOPCol_MapOfInteger aMI;
191   BOPAlgo_VectorOfFaceFace aVFaceFace;
192   //
193   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
194   aFFs.SetIncrement(iSize);
195   //
196   bApp=mySectionAttribute.Approximation();
197   bCompC2D1=mySectionAttribute.PCurveOnS1();
198   bCompC2D2=mySectionAttribute.PCurveOnS2();
199   aApproxTol=1.e-7;
200   bToSplit = Standard_False;
201   //
202   for (; myIterator->More(); myIterator->Next()) {
203     myIterator->Value(nF1, nF2, bJustAdd);
204     if(bJustAdd) {
205       continue;
206     }
207     //
208     const TopoDS_Face& aF1=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
209     const TopoDS_Face& aF2=(*(TopoDS_Face *)(&myDS->Shape(nF2)));
210     //
211     if (aMI.Add(nF1)) {
212       myDS->UpdateFaceInfoOn(nF1);
213       myDS->UpdateFaceInfoIn(nF1);
214     }
215     if (aMI.Add(nF2)) {
216       myDS->UpdateFaceInfoOn(nF2);
217       myDS->UpdateFaceInfoIn(nF2);
218     }
219     //
220     aBAS1.Initialize(aF1, Standard_False);
221     aBAS2.Initialize(aF2, Standard_False);
222     if (aBAS1.GetType() == GeomAbs_Plane && 
223         aBAS2.GetType() == GeomAbs_Plane) {
224       Standard_Boolean bToIntersect;
225       //
226       bToIntersect = CheckPlanes(nF1, nF2);
227       if (!bToIntersect) {
228         myDS->AddInterf(nF1, nF2);
229         BOPDS_InterfFF& aFF=aFFs.Append1();
230         aFF.SetIndices(nF1, nF2);
231         aFF.Init(0, 0);
232         continue;
233       }
234     }
235     //
236     ToleranceFF(aBAS1, aBAS2, aTolFF); 
237     //
238     BOPAlgo_FaceFace& aFaceFace=aVFaceFace.Append1();
239     //
240     aFaceFace.SetIndices(nF1, nF2);
241     aFaceFace.SetFaces(aF1, aF2);
242     aFaceFace.SetTolFF(aTolFF);
243     //
244     IntSurf_ListOfPntOn2S aListOfPnts;
245     GetEFPnts(nF1, nF2, aListOfPnts);
246     aNbLP = aListOfPnts.Extent();
247     if (aNbLP) {
248       aFaceFace.SetList(aListOfPnts);
249     }
250     //
251     aFaceFace.SetParameters(bApp, bCompC2D1, bCompC2D2, aApproxTol);
252     aFaceFace.SetProgressIndicator(myProgressIndicator);
253   }//for (; myIterator->More(); myIterator->Next()) {
254   //
255   aNbFaceFace=aVFaceFace.Extent();
256   //======================================================
257   BOPAlgo_FaceFaceCnt::Perform(myRunParallel, aVFaceFace);
258   //======================================================
259   //
260   for (k=0; k < aNbFaceFace; ++k) {
261     BOPAlgo_FaceFace& aFaceFace=aVFaceFace(k);
262     //
263     aFaceFace.Indices(nF1, nF2);
264     aTolFF=aFaceFace.TolFF();
265     //
266     bIsDone=aFaceFace.IsDone();
267     if (bIsDone) {
268       aTolR3D=aFaceFace.TolReached3d();
269       aTolR2D=aFaceFace.TolReached2d();
270       aTolReal = aFaceFace.TolReal();
271       bTangentFaces=aFaceFace.TangentFaces();
272       //
273       if (aTolR3D < aTolFF){
274         aTolR3D=aTolFF;
275       }
276       if (aTolReal < aTolFF) {
277         aTolReal = aTolFF;
278       }
279       if (aTolR2D < 1.e-7){
280         aTolR2D=1.e-7;
281       }
282       //
283       aFaceFace.PrepareLines3D(bToSplit);
284       //
285       const IntTools_SequenceOfCurves& aCvsX=aFaceFace.Lines();
286       const IntTools_SequenceOfPntOn2Faces& aPntsX=aFaceFace.Points();
287       //
288       aNbCurves=aCvsX.Length();
289       aNbPoints=aPntsX.Length();
290       //
291       if (aNbCurves || aNbPoints) {
292         myDS->AddInterf(nF1, nF2);
293       } 
294       //
295       BOPDS_InterfFF& aFF=aFFs.Append1();
296       aFF.SetIndices(nF1, nF2);
297       //
298       aFF.SetTolR3D(aTolR3D);
299       aFF.SetTolR2D(aTolR2D);
300       aFF.SetTolReal(aTolReal);
301       aFF.SetTangentFaces(bTangentFaces);
302       //
303       // Curves, Points 
304       aFF.Init(aNbCurves, aNbPoints);
305       //
306       // Curves
307
308       // Fix bounding box expanding coefficient.
309       Standard_Real aBoxExpandValue = aTolR3D;
310       if (aNbCurves > 0)
311       {
312         // Modify geometric expanding coefficient by topology value,
313         // since this bounging box used in sharing (vertex or edge).
314         Standard_Real aMaxVertexTol = Max (BRep_Tool::MaxTolerance(aFaceFace.Face1(), TopAbs_VERTEX),
315                                            BRep_Tool::MaxTolerance(aFaceFace.Face2(), TopAbs_VERTEX));
316         aBoxExpandValue += aMaxVertexTol;
317       }
318
319       BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
320       for (i=1; i<=aNbCurves; ++i) {
321         Bnd_Box aBox;
322         //
323         const IntTools_Curve& aIC=aCvsX(i);
324         const Handle(Geom_Curve)& aC3D= aIC.Curve();
325         bValid=IntTools_Tools::CheckCurve(aC3D, aBoxExpandValue, aBox);
326         if (bValid) {
327           BOPDS_Curve& aNC=aVNC.Append1();
328           aNC.SetCurve(aIC);
329           aNC.SetBox(aBox);
330         }
331       }
332       //
333       // Points
334       BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
335       for (i=1; i<=aNbPoints; ++i) {
336         const IntTools_PntOn2Faces& aPi=aPntsX(i);
337         const gp_Pnt& aP=aPi.P1().Pnt();
338         //
339         BOPDS_Point& aNP=aVNP.Append1();
340         aNP.SetPnt(aP);
341       }
342     //}// if (aNbCs || aNbPs)
343     }// if (bIsDone) {
344     else {// 904/L1
345       BOPDS_InterfFF& aFF=aFFs.Append1();
346       aFF.SetIndices(nF1, nF2);
347       aNbCurves=0;
348       aNbPoints=0;
349       aFF.Init(aNbCurves, aNbPoints);
350     }
351   }// for (k=0; k < aNbFaceFace; ++k) {
352 }
353 //=======================================================================
354 //function : MakeBlocks
355 //purpose  : 
356 //=======================================================================
357 void BOPAlgo_PaveFiller::MakeBlocks()
358 {
359   Standard_Integer aNbFF;
360   //
361   myErrorStatus=0;
362   //
363   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
364   aNbFF=aFFs.Extent();
365   if (!aNbFF) {
366     return;
367   }
368   //
369   Standard_Boolean bExist, bValid2D;
370   Standard_Integer i, nF1, nF2, aNbC, aNbP, j;
371   Standard_Integer nV1, nV2;
372   Standard_Real aTolR3D, aT1, aT2, aTol;
373   Handle(NCollection_BaseAllocator) aAllocator;
374   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
375   TopoDS_Edge aES;
376   Handle(BOPDS_PaveBlock) aPBOut;
377   //
378   //-----------------------------------------------------scope f
379   aAllocator=
380     NCollection_BaseAllocator::CommonBaseAllocator();
381   //
382   BOPCol_ListOfInteger aLSE(aAllocator), aLBV(aAllocator);
383   BOPCol_MapOfInteger aMVOnIn(100, aAllocator), aMF(100, aAllocator),
384                       aMVStick(100,aAllocator), aMVEF(100, aAllocator),
385                       aMI(100, aAllocator);
386   BOPDS_IndexedMapOfPaveBlock aMPBOnIn(100, aAllocator);
387   BOPDS_MapOfPaveBlock aMPBAdd(100, aAllocator);
388   BOPDS_ListOfPaveBlock aLPB(aAllocator);
389   BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks aMSCPB(100, aAllocator); 
390   BOPCol_DataMapOfShapeInteger aMVI(100, aAllocator);
391   BOPDS_DataMapOfPaveBlockListOfPaveBlock aDMExEdges(100, aAllocator);
392   BOPCol_DataMapOfIntegerReal aMVTol(100, aAllocator);
393   BOPCol_DataMapOfIntegerInteger aDMI(100, aAllocator);
394   BOPCol_DataMapOfIntegerListOfInteger aDMBV(100, aAllocator);
395   BOPCol_DataMapIteratorOfDataMapOfIntegerReal aItMV;
396   //
397   for (i=0; i<aNbFF; ++i) {
398     //
399     UserBreak();
400     //
401     BOPDS_InterfFF& aFF=aFFs(i);
402     aFF.Indices(nF1, nF2);
403     //
404     BOPDS_VectorOfPoint& aVP=aFF.ChangePoints();
405     aNbP=aVP.Extent();
406     BOPDS_VectorOfCurve& aVC=aFF.ChangeCurves();
407     aNbC=aVC.Extent();
408     if (!aNbP && !aNbC) {
409       continue;
410     }
411     //
412     const TopoDS_Face& aF1=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
413     const TopoDS_Face& aF2=(*(TopoDS_Face *)(&myDS->Shape(nF2)));
414     //
415     aTolR3D=aFF.TolR3D();
416     //
417     // Update face info
418     if (aMF.Add(nF1)) {
419       myDS->UpdateFaceInfoOn(nF1);
420       myDS->UpdateFaceInfoIn(nF1);
421     }
422     if (aMF.Add(nF2)) {
423       myDS->UpdateFaceInfoOn(nF2);
424       myDS->UpdateFaceInfoIn(nF2);
425     }
426     //
427     BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
428     BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
429     //
430     aMVOnIn.Clear();
431     aMPBOnIn.Clear();
432     aDMBV.Clear();
433     aMVTol.Clear();
434     //
435     myDS->VerticesOnIn(nF1, nF2, aMVOnIn, aMPBOnIn);
436     myDS->SharedEdges(nF1, nF2, aLSE, aAllocator);
437     
438     // 1. Treat Points
439     for (j=0; j<aNbP; ++j) {
440       TopoDS_Vertex aV;
441       BOPDS_CoupleOfPaveBlocks aCPB;
442       //
443       BOPDS_Point& aNP=aVP.ChangeValue(j);
444       const gp_Pnt& aP=aNP.Pnt();
445       //
446       bExist=IsExistingVertex(aP, aTolR3D, aMVOnIn);
447       if (!bExist) {
448         BOPTools_AlgoTools::MakeNewVertex(aP, aTolR3D, aV);
449         //
450         aCPB.SetIndexInterf(i);
451         aCPB.SetIndex(j);
452         aMSCPB.Add(aV, aCPB);
453       }
454     }
455     //
456     // 2. Treat Curves
457     aMVStick.Clear();
458     aMVEF.Clear();
459     GetStickVertices(nF1, nF2, aMVStick, aMVEF, aMI);
460     //
461     for (j=0; j<aNbC; ++j) {
462       BOPDS_Curve& aNC=aVC.ChangeValue(j);
463       const IntTools_Curve& aIC=aNC.Curve();
464       // DEBt
465       aNC.InitPaveBlock1();
466       //
467       PutPavesOnCurve(aMVOnIn, aTolR3D, aNC, nF1, nF2, aMI, aMVEF, aMVTol);
468       //
469       PutStickPavesOnCurve(aF1, aF2, aMI, aNC, aMVStick, aMVTol);
470       //904/F7
471       if (aNbC == 1) {
472         PutEFPavesOnCurve(aNC, aMI, aMVEF, aMVTol);
473       }
474       //
475       if (aIC.HasBounds()) {
476         aLBV.Clear();
477         //
478         PutBoundPaveOnCurve(aF1, aF2, aTolR3D, aNC, aLBV);
479         //
480         if (!aLBV.IsEmpty()) {
481           aDMBV.Bind(j, aLBV);
482         }
483       }
484     }//for (j=0; j<aNbC; ++j) {
485     //
486     // Put closing pave if needed
487     for (j=0; j<aNbC; ++j) {
488       BOPDS_Curve& aNC=aVC.ChangeValue(j);
489       PutClosingPaveOnCurve (aNC);
490     }
491     //
492     // 3. Make section edges
493     for (j=0; j<aNbC; ++j) {
494       BOPDS_Curve& aNC=aVC.ChangeValue(j);
495       const IntTools_Curve& aIC=aNC.Curve();
496       //
497       BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
498       Handle(BOPDS_PaveBlock)& aPB1=aNC.ChangePaveBlock1();
499       //
500       aLPB.Clear();
501       aPB1->Update(aLPB, Standard_False);
502       //
503       aItLPB.Initialize(aLPB);
504       for (; aItLPB.More(); aItLPB.Next()) {
505         Handle(BOPDS_PaveBlock)& aPB=aItLPB.ChangeValue();
506         aPB->Indices(nV1, nV2);
507         aPB->Range  (aT1, aT2);
508         //
509         if (fabs(aT1 - aT2) < Precision::PConfusion()) {
510           continue;
511         }
512         //
513         bValid2D=myContext->IsValidBlockForFaces(aT1, aT2, aIC, 
514                                                  aF1, aF2, aTolR3D);
515         if (!bValid2D) {
516           continue;
517         }
518         //
519         bExist=IsExistingPaveBlock(aPB, aNC, aTolR3D, aLSE);
520         if (bExist) {
521           continue;
522         }
523         //
524         Standard_Real aTolNew;
525         bExist=IsExistingPaveBlock(aPB, aNC, aTolR3D, aMPBOnIn, aPBOut, aTolNew);
526         if (bExist) {
527           if (aMPBAdd.Add(aPBOut)) {
528             Standard_Boolean bInBothFaces = Standard_True;
529             if (!myDS->IsCommonBlock(aPBOut)) {
530               Standard_Integer nE;
531               Standard_Real aTolE;
532               //
533               nE = aPBOut->Edge();
534               const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(nE);
535               aTolE = BRep_Tool::Tolerance(aE);
536               if (aTolNew < aFF.TolReal())
537                 aTolNew = aFF.TolReal();  // use real tolerance of intersection
538               if (aTolNew > aTolE) {
539                 UpdateEdgeTolerance(nE, aTolNew);
540               }
541               bInBothFaces = Standard_False;
542             } 
543             else {
544               bInBothFaces = (aFI1.PaveBlocksOn().Contains(aPBOut) ||
545                               aFI1.PaveBlocksIn().Contains(aPBOut))&&
546                              (aFI2.PaveBlocksOn().Contains(aPBOut) ||
547                               aFI2.PaveBlocksIn().Contains(aPBOut));
548             }
549             if (!bInBothFaces) {
550               PreparePostTreatFF(i, j, aPBOut, aMSCPB, aMVI, aLPBC);
551             }
552           }
553           continue;
554         }
555         //
556         // Make Edge
557         const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
558         const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
559         //
560         BOPTools_AlgoTools::MakeEdge (aIC, aV1, aT1, 
561                                       aV2, aT2, aTolR3D, aES);
562         BOPTools_AlgoTools::MakePCurve(aES, aF1, aF2, aIC, 
563                                        mySectionAttribute.PCurveOnS1(),
564                                        mySectionAttribute.PCurveOnS2());
565         //
566         if (BOPTools_AlgoTools::IsMicroEdge(aES, myContext)) {
567           continue;
568         }
569         //
570         // Append the Pave Block to the Curve j
571         aLPBC.Append(aPB);
572         //
573         // Keep info for post treatment 
574         BOPDS_CoupleOfPaveBlocks aCPB;
575         aCPB.SetIndexInterf(i);
576         aCPB.SetIndex(j);
577         aCPB.SetPaveBlock1(aPB);
578         //
579         aMSCPB.Add(aES, aCPB);
580         aMVI.Bind(aV1, nV1);
581         aMVI.Bind(aV2, nV2);
582         //
583         aMVTol.UnBind(nV1);
584         aMVTol.UnBind(nV2);
585       }
586       //
587       aLPBC.RemoveFirst();
588     }//for (j=0; j<aNbC; ++j) {
589     //back to previous tolerance values for unused vertices
590     aItMV.Initialize(aMVTol);
591     for (; aItMV.More(); aItMV.Next()) {
592       nV1 = aItMV.Key();
593       aTol = aItMV.Value();
594       //
595       const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&myDS->Shape(nV1);
596       const Handle(BRep_TVertex)& TV = 
597         *((Handle(BRep_TVertex)*)&aV.TShape());
598       TV->Tolerance(aTol);
599     }
600     //
601     ProcessExistingPaveBlocks(i, aMPBOnIn, aDMBV, aMSCPB, aMVI, aMPBAdd);
602   }//for (i=0; i<aNbFF; ++i) {
603   // 
604   // post treatment
605   myErrorStatus=PostTreatFF(aMSCPB, aMVI, aDMExEdges, aDMI, aAllocator);
606   if (myErrorStatus) {
607     return;
608   }
609   // reduce tolerances of section edges where it is appropriate
610   CorrectToleranceOfSE();
611   //
612   // update face info
613   UpdateFaceInfo(aDMExEdges, aDMI);
614   //Update all pave blocks
615   UpdatePaveBlocks(aDMI);
616   //-----------------------------------------------------scope t
617   aMF.Clear();
618   aMVStick.Clear();
619   aMPBOnIn.Clear();
620   aMVOnIn.Clear();
621   aDMExEdges.Clear();
622   aMI.Clear();
623   aDMI.Clear();
624 }
625
626 //=======================================================================
627 //function : PostTreatFF
628 //purpose  : 
629 //=======================================================================
630 Standard_Integer BOPAlgo_PaveFiller::PostTreatFF
631     (BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& theMSCPB,
632      BOPCol_DataMapOfShapeInteger& aMVI,
633      BOPDS_DataMapOfPaveBlockListOfPaveBlock& aDMExEdges,
634      BOPCol_DataMapOfIntegerInteger& aDMI,
635      const Handle(NCollection_BaseAllocator)& theAllocator)
636 {
637   Standard_Integer iRet, aNbS;
638   //
639   iRet=0;
640   aNbS=theMSCPB.Extent();
641   if (!aNbS) {
642     return iRet;
643   }
644   //
645   Standard_Boolean bHasPaveBlocks, bOld;
646   Standard_Integer iErr, nSx, nVSD, iX, iP, iC, j, nV, iV = 0, iE, k;
647   Standard_Integer aNbLPBx;
648   TopAbs_ShapeEnum aType;
649   TopoDS_Shape aV, aE;
650   BOPCol_ListIteratorOfListOfShape aItLS;
651   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
652   BOPDS_PDS aPDS;
653   Handle(BOPDS_PaveBlock) aPB1;
654   BOPDS_Pave aPave[2];
655   BOPDS_ShapeInfo aSI;
656   //
657   BOPCol_ListOfShape aLS(theAllocator);
658   BOPAlgo_PaveFiller aPF(theAllocator);
659   aPF.SetIsPrimary(Standard_False);
660   aPF.SetNonDestructive(myNonDestructive);
661   //
662   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
663   //
664   // 0
665   if (aNbS==1) {
666     const TopoDS_Shape& aS=theMSCPB.FindKey(1);
667     const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromIndex(1);
668     
669     //
670     aType=aS.ShapeType();
671     if (aType==TopAbs_VERTEX) {
672       aSI.SetShapeType(aType);
673       aSI.SetShape(aS);
674       iV=myDS->Append(aSI);
675       //
676       iX=aCPB.IndexInterf();
677       iP=aCPB.Index();
678       BOPDS_InterfFF& aFF=aFFs(iX); 
679       BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
680       BOPDS_Point& aNP=aVNP(iP);
681       aNP.SetIndex(iV);
682     }
683     else if (aType==TopAbs_EDGE) {
684       aPB1=aCPB.PaveBlock1();
685       //
686       if (aPB1->HasEdge()) {
687         BOPDS_ListOfPaveBlock aLPBx;
688         aLPBx.Append(aPB1);
689         aDMExEdges.Bind(aPB1, aLPBx);
690       } else {
691         aSI.SetShapeType(aType);
692         aSI.SetShape(aS);
693         iE=myDS->Append(aSI);
694         //
695         aPB1->SetEdge(iE);
696       }
697     }
698     return iRet;
699   }
700   //
701   // 1 prepare arguments
702   for (k=1; k<=aNbS; ++k) {
703     const TopoDS_Shape& aS=theMSCPB.FindKey(k);
704     aLS.Append(aS);
705   }
706   //
707   // 2 Fuse shapes
708   aPF.SetProgressIndicator(myProgressIndicator);
709   aPF.SetRunParallel(myRunParallel);
710   aPF.SetArguments(aLS);
711   aPF.Perform();
712   iErr=aPF.ErrorStatus();
713   if (iErr) {
714     return iRet;
715   }
716   aPDS=aPF.PDS();
717   //
718   aItLS.Initialize(aLS);
719   for (; aItLS.More(); aItLS.Next()) {
720     const TopoDS_Shape& aSx=aItLS.Value();
721     nSx=aPDS->Index(aSx);
722     const BOPDS_ShapeInfo& aSIx=aPDS->ShapeInfo(nSx);
723     //
724     aType=aSIx.ShapeType();
725     //
726     if (aType==TopAbs_VERTEX) {
727       if (aPDS->HasShapeSD(nSx, nVSD)) {
728         aV=aPDS->Shape(nVSD);
729       }
730       else {
731         aV=aSx;
732       }
733       // index of new vertex in theDS -> iV
734       if (!aMVI.IsBound(aV)) {
735         aSI.SetShapeType(aType);
736         aSI.SetShape(aV);
737         iV=myDS->Append(aSI);
738         //
739         aMVI.Bind(aV, iV);
740       }
741       else {
742         iV=aMVI.Find(aV);
743       }
744       // update FF interference
745       const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromKey(aSx);
746       iX=aCPB.IndexInterf();
747       iP=aCPB.Index();
748       BOPDS_InterfFF& aFF=aFFs(iX);
749       BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
750       BOPDS_Point& aNP=aVNP(iP);
751       aNP.SetIndex(iV);
752     }//if (aType==TopAbs_VERTEX) {
753     //
754     else if (aType==TopAbs_EDGE) {
755       bHasPaveBlocks=aPDS->HasPaveBlocks(nSx);
756       const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromKey(aSx);
757       iX=aCPB.IndexInterf();
758       iC=aCPB.Index();
759       aPB1=aCPB.PaveBlock1();
760       //
761       bOld = aPB1->HasEdge();
762       if (bOld) {
763         BOPDS_ListOfPaveBlock aLPBx;
764         aDMExEdges.Bind(aPB1, aLPBx);
765       }
766       //
767       if (!bHasPaveBlocks) {
768         if (bOld) {
769           aDMExEdges.ChangeFind(aPB1).Append(aPB1);
770         }
771         else {
772           aSI.SetShapeType(aType);
773           aSI.SetShape(aSx);
774           iE = myDS->Append(aSI);
775           //
776           aPB1->SetEdge(iE);
777         }
778       }
779       else {
780         BOPDS_InterfFF& aFF=aFFs(iX);
781         BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
782         BOPDS_Curve& aNC=aVNC(iC);
783         BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
784         //
785         // check if edge occured to be micro edge;
786         // note we check not the edge aSx itself, but its image in aPDS
787         const BOPDS_ListOfPaveBlock& aLPBx = aPDS->PaveBlocks(nSx);
788         aNbLPBx = aLPBx.Extent();
789         if (aPDS->HasPaveBlocks(nSx) &&
790             (aNbLPBx == 0 || (aNbLPBx == 1 && !aLPBx.First()->HasShrunkData()))) {
791           BOPDS_ListIteratorOfListOfPaveBlock it(aLPBC);
792           for (; it.More(); it.Next()) {
793             if (it.Value() == aPB1) {
794               aLPBC.Remove(it);
795               break;
796             }
797           }
798           continue;
799         }
800         //
801         if (bOld && !aNbLPBx) {
802           aDMExEdges.ChangeFind(aPB1).Append(aPB1);
803           continue;
804         }
805         //
806         if (!bOld) {
807           aItLPB.Initialize(aLPBC);
808           for (; aItLPB.More(); aItLPB.Next()) {
809             const Handle(BOPDS_PaveBlock)& aPBC=aItLPB.Value();
810             if (aPBC==aPB1) {
811               aLPBC.Remove(aItLPB);
812               break;
813             }
814           } 
815         }
816         //
817         if (!aNbLPBx) {
818           aE=aSx;
819           //
820           if (!aMVI.IsBound(aE)) {
821             aSI.SetShapeType(aType);
822             aSI.SetShape(aE);
823             iE=myDS->Append(aSI);
824             aMVI.Bind(aE, iE);
825           }
826           else {
827             iE=aMVI.Find(aE);
828           }
829           // append new PaveBlock to aLPBC
830           aPB1->SetEdge(iE);
831           aLPBC.Append(aPB1);
832         } // if (!aNbLPBx) {
833         //
834         else {
835           aItLPB.Initialize(aLPBx);
836           for (; aItLPB.More(); aItLPB.Next()) {
837             const Handle(BOPDS_PaveBlock)& aPBx=aItLPB.Value();
838             const Handle(BOPDS_PaveBlock) aPBRx=aPDS->RealPaveBlock(aPBx);
839             //
840             // update vertices of paves
841             aPave[0]=aPBx->Pave1();
842             aPave[1]=aPBx->Pave2();
843             for (j=0; j<2; ++j) {
844               nV = aPave[j].Index();
845               aV = aPDS->Shape(nV);
846               //
847               if (!aMVI.IsBound(aV)) {
848                 // index of new vertex in theDS -> iV
849                 aSI.SetShapeType(TopAbs_VERTEX);
850                 aSI.SetShape(aV);
851                 iV = myDS->Append(aSI);
852                 aMVI.Bind(aV, iV);
853               }
854               else {
855                 iV = aMVI.Find(aV);
856               }
857               const BOPDS_Pave& aP1 = !j ? aPB1->Pave1() : aPB1->Pave2();
858               if (aP1.Parameter() == aPave[j].Parameter() && 
859                   aP1.Index() != iV) {
860                 aDMI.Bind(aP1.Index(), iV);
861                 myDS->AddShapeSD(aP1.Index(), iV);
862               }
863               //
864               aPave[j].SetIndex(iV);
865             }
866             //
867             // add edge
868             aE=aPDS->Shape(aPBRx->Edge());
869             //
870             if (!aMVI.IsBound(aE)) {
871               aSI.SetShapeType(aType);
872               aSI.SetShape(aE);
873               iE=myDS->Append(aSI);
874               aMVI.Bind(aE, iE);
875               // update real edge tolerance according to distances in common block if any
876               if (aPDS->IsCommonBlock(aPBx)) {
877                 const Handle(BOPDS_CommonBlock)& aCB = aPDS->CommonBlock(aPBx);
878                 Standard_Real aTol = BOPAlgo_Tools::ComputeToleranceOfCB(aCB, aPDS, aPF.Context());
879                 if (aFF.TolReal() < aTol) {
880                   aFF.SetTolReal(aTol);
881                 }
882               }
883             }
884             else {
885               iE=aMVI.Find(aE);
886             }
887             // append new PaveBlock to aLPBC
888             Handle(BOPDS_PaveBlock) aPBC=new BOPDS_PaveBlock();
889             //
890             aPBC->SetPave1(aPave[0]);
891             aPBC->SetPave2(aPave[1]);
892             aPBC->SetEdge(iE);
893             if (bOld) {
894               aPBC->SetOriginalEdge(aPB1->OriginalEdge());
895               aDMExEdges.ChangeFind(aPB1).Append(aPBC);
896             }
897             else {
898               aLPBC.Append(aPBC);
899             }
900           }
901         }
902       }
903     }//else if (aType==TopAbs_EDGE)
904   }//for (; aItLS.More(); aItLS.Next()) {
905   return iRet;
906 }
907
908 //=======================================================================
909 //function : UpdateFaceInfo
910 //purpose  : 
911 //=======================================================================
912 void BOPAlgo_PaveFiller::UpdateFaceInfo
913   (BOPDS_DataMapOfPaveBlockListOfPaveBlock& theDME,
914    const BOPCol_DataMapOfIntegerInteger& theDMV)
915 {
916   Standard_Integer i, j, nV1, nF1, nF2, 
917                    aNbFF, aNbC, aNbP;
918   BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
919   BOPCol_MapOfInteger aMF;
920   //
921   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
922   aNbFF=aFFs.Extent();
923   //
924   //1. Sections (curves, points);
925   for (i=0; i<aNbFF; ++i) {
926     BOPDS_InterfFF& aFF=aFFs(i);
927     aFF.Indices(nF1, nF2);
928     //
929     BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
930     BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
931     //
932     // 1.1. Section edges
933     BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
934     aNbC=aVNC.Extent();
935     for (j=0; j<aNbC; ++j) {
936       BOPDS_Curve& aNC=aVNC(j);
937       BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
938       //
939       // Add section edges to face info
940       aItLPB.Initialize(aLPBC);
941       for (; aItLPB.More(); ) {
942         const Handle(BOPDS_PaveBlock)& aPB=aItLPB.Value();
943         //
944         // Treat existing pave blocks
945         if (theDME.IsBound(aPB)) {
946           BOPDS_ListOfPaveBlock& aLPB=theDME.ChangeFind(aPB);
947           UpdateExistingPaveBlocks(aPB, aLPB, nF1, nF2);
948           aLPBC.Remove(aItLPB);
949           continue;
950         }
951         //
952         aFI1.ChangePaveBlocksSc().Add(aPB);
953         aFI2.ChangePaveBlocksSc().Add(aPB);
954         aItLPB.Next();
955       }
956     }
957     //
958     // 1.2. Section vertices
959     const BOPDS_VectorOfPoint& aVNP=aFF.Points();
960     aNbP=aVNP.Extent();
961     for (j=0; j<aNbP; ++j) {
962       const BOPDS_Point& aNP=aVNP(j);
963       nV1=aNP.Index();
964       if (nV1<0) {
965         continue;
966       }
967       aFI1.ChangeVerticesSc().Add(nV1);
968       aFI2.ChangeVerticesSc().Add(nV1);
969     }
970     //
971     aMF.Add(nF1);
972     aMF.Add(nF2);
973   }
974   //
975   Standard_Boolean bVerts, bEdges;
976   //
977   bVerts = theDMV.Extent() > 0;
978   bEdges = theDME.Extent() > 0;
979   //
980   if (!bVerts && !bEdges) {
981     return;
982   }
983   //
984   // 2. Update Face Info information with new vertices and new
985   //    pave blocks created in PostTreatFF from existing ones
986   Standard_Integer nV2, aNbPB;
987   BOPCol_MapIteratorOfMapOfInteger aItMF;
988   BOPCol_DataMapIteratorOfDataMapOfIntegerInteger aItMV;
989   //
990   aItMF.Initialize(aMF);
991   for (; aItMF.More(); aItMF.Next()) {
992     nF1 = aItMF.Value();
993     //
994     BOPDS_FaceInfo& aFI = myDS->ChangeFaceInfo(nF1);
995     //
996     // 2.1. Update information about vertices
997     if (bVerts) {
998       BOPCol_MapOfInteger& aMVOn = aFI.ChangeVerticesOn();
999       BOPCol_MapOfInteger& aMVIn = aFI.ChangeVerticesIn();
1000       //
1001       aItMV.Initialize(theDMV);
1002       for (; aItMV.More(); aItMV.Next()) {
1003         nV1 = aItMV.Key();
1004         nV2 = aItMV.Value();
1005         //
1006         if (aMVOn.Remove(nV1)) {
1007           aMVOn.Add(nV2);
1008         }
1009         //
1010         if (aMVIn.Remove(nV1)) {
1011           aMVIn.Add(nV2);
1012         }
1013       } // for (; aItMV.More(); aItMV.Next()) {
1014     } // if (bVerts) {
1015     //
1016     // 2.2. Update information about pave blocks
1017     if (bEdges) {
1018       BOPDS_IndexedMapOfPaveBlock& aMPBOn = aFI.ChangePaveBlocksOn();
1019       BOPDS_IndexedMapOfPaveBlock& aMPBIn = aFI.ChangePaveBlocksIn();
1020       //
1021       BOPDS_IndexedMapOfPaveBlock aMPBCopy;
1022       for (i = 0; i < 2; ++i) {
1023         BOPDS_IndexedMapOfPaveBlock& aMPBOnIn = !i ? aMPBOn : aMPBIn;
1024         aMPBCopy = aMPBOnIn;
1025         aMPBOnIn.Clear();
1026         //
1027         aNbPB = aMPBCopy.Extent();
1028         for (j = 1; j <= aNbPB; ++j) {
1029           const Handle(BOPDS_PaveBlock)& aPB = aMPBCopy(j);
1030           if (theDME.IsBound(aPB)) {
1031             const BOPDS_ListOfPaveBlock& aLPB = theDME.Find(aPB);
1032             if (aLPB.IsEmpty()) {
1033               aMPBOnIn.Add(aPB);
1034               continue;
1035             }
1036             //
1037             aItLPB.Initialize(aLPB);
1038             for (; aItLPB.More(); aItLPB.Next()) {
1039               const Handle(BOPDS_PaveBlock)& aPB1 = aItLPB.Value();
1040               aMPBOnIn.Add(aPB1);
1041             }
1042           }
1043           else {
1044             aMPBOnIn.Add(aPB);
1045           }
1046         } // for (j = 1; j <= aNbPB; ++j) {
1047       } // for (i = 0; i < 2; ++i) {
1048     } // if (bEdges) {
1049   }
1050 }
1051 //=======================================================================
1052 //function : IsExistingVertex
1053 //purpose  : 
1054 //=======================================================================
1055 Standard_Boolean BOPAlgo_PaveFiller::IsExistingVertex
1056   (const gp_Pnt& aP,
1057    const Standard_Real theTolR3D,
1058    const BOPCol_MapOfInteger& aMVOnIn)const
1059 {
1060   Standard_Boolean bRet;
1061   Standard_Integer nV, iFlag;
1062   gp_Pnt aPV;
1063   Bnd_Box aBoxP;
1064   BOPCol_MapIteratorOfMapOfInteger aIt;
1065   //
1066   bRet=Standard_True;
1067   //
1068   aBoxP.Add(aP);
1069   aBoxP.Enlarge(theTolR3D);
1070   //
1071   aIt.Initialize(aMVOnIn);
1072   for (; aIt.More(); aIt.Next()) {
1073     nV=aIt.Value();
1074     const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
1075     const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&aSIV.Shape()));
1076     const Bnd_Box& aBoxV=aSIV.Box();
1077     //
1078     if (!aBoxP.IsOut(aBoxV)) {
1079       iFlag=BOPTools_AlgoTools::ComputeVV(aV, aP, theTolR3D);
1080       if (!iFlag) {
1081         return bRet;
1082       }
1083     }
1084   }
1085   return !bRet;
1086 }
1087 //=======================================================================
1088 //function : IsExistingPaveBlock
1089 //purpose  : 
1090 //=======================================================================
1091 Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
1092   (const Handle(BOPDS_PaveBlock)& thePB,
1093    const BOPDS_Curve& theNC,
1094    const Standard_Real theTolR3D,
1095    const BOPCol_ListOfInteger& theLSE)
1096 {
1097   Standard_Boolean bRet=Standard_True;
1098   //
1099   if (theLSE.IsEmpty()) {
1100     return !bRet;
1101   } 
1102   //
1103   Standard_Real aT1, aT2, aTm, aTx, aTol, aDist;
1104   Standard_Integer nE, iFlag;
1105   gp_Pnt aPm;
1106   Bnd_Box aBoxPm;
1107   BOPCol_ListIteratorOfListOfInteger aItLI;
1108   //
1109   thePB->Range(aT1, aT2);
1110   aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
1111   theNC.Curve().D0(aTm, aPm);
1112   aBoxPm.Add(aPm);
1113   aBoxPm.Enlarge(theTolR3D);
1114   //
1115   aItLI.Initialize(theLSE);
1116   for (; aItLI.More(); aItLI.Next()) {
1117     nE=aItLI.Value();
1118     if (nE < 0)
1119       continue;
1120     const BOPDS_ShapeInfo& aSIE=myDS->ChangeShapeInfo(nE);
1121     const Bnd_Box& aBoxE=aSIE.Box();
1122     if (!aBoxE.IsOut(aBoxPm)) {
1123       const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&aSIE.Shape()));
1124       aTol = BRep_Tool::Tolerance(aE);
1125       aTol = aTol > theTolR3D ? aTol : theTolR3D;
1126       iFlag=myContext->ComputePE(aPm, aTol, aE, aTx, aDist);
1127       if (!iFlag) {
1128         return bRet;
1129       }
1130     }
1131   }
1132   return !bRet;
1133 }
1134
1135 //=======================================================================
1136 //function : IsExistingPaveBlock
1137 //purpose  : 
1138 //=======================================================================
1139   Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
1140     (const Handle(BOPDS_PaveBlock)& thePB,
1141      const BOPDS_Curve& theNC,
1142      const Standard_Real theTolR3D,
1143      const BOPDS_IndexedMapOfPaveBlock& theMPBOnIn,
1144      Handle(BOPDS_PaveBlock)& aPBOut,
1145      Standard_Real& theTolNew)
1146 {
1147   Standard_Boolean bRet;
1148   Standard_Real aT1, aT2, aTm, aTx;
1149   Standard_Integer nSp, iFlag1, iFlag2, nV11, nV12, nV21, nV22, i, aNbPB;
1150   gp_Pnt aP1, aPm, aP2;
1151   Bnd_Box aBoxP1, aBoxPm, aBoxP2;
1152   //
1153   bRet=Standard_False;
1154   const IntTools_Curve& aIC=theNC.Curve();
1155   //
1156   thePB->Range(aT1, aT2);
1157   thePB->Indices(nV11, nV12);
1158   //first point
1159   aIC.D0(aT1, aP1);
1160   aBoxP1.Add(aP1);
1161   aBoxP1.Enlarge(theTolR3D);
1162   //intermediate point
1163   aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
1164   aIC.D0(aTm, aPm);
1165   aBoxPm.Add(aPm);
1166   aBoxPm.Enlarge(theTolR3D);
1167   //last point
1168   aIC.D0(aT2, aP2);
1169   aBoxP2.Add(aP2);
1170   aBoxP2.Enlarge(theTolR3D);
1171   //
1172   theTolNew = 0.;
1173   aNbPB = theMPBOnIn.Extent();
1174   for (i = 1; i <= aNbPB; ++i) {
1175     const Handle(BOPDS_PaveBlock)& aPB = theMPBOnIn(i);
1176     aPB->Indices(nV21, nV22);
1177     nSp=aPB->Edge();
1178     if (nSp < 0)
1179       continue;
1180     const BOPDS_ShapeInfo& aSISp=myDS->ChangeShapeInfo(nSp);
1181     const TopoDS_Edge& aSp=(*(TopoDS_Edge *)(&aSISp.Shape()));
1182     const Bnd_Box& aBoxSp=aSISp.Box();
1183     //
1184     iFlag1 = (nV11 == nV21 || nV11 == nV22) ? 2 : 
1185       (!aBoxSp.IsOut(aBoxP1) ? 1 : 0);
1186     iFlag2 = (nV12 == nV21 || nV12 == nV22) ? 2 : 
1187       (!aBoxSp.IsOut(aBoxP2) ? 1 : 0);
1188     if (iFlag1 && iFlag2) {
1189       Standard_Real aDist;
1190       if (aBoxSp.IsOut(aBoxPm) || myContext->ComputePE(aPm, 
1191                                                        theTolR3D, 
1192                                                        aSp, 
1193                                                        aTx, theTolNew)) {
1194         continue;
1195       }
1196       //
1197       if (iFlag1 == 1) {
1198         iFlag1 = !myContext->ComputePE(aP1, theTolR3D, aSp, aTx, aDist);
1199         if (theTolNew < aDist)
1200           theTolNew = aDist;
1201       }
1202       //
1203       if (iFlag2 == 1) {
1204         iFlag2 = !myContext->ComputePE(aP2, theTolR3D, aSp, aTx, aDist);
1205         if (theTolNew < aDist)
1206           theTolNew = aDist;
1207       }
1208       //
1209       if (iFlag1 && iFlag2) {
1210         aPBOut = aPB;
1211         bRet=Standard_True;
1212         break;
1213       }
1214     }
1215   }
1216   return bRet;
1217 }
1218 //=======================================================================
1219 //function : PutBoundPaveOnCurve
1220 //purpose  : 
1221 //=======================================================================
1222   void BOPAlgo_PaveFiller::PutBoundPaveOnCurve(const TopoDS_Face& aF1,
1223                                                const TopoDS_Face& aF2,
1224                                                const Standard_Real aTolR3D,
1225                                                BOPDS_Curve& aNC,
1226                                                BOPCol_ListOfInteger& aLVB)
1227 {
1228   Standard_Boolean bVF;
1229   Standard_Integer nV, iFlag, nVn, j, aNbEP;
1230   Standard_Real aT[2], aTmin, aTmax, aTV, aTol, aTolVnew;
1231   gp_Pnt aP[2];
1232   TopoDS_Vertex aVn;
1233   BOPDS_ListIteratorOfListOfPave aItLP;
1234   BOPDS_Pave aPn, aPMM[2];
1235   //
1236   aTolVnew = Precision::Confusion();
1237   //
1238   const IntTools_Curve& aIC=aNC.Curve();
1239   aIC.Bounds(aT[0], aT[1], aP[0], aP[1]);
1240   //
1241   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1242   const BOPDS_ListOfPave& aLP=aPB->ExtPaves();
1243   //
1244   aNbEP=aLP.Extent();
1245   if (aNbEP) {
1246     aTmin=1.e10;
1247     aTmax=-aTmin;
1248     //
1249     aItLP.Initialize(aLP);
1250     for (; aItLP.More(); aItLP.Next()) {
1251       const BOPDS_Pave& aPv=aItLP.Value();
1252       aPv.Contents(nV, aTV);
1253       if (aTV<aTmin) {
1254         aPMM[0]=aPv;
1255         aTmin=aTV;
1256       }
1257       if (aTV>aTmax) {
1258         aPMM[1]=aPv;
1259         aTmax=aTV;
1260       }
1261     }
1262   }
1263   //
1264   for (j=0; j<2; ++j) {
1265     //if curve is closed, process only one bound
1266     if (j && aP[1].IsEqual(aP[0], aTolVnew)) {
1267       continue;
1268     }
1269     //
1270     iFlag=1;
1271     //
1272     if (aNbEP) {
1273       Bnd_Box aBoxP;
1274       //
1275       aBoxP.Set(aP[j]);
1276       aTol = aTolR3D+Precision::Confusion();
1277       aBoxP.Enlarge(aTol);
1278       const BOPDS_Pave& aPV=aPMM[j];
1279       nV=aPV.Index();
1280       const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
1281       const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&aSIV.Shape()));
1282       const Bnd_Box& aBoxV=aSIV.Box();
1283       if (!aBoxP.IsOut(aBoxV)){
1284         iFlag=BOPTools_AlgoTools::ComputeVV(aV, aP[j], aTol);
1285       }
1286     }
1287     if (iFlag) {
1288       // 900/L5
1289       bVF=myContext->IsValidPointForFaces (aP[j], aF1, aF2, aTolR3D);
1290       if (!bVF) {
1291         continue;
1292       }
1293       //
1294       BOPDS_ShapeInfo aSIVn;
1295       //
1296       BOPTools_AlgoTools::MakeNewVertex(aP[j], aTolR3D, aVn);
1297       aSIVn.SetShapeType(TopAbs_VERTEX);
1298       aSIVn.SetShape(aVn);
1299       //
1300       nVn=myDS->Append(aSIVn);
1301       //
1302       aPn.SetIndex(nVn);
1303       aPn.SetParameter(aT[j]);
1304       aPB->AppendExtPave(aPn);
1305       //
1306       aVn=(*(TopoDS_Vertex *)(&myDS->Shape(nVn)));
1307       BOPTools_AlgoTools::UpdateVertex (aIC, aT[j], aVn);
1308       //
1309       aTolVnew = BRep_Tool::Tolerance(aVn);
1310       //
1311       BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nVn);
1312       Bnd_Box& aBoxDS=aSIDS.ChangeBox();
1313       BRepBndLib::Add(aVn, aBoxDS);
1314       aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
1315       //
1316       aLVB.Append(nVn);
1317     }
1318   }
1319 }
1320
1321 //=======================================================================
1322 //function : PutPavesOnCurve
1323 //purpose  : 
1324 //=======================================================================
1325   void BOPAlgo_PaveFiller::PutPavesOnCurve
1326   (const BOPCol_MapOfInteger& aMVOnIn,
1327    const Standard_Real aTolR3D,
1328    BOPDS_Curve& aNC,
1329    const Standard_Integer nF1,
1330    const Standard_Integer nF2,
1331    const BOPCol_MapOfInteger& aMI,
1332    const BOPCol_MapOfInteger& aMVEF,
1333    BOPCol_DataMapOfIntegerReal& aMVTol)
1334 {
1335   Standard_Boolean bInBothFaces;
1336   Standard_Integer nV;
1337   BOPCol_MapIteratorOfMapOfInteger aIt;
1338   //
1339   const Bnd_Box& aBoxC=aNC.Box();
1340   //
1341   //Put EF vertices first
1342   aIt.Initialize(aMVEF);
1343   for (; aIt.More(); aIt.Next()) {
1344     nV=aIt.Value();
1345     PutPaveOnCurve(nV, aTolR3D, aNC, aMI, aMVTol, 2);
1346   }
1347   //Put all other vertices
1348   aIt.Initialize(aMVOnIn);
1349   for (; aIt.More(); aIt.Next()) {
1350     nV=aIt.Value();
1351     if (aMVEF.Contains(nV)) {
1352       continue;
1353     }
1354     //
1355     const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
1356     const Bnd_Box& aBoxV=aSIV.Box();
1357     //
1358     if (aBoxC.IsOut(aBoxV)){
1359       continue;
1360     }
1361     if (!myDS->IsNewShape(nV)) {
1362       const BOPDS_FaceInfo& aFI1 = myDS->FaceInfo(nF1);
1363       const BOPDS_FaceInfo& aFI2 = myDS->FaceInfo(nF2);
1364       //
1365       bInBothFaces = (aFI1.VerticesOn().Contains(nV) ||
1366                       aFI1.VerticesIn().Contains(nV))&&
1367                      (aFI2.VerticesOn().Contains(nV) ||
1368                       aFI2.VerticesIn().Contains(nV));
1369       if (!bInBothFaces) {
1370         continue;
1371       }
1372     }
1373     //
1374     PutPaveOnCurve(nV, aTolR3D, aNC, aMI, aMVTol, 1);
1375   }
1376 }
1377
1378 //=======================================================================
1379 //function : ExtendedTolerance
1380 //purpose  : 
1381 //=======================================================================
1382 Standard_Boolean BOPAlgo_PaveFiller::ExtendedTolerance
1383   (const Standard_Integer nV,
1384    const BOPCol_MapOfInteger& aMI,
1385    Standard_Real& aTolVExt,
1386    const Standard_Integer aType)
1387 {
1388   Standard_Boolean bFound = Standard_False;
1389   if (!(myDS->IsNewShape(nV))) {
1390     return bFound;
1391   }
1392   //
1393   Standard_Integer i, k, aNbLines, aNbInt;
1394   Standard_Real aT11, aT12, aD1, aD2, aD;
1395   TopoDS_Vertex aV;
1396   gp_Pnt aPV, aP11, aP12;
1397   //
1398   k = 0;
1399   aNbInt = 2;
1400   if (aType == 1) {
1401     aNbInt = 1;
1402   } else if (aType == 2) {
1403     k = 1;
1404   }
1405   //
1406   aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
1407   aPV=BRep_Tool::Pnt(aV);
1408   //
1409   BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
1410   BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1411   //
1412   for (; k<aNbInt; ++k) {
1413     aNbLines = !k ? aEEs.Extent() : aEFs.Extent();
1414     for (i = 0; i < aNbLines; ++i) {
1415       BOPDS_Interf *aInt = !k ? (BOPDS_Interf*) (&aEEs(i)) :
1416         (BOPDS_Interf*) (&aEFs(i));
1417       if (aInt->IndexNew() == nV) {
1418         if (aMI.Contains(aInt->Index1()) && 
1419             aMI.Contains(aInt->Index2())) {
1420           const IntTools_CommonPrt& aComPrt = !k ? aEEs(i).CommonPart() :
1421             aEFs(i).CommonPart();
1422           //
1423           const TopoDS_Edge& aE1=aComPrt.Edge1();
1424           aComPrt.Range1(aT11, aT12);
1425           BOPTools_AlgoTools::PointOnEdge(aE1, aT11, aP11);
1426           BOPTools_AlgoTools::PointOnEdge(aE1, aT12, aP12);
1427           aD1=aPV.Distance(aP11);
1428           aD2=aPV.Distance(aP12);
1429           aD=(aD1>aD2)? aD1 : aD2;
1430           if (aD>aTolVExt) {
1431             aTolVExt=aD;
1432           }
1433           return !bFound;
1434         }//if (aMI.Contains(aEF.Index1()) && aMI.Contains(aEF.Index2())) {
1435       }//if (aInt->IndexNew() == nV) {
1436     }//for (i = 0; i < aNbLines; ++i) {
1437   }//for (k=0; k<2; ++k) {
1438   return bFound;
1439 }
1440
1441 //=======================================================================
1442 //function : GetEFPnts
1443 //purpose  : 
1444 //=======================================================================
1445 void BOPAlgo_PaveFiller::GetEFPnts
1446   (const Standard_Integer nF1,
1447    const Standard_Integer nF2,
1448    IntSurf_ListOfPntOn2S& aListOfPnts)
1449 {
1450   Standard_Integer nE, nF, nFOpposite, aNbEFs, i;
1451   Standard_Real U1, U2, V1, V2, f, l;
1452   BOPCol_MapOfInteger aMI;
1453   //
1454   //collect indexes of all shapes from nF1 and nF2.
1455   GetFullShapeMap(nF1, aMI);
1456   GetFullShapeMap(nF2, aMI);
1457   //
1458   BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1459   aNbEFs = aEFs.Extent();
1460   //
1461   for(i = 0; i < aNbEFs; ++i) {
1462     const BOPDS_InterfEF& aEF = aEFs(i);
1463     if (aEF.HasIndexNew()) {
1464       aEF.Indices(nE, nFOpposite);
1465       if(aMI.Contains(nE) && aMI.Contains(nFOpposite)) {
1466         const IntTools_CommonPrt& aCP = aEF.CommonPart();
1467         Standard_Real aPar = aCP.VertexParameter1();
1468         const TopoDS_Edge& aE = (*(TopoDS_Edge*)(&myDS->Shape(nE)));
1469         const TopoDS_Face& aFOpposite = 
1470           (*(TopoDS_Face*)(&myDS->Shape(nFOpposite)));
1471         //
1472         const Handle(Geom_Curve)& aCurve = BRep_Tool::Curve(aE, f, l);
1473         //
1474         nF = (nFOpposite == nF1) ? nF2 : nF1;
1475         const TopoDS_Face& aF = (*(TopoDS_Face*)(&myDS->Shape(nF)));
1476         Handle(Geom2d_Curve) aPCurve = 
1477           BRep_Tool::CurveOnSurface(aE, aF, f, l);
1478         //
1479         GeomAPI_ProjectPointOnSurf& aProj=myContext->ProjPS(aFOpposite);
1480         //
1481         gp_Pnt aPoint;
1482         aCurve->D0(aPar, aPoint);
1483         IntSurf_PntOn2S aPnt;
1484         if(!aPCurve.IsNull()) {
1485           gp_Pnt2d aP2d = aPCurve->Value(aPar);
1486           aProj.Perform(aPoint);
1487           if(aProj.IsDone()) {
1488             aProj.LowerDistanceParameters(U1,V1);
1489             if (nF == nF1) {
1490               aPnt.SetValue(aP2d.X(),aP2d.Y(),U1,V1);
1491             } else {
1492               aPnt.SetValue(U1,V1,aP2d.X(),aP2d.Y());
1493             }
1494             aListOfPnts.Append(aPnt);
1495           }
1496         }
1497         else {
1498           GeomAPI_ProjectPointOnSurf& aProj1 = myContext->ProjPS(aF);
1499           aProj1.Perform(aPoint);
1500           aProj.Perform(aPoint);
1501           if(aProj1.IsDone() && aProj.IsDone()){
1502             aProj1.LowerDistanceParameters(U1,V1);
1503             aProj.LowerDistanceParameters(U2,V2);
1504             if (nF == nF1) {
1505               aPnt.SetValue(U1,V1,U2,V2);
1506             } else {
1507               aPnt.SetValue(U2,V2,U1,V1);
1508             }
1509             aListOfPnts.Append(aPnt);
1510           }
1511         }
1512       }
1513     }
1514   }
1515 }
1516
1517 //=======================================================================
1518 //function : ProcessUnUsedVertices
1519 //purpose  : 
1520 //=======================================================================
1521   void BOPAlgo_PaveFiller::PutEFPavesOnCurve
1522   (BOPDS_Curve& aNC,
1523    const BOPCol_MapOfInteger& aMI,
1524    const BOPCol_MapOfInteger& aMVEF,
1525    BOPCol_DataMapOfIntegerReal& aMVTol)
1526 {
1527   if (!aMVEF.Extent()) {
1528     return;
1529   }
1530   //
1531   const IntTools_Curve& aIC=aNC.Curve();
1532   GeomAbs_CurveType aTypeC;
1533   aTypeC=aIC.Type();
1534   if (!(aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve)) {
1535     return;
1536   }
1537   //
1538   Standard_Integer nV;
1539   BOPCol_MapOfInteger aMV;
1540   //
1541   aMV.Assign(aMVEF);
1542   RemoveUsedVertices(aNC, aMV);
1543   if (!aMV.Extent()) {
1544     return;
1545   }
1546   //
1547   Standard_Real aDist;
1548   BOPDS_Pave aPave;
1549   //
1550   const Handle(Geom_Curve)& aC3D=aIC.Curve();
1551   GeomAPI_ProjectPointOnCurve& aProjPT = myContext->ProjPT(aC3D);
1552   //
1553   BOPCol_MapIteratorOfMapOfInteger aItMI;
1554   aItMI.Initialize(aMV);
1555   for (; aItMI.More(); aItMI.Next()) {
1556     nV = aItMI.Value();
1557     const TopoDS_Vertex& aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
1558     gp_Pnt aPV = BRep_Tool::Pnt(aV);
1559     aProjPT.Perform(aPV);
1560     Standard_Integer aNbPoints = aProjPT.NbPoints();
1561     if (aNbPoints) {
1562       aDist = aProjPT.LowerDistance();
1563       PutPaveOnCurve(nV, aDist, aNC, aMI, aMVTol);
1564     }
1565   }
1566 }
1567
1568 //=======================================================================
1569 //function : ProcessUnUsedVertices
1570 //purpose  : 
1571 //=======================================================================
1572   void BOPAlgo_PaveFiller::PutStickPavesOnCurve
1573   (const TopoDS_Face& aF1,
1574    const TopoDS_Face& aF2,
1575    const BOPCol_MapOfInteger& aMI,
1576    BOPDS_Curve& aNC,
1577    const BOPCol_MapOfInteger& aMVStick,
1578    BOPCol_DataMapOfIntegerReal& aMVTol)
1579 {
1580   BOPCol_MapOfInteger aMV;
1581   aMV.Assign(aMVStick);
1582   RemoveUsedVertices(aNC, aMV);
1583   //
1584   if (!aMV.Extent()) {
1585     return;
1586   }
1587   //
1588   Handle(Geom_Surface) aS1=BRep_Tool::Surface(aF1);
1589   Handle(Geom_Surface) aS2=BRep_Tool::Surface(aF2);
1590   //
1591   const IntTools_Curve& aIC=aNC.Curve();
1592   //if (aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve) {
1593   Handle(Geom2d_Curve) aC2D[2];
1594   //
1595   aC2D[0]=aIC.FirstCurve2d();
1596   aC2D[1]=aIC.SecondCurve2d();
1597   if (!aC2D[0].IsNull() && !aC2D[1].IsNull()) {
1598     Standard_Integer nV, m, n;
1599     Standard_Real aTC[2], aD, aD2, u, v, aDT2, aScPr, aDScPr;
1600     gp_Pnt aPC[2], aPV;
1601     gp_Dir aDN[2];
1602     gp_Pnt2d aP2D;
1603     BOPCol_MapIteratorOfMapOfInteger aItMI, aItMI1;
1604     //
1605     aDT2=2e-7;     // the rich criteria
1606     aDScPr=5.e-9;  // the creasing criteria
1607     aIC.Bounds(aTC[0], aTC[1], aPC[0], aPC[1]);
1608     //
1609     aItMI.Initialize(aMV);
1610     for (; aItMI.More(); aItMI.Next()) {
1611       nV = aItMI.Value();
1612       const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&myDS->Shape(nV));
1613       aPV=BRep_Tool::Pnt(aV);
1614       //
1615       for (m=0; m<2; ++m) {
1616         aD2=aPC[m].SquareDistance(aPV);
1617         if (aD2>aDT2) {// no rich
1618           continue; 
1619         }
1620         //
1621         for (n=0; n<2; ++n) {
1622           Handle(Geom_Surface)& aS=(!n)? aS1 : aS2;
1623           aC2D[n]->D0(aTC[m], aP2D);
1624           aP2D.Coord(u, v);
1625           BOPTools_AlgoTools3D::GetNormalToSurface(aS, u, v, aDN[n]);
1626         }
1627         // 
1628         aScPr=aDN[0]*aDN[1];
1629         if (aScPr<0.) {
1630           aScPr=-aScPr;
1631         }
1632         aScPr=1.-aScPr;
1633         //
1634         if (aScPr>aDScPr) {
1635           continue;
1636         }
1637         //
1638         // The intersection curve aIC is vanishing curve (the crease)
1639         aD=sqrt(aD2);
1640         //
1641         PutPaveOnCurve(nV, aD, aNC, aMI, aMVTol);
1642       }
1643     }//for (jVU=1; jVU=aNbVU; ++jVU) {
1644   }
1645   //}//if (aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve) {
1646   //}//if(aType1==GeomAbs_Torus  || aType2==GeomAbs_Torus) {
1647 }
1648
1649 //=======================================================================
1650 //function : GetStickVertices
1651 //purpose  : 
1652 //=======================================================================
1653 void BOPAlgo_PaveFiller::GetStickVertices(const Standard_Integer nF1,
1654                                           const Standard_Integer nF2,
1655                                           BOPCol_MapOfInteger& aMVStick,
1656                                           BOPCol_MapOfInteger& aMVEF,
1657                                           BOPCol_MapOfInteger& aMI)
1658 {
1659   Standard_Integer nS1, nS2, nVNew, aTypeInt, i;
1660   //
1661   BOPDS_VectorOfInterfVV& aVVs=myDS->InterfVV();
1662   BOPDS_VectorOfInterfVE& aVEs=myDS->InterfVE();
1663   BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
1664   BOPDS_VectorOfInterfVF& aVFs=myDS->InterfVF();
1665   BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1666   //
1667   Standard_Integer aNbLines[5] = {
1668     aVVs.Extent(), aVEs.Extent(), aEEs.Extent(),
1669     aVFs.Extent(), aEFs.Extent()
1670     };
1671   //collect indices of all shapes from nF1 and nF2.
1672   aMI.Clear();
1673   GetFullShapeMap(nF1, aMI);
1674   GetFullShapeMap(nF2, aMI);
1675   //
1676   //collect VV, VE, EE, VF interferences
1677   for (aTypeInt = 0; aTypeInt < 4; ++aTypeInt) {
1678     for (i = 0; i < aNbLines[aTypeInt]; ++i) {
1679       BOPDS_Interf* aInt = (aTypeInt==0) ? (BOPDS_Interf*)(&aVVs(i)) : 
1680         ((aTypeInt==1) ? (BOPDS_Interf*)(&aVEs(i)) :
1681          ((aTypeInt==2) ? (BOPDS_Interf*)(&aEEs(i)) : 
1682           (BOPDS_Interf*)(&aVFs(i))));
1683       if (aInt->HasIndexNew()) {
1684         aInt->Indices(nS1, nS2);
1685         if(aMI.Contains(nS1) && aMI.Contains(nS2)) {
1686           nVNew = aInt->IndexNew();
1687           aMVStick.Add(nVNew);
1688         }
1689       }
1690     }
1691   }
1692   //collect EF interferences
1693   for (i = 0; i < aNbLines[4]; ++i) {
1694     const BOPDS_InterfEF& aInt = aEFs(i);
1695     if (aInt.HasIndexNew()) {
1696       aInt.Indices(nS1, nS2);
1697       if(aMI.Contains(nS1) && aMI.Contains(nS2)) {
1698         nVNew = aInt.IndexNew();
1699         aMVStick.Add(nVNew);
1700         aMVEF.Add(nVNew);
1701       }
1702     }
1703   }
1704 }
1705
1706 //=======================================================================
1707 // function: GetFullShapeMap
1708 // purpose: 
1709 //=======================================================================
1710 void BOPAlgo_PaveFiller::GetFullShapeMap(const Standard_Integer nF,
1711                                          BOPCol_MapOfInteger& aMI)
1712 {
1713   BOPCol_ListIteratorOfListOfInteger aIt;
1714   Standard_Integer nS;
1715   //
1716   const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(nF);
1717   const BOPCol_ListOfInteger& aLI = aSI.SubShapes();
1718   //
1719   aMI.Add(nF);
1720   aIt.Initialize(aLI);
1721   for (; aIt.More(); aIt.Next()) {
1722     nS = aIt.Value();
1723     aMI.Add(nS);
1724   }
1725 }
1726
1727 //=======================================================================
1728 // function: RemoveUsedVertices
1729 // purpose: 
1730 //=======================================================================
1731 void BOPAlgo_PaveFiller::RemoveUsedVertices(BOPDS_Curve& aNC,
1732                                             BOPCol_MapOfInteger& aMV)
1733 {
1734   if (!aMV.Extent()) {
1735     return;
1736   }
1737   Standard_Integer nV;
1738   BOPDS_Pave aPave;
1739   BOPDS_ListIteratorOfListOfPave aItLP;
1740   //
1741   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1742   const BOPDS_ListOfPave& aLP = aPB->ExtPaves();
1743   aItLP.Initialize(aLP);
1744   for (;aItLP.More();aItLP.Next()) {
1745     aPave = aItLP.Value();
1746     nV = aPave.Index();
1747     aMV.Remove(nV);
1748   }
1749 }
1750
1751 //=======================================================================
1752 //function : PutPaveOnCurve
1753 //purpose  : 
1754 //=======================================================================
1755   void BOPAlgo_PaveFiller::PutPaveOnCurve
1756   (const Standard_Integer nV,
1757    const Standard_Real aTolR3D,
1758    BOPDS_Curve& aNC,
1759    const BOPCol_MapOfInteger& aMI,
1760    BOPCol_DataMapOfIntegerReal& aMVTol,
1761    const Standard_Integer iCheckExtend)
1762 {
1763   Standard_Boolean bIsVertexOnLine;
1764   Standard_Real aT, aTolV;
1765   //
1766   const TopoDS_Vertex& aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
1767   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1768   const IntTools_Curve& aIC = aNC.Curve();
1769   //
1770   bIsVertexOnLine=myContext->IsVertexOnLine(aV, aIC, aTolR3D, aT);
1771   if (!bIsVertexOnLine && iCheckExtend) {
1772     aTolV = BRep_Tool::Tolerance(aV);
1773     //
1774     ExtendedTolerance(nV, aMI, aTolV, iCheckExtend);
1775     bIsVertexOnLine=myContext->IsVertexOnLine(aV, aTolV, aIC, aTolR3D, aT);
1776   }
1777   //
1778   if (bIsVertexOnLine) {
1779     // check if aPB contains the parameter aT
1780     Standard_Boolean bExist;
1781     Standard_Integer nVToUpdate;
1782     Standard_Real aPTol, aDist, aTolVNew, aTolV2, aDTol;
1783     TopoDS_Vertex aVToUpdate;
1784     gp_Pnt aP1, aP2;
1785     //
1786     aTolV2 = 0.;
1787     aDTol = 1.e-12;
1788     //
1789     GeomAdaptor_Curve aGAC(aIC.Curve());
1790     aPTol = aGAC.Resolution(aTolR3D);
1791     //
1792     bExist = aPB->ContainsParameter(aT, aPTol, nVToUpdate);
1793     if (bExist) {
1794       // use existing pave
1795       aP1 = BRep_Tool::Pnt(aV);
1796       aTolV2 = BRep_Tool::Tolerance(aV);
1797       aVToUpdate = (*(TopoDS_Vertex *)(&myDS->Shape(nVToUpdate)));
1798     }
1799     else {
1800       // add new pave
1801       BOPDS_Pave aPave;
1802       aPave.SetIndex(nV);
1803       aPave.SetParameter(aT);
1804       aPB->AppendExtPave(aPave);
1805       //
1806       aP1 = aGAC.Value(aT);
1807       nVToUpdate = nV;
1808       aVToUpdate = aV;
1809     }
1810     //
1811     aTolV = BRep_Tool::Tolerance(aVToUpdate);
1812     aP2 = BRep_Tool::Pnt(aVToUpdate);
1813     aDist = aP1.Distance(aP2);
1814     aTolVNew = aDist - aTolV2;
1815     //
1816     if (aTolVNew > aTolV) {
1817       BRep_Builder aBB;
1818       aBB.UpdateVertex(aVToUpdate, aTolVNew+aDTol);
1819       //
1820       if (!aMVTol.IsBound(nVToUpdate)) {
1821         aMVTol.Bind(nVToUpdate, aTolV);
1822       }
1823       // 
1824       BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nVToUpdate);
1825       Bnd_Box& aBoxDS=aSIDS.ChangeBox();
1826       BRepBndLib::Add(aVToUpdate, aBoxDS);
1827       aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
1828     }
1829   }
1830 }
1831
1832 //=======================================================================
1833 //function : ProcessOldPaveBlocks
1834 //purpose  : 
1835 //=======================================================================
1836 void BOPAlgo_PaveFiller::ProcessExistingPaveBlocks
1837     (const Standard_Integer theInt,
1838      const BOPDS_IndexedMapOfPaveBlock& aMPBOnIn,
1839      const BOPCol_DataMapOfIntegerListOfInteger& aDMBV,
1840      BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
1841      BOPCol_DataMapOfShapeInteger& aMVI,
1842      BOPDS_MapOfPaveBlock& aMPB)
1843 {
1844   if (aDMBV.IsEmpty()) {
1845     return;
1846   }
1847   //
1848   Standard_Real aT, dummy;
1849   Standard_Integer i, nV, nE, iC, aNbPB, iFlag;
1850   BOPCol_ListIteratorOfListOfInteger aItLI;
1851   BOPCol_DataMapIteratorOfDataMapOfIntegerListOfInteger aItBV;
1852   //
1853   BOPDS_VectorOfInterfFF& aFFs = myDS->InterfFF();
1854   BOPDS_InterfFF& aFF = aFFs(theInt);
1855   BOPDS_VectorOfCurve& aVC = aFF.ChangeCurves();
1856   //
1857   aNbPB = aMPBOnIn.Extent();
1858   //
1859   aItBV.Initialize(aDMBV);
1860   for (; aItBV.More(); aItBV.Next()) {
1861     iC = aItBV.Key();
1862     const BOPCol_ListOfInteger& aLBV = aItBV.Value();
1863     //
1864     BOPDS_Curve& aNC = aVC.ChangeValue(iC);
1865     BOPDS_ListOfPaveBlock& aLPBC = aNC.ChangePaveBlocks();
1866     //
1867     aItLI.Initialize(aLBV);
1868     for (; aItLI.More(); aItLI.Next()) {
1869       nV = aItLI.Value();
1870       const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
1871       const Bnd_Box& aBoxV=aSIV.Box();
1872       const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&aSIV.Shape();
1873       if (!aMVI.IsBound(aV)) {
1874         continue;
1875       }
1876       //
1877       for (i = 1; i <= aNbPB; ++i) {
1878         const Handle(BOPDS_PaveBlock)& aPB = aMPBOnIn(i);
1879         if (aPB->Pave1().Index() == nV || aPB->Pave2().Index() == nV) {
1880           continue;
1881         }
1882         //
1883         if (aMPB.Contains(aPB)) {
1884           continue;
1885         }
1886         if (myDS->ShapeInfo(aPB->OriginalEdge()).HasFlag()) { // skip degenerated edges
1887           continue;
1888         }
1889         //
1890         nE = aPB->Edge();
1891         const BOPDS_ShapeInfo& aSIE = myDS->ShapeInfo(nE);
1892         const Bnd_Box& aBoxE = aSIE.Box();
1893         //
1894         if (aBoxV.IsOut(aBoxE)) {
1895           continue;
1896         }
1897         //
1898         const TopoDS_Edge& aE = *(TopoDS_Edge*)&aSIE.Shape();
1899         //
1900         iFlag = myContext->ComputeVE(aV, aE, aT, dummy);
1901         if (!iFlag) {
1902           aMPB.Add(aPB);
1903           PreparePostTreatFF(theInt, iC, aPB, aMSCPB, aMVI, aLPBC);
1904         }
1905       }
1906     }
1907   }
1908 }
1909 //=======================================================================
1910 //function : UpdateExistingPaveBlocks
1911 //purpose  : 
1912 //=======================================================================
1913 void BOPAlgo_PaveFiller::UpdateExistingPaveBlocks
1914   (const Handle(BOPDS_PaveBlock)& aPBf,
1915    BOPDS_ListOfPaveBlock& aLPB,
1916    const Standard_Integer nF1,
1917    const Standard_Integer nF2) 
1918 {
1919   Standard_Integer nE;
1920   Standard_Boolean bCB;
1921   Handle(BOPDS_PaveBlock) aPB, aPB1, aPB2, aPB2n;
1922   Handle(BOPDS_CommonBlock) aCB;
1923   BOPDS_ListIteratorOfListOfPaveBlock aIt, aIt1, aIt2;
1924   //
1925   // 1. Remove micro edges from aLPB
1926   aIt.Initialize(aLPB);
1927   for (; aIt.More();) {
1928     aPB = aIt.Value();
1929     const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPB->Edge());
1930     if (BOPTools_AlgoTools::IsMicroEdge(aE, myContext)) {
1931       aLPB.Remove(aIt);
1932       continue;
1933     }
1934     aIt.Next();
1935   }
1936   //
1937   if (!aLPB.Extent()) {
1938     return;
1939   }
1940   //
1941   BOPDS_FaceInfo& aFI1 = myDS->ChangeFaceInfo(nF1);
1942   BOPDS_FaceInfo& aFI2 = myDS->ChangeFaceInfo(nF2);
1943   //
1944   BOPDS_IndexedMapOfPaveBlock& aMPBOn1 = aFI1.ChangePaveBlocksOn();
1945   BOPDS_IndexedMapOfPaveBlock& aMPBIn1 = aFI1.ChangePaveBlocksIn();
1946   BOPDS_IndexedMapOfPaveBlock& aMPBOn2 = aFI2.ChangePaveBlocksOn();
1947   BOPDS_IndexedMapOfPaveBlock& aMPBIn2 = aFI2.ChangePaveBlocksIn();
1948   //
1949   // 2. Remove old pave blocks
1950   const Handle(BOPDS_CommonBlock)& aCB1 = myDS->CommonBlock(aPBf);
1951   bCB = !aCB1.IsNull();
1952   BOPDS_ListOfPaveBlock aLPB1;
1953   //
1954   if (bCB) {
1955     aLPB1.Assign(aCB1->PaveBlocks());
1956   } else {
1957     aLPB1.Append(aPBf);
1958   }
1959   aIt1.Initialize(aLPB1);
1960   for (; aIt1.More(); aIt1.Next()) {
1961     aPB1 = aIt1.Value();
1962     nE = aPB1->OriginalEdge();
1963     //
1964     BOPDS_ListOfPaveBlock& aLPB2 = myDS->ChangePaveBlocks(nE);
1965     aIt2.Initialize(aLPB2);
1966     for (; aIt2.More(); aIt2.Next()) {
1967       aPB2 = aIt2.Value();
1968       if (aPB1 == aPB2) {
1969         aLPB2.Remove(aIt2);
1970         break;
1971       }
1972     }
1973   }
1974   //
1975   // 3. Update pave blocks
1976   if (bCB) {
1977     //create new common blocks
1978     BOPDS_ListOfPaveBlock aLPBNew;
1979     const BOPCol_ListOfInteger& aFaces = aCB1->Faces();
1980     aIt.Initialize(aLPB);
1981     for (; aIt.More(); aIt.Next()) {
1982       const Handle(BOPDS_PaveBlock)& aPBValue = aIt.Value();
1983       //
1984       aCB = new BOPDS_CommonBlock;
1985       aIt1.Initialize(aLPB1);
1986       for (; aIt1.More(); aIt1.Next()) {
1987         aPB2 = aIt1.Value();
1988         nE = aPB2->OriginalEdge();
1989         //
1990         aPB2n = new BOPDS_PaveBlock;
1991         aPB2n->SetPave1(aPBValue->Pave1());
1992         aPB2n->SetPave2(aPBValue->Pave2());
1993         aPB2n->SetEdge(aPBValue->Edge());
1994         aPB2n->SetOriginalEdge(nE);
1995         aCB->AddPaveBlock(aPB2n);
1996         myDS->SetCommonBlock(aPB2n, aCB);
1997         myDS->ChangePaveBlocks(nE).Append(aPB2n);
1998       }
1999       aCB->SetFaces(aFaces);
2000       myDS->SortPaveBlocks(aCB);
2001       //
2002       const Handle(BOPDS_PaveBlock)& aPBNew = aCB->PaveBlocks().First();
2003       aLPBNew.Append(aPBNew);
2004     }
2005     //
2006     aLPB = aLPBNew;
2007   } 
2008   else {
2009     nE = aPBf->OriginalEdge();
2010     BOPDS_ListOfPaveBlock& aLPBE = myDS->ChangePaveBlocks(nE);
2011     aIt.Initialize(aLPB);
2012     for (; aIt.More(); aIt.Next()) {
2013       aPB = aIt.Value();
2014       aLPBE.Append(aPB);
2015     }
2016   }
2017   //
2018   Standard_Boolean bIn1, bIn2;
2019   //
2020   bIn1 = aMPBOn1.Contains(aPBf) || aMPBIn1.Contains(aPBf);
2021   bIn2 = aMPBOn2.Contains(aPBf) || aMPBIn2.Contains(aPBf);
2022   //
2023   if (bIn1 && bIn2) {
2024     return;
2025   }
2026   //
2027   // 4. Check new pave blocks for coincidence 
2028   //    with the opposite face.
2029   //    In case of coincidence create common blocks
2030   Standard_Integer nF;
2031   Standard_Real aTolE, aTolF;
2032   //
2033   nF = bIn1 ? nF2 : nF1;
2034   const TopoDS_Face& aF = *(TopoDS_Face*)&myDS->Shape(nF);
2035   BOPDS_IndexedMapOfPaveBlock& aMPBIn = bIn1 ? aMPBIn2 : aMPBIn1;
2036   aTolF = BRep_Tool::Tolerance(aF);
2037   //
2038   aIt.Initialize(aLPB);
2039   for (; aIt.More(); aIt.Next()) {
2040     Handle(BOPDS_PaveBlock)& aPBChangeValue = aIt.ChangeValue();
2041     const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPBChangeValue->Edge());
2042     aTolE = BRep_Tool::Tolerance(aE);
2043     //
2044     IntTools_EdgeFace anEF;
2045     anEF.SetEdge(aE);
2046     anEF.SetFace(aF);
2047     anEF.SetTolE(aTolE);
2048     anEF.SetTolF(aTolF);
2049     anEF.SetRange(aPBChangeValue->Pave1().Parameter(), aPBChangeValue->Pave2().Parameter());
2050     anEF.SetContext(myContext);
2051     anEF.Perform();
2052     //
2053     const IntTools_SequenceOfCommonPrts& aCPrts=anEF.CommonParts();
2054     if (aCPrts.Length() == 1) {
2055       Standard_Boolean bCoinc = (aCPrts(1).Type() == TopAbs_EDGE);
2056       if (bCoinc) {
2057         if (bCB) {
2058           aCB = myDS->CommonBlock(aPBChangeValue);
2059         } else {
2060           aCB = new BOPDS_CommonBlock;
2061           aCB->AddPaveBlock(aPBChangeValue);
2062           myDS->SetCommonBlock(aPBChangeValue, aCB);
2063         }
2064         aCB->AddFace(nF);
2065         //
2066         aMPBIn.Add(aPBChangeValue);
2067       }
2068     }
2069   }
2070 }
2071 //=======================================================================
2072 // function: PutClosingPaveOnCurve
2073 // purpose:
2074 //=======================================================================
2075 void BOPAlgo_PaveFiller::PutClosingPaveOnCurve(BOPDS_Curve& aNC)
2076 {
2077   Standard_Boolean bIsClosed, bHasBounds, bAdded;
2078   Standard_Integer nVC, j;
2079   Standard_Real aT[2], aTC, dT, aTx;
2080   gp_Pnt aP[2] ; 
2081   BOPDS_Pave aPVx;
2082   BOPDS_ListIteratorOfListOfPave aItLP;
2083   //
2084   const IntTools_Curve& aIC=aNC.Curve();
2085   const Handle(Geom_Curve)& aC3D=aIC.Curve();
2086   if(aC3D.IsNull()) {
2087     return;
2088   }
2089   //
2090   bIsClosed=IntTools_Tools::IsClosed(aC3D);
2091   if (!bIsClosed) {
2092     return;
2093   }
2094   //
2095   bHasBounds=aIC.HasBounds ();
2096   if (!bHasBounds){
2097     return;
2098   }
2099   // 
2100   bAdded=Standard_False;
2101   dT=Precision::PConfusion();
2102   aIC.Bounds (aT[0], aT[1], aP[0], aP[1]);
2103   //
2104   Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
2105   BOPDS_ListOfPave& aLP=aPB->ChangeExtPaves();
2106   //
2107   aItLP.Initialize(aLP);
2108   for (; aItLP.More() && !bAdded; aItLP.Next()) {
2109     const BOPDS_Pave& aPC=aItLP.Value();
2110     nVC=aPC.Index();
2111     aTC=aPC.Parameter();
2112     //
2113     for (j=0; j<2; ++j) {
2114       if (fabs(aTC-aT[j]) < dT) {
2115         aTx=(!j) ? aT[1] : aT[0];
2116         aPVx.SetIndex(nVC);
2117         aPVx.SetParameter(aTx);
2118         aLP.Append(aPVx);
2119         //
2120         bAdded=Standard_True;
2121         break;
2122       }
2123     }
2124   }
2125 }
2126 //=======================================================================
2127 //function : PreparePostTreatFF
2128 //purpose  : 
2129 //=======================================================================
2130 void BOPAlgo_PaveFiller::PreparePostTreatFF
2131     (const Standard_Integer aInt,
2132      const Standard_Integer aCur,
2133      const Handle(BOPDS_PaveBlock)& aPB,
2134      BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
2135      BOPCol_DataMapOfShapeInteger& aMVI,
2136      BOPDS_ListOfPaveBlock& aLPBC)
2137 {
2138   Standard_Integer nV1, nV2;
2139   //
2140   aLPBC.Append(aPB);
2141   //
2142   aPB->Indices(nV1, nV2);
2143   const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
2144   const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
2145   const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPB->Edge());
2146   // Keep info for post treatment 
2147   BOPDS_CoupleOfPaveBlocks aCPB;
2148   aCPB.SetIndexInterf(aInt);
2149   aCPB.SetIndex(aCur);
2150   aCPB.SetPaveBlock1(aPB);
2151   //
2152   aMSCPB.Add(aE, aCPB);
2153   aMVI.Bind(aV1, nV1);
2154   aMVI.Bind(aV2, nV2);
2155 }
2156
2157 //=======================================================================
2158 //function : CheckPlanes
2159 //purpose  : 
2160 //=======================================================================
2161 Standard_Boolean BOPAlgo_PaveFiller::CheckPlanes
2162   (const Standard_Integer nF1,
2163    const Standard_Integer nF2)const
2164 {
2165   Standard_Boolean bToIntersect;
2166   Standard_Integer i, nV2, iCnt;
2167   BOPCol_MapIteratorOfMapOfInteger aIt;
2168   //
2169   bToIntersect=Standard_False;
2170   //
2171   const BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
2172   const BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
2173   //
2174   const BOPCol_MapOfInteger& aMVIn1=aFI1.VerticesIn();
2175   const BOPCol_MapOfInteger& aMVOn1=aFI1.VerticesOn();
2176   //
2177   iCnt=0;
2178   for (i=0; (i<2 && !bToIntersect); ++i) {
2179     const BOPCol_MapOfInteger& aMV2=(!i) ? aFI2.VerticesIn() 
2180       : aFI2.VerticesOn();
2181     //
2182     aIt.Initialize(aMV2);
2183     for (; aIt.More(); aIt.Next()) {
2184       nV2=aIt.Value();
2185       if (aMVIn1.Contains(nV2) || aMVOn1.Contains(nV2)) {
2186         ++iCnt;
2187         if (iCnt>1) {
2188           bToIntersect=!bToIntersect;
2189           break;
2190         }
2191       }
2192     }
2193   }
2194   //
2195   return bToIntersect;
2196 }
2197 //=======================================================================
2198 //function : UpdatePaveBlocks
2199 //purpose  : 
2200 //=======================================================================
2201 void BOPAlgo_PaveFiller::UpdatePaveBlocks
2202 (const BOPCol_DataMapOfIntegerInteger& aDMI)
2203 {
2204   if (aDMI.IsEmpty()) {
2205     return;
2206   }
2207   //
2208   Standard_Integer nSp, aNbPBP, nV[2], i, j;
2209   Standard_Real aT[2];
2210   Standard_Boolean bCB, bRebuild;
2211   BOPDS_ListIteratorOfListOfPaveBlock aItPB;
2212   BOPDS_MapOfPaveBlock aMPB;
2213   BOPCol_MapOfInteger aMicroEdges;
2214   //
2215   BOPDS_VectorOfListOfPaveBlock& aPBP = myDS->ChangePaveBlocksPool();
2216   aNbPBP = aPBP.Extent();
2217   for (i = 0; i < aNbPBP; ++i) {
2218     BOPDS_ListOfPaveBlock& aLPB = aPBP(i);
2219     //
2220     aItPB.Initialize(aLPB);
2221     for (; aItPB.More(); aItPB.Next()) {
2222       Handle(BOPDS_PaveBlock) aPB = aItPB.Value();
2223       const Handle(BOPDS_CommonBlock)& aCB = myDS->CommonBlock(aPB);
2224       bCB = !aCB.IsNull();
2225       if (bCB) {
2226         aPB = aCB->PaveBlock1();
2227       }
2228       //
2229       if (aMPB.Add(aPB)) {
2230         bRebuild = Standard_False;
2231         aPB->Indices(nV[0], nV[1]);
2232         aPB->Range(aT[0], aT[1]);
2233         // remember the fact if the edge had different vertices before substitution
2234         Standard_Boolean wasRegularEdge = (nV[0] != nV[1]);
2235         //
2236         for (j = 0; j < 2; ++j) {
2237           if (aDMI.IsBound(nV[j])) {
2238             BOPDS_Pave aPave;
2239             //
2240             nV[j] = aDMI.Find(nV[j]);
2241             aPave.SetIndex(nV[j]);
2242             aPave.SetParameter(aT[j]);
2243             //
2244             bRebuild = Standard_True;
2245             if (!j) {
2246               aPB->SetPave1(aPave);
2247             }
2248             else {
2249               aPB->SetPave2(aPave);
2250             }
2251           }
2252         }
2253         //
2254         if (bRebuild) {
2255           Standard_Boolean isDegEdge = myDS->ShapeInfo(aPB->Edge()).HasFlag();
2256           if (wasRegularEdge && !isDegEdge && nV[0] == nV[1]) {
2257             // now edge has the same vertex on both ends;
2258             // check if it is not a regular closed curve.
2259             const TopoDS_Edge& aE = TopoDS::Edge(myDS->Shape(aPB->Edge()));
2260             const TopoDS_Vertex& aV = TopoDS::Vertex(myDS->Shape(nV[0]));
2261             Standard_Real aLength = IntTools::Length(aE);
2262             Standard_Real aTolV = BRep_Tool::Tolerance(aV);
2263             if (aLength <= aTolV * 2.) {
2264               // micro edge, so mark it for removal
2265               aMicroEdges.Add(aPB->Edge());
2266               continue;
2267             }
2268           }
2269           nSp = SplitEdge(aPB->OriginalEdge(), nV[0], aT[0], nV[1], aT[1]);
2270           if (bCB) {
2271             aCB->SetEdge(nSp);
2272           }
2273           else {
2274             aPB->SetEdge(nSp);
2275           }
2276         }// if (bRebuild) {
2277       }// if (aMPB.Add(aPB)) {
2278     }// for (; aItPB.More(); aItPB.Next()) {
2279   }// for (i=0; i<aNbPBP; ++i) {
2280   aMPB.Clear();
2281
2282   if (aMicroEdges.Extent())
2283     RemovePaveBlocks(aMicroEdges);
2284 }
2285 //=======================================================================
2286 //function : RemovePaveBlocks
2287 //purpose  : 
2288 //=======================================================================
2289 void BOPAlgo_PaveFiller::RemovePaveBlocks(const BOPCol_MapOfInteger theEdges)
2290 {
2291   // Remove all pave blocks referring to input edges:
2292   //
2293   // 1. from the Pave Blocks Pool
2294   BOPDS_VectorOfListOfPaveBlock& aPBP = myDS->ChangePaveBlocksPool();
2295   Standard_Integer aNbPBP = aPBP.Extent(), i;
2296   for (i = 0; i < aNbPBP; ++i) {
2297     BOPDS_ListOfPaveBlock& aLPB = aPBP(i);
2298     //
2299     BOPDS_ListIteratorOfListOfPaveBlock aItPB(aLPB);
2300     while (aItPB.More()) {
2301       const Handle(BOPDS_PaveBlock)& aPB = aItPB.Value();
2302       if (theEdges.Contains(aPB->Edge()))
2303         aLPB.Remove(aItPB);
2304       else
2305         aItPB.Next();
2306     }
2307   }
2308
2309   // 2. from Face Info and section curves
2310   BOPCol_MapOfInteger aMPassed;
2311   BOPDS_VectorOfInterfFF& aFFs = myDS->InterfFF();
2312   Standard_Integer aNbFF = aFFs.Extent(), j;
2313   for (i = 0; i < aNbFF; ++i) {
2314     BOPDS_InterfFF& aFF = aFFs(i);
2315     Standard_Integer nF1, nF2;
2316     aFF.Indices(nF1, nF2);
2317     //
2318     // rebuild pave block maps of face info
2319     for (j = 0; j < 2; j++) {
2320       Standard_Integer nF = (j == 0 ? nF1 : nF2);
2321       if (!aMPassed.Add(nF))
2322         continue;
2323       BOPDS_FaceInfo& aFI = myDS->ChangeFaceInfo(nF);
2324       BOPDS_IndexedMapOfPaveBlock* aIMPB[] = { &aFI.ChangePaveBlocksIn(),
2325         &aFI.ChangePaveBlocksOn(), &aFI.ChangePaveBlocksSc() };
2326       for (Standard_Integer k = 0; k < 3; k++) {
2327         Standard_Integer aNbPB = aIMPB[k]->Extent(), m;
2328         for (m = 1; m <= aNbPB; ++m) {
2329           const Handle(BOPDS_PaveBlock)& aPB = aIMPB[k]->FindKey(m);
2330           if (theEdges.Contains(aPB->Edge()))
2331             break;
2332         }
2333         if (m <= aNbPB) {
2334           BOPDS_IndexedMapOfPaveBlock aMPBCopy = *aIMPB[k];
2335           aIMPB[k]->Clear();
2336           for (m = 1; m <= aNbPB; ++m) {
2337             const Handle(BOPDS_PaveBlock)& aPB = aMPBCopy(m);
2338             if (!theEdges.Contains(aPB->Edge()))
2339               aIMPB[k]->Add(aPB);
2340           }
2341         }
2342       }
2343     }
2344     // remove from Section pave blocks
2345     BOPDS_VectorOfCurve& aVNC = aFF.ChangeCurves();
2346     Standard_Integer aNbC = aVNC.Extent();
2347     for (j = 0; j < aNbC; ++j) {
2348       BOPDS_Curve& aNC = aVNC(j);
2349       BOPDS_ListOfPaveBlock& aLPB = aNC.ChangePaveBlocks();
2350       BOPDS_ListIteratorOfListOfPaveBlock aItPB(aLPB);
2351       while (aItPB.More()) {
2352         const Handle(BOPDS_PaveBlock)& aPB = aItPB.Value();
2353         if (theEdges.Contains(aPB->Edge()))
2354           aLPB.Remove(aItPB);
2355         else
2356           aItPB.Next();
2357       }
2358     }
2359   }
2360 }
2361
2362 //=======================================================================
2363 //function : ToleranceFF
2364 //purpose  : Computes the TolFF according to the tolerance value and 
2365 //           types of the faces.
2366 //=======================================================================
2367   void ToleranceFF(const BRepAdaptor_Surface& aBAS1,
2368                    const BRepAdaptor_Surface& aBAS2,
2369                    Standard_Real& aTolFF)
2370 {
2371   Standard_Real aTol1, aTol2;
2372   Standard_Boolean isAna1, isAna2;
2373   //
2374   aTol1 = aBAS1.Tolerance();
2375   aTol2 = aBAS2.Tolerance();
2376   aTolFF = Max(aTol1, aTol2);
2377   //
2378   isAna1 = (aBAS1.GetType() == GeomAbs_Plane ||
2379             aBAS1.GetType() == GeomAbs_Cylinder ||
2380             aBAS1.GetType() == GeomAbs_Cone ||
2381             aBAS1.GetType() == GeomAbs_Sphere ||
2382             aBAS1.GetType() == GeomAbs_Torus);
2383   //
2384   isAna2 = (aBAS2.GetType() == GeomAbs_Plane ||
2385             aBAS2.GetType() == GeomAbs_Cylinder ||
2386             aBAS2.GetType() == GeomAbs_Cone ||
2387             aBAS2.GetType() == GeomAbs_Sphere ||
2388             aBAS2.GetType() == GeomAbs_Torus);
2389   //
2390   if (!isAna1 || !isAna2) {
2391     aTolFF =  Max(aTolFF, 5.e-6);
2392   }
2393 }
2394 //=======================================================================
2395 //function : UpdateBlocksWithSharedVertices
2396 //purpose  : 
2397 //=======================================================================
2398 void BOPAlgo_PaveFiller::UpdateBlocksWithSharedVertices()
2399 {
2400   if (!myNonDestructive) {
2401     return;
2402   }
2403   //
2404   myErrorStatus=0;
2405   //
2406   Standard_Integer aNbFF;
2407   //
2408   BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
2409   aNbFF=aFFs.Extent();
2410   if (!aNbFF) {
2411     return;
2412   }
2413   //
2414   Standard_Boolean bOnCurve, bHasShapeSD;
2415   Standard_Integer i, nF1, nF2, aNbC, j, nV, nVSD;
2416   Standard_Real aTolR3D, aTolV;
2417   BOPCol_MapOfInteger aMF;
2418   //
2419   for (i=0; i<aNbFF; ++i) {
2420     BOPDS_InterfFF& aFF=aFFs(i);
2421     //
2422     BOPDS_VectorOfCurve& aVC=aFF.ChangeCurves();
2423     aNbC=aVC.Extent();
2424     if (!aNbC) {
2425       continue;
2426     }
2427     //
2428     aFF.Indices(nF1, nF2);
2429     aTolR3D=aFF.TolR3D();
2430     //
2431     if (aMF.Add(nF1)) {
2432       myDS->UpdateFaceInfoOn(nF1);
2433     }
2434     if (aMF.Add(nF2)) {
2435       myDS->UpdateFaceInfoOn(nF2);
2436     }
2437     //
2438     // Collect old vertices that are shared for nF1, nF2 ->aMI;
2439     BOPCol_MapOfInteger aMI;
2440     BOPCol_MapIteratorOfMapOfInteger aItMI;
2441     //
2442     BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
2443     BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
2444     //
2445     const BOPCol_MapOfInteger& aMVOn1=aFI1.VerticesOn();
2446     const BOPCol_MapOfInteger& aMVIn1=aFI1.VerticesIn();
2447     const BOPCol_MapOfInteger& aMVOn2=aFI2.VerticesOn();
2448     const BOPCol_MapOfInteger& aMVIn2=aFI2.VerticesIn();
2449     //
2450     for (j=0; j<2; ++j) {
2451       const BOPCol_MapOfInteger& aMV1=(!j) ? aMVOn1 : aMVIn1;
2452       aItMI.Initialize(aMV1);
2453       for (; aItMI.More(); aItMI.Next()) {
2454         nV=aItMI.Value();
2455         if (myDS->IsNewShape(nV)) {
2456           continue;
2457         }
2458         if (aMVOn2.Contains(nV) || aMVIn2.Contains(nV)) {
2459           aMI.Add(nV);
2460         }
2461       }
2462     }
2463     //
2464     // Try to put vertices aMI on curves
2465     for (j=0; j<aNbC; ++j) {
2466       BOPDS_Curve& aNC=aVC.ChangeValue(j);
2467       //const IntTools_Curve& aIC=aNC.Curve();
2468       //
2469       aItMI.Initialize(aMI);
2470       for (; aItMI.More(); aItMI.Next()) {
2471         nV=aItMI.Value();
2472         //
2473         bHasShapeSD=myDS->HasShapeSD(nV, nVSD);
2474         if (bHasShapeSD) {
2475           continue;
2476         }
2477         //
2478         bOnCurve=EstimatePaveOnCurve(nV, aNC, aTolR3D);
2479         if (!bOnCurve) {
2480           continue;
2481         }
2482         //
2483         const TopoDS_Vertex& aV=*((TopoDS_Vertex *)&myDS->Shape(nV));
2484         aTolV=BRep_Tool::Tolerance(aV);
2485         //
2486         UpdateVertex(nV, aTolV);
2487       }
2488     }//for (j=0; j<aNbC; ++j) {
2489   }//for (i=0; i<aNbFF; ++i) {
2490   //
2491   UpdateCommonBlocksWithSDVertices();
2492 }
2493 //=======================================================================
2494 //function : EstimatePaveOnCurve
2495 //purpose  : 
2496 //=======================================================================
2497 Standard_Boolean BOPAlgo_PaveFiller::EstimatePaveOnCurve
2498   (const Standard_Integer nV,
2499    const BOPDS_Curve& aNC,
2500    const Standard_Real aTolR3D)
2501 {
2502   Standard_Boolean bIsVertexOnLine;
2503   Standard_Real aT;
2504   //
2505   const TopoDS_Vertex& aV=*((TopoDS_Vertex *)&myDS->Shape(nV));
2506   const IntTools_Curve& aIC=aNC.Curve();
2507   //
2508   bIsVertexOnLine=myContext->IsVertexOnLine(aV, aIC, aTolR3D, aT);
2509   return bIsVertexOnLine;
2510 }
2511
2512 //=======================================================================
2513 //function : CorrectToleranceOfSE
2514 //purpose  : 
2515 //=======================================================================
2516 void BOPAlgo_PaveFiller::CorrectToleranceOfSE()
2517 {
2518   BOPDS_VectorOfInterfFF& aFFs = myDS->InterfFF();
2519   NCollection_IndexedDataMap<Standard_Integer,BOPDS_ListOfPaveBlock> aMVIPBs;
2520   //
2521   // iterate on all sections F-F
2522   Standard_Integer aNb = aFFs.Extent(), i;
2523   for (i = 0; i < aNb; ++i) {
2524     const BOPDS_InterfFF& aFF = aFFs(i);
2525     Standard_Real aTolR3D = aFF.TolR3D();
2526     Standard_Real aTolReal = aFF.TolReal();
2527     if (aTolReal < aTolR3D) {
2528       // tolerance of intersection has been increased, so process this intersection
2529       const BOPDS_VectorOfCurve& aVNC = aFF.Curves();
2530       Standard_Integer aNbC = aVNC.Extent(), k;
2531       for (k = 0; k < aNbC; ++k) {
2532         const BOPDS_Curve& aNC = aVNC(k);
2533         const BOPDS_ListOfPaveBlock& aLPB = aNC.PaveBlocks();
2534         BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPB);
2535         for (; aItLPB.More(); aItLPB.Next()) {
2536           const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
2537           Standard_Integer nE;
2538           if (!aPB->HasEdge(nE) || aPB->OriginalEdge() >= 0) {
2539             continue;
2540           }
2541           const TopoDS_Edge& aE = TopoDS::Edge(myDS->Shape(nE));
2542           Standard_Real aTolE = BRep_Tool::Tolerance(aE);
2543           if (aTolReal < aTolE) {
2544             // reduce edge tolerance
2545             reinterpret_cast<BRep_TEdge*>(aE.TShape().operator->())->Tolerance(aTolReal);
2546           }
2547
2548           // fill in the map vertex index - pave blocks
2549           Handle(BOPDS_PaveBlock) aPBR = myDS->RealPaveBlock(aPB);
2550           for (Standard_Integer j=0; j < 2; j++) {
2551             Standard_Integer nV = (j == 0 ? aPBR->Pave1().Index() : aPBR->Pave2().Index());
2552             BOPDS_ListOfPaveBlock *pPBList = aMVIPBs.ChangeSeek(nV);
2553             if (!pPBList) {
2554               pPBList = &aMVIPBs.ChangeFromIndex(aMVIPBs.Add(nV, BOPDS_ListOfPaveBlock()));
2555             }
2556             pPBList->Append(aPBR);
2557           }
2558         }
2559       }
2560     }
2561   }
2562   // try to reduce tolerances of connected vertices
2563   aNb = aMVIPBs.Extent();
2564   for (i = 1; i <= aNb; ++i) {
2565     Standard_Integer nV = aMVIPBs.FindKey(i);
2566     const TopoDS_Vertex& aV = TopoDS::Vertex(myDS->Shape(nV));
2567     gp_Pnt aP = BRep_Tool::Pnt(aV);
2568     //
2569     // compute the maximal distance from the vertex to the adjacent edges
2570     Standard_Real aMaxTol = 0.;
2571     const BOPDS_ListOfPaveBlock& aLPB = aMVIPBs.FindFromIndex(i);
2572     BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPB);
2573     for (; aItLPB.More(); aItLPB.Next()) {
2574       const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
2575       Standard_Integer nE = aPB->Edge();
2576       const TopoDS_Edge& aE = TopoDS::Edge(myDS->Shape(nE));
2577       const BOPDS_Pave& aPave = (aPB->Pave1().Index() == nV ? aPB->Pave1() : aPB->Pave2());
2578       BRepAdaptor_Curve aC(aE);
2579       gp_Pnt aPonE = aC.Value(aPave.Parameter());
2580       Standard_Real aDist = aP.Distance(aPonE);
2581       aDist += BRep_Tool::Tolerance(aE);
2582       if (aDist > aMaxTol) {
2583         aMaxTol = aDist;
2584       }
2585     }
2586     Standard_Real aTolV = BRep_Tool::Tolerance(aV);
2587     if (aMaxTol < aTolV) {
2588       reinterpret_cast<BRep_TVertex*>(aV.TShape().operator->())->Tolerance(aMaxTol);
2589     }
2590   }
2591 }