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