7e53483e47c30d2624aa89123ba0f1c406363d23
[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_Collections.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 public: //! \name plugin API
107
108   //! Plugin interface for the Mesh Factories.
109   //! Initializes meshing algorithm with the given parameters.
110   //! \param theShape shape to be meshed.
111   //! \param theLinDeflection linear deflection.
112   //! \param theAngDeflection angular deflection.
113   //! \param[out] theAlgo pointer to initialized algorithm.
114   Standard_EXPORT static Standard_Integer Discret(const TopoDS_Shape&    theShape,
115                                                   const Standard_Real    theLinDeflection,
116                                                   const Standard_Real    theAngDeflection,
117                                                   BRepMesh_DiscretRoot* &theAlgo);
118   
119   //! Returns multi-threading usage flag set by default in 
120   //! Discret() static method (thus applied only to Mesh Factories).
121   Standard_EXPORT static Standard_Boolean IsParallelDefault();
122   
123   //! Setup multi-threading usage flag set by default in 
124   //! Discret() static method (thus applied only to Mesh Factories).
125   Standard_EXPORT static void SetParallelDefault(const Standard_Boolean isInParallel);
126
127   //! Returns mesh tool storing mesh data.
128   inline const Handle(BRepMesh_FastDiscret)& Mesh() const
129   {
130     return myMesh;
131   }
132
133   DEFINE_STANDARD_RTTI(BRepMesh_IncrementalMesh)
134
135 protected:
136
137   Standard_EXPORT virtual void init();
138
139 private:
140
141   //! Checks is the shape to be meshed has correct poly data, 
142   //! i.e. PolygonOnTriangulation of particular edge connected 
143   //! to the same Triangulation data structure as stored inside 
144   //! a parent face.
145   //! \return TRUE on success, FALSE in case of inconsistencies.
146   Standard_Boolean isCorrectPolyData();
147
148   //! Builds the incremental mesh for the shape.
149   void update();
150
151   //! Checks triangulation of the given face for consistency 
152   //! with the chosen tolerance. If some edge of face has no
153   //! discrete representation triangulation will be calculated.
154   //! \param theFace face to be checked.
155   void update(const TopoDS_Face& theFace);
156
157   //! Checks discretization of the given edge for consistency 
158   //! with the chosen tolerance.
159   //! \param theEdge edge to be checked.
160   void update(const TopoDS_Edge& theEdge);
161
162   //! Collects faces suitable for meshing.
163   void collectFaces();
164
165   //! Discretizes edges that have no associations with faces.
166   void discretizeFreeEdges();
167
168   //! Returns deflection of the given edge.
169   //! \param theEdge edge which tolerance should be taken.
170   Standard_Real edgeDeflection(const TopoDS_Edge& theEdge);
171
172   //! Returns deflection of the given face.
173   //! If relative flag is set, calculates relative deflection of the face 
174   //! as an average value of relative deflection regarding face's edges.
175   //! Returns value of deflection set by user elsewhere.
176   Standard_Real faceDeflection(const TopoDS_Face& theFace);
177
178   //! Prepares the given face for meshing.
179   //! Nullifies triangulation of face and polygons of face's edges.
180   //! \param theFace face to be checked.
181   //! \param isWithCheck if TRUE, checks parameters of triangulation 
182   //! existing in face. If its deflection satisfies the given value and
183   //! each edge of face has polygon corresponded to this triangulation,
184   //! method return FALSE.
185   //! \return TRUE in case if the given face should be meshed.
186   Standard_Boolean toBeMeshed(const TopoDS_Face&     theFace,
187                               const Standard_Boolean isWithCheck);
188
189   //! Stores mesh to the shape.
190   void commit();
191
192   //! Stores mesh to the face.
193   void commitFace(const TopoDS_Face& theFace);
194
195 protected:
196
197   Standard_Boolean                                    myRelative;
198   Standard_Boolean                                    myInParallel;
199   BRepMeshCol::DMapOfEdgeListOfTriangulation          myEmptyEdges;
200   Handle(BRepMesh_FastDiscret)                        myMesh;
201   Standard_Boolean                                    myModified;
202   TopTools_DataMapOfShapeReal                         myEdgeDeflection;
203   Standard_Real                                       myMaxShapeSize;
204   Standard_Integer                                    myStatus;
205   std::vector<TopoDS_Face>                            myFaces;
206 };
207
208 DEFINE_STANDARD_HANDLE(BRepMesh_IncrementalMesh,BRepMesh_DiscretRoot)
209
210 #endif