5c99d494be2ca9f26d5438c7ce8bfcba7f3848e0
[occt.git] / src / BRepMesh / BRepMesh_FaceAttribute.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_FaceAttribute_HeaderFile
15 #define _BRepMesh_FaceAttribute_HeaderFile
16
17 #include <Standard.hxx>
18 #include <Standard_Transient.hxx>
19 #include <Standard_Type.hxx>
20
21 #include <BRepMesh_Status.hxx>
22 #include <BRepMesh.hxx>
23 #include <BRepMesh_DataStructureOfDelaun.hxx>
24
25 class BRepAdaptor_HSurface;
26
27 //! Auxiliary class for FastDiscret and FastDiscretFace classes.
28 class BRepMesh_FaceAttribute : public Standard_Transient
29 {
30 public:
31
32   //! Default constructor.
33   Standard_EXPORT BRepMesh_FaceAttribute();
34
35   //! Constructor.
36   //! @param theFace face the attribute is created for. 
37   //! Used for default initialization. Attribute keeps reference 
38   //! to the source face with forward orientation.
39   //! @param theBoundaryVertices shared map of shape vertices.
40   //! @param theBoundaryPoints shared discretization points of shape boundaries.
41   Standard_EXPORT BRepMesh_FaceAttribute(
42     const TopoDS_Face&                    theFace,
43     const BRepMesh::HDMapOfVertexInteger& theBoundaryVertices,
44     const BRepMesh::HDMapOfIntegerPnt&    theBoundaryPoints);
45
46   //! Destructor.
47   Standard_EXPORT virtual ~BRepMesh_FaceAttribute();
48
49 public: //! @name main geometrical properties.
50
51   //! Returns face's surface.
52   inline const Handle(BRepAdaptor_HSurface)& Surface() const
53   {
54     return mySurface;
55   }
56
57   //! Sets reference face.
58   inline void SetFace(const TopoDS_Face& theFace)
59   {
60     myFace = theFace;
61   }
62
63   //! Returns forward oriented face to be used for calculations.
64   inline const TopoDS_Face& Face() const
65   {
66     return myFace;
67   }
68
69   //! Sets boundary vertices map.
70   inline void SetBoundaryVertices(const BRepMesh::HDMapOfVertexInteger& theVertices)
71   {
72     myBoundaryVertices = theVertices;
73   }
74
75   //! Sets boundary points map.
76   inline void SetBoundaryPoints(const BRepMesh::HDMapOfIntegerPnt& theBoundaryPoints)
77   {
78     myBoundaryPoints = theBoundaryPoints;
79   }
80
81   //! Returns U tolerance of face calculated regarding its parameters.
82   Standard_EXPORT Standard_Real ToleranceU() const;
83   
84   //! Returns V tolerance of face calculated regarding its parameters.
85   Standard_EXPORT Standard_Real ToleranceV() const;
86   
87   //! Gives face deflection parameter.
88   inline Standard_Real GetDefFace() const
89   {
90     return myDefFace;
91   }
92
93   //! Sets face deflection.
94   inline void SetDefFace(const Standard_Real theDefFace)
95   {
96     myDefFace = theDefFace;
97   }
98
99   //! Gives minimal value in U domain.
100   inline Standard_Real GetUMin() const
101   {
102     return myUMin;
103   }
104
105   //! Sets minimal value in U domain.
106   inline void SetUMin(const Standard_Real theUMin)
107   {
108     myUMin = theUMin;
109   }
110
111   //! Gives minimal value in V domain.
112   inline Standard_Real GetVMin() const
113   {
114     return myVMin;
115   }
116
117   //! Sets minimal value in V domain.
118   inline void SetVMin(const Standard_Real theVMin)
119   {
120     myVMin = theVMin;
121   }
122
123   //! Gives maximal value in U domain.
124   inline Standard_Real GetUMax() const
125   {
126     return myUMax;
127   }
128
129   //! Sets maximal value in U domain.
130   inline void SetUMax(const Standard_Real theUMax)
131   {
132     myUMax = theUMax;
133   }
134
135   //! Gives maximal value in V domain.
136   inline Standard_Real GetVMax() const
137   {
138     return myVMax;
139   }
140
141   //! Sets maximal value in V domain.
142   inline void SetVMax(const Standard_Real theVMax)
143   {
144     myVMax = theVMax;
145   }
146
147   //! Gives value of step in U domain.
148   inline Standard_Real GetDeltaX() const
149   {
150     return myDeltaX;
151   }
152
153   //! Sets value of step in U domain.
154   inline void SetDeltaX(const Standard_Real theDeltaX)
155   {
156     myDeltaX = theDeltaX;
157   }
158
159   //! Gives value of step in V domain.
160   inline Standard_Real GetDeltaY() const
161   {
162     return myDeltaY;
163   }
164
165   //! Sets value of step in V domain.
166   inline void SetDeltaY(const Standard_Real theDeltaY)
167   {
168     myDeltaY = theDeltaY;
169   }
170
171   //! Sets set of status flags for this face.
172   inline Standard_Integer GetStatus() const
173   {
174     return myStatus;
175   }
176
177   //! Sets status flag for this face.
178   inline void SetStatus(const BRepMesh_Status theStatus)
179   {
180     myStatus |= theStatus;
181   }
182
183   //! Returns TRUE in case if computed data is valid.
184   inline Standard_Boolean IsValid() const
185   {
186     return (myStatus == BRepMesh_NoError || myStatus == BRepMesh_ReMesh);
187   }
188
189 public: //! @name auxiliary structures
190
191   //! Clear face attribute.
192   Standard_EXPORT void Clear();
193
194   //! Gives reference to map of internal edges of face.
195   inline BRepMesh::HDMapOfShapePairOfPolygon& ChangeInternalEdges()
196   {
197     return myInternalEdges;
198   }
199
200   //! Gives reference to map of 2D points of discretization.
201   inline BRepMesh::HDMapOfIntegerListOfXY& ChangeLocation2D()
202   {
203     return myLocation2D;
204   }
205
206   //! Gives reference to map of 3D points of discretization.
207   inline BRepMesh::HDMapOfIntegerPnt& ChangeSurfacePoints()
208   {
209     return mySurfacePoints;
210   }
211
212   //! Gives reference to map of vertices of discretization.
213   inline BRepMesh::HDMapOfVertexInteger& ChangeSurfaceVertices()
214   {
215     return mySurfaceVertices;
216   }
217
218   //! Gives reference on map of (vertex, edge) pairs of face.
219   inline BRepMesh::HIMapOfInteger& ChangeVertexEdgeMap()
220   {
221     return myVertexEdgeMap;
222   }
223
224   //! Gives Delaunay data structure.
225   inline Handle(BRepMesh_DataStructureOfDelaun)& ChangeStructure()
226   {
227     return myStructure;
228   }
229
230   //! Returns classifier.
231   inline BRepMesh::HClassifier& ChangeClassifier()
232   {
233     return myClassifier;
234   }
235
236   //! Returns mesh nodes calculated for boundaries.
237   inline BRepMesh::HVectorOfVertex& ChangeMeshNodes()
238   {
239     return myMeshNodes;
240   }
241
242 public: //! @name Point/Vertex/Node manipulators
243
244   //! Gives the number of different locations in 3D space.
245   inline Standard_Integer LastPointId() const
246   {
247     return (myBoundaryPoints.IsNull() ? 0 : myBoundaryPoints->Extent()) + 
248       (mySurfacePoints.IsNull() ? 0 : mySurfacePoints->Extent());
249   }
250
251   //! Gives the 3D location of the vertex.
252   inline const gp_Pnt& GetPoint(const BRepMesh_Vertex& theVertex) const
253   {
254     return GetPoint(theVertex.Location3d());
255   }
256
257   //! Gives the 3D location of the vertex.
258   inline const gp_Pnt& GetPoint(const Standard_Integer theIndex) const
259   {
260     if (!mySurfacePoints.IsNull() && theIndex > myBoundaryPoints->Extent())
261       return mySurfacePoints->Find(theIndex);
262
263     return myBoundaryPoints->Find(theIndex);
264   }
265
266   //! Returns index of the given vertex if it exists in cache, 
267   //! elsewhere adds it to cache and returns cached index.
268   //! @param theVertexExplorer template parameter intended to transfer
269   //! parameters of vertex to method. Explorer class can implement different
270   //! approaches of extraction of target parameters.
271   //! @param isFillEdgeVertices if TRUE adds vertex to shared map of 
272   //! edges vertices, elsewhere adds it map of face vertices.
273   template<class HVertexExplorer>
274     Standard_Integer GetVertexIndex(
275       const HVertexExplorer& theVertexExplorer,
276       const Standard_Boolean isFillEdgeVertices = Standard_False)
277   {
278     const TopoDS_Vertex& aVertex = theVertexExplorer->Vertex();
279     Standard_Integer aNewVertexIndex = 0;
280     if (getVertexIndex(aVertex, aNewVertexIndex))
281       return aNewVertexIndex;
282
283     if (!theVertexExplorer->IsSameUV() ||
284         !getVertexIndex(theVertexExplorer->SameVertex(), aNewVertexIndex))
285     {
286       aNewVertexIndex = LastPointId() + 1;
287
288       BRepMesh::DMapOfIntegerPnt& aPointsMap = isFillEdgeVertices ?
289         *myBoundaryPoints : *mySurfacePoints;
290
291       aPointsMap.Bind(aNewVertexIndex, theVertexExplorer->Point());
292     }
293
294     BRepMesh::DMapOfVertexInteger& aVertexMap = isFillEdgeVertices ?
295       *myBoundaryVertices : *mySurfaceVertices;
296
297     aVertexMap.Bind(aVertex, aNewVertexIndex);
298
299     return aNewVertexIndex;
300   }
301
302   //! Adds node with the given parameters to mesh.
303   //! @param theIndex index of 3D point corresponded to the node.
304   //! @param theUV node position.
305   //! @param theMovability movability of a node.
306   //! @param theNodeIndex index of vertex in mesh structure.
307   //! @param theNodeOnEdgeIndex ordered index of node on the boundary.
308   Standard_EXPORT void AddNode(const Standard_Integer         theIndex,
309                                const gp_XY&                   theUV,
310                                const BRepMesh_DegreeOfFreedom theMovability,
311                                Standard_Integer&              theNodeIndex,
312                                Standard_Integer&              theNodeOnEdgeIndex);
313
314 public: //! @name Auxiliary methods
315   
316   //! Scales the given point from real parametric space 
317   //! to face basis and otherwise.
318   //! @param thePoint2d point to be scaled.
319   //! @param isToFaceBasis if TRUE converts point to face basis,
320   //! otherwise performs reverse conversion.
321   //! @return scaled point.
322   Standard_EXPORT gp_XY Scale(const gp_XY&           thePoint2d, 
323                               const Standard_Boolean isToFaceBasis);
324
325   DEFINE_STANDARD_RTTI(BRepMesh_FaceAttribute, Standard_Transient)
326
327 private:
328
329   //! Assignment operator.
330   void operator =(const BRepMesh_FaceAttribute& /*theOther*/)
331   {
332   }
333
334   //! Initializes internal data structures.
335   void init();
336
337   //! Computes parametric tolerance of a face regarding the given limits.
338   Standard_Real computeParametricTolerance(
339     const Standard_Real theFirstParam,
340     const Standard_Real theLastParam) const;
341
342   //! Clears internal data structures local to face.
343   void clearLocal(
344     const Standard_Boolean isClearSurfaceDataOnly = Standard_False);
345
346   //! Returns index of the given vertex if it exists in cache.
347   //! @param theVertex vertex which index should be retrieved.
348   //! @param theVertexIndex index of the given vertex.
349   //! @return TRUE if cached value is found, FALSE elsewhere.
350   Standard_EXPORT Standard_Boolean getVertexIndex(
351     const TopoDS_Vertex& theVertex,
352     Standard_Integer&    theVertexIndex) const;
353
354 private:
355
356   Standard_Real                           myDefFace;       //!< Restore face deflection
357   Standard_Real                           myUMin;          //!< Describes minimal value in U domain
358   Standard_Real                           myUMax;          //!< Describes maximal value in U domain
359   Standard_Real                           myVMin;          //!< Describes minimal value in V domain
360   Standard_Real                           myVMax;          //!< Describes maximal value in V domain
361   Standard_Real                           myDeltaX;
362   Standard_Real                           myDeltaY;
363   Standard_Integer                        myStatus;
364
365   BRepMesh::HDMapOfVertexInteger          myBoundaryVertices;
366   BRepMesh::HDMapOfIntegerPnt             myBoundaryPoints;
367
368   TopoDS_Face                             myFace;
369   Handle(BRepAdaptor_HSurface)            mySurface;
370   BRepMesh::HClassifier                   myClassifier;
371
372   BRepMesh::HDMapOfShapePairOfPolygon     myInternalEdges;
373
374   BRepMesh::HDMapOfIntegerListOfXY        myLocation2D;
375   BRepMesh::HIMapOfInteger                myVertexEdgeMap;
376
377   // This field is intended to keep calculated mesh nodes to prevent
378   // extremely high memory consumption in case if the whole structure is kept.
379   BRepMesh::HVectorOfVertex               myMeshNodes;
380
381   BRepMesh::HDMapOfVertexInteger          mySurfaceVertices;
382   BRepMesh::HDMapOfIntegerPnt             mySurfacePoints;
383   Handle(BRepMesh_DataStructureOfDelaun)  myStructure;
384 };
385
386 DEFINE_STANDARD_HANDLE(BRepMesh_FaceAttribute, Standard_Transient)
387
388 #endif