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