291edb2de862bcd14befeabe32c1fa90bd026fb3
[occt.git] / src / BOPAlgo / BOPAlgo_PaveFiller_4.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 <BOPAlgo_PaveFiller.hxx>
20 #include <BOPAlgo_SectionAttribute.hxx>
21 #include <BOPCol_MapOfInteger.hxx>
22 #include <BOPCol_NCVector.hxx>
23 #include <BOPCol_Parallel.hxx>
24 #include <BOPDS_Curve.hxx>
25 #include <BOPDS_DS.hxx>
26 #include <BOPDS_FaceInfo.hxx>
27 #include <BOPDS_Interf.hxx>
28 #include <BOPDS_Iterator.hxx>
29 #include <BOPDS_MapOfPaveBlock.hxx>
30 #include <BOPDS_PaveBlock.hxx>
31 #include <BOPDS_SubIterator.hxx>
32 #include <BOPDS_VectorOfInterfVF.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepBndLib.hxx>
36 #include <gp_Pnt.hxx>
37 #include <IntTools_Context.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Vertex.hxx>
40
41 //=======================================================================
42 //class    : BOPAlgo_VertexFace
43 //purpose  : 
44 //=======================================================================
45 class BOPAlgo_VertexFace : public BOPAlgo_Algo {
46  public:
47   DEFINE_STANDARD_ALLOC
48
49   BOPAlgo_VertexFace() : 
50     BOPAlgo_Algo(),
51     myIV(-1), myIF(-1),
52     myFlag(-1), myT1(-1.),  myT2(-1.), myTolVNew(-1.) {
53   }
54   //
55   virtual ~BOPAlgo_VertexFace(){
56   }
57   //
58   void SetIndices(const Standard_Integer nV,
59                   const Standard_Integer nF) {
60     myIV=nV;
61     myIF=nF;
62   }
63   //
64   void Indices(Standard_Integer& nV,
65                Standard_Integer& nF) const {
66     nV=myIV;
67     nF=myIF;
68   }
69   //
70   void SetVertex(const TopoDS_Vertex& aV) {
71     myV=aV;
72   }
73   //
74   const TopoDS_Vertex& Vertex()const {
75     return myV;
76   }
77   //
78   void SetFace(const TopoDS_Face& aF) {
79     myF=aF;
80   }
81   //
82   const TopoDS_Face& Face()const {
83     return myF;
84   }
85   //
86   Standard_Integer Flag()const {
87     return myFlag;
88   }
89   //
90   void Parameters(Standard_Real& aT1,
91                   Standard_Real& aT2)const {
92     aT1=myT1;
93     aT2=myT2;
94   }
95   //
96   Standard_Real VertexNewTolerance()const {
97     return myTolVNew;
98   }
99   //
100   void SetContext(const Handle(IntTools_Context)& aContext) {
101     myContext=aContext;
102   }
103   //
104   const Handle(IntTools_Context)& Context()const {
105     return myContext;
106   }
107   //
108   virtual void Perform() {
109     BOPAlgo_Algo::UserBreak();
110     myFlag=myContext->ComputeVF(myV, myF, myT1, myT2, myTolVNew, myFuzzyValue);
111   }
112   //
113  protected:
114   Standard_Integer myIV;
115   Standard_Integer myIF;
116   Standard_Integer myFlag;
117   Standard_Real myT1;
118   Standard_Real myT2;
119   Standard_Real myTolVNew;
120   TopoDS_Vertex myV;
121   TopoDS_Face myF;
122   Handle(IntTools_Context) myContext;
123 };
124 //=======================================================================
125 typedef BOPCol_NCVector<BOPAlgo_VertexFace>
126   BOPAlgo_VectorOfVertexFace; 
127 //
128 typedef BOPCol_ContextFunctor 
129   <BOPAlgo_VertexFace,
130   BOPAlgo_VectorOfVertexFace,
131   Handle(IntTools_Context), 
132   IntTools_Context> BOPAlgo_VertexFaceFunctor;
133 //
134 typedef BOPCol_ContextCnt 
135   <BOPAlgo_VertexFaceFunctor,
136   BOPAlgo_VectorOfVertexFace,
137   Handle(IntTools_Context)> BOPAlgo_VertexFaceCnt;
138 //
139 //=======================================================================
140 // function: PerformVF
141 // purpose: 
142 //=======================================================================
143 void BOPAlgo_PaveFiller::PerformVF()
144 {
145   myIterator->Initialize(TopAbs_VERTEX, TopAbs_FACE);
146   Standard_Integer iSize = myIterator->ExpectedLength();
147   //
148   Standard_Integer nV, nF;
149   //
150   if (myGlue == BOPAlgo_GlueFull) {
151     // there is no need to intersect vertices with faces in this mode
152     // just initialize FaceInfo for all faces
153     for (; myIterator->More(); myIterator->Next()) {
154       myIterator->Value(nV, nF);
155       if (!myDS->IsSubShape(nV, nF)) {
156         myDS->ChangeFaceInfo(nF);
157       }
158     }
159     return;
160   }
161   //
162   BOPDS_VectorOfInterfVF& aVFs = myDS->InterfVF();
163   if (!iSize) {
164     iSize = 10;
165     aVFs.SetIncrement(iSize);
166     //
167     TreatVerticesEE();
168     return;
169   }
170   //
171   Standard_Integer nVSD, iFlag, nVx, aNbVF, k;
172   Standard_Real aT1, aT2;
173   BOPAlgo_VectorOfVertexFace aVVF; 
174   //
175   aVFs.SetIncrement(iSize);
176   //
177   for (; myIterator->More(); myIterator->Next()) {
178     myIterator->Value(nV, nF);
179     //
180     if (myDS->IsSubShape(nV, nF)) {
181       continue;
182     }
183     //
184     myDS->ChangeFaceInfo(nF);
185     if (myDS->HasInterfShapeSubShapes(nV, nF)) {
186       continue;
187     }
188     //
189     nVx=nV;
190     if (myDS->HasShapeSD(nV, nVSD)) {
191       nVx=nVSD;
192     }
193     //
194     const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&myDS->Shape(nVx))); 
195     const TopoDS_Face& aF=(*(TopoDS_Face *)(&myDS->Shape(nF))); 
196     //
197     BOPAlgo_VertexFace& aVertexFace=aVVF.Append1();
198     //
199     aVertexFace.SetIndices(nV, nF);
200     aVertexFace.SetVertex(aV);
201     aVertexFace.SetFace(aF);
202     aVertexFace.SetFuzzyValue(myFuzzyValue);
203     aVertexFace.SetProgressIndicator(myProgressIndicator);
204   }//for (; myIterator->More(); myIterator->Next()) {
205   //
206   aNbVF=aVVF.Extent();
207   //================================================================
208   BOPAlgo_VertexFaceCnt::Perform(myRunParallel, aVVF, myContext);
209   //================================================================
210   //
211   for (k=0; k < aNbVF; ++k) {
212     const BOPAlgo_VertexFace& aVertexFace=aVVF(k);
213     // 
214     iFlag=aVertexFace.Flag();
215     if (iFlag) {
216       continue;
217     }
218     //
219     aVertexFace.Indices(nV, nF);
220     aVertexFace.Parameters(aT1, aT2);
221     // 1
222     BOPDS_InterfVF& aVF=aVFs.Append1();
223     aVF.SetIndices(nV, nF);
224     aVF.SetUV(aT1, aT2);
225     // 2
226     myDS->AddInterf(nV, nF);
227     //
228     // 3 update vertex V/F if necessary
229     Standard_Real aTolVNew = aVertexFace.VertexNewTolerance();
230     nVx=UpdateVertex(nV, aTolVNew);
231     //
232     // 4
233     if (myDS->IsNewShape(nVx)) {
234       aVF.SetIndexNew(nVx);
235     }
236     // 5 update FaceInfo
237     BOPDS_FaceInfo& aFI=myDS->ChangeFaceInfo(nF);
238     BOPCol_MapOfInteger& aMVIn=aFI.ChangeVerticesIn();
239     aMVIn.Add(nVx);
240   }//for (k=0; k < aNbVF; ++k) {
241   //
242   TreatVerticesEE();
243 }
244 //=======================================================================
245 //function : TreatVerticesEE
246 //purpose  : 
247 //=======================================================================
248 void BOPAlgo_PaveFiller::TreatVerticesEE()
249 {
250   Standard_Integer i, aNbS,aNbEEs, nF, nV, iFlag;
251   Standard_Real aT1, aT2, dummy;
252   BOPCol_ListIteratorOfListOfInteger aItLI;
253   Handle(NCollection_BaseAllocator) aAllocator;
254   //
255   aAllocator=
256     NCollection_BaseAllocator::CommonBaseAllocator();
257   BOPCol_ListOfInteger aLIV(aAllocator), aLIF(aAllocator);
258   BOPCol_MapOfInteger aMI(100, aAllocator);
259   BOPDS_MapOfPaveBlock aMPBF(100, aAllocator);
260   //
261   aNbS=myDS->NbSourceShapes();
262   //
263   BOPDS_VectorOfInterfEE& aEEs=myDS->InterfEE();
264   aNbEEs=aEEs.Extent();
265   for (i=0; i<aNbEEs; ++i) {
266     BOPDS_InterfEE& aEE=aEEs(i);
267     if (aEE.HasIndexNew()) {
268       nV=aEE.IndexNew();
269       if (aMI.Add(nV)) {
270         aLIV.Append(nV);
271       }   
272     }   
273   }
274   if (!aLIV.Extent()) {
275     aAllocator.Nullify();
276     return;
277   }
278   //
279   aNbS=myDS->NbSourceShapes();
280   for (nF=0; nF<aNbS; ++nF) {
281     const BOPDS_ShapeInfo& aSI=myDS->ShapeInfo(nF);
282     if (aSI.ShapeType()==TopAbs_FACE) {
283       aLIF.Append(nF);
284     }
285   }
286   if (!aLIF.Extent()) {
287     aAllocator.Nullify();
288     return;
289   }
290   //-------------------------------------------------------------
291   BOPDS_VectorOfInterfVF& aVFs=myDS->InterfVF();
292   //
293   BOPDS_SubIterator aIt(aAllocator);
294   //
295   aIt.SetDS(myDS);
296   aIt.SetSubSet1(aLIF);
297   aIt.SetSubSet2(aLIV);
298   aIt.Prepare();
299   aIt.Initialize();
300   for (; aIt.More(); aIt.Next()) {
301     aIt.Value(nV, nF);
302     //
303     BOPDS_FaceInfo& aFI=myDS->ChangeFaceInfo(nF);
304     const BOPCol_MapOfInteger& aMVOn=aFI.VerticesOn();
305     //
306     if (!aMVOn.Contains(nV)) {
307       const TopoDS_Vertex& aV=(*(TopoDS_Vertex *)(&myDS->Shape(nV))); 
308       const TopoDS_Face& aF=(*(TopoDS_Face *)(&myDS->Shape(nF))); 
309       iFlag = myContext->ComputeVF(aV, aF, aT1, aT2, dummy, myFuzzyValue);
310       if (!iFlag) {
311         // 1
312         BOPDS_InterfVF& aVF=aVFs.Append1();
313         i=aVFs.Extent()-1;
314         aVF.SetIndices(nV, nF);
315         aVF.SetUV(aT1, aT2);
316         // 2
317         myDS->AddInterf(nV, nF);
318         //
319         BOPCol_MapOfInteger& aMVIn=aFI.ChangeVerticesIn();
320         aMVIn.Add(nV);
321       }
322     }
323   }
324 }