0028599: Replacement of old Boolean operations with new ones in BRepProj_Projection...
[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_Tools.hxx>
22 #include <BOPAlgo_Alerts.hxx>
23 #include <BOPDS_DS.hxx>
24 #include <BOPDS_Iterator.hxx>
25 #include <BOPDS_PaveBlock.hxx>
26 #include <BOPDS_ShapeInfo.hxx>
27 #include <BOPDS_VectorOfInterfVV.hxx>
28 #include <BOPTools_AlgoTools.hxx>
29 #include <BRep_Builder.hxx>
30 #include <BRep_TVertex.hxx>
31 #include <BRep_Tool.hxx>
32 #include <gp_Pnt.hxx>
33 #include <IntTools_Context.hxx>
34 #include <NCollection_BaseAllocator.hxx>
35 #include <Precision.hxx>
36 #include <TColStd_DataMapOfIntegerInteger.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Face.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <TopoDS_Compound.hxx>
41 #include <TopTools_ListOfShape.hxx>
42
43 //=======================================================================
44 // function: PerformVV
45 // purpose: 
46 //=======================================================================
47 void BOPAlgo_PaveFiller::PerformVV() 
48 {
49   Standard_Integer n1, n2, iFlag, aSize;
50   Handle(NCollection_BaseAllocator) aAllocator;
51   //
52   myIterator->Initialize(TopAbs_VERTEX, TopAbs_VERTEX);
53   aSize=myIterator->ExpectedLength();
54   if (!aSize) {
55     return; 
56   }
57   //
58   BOPDS_VectorOfInterfVV& aVVs=myDS->InterfVV();
59   aVVs.SetIncrement(aSize);
60   //
61   //-----------------------------------------------------scope f
62   aAllocator=
63     NCollection_BaseAllocator::CommonBaseAllocator();
64   NCollection_IndexedDataMap<Standard_Integer, TColStd_ListOfInteger>aMILI(100, aAllocator);
65   NCollection_List<TColStd_ListOfInteger> aMBlocks(aAllocator);
66   //
67   // 1. Map V/LV
68   for (; myIterator->More(); myIterator->Next()) {
69     myIterator->Value(n1, n2);
70     //
71     const TopoDS_Vertex& aV1=(*(TopoDS_Vertex *)(&myDS->Shape(n1))); 
72     const TopoDS_Vertex& aV2=(*(TopoDS_Vertex *)(&myDS->Shape(n2))); 
73     //
74     iFlag=BOPTools_AlgoTools::ComputeVV(aV1, aV2, myFuzzyValue);
75     if (!iFlag) {
76       BOPAlgo_Tools::FillMap<Standard_Integer, TColStd_MapIntegerHasher>(n1, n2, aMILI, aAllocator);
77     }
78   } 
79   //
80   // 2. Make blocks
81   BOPAlgo_Tools::MakeBlocks<Standard_Integer, TColStd_MapIntegerHasher>(aMILI, aMBlocks, aAllocator);
82   //
83   // 3. Make vertices
84   NCollection_List<TColStd_ListOfInteger>::Iterator aItB(aMBlocks);
85   for (; aItB.More(); aItB.Next()) {
86     const TColStd_ListOfInteger& aLI = aItB.Value();
87     MakeSDVertices(aLI);
88   }
89   //
90   TColStd_DataMapIteratorOfDataMapOfIntegerInteger aItDMII;
91   //
92   TColStd_DataMapOfIntegerInteger& aDMII=myDS->ShapesSD();
93   aItDMII.Initialize(aDMII);
94   for (; aItDMII.More(); aItDMII.Next()) {
95     n1=aItDMII.Key();
96     myDS->InitPaveBlocksForVertex(n1);
97   }
98   //
99   //-----------------------------------------------------scope t
100   aMBlocks.Clear();
101   aMILI.Clear();
102 }
103
104 //=======================================================================
105 // function: PerformVV
106 // purpose: 
107 //=======================================================================
108 Standard_Integer BOPAlgo_PaveFiller::MakeSDVertices
109    (const TColStd_ListOfInteger& theVertIndices,
110     const Standard_Boolean theAddInterfs)
111 {
112   TopoDS_Vertex aVSD, aVn;
113   Standard_Integer nSD = -1;
114   TColStd_ListIteratorOfListOfInteger aItLI(theVertIndices);
115   TopTools_ListOfShape aLV;
116   for (; aItLI.More(); aItLI.Next()) {
117     Standard_Integer nX = aItLI.Value(), nSD1;
118     if (myDS->HasShapeSD(nX, nSD1)) {
119       const TopoDS_Shape& aVSD1 = myDS->Shape(nSD1);
120       if (nSD == -1) {
121         aVSD = TopoDS::Vertex(aVSD1);
122         nSD = nSD1;
123       }
124       else {
125         aLV.Append(aVSD1);
126       }
127     }
128     const TopoDS_Shape& aV = myDS->Shape(nX);
129     aLV.Append(aV);
130   }
131   BOPTools_AlgoTools::MakeVertex(aLV, aVn);
132   Standard_Integer nV;
133   if (nSD != -1) {
134     // update old SD vertex with new value
135     Handle(BRep_TVertex)& aTVertex =
136       reinterpret_cast<Handle(BRep_TVertex)&>(const_cast<Handle(TopoDS_TShape)&>(aVSD.TShape()));
137     aTVertex->Pnt(BRep_Tool::Pnt(aVn));
138     aTVertex->Tolerance(BRep_Tool::Tolerance(aVn));
139     aVn = aVSD;
140     nV = nSD;
141   }
142   else {
143     // Append new vertex to the DS
144     BOPDS_ShapeInfo aSIn;
145     aSIn.SetShapeType(TopAbs_VERTEX);
146     aSIn.SetShape(aVn);
147     nV = myDS->Append(aSIn);
148   }
149   BOPDS_ShapeInfo& aSIDS = myDS->ChangeShapeInfo(nV);
150   Bnd_Box& aBox = aSIDS.ChangeBox();
151   aBox.Add(BRep_Tool::Pnt(aVn));
152   aBox.SetGap(BRep_Tool::Tolerance(aVn) + Precision::Confusion());
153   //
154   // Fill ShapesSD
155   BOPDS_VectorOfInterfVV& aVVs = myDS->InterfVV();
156   if (theAddInterfs)
157     aVVs.SetIncrement(theVertIndices.Extent());
158   //
159   aItLI.Initialize(theVertIndices);
160   for (; aItLI.More(); aItLI.Next()) {
161     Standard_Integer n1 = aItLI.Value();
162     myDS->AddShapeSD(n1, nV);
163     //
164     Standard_Integer iR1 = myDS->Rank(n1);
165     const TopoDS_Shape& aV1 = myDS->Shape(n1);
166     //
167     TColStd_ListIteratorOfListOfInteger aItLI2 = aItLI;
168     aItLI2.Next();
169     for (; aItLI2.More(); aItLI2.Next()) {
170       Standard_Integer n2 = aItLI2.Value();
171       //
172       if (iR1 >= 0 && iR1 == myDS->Rank(n2)) {
173         // add warning status
174         const TopoDS_Shape& aV2 = myDS->Shape(n2);
175         //
176         TopoDS_Compound aWC;
177         BRep_Builder().MakeCompound(aWC);
178         BRep_Builder().Add(aWC, aV1);
179         BRep_Builder().Add(aWC, aV2);
180         //
181         AddWarning (new BOPAlgo_AlertSelfInterferingShape (aWC));
182       }
183       //
184       if (theAddInterfs) {
185         myDS->AddInterf(n1, n2);
186         BOPDS_InterfVV& aVV = aVVs.Appended();
187         //
188         aVV.SetIndices(n1, n2);
189         aVV.SetIndexNew(nV);
190       }
191     }
192   }
193   return nV;
194 }