0029843: Modeling Algorithms - Boolean FUSE produces incorrect result
[occt.git] / src / BOPAlgo / BOPAlgo_Tools.hxx
1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _BOPAlgo_Tools_HeaderFile
16 #define _BOPAlgo_Tools_HeaderFile
17
18 #include <Standard.hxx>
19 #include <Standard_DefineAlloc.hxx>
20 #include <Standard_Handle.hxx>
21
22 #include <BOPDS_IndexedDataMapOfPaveBlockListOfInteger.hxx>
23 #include <BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock.hxx>
24 #include <BOPDS_PDS.hxx>
25 #include <NCollection_BaseAllocator.hxx>
26 #include <TopTools_DataMapOfShapeBox.hxx>
27 #include <TopTools_DataMapOfShapeListOfShape.hxx>
28 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
29 #include <TopTools_IndexedDataMapOfShapeReal.hxx>
30 #include <TopTools_ListOfListOfShape.hxx>
31 #include <TopTools_ListOfShape.hxx>
32 #include <TopTools_MapOfShape.hxx>
33 #include <Standard_Integer.hxx>
34
35 class BOPDS_PaveBlock;
36 class BOPDS_CommonBlock;
37 class IntTools_Context;
38 class TopoDS_Shape;
39
40 //! Provides tools used in the intersection part of Boolean operations
41 class BOPAlgo_Tools
42 {
43 public:
44
45   //! Makes the chains of the connected elements from the given connexity map
46   template <class theType, class theTypeHasher>
47   static void MakeBlocks(const NCollection_IndexedDataMap<theType, NCollection_List<theType>, theTypeHasher>& theMILI,
48                          NCollection_List<NCollection_List<theType>>& theMBlocks,
49                          const Handle(NCollection_BaseAllocator)& theAllocator)
50   {
51     NCollection_Map<theType, theTypeHasher> aMFence;
52     Standard_Integer i, aNb = theMILI.Extent();
53     for (i = 1; i <= aNb; ++i) {
54       const theType& n = theMILI.FindKey(i);
55       if (!aMFence.Add(n))
56         continue;
57       //
58       // Start the chain
59       NCollection_List<theType>& aChain = theMBlocks.Append(NCollection_List<theType>(theAllocator));
60       aChain.Append(n);
61       // Look for connected elements
62       typename NCollection_List<theType>::Iterator aItLChain(aChain);
63       for (; aItLChain.More(); aItLChain.Next()) {
64         const theType& n1 = aItLChain.Value();
65         const NCollection_List<theType>& aLI = theMILI.FindFromKey(n1);
66         // Add connected elements into the chain
67         typename NCollection_List<theType>::Iterator aItLI(aLI);
68         for (; aItLI.More(); aItLI.Next()) {
69           const theType& n2 = aItLI.Value();
70           if (aMFence.Add(n2)) {
71             aChain.Append(n2);
72           }
73         }
74       }
75     }
76   }
77
78   //! Fills the map with the connected entities
79   template <class theType, class theTypeHasher>
80   static void FillMap(const theType& n1,
81                       const theType& n2,
82                       NCollection_IndexedDataMap<theType, NCollection_List<theType>, theTypeHasher>& theMILI,
83                       const Handle(NCollection_BaseAllocator)& theAllocator)
84   {
85     NCollection_List<theType> *pList1 = theMILI.ChangeSeek(n1);
86     if (!pList1) {
87       pList1 = &theMILI(theMILI.Add(n1, NCollection_List<theType>(theAllocator)));
88     }
89     pList1->Append(n2);
90     //
91     NCollection_List<theType> *pList2 = theMILI.ChangeSeek(n2);
92     if (!pList2) {
93       pList2 = &theMILI(theMILI.Add(n2, NCollection_List<theType>(theAllocator)));
94     }
95     pList2->Append(n1);
96   }
97
98   Standard_EXPORT static void FillMap(const Handle(BOPDS_PaveBlock)& thePB1,
99                                       const Standard_Integer theF,
100                                       BOPDS_IndexedDataMapOfPaveBlockListOfInteger& theMILI,
101                                       const Handle(NCollection_BaseAllocator)& theAllocator);
102
103   //! Create Common Blocks from the groups of pave blocks of <theMBlocks>
104   //! connection map.
105   Standard_EXPORT static void PerformCommonBlocks(BOPDS_IndexedDataMapOfPaveBlockListOfPaveBlock& theMBlocks,
106                                                   const Handle(NCollection_BaseAllocator)& theAllocator,
107                                                   BOPDS_PDS& theDS,
108                                                   const Handle(IntTools_Context)& theContext = Handle(IntTools_Context)());
109
110   //! Create Common Blocks on faces using the PB->Faces connection map <theMBlocks>.
111   Standard_EXPORT static void PerformCommonBlocks(const BOPDS_IndexedDataMapOfPaveBlockListOfInteger& theMBlocks,
112                                                   const Handle(NCollection_BaseAllocator)& theAllocator,
113                                                   BOPDS_PDS& pDS,
114                                                   const Handle(IntTools_Context)& theContext = Handle(IntTools_Context)());
115
116   Standard_EXPORT static Standard_Real ComputeToleranceOfCB
117                                         (const Handle(BOPDS_CommonBlock)& theCB,
118                                          const BOPDS_PDS theDS,
119                                          const Handle(IntTools_Context)& theContext);
120
121   //! Creates planar wires from the given edges.<br>
122   //! The input edges are expected to be planar. And for the performance
123   //! sake the method does not check if the edges are really planar.<br>
124   //! Thus, the result wires will also be not planar if the input edges are not planar.<br>
125   //! The edges may be not shared, but the resulting wires will be sharing the
126   //! coinciding parts and intersecting parts.<br>
127   //! The output wires may be non-manifold and contain free and multi-connected vertices.<br>
128   //! Parameters:
129   //! <theEdges> - input edges;<br>
130   //! <theWires> - output wires;<br>
131   //! <theShared> - boolean flag which defines whether the input edges are already
132   //!               shared or have to be intersected;<br>
133   //! <theAngTol> - the angular tolerance which will be used for distinguishing
134   //!               the planes in which the edges are located. Default value is
135   //!               1.e-8 which is used for intersection of planes in IntTools_FaceFace.<br>
136   //! Method returns the following error statuses:<br>
137   //! 0 - in case of success (at least one wire has been built);<br>
138   //! 1 - in case there are no edges in the given shape;<br>
139   //! 2 - sharing of the edges has failed.<br>
140   Standard_EXPORT static Standard_Integer EdgesToWires(const TopoDS_Shape& theEdges,
141                                                        TopoDS_Shape& theWires,
142                                                        const Standard_Boolean theShared = Standard_False,
143                                                        const Standard_Real theAngTol = 1.e-8);
144
145   //! Creates planar faces from given planar wires.<br>
146   //! The method does not check if the wires are really planar.<br>
147   //! The input wires may be non-manifold but should be shared.<br>
148   //! The wires located in the same planes and included into other wires will create
149   //! holes in the faces built from outer wires.<br>
150   //! The tolerance values of the input shapes may be modified during the operation
151   //! due to projection of the edges on the planes for creation of 2D curves.<br>
152   //! Parameters:
153   //! <theWires> - the given wires;<br>
154   //! <theFaces> - the output faces;<br>
155   //! <theAngTol> - the angular tolerance for distinguishing the planes in which
156   //!               the wires are located. Default value is 1.e-8 which is used
157   //!               for intersection of planes in IntTools_FaceFace.<br>
158   //! Method returns TRUE in case of success, i.e. at least one face has been built.<br>
159   Standard_EXPORT static Standard_Boolean WiresToFaces(const TopoDS_Shape& theWires,
160                                                        TopoDS_Shape& theFaces,
161                                                        const Standard_Real theAngTol = 1.e-8);
162
163   //! Finds chains of intersecting vertices
164   Standard_EXPORT static void IntersectVertices(const TopTools_IndexedDataMapOfShapeReal& theVertices,
165                                                 const Standard_Real theFuzzyValue,
166                                                 TopTools_ListOfListOfShape& theChains);
167
168   //! Collect in the output list recursively all non-compound subshapes of the first level
169   //! of the given shape theS. If a shape presents in the map theMFence it is skipped.
170   //! All shapes put in the output are also added into theMFence.
171   Standard_EXPORT static void TreatCompound(const TopoDS_Shape& theS,
172                                             TopTools_MapOfShape& theMFence,
173                                             TopTools_ListOfShape& theLS);
174
175   //! Classifies the faces <theFaces> relatively solids <theSolids>.
176   //! The IN faces for solids are stored into output data map <theInParts>.
177   //!
178   //! The map <theSolidsIF> contains INTERNAL faces of the solids, to avoid
179   //! their additional classification.
180   //!
181   //! Firstly, it checks the intersection of bounding boxes of the shapes.
182   //! If the Box is not stored in the <theShapeBoxMap> map, it builds the box.
183   //! If the bounding boxes of solid and face are interfering the classification is performed.
184   //!
185   //! It is assumed that all faces and solids are already intersected and
186   //! do not have any geometrically coinciding parts without topological
187   //! sharing of these parts
188   Standard_EXPORT static void ClassifyFaces(const TopTools_ListOfShape& theFaces,
189                                             const TopTools_ListOfShape& theSolids,
190                                             const Standard_Boolean theRunParallel,
191                                             Handle(IntTools_Context)& theContext,
192                                             TopTools_IndexedDataMapOfShapeListOfShape& theInParts,
193                                             const TopTools_DataMapOfShapeBox& theShapeBoxMap = TopTools_DataMapOfShapeBox(),
194                                             const TopTools_DataMapOfShapeListOfShape& theSolidsIF = TopTools_DataMapOfShapeListOfShape());
195
196   //! Classifies the given parts relatively the given solids and
197   //! fills the solids with the parts classified as INTERNAL.
198   //!
199   //! @param theSolids  - The solids to put internals to
200   //! @param theParts   - The parts to classify relatively solids
201   //! @param theImages  - Possible images of the parts that has to be classified
202   //! @param theContext - Cashed geometrical tools to speed-up classifications
203   Standard_EXPORT static void FillInternals(const TopTools_ListOfShape& theSolids,
204                                             const TopTools_ListOfShape& theParts,
205                                             const TopTools_DataMapOfShapeListOfShape& theImages,
206                                             const Handle(IntTools_Context)& theContext);
207
208   //! Computes the transformation needed to move the objects
209   //! to the given point to increase the quality of computations.
210   //! Returns true if the objects are located far from the given point
211   //! (relatively given criteria), false otherwise.
212   //! @param theBox1 the AABB of the first object
213   //! @param theBox2 the AABB of the second object
214   //! @param theTrsf the computed transformation
215   //! @param thePoint the Point to compute transformation to
216   //! @param theCriteria the Criteria to check whether thranformation is required
217   Standard_EXPORT static Standard_Boolean TrsfToPoint (const Bnd_Box& theBox1,
218                                                        const Bnd_Box& theBox2,
219                                                        gp_Trsf&       theTrsf,
220                                                        const gp_Pnt&  thePoint = gp_Pnt (0.0, 0.0, 0.0),
221                                                        const Standard_Real theCriteria = 1.e+5);
222 };
223
224 #endif // _BOPAlgo_Tools_HeaderFile