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