e09cc42275c47bd3f393aa34aea0b0094f56f44f
[occt.git] / src / BRepMesh / BRepMesh_DataStructureOfDelaun.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_DataStructureOfDelaun_HeaderFile
15 #define _BRepMesh_DataStructureOfDelaun_HeaderFile
16
17 #include <Standard.hxx>
18 #include <Standard_DefineHandle.hxx>
19 #include <BRepMesh_VertexTool.hxx>
20 #include <Standard_Transient.hxx>
21 #include <BRepMesh_Triangle.hxx>
22 #include <BRepMesh_PairOfIndex.hxx>
23 #include <Standard_OStream.hxx>
24 #include <BRepMesh.hxx>
25
26 class BRepMesh_Vertex;
27 class BRepMesh_VertexTool;
28 class BRepMesh_Edge;
29
30 //! Describes the data structure necessary for the mesh algorithms in 
31 //! two dimensions plane or on surface by meshing in UV space.
32 class BRepMesh_DataStructureOfDelaun : public Standard_Transient
33 {
34 public:
35
36   //! Constructor.
37   //! @param theAllocator memory allocator to be used by internal structures.
38   //! @param theReservedNodeSize presumed number of nodes in this mesh.
39   Standard_EXPORT BRepMesh_DataStructureOfDelaun(
40     const Handle(NCollection_IncAllocator)& theAllocator,
41     const Standard_Integer                  theReservedNodeSize = 100);
42
43
44
45 public: //! @name API for accessing mesh nodes.
46
47   //! Returns number of nodes.
48   inline Standard_Integer NbNodes() const
49   {
50     return myNodes->Extent();
51   }
52
53
54   //! Adds node to the mesh if it is not already in the mesh.
55   //! @param theNode node to be added to the mesh.
56   //! @param isForceAdd adds the given node to structure without 
57   //! checking on coincidence with other nodes.
58   //! @return index of the node in the structure.
59   Standard_EXPORT Standard_Integer AddNode(
60     const BRepMesh_Vertex& theNode,
61     const Standard_Boolean isForceAdd = Standard_False);
62
63   //! Finds the index of the given node.
64   //! @param theNode node to find.
65   //! @return index of the given element of zero if node is not in the mesh.
66   Standard_EXPORT Standard_Integer IndexOf(const BRepMesh_Vertex& theNode)
67   {
68     return myNodes->FindIndex(theNode);
69   }
70
71   //! Get node by the index.
72   //! @param theIndex index of a node.
73   //! @return node with the given index.
74   inline const BRepMesh_Vertex& GetNode(const Standard_Integer theIndex)
75   {
76     return myNodes->FindKey(theIndex);
77   }
78
79   //! Alias for GetNode.
80   const BRepMesh_Vertex& operator ()(const Standard_Integer theIndex)
81   {
82     return GetNode(theIndex);
83   }
84
85   //! Substitutes the node with the given index by new one.
86   //! @param theIndex index of node to be substituted.
87   //! @param theNewNode substituting node.
88   //! @return FALSE in case if new node is already in the structure, TRUE elsewhere.
89   Standard_EXPORT Standard_Boolean SubstituteNode(
90     const Standard_Integer theIndex,
91     const BRepMesh_Vertex& theNewNode);
92
93   //! Removes node from the mesh in case if it has no connected links 
94   //! and its type is Free.
95   //! @param theIndex index of node to be removed.
96   //! @param isForce if TRUE node will be removed even if movability
97   //! is not Free.
98   Standard_EXPORT void RemoveNode(const Standard_Integer theIndex,
99                                   const Standard_Boolean isForce = Standard_False)
100   {
101     if (isForce || myNodes->FindKey(theIndex).Movability() == BRepMesh_Free)
102     {
103       if (LinksConnectedTo(theIndex).Extent()==0)
104         myNodes->Delete(theIndex);
105     }
106   }
107
108   //! Get list of links attached to the node with the given index.
109   //! @param theIndex index of node whose links should be retrieved.
110   //! @return list of links attached to the node.
111   inline const BRepMesh::ListOfInteger& LinksConnectedTo(
112     const Standard_Integer theIndex) const
113   {
114     return linksConnectedTo(theIndex);
115   }
116
117
118 public: //! @name API for accessing mesh links.
119
120   //! Returns number of links.
121   inline Standard_Integer NbLinks() const
122   {
123     return myLinks.Extent();
124   }
125
126   //! Adds link to the mesh if it is not already in the mesh.
127   //! @param theLink link to be added to the mesh.
128   //! @return index of the link in the structure.
129   Standard_EXPORT Standard_Integer AddLink(const BRepMesh_Edge& theLink);
130
131   //! Finds the index of the given link.
132   //! @param theLink link to find.
133   //! @return index of the given element of zero if link is not in the mesh.
134   Standard_EXPORT Standard_Integer IndexOf(const BRepMesh_Edge& theLink) const
135   {
136     return myLinks.FindIndex(theLink);
137   }
138
139   //! Get link by the index.
140   //! @param theIndex index of a link.
141   //! @return link with the given index.
142   Standard_EXPORT const BRepMesh_Edge& GetLink(const Standard_Integer theIndex)
143   {
144     return myLinks.FindKey(theIndex);
145   }
146
147   //! Returns map of indices of links registered in mesh.
148   inline const BRepMesh::MapOfInteger& LinksOfDomain() const
149   {
150     return myLinksOfDomain;
151   }
152
153   //! Substitutes the link with the given index by new one.
154   //! @param theIndex index of link to be substituted.
155   //! @param theNewLink substituting link.
156   //! @return FALSE in case if new link is already in the structure, TRUE elsewhere.
157   Standard_EXPORT Standard_Boolean SubstituteLink(const Standard_Integer theIndex,
158                                                   const BRepMesh_Edge&   theNewLink);
159
160   //! Removes link from the mesh in case if it has no connected elements 
161   //! and its type is Free.
162   //! @param theIndex index of link to be removed.
163   //! @param isForce if TRUE link will be removed even if movability
164   //! is not Free.
165   Standard_EXPORT void RemoveLink(const Standard_Integer theIndex,
166                                   const Standard_Boolean isForce = Standard_False);
167
168   //! Returns indices of elements conected to the link with the given index.
169   //! @param theLinkIndex index of link whose data should be retrieved.
170   //! @return indices of elements conected to the link.
171   Standard_EXPORT const BRepMesh_PairOfIndex& ElementsConnectedTo(
172     const Standard_Integer theLinkIndex) const
173   {
174     return myLinks.FindFromIndex(theLinkIndex);
175   }
176
177
178
179 public: //! @name API for accessing mesh elements.
180
181   //! Returns number of links.
182   inline Standard_Integer NbElements() const
183   {
184     return myElements.Extent();
185   }
186
187   //! Adds element to the mesh if it is not already in the mesh.
188   //! @param theElement element to be added to the mesh.
189   //! @return index of the element in the structure.
190   Standard_EXPORT Standard_Integer AddElement(const BRepMesh_Triangle& theElement);
191
192   //! Finds the index of the given element.
193   //! @param theElement element to find.
194   //! @return index of the given element of zero if element is not in the mesh.
195   Standard_EXPORT Standard_Integer IndexOf(const BRepMesh_Triangle& theElement) const
196   {
197     return myElements.FindIndex(theElement);
198   }
199
200   //! Get element by the index.
201   //! @param theIndex index of an element.
202   //! @return element with the given index.
203   Standard_EXPORT const BRepMesh_Triangle& GetElement(const Standard_Integer theIndex)
204   {
205     return myElements.FindKey(theIndex);
206   }
207
208   //! Returns map of indices of elements registered in mesh.
209   inline const BRepMesh::MapOfInteger& ElementsOfDomain() const
210   {
211     return myElementsOfDomain;
212   }
213
214   //! Substitutes the element with the given index by new one.
215   //! @param theIndex index of element to be substituted.
216   //! @param theNewLink substituting element.
217   //! @return FALSE in case if new element is already in the structure, TRUE elsewhere.
218   Standard_EXPORT Standard_Boolean SubstituteElement(const Standard_Integer   theIndex,
219                                                      const BRepMesh_Triangle& theNewElement);
220
221   //! Removes element from the mesh.
222   //! @param theIndex index of element to be removed.
223   Standard_EXPORT void RemoveElement(const Standard_Integer theIndex);
224
225   //! Returns indices of nodes forming the given element.
226   //! @param theElement element which nodes should be retrieved.
227   //! @param[out] theNodes nodes of the given element.
228   Standard_EXPORT void ElementNodes(
229     const BRepMesh_Triangle& theElement,
230     Standard_Integer         (&theNodes)[3]);
231
232
233
234 public: //! @name Auxilary API
235
236   //! Dumps information about this structure.
237   //! @param theStream stream to be used for dump.
238   Standard_EXPORT void Statistics(Standard_OStream& theStream) const;
239   
240   //! Returns memory allocator used by the structure.
241   inline const Handle(NCollection_IncAllocator)& Allocator() const
242   {
243     return myAllocator;
244   }
245
246   //! Gives the data structure for initialization of cell size and tolerance.
247   inline BRepMesh::HVertexTool& Data()
248   {
249     return myNodes;
250   }
251
252   //! Removes all elements.
253   Standard_EXPORT void ClearDomain();
254
255   //! Substitutes deleted items by the last one from corresponding map 
256   //! to have only non-deleted elements, links or nodes in the structure.
257   Standard_EXPORT void ClearDeleted()
258   {
259     clearDeletedLinks();
260     clearDeletedNodes();
261   }
262
263   DEFINE_STANDARD_RTTI(BRepMesh_DataStructureOfDelaun)
264
265 private: 
266
267   //! Get list of links attached to the node with the given index.
268   //! @param theIndex index of node whose links should be retrieved.
269   //! @return list of links attached to the node.
270   inline BRepMesh::ListOfInteger& linksConnectedTo(
271     const Standard_Integer theIndex) const
272   {
273     return (BRepMesh::ListOfInteger&)myNodeLinks.Find(theIndex);
274   }
275
276   //! Substitutes deleted links by the last one from corresponding map 
277   //! to have only non-deleted links in the structure.
278   Standard_EXPORT void clearDeletedLinks();
279
280   //! Substitutes deleted nodes by the last one from corresponding map 
281   //! to have only non-deleted nodes in the structure.
282   Standard_EXPORT void clearDeletedNodes();
283
284   //! Cleans dependent structures from the given link.
285   //! @param theIndex index of link in the data structure.
286   //! @param theLink reference to the link to avoid double accessing 
287   //! to map of links.
288   void cleanLink(const Standard_Integer theIndex,
289                  const BRepMesh_Edge&   theLink);
290
291   //! Cleans dependent structures from the given element.
292   //! @param theIndex index of element in the data structure.
293   //! @param theElement reference to the element to avoid double accessing 
294   //! to map of elements.
295   void cleanElement(const Standard_Integer   theIndex,
296                     const BRepMesh_Triangle& theElement);
297
298   //! Removes element index from the given pair. Used by cleanElement.
299   //! @param theIndex index of element to be removed.
300   //! @param thePair pair of elements to be cleaned.
301   void removeElementIndex(const Standard_Integer theIndex,
302                           BRepMesh_PairOfIndex&  thePair);
303
304
305 private:
306
307   Handle(NCollection_IncAllocator)      myAllocator;
308   BRepMesh::HVertexTool                 myNodes;
309   BRepMesh::DMapOfIntegerListOfInteger  myNodeLinks;
310   BRepMesh::IDMapOfLink                 myLinks;
311   BRepMesh::ListOfInteger               myDelLinks;
312   BRepMesh::IMapOfElement               myElements;
313   BRepMesh::MapOfInteger                myElementsOfDomain;
314   BRepMesh::MapOfInteger                myLinksOfDomain;
315 };
316
317 DEFINE_STANDARD_HANDLE(BRepMesh_DataStructureOfDelaun,Standard_Transient)
318
319 #endif