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