0025398: Modeling Algorithms - Provide shape proximity detector
[occt.git] / src / BRepExtrema / BRepExtrema_ShapeProximity.hxx
1 // Created on: 2014-10-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _BRepExtrema_ShapeProximity_HeaderFile
17 #define _BRepExtrema_ShapeProximity_HeaderFile
18
19 #include <BVH_Geometry.hxx>
20 #include <BRepExtrema_TriangleSet.hxx>
21 #include <TColStd_PackedMapOfInteger.hxx>
22 #include <NCollection_DataMap.hxx>
23
24 //! Set of overlapped sub-shapes.
25 typedef NCollection_DataMap<Standard_Integer, TColStd_PackedMapOfInteger > BRepExtrema_OverlappedSubShapes;
26
27 //! Tool class for shape proximity detection.
28 //! For two given shapes and given tolerance (offset from the mesh) the algorithm allows
29 //! to determine whether or not they are overlapped. The algorithm input consists of any
30 //! shapes which can be decomposed into individual faces (used as basic shape elements).
31 //! High performance is achieved through the use of existing triangulation of faces. So
32 //! poly triangulation (with the desired deflection) should already be built. Note that
33 //! solution is approximate (and corresponds to the deflection used for triangulation).
34 //!
35 //! The algorithm can be run in two modes. If tolerance is set to zero, the algorithm
36 //! will detect only intersecting faces (containing triangles with common points). If
37 //! tolerance is set to positive value, the algorithm will also detect faces located
38 //! on distance less than the given tolerance from each other.
39 class BRepExtrema_ShapeProximity
40 {
41  public:
42
43   //! Creates empty proximity tool.
44   Standard_EXPORT BRepExtrema_ShapeProximity (const Standard_Real theTolerance = 0.0);
45
46   //! Creates proximity tool for the given two shapes.
47   Standard_EXPORT BRepExtrema_ShapeProximity (const TopoDS_Shape& theShape1,
48                                               const TopoDS_Shape& theShape2,
49                                               const Standard_Real theTolerance = 0.0);
50
51 public:
52
53   //! Returns tolerance value for overlap test (distance between shapes).
54   Standard_Real Tolerance() const
55   {
56     return myTolerance;
57   }
58
59   //! Sets tolerance value for overlap test (distance between shapes).
60   void SetTolerance (const Standard_Real theTolerance)
61   {
62     myTolerance = theTolerance;
63   }
64
65   //! Loads 1st shape into proximity tool.
66   Standard_EXPORT Standard_Boolean LoadShape1 (const TopoDS_Shape& theShape1);
67
68   //! Loads 2nd shape into proximity tool.
69   Standard_EXPORT Standard_Boolean LoadShape2 (const TopoDS_Shape& theShape2);
70
71   //! Performs search for overlapped faces.
72   Standard_EXPORT void Perform();
73
74   //! True if the search is completed.
75   Standard_Boolean IsDone() const
76   { 
77     return myIsDone;
78   }
79
80   //! Returns set of all the face triangles of the 1st shape.
81   const NCollection_Handle<BRepExtrema_TriangleSet>& PrimitiveSet1() const
82   {
83     return myPrimitiveSet1;
84   }
85
86   //! Returns set of all the face triangles of the 2nd shape.
87   const NCollection_Handle<BRepExtrema_TriangleSet>& PrimitiveSet2() const
88   {
89     return myPrimitiveSet2;
90   }
91
92   //! Returns set of IDs of overlapped faces of 1st shape.
93   const BRepExtrema_OverlappedSubShapes& OverlapSubShapes1() const
94   {
95     return myOverlapSubShapes1;
96   }
97
98   //! Returns set of IDs of overlapped faces of 2nd shape.
99   const BRepExtrema_OverlappedSubShapes& OverlapSubShapes2() const
100   {
101     return myOverlapSubShapes2;
102   }
103
104   //! Returns sub-shape from 1st shape with the given index.
105   const TopoDS_Face& GetSubShape1 (const Standard_Integer theID) const
106   {
107     return myFaceList1.Value (theID);
108   }
109
110   //! Returns sub-shape from 1st shape with the given index.
111   const TopoDS_Face& GetSubShape2 (const Standard_Integer theID) const
112   {
113     return myFaceList2.Value (theID);
114   }
115
116 protected:
117
118   //! Performs narrow-phase of overlap test (exact intersection).
119   void IntersectLeavesExact (const BVH_Vec4i& theLeaf1, const BVH_Vec4i& theLeaf2);
120
121   //! Performs narrow-phase of overlap test (intersection with non-zero tolerance).
122   void IntersectLeavesToler (const BVH_Vec4i& theLeaf1, const BVH_Vec4i& theLeaf2);
123
124 private:
125
126   //! Maximum overlapping distance.
127   Standard_Real myTolerance;
128
129   //! Is the 1st shape initialized?
130   Standard_Boolean myIsInitS1;
131   //! Is the 2nd shape initialized?
132   Standard_Boolean myIsInitS2;
133
134   //! List of faces of the 1st shape.
135   BRepExtrema_ShapeList myFaceList1;
136   //! List of faces of the 2nd shape.
137   BRepExtrema_ShapeList myFaceList2;
138
139   //! Set of all the face triangles of the 1st shape.
140   NCollection_Handle<BRepExtrema_TriangleSet> myPrimitiveSet1;
141   //! Set of all the face triangles of the 2nd shape.
142   NCollection_Handle<BRepExtrema_TriangleSet> myPrimitiveSet2;
143
144   //! Set of overlapped faces of 1st shape.
145   BRepExtrema_OverlappedSubShapes myOverlapSubShapes1;
146   //! Set of overlapped faces of 2nd shape.
147   BRepExtrema_OverlappedSubShapes myOverlapSubShapes2;
148
149   //! Is overlap test completed?
150   Standard_Boolean myIsDone;
151
152 };
153
154 #endif // _BRepExtrema_ShapeProximity_HeaderFile