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
33 #include <TopoDS_Shape.hxx>
34 #include <TopTools_IndexedMapOfOrientedShape.hxx>
39 class SMDS_MeshElement;
41 class SMDS_MeshVolume;
44 class SMESHDS_SubMesh;
51 // Class allowing meshing by mapping of pre-defined patterns: it generates
52 // a 2D mesh on a geometrical face or a 3D mesh inside a geometrical block
64 bool Load (const char* theFileContents);
65 // Load a pattern from <theFileContents>
67 bool Load (SMESH_Mesh* theMesh,
68 const TopoDS_Face& theFace,
69 bool theProject = false);
70 // Create a pattern from the mesh built on <theFace>.
71 // <theProject>==true makes override nodes positions
72 // on <theFace> computed by mesher
74 bool Load (SMESH_Mesh* theMesh,
75 const TopoDS_Shell& theBlock);
76 // Create a pattern from the mesh built on <theBlock>
78 bool Save (std::ostream& theFile);
79 // Save the loaded pattern into theFile
81 bool Apply (const TopoDS_Face& theFace,
82 const TopoDS_Vertex& theVertexOnKeyPoint1,
83 const bool theReverse);
84 // Compute nodes coordinates applying
85 // the loaded pattern to <theFace>. The first key-point
86 // will be mapped into <theVertexOnKeyPoint1>, which must
87 // be in the outer wire of theFace
89 bool Apply (const TopoDS_Shell& theBlock,
90 const TopoDS_Vertex& theVertex000,
91 const TopoDS_Vertex& theVertex001);
92 // Compute nodes coordinates applying
93 // the loaded pattern to <theBlock>. The (0,0,0) key-point
94 // will be mapped into <theVertex000>. The
95 // (0,0,1) key-point will be mapped into <theVertex001>.
97 bool Apply (const SMDS_MeshFace* theFace,
98 const int theNodeIndexOnKeyPoint1,
99 const bool theReverse);
100 // Compute nodes coordinates applying
101 // the loaded pattern to <theFace>. The first key-point
102 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
104 bool Apply (std::set<const SMDS_MeshFace*>& theFaces,
105 const int theNodeIndexOnKeyPoint1,
106 const bool theReverse);
107 // Compute nodes coordinates applying
108 // the loaded pattern to <theFaces>. The first key-point
109 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
111 bool Apply (const SMDS_MeshVolume* theVolume,
112 const int theNode000Index,
113 const int theNode001Index);
114 // Compute nodes coordinates applying
115 // the loaded pattern to <theVolume>. The (0,0,0) key-point
116 // will be mapped into <theNode000Index>-th node. The
117 // (0,0,1) key-point will be mapped into <theNode000Index>-th
120 bool Apply (std::set<const SMDS_MeshVolume*>& theVolumes,
121 const int theNode000Index,
122 const int theNode001Index);
123 // Compute nodes coordinates applying
124 // the loaded pattern to <theVolumes>. The (0,0,0) key-point
125 // will be mapped into <theNode000Index>-th node. The
126 // (0,0,1) key-point will be mapped into <theNode000Index>-th
129 bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
130 // Return nodes coordinates computed by Apply() method
132 bool MakeMesh(SMESH_Mesh* theMesh,
133 const bool toCreatePolygons = false,
134 const bool toCreatePolyedrs = false);
135 // Create nodes and elements in <theMesh> using nodes
136 // coordinates computed by either of Apply...() methods
145 ERR_READ_NB_POINTS, // couldn't read nb of points
146 ERR_READ_POINT_COORDS, // invalid nb of point coordinates
147 ERR_READ_TOO_FEW_POINTS, // too few points in a pattern
148 ERR_READ_3D_COORD, // coordinate of 3D point out of [0,1] range
149 ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
150 ERR_READ_BAD_INDEX, // invalid point index
151 ERR_READ_ELEM_POINTS, // invalid nb of points in element
152 ERR_READ_NO_ELEMS, // no elements in a pattern
153 ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
155 ERR_SAVE_NOT_LOADED, // pattern was not loaded
157 ERR_LOAD_EMPTY_SUBMESH, // no elements to load
159 ERR_LOADF_NARROW_FACE, // too narrow face
160 ERR_LOADF_CLOSED_FACE, // closed face
161 ERR_LOADF_CANT_PROJECT, // impossible to project nodes
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;
205 // -----------------------------
206 // Utilities for advanced usage
207 // -----------------------------
209 TopoDS_Shape GetSubShape( const int i ) const {
210 if ( i < 1 || i > myShapeIDMap.Extent() ) return TopoDS_Shape();
211 return myShapeIDMap( i );
213 // Return a shape from myShapeIDMap where shapes are indexed so that first go
214 // ordered vertices, then ordered edge, then faces and maybe a shell
220 gp_XYZ myInitXYZ; // loaded postion
222 double myInitU; // [0,1]
223 gp_Pnt myXYZ; // position to compute
228 friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
230 bool setErrorCode( const ErrorCode theErrorCode )
231 { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
232 // set ErrorCode and return true if it is Ok
234 bool setShapeToMesh(const TopoDS_Shape& theShape);
235 // Set a shape to be meshed. Return True if meshing is possible
237 list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
238 // Return list of points located on theShape.
239 // A list of edge-points include vertex-points (for 2D pattern only).
240 // A list of face-points doesnt include edge-points.
241 // A list of volume-points doesnt include face-points.
243 list< TPoint* > & getShapePoints(const int theShapeID);
244 // Return list of points located on the shape
246 bool findBoundaryPoints();
247 // If loaded from file, find points to map on edges and faces and
248 // compute their parameters
250 void arrangeBoundaries (list< list< TPoint* > >& boundaryPoints);
251 // if there are several wires, arrange boundaryPoints so that
252 // the outer wire goes first and fix inner wires orientation;
253 // update myKeyPointIDs to correspond to the order of key-points
254 // in boundaries; sort internal boundaries by the nb of key-points
256 void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
257 // compute coordinates of points on theEdge
259 bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
260 const gp_XY& theInitUV,
262 bool & theIsDeformed);
263 // compute UV by intersection of iso-lines found by points on edges
265 bool compUVByElasticIsolines(const list< list< TPoint* > >& boundaryPoints,
266 const list< TPoint* >& pointsToCompute);
267 // compute UV as nodes of iso-poly-lines consisting of
268 // segments keeping relative size as in the pattern
270 double setFirstEdge (list< TopoDS_Edge > & theWire, int theFirstEdgeID);
271 // choose the best first edge of theWire; return the summary distance
272 // between point UV computed by isolines intersection and
273 // eventual UV got from edge p-curves
275 typedef list< list< TopoDS_Edge > > TListOfEdgesList;
277 bool sortSameSizeWires (TListOfEdgesList & theWireList,
278 const TListOfEdgesList::iterator& theFromWire,
279 const TListOfEdgesList::iterator& theToWire,
280 const int theFirstEdgeID,
281 list< list< TPoint* > >& theEdgesPointsList );
282 // sort wires in theWireList from theFromWire until theToWire,
283 // the wires are set in the order to correspond to the order
284 // of boundaries; after sorting, edges in the wires are put
285 // in a good order, point UVs on edges are computed and points
286 // are appended to theEdgesPointsList
288 typedef std::set<const SMDS_MeshNode*> TNodeSet;
290 void mergePoints (const bool uniteGroups);
291 // Merge XYZ on edges and/or faces.
293 void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
294 const bool toCreatePolygons,
295 const bool toCreatePolyedrs);
296 // prepare intermediate data to create Polygons and Polyhedrons
298 void createElements(SMESH_Mesh* theMesh,
299 const std::vector<const SMDS_MeshNode* >& theNodesVector,
300 const std::list< std::list< int > > & theElemNodeIDs,
301 const std::vector<const SMDS_MeshElement*>& theElements);
302 // add elements to the mesh
304 bool getFacesDefinition(const SMDS_MeshNode** theBndNodes,
305 const int theNbBndNodes,
306 const std::vector< const SMDS_MeshNode* >& theNodes,
307 std::list< int >& theFaceDefs,
308 std::vector<int>& theQuantity);
309 // fill faces definition for a volume face defined by theBndNodes
310 // return true if a face definition changes
313 bool isReversed(const SMDS_MeshNode* theFirstNode,
314 const std::list< int >& theIdsList) const;
315 // check xyz ids order in theIdsList taking into account
316 // theFirstNode on a link
318 void clearMesh(SMESH_Mesh* theMesh) const;
319 // clear mesh elements existing on myShape in theMesh
321 static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh* theMesh,
322 const TopoDS_Shape& theShape);
323 // return submesh containing elements bound to theShape in theMesh
328 typedef std::list< int > TElemDef; // element definition is its nodes ids
331 std::vector< TPoint > myPoints;
332 std::list< int > myKeyPointIDs;
333 std::list< TElemDef > myElemPointIDs;
335 ErrorCode myErrorCode;
337 bool myIsBoundaryPointsFound;
339 TopoDS_Shape myShape;
340 // all functions assure that shapes are indexed so that first go
341 // ordered vertices, then ordered edge, then faces and maybe a shell
342 TopTools_IndexedMapOfOrientedShape myShapeIDMap;
343 std::map< int, list< TPoint* > > myShapeIDToPointsMap;
346 // nb of key-points in each of pattern boundaries
347 std::list< int > myNbKeyPntInBoundary;
350 // to compute while applying to mesh elements, not to shapes
352 std::vector<gp_XYZ> myXYZ; // XYZ of nodes to create
353 std::list< TElemDef > myElemXYZIDs; // new elements definitions
354 std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
355 std::vector<const SMDS_MeshElement*> myElements; // refined elements
356 std::vector<const SMDS_MeshNode*> myOrderedNodes;
358 // elements to replace with polygon or polyhedron
359 std::vector<const SMDS_MeshElement*> myPolyElems;
360 // definitions of new poly elements
361 std::list< TElemDef > myPolyElemXYZIDs;
362 std::list< std::vector<int> > myPolyhedronQuantities;
364 // map a boundary to XYZs on it;
365 // a boundary (edge or face) is defined as a set of its nodes,
366 // XYZs on a boundary are indices of myXYZ s
367 std::map<TNodeSet,std::list<std::list<int> > > myIdsOnBoundary;
368 // map XYZ id to element it is in
369 std::map< int, std::list< TElemDef* > > myReverseConnectivity;