Salome HOME
Fix PAL8562: rpath (rpath-link) option needs parameter - directory to search shared...
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 <Geom2d_Curve.hxx>
29 #include <Geom_Curve.hxx>
30 #include <Geom_Surface.hxx>
31 #include <TopExp.hxx>
32 #include <TopTools_IndexedMapOfOrientedShape.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS_Shell.hxx>
35 #include <TopoDS_Vertex.hxx>
36 #include <gp_Pnt.hxx>
37 #include <gp_Trsf.hxx>
38 #include <gp_XY.hxx>
39 #include <gp_XYZ.hxx>
40 #include <math_FunctionSetWithDerivatives.hxx>
41 #include <math_Matrix.hxx>
42 #include <math_Vector.hxx>
43
44 #include <ostream>
45 #include <vector>
46
47 class SMDS_MeshVolume;
48 class SMDS_MeshNode;
49
50 // =========================================================
51 // class calculating coordinates of 3D points by normalized
52 // parameters inside the block and vice versa
53 // =========================================================
54
55 class SMESH_Block: public math_FunctionSetWithDerivatives
56 {
57  public:
58   enum TShapeID { // ids of the block sub-shapes
59     ID_NONE = 0,
60
61     ID_V000 = 1, ID_V100, ID_V010, ID_V110, ID_V001, ID_V101, ID_V011, ID_V111,
62
63     ID_Ex00, ID_Ex10, ID_Ex01, ID_Ex11,
64     ID_E0y0, ID_E1y0, ID_E0y1, ID_E1y1,
65     ID_E00z, ID_E10z, ID_E01z, ID_E11z,
66
67     ID_Fxy0, ID_Fxy1, ID_Fx0z, ID_Fx1z, ID_F0yz, ID_F1yz,
68
69     ID_Shell
70     };
71
72
73  public: // methods about ids of the block sub-shapes
74
75   static int NbVertices()  { return  8; }
76   static int NbEdges()     { return 12; }
77   static int NbFaces()     { return  6; }
78   static int NbSubShapes() { return ID_Shell; }
79   // to avoid magic numbers when allocating memory for subshapes
80
81   static inline bool IsVertexID( int theShapeID )
82   { return ( theShapeID >= ID_V000 && theShapeID <= ID_V111 ); }
83
84   static inline bool IsEdgeID( int theShapeID )
85   { return ( theShapeID >= ID_Ex00 && theShapeID <= ID_E11z ); }
86
87   static inline bool IsFaceID( int theShapeID )
88   { return ( theShapeID >= ID_Fxy0 && theShapeID <= ID_F1yz ); }
89
90   static int ShapeIndex( int theShapeID )
91   {
92     if ( IsVertexID( theShapeID )) return theShapeID - ID_V000;
93     if ( IsEdgeID( theShapeID ))   return theShapeID - ID_Ex00;
94     if ( IsFaceID( theShapeID ))   return theShapeID - ID_Fxy0;
95     return 0;
96   }
97   // return index [0-...] for each type of sub-shapes,
98   // for example :
99   // ShapeIndex( ID_Ex00 ) == 0
100   // ShapeIndex( ID_Ex10 ) == 1
101
102   static void GetFaceEdgesIDs (const int faceID, vector< int >& edgeVec );
103   // return edges IDs of a face in the order u0, u1, 0v, 1v
104
105   static void GetEdgeVertexIDs (const int edgeID, vector< int >& vertexVec );
106   // return vertex IDs of an edge
107
108   static int GetCoordIndOnEdge (const int theEdgeID)
109   { return (theEdgeID < ID_E0y0) ? 1 : (theEdgeID < ID_E00z) ? 2 : 3; }
110   // return an index of a coordinate which varies along the edge
111
112   static double* GetShapeCoef (const int theShapeID);
113   // for theShapeID( TShapeID ), returns 3 coefficients used
114   // to compute an addition of an on-theShape point to coordinates
115   // of an in-shell point. If an in-shell point has parameters (Px,Py,Pz),
116   // then the addition of a point P is computed as P*kx*ky*kz and ki is
117   // defined by the returned coef like this:
118   // ki = (coef[i] == 0) ? 1 : (coef[i] < 0) ? 1 - Pi : Pi
119
120   static int GetShapeIDByParams ( const gp_XYZ& theParams );
121   // define an id of the block sub-shape by point parameters
122
123   static ostream& DumpShapeID (const int theBlockShapeID, ostream& stream);
124   // DEBUG: dump an id of a block sub-shape
125
126
127  public: // initialization
128
129   SMESH_Block (): myNbIterations(0), mySumDist(0.) {}
130
131   bool LoadBlockShapes(const TopoDS_Shell&         theShell,
132                        const TopoDS_Vertex&        theVertex000,
133                        const TopoDS_Vertex&        theVertex001,
134                        TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
135   // add sub-shapes of theBlock to theShapeIDMap so that they get
136   // IDs acoording to enum TShapeID
137
138   bool LoadMeshBlock(const SMDS_MeshVolume*        theVolume,
139                      const int                     theNode000Index,
140                      const int                     theNode001Index,
141                      vector<const SMDS_MeshNode*>& theOrderedNodes);
142   // prepare to work with theVolume and
143   // return nodes in the order of TShapeID enum
144
145
146  public: // define coordinates by parameters
147
148   bool VertexPoint( const int theVertexID, gp_XYZ& thePoint ) const {
149     if ( !IsVertexID( theVertexID ))           return false;
150     thePoint = myPnt[ theVertexID - ID_V000 ]; return true;
151   }
152   // return vertex coordinates, parameters are defined by theVertexID
153
154   bool EdgePoint( const int theEdgeID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
155     if ( !IsEdgeID( theEdgeID ))                                 return false;
156     thePoint = myEdge[ theEdgeID - ID_Ex00 ].Point( theParams ); return true;
157   }
158   // return coordinates of a point on edge
159
160   bool FacePoint( const int theFaceID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
161     if ( !IsFaceID ( theFaceID ))                                return false;
162     thePoint = myFace[ theFaceID - ID_Fxy0 ].Point( theParams ); return true;
163   }
164   // return coordinates of a point on face
165
166   bool ShellPoint( const gp_XYZ& theParams, gp_XYZ& thePoint ) const;
167   // return coordinates of a point in shell
168
169   static bool ShellPoint(const gp_XYZ&         theParams,
170                          const vector<gp_XYZ>& thePointOnShape,
171                          gp_XYZ&               thePoint );
172   // computes coordinates of a point in shell by points on sub-shapes;
173   // thePointOnShape[ subShapeID ] must be a point on a subShape;
174   // thePointOnShape.size() == ID_Shell, thePointOnShape[0] not used
175
176
177  public: // define parameters by coordinates
178
179   bool ComputeParameters (const gp_Pnt& thePoint,
180                           gp_XYZ&       theParams,
181                           const int     theShapeID = ID_Shell);
182   // compute point parameters in the block.
183   // Note: for edges, it is better to use EdgeParameters()
184
185   bool VertexParameters(const int theVertexID, gp_XYZ& theParams);
186   // return parameters of a vertex given by TShapeID
187
188   bool EdgeParameters(const int theEdgeID, const double theU, gp_XYZ& theParams);
189   // return parameters of a point given by theU on edge
190
191
192  public: // services
193
194   static bool IsForwardEdge (const TopoDS_Edge &                 theEdge,
195                              TopTools_IndexedMapOfOrientedShape& theShapeIDMap) {
196     int v1ID = theShapeIDMap.FindIndex( TopExp::FirstVertex( theEdge ).Oriented( TopAbs_FORWARD ));
197     int v2ID = theShapeIDMap.FindIndex( TopExp::LastVertex( theEdge ).Oriented( TopAbs_FORWARD ));
198     return ( v1ID < v2ID );
199   }
200   // Return true if an in-block parameter increases along theEdge curve
201
202   static void Swap(double& a, double& b) { double tmp = a; a = b; b = tmp; }
203
204
205  public:
206   // methods of math_FunctionSetWithDerivatives used internally
207   // to define parameters by coordinates
208   Standard_Integer NbVariables() const;
209   Standard_Integer NbEquations() const;
210   Standard_Boolean Value(const math_Vector& X,math_Vector& F) ;
211   Standard_Boolean Derivatives(const math_Vector& X,math_Matrix& D) ;
212   Standard_Boolean Values(const math_Vector& X,math_Vector& F,math_Matrix& D) ;
213   Standard_Integer GetStateNumber ();
214
215  private:
216
217   struct TEdge {
218     int                myCoordInd;
219     double             myFirst;
220     double             myLast;
221     Handle(Geom_Curve) myC3d;
222     gp_Trsf            myTrsf;
223     double GetU( const gp_XYZ& theParams ) const;
224     gp_XYZ Point( const gp_XYZ& theParams ) const;
225     // if mesh volume
226     gp_XYZ             myNodes[2];
227   };
228
229   struct TFace {
230     // 4 edges in the order u0, u1, 0v, 1v
231     int                  myCoordInd[ 4 ];
232     double               myFirst   [ 4 ];
233     double               myLast    [ 4 ];
234     Handle(Geom2d_Curve) myC2d     [ 4 ];
235     // 4 corner points in the order 00, 10, 11, 01
236     gp_XY                myCorner  [ 4 ];
237     // surface
238     Handle(Geom_Surface) myS;
239     gp_Trsf              myTrsf;
240     gp_XY  GetUV( const gp_XYZ& theParams ) const;
241     gp_XYZ Point( const gp_XYZ& theParams ) const;
242     int GetUInd() const { return myCoordInd[ 0 ]; }
243     int GetVInd() const { return myCoordInd[ 2 ]; }
244     void GetCoefs( int i, const gp_XYZ& theParams, double& eCoef, double& vCoef ) const;
245     // if mesh volume
246     gp_XYZ               myNodes[4];
247   };
248
249   TopoDS_Shell myShell;
250   // geometry in the order as in TShapeID:
251   // 8 vertices
252   gp_XYZ myPnt[ 8 ];
253   // 12 edges
254   TEdge  myEdge[ 12 ];
255   // 6 faces
256   TFace  myFace[ 6 ];
257
258   // for param computation
259
260   int      myFaceIndex;
261   double   myFaceParam;
262   int      myNbIterations;
263   double   mySumDist;
264
265   gp_XYZ   myPoint; // the given point
266   gp_XYZ   myParam; // the best parameters guess
267   double   myValues[ 4 ]; // values computed at myParam
268
269   typedef pair<gp_XYZ,gp_XYZ> TxyzPair;
270   TxyzPair my3x3x3GridNodes[ 27 ];
271   bool     myGridComputed;
272 };
273
274
275 #endif