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