Salome HOME
Merge from V5_1_main 14/05/2010
[modules/smesh.git] / src / SMESH / SMESH_Block.hxx
index 7ab38891a2ed1b53c143ea26ec5243c7728553bc..f3d9510691666072b6cce9cb622a0e7cad40651b 100644 (file)
@@ -1,61 +1,71 @@
-//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
-// 
-//  This library is free software; you can redistribute it and/or 
-//  modify it under the terms of the GNU Lesser General Public 
-//  License as published by the Free Software Foundation; either 
-//  version 2.1 of the License. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  License along with this library; if not, write to the Free Software 
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-// 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+//  This library is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU Lesser General Public
+//  License as published by the Free Software Foundation; either
+//  version 2.1 of the License.
+//
+//  This library is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+//  Lesser General Public License for more details.
+//
+//  You should have received a copy of the GNU Lesser General Public
+//  License along with this library; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 // File      : SMESH_Block.hxx
 // Created   : Tue Nov 30 12:42:18 2004
 // Author    : Edward AGAPOV (eap)
-
-
+//
 #ifndef SMESH_Block_HeaderFile
 #define SMESH_Block_HeaderFile
 
-#include <Geom2d_Curve.hxx>
-#include <Geom_Curve.hxx>
-#include <Geom_Surface.hxx>
+#include "SMESH_SMESH.hxx"
+
+//#include <Geom2d_Curve.hxx>
+//#include <Geom_Curve.hxx>
+//#include <Geom_Surface.hxx>
+
 #include <TopExp.hxx>
 #include <TopTools_IndexedMapOfOrientedShape.hxx>
 #include <TopoDS_Edge.hxx>
+#include <TopoDS_Face.hxx>
 #include <TopoDS_Shell.hxx>
 #include <TopoDS_Vertex.hxx>
-#include <gp_Pnt.hxx>
-#include <gp_Trsf.hxx>
 #include <gp_XY.hxx>
 #include <gp_XYZ.hxx>
 #include <math_FunctionSetWithDerivatives.hxx>
-#include <math_Matrix.hxx>
-#include <math_Vector.hxx>
 
 #include <ostream>
 #include <vector>
+#include <list>
 
 class SMDS_MeshVolume;
 class SMDS_MeshNode;
+class Adaptor3d_Surface;
+class Adaptor2d_Curve2d;
+class Adaptor3d_Curve;
+class gp_Pnt;
 
 // =========================================================
 // class calculating coordinates of 3D points by normalized
 // parameters inside the block and vice versa
 // =========================================================
 
-class SMESH_Block: public math_FunctionSetWithDerivatives
+class SMESH_EXPORT SMESH_Block: public math_FunctionSetWithDerivatives
 {
  public:
-  enum TShapeID { // ids of the block sub-shapes
+  enum TShapeID {
+    // ----------------------------
+    // Ids of the block sub-shapes
+    // ----------------------------
     ID_NONE = 0,
 
     ID_V000 = 1, ID_V100, ID_V010, ID_V110, ID_V001, ID_V101, ID_V011, ID_V111,
@@ -68,6 +78,24 @@ class SMESH_Block: public math_FunctionSetWithDerivatives
 
     ID_Shell
     };
+  enum { // to use TShapeID for indexing certain type subshapes
+
+    ID_FirstV = ID_V000, ID_FirstE = ID_Ex00, ID_FirstF = ID_Fxy0
+
+  };
+
+
+ public:
+  // -------------------------------------------------
+  // Block topology in terms of block sub-shapes' ids
+  // -------------------------------------------------
+
+  static int NbVertices()  { return  8; }
+  static int NbEdges()     { return 12; }
+  static int NbFaces()     { return  6; }
+  static int NbSubShapes() { return ID_Shell; }
+  // to avoid magic numbers when allocating memory for subshapes
+
   static inline bool IsVertexID( int theShapeID )
   { return ( theShapeID >= ID_V000 && theShapeID <= ID_V111 ); }
 
@@ -77,83 +105,192 @@ class SMESH_Block: public math_FunctionSetWithDerivatives
   static inline bool IsFaceID( int theShapeID )
   { return ( theShapeID >= ID_Fxy0 && theShapeID <= ID_F1yz ); }
 
+  static int ShapeIndex( int theShapeID )
+  {
+    if ( IsVertexID( theShapeID )) return theShapeID - ID_V000;
+    if ( IsEdgeID( theShapeID ))   return theShapeID - ID_Ex00;
+    if ( IsFaceID( theShapeID ))   return theShapeID - ID_Fxy0;
+    return 0;
+  }
+  // return index [0-...] for each type of sub-shapes,
+  // for example :
+  // ShapeIndex( ID_Ex00 ) == 0
+  // ShapeIndex( ID_Ex10 ) == 1
+
+  static void GetFaceEdgesIDs (const int faceID, std::vector< int >& edgeVec );
+  // return edges IDs of a face in the order u0, u1, 0v, 1v
+
+  static void GetEdgeVertexIDs (const int edgeID, std::vector< int >& vertexVec );
+  // return vertex IDs of an edge
+
+  static int GetCoordIndOnEdge (const int theEdgeID)
+  { return (theEdgeID < ID_E0y0) ? 1 : (theEdgeID < ID_E00z) ? 2 : 3; }
+  // return an index of a coordinate which varies along the edge
+
+  static double* GetShapeCoef (const int theShapeID);
+  // for theShapeID( TShapeID ), returns 3 coefficients used
+  // to compute an addition of an on-theShape point to coordinates
+  // of an in-shell point. If an in-shell point has parameters (Px,Py,Pz),
+  // then the addition of a point P is computed as P*kx*ky*kz and ki is
+  // defined by the returned coef like this:
+  // ki = (coef[i] == 0) ? 1 : (coef[i] < 0) ? 1 - Pi : Pi
+
+  static int GetShapeIDByParams ( const gp_XYZ& theParams );
+  // define an id of the block sub-shape by point parameters
+
+  static std::ostream& DumpShapeID (const int theBlockShapeID, std::ostream& stream);
+  // DEBUG: dump an id of a block sub-shape
 
-  SMESH_Block (): myNbIterations(0), mySumDist(0.) {}
+
+ public:
+  // ---------------
+  // Initialization
+  // ---------------
+
+  SMESH_Block();
 
   bool LoadBlockShapes(const TopoDS_Shell&         theShell,
                        const TopoDS_Vertex&        theVertex000,
                        const TopoDS_Vertex&        theVertex001,
-//                       TopTools_IndexedMapOfShape& theShapeIDMap
                        TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
+  // Initialize block geometry with theShell,
   // add sub-shapes of theBlock to theShapeIDMap so that they get
   // IDs acoording to enum TShapeID
 
+  bool LoadBlockShapes(const TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
+  // Initialize block geometry with shapes from theShapeIDMap
+
   bool LoadMeshBlock(const SMDS_MeshVolume*        theVolume,
                      const int                     theNode000Index,
                      const int                     theNode001Index,
-                     vector<const SMDS_MeshNode*>& theOrderedNodes);
+                     std::vector<const SMDS_MeshNode*>& theOrderedNodes);
   // prepare to work with theVolume and
-  // return nodes in the order of TShapeID enum
+  // return nodes in theVolume corners in the order of TShapeID enum
+
+  bool LoadFace(const TopoDS_Face& theFace,
+                const int          theFaceID,
+                const TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
+  // Load face geometry.
+  // It is enough to compute params or coordinates on the face.
+  // Face subshapes must be loaded into theShapeIDMap before
+
+  static bool Insert(const TopoDS_Shape& theShape,
+                     const int           theShapeID,
+                     TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
+  // Insert theShape into theShapeIDMap with theShapeID,
+  // Not yet set shapes preceding theShapeID are filled with compounds
+  // Return true if theShape was successfully bound to theShapeID
+
+  static bool FindBlockShapes(const TopoDS_Shell&         theShell,
+                              const TopoDS_Vertex&        theVertex000,
+                              const TopoDS_Vertex&        theVertex001,
+                              TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
+  // add sub-shapes of theBlock to theShapeIDMap so that they get
+  // IDs acoording to enum TShapeID
 
-  static int GetShapeIDByParams ( const gp_XYZ& theParams );
-  // define an id of the block sub-shape by point parameters
+public:
+  // ---------------------------------
+  // Define coordinates by parameters
+  // ---------------------------------
 
   bool VertexPoint( const int theVertexID, gp_XYZ& thePoint ) const {
     if ( !IsVertexID( theVertexID ))           return false;
-    thePoint = myPnt[ theVertexID - ID_V000 ]; return true;
+    thePoint = myPnt[ theVertexID - ID_FirstV ]; return true;
   }
-  // return vertex coordinates
+  // return vertex coordinates, parameters are defined by theVertexID
 
   bool EdgePoint( const int theEdgeID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
     if ( !IsEdgeID( theEdgeID ))                                 return false;
-    thePoint = myEdge[ theEdgeID - ID_Ex00 ].Point( theParams ); return true;
+    thePoint = myEdge[ theEdgeID - ID_FirstE ].Point( theParams ); return true;
   }
   // return coordinates of a point on edge
 
+  bool EdgeU( const int theEdgeID, const gp_XYZ& theParams, double& theU ) const {
+    if ( !IsEdgeID( theEdgeID ))                              return false;
+    theU = myEdge[ theEdgeID - ID_FirstE ].GetU( theParams ); return true;
+  }
+  // return parameter on edge by in-block parameters
+
   bool FacePoint( const int theFaceID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
     if ( !IsFaceID ( theFaceID ))                                return false;
-    thePoint = myFace[ theFaceID - ID_Fxy0 ].Point( theParams ); return true;
+    thePoint = myFace[ theFaceID - ID_FirstF ].Point( theParams ); return true;
   }
   // return coordinates of a point on face
 
+  bool FaceUV( const int theFaceID, const gp_XYZ& theParams, gp_XY& theUV ) const {
+    if ( !IsFaceID ( theFaceID ))                               return false;
+    theUV = myFace[ theFaceID - ID_FirstF ].GetUV( theParams ); return true;
+  }
+  // return UV coordinates on a face by in-block parameters
+
   bool ShellPoint( const gp_XYZ& theParams, gp_XYZ& thePoint ) const;
   // return coordinates of a point in shell
 
+  static bool ShellPoint(const gp_XYZ&         theParams,
+                         const std::vector<gp_XYZ>& thePointOnShape,
+                         gp_XYZ&               thePoint );
+  // computes coordinates of a point in shell by points on sub-shapes
+  // and point parameters.
+  // thePointOnShape[ subShapeID ] must be a point on a subShape;
+  // thePointOnShape.size() == ID_Shell, thePointOnShape[0] not used
+
+
+ public:
+  // ---------------------------------
+  // Define parameters by coordinates
+  // ---------------------------------
+
   bool ComputeParameters (const gp_Pnt& thePoint,
                           gp_XYZ&       theParams,
-                          const int     theShapeID = ID_Shell);
-  // compute point parameters in the block
+                          const int     theShapeID    = ID_Shell,
+                          const gp_XYZ& theParamsHint = gp_XYZ(-1,-1,-1));
+  // compute point parameters in the block.
+  // Note: for edges, it is better to use EdgeParameters()
 
-  static void GetFaceEdgesIDs (const int faceID, vector< int >& edgeVec );
-  // return edges IDs of a face in the order u0, u1, 0v, 1v
+  bool VertexParameters(const int theVertexID, gp_XYZ& theParams);
+  // return parameters of a vertex given by TShapeID
 
-  static void GetEdgeVertexIDs (const int edgeID, vector< int >& vertexVec );
-  // return vertex IDs of an edge
+  bool EdgeParameters(const int theEdgeID, const double theU, gp_XYZ& theParams);
+  // return parameters of a point given by theU on edge
 
-  static int GetCoordIndOnEdge (const int theEdgeID)
-  { return (theEdgeID < ID_E0y0) ? 1 : (theEdgeID < ID_E00z) ? 2 : 3; }
-  // return an index of a coordinate which varies along the edge
 
-  static double* GetShapeCoef (const int theShapeID);
-  // for theShapeID( TShapeID ), returns 3 coefficients used
-  // to compute an addition of an on-theShape point to coordinates
-  // of an in-shell point. If an in-shell point has parameters (Px,Py,Pz),
-  // then the addition of a point P is computed as P*kx*ky*kz and ki is
-  // defined by the returned coef like this:
-  // ki = (coef[i] == 0) ? 1 : (coef[i] < 0) ? 1 - Pi : Pi
+ public:
+  // ---------------
+  // Block geomerty
+  // ---------------
+
+  
+  
+ public:
+  // ---------
+  // Services
+  // ---------
 
-  static bool IsForwardEdge (const TopoDS_Edge &         theEdge, 
-                             //TopTools_IndexedMapOfShape& theShapeIDMap
-                             TopTools_IndexedMapOfOrientedShape& theShapeIDMap) {
+  static bool IsForwardEdge (const TopoDS_Edge &                       theEdge,
+                             const TopTools_IndexedMapOfOrientedShape& theShapeIDMap) {
     int v1ID = theShapeIDMap.FindIndex( TopExp::FirstVertex( theEdge ).Oriented( TopAbs_FORWARD ));
     int v2ID = theShapeIDMap.FindIndex( TopExp::LastVertex( theEdge ).Oriented( TopAbs_FORWARD ));
     return ( v1ID < v2ID );
   }
   // Return true if an in-block parameter increases along theEdge curve
 
-  static void Swap(double& a, double& b) { double tmp = a; a = b; b = tmp; }
+  static int GetOrderedEdges (const TopoDS_Face&        theFace,
+                              TopoDS_Vertex             theFirstVertex,
+                              std::list< TopoDS_Edge >& theEdges,
+                              std::list< int >  &       theNbVertexInWires,
+                              const bool                theShapeAnalysisAlgo=false);
+  // Return nb wires and a list of oredered edges.
+  // It is used to assign indices to subshapes.
+  // theFirstVertex may be NULL.
+  // Always try to set a seam edge first
+  // if (theShapeAnalysisAlgo) then ShapeAnalysis::OuterWire() is used to find the outer
+  // wire else BRepTools::OuterWire() is used
 
-  // methods of math_FunctionSetWithDerivatives
+ public:
+  // -----------------------------------------------------------
+  // Methods of math_FunctionSetWithDerivatives used internally
+  // to define parameters by coordinates
+  // -----------------------------------------------------------
   Standard_Integer NbVariables() const;
   Standard_Integer NbEquations() const;
   Standard_Boolean Value(const math_Vector& X,math_Vector& F) ;
@@ -161,44 +298,64 @@ class SMESH_Block: public math_FunctionSetWithDerivatives
   Standard_Boolean Values(const math_Vector& X,math_Vector& F,math_Matrix& D) ;
   Standard_Integer GetStateNumber ();
 
-  static ostream& DumpShapeID (const int theBlockShapeID, ostream& stream);
-  // DEBUG: dump an id of a block sub-shape
+ protected:
+
+  /*!
+   * \brief Call it after geometry initialisation
+   */
+  void init();
+
+  // Note: to compute params of a point on a face, it is enough to set
+  // TFace, TEdge's and points for that face only
 
- private:
+  // Note 2: curve adaptors need to have only Value(double), FirstParameter() and
+  // LastParameter() defined to be used by Block algoritms
 
-  struct TEdge {
+  class SMESH_EXPORT TEdge {
     int                myCoordInd;
     double             myFirst;
     double             myLast;
-    Handle(Geom_Curve) myC3d;
-    gp_Trsf            myTrsf;
-    double GetU( const gp_XYZ& theParams ) const;
-    gp_XYZ Point( const gp_XYZ& theParams ) const;
+    Adaptor3d_Curve*   myC3d;
     // if mesh volume
     gp_XYZ             myNodes[2];
+  public:
+    void Set( const int edgeID, Adaptor3d_Curve* curve, const bool isForward );
+    void Set( const int edgeID, const gp_XYZ& node1, const gp_XYZ& node2 );
+    Adaptor3d_Curve* GetCurve() const { return myC3d; }
+    double EndParam(int i) const { return i ? myLast : myFirst; }
+    int CoordInd() const { return myCoordInd; }
+    const gp_XYZ& NodeXYZ(int i) const { return i ? myNodes[1] : myNodes[0]; }
+    gp_XYZ Point( const gp_XYZ& theParams ) const; // Return coord by params
+    double GetU( const gp_XYZ& theParams ) const;  // Return U by params
+    TEdge(): myC3d(0) {}
+    ~TEdge();
   };
 
-  struct TFace {
+  class SMESH_EXPORT TFace {
     // 4 edges in the order u0, u1, 0v, 1v
     int                  myCoordInd[ 4 ];
     double               myFirst   [ 4 ];
     double               myLast    [ 4 ];
-    Handle(Geom2d_Curve) myC2d     [ 4 ];
+    Adaptor2d_Curve2d*   myC2d     [ 4 ];
     // 4 corner points in the order 00, 10, 11, 01
     gp_XY                myCorner  [ 4 ];
     // surface
-    Handle(Geom_Surface) myS;
-    gp_Trsf              myTrsf;
+    Adaptor3d_Surface*   myS;
+    // if mesh volume
+    gp_XYZ               myNodes[4];
+  public:
+    void Set( const int faceID, Adaptor3d_Surface* S, // must be in GetFaceEdgesIDs() order:
+              Adaptor2d_Curve2d* c2d[4], const bool isForward[4] );
+    void Set( const int faceID, const TEdge& edgeU0, const TEdge& edgeU1 );
     gp_XY  GetUV( const gp_XYZ& theParams ) const;
     gp_XYZ Point( const gp_XYZ& theParams ) const;
     int GetUInd() const { return myCoordInd[ 0 ]; }
     int GetVInd() const { return myCoordInd[ 2 ]; }
     void GetCoefs( int i, const gp_XYZ& theParams, double& eCoef, double& vCoef ) const;
-    // if mesh volume
-    gp_XYZ               myNodes[4];
+    TFace(): myS(0) { myC2d[0]=myC2d[1]=myC2d[2]=myC2d[3]=0; }
+    ~TFace();
   };
 
-  TopoDS_Shell myShell;
   // geometry in the order as in TShapeID:
   // 8 vertices
   gp_XYZ myPnt[ 8 ];
@@ -209,17 +366,24 @@ class SMESH_Block: public math_FunctionSetWithDerivatives
 
   // for param computation
 
+  enum { SQUARE_DIST = 0, DRV_1, DRV_2, DRV_3 };
+  double distance () const { return sqrt( myValues[ SQUARE_DIST ]); }
+  double funcValue(double sqDist) const { return mySquareFunc ? sqDist : sqrt(sqDist); }
+  bool computeParameters(const gp_Pnt& thePoint, gp_XYZ& theParams, const gp_XYZ& theParamsHint);
+
   int      myFaceIndex;
   double   myFaceParam;
   int      myNbIterations;
   double   mySumDist;
+  double   myTolerance;
+  bool     mySquareFunc;
 
   gp_XYZ   myPoint; // the given point
   gp_XYZ   myParam; // the best parameters guess
-  double   myValues[ 4 ]; // values computed at myParam
+  double   myValues[ 4 ]; // values computed at myParam: square distance and 3 derivatives
 
-  typedef pair<gp_XYZ,gp_XYZ> TxyzPair;
-  TxyzPair my3x3x3GridNodes[ 27 ];
+  typedef std::pair<gp_XYZ,gp_XYZ> TxyzPair;
+  TxyzPair my3x3x3GridNodes[ 27 ]; // to compute the first param guess
   bool     myGridComputed;
 };