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