Salome HOME
Merging with branch OCC_development_for_3_2_0b1
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 "SMESH_SMESH.hxx"
28
29 #include <vector>
30 #include <list>
31 #include <map>
32 #include <set>
33 #include <iostream>
34
35 #include <TopoDS_Shape.hxx>
36 #include <TopTools_IndexedMapOfOrientedShape.hxx>
37 #include <gp_XYZ.hxx>
38 #include <gp_XY.hxx>
39 #include <gp_Pnt.hxx>
40
41 class SMDS_MeshElement;
42 class SMDS_MeshFace;
43 class SMDS_MeshVolume;
44 class SMDS_MeshNode;
45 class SMESH_Mesh;
46 class SMESHDS_SubMesh;
47 class TopoDS_Shell;
48 class TopoDS_Vertex;
49 class TopoDS_Face;
50 class TopoDS_Edge;
51
52 //
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
55 // of 6 faces.
56 //
57
58 class SMESH_EXPORT SMESH_Pattern {
59  public:
60   
61   SMESH_Pattern ();
62
63   void Clear();
64   // clear fields
65
66   bool Load (const char* theFileContents);
67   // Load a pattern from <theFileContents>
68
69   bool Load (SMESH_Mesh*        theMesh,
70              const TopoDS_Face& theFace,
71              bool               theProject);
72   // Create a pattern from the mesh built on <theFace>.
73   // <theProject>==true makes override nodes positions
74   // on <theFace> computed by mesher
75
76   bool Load (SMESH_Mesh*         theMesh,
77              const TopoDS_Shell& theBlock);
78   // Create a pattern from the mesh built on <theBlock>
79
80   bool Save (std::ostream& theFile);
81   // Save the loaded pattern into theFile
82
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
90
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>.
98
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
105
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
112
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
120   // node.
121
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
129   // node.
130
131   bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
132   // Return nodes coordinates computed by Apply() method
133
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
139
140
141   // Inquiries
142
143   enum ErrorCode {
144     ERR_OK,
145     // Load(file)
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
155     // Save(file)
156     ERR_SAVE_NOT_LOADED, // pattern was not loaded
157     // Load(shape)
158     ERR_LOAD_EMPTY_SUBMESH, // no elements to load
159     // Load(face)
160     ERR_LOADF_NARROW_FACE, // too narrow face
161     ERR_LOADF_CLOSED_FACE, // closed face
162     // Load(volume)
163     ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
164     ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
165     // Apply(shape)
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
170     // Apply(face)
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
174     // Apply(volume)
175     ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
176     // Apply(mesh_face)
177     ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
178     // MakeMesh
179     ERR_MAKEM_NOT_COMPUTED // mapping failed
180   };
181
182   ErrorCode GetErrorCode() const { return myErrorCode; }
183   // return ErrorCode of the last operation
184
185   bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
186   // Return true if a pattern was successfully loaded
187
188   bool Is2D() const { return myIs2D; }
189   // Return true if the loaded pattern is a 2D one
190
191   bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
192   // Return nodes coordinates of the pattern
193
194   const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
195   // Return indices of key-points within the sequences returned by
196   // GetPoints() and GetMappedPoints()
197   
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
201
202   void DumpPoints() const;
203   // Debug
204
205
206  private:
207   // private methods
208
209   struct TPoint {
210     gp_XYZ myInitXYZ; // loaded postion
211     gp_XY  myInitUV;
212     double myInitU; // [0,1]
213     gp_Pnt myXYZ; // position to compute
214     gp_XY  myUV;
215     double myU;
216     TPoint();
217   };
218   friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
219
220   bool setErrorCode( const ErrorCode theErrorCode )
221   { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
222   // set ErrorCode and return true if it is Ok
223
224   bool setShapeToMesh(const TopoDS_Shape& theShape);
225   // Set a shape to be meshed. Return True if meshing is possible
226
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.
232
233   list< TPoint* > & getShapePoints(const int theShapeID);
234   // Return list of points located on the shape
235
236   bool findBoundaryPoints();
237   // If loaded from file, find points to map on edges and faces and
238   // compute their parameters
239
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
245
246   void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
247   // compute coordinates of points on theEdge
248
249   bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
250                                 const gp_XY&                   theInitUV,
251                                 gp_XY&                         theUV,
252                                 bool &                         theIsDeformed);
253   // compute UV by intersection of iso-lines found by points on edges
254
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
259
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
264
265   typedef list< list< TopoDS_Edge > > TListOfEdgesList;
266
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
277
278   typedef std::set<const SMDS_MeshNode*> TNodeSet;
279
280   void mergePoints (const bool uniteGroups);
281   // Merge XYZ on edges and/or faces.
282
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
287
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
293
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
301   
302
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
307
308   void clearMesh(SMESH_Mesh* theMesh) const;
309   // clear mesh elements existing on myShape in theMesh
310
311   static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh*         theMesh,
312                                                   const TopoDS_Shape& theShape);
313   // return submesh containing elements bound to theShape in theMesh
314
315  private:
316   // fields
317
318   typedef std::list< int > TElemDef; // element definition is its nodes ids
319
320   bool                                 myIs2D;
321   std::vector< TPoint >                myPoints;
322   std::list< int >                     myKeyPointIDs;
323   std::list< TElemDef >                myElemPointIDs;
324
325   ErrorCode                            myErrorCode;
326   bool                                 myIsComputed;
327   bool                                 myIsBoundaryPointsFound;
328
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;
334
335   // for the 2d case:
336   // nb of key-points in each of pattern boundaries
337   std::list< int >                     myNbKeyPntInBoundary;
338
339   
340   // to compute while applying to mesh elements, not to shapes
341
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;
347
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;
353
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;
360 };
361
362
363 #endif