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