BOPAlgo_PaveFiller_6.cxx 81.8 KB
Newer Older
1
// Created by: Peter KURNEV
2
// Copyright (c) 2010-2014 OPEN CASCADE SAS
3
4
5
6
// Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
// Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
//                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
//
7
// This file is part of Open CASCADE Technology software library.
8
//
9
10
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
11
12
13
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
14
//
15
16
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
17

18
19
20
21
22
#include <Bnd_Box.hxx>
#include <BOPAlgo_PaveFiller.hxx>
#include <BOPAlgo_SectionAttribute.hxx>
#include <BOPAlgo_Tools.hxx>
#include <BOPCol_DataMapOfIntegerReal.hxx>
23
#include <BOPCol_DataMapOfShapeInteger.hxx>
24
25
26
#include <BOPCol_ListOfInteger.hxx>
#include <BOPCol_ListOfShape.hxx>
#include <BOPCol_MapOfInteger.hxx>
27
#include <BOPCol_NCVector.hxx>
28
#include <BOPCol_Parallel.hxx>
29
30
#include <BOPDS_CommonBlock.hxx>
#include <BOPDS_CoupleOfPaveBlocks.hxx>
31
#include <BOPDS_Curve.hxx>
32
33
#include <BOPDS_DataMapOfPaveBlockListOfPaveBlock.hxx>
#include <BOPDS_DS.hxx>
34
#include <BOPDS_FaceInfo.hxx>
35
36
37
38
#include <BOPDS_Interf.hxx>
#include <BOPDS_Iterator.hxx>
#include <BOPDS_ListOfPave.hxx>
#include <BOPDS_ListOfPaveBlock.hxx>
39
40
#include <BOPDS_MapOfPaveBlock.hxx>
#include <BOPDS_PaveBlock.hxx>
41
42
#include <BOPDS_Point.hxx>
#include <BOPDS_ShapeInfo.hxx>
43
44
#include <BOPDS_VectorOfCurve.hxx>
#include <BOPDS_VectorOfPoint.hxx>
45
#include <BOPTools.hxx>
46
47
48
49
#include <BOPTools_AlgoTools.hxx>
#include <BOPTools_AlgoTools3D.hxx>
#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
50
#include <BRep_TEdge.hxx>
51
52
53
54
55
56
57
58
59
60
61
62
#include <BRepAdaptor_Curve.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepTools.hxx>
#include <Geom2d_Curve.hxx>
#include <Geom_Curve.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <GeomAPI_ProjectPointOnSurf.hxx>
#include <gp_Pnt.hxx>
#include <IntSurf_ListOfPntOn2S.hxx>
#include <IntSurf_PntOn2S.hxx>
63
#include <IntTools.hxx>
64
65
66
67
68
69
70
71
72
73
74
75
#include <IntTools_Context.hxx>
#include <IntTools_Curve.hxx>
#include <IntTools_EdgeFace.hxx>
#include <IntTools_FaceFace.hxx>
#include <IntTools_PntOn2Faces.hxx>
#include <IntTools_SequenceOfCurves.hxx>
#include <IntTools_SequenceOfPntOn2Faces.hxx>
#include <IntTools_ShrunkRange.hxx>
#include <IntTools_Tools.hxx>
#include <Precision.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
76
#include <TopoDS.hxx>
77
78
79
80
#include <TopoDS_Compound.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Vertex.hxx>
81

82
//
83
84
static void ToleranceFF(const BRepAdaptor_Surface& aBAS1,
                        const BRepAdaptor_Surface& aBAS2,
85
86
                        Standard_Real& aTolFF);

87
88
89
90
91
/////////////////////////////////////////////////////////////////////////
//=======================================================================
//class    : BOPAlgo_FaceFace
//purpose  : 
//=======================================================================
92
93
94
95
class BOPAlgo_FaceFace : 
  public IntTools_FaceFace,
  public BOPAlgo_Algo {

96
 public:
97
98
99
100
101
102
  DEFINE_STANDARD_ALLOC

  BOPAlgo_FaceFace() : 
    IntTools_FaceFace(),  
    BOPAlgo_Algo(),
    myIF1(-1), myIF2(-1), myTolFF(1.e-7) {
103
104
  }
  //
105
  virtual ~BOPAlgo_FaceFace() {
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  }
  //
  void SetIndices(const Standard_Integer nF1,
                  const Standard_Integer nF2) {
    myIF1=nF1;
    myIF2=nF2;
  }
  //
  void Indices(Standard_Integer& nF1,
               Standard_Integer& nF2) const {
    nF1=myIF1;
    nF2=myIF2;
  }
  //
  void SetFaces(const TopoDS_Face& aF1,
                const TopoDS_Face& aF2) {
    myF1=aF1;
    myF2=aF2;
  }
  //
  const TopoDS_Face& Face1()const {
    return myF1;
  }
  //
  const TopoDS_Face& Face2()const {
    return myF2;
  }
  //
  void SetTolFF(const Standard_Real aTolFF) {
    myTolFF=aTolFF;
  }
  //
  Standard_Real TolFF() const{
    return myTolFF;
  }
  //
142
143
  virtual void Perform() {
    BOPAlgo_Algo::UserBreak();
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    IntTools_FaceFace::Perform(myF1, myF2);
  }
  //
 protected:
  Standard_Integer myIF1;
  Standard_Integer myIF2;
  Standard_Real myTolFF;
  TopoDS_Face myF1;
  TopoDS_Face myF2;
};
//
//=======================================================================
typedef BOPCol_NCVector
  <BOPAlgo_FaceFace> BOPAlgo_VectorOfFaceFace; 
//
159
typedef BOPCol_Functor 
160
161
162
  <BOPAlgo_FaceFace,
  BOPAlgo_VectorOfFaceFace> BOPAlgo_FaceFaceFunctor;
//
163
typedef BOPCol_Cnt 
164
165
166
  <BOPAlgo_FaceFaceFunctor,
  BOPAlgo_VectorOfFaceFace> BOPAlgo_FaceFaceCnt;
/////////////////////////////////////////////////////////////////////////
167
168
169
170
//=======================================================================
//function : PerformFF
//purpose  : 
//=======================================================================
171
void BOPAlgo_PaveFiller::PerformFF()
172
173
174
175
176
177
178
179
180
181
182
183
184
{
  Standard_Integer iSize;
  Standard_Boolean bValid;
  //
  myErrorStatus=0;
  //
  myIterator->Initialize(TopAbs_FACE, TopAbs_FACE);
  iSize=myIterator->ExpectedLength();
  if (!iSize) {
    return; 
  }
  //
  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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  //
  BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
  aFFs.SetIncrement(iSize);
  //
  bApp=mySectionAttribute.Approximation();
  bCompC2D1=mySectionAttribute.PCurveOnS1();
  bCompC2D2=mySectionAttribute.PCurveOnS2();
  aApproxTol=1.e-7;
  bToSplit = Standard_False;
  //
  for (; myIterator->More(); myIterator->Next()) {
    myIterator->Value(nF1, nF2, bJustAdd);
    if(bJustAdd) {
      continue;
    }
    //
    const TopoDS_Face& aF1=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
    const TopoDS_Face& aF2=(*(TopoDS_Face *)(&myDS->Shape(nF2)));
    //
211
212
213
214
215
216
217
218
219
    if (aMI.Add(nF1)) {
      myDS->UpdateFaceInfoOn(nF1);
      myDS->UpdateFaceInfoIn(nF1);
    }
    if (aMI.Add(nF2)) {
      myDS->UpdateFaceInfoOn(nF2);
      myDS->UpdateFaceInfoIn(nF2);
    }
    //
220
221
222
223
224
225
226
227
228
    aBAS1.Initialize(aF1, Standard_False);
    aBAS2.Initialize(aF2, Standard_False);
    if (aBAS1.GetType() == GeomAbs_Plane && 
        aBAS2.GetType() == GeomAbs_Plane) {
      Standard_Boolean bToIntersect;
      //
      bToIntersect = CheckPlanes(nF1, nF2);
      if (!bToIntersect) {
        myDS->AddInterf(nF1, nF2);
229
        BOPDS_InterfFF& aFF=aFFs.Append1();
230
231
232
233
234
235
        aFF.SetIndices(nF1, nF2);
        aFF.Init(0, 0);
        continue;
      }
    }
    //
236
237
238
239
240
241
242
    ToleranceFF(aBAS1, aBAS2, aTolFF); 
    //
    BOPAlgo_FaceFace& aFaceFace=aVFaceFace.Append1();
    //
    aFaceFace.SetIndices(nF1, nF2);
    aFaceFace.SetFaces(aF1, aF2);
    aFaceFace.SetTolFF(aTolFF);
243
244
245
246
247
248
249
    //
    IntSurf_ListOfPntOn2S aListOfPnts;
    GetEFPnts(nF1, nF2, aListOfPnts);
    aNbLP = aListOfPnts.Extent();
    if (aNbLP) {
      aFaceFace.SetList(aListOfPnts);
    }
250
    //
251
    aFaceFace.SetParameters(bApp, bCompC2D1, bCompC2D2, aApproxTol);
252
    aFaceFace.SetProgressIndicator(myProgressIndicator);
253
254
255
256
257
258
259
260
261
262
263
264
  }//for (; myIterator->More(); myIterator->Next()) {
  //
  aNbFaceFace=aVFaceFace.Extent();
  //======================================================
  BOPAlgo_FaceFaceCnt::Perform(myRunParallel, aVFaceFace);
  //======================================================
  //
  for (k=0; k < aNbFaceFace; ++k) {
    BOPAlgo_FaceFace& aFaceFace=aVFaceFace(k);
    //
    aFaceFace.Indices(nF1, nF2);
    aTolFF=aFaceFace.TolFF();
265
266
267
268
269
    //
    bIsDone=aFaceFace.IsDone();
    if (bIsDone) {
      aTolR3D=aFaceFace.TolReached3d();
      aTolR2D=aFaceFace.TolReached2d();
270
      aTolReal = aFaceFace.TolReal();
271
      bTangentFaces=aFaceFace.TangentFaces();
272
273
274
275
      //
      if (aTolR3D < aTolFF){
        aTolR3D=aTolFF;
      }
276
277
278
      if (aTolReal < aTolFF) {
        aTolReal = aTolFF;
      }
279
280
281
282
283
284
285
286
287
288
289
290
      if (aTolR2D < 1.e-7){
        aTolR2D=1.e-7;
      }
      //
      aFaceFace.PrepareLines3D(bToSplit);
      //
      const IntTools_SequenceOfCurves& aCvsX=aFaceFace.Lines();
      const IntTools_SequenceOfPntOn2Faces& aPntsX=aFaceFace.Points();
      //
      aNbCurves=aCvsX.Length();
      aNbPoints=aPntsX.Length();
      //
pkv's avatar
pkv committed
291
292
293
      if (aNbCurves || aNbPoints) {
        myDS->AddInterf(nF1, nF2);
      } 
294
      //
295
      BOPDS_InterfFF& aFF=aFFs.Append1();
296
297
298
299
      aFF.SetIndices(nF1, nF2);
      //
      aFF.SetTolR3D(aTolR3D);
      aFF.SetTolR2D(aTolR2D);
300
      aFF.SetTolReal(aTolReal);
301
      aFF.SetTangentFaces(bTangentFaces);
302
303
304
305
306
      //
      // Curves, Points 
      aFF.Init(aNbCurves, aNbPoints);
      //
      // Curves
307
308
309
310
311
312
313
314
315
316
317
318

      // Fix bounding box expanding coefficient.
      Standard_Real aBoxExpandValue = aTolR3D;
      if (aNbCurves > 0)
      {
        // Modify geometric expanding coefficient by topology value,
        // since this bounging box used in sharing (vertex or edge).
        Standard_Real aMaxVertexTol = Max (BRep_Tool::MaxTolerance(aFaceFace.Face1(), TopAbs_VERTEX),
                                           BRep_Tool::MaxTolerance(aFaceFace.Face2(), TopAbs_VERTEX));
        aBoxExpandValue += aMaxVertexTol;
      }

319
320
321
322
323
324
      BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
      for (i=1; i<=aNbCurves; ++i) {
        Bnd_Box aBox;
        //
        const IntTools_Curve& aIC=aCvsX(i);
        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
329
330
331
332
333
334
335
336
337
338
          aNC.SetCurve(aIC);
          aNC.SetBox(aBox);
        }
      }
      //
      // Points
      BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
      for (i=1; i<=aNbPoints; ++i) {
        const IntTools_PntOn2Faces& aPi=aPntsX(i);
        const gp_Pnt& aP=aPi.P1().Pnt();
        //
339
        BOPDS_Point& aNP=aVNP.Append1();
340
341
342
343
344
        aNP.SetPnt(aP);
      }
    //}// if (aNbCs || aNbPs)
    }// if (bIsDone) {
    else {// 904/L1
345
      BOPDS_InterfFF& aFF=aFFs.Append1();
346
347
348
349
350
      aFF.SetIndices(nF1, nF2);
      aNbCurves=0;
      aNbPoints=0;
      aFF.Init(aNbCurves, aNbPoints);
    }
351
  }// for (k=0; k < aNbFaceFace; ++k) {
352
353
354
355
356
}
//=======================================================================
//function : MakeBlocks
//purpose  : 
//=======================================================================
357
void BOPAlgo_PaveFiller::MakeBlocks()
358
359
360
361
362
363
364
365
366
367
368
369
370
371
{
  Standard_Integer aNbFF;
  //
  myErrorStatus=0;
  //
  BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
  aNbFF=aFFs.Extent();
  if (!aNbFF) {
    return;
  }
  //
  Standard_Boolean bExist, bValid2D;
  Standard_Integer i, nF1, nF2, aNbC, aNbP, j;
  Standard_Integer nV1, nV2;
372
  Standard_Real aTolR3D, aT1, aT2, aTol;
373
  Handle(NCollection_BaseAllocator) aAllocator;
374
375
376
377
378
  BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
  TopoDS_Edge aES;
  Handle(BOPDS_PaveBlock) aPBOut;
  //
  //-----------------------------------------------------scope f
379
380
  aAllocator=
    NCollection_BaseAllocator::CommonBaseAllocator();
381
  //
382
  BOPCol_ListOfInteger aLSE(aAllocator), aLBV(aAllocator);
383
384
  BOPCol_MapOfInteger aMVOnIn(100, aAllocator), aMF(100, aAllocator),
                      aMVStick(100,aAllocator), aMVEF(100, aAllocator),
385
                      aMI(100, aAllocator), aMVBounds(100, aAllocator);
emv's avatar
emv committed
386
387
  BOPDS_IndexedMapOfPaveBlock aMPBOnIn(100, aAllocator);
  BOPDS_MapOfPaveBlock aMPBAdd(100, aAllocator);
388
389
390
  BOPDS_ListOfPaveBlock aLPB(aAllocator);
  BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks aMSCPB(100, aAllocator); 
  BOPCol_DataMapOfShapeInteger aMVI(100, aAllocator);
391
  BOPDS_DataMapOfPaveBlockListOfPaveBlock aDMExEdges(100, aAllocator);
392
  BOPCol_DataMapOfIntegerReal aMVTol(100, aAllocator);
393
  BOPCol_DataMapOfIntegerInteger aDMI(100, aAllocator);
394
395
  BOPCol_DataMapOfIntegerListOfInteger aDMBV(100, aAllocator);
  BOPCol_DataMapIteratorOfDataMapOfIntegerReal aItMV;
396
  BOPCol_IndexedMapOfShape aMicroEdges(100, aAllocator);
397
398
  //
  for (i=0; i<aNbFF; ++i) {
399
400
401
    //
    UserBreak();
    //
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
    BOPDS_InterfFF& aFF=aFFs(i);
    aFF.Indices(nF1, nF2);
    //
    BOPDS_VectorOfPoint& aVP=aFF.ChangePoints();
    aNbP=aVP.Extent();
    BOPDS_VectorOfCurve& aVC=aFF.ChangeCurves();
    aNbC=aVC.Extent();
    if (!aNbP && !aNbC) {
      continue;
    }
    //
    const TopoDS_Face& aF1=(*(TopoDS_Face *)(&myDS->Shape(nF1)));
    const TopoDS_Face& aF2=(*(TopoDS_Face *)(&myDS->Shape(nF2)));
    //
    aTolR3D=aFF.TolR3D();
    //
    // Update face info
    if (aMF.Add(nF1)) {
      myDS->UpdateFaceInfoOn(nF1);
421
      myDS->UpdateFaceInfoIn(nF1);
422
423
424
    }
    if (aMF.Add(nF2)) {
      myDS->UpdateFaceInfoOn(nF2);
425
      myDS->UpdateFaceInfoIn(nF2);
426
    }
427
    //
428
429
430
431
432
    BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
    BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
    //
    aMVOnIn.Clear();
    aMPBOnIn.Clear();
433
    aDMBV.Clear();
434
    aMVTol.Clear();
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    //
    myDS->VerticesOnIn(nF1, nF2, aMVOnIn, aMPBOnIn);
    myDS->SharedEdges(nF1, nF2, aLSE, aAllocator);
    
    // 1. Treat Points
    for (j=0; j<aNbP; ++j) {
      TopoDS_Vertex aV;
      BOPDS_CoupleOfPaveBlocks aCPB;
      //
      BOPDS_Point& aNP=aVP.ChangeValue(j);
      const gp_Pnt& aP=aNP.Pnt();
      //
      bExist=IsExistingVertex(aP, aTolR3D, aMVOnIn);
      if (!bExist) {
        BOPTools_AlgoTools::MakeNewVertex(aP, aTolR3D, aV);
        //
        aCPB.SetIndexInterf(i);
        aCPB.SetIndex(j);
        aMSCPB.Add(aV, aCPB);
      }
    }
    //
    // 2. Treat Curves
    aMVStick.Clear();
    aMVEF.Clear();
460
    GetStickVertices(nF1, nF2, aMVStick, aMVEF, aMI);
461
462
463
464
465
466
467
    //
    for (j=0; j<aNbC; ++j) {
      BOPDS_Curve& aNC=aVC.ChangeValue(j);
      const IntTools_Curve& aIC=aNC.Curve();
      // DEBt
      aNC.InitPaveBlock1();
      //
468
      PutPavesOnCurve(aMVOnIn, aTolR3D, aNC, nF1, nF2, aMI, aMVEF, aMVTol);
469
      //
470
      PutStickPavesOnCurve(aF1, aF2, aMI, aNC, aMVStick, aMVTol);
471
      //904/F7
472
      if (aNbC == 1) {
473
        PutEFPavesOnCurve(aNC, aMI, aMVEF, aMVTol);
474
475
476
      }
      //
      if (aIC.HasBounds()) {
477
478
479
480
481
482
        aLBV.Clear();
        //
        PutBoundPaveOnCurve(aF1, aF2, aTolR3D, aNC, aLBV);
        //
        if (!aLBV.IsEmpty()) {
          aDMBV.Bind(j, aLBV);
483
484
485
486
          BOPCol_ListIteratorOfListOfInteger aItI(aLBV);
          for (; aItI.More(); aItI.Next()) {
            aMVBounds.Add(aItI.Value());
          }
487
        }
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
      }
    }//for (j=0; j<aNbC; ++j) {
    //
    // Put closing pave if needed
    for (j=0; j<aNbC; ++j) {
      BOPDS_Curve& aNC=aVC.ChangeValue(j);
      PutClosingPaveOnCurve (aNC);
    }
    //
    // 3. Make section edges
    for (j=0; j<aNbC; ++j) {
      BOPDS_Curve& aNC=aVC.ChangeValue(j);
      const IntTools_Curve& aIC=aNC.Curve();
      //
      BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
      Handle(BOPDS_PaveBlock)& aPB1=aNC.ChangePaveBlock1();
      //
      aLPB.Clear();
      aPB1->Update(aLPB, Standard_False);
      //
      aItLPB.Initialize(aLPB);
      for (; aItLPB.More(); aItLPB.Next()) {
        Handle(BOPDS_PaveBlock)& aPB=aItLPB.ChangeValue();
        aPB->Indices(nV1, nV2);
        aPB->Range  (aT1, aT2);
        //
        if (fabs(aT1 - aT2) < Precision::PConfusion()) {
          continue;
        }
        //
518
519
        bValid2D=myContext->IsValidBlockForFaces(aT1, aT2, aIC, 
                                                 aF1, aF2, aTolR3D);
520
521
522
523
        if (!bValid2D) {
          continue;
        }
        //
524
        bExist=IsExistingPaveBlock(aPB, aNC, aLSE);
525
526
527
528
        if (bExist) {
          continue;
        }
        //
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
        const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
        const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
        //
        // Make Edge
        BOPTools_AlgoTools::MakeEdge (aIC, aV1, aT1, aV2, aT2, aTolR3D, aES);
        //
        // check for micro edge
        if (BOPTools_AlgoTools::IsMicroEdge(aES, myContext, Standard_False)) {
          // If the section edge is a micro edge, i.e. the whole edge is
          // covered by the tolerance spheres of its vertices, it will be
          // passed into post treatment process to fuse its vertices.
          // The edge itself will not be kept.
          if (!aMVBounds.Contains(nV1) && !aMVBounds.Contains(nV2)) {
            aMicroEdges.Add(aES);
            // keep vertices for post treatment
            aMVI.Bind(aV1, nV1);
            aMVI.Bind(aV2, nV2);
          }
          continue;
        }
        //
550
551
        Standard_Real aTolNew;
        bExist=IsExistingPaveBlock(aPB, aNC, aTolR3D, aMPBOnIn, aPBOut, aTolNew);
552
553
554
        if (bExist) {
          if (aMPBAdd.Add(aPBOut)) {
            Standard_Boolean bInBothFaces = Standard_True;
omy's avatar
omy committed
555
            if (!myDS->IsCommonBlock(aPBOut)) {
556
557
558
559
560
561
              Standard_Integer nE;
              Standard_Real aTolE;
              //
              nE = aPBOut->Edge();
              const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(nE);
              aTolE = BRep_Tool::Tolerance(aE);
562
563
564
565
              if (aTolNew < aFF.TolReal())
                aTolNew = aFF.TolReal();  // use real tolerance of intersection
              if (aTolNew > aTolE) {
                UpdateEdgeTolerance(nE, aTolNew);
566
567
              }
              bInBothFaces = Standard_False;
568
569
            } 
            else {
570
571
572
573
574
575
              bInBothFaces = (aFI1.PaveBlocksOn().Contains(aPBOut) ||
                              aFI1.PaveBlocksIn().Contains(aPBOut))&&
                             (aFI2.PaveBlocksOn().Contains(aPBOut) ||
                              aFI2.PaveBlocksIn().Contains(aPBOut));
            }
            if (!bInBothFaces) {
576
              PreparePostTreatFF(i, j, aPBOut, aMSCPB, aMVI, aLPBC);
577
578
579
580
581
            }
          }
          continue;
        }
        //
582
        // Make p-curves
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
        BOPTools_AlgoTools::MakePCurve(aES, aF1, aF2, aIC, 
                                       mySectionAttribute.PCurveOnS1(),
                                       mySectionAttribute.PCurveOnS2());
        //
        // Append the Pave Block to the Curve j
        aLPBC.Append(aPB);
        //
        // Keep info for post treatment 
        BOPDS_CoupleOfPaveBlocks aCPB;
        aCPB.SetIndexInterf(i);
        aCPB.SetIndex(j);
        aCPB.SetPaveBlock1(aPB);
        //
        aMSCPB.Add(aES, aCPB);
        aMVI.Bind(aV1, nV1);
        aMVI.Bind(aV2, nV2);
599
600
601
        //
        aMVTol.UnBind(nV1);
        aMVTol.UnBind(nV2);
602
603
604
605
      }
      //
      aLPBC.RemoveFirst();
    }//for (j=0; j<aNbC; ++j) {
606
607
608
609
610
611
612
    //back to previous tolerance values for unused vertices
    aItMV.Initialize(aMVTol);
    for (; aItMV.More(); aItMV.Next()) {
      nV1 = aItMV.Key();
      aTol = aItMV.Value();
      //
      const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&myDS->Shape(nV1);
613
614
      const Handle(BRep_TVertex)& TV = 
        *((Handle(BRep_TVertex)*)&aV.TShape());
615
616
617
      TV->Tolerance(aTol);
    }
    //
618
    ProcessExistingPaveBlocks(i, aMPBOnIn, aDMBV, aMSCPB, aMVI, aMPBAdd);
619
620
621
  }//for (i=0; i<aNbFF; ++i) {
  // 
  // post treatment
622
  myErrorStatus=PostTreatFF(aMSCPB, aMVI, aDMExEdges, aDMI, aMicroEdges, aAllocator);
623
624
625
  if (myErrorStatus) {
    return;
  }
626
627
  // reduce tolerances of section edges where it is appropriate
  CorrectToleranceOfSE();
628
629
  //
  // update face info
630
  UpdateFaceInfo(aDMExEdges, aDMI);
631
632
  //Update all pave blocks
  UpdatePaveBlocks(aDMI);
633
634
635
636
637
638
  //-----------------------------------------------------scope t
  aMF.Clear();
  aMVStick.Clear();
  aMPBOnIn.Clear();
  aMVOnIn.Clear();
  aDMExEdges.Clear();
639
640
  aMI.Clear();
  aDMI.Clear();
641
642
643
644
645
646
}

//=======================================================================
//function : PostTreatFF
//purpose  : 
//=======================================================================
647
Standard_Integer BOPAlgo_PaveFiller::PostTreatFF
648
649
650
    (BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& theMSCPB,
     BOPCol_DataMapOfShapeInteger& aMVI,
     BOPDS_DataMapOfPaveBlockListOfPaveBlock& aDMExEdges,
651
     BOPCol_DataMapOfIntegerInteger& aDMI,
652
     const BOPCol_IndexedMapOfShape& theMicroEdges,
653
     const Handle(NCollection_BaseAllocator)& theAllocator)
654
655
656
657
658
659
660
661
662
663
{
  Standard_Integer iRet, aNbS;
  //
  iRet=0;
  aNbS=theMSCPB.Extent();
  if (!aNbS) {
    return iRet;
  }
  //
  Standard_Boolean bHasPaveBlocks, bOld;
664
  Standard_Integer iErr, nSx, nVSD, iX, iP, iC, j, nV, iV = 0, iE, k;
665
  Standard_Integer aNbLPBx;
666
667
668
669
670
671
  TopAbs_ShapeEnum aType;
  TopoDS_Shape aV, aE;
  BOPCol_ListIteratorOfListOfShape aItLS;
  BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
  BOPDS_PDS aPDS;
  Handle(BOPDS_PaveBlock) aPB1;
672
  BOPDS_Pave aPave[2];
673
674
675
676
  BOPDS_ShapeInfo aSI;
  //
  BOPCol_ListOfShape aLS(theAllocator);
  BOPAlgo_PaveFiller aPF(theAllocator);
677
678
  aPF.SetIsPrimary(Standard_False);
  aPF.SetNonDestructive(myNonDestructive);
679
680
681
  //
  BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
  //
682
  Standard_Integer aNbME = theMicroEdges.Extent();
683
  // 0
684
  if (aNbS==1 && (aNbME == 0)) {
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    const TopoDS_Shape& aS=theMSCPB.FindKey(1);
    const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromIndex(1);
    //
    aType=aS.ShapeType();
    if (aType==TopAbs_VERTEX) {
      aSI.SetShapeType(aType);
      aSI.SetShape(aS);
      iV=myDS->Append(aSI);
      //
      iX=aCPB.IndexInterf();
      iP=aCPB.Index();
      BOPDS_InterfFF& aFF=aFFs(iX); 
      BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
      BOPDS_Point& aNP=aVNP(iP);
      aNP.SetIndex(iV);
    }
    else if (aType==TopAbs_EDGE) {
      aPB1=aCPB.PaveBlock1();
      //
      if (aPB1->HasEdge()) {
        BOPDS_ListOfPaveBlock aLPBx;
        aLPBx.Append(aPB1);
        aDMExEdges.Bind(aPB1, aLPBx);
      } else {
        aSI.SetShapeType(aType);
        aSI.SetShape(aS);
        iE=myDS->Append(aSI);
        //
        aPB1->SetEdge(iE);
      }
    }
    return iRet;
  }
  //
  // 1 prepare arguments
  for (k=1; k<=aNbS; ++k) {
    const TopoDS_Shape& aS=theMSCPB.FindKey(k);
    aLS.Append(aS);
  }
  //
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
  // The section edges considered as a micro should be
  // specially treated - their vertices should be united and
  // the edge itself should be removed. Thus, we add only
  // its vertices into operation.
  //
  BRep_Builder aBB;
  for (k = 1; k <= aNbME; ++k) {
    const TopoDS_Edge& aE = TopoDS::Edge(theMicroEdges(k));
    //
    TopoDS_Vertex aV1, aV2;
    TopExp::Vertices(aE, aV1, aV2);
    //
    aLS.Append(aV1);
    aLS.Append(aV2);
    //
    // make sure these vertices will be united
    const gp_Pnt& aP1 = BRep_Tool::Pnt(aV1);
    const gp_Pnt& aP2 = BRep_Tool::Pnt(aV2);
    //
    Standard_Real aDist = aP1.Distance(aP2);
    Standard_Real aTolV1 = BRep_Tool::Tolerance(aV1);
    Standard_Real aTolV2 = BRep_Tool::Tolerance(aV2);
    //
    aDist -= (aTolV1 + aTolV2);
    if (aDist > 0.) {
      aDist /= 2.;
      aBB.UpdateVertex(aV1, aTolV1 + aDist);
      aBB.UpdateVertex(aV2, aTolV2 + aDist);
    }
  }
  //
756
  // 2 Fuse shapes
757
  aPF.SetProgressIndicator(myProgressIndicator);
758
  aPF.SetRunParallel(myRunParallel);
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
  aPF.SetArguments(aLS);
  aPF.Perform();
  iErr=aPF.ErrorStatus();
  if (iErr) {
    return iRet;
  }
  aPDS=aPF.PDS();
  //
  aItLS.Initialize(aLS);
  for (; aItLS.More(); aItLS.Next()) {
    const TopoDS_Shape& aSx=aItLS.Value();
    nSx=aPDS->Index(aSx);
    const BOPDS_ShapeInfo& aSIx=aPDS->ShapeInfo(nSx);
    //
    aType=aSIx.ShapeType();
    //
    if (aType==TopAbs_VERTEX) {
776
777
      Standard_Boolean bIntersectionPoint = theMSCPB.Contains(aSx);
      //
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
      if (aPDS->HasShapeSD(nSx, nVSD)) {
        aV=aPDS->Shape(nVSD);
      }
      else {
        aV=aSx;
      }
      // index of new vertex in theDS -> iV
      if (!aMVI.IsBound(aV)) {
        aSI.SetShapeType(aType);
        aSI.SetShape(aV);
        iV=myDS->Append(aSI);
        //
        aMVI.Bind(aV, iV);
      }
      else {
        iV=aMVI.Find(aV);
      }
795
796
797
798
799
800
801
802
      //
      if (!bIntersectionPoint) {
        // save SD connection
        nSx = aMVI.Find(aSx);
        aDMI.Bind(nSx, iV);
        myDS->AddShapeSD(nSx, iV);
      }
      else {
803
      // update FF interference
804
805
806
807
808
809
810
811
        const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromKey(aSx);
        iX=aCPB.IndexInterf();
        iP=aCPB.Index();
        BOPDS_InterfFF& aFF=aFFs(iX);
        BOPDS_VectorOfPoint& aVNP=aFF.ChangePoints();
        BOPDS_Point& aNP=aVNP(iP);
        aNP.SetIndex(iV);
      }
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
    }//if (aType==TopAbs_VERTEX) {
    //
    else if (aType==TopAbs_EDGE) {
      bHasPaveBlocks=aPDS->HasPaveBlocks(nSx);
      const BOPDS_CoupleOfPaveBlocks &aCPB=theMSCPB.FindFromKey(aSx);
      iX=aCPB.IndexInterf();
      iC=aCPB.Index();
      aPB1=aCPB.PaveBlock1();
      //
      bOld = aPB1->HasEdge();
      if (bOld) {
        BOPDS_ListOfPaveBlock aLPBx;
        aDMExEdges.Bind(aPB1, aLPBx);
      }
      //
      if (!bHasPaveBlocks) {
        if (bOld) {
          aDMExEdges.ChangeFind(aPB1).Append(aPB1);
830
831
        }
        else {
832
833
          aSI.SetShapeType(aType);
          aSI.SetShape(aSx);
834
          iE = myDS->Append(aSI);
835
836
837
838
839
840
841
842
843
844
          //
          aPB1->SetEdge(iE);
        }
      }
      else {
        BOPDS_InterfFF& aFF=aFFs(iX);
        BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
        BOPDS_Curve& aNC=aVNC(iC);
        BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
        //
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
        // check if edge occured to be micro edge;
        // note we check not the edge aSx itself, but its image in aPDS
        const BOPDS_ListOfPaveBlock& aLPBx = aPDS->PaveBlocks(nSx);
        aNbLPBx = aLPBx.Extent();
        if (aPDS->HasPaveBlocks(nSx) &&
            (aNbLPBx == 0 || (aNbLPBx == 1 && !aLPBx.First()->HasShrunkData()))) {
          BOPDS_ListIteratorOfListOfPaveBlock it(aLPBC);
          for (; it.More(); it.Next()) {
            if (it.Value() == aPB1) {
              aLPBC.Remove(it);
              break;
            }
          }
          continue;
        }
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
        //
        if (bOld && !aNbLPBx) {
          aDMExEdges.ChangeFind(aPB1).Append(aPB1);
          continue;
        }
        //
        if (!bOld) {
          aItLPB.Initialize(aLPBC);
          for (; aItLPB.More(); aItLPB.Next()) {
            const Handle(BOPDS_PaveBlock)& aPBC=aItLPB.Value();
            if (aPBC==aPB1) {
              aLPBC.Remove(aItLPB);
              break;
            }
          } 
        }
        //
        if (!aNbLPBx) {
          aE=aSx;
          //
          if (!aMVI.IsBound(aE)) {
            aSI.SetShapeType(aType);
            aSI.SetShape(aE);
            iE=myDS->Append(aSI);
            aMVI.Bind(aE, iE);
          }
          else {
            iE=aMVI.Find(aE);
          }
          // append new PaveBlock to aLPBC
          aPB1->SetEdge(iE);
          aLPBC.Append(aPB1);
        } // if (!aNbLPBx) {
        //
        else {
          aItLPB.Initialize(aLPBx);
          for (; aItLPB.More(); aItLPB.Next()) {
            const Handle(BOPDS_PaveBlock)& aPBx=aItLPB.Value();
omy's avatar
omy committed
898
            const Handle(BOPDS_PaveBlock) aPBRx=aPDS->RealPaveBlock(aPBx);
899
900
901
902
903
            //
            // update vertices of paves
            aPave[0]=aPBx->Pave1();
            aPave[1]=aPBx->Pave2();
            for (j=0; j<2; ++j) {
904
905
906
907
908
909
910
911
912
913
914
915
              nV = aPave[j].Index();
              aV = aPDS->Shape(nV);
              //
              if (!aMVI.IsBound(aV)) {
                // index of new vertex in theDS -> iV
                aSI.SetShapeType(TopAbs_VERTEX);
                aSI.SetShape(aV);
                iV = myDS->Append(aSI);
                aMVI.Bind(aV, iV);
              }
              else {
                iV = aMVI.Find(aV);
916
              }
917
              const BOPDS_Pave& aP1 = !j ? aPB1->Pave1() : aPB1->Pave2();
918
919
              if (aP1.Parameter() == aPave[j].Parameter() && 
                  aP1.Index() != iV) {
920
                aDMI.Bind(aP1.Index(), iV);
921
                myDS->AddShapeSD(aP1.Index(), iV);
922
923
              }
              //
924
925
926
927
928
929
930
931
932
933
934
              aPave[j].SetIndex(iV);
            }
            //
            // add edge
            aE=aPDS->Shape(aPBRx->Edge());
            //
            if (!aMVI.IsBound(aE)) {
              aSI.SetShapeType(aType);
              aSI.SetShape(aE);
              iE=myDS->Append(aSI);
              aMVI.Bind(aE, iE);
935
936
937
938
939
940
941
942
              // update real edge tolerance according to distances in common block if any
              if (aPDS->IsCommonBlock(aPBx)) {
                const Handle(BOPDS_CommonBlock)& aCB = aPDS->CommonBlock(aPBx);
                Standard_Real aTol = BOPAlgo_Tools::ComputeToleranceOfCB(aCB, aPDS, aPF.Context());
                if (aFF.TolReal() < aTol) {
                  aFF.SetTolReal(aTol);
                }
              }
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
            }
            else {
              iE=aMVI.Find(aE);
            }
            // append new PaveBlock to aLPBC
            Handle(BOPDS_PaveBlock) aPBC=new BOPDS_PaveBlock();
            //
            aPBC->SetPave1(aPave[0]);
            aPBC->SetPave2(aPave[1]);
            aPBC->SetEdge(iE);
            if (bOld) {
              aPBC->SetOriginalEdge(aPB1->OriginalEdge());
              aDMExEdges.ChangeFind(aPB1).Append(aPBC);
            }
            else {
              aLPBC.Append(aPBC);
            }
          }
        }
      }
    }//else if (aType==TopAbs_EDGE)
  }//for (; aItLS.More(); aItLS.Next()) {
  return iRet;
}

//=======================================================================
//function : UpdateFaceInfo
//purpose  : 
//=======================================================================
972
void BOPAlgo_PaveFiller::UpdateFaceInfo
973
974
  (BOPDS_DataMapOfPaveBlockListOfPaveBlock& theDME,
   const BOPCol_DataMapOfIntegerInteger& theDMV)
975
976
{
  Standard_Integer i, j, nV1, nF1, nF2, 
977
                   aNbFF, aNbC, aNbP;
978
  BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
979
  BOPCol_MapOfInteger aMF;
980
981
982
983
984
985
986
987
988
989
990
991
  //
  BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
  aNbFF=aFFs.Extent();
  //
  //1. Sections (curves, points);
  for (i=0; i<aNbFF; ++i) {
    BOPDS_InterfFF& aFF=aFFs(i);
    aFF.Indices(nF1, nF2);
    //
    BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
    BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
    //
992
    // 1.1. Section edges
993
994
995
996
997
998
    BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
    aNbC=aVNC.Extent();
    for (j=0; j<aNbC; ++j) {
      BOPDS_Curve& aNC=aVNC(j);
      BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
      //
999
      // Add section edges to face info
1000
1001
      aItLPB.Initialize(aLPBC);
      for (; aItLPB.More(); ) {
1002
        const Handle(BOPDS_PaveBlock)& aPB=aItLPB.Value();
1003
1004
1005
1006
1007
1008
1009
1010
1011
        //
        // Treat existing pave blocks
        if (theDME.IsBound(aPB)) {
          BOPDS_ListOfPaveBlock& aLPB=theDME.ChangeFind(aPB);
          UpdateExistingPaveBlocks(aPB, aLPB, nF1, nF2);
          aLPBC.Remove(aItLPB);
          continue;
        }
        //
1012
1013
        aFI1.ChangePaveBlocksSc().Add(aPB);
        aFI2.ChangePaveBlocksSc().Add(aPB);
1014
        aItLPB.Next();
1015
1016
      }
    }
1017
1018
    //
    // 1.2. Section vertices
1019
1020
1021
1022
1023
    const BOPDS_VectorOfPoint& aVNP=aFF.Points();
    aNbP=aVNP.Extent();
    for (j=0; j<aNbP; ++j) {
      const BOPDS_Point& aNP=aVNP(j);
      nV1=aNP.Index();
pkv's avatar
pkv committed
1024
1025
1026
      if (nV1<0) {
        continue;
      }
1027
1028
1029
      aFI1.ChangeVerticesSc().Add(nV1);
      aFI2.ChangeVerticesSc().Add(nV1);
    }
1030
1031
1032
    //
    aMF.Add(nF1);
    aMF.Add(nF2);
1033
1034
  }
  //
1035
1036
1037
1038
1039
1040
  Standard_Boolean bVerts, bEdges;
  //
  bVerts = theDMV.Extent() > 0;
  bEdges = theDME.Extent() > 0;
  //
  if (!bVerts && !bEdges) {
1041
1042
1043
    return;
  }
  //
1044
1045
1046
1047
1048
1049
1050
1051
1052
  // 2. Update Face Info information with new vertices and new
  //    pave blocks created in PostTreatFF from existing ones
  Standard_Integer nV2, aNbPB;
  BOPCol_MapIteratorOfMapOfInteger aItMF;
  BOPCol_DataMapIteratorOfDataMapOfIntegerInteger aItMV;
  //
  aItMF.Initialize(aMF);
  for (; aItMF.More(); aItMF.Next()) {
    nF1 = aItMF.Value();
1053
    //
1054
    BOPDS_FaceInfo& aFI = myDS->ChangeFaceInfo(nF1);
1055
    //
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
    // 2.1. Update information about vertices
    if (bVerts) {
      BOPCol_MapOfInteger& aMVOn = aFI.ChangeVerticesOn();
      BOPCol_MapOfInteger& aMVIn = aFI.ChangeVerticesIn();
      //
      aItMV.Initialize(theDMV);
      for (; aItMV.More(); aItMV.Next()) {
        nV1 = aItMV.Key();
        nV2 = aItMV.Value();
        //
        if (aMVOn.Remove(nV1)) {
          aMVOn.Add(nV2);
1068
        }
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
        //
        if (aMVIn.Remove(nV1)) {
          aMVIn.Add(nV2);
        }
      } // for (; aItMV.More(); aItMV.Next()) {
    } // if (bVerts) {
    //
    // 2.2. Update information about pave blocks
    if (bEdges) {
      BOPDS_IndexedMapOfPaveBlock& aMPBOn = aFI.ChangePaveBlocksOn();
      BOPDS_IndexedMapOfPaveBlock& aMPBIn = aFI.ChangePaveBlocksIn();
      //
      BOPDS_IndexedMapOfPaveBlock aMPBCopy;
      for (i = 0; i < 2; ++i) {
        BOPDS_IndexedMapOfPaveBlock& aMPBOnIn = !i ? aMPBOn : aMPBIn;
        aMPBCopy = aMPBOnIn;
        aMPBOnIn.Clear();
        //
        aNbPB = aMPBCopy.Extent();
        for (j = 1; j <= aNbPB; ++j) {
          const Handle(BOPDS_PaveBlock)& aPB = aMPBCopy(j);
          if (theDME.IsBound(aPB)) {
            const BOPDS_ListOfPaveBlock& aLPB = theDME.Find(aPB);
1092
1093
1094
1095
1096
            if (aLPB.IsEmpty()) {
              aMPBOnIn.Add(aPB);
              continue;
            }
            //
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
            aItLPB.Initialize(aLPB);
            for (; aItLPB.More(); aItLPB.Next()) {
              const Handle(BOPDS_PaveBlock)& aPB1 = aItLPB.Value();
              aMPBOnIn.Add(aPB1);
            }
          }
          else {
            aMPBOnIn.Add(aPB);
          }
        } // for (j = 1; j <= aNbPB; ++j) {
      } // for (i = 0; i < 2; ++i) {
    } // if (bEdges) {
  }
1110
1111
1112
1113
1114
}
//=======================================================================
//function : IsExistingVertex
//purpose  : 
//=======================================================================
1115
1116
1117
1118
Standard_Boolean BOPAlgo_PaveFiller::IsExistingVertex
  (const gp_Pnt& aP,
   const Standard_Real theTolR3D,
   const BOPCol_MapOfInteger& aMVOnIn)const
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
{
  Standard_Boolean bRet;
  Standard_Integer nV, iFlag;
  gp_Pnt aPV;
  Bnd_Box aBoxP;
  BOPCol_MapIteratorOfMapOfInteger aIt;
  //
  bRet=Standard_True;
  //
  aBoxP.Add(aP);
  aBoxP.Enlarge(theTolR3D);
  //
  aIt.Initialize(aMVOnIn);
  for (; aIt.More(); aIt.Next()) {
    nV=aIt.Value();
1134
1135
1136
    const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
    const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&aSIV.Shape()));
    const Bnd_Box& aBoxV=aSIV.Box();
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
    //
    if (!aBoxP.IsOut(aBoxV)) {
      iFlag=BOPTools_AlgoTools::ComputeVV(aV, aP, theTolR3D);
      if (!iFlag) {
        return bRet;
      }
    }
  }
  return !bRet;
}
//=======================================================================
//function : IsExistingPaveBlock
//purpose  : 
//=======================================================================
1151
1152
1153
1154
Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
  (const Handle(BOPDS_PaveBlock)& thePB,
   const BOPDS_Curve& theNC,
   const BOPCol_ListOfInteger& theLSE)
1155
1156
1157
1158
1159
1160
1161
{
  Standard_Boolean bRet=Standard_True;
  //
  if (theLSE.IsEmpty()) {
    return !bRet;
  } 
  //
1162
  Standard_Real aT1, aT2, aTm, aTx, aTol, aDist;
1163
  Standard_Integer nE, iFlag, nV1, nV2;
1164
1165
1166
1167
1168
  gp_Pnt aPm;
  Bnd_Box aBoxPm;
  BOPCol_ListIteratorOfListOfInteger aItLI;
  //
  thePB->Range(aT1, aT2);
1169
1170
1171
1172
1173
1174
1175
1176
  thePB->Indices(nV1, nV2);
  const TopoDS_Vertex &aV1 = TopoDS::Vertex(myDS->Shape(nV1)),
                      &aV2 = TopoDS::Vertex(myDS->Shape(nV2));
  const Standard_Real aTolV1 = BRep_Tool::Tolerance(aV1),
                      aTolV2 = BRep_Tool::Tolerance(aV2);

  aTol = Max(aTolV1, aTolV2);

1177
1178
1179
  aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
  theNC.Curve().D0(aTm, aPm);
  aBoxPm.Add(aPm);
1180
  aBoxPm.Enlarge(aTol);
1181
1182
1183
1184
  //
  aItLI.Initialize(theLSE);
  for (; aItLI.More(); aItLI.Next()) {
    nE=aItLI.Value();
1185
1186
    if (nE < 0)
      continue;
1187
1188
1189
1190
    const BOPDS_ShapeInfo& aSIE=myDS->ChangeShapeInfo(nE);
    const Bnd_Box& aBoxE=aSIE.Box();
    if (!aBoxE.IsOut(aBoxPm)) {
      const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&aSIE.Shape()));
1191
1192
      const Standard_Real aTol1 = Max(BRep_Tool::Tolerance(aE), aTol);
      iFlag=myContext->ComputePE(aPm, aTol1, aE, aTx, aDist);
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
      if (!iFlag) {
        return bRet;
      }
    }
  }
  return !bRet;
}

//=======================================================================
//function : IsExistingPaveBlock
//purpose  : 
//=======================================================================
  Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
    (const Handle(BOPDS_PaveBlock)& thePB,
     const BOPDS_Curve& theNC,
     const Standard_Real theTolR3D,
emv's avatar
emv committed
1209
     const BOPDS_IndexedMapOfPaveBlock& theMPBOnIn,
1210
1211
     Handle(BOPDS_PaveBlock)& aPBOut,
     Standard_Real& theTolNew)
1212
1213
1214
{
  Standard_Boolean bRet;
  Standard_Real aT1, aT2, aTm, aTx;
emv's avatar
emv committed
1215
  Standard_Integer nSp, iFlag1, iFlag2, nV11, nV12, nV21, nV22, i, aNbPB;
1216
  gp_Pnt aP1, aPm, aP2;
1217
  Bnd_Box aBoxP1, aBoxPm, aBoxP2, aBoxTmp;
1218
  //
1219
  bRet=Standard_False;
1220
1221
1222
1223
  const IntTools_Curve& aIC=theNC.Curve();
  //
  thePB->Range(aT1, aT2);
  thePB->Indices(nV11, nV12);
1224
1225
1226
1227
  const Standard_Real aTolV11 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV11)));
  const Standard_Real aTolV12 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV12)));
  const Standard_Real aTolV1 = Max(aTolV11, aTolV12);

1228
1229
1230
  //first point
  aIC.D0(aT1, aP1);
  aBoxP1.Add(aP1);
1231
  aBoxP1.Enlarge(aTolV11);
1232
1233
1234
1235
1236
1237
1238
  //intermediate point
  aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
  aIC.D0(aTm, aPm);
  aBoxPm.Add(aPm);
  //last point
  aIC.D0(aT2, aP2);
  aBoxP2.Add(aP2);
1239
  aBoxP2.Enlarge(aTolV12);
1240
  //
1241
  theTolNew = 0.;
emv's avatar
emv committed
1242
1243
1244
  aNbPB = theMPBOnIn.Extent();
  for (i = 1; i <= aNbPB; ++i) {
    const Handle(BOPDS_PaveBlock)& aPB = theMPBOnIn(i);
1245
    aPB->Indices(nV21, nV22);
1246
1247
1248
    const Standard_Real aTolV21 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV21)));
    const Standard_Real aTolV22 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV22)));
    const Standard_Real aTolV2 = Max(aTolV21, aTolV22);
1249
    nSp=aPB->Edge();
1250
1251
    if (nSp < 0)
      continue;
1252
    const BOPDS_ShapeInfo& aSISp=myDS->ChangeShapeInfo(nSp);
1253
    const TopoDS_Edge& aSp=(*(TopoDS_Edge *)(&aSISp.Shape()));
1254
    const Bnd_Box& aBoxSp=aSISp.Box();
1255
    //
1256
1257
1258
1259
    iFlag1 = (nV11 == nV21 || nV11 == nV22) ? 2 : 
      (!aBoxSp.IsOut(aBoxP1) ? 1 : 0);
    iFlag2 = (nV12 == nV21 || nV12 == nV22) ? 2 : 
      (!aBoxSp.IsOut(aBoxP2) ? 1 : 0);
1260
    if (iFlag1 && iFlag2) {
1261
      Standard_Real aDist;
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272

      const Standard_Real aRealTol = myDS->IsCommonBlock(aPB) ?
                                     Max(aTolV1, aTolV2) : theTolR3D;

      aBoxTmp = aBoxPm;
      aBoxTmp.Enlarge(aRealTol);

      if (aBoxSp.IsOut(aBoxTmp) || myContext->ComputePE(aPm, 
                                                        aRealTol,
                                                        aSp, 
                                                        aTx, theTolNew)) {
1273
1274
1275
1276
        continue;
      }
      //
      if (iFlag1 == 1) {
1277
        iFlag1 = !myContext->ComputePE(aP1, aRealTol, aSp, aTx, aDist);
1278
1279
        if (theTolNew < aDist)
          theTolNew = aDist;
1280
1281
1282
      }
      //
      if (iFlag2 == 1) {
1283
        iFlag2 = !myContext->ComputePE(aP2, aRealTol, aSp, aTx, aDist);
1284
1285
        if (theTolNew < aDist)
          theTolNew = aDist;
1286
1287
1288
1289
1290
1291
      }
      //
      if (iFlag1 && iFlag2) {
        aPBOut = aPB;
        bRet=Standard_True;
        break;
1292
1293
1294
      }
    }
  }
1295
  return bRet;
1296
1297
1298
1299
1300
1301
1302
1303
1304
}
//=======================================================================
//function : PutBoundPaveOnCurve
//purpose  : 
//=======================================================================
  void BOPAlgo_PaveFiller::PutBoundPaveOnCurve(const TopoDS_Face& aF1,
                                               const TopoDS_Face& aF2,
                                               const Standard_Real aTolR3D,
                                               BOPDS_Curve& aNC,
1305
                                               BOPCol_ListOfInteger& aLVB)
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
{
  Standard_Boolean bVF;
  Standard_Integer nV, iFlag, nVn, j, aNbEP;
  Standard_Real aT[2], aTmin, aTmax, aTV, aTol, aTolVnew;
  gp_Pnt aP[2];
  TopoDS_Vertex aVn;
  BOPDS_ListIteratorOfListOfPave aItLP;
  BOPDS_Pave aPn, aPMM[2];
  //
  aTolVnew = Precision::Confusion();
  //
  const IntTools_Curve& aIC=aNC.Curve();
  aIC.Bounds(aT[0], aT[1], aP[0], aP[1]);
  //
  Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
  const BOPDS_ListOfPave& aLP=aPB->ExtPaves();
  //
  aNbEP=aLP.Extent();
  if (aNbEP) {
    aTmin=1.e10;
    aTmax=-aTmin;
    //
    aItLP.Initialize(aLP);
    for (; aItLP.More(); aItLP.Next()) {
      const BOPDS_Pave& aPv=aItLP.Value();
      aPv.Contents(nV, aTV);
      if (aTV<aTmin) {
        aPMM[0]=aPv;
        aTmin=aTV;
      }
      if (aTV>aTmax) {
        aPMM[1]=aPv;
        aTmax=aTV;
      }
    }
  }
  //
  for (j=0; j<2; ++j) {
    //if curve is closed, process only one bound
    if (j && aP[1].IsEqual(aP[0], aTolVnew)) {
      continue;
    }
    //
    iFlag=1;
    //
    if (aNbEP) {
      Bnd_Box aBoxP;
      //
      aBoxP.Set(aP[j]);
      aTol = aTolR3D+Precision::Confusion();
      aBoxP.Enlarge(aTol);
      const BOPDS_Pave& aPV=aPMM[j];
      nV=aPV.Index();
      const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
      const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&aSIV.Shape()));
      const Bnd_Box& aBoxV=aSIV.Box();
      if (!aBoxP.IsOut(aBoxV)){
        iFlag=BOPTools_AlgoTools::ComputeVV(aV, aP[j], aTol);
      }
    }
    if (iFlag) {
      // 900/L5
      bVF=myContext->IsValidPointForFaces (aP[j], aF1, aF2, aTolR3D);
      if (!bVF) {
        continue;
      }
      //
      BOPDS_ShapeInfo aSIVn;
      //
      BOPTools_AlgoTools::MakeNewVertex(aP[j], aTolR3D, aVn);
      aSIVn.SetShapeType(TopAbs_VERTEX);
      aSIVn.SetShape(aVn);
      //
      nVn=myDS->Append(aSIVn);
      //
      aPn.SetIndex(nVn);
      aPn.SetParameter(aT[j]);
      aPB->AppendExtPave(aPn);
      //
      aVn=(*(TopoDS_Vertex *)(&myDS->Shape(nVn)));
      BOPTools_AlgoTools::UpdateVertex (aIC, aT[j], aVn);
      //
      aTolVnew = BRep_Tool::Tolerance(aVn);
      //
      BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nVn);
      Bnd_Box& aBoxDS=aSIDS.ChangeBox();
      BRepBndLib::Add(aVn, aBoxDS);
1393
      aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
1394
1395
      //
      aLVB.Append(nVn);
1396
1397
1398
1399
1400
    }
  }
}

//=======================================================================
1401
//function : PutPavesOnCurve
1402
1403
//purpose  : 
//=======================================================================
1404
1405
1406
1407
1408
1409
1410
1411
1412
  void BOPAlgo_PaveFiller::PutPavesOnCurve
  (const BOPCol_MapOfInteger& aMVOnIn,
   const Standard_Real aTolR3D,
   BOPDS_Curve& aNC,
   const Standard_Integer nF1,
   const Standard_Integer nF2,
   const BOPCol_MapOfInteger& aMI,
   const BOPCol_MapOfInteger& aMVEF,
   BOPCol_DataMapOfIntegerReal& aMVTol)
1413
{
1414
  Standard_Boolean bInBothFaces;
1415
1416
1417
1418
1419
  Standard_Integer nV;
  BOPCol_MapIteratorOfMapOfInteger aIt;
  //
  const Bnd_Box& aBoxC=aNC.Box();
  //
1420
1421
1422
1423
1424
1425
1426
  //Put EF vertices first
  aIt.Initialize(aMVEF);
  for (; aIt.More(); aIt.Next()) {
    nV=aIt.Value();
    PutPaveOnCurve(nV, aTolR3D, aNC, aMI, aMVTol, 2);
  }
  //Put all other vertices
1427
1428
1429
  aIt.Initialize(aMVOnIn);
  for (; aIt.More(); aIt.Next()) {
    nV=aIt.Value();
1430
1431
    if (aMVEF.Contains(nV)) {
      continue;
1432
1433
    }
    //
1434
1435
    const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
    const Bnd_Box& aBoxV=aSIV.Box();
1436
    //
1437
1438
1439
1440
1441
1442
    if (aBoxC.IsOut(aBoxV)){
      continue;
    }
    if (!myDS->IsNewShape(nV)) {
      const BOPDS_FaceInfo& aFI1 = myDS->FaceInfo(nF1);
      const BOPDS_FaceInfo& aFI2 = myDS->FaceInfo(nF2);
1443
      //
1444
1445
1446
1447
1448
1449
      bInBothFaces = (aFI1.VerticesOn().Contains(nV) ||
                      aFI1.VerticesIn().Contains(nV))&&
                     (aFI2.VerticesOn().Contains(nV) ||
                      aFI2.VerticesIn().Contains(nV));
      if (!bInBothFaces) {
        continue;
1450
      }
1451
    }
1452
1453
    //
    PutPaveOnCurve(nV, aTolR3D, aNC, aMI, aMVTol, 1);
1454
1455
1456
1457
1458
1459
1460
  }
}

//=======================================================================
//function : ExtendedTolerance
//purpose  : 
//=======================================================================
1461
1462
1463
1464
1465
Standard_Boolean BOPAlgo_PaveFiller::ExtendedTolerance
  (const Standard_Integer nV,
   const BOPCol_MapOfInteger& aMI,
   Standard_Real& aTolVExt,
   const Standard_Integer aType)
1466
1467
1468
1469
1470
{
  Standard_Boolean bFound = Standard_False;
  if (!(myDS->IsNewShape(nV))) {
    return bFound;
  }
1471
1472
  //
  Standard_Integer i, k, aNbLines, aNbInt;
1473
1474
1475
  Standard_Real aT11, aT12, aD1, aD2, aD;
  TopoDS_Vertex aV;
  gp_Pnt aPV, aP11, aP12;
1476
1477
1478
1479
1480
1481
1482
1483
1484
  //
  k = 0;
  aNbInt = 2;
  if (aType == 1) {
    aNbInt = 1;
  } else if (aType == 2) {
    k = 1;
  }
  //
1485
1486
1487
1488
1489
  aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
  aPV=BRep_Tool::Pnt(aV);
  //
  BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
  BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1490
1491
  //
  for (; k<aNbInt; ++k) {
1492
1493
1494
    aNbLines = !k ? aEEs.Extent() : aEFs.Extent();
    for (i = 0; i < aNbLines; ++i) {
      BOPDS_Interf *aInt = !k ? (BOPDS_Interf*) (&aEEs(i)) :
1495
        (BOPDS_Interf*) (&aEFs(i));
1496
      if (aInt->IndexNew() == nV) {
1497
1498
        if (aMI.Contains(aInt->Index1()) && 
            aMI.Contains(aInt->Index2())) {
1499
          const IntTools_CommonPrt& aComPrt = !k ? aEEs(i).CommonPart() :
1500
            aEFs(i).CommonPart();
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
          //
          const TopoDS_Edge& aE1=aComPrt.Edge1();
          aComPrt.Range1(aT11, aT12);
          BOPTools_AlgoTools::PointOnEdge(aE1, aT11, aP11);
          BOPTools_AlgoTools::PointOnEdge(aE1, aT12, aP12);
          aD1=aPV.Distance(aP11);
          aD2=aPV.Distance(aP12);
          aD=(aD1>aD2)? aD1 : aD2;
          if (aD>aTolVExt) {
            aTolVExt=aD;
          }
          return !bFound;
        }//if (aMI.Contains(aEF.Index1()) && aMI.Contains(aEF.Index2())) {
      }//if (aInt->IndexNew() == nV) {
    }//for (i = 0; i < aNbLines; ++i) {
  }//for (k=0; k<2; ++k) {
  return bFound;
}

//=======================================================================
//function : GetEFPnts
//purpose  : 
//=======================================================================
1524
1525
1526
1527
void BOPAlgo_PaveFiller::GetEFPnts
  (const Standard_Integer nF1,
   const Standard_Integer nF2,
   IntSurf_ListOfPntOn2S& aListOfPnts)
1528
1529
1530
{
  Standard_Integer nE, nF, nFOpposite, aNbEFs, i;
  Standard_Real U1, U2, V1, V2, f, l;
1531
  BOPCol_MapOfInteger aMI;
1532
1533
  //
  //collect indexes of all shapes from nF1 and nF2.
1534
1535
  GetFullShapeMap(nF1, aMI);
  GetFullShapeMap(nF2, aMI);
1536
1537
1538
1539
1540
1541
  //
  BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
  aNbEFs = aEFs.Extent();
  //
  for(i = 0; i < aNbEFs; ++i) {
    const BOPDS_InterfEF& aEF = aEFs(i);
1542
1543
1544
1545
    if (aEF.HasIndexNew()) {
      aEF.Indices(nE, nFOpposite);
      if(aMI.Contains(nE) && aMI.Contains(nFOpposite)) {
        const IntTools_CommonPrt& aCP = aEF.CommonPart();
1546
1547
        Standard_Real aPar = aCP.VertexParameter1();
        const TopoDS_Edge& aE = (*(TopoDS_Edge*)(&myDS->Shape(nE)));
1548
1549
        const TopoDS_Face& aFOpposite = 
          (*(TopoDS_Face*)(&myDS->Shape(nFOpposite)));
1550
1551
1552
1553
1554
        //
        const Handle(Geom_Curve)& aCurve = BRep_Tool::Curve(aE, f, l);
        //
        nF = (nFOpposite == nF1) ? nF2 : nF1;
        const TopoDS_Face& aF = (*(TopoDS_Face*)(&myDS->Shape(nF)));
1555
1556
        Handle(Geom2d_Curve) aPCurve = 
          BRep_Tool::CurveOnSurface(aE, aF, f, l);
1557
        //
1558
        GeomAPI_ProjectPointOnSurf& aProj=myContext->ProjPS(aFOpposite);
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
        //
        gp_Pnt aPoint;
        aCurve->D0(aPar, aPoint);
        IntSurf_PntOn2S aPnt;
        if(!aPCurve.IsNull()) {
          gp_Pnt2d aP2d = aPCurve->Value(aPar);
          aProj.Perform(aPoint);
          if(aProj.IsDone()) {
            aProj.LowerDistanceParameters(U1,V1);
            if (nF == nF1) {
              aPnt.SetValue(aP2d.X(),aP2d.Y(),U1,V1);
            } else {
              aPnt.SetValue(U1,V1,aP2d.X(),aP2d.Y());
            }
            aListOfPnts.Append(aPnt);
          }
        }
        else {
          GeomAPI_ProjectPointOnSurf& aProj1 = myContext->ProjPS(aF);
          aProj1.Perform(aPoint);
          aProj.Perform(aPoint);
          if(aProj1.IsDone() && aProj.IsDone()){
            aProj1.LowerDistanceParameters(U1,V1);
            aProj.LowerDistanceParameters(U2,V2);
            if (nF == nF1) {
              aPnt.SetValue(U1,V1,U2,V2);
            } else {
              aPnt.SetValue(U2,V2,U1,V1);
            }
            aListOfPnts.Append(aPnt);
          }
        }
      }
    }
  }
}

//=======================================================================
//function : ProcessUnUsedVertices
//purpose  : 
//=======================================================================
1600
1601
1602
1603
1604
  void BOPAlgo_PaveFiller::PutEFPavesOnCurve
  (BOPDS_Curve& aNC,
   const BOPCol_MapOfInteger& aMI,
   const BOPCol_MapOfInteger& aMVEF,
   BOPCol_DataMapOfIntegerReal& aMVTol)
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
{
  if (!aMVEF.Extent()) {
    return;
  }
  //
  const IntTools_Curve& aIC=aNC.Curve();
  GeomAbs_CurveType aTypeC;
  aTypeC=aIC.Type();
  if (!(aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve)) {
    return;
  }
  //
  Standard_Integer nV;
  BOPCol_MapOfInteger aMV;
  //
  aMV.Assign(aMVEF);
  RemoveUsedVertices(aNC, aMV);
  if (!aMV.Extent()) {
    return;
  }
  //
  Standard_Real aDist;
  BOPDS_Pave aPave;
  //
  const Handle(Geom_Curve)& aC3D=aIC.Curve();
  GeomAPI_ProjectPointOnCurve& aProjPT = myContext->ProjPT(aC3D);
1631
  //
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
  BOPCol_MapIteratorOfMapOfInteger aItMI;
  aItMI.Initialize(aMV);
  for (; aItMI.More(); aItMI.Next()) {
    nV = aItMI.Value();
    const TopoDS_Vertex& aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
    gp_Pnt aPV = BRep_Tool::Pnt(aV);
    aProjPT.Perform(aPV);
    Standard_Integer aNbPoints = aProjPT.NbPoints();
    if (aNbPoints) {
      aDist = aProjPT.LowerDistance();
1642
      PutPaveOnCurve(nV, aDist, aNC, aMI, aMVTol);
1643
1644
1645
1646
1647
1648
1649
1650
    }
  }
}

//=======================================================================
//function : ProcessUnUsedVertices
//purpose  : 
//=======================================================================
1651
1652
1653
1654
1655
1656
1657
  void BOPAlgo_PaveFiller::PutStickPavesOnCurve
  (const TopoDS_Face& aF1,
   const TopoDS_Face& aF2,
   const BOPCol_MapOfInteger& aMI,
   BOPDS_Curve& aNC,
   const BOPCol_MapOfInteger& aMVStick,
   BOPCol_DataMapOfIntegerReal& aMVTol)
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
{
  BOPCol_MapOfInteger aMV;
  aMV.Assign(aMVStick);
  RemoveUsedVertices(aNC, aMV);
  //
  if (!aMV.Extent()) {
    return;
  }
  //
  Handle(Geom_Surface) aS1=BRep_Tool::Surface(aF1);
  Handle(Geom_Surface) aS2=BRep_Tool::Surface(aF2);
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
  //
  const IntTools_Curve& aIC=aNC.Curve();
  //if (aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve) {
  Handle(Geom2d_Curve) aC2D[2];
  //
  aC2D[0]=aIC.FirstCurve2d();
  aC2D[1]=aIC.SecondCurve2d();
  if (!aC2D[0].IsNull() && !aC2D[1].IsNull()) {
    Standard_Integer nV, m, n;
    Standard_Real aTC[2], aD, aD2, u, v, aDT2, aScPr, aDScPr;
    gp_Pnt aPC[2], aPV;
    gp_Dir aDN[2];
    gp_Pnt2d aP2D;
    BOPCol_MapIteratorOfMapOfInteger aItMI, aItMI1;
    //
    aDT2=2e-7;     // the rich criteria
    aDScPr=5.e-9;  // the creasing criteria
    aIC.Bounds(aTC[0], aTC[1], aPC[0], aPC[1]);
1687
    //
1688
1689
1690
1691
1692
    aItMI.Initialize(aMV);
    for (; aItMI.More(); aItMI.Next()) {
      nV = aItMI.Value();
      const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&myDS->Shape(nV));
      aPV=BRep_Tool::Pnt(aV);
1693
      //
1694
1695
1696
1697
1698
      for (m=0; m<2; ++m) {
        aD2=aPC[m].SquareDistance(aPV);
        if (aD2>aDT2) {// no rich
          continue; 
        }
1699
        //
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
        for (n=0; n<2; ++n) {
          Handle(Geom_Surface)& aS=(!n)? aS1 : aS2;
          aC2D[n]->D0(aTC[m], aP2D);
          aP2D.Coord(u, v);
          BOPTools_AlgoTools3D::GetNormalToSurface(aS, u, v, aDN[n]);
        }
        // 
        aScPr=aDN[0]*aDN[1];
        if (aScPr<0.) {
          aScPr=-aScPr;
        }
        aScPr=1.-aScPr;
1712
        //
1713
1714
1715
1716
1717
1718
1719
1720
        if (aScPr>aDScPr) {
          continue;
        }
        //
        // The intersection curve aIC is vanishing curve (the crease)
        aD=sqrt(aD2);
        //
        PutPaveOnCurve(nV, aD, aNC, aMI, aMVTol);
1721
      }
1722
1723
1724
1725
    }//for (jVU=1; jVU=aNbVU; ++jVU) {
  }
  //}//if (aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve) {
  //}//if(aType1==GeomAbs_Torus  || aType2==GeomAbs_Torus) {
1726
1727
1728
1729
1730
1731
}

//=======================================================================
//function : GetStickVertices
//purpose  : 
//=======================================================================
1732
1733
1734
1735
1736
void BOPAlgo_PaveFiller::GetStickVertices(const Standard_Integer nF1,
                                          const Standard_Integer nF2,
                                          BOPCol_MapOfInteger& aMVStick,
                                          BOPCol_MapOfInteger& aMVEF,
                                          BOPCol_MapOfInteger& aMI)
1737
{
1738
  Standard_Integer nS1, nS2, nVNew, aTypeInt, i;
1739
1740
1741
1742
1743
1744
1745
  //
  BOPDS_VectorOfInterfVV& aVVs=myDS->InterfVV();
  BOPDS_VectorOfInterfVE& aVEs=myDS->InterfVE();
  BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
  BOPDS_VectorOfInterfVF& aVFs=myDS->InterfVF();
  BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
  //
1746
1747
1748
1749
  Standard_Integer aNbLines[5] = {
    aVVs.Extent(), aVEs.Extent(), aEEs.Extent(),
    aVFs.Extent(), aEFs.Extent()
    };
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
  //collect indices of all shapes from nF1 and nF2.
  aMI.Clear();
  GetFullShapeMap(nF1, aMI);
  GetFullShapeMap(nF2, aMI);
  //
  //collect VV, VE, EE, VF interferences
  for (aTypeInt = 0; aTypeInt < 4; ++aTypeInt) {
    for (i = 0; i < aNbLines[aTypeInt]; ++i) {
      BOPDS_Interf* aInt = (aTypeInt==0) ? (BOPDS_Interf*)(&aVVs(i)) : 
        ((aTypeInt==1) ? (BOPDS_Interf*)(&aVEs(i)) :
1760
1761
         ((aTypeInt==2) ? (BOPDS_Interf*)(&aEEs(i)) : 
          (BOPDS_Interf*)(&aVFs(i))));
1762
1763
1764
1765
1766
1767
      if (aInt->HasIndexNew()) {
        aInt->Indices(nS1, nS2);
        if(aMI.Contains(nS1) && aMI.Contains(nS2)) {
          nVNew = aInt->IndexNew();
          aMVStick.Add(nVNew);
        }
1768
1769
1770
1771
      }
    }
  }
  //collect EF interferences
1772
1773
1774
1775
1776
1777
  for (i = 0; i < aNbLines[4]; ++i) {
    const BOPDS_InterfEF& aInt = aEFs(i);
    if (aInt.HasIndexNew()) {
      aInt.Indices(nS1, nS2);
      if(aMI.Contains(nS1) && aMI.Contains(nS2)) {
        nVNew = aInt.IndexNew();
1778
1779
1780
1781
1782
1783
1784
1785
        aMVStick.Add(nVNew);
        aMVEF.Add(nVNew);
      }
    }
  }
}

//=======================================================================
1786
// function: GetFullShapeMap
1787
1788
// purpose: 
//=======================================================================
1789
1790
void BOPAlgo_PaveFiller::GetFullShapeMap(const Standard_Integer nF,
                                         BOPCol_MapOfInteger& aMI)
1791
{
1792
1793
1794
1795
1796
1797
  BOPCol_ListIteratorOfListOfInteger aIt;
  Standard_Integer nS;
  //
  const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(nF);
  const BOPCol_ListOfInteger& aLI = aSI.SubShapes();
  //
1798
  aMI.Add(nF);
1799
1800
1801
1802
  aIt.Initialize(aLI);
  for (; aIt.More(); aIt.Next()) {
    nS = aIt.Value();
    aMI.Add(nS);
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
  }
}

//=======================================================================
// function: RemoveUsedVertices
// purpose: 
//=======================================================================
void BOPAlgo_PaveFiller::RemoveUsedVertices(BOPDS_Curve& aNC,
                                            BOPCol_MapOfInteger& aMV)
{
  if (!aMV.Extent()) {
    return;
  }
  Standard_Integer nV;
  BOPDS_Pave aPave;
  BOPDS_ListIteratorOfListOfPave aItLP;
  //
  Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
  const BOPDS_ListOfPave& aLP = aPB->ExtPaves();
  aItLP.Initialize(aLP);
  for (;aItLP.More();aItLP.Next()) {
    aPave = aItLP.Value();
    nV = aPave.Index();
    aMV.Remove(nV);
  }
}

//=======================================================================
//function : PutPaveOnCurve
//purpose  : 
//=======================================================================
1834
1835
1836
1837
1838
1839
1840
  void BOPAlgo_PaveFiller::PutPaveOnCurve
  (const Standard_Integer nV,
   const Standard_Real aTolR3D,
   BOPDS_Curve& aNC,
   const BOPCol_MapOfInteger& aMI,
   BOPCol_DataMapOfIntegerReal& aMVTol,
   const Standard_Integer iCheckExtend)
1841
1842
{
  Standard_Boolean bIsVertexOnLine;
1843
  Standard_Real aT, aTolV;
1844
  //
1845
  const TopoDS_Vertex& aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
1846
  Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
1847
1848
1849
  const IntTools_Curve& aIC = aNC.Curve();
  //
  bIsVertexOnLine=myContext->IsVertexOnLine(aV, aIC, aTolR3D, aT);
1850
  if (!bIsVertexOnLine && iCheckExtend) {
1851
    aTolV = BRep_Tool::Tolerance(aV);
1852
    //
1853
1854
    ExtendedTolerance(nV, aMI, aTolV, iCheckExtend);
    bIsVertexOnLine=myContext->IsVertexOnLine(aV, aTolV, aIC, aTolR3D, aT);
1855
1856
  }
  //
1857
  if (bIsVertexOnLine) {
1858
1859
1860
1861
1862
1863
    // check if aPB contains the parameter aT
    Standard_Boolean bExist;
    Standard_Integer nVToUpdate;
    Standard_Real aPTol, aDist, aTolVNew, aTolV2, aDTol;
    TopoDS_Vertex aVToUpdate;
    gp_Pnt aP1, aP2;
1864
    //
1865
1866
    aTolV2 = 0.;
    aDTol = 1.e-12;
1867
    //
1868
1869
    GeomAdaptor_Curve aGAC(aIC.Curve());
    aPTol = aGAC.Resolution(aTolR3D);
1870
    //
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
    bExist = aPB->ContainsParameter(aT, aPTol, nVToUpdate);
    if (bExist) {
      // use existing pave
      aP1 = BRep_Tool::Pnt(aV);
      aTolV2 = BRep_Tool::Tolerance(aV);
      aVToUpdate = (*(TopoDS_Vertex *)(&myDS->Shape(nVToUpdate)));
    }
    else {
      // add new pave
      BOPDS_Pave aPave;
      aPave.SetIndex(nV);
      aPave.SetParameter(aT);
      aPB->AppendExtPave(aPave);
      //
      aP1 = aGAC.Value(aT);
      nVToUpdate = nV;
      aVToUpdate = aV;
    }
1889
    //
1890
1891
1892
    aTolV = BRep_Tool::Tolerance(aVToUpdate);
    aP2 = BRep_Tool::Pnt(aVToUpdate);
    aDist = aP1.Distance(aP2);
1893
    aTolVNew = Max(aDist - aTolV2, aTolR3D);
1894
1895
1896
1897
1898
1899
1900
    //
    if (aTolVNew > aTolV) {
      BRep_Builder aBB;
      aBB.UpdateVertex(aVToUpdate, aTolVNew+aDTol);
      //
      if (!aMVTol.IsBound(nVToUpdate)) {
        aMVTol.Bind(nVToUpdate, aTolV);
1901
      }
1902
1903
1904
1905
      // 
      BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nVToUpdate);
      Bnd_Box& aBoxDS=aSIDS.ChangeBox();
      BRepBndLib::Add(aVToUpdate, aBoxDS);
1906
      aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
1907
    }
1908
1909
1910
1911
1912
1913
1914
  }
}

//=======================================================================
//function : ProcessOldPaveBlocks
//purpose  : 
//=======================================================================
1915
void BOPAlgo_PaveFiller::ProcessExistingPaveBlocks
1916
    (const Standard_Integer theInt,
emv's avatar
emv committed
1917
     const BOPDS_IndexedMapOfPaveBlock& aMPBOnIn,
1918
     const BOPCol_DataMapOfIntegerListOfInteger& aDMBV,
1919
1920
1921
1922
     BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
     BOPCol_DataMapOfShapeInteger& aMVI,
     BOPDS_MapOfPaveBlock& aMPB)
{
1923
1924
1925
1926
  if (aDMBV.IsEmpty()) {
    return;
  }
  //
1927
  Standard_Real aT, dummy;
1928
1929
1930
  Standard_Integer i, nV, nE, iC, aNbPB, iFlag;
  BOPCol_ListIteratorOfListOfInteger aItLI;
  BOPCol_DataMapIteratorOfDataMapOfIntegerListOfInteger aItBV;
1931
  //
1932
  BOPDS_VectorOfInterfFF& aFFs = myDS->InterfFF();
1933
  BOPDS_InterfFF& aFF = aFFs(theInt);
1934
1935
1936
1937
1938
1939
1940
1941
  BOPDS_VectorOfCurve& aVC = aFF.ChangeCurves();
  //
  aNbPB = aMPBOnIn.Extent();
  //
  aItBV.Initialize(aDMBV);
  for (; aItBV.More(); aItBV.Next()) {
    iC = aItBV.Key();
    const BOPCol_ListOfInteger& aLBV = aItBV.Value();
1942
    //
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
    BOPDS_Curve& aNC = aVC.ChangeValue(iC);
    BOPDS_ListOfPaveBlock& aLPBC = aNC.ChangePaveBlocks();
    //
    aItLI.Initialize(aLBV);
    for (; aItLI.More(); aItLI.Next()) {
      nV = aItLI.Value();
      const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
      const Bnd_Box& aBoxV=aSIV.Box();
      const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&aSIV.Shape();
      if (!aMVI.IsBound(aV)) {
1953
1954
1955
        continue;
      }
      //
1956
1957
1958
1959
1960
1961
1962
1963
1964
      for (i = 1; i <= aNbPB; ++i) {
        const Handle(BOPDS_PaveBlock)& aPB = aMPBOnIn(i);
        if (aPB->Pave1().Index() == nV || aPB->Pave2().Index() == nV) {
          continue;
        }
        //
        if (aMPB.Contains(aPB)) {
          continue;
        }
1965
1966
1967
        if (myDS->ShapeInfo(aPB->OriginalEdge()).HasFlag()) { // skip degenerated edges
          continue;
        }
1968
1969
1970
1971
1972
1973
1974
1975
1976
        //
        nE = aPB->Edge();
        const BOPDS_ShapeInfo& aSIE = myDS->ShapeInfo(nE);
        const Bnd_Box& aBoxE = aSIE.Box();
        //
        if (aBoxV.IsOut(aBoxE)) {
          continue;
        }
        //
1977
1978
        const TopoDS_Edge& aE = *(TopoDS_Edge*)&aSIE.Shape();
        //
1979
        iFlag = myContext->ComputeVE(aV, aE, aT, dummy);
1980
1981
        if (!iFlag) {
          aMPB.Add(aPB);
1982
          PreparePostTreatFF(theInt, iC, aPB, aMSCPB, aMVI, aLPBC);
1983
1984
1985
1986
1987
1988
1989
1990
1991
        }
      }
    }
  }
}
//=======================================================================
//function : UpdateExistingPaveBlocks
//purpose  : 
//=======================================================================
1992
1993
1994
1995
1996
void BOPAlgo_PaveFiller::UpdateExistingPaveBlocks
  (const Handle(BOPDS_PaveBlock)& aPBf,
   BOPDS_ListOfPaveBlock& aLPB,
   const Standard_Integer nF1,
   const Standard_Integer nF2) 
1997
{
1998
1999
2000
2001
  if (!aLPB.Extent()) {
    return;
  }
  //
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
  Standard_Integer nE;
  Standard_Boolean bCB;
  Handle(BOPDS_PaveBlock) aPB, aPB1, aPB2, aPB2n;
  Handle(BOPDS_CommonBlock) aCB;
  BOPDS_ListIteratorOfListOfPaveBlock aIt, aIt1, aIt2;
  //
  BOPDS_FaceInfo& aFI1 = myDS->ChangeFaceInfo(nF1);
  BOPDS_FaceInfo& aFI2 = myDS->ChangeFaceInfo(nF2);
  //
  BOPDS_IndexedMapOfPaveBlock& aMPBOn1 = aFI1.ChangePaveBlocksOn();
  BOPDS_IndexedMapOfPaveBlock& aMPBIn1 = aFI1.ChangePaveBlocksIn();
  BOPDS_IndexedMapOfPaveBlock& aMPBOn2 = aFI2.ChangePaveBlocksOn();
  BOPDS_IndexedMapOfPaveBlock& aMPBIn2 = aFI2.ChangePaveBlocksIn();
  //
2016
  // 1. Remove old pave blocks
omy's avatar
omy committed
2017
  const Handle(BOPDS_CommonBlock)& aCB1 = myDS->CommonBlock(aPBf);
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
  bCB = !aCB1.IsNull();
  BOPDS_ListOfPaveBlock aLPB1;
  //
  if (bCB) {
    aLPB1.Assign(aCB1->PaveBlocks());
  } else {
    aLPB1.Append(aPBf);
  }
  aIt1.Initialize(aLPB1);
  for (; aIt1.More(); aIt1.Next()) {
    aPB1 = aIt1.Value();
    nE = aPB1->OriginalEdge();
    //
    BOPDS_ListOfPaveBlock& aLPB2 = myDS->ChangePaveBlocks(nE);
    aIt2.Initialize(aLPB2);
    for (; aIt2.More(); aIt2.Next()) {
      aPB2 = aIt2.Value();
      if (aPB1 == aPB2) {
        aLPB2.Remove(aIt2);
        break;
      }
    }
  }
  //
2042
  // 2. Update pave blocks
2043
  if (bCB) {
2044
    //create new common blocks
2045
    BOPDS_ListOfPaveBlock aLPBNew;
2046
2047
2048
    const BOPCol_ListOfInteger& aFaces = aCB1->Faces();
    aIt.Initialize(aLPB);
    for (; aIt.More(); aIt.Next()) {
2049
      const Handle(BOPDS_PaveBlock)& aPBValue = aIt.Value();
2050
2051
2052
2053
2054
2055
2056
2057
      //
      aCB = new BOPDS_CommonBlock;
      aIt1.Initialize(aLPB1);
      for (; aIt1.More(); aIt1.Next()) {
        aPB2 = aIt1.Value();
        nE = aPB2->OriginalEdge();
        //
        aPB2n = new BOPDS_PaveBlock;
2058
2059
2060
        aPB2n->SetPave1(aPBValue->Pave1());
        aPB2n->SetPave2(aPBValue->Pave2());
        aPB2n->SetEdge(aPBValue->Edge());
2061
2062
        aPB2n->SetOriginalEdge(nE);
        aCB->AddPaveBlock(aPB2n);
omy's avatar
omy committed
2063
        myDS->SetCommonBlock(aPB2n, aCB);
2064
2065
        myDS->ChangePaveBlocks(nE).Append(aPB2n);
      }
2066
      aCB->SetFaces(aFaces);
2067
2068
      myDS->SortPaveBlocks(aCB);
      //
2069
2070
      const Handle(BOPDS_PaveBlock)& aPBNew = aCB->PaveBlocks().First();
      aLPBNew.Append(aPBNew);
2071
    }
2072
2073
    //
    aLPB = aLPBNew;
2074
  } 
2075
2076
2077
2078
2079
2080
2081
  else {
    nE = aPBf->OriginalEdge();
    BOPDS_ListOfPaveBlock& aLPBE = myDS->ChangePaveBlocks(nE);
    aIt.Initialize(aLPB);
    for (; aIt.More(); aIt.Next()) {
      aPB = aIt.Value();
      aLPBE.Append(aPB);
2082
2083
2084
2085
2086
2087
2088
2089
    }
  }
  //
  Standard_Boolean bIn1, bIn2;
  //
  bIn1 = aMPBOn1.Contains(aPBf) || aMPBIn1.Contains(aPBf);
  bIn2 = aMPBOn2.Contains(aPBf) || aMPBIn2.Contains(aPBf);
  //
2090
2091
2092
2093
  if (bIn1 && bIn2) {
    return;
  }
  //
2094
  // 3. Check new pave blocks for coincidence 
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
  //    with the opposite face.
  //    In case of coincidence create common blocks
  Standard_Integer nF;
  Standard_Real aTolE, aTolF;
  //
  nF = bIn1 ? nF2 : nF1;
  const TopoDS_Face& aF = *(TopoDS_Face*)&myDS->Shape(nF);
  BOPDS_IndexedMapOfPaveBlock& aMPBIn = bIn1 ? aMPBIn2 : aMPBIn1;
  aTolF = BRep_Tool::Tolerance(aF);
  //
  aIt.Initialize(aLPB);
  for (; aIt.More(); aIt.Next()) {
2107
2108
    Handle(BOPDS_PaveBlock)& aPBChangeValue = aIt.ChangeValue();
    const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPBChangeValue->Edge());
2109
2110
2111
2112
2113
2114
2115
    aTolE = BRep_Tool::Tolerance(aE);
    //
    IntTools_EdgeFace anEF;
    anEF.SetEdge(aE);
    anEF.SetFace(aF);
    anEF.SetTolE(aTolE);
    anEF.SetTolF(aTolF);
2116
    anEF.SetRange(aPBChangeValue->Pave1().Parameter(), aPBChangeValue->Pave2().Parameter());
2117
2118
    anEF.SetContext(myContext);
    anEF.Perform();
2119
    //
2120
2121
2122
2123
2124
    const IntTools_SequenceOfCommonPrts& aCPrts=anEF.CommonParts();
    if (aCPrts.Length() == 1) {
      Standard_Boolean bCoinc = (aCPrts(1).Type() == TopAbs_EDGE);
      if (bCoinc) {
        if (bCB) {
2125
          aCB = myDS->CommonBlock(aPBChangeValue);
2126
2127
        } else {
          aCB = new BOPDS_CommonBlock;
2128
2129
          aCB->AddPaveBlock(aPBChangeValue);
          myDS->SetCommonBlock(aPBChangeValue, aCB);
2130
2131
2132
        }
        aCB->AddFace(nF);
        //
2133
        aMPBIn.Add(aPBChangeValue);
2134
      }
2135
2136
2137
2138
2139
2140
2141
    }
  }
}
//=======================================================================
// function: PutClosingPaveOnCurve
// purpose:
//=======================================================================
2142
void BOPAlgo_PaveFiller::PutClosingPaveOnCurve(BOPDS_Curve& aNC)
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
{
  Standard_Boolean bIsClosed, bHasBounds, bAdded;
  Standard_Integer nVC, j;
  Standard_Real aT[2], aTC, dT, aTx;
  gp_Pnt aP[2] ; 
  BOPDS_Pave aPVx;
  BOPDS_ListIteratorOfListOfPave aItLP;
  //
  const IntTools_Curve& aIC=aNC.Curve();
  const Handle(Geom_Curve)& aC3D=aIC.Curve();
  if(aC3D.IsNull()) {
    return;
  }
  //
  bIsClosed=IntTools_Tools::IsClosed(aC3D);
  if (!bIsClosed) {
    return;
  }
  //
  bHasBounds=aIC.HasBounds ();
  if (!bHasBounds){
    return;
  }
  // 
  bAdded=Standard_False;
  dT=Precision::PConfusion();
  aIC.Bounds (aT[0], aT[1], aP[0], aP[1]);
  //
  Handle(BOPDS_PaveBlock)& aPB=aNC.ChangePaveBlock1();
  BOPDS_ListOfPave& aLP=aPB->ChangeExtPaves();
  //
  aItLP.Initialize(aLP);
  for (; aItLP.More() && !bAdded; aItLP.Next()) {
    const BOPDS_Pave& aPC=aItLP.Value();
    nVC=aPC.Index();
    aTC=aPC.Parameter();
    //
    for (j=0; j<2; ++j) {
      if (fabs(aTC-aT[j]) < dT) {
        aTx=(!j) ? aT[1] : aT[0];
        aPVx.SetIndex(nVC);
        aPVx.SetParameter(aTx);
        aLP.Append(aPVx);
        //
        bAdded=Standard_True;
        break;
      }
    }
  }
}
//=======================================================================
//function : PreparePostTreatFF
//purpose  : 
//=======================================================================
2197
void BOPAlgo_PaveFiller::PreparePostTreatFF
2198
    (const Standard_Integer aInt,
2199
     const Standard_Integer aCur,
2200
2201
2202
     const Handle(BOPDS_PaveBlock)& aPB,
     BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
     BOPCol_DataMapOfShapeInteger& aMVI,
2203
     BOPDS_ListOfPaveBlock& aLPBC)
2204
{
2205
  Standard_Integer nV1, nV2;
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
  //
  aLPBC.Append(aPB);
  //
  aPB->Indices(nV1, nV2);
  const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(nV1)));
  const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(nV2)));
  const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPB->Edge());
  // Keep info for post treatment 
  BOPDS_CoupleOfPaveBlocks aCPB;
  aCPB.SetIndexInterf(aInt);
2216
  aCPB.SetIndex(aCur);
2217
2218
2219
2220
2221
2222
2223
  aCPB.SetPaveBlock1(aPB);
  //
  aMSCPB.Add(aE, aCPB);
  aMVI.Bind(aV1, nV1);
  aMVI.Bind(aV2, nV2);
}

2224
2225
2226
2227
//=======================================================================
//function : CheckPlanes
//purpose  : 
//=======================================================================
2228
2229
2230
Standard_Boolean BOPAlgo_PaveFiller::CheckPlanes
  (const Standard_Integer nF1,
   const Standard_Integer nF2)const
2231
2232
{
  Standard_Boolean bToIntersect;
2233
  Standard_Integer i, nV2, iCnt;
2234
2235
  BOPCol_MapIteratorOfMapOfInteger aIt;
  //
2236
  bToIntersect=Standard_False;
2237
  //
2238
2239
  const BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
  const BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
2240
  //
2241
2242
  const BOPCol_MapOfInteger& aMVIn1=aFI1.VerticesIn();
  const BOPCol_MapOfInteger& aMVOn1=aFI1.VerticesOn();
2243
  //
2244
2245
2246
2247
2248
2249
  iCnt=0;
  for (i=0; (i<2 && !bToIntersect); ++i) {
    const BOPCol_MapOfInteger& aMV2=(!i) ? aFI2.VerticesIn() 
      : aFI2.VerticesOn();
    //
    aIt.Initialize(aMV2);
2250
    for (; aIt.More(); aIt.Next()) {
2251
2252
      nV2=aIt.Value();
      if (aMVIn1.Contains(nV2) || aMVOn1.Contains(nV2)) {
2253
2254
2255
2256
2257
        ++iCnt;
        if (iCnt>1) {
          bToIntersect=!bToIntersect;
          break;
        }
2258
2259
2260
2261
2262
2263
      }
    }
  }
  //
  return bToIntersect;
}
2264
//=======================================================================
2265
2266
2267
//function : UpdatePaveBlocks
//purpose  : 
//=======================================================================
2268
void BOPAlgo_PaveFiller::UpdatePaveBlocks
2269
(const BOPCol_DataMapOfIntegerInteger& aDMI)
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
{
  if (aDMI.IsEmpty()) {
    return;
  }
  //
  Standard_Integer nSp, aNbPBP, nV[2], i, j;
  Standard_Real aT[2];
  Standard_Boolean bCB, bRebuild;
  BOPDS_ListIteratorOfListOfPaveBlock aItPB;
  BOPDS_MapOfPaveBlock aMPB;
2280
  BOPCol_MapOfInteger aMicroEdges;
2281
  //
2282
  BOPDS_VectorOfListOfPaveBlock& aPBP = myDS->ChangePaveBlocksPool();
2283
  aNbPBP = aPBP.Extent();
2284
2285
  for (i = 0; i < aNbPBP; ++i) {
    BOPDS_ListOfPaveBlock& aLPB = aPBP(i);
2286
2287
2288
    //
    aItPB.Initialize(aLPB);
    for (; aItPB.More(); aItPB.Next()) {
2289
2290
      Handle(BOPDS_PaveBlock) aPB = aItPB.Value();
      const Handle(BOPDS_CommonBlock)& aCB = myDS->CommonBlock(aPB);
2291
2292
      bCB = !aCB.IsNull();
      if (bCB) {
2293
        aPB = aCB->PaveBlock1();
2294
2295
2296
2297
2298
2299
      }
      //
      if (aMPB.Add(aPB)) {
        bRebuild = Standard_False;
        aPB->Indices(nV[0], nV[1]);
        aPB->Range(aT[0], aT[1]);
2300
2301
        // remember the fact if the edge had different vertices before substitution
        Standard_Boolean wasRegularEdge = (nV[0] != nV[1]);
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
        //
        for (j = 0; j < 2; ++j) {
          if (aDMI.IsBound(nV[j])) {
            BOPDS_Pave aPave;
            //
            nV[j] = aDMI.Find(nV[j]);
            aPave.SetIndex(nV[j]);
            aPave.SetParameter(aT[j]);
            //
            bRebuild = Standard_True;
            if (!j) {
              aPB->SetPave1(aPave);
2314
2315
            }
            else {
2316
2317
2318
2319
2320
2321
              aPB->SetPave2(aPave);
            }
          }
        }
        //
        if (bRebuild) {
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
          Standard_Boolean isDegEdge = myDS->ShapeInfo(aPB->Edge()).HasFlag();
          if (wasRegularEdge && !isDegEdge && nV[0] == nV[1]) {
            // now edge has the same vertex on both ends;
            // check if it is not a regular closed curve.
            const TopoDS_Edge& aE = TopoDS::Edge(myDS->Shape(aPB->Edge()));
            const TopoDS_Vertex& aV = TopoDS::Vertex(myDS->Shape(nV[0]));
            Standard_Real aLength = IntTools::Length(aE);
            Standard_Real aTolV = BRep_Tool::Tolerance(aV);
            if (aLength <= aTolV * 2.) {
              // micro edge, so mark it for removal
              aMicroEdges.Add(aPB->Edge());
              continue;
            }
          }
2336
          nSp = SplitEdge(aPB->OriginalEdge(), nV[0], aT[0], nV[1], aT[1]);
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
          if (bCB) {
            aCB->SetEdge(nSp);
          }
          else {
            aPB->SetEdge(nSp);
          }
        }// if (bRebuild) {
      }// if (aMPB.Add(aPB)) {
    }// for (; aItPB.More(); aItPB.Next()) {
  }// for (i=0; i<aNbPBP; ++i) {
  aMPB.Clear();
2348
2349
2350

  if (aMicroEdges.Extent())
    RemovePaveBlocks(aMicroEdges);
2351
}
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
//=======================================================================
//function : RemovePaveBlocks
//purpose  : 
//=======================================================================
void BOPAlgo_PaveFiller::RemovePaveBlocks(const BOPCol_MapOfInteger theEdges)
{
  // Remove all pave blocks referring to input edges:
  //
  // 1. from the Pave Blocks Pool
  BOPDS_VectorOfListOfPaveBlock& aPBP = myDS->ChangePaveBlocksPool();
  Standard_Integer aNbPBP = aPBP.Extent(), i;
  for (i = 0; i < aNbPBP; ++i) {
    BOPDS_ListOfPaveBlock& aLPB = aPBP(i);
    //
    BOPDS_ListIteratorOfListOfPaveBlock aItPB(aLPB);
    while (aItPB.More()) {
      const Handle(BOPDS_PaveBlock)& aPB = aItPB.Value();
      if (theEdges.Contains(aPB->Edge()))
        aLPB.Remove(aItPB);
      else
        aItPB.Next();
    }
  }

  // 2. from Face Info and section curves
  BOPCol_MapOfInteger aMPassed;
  BOPDS_VectorOfInterfFF& aFFs = myDS->InterfFF();
  Standard_Integer aNbFF = aFFs.Extent(), j;
  for (i = 0; i < aNbFF; ++i) {
    BOPDS_InterfFF& aFF = aFFs(i);
    Standard_Integer nF1, nF2;
    aFF.Indices(nF1, nF2);
    //
    // rebuild pave block maps of face info
    for (j = 0; j < 2; j++) {
      Standard_Integer nF = (j == 0 ? nF1 : nF2);
      if (!aMPassed.Add(nF))
        continue;
      BOPDS_FaceInfo& aFI = myDS->ChangeFaceInfo(nF);
      BOPDS_IndexedMapOfPaveBlock* aIMPB[] = { &aFI.ChangePaveBlocksIn(),
        &aFI.ChangePaveBlocksOn(), &aFI.ChangePaveBlocksSc() };
      for (Standard_Integer k = 0; k < 3; k++) {
        Standard_Integer aNbPB = aIMPB[k]->Extent(), m;
        for (m = 1; m <= aNbPB; ++m) {
          const Handle(BOPDS_PaveBlock)& aPB = aIMPB[k]->FindKey(m);
          if (theEdges.Contains(aPB->Edge()))
            break;
        }
        if (m <= aNbPB) {
          BOPDS_IndexedMapOfPaveBlock aMPBCopy = *aIMPB[k];
          aIMPB[k]->Clear();
          for (m = 1; m <= aNbPB; ++m) {
            const Handle(BOPDS_PaveBlock)& aPB = aMPBCopy(m);
            if (!theEdges.Contains(aPB->Edge()))
              aIMPB[k]->Add(aPB);
          }
        }
      }
    }
    // remove from Section pave blocks
    BOPDS_VectorOfCurve& aVNC = aFF.ChangeCurves();
    Standard_Integer aNbC = aVNC.Extent();
    for (j = 0; j < aNbC; ++j) {
      BOPDS_Curve& aNC = aVNC(j);
      BOPDS_ListOfPaveBlock& aLPB = aNC.ChangePaveBlocks();
      BOPDS_ListIteratorOfListOfPaveBlock aItPB(aLPB);
      while (aItPB.More()) {
        const Handle(BOPDS_PaveBlock)& aPB = aItPB.Value();
        if (theEdges.Contains(aPB->Edge()))
          aLPB.Remove(aItPB);
        else
          aItPB.Next();
      }
    }
  }
}

2429
//=======================================================================
2430
2431
2432
2433
//function : ToleranceFF
//purpose  : Computes the TolFF according to the tolerance value and 
//           types of the faces.
//=======================================================================
2434
2435
  void ToleranceFF(const BRepAdaptor_Surface& aBAS1,
                   const BRepAdaptor_Surface& aBAS2,
2436
2437
2438
2439
2440
                   Standard_Real& aTolFF)
{
  Standard_Real aTol1, aTol2;
  Standard_Boolean isAna1, isAna2;
  //
2441
2442
  aTol1 = aBAS1.Tolerance();
  aTol2 = aBAS2.Tolerance();
2443
2444
  aTolFF = Max(aTol1, aTol2);
  //
2445
2446
2447
2448
2449
  isAna1 = (aBAS1.GetType() == GeomAbs_Plane ||
            aBAS1.GetType() == GeomAbs_Cylinder ||
            aBAS1.GetType() == GeomAbs_Cone ||
            aBAS1.GetType() == GeomAbs_Sphere ||
            aBAS1.GetType() == GeomAbs_Torus);
2450
  //
2451
2452
2453
2454
2455
  isAna2 = (aBAS2.GetType() == GeomAbs_Plane ||
            aBAS2.GetType() == GeomAbs_Cylinder ||
            aBAS2.GetType() == GeomAbs_Cone ||
            aBAS2.GetType() == GeomAbs_Sphere ||
            aBAS2.GetType() == GeomAbs_Torus);
2456
  //
2457
2458
2459
  if (!isAna1 || !isAna2) {
    aTolFF =  Max(aTolFF, 5.e-6);
  }
2460
}
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
//=======================================================================
//function : UpdateBlocksWithSharedVertices
//purpose  : 
//=======================================================================
void BOPAlgo_PaveFiller::UpdateBlocksWithSharedVertices()
{
  if (!myNonDestructive) {
    return;
  }
  //
  myErrorStatus=0;
  //
  Standard_Integer aNbFF;
  //
  BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
  aNbFF=aFFs.Extent();
  if (!aNbFF) {
    return;
  }
  //
  Standard_Boolean bOnCurve, bHasShapeSD;
  Standard_Integer i, nF1, nF2, aNbC, j, nV, nVSD;
  Standard_Real aTolR3D, aTolV;
  BOPCol_MapOfInteger aMF;
  //
  for (i=0; i<aNbFF; ++i) {
    BOPDS_InterfFF& aFF=aFFs(i);
    //
    BOPDS_VectorOfCurve& aVC=aFF.ChangeCurves();
    aNbC=aVC.Extent();
    if (!aNbC) {
      continue;
    }
    //
    aFF.Indices(nF1, nF2);
    aTolR3D=aFF.TolR3D();
    //
    if (aMF.Add(nF1)) {
      myDS->UpdateFaceInfoOn(nF1);
    }
    if (aMF.Add(nF2)) {
      myDS->UpdateFaceInfoOn(nF2);
    }
    //
    // Collect old vertices that are shared for nF1, nF2 ->aMI;
    BOPCol_MapOfInteger aMI;
    BOPCol_MapIteratorOfMapOfInteger aItMI;
    //
    BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
    BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
    //
    const BOPCol_MapOfInteger& aMVOn1=aFI1.VerticesOn();
    const BOPCol_MapOfInteger& aMVIn1=aFI1.VerticesIn();
    const BOPCol_MapOfInteger& aMVOn2=aFI2.VerticesOn();
    const BOPCol_MapOfInteger& aMVIn2=aFI2.VerticesIn();
    //
    for (j=0; j<2; ++j) {
      const BOPCol_MapOfInteger& aMV1=(!j) ? aMVOn1 : aMVIn1;
      aItMI.Initialize(aMV1);
      for (; aItMI.More(); aItMI.Next()) {
        nV=aItMI.Value();
        if (myDS->IsNewShape(nV)) {
          continue;
        }
        if (aMVOn2.Contains(nV) || aMVIn2.Contains(nV)) {
          aMI.Add(nV);
        }
      }
    }
    //
    // Try to put vertices aMI on curves
    for (j=0; j<aNbC; ++j) {
      BOPDS_Curve& aNC=aVC.ChangeValue(j);
      //const IntTools_Curve& aIC=aNC.Curve();
      //
      aItMI.Initialize(aMI);
      for (; aItMI.More(); aItMI.Next()) {
        nV=aItMI.Value();
        //
        bHasShapeSD=myDS->HasShapeSD(nV, nVSD);
        if (bHasShapeSD) {
          continue;
        }
        //
        bOnCurve=EstimatePaveOnCurve(nV, aNC, aTolR3D);
        if (!bOnCurve) {
          continue;
        }
        //
        const TopoDS_Vertex& aV=*((TopoDS_Vertex *)&myDS->Shape(nV));
        aTolV=BRep_Tool::Tolerance(aV);
        //
        UpdateVertex(nV, aTolV);
      }
    }//for (j=0; j<aNbC; ++j) {
  }//for (i=0; i<aNbFF; ++i) {
  //
  UpdateCommonBlocksWithSDVertices();
}
//=======================================================================
//function : EstimatePaveOnCurve
//purpose  : 
//=======================================================================
Standard_Boolean BOPAlgo_PaveFiller::EstimatePaveOnCurve
  (const Standard_Integer nV,
   const BOPDS_Curve& aNC,
   const Standard_Real aTolR3D)
{
  Standard_Boolean bIsVertexOnLine;
  Standard_Real aT;
  //
  const TopoDS_Vertex& aV=*((TopoDS_Vertex *)&myDS->Shape(nV));
  const IntTools_Curve& aIC=aNC.Curve();
  //
  bIsVertexOnLine=myContext->IsVertexOnLine(aV, aIC, aTolR3D, aT);
  return bIsVertexOnLine;
}

//=======================================================================
//function : CorrectToleranceOfSE
//purpose  : 
//=======================================================================
void BOPAlgo_PaveFiller::CorrectToleranceOfSE()
{
  BOPDS_VectorOfInterfFF& aFFs = myDS->InterfFF();
  NCollection_IndexedDataMap<Standard_Integer,BOPDS_ListOfPaveBlock> aMVIPBs;
2587
  BOPCol_MapOfInteger aMVIToReduce;
2588
  //
2589
  // 1. iterate on all sections F-F
2590
2591
  Standard_Integer aNb = aFFs.Extent(), i;
  for (i = 0; i < aNb; ++i) {
2592
    BOPDS_InterfFF& aFF = aFFs(i);
2593
2594
    Standard_Real aTolR3D = aFF.TolR3D();
    Standard_Real aTolReal = aFF.TolReal();
2595
2596
    Standard_Boolean bToReduce = aTolReal < aTolR3D;
    // tolerance of intersection has been increased, so process this intersection
2597
    BOPDS_VectorOfCurve& aVNC = aFF.ChangeCurves();
2598
2599
    Standard_Integer aNbC = aVNC.Extent(), k;
    for (k = 0; k < aNbC; ++k) {
2600
2601
      BOPDS_Curve& aNC = aVNC(k);
      BOPDS_ListOfPaveBlock& aLPB = aNC.ChangePaveBlocks();
2602
      BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPB);
2603
      for (; aItLPB.More(); ) {
2604
2605
2606
        const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
        Standard_Integer nE;
        if (!aPB->HasEdge(nE)) {
2607
          aLPB.Remove(aItLPB);
2608
2609
2610
2611
2612
          continue;
        }
        //
        Standard_Boolean bIsReduced = Standard_False;
        if (bToReduce && (aPB->OriginalEdge() < 0)) {
2613
2614
2615
2616
2617
          const TopoDS_Edge& aE = TopoDS::Edge(myDS->Shape(nE));
          Standard_Real aTolE = BRep_Tool::Tolerance(aE);
          if (aTolReal < aTolE) {
            // reduce edge tolerance
            reinterpret_cast<BRep_TEdge*>(aE.TShape().operator->())->Tolerance(aTolReal);
2618
            bIsReduced = Standard_True;
2619
          }
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
        }
        //
        // fill in the map vertex index - pave blocks
        for (Standard_Integer j=0; j < 2; j++) {
          Standard_Integer nV = (j == 0 ? aPB->Pave1().Index() : aPB->Pave2().Index());
          BOPDS_ListOfPaveBlock *pPBList = aMVIPBs.ChangeSeek(nV);
          if (!pPBList) {
            pPBList = &aMVIPBs.ChangeFromIndex(aMVIPBs.Add(nV, BOPDS_ListOfPaveBlock()));
          }
          pPBList->Append(aPB);
          if (bIsReduced) {
            aMVIToReduce.Add(nV);
          }
        }
2634
        aItLPB.Next();
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
      }
    }
  }
  //
  if (aMVIToReduce.IsEmpty()) {
    return;
  }
  //
  // 2. try to reduce tolerances of connected vertices
  // 2.1 find all other edges containing these connected vertices to avoid
  //     reducing the tolerance to the value less than the tolerances of edges,
  //     i.e. minimal tolerance for the vertex is the max tolerance of the
  //     edges containing this vertex
  BOPCol_DataMapOfIntegerReal aMVITol;
  BOPDS_VectorOfListOfPaveBlock& aPBP = myDS->ChangePaveBlocksPool();
  aNb = aPBP.Extent();
  for (i = 0; i < aNb; ++i) {
    const BOPDS_ListOfPaveBlock& aLPB = aPBP(i);
    BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPB);
    for (; aItLPB.More(); aItLPB.Next()) {
      const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
      Standard_Integer nE;
      if (!aPB->HasEdge(nE)) {
        continue;
      }
      const TopoDS_Edge& aE = TopoDS::Edge(myDS->Shape(nE));
      Standard_Real aTolE = BRep_Tool::Tolerance(aE);
      //
      Standard_Integer nV[2];
      aPB->Indices(nV[0], nV[1]);
      //
      for (Standard_Integer j = 0; j < 2; j++) {
        if (aMVIToReduce.Contains(nV[j])) {
          Standard_Real *aMaxTol = aMVITol.ChangeSeek(nV[j]);
          if (!aMaxTol) {
            aMVITol.Bind(nV[j], aTolE);
          }
          else if (aTolE > *aMaxTol) {
            *aMaxTol = aTolE;
2674
2675
2676
2677
2678
          }
        }
      }
    }
  }
2679
2680
  //
  // 2.2 reduce tolerances if possible
2681
2682
2683
  aNb = aMVIPBs.Extent();
  for (i = 1; i <= aNb; ++i) {
    Standard_Integer nV = aMVIPBs.FindKey(i);
2684
2685
2686
2687
    if (!aMVIToReduce.Contains(nV)) {
      continue;
    }
    //
2688
    const TopoDS_Vertex& aV = TopoDS::Vertex(myDS->Shape(nV));
2689
2690
2691
2692
2693
2694
2695
    Standard_Real aTolV = BRep_Tool::Tolerance(aV);
    Standard_Real aMaxTol = aMVITol.IsBound(nV) ? aMVITol.Find(nV) : 0.;
    // it makes no sense to compute the real tolerance if it is
    // impossible to reduce the tolerance at least 0.1% of the current value
    if (aTolV - aMaxTol < 0.001 * aTolV) {
      continue;
    }
2696
2697
    //
    // compute the maximal distance from the vertex to the adjacent edges
2698
2699
    gp_Pnt aP = BRep_Tool::Pnt(aV);
    //
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
    const BOPDS_ListOfPaveBlock& aLPB = aMVIPBs.FindFromIndex(i);
    BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPB);
    for (; aItLPB.More(); aItLPB.Next()) {
      const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
      Standard_Integer nE = aPB->Edge();
      const TopoDS_Edge& aE = TopoDS::Edge(myDS->Shape(nE));
      const BOPDS_Pave& aPave = (aPB->Pave1().Index() == nV ? aPB->Pave1() : aPB->Pave2());
      BRepAdaptor_Curve aC(aE);
      gp_Pnt aPonE = aC.Value(aPave.Parameter());
      Standard_Real aDist = aP.Distance(aPonE);
      aDist += BRep_Tool::Tolerance(aE);
      if (aDist > aMaxTol) {
        aMaxTol = aDist;
      }
    }
2715
    //
2716
2717
2718
2719
2720
    if (aMaxTol < aTolV) {
      reinterpret_cast<BRep_TVertex*>(aV.TShape().operator->())->Tolerance(aMaxTol);
    }
  }
}