Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/smesh.git] / src / SMESH / SMESH_Block.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_Block.hxx
21 // Created   : Tue Nov 30 12:42:18 2004
22 // Author    : Edward AGAPOV (eap)
23
24
25 #ifndef SMESH_Block_HeaderFile
26 #define SMESH_Block_HeaderFile
27
28 #include "SMESH_SMESH.hxx"
29
30 //#include <Geom2d_Curve.hxx>
31 //#include <Geom_Curve.hxx>
32 //#include <Geom_Surface.hxx>
33
34 #include <TopExp.hxx>
35 #include <TopTools_IndexedMapOfOrientedShape.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Vertex.hxx>
40 #include <gp_XY.hxx>
41 #include <gp_XYZ.hxx>
42 #include <math_FunctionSetWithDerivatives.hxx>
43
44 #include <ostream>
45 #include <vector>
46 #include <list>
47
48 class SMDS_MeshVolume;
49 class SMDS_MeshNode;
50 class Adaptor3d_Surface;
51 class Adaptor2d_Curve2d;
52 class Adaptor3d_Curve;
53 class gp_Pnt;
54
55 // =========================================================
56 // class calculating coordinates of 3D points by normalized
57 // parameters inside the block and vice versa
58 // =========================================================
59
60 class SMESH_EXPORT SMESH_Block: public math_FunctionSetWithDerivatives
61 {
62  public:
63   enum TShapeID {
64     // ----------------------------
65     // Ids of the block sub-shapes
66     // ----------------------------
67     ID_NONE = 0,
68
69     ID_V000 = 1, ID_V100, ID_V010, ID_V110, ID_V001, ID_V101, ID_V011, ID_V111,
70
71     ID_Ex00, ID_Ex10, ID_Ex01, ID_Ex11,
72     ID_E0y0, ID_E1y0, ID_E0y1, ID_E1y1,
73     ID_E00z, ID_E10z, ID_E01z, ID_E11z,
74
75     ID_Fxy0, ID_Fxy1, ID_Fx0z, ID_Fx1z, ID_F0yz, ID_F1yz,
76
77     ID_Shell
78     };
79   enum { // to use TShapeID for indexing certain type subshapes
80
81     ID_FirstV = ID_V000, ID_FirstE = ID_Ex00, ID_FirstF = ID_Fxy0
82
83   };
84
85
86  public:
87   // -------------------------------------------------
88   // Block topology in terms of block sub-shapes' ids
89   // -------------------------------------------------
90
91   static int NbVertices()  { return  8; }
92   static int NbEdges()     { return 12; }
93   static int NbFaces()     { return  6; }
94   static int NbSubShapes() { return ID_Shell; }
95   // to avoid magic numbers when allocating memory for subshapes
96
97   static inline bool IsVertexID( int theShapeID )
98   { return ( theShapeID >= ID_V000 && theShapeID <= ID_V111 ); }
99
100   static inline bool IsEdgeID( int theShapeID )
101   { return ( theShapeID >= ID_Ex00 && theShapeID <= ID_E11z ); }
102
103   static inline bool IsFaceID( int theShapeID )
104   { return ( theShapeID >= ID_Fxy0 && theShapeID <= ID_F1yz ); }
105
106   static int ShapeIndex( int theShapeID )
107   {
108     if ( IsVertexID( theShapeID )) return theShapeID - ID_V000;
109     if ( IsEdgeID( theShapeID ))   return theShapeID - ID_Ex00;
110     if ( IsFaceID( theShapeID ))   return theShapeID - ID_Fxy0;
111     return 0;
112   }
113   // return index [0-...] for each type of sub-shapes,
114   // for example :
115   // ShapeIndex( ID_Ex00 ) == 0
116   // ShapeIndex( ID_Ex10 ) == 1
117
118   static void GetFaceEdgesIDs (const int faceID, vector< int >& edgeVec );
119   // return edges IDs of a face in the order u0, u1, 0v, 1v
120
121   static void GetEdgeVertexIDs (const int edgeID, vector< int >& vertexVec );
122   // return vertex IDs of an edge
123
124   static int GetCoordIndOnEdge (const int theEdgeID)
125   { return (theEdgeID < ID_E0y0) ? 1 : (theEdgeID < ID_E00z) ? 2 : 3; }
126   // return an index of a coordinate which varies along the edge
127
128   static double* GetShapeCoef (const int theShapeID);
129   // for theShapeID( TShapeID ), returns 3 coefficients used
130   // to compute an addition of an on-theShape point to coordinates
131   // of an in-shell point. If an in-shell point has parameters (Px,Py,Pz),
132   // then the addition of a point P is computed as P*kx*ky*kz and ki is
133   // defined by the returned coef like this:
134   // ki = (coef[i] == 0) ? 1 : (coef[i] < 0) ? 1 - Pi : Pi
135
136   static int GetShapeIDByParams ( const gp_XYZ& theParams );
137   // define an id of the block sub-shape by point parameters
138
139   static ostream& DumpShapeID (const int theBlockShapeID, ostream& stream);
140   // DEBUG: dump an id of a block sub-shape
141
142
143  public:
144   // ---------------
145   // Initialization
146   // ---------------
147
148   SMESH_Block();
149
150   bool LoadBlockShapes(const TopoDS_Shell&         theShell,
151                        const TopoDS_Vertex&        theVertex000,
152                        const TopoDS_Vertex&        theVertex001,
153                        TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
154   // Initialize block geometry with theShell,
155   // add sub-shapes of theBlock to theShapeIDMap so that they get
156   // IDs acoording to enum TShapeID
157
158   bool LoadBlockShapes(const TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
159   // Initialize block geometry with shapes from theShapeIDMap
160
161   bool LoadMeshBlock(const SMDS_MeshVolume*        theVolume,
162                      const int                     theNode000Index,
163                      const int                     theNode001Index,
164                      vector<const SMDS_MeshNode*>& theOrderedNodes);
165   // prepare to work with theVolume and
166   // return nodes in theVolume corners in the order of TShapeID enum
167
168   bool LoadFace(const TopoDS_Face& theFace,
169                 const int          theFaceID,
170                 const TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
171   // Load face geometry.
172   // It is enough to compute params or coordinates on the face.
173   // Face subshapes must be loaded into theShapeIDMap before
174
175   static bool Insert(const TopoDS_Shape& theShape,
176                      const int           theShapeID,
177                      TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
178   // Insert theShape into theShapeIDMap with theShapeID,
179   // Not yet set shapes preceding theShapeID are filled with compounds
180   // Return true if theShape was successfully bound to theShapeID
181
182   static bool FindBlockShapes(const TopoDS_Shell&         theShell,
183                               const TopoDS_Vertex&        theVertex000,
184                               const TopoDS_Vertex&        theVertex001,
185                               TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
186   // add sub-shapes of theBlock to theShapeIDMap so that they get
187   // IDs acoording to enum TShapeID
188
189 public:
190   // ---------------------------------
191   // Define coordinates by parameters
192   // ---------------------------------
193
194   bool VertexPoint( const int theVertexID, gp_XYZ& thePoint ) const {
195     if ( !IsVertexID( theVertexID ))           return false;
196     thePoint = myPnt[ theVertexID - ID_FirstV ]; return true;
197   }
198   // return vertex coordinates, parameters are defined by theVertexID
199
200   bool EdgePoint( const int theEdgeID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
201     if ( !IsEdgeID( theEdgeID ))                                 return false;
202     thePoint = myEdge[ theEdgeID - ID_FirstE ].Point( theParams ); return true;
203   }
204   // return coordinates of a point on edge
205
206   bool EdgeU( const int theEdgeID, const gp_XYZ& theParams, double& theU ) const {
207     if ( !IsEdgeID( theEdgeID ))                              return false;
208     theU = myEdge[ theEdgeID - ID_FirstE ].GetU( theParams ); return true;
209   }
210   // return parameter on edge by in-block parameters
211
212   bool FacePoint( const int theFaceID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
213     if ( !IsFaceID ( theFaceID ))                                return false;
214     thePoint = myFace[ theFaceID - ID_FirstF ].Point( theParams ); return true;
215   }
216   // return coordinates of a point on face
217
218   bool FaceUV( const int theFaceID, const gp_XYZ& theParams, gp_XY& theUV ) const {
219     if ( !IsFaceID ( theFaceID ))                               return false;
220     theUV = myFace[ theFaceID - ID_FirstF ].GetUV( theParams ); return true;
221   }
222   // return UV coordinates on a face by in-block parameters
223
224   bool ShellPoint( const gp_XYZ& theParams, gp_XYZ& thePoint ) const;
225   // return coordinates of a point in shell
226
227   static bool ShellPoint(const gp_XYZ&         theParams,
228                          const vector<gp_XYZ>& thePointOnShape,
229                          gp_XYZ&               thePoint );
230   // computes coordinates of a point in shell by points on sub-shapes
231   // and point parameters.
232   // thePointOnShape[ subShapeID ] must be a point on a subShape;
233   // thePointOnShape.size() == ID_Shell, thePointOnShape[0] not used
234
235
236  public:
237   // ---------------------------------
238   // Define parameters by coordinates
239   // ---------------------------------
240
241   bool ComputeParameters (const gp_Pnt& thePoint,
242                           gp_XYZ&       theParams,
243                           const int     theShapeID    = ID_Shell,
244                           const gp_XYZ& theParamsHint = gp_XYZ(-1,-1,-1));
245   // compute point parameters in the block.
246   // Note: for edges, it is better to use EdgeParameters()
247
248   bool VertexParameters(const int theVertexID, gp_XYZ& theParams);
249   // return parameters of a vertex given by TShapeID
250
251   bool EdgeParameters(const int theEdgeID, const double theU, gp_XYZ& theParams);
252   // return parameters of a point given by theU on edge
253
254
255  public:
256   // ---------------
257   // Block geomerty
258   // ---------------
259
260   
261   
262  public:
263   // ---------
264   // Services
265   // ---------
266
267   static bool IsForwardEdge (const TopoDS_Edge &                       theEdge,
268                              const TopTools_IndexedMapOfOrientedShape& theShapeIDMap) {
269     int v1ID = theShapeIDMap.FindIndex( TopExp::FirstVertex( theEdge ).Oriented( TopAbs_FORWARD ));
270     int v2ID = theShapeIDMap.FindIndex( TopExp::LastVertex( theEdge ).Oriented( TopAbs_FORWARD ));
271     return ( v1ID < v2ID );
272   }
273   // Return true if an in-block parameter increases along theEdge curve
274
275   static int GetOrderedEdges (const TopoDS_Face&        theFace,
276                               TopoDS_Vertex             theFirstVertex,
277                               std::list< TopoDS_Edge >& theEdges,
278                               std::list< int >  &       theNbVertexInWires);
279   // Return nb wires and a list of oredered edges.
280   // It is used to assign indices to subshapes.
281   // theFirstVertex may be NULL.
282   // Always try to set a seam edge first
283
284  public:
285   // -----------------------------------------------------------
286   // Methods of math_FunctionSetWithDerivatives used internally
287   // to define parameters by coordinates
288   // -----------------------------------------------------------
289   Standard_Integer NbVariables() const;
290   Standard_Integer NbEquations() const;
291   Standard_Boolean Value(const math_Vector& X,math_Vector& F) ;
292   Standard_Boolean Derivatives(const math_Vector& X,math_Matrix& D) ;
293   Standard_Boolean Values(const math_Vector& X,math_Vector& F,math_Matrix& D) ;
294   Standard_Integer GetStateNumber ();
295
296  protected:
297
298   /*!
299    * \brief Call it after geometry initialisation
300    */
301   void init();
302
303   // Note: to compute params of a point on a face, it is enough to set
304   // TFace, TEdge's and points for that face only
305
306   // Note 2: curve adaptors need to have only Value(double), FirstParameter() and
307   // LastParameter() defined to be used by Block algoritms
308
309   class SMESH_EXPORT TEdge {
310     int                myCoordInd;
311     double             myFirst;
312     double             myLast;
313     Adaptor3d_Curve*   myC3d;
314     // if mesh volume
315     gp_XYZ             myNodes[2];
316   public:
317     void Set( const int edgeID, Adaptor3d_Curve* curve, const bool isForward );
318     void Set( const int edgeID, const gp_XYZ& node1, const gp_XYZ& node2 );
319     Adaptor3d_Curve* GetCurve() const { return myC3d; }
320     double EndParam(int i) const { return i ? myLast : myFirst; }
321     int CoordInd() const { return myCoordInd; }
322     const gp_XYZ& NodeXYZ(int i) const { return i ? myNodes[1] : myNodes[0]; }
323     gp_XYZ Point( const gp_XYZ& theParams ) const; // Return coord by params
324     double GetU( const gp_XYZ& theParams ) const;  // Return U by params
325     TEdge(): myC3d(0) {}
326     ~TEdge();
327   };
328
329   class SMESH_EXPORT TFace {
330     // 4 edges in the order u0, u1, 0v, 1v
331     int                  myCoordInd[ 4 ];
332     double               myFirst   [ 4 ];
333     double               myLast    [ 4 ];
334     Adaptor2d_Curve2d*   myC2d     [ 4 ];
335     // 4 corner points in the order 00, 10, 11, 01
336     gp_XY                myCorner  [ 4 ];
337     // surface
338     Adaptor3d_Surface*   myS;
339     // if mesh volume
340     gp_XYZ               myNodes[4];
341   public:
342     void Set( const int faceID, Adaptor3d_Surface* S, // must be in GetFaceEdgesIDs() order:
343               Adaptor2d_Curve2d* c2d[4], const bool isForward[4] );
344     void Set( const int faceID, const TEdge& edgeU0, const TEdge& edgeU1 );
345     gp_XY  GetUV( const gp_XYZ& theParams ) const;
346     gp_XYZ Point( const gp_XYZ& theParams ) const;
347     int GetUInd() const { return myCoordInd[ 0 ]; }
348     int GetVInd() const { return myCoordInd[ 2 ]; }
349     void GetCoefs( int i, const gp_XYZ& theParams, double& eCoef, double& vCoef ) const;
350     TFace(): myS(0) { myC2d[0]=myC2d[1]=myC2d[2]=myC2d[3]=0; }
351     ~TFace();
352   };
353
354   // geometry in the order as in TShapeID:
355   // 8 vertices
356   gp_XYZ myPnt[ 8 ];
357   // 12 edges
358   TEdge  myEdge[ 12 ];
359   // 6 faces
360   TFace  myFace[ 6 ];
361
362   // for param computation
363
364   enum { SQUARE_DIST = 0, DRV_1, DRV_2, DRV_3 };
365   double distance () const { return sqrt( myValues[ SQUARE_DIST ]); }
366   double funcValue(double sqDist) const { return mySquareFunc ? sqDist : sqrt(sqDist); }
367   bool computeParameters(const gp_Pnt& thePoint, gp_XYZ& theParams, const gp_XYZ& theParamsHint);
368
369   int      myFaceIndex;
370   double   myFaceParam;
371   int      myNbIterations;
372   double   mySumDist;
373   double   myTolerance;
374   bool     mySquareFunc;
375
376   gp_XYZ   myPoint; // the given point
377   gp_XYZ   myParam; // the best parameters guess
378   double   myValues[ 4 ]; // values computed at myParam: square distance and 3 derivatives
379
380   typedef pair<gp_XYZ,gp_XYZ> TxyzPair;
381   TxyzPair my3x3x3GridNodes[ 27 ]; // to compute the first param guess
382   bool     myGridComputed;
383 };
384
385
386 #endif