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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
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,
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 (std::set<const SMDS_MeshFace*>& theFaces,
107 const int theNodeIndexOnKeyPoint1,
108 const bool theReverse);
109 // Compute nodes coordinates applying
110 // the loaded pattern to <theFaces>. The first key-point
111 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
113 bool Apply (const SMDS_MeshVolume* theVolume,
114 const int theNode000Index,
115 const int theNode001Index);
116 // Compute nodes coordinates applying
117 // the loaded pattern to <theVolume>. The (0,0,0) key-point
118 // will be mapped into <theNode000Index>-th node. The
119 // (0,0,1) key-point will be mapped into <theNode000Index>-th
122 bool Apply (std::set<const SMDS_MeshVolume*>& theVolumes,
123 const int theNode000Index,
124 const int theNode001Index);
125 // Compute nodes coordinates applying
126 // the loaded pattern to <theVolumes>. The (0,0,0) key-point
127 // will be mapped into <theNode000Index>-th node. The
128 // (0,0,1) key-point will be mapped into <theNode000Index>-th
131 bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
132 // Return nodes coordinates computed by Apply() method
134 bool MakeMesh(SMESH_Mesh* theMesh,
135 const bool toCreatePolygons = false,
136 const bool toCreatePolyedrs = false);
137 // Create nodes and elements in <theMesh> using nodes
138 // coordinates computed by either of Apply...() methods
146 ERR_READ_NB_POINTS, // couldn't read nb of points
147 ERR_READ_POINT_COORDS, // invalid nb of point coordinates
148 ERR_READ_TOO_FEW_POINTS, // too few points in a pattern
149 ERR_READ_3D_COORD, // coordinate of 3D point out of [0,1] range
150 ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
151 ERR_READ_BAD_INDEX, // invalid point index
152 ERR_READ_ELEM_POINTS, // invalid nb of points in element
153 ERR_READ_NO_ELEMS, // no elements in a pattern
154 ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
156 ERR_SAVE_NOT_LOADED, // pattern was not loaded
158 ERR_LOAD_EMPTY_SUBMESH, // no elements to load
160 ERR_LOADF_NARROW_FACE, // too narrow face
161 ERR_LOADF_CLOSED_FACE, // closed face
163 ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
164 ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
166 ERR_APPL_NOT_COMPUTED, // mapping failed
167 ERR_APPL_NOT_LOADED, // pattern was not loaded
168 ERR_APPL_BAD_DIMENTION, // wrong shape dimention
169 ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
171 ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
172 ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
173 ERR_APPLF_INTERNAL_EEROR, // program error
175 ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
177 ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
179 ERR_MAKEM_NOT_COMPUTED // mapping failed
182 ErrorCode GetErrorCode() const { return myErrorCode; }
183 // return ErrorCode of the last operation
185 bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
186 // Return true if a pattern was successfully loaded
188 bool Is2D() const { return myIs2D; }
189 // Return true if the loaded pattern is a 2D one
191 bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
192 // Return nodes coordinates of the pattern
194 const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
195 // Return indices of key-points within the sequences returned by
196 // GetPoints() and GetMappedPoints()
198 const std::list< std::list< int > >& GetElementPointIDs (bool applied) const
199 { return myElemXYZIDs.empty() || !applied ? myElemPointIDs : myElemXYZIDs; }
200 // Return nodal connectivity of the elements of the pattern
202 void DumpPoints() const;
210 gp_XYZ myInitXYZ; // loaded postion
212 double myInitU; // [0,1]
213 gp_Pnt myXYZ; // position to compute
218 friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
220 bool setErrorCode( const ErrorCode theErrorCode )
221 { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
222 // set ErrorCode and return true if it is Ok
224 bool setShapeToMesh(const TopoDS_Shape& theShape);
225 // Set a shape to be meshed. Return True if meshing is possible
227 list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
228 // Return list of points located on theShape.
229 // A list of edge-points include vertex-points (for 2D pattern only).
230 // A list of face-points doesnt include edge-points.
231 // A list of volume-points doesnt include face-points.
233 list< TPoint* > & getShapePoints(const int theShapeID);
234 // Return list of points located on the shape
236 bool findBoundaryPoints();
237 // If loaded from file, find points to map on edges and faces and
238 // compute their parameters
240 void arrangeBoundaries (list< list< TPoint* > >& boundaryPoints);
241 // if there are several wires, arrange boundaryPoints so that
242 // the outer wire goes first and fix inner wires orientation;
243 // update myKeyPointIDs to correspond to the order of key-points
244 // in boundaries; sort internal boundaries by the nb of key-points
246 void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
247 // compute coordinates of points on theEdge
249 bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
250 const gp_XY& theInitUV,
252 bool & theIsDeformed);
253 // compute UV by intersection of iso-lines found by points on edges
255 bool compUVByElasticIsolines(const list< list< TPoint* > >& boundaryPoints,
256 const list< TPoint* >& pointsToCompute);
257 // compute UV as nodes of iso-poly-lines consisting of
258 // segments keeping relative size as in the pattern
260 double setFirstEdge (list< TopoDS_Edge > & theWire, int theFirstEdgeID);
261 // choose the best first edge of theWire; return the summary distance
262 // between point UV computed by isolines intersection and
263 // eventual UV got from edge p-curves
265 typedef list< list< TopoDS_Edge > > TListOfEdgesList;
267 bool sortSameSizeWires (TListOfEdgesList & theWireList,
268 const TListOfEdgesList::iterator& theFromWire,
269 const TListOfEdgesList::iterator& theToWire,
270 const int theFirstEdgeID,
271 list< list< TPoint* > >& theEdgesPointsList );
272 // sort wires in theWireList from theFromWire until theToWire,
273 // the wires are set in the order to correspond to the order
274 // of boundaries; after sorting, edges in the wires are put
275 // in a good order, point UVs on edges are computed and points
276 // are appended to theEdgesPointsList
278 typedef std::set<const SMDS_MeshNode*> TNodeSet;
280 void mergePoints (const bool uniteGroups);
281 // Merge XYZ on edges and/or faces.
283 void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
284 const bool toCreatePolygons,
285 const bool toCreatePolyedrs);
286 // prepare intermediate data to create Polygons and Polyhedrons
288 void createElements(SMESH_Mesh* theMesh,
289 const std::vector<const SMDS_MeshNode* >& theNodesVector,
290 const std::list< std::list< int > > & theElemNodeIDs,
291 const std::vector<const SMDS_MeshElement*>& theElements);
292 // add elements to the mesh
294 bool getFacesDefinition(const SMDS_MeshNode** theBndNodes,
295 const int theNbBndNodes,
296 const std::vector< const SMDS_MeshNode* >& theNodes,
297 std::list< int >& theFaceDefs,
298 std::vector<int>& theQuantity);
299 // fill faces definition for a volume face defined by theBndNodes
300 // return true if a face definition changes
303 bool isReversed(const SMDS_MeshNode* theFirstNode,
304 const std::list< int >& theIdsList) const;
305 // check xyz ids order in theIdsList taking into account
306 // theFirstNode on a link
308 void clearMesh(SMESH_Mesh* theMesh) const;
309 // clear mesh elements existing on myShape in theMesh
311 static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh* theMesh,
312 const TopoDS_Shape& theShape);
313 // return submesh containing elements bound to theShape in theMesh
318 typedef std::list< int > TElemDef; // element definition is its nodes ids
321 std::vector< TPoint > myPoints;
322 std::list< int > myKeyPointIDs;
323 std::list< TElemDef > myElemPointIDs;
325 ErrorCode myErrorCode;
327 bool myIsBoundaryPointsFound;
329 TopoDS_Shape myShape;
330 // all functions assure that shapes are indexed so that first go
331 // ordered vertices, then ordered edge, then faces and maybe a shell
332 TopTools_IndexedMapOfOrientedShape myShapeIDMap;
333 std::map< int, list< TPoint* > > myShapeIDToPointsMap;
336 // nb of key-points in each of pattern boundaries
337 std::list< int > myNbKeyPntInBoundary;
340 // to compute while applying to mesh elements, not to shapes
342 std::vector<gp_XYZ> myXYZ; // XYZ of nodes to create
343 std::list< TElemDef > myElemXYZIDs; // new elements definitions
344 std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
345 std::vector<const SMDS_MeshElement*> myElements; // refined elements
346 std::vector<const SMDS_MeshNode*> myOrderedNodes;
348 // elements to replace with polygon or polyhedron
349 std::vector<const SMDS_MeshElement*> myPolyElems;
350 // definitions of new poly elements
351 std::list< TElemDef > myPolyElemXYZIDs;
352 std::list< std::vector<int> > myPolyhedronQuantities;
354 // map a boundary to XYZs on it;
355 // a boundary (edge or face) is defined as a set of its nodes,
356 // XYZs on a boundary are indices of myXYZ s
357 std::map<TNodeSet,std::list<std::list<int> > > myIdsOnBoundary;
358 // map XYZ id to element it is in
359 std::map< int, std::list< TElemDef* > > myReverseConnectivity;