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
33 #include <TopoDS_Shape.hxx>
34 #include <TopTools_IndexedMapOfOrientedShape.hxx>
39 class SMDS_MeshElement;
41 class SMDS_MeshVolume;
50 // Class allowing meshing by mapping of pre-defined patterns: it generates
51 // a 2D mesh on a geometrical face or a 3D mesh inside a geometrical block
63 bool Load (const char* theFileContents);
64 // Load a pattern from <theFileContents>
66 bool Load (SMESH_Mesh* theMesh,
67 const TopoDS_Face& theFace,
69 // Create a pattern from the mesh built on <theFace>.
70 // <theProject>==true makes override nodes positions
71 // on <theFace> computed by mesher
73 bool Load (SMESH_Mesh* theMesh,
74 const TopoDS_Shell& theBlock);
75 // Create a pattern from the mesh built on <theBlock>
77 bool Save (std::ostream& theFile);
78 // Save the loaded pattern into theFile
80 bool Apply (const TopoDS_Face& theFace,
81 const TopoDS_Vertex& theVertexOnKeyPoint1,
82 const bool theReverse);
83 // Compute nodes coordinates applying
84 // the loaded pattern to <theFace>. The first key-point
85 // will be mapped into <theVertexOnKeyPoint1>, which must
86 // be in the outer wire of theFace
88 bool Apply (const TopoDS_Shell& theBlock,
89 const TopoDS_Vertex& theVertex000,
90 const TopoDS_Vertex& theVertex001);
91 // Compute nodes coordinates applying
92 // the loaded pattern to <theBlock>. The (0,0,0) key-point
93 // will be mapped into <theVertex000>. The
94 // (0,0,1) key-point will be mapped into <theVertex001>.
96 bool Apply (const SMDS_MeshFace* theFace,
97 const int theNodeIndexOnKeyPoint1,
98 const bool theReverse);
99 // Compute nodes coordinates applying
100 // the loaded pattern to <theFace>. The first key-point
101 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
103 bool Apply (std::set<const SMDS_MeshFace*>& theFaces,
104 const int theNodeIndexOnKeyPoint1,
105 const bool theReverse);
106 // Compute nodes coordinates applying
107 // the loaded pattern to <theFaces>. The first key-point
108 // will be mapped into <theNodeIndexOnKeyPoint1>-th node
110 bool Apply (const SMDS_MeshVolume* theVolume,
111 const int theNode000Index,
112 const int theNode001Index);
113 // Compute nodes coordinates applying
114 // the loaded pattern to <theVolume>. The (0,0,0) key-point
115 // will be mapped into <theNode000Index>-th node. The
116 // (0,0,1) key-point will be mapped into <theNode000Index>-th
119 bool Apply (std::set<const SMDS_MeshVolume*>& theVolumes,
120 const int theNode000Index,
121 const int theNode001Index);
122 // Compute nodes coordinates applying
123 // the loaded pattern to <theVolumes>. The (0,0,0) key-point
124 // will be mapped into <theNode000Index>-th node. The
125 // (0,0,1) key-point will be mapped into <theNode000Index>-th
128 bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
129 // Return nodes coordinates computed by Apply() method
131 bool MakeMesh(SMESH_Mesh* theMesh,
132 const bool toCreatePolygons = false,
133 const bool toCreatePolyedrs = false);
134 // Create nodes and elements in <theMesh> using nodes
135 // coordinates computed by either of Apply...() methods
143 ERR_READ_NB_POINTS, // couldn't read nb of points
144 ERR_READ_POINT_COORDS, // invalid nb of point coordinates
145 ERR_READ_TOO_FEW_POINTS, // too few points in a pattern
146 ERR_READ_3D_COORD, // coordinate of 3D point out of [0,1] range
147 ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
148 ERR_READ_BAD_INDEX, // invalid point index
149 ERR_READ_ELEM_POINTS, // invalid nb of points in element
150 ERR_READ_NO_ELEMS, // no elements in a pattern
151 ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
153 ERR_SAVE_NOT_LOADED, // pattern was not loaded
155 ERR_LOAD_EMPTY_SUBMESH, // no elements to load
157 ERR_LOADF_NARROW_FACE, // too narrow face
158 ERR_LOADF_CLOSED_FACE, // closed face
160 ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
161 ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
163 ERR_APPL_NOT_COMPUTED, // mapping failed
164 ERR_APPL_NOT_LOADED, // pattern was not loaded
165 ERR_APPL_BAD_DIMENTION, // wrong shape dimention
166 ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
168 ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
169 ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
170 ERR_APPLF_INTERNAL_EEROR, // program error
172 ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
174 ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
176 ERR_MAKEM_NOT_COMPUTED // mapping failed
179 ErrorCode GetErrorCode() const { return myErrorCode; }
180 // return ErrorCode of the last operation
182 bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
183 // Return true if a pattern was successfully loaded
185 bool Is2D() const { return myIs2D; }
186 // Return true if the loaded pattern is a 2D one
188 bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
189 // Return nodes coordinates of the pattern
191 const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
192 // Return indices of key-points within the sequences returned by
193 // GetPoints() and GetMappedPoints()
195 const std::list< std::list< int > >& GetElementPointIDs (bool applied) const
196 { return myElemXYZIDs.empty() || !applied ? myElemPointIDs : myElemXYZIDs; }
197 // Return nodal connectivity of the elements of the pattern
199 void DumpPoints() const;
207 gp_XYZ myInitXYZ; // loaded postion
209 double myInitU; // [0,1]
210 gp_Pnt myXYZ; // position to compute
215 friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
217 bool setErrorCode( const ErrorCode theErrorCode )
218 { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
219 // set ErrorCode and return true if it is Ok
221 bool setShapeToMesh(const TopoDS_Shape& theShape);
222 // Set a shape to be meshed. Return True if meshing is possible
224 list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
225 // Return list of points located on theShape.
226 // A list of edge-points include vertex-points (for 2D pattern only).
227 // A list of face-points doesnt include edge-points.
228 // A list of volume-points doesnt include face-points.
230 list< TPoint* > & getShapePoints(const int theShapeID);
231 // Return list of points located on the shape
233 bool findBoundaryPoints();
234 // If loaded from file, find points to map on edges and faces and
235 // compute their parameters
237 void arrangeBoundaries (list< list< TPoint* > >& boundaryPoints);
238 // if there are several wires, arrange boundaryPoints so that
239 // the outer wire goes first and fix inner wires orientation;
240 // update myKeyPointIDs to correspond to the order of key-points
241 // in boundaries; sort internal boundaries by the nb of key-points
243 void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
244 // compute coordinates of points on theEdge
246 bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
247 const gp_XY& theInitUV,
249 bool & theIsDeformed);
250 // compute UV by intersection of iso-lines found by points on edges
252 bool compUVByElasticIsolines(const list< list< TPoint* > >& boundaryPoints,
253 const list< TPoint* >& pointsToCompute);
254 // compute UV as nodes of iso-poly-lines consisting of
255 // segments keeping relative size as in the pattern
257 double setFirstEdge (list< TopoDS_Edge > & theWire, int theFirstEdgeID);
258 // choose the best first edge of theWire; return the summary distance
259 // between point UV computed by isolines intersection and
260 // eventual UV got from edge p-curves
262 typedef list< list< TopoDS_Edge > > TListOfEdgesList;
264 bool sortSameSizeWires (TListOfEdgesList & theWireList,
265 const TListOfEdgesList::iterator& theFromWire,
266 const TListOfEdgesList::iterator& theToWire,
267 const int theFirstEdgeID,
268 list< list< TPoint* > >& theEdgesPointsList );
269 // sort wires in theWireList from theFromWire until theToWire,
270 // the wires are set in the order to correspond to the order
271 // of boundaries; after sorting, edges in the wires are put
272 // in a good order, point UVs on edges are computed and points
273 // are appended to theEdgesPointsList
275 typedef std::set<const SMDS_MeshNode*> TNodeSet;
277 void mergePoints (const bool uniteGroups);
278 // Merge XYZ on edges and/or faces.
280 void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
281 const bool toCreatePolygons,
282 const bool toCreatePolyedrs);
283 // prepare intermediate data to create Polygons and Polyhedrons
285 void createElements(SMESH_Mesh* theMesh,
286 const std::vector<const SMDS_MeshNode* >& theNodesVector,
287 const std::list< std::list< int > > & theElemNodeIDs,
288 const std::vector<const SMDS_MeshElement*>& theElements);
289 // add elements to the mesh
291 bool getFacesDefinition(const SMDS_MeshNode** theBndNodes,
292 const int theNbBndNodes,
293 const std::vector< const SMDS_MeshNode* >& theNodes,
294 std::list< int >& theFaceDefs,
295 std::vector<int>& theQuantity);
296 // fill faces definition for a volume face defined by theBndNodes
297 // return true if a face definition changes
300 bool isReversed(const SMDS_MeshNode* theFirstNode,
301 const std::list< int >& theIdsList) const;
302 // check xyz ids order in theIdsList taking into account
303 // theFirstNode on a link
308 typedef std::list< int > TElemDef; // element definition is its nodes ids
311 std::vector< TPoint > myPoints;
312 std::list< int > myKeyPointIDs;
313 std::list< TElemDef > myElemPointIDs;
315 ErrorCode myErrorCode;
317 bool myIsBoundaryPointsFound;
319 TopoDS_Shape myShape;
320 // all functions assure that shapes are indexed so that first go
321 // ordered vertices, then ordered edge, then faces and maybe a shell
322 TopTools_IndexedMapOfOrientedShape myShapeIDMap;
323 std::map< int, list< TPoint* > > myShapeIDToPointsMap;
326 // nb of key-points in each of pattern boundaries
327 std::list< int > myNbKeyPntInBoundary;
330 // to compute while applying to mesh elements, not to shapes
332 std::vector<gp_XYZ> myXYZ; // XYZ of nodes to create
333 std::list< TElemDef > myElemXYZIDs; // new elements definitions
334 std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
335 std::vector<const SMDS_MeshElement*> myElements; // refined elements
336 std::vector<const SMDS_MeshNode*> myOrderedNodes;
338 // elements to replace with polygon or polyhedron
339 std::vector<const SMDS_MeshElement*> myPolyElems;
340 // definitions of new poly elements
341 std::list< TElemDef > myPolyElemXYZIDs;
342 std::list< std::vector<int> > myPolyhedronQuantities;
344 // map a boundary to XYZs on it;
345 // a boundary (edge or face) is defined as a set of its nodes,
346 // XYZs on a boundary are indices of myXYZ s
347 std::map<TNodeSet,std::list<std::list<int> > > myIdsOnBoundary;
348 // map XYZ id to element it is in
349 std::map< int, std::list< TElemDef* > > myReverseConnectivity;