Salome HOME
PAL7935. In Load(face), load from a forward face
[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   static inline bool IsVertexID( int theShapeID )
72   { return ( theShapeID >= ID_V000 && theShapeID <= ID_V111 ); }
73
74   static inline bool IsEdgeID( int theShapeID )
75   { return ( theShapeID >= ID_Ex00 && theShapeID <= ID_E11z ); }
76
77   static inline bool IsFaceID( int theShapeID )
78   { return ( theShapeID >= ID_Fxy0 && theShapeID <= ID_F1yz ); }
79
80
81   SMESH_Block (): myNbIterations(0), mySumDist(0.) {}
82
83   bool LoadBlockShapes(const TopoDS_Shell&         theShell,
84                        const TopoDS_Vertex&        theVertex000,
85                        const TopoDS_Vertex&        theVertex001,
86 //                       TopTools_IndexedMapOfShape& theShapeIDMap
87                        TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
88   // add sub-shapes of theBlock to theShapeIDMap so that they get
89   // IDs acoording to enum TShapeID
90
91   bool LoadMeshBlock(const SMDS_MeshVolume*        theVolume,
92                      const int                     theNode000Index,
93                      const int                     theNode001Index,
94                      vector<const SMDS_MeshNode*>& theOrderedNodes);
95   // prepare to work with theVolume and
96   // return nodes in the order of TShapeID enum
97
98   static int GetShapeIDByParams ( const gp_XYZ& theParams );
99   // define an id of the block sub-shape by point parameters
100
101   bool VertexPoint( const int theVertexID, gp_XYZ& thePoint ) const {
102     if ( !IsVertexID( theVertexID ))           return false;
103     thePoint = myPnt[ theVertexID - ID_V000 ]; return true;
104   }
105   // return vertex coordinates
106
107   bool EdgePoint( const int theEdgeID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
108     if ( !IsEdgeID( theEdgeID ))                                 return false;
109     thePoint = myEdge[ theEdgeID - ID_Ex00 ].Point( theParams ); return true;
110   }
111   // return coordinates of a point on edge
112
113   bool FacePoint( const int theFaceID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
114     if ( !IsFaceID ( theFaceID ))                                return false;
115     thePoint = myFace[ theFaceID - ID_Fxy0 ].Point( theParams ); return true;
116   }
117   // return coordinates of a point on face
118
119   bool ShellPoint( const gp_XYZ& theParams, gp_XYZ& thePoint ) const;
120   // return coordinates of a point in shell
121
122   bool ComputeParameters (const gp_Pnt& thePoint,
123                           gp_XYZ&       theParams,
124                           const int     theShapeID = ID_Shell);
125   // compute point parameters in the block.
126   // Note: for edges, it is better to use EdgeParameters()
127
128   bool VertexParameters(const int theVertexID, gp_XYZ& theParams);
129   // return parameters of a vertex given by TShapeID
130
131   bool EdgeParameters(const int theEdgeID, const double theU, gp_XYZ& theParams);
132   // return parameters of a point given by theU on edge
133
134   static void GetFaceEdgesIDs (const int faceID, vector< int >& edgeVec );
135   // return edges IDs of a face in the order u0, u1, 0v, 1v
136
137   static void GetEdgeVertexIDs (const int edgeID, vector< int >& vertexVec );
138   // return vertex IDs of an edge
139
140   static int GetCoordIndOnEdge (const int theEdgeID)
141   { return (theEdgeID < ID_E0y0) ? 1 : (theEdgeID < ID_E00z) ? 2 : 3; }
142   // return an index of a coordinate which varies along the edge
143
144   static double* GetShapeCoef (const int theShapeID);
145   // for theShapeID( TShapeID ), returns 3 coefficients used
146   // to compute an addition of an on-theShape point to coordinates
147   // of an in-shell point. If an in-shell point has parameters (Px,Py,Pz),
148   // then the addition of a point P is computed as P*kx*ky*kz and ki is
149   // defined by the returned coef like this:
150   // ki = (coef[i] == 0) ? 1 : (coef[i] < 0) ? 1 - Pi : Pi
151
152   static bool IsForwardEdge (const TopoDS_Edge &         theEdge, 
153                              //TopTools_IndexedMapOfShape& theShapeIDMap
154                              TopTools_IndexedMapOfOrientedShape& theShapeIDMap) {
155     int v1ID = theShapeIDMap.FindIndex( TopExp::FirstVertex( theEdge ).Oriented( TopAbs_FORWARD ));
156     int v2ID = theShapeIDMap.FindIndex( TopExp::LastVertex( theEdge ).Oriented( TopAbs_FORWARD ));
157     return ( v1ID < v2ID );
158   }
159   // Return true if an in-block parameter increases along theEdge curve
160
161   static void Swap(double& a, double& b) { double tmp = a; a = b; b = tmp; }
162
163   // methods of math_FunctionSetWithDerivatives
164   Standard_Integer NbVariables() const;
165   Standard_Integer NbEquations() const;
166   Standard_Boolean Value(const math_Vector& X,math_Vector& F) ;
167   Standard_Boolean Derivatives(const math_Vector& X,math_Matrix& D) ;
168   Standard_Boolean Values(const math_Vector& X,math_Vector& F,math_Matrix& D) ;
169   Standard_Integer GetStateNumber ();
170
171   static ostream& DumpShapeID (const int theBlockShapeID, ostream& stream);
172   // DEBUG: dump an id of a block sub-shape
173
174  private:
175
176   struct TEdge {
177     int                myCoordInd;
178     double             myFirst;
179     double             myLast;
180     Handle(Geom_Curve) myC3d;
181     gp_Trsf            myTrsf;
182     double GetU( const gp_XYZ& theParams ) const;
183     gp_XYZ Point( const gp_XYZ& theParams ) const;
184     // if mesh volume
185     gp_XYZ             myNodes[2];
186   };
187
188   struct TFace {
189     // 4 edges in the order u0, u1, 0v, 1v
190     int                  myCoordInd[ 4 ];
191     double               myFirst   [ 4 ];
192     double               myLast    [ 4 ];
193     Handle(Geom2d_Curve) myC2d     [ 4 ];
194     // 4 corner points in the order 00, 10, 11, 01
195     gp_XY                myCorner  [ 4 ];
196     // surface
197     Handle(Geom_Surface) myS;
198     gp_Trsf              myTrsf;
199     gp_XY  GetUV( const gp_XYZ& theParams ) const;
200     gp_XYZ Point( const gp_XYZ& theParams ) const;
201     int GetUInd() const { return myCoordInd[ 0 ]; }
202     int GetVInd() const { return myCoordInd[ 2 ]; }
203     void GetCoefs( int i, const gp_XYZ& theParams, double& eCoef, double& vCoef ) const;
204     // if mesh volume
205     gp_XYZ               myNodes[4];
206   };
207
208   TopoDS_Shell myShell;
209   // geometry in the order as in TShapeID:
210   // 8 vertices
211   gp_XYZ myPnt[ 8 ];
212   // 12 edges
213   TEdge  myEdge[ 12 ];
214   // 6 faces
215   TFace  myFace[ 6 ];
216
217   // for param computation
218
219   int      myFaceIndex;
220   double   myFaceParam;
221   int      myNbIterations;
222   double   mySumDist;
223
224   gp_XYZ   myPoint; // the given point
225   gp_XYZ   myParam; // the best parameters guess
226   double   myValues[ 4 ]; // values computed at myParam
227
228   typedef pair<gp_XYZ,gp_XYZ> TxyzPair;
229   TxyzPair my3x3x3GridNodes[ 27 ];
230   bool     myGridComputed;
231 };
232
233
234 #endif