Salome HOME
cc0bd159a1e317fbd22c4ccaa6bdb99552a72327
[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 <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 TopoDS_Shell;
45 class TopoDS_Vertex;
46 class TopoDS_Face;
47 class TopoDS_Edge;
48
49 //
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
52 // of 6 faces.
53 //
54
55 class SMESH_Pattern {
56  public:
57   
58   SMESH_Pattern ();
59
60   void Clear();
61   // clear fields
62
63   bool Load (const char* theFileContents);
64   // Load a pattern from <theFileContents>
65
66   bool Load (SMESH_Mesh*        theMesh,
67              const TopoDS_Face& theFace,
68              bool               theProject);
69   // Create a pattern from the mesh built on <theFace>.
70   // <theProject>==true makes override nodes positions
71   // on <theFace> computed by mesher
72
73   bool Load (SMESH_Mesh*         theMesh,
74              const TopoDS_Shell& theBlock);
75   // Create a pattern from the mesh built on <theBlock>
76
77   bool Save (std::ostream& theFile);
78   // Save the loaded pattern into theFile
79
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
87
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>.
95
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
102
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
109
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
117   // node.
118
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
126   // node.
127
128   bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints ) const;
129   // Return nodes coordinates computed by Apply() method
130
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
136
137
138   // Inquiries
139
140   enum ErrorCode {
141     ERR_OK,
142     // Load(file)
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
152     // Save(file)
153     ERR_SAVE_NOT_LOADED, // pattern was not loaded
154     // Load(shape)
155     ERR_LOAD_EMPTY_SUBMESH, // no elements to load
156     // Load(face)
157     ERR_LOADF_NARROW_FACE, // too narrow face
158     ERR_LOADF_CLOSED_FACE, // closed face
159     // Load(volume)
160     ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
161     ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
162     // Apply(shape)
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
167     // Apply(face)
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
171     // Apply(volume)
172     ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
173     // Apply(mesh_face)
174     ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
175     // MakeMesh
176     ERR_MAKEM_NOT_COMPUTED // mapping failed
177   };
178
179   ErrorCode GetErrorCode() const { return myErrorCode; }
180   // return ErrorCode of the last operation
181
182   bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
183   // Return true if a pattern was successfully loaded
184
185   bool Is2D() const { return myIs2D; }
186   // Return true if the loaded pattern is a 2D one
187
188   bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
189   // Return nodes coordinates of the pattern
190
191   const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
192   // Return indices of key-points within the sequences returned by
193   // GetPoints() and GetMappedPoints()
194   
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
198
199   void DumpPoints() const;
200   // Debug
201
202
203  private:
204   // private methods
205
206   struct TPoint {
207     gp_XYZ myInitXYZ; // loaded postion
208     gp_XY  myInitUV;
209     double myInitU; // [0,1]
210     gp_Pnt myXYZ; // position to compute
211     gp_XY  myUV;
212     double myU;
213     TPoint();
214   };
215   friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
216
217   bool setErrorCode( const ErrorCode theErrorCode )
218   { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
219   // set ErrorCode and return true if it is Ok
220
221   bool setShapeToMesh(const TopoDS_Shape& theShape);
222   // Set a shape to be meshed. Return True if meshing is possible
223
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.
229
230   list< TPoint* > & getShapePoints(const int theShapeID);
231   // Return list of points located on the shape
232
233   bool findBoundaryPoints();
234   // If loaded from file, find points to map on edges and faces and
235   // compute their parameters
236
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
242
243   void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
244   // compute coordinates of points on theEdge
245
246   bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
247                                 const gp_XY&                   theInitUV,
248                                 gp_XY&                         theUV,
249                                 bool &                         theIsDeformed);
250   // compute UV by intersection of iso-lines found by points on edges
251
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
256
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
261
262   typedef list< list< TopoDS_Edge > > TListOfEdgesList;
263
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
274
275   typedef std::set<const SMDS_MeshNode*> TNodeSet;
276
277   void mergePoints (const bool uniteGroups);
278   // Merge XYZ on edges and/or faces.
279
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
284
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
290
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
298   
299
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
304
305   void clearMesh(SMESH_Mesh* theMesh) const;
306   // clear mesh elements existing on myShape in theMesh
307
308  private:
309   // fields
310
311   typedef std::list< int > TElemDef; // element definition is its nodes ids
312
313   bool                                 myIs2D;
314   std::vector< TPoint >                myPoints;
315   std::list< int >                     myKeyPointIDs;
316   std::list< TElemDef >                myElemPointIDs;
317
318   ErrorCode                            myErrorCode;
319   bool                                 myIsComputed;
320   bool                                 myIsBoundaryPointsFound;
321
322   TopoDS_Shape                         myShape;
323   // all functions assure that shapes are indexed so that first go
324   // ordered vertices, then ordered edge, then faces and maybe a shell
325   TopTools_IndexedMapOfOrientedShape   myShapeIDMap;
326   std::map< int, list< TPoint* > >     myShapeIDToPointsMap;
327
328   // for the 2d case:
329   // nb of key-points in each of pattern boundaries
330   std::list< int >                     myNbKeyPntInBoundary;
331
332   
333   // to compute while applying to mesh elements, not to shapes
334
335   std::vector<gp_XYZ>                  myXYZ;            // XYZ of nodes to create
336   std::list< TElemDef >                myElemXYZIDs;     // new elements definitions
337   std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map XYZ id to node of a refined element
338   std::vector<const SMDS_MeshElement*> myElements;       // refined elements
339   std::vector<const SMDS_MeshNode*>    myOrderedNodes;
340
341    // elements to replace with polygon or polyhedron
342   std::vector<const SMDS_MeshElement*> myPolyElems;
343   // definitions of new poly elements
344   std::list< TElemDef >                myPolyElemXYZIDs;
345   std::list< std::vector<int> >        myPolyhedronQuantities;
346
347   // map a boundary to XYZs on it;
348   // a boundary (edge or face) is defined as a set of its nodes,
349   // XYZs on a boundary are indices of myXYZ s
350   std::map<TNodeSet,std::list<std::list<int> > >  myIdsOnBoundary;
351   // map XYZ id to element it is in
352   std::map< int, std::list< TElemDef* > >         myReverseConnectivity;
353 };
354
355
356 #endif