Salome HOME
Merging with WPdev
[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 "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 = false);
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
144   enum ErrorCode {
145     ERR_OK,
146     // Load(file)
147     ERR_READ_NB_POINTS, // couldn't read nb of points
148     ERR_READ_POINT_COORDS, // invalid nb of point coordinates
149     ERR_READ_TOO_FEW_POINTS,  // too few points in a pattern
150     ERR_READ_3D_COORD,  // coordinate of 3D point out of [0,1] range
151     ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
152     ERR_READ_BAD_INDEX, // invalid point index
153     ERR_READ_ELEM_POINTS, // invalid nb of points in element
154     ERR_READ_NO_ELEMS, // no elements in a pattern
155     ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
156     // Save(file)
157     ERR_SAVE_NOT_LOADED, // pattern was not loaded
158     // Load(shape)
159     ERR_LOAD_EMPTY_SUBMESH, // no elements to load
160     // Load(face)
161     ERR_LOADF_NARROW_FACE, // too narrow face
162     ERR_LOADF_CLOSED_FACE, // closed face
163     ERR_LOADF_CANT_PROJECT, // impossible to project nodes
164     // Load(volume)
165     ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
166     ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
167     // Apply(shape)
168     ERR_APPL_NOT_COMPUTED, // mapping failed
169     ERR_APPL_NOT_LOADED, // pattern was not loaded
170     ERR_APPL_BAD_DIMENTION, // wrong shape dimention
171     ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
172     // Apply(face)
173     ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
174     ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
175     ERR_APPLF_INTERNAL_EEROR, // program error
176     // Apply(volume)
177     ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
178     // Apply(mesh_face)
179     ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
180     // MakeMesh
181     ERR_MAKEM_NOT_COMPUTED // mapping failed
182   };
183
184   ErrorCode GetErrorCode() const { return myErrorCode; }
185   // return ErrorCode of the last operation
186
187   bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
188   // Return true if a pattern was successfully loaded
189
190   bool Is2D() const { return myIs2D; }
191   // Return true if the loaded pattern is a 2D one
192
193   bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
194   // Return nodes coordinates of the pattern
195
196   const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
197   // Return indices of key-points within the sequences returned by
198   // GetPoints() and GetMappedPoints()
199   
200   const std::list< std::list< int > >& GetElementPointIDs (bool applied) const
201   { return myElemXYZIDs.empty() || !applied ? myElemPointIDs : myElemXYZIDs; }
202   // Return nodal connectivity of the elements of the pattern
203
204   void DumpPoints() const;
205   // Debug
206
207   // -----------------------------
208   // Utilities for advanced usage
209   // -----------------------------
210
211   TopoDS_Shape GetSubShape( const int i ) const {
212     if ( i < 1 || i > myShapeIDMap.Extent() ) return TopoDS_Shape();
213     return myShapeIDMap( i );
214   }
215   // Return a shape from myShapeIDMap where shapes are indexed so that first go
216   // ordered vertices, then ordered edge, then faces and maybe a shell
217
218 private:
219   // private methods
220
221   struct TPoint {
222     gp_XYZ myInitXYZ; // loaded postion
223     gp_XY  myInitUV;
224     double myInitU; // [0,1]
225     gp_Pnt myXYZ; // position to compute
226     gp_XY  myUV;
227     double myU;
228     TPoint();
229   };
230   friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
231
232   bool setErrorCode( const ErrorCode theErrorCode )
233   { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
234   // set ErrorCode and return true if it is Ok
235
236   bool setShapeToMesh(const TopoDS_Shape& theShape);
237   // Set a shape to be meshed. Return True if meshing is possible
238
239   list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
240   // Return list of points located on theShape.
241   // A list of edge-points include vertex-points (for 2D pattern only).
242   // A list of face-points doesnt include edge-points.
243   // A list of volume-points doesnt include face-points.
244
245   list< TPoint* > & getShapePoints(const int theShapeID);
246   // Return list of points located on the shape
247
248   bool findBoundaryPoints();
249   // If loaded from file, find points to map on edges and faces and
250   // compute their parameters
251
252   void arrangeBoundaries (list< list< TPoint* > >& boundaryPoints);
253   // if there are several wires, arrange boundaryPoints so that
254   // the outer wire goes first and fix inner wires orientation;
255   // update myKeyPointIDs to correspond to the order of key-points
256   // in boundaries; sort internal boundaries by the nb of key-points
257
258   void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
259   // compute coordinates of points on theEdge
260
261   bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
262                                 const gp_XY&                   theInitUV,
263                                 gp_XY&                         theUV,
264                                 bool &                         theIsDeformed);
265   // compute UV by intersection of iso-lines found by points on edges
266
267   bool compUVByElasticIsolines(const list< list< TPoint* > >& boundaryPoints,
268                                const list< TPoint* >&         pointsToCompute);
269   // compute UV as nodes of iso-poly-lines consisting of
270   // segments keeping relative size as in the pattern
271
272   double setFirstEdge (list< TopoDS_Edge > & theWire, int theFirstEdgeID);
273   // choose the best first edge of theWire; return the summary distance
274   // between point UV computed by isolines intersection and
275   // eventual UV got from edge p-curves
276
277   typedef list< list< TopoDS_Edge > > TListOfEdgesList;
278
279   bool sortSameSizeWires (TListOfEdgesList &                theWireList,
280                           const TListOfEdgesList::iterator& theFromWire,
281                           const TListOfEdgesList::iterator& theToWire,
282                           const int                         theFirstEdgeID,
283                           list< list< TPoint* > >&          theEdgesPointsList );
284   // sort wires in theWireList from theFromWire until theToWire,
285   // the wires are set in the order to correspond to the order
286   // of boundaries; after sorting, edges in the wires are put
287   // in a good order, point UVs on edges are computed and points
288   // are appended to theEdgesPointsList
289
290   typedef std::set<const SMDS_MeshNode*> TNodeSet;
291
292   void mergePoints (const bool uniteGroups);
293   // Merge XYZ on edges and/or faces.
294
295   void makePolyElements(const std::vector< const SMDS_MeshNode* >& theNodes,
296                         const bool                                 toCreatePolygons,
297                         const bool                                 toCreatePolyedrs);
298   // prepare intermediate data to create Polygons and Polyhedrons
299
300   void createElements(SMESH_Mesh*                                 theMesh,
301                       const std::vector<const SMDS_MeshNode* >&   theNodesVector,
302                       const std::list< std::list< int > > &       theElemNodeIDs,
303                       const std::vector<const SMDS_MeshElement*>& theElements);
304   // add elements to the mesh
305
306   bool getFacesDefinition(const SMDS_MeshNode**                      theBndNodes,
307                           const int                                  theNbBndNodes,
308                           const std::vector< const SMDS_MeshNode* >& theNodes,
309                           std::list< int >&                          theFaceDefs,
310                           std::vector<int>&                          theQuantity);
311   // fill faces definition for a volume face defined by theBndNodes
312   // return true if a face definition changes
313   
314
315   bool isReversed(const SMDS_MeshNode*    theFirstNode,
316                   const std::list< int >& theIdsList) const;
317   // check xyz ids order in theIdsList taking into account
318   // theFirstNode on a link
319
320   void clearMesh(SMESH_Mesh* theMesh) const;
321   // clear mesh elements existing on myShape in theMesh
322
323   static SMESHDS_SubMesh * getSubmeshWithElements(SMESH_Mesh*         theMesh,
324                                                   const TopoDS_Shape& theShape);
325   // return submesh containing elements bound to theShape in theMesh
326
327  private:
328   // fields
329
330   typedef std::list< int > TElemDef; // element definition is its nodes ids
331
332   bool                                 myIs2D;
333   std::vector< TPoint >                myPoints;
334   std::list< int >                     myKeyPointIDs;
335   std::list< TElemDef >                myElemPointIDs;
336
337   ErrorCode                            myErrorCode;
338   bool                                 myIsComputed;
339   bool                                 myIsBoundaryPointsFound;
340
341   TopoDS_Shape                         myShape;
342   // all functions assure that shapes are indexed so that first go
343   // ordered vertices, then ordered edge, then faces and maybe a shell
344   TopTools_IndexedMapOfOrientedShape   myShapeIDMap;
345   std::map< int, list< TPoint* > >     myShapeIDToPointsMap;
346
347   // for the 2d case:
348   // nb of key-points in each of pattern boundaries
349   std::list< int >                     myNbKeyPntInBoundary;
350
351   
352   // to compute while applying to mesh elements, not to shapes
353
354   std::vector<gp_XYZ>                  myXYZ;            // XYZ of nodes to create
355   std::list< TElemDef >                myElemXYZIDs;     // new elements definitions
356   std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
357   std::vector<const SMDS_MeshElement*> myElements;       // refined elements
358   std::vector<const SMDS_MeshNode*>    myOrderedNodes;
359
360    // elements to replace with polygon or polyhedron
361   std::vector<const SMDS_MeshElement*> myPolyElems;
362   // definitions of new poly elements
363   std::list< TElemDef >                myPolyElemXYZIDs;
364   std::list< std::vector<int> >        myPolyhedronQuantities;
365
366   // map a boundary to XYZs on it;
367   // a boundary (edge or face) is defined as a set of its nodes,
368   // XYZs on a boundary are indices of myXYZ s
369   std::map<TNodeSet,std::list<std::list<int> > >  myIdsOnBoundary;
370   // map XYZ id to element it is in
371   std::map< int, std::list< TElemDef* > >         myReverseConnectivity;
372 };
373
374
375 #endif