66e05b1c73618b21c16ab1b5d4db8148f5dac90b
[occt.git] / src / BRepMesh / BRepMesh_FastDiscretFace.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_FastDiscretFace_HeaderFile
15 #define _BRepMesh_FastDiscretFace_HeaderFile
16
17 #include <Standard.hxx>
18 #include <Standard_DefineHandle.hxx>
19 #include <BRepMesh_FastDiscretFace.hxx>
20 #include <BRepMesh_DataStructureOfDelaun.hxx>
21 #include <BRepMesh.hxx>
22 #include <BRepMesh_FaceAttribute.hxx>
23 #include <Standard_Transient.hxx>
24 #include <TopTools_MutexForShapeProvider.hxx>
25 #include <Handle_BRepAdaptor_HSurface.hxx>
26 #include <Handle_Poly_Triangulation.hxx>
27 #include <BRepMesh_Delaun.hxx>
28 #include <BRepMesh_Triangle.hxx>
29 #include <BRepMesh_Classifier.hxx>
30 #include <ElSLib.hxx>
31
32 class BRepMesh_DataStructureOfDelaun;
33 class BRepMesh_FaceAttribute;
34 class TopoDS_Face;
35 class TopTools_DataMapOfShapeReal;
36 class TopoDS_Vertex;
37 class BRepAdaptor_HSurface;
38 class TopoDS_Edge;
39 class Poly_Triangulation;
40 class TopLoc_Location;
41 class gp_XY;
42 class gp_Pnt2d;
43 class BRepMesh_Edge;
44 class BRepMesh_Vertex;
45 class gp_Pnt;
46
47 //! Algorithm to mesh a face with respect of the frontier 
48 //! the deflection and by option the shared components.
49 class BRepMesh_FastDiscretFace : public Standard_Transient 
50 {
51 public:
52   
53   //! Constructor.
54   //! @param theAngle deviation angle to be used for surface tessellation.
55   //! @param isInternalVerticesMode flag enabling/disabling internal 
56   //! vertices mode.
57   //! @param isControlSurfaceDeflection enables/disables adaptive 
58   //! reconfiguration of mesh.
59   Standard_EXPORT BRepMesh_FastDiscretFace(
60     const Standard_Real    theAngle,
61     const Standard_Real    theMinSize,
62     const Standard_Boolean isInternalVerticesMode,
63     const Standard_Boolean isControlSurfaceDeflection);
64
65   Standard_EXPORT void Perform(const Handle(BRepMesh_FaceAttribute)& theAttribute);
66
67   DEFINE_STANDARD_RTTI(BRepMesh_FastDiscretFace)
68
69 private:
70
71   void add(const Handle(BRepMesh_FaceAttribute)& theAttribute);
72   void add(const TopoDS_Vertex& theVertex);
73
74   Standard_Real control(BRepMesh::ListOfVertex&  theNewVertices,
75                         BRepMesh_Delaun&         theMeshBuilder,
76                         const Standard_Boolean   theIsFirst);
77
78   //! Registers the given nodes in mesh data structure and
79   //! performs refinement of existing mesh.
80   //! @param theVertices nodes to be inserted.
81   //! @param theMeshBuilder initialized tool refining mesh 
82   //! in respect to inserting nodes.
83   //! @return TRUE if vertices were been inserted, FALSE elewhere.
84   Standard_Boolean addVerticesToMesh(
85     const BRepMesh::ListOfVertex& theVertices,
86     BRepMesh_Delaun&              theMeshBuilder);
87
88   //! Calculates nodes lying on face's surface and inserts them to a mesh.
89   //! @param theNewVertices list of vertices to be extended and added to mesh.
90   //! @param theMeshBuilder initialized tool refining mesh 
91   //! in respect to inserting nodes.
92   void insertInternalVertices(BRepMesh::ListOfVertex&  theNewVertices,
93                               BRepMesh_Delaun&         theMeshBuilder);
94
95   //! Calculates nodes lying on spherical surface.
96   //! @param theNewVertices list of vertices to be extended and added to mesh.
97   void insertInternalVerticesSphere(BRepMesh::ListOfVertex& theNewVertices);
98
99   //! Calculates nodes lying on cylindrical surface.
100   //! @param theNewVertices list of vertices to be extended and added to mesh.
101   void insertInternalVerticesCylinder(BRepMesh::ListOfVertex& theNewVertices);
102
103   //! Calculates nodes lying on conical surface.
104   //! @param theNewVertices list of vertices to be extended and added to mesh.
105   void insertInternalVerticesCone(BRepMesh::ListOfVertex& theNewVertices);
106
107   //! Calculates nodes lying on toroidal surface.
108   //! @param theNewVertices list of vertices to be extended and added to mesh.
109   void insertInternalVerticesTorus(BRepMesh::ListOfVertex& theNewVertices);
110
111   //! Calculates nodes lying on Bezier/BSpline surface.
112   //! @param theNewVertices list of vertices to be extended and added to mesh.
113   void insertInternalVerticesBSpline(BRepMesh::ListOfVertex& theNewVertices);
114
115   //! Calculates nodes lying on custom-type surface.
116   //! @param theNewVertices list of vertices to be extended and added to mesh.
117   void insertInternalVerticesOther(BRepMesh::ListOfVertex& theNewVertices);
118   
119   //! Template method trying to insert new internal vertex corresponded to
120   //! the given 2d point. Calculates 3d position analytically using the given
121   //! surface.
122   //! @param thePnt2d 2d point to be inserted to the list.
123   //! @param theAnalyticSurface analytic surface to calculate 3d point.
124   //! @param[out] theVertices list of vertices to be updated.
125   template<class AnalyticSurface>
126   void tryToInsertAnalyticVertex(const gp_Pnt2d&         thePnt2d,
127                                  const AnalyticSurface&  theAnalyticSurface,
128                                  BRepMesh::ListOfVertex& theVertices)
129   {
130     const BRepMesh::HClassifier& aClassifier = myAttribute->ChangeClassifier();
131     if (aClassifier->Perform(thePnt2d) != TopAbs_IN)
132       return;
133
134     gp_Pnt aPnt;
135     ElSLib::D0(thePnt2d.X(), thePnt2d.Y(), theAnalyticSurface, aPnt);
136     insertVertex(aPnt, thePnt2d.Coord(), theVertices);
137   }
138
139   //! Creates new vertex with the given parameters.
140   //! @param thePnt3d 3d point corresponded to the vertex.
141   //! @param theUV UV point corresponded to the vertex.
142   //! @param[out] theVertices list of vertices to be updated.
143   void insertVertex(const gp_Pnt&           thePnt3d,
144                     const gp_XY&            theUV,
145                     BRepMesh::ListOfVertex& theVertices);
146
147   //! Stores mesh into the face (without internal edges).
148   void commitSurfaceTriangulation();
149
150   //! Performs initialization of data structure using existing data.
151   void initDataStructure();
152
153   //! Adds new link to the mesh data structure.
154   //! Movability of the link and order of nodes depend on orientation parameter.
155   void addLinkToMesh(const Standard_Integer   theFirstNodeId,
156                      const Standard_Integer   theLastNodeId,
157                      const TopAbs_Orientation theOrientation);
158
159   //! Inserts new node into a mesh in case if smoothed region build 
160   //! using the given node has better deflection metrics than source state.
161   //! @param thePnt3d 3d point corresponded to the vertex.
162   //! @param theUV UV point corresponded to the vertex.
163   //! @param isDeflectionCheckOnly if TRUE new node will not be added to a mesh
164   //! even if deflection parameter is better.
165   //! @param theTriangleDeflection deflection of a triangle from real geometry.
166   //! @param theFaceDeflection deflection to be achieved.
167   //! @param theCircleTool tool used for fast extraction of triangles 
168   //! touched by the given point.
169   //! @param[out] theVertices list of vertices to be updated.
170   //! @param[in out] theMaxTriangleDeflection maximal deflection of a mesh.
171   //! @return TRUE in case if the given deflection of triangle is fine and
172   //! there is no necessity to insert new node or new node was being inserted
173   //! successfully, FALSE in case if new configuration is better but 
174   //! isDeflectionCheckOnly flag is set.
175   Standard_Boolean checkDeflectionAndInsert(
176     const gp_Pnt&              thePnt3d,
177     const gp_XY&               theUV,
178     const Standard_Boolean     isDeflectionCheckOnly,
179     const Standard_Real        theTriangleDeflection,
180     const Standard_Real        theFaceDeflection,
181     const BRepMesh_CircleTool& theCircleTool,
182     BRepMesh::ListOfVertex&    theVertices,
183     Standard_Real&             theMaxTriangleDeflection);
184
185 private:
186
187   Standard_Real                          myAngle;
188   Standard_Boolean                       myInternalVerticesMode;
189   BRepMesh::IMapOfReal                   myUParam;
190   BRepMesh::IMapOfReal                   myVParam;
191
192   // Fast access to attributes of current face
193   Handle(BRepMesh_FaceAttribute)         myAttribute;
194   Handle(BRepMesh_DataStructureOfDelaun) myStructure;
195
196   Standard_Real                          myMinSize;
197   Standard_Boolean                       myIsControlSurfaceDeflection;
198 };
199
200 DEFINE_STANDARD_HANDLE (BRepMesh_FastDiscretFace, Standard_Transient)
201
202 #endif