BOPAlgo_PaveFiller_6.cxx 88.1 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
#include <Bnd_Box.hxx>
#include <BOPAlgo_PaveFiller.hxx>
#include <BOPAlgo_SectionAttribute.hxx>
#include <BOPAlgo_Tools.hxx>
22
#include <BOPCol_DataMapOfShapeInteger.hxx>
23
24
25
#include <BOPCol_ListOfInteger.hxx>
#include <BOPCol_ListOfShape.hxx>
#include <BOPCol_MapOfInteger.hxx>
26
#include <BOPCol_NCVector.hxx>
27
#include <BOPCol_Parallel.hxx>
28
29
#include <BOPDS_CommonBlock.hxx>
#include <BOPDS_CoupleOfPaveBlocks.hxx>
30
#include <BOPDS_Curve.hxx>
31
32
#include <BOPDS_DataMapOfPaveBlockListOfPaveBlock.hxx>
#include <BOPDS_DS.hxx>
33
#include <BOPDS_FaceInfo.hxx>
34
35
36
37
#include <BOPDS_Interf.hxx>
#include <BOPDS_Iterator.hxx>
#include <BOPDS_ListOfPave.hxx>
#include <BOPDS_ListOfPaveBlock.hxx>
38
39
#include <BOPDS_MapOfPaveBlock.hxx>
#include <BOPDS_PaveBlock.hxx>
40
41
#include <BOPDS_Point.hxx>
#include <BOPDS_ShapeInfo.hxx>
42
43
#include <BOPDS_VectorOfCurve.hxx>
#include <BOPDS_VectorOfPoint.hxx>
44
#include <BOPTools.hxx>
45
46
47
48
#include <BOPTools_AlgoTools.hxx>
#include <BOPTools_AlgoTools3D.hxx>
#include <BRep_Builder.hxx>
#include <BRep_Tool.hxx>
49
#include <BRep_TEdge.hxx>
50
51
52
53
54
55
56
57
58
59
60
61
#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>
62
#include <IntTools.hxx>
63
64
65
66
67
68
69
70
71
72
73
74
#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>
75
#include <TopoDS.hxx>
76
77
78
79
#include <TopoDS_Compound.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Vertex.hxx>
80

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

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

95
 public:
96
97
98
99
100
101
  DEFINE_STANDARD_ALLOC

  BOPAlgo_FaceFace() : 
    IntTools_FaceFace(),  
    BOPAlgo_Algo(),
    myIF1(-1), myIF2(-1), myTolFF(1.e-7) {
102
103
  }
  //
104
  virtual ~BOPAlgo_FaceFace() {
105
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
  }
  //
  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;
  }
  //
141
142
143
144
  void SetFuzzyValue(const Standard_Real theFuzz) {
    IntTools_FaceFace::SetFuzzyValue(theFuzz);
  }
  //
145
146
  virtual void Perform() {
    BOPAlgo_Algo::UserBreak();
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    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; 
//
162
typedef BOPCol_Functor 
163
164
165
  <BOPAlgo_FaceFace,
  BOPAlgo_VectorOfFaceFace> BOPAlgo_FaceFaceFunctor;
//
166
typedef BOPCol_Cnt 
167
168
169
  <BOPAlgo_FaceFaceFunctor,
  BOPAlgo_VectorOfFaceFace> BOPAlgo_FaceFaceCnt;
/////////////////////////////////////////////////////////////////////////
170
171
172
173
//=======================================================================
//function : PerformFF
//purpose  : 
//=======================================================================
174
void BOPAlgo_PaveFiller::PerformFF()
175
176
177
178
179
180
181
182
183
184
185
186
187
{
  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;
188
  Standard_Boolean bToSplit, bTangentFaces;
189
  Standard_Integer nF1, nF2, aNbCurves, aNbPoints, i, aNbLP;
190
  Standard_Integer aNbFaceFace, k;
191
  Standard_Real aApproxTol, aTolR3D, aTolR2D, aTolFF, aTolReal;
192
  BRepAdaptor_Surface aBAS1, aBAS2;
193
  BOPCol_MapOfInteger aMI;
194
  BOPAlgo_VectorOfFaceFace aVFaceFace;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  //
  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)));
    //
214
215
216
217
218
219
220
221
222
    if (aMI.Add(nF1)) {
      myDS->UpdateFaceInfoOn(nF1);
      myDS->UpdateFaceInfoIn(nF1);
    }
    if (aMI.Add(nF2)) {
      myDS->UpdateFaceInfoOn(nF2);
      myDS->UpdateFaceInfoIn(nF2);
    }
    //
223
224
225
226
227
228
229
230
231
    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);
232
        BOPDS_InterfFF& aFF=aFFs.Append1();
233
234
235
236
237
238
        aFF.SetIndices(nF1, nF2);
        aFF.Init(0, 0);
        continue;
      }
    }
    //
239
240
241
242
243
244
245
    ToleranceFF(aBAS1, aBAS2, aTolFF); 
    //
    BOPAlgo_FaceFace& aFaceFace=aVFaceFace.Append1();
    //
    aFaceFace.SetIndices(nF1, nF2);
    aFaceFace.SetFaces(aF1, aF2);
    aFaceFace.SetTolFF(aTolFF);
246
247
248
249
250
251
252
    //
    IntSurf_ListOfPntOn2S aListOfPnts;
    GetEFPnts(nF1, nF2, aListOfPnts);
    aNbLP = aListOfPnts.Extent();
    if (aNbLP) {
      aFaceFace.SetList(aListOfPnts);
    }
253
    //
254
    aFaceFace.SetParameters(bApp, bCompC2D1, bCompC2D2, aApproxTol);
255
    aFaceFace.SetFuzzyValue(myFuzzyValue);
256
    aFaceFace.SetProgressIndicator(myProgressIndicator);
257
258
259
260
261
262
263
264
265
266
267
268
  }//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();
269
270
271
272
273
    //
    bIsDone=aFaceFace.IsDone();
    if (bIsDone) {
      aTolR3D=aFaceFace.TolReached3d();
      aTolR2D=aFaceFace.TolReached2d();
274
      aTolReal = aFaceFace.TolReal();
275
      bTangentFaces=aFaceFace.TangentFaces();
276
277
278
279
      //
      if (aTolR3D < aTolFF){
        aTolR3D=aTolFF;
      }
280
281
282
      if (aTolReal < aTolFF) {
        aTolReal = aTolFF;
      }
283
284
285
286
287
288
289
290
291
292
293
294
      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
295
296
297
      if (aNbCurves || aNbPoints) {
        myDS->AddInterf(nF1, nF2);
      } 
298
      //
299
      BOPDS_InterfFF& aFF=aFFs.Append1();
300
301
302
303
      aFF.SetIndices(nF1, nF2);
      //
      aFF.SetTolR3D(aTolR3D);
      aFF.SetTolR2D(aTolR2D);
304
      aFF.SetTolReal(aTolReal);
305
      aFF.SetTangentFaces(bTangentFaces);
306
307
308
309
310
      //
      // Curves, Points 
      aFF.Init(aNbCurves, aNbPoints);
      //
      // Curves
311
312
313
314
315
316
317
318
319
320
321
322

      // 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;
      }

323
324
325
326
327
328
      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();
329
        bValid=IntTools_Tools::CheckCurve(aC3D, aBoxExpandValue, aBox);
330
        if (bValid) {
331
          BOPDS_Curve& aNC=aVNC.Append1();
332
333
334
335
336
337
338
339
340
341
342
          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();
        //
343
        BOPDS_Point& aNP=aVNP.Append1();
344
345
346
347
348
        aNP.SetPnt(aP);
      }
    //}// if (aNbCs || aNbPs)
    }// if (bIsDone) {
    else {// 904/L1
349
      BOPDS_InterfFF& aFF=aFFs.Append1();
350
351
352
353
354
      aFF.SetIndices(nF1, nF2);
      aNbCurves=0;
      aNbPoints=0;
      aFF.Init(aNbCurves, aNbPoints);
    }
355
  }// for (k=0; k < aNbFaceFace; ++k) {
356
357
358
359
360
}
//=======================================================================
//function : MakeBlocks
//purpose  : 
//=======================================================================
361
void BOPAlgo_PaveFiller::MakeBlocks()
362
363
364
365
366
367
368
369
370
371
372
373
374
375
{
  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;
376
  Standard_Real aTolR3D, aT1, aT2;
377
  Handle(NCollection_BaseAllocator) aAllocator;
378
379
380
381
382
  BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
  TopoDS_Edge aES;
  Handle(BOPDS_PaveBlock) aPBOut;
  //
  //-----------------------------------------------------scope f
383
384
  aAllocator=
    NCollection_BaseAllocator::CommonBaseAllocator();
385
  //
386
  BOPCol_ListOfInteger aLSE(aAllocator), aLBV(aAllocator);
387
388
  BOPCol_MapOfInteger aMVOnIn(100, aAllocator), aMF(100, aAllocator),
                      aMVStick(100,aAllocator), aMVEF(100, aAllocator),
389
                      aMI(100, aAllocator), aMVBounds(100, aAllocator);
emv's avatar
emv committed
390
  BOPDS_IndexedMapOfPaveBlock aMPBOnIn(100, aAllocator);
391
  BOPDS_MapOfPaveBlock aMPBAdd(100, aAllocator), aMPBCommon;
392
393
394
  BOPDS_ListOfPaveBlock aLPB(aAllocator);
  BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks aMSCPB(100, aAllocator); 
  BOPCol_DataMapOfShapeInteger aMVI(100, aAllocator);
395
  BOPDS_DataMapOfPaveBlockListOfPaveBlock aDMExEdges(100, aAllocator);
396
  BOPCol_DataMapOfIntegerReal aMVTol(100, aAllocator);
397
398
  BOPCol_DataMapOfIntegerInteger aDMNewSD(100, aAllocator);
  BOPCol_DataMapOfIntegerListOfInteger aDMVLV;
399
400
  BOPCol_DataMapOfIntegerListOfInteger aDMBV(100, aAllocator);
  BOPCol_DataMapIteratorOfDataMapOfIntegerReal aItMV;
401
  BOPCol_IndexedMapOfShape aMicroEdges(100, aAllocator);
402
403
  //
  for (i=0; i<aNbFF; ++i) {
404
405
406
    //
    UserBreak();
    //
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
    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);
426
      myDS->UpdateFaceInfoIn(nF1);
427
428
429
    }
    if (aMF.Add(nF2)) {
      myDS->UpdateFaceInfoOn(nF2);
430
      myDS->UpdateFaceInfoIn(nF2);
431
    }
432
    //
433
434
435
436
437
    BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
    BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
    //
    aMVOnIn.Clear();
    aMPBOnIn.Clear();
438
    aMPBCommon.Clear();
439
    aDMBV.Clear();
440
    aMVTol.Clear();
441
    aLSE.Clear();
442
    //
443
    myDS->SubShapesOnIn(nF1, nF2, aMVOnIn, aMPBOnIn, aMPBCommon);
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
    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();
467
    GetStickVertices(nF1, nF2, aMVStick, aMVEF, aMI);
468
    //
469
470
    for (j = 0; j < aNbC; ++j) {
      BOPDS_Curve& aNC = aVC.ChangeValue(j);
471
472
473
      // DEBt
      aNC.InitPaveBlock1();
      //
474
475
476
477
478
479
480
481
482
483
484
      PutPavesOnCurve(aMVOnIn, aTolR3D, aNC, nF1, nF2, aMI, aMVEF, aMVTol, aDMVLV);
    }

    // if some E-F vertex was put on a curve due to large E-F intersection range,
    // and it also was put on another curve correctly then remove this vertex from
    // the first curve. Detect such case if the distance to curve exceeds aTolR3D.
    FilterPavesOnCurves(aVC, aTolR3D);

    for (j = 0; j<aNbC; ++j) {
      BOPDS_Curve& aNC=aVC.ChangeValue(j);
      const IntTools_Curve& aIC=aNC.Curve();
485
      //
486
      PutStickPavesOnCurve(aF1, aF2, aMI, aNC, aMVStick, aMVTol, aDMVLV);
487
      //904/F7
488
      if (aNbC == 1) {
489
        PutEFPavesOnCurve(aNC, aMI, aMVEF, aMVTol, aDMVLV);
490
491
492
      }
      //
      if (aIC.HasBounds()) {
493
494
495
496
497
498
        aLBV.Clear();
        //
        PutBoundPaveOnCurve(aF1, aF2, aTolR3D, aNC, aLBV);
        //
        if (!aLBV.IsEmpty()) {
          aDMBV.Bind(j, aLBV);
499
500
501
502
          BOPCol_ListIteratorOfListOfInteger aItI(aLBV);
          for (; aItI.More(); aItI.Next()) {
            aMVBounds.Add(aItI.Value());
          }
503
        }
504
505
      }
    }//for (j=0; j<aNbC; ++j) {
506

507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
    // 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;
        }
        //
534
535
        bValid2D=myContext->IsValidBlockForFaces(aT1, aT2, aIC, 
                                                 aF1, aF2, aTolR3D);
536
537
538
539
        if (!bValid2D) {
          continue;
        }
        //
540
        bExist=IsExistingPaveBlock(aPB, aNC, aLSE);
541
542
543
544
        if (bExist) {
          continue;
        }
        //
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
        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;
        }
        //
566
        Standard_Real aTolNew;
567
        bExist=IsExistingPaveBlock(aPB, aNC, aTolR3D, aMPBOnIn, aMPBCommon, aPBOut, aTolNew);
568
569
570
        if (bExist) {
          if (aMPBAdd.Add(aPBOut)) {
            Standard_Boolean bInBothFaces = Standard_True;
omy's avatar
omy committed
571
            if (!myDS->IsCommonBlock(aPBOut)) {
572
573
574
575
576
577
              Standard_Integer nE;
              Standard_Real aTolE;
              //
              nE = aPBOut->Edge();
              const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(nE);
              aTolE = BRep_Tool::Tolerance(aE);
578
579
580
581
              if (aTolNew < aFF.TolReal())
                aTolNew = aFF.TolReal();  // use real tolerance of intersection
              if (aTolNew > aTolE) {
                UpdateEdgeTolerance(nE, aTolNew);
582
583
              }
              bInBothFaces = Standard_False;
584
585
            } 
            else {
586
587
588
589
590
591
              bInBothFaces = (aFI1.PaveBlocksOn().Contains(aPBOut) ||
                              aFI1.PaveBlocksIn().Contains(aPBOut))&&
                             (aFI2.PaveBlocksOn().Contains(aPBOut) ||
                              aFI2.PaveBlocksIn().Contains(aPBOut));
            }
            if (!bInBothFaces) {
592
              PreparePostTreatFF(i, j, aPBOut, aMSCPB, aMVI, aLPBC);
593
594
595
596
597
            }
          }
          continue;
        }
        //
598
        // Make p-curves
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
        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);
615
616
617
        //
        aMVTol.UnBind(nV1);
        aMVTol.UnBind(nV2);
618
619
620
621
      }
      //
      aLPBC.RemoveFirst();
    }//for (j=0; j<aNbC; ++j) {
622
    //
623
    //back to previous tolerance values for unused vertices
624
    //and forget about SD groups of such vertices
625
626
627
    aItMV.Initialize(aMVTol);
    for (; aItMV.More(); aItMV.Next()) {
      nV1 = aItMV.Key();
628
      Standard_Real aTol = aItMV.Value();
629
630
      //
      const TopoDS_Vertex& aV = *(TopoDS_Vertex*)&myDS->Shape(nV1);
631
632
      const Handle(BRep_TVertex)& TV = 
        *((Handle(BRep_TVertex)*)&aV.TShape());
633
      TV->Tolerance(aTol);
634
635
636
637
638
639
640
641
642
      // reset bnd box
      BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nV1);
      Bnd_Box& aBoxDS=aSIDS.ChangeBox();
      aBoxDS = Bnd_Box();
      BRepBndLib::Add(aV, aBoxDS);
      aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
      //
      if (aDMVLV.IsBound(nV1))
        aDMVLV.UnBind(nV1);
643
644
    }
    //
645
    ProcessExistingPaveBlocks(i, aMPBOnIn, aDMBV, aMSCPB, aMVI, aMPBAdd);
646
647
648
  }//for (i=0; i<aNbFF; ++i) {
  // 
  // post treatment
649
650
  MakeSDVerticesFF(aDMVLV, aDMNewSD);
  myErrorStatus=PostTreatFF(aMSCPB, aMVI, aDMExEdges, aDMNewSD, aMicroEdges, aAllocator);
651
652
653
  if (myErrorStatus) {
    return;
  }
654
655
  // reduce tolerances of section edges where it is appropriate
  CorrectToleranceOfSE();
656
657
  //
  // update face info
658
  UpdateFaceInfo(aDMExEdges, aDMNewSD);
659
  //Update all pave blocks
660
  UpdatePaveBlocks(aDMNewSD);
661
662
663
664
665
666
  //-----------------------------------------------------scope t
  aMF.Clear();
  aMVStick.Clear();
  aMPBOnIn.Clear();
  aMVOnIn.Clear();
  aDMExEdges.Clear();
667
  aMI.Clear();
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
  aDMNewSD.Clear();
}

//=======================================================================
//function : MakeSDVerticesFF
//purpose  : 
//=======================================================================
void BOPAlgo_PaveFiller::MakeSDVerticesFF
  (const BOPCol_DataMapOfIntegerListOfInteger& theDMVLV,
   BOPCol_DataMapOfIntegerInteger& theDMNewSD)
{
  // Create a new SD vertex for each group of coinciding vertices
  // and put new substitutions to theDMNewSD.
  BOPCol_DataMapIteratorOfDataMapOfIntegerListOfInteger aItG(theDMVLV);
  for (; aItG.More(); aItG.Next()) {
    const BOPCol_ListOfInteger& aList = aItG.Value();
    // make SD vertices w/o creation of interfs
    Standard_Integer nSD = MakeSDVertices(aList, Standard_False);
    // update theDMNewSD
    BOPCol_ListIteratorOfListOfInteger aItL(aList);
    for (; aItL.More(); aItL.Next()) {
      Standard_Integer nV = aItL.Value();
      theDMNewSD.Bind(nV, nSD);
    }
  }
693
694
695
696
697
698
}

//=======================================================================
//function : PostTreatFF
//purpose  : 
//=======================================================================
699
Standard_Integer BOPAlgo_PaveFiller::PostTreatFF
700
701
702
    (BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& theMSCPB,
     BOPCol_DataMapOfShapeInteger& aMVI,
     BOPDS_DataMapOfPaveBlockListOfPaveBlock& aDMExEdges,
703
     BOPCol_DataMapOfIntegerInteger& aDMNewSD,
704
     const BOPCol_IndexedMapOfShape& theMicroEdges,
705
     const Handle(NCollection_BaseAllocator)& theAllocator)
706
707
708
709
710
711
712
713
714
715
{
  Standard_Integer iRet, aNbS;
  //
  iRet=0;
  aNbS=theMSCPB.Extent();
  if (!aNbS) {
    return iRet;
  }
  //
  Standard_Boolean bHasPaveBlocks, bOld;
716
  Standard_Integer iErr, nSx, nVSD, iX, iP, iC, j, nV, iV = 0, iE, k;
717
  Standard_Integer aNbLPBx;
718
719
720
721
722
723
  TopAbs_ShapeEnum aType;
  TopoDS_Shape aV, aE;
  BOPCol_ListIteratorOfListOfShape aItLS;
  BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
  BOPDS_PDS aPDS;
  Handle(BOPDS_PaveBlock) aPB1;
724
  BOPDS_Pave aPave[2];
725
726
727
728
  BOPDS_ShapeInfo aSI;
  //
  BOPCol_ListOfShape aLS(theAllocator);
  BOPAlgo_PaveFiller aPF(theAllocator);
729
730
  aPF.SetIsPrimary(Standard_False);
  aPF.SetNonDestructive(myNonDestructive);
731
732
733
  //
  BOPDS_VectorOfInterfFF& aFFs=myDS->InterfFF();
  //
734
  Standard_Integer aNbME = theMicroEdges.Extent();
735
  // 0
736
  if (aNbS==1 && (aNbME == 0)) {
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
    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);
  }
  //
777
778
779
780
781
782
783
  // 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) {
784
    const TopoDS_Edge& aEM = TopoDS::Edge(theMicroEdges(k));
785
786
    //
    TopoDS_Vertex aV1, aV2;
787
    TopExp::Vertices(aEM, aV1, aV2);
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
    //
    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);
    }
  }
  //
808
  // 2 Fuse shapes
809
  aPF.SetProgressIndicator(myProgressIndicator);
810
  aPF.SetRunParallel(myRunParallel);
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
  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) {
828
829
      Standard_Boolean bIntersectionPoint = theMSCPB.Contains(aSx);
      //
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
      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);
      }
847
848
849
850
      //
      if (!bIntersectionPoint) {
        // save SD connection
        nSx = aMVI.Find(aSx);
851
        aDMNewSD.Bind(nSx, iV);
852
853
854
        myDS->AddShapeSD(nSx, iV);
      }
      else {
855
      // update FF interference
856
857
858
859
860
861
862
      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);
863
      }
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
    }//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);
882
883
        }
        else {
884
885
          aSI.SetShapeType(aType);
          aSI.SetShape(aSx);
886
          iE = myDS->Append(aSI);
887
888
889
890
891
892
893
894
895
896
          //
          aPB1->SetEdge(iE);
        }
      }
      else {
        BOPDS_InterfFF& aFF=aFFs(iX);
        BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
        BOPDS_Curve& aNC=aVNC(iC);
        BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
        //
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
        // 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;
        }
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
        //
        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
950
            const Handle(BOPDS_PaveBlock) aPBRx=aPDS->RealPaveBlock(aPBx);
951
952
953
954
955
            //
            // update vertices of paves
            aPave[0]=aPBx->Pave1();
            aPave[1]=aPBx->Pave2();
            for (j=0; j<2; ++j) {
956
957
958
959
960
961
962
963
964
965
966
967
              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);
968
              }
969
              const BOPDS_Pave& aP1 = !j ? aPB1->Pave1() : aPB1->Pave2();
970
971
              if (aP1.Parameter() == aPave[j].Parameter() && 
                  aP1.Index() != iV) {
972
                aDMNewSD.Bind(aP1.Index(), iV);
973
                myDS->AddShapeSD(aP1.Index(), iV);
974
975
              }
              //
976
977
978
979
980
981
982
983
984
985
986
              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);
987
988
989
990
991
992
993
994
              // 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);
                }
              }
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
            }
            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  : 
//=======================================================================
1024
void BOPAlgo_PaveFiller::UpdateFaceInfo
1025
1026
  (BOPDS_DataMapOfPaveBlockListOfPaveBlock& theDME,
   const BOPCol_DataMapOfIntegerInteger& theDMV)
1027
1028
{
  Standard_Integer i, j, nV1, nF1, nF2, 
1029
                   aNbFF, aNbC, aNbP;
1030
  BOPDS_ListIteratorOfListOfPaveBlock aItLPB;
1031
  BOPCol_MapOfInteger aMF;
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  //
  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);
    //
1044
    // 1.1. Section edges
1045
1046
1047
1048
1049
1050
    BOPDS_VectorOfCurve& aVNC=aFF.ChangeCurves();
    aNbC=aVNC.Extent();
    for (j=0; j<aNbC; ++j) {
      BOPDS_Curve& aNC=aVNC(j);
      BOPDS_ListOfPaveBlock& aLPBC=aNC.ChangePaveBlocks();
      //
1051
      // Add section edges to face info
1052
1053
      aItLPB.Initialize(aLPBC);
      for (; aItLPB.More(); ) {
1054
        const Handle(BOPDS_PaveBlock)& aPB=aItLPB.Value();
1055
1056
1057
1058
1059
1060
1061
1062
1063
        //
        // Treat existing pave blocks
        if (theDME.IsBound(aPB)) {
          BOPDS_ListOfPaveBlock& aLPB=theDME.ChangeFind(aPB);
          UpdateExistingPaveBlocks(aPB, aLPB, nF1, nF2);
          aLPBC.Remove(aItLPB);
          continue;
        }
        //
1064
1065
        aFI1.ChangePaveBlocksSc().Add(aPB);
        aFI2.ChangePaveBlocksSc().Add(aPB);
1066
        aItLPB.Next();
1067
1068
      }
    }
1069
1070
    //
    // 1.2. Section vertices
1071
1072
1073
1074
1075
    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
1076
1077
1078
      if (nV1<0) {
        continue;
      }
1079
1080
1081
      aFI1.ChangeVerticesSc().Add(nV1);
      aFI2.ChangeVerticesSc().Add(nV1);
    }
1082
1083
1084
    //
    aMF.Add(nF1);
    aMF.Add(nF2);
1085
1086
  }
  //
1087
1088
1089
1090
1091
1092
  Standard_Boolean bVerts, bEdges;
  //
  bVerts = theDMV.Extent() > 0;
  bEdges = theDME.Extent() > 0;
  //
  if (!bVerts && !bEdges) {
1093
1094
1095
    return;
  }
  //
1096
1097
1098
1099
1100
1101
1102
1103
1104
  // 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();
1105
    //
1106
    BOPDS_FaceInfo& aFI = myDS->ChangeFaceInfo(nF1);
1107
    //
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
    // 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);
1120
        }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
        //
        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);
1144
1145
1146
1147
1148
            if (aLPB.IsEmpty()) {
              aMPBOnIn.Add(aPB);
              continue;
            }
            //
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
            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) {
  }
1162
1163
1164
1165
1166
}
//=======================================================================
//function : IsExistingVertex
//purpose  : 
//=======================================================================
1167
1168
1169
1170
Standard_Boolean BOPAlgo_PaveFiller::IsExistingVertex
  (const gp_Pnt& aP,
   const Standard_Real theTolR3D,
   const BOPCol_MapOfInteger& aMVOnIn)const
1171
1172
1173
{
  Standard_Boolean bRet;
  Standard_Integer nV, iFlag;
1174
  Standard_Real aTolCheck;
1175
1176
1177
1178
  gp_Pnt aPV;
  Bnd_Box aBoxP;
  BOPCol_MapIteratorOfMapOfInteger aIt;
  //
1179
  aTolCheck = theTolR3D + myFuzzyValue;
1180
1181
1182
1183
1184
1185
1186
1187
  bRet=Standard_True;
  //
  aBoxP.Add(aP);
  aBoxP.Enlarge(theTolR3D);
  //
  aIt.Initialize(aMVOnIn);
  for (; aIt.More(); aIt.Next()) {
    nV=aIt.Value();
1188
1189
1190
    const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
    const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&aSIV.Shape()));
    const Bnd_Box& aBoxV=aSIV.Box();
1191
1192
    //
    if (!aBoxP.IsOut(aBoxV)) {
1193
      iFlag=BOPTools_AlgoTools::ComputeVV(aV, aP, aTolCheck);
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
      if (!iFlag) {
        return bRet;
      }
    }
  }
  return !bRet;
}
//=======================================================================
//function : IsExistingPaveBlock
//purpose  : 
//=======================================================================
1205
1206
1207
1208
Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
  (const Handle(BOPDS_PaveBlock)& thePB,
   const BOPDS_Curve& theNC,
   const BOPCol_ListOfInteger& theLSE)
1209
1210
1211
1212
1213
1214
1215
{
  Standard_Boolean bRet=Standard_True;
  //
  if (theLSE.IsEmpty()) {
    return !bRet;
  } 
  //
1216
  Standard_Real aT1, aT2, aTm, aTx, aTolE, aTolCheck, aTol, aDist;
1217
  Standard_Integer nE, iFlag, nV1, nV2;
1218
1219
1220
1221
1222
  gp_Pnt aPm;
  Bnd_Box aBoxPm;
  BOPCol_ListIteratorOfListOfInteger aItLI;
  //
  thePB->Range(aT1, aT2);
1223
1224
1225
1226
1227
1228
1229
1230
  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);

1231
1232
1233
  aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
  theNC.Curve().D0(aTm, aPm);
  aBoxPm.Add(aPm);
1234
  aBoxPm.Enlarge(aTol);
1235
1236
1237
1238
  //
  aItLI.Initialize(theLSE);
  for (; aItLI.More(); aItLI.Next()) {
    nE=aItLI.Value();
1239
1240
    if (nE < 0)
      continue;
1241
1242
1243
1244
    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()));
1245
1246
1247
      aTolE = BRep_Tool::Tolerance(aE);
      aTolCheck = Max(aTolE, aTol) + myFuzzyValue;
      iFlag = myContext->ComputePE(aPm, aTolCheck, aE, aTx, aDist);
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
      if (!iFlag) {
        return bRet;
      }
    }
  }
  return !bRet;
}

//=======================================================================
//function : IsExistingPaveBlock
//purpose  : 
//=======================================================================
1260
Standard_Boolean BOPAlgo_PaveFiller::IsExistingPaveBlock
1261
1262
1263
    (const Handle(BOPDS_PaveBlock)& thePB,
     const BOPDS_Curve& theNC,
     const Standard_Real theTolR3D,
emv's avatar
emv committed
1264
     const BOPDS_IndexedMapOfPaveBlock& theMPBOnIn,
1265
     const BOPDS_MapOfPaveBlock& theMPBCommon,
1266
1267
     Handle(BOPDS_PaveBlock)& aPBOut,
     Standard_Real& theTolNew)
1268
1269
{
  Standard_Boolean bRet;
1270
  Standard_Real aT1, aT2, aTm, aTx, aTolCheck;
emv's avatar
emv committed
1271
  Standard_Integer nSp, iFlag1, iFlag2, nV11, nV12, nV21, nV22, i, aNbPB;
1272
  gp_Pnt aP1, aPm, aP2;
1273
  Bnd_Box aBoxP1, aBoxPm, aBoxP2, aBoxTmp;
1274
  //
1275
  bRet=Standard_False;
1276
  aTolCheck = theTolR3D + myFuzzyValue;
1277
1278
1279
1280
  const IntTools_Curve& aIC=theNC.Curve();
  //
  thePB->Range(aT1, aT2);
  thePB->Indices(nV11, nV12);
1281
1282
  const Standard_Real aTolV11 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV11)));
  const Standard_Real aTolV12 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV12)));
1283
  const Standard_Real aTolV1 = Max(aTolV11, aTolV12) + myFuzzyValue;
1284

1285
1286
1287
  //first point
  aIC.D0(aT1, aP1);
  aBoxP1.Add(aP1);
1288
  aBoxP1.Enlarge(aTolV11);
1289
1290
1291
1292
1293
1294
1295
  //intermediate point
  aTm=IntTools_Tools::IntermediatePoint (aT1, aT2);
  aIC.D0(aTm, aPm);
  aBoxPm.Add(aPm);
  //last point
  aIC.D0(aT2, aP2);
  aBoxP2.Add(aP2);
1296
  aBoxP2.Enlarge(aTolV12);
1297
  //
1298
  theTolNew = 0.;
emv's avatar
emv committed
1299
1300
1301
  aNbPB = theMPBOnIn.Extent();
  for (i = 1; i <= aNbPB; ++i) {
    const Handle(BOPDS_PaveBlock)& aPB = theMPBOnIn(i);
1302
    aPB->Indices(nV21, nV22);
1303
1304
    const Standard_Real aTolV21 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV21)));
    const Standard_Real aTolV22 = BRep_Tool::Tolerance(TopoDS::Vertex(myDS->Shape(nV22)));
1305
    const Standard_Real aTolV2 = Max(aTolV21, aTolV22) + myFuzzyValue;
1306
    nSp=aPB->Edge();
1307
1308
    if (nSp < 0)
      continue;
1309
    const BOPDS_ShapeInfo& aSISp=myDS->ChangeShapeInfo(nSp);
1310
    const TopoDS_Edge& aSp=(*(TopoDS_Edge *)(&aSISp.Shape()));
1311
    const Bnd_Box& aBoxSp=aSISp.Box();
1312
    //
1313
1314
1315
1316
    iFlag1 = (nV11 == nV21 || nV11 == nV22) ? 2 : 
      (!aBoxSp.IsOut(aBoxP1) ? 1 : 0);
    iFlag2 = (nV12 == nV21 || nV12 == nV22) ? 2 : 
      (!aBoxSp.IsOut(aBoxP2) ? 1 : 0);
1317
    if (iFlag1 && iFlag2) {
1318
      Standard_Real aDist = 0.;
1319

1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
      Standard_Real aRealTol = aTolCheck;
      if (myDS->IsCommonBlock(aPB))
      {
        aRealTol = Max(aRealTol, Max(aTolV1, aTolV2));
        if (theMPBCommon.Contains(aPB))
          // for an edge, which is a common block with a face,
          // increase the chance to coincide with section curve
          aRealTol *= 2.;
      }
      
1330
1331
1332
1333
1334
1335
1336
      aBoxTmp = aBoxPm;
      aBoxTmp.Enlarge(aRealTol);

      if (aBoxSp.IsOut(aBoxTmp) || myContext->ComputePE(aPm, 
                                                        aRealTol,
                                                        aSp, 
                                                        aTx, theTolNew)) {
1337
1338
1339
1340
        continue;
      }
      //
      if (iFlag1 == 1) {
1341
        iFlag1 = !myContext->ComputePE(aP1, aRealTol, aSp, aTx, aDist);
1342
        if (iFlag1 && theTolNew < aDist)
1343
          theTolNew = aDist;
1344
1345
1346
      }
      //
      if (iFlag2 == 1) {
1347
        iFlag2 = !myContext->ComputePE(aP2, aRealTol, aSp, aTx, aDist);
1348
        if (iFlag2 && theTolNew < aDist)
1349
          theTolNew = aDist;
1350
1351
1352
1353
1354
1355
      }
      //
      if (iFlag1 && iFlag2) {
        aPBOut = aPB;
        bRet=Standard_True;
        break;
1356
1357
1358
      }
    }
  }
1359
  return bRet;
1360
1361
1362
1363
1364
}
//=======================================================================
//function : PutBoundPaveOnCurve
//purpose  : 
//=======================================================================
1365
1366
1367
1368
1369
void BOPAlgo_PaveFiller::PutBoundPaveOnCurve(const TopoDS_Face& aF1,
                                             const TopoDS_Face& aF2,
                                             const Standard_Real aTolR3D,
                                             BOPDS_Curve& aNC,
                                             BOPCol_ListOfInteger& aLVB)
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
{
  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);
1457
      aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
1458
1459
      //
      aLVB.Append(nVn);
1460
1461
1462
1463
1464
    }
  }
}

//=======================================================================
1465
//function : PutPavesOnCurve
1466
1467
//purpose  : 
//=======================================================================
1468
void BOPAlgo_PaveFiller::PutPavesOnCurve
1469
1470
1471
1472
1473
1474
1475
  (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,
1476
1477
   BOPCol_DataMapOfIntegerReal& aMVTol,
   BOPCol_DataMapOfIntegerListOfInteger& aDMVLV)
1478
{
1479
  Standard_Boolean bInBothFaces;
1480
1481
1482
1483
1484
  Standard_Integer nV;
  BOPCol_MapIteratorOfMapOfInteger aIt;
  //
  const Bnd_Box& aBoxC=aNC.Box();
  //
1485
1486
1487
1488
  //Put EF vertices first
  aIt.Initialize(aMVEF);
  for (; aIt.More(); aIt.Next()) {
    nV=aIt.Value();
1489
    PutPaveOnCurve(nV, aTolR3D, aNC, aMI, aMVTol, aDMVLV, 2);
1490
1491
  }
  //Put all other vertices
1492
1493
1494
  aIt.Initialize(aMVOnIn);
  for (; aIt.More(); aIt.Next()) {
    nV=aIt.Value();
1495
1496
    if (aMVEF.Contains(nV)) {
      continue;
1497
1498
    }
    //
1499
1500
    const BOPDS_ShapeInfo& aSIV=myDS->ShapeInfo(nV);
    const Bnd_Box& aBoxV=aSIV.Box();
1501
    //
1502
1503
1504
1505
1506
1507
    if (aBoxC.IsOut(aBoxV)){
      continue;
    }
    if (!myDS->IsNewShape(nV)) {
      const BOPDS_FaceInfo& aFI1 = myDS->FaceInfo(nF1);
      const BOPDS_FaceInfo& aFI2 = myDS->FaceInfo(nF2);
1508
      //
1509
1510
1511
1512
1513
1514
      bInBothFaces = (aFI1.VerticesOn().Contains(nV) ||
                      aFI1.VerticesIn().Contains(nV))&&
                     (aFI2.VerticesOn().Contains(nV) ||
                      aFI2.VerticesIn().Contains(nV));
      if (!bInBothFaces) {
        continue;
1515
      }
1516
    }
1517
    //
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
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
1600
1601
1602
1603
1604
1605
1606
1607
1608
    PutPaveOnCurve(nV, aTolR3D, aNC, aMI, aMVTol, aDMVLV, 1);
  }
}

//=======================================================================
//function : FilterPavesOnCurves
//purpose  : 
//=======================================================================
namespace {
  struct PaveBlockDist {
    Handle(BOPDS_PaveBlock) PB;
    Standard_Real Dist; // square distance from vertex to the paveblock
    Standard_Real SinAngle; // sinus of angle between projection vector 
    // and tangent at projection point
  };
}
void BOPAlgo_PaveFiller::FilterPavesOnCurves(const BOPDS_VectorOfCurve& theVNC,
                                             const Standard_Real theTolR3D)
{
  Standard_Real aSqTol = theTolR3D * theTolR3D;

  // For each vertex found in ExtPaves of pave blocks of section curves
  // collect list of pave blocks with distance to the curve
  NCollection_IndexedDataMap<Standard_Integer,NCollection_List<PaveBlockDist> > aIDMVertPBs;
  Standard_Integer i;
  const Standard_Real anEps = gp::Resolution();
  for (i = 0; i < theVNC.Extent(); ++i)
  {
    const BOPDS_Curve& aNC = theVNC(i);
    const IntTools_Curve& aIC = aNC.Curve();
    GeomAdaptor_Curve aGAC(aIC.Curve());
    const Handle(BOPDS_PaveBlock)& aPB = aNC.PaveBlocks().First();
    const BOPDS_ListOfPave& aPaves = aPB->ExtPaves();
    BOPDS_ListOfPave::Iterator itPaves(aPaves);
    for (; itPaves.More(); itPaves.Next())
    {
      const BOPDS_Pave& aPave = itPaves.Value();
      Standard_Integer nV = aPave.Index();
      const TopoDS_Vertex& aV = TopoDS::Vertex(myDS->Shape(nV));
      // compute distance from vertex to the point on curve with vertex parameter
      gp_Pnt aPV = BRep_Tool::Pnt(aV);
      Standard_Real aPar = aPave.Parameter();
      gp_Pnt aPonC;
      gp_Vec aD1;
      aGAC.D1(aPar, aPonC, aD1);
      gp_Vec aProjVec(aPV, aPonC);
      Standard_Real aSqDist = aProjVec.SquareMagnitude();
      Standard_Real aSqD1Mod = aD1.SquareMagnitude();
      Standard_Real aSin = aProjVec.CrossSquareMagnitude(aD1);
      if (aSqDist > anEps && aSqD1Mod > anEps)
        aSin = sqrt(aSin / aSqDist / aSqD1Mod);
      NCollection_List<PaveBlockDist>* pList = aIDMVertPBs.ChangeSeek(nV);
      if (!pList)
        pList = &aIDMVertPBs.ChangeFromIndex(aIDMVertPBs.Add(nV, NCollection_List<PaveBlockDist>()));
      PaveBlockDist aPBD = { aPB, aSqDist, aSin };
      pList->Append(aPBD);
    }
  }

  // Process each vertex
  const Standard_Real aSinAngleMin = 0.5; // angle below which projection is suspicious
  for (i = 1; i <= aIDMVertPBs.Extent(); i++)
  {
    Standard_Integer nV = aIDMVertPBs.FindKey(i);
    const NCollection_List<PaveBlockDist>& aList = aIDMVertPBs(i);
    // Find a pave with minimal distance
    Standard_Real aMinDist = RealLast();
    Handle(BOPDS_PaveBlock) aPBMinDist;
    NCollection_List<PaveBlockDist>::Iterator itL(aList);
    for (; itL.More(); itL.Next())
    {
      const PaveBlockDist& aPBD = itL.Value();
      if (aPBD.Dist < aMinDist)
      {
        aMinDist = aPBD.Dist;
        aPBMinDist = aPBD.PB;
      }
    }
    // Remove a vertex from a pave block if the distance is greater than the tolerance 
    // and there are other pave blocks for which the distance is less than the current.
    // Do not remove a vertex if it is projected on the curve with quite large angle
    // (see test bugs modalg_6 bug27761).
    Standard_Real aCheckDist = 100. * Max(aSqTol, aMinDist);
    for (itL.Init(aList); itL.More(); itL.Next())
    {
      const PaveBlockDist& aPBD = itL.Value();
      if (aPBD.Dist > aCheckDist && aPBD.SinAngle < aSinAngleMin)
      {
        aPBD.PB->RemoveExtPave(nV);
      }
    }
1609
1610
1611
1612
1613
1614
1615
  }
}

//=======================================================================
//function : ExtendedTolerance
//purpose  : 
//=======================================================================
1616
1617
1618
1619
1620
Standard_Boolean BOPAlgo_PaveFiller::ExtendedTolerance
  (const Standard_Integer nV,
   const BOPCol_MapOfInteger& aMI,
   Standard_Real& aTolVExt,
   const Standard_Integer aType)
1621
1622
1623
1624
1625
{
  Standard_Boolean bFound = Standard_False;
  if (!(myDS->IsNewShape(nV))) {
    return bFound;
  }
1626
1627
  //
  Standard_Integer i, k, aNbLines, aNbInt;
1628
1629
1630
  Standard_Real aT11, aT12, aD1, aD2, aD;
  TopoDS_Vertex aV;
  gp_Pnt aPV, aP11, aP12;
1631
1632
1633
1634
1635
1636
1637
1638
1639
  //
  k = 0;
  aNbInt = 2;
  if (aType == 1) {
    aNbInt = 1;
  } else if (aType == 2) {
    k = 1;
  }
  //
1640
1641
1642
1643
1644
  aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
  aPV=BRep_Tool::Pnt(aV);
  //
  BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
  BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
1645
1646
  //
  for (; k<aNbInt; ++k) {
1647
1648
1649
    aNbLines = !k ? aEEs.Extent() : aEFs.Extent();
    for (i = 0; i < aNbLines; ++i) {
      BOPDS_Interf *aInt = !k ? (BOPDS_Interf*) (&aEEs(i)) :
1650
        (BOPDS_Interf*) (&aEFs(i));
1651
      if (aInt->IndexNew() == nV) {
1652
1653
        if (aMI.Contains(aInt->Index1()) && 
            aMI.Contains(aInt->Index2())) {
1654
          const IntTools_CommonPrt& aComPrt = !k ? aEEs(i).CommonPart() :
1655
            aEFs(i).CommonPart();
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
          //
          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  : 
//=======================================================================
1679
1680
1681
1682
void BOPAlgo_PaveFiller::GetEFPnts
  (const Standard_Integer nF1,
   const Standard_Integer nF2,
   IntSurf_ListOfPntOn2S& aListOfPnts)
1683
1684
1685
{
  Standard_Integer nE, nF, nFOpposite, aNbEFs, i;
  Standard_Real U1, U2, V1, V2, f, l;
1686
  BOPCol_MapOfInteger aMI;
1687
1688
  //
  //collect indexes of all shapes from nF1 and nF2.
1689
1690
  GetFullShapeMap(nF1, aMI);
  GetFullShapeMap(nF2, aMI);
1691
1692
1693
1694
1695
1696
  //
  BOPDS_VectorOfInterfEF& aEFs=myDS->InterfEF();
  aNbEFs = aEFs.Extent();
  //
  for(i = 0; i < aNbEFs; ++i) {
    const BOPDS_InterfEF& aEF = aEFs(i);
1697
1698
1699
1700
    if (aEF.HasIndexNew()) {
      aEF.Indices(nE, nFOpposite);
      if(aMI.Contains(nE) && aMI.Contains(nFOpposite)) {
        const IntTools_CommonPrt& aCP = aEF.CommonPart();
1701
1702
        Standard_Real aPar = aCP.VertexParameter1();
        const TopoDS_Edge& aE = (*(TopoDS_Edge*)(&myDS->Shape(nE)));
1703
1704
        const TopoDS_Face& aFOpposite = 
          (*(TopoDS_Face*)(&myDS->Shape(nFOpposite)));
1705
1706
1707
1708
1709
        //
        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)));
1710
1711
        Handle(Geom2d_Curve) aPCurve = 
          BRep_Tool::CurveOnSurface(aE, aF, f, l);
1712
        //
1713
        GeomAPI_ProjectPointOnSurf& aProj=myContext->ProjPS(aFOpposite);
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
        //
        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);
          }
        }
      }
    }
  }
}

//=======================================================================
1752
//function : PutEFPavesOnCurve
1753
1754
//purpose  : 
//=======================================================================
1755
1756
1757
1758
  void BOPAlgo_PaveFiller::PutEFPavesOnCurve
  (BOPDS_Curve& aNC,
   const BOPCol_MapOfInteger& aMI,
   const BOPCol_MapOfInteger& aMVEF,
1759
1760
   BOPCol_DataMapOfIntegerReal& aMVTol,
   BOPCol_DataMapOfIntegerListOfInteger& aDMVLV)
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
{
  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);
1787
  //
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
  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();
1798
      PutPaveOnCurve(nV, aDist, aNC, aMI, aMVTol, aDMVLV);
1799
1800
1801
1802
1803
1804
1805
1806
    }
  }
}

//=======================================================================
//function : ProcessUnUsedVertices
//purpose  : 
//=======================================================================
1807
1808
1809
1810
1811
1812
  void BOPAlgo_PaveFiller::PutStickPavesOnCurve
  (const TopoDS_Face& aF1,
   const TopoDS_Face& aF2,
   const BOPCol_MapOfInteger& aMI,
   BOPDS_Curve& aNC,
   const BOPCol_MapOfInteger& aMVStick,
1813
1814
   BOPCol_DataMapOfIntegerReal& aMVTol,
   BOPCol_DataMapOfIntegerListOfInteger& aDMVLV)
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
{
  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);
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
  //
  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]);
1844
    //
1845
1846
1847
1848
1849
    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);
1850
      //
1851
1852
1853
1854
1855
      for (m=0; m<2; ++m) {
        aD2=aPC[m].SquareDistance(aPV);
        if (aD2>aDT2) {// no rich
          continue; 
        }
1856
        //
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
        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;
1869
        //
1870
1871
1872
1873
1874
1875
1876
        if (aScPr>aDScPr) {
          continue;
        }
        //
        // The intersection curve aIC is vanishing curve (the crease)
        aD=sqrt(aD2);
        //
1877
        PutPaveOnCurve(nV, aD, aNC, aMI, aMVTol, aDMVLV);
1878
      }
1879
1880
1881
1882
    }//for (jVU=1; jVU=aNbVU; ++jVU) {
  }
  //}//if (aTypeC==GeomAbs_BezierCurve || aTypeC==GeomAbs_BSplineCurve) {
  //}//if(aType1==GeomAbs_Torus  || aType2==GeomAbs_Torus) {
1883
1884
1885
1886
1887
1888
}

//=======================================================================
//function : GetStickVertices
//purpose  : 
//=======================================================================
1889
1890
1891
1892
1893
void BOPAlgo_PaveFiller::GetStickVertices(const Standard_Integer nF1,
                                          const Standard_Integer nF2,
                                          BOPCol_MapOfInteger& aMVStick,
                                          BOPCol_MapOfInteger& aMVEF,
                                          BOPCol_MapOfInteger& aMI)
1894
{
1895
  Standard_Integer nS1, nS2, nVNew, aTypeInt, i;
1896
1897
1898
1899
1900
1901
1902
  //
  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();
  //
1903
1904
1905
1906
  Standard_Integer aNbLines[5] = {
    aVVs.Extent(), aVEs.Extent(), aEEs.Extent(),
    aVFs.Extent(), aEFs.Extent()
    };
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
  //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)) :
1917
1918
         ((aTypeInt==2) ? (BOPDS_Interf*)(&aEEs(i)) : 
          (BOPDS_Interf*)(&aVFs(i))));
1919
1920
1921
1922
1923
1924
      if (aInt->HasIndexNew()) {
        aInt->Indices(nS1, nS2);
        if(aMI.Contains(nS1) && aMI.Contains(nS2)) {
          nVNew = aInt->IndexNew();
          aMVStick.Add(nVNew);
        }
1925
1926
1927
1928
      }
    }
  }
  //collect EF interferences
1929
1930
1931
1932
1933
1934
  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();
1935
1936
1937
1938
1939
1940
1941
1942
        aMVStick.Add(nVNew);
        aMVEF.Add(nVNew);
      }
    }
  }
}

//=======================================================================
1943
// function: GetFullShapeMap
1944
1945
// purpose: 
//=======================================================================
1946
1947
void BOPAlgo_PaveFiller::GetFullShapeMap(const Standard_Integer nF,
                                         BOPCol_MapOfInteger& aMI)
1948
{
1949
1950
1951
1952
1953
1954
  BOPCol_ListIteratorOfListOfInteger aIt;
  Standard_Integer nS;
  //
  const BOPDS_ShapeInfo& aSI = myDS->ShapeInfo(nF);
  const BOPCol_ListOfInteger& aLI = aSI.SubShapes();
  //
1955
  aMI.Add(nF);
1956
1957
1958
1959
  aIt.Initialize(aLI);
  for (; aIt.More(); aIt.Next()) {
    nS = aIt.Value();
    aMI.Add(nS);
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
  }
}

//=======================================================================
// 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  : 
//=======================================================================
1991
void BOPAlgo_PaveFiller::PutPaveOnCurve
1992
1993
  (const Standard_Integer nV,
   const Standard_Real aTolR3D,
1994
   const BOPDS_Curve& aNC,
1995
1996
   const BOPCol_MapOfInteger& aMI,
   BOPCol_DataMapOfIntegerReal& aMVTol,
1997
   BOPCol_DataMapOfIntegerListOfInteger& aDMVLV,
1998
   const Standard_Integer iCheckExtend)
1999
2000
{
  Standard_Boolean bIsVertexOnLine;
2001
  Standard_Real aT;
2002
  //
2003
  const TopoDS_Vertex& aV = (*(TopoDS_Vertex *)(&myDS->Shape(nV)));
2004
  const Handle(BOPDS_PaveBlock)& aPB = aNC.PaveBlocks().First();
2005
2006
  const IntTools_Curve& aIC = aNC.Curve();
  //
2007
2008
2009
  Standard_Real aTolV = (aMVTol.IsBound(nV) ? aMVTol(nV) : BRep_Tool::Tolerance(aV));

  bIsVertexOnLine = myContext->IsVertexOnLine(aV, aTolV, aIC, aTolR3D + myFuzzyValue, aT);
2010
  if (!bIsVertexOnLine && iCheckExtend) {
2011
    ExtendedTolerance(nV, aMI, aTolV, iCheckExtend);
2012
    bIsVertexOnLine = myContext->IsVertexOnLine(aV, aTolV, aIC, aTolR3D + myFuzzyValue, aT);
2013
2014
  }
  //
2015
  if (bIsVertexOnLine) {
2016
2017
    // check if aPB contains the parameter aT
    Standard_Boolean bExist;
2018
2019
    Standard_Integer nVUsed;
    Standard_Real aPTol, aDTol;
2020
    //
2021
    aDTol = 1.e-12;
2022
    //
2023
2024
    GeomAdaptor_Curve aGAC(aIC.Curve());
    aPTol = aGAC.Resolution(aTolR3D);
2025
    //
2026
    bExist = aPB->ContainsParameter(aT, aPTol, nVUsed);
2027
2028
    if (bExist) {
      // use existing pave
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
      BOPCol_ListOfInteger* pList = aDMVLV.ChangeSeek(nVUsed);
      if (!pList) {
        pList = aDMVLV.Bound(nVUsed, BOPCol_ListOfInteger());
        pList->Append(nVUsed);
        if (!aMVTol.IsBound(nVUsed)) {
          const TopoDS_Vertex& aVUsed = (*(TopoDS_Vertex *)(&myDS->Shape(nVUsed)));
          aTolV = BRep_Tool::Tolerance(aVUsed);
          aMVTol.Bind(nVUsed, aTolV);
        }
      }
      pList->Append(nV);
      if (!aMVTol.IsBound(nV)) {
        aTolV = BRep_Tool::Tolerance(aV);
        aMVTol.Bind(nV, aTolV);
      }
2044
2045
2046
2047
2048
2049
2050
2051
    }
    else {
      // add new pave
      BOPDS_Pave aPave;
      aPave.SetIndex(nV);
      aPave.SetParameter(aT);
      aPB->AppendExtPave(aPave);
      //
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
      gp_Pnt aP1 = aGAC.Value(aT);
      aTolV = BRep_Tool::Tolerance(aV);
      gp_Pnt aP2 = BRep_Tool::Pnt(aV);
      Standard_Real aDist = aP1.Distance(aP2);
      if (aDist > aTolV) {
        BRep_Builder().UpdateVertex(aV, aDist + aDTol);
        //
        if (!aMVTol.IsBound(nV)) {
          aMVTol.Bind(nV, aTolV);
        }
        //
        BOPDS_ShapeInfo& aSIDS=myDS->ChangeShapeInfo(nV);
        Bnd_Box& aBoxDS=aSIDS.ChangeBox();
        BRepBndLib::Add(aV, aBoxDS);
        aBoxDS.SetGap(aBoxDS.GetGap() + Precision::Confusion());
2067
2068
      }
    }
2069
2070
2071
2072
  }
}

//=======================================================================
2073
//function : ProcessExistingPaveBlocks
2074
2075
//purpose  : 
//=======================================================================
2076
void BOPAlgo_PaveFiller::ProcessExistingPaveBlocks
2077
    (const Standard_Integer theInt,
emv's avatar
emv committed
2078
     const BOPDS_IndexedMapOfPaveBlock& aMPBOnIn,
2079
     const BOPCol_DataMapOfIntegerListOfInteger& aDMBV,
2080
2081
2082
2083
     BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
     BOPCol_DataMapOfShapeInteger& aMVI,
     BOPDS_MapOfPaveBlock& aMPB)
{
2084
2085
2086
2087
  if (aDMBV.IsEmpty()) {
    return;
  }
  //
2088
  Standard_Real aT, dummy;
2089
2090
2091
  Standard_Integer i, nV, nE, iC, aNbPB, iFlag;
  BOPCol_ListIteratorOfListOfInteger aItLI;
  BOPCol_DataMapIteratorOfDataMapOfIntegerListOfInteger aItBV;
2092
  //
2093
  BOPDS_VectorOfInterfFF& aFFs = myDS->InterfFF();
2094
  BOPDS_InterfFF& aFF = aFFs(theInt);
2095
2096
2097
2098
2099
2100
2101
2102
  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();
2103
    //
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
    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)) {
2114
2115
2116
        continue;
      }
      //
2117
2118
2119
2120
2121
2122
2123
2124
2125
      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;
        }
2126
2127
2128
        if (myDS->ShapeInfo(aPB->OriginalEdge()).HasFlag()) { // skip degenerated edges
          continue;
        }
2129
2130
2131
2132
2133
2134
2135
2136
2137
        //
        nE = aPB->Edge();
        const BOPDS_ShapeInfo& aSIE = myDS->ShapeInfo(nE);
        const Bnd_Box& aBoxE = aSIE.Box();
        //
        if (aBoxV.IsOut(aBoxE)) {
          continue;
        }
        //
2138
2139
        const TopoDS_Edge& aE = *(TopoDS_Edge*)&aSIE.Shape();
        //
2140
        iFlag = myContext->ComputeVE(aV, aE, aT, dummy, myFuzzyValue);
2141
2142
        if (!iFlag) {
          aMPB.Add(aPB);
2143
          PreparePostTreatFF(theInt, iC, aPB, aMSCPB, aMVI, aLPBC);
2144
2145
2146
2147
2148
2149
2150
2151
2152
        }
      }
    }
  }
}
//=======================================================================
//function : UpdateExistingPaveBlocks
//purpose  : 
//=======================================================================
2153
2154
2155
2156
2157
void BOPAlgo_PaveFiller::UpdateExistingPaveBlocks
  (const Handle(BOPDS_PaveBlock)& aPBf,
   BOPDS_ListOfPaveBlock& aLPB,
   const Standard_Integer nF1,
   const Standard_Integer nF2) 
2158
{
2159
2160
2161
2162
  if (!aLPB.Extent()) {
    return;
  }
  //
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
  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();
  //
2177
  // 1. Remove old pave blocks
omy's avatar
omy committed
2178
  const Handle(BOPDS_CommonBlock)& aCB1 = myDS->CommonBlock(aPBf);
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
  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;
      }
    }
  }
  //
2203
  // 2. Update pave blocks
2204
  if (bCB) {
2205
    //create new common blocks
2206
    BOPDS_ListOfPaveBlock aLPBNew;
2207
2208
2209
    const BOPCol_ListOfInteger& aFaces = aCB1->Faces();
    aIt.Initialize(aLPB);
    for (; aIt.More(); aIt.Next()) {
2210
      const Handle(BOPDS_PaveBlock)& aPBValue = aIt.Value();
2211
2212
2213
2214
2215
2216
2217
2218
      //
      aCB = new BOPDS_CommonBlock;
      aIt1.Initialize(aLPB1);
      for (; aIt1.More(); aIt1.Next()) {
        aPB2 = aIt1.Value();
        nE = aPB2->OriginalEdge();
        //
        aPB2n = new BOPDS_PaveBlock;
2219
2220
2221
        aPB2n->SetPave1(aPBValue->Pave1());
        aPB2n->SetPave2(aPBValue->Pave2());
        aPB2n->SetEdge(aPBValue->Edge());
2222
2223
        aPB2n->SetOriginalEdge(nE);
        aCB->AddPaveBlock(aPB2n);
omy's avatar
omy committed
2224
        myDS->SetCommonBlock(aPB2n, aCB);
2225
2226
        myDS->ChangePaveBlocks(nE).Append(aPB2n);
      }
2227
      aCB->SetFaces(aFaces);
2228
2229
      myDS->SortPaveBlocks(aCB);
      //
2230
2231
      const Handle(BOPDS_PaveBlock)& aPBNew = aCB->PaveBlocks().First();
      aLPBNew.Append(aPBNew);
2232
    }
2233
2234
    //
    aLPB = aLPBNew;
2235
  } 
2236
2237
2238
2239
2240
2241
2242
  else {
    nE = aPBf->OriginalEdge();
    BOPDS_ListOfPaveBlock& aLPBE = myDS->ChangePaveBlocks(nE);
    aIt.Initialize(aLPB);
    for (; aIt.More(); aIt.Next()) {
      aPB = aIt.Value();
      aLPBE.Append(aPB);
2243
2244
2245
2246
2247
2248
2249
2250
    }
  }
  //
  Standard_Boolean bIn1, bIn2;
  //
  bIn1 = aMPBOn1.Contains(aPBf) || aMPBIn1.Contains(aPBf);
  bIn2 = aMPBOn2.Contains(aPBf) || aMPBIn2.Contains(aPBf);
  //
2251
2252
2253
2254
  if (bIn1 && bIn2) {
    return;
  }
  //
2255
  // 3. Check new pave blocks for coincidence 
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
  //    with the opposite face.
  //    In case of coincidence create common blocks
  Standard_Integer nF;
  //
  nF = bIn1 ? nF2 : nF1;
  const TopoDS_Face& aF = *(TopoDS_Face*)&myDS->Shape(nF);
  BOPDS_IndexedMapOfPaveBlock& aMPBIn = bIn1 ? aMPBIn2 : aMPBIn1;
  //
  aIt.Initialize(aLPB);
  for (; aIt.More(); aIt.Next()) {
2266
2267
    Handle(BOPDS_PaveBlock)& aPBChangeValue = aIt.ChangeValue();
    const TopoDS_Edge& aE = *(TopoDS_Edge*)&myDS->Shape(aPBChangeValue->Edge());
2268
2269
2270
2271
    //
    IntTools_EdgeFace anEF;
    anEF.SetEdge(aE);
    anEF.SetFace(aF);
2272
    anEF.SetFuzzyValue(myFuzzyValue);
2273
    anEF.SetRange(aPBChangeValue->Pave1().Parameter(), aPBChangeValue->Pave2().Parameter());
2274
2275
    anEF.SetContext(myContext);
    anEF.Perform();
2276
    //
2277
2278
2279
2280
2281
    const IntTools_SequenceOfCommonPrts& aCPrts=anEF.CommonParts();
    if (aCPrts.Length() == 1) {
      Standard_Boolean bCoinc = (aCPrts(1).Type() == TopAbs_EDGE);
      if (bCoinc) {
        if (bCB) {
2282
          aCB = myDS->CommonBlock(aPBChangeValue);
2283
2284
        } else {
          aCB = new BOPDS_CommonBlock;
2285
2286
          aCB->AddPaveBlock(aPBChangeValue);
          myDS->SetCommonBlock(aPBChangeValue, aCB);
2287
2288
2289
        }
        aCB->AddFace(nF);
        //
2290
        aMPBIn.Add(aPBChangeValue);
2291
      }
2292
2293
2294
2295
2296
2297
2298
    }
  }
}
//=======================================================================
// function: PutClosingPaveOnCurve
// purpose:
//=======================================================================
2299
void BOPAlgo_PaveFiller::PutClosingPaveOnCurve(BOPDS_Curve& aNC)
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
{
  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  : 
//=======================================================================
2354
void BOPAlgo_PaveFiller::PreparePostTreatFF
2355
    (const Standard_Integer aInt,
2356
     const Standard_Integer aCur,
2357
2358
2359
     const Handle(BOPDS_PaveBlock)& aPB,
     BOPDS_IndexedDataMapOfShapeCoupleOfPaveBlocks& aMSCPB,
     BOPCol_DataMapOfShapeInteger& aMVI,
2360
     BOPDS_ListOfPaveBlock& aLPBC)
2361
{
2362
  Standard_Integer nV1, nV2;
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
  //
  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);
2373
  aCPB.SetIndex(aCur);
2374
2375
2376
2377
2378
2379
2380
  aCPB.SetPaveBlock1(aPB);
  //
  aMSCPB.Add(aE, aCPB);
  aMVI.Bind(aV1, nV1);
  aMVI.Bind(aV2, nV2);
}

2381
2382
2383
2384
//=======================================================================
//function : CheckPlanes
//purpose  : 
//=======================================================================
2385
2386
2387
Standard_Boolean BOPAlgo_PaveFiller::CheckPlanes
  (const Standard_Integer nF1,
   const Standard_Integer nF2)const
2388
2389
{
  Standard_Boolean bToIntersect;
2390
  Standard_Integer i, nV2, iCnt;
2391
2392
  BOPCol_MapIteratorOfMapOfInteger aIt;
  //
2393
  bToIntersect=Standard_False;
2394
  //
2395
2396
  const BOPDS_FaceInfo& aFI1=myDS->ChangeFaceInfo(nF1);
  const BOPDS_FaceInfo& aFI2=myDS->ChangeFaceInfo(nF2);
2397
  //
2398
2399
  const BOPCol_MapOfInteger& aMVIn1=aFI1.VerticesIn();
  const BOPCol_MapOfInteger& aMVOn1=aFI1.VerticesOn();
2400
  //
2401
2402
2403
2404
2405
2406
  iCnt=0;
  for (i=0; (i<2 && !bToIntersect); ++i) {
    const BOPCol_MapOfInteger& aMV2=(!i) ? aFI2.VerticesIn() 
      : aFI2.VerticesOn();
    //
    aIt.Initialize(aMV2);
2407
    for (; aIt.More(); aIt.Next()) {
2408
2409
      nV2=aIt.Value();
      if (aMVIn1.Contains(nV2) || aMVOn1.Contains(nV2)) {
2410
2411
2412
2413
2414
        ++iCnt;
        if (iCnt>1) {
          bToIntersect=!bToIntersect;
          break;
        }
2415
2416
2417
2418
2419
2420
      }
    }
  }
  //
  return bToIntersect;
}
2421
//=======================================================================
2422
2423
2424
//function : UpdatePaveBlocks
//purpose  : 
//=======================================================================
2425
void BOPAlgo_PaveFiller::UpdatePaveBlocks
2426
(const BOPCol_DataMapOfIntegerInteger& aDMNewSD)
2427
{
2428
  if (aDMNewSD.IsEmpty()) {
2429
2430
2431
2432
2433
2434
2435
2436
    return;
  }
  //
  Standard_Integer nSp, aNbPBP, nV[2], i, j;
  Standard_Real aT[2];
  Standard_Boolean bCB, bRebuild;
  BOPDS_ListIteratorOfListOfPaveBlock aItPB;
  BOPDS_MapOfPaveBlock aMPB;
2437
  BOPCol_MapOfInteger aMicroEdges;
2438
  //
2439
  BOPDS_VectorOfListOfPaveBlock& aPBP = myDS->ChangePaveBlocksPool();
2440
  aNbPBP = aPBP.Extent();
2441
2442
  for (i = 0; i < aNbPBP; ++i) {
    BOPDS_ListOfPaveBlock& aLPB = aPBP(i);
2443
2444
2445
    //
    aItPB.Initialize(aLPB);
    for (; aItPB.More(); aItPB.Next()) {
2446
2447
      Handle(BOPDS_PaveBlock) aPB = aItPB.Value();
      const Handle(BOPDS_CommonBlock)& aCB = myDS->CommonBlock(aPB);
2448
2449
      bCB = !aCB.IsNull();
      if (bCB) {
2450
        aPB = aCB->PaveBlock1();
2451
2452
2453
2454
2455
2456
      }
      //
      if (aMPB.Add(aPB)) {
        bRebuild = Standard_False;
        aPB->Indices(nV[0], nV[1]);
        aPB->Range(aT[0], aT[1]);
2457
2458
        // remember the fact if the edge had different vertices before substitution
        Standard_Boolean wasRegularEdge = (nV[0] != nV[1]);
2459
2460
        //
        for (j = 0; j < 2; ++j) {
2461
          if (aDMNewSD.IsBound(nV[j])) {
2462
2463
            BOPDS_Pave aPave;
            //
2464
            nV[j] = aDMNewSD.Find(nV[j]);
2465
2466
2467
2468
2469
2470
            aPave.SetIndex(nV[j]);
            aPave.SetParameter(aT[j]);
            //
            bRebuild = Standard_True;
            if (!j) {
              aPB->SetPave1(aPave);
2471
2472
            }
            else {
2473
2474
2475
2476
2477
2478
              aPB->SetPave2(aPave);
            }
          }
        }
        //
        if (bRebuild) {
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
          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;
            }
          }
2493
          nSp = SplitEdge(aPB->OriginalEdge(), nV[0], aT[0], nV[1], aT[1]);
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
          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();
2505
2506
2507

  if (aMicroEdges.Extent())
    RemovePaveBlocks(aMicroEdges);
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
//=======================================================================
//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();
      }
    }
  }
}

2586
//=======================================================================
2587
2588
2589
2590
//function : ToleranceFF
//purpose  : Computes the TolFF according to the tolerance value and 
//           types of the faces.
//=======================================================================
2591
2592
  void ToleranceFF(const BRepAdaptor_Surface& aBAS1,
                   const BRepAdaptor_Surface& aBAS2,
2593
2594
2595
2596
2597
                   Standard_Real& aTolFF)
{
  Standard_Real aTol1, aTol2;
  Standard_Boolean isAna1, isAna2;
  //
2598
2599
  aTol1 = aBAS1.Tolerance();
  aTol2 = aBAS2.Tolerance();
2600
2601
  aTolFF = Max(aTol1, aTol2);
  //
2602
2603
2604
2605
2606
  isAna1 = (aBAS1.GetType() == GeomAbs_Plane ||
            aBAS1.GetType() == GeomAbs_Cylinder ||
            aBAS1.GetType() == GeomAbs_Cone ||
            aBAS1.GetType() == GeomAbs_Sphere ||
            aBAS1.GetType() == GeomAbs_Torus);
2607
  //
2608
2609
2610
2611
2612
  isAna2 = (aBAS2.GetType() == GeomAbs_Plane ||
            aBAS2.GetType() == GeomAbs_Cylinder ||
            aBAS2.GetType() == GeomAbs_Cone ||
            aBAS2.GetType() == GeomAbs_Sphere ||
            aBAS2.GetType() == GeomAbs_Torus);
2613
  //
2614
2615
2616
  if (!isAna1 || !isAna2) {
    aTolFF =  Max(aTolFF, 5.e-6);
  }
2617
}
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
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
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
//=======================================================================
//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;
2744
  BOPCol_MapOfInteger aMVIToReduce;
2745
  //
2746
  // 1. iterate on all sections F-F
2747
2748
  Standard_Integer aNb = aFFs.Extent(), i;
  for (i = 0; i < aNb; ++i) {
2749
    BOPDS_InterfFF& aFF = aFFs(i);
2750
2751
    Standard_Real aTolR3D = aFF.TolR3D();
    Standard_Real aTolReal = aFF.TolReal();
2752
    Standard_Boolean bToReduce = aTolReal < aTolR3D;
2753
      // tolerance of intersection has been increased, so process this intersection
2754
    BOPDS_VectorOfCurve& aVNC = aFF.ChangeCurves();
2755
2756
      Standard_Integer aNbC = aVNC.Extent(), k;
      for (k = 0; k < aNbC; ++k) {
2757
2758
      BOPDS_Curve& aNC = aVNC(k);
      BOPDS_ListOfPaveBlock& aLPB = aNC.ChangePaveBlocks();
2759
        BOPDS_ListIteratorOfListOfPaveBlock aItLPB(aLPB);
2760
      for (; aItLPB.More(); ) {
2761
2762
          const Handle(BOPDS_PaveBlock)& aPB = aItLPB.Value();
          Standard_Integer nE;
2763
        if (!aPB->HasEdge(nE)) {
2764
          aLPB.Remove(aItLPB);
2765
2766
            continue;
          }
2767
2768
2769
        //
        Standard_Boolean bIsReduced = Standard_False;
        if (bToReduce && (aPB->OriginalEdge() < 0)) {
2770
2771
2772
2773
2774
          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);
2775
            bIsReduced = Standard_True;
2776
          }
2777
2778
        }
        //
2779
2780
          // fill in the map vertex index - pave blocks
          for (Standard_Integer j=0; j < 2; j++) {
2781
          Standard_Integer nV = (j == 0 ? aPB->Pave1().Index() : aPB->Pave2().Index());
2782
2783
2784
2785
            BOPDS_ListOfPaveBlock *pPBList = aMVIPBs.ChangeSeek(nV);
            if (!pPBList) {
              pPBList = &aMVIPBs.ChangeFromIndex(aMVIPBs.Add(nV, BOPDS_ListOfPaveBlock()));
            }
2786
2787
2788
2789
2790
          pPBList->Append(aPB);
          if (bIsReduced) {
            aMVIToReduce.Add(nV);
          }
        }
2791
        aItLPB.Next();
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
      }
    }
  }
  //
  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;
2831
2832
2833
2834
2835
          }
        }
      }
    }
  }
2836
2837
  //
  // 2.2 reduce tolerances if possible
2838
2839
2840
  aNb = aMVIPBs.Extent();
  for (i = 1; i <= aNb; ++i) {
    Standard_Integer nV = aMVIPBs.FindKey(i);
2841
2842
2843
2844
    if (!aMVIToReduce.Contains(nV)) {
      continue;
    }
    //
2845
    const TopoDS_Vertex& aV = TopoDS::Vertex(myDS->Shape(nV));
2846
2847
2848
2849
2850
2851
2852
    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;
    }
2853
2854
    //
    // compute the maximal distance from the vertex to the adjacent edges
2855
2856
    gp_Pnt aP = BRep_Tool::Pnt(aV);
    //
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
    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;
      }
    }
2872
    //
2873
2874
2875
2876
2877
    if (aMaxTol < aTolV) {
      reinterpret_cast<BRep_TVertex*>(aV.TShape().operator->())->Tolerance(aMaxTol);
    }
  }
}