1 // Copyright (C) 2007-2010 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_Shape.hxx>
39 #include <TopTools_IndexedMapOfOrientedShape.hxx>
44 class SMDS_MeshElement;
46 class SMDS_MeshVolume;
49 class SMESHDS_SubMesh;
56 // Class allowing meshing by mapping of pre-defined patterns: it generates
57 // a 2D mesh on a geometrical face or a 3D mesh inside a geometrical block
61 class SMESH_EXPORT SMESH_Pattern {
69 bool Load (const char* theFileContents);
70 // Load a pattern from <theFileContents>
72 bool Load (SMESH_Mesh* theMesh,
73 const TopoDS_Face& theFace,
74 bool theProject = false);
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
197 ErrorCode GetErrorCode() const { return myErrorCode; }
198 // return ErrorCode of the last operation
200 bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
201 // Return true if a pattern was successfully loaded
203 bool Is2D() const { return myIs2D; }
204 // Return true if the loaded pattern is a 2D one
206 bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
207 // Return nodes coordinates of the pattern
209 const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
210 // Return indices of key-points within the sequences returned by
211 // GetPoints() and GetMappedPoints()
213 const std::list< std::list< int > >& GetElementPointIDs (bool applied) const
214 { return myElemXYZIDs.empty() || !applied ? myElemPointIDs : myElemXYZIDs; }
215 // Return nodal connectivity of the elements of the pattern
217 void DumpPoints() const;
220 // -----------------------------
221 // Utilities for advanced usage
222 // -----------------------------
224 TopoDS_Shape GetSubShape( const int i ) const {
225 if ( i < 1 || i > myShapeIDMap.Extent() ) return TopoDS_Shape();
226 return myShapeIDMap( i );
228 // Return a shape from myShapeIDMap where shapes are indexed so that first go
229 // ordered vertices, then ordered edge, then faces and maybe a shell
235 gp_XYZ myInitXYZ; // loaded postion
237 double myInitU; // [0,1]
238 gp_Pnt myXYZ; // position to compute
243 friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
245 bool setErrorCode( const ErrorCode theErrorCode )
246 { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
247 // set ErrorCode and return true if it is Ok
249 bool setShapeToMesh(const TopoDS_Shape& theShape);
250 // Set a shape to be meshed. Return True if meshing is possible
252 std::list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
253 // Return list of points located on theShape.
254 // A list of edge-points include vertex-points (for 2D pattern only).
255 // A list of face-points doesnt include edge-points.
256 // A list of volume-points doesnt include face-points.
258 std::list< TPoint* > & getShapePoints(const int theShapeID);
259 // Return list of points located on the shape
261 bool findBoundaryPoints();
262 // If loaded from file, find points to map on edges and faces and
263 // compute their parameters
265 void arrangeBoundaries (std::list< std::list< TPoint* > >& boundaryPoints);
266 // if there are several wires, arrange boundaryPoints so that
267 // the outer wire goes first and fix inner wires orientation;
268 // update myKeyPointIDs to correspond to the order of key-points
269 // in boundaries; sort internal boundaries by the nb of key-points
271 void computeUVOnEdge( const TopoDS_Edge& theEdge, const std::list< TPoint* > & ePoints );
272 // compute coordinates of points on theEdge
274 bool compUVByIsoIntersection (const std::list< std::list< TPoint* > >& boundaryPoints,
275 const gp_XY& theInitUV,
277 bool & theIsDeformed);
278 // compute UV by intersection of iso-lines found by points on edges
280 bool compUVByElasticIsolines(const std::list< std::list< TPoint* > >& boundaryPoints,
281 const std::list< TPoint* >& pointsToCompute);
282 // compute UV as nodes of iso-poly-lines consisting of
283 // segments keeping relative size as in the pattern
285 double setFirstEdge (std::list< TopoDS_Edge > & theWire, int theFirstEdgeID);
286 // choose the best first edge of theWire; return the summary distance
287 // between point UV computed by isolines intersection and
288 // eventual UV got from edge p-curves
290 typedef std::list< std::list< TopoDS_Edge > > TListOfEdgesList;
292 bool sortSameSizeWires (TListOfEdgesList & theWireList,
293 const TListOfEdgesList::iterator& theFromWire,
294 const TListOfEdgesList::iterator& theToWire,
295 const int theFirstEdgeID,
296 std::list< std::list< TPoint* > >& theEdgesPointsList );
297 // sort wires in theWireList from theFromWire until theToWire,
298 // the wires are set in the order to correspond to the order
299 // of boundaries; after sorting, edges in the wires are put
300 // in a good order, point UVs on edges are computed and points
301 // are appended to theEdgesPointsList
303 typedef std::set<const SMDS_MeshNode*> TNodeSet;
305 void mergePoints (const bool uniteGroups);
306 // Merge XYZ on edges and/or faces.
308 void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
309 const bool toCreatePolygons,
310 const bool toCreatePolyedrs);
311 // prepare intermediate data to create Polygons and Polyhedrons
313 void createElements(SMESH_Mesh* theMesh,
314 const std::vector<const SMDS_MeshNode* >& theNodesVector,
315 const std::list< std::list< int > > & theElemNodeIDs,
316 const std::vector<const SMDS_MeshElement*>& theElements);
317 // add elements to the mesh
319 bool getFacesDefinition(const SMDS_MeshNode** theBndNodes,
320 const int theNbBndNodes,
321 const std::vector< const SMDS_MeshNode* >& theNodes,
322 std::list< int >& theFaceDefs,
323 std::vector<int>& theQuantity);
324 // fill faces definition for a volume face defined by theBndNodes
325 // return true if a face definition changes
328 bool isReversed(const SMDS_MeshNode* theFirstNode,
329 const std::list< int >& theIdsList) const;
330 // check xyz ids order in theIdsList taking into account
331 // theFirstNode on a link
333 void clearMesh(SMESH_Mesh* theMesh) const;
334 // clear mesh elements existing on myShape in theMesh
336 static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh* theMesh,
337 const TopoDS_Shape& theShape);
338 // return submesh containing elements bound to theShape in theMesh
343 typedef std::list< int > TElemDef; // element definition is its nodes ids
346 std::vector< TPoint > myPoints;
347 std::list< int > myKeyPointIDs;
348 std::list< TElemDef > myElemPointIDs;
350 ErrorCode myErrorCode;
352 bool myIsBoundaryPointsFound;
354 TopoDS_Shape myShape;
355 // all functions assure that shapes are indexed so that first go
356 // ordered vertices, then ordered edge, then faces and maybe a shell
357 TopTools_IndexedMapOfOrientedShape myShapeIDMap;
358 std::map< int, std::list< TPoint* > > myShapeIDToPointsMap;
361 // nb of key-points in each of pattern boundaries
362 std::list< int > myNbKeyPntInBoundary;
365 // to compute while applying to mesh elements, not to shapes
367 std::vector<gp_XYZ> myXYZ; // XYZ of nodes to create
368 std::list< TElemDef > myElemXYZIDs; // new elements definitions
369 std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
370 std::vector<const SMDS_MeshElement*> myElements; // refined elements
371 std::vector<const SMDS_MeshNode*> myOrderedNodes;
373 // elements to replace with polygon or polyhedron
374 std::vector<const SMDS_MeshElement*> myPolyElems;
375 // definitions of new poly elements
376 std::list< TElemDef > myPolyElemXYZIDs;
377 std::list< std::vector<int> > myPolyhedronQuantities;
379 // map a boundary to XYZs on it;
380 // a boundary (edge or face) is defined as a set of its nodes,
381 // XYZs on a boundary are indices of myXYZ s
382 std::map<TNodeSet,std::list<std::list<int> > > myIdsOnBoundary;
383 // map XYZ id to element it is in
384 std::map< int, std::list< TElemDef* > > myReverseConnectivity;