SALOME - SMESH
SMESH_Pattern.hxx
Go to the documentation of this file.
1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File : SMESH_Pattern.hxx
23 // Created : Mon Aug 2 10:30:00 2004
24 // Author : Edward AGAPOV (eap)
25 //
26 #ifndef SMESH_Pattern_HeaderFile
27 #define SMESH_Pattern_HeaderFile
28 
29 #include "SMESH_SMESH.hxx"
30 
31 #include <vector>
32 #include <list>
33 #include <map>
34 #include <set>
35 #include <iostream>
36 
37 #include <TopoDS_Shape.hxx>
38 #include <TopTools_IndexedMapOfOrientedShape.hxx>
39 #include <gp_XYZ.hxx>
40 #include <gp_XY.hxx>
41 #include <gp_Pnt.hxx>
42 
43 #ifdef __BORLANDC__
44 #include <TopoDS_Edge.hxx>
45 #endif
46 
47 class SMDS_MeshElement;
48 class SMDS_MeshFace;
49 class SMDS_MeshVolume;
50 class SMDS_MeshNode;
51 class SMESH_Mesh;
52 class SMESHDS_SubMesh;
53 class TopoDS_Shell;
54 class TopoDS_Vertex;
55 class TopoDS_Face;
56 
57 #ifndef __BORLANDC__
58 class TopoDS_Edge;
59 #endif
60 
61 //
62 // Class allowing meshing by mapping of pre-defined patterns: it generates
63 // a 2D mesh on a geometrical face or a 3D mesh inside a geometrical block
64 // of 6 faces.
65 //
66 
68  public:
69 
70  SMESH_Pattern ();
71 
72  void Clear();
73  // clear fields
74 
75  bool Load (const char* theFileContents);
76  // Load a pattern from <theFileContents>
77 
78  bool Load (SMESH_Mesh* theMesh,
79  const TopoDS_Face& theFace,
80  bool theProject = false);
81  // Create a pattern from the mesh built on <theFace>.
82  // <theProject>==true makes override nodes positions
83  // on <theFace> computed by mesher
84 
85  bool Load (SMESH_Mesh* theMesh,
86  const TopoDS_Shell& theBlock);
87  // Create a pattern from the mesh built on <theBlock>
88 
89  bool Save (std::ostream& theFile);
90  // Save the loaded pattern into theFile
91 
92  bool Apply (const TopoDS_Face& theFace,
93  const TopoDS_Vertex& theVertexOnKeyPoint1,
94  const bool theReverse);
95  // Compute nodes coordinates applying
96  // the loaded pattern to <theFace>. The first key-point
97  // will be mapped into <theVertexOnKeyPoint1>, which must
98  // be in the outer wire of theFace
99 
100  bool Apply (const TopoDS_Shell& theBlock,
101  const TopoDS_Vertex& theVertex000,
102  const TopoDS_Vertex& theVertex001);
103  // Compute nodes coordinates applying
104  // the loaded pattern to <theBlock>. The (0,0,0) key-point
105  // will be mapped into <theVertex000>. The
106  // (0,0,1) key-point will be mapped into <theVertex001>.
107 
108  bool Apply (const SMDS_MeshFace* theFace,
109  const int theNodeIndexOnKeyPoint1,
110  const bool theReverse);
111  // Compute nodes coordinates applying
112  // the loaded pattern to <theFace>. The first key-point
113  // will be mapped into <theNodeIndexOnKeyPoint1>-th node
114 
115  bool Apply (SMESH_Mesh* theMesh,
116  const SMDS_MeshFace* theFace,
117  const TopoDS_Shape& theSurface,
118  const int theNodeIndexOnKeyPoint1,
119  const bool theReverse);
120  // Compute nodes coordinates applying
121  // the loaded pattern to <theFace>. The first key-point
122  // will be mapped into <theNodeIndexOnKeyPoint1>-th node
123 
124  bool Apply (SMESH_Mesh* theMesh,
125  std::set<const SMDS_MeshFace*>& theFaces,
126  const int theNodeIndexOnKeyPoint1,
127  const bool theReverse);
128  // Compute nodes coordinates applying
129  // the loaded pattern to <theFaces>. The first key-point
130  // will be mapped into <theNodeIndexOnKeyPoint1>-th node
131 
132  bool Apply (const SMDS_MeshVolume* theVolume,
133  const int theNode000Index,
134  const int theNode001Index);
135  // Compute nodes coordinates applying
136  // the loaded pattern to <theVolume>. The (0,0,0) key-point
137  // will be mapped into <theNode000Index>-th node. The
138  // (0,0,1) key-point will be mapped into <theNode000Index>-th
139  // node.
140 
141  bool Apply (std::set<const SMDS_MeshVolume*>& theVolumes,
142  const int theNode000Index,
143  const int theNode001Index);
144  // Compute nodes coordinates applying
145  // the loaded pattern to <theVolumes>. The (0,0,0) key-point
146  // will be mapped into <theNode000Index>-th node. The
147  // (0,0,1) key-point will be mapped into <theNode000Index>-th
148  // node.
149 
150  bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
151  // Return nodes coordinates computed by Apply() method
152 
153  bool MakeMesh(SMESH_Mesh* theMesh,
154  const bool toCreatePolygons = false,
155  const bool toCreatePolyedrs = false);
156  // Create nodes and elements in <theMesh> using nodes
157  // coordinates computed by either of Apply...() methods
158 
159  // ----------
160  // Inquiries
161  // ----------
162 
163  enum ErrorCode {
165  // Load(file)
166  ERR_READ_NB_POINTS, // couldn't read nb of points
167  ERR_READ_POINT_COORDS, // invalid nb of point coordinates
168  ERR_READ_TOO_FEW_POINTS, // too few points in a pattern
169  ERR_READ_3D_COORD, // coordinate of 3D point out of [0,1] range
170  ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
171  ERR_READ_BAD_INDEX, // invalid point index
172  ERR_READ_ELEM_POINTS, // invalid nb of points in element
173  ERR_READ_NO_ELEMS, // no elements in a pattern
174  ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
175  // Save(file)
176  ERR_SAVE_NOT_LOADED, // pattern was not loaded
177  // Load(shape)
178  ERR_LOAD_EMPTY_SUBMESH, // no elements to load
179  // Load(face)
180  ERR_LOADF_NARROW_FACE, // too narrow face
181  ERR_LOADF_CLOSED_FACE, // closed face
182  ERR_LOADF_CANT_PROJECT, // impossible to project nodes
183  // Load(volume)
184  ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
185  ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
186  // Apply(shape)
187  ERR_APPL_NOT_COMPUTED, // mapping failed
188  ERR_APPL_NOT_LOADED, // pattern was not loaded
189  ERR_APPL_BAD_DIMENTION, // wrong shape dimention
190  ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
191  // Apply(face)
192  ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
193  ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
194  ERR_APPLF_INTERNAL_EEROR, // program error
195  // Apply(volume)
196  ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
197  // Apply(mesh_face)
198  ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
199  // MakeMesh
200  ERR_MAKEM_NOT_COMPUTED // mapping failed
201  };
202 
203  ErrorCode GetErrorCode() const { return myErrorCode; }
204  // return ErrorCode of the last operation
205 
206  bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
207  // Return true if a pattern was successfully loaded
208 
209  bool Is2D() const { return myIs2D; }
210  // Return true if the loaded pattern is a 2D one
211 
212  bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
213  // Return nodes coordinates of the pattern
214 
215  const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
216  // Return indices of key-points within the sequences returned by
217  // GetPoints() and GetMappedPoints()
218 
219  const std::list< std::list< int > >& GetElementPointIDs (bool applied) const
220  { return myElemXYZIDs.empty() || !applied ? myElemPointIDs : myElemXYZIDs; }
221  // Return nodal connectivity of the elements of the pattern
222 
223  void DumpPoints() const;
224  // Debug
225 
226  // -----------------------------
227  // Utilities for advanced usage
228  // -----------------------------
229 
230  TopoDS_Shape GetSubShape( const int i ) const {
231  if ( i < 1 || i > myShapeIDMap.Extent() ) return TopoDS_Shape();
232  return myShapeIDMap( i );
233  }
234  // Return a shape from myShapeIDMap where shapes are indexed so that first go
235  // ordered vertices, then ordered edge, then faces and maybe a shell
236 
237 private:
238  // private methods
239 
240  struct TPoint {
241  gp_XYZ myInitXYZ; // loaded postion
242  gp_XY myInitUV;
243  double myInitU; // [0,1]
244  gp_Pnt myXYZ; // position to compute
245  gp_XY myUV;
246  double myU;
247  TPoint();
248  };
249  friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
250 
251  bool setErrorCode( const ErrorCode theErrorCode )
252  { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
253  // set ErrorCode and return true if it is Ok
254 
255  bool setShapeToMesh(const TopoDS_Shape& theShape);
256  // Set a shape to be meshed. Return True if meshing is possible
257 
258  std::list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
259  // Return list of points located on theShape.
260  // A list of edge-points include vertex-points (for 2D pattern only).
261  // A list of face-points doesnt include edge-points.
262  // A list of volume-points doesnt include face-points.
263 
264  std::list< TPoint* > & getShapePoints(const int theShapeID);
265  // Return list of points located on the shape
266 
267  bool findBoundaryPoints();
268  // If loaded from file, find points to map on edges and faces and
269  // compute their parameters
270 
271  void arrangeBoundaries (std::list< std::list< TPoint* > >& boundaryPoints);
272  // if there are several wires, arrange boundaryPoints so that
273  // the outer wire goes first and fix inner wires orientation;
274  // update myKeyPointIDs to correspond to the order of key-points
275  // in boundaries; sort internal boundaries by the nb of key-points
276 
277  void computeUVOnEdge( const TopoDS_Edge& theEdge, const std::list< TPoint* > & ePoints );
278  // compute coordinates of points on theEdge
279 
280  bool compUVByIsoIntersection (const std::list< std::list< TPoint* > >& boundaryPoints,
281  const gp_XY& theInitUV,
282  gp_XY& theUV,
283  bool & theIsDeformed);
284  // compute UV by intersection of iso-lines found by points on edges
285 
286  bool compUVByElasticIsolines(const std::list< std::list< TPoint* > >& boundaryPoints,
287  const std::list< TPoint* >& pointsToCompute);
288  // compute UV as nodes of iso-poly-lines consisting of
289  // segments keeping relative size as in the pattern
290 
291  double setFirstEdge (std::list< TopoDS_Edge > & theWire, int theFirstEdgeID);
292  // choose the best first edge of theWire; return the summary distance
293  // between point UV computed by isolines intersection and
294  // eventual UV got from edge p-curves
295 
296  typedef std::list< std::list< TopoDS_Edge > > TListOfEdgesList;
297 
298  bool sortSameSizeWires (TListOfEdgesList & theWireList,
299  const TListOfEdgesList::iterator& theFromWire,
300  const TListOfEdgesList::iterator& theToWire,
301  const int theFirstEdgeID,
302  std::list< std::list< TPoint* > >& theEdgesPointsList );
303  // sort wires in theWireList from theFromWire until theToWire,
304  // the wires are set in the order to correspond to the order
305  // of boundaries; after sorting, edges in the wires are put
306  // in a good order, point UVs on edges are computed and points
307  // are appended to theEdgesPointsList
308 
309  typedef std::set<const SMDS_MeshNode*> TNodeSet;
310 
311  void mergePoints (const bool uniteGroups);
312  // Merge XYZ on edges and/or faces.
313 
314  void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
315  const bool toCreatePolygons,
316  const bool toCreatePolyedrs);
317  // prepare intermediate data to create Polygons and Polyhedrons
318 
319  void createElements(SMESH_Mesh* theMesh,
320  const std::vector<const SMDS_MeshNode* >& theNodesVector,
321  const std::list< std::list< int > > & theElemNodeIDs,
322  const std::vector<const SMDS_MeshElement*>& theElements);
323  // add elements to the mesh
324 
325  bool getFacesDefinition(const SMDS_MeshNode** theBndNodes,
326  const int theNbBndNodes,
327  const std::vector< const SMDS_MeshNode* >& theNodes,
328  std::list< int >& theFaceDefs,
329  std::vector<int>& theQuantity);
330  // fill faces definition for a volume face defined by theBndNodes
331  // return true if a face definition changes
332 
333 
334  bool isReversed(const SMDS_MeshNode* theFirstNode,
335  const std::list< int >& theIdsList) const;
336  // check xyz ids order in theIdsList taking into account
337  // theFirstNode on a link
338 
339  void clearMesh(SMESH_Mesh* theMesh) const;
340  // clear mesh elements existing on myShape in theMesh
341 
342  static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh* theMesh,
343  const TopoDS_Shape& theShape);
344  // return submesh containing elements bound to theShape in theMesh
345 
346  private:
347  // fields
348 
349  typedef std::list< int > TElemDef; // element definition is its nodes ids
350 
351  bool myIs2D;
352  std::vector< TPoint > myPoints;
353  std::list< int > myKeyPointIDs;
354  std::list< TElemDef > myElemPointIDs;
355 
359 
360  TopoDS_Shape myShape;
361  // all functions assure that shapes are indexed so that first go
362  // ordered vertices, then ordered edge, then faces and maybe a shell
363  TopTools_IndexedMapOfOrientedShape myShapeIDMap;
364  std::map< int, std::list< TPoint* > > myShapeIDToPointsMap;
365 
366  // for the 2d case:
367  // nb of key-points in each of pattern boundaries
368  std::list< int > myNbKeyPntInBoundary;
369 
370 
371  // to compute while applying to mesh elements, not to shapes
372 
373  std::vector<gp_XYZ> myXYZ; // XYZ of nodes to create
374  std::list< TElemDef > myElemXYZIDs; // new elements definitions
375  std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
376  std::vector<const SMDS_MeshElement*> myElements; // refined elements
377  std::vector<const SMDS_MeshNode*> myOrderedNodes;
378 
379  // elements to replace with polygon or polyhedron
380  std::vector<const SMDS_MeshElement*> myPolyElems;
381  // definitions of new poly elements
382  std::list< TElemDef > myPolyElemXYZIDs;
383  std::list< std::vector<int> > myPolyhedronQuantities;
384 
385  // map a boundary to XYZs on it;
386  // a boundary (edge or face) is defined as a set of its nodes,
387  // XYZs on a boundary are indices of myXYZ s
388  std::map<TNodeSet,std::list<std::list<int> > > myIdsOnBoundary;
389  // map XYZ id to element it is in
390  std::map< int, std::list< TElemDef* > > myReverseConnectivity;
391 };
392 
393 
394 #endif
bool Is2D() const
std::list< std::list< TopoDS_Edge > > TListOfEdgesList
std::map< int, std::list< TElemDef *> > myReverseConnectivity
const std::list< int > & GetKeyPointIDs() const
bool setErrorCode(const ErrorCode theErrorCode)
std::map< int, std::list< TPoint *> > myShapeIDToPointsMap
const std::list< std::list< int > > & GetElementPointIDs(bool applied) const
std::vector< const SMDS_MeshElement * > myElements
TopTools_IndexedMapOfOrientedShape myShapeIDMap
std::map< int, const SMDS_MeshNode * > myXYZIdToNodeMap
std::list< TElemDef > myElemXYZIDs
std::vector< TPoint > myPoints
std::vector< const SMDS_MeshElement * > myPolyElems
std::set< const SMDS_MeshNode * > TNodeSet
std::list< int > TElemDef
std::list< TElemDef > myPolyElemXYZIDs
Base class for elements.
std::vector< const SMDS_MeshNode * > myOrderedNodes
#define SMESH_EXPORT
Definition: SMESH_SMESH.hxx:36
std::vector< gp_XYZ > myXYZ
bool IsLoaded() const
ErrorCode GetErrorCode() const
ErrorCode myErrorCode
bool myIsBoundaryPointsFound
std::list< int > myNbKeyPntInBoundary
TopoDS_Shape GetSubShape(const int i) const
TopoDS_Shape myShape
std::list< int > myKeyPointIDs
std::list< std::vector< int > > myPolyhedronQuantities
std::map< TNodeSet, std::list< std::list< int > > > myIdsOnBoundary
std::list< TElemDef > myElemPointIDs