Salome HOME
0a54cfa711b7dc4e07225f590e6fc4244bb39535
[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   // Create nodes and elements in <theMesh> using nodes
133   // coordinates computed by either of Apply...() methods
134
135
136   // Inquiries
137
138   enum ErrorCode {
139     ERR_OK,
140     // Load(file)
141     ERR_READ_NB_POINTS, // couldn't read nb of points
142     ERR_READ_POINT_COORDS, // invalid nb of point coordinates
143     ERR_READ_TOO_FEW_POINTS,  // too few points in a pattern
144     ERR_READ_3D_COORD,  // coordinate of 3D point out of [0,1] range
145     ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
146     ERR_READ_BAD_INDEX, // invalid point index
147     ERR_READ_ELEM_POINTS, // invalid nb of points in element
148     ERR_READ_NO_ELEMS, // no elements in a pattern
149     ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
150     // Save(file)
151     ERR_SAVE_NOT_LOADED, // pattern was not loaded
152     // Load(shape)
153     ERR_LOAD_EMPTY_SUBMESH, // no elements to load
154     // Load(face)
155     ERR_LOADF_NARROW_FACE, // too narrow face
156     ERR_LOADF_CLOSED_FACE, // closed face
157     // Load(volume)
158     ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
159     ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
160     // Apply(shape)
161     ERR_APPL_NOT_COMPUTED, // mapping failed
162     ERR_APPL_NOT_LOADED, // pattern was not loaded
163     ERR_APPL_BAD_DIMENTION, // wrong shape dimention
164     ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
165     // Apply(face)
166     ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
167     ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
168     ERR_APPLF_INTERNAL_EEROR, // program error
169     // Apply(volume)
170     ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
171     // Apply(mesh_face)
172     ERR_APPLF_BAD_FACE_GEOM, // bad face geometry
173     // MakeMesh
174     ERR_MAKEM_NOT_COMPUTED // mapping failed
175   };
176
177   ErrorCode GetErrorCode() const { return myErrorCode; }
178   // return ErrorCode of the last operation
179
180   bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
181   // Return true if a pattern was successfully loaded
182
183   bool Is2D() const { return myIs2D; }
184   // Return true if the loaded pattern is a 2D one
185
186   bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
187   // Return nodes coordinates of the pattern
188
189   const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
190   // Return indices of key-points within the sequences returned by
191   // GetPoints() and GetMappedPoints()
192   
193   const std::list< std::list< int > >& GetElementPointIDs () const
194   { return myElemXYZIDs.empty() ? myElemPointIDs : myElemXYZIDs; }
195   // Return nodal connectivity of the elements of the pattern
196
197   void DumpPoints() const;
198   // Debug
199
200
201  private:
202   // private methods
203
204   struct TPoint {
205     gp_XYZ myInitXYZ; // loaded postion
206     gp_XY  myInitUV;
207     double myInitU; // [0,1]
208     gp_Pnt myXYZ; // position to compute
209     gp_XY  myUV;
210     double myU;
211     TPoint();
212   };
213   friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
214
215   bool setErrorCode( const ErrorCode theErrorCode )
216   { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
217   // set ErrorCode and return true if it is Ok
218
219   bool setShapeToMesh(const TopoDS_Shape& theShape);
220   // Set a shape to be meshed. Return True if meshing is possible
221
222   list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
223   // Return list of points located on theShape.
224   // A list of edge-points include vertex-points (for 2D pattern only).
225   // A list of face-points doesnt include edge-points.
226   // A list of volume-points doesnt include face-points.
227
228   list< TPoint* > & getShapePoints(const int theShapeID);
229   // Return list of points located on the shape
230
231   bool findBoundaryPoints();
232   // If loaded from file, find points to map on edges and faces and
233   // compute their parameters
234
235   void arrangeBoundaries (list< list< TPoint* > >& boundaryPoints);
236   // if there are several wires, arrange boundaryPoints so that
237   // the outer wire goes first and fix inner wires orientation;
238   // update myKeyPointIDs to correspond to the order of key-points
239   // in boundaries; sort internal boundaries by the nb of key-points
240
241   void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
242   // compute coordinates of points on theEdge
243
244   bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
245                                 const gp_XY&                   theInitUV,
246                                 gp_XY&                         theUV,
247                                 bool &                         theIsDeformed);
248   // compute UV by intersection of iso-lines found by points on edges
249
250   bool compUVByElasticIsolines(const list< list< TPoint* > >& boundaryPoints,
251                                const list< TPoint* >&         pointsToCompute);
252   // compute UV as nodes of iso-poly-lines consisting of
253   // segments keeping relative size as in the pattern
254
255   double setFirstEdge (list< TopoDS_Edge > & theWire, int theFirstEdgeID);
256   // choose the best first edge of theWire; return the summary distance
257   // between point UV computed by isolines intersection and
258   // eventual UV got from edge p-curves
259
260   typedef list< list< TopoDS_Edge > > TListOfEdgesList;
261
262   bool sortSameSizeWires (TListOfEdgesList &                theWireList,
263                           const TListOfEdgesList::iterator& theFromWire,
264                           const TListOfEdgesList::iterator& theToWire,
265                           const int                         theFirstEdgeID,
266                           list< list< TPoint* > >&          theEdgesPointsList );
267   // sort wires in theWireList from theFromWire until theToWire,
268   // the wires are set in the order to correspond to the order
269   // of boundaries; after sorting, edges in the wires are put
270   // in a good order, point UVs on edges are computed and points
271   // are appended to theEdgesPointsList
272
273   typedef std::set<const SMDS_MeshNode*> TNodeSet;
274   void mergePoints (std::map<TNodeSet,std::list<std::list<int> > >&  xyzIndGroups,
275                     std::map< int, std::list< std::list< int >* > >& reverseConnectivity);
276   // Look for coincident points between myXYZs indexed with
277   // list<int> of each element of xyzIndGroups. Coincident indices
278   // are merged in myElemXYZIDs using reverseConnectivity.
279
280  private:
281   // fields
282
283   bool                              myIs2D;
284   std::vector< TPoint >             myPoints;
285   std::list< int >                  myKeyPointIDs;
286   std::list< std::list< int > >     myElemPointIDs;
287
288   ErrorCode                         myErrorCode;
289   bool                              myIsComputed;
290   bool                              myIsBoundaryPointsFound;
291
292   TopoDS_Shape                      myShape;
293   // all functions assure that shapes are indexed so that first go
294   // ordered vertices, then ordered edge, then faces and maybe a shell
295   TopTools_IndexedMapOfOrientedShape myShapeIDMap;
296   //TopTools_IndexedMapOfShape        myShapeIDMap;
297   std::map< int, list< TPoint* > >  myShapeIDToPointsMap;
298
299   // for the 2d case:
300   // nb of key-points in each of pattern boundaries
301   std::list< int >                  myNbKeyPntInBoundary;
302
303   // to compute while applying to mesh elements, not to shapes
304   std::vector<gp_XYZ>               myXYZ;
305   std::list< std::list< int > >     myElemXYZIDs;
306   std::map< int, const SMDS_MeshNode*> myXYZIdToNodeMap; // map id to node of a refined element
307   std::vector<const SMDS_MeshElement*> myElements; // refined elements
308   std::vector<const SMDS_MeshNode*> myOrderedNodes;
309 };
310
311
312 #endif