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