Back to index

salome-smesh  6.5.0
SMESH_Block.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File      : SMESH_Block.hxx
00024 // Created   : Tue Nov 30 12:42:18 2004
00025 // Author    : Edward AGAPOV (eap)
00026 //
00027 #ifndef SMESH_Block_HeaderFile
00028 #define SMESH_Block_HeaderFile
00029 
00030 #include "SMESH_Utils.hxx"
00031 
00032 //#include <Geom2d_Curve.hxx>
00033 //#include <Geom_Curve.hxx>
00034 //#include <Geom_Surface.hxx>
00035 
00036 #include <TopExp.hxx>
00037 #include <TopTools_IndexedMapOfOrientedShape.hxx>
00038 #include <TopoDS_Edge.hxx>
00039 #include <TopoDS_Face.hxx>
00040 #include <TopoDS_Shell.hxx>
00041 #include <TopoDS_Vertex.hxx>
00042 #include <gp_XY.hxx>
00043 #include <gp_XYZ.hxx>
00044 #include <math_FunctionSetWithDerivatives.hxx>
00045 
00046 #include <ostream>
00047 #include <vector>
00048 #include <list>
00049 
00050 class SMDS_MeshVolume;
00051 class SMDS_MeshNode;
00052 class Adaptor3d_Surface;
00053 class Adaptor2d_Curve2d;
00054 class Adaptor3d_Curve;
00055 class gp_Pnt;
00056 
00057 // =========================================================
00058 // class calculating coordinates of 3D points by normalized
00059 // parameters inside the block and vice versa
00060 // =========================================================
00061 
00062 class SMESHUtils_EXPORT SMESH_Block: public math_FunctionSetWithDerivatives
00063 {
00064  public:
00065   enum TShapeID {
00066     // ----------------------------
00067     // Ids of the block sub-shapes
00068     // ----------------------------
00069     ID_NONE = 0,
00070 
00071     ID_V000 = 1, ID_V100, ID_V010, ID_V110, ID_V001, ID_V101, ID_V011, ID_V111,
00072 
00073     ID_Ex00, ID_Ex10, ID_Ex01, ID_Ex11,
00074     ID_E0y0, ID_E1y0, ID_E0y1, ID_E1y1,
00075     ID_E00z, ID_E10z, ID_E01z, ID_E11z,
00076 
00077     ID_Fxy0, ID_Fxy1, ID_Fx0z, ID_Fx1z, ID_F0yz, ID_F1yz,
00078 
00079     ID_Shell
00080     };
00081   enum { // to use TShapeID for indexing certain type subshapes
00082 
00083     ID_FirstV = ID_V000, ID_FirstE = ID_Ex00, ID_FirstF = ID_Fxy0
00084 
00085   };
00086 
00087 
00088  public:
00089   // -------------------------------------------------
00090   // Block topology in terms of block sub-shapes' ids
00091   // -------------------------------------------------
00092 
00093   static int NbVertices()  { return  8; }
00094   static int NbEdges()     { return 12; }
00095   static int NbFaces()     { return  6; }
00096   static int NbSubShapes() { return ID_Shell; }
00097   // to avoid magic numbers when allocating memory for subshapes
00098 
00099   static inline bool IsVertexID( int theShapeID )
00100   { return ( theShapeID >= ID_V000 && theShapeID <= ID_V111 ); }
00101 
00102   static inline bool IsEdgeID( int theShapeID )
00103   { return ( theShapeID >= ID_Ex00 && theShapeID <= ID_E11z ); }
00104 
00105   static inline bool IsFaceID( int theShapeID )
00106   { return ( theShapeID >= ID_Fxy0 && theShapeID <= ID_F1yz ); }
00107 
00108   static int ShapeIndex( int theShapeID )
00109   {
00110     if ( IsVertexID( theShapeID )) return theShapeID - ID_V000;
00111     if ( IsEdgeID( theShapeID ))   return theShapeID - ID_Ex00;
00112     if ( IsFaceID( theShapeID ))   return theShapeID - ID_Fxy0;
00113     return 0;
00114   }
00115   // return index [0-...] for each type of sub-shapes,
00116   // for example :
00117   // ShapeIndex( ID_Ex00 ) == 0
00118   // ShapeIndex( ID_Ex10 ) == 1
00119 
00120   static void GetFaceEdgesIDs (const int faceID, std::vector< int >& edgeVec );
00121   // return edges IDs of a face in the order u0, u1, 0v, 1v
00122 
00123   static void GetEdgeVertexIDs (const int edgeID, std::vector< int >& vertexVec );
00124   // return vertex IDs of an edge
00125 
00126   static int GetCoordIndOnEdge (const int theEdgeID)
00127   { return (theEdgeID < ID_E0y0) ? 1 : (theEdgeID < ID_E00z) ? 2 : 3; }
00128   // return an index of a coordinate which varies along the edge
00129 
00130   static double* GetShapeCoef (const int theShapeID);
00131   // for theShapeID( TShapeID ), returns 3 coefficients used
00132   // to compute an addition of an on-theShape point to coordinates
00133   // of an in-shell point. If an in-shell point has parameters (Px,Py,Pz),
00134   // then the addition of a point P is computed as P*kx*ky*kz and ki is
00135   // defined by the returned coef like this:
00136   // ki = (coef[i] == 0) ? 1 : (coef[i] < 0) ? 1 - Pi : Pi
00137 
00138   static int GetShapeIDByParams ( const gp_XYZ& theParams );
00139   // define an id of the block sub-shape by point parameters
00140 
00141   static std::ostream& DumpShapeID (const int theBlockShapeID, std::ostream& stream);
00142   // DEBUG: dump an id of a block sub-shape
00143 
00144 
00145  public:
00146   // ---------------
00147   // Initialization
00148   // ---------------
00149 
00150   SMESH_Block();
00151 
00152   bool LoadBlockShapes(const TopoDS_Shell&         theShell,
00153                        const TopoDS_Vertex&        theVertex000,
00154                        const TopoDS_Vertex&        theVertex001,
00155                        TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
00156   // Initialize block geometry with theShell,
00157   // add sub-shapes of theBlock to theShapeIDMap so that they get
00158   // IDs acoording to enum TShapeID
00159 
00160   bool LoadBlockShapes(const TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
00161   // Initialize block geometry with shapes from theShapeIDMap
00162 
00163   bool LoadMeshBlock(const SMDS_MeshVolume*        theVolume,
00164                      const int                     theNode000Index,
00165                      const int                     theNode001Index,
00166                      std::vector<const SMDS_MeshNode*>& theOrderedNodes);
00167   // prepare to work with theVolume and
00168   // return nodes in theVolume corners in the order of TShapeID enum
00169 
00170   bool LoadFace(const TopoDS_Face& theFace,
00171                 const int          theFaceID,
00172                 const TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
00173   // Load face geometry.
00174   // It is enough to compute params or coordinates on the face.
00175   // Face subshapes must be loaded into theShapeIDMap before
00176 
00177   static bool Insert(const TopoDS_Shape& theShape,
00178                      const int           theShapeID,
00179                      TopTools_IndexedMapOfOrientedShape& theShapeIDMap);
00180   // Insert theShape into theShapeIDMap with theShapeID,
00181   // Not yet set shapes preceding theShapeID are filled with compounds
00182   // Return true if theShape was successfully bound to theShapeID
00183 
00184   static bool FindBlockShapes(const TopoDS_Shell&         theShell,
00185                               const TopoDS_Vertex&        theVertex000,
00186                               const TopoDS_Vertex&        theVertex001,
00187                               TopTools_IndexedMapOfOrientedShape& theShapeIDMap );
00188   // add sub-shapes of theBlock to theShapeIDMap so that they get
00189   // IDs acoording to enum TShapeID
00190 
00191 public:
00192   // ---------------------------------
00193   // Define coordinates by parameters
00194   // ---------------------------------
00195 
00196   bool VertexPoint( const int theVertexID, gp_XYZ& thePoint ) const {
00197     if ( !IsVertexID( theVertexID ))           return false;
00198     thePoint = myPnt[ theVertexID - ID_FirstV ]; return true;
00199   }
00200   // return vertex coordinates, parameters are defined by theVertexID
00201 
00202   bool EdgePoint( const int theEdgeID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
00203     if ( !IsEdgeID( theEdgeID ))                                 return false;
00204     thePoint = myEdge[ theEdgeID - ID_FirstE ].Point( theParams ); return true;
00205   }
00206   // return coordinates of a point on edge
00207 
00208   bool EdgeU( const int theEdgeID, const gp_XYZ& theParams, double& theU ) const {
00209     if ( !IsEdgeID( theEdgeID ))                              return false;
00210     theU = myEdge[ theEdgeID - ID_FirstE ].GetU( theParams ); return true;
00211   }
00212   // return parameter on edge by in-block parameters
00213 
00214   bool FacePoint( const int theFaceID, const gp_XYZ& theParams, gp_XYZ& thePoint ) const {
00215     if ( !IsFaceID ( theFaceID ))                                return false;
00216     thePoint = myFace[ theFaceID - ID_FirstF ].Point( theParams ); return true;
00217   }
00218   // return coordinates of a point on face
00219 
00220   bool FaceUV( const int theFaceID, const gp_XYZ& theParams, gp_XY& theUV ) const {
00221     if ( !IsFaceID ( theFaceID ))                               return false;
00222     theUV = myFace[ theFaceID - ID_FirstF ].GetUV( theParams ); return true;
00223   }
00224   // return UV coordinates on a face by in-block parameters
00225 
00226   bool ShellPoint( const gp_XYZ& theParams, gp_XYZ& thePoint ) const;
00227   // return coordinates of a point in shell
00228 
00229   static bool ShellPoint(const gp_XYZ&         theParams,
00230                          const std::vector<gp_XYZ>& thePointOnShape,
00231                          gp_XYZ&               thePoint );
00232   // computes coordinates of a point in shell by points on sub-shapes
00233   // and point parameters.
00234   // thePointOnShape[ subShapeID ] must be a point on a subShape;
00235   // thePointOnShape.size() == ID_Shell, thePointOnShape[0] not used
00236 
00237 
00238  public:
00239   // ---------------------------------
00240   // Define parameters by coordinates
00241   // ---------------------------------
00242 
00243   bool ComputeParameters (const gp_Pnt& thePoint,
00244                           gp_XYZ&       theParams,
00245                           const int     theShapeID    = ID_Shell,
00246                           const gp_XYZ& theParamsHint = gp_XYZ(-1,-1,-1));
00247   // compute point parameters in the block.
00248   // Note: for edges, it is better to use EdgeParameters()
00249 
00250   bool VertexParameters(const int theVertexID, gp_XYZ& theParams);
00251   // return parameters of a vertex given by TShapeID
00252 
00253   bool EdgeParameters(const int theEdgeID, const double theU, gp_XYZ& theParams);
00254   // return parameters of a point given by theU on edge
00255 
00256 
00257  public:
00258   // ---------------
00259   // Block geomerty
00260   // ---------------
00261 
00262   
00263   
00264  public:
00265   // ---------
00266   // Services
00267   // ---------
00268 
00269   static bool IsForwardEdge (const TopoDS_Edge &                       theEdge,
00270                              const TopTools_IndexedMapOfOrientedShape& theShapeIDMap) {
00271     int v1ID = theShapeIDMap.FindIndex( TopExp::FirstVertex( theEdge ).Oriented( TopAbs_FORWARD ));
00272     int v2ID = theShapeIDMap.FindIndex( TopExp::LastVertex( theEdge ).Oriented( TopAbs_FORWARD ));
00273     return ( v1ID < v2ID );
00274   }
00275   // Return true if an in-block parameter increases along theEdge curve
00276 
00277   static int GetOrderedEdges (const TopoDS_Face&        theFace,
00278                               TopoDS_Vertex             theFirstVertex,
00279                               std::list< TopoDS_Edge >& theEdges,
00280                               std::list< int >  &       theNbEdgesInWires,
00281                               const bool                theShapeAnalysisAlgo=false);
00282   // Return nb wires and a list of oredered edges.
00283   // It is used to assign indices to subshapes.
00284   // theFirstVertex may be NULL.
00285   // Always try to set a seam edge first
00286   // if (theShapeAnalysisAlgo) then ShapeAnalysis::OuterWire() is used to find the outer
00287   // wire else BRepTools::OuterWire() is used
00288 
00289  public:
00290   // -----------------------------------------------------------
00291   // Methods of math_FunctionSetWithDerivatives used internally
00292   // to define parameters by coordinates
00293   // -----------------------------------------------------------
00294   Standard_Integer NbVariables() const;
00295   Standard_Integer NbEquations() const;
00296   Standard_Boolean Value(const math_Vector& X,math_Vector& F) ;
00297   Standard_Boolean Derivatives(const math_Vector& X,math_Matrix& D) ;
00298   Standard_Boolean Values(const math_Vector& X,math_Vector& F,math_Matrix& D) ;
00299   Standard_Integer GetStateNumber ();
00300 
00301  protected:
00302 
00306   void init();
00307 
00308   // Note: to compute params of a point on a face, it is enough to set
00309   // TFace, TEdge's and points for that face only
00310 
00311   // Note 2: curve adaptors need to have only Value(double), FirstParameter() and
00312   // LastParameter() defined to be used by Block algoritms
00313 
00314   class SMESHUtils_EXPORT TEdge {
00315     int                myCoordInd;
00316     double             myFirst;
00317     double             myLast;
00318     Adaptor3d_Curve*   myC3d;
00319     // if mesh volume
00320     gp_XYZ             myNodes[2];
00321   public:
00322     void Set( const int edgeID, Adaptor3d_Curve* curve, const bool isForward );
00323     void Set( const int edgeID, const gp_XYZ& node1, const gp_XYZ& node2 );
00324     Adaptor3d_Curve* GetCurve() const { return myC3d; }
00325     double EndParam(int i) const { return i ? myLast : myFirst; }
00326     int CoordInd() const { return myCoordInd; }
00327     const gp_XYZ& NodeXYZ(int i) const { return i ? myNodes[1] : myNodes[0]; }
00328     gp_XYZ Point( const gp_XYZ& theParams ) const; // Return coord by params
00329     double GetU( const gp_XYZ& theParams ) const;  // Return U by params
00330     TEdge(): myC3d(0) {}
00331     ~TEdge();
00332   };
00333 
00334   class SMESHUtils_EXPORT TFace {
00335     // 4 edges in the order u0, u1, 0v, 1v
00336     int                  myCoordInd[ 4 ];
00337     double               myFirst   [ 4 ];
00338     double               myLast    [ 4 ];
00339     Adaptor2d_Curve2d*   myC2d     [ 4 ];
00340     // 4 corner points in the order 00, 10, 11, 01
00341     gp_XY                myCorner  [ 4 ];
00342     // surface
00343     Adaptor3d_Surface*   myS;
00344     // if mesh volume
00345     gp_XYZ               myNodes[4];
00346   public:
00347     void Set( const int faceID, Adaptor3d_Surface* S, // must be in GetFaceEdgesIDs() order:
00348               Adaptor2d_Curve2d* c2d[4], const bool isForward[4] );
00349     void Set( const int faceID, const TEdge& edgeU0, const TEdge& edgeU1 );
00350     gp_XY  GetUV( const gp_XYZ& theParams ) const;
00351     gp_XYZ Point( const gp_XYZ& theParams ) const;
00352     int GetUInd() const { return myCoordInd[ 0 ]; }
00353     int GetVInd() const { return myCoordInd[ 2 ]; }
00354     void GetCoefs( int i, const gp_XYZ& theParams, double& eCoef, double& vCoef ) const;
00355     TFace(): myS(0) { myC2d[0]=myC2d[1]=myC2d[2]=myC2d[3]=0; }
00356     ~TFace();
00357   };
00358 
00359   // geometry in the order as in TShapeID:
00360   // 8 vertices
00361   gp_XYZ myPnt[ 8 ];
00362   // 12 edges
00363   TEdge  myEdge[ 12 ];
00364   // 6 faces
00365   TFace  myFace[ 6 ];
00366 
00367   // for param computation
00368 
00369   enum { SQUARE_DIST = 0, DRV_1, DRV_2, DRV_3 };
00370   double distance () const { return sqrt( myValues[ SQUARE_DIST ]); }
00371   double funcValue(double sqDist) const { return mySquareFunc ? sqDist : sqrt(sqDist); }
00372   bool computeParameters(const gp_Pnt& thePoint, gp_XYZ& theParams, const gp_XYZ& theParamsHint);
00373 
00374   int      myFaceIndex;
00375   double   myFaceParam;
00376   int      myNbIterations;
00377   double   mySumDist;
00378   double   myTolerance;
00379   bool     mySquareFunc;
00380 
00381   gp_XYZ   myPoint; // the given point
00382   gp_XYZ   myParam; // the best parameters guess
00383   double   myValues[ 4 ]; // values computed at myParam: square distance and 3 derivatives
00384 
00385   typedef std::pair<gp_XYZ,gp_XYZ> TxyzPair;
00386   TxyzPair my3x3x3GridNodes[ 27 ]; // to compute the first param guess
00387   bool     myGridComputed;
00388 };
00389 
00390 
00391 #endif