Salome HOME
Merge with OCC-V2_1_0_deb
[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 <iostream>
31
32 #include <TopoDS_Shape.hxx>
33 #include <TopTools_IndexedMapOfOrientedShape.hxx>
34 #include <gp_XYZ.hxx>
35 #include <gp_XY.hxx>
36 #include <gp_Pnt.hxx>
37
38 class SMESH_Mesh;
39 class TopoDS_Shell;
40 class TopoDS_Vertex;
41 class TopoDS_Face;
42 class TopoDS_Edge;
43
44 //
45 // Class allowing meshing by mapping of pre-defined patterns: it generates
46 // a 2D mesh on a geometrical face or a 3D mesh inside a geometrical block
47 // of 6 faces.
48 //
49
50 class SMESH_Pattern {
51  public:
52   
53   SMESH_Pattern ();
54
55   void Clear();
56   // clear fields
57
58   bool Load (const char* theFileContents);
59   // Load a pattern from <theFile>
60
61   bool Load (SMESH_Mesh*        theMesh,
62              const TopoDS_Face& theFace,
63              bool               theProject);
64   // Create a pattern from the mesh built on <theFace>.
65   // <theProject>==true makes override nodes positions
66   // on <theFace> computed by mesher
67
68   bool Load (SMESH_Mesh*         theMesh,
69              const TopoDS_Shell& theBlock);
70   // Create a pattern from the mesh built on <theBlock>
71
72   bool Save (std::ostream& theFile);
73   // Save the loaded pattern into theFile
74
75   bool Apply (const TopoDS_Face&   theFace,
76               const TopoDS_Vertex& theVertexOnKeyPoint1,
77               const bool           theReverse);
78   // Compute nodes coordinates applying
79   // the loaded pattern to <theFace>. The first key-point
80   // will be mapped into <theVertexOnKeyPoint1>, which must
81   // be in the outer wire of theFace
82
83   bool Apply (const TopoDS_Shell&  theBlock,
84               const TopoDS_Vertex& theVertex000,
85               const TopoDS_Vertex& theVertex001);
86   // Compute nodes coordinates applying
87   // the loaded pattern to <theBlock>. The (0,0,0) key-point
88   // will be mapped into <theVertex000>. The
89   // (0,0,1) key-point will be mapped into <theVertex001>.
90
91   bool GetMappedPoints ( std::list<const gp_XYZ *> & thePoints );
92   // Return nodes coordinates computed by Apply() method
93
94   bool MakeMesh(SMESH_Mesh* theMesh);
95   // Create nodes and elements in <theMesh> using nodes
96   // coordinates computed by either of Apply...() methods
97
98
99   // Inquiries
100
101   enum ErrorCode {
102     ERR_OK,
103     // Load(file)
104     ERR_READ_NB_POINTS, // couldn't read nb of points
105     ERR_READ_POINT_COORDS, // invalid nb of point coordinates
106     ERR_READ_TOO_FEW_POINTS,  // too few points in a pattern
107     ERR_READ_3D_COORD,  // coordinate of 3D point out of [0,1] range
108     ERR_READ_NO_KEYPOINT, // no key-points in 2D pattern
109     ERR_READ_BAD_INDEX, // invalid point index
110     ERR_READ_ELEM_POINTS, // invalid nb of points in element
111     ERR_READ_NO_ELEMS, // no elements in a pattern
112     ERR_READ_BAD_KEY_POINT, // a key-point not on a boundary
113     // Save(file)
114     ERR_SAVE_NOT_LOADED, // pattern was not loaded
115     // Load(shape)
116     ERR_LOAD_EMPTY_SUBMESH, // no elements to load
117     // Load(face)
118     ERR_LOADF_NARROW_FACE, // too narrow face
119     ERR_LOADF_CLOSED_FACE, // closed face
120     // Load(volume)
121     ERR_LOADV_BAD_SHAPE, // volume is not a brick of 6 faces
122     ERR_LOADV_COMPUTE_PARAMS, // cant compute point parameters
123     // Apply(shape)
124     ERR_APPL_NOT_COMPUTED, // mapping failed
125     ERR_APPL_NOT_LOADED, // pattern was not loaded
126     ERR_APPL_BAD_DIMENTION, // wrong shape dimention
127     ERR_APPL_BAD_NB_VERTICES, // keypoints - vertices mismatch
128     // Apply(face)
129     ERR_APPLF_BAD_TOPOLOGY, // bad pattern topology
130     ERR_APPLF_BAD_VERTEX, // first vertex not on an outer face boundary
131     ERR_APPLF_INTERNAL_EEROR, // program error
132     // Apply(volume)
133     ERR_APPLV_BAD_SHAPE, // volume is not a brick of 6 faces
134     // MakeMesh
135     ERR_MAKEM_NOT_COMPUTED // mapping failed
136   };
137
138   ErrorCode GetErrorCode() const { return myErrorCode; }
139   // return ErrorCode of the last operation
140
141   bool IsLoaded() const { return !myPoints.empty() && !myElemPointIDs.empty(); }
142   // Return true if a pattern was successfully loaded
143
144   bool Is2D() const { return myIs2D; }
145   // Return true if the loaded pattern is a 2D one
146
147   bool GetPoints ( std::list<const gp_XYZ *> & thePoints ) const;
148   // Return nodes coordinates of the pattern
149
150   const std::list< int > & GetKeyPointIDs () const { return myKeyPointIDs; }
151   // Return indices of key-points within the sequences returned by
152   // GetPoints() and GetMappedPoints()
153   
154   const std::list< std::list< int > >& GetElementPointIDs () const
155   { return myElemPointIDs; }
156   // Return nodal connectivity of the elements of the pattern
157
158   void DumpPoints() const;
159   // Debug
160
161
162  private:
163   // private methods
164
165   struct TPoint {
166     gp_XYZ myInitXYZ; // loaded postion
167     gp_XY  myInitUV;
168     double myInitU; // [0,1]
169     gp_Pnt myXYZ; // position to compute
170     gp_XY  myUV;
171     double myU;
172     TPoint();
173   };
174   friend std::ostream & operator <<(std::ostream & OS, const TPoint& p);
175
176   bool setErrorCode( const ErrorCode theErrorCode )
177   { myErrorCode = theErrorCode; return myErrorCode == ERR_OK; }
178   // set ErrorCode and return true if it is Ok
179
180   bool setShapeToMesh(const TopoDS_Shape& theShape);
181   // Set a shape to be meshed. Return True if meshing is possible
182
183   list< TPoint* > & getShapePoints(const TopoDS_Shape& theShape);
184   // Return list of points located on theShape.
185   // A list of edge-points include vertex-points (for 2D pattern only).
186   // A list of face-points doesnt include edge-points.
187   // A list of volume-points doesnt include face-points.
188
189   list< TPoint* > & getShapePoints(const int theShapeID);
190   // Return list of points located on the shape
191
192   bool findBoundaryPoints();
193   // If loaded from file, find points to map on edges and faces and
194   // compute their parameters
195
196   void arrangeBoundaries (list< list< TPoint* > >& boundaryPoints);
197   // if there are several wires, arrange boundaryPoints so that
198   // the outer wire goes first and fix inner wires orientation;
199   // update myKeyPointIDs to correspond to the order of key-points
200   // in boundaries; sort internal boundaries by the nb of key-points
201
202   void computeUVOnEdge( const TopoDS_Edge& theEdge, const list< TPoint* > & ePoints );
203   // compute coordinates of points on theEdge
204
205   bool compUVByIsoIntersection (const list< list< TPoint* > >& boundaryPoints,
206                                 const gp_XY&                   theInitUV,
207                                 gp_XY&                         theUV,
208                                 bool &                         theIsDeformed);
209   // compute UV by intersection of iso-lines found by points on edges
210
211   bool compUVByElasticIsolines(const list< list< TPoint* > >& boundaryPoints,
212                                const list< TPoint* >&         pointsToCompute);
213   // compute UV as nodes of iso-poly-lines consisting of
214   // segments keeping relative size as in the pattern
215
216   double setFirstEdge (list< TopoDS_Edge > & theWire, int theFirstEdgeID);
217   // choose the best first edge of theWire; return the summary distance
218   // between point UV computed by isolines intersection and
219   // eventual UV got from edge p-curves
220
221   typedef list< list< TopoDS_Edge > > TListOfEdgesList;
222
223   bool sortSameSizeWires (TListOfEdgesList &                theWireList,
224                           const TListOfEdgesList::iterator& theFromWire,
225                           const TListOfEdgesList::iterator& theToWire,
226                           const int                         theFirstEdgeID,
227                           list< list< TPoint* > >&          theEdgesPointsList );
228   // sort wires in theWireList from theFromWire until theToWire,
229   // the wires are set in the order to correspond to the order
230   // of boundaries; after sorting, edges in the wires are put
231   // in a good order, point UVs on edges are computed and points
232   // are appended to theEdgesPointsList
233   
234  private:
235   // fields
236
237   bool                              myIs2D;
238   std::vector< TPoint >             myPoints;
239   std::list< int >                  myKeyPointIDs;
240   std::list< std::list< int > >     myElemPointIDs;
241
242   ErrorCode                         myErrorCode;
243   bool                              myIsComputed;
244   bool                              myIsBoundaryPointsFound;
245
246   TopoDS_Shape                      myShape;
247   // all functions assure that shapes are indexed so that first go
248   // ordered vertices, then ordered edge, then faces and a shell
249   TopTools_IndexedMapOfOrientedShape myShapeIDMap;
250   //TopTools_IndexedMapOfShape        myShapeIDMap;
251   std::map< int, list< TPoint* > >  myShapeIDToPointsMap;
252
253   std::list< int >                  myNbKeyPntInBoundary; //for the 2d case
254 };
255
256
257 #endif