0025039: Improvement of code structure of general and supporting tools implemented...
[occt.git] / src / BRepMesh / BRepMesh_FastDiscret.hxx
1 // Copyright (c) 2013 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _BRepMesh_FastDiscret_HeaderFile
15 #define _BRepMesh_FastDiscret_HeaderFile
16
17 #include <Standard.hxx>
18 #include <Standard_DefineHandle.hxx>
19 #include <BRepMesh_FastDiscret.hxx>
20 #include <BRepMesh_DataStructureOfDelaun.hxx>
21 #include <TColStd_IndexedMapOfInteger.hxx>
22 #include <BRepMesh_Status.hxx>
23 #include <TopTools_DataMapOfShapeReal.hxx>
24 #include <TopTools_ListOfShape.hxx>
25 #include <TopTools_MutexForShapeProvider.hxx>
26 #include <Standard_Transient.hxx>
27 #include <Handle_BRepAdaptor_HSurface.hxx>
28 #include <Handle_Geom2d_Curve.hxx>
29 #include <BRepMesh_Delaun.hxx>
30 #include <TopAbs_ShapeEnum.hxx>
31 #include <BRepMesh_Triangle.hxx>
32 #include <BRepMesh_FaceAttribute.hxx>
33 #include <BRepMesh_Collections.hxx>
34
35 class BRepMesh_DataStructureOfDelaun;
36 class Bnd_Box;
37 class TopoDS_Shape;
38 class TopoDS_Face;
39 class TopTools_IndexedDataMapOfShapeListOfShape;
40 class TopoDS_Edge;
41 class BRepAdaptor_HSurface;
42 class Geom2d_Curve;
43 class TopoDS_Vertex;
44 class BRepMesh_Edge;
45 class BRepMesh_Vertex;
46 class gp_Pnt;
47 class BRepMesh_FaceAttribute;
48 class TopTools_DataMapOfShapeReal;
49
50
51 //! Algorithm to mesh a shape with respect of the <br>
52 //! frontier the deflection and by option the shared <br>
53 //! components. <br>
54 class BRepMesh_FastDiscret : public Standard_Transient
55 {
56 public:
57   
58   Standard_EXPORT BRepMesh_FastDiscret(const Standard_Real defle,
59                                        const Standard_Real angle,
60                                        const Bnd_Box& B,
61                                        const Standard_Boolean withShare = Standard_True,
62                                        const Standard_Boolean inshape = Standard_False,
63                                        const Standard_Boolean relative = Standard_False,
64                                        const Standard_Boolean shapetrigu = Standard_False,
65                                        const Standard_Boolean isInParallel = Standard_False);
66
67   //! if the boolean <relative> is True, the <br>
68   //! deflection used for the polygonalisation of <br>
69   //! each edge will be <defle> * Size of Edge. <br>
70   //! the deflection used for the faces will be the maximum <br>
71   //! deflection of their edges. <br>
72   //! <br>
73   //! if <shapetrigu> is True, the triangulation, if exists <br>
74   //! with a correct deflection, can be used to re-triangulate <br>
75   //! the shape. <br>
76   //! <br>
77   //! if <inshape> is True, the calculated <br>
78   //! triangulation will be stored in the shape. <br>
79   Standard_EXPORT BRepMesh_FastDiscret(const TopoDS_Shape& shape,
80                                        const Standard_Real defle,
81                                        const Standard_Real angle,
82                                        const Bnd_Box& B,
83                                        const Standard_Boolean withShare = Standard_True,
84                                        const Standard_Boolean inshape = Standard_False,
85                                        const Standard_Boolean relative = Standard_False,
86                                        const Standard_Boolean shapetrigu = Standard_False,
87                                        const Standard_Boolean isInParallel = Standard_False);
88
89   //! Build triangulation on the whole shape <br>
90   Standard_EXPORT void Perform(const TopoDS_Shape& shape);
91
92   //! Record a face for further processing. <br>
93   Standard_EXPORT void Add(const TopoDS_Face& face,
94                            const TopTools_IndexedDataMapOfShapeListOfShape& ancestor) ;
95
96   //! Triangulate a face previously recorded for <br>
97   //! processing by call to Add(). Can be executed in <br>
98   //! parallel threads. <br>
99   Standard_EXPORT void Process(const TopoDS_Face& face) const;
100
101   void operator ()(const TopoDS_Face& face) const
102   {
103     Process(face);
104   }
105   
106   Standard_EXPORT BRepMesh_Status CurrentFaceStatus() const;
107   
108   //! Request algorithm to launch in multiple threads <br>
109   //! to improve performance (should be supported by plugin). <br>
110   Standard_EXPORT void SetParallel(const Standard_Boolean theInParallel);
111   
112   //! Returns the multi-threading usage flag. <br>
113   Standard_EXPORT Standard_Boolean IsParallel() const;
114   
115   //! Creates mutexes for each sub-shape of type theType in theShape. <br>
116   //! Used to avoid data races. <br>
117   Standard_EXPORT void CreateMutexesForSubShapes(const TopoDS_Shape& theShape,
118                                                  const TopAbs_ShapeEnum theType);
119   
120   //! Removes all created mutexes <br>
121   Standard_EXPORT void RemoveAllMutexes();
122
123   //! Gives the number of built triangles. <br>
124   Standard_EXPORT Standard_Integer NbTriangles() const;
125
126   //! Gives the triangle of <Index>. <br>
127   Standard_EXPORT const BRepMesh_Triangle& Triangle(const Standard_Integer Index) const;
128
129   //! Gives the number of built Edges <br>
130   Standard_EXPORT Standard_Integer NbEdges() const;
131
132   //! Gives the edge of index <Index>. <br>
133   Standard_EXPORT const BRepMesh_Edge& Edge(const Standard_Integer Index) const;
134
135   //! Gives the number of built Vertices. <br>
136   Standard_EXPORT Standard_Integer NbVertices() const;
137
138   //! Gives the vertex of <Index>. <br>
139   Standard_EXPORT const BRepMesh_Vertex& Vertex(const Standard_Integer Index) const;
140
141   //! Gives the nodes of triangle with the given index.
142   Standard_EXPORT void TriangleNodes(const Standard_Integer theIndex,
143                                      Standard_Integer       (&theNodes)[3]) const
144   {
145     myStructure->ElementNodes(Triangle(theIndex), theNodes);
146   }
147
148   //! Gives the location3d of the vertex of <Index>. <br>
149   Standard_EXPORT const gp_Pnt& Pnt(const Standard_Integer Index) const;
150
151   //! Gives the list of indices of the vertices <br>
152   Standard_EXPORT void VerticesOfDomain(BRepMeshCol::MapOfInteger& Indices) const;
153
154   //! Gives the list of indices of the edges <br>
155   inline void EdgesOfDomain(BRepMeshCol::MapOfInteger& Indices) const
156   { 
157     Indices = myStructure->LinksOfDomain();
158   }
159
160   //! Gives the list of indices of the triangles <br>
161   inline void TrianglesOfDomain(BRepMeshCol::MapOfInteger& Indices) const
162   { 
163     Indices = myStructure->ElementsOfDomain();
164   }
165
166   //! Gives the number of different location in 3d space.  
167   //! It is different of the number of vertices if there 
168   //! is more than one surface. <br>
169   //! Even for one surface, the number can be different
170   //! if an edge is shared. <br>
171   inline Standard_Integer NbPoint3d() const
172   {
173     return myNbLocat;
174   }
175
176   //! Gives the 3d space location of the vertex <Index>. <br>
177   inline const gp_Pnt& Point3d(const Standard_Integer Index) const
178   {
179     return myLocation3d(Index);
180   }
181
182   //! returns the deflection value. <br>
183   inline Standard_Real GetDeflection() const
184   {
185     return myDeflection;
186   }
187
188   //! returns the deflection value. <br>
189   inline Standard_Real GetAngle() const
190   {
191     return myAngle;
192   }
193   
194   inline Standard_Boolean WithShare() const
195   {
196     return myWithShare;
197   }
198   
199   inline Standard_Boolean InShape() const
200   {
201     return myInshape;
202   }
203   
204   inline Standard_Boolean ShapeTrigu() const
205   {
206     return myShapetrigu;
207   }
208
209   //! returns the face deflection value. <br>
210   Standard_EXPORT Standard_Boolean GetFaceAttribute(const TopoDS_Face& face,Handle(BRepMesh_FaceAttribute)& fattrib) const;
211
212   //! remove face attribute as useless to free locate memory <br>
213   Standard_EXPORT void RemoveFaceAttribute(const TopoDS_Face& face);
214   
215   inline const TopTools_DataMapOfShapeReal& GetMapOfDefEdge() const
216   {
217     return myMapdefle;
218   }
219
220
221   DEFINE_STANDARD_RTTI(BRepMesh_FastDiscret)
222
223 private: 
224   
225   void Add(const TopoDS_Edge& edge,
226            const TopoDS_Face& face,
227            const Handle(BRepAdaptor_HSurface)& S,
228            const Handle(Geom2d_Curve)& C,
229            const TopTools_IndexedDataMapOfShapeListOfShape& ancestor,
230            const Standard_Real defedge,
231            const Standard_Real first,
232            const Standard_Real last);
233   
234   void Add(const TopoDS_Vertex& theVert,
235            const TopoDS_Face& face,
236            const Handle(BRepAdaptor_HSurface)& S);
237   
238   Standard_Boolean Update(const TopoDS_Edge& Edge,
239                           const TopoDS_Face& Face,
240                           const Handle(Geom2d_Curve)& C,
241                           const Standard_Real defedge,
242                           const Standard_Real first,
243                           const Standard_Real last);
244   
245   void InternalVertices(const Handle(BRepAdaptor_HSurface)& caro,
246                         BRepMeshCol::ListOfVertex& inter,
247                         const Standard_Real defedge,
248                         const BRepMeshCol::HClassifier& classifier);
249   
250   Standard_Real Control(const Handle(BRepAdaptor_HSurface)& caro,
251                         const Standard_Real defface,
252                         BRepMeshCol::ListOfVertex& inter,
253                         BRepMeshCol::ListOfInteger& badTri,
254                         BRepMeshCol::ListOfInteger& nulTri,
255                         BRepMesh_Delaun& trigu,
256                         const Standard_Boolean isfirst);
257   
258   void AddInShape(const TopoDS_Face& face,
259                   const Standard_Real defedge);
260
261 private:
262
263   Standard_Real                             myAngle;
264   Standard_Real                             myDeflection;
265   Standard_Real                             myDtotale;
266   Standard_Boolean                          myWithShare;
267   Standard_Boolean                          myInParallel;
268   BRepMeshCol::DMapOfVertexInteger          myVertices;
269   BRepMeshCol::DMapOfShapePairOfPolygon     myEdges;
270   BRepMeshCol::DMapOfShapePairOfPolygon     myInternaledges;
271   Standard_Integer                          myNbLocat;
272   BRepMeshCol::DMapOfIntegerPnt             myLocation3d;
273   Handle_BRepMesh_DataStructureOfDelaun     myStructure;
274   BRepMeshCol::DMapOfFaceAttribute          myMapattrib;
275   TColStd_IndexedMapOfInteger               myVemap;
276   BRepMeshCol::DMapOfIntegerListOfXY        myLocation2d;
277   Standard_Boolean                          myRelative;
278   Standard_Boolean                          myShapetrigu;
279   Standard_Boolean                          myInshape;
280   BRepMesh_Status                           myFacestate;
281   TopTools_DataMapOfShapeReal               myMapdefle;
282   TopTools_ListOfShape                      myNottriangulated;
283   BRepMeshCol::Allocator                    myAllocator;
284   TopTools_MutexForShapeProvider            myMutexProvider;
285 };
286
287 DEFINE_STANDARD_HANDLE(BRepMesh_FastDiscret, Standard_Transient)
288
289 #endif