1169a0c37859f651651cdcef1e32cb8ea5774c10
[occt.git] / src / BRepMesh / BRepMesh_IncrementalMesh.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_IncrementalMesh_HeaderFile
15 #define _BRepMesh_IncrementalMesh_HeaderFile
16
17 #include <Standard.hxx>
18 #include <Standard_DefineHandle.hxx>
19
20 #include <BRepMesh_FastDiscret.hxx>
21 #include <TopTools_MapOfShape.hxx>
22 #include <TopTools_DataMapOfShapeReal.hxx>
23 #include <BRepMesh_DiscretRoot.hxx>
24 #include <Handle_Poly_Triangulation.hxx>
25 #include <BRepMesh.hxx>
26
27 #include <vector>
28
29 class Poly_Triangulation;
30 class TopoDS_Shape;
31 class TopoDS_Edge;
32 class TopoDS_Face;
33
34 //! Builds the mesh of a shape with respect of their 
35 //! correctly triangulated parts 
36 class BRepMesh_IncrementalMesh : public BRepMesh_DiscretRoot
37 {
38 public: //! @name mesher API
39
40   //! Default constructor
41   Standard_EXPORT BRepMesh_IncrementalMesh();
42
43   //! Destructor
44   Standard_EXPORT virtual ~BRepMesh_IncrementalMesh();
45
46   //! Constructor.
47   //! Automatically calls method Perform.
48   //! @param theShape shape to be meshed.
49   //! @param theLinDeflection linear deflection.
50   //! @param isRelative if TRUE deflection used for discretization of 
51   //! each edge will be <theLinDeflection> * <size of edge>. Deflection 
52   //! used for the faces will be the maximum deflection of their edges.
53   //! @param theAngDeflection angular deflection.
54   //! @param isInParallel if TRUE shape will be meshed in parallel.
55   Standard_EXPORT BRepMesh_IncrementalMesh(
56     const TopoDS_Shape&    theShape,
57     const Standard_Real    theLinDeflection,
58     const Standard_Boolean isRelative       = Standard_False,
59     const Standard_Real    theAngDeflection = 0.5,
60     const Standard_Boolean isInParallel     = Standard_False);
61
62   //! Performs meshing ot the shape.
63   Standard_EXPORT virtual void Perform();
64   
65 public: //! @name accessing to parameters.
66
67   //! Enables using relative deflection.
68   //! @param isRelative if TRUE deflection used for discretization of 
69   //! each edge will be <theLinDeflection> * <size of edge>. Deflection 
70   //! used for the faces will be the maximum deflection of their edges.
71   inline void SetRelative(const Standard_Boolean isRelative)
72   {
73     myRelative = isRelative;
74   }
75   
76   //! Returns relative deflection flag.
77   inline Standard_Boolean IsRelative() const
78   {
79     return myRelative;
80   }
81   
82   //! Returns modified flag.
83   inline Standard_Boolean IsModified() const
84   {
85     return myModified;
86   }
87   
88   //! Returns accumulated status flags faced during meshing.
89   inline Standard_Integer GetStatusFlags() const
90   {
91     return myStatus;
92   }
93   
94   //! Request algorithm to launch in multiple threads to improve performance.
95   inline void SetParallel(const Standard_Boolean isInParallel)
96   {
97     myInParallel = isInParallel;
98   }
99   
100   //! Returns the multi-threading usage flag.
101   inline Standard_Boolean IsParallel() const
102   {
103     return myInParallel;
104   }
105
106   //! Sets min size parameter.
107   inline void SetMinSize(const Standard_Real theMinSize)
108   {
109     myMinSize = Max(theMinSize, Precision::Confusion());
110   }
111
112   //! Returns min size parameter.
113   inline Standard_Real GetMinSize() const
114   {
115     return myMinSize;
116   }
117
118   //! Enables/disables internal vertices mode (enabled by default).
119   inline void SetInternalVerticesMode(const Standard_Boolean isEnabled)
120   {
121     myInternalVerticesMode = isEnabled;
122   }
123   
124   //! Returns flag indicating is internal vertices mode enabled/disabled.
125   inline Standard_Boolean IsInternalVerticesMode() const
126   {
127     return myInternalVerticesMode;
128   }
129
130   //! Enables/disables control of deflection of mesh from real surface 
131   //! (enabled by default).
132   inline void SetControlSurfaceDeflection(const Standard_Boolean isEnabled)
133   {
134     myIsControlSurfaceDeflection = isEnabled;
135   }
136
137   //! Returns flag indicating is adaptive reconfiguration 
138   //! of mesh enabled/disabled.
139   inline Standard_Boolean IsControlSurfaceDeflection() const
140   {
141     return myIsControlSurfaceDeflection;
142   }
143
144 public: //! @name plugin API
145
146   //! Plugin interface for the Mesh Factories.
147   //! Initializes meshing algorithm with the given parameters.
148   //! @param theShape shape to be meshed.
149   //! @param theLinDeflection linear deflection.
150   //! @param theAngDeflection angular deflection.
151   //! @param[out] theAlgo pointer to initialized algorithm.
152   Standard_EXPORT static Standard_Integer Discret(const TopoDS_Shape&    theShape,
153                                                   const Standard_Real    theLinDeflection,
154                                                   const Standard_Real    theAngDeflection,
155                                                   BRepMesh_DiscretRoot* &theAlgo);
156   
157   //! Returns multi-threading usage flag set by default in 
158   //! Discret() static method (thus applied only to Mesh Factories).
159   Standard_EXPORT static Standard_Boolean IsParallelDefault();
160   
161   //! Setup multi-threading usage flag set by default in 
162   //! Discret() static method (thus applied only to Mesh Factories).
163   Standard_EXPORT static void SetParallelDefault(const Standard_Boolean isInParallel);
164
165   DEFINE_STANDARD_RTTI(BRepMesh_IncrementalMesh)
166
167 protected:
168
169   Standard_EXPORT virtual void init();
170
171 private:
172
173   //! Builds the incremental mesh for the shape.
174   void update();
175
176   //! Checks triangulation of the given face for consistency 
177   //! with the chosen tolerance. If some edge of face has no
178   //! discrete representation triangulation will be calculated.
179   //! @param theFace face to be checked.
180   void update(const TopoDS_Face& theFace);
181
182   //! Checks discretization of the given edge for consistency 
183   //! with the chosen tolerance.
184   //! @param theEdge edge to be checked.
185   void update(const TopoDS_Edge& theEdge);
186
187   //! Collects faces suitable for meshing.
188   void collectFaces();
189
190   //! Discretizes edges that have no associations with faces.
191   void discretizeFreeEdges();
192
193   //! Returns deflection of the given edge.
194   //! @param theEdge edge which tolerance should be taken.
195   Standard_Real edgeDeflection(const TopoDS_Edge& theEdge);
196
197   //! Returns deflection of the given face.
198   //! If relative flag is set, calculates relative deflection of the face 
199   //! as an average value of relative deflection regarding face's edges.
200   //! Returns value of deflection set by user elsewhere.
201   Standard_Real faceDeflection(const TopoDS_Face& theFace);
202
203   //! Prepares the given face for meshing.
204   //! Nullifies triangulation of face and polygons of face's edges.
205   //! @param theFace face to be checked.
206   //! @param isWithCheck if TRUE, checks parameters of triangulation 
207   //! existing in face. If its deflection satisfies the given value and
208   //! each edge of face has polygon corresponded to this triangulation,
209   //! method return FALSE.
210   //! @return TRUE in case if the given face should be meshed.
211   Standard_Boolean toBeMeshed(const TopoDS_Face&     theFace,
212                               const Standard_Boolean isWithCheck);
213
214   //! Stores mesh to the shape.
215   void commit();
216
217   //! Stores mesh of internal edges to the face.
218   void commitEdges(const TopoDS_Face& theFace);
219   
220   //! Clears internal data structures.
221   void clear();
222
223 protected:
224
225   Standard_Boolean                            myRelative;
226   Standard_Boolean                            myInParallel;
227   BRepMesh::DMapOfEdgeListOfTriangulationBool myEdges;
228   Handle(BRepMesh_FastDiscret)                myMesh;
229   Standard_Boolean                            myModified;
230   TopTools_DataMapOfShapeReal                 myEdgeDeflection;
231   Standard_Real                               myMaxShapeSize;
232   Standard_Integer                            myStatus;
233   NCollection_Vector<TopoDS_Face>             myFaces;
234   Standard_Real                               myMinSize;
235   Standard_Boolean                            myInternalVerticesMode;
236   Standard_Boolean                            myIsControlSurfaceDeflection;
237 };
238
239 DEFINE_STANDARD_HANDLE(BRepMesh_IncrementalMesh,BRepMesh_DiscretRoot)
240
241 #endif