686d9073f52e530104790194fa2c71ac4356e43c
[occt.git] / src / BOPAlgo / BOPAlgo_PaveFiller_1.cxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 //                         EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 //
7 // This file is part of Open CASCADE Technology software library.
8 //
9 // This library is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU Lesser General Public License version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
14 //
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
17
18
19 #include <Bnd_Box.hxx>
20 #include <BOPAlgo_PaveFiller.hxx>
21 #include <BOPAlgo_SectionAttribute.hxx>
22 #include <BOPAlgo_Tools.hxx>
23 #include <BOPCol_DataMapOfIntegerInteger.hxx>
24 #include <BOPCol_DataMapOfIntegerListOfInteger.hxx>
25 #include <BOPCol_ListOfShape.hxx>
26 #include <BOPCol_MapOfInteger.hxx>
27 #include <BOPDS_Curve.hxx>
28 #include <BOPDS_DS.hxx>
29 #include <BOPDS_Iterator.hxx>
30 #include <BOPDS_PaveBlock.hxx>
31 #include <BOPDS_ShapeInfo.hxx>
32 #include <BOPDS_VectorOfInterfVV.hxx>
33 #include <BOPTools_AlgoTools.hxx>
34 #include <BRepBndLib.hxx>
35 #include <BRep_TVertex.hxx>
36 #include <BRep_Tool.hxx>
37 #include <gp_Pnt.hxx>
38 #include <IntTools_Context.hxx>
39 #include <NCollection_BaseAllocator.hxx>
40 #include <Precision.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Face.hxx>
43 #include <TopoDS_Vertex.hxx>
44
45 //=======================================================================
46 // function: PerformVV
47 // purpose: 
48 //=======================================================================
49 void BOPAlgo_PaveFiller::PerformVV() 
50 {
51   Standard_Boolean bWithSubShape;
52   Standard_Integer n1, n2, iFlag, aSize, k, aNbBlocks;
53   Handle(NCollection_BaseAllocator) aAllocator;
54   //
55   myErrorStatus=0;
56   //
57   myIterator->Initialize(TopAbs_VERTEX, TopAbs_VERTEX);
58   aSize=myIterator->ExpectedLength();
59   if (!aSize) {
60     return; 
61   }
62   //
63   BOPDS_VectorOfInterfVV& aVVs=myDS->InterfVV();
64   aVVs.SetIncrement(aSize);
65   //
66   //-----------------------------------------------------scope f
67   aAllocator=
68     NCollection_BaseAllocator::CommonBaseAllocator();
69   BOPCol_IndexedDataMapOfIntegerListOfInteger aMILI(100, aAllocator);
70   BOPCol_DataMapOfIntegerListOfInteger aMBlocks(100, aAllocator);
71   //
72   // 1. Map V/LV
73   for (; myIterator->More(); myIterator->Next()) {
74     myIterator->Value(n1, n2, bWithSubShape);
75     //
76     const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(n1))); 
77     const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(n2))); 
78     //
79     iFlag=BOPTools_AlgoTools::ComputeVV(aV1, aV2, myFuzzyValue);
80     if (!iFlag) {
81       BOPAlgo_Tools::FillMap(n1, n2, aMILI, aAllocator);
82     }
83   } 
84   //
85   // 2. Make blocks
86   BOPAlgo_Tools::MakeBlocksCnx(aMILI, aMBlocks, aAllocator);
87   //
88   // 3. Make vertices
89   aNbBlocks=aMBlocks.Extent();
90   for (k=0; k<aNbBlocks; ++k) {
91     const BOPCol_ListOfInteger& aLI=aMBlocks.Find(k);
92     //
93     MakeSDVertices(aLI);
94   }
95   //
96   BOPCol_DataMapIteratorOfDataMapOfIntegerInteger aItDMII;
97   //
98   BOPCol_DataMapOfIntegerInteger& aDMII=myDS->ShapesSD();
99   aItDMII.Initialize(aDMII);
100   for (; aItDMII.More(); aItDMII.Next()) {
101     n1=aItDMII.Key();
102     myDS->InitPaveBlocksForVertex(n1);
103   }
104   //
105   //-----------------------------------------------------scope t
106   aMBlocks.Clear();
107   aMILI.Clear();
108 }
109
110 //=======================================================================
111 // function: PerformVV
112 // purpose: 
113 //=======================================================================
114 Standard_Integer BOPAlgo_PaveFiller::MakeSDVertices
115    (const BOPCol_ListOfInteger& theVertIndices,
116     const Standard_Boolean theAddInterfs)
117 {
118   TopoDS_Vertex aVSD, aVn;
119   Standard_Integer nSD = -1;
120   BOPCol_ListIteratorOfListOfInteger aItLI(theVertIndices);
121   BOPCol_ListOfShape aLV;
122   for (; aItLI.More(); aItLI.Next()) {
123     Standard_Integer nX = aItLI.Value(), nSD1;
124     if (myDS->HasShapeSD(nX, nSD1)) {
125       const TopoDS_Shape& aVSD1 = myDS->Shape(nSD1);
126       if (nSD == -1) {
127         aVSD = TopoDS::Vertex(aVSD1);
128         nSD = nSD1;
129       }
130       else {
131         aLV.Append(aVSD1);
132       }
133     }
134     const TopoDS_Shape& aV = myDS->Shape(nX);
135     aLV.Append(aV);
136   }
137   BOPTools_AlgoTools::MakeVertex(aLV, aVn);
138   Standard_Integer nV;
139   if (nSD != -1) {
140     // update old SD vertex with new value
141     Handle(BRep_TVertex)& aTVertex =
142       reinterpret_cast<Handle(BRep_TVertex)&>(const_cast<Handle(TopoDS_TShape)&>(aVSD.TShape()));
143     aTVertex->Pnt(BRep_Tool::Pnt(aVn));
144     aTVertex->Tolerance(BRep_Tool::Tolerance(aVn));
145     aVn = aVSD;
146     nV = nSD;
147   }
148   else {
149     // Append new vertex to the DS
150     BOPDS_ShapeInfo aSIn;
151     aSIn.SetShapeType(TopAbs_VERTEX);
152     aSIn.SetShape(aVn);
153     nV = myDS->Append(aSIn);
154   }
155   BOPDS_ShapeInfo& aSIDS = myDS->ChangeShapeInfo(nV);
156   Bnd_Box& aBox = aSIDS.ChangeBox();
157   BRepBndLib::Add(aVn, aBox);
158   aBox.SetGap(aBox.GetGap() + Precision::Confusion());
159   //
160   // Fill ShapesSD
161   BOPDS_VectorOfInterfVV& aVVs = myDS->InterfVV();
162   if (theAddInterfs)
163     aVVs.SetIncrement(theVertIndices.Extent());
164   //
165   aItLI.Initialize(theVertIndices);
166   for (; aItLI.More(); aItLI.Next()) {
167     Standard_Integer n1 = aItLI.Value();
168     myDS->AddShapeSD(n1, nV);
169     //
170     if (theAddInterfs) {
171       BOPCol_ListIteratorOfListOfInteger aItLI2 = aItLI;
172       aItLI2.Next();
173       for (; aItLI2.More(); aItLI2.Next()) {
174         Standard_Integer n2 = aItLI2.Value();
175         //
176         myDS->AddInterf(n1, n2);
177         BOPDS_InterfVV& aVV = aVVs.Append1();
178         //
179         aVV.SetIndices(n1, n2);
180         aVV.SetIndexNew(nV);
181       }
182     }
183   }
184   return nV;
185 }