1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SMESH_Pattern.hxx
24 // Created : Mon Aug 2 10:30:00 2004
25 // Author : Edward AGAPOV (eap)
27 #ifndef SMESH_Pattern_HeaderFile
28 #define SMESH_Pattern_HeaderFile
30 #include "SMESH_SMESH.hxx"
38 #include <TopoDS_Vertex.hxx>
39 #include <TopTools_IndexedMapOfOrientedShape.hxx>
44 class SMDS_MeshElement;
46 class SMDS_MeshVolume;
49 class SMESHDS_SubMesh;
55 // Class allowing meshing by mapping of pre-defined patterns: it generates
56 // a 2D mesh on a geometrical face or a 3D mesh inside a geometrical block
60 class SMESH_EXPORT SMESH_Pattern {
68 bool Load (const char* theFileContents);
69 // Load a pattern from <theFileContents>
71 bool Load (SMESH_Mesh* theMesh,
72 const TopoDS_Face& theFace,
73 bool theProject = false,
74 TopoDS_Vertex the1stVertex=TopoDS_Vertex());
75 // Create a pattern from the mesh built on <theFace>.
76 // <theProject>==true makes override nodes positions
77 // on <theFace> computed by mesher
79 bool Load (SMESH_Mesh* theMesh,
80 const TopoDS_Shell& theBlock);
81 // Create a pattern from the mesh built on <theBlock>
83 bool Save (std::ostream& theFile);
84 // Save the loaded pattern into theFile
86 bool Apply (const TopoDS_Face& theFace,
87 const TopoDS_Vertex& theVertexOnKeyPoint1,
88 const bool theReverse);
89 // Compute nodes coordinates applying
90 // the loaded pattern to <theFace>. The first key-point
91 // will be mapped into <theVertexOnKeyPoint1>, which must
92 // be in the outer wire of theFace
94 bool Apply (const TopoDS_Shell& theBlock,
95 const TopoDS_Vertex& theVertex000,
96 const TopoDS_Vertex& theVertex001);
97 // Compute nodes coordinates applying
98 // the loaded pattern to <theBlock>. The (0,0,0) key-point
99 // will be mapped into <theVertex000>. The
100 // (0,0,1) key-point will be mapped into <theVertex001>.
102 bool Apply (const SMDS_MeshFace* theFace,
103 const int theNodeIndexOnKeyPoint1,
104 const bool theReverse);
105 // Compute nodes coordinates applying
106 // the loaded pattern to <theFace>. The first key-point
107 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
109 bool Apply (SMESH_Mesh* theMesh,
110 const SMDS_MeshFace* theFace,
111 const TopoDS_Shape& theSurface,
112 const int theNodeIndexOnKeyPoint1,
113 const bool theReverse);
114 // Compute nodes coordinates applying
115 // the loaded pattern to <theFace>. The first key-point
116 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
118 bool Apply (SMESH_Mesh* theMesh,
119 std::set<const SMDS_MeshFace*>& theFaces,
120 const int theNodeIndexOnKeyPoint1,
121 const bool theReverse);
122 // Compute nodes coordinates applying
123 // the loaded pattern to <theFaces>. The first key-point
124 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
126 bool Apply (const SMDS_MeshVolume* theVolume,
127 const int theNode000Index,
128 const int theNode001Index);
129 // Compute nodes coordinates applying
130 // the loaded pattern to <theVolume>. The (0,0,0) key-point
131 // will be mapped into <theNode000Index>-th node. The
132 // (0,0,1) key-point will be mapped into <theNode000Index>-th
135 bool Apply (std::set<const SMDS_MeshVolume*>& theVolumes,
136 const int theNode000Index,
137 const int theNode001Index);
138 // Compute nodes coordinates applying
139 // the loaded pattern to <theVolumes>. The (0,0,0) key-point
140 // will be mapped into <theNode000Index>-th node. The
141 // (0,0,1) key-point will be mapped into <theNode000Index>-th
144 bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
145 // Return nodes coordinates computed by Apply() method
147 bool MakeMesh(SMESH_Mesh* theMesh,
148 const bool toCreatePolygons = false,
149 const bool toCreatePolyedrs = false);
150 // Create nodes and elements in <theMesh> using nodes
151 // coordinates computed by either of Apply...() methods
160 ERR_READ_NB_POINTS, // couldn't read nb of points
161 ERR_READ_POINT_COORDS, // invalid nb of point coordinates
162 ERR_READ_TOO_FEW_POINTS, // too few points in a pattern
163 ERR_READ_3D_COORD, // coordinate of 3D point out of [0,1] range
164 ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
165 ERR_READ_BAD_INDEX, // invalid point index
166 ERR_READ_ELEM_POINTS, // invalid nb of points in element
167 ERR_READ_NO_ELEMS, // no elements in a pattern
168 ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
170 ERR_SAVE_NOT_LOADED, // pattern was not loaded
172 ERR_LOAD_EMPTY_SUBMESH, // no elements to load
174 ERR_LOADF_NARROW_FACE, // too narrow face
175 ERR_LOADF_CLOSED_FACE, // closed face
176 ERR_LOADF_CANT_PROJECT, // impossible to project nodes
178 ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
179 ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
181 ERR_APPL_NOT_COMPUTED, // mapping failed
182 ERR_APPL_NOT_LOADED, // pattern was not loaded
183 ERR_APPL_BAD_DIMENTION, // wrong shape dimention
184 ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
186 ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
187 ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
188 ERR_APPLF_INTERNAL_EEROR, // program error
190 ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
192 ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
194 ERR_MAKEM_NOT_COMPUTED, // mapping failed
196 ERR_UNEXPECTED // Unexpected of the pattern mapping alorithm
199 ErrorCode GetErrorCode() const { return myErrorCode; }
200 // return ErrorCode of the last operation
202 bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
203 // Return true if a pattern was successfully loaded
205 bool Is2D() const { return myIs2D; }
206 // Return true if the loaded pattern is a 2D one
208 bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
209 // Return nodes coordinates of the pattern
211 const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
212 // Return indices of key-points within the sequences returned by
213 // GetPoints() and GetMappedPoints()
215 const std::list< std::list< int > >& GetElementPointIDs (bool applied) const
216 { return myElemXYZIDs.empty() || !applied ? myElemPointIDs : myElemXYZIDs; }
217 // Return nodal connectivity of the elements of the pattern
219 void DumpPoints() const;
222 // -----------------------------
223 // Utilities for advanced usage
224 // -----------------------------
226 TopoDS_Shape GetSubShape( const int i ) const {
227 if ( i < 1 || i > myShapeIDMap.Extent() ) return TopoDS_Shape();
228 return myShapeIDMap( i );
230 // Return a shape from myShapeIDMap where shapes are indexed so that first go
231 // ordered vertices, then ordered edge, then faces and maybe a shell
237 gp_XYZ myInitXYZ; // loaded postion
239 double myInitU; // [0,1]
240 gp_Pnt myXYZ; // position to compute
245 friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
247 bool setErrorCode( const ErrorCode theErrorCode );
248 // set ErrorCode and return true if it is Ok
250 bool setShapeToMesh(const TopoDS_Shape& theShape);
251 // Set a shape to be meshed. Return True if meshing is possible
253 std::list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
254 // Return list of points located on theShape.
255 // A list of edge-points include vertex-points (for 2D pattern only).
256 // A list of face-points doesnt include edge-points.
257 // A list of volume-points doesnt include face-points.
259 std::list< TPoint* > & getShapePoints(const int theShapeID);
260 // Return list of points located on the shape
262 bool findBoundaryPoints();
263 // If loaded from file, find points to map on edges and faces and
264 // compute their parameters
266 void arrangeBoundaries (std::list< std::list< TPoint* > >& boundaryPoints);
267 // if there are several wires, arrange boundaryPoints so that
268 // the outer wire goes first and fix inner wires orientation;
269 // update myKeyPointIDs to correspond to the order of key-points
270 // in boundaries; sort internal boundaries by the nb of key-points
272 void computeUVOnEdge( const TopoDS_Edge& theEdge, const std::list< TPoint* > & ePoints );
273 // compute coordinates of points on theEdge
275 bool compUVByIsoIntersection (const std::list< std::list< TPoint* > >& boundaryPoints,
276 const gp_XY& theInitUV,
278 bool & theIsDeformed);
279 // compute UV by intersection of iso-lines found by points on edges
281 bool compUVByElasticIsolines(const std::list< std::list< TPoint* > >& boundaryPoints,
282 const std::list< TPoint* >& pointsToCompute);
283 // compute UV as nodes of iso-poly-lines consisting of
284 // segments keeping relative size as in the pattern
286 double setFirstEdge (std::list< TopoDS_Edge > & theWire, int theFirstEdgeID);
287 // choose the best first edge of theWire; return the summary distance
288 // between point UV computed by isolines intersection and
289 // eventual UV got from edge p-curves
291 typedef std::list< std::list< TopoDS_Edge > > TListOfEdgesList;
293 bool sortSameSizeWires (TListOfEdgesList & theWireList,
294 const TListOfEdgesList::iterator& theFromWire,
295 const TListOfEdgesList::iterator& theToWire,
296 const int theFirstEdgeID,
297 std::list< std::list< TPoint* > >& theEdgesPointsList );
298 // sort wires in theWireList from theFromWire until theToWire,
299 // the wires are set in the order to correspond to the order
300 // of boundaries; after sorting, edges in the wires are put
301 // in a good order, point UVs on edges are computed and points
302 // are appended to theEdgesPointsList
304 typedef std::set<const SMDS_MeshNode*> TNodeSet;
306 void mergePoints (const bool uniteGroups);
307 // Merge XYZ on edges and/or faces.
309 void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
310 const bool toCreatePolygons,
311 const bool toCreatePolyedrs);
312 // prepare intermediate data to create Polygons and Polyhedrons
314 void createElements(SMESH_Mesh* theMesh,
315 const std::vector<const SMDS_MeshNode* >& theNodesVector,
316 const std::list< std::list< int > > & theElemNodeIDs,
317 const std::vector<const SMDS_MeshElement*>& theElements);
318 // add elements to the mesh
320 bool getFacesDefinition(const SMDS_MeshNode** theBndNodes,
321 const int theNbBndNodes,
322 const std::vector< const SMDS_MeshNode* >& theNodes,
323 std::list< int >& theFaceDefs,
324 std::vector<int>& theQuantity);
325 // fill faces definition for a volume face defined by theBndNodes
326 // return true if a face definition changes
329 bool isReversed(const SMDS_MeshNode* theFirstNode,
330 const std::list< int >& theIdsList) const;
331 // check xyz ids order in theIdsList taking into account
332 // theFirstNode on a link
334 void clearMesh(SMESH_Mesh* theMesh) const;
335 // clear mesh elements existing on myShape in theMesh
337 static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh* theMesh,
338 const TopoDS_Shape& theShape);
339 // return submesh containing elements bound to theShape in theMesh
344 typedef std::list< int > TElemDef; // element definition is its nodes ids
347 std::vector< TPoint > myPoints;
348 std::list< int > myKeyPointIDs;
349 std::list< TElemDef > myElemPointIDs;
351 ErrorCode myErrorCode;
353 bool myIsBoundaryPointsFound;
355 TopoDS_Shape myShape;
356 // all functions assure that shapes are indexed so that first go
357 // ordered vertices, then ordered edge, then faces and maybe a shell
358 TopTools_IndexedMapOfOrientedShape myShapeIDMap;
359 std::map< int, std::list< TPoint* > > myShapeIDToPointsMap;
362 // nb of key-points in each of pattern boundaries
363 std::list< int > myNbKeyPntInBoundary;
366 // to compute while applying to mesh elements, not to shapes
368 std::vector<gp_XYZ> myXYZ; // XYZ of nodes to create
369 std::list< TElemDef > myElemXYZIDs; // new elements definitions
370 std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
371 std::vector<const SMDS_MeshElement*> myElements; // refined elements
372 std::vector<const SMDS_MeshNode*> myOrderedNodes;
374 // elements to replace with polygon or polyhedron
375 std::vector<const SMDS_MeshElement*> myPolyElems;
376 // definitions of new poly elements
377 std::list< TElemDef > myPolyElemXYZIDs;
378 std::list< std::vector<int> > myPolyhedronQuantities;
380 // map a boundary to XYZs on it;
381 // a boundary (edge or face) is defined as a set of its nodes,
382 // XYZs on a boundary are indices of myXYZ s
383 std::map<TNodeSet,std::list<std::list<int> > > myIdsOnBoundary;
384 // map XYZ id to element it is in
385 std::map< int, std::list< TElemDef* > > myReverseConnectivity;