Salome HOME
a2ee3050da6f78b98419795292def1603b87f96f
[modules/smesh.git] / src / SMESH / SMESH_Pattern.hxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
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. 
8 // 
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. 
13 // 
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 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 // File      : SMESH_Pattern.hxx
21 // Created   : Mon Aug  2 10:30:00 2004
22 // Author    : Edward AGAPOV (eap)
23
24 #ifndef SMESH_Pattern_HeaderFile
25 #define SMESH_Pattern_HeaderFile
26
27 #include <vector>
28 #include <list>
29 #include <map>
30 #include <set>
31 #include <iostream>
32
33 #include <TopoDS_Shape.hxx>
34 #include <TopTools_IndexedMapOfOrientedShape.hxx>
35 #include <gp_XYZ.hxx>
36 #include <gp_XY.hxx>
37 #include <gp_Pnt.hxx>
38
39 class SMDS_MeshElement;
40 class SMDS_MeshFace;
41 class SMDS_MeshVolume;
42 class SMDS_MeshNode;
43 class SMESH_Mesh;
44 class SMESHDS_SubMesh;
45 class TopoDS_Shell;
46 class TopoDS_Vertex;
47 class TopoDS_Face;
48 class TopoDS_Edge;
49
50 //
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
53 // of 6 faces.
54 //
55
56 class SMESH_Pattern {
57  public:
58   
59   SMESH_Pattern ();
60
61   void Clear();
62   // clear fields
63
64   bool Load (const char* theFileContents);
65   // Load a pattern from <theFileContents>
66
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
73
74   bool Load (SMESH_Mesh*         theMesh,
75              const TopoDS_Shell& theBlock);
76   // Create a pattern from the mesh built on <theBlock>
77
78   bool Save (std::ostream& theFile);
79   // Save the loaded pattern into theFile
80
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
88
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>.
96
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
103
104   bool Apply (SMESH_Mesh*          theMesh,
105               const SMDS_MeshFace* theFace,
106               const TopoDS_Shape&  theSurface,
107               const int            theNodeIndexOnKeyPoint1,
108               const bool           theReverse);
109   // Compute nodes coordinates applying
110   // the loaded pattern to <theFace>. The first key-point
111   // will be mapped into <theNodeIndexOnKeyPoint1>-th node
112
113   bool Apply (SMESH_Mesh*                     theMesh,
114               std::set<const SMDS_MeshFace*>& theFaces,
115               const int                       theNodeIndexOnKeyPoint1,
116               const bool                      theReverse);
117   // Compute nodes coordinates applying
118   // the loaded pattern to <theFaces>. The first key-point
119   // will be mapped into <theNodeIndexOnKeyPoint1>-th node
120
121   bool Apply (const SMDS_MeshVolume* theVolume,
122               const int              theNode000Index,
123               const int              theNode001Index);
124   // Compute nodes coordinates applying
125   // the loaded pattern to <theVolume>. The (0,0,0) key-point
126   // will be mapped into <theNode000Index>-th node. The
127   // (0,0,1) key-point will be mapped into <theNode000Index>-th
128   // node.
129
130   bool Apply (std::set<const SMDS_MeshVolume*>& theVolumes,
131               const int                         theNode000Index,
132               const int                         theNode001Index);
133   // Compute nodes coordinates applying
134   // the loaded pattern to <theVolumes>. The (0,0,0) key-point
135   // will be mapped into <theNode000Index>-th node. The
136   // (0,0,1) key-point will be mapped into <theNode000Index>-th
137   // node.
138
139   bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
140   // Return nodes coordinates computed by Apply() method
141
142   bool MakeMesh(SMESH_Mesh* theMesh,
143                 const bool toCreatePolygons = false,
144                 const bool toCreatePolyedrs = false);
145   // Create nodes and elements in <theMesh> using nodes
146   // coordinates computed by either of Apply...() methods
147
148   // ----------
149   // Inquiries
150   // ----------
151
152   enum ErrorCode {
153     ERR_OK,
154     // Load(file)
155     ERR_READ_NB_POINTS, // couldn't read nb of points
156     ERR_READ_POINT_COORDS, // invalid nb of point coordinates
157     ERR_READ_TOO_FEW_POINTS,  // too few points in a pattern
158     ERR_READ_3D_COORD,  // coordinate of 3D point out of [0,1] range
159     ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
160     ERR_READ_BAD_INDEX, // invalid point index
161     ERR_READ_ELEM_POINTS, // invalid nb of points in element
162     ERR_READ_NO_ELEMS, // no elements in a pattern
163     ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
164     // Save(file)
165     ERR_SAVE_NOT_LOADED, // pattern was not loaded
166     // Load(shape)
167     ERR_LOAD_EMPTY_SUBMESH, // no elements to load
168     // Load(face)
169     ERR_LOADF_NARROW_FACE, // too narrow face
170     ERR_LOADF_CLOSED_FACE, // closed face
171     ERR_LOADF_CANT_PROJECT, // impossible to project nodes
172     // Load(volume)
173     ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
174     ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
175     // Apply(shape)
176     ERR_APPL_NOT_COMPUTED, // mapping failed
177     ERR_APPL_NOT_LOADED, // pattern was not loaded
178     ERR_APPL_BAD_DIMENTION, // wrong shape dimention
179     ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
180     // Apply(face)
181     ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
182     ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
183     ERR_APPLF_INTERNAL_EEROR, // program error
184     // Apply(volume)
185     ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
186     // Apply(mesh_face)
187     ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
188     // MakeMesh
189     ERR_MAKEM_NOT_COMPUTED // mapping failed
190   };
191
192   ErrorCode GetErrorCode() const { return myErrorCode; }
193   // return ErrorCode of the last operation
194
195   bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
196   // Return true if a pattern was successfully loaded
197
198   bool Is2D() const { return myIs2D; }
199   // Return true if the loaded pattern is a 2D one
200
201   bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
202   // Return nodes coordinates of the pattern
203
204   const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
205   // Return indices of key-points within the sequences returned by
206   // GetPoints() and GetMappedPoints()
207   
208   const std::list< std::list< int > >& GetElementPointIDs (bool applied) const
209   { return myElemXYZIDs.empty() || !applied ? myElemPointIDs : myElemXYZIDs; }
210   // Return nodal connectivity of the elements of the pattern
211
212   void DumpPoints() const;
213   // Debug
214
215   // -----------------------------
216   // Utilities for advanced usage
217   // -----------------------------
218
219   TopoDS_Shape GetSubShape( const int i ) const {
220     if ( i < 1 || i > myShapeIDMap.Extent() ) return TopoDS_Shape();
221     return myShapeIDMap( i );
222   }
223   // Return a shape from myShapeIDMap where shapes are indexed so that first go
224   // ordered vertices, then ordered edge, then faces and maybe a shell
225
226 private:
227   // private methods
228
229   struct TPoint {
230     gp_XYZ myInitXYZ; // loaded postion
231     gp_XY  myInitUV;
232     double myInitU; // [0,1]
233     gp_Pnt myXYZ; // position to compute
234     gp_XY  myUV;
235     double myU;
236     TPoint();
237   };
238   friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
239
240   bool setErrorCode( const ErrorCode theErrorCode )
241   { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
242   // set ErrorCode and return true if it is Ok
243
244   bool setShapeToMesh(const TopoDS_Shape& theShape);
245   // Set a shape to be meshed. Return True if meshing is possible
246
247   list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
248   // Return list of points located on theShape.
249   // A list of edge-points include vertex-points (for 2D pattern only).
250   // A list of face-points doesnt include edge-points.
251   // A list of volume-points doesnt include face-points.
252
253   list< TPoint* > & getShapePoints(const int theShapeID);
254   // Return list of points located on the shape
255
256   bool findBoundaryPoints();
257   // If loaded from file, find points to map on edges and faces and
258   // compute their parameters
259
260   void arrangeBoundaries (list< list< TPoint* > >& boundaryPoints);
261   // if there are several wires, arrange boundaryPoints so that
262   // the outer wire goes first and fix inner wires orientation;
263   // update myKeyPointIDs to correspond to the order of key-points
264   // in boundaries; sort internal boundaries by the nb of key-points
265
266   void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
267   // compute coordinates of points on theEdge
268
269   bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
270                                 const gp_XY&                   theInitUV,
271                                 gp_XY&                         theUV,
272                                 bool &                         theIsDeformed);
273   // compute UV by intersection of iso-lines found by points on edges
274
275   bool compUVByElasticIsolines(const list< list< TPoint* > >& boundaryPoints,
276                                const list< TPoint* >&         pointsToCompute);
277   // compute UV as nodes of iso-poly-lines consisting of
278   // segments keeping relative size as in the pattern
279
280   double setFirstEdge (list< TopoDS_Edge > & theWire, int theFirstEdgeID);
281   // choose the best first edge of theWire; return the summary distance
282   // between point UV computed by isolines intersection and
283   // eventual UV got from edge p-curves
284
285   typedef list< list< TopoDS_Edge > > TListOfEdgesList;
286
287   bool sortSameSizeWires (TListOfEdgesList &                theWireList,
288                           const TListOfEdgesList::iterator& theFromWire,
289                           const TListOfEdgesList::iterator& theToWire,
290                           const int                         theFirstEdgeID,
291                           list< list< TPoint* > >&          theEdgesPointsList );
292   // sort wires in theWireList from theFromWire until theToWire,
293   // the wires are set in the order to correspond to the order
294   // of boundaries; after sorting, edges in the wires are put
295   // in a good order, point UVs on edges are computed and points
296   // are appended to theEdgesPointsList
297
298   typedef std::set<const SMDS_MeshNode*> TNodeSet;
299
300   void mergePoints (const bool uniteGroups);
301   // Merge XYZ on edges and/or faces.
302
303   void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
304                         const bool                                 toCreatePolygons,
305                         const bool                                 toCreatePolyedrs);
306   // prepare intermediate data to create Polygons and Polyhedrons
307
308   void createElements(SMESH_Mesh*                                 theMesh,
309                       const std::vector<const SMDS_MeshNode* >&   theNodesVector,
310                       const std::list< std::list< int > > &       theElemNodeIDs,
311                       const std::vector<const SMDS_MeshElement*>& theElements);
312   // add elements to the mesh
313
314   bool getFacesDefinition(const SMDS_MeshNode**                      theBndNodes,
315                           const int                                  theNbBndNodes,
316                           const std::vector< const SMDS_MeshNode* >& theNodes,
317                           std::list< int >&                          theFaceDefs,
318                           std::vector<int>&                          theQuantity);
319   // fill faces definition for a volume face defined by theBndNodes
320   // return true if a face definition changes
321   
322
323   bool isReversed(const SMDS_MeshNode*    theFirstNode,
324                   const std::list< int >& theIdsList) const;
325   // check xyz ids order in theIdsList taking into account
326   // theFirstNode on a link
327
328   void clearMesh(SMESH_Mesh* theMesh) const;
329   // clear mesh elements existing on myShape in theMesh
330
331   static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh*         theMesh,
332                                                   const TopoDS_Shape& theShape);
333   // return submesh containing elements bound to theShape in theMesh
334
335  private:
336   // fields
337
338   typedef std::list< int > TElemDef; // element definition is its nodes ids
339
340   bool                                 myIs2D;
341   std::vector< TPoint >                myPoints;
342   std::list< int >                     myKeyPointIDs;
343   std::list< TElemDef >                myElemPointIDs;
344
345   ErrorCode                            myErrorCode;
346   bool                                 myIsComputed;
347   bool                                 myIsBoundaryPointsFound;
348
349   TopoDS_Shape                         myShape;
350   // all functions assure that shapes are indexed so that first go
351   // ordered vertices, then ordered edge, then faces and maybe a shell
352   TopTools_IndexedMapOfOrientedShape   myShapeIDMap;
353   std::map< int, list< TPoint* > >     myShapeIDToPointsMap;
354
355   // for the 2d case:
356   // nb of key-points in each of pattern boundaries
357   std::list< int >                     myNbKeyPntInBoundary;
358
359   
360   // to compute while applying to mesh elements, not to shapes
361
362   std::vector<gp_XYZ>                  myXYZ;            // XYZ of nodes to create
363   std::list< TElemDef >                myElemXYZIDs;     // new elements definitions
364   std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
365   std::vector<const SMDS_MeshElement*> myElements;       // refined elements
366   std::vector<const SMDS_MeshNode*>    myOrderedNodes;
367
368    // elements to replace with polygon or polyhedron
369   std::vector<const SMDS_MeshElement*> myPolyElems;
370   // definitions of new poly elements
371   std::list< TElemDef >                myPolyElemXYZIDs;
372   std::list< std::vector<int> >        myPolyhedronQuantities;
373
374   // map a boundary to XYZs on it;
375   // a boundary (edge or face) is defined as a set of its nodes,
376   // XYZs on a boundary are indices of myXYZ s
377   std::map<TNodeSet,std::list<std::list<int> > >  myIdsOnBoundary;
378   // map XYZ id to element it is in
379   std::map< int, std::list< TElemDef* > >         myReverseConnectivity;
380 };
381
382
383 #endif