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