X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FStdMeshers%2FStdMeshers_Cartesian_3D.cxx;h=2ca7990d5315633d08c839689bb3cb365cafcd1e;hp=f4700221fbd2cf65939f7bff69c8cd218786771b;hb=b7a7d49664daa32e1befb558280e13ed0bde37c9;hpb=86aa8ad014c9dffcded9d98528feec1e8bf6dcc6 diff --git a/src/StdMeshers/StdMeshers_Cartesian_3D.cxx b/src/StdMeshers/StdMeshers_Cartesian_3D.cxx index f4700221f..2ca7990d5 100644 --- a/src/StdMeshers/StdMeshers_Cartesian_3D.cxx +++ b/src/StdMeshers/StdMeshers_Cartesian_3D.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2016 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 @@ -6,7 +6,7 @@ // 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. +// version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -25,6 +25,7 @@ #include "StdMeshers_Cartesian_3D.hxx" #include "SMDS_MeshNode.hxx" +#include "SMESHDS_Mesh.hxx" #include "SMESH_Block.hxx" #include "SMESH_Comment.hxx" #include "SMESH_Mesh.hxx" @@ -35,7 +36,6 @@ #include #include -#include #include @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +55,8 @@ #include #include #include +#include +#include #include #include #include @@ -70,6 +73,7 @@ #include #include #include +#include #include #include #include @@ -83,7 +87,9 @@ #include #include -#undef WITH_TBB +#include + +//#undef WITH_TBB #ifdef WITH_TBB #include //#include @@ -95,25 +101,14 @@ using namespace std; //#define _MY_DEBUG_ #endif -#if OCC_VERSION_LARGE <= 0x06050300 -// workaround is required only for OCCT6.5.3 and older (see OCC22809) -#define ELLIPSOLID_WORKAROUND -#endif - -#ifdef ELLIPSOLID_WORKAROUND -#include -#include -#include -#endif - //============================================================================= /*! * Constructor */ //============================================================================= -StdMeshers_Cartesian_3D::StdMeshers_Cartesian_3D(int hypId, int studyId, SMESH_Gen * gen) - :SMESH_3D_Algo(hypId, studyId, gen) +StdMeshers_Cartesian_3D::StdMeshers_Cartesian_3D(int hypId, SMESH_Gen * gen) + :SMESH_3D_Algo(hypId, gen) { _name = "Cartesian_3D"; _shapeType = (1 << TopAbs_SOLID); // 1 bit /shape type @@ -224,7 +219,7 @@ namespace */ struct GridPlanes { - gp_XYZ _uNorm, _vNorm, _zNorm; + gp_XYZ _zNorm; vector< gp_XYZ > _origins; // origin points of all planes in one direction vector< double > _zProjs; // projections of origins to _zNorm }; @@ -277,11 +272,10 @@ namespace { vector< double > _coords[3]; // coordinates of grid nodes gp_XYZ _axes [3]; // axis directions - vector< GridLine > _lines [3]; // in 3 directions + vector< GridLine > _lines [3]; // in 3 directions double _tol, _minCellSize; gp_XYZ _origin; gp_Mat _invB; // inverted basis of _axes - //bool _isOrthogonalAxes; vector< const SMDS_MeshNode* > _nodes; // mesh nodes at grid nodes vector< const F_IntersectPoint* > _gridIntP; // grid node intersection with geometry @@ -289,6 +283,8 @@ namespace list< E_IntersectPoint > _edgeIntP; // intersections with EDGEs TopTools_IndexedMapOfShape _shapes; + SMESH_MesherHelper* _helper; + size_t CellIndex( size_t i, size_t j, size_t k ) const { return i + j*(_coords[0].size()-1) + k*(_coords[0].size()-1)*(_coords[1].size()-1); @@ -311,41 +307,6 @@ namespace void ComputeUVW(const gp_XYZ& p, double uvw[3]); void ComputeNodes(SMESH_MesherHelper& helper); }; -#ifdef ELLIPSOLID_WORKAROUND - // -------------------------------------------------------------------------- - /*! - * \brief struct temporary replacing IntCurvesFace_Intersector until - * OCCT bug 0022809 is fixed - * http://tracker.dev.opencascade.org/view.php?id=22809 - */ - struct TMP_IntCurvesFace_Intersector - { - BRepAdaptor_Surface _surf; - double _tol; - BRepIntCurveSurface_Inter _intcs; - vector _points; - BRepTopAdaptor_TopolTool _clsf; - - TMP_IntCurvesFace_Intersector(const TopoDS_Face& face, const double tol) - :_surf( face ), _tol( tol ), _clsf( new BRepAdaptor_HSurface(_surf) ) {} - Bnd_Box Bounding() const { Bnd_Box b; BRepBndLib::Add (_surf.Face(), b); return b; } - void Perform( const gp_Lin& line, const double w0, const double w1 ) - { - _points.clear(); - for ( _intcs.Init( _surf.Face(), line, _tol ); _intcs.More(); _intcs.Next() ) - if ( w0 <= _intcs.W() && _intcs.W() <= w1 ) - _points.push_back( _intcs.Point() ); - } - bool IsDone() const { return true; } - int NbPnt() const { return _points.size(); } - IntCurveSurface_TransitionOnCurve Transition( const int i ) const { return _points[ i-1 ].Transition(); } - double WParameter( const int i ) const { return _points[ i-1 ].W(); } - TopAbs_State ClassifyUVPoint(const gp_Pnt2d& p) { return _clsf.Classify( p, _tol ); } - }; -#define __IntCurvesFace_Intersector TMP_IntCurvesFace_Intersector -#else -#define __IntCurvesFace_Intersector IntCurvesFace_Intersector -#endif // -------------------------------------------------------------------------- /*! * \brief Intersector of TopoDS_Face with all GridLine's @@ -356,12 +317,11 @@ namespace TGeomID _faceID; Grid* _grid; Bnd_Box _bndBox; - __IntCurvesFace_Intersector* _surfaceInt; + IntCurvesFace_Intersector* _surfaceInt; vector< std::pair< GridLine*, F_IntersectPoint > > _intersections; FaceGridIntersector(): _grid(0), _surfaceInt(0) {} void Intersect(); - bool IsInGrid(const Bnd_Box& gridBox); void StoreIntersections() { @@ -378,11 +338,11 @@ namespace GetCurveFaceIntersector(); return _bndBox; } - __IntCurvesFace_Intersector* GetCurveFaceIntersector() + IntCurvesFace_Intersector* GetCurveFaceIntersector() { if ( !_surfaceInt ) { - _surfaceInt = new __IntCurvesFace_Intersector( _face, Precision::PConfusion() ); + _surfaceInt = new IntCurvesFace_Intersector( _face, Precision::PConfusion() ); _bndBox = _surfaceInt->Bounding(); if ( _bndBox.IsVoid() ) BRepBndLib::Add (_face, _bndBox); @@ -407,7 +367,7 @@ namespace gp_Cone _cone; gp_Sphere _sphere; gp_Torus _torus; - __IntCurvesFace_Intersector* _surfaceInt; + IntCurvesFace_Intersector* _surfaceInt; vector< F_IntersectPoint > _intPoints; @@ -440,18 +400,20 @@ namespace // -------------------------------------------------------------------------------- struct _Node //!< node either at a hexahedron corner or at intersection { - const SMDS_MeshNode* _node; // mesh node at hexahedron corner + const SMDS_MeshNode* _node; // mesh node at hexahedron corner const B_IntersectPoint* _intPoint; - bool _isUsedInFace; + const _Face* _usedInFace; _Node(const SMDS_MeshNode* n=0, const B_IntersectPoint* ip=0) - :_node(n), _intPoint(ip), _isUsedInFace(0) {} + :_node(n), _intPoint(ip), _usedInFace(0) {} const SMDS_MeshNode* Node() const { return ( _intPoint && _intPoint->_node ) ? _intPoint->_node : _node; } - const F_IntersectPoint* FaceIntPnt() const - { return static_cast< const F_IntersectPoint* >( _intPoint ); } const E_IntersectPoint* EdgeIntPnt() const { return static_cast< const E_IntersectPoint* >( _intPoint ); } + bool IsUsedInFace( const _Face* polygon = 0 ) + { + return polygon ? ( _usedInFace == polygon ) : bool( _usedInFace ); + } void Add( const E_IntersectPoint* ip ) { if ( !_intPoint ) { @@ -465,12 +427,12 @@ namespace _intPoint->Add( ip->_faceIDs ); } } - int IsLinked( const B_IntersectPoint* other, - int avoidFace=-1 ) const // returns id of a common face + TGeomID IsLinked( const B_IntersectPoint* other, + TGeomID avoidFace=-1 ) const // returns id of a common face { return _intPoint ? _intPoint->HasCommonFace( other, avoidFace ) : 0; } - bool IsOnFace( int faceID ) const // returns true if faceID is found + bool IsOnFace( TGeomID faceID ) const // returns true if faceID is found { return _intPoint ? _intPoint->IsOnFace( faceID ) : false; } @@ -483,14 +445,22 @@ namespace return eip->_point; return gp_Pnt( 1e100, 0, 0 ); } + TGeomID ShapeID() const + { + if ( const E_IntersectPoint* eip = dynamic_cast< const E_IntersectPoint* >( _intPoint )) + return eip->_shapeID; + return 0; + } }; // -------------------------------------------------------------------------------- struct _Link // link connecting two _Node's { _Node* _nodes[2]; - vector< _Node > _intNodes; // _Node's at GridLine intersections - vector< _Link > _splits; - vector< _Face*> _faces; + _Face* _faces[2]; // polygons sharing a link + vector< const F_IntersectPoint* > _fIntPoints; // GridLine intersections with FACEs + vector< _Node* > _fIntNodes; // _Node's at _fIntPoints + vector< _Link > _splits; + _Link() { _faces[0] = 0; } }; // -------------------------------------------------------------------------------- struct _OrientedLink @@ -526,32 +496,82 @@ namespace return ( dynamic_cast< const E_IntersectPoint* >( _link->_nodes[0]->_intPoint ) || dynamic_cast< const E_IntersectPoint* >( _link->_nodes[1]->_intPoint )); } + int NbFaces() const + { + return !_link->_faces[0] ? 0 : 1 + bool( _link->_faces[1] ); + } + void AddFace( _Face* f ) + { + if ( _link->_faces[0] ) + { + _link->_faces[1] = f; + } + else + { + _link->_faces[0] = f; + _link->_faces[1] = 0; + } + } + void RemoveFace( _Face* f ) + { + if ( !_link->_faces[0] ) return; + + if ( _link->_faces[1] == f ) + { + _link->_faces[1] = 0; + } + else if ( _link->_faces[0] == f ) + { + _link->_faces[0] = 0; + if ( _link->_faces[1] ) + { + _link->_faces[0] = _link->_faces[1]; + _link->_faces[1] = 0; + } + } + } }; // -------------------------------------------------------------------------------- struct _Face { vector< _OrientedLink > _links; // links on GridLine's vector< _Link > _polyLinks; // links added to close a polygonal face - vector< _Node > _edgeNodes; // nodes at intersection with EDGEs + vector< _Node* > _eIntNodes; // nodes at intersection with EDGEs + bool IsPolyLink( const _OrientedLink& ol ) + { + return _polyLinks.empty() ? false : + ( &_polyLinks[0] <= ol._link && ol._link <= &_polyLinks.back() ); + } + void AddPolyLink(_Node* n0, _Node* n1, _Face* faceToFindEqual=0) + { + if ( faceToFindEqual && faceToFindEqual != this ) { + for ( size_t iL = 0; iL < faceToFindEqual->_polyLinks.size(); ++iL ) + if ( faceToFindEqual->_polyLinks[iL]._nodes[0] == n1 && + faceToFindEqual->_polyLinks[iL]._nodes[1] == n0 ) + { + _links.push_back + ( _OrientedLink( & faceToFindEqual->_polyLinks[iL], /*reverse=*/true )); + return; + } + } + _Link l; + l._nodes[0] = n0; + l._nodes[1] = n1; + _polyLinks.push_back( l ); + _links.push_back( _OrientedLink( &_polyLinks.back() )); + } }; // -------------------------------------------------------------------------------- struct _volumeDef // holder of nodes of a volume mesh element { - //vector< const SMDS_MeshNode* > _nodes; vector< _Node* > _nodes; vector< int > _quantities; typedef boost::shared_ptr<_volumeDef> Ptr; void set( const vector< _Node* >& nodes, const vector< int >& quant = vector< int >() ) { _nodes = nodes; _quantities = quant; } - // static Ptr New( const vector< const SMDS_MeshNode* >& nodes, - // const vector< int > quant = vector< int >() ) - // { - // _volumeDef* def = new _volumeDef; - // def->_nodes = nodes; - // def->_quantities = quant; - // return Ptr( def ); - // } + void set( _Node** nodes, int nb ) + { _nodes.assign( nodes, nodes + nb ); } }; // topology of a hexahedron @@ -564,10 +584,13 @@ namespace vector< _Face > _polygons; // intresections with EDGEs - vector< const E_IntersectPoint* > _edgeIntPnts; + vector< const E_IntersectPoint* > _eIntPoints; + + // additional nodes created at intersection points + vector< _Node > _intNodes; // nodes inside the hexahedron (at VERTEXes) - vector< _Node > _vertexNodes; + vector< _Node* > _vIntNodes; // computed volume elements //vector< _volumeDef::Ptr > _volumeDefs; @@ -575,7 +598,7 @@ namespace Grid* _grid; double _sizeThreshold, _sideLength[3]; - int _nbCornerNodes, _nbIntNodes, _nbBndNodes; + int _nbCornerNodes, _nbFaceIntNodes, _nbBndNodes; int _origNodeInd; // index of _hexNodes[0] node within the _grid size_t _i,_j,_k; @@ -602,8 +625,15 @@ namespace int ijk[], int dIJK[] ); bool findChain( _Node* n1, _Node* n2, _Face& quad, vector<_Node*>& chainNodes ); bool closePolygon( _Face* polygon, vector<_Node*>& chainNodes ) const; + bool findChainOnEdge( const vector< _OrientedLink >& splits, + const _OrientedLink& prevSplit, + const _OrientedLink& avoidSplit, + size_t & iS, + _Face& quad, + vector<_Node*>& chn); int addElements(SMESH_MesherHelper& helper); - bool is1stNodeOut( int iLink ) const; + bool isOutPoint( _Link& link, int iP, SMESH_MesherHelper& helper ) const; + void sortVertexNodes(vector<_Node*>& nodes, _Node* curNode, TGeomID face); bool isInHole() const; bool checkPolyhedronSize() const; bool addHexa (); @@ -611,15 +641,18 @@ namespace bool addPenta(); bool addPyra (); bool debugDumpLink( _Link* link ); - _Node* FindEqualNode( vector< _Node >& nodes, + _Node* findEqualNode( vector< _Node* >& nodes, const E_IntersectPoint* ip, const double tol2 ) { for ( size_t i = 0; i < nodes.size(); ++i ) - if ( nodes[i].Point().SquareDistance( ip->_point ) <= tol2 ) - return & nodes[i]; + if ( nodes[i]->EdgeIntPnt() == ip || + nodes[i]->Point().SquareDistance( ip->_point ) <= tol2 ) + return nodes[i]; return 0; } + bool isImplementEdges() const { return !_grid->_edgeIntP.empty(); } + bool isOutParam(const double uvw[3]) const; }; #ifdef WITH_TBB @@ -630,12 +663,11 @@ namespace struct ParallelHexahedron { vector< Hexahedron* >& _hexVec; - vector& _index; - ParallelHexahedron( vector< Hexahedron* >& hv, vector& ind): _hexVec(hv), _index(ind) {} + ParallelHexahedron( vector< Hexahedron* >& hv ): _hexVec(hv) {} void operator() ( const tbb::blocked_range& r ) const { for ( size_t i = r.begin(); i != r.end(); ++i ) - if ( Hexahedron* hex = _hexVec[ _index[i]] ) + if ( Hexahedron* hex = _hexVec[ i ] ) hex->ComputeElements(); } }; @@ -665,17 +697,17 @@ namespace int& di, double tol ) { //val += values[0]; // input \a val is measured from 0. - if ( i > values.size()-2 ) + if ( i > (int) values.size()-2 ) i = values.size()-2; else - while ( i+2 < values.size() && val > values[ i+1 ]) + while ( i+2 < (int) values.size() && val > values[ i+1 ]) ++i; while ( i > 0 && val < values[ i ]) --i; if ( i > 0 && val - values[ i ] < tol ) di = -1; - else if ( i+2 < values.size() && values[ i+1 ] - val < tol ) + else if ( i+2 < (int) values.size() && values[ i+1 ] - val < tol ) di = 1; else di = 0; @@ -825,10 +857,6 @@ namespace _invB.SetCols( _axes[0], _axes[1], _axes[2] ); _invB.Invert(); - // _isOrthogonalAxes = ( Abs( _axes[0] * _axes[1] ) < 1e-20 && - // Abs( _axes[1] * _axes[2] ) < 1e-20 && - // Abs( _axes[2] * _axes[0] ) < 1e-20 ); - // compute tolerance _minCellSize = Precision::Infinite(); for ( int iDir = 0; iDir < 3; ++iDir ) // loop on 3 line directions @@ -897,13 +925,6 @@ namespace */ void Grid::ComputeUVW(const gp_XYZ& P, double UVW[3]) { - // gp_XYZ p = P - _origin; - // UVW[ 0 ] = p.X() * _invB( 1, 1 ) + p.Y() * _invB( 1, 2 ) + p.Z() * _invB( 1, 3 ); - // UVW[ 1 ] = p.X() * _invB( 2, 1 ) + p.Y() * _invB( 2, 2 ) + p.Z() * _invB( 2, 3 ); - // UVW[ 2 ] = p.X() * _invB( 3, 1 ) + p.Y() * _invB( 3, 2 ) + p.Z() * _invB( 3, 3 ); - // UVW[ 0 ] += _coords[0][0]; - // UVW[ 1 ] += _coords[1][0]; - // UVW[ 2 ] += _coords[2][0]; gp_XYZ p = P * _invB; p.Coord( UVW[0], UVW[1], UVW[2] ); } @@ -939,7 +960,7 @@ namespace const gp_XYZ lineLoc = line._line.Location().XYZ(); const gp_XYZ lineDir = line._line.Direction().XYZ(); line.RemoveExcessIntPoints( _tol ); - multiset< F_IntersectPoint >& intPnts = _lines[ iDir ][ li.LineIndex() ]._intPoints; + multiset< F_IntersectPoint >& intPnts = line._intPoints; multiset< F_IntersectPoint >::iterator ip = intPnts.begin(); bool isOut = true; @@ -1060,80 +1081,6 @@ namespace #endif } - //============================================================================= - /* - * Checks if the face is encosed by the grid - */ - bool FaceGridIntersector::IsInGrid(const Bnd_Box& gridBox) - { - // double x0,y0,z0, x1,y1,z1; - // const Bnd_Box& faceBox = GetFaceBndBox(); - // faceBox.Get(x0,y0,z0, x1,y1,z1); - - // if ( !gridBox.IsOut( gp_Pnt( x0,y0,z0 )) && - // !gridBox.IsOut( gp_Pnt( x1,y1,z1 ))) - // return true; - - // double X0,Y0,Z0, X1,Y1,Z1; - // gridBox.Get(X0,Y0,Z0, X1,Y1,Z1); - // double faceP[6] = { x0,y0,z0, x1,y1,z1 }; - // double gridP[6] = { X0,Y0,Z0, X1,Y1,Z1 }; - // gp_Dir axes[3] = { gp::DX(), gp::DY(), gp::DZ() }; - // for ( int iDir = 0; iDir < 6; ++iDir ) - // { - // if ( iDir < 3 && gridP[ iDir ] <= faceP[ iDir ] ) continue; - // if ( iDir >= 3 && gridP[ iDir ] >= faceP[ iDir ] ) continue; - - // // check if the face intersects a side of a gridBox - - // gp_Pnt p = iDir < 3 ? gp_Pnt( X0,Y0,Z0 ) : gp_Pnt( X1,Y1,Z1 ); - // gp_Ax1 norm( p, axes[ iDir % 3 ] ); - // if ( iDir < 3 ) norm.Reverse(); - - // gp_XYZ O = norm.Location().XYZ(), N = norm.Direction().XYZ(); - - // TopLoc_Location loc = _face.Location(); - // Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(_face,loc); - // if ( !aPoly.IsNull() ) - // { - // if ( !loc.IsIdentity() ) - // { - // norm.Transform( loc.Transformation().Inverted() ); - // O = norm.Location().XYZ(), N = norm.Direction().XYZ(); - // } - // const double deflection = aPoly->Deflection(); - - // const TColgp_Array1OfPnt& nodes = aPoly->Nodes(); - // for ( int i = nodes.Lower(); i <= nodes.Upper(); ++i ) - // if (( nodes( i ).XYZ() - O ) * N > _grid->_tol + deflection ) - // return false; - // } - // else - // { - // BRepAdaptor_Surface surf( _face ); - // double u0, u1, v0, v1, du, dv, u, v; - // BRepTools::UVBounds( _face, u0, u1, v0, v1); - // if ( surf.GetType() == GeomAbs_Plane ) { - // du = u1 - u0, dv = v1 - v0; - // } - // else { - // du = surf.UResolution( _grid->_minCellSize / 10. ); - // dv = surf.VResolution( _grid->_minCellSize / 10. ); - // } - // for ( u = u0, v = v0; u <= u1 && v <= v1; u += du, v += dv ) - // { - // gp_Pnt p = surf.Value( u, v ); - // if (( p.XYZ() - O ) * N > _grid->_tol ) - // { - // TopAbs_State state = GetCurveFaceIntersector()->ClassifyUVPoint(gp_Pnt2d( u, v )); - // if ( state == TopAbs_IN || state == TopAbs_ON ) - // return false; - // } - // } - // } - // } - return true; - } //============================================================================= /* * Intersects TopoDS_Face with all GridLine's @@ -1149,31 +1096,39 @@ namespace typedef void (FaceLineIntersector::* PIntFun )(const GridLine& gridLine); PIntFun interFunction; + bool isDirect = true; BRepAdaptor_Surface surf( _face ); switch ( surf.GetType() ) { case GeomAbs_Plane: intersector._plane = surf.Plane(); interFunction = &FaceLineIntersector::IntersectWithPlane; + isDirect = intersector._plane.Direct(); break; case GeomAbs_Cylinder: intersector._cylinder = surf.Cylinder(); interFunction = &FaceLineIntersector::IntersectWithCylinder; + isDirect = intersector._cylinder.Direct(); break; case GeomAbs_Cone: intersector._cone = surf.Cone(); interFunction = &FaceLineIntersector::IntersectWithCone; + //isDirect = intersector._cone.Direct(); break; case GeomAbs_Sphere: intersector._sphere = surf.Sphere(); interFunction = &FaceLineIntersector::IntersectWithSphere; + isDirect = intersector._sphere.Direct(); break; case GeomAbs_Torus: intersector._torus = surf.Torus(); interFunction = &FaceLineIntersector::IntersectWithTorus; + //isDirect = intersector._torus.Direct(); break; default: interFunction = &FaceLineIntersector::IntersectWithSurface; } + if ( !isDirect ) + std::swap( intersector._transOut, intersector._transIn ); _intersections.clear(); for ( int iDir = 0; iDir < 3; ++iDir ) // loop on 3 line directions @@ -1238,7 +1193,7 @@ namespace /* * Intersect a line with a plane */ - void FaceLineIntersector::IntersectWithPlane (const GridLine& gridLine) + void FaceLineIntersector::IntersectWithPlane(const GridLine& gridLine) { IntAna_IntConicQuad linPlane( gridLine._line, _plane, Precision::Angular()); _w = linPlane.ParamOnConic(1); @@ -1415,7 +1370,7 @@ namespace } if ( surf->IsKind( STANDARD_TYPE(Geom_BSplineSurface )) || surf->IsKind( STANDARD_TYPE(Geom_BezierSurface ))) - if ( !noSafeTShapes.insert((const Standard_Transient*) _face.TShape() ).second ) + if ( !noSafeTShapes.insert( _face.TShape().get() ).second ) isSafe = false; double f, l; @@ -1455,7 +1410,7 @@ namespace edgeIsSafe = false; } } - if ( !edgeIsSafe && !noSafeTShapes.insert((const Standard_Transient*) e.TShape() ).second ) + if ( !edgeIsSafe && !noSafeTShapes.insert( e.TShape().get() ).second ) isSafe = false; } return isSafe; @@ -1465,7 +1420,7 @@ namespace * \brief Creates topology of the hexahedron */ Hexahedron::Hexahedron(const double sizeThreshold, Grid* grid) - : _grid( grid ), _sizeThreshold( sizeThreshold ), _nbIntNodes(0) + : _grid( grid ), _sizeThreshold( sizeThreshold ), _nbFaceIntNodes(0) { _polygons.reserve(100); // to avoid reallocation; @@ -1498,8 +1453,6 @@ namespace _Link& link = _hexLinks[ SMESH_Block::ShapeIndex( linkID )]; link._nodes[0] = &_hexNodes[ SMESH_Block::ShapeIndex( idVec[0] )]; link._nodes[1] = &_hexNodes[ SMESH_Block::ShapeIndex( idVec[1] )]; - link._intNodes.reserve( 10 ); // to avoid reallocation - link._splits.reserve( 10 ); } // set links to faces @@ -1530,7 +1483,7 @@ namespace * \brief Copy constructor */ Hexahedron::Hexahedron( const Hexahedron& other ) - :_grid( other._grid ), _sizeThreshold( other._sizeThreshold ), _nbIntNodes(0) + :_grid( other._grid ), _sizeThreshold( other._sizeThreshold ), _nbFaceIntNodes(0) { _polygons.reserve(100); // to avoid reallocation; @@ -1543,8 +1496,6 @@ namespace _Link& tgtLink = this->_hexLinks[ i ]; tgtLink._nodes[0] = _hexNodes + ( srcLink._nodes[0] - other._hexNodes ); tgtLink._nodes[1] = _hexNodes + ( srcLink._nodes[1] - other._hexNodes ); - tgtLink._intNodes.reserve( 10 ); // to avoid reallocation - tgtLink._splits.reserve( 10 ); } for ( int i = 0; i < 6; ++i ) @@ -1561,7 +1512,7 @@ namespace } } } - + //================================================================================ /*! * \brief Initializes its data by given grid cell @@ -1580,46 +1531,65 @@ namespace _nbCornerNodes += bool( _hexNodes[iN]._node ); _nbBndNodes += bool( _hexNodes[iN]._intPoint ); } - _sideLength[0] = _grid->_coords[0][i+1] - _grid->_coords[0][i]; _sideLength[1] = _grid->_coords[1][j+1] - _grid->_coords[1][j]; _sideLength[2] = _grid->_coords[2][k+1] - _grid->_coords[2][k]; - if ( _nbIntNodes + _edgeIntPnts.size() > 0 && - _nbIntNodes + _nbCornerNodes + _edgeIntPnts.size() > 3) + _intNodes.clear(); + _vIntNodes.clear(); + + if ( _nbFaceIntNodes + _eIntPoints.size() > 0 && + _nbFaceIntNodes + _nbCornerNodes + _eIntPoints.size() > 3) { + _intNodes.reserve( 3 * _nbBndNodes + _nbFaceIntNodes + _eIntPoints.size() ); + + // this method can be called in parallel, so use own helper + SMESH_MesherHelper helper( *_grid->_helper->GetMesh() ); + + // create sub-links (_splits) by splitting links with _fIntPoints _Link split; - // create sub-links (_splits) by splitting links with _intNodes for ( int iLink = 0; iLink < 12; ++iLink ) { _Link& link = _hexLinks[ iLink ]; + link._fIntNodes.resize( link._fIntPoints.size() ); + for ( size_t i = 0; i < link._fIntPoints.size(); ++i ) + { + _intNodes.push_back( _Node( 0, link._fIntPoints[i] )); + link._fIntNodes[ i ] = & _intNodes.back(); + } + link._splits.clear(); split._nodes[ 0 ] = link._nodes[0]; - bool isOut = ( ! link._nodes[0]->Node() ); // is1stNodeOut( iLink ); + bool isOut = ( ! link._nodes[0]->Node() ); bool checkTransition; - for ( size_t i = 0; i < link._intNodes.size(); ++i ) + for ( size_t i = 0; i < link._fIntNodes.size(); ++i ) { - if ( link._intNodes[i].Node() ) // intersection non-coinsident with a grid node + const bool isGridNode = ( ! link._fIntNodes[i]->Node() ); + if ( !isGridNode ) // intersection non-coincident with a grid node { if ( split._nodes[ 0 ]->Node() && !isOut ) { - split._nodes[ 1 ] = &link._intNodes[i]; + split._nodes[ 1 ] = link._fIntNodes[i]; link._splits.push_back( split ); } - split._nodes[ 0 ] = &link._intNodes[i]; + split._nodes[ 0 ] = link._fIntNodes[i]; checkTransition = true; } - else // FACE intersection coinsident with a grid node + else // FACE intersection coincident with a grid node (at link ends) { - checkTransition = ( link._nodes[0]->Node() ); + checkTransition = ( i == 0 && link._nodes[0]->Node() ); } if ( checkTransition ) { - switch ( link._intNodes[i].FaceIntPnt()->_transition ) { - case Trans_OUT: isOut = true; break; - case Trans_IN : isOut = false; break; - default:; // isOut remains the same - } + if ( link._fIntPoints[i]->_faceIDs.size() > 1 || _eIntPoints.size() > 0 ) + isOut = isOutPoint( link, i, helper ); + else + switch ( link._fIntPoints[i]->_transition ) { + case Trans_OUT: isOut = true; break; + case Trans_IN : isOut = false; break; + default: + isOut = isOutPoint( link, i, helper ); + } } } if ( link._nodes[ 1 ]->Node() && split._nodes[ 0 ]->Node() && !isOut ) @@ -1634,21 +1604,21 @@ namespace const double tol2 = _grid->_tol * _grid->_tol; int facets[3], nbFacets, subEntity; - for ( size_t iP = 0; iP < _edgeIntPnts.size(); ++iP ) + for ( size_t iP = 0; iP < _eIntPoints.size(); ++iP ) { - nbFacets = getEntity( _edgeIntPnts[iP], facets, subEntity ); + nbFacets = getEntity( _eIntPoints[iP], facets, subEntity ); _Node* equalNode = 0; switch( nbFacets ) { case 1: // in a _Face { _Face& quad = _hexQuads[ facets[0] - SMESH_Block::ID_FirstF ]; - equalNode = FindEqualNode( quad._edgeNodes, _edgeIntPnts[ iP ], tol2 ); + equalNode = findEqualNode( quad._eIntNodes, _eIntPoints[ iP ], tol2 ); if ( equalNode ) { - equalNode->Add( _edgeIntPnts[ iP ] ); + equalNode->Add( _eIntPoints[ iP ] ); } else { - quad._edgeNodes.push_back( _Node( 0, _edgeIntPnts[ iP ])); - ++_nbIntNodes; + _intNodes.push_back( _Node( 0, _eIntPoints[ iP ])); + quad._eIntNodes.push_back( & _intNodes.back() ); } break; } @@ -1657,22 +1627,22 @@ namespace _Link& link = _hexLinks[ subEntity - SMESH_Block::ID_FirstE ]; if ( link._splits.size() > 0 ) { - equalNode = FindEqualNode( link._intNodes, _edgeIntPnts[ iP ], tol2 ); + equalNode = findEqualNode( link._fIntNodes, _eIntPoints[ iP ], tol2 ); if ( equalNode ) - equalNode->Add( _edgeIntPnts[ iP ] ); + equalNode->Add( _eIntPoints[ iP ] ); } else { + _intNodes.push_back( _Node( 0, _eIntPoints[ iP ])); for ( int iF = 0; iF < 2; ++iF ) { _Face& quad = _hexQuads[ facets[iF] - SMESH_Block::ID_FirstF ]; - equalNode = FindEqualNode( quad._edgeNodes, _edgeIntPnts[ iP ], tol2 ); + equalNode = findEqualNode( quad._eIntNodes, _eIntPoints[ iP ], tol2 ); if ( equalNode ) { - equalNode->Add( _edgeIntPnts[ iP ] ); + equalNode->Add( _eIntPoints[ iP ] ); } else { - quad._edgeNodes.push_back( _Node( 0, _edgeIntPnts[ iP ])); - ++_nbIntNodes; + quad._eIntNodes.push_back( & _intNodes.back() ); } } } @@ -1684,41 +1654,43 @@ namespace if ( node.Node() > 0 ) { if ( node._intPoint ) - node._intPoint->Add( _edgeIntPnts[ iP ]->_faceIDs, _edgeIntPnts[ iP ]->_node ); + node._intPoint->Add( _eIntPoints[ iP ]->_faceIDs, _eIntPoints[ iP ]->_node ); } else { + _intNodes.push_back( _Node( 0, _eIntPoints[ iP ])); for ( int iF = 0; iF < 3; ++iF ) { _Face& quad = _hexQuads[ facets[iF] - SMESH_Block::ID_FirstF ]; - equalNode = FindEqualNode( quad._edgeNodes, _edgeIntPnts[ iP ], tol2 ); + equalNode = findEqualNode( quad._eIntNodes, _eIntPoints[ iP ], tol2 ); if ( equalNode ) { - equalNode->Add( _edgeIntPnts[ iP ] ); + equalNode->Add( _eIntPoints[ iP ] ); } else { - quad._edgeNodes.push_back( _Node( 0, _edgeIntPnts[ iP ])); - ++_nbIntNodes; + quad._eIntNodes.push_back( & _intNodes.back() ); } } } break; } - default: // inside a hex + } // switch( nbFacets ) + + if ( nbFacets == 0 || + _grid->_shapes( _eIntPoints[ iP ]->_shapeID ).ShapeType() == TopAbs_VERTEX ) { - equalNode = FindEqualNode( _vertexNodes, _edgeIntPnts[ iP ], tol2 ); + equalNode = findEqualNode( _vIntNodes, _eIntPoints[ iP ], tol2 ); if ( equalNode ) { - equalNode->Add( _edgeIntPnts[ iP ] ); + equalNode->Add( _eIntPoints[ iP ] ); } - else { - _vertexNodes.push_back( _Node( 0, _edgeIntPnts[iP] )); - ++_nbIntNodes; + else if ( nbFacets == 0 ) { + if ( _intNodes.empty() || _intNodes.back().EdgeIntPnt() != _eIntPoints[ iP ]) + _intNodes.push_back( _Node( 0, _eIntPoints[ iP ])); + _vIntNodes.push_back( & _intNodes.back() ); } } - } // switch( nbFacets ) - - } // loop on _edgeIntPnts + } // loop on _eIntPoints } - else if ( 3 < _nbCornerNodes && _nbCornerNodes < 8 ) // _nbIntNodes == 0 + else if ( 3 < _nbCornerNodes && _nbCornerNodes < 8 ) // _nbFaceIntNodes == 0 { _Link split; // create sub-links (_splits) of whole links @@ -1758,10 +1730,11 @@ namespace { Init(); - if ( _nbCornerNodes + _nbIntNodes < 4 ) + int nbIntersections = _nbFaceIntNodes + _eIntPoints.size(); + if ( _nbCornerNodes + nbIntersections < 4 ) return; - if ( _nbBndNodes == _nbCornerNodes && _nbIntNodes == 0 && isInHole() ) + if ( _nbBndNodes == _nbCornerNodes && nbIntersections == 0 && isInHole() ) return; _polygons.clear(); @@ -1770,11 +1743,11 @@ namespace // Create polygons from quadrangles // -------------------------------- - _Link polyLink; vector< _OrientedLink > splits; - vector<_Node*> chainNodes; + vector<_Node*> chainNodes; + _Face* coplanarPolyg; - bool hasEdgeIntersections = !_edgeIntPnts.empty(); + bool hasEdgeIntersections = !_eIntPoints.empty(); for ( int iF = 0; iF < 6; ++iF ) // loop on 6 sides of a hexahedron { @@ -1793,28 +1766,19 @@ namespace // polygon's boundary closed int nbSplits = splits.size(); - if ( nbSplits < 2 && quad._edgeNodes.empty() ) + if (( nbSplits == 1 ) && + ( quad._eIntNodes.empty() || + splits[0].FirstNode()->IsLinked( splits[0].LastNode()->_intPoint ))) + //( quad._eIntNodes.empty() || _nbCornerNodes + nbIntersections > 6 )) nbSplits = 0; - if ( nbSplits == 0 && !quad._edgeNodes.empty() ) - { - // make _vertexNodes from _edgeNodes of an empty quad - const double tol2 = _grid->_tol * _grid->_tol; - for ( size_t iP = 0; iP < quad._edgeNodes.size(); ++iP ) - { - _Node* equalNode = - FindEqualNode( _vertexNodes, quad._edgeNodes[ iP ].EdgeIntPnt(), tol2 ); - if ( equalNode ) - equalNode->Add( quad._edgeNodes[ iP ].EdgeIntPnt() ); - else - _vertexNodes.push_back( quad._edgeNodes[ iP ]); - } - } #ifdef _DEBUG_ - for ( size_t iP = 0; iP < quad._edgeNodes.size(); ++iP ) - quad._edgeNodes[ iP ]._isUsedInFace = false; + for ( size_t iP = 0; iP < quad._eIntNodes.size(); ++iP ) + if ( quad._eIntNodes[ iP ]->IsUsedInFace( polygon )) + quad._eIntNodes[ iP ]->_usedInFace = 0; #endif - int nbUsedEdgeNodes = 0; + size_t nbUsedEdgeNodes = 0; + _Face* prevPolyg = 0; // polygon previously created from this quad while ( nbSplits > 0 ) { @@ -1840,19 +1804,30 @@ namespace if ( !split ) continue; n1 = split.FirstNode(); - if ( n1 != n2 ) + if ( n1 == n2 && + n1->_intPoint && + n1->_intPoint->_faceIDs.size() > 1 ) + { + // n1 is at intersection with EDGE + if ( findChainOnEdge( splits, polygon->_links.back(), split, iS, quad, chainNodes )) + { + for ( size_t i = 1; i < chainNodes.size(); ++i ) + polygon->AddPolyLink( chainNodes[i-1], chainNodes[i], prevPolyg ); + prevPolyg = polygon; + n2 = chainNodes.back(); + continue; + } + } + else if ( n1 != n2 ) { // try to connect to intersections with EDGEs - if ( quad._edgeNodes.size() > nbUsedEdgeNodes && + if ( quad._eIntNodes.size() > nbUsedEdgeNodes && findChain( n2, n1, quad, chainNodes )) { for ( size_t i = 1; i < chainNodes.size(); ++i ) { - polyLink._nodes[0] = chainNodes[i-1]; - polyLink._nodes[1] = chainNodes[i]; - polygon->_polyLinks.push_back( polyLink ); - polygon->_links.push_back( _OrientedLink( &polygon->_polyLinks.back() )); - nbUsedEdgeNodes += polyLink._nodes[1]->_isUsedInFace; + polygon->AddPolyLink( chainNodes[i-1], chainNodes[i] ); + nbUsedEdgeNodes += ( chainNodes[i]->IsUsedInFace( polygon )); } if ( chainNodes.back() != n1 ) { @@ -1865,14 +1840,10 @@ namespace else { _OrientedLink foundSplit; - for ( int i = iS; i < splits.size() && !foundSplit; ++i ) + for ( size_t i = iS; i < splits.size() && !foundSplit; ++i ) if (( foundSplit = splits[ i ]) && ( n2->IsLinked( foundSplit.FirstNode()->_intPoint ))) { - polyLink._nodes[0] = n2; - polyLink._nodes[1] = foundSplit.FirstNode(); - polygon->_polyLinks.push_back( polyLink ); - polygon->_links.push_back( _OrientedLink( &polygon->_polyLinks.back() )); iS = i - 1; } else @@ -1881,20 +1852,22 @@ namespace } if ( foundSplit ) { - n2 = foundSplit.FirstNode(); + if ( n2 != foundSplit.FirstNode() ) + { + polygon->AddPolyLink( n2, foundSplit.FirstNode() ); + n2 = foundSplit.FirstNode(); + } continue; } else { if ( n2->IsLinked( nFirst->_intPoint )) break; - polyLink._nodes[0] = n2; - polyLink._nodes[1] = n1; - polygon->_polyLinks.push_back( polyLink ); - polygon->_links.push_back( _OrientedLink( &polygon->_polyLinks.back() )); + polygon->AddPolyLink( n2, n1, prevPolyg ); } } - } + } // if ( n1 != n2 ) + polygon->_links.push_back( split ); split._link = 0; --nbSplits; @@ -1907,14 +1880,13 @@ namespace if ( !findChain( n2, nFirst, quad, chainNodes )) { if ( !closePolygon( polygon, chainNodes )) - ;//chainNodes.push_back( nFirst ); + if ( !isImplementEdges() ) + chainNodes.push_back( nFirst ); } for ( size_t i = 1; i < chainNodes.size(); ++i ) { - polyLink._nodes[0] = chainNodes[i-1]; - polyLink._nodes[1] = chainNodes[i]; - polygon->_polyLinks.push_back( polyLink ); - polygon->_links.push_back( _OrientedLink( &polygon->_polyLinks.back() )); + polygon->AddPolyLink( chainNodes[i-1], chainNodes[i], prevPolyg ); + nbUsedEdgeNodes += bool( chainNodes[i]->IsUsedInFace( polygon )); } } @@ -1926,21 +1898,26 @@ namespace } // while ( nbSplits > 0 ) if ( polygon->_links.size() < 3 ) + { _polygons.pop_back(); - - } // loop on 6 sides of a hexahedron + } + } // loop on 6 hexahedron sides // Create polygons closing holes in a polyhedron // ---------------------------------------------- - // add polygons to their links + // clear _usedInFace + for ( size_t iN = 0; iN < _intNodes.size(); ++iN ) + _intNodes[ iN ]._usedInFace = 0; + + // add polygons to their links and mark used nodes for ( size_t iP = 0; iP < _polygons.size(); ++iP ) { _Face& polygon = _polygons[ iP ]; for ( size_t iL = 0; iL < polygon._links.size(); ++iL ) { - polygon._links[ iL ]._link->_faces.reserve( 2 ); - polygon._links[ iL ]._link->_faces.push_back( &polygon ); + polygon._links[ iL ].AddFace( &polygon ); + polygon._links[ iL ].FirstNode()->_usedInFace = &polygon; } } // find free links @@ -1950,25 +1927,55 @@ namespace { _Face& polygon = _polygons[ iP ]; for ( size_t iL = 0; iL < polygon._links.size(); ++iL ) - if ( polygon._links[ iL ]._link->_faces.size() < 2 ) + if ( polygon._links[ iL ].NbFaces() < 2 ) freeLinks.push_back( & polygon._links[ iL ]); } int nbFreeLinks = freeLinks.size(); - if ( nbFreeLinks < 3 ) return; + if ( nbFreeLinks == 1 ) return; + + // put not used intersection nodes to _vIntNodes + int nbVertexNodes = 0; // nb not used vertex nodes + { + for ( size_t iN = 0; iN < _vIntNodes.size(); ++iN ) + nbVertexNodes += ( !_vIntNodes[ iN ]->IsUsedInFace() ); + + const double tol = 1e-3 * Min( Min( _sideLength[0], _sideLength[1] ), _sideLength[0] ); + for ( size_t iN = _nbFaceIntNodes; iN < _intNodes.size(); ++iN ) + { + if ( _intNodes[ iN ].IsUsedInFace() ) continue; + if ( dynamic_cast< const F_IntersectPoint* >( _intNodes[ iN ]._intPoint )) continue; + _Node* equalNode = + findEqualNode( _vIntNodes, _intNodes[ iN ].EdgeIntPnt(), tol*tol ); + if ( !equalNode ) + { + _vIntNodes.push_back( &_intNodes[ iN ]); + ++nbVertexNodes; + } + } + } set usedFaceIDs; + vector< TGeomID > faces; + TGeomID curFace = 0; + const size_t nbQuadPolygons = _polygons.size(); + E_IntersectPoint ipTmp; - // make closed chains of free links + // create polygons by making closed chains of free links + size_t iPolygon = _polygons.size(); while ( nbFreeLinks > 0 ) { - _polygons.resize( _polygons.size() + 1 ); - _Face& polygon = _polygons.back(); - polygon._polyLinks.reserve( 20 ); - polygon._links.reserve( 20 ); + if ( iPolygon == _polygons.size() ) + { + _polygons.resize( _polygons.size() + 1 ); + _polygons[ iPolygon ]._polyLinks.reserve( 20 ); + _polygons[ iPolygon ]._links.reserve( 20 ); + } + _Face& polygon = _polygons[ iPolygon ]; _OrientedLink* curLink = 0; _Node* curNode; - if ( !hasEdgeIntersections ) + if (( !hasEdgeIntersections ) || + ( nbFreeLinks < 4 && nbVertexNodes == 0 )) { // get a remaining link to start from for ( size_t iL = 0; iL < freeLinks.size() && !curLink; ++iL ) @@ -1993,41 +2000,40 @@ namespace } else // there are intersections with EDGEs { - TGeomID curFace; - // get a remaining link to start from, one lying on minimal - // nb of FACEs + // get a remaining link to start from, one lying on minimal nb of FACEs { - vector< pair< TGeomID, int > > facesOfLink[3]; - pair< TGeomID, int > faceOfLink( -1, -1 ); - vector< TGeomID > faces; + typedef pair< TGeomID, int > TFaceOfLink; + TFaceOfLink faceOfLink( -1, -1 ); + TFaceOfLink facesOfLink[3] = { faceOfLink, faceOfLink, faceOfLink }; for ( size_t iL = 0; iL < freeLinks.size(); ++iL ) if ( freeLinks[ iL ] ) { faces = freeLinks[ iL ]->GetNotUsedFace( usedFaceIDs ); if ( faces.size() == 1 ) { - faceOfLink = make_pair( faces[0], iL ); + faceOfLink = TFaceOfLink( faces[0], iL ); if ( !freeLinks[ iL ]->HasEdgeNodes() ) break; - facesOfLink[0].push_back( faceOfLink ); + facesOfLink[0] = faceOfLink; } - else if ( facesOfLink[0].empty() ) + else if ( facesOfLink[0].first < 0 ) { - faceOfLink = make_pair(( faces.empty() ? -1 : faces[0]), iL ); - facesOfLink[ 1 + faces.empty() ].push_back( faceOfLink ); + faceOfLink = TFaceOfLink(( faces.empty() ? -1 : faces[0]), iL ); + facesOfLink[ 1 + faces.empty() ] = faceOfLink; } } - for ( int i = 0; faceOfLink.second < 0 && i < 3; ++i ) - if ( !facesOfLink[i].empty() ) - faceOfLink = facesOfLink[i][0]; + for ( int i = 0; faceOfLink.first < 0 && i < 3; ++i ) + faceOfLink = facesOfLink[i]; if ( faceOfLink.first < 0 ) // all faces used { - for ( size_t i = 0; i < facesOfLink[2].size() && faceOfLink.first < 1; ++i ) - { - curLink = freeLinks[ facesOfLink[2][i].second ]; - faceOfLink.first = curLink->FirstNode()->IsLinked( curLink->FirstNode()->_intPoint ); - } + for ( size_t iL = 0; iL < freeLinks.size() && faceOfLink.first < 1; ++iL ) + if (( curLink = freeLinks[ iL ])) + { + faceOfLink.first = + curLink->FirstNode()->IsLinked( curLink->LastNode()->_intPoint ); + faceOfLink.second = iL; + } usedFaceIDs.clear(); } curFace = faceOfLink.first; @@ -2038,7 +2044,7 @@ namespace polygon._links.push_back( *curLink ); --nbFreeLinks; - // find all links bounding a FACE of curLink + // find all links lying on a curFace do { // go forward from curLink @@ -2080,36 +2086,37 @@ namespace if ( polygon._links[0].LastNode() != curNode ) { - if ( !_vertexNodes.empty() ) + if ( nbVertexNodes > 0 ) { - // add links with _vertexNodes if not already used - for ( size_t iN = 0; iN < _vertexNodes.size(); ++iN ) - if ( _vertexNodes[ iN ].IsOnFace( curFace )) + // add links with _vIntNodes if not already used + chainNodes.clear(); + for ( size_t iN = 0; iN < _vIntNodes.size(); ++iN ) + if ( !_vIntNodes[ iN ]->IsUsedInFace() && + _vIntNodes[ iN ]->IsOnFace( curFace )) { - bool used = ( curNode == &_vertexNodes[ iN ] ); - for ( size_t iL = 0; iL < polygon._links.size() && !used; ++iL ) - used = ( &_vertexNodes[ iN ] == polygon._links[ iL ].LastNode() ); - if ( !used ) - { - polyLink._nodes[0] = &_vertexNodes[ iN ]; - polyLink._nodes[1] = curNode; - polygon._polyLinks.push_back( polyLink ); - polygon._links.push_back( _OrientedLink( &polygon._polyLinks.back() )); - freeLinks.push_back( &polygon._links.back() ); - ++nbFreeLinks; - curNode = &_vertexNodes[ iN ]; - } - // TODO: to reorder _vertexNodes within polygon, if there are several ones + _vIntNodes[ iN ]->_usedInFace = &polygon; + chainNodes.push_back( _vIntNodes[ iN ] ); } + if ( chainNodes.size() > 1 ) + { + sortVertexNodes( chainNodes, curNode, curFace ); + } + for ( size_t i = 0; i < chainNodes.size(); ++i ) + { + polygon.AddPolyLink( chainNodes[ i ], curNode ); + curNode = chainNodes[ i ]; + freeLinks.push_back( &polygon._links.back() ); + ++nbFreeLinks; + } + nbVertexNodes -= chainNodes.size(); + } + // if ( polygon._links.size() > 1 ) + { + polygon.AddPolyLink( polygon._links[0].LastNode(), curNode ); + freeLinks.push_back( &polygon._links.back() ); + ++nbFreeLinks; } - polyLink._nodes[0] = polygon._links[0].LastNode(); - polyLink._nodes[1] = curNode; - polygon._polyLinks.push_back( polyLink ); - polygon._links.push_back( _OrientedLink( &polygon._polyLinks.back() )); - freeLinks.push_back( &polygon._links.back() ); - ++nbFreeLinks; } - } // if there are intersections with EDGEs if ( polygon._links.size() < 2 || @@ -2120,27 +2127,119 @@ namespace { if ( freeLinks.back() == &polygon._links.back() ) { - freeLinks.back() = 0; + freeLinks.pop_back(); --nbFreeLinks; } - vector< _Face*>& polygs1 = polygon._links.front()._link->_faces; - vector< _Face*>& polygs2 = polygon._links.back()._link->_faces; - _Face* polyg1 = ( polygs1.empty() ? 0 : polygs1[0] ); - _Face* polyg2 = ( polygs2.empty() ? 0 : polygs2[0] ); - if ( polyg1 ) polygs2.push_back( polyg1 ); - if ( polyg2 ) polygs1.push_back( polyg2 ); - _polygons.pop_back(); + if ( polygon._links.front().NbFaces() > 0 ) + polygon._links.back().AddFace( polygon._links.front()._link->_faces[0] ); + if ( polygon._links.back().NbFaces() > 0 ) + polygon._links.front().AddFace( polygon._links.back()._link->_faces[0] ); + + if ( iPolygon == _polygons.size()-1 ) + _polygons.pop_back(); } - else + else // polygon._links.size() >= 2 { // add polygon to its links for ( size_t iL = 0; iL < polygon._links.size(); ++iL ) { - polygon._links[ iL ]._link->_faces.reserve( 2 ); - polygon._links[ iL ]._link->_faces.push_back( &polygon ); + polygon._links[ iL ].AddFace( &polygon ); polygon._links[ iL ].Reverse(); } - } + if ( /*hasEdgeIntersections &&*/ iPolygon == _polygons.size() - 1 ) + { + // check that a polygon does not lie on a hexa side + coplanarPolyg = 0; + for ( size_t iL = 0; iL < polygon._links.size() && !coplanarPolyg; ++iL ) + { + if ( polygon._links[ iL ].NbFaces() < 2 ) + continue; // it's a just added free link + // look for a polygon made on a hexa side and sharing + // two or more haxa links + size_t iL2; + coplanarPolyg = polygon._links[ iL ]._link->_faces[0]; + for ( iL2 = iL + 1; iL2 < polygon._links.size(); ++iL2 ) + if ( polygon._links[ iL2 ]._link->_faces[0] == coplanarPolyg && + !coplanarPolyg->IsPolyLink( polygon._links[ iL ]) && + !coplanarPolyg->IsPolyLink( polygon._links[ iL2 ]) && + coplanarPolyg < & _polygons[ nbQuadPolygons ]) + break; + if ( iL2 == polygon._links.size() ) + coplanarPolyg = 0; + } + if ( coplanarPolyg ) // coplanar polygon found + { + freeLinks.resize( freeLinks.size() - polygon._polyLinks.size() ); + nbFreeLinks -= polygon._polyLinks.size(); + + // an E_IntersectPoint used to mark nodes of coplanarPolyg + // as lying on curFace while they are not at intersection with geometry + ipTmp._faceIDs.resize(1); + ipTmp._faceIDs[0] = curFace; + + // fill freeLinks with links not shared by coplanarPolyg and polygon + for ( size_t iL = 0; iL < polygon._links.size(); ++iL ) + if ( polygon._links[ iL ]._link->_faces[1] && + polygon._links[ iL ]._link->_faces[0] != coplanarPolyg ) + { + _Face* p = polygon._links[ iL ]._link->_faces[0]; + for ( size_t iL2 = 0; iL2 < p->_links.size(); ++iL2 ) + if ( p->_links[ iL2 ]._link == polygon._links[ iL ]._link ) + { + freeLinks.push_back( & p->_links[ iL2 ] ); + ++nbFreeLinks; + freeLinks.back()->RemoveFace( &polygon ); + break; + } + } + for ( size_t iL = 0; iL < coplanarPolyg->_links.size(); ++iL ) + if ( coplanarPolyg->_links[ iL ]._link->_faces[1] && + coplanarPolyg->_links[ iL ]._link->_faces[1] != &polygon ) + { + _Face* p = coplanarPolyg->_links[ iL ]._link->_faces[0]; + if ( p == coplanarPolyg ) + p = coplanarPolyg->_links[ iL ]._link->_faces[1]; + for ( size_t iL2 = 0; iL2 < p->_links.size(); ++iL2 ) + if ( p->_links[ iL2 ]._link == coplanarPolyg->_links[ iL ]._link ) + { + // set links of coplanarPolyg in place of used freeLinks + // to re-create coplanarPolyg next + size_t iL3 = 0; + for ( ; iL3 < freeLinks.size() && freeLinks[ iL3 ]; ++iL3 ); + if ( iL3 < freeLinks.size() ) + freeLinks[ iL3 ] = ( & p->_links[ iL2 ] ); + else + freeLinks.push_back( & p->_links[ iL2 ] ); + ++nbFreeLinks; + freeLinks[ iL3 ]->RemoveFace( coplanarPolyg ); + // mark nodes of coplanarPolyg as lying on curFace + for ( int iN = 0; iN < 2; ++iN ) + { + _Node* n = freeLinks[ iL3 ]->_link->_nodes[ iN ]; + if ( n->_intPoint ) n->_intPoint->Add( ipTmp._faceIDs ); + else n->_intPoint = &ipTmp; + } + break; + } + } + // set coplanarPolyg to be re-created next + for ( size_t iP = 0; iP < _polygons.size(); ++iP ) + if ( coplanarPolyg == & _polygons[ iP ] ) + { + iPolygon = iP; + _polygons[ iPolygon ]._links.clear(); + _polygons[ iPolygon ]._polyLinks.clear(); + break; + } + _polygons.pop_back(); + usedFaceIDs.erase( curFace ); + continue; + } // if ( coplanarPolyg ) + } // if ( hasEdgeIntersections ) - search for coplanarPolyg + + iPolygon = _polygons.size(); + + } // end of case ( polygon._links.size() > 2 ) } // while ( nbFreeLinks > 0 ) if ( ! checkPolyhedronSize() ) @@ -2148,13 +2247,28 @@ namespace return; } + for ( size_t i = 0; i < 8; ++i ) + if ( _hexNodes[ i ]._intPoint == &ipTmp ) + _hexNodes[ i ]._intPoint = 0; + // create a classic cell if possible - const int nbNodes = _nbCornerNodes + _nbIntNodes; + + int nbPolygons = 0; + for ( size_t iF = 0; iF < _polygons.size(); ++iF ) + nbPolygons += (_polygons[ iF ]._links.size() > 0 ); + + //const int nbNodes = _nbCornerNodes + nbIntersections; + int nbNodes = 0; + for ( size_t i = 0; i < 8; ++i ) + nbNodes += _hexNodes[ i ].IsUsedInFace(); + for ( size_t i = 0; i < _intNodes.size(); ++i ) + nbNodes += _intNodes[ i ].IsUsedInFace(); + bool isClassicElem = false; - if ( nbNodes == 8 && _polygons.size() == 6 ) isClassicElem = addHexa(); - else if ( nbNodes == 4 && _polygons.size() == 4 ) isClassicElem = addTetra(); - else if ( nbNodes == 6 && _polygons.size() == 5 ) isClassicElem = addPenta(); - else if ( nbNodes == 5 && _polygons.size() == 5 ) isClassicElem = addPyra (); + if ( nbNodes == 8 && nbPolygons == 6 ) isClassicElem = addHexa(); + else if ( nbNodes == 4 && nbPolygons == 4 ) isClassicElem = addTetra(); + else if ( nbNodes == 6 && nbPolygons == 5 ) isClassicElem = addPenta(); + else if ( nbNodes == 5 && nbPolygons == 5 ) isClassicElem = addPyra (); if ( !isClassicElem ) { _volumeDefs._nodes.clear(); @@ -2163,6 +2277,7 @@ namespace for ( size_t iF = 0; iF < _polygons.size(); ++iF ) { const size_t nbLinks = _polygons[ iF ]._links.size(); + if ( nbLinks == 0 ) continue; _volumeDefs._quantities.push_back( nbLinks ); for ( size_t iL = 0; iL < nbLinks; ++iL ) _volumeDefs._nodes.push_back( _polygons[ iF ]._links[ iL ].FirstNode() ); @@ -2182,10 +2297,10 @@ namespace _grid->_coords[1].size() - 1, _grid->_coords[2].size() - 1 }; const size_t nbGridCells = nbCells[0] * nbCells[1] * nbCells[2]; - vector< Hexahedron* > intersectedHex( nbGridCells, 0 ); + vector< Hexahedron* > allHexa( nbGridCells, 0 ); int nbIntHex = 0; - // set intersection nodes from GridLine's to links of intersectedHex + // set intersection nodes from GridLine's to links of allHexa int i,j,k, iDirOther[3][2] = {{ 1,2 },{ 0,2 },{ 0,1 }}; for ( int iDir = 0; iDir < 3; ++iDir ) { @@ -2208,12 +2323,12 @@ namespace i = int(lineInd.I()) + dInd[iL][0]; j = int(lineInd.J()) + dInd[iL][1]; k = int(lineInd.K()) + dInd[iL][2]; - if ( i < 0 || i >= nbCells[0] || - j < 0 || j >= nbCells[1] || - k < 0 || k >= nbCells[2] ) continue; + if ( i < 0 || i >= (int) nbCells[0] || + j < 0 || j >= (int) nbCells[1] || + k < 0 || k >= (int) nbCells[2] ) continue; const size_t hexIndex = _grid->CellIndex( i,j,k ); - Hexahedron *& hex = intersectedHex[ hexIndex ]; + Hexahedron *& hex = allHexa[ hexIndex ]; if ( !hex) { hex = new Hexahedron( *this ); @@ -2223,27 +2338,26 @@ namespace ++nbIntHex; } const int iLink = iL + iDir * 4; - hex->_hexLinks[iLink]._intNodes.push_back( _Node( 0, &(*ip) )); - hex->_nbIntNodes += bool( ip->_node ); + hex->_hexLinks[iLink]._fIntPoints.push_back( &(*ip) ); + hex->_nbFaceIntNodes += bool( ip->_node ); } } } } // implement geom edges into the mesh - addEdges( helper, intersectedHex, edge2faceIDsMap ); + addEdges( helper, allHexa, edge2faceIDsMap ); // add not split hexadrons to the mesh int nbAdded = 0; - vector intHexInd( nbIntHex ); - nbIntHex = 0; - for ( size_t i = 0; i < intersectedHex.size(); ++i ) + vector< Hexahedron* > intHexa( nbIntHex, (Hexahedron*) NULL ); + for ( size_t i = 0; i < allHexa.size(); ++i ) { - Hexahedron * & hex = intersectedHex[ i ]; + Hexahedron * & hex = allHexa[ i ]; if ( hex ) { - intHexInd[ nbIntHex++ ] = i; - if ( hex->_nbIntNodes > 0 || ! hex->_edgeIntPnts.empty()) + intHexa.push_back( hex ); + if ( hex->_nbFaceIntNodes > 0 || hex->_eIntPoints.size() > 0 ) continue; // treat intersected hex later this->init( hex->_i, hex->_j, hex->_k ); } @@ -2263,46 +2377,39 @@ namespace mesh->SetMeshElementOnShape( el, helper.GetSubShapeID() ); ++nbAdded; if ( hex ) - { - delete hex; - intersectedHex[ i ] = 0; - --nbIntHex; - } + intHexa.pop_back(); } else if ( _nbCornerNodes > 3 && !hex ) { // all intersection of hex with geometry are at grid nodes hex = new Hexahedron( *this ); - //hex->init( i ); hex->_i = _i; hex->_j = _j; hex->_k = _k; - intHexInd.push_back(0); - intHexInd[ nbIntHex++ ] = i; + intHexa.push_back( hex ); } } // add elements resulted from hexadron intersection #ifdef WITH_TBB - intHexInd.resize( nbIntHex ); - tbb::parallel_for ( tbb::blocked_range( 0, nbIntHex ), - ParallelHexahedron( intersectedHex, intHexInd ), + tbb::parallel_for ( tbb::blocked_range( 0, intHexa.size() ), + ParallelHexahedron( intHexa ), tbb::simple_partitioner()); // ComputeElements() is called here - for ( size_t i = 0; i < intHexInd.size(); ++i ) - if ( Hexahedron * hex = intersectedHex[ intHexInd[ i ]] ) + for ( size_t i = 0; i < intHexa.size(); ++i ) + if ( Hexahedron * hex = intHexa[ i ] ) nbAdded += hex->addElements( helper ); #else - for ( size_t i = 0; i < intHexInd.size(); ++i ) - if ( Hexahedron * hex = intersectedHex[ intHexInd[ i ]] ) + for ( size_t i = 0; i < intHexa.size(); ++i ) + if ( Hexahedron * hex = intHexa[ i ] ) { hex->ComputeElements(); nbAdded += hex->addElements( helper ); } #endif - for ( size_t i = 0; i < intersectedHex.size(); ++i ) - if ( intersectedHex[ i ] ) - delete intersectedHex[ i ]; + for ( size_t i = 0; i < allHexa.size(); ++i ) + if ( allHexa[ i ] ) + delete allHexa[ i ]; return nbAdded; } @@ -2325,14 +2432,12 @@ namespace GridPlanes& planes = pln[ iDirZ ]; int iDirX = ( iDirZ + 1 ) % 3; int iDirY = ( iDirZ + 2 ) % 3; - // planes._uNorm = ( _grid->_axes[ iDirY ] ^ _grid->_axes[ iDirZ ] ).Normalized(); - // planes._vNorm = ( _grid->_axes[ iDirZ ] ^ _grid->_axes[ iDirX ] ).Normalized(); planes._zNorm = ( _grid->_axes[ iDirX ] ^ _grid->_axes[ iDirY ] ).Normalized(); planes._zProjs.resize ( _grid->_coords[ iDirZ ].size() ); planes._zProjs [0] = 0; const double zFactor = _grid->_axes[ iDirZ ] * planes._zNorm; const vector< double > & u = _grid->_coords[ iDirZ ]; - for ( int i = 1; i < planes._zProjs.size(); ++i ) + for ( size_t i = 1; i < planes._zProjs.size(); ++i ) { planes._zProjs [i] = zFactor * ( u[i] - u[0] ); } @@ -2355,7 +2460,7 @@ namespace ip._faceIDs = e2fIt->second; ip._shapeID = edgeID; - // discretize the EGDE + // discretize the EDGE GCPnts_UniformDeflection discret( curve, deflection, true ); if ( !discret.IsDone() || discret.NbPoints() < 2 ) continue; @@ -2369,7 +2474,6 @@ namespace double xLen = _grid->_coords[ iDirX ].back() - _grid->_coords[ iDirX ][0]; double yLen = _grid->_coords[ iDirY ].back() - _grid->_coords[ iDirY ][0]; double zLen = _grid->_coords[ iDirZ ].back() - _grid->_coords[ iDirZ ][0]; - //double zFactor = _grid->_axes[ iDirZ ] * planes._zNorm; int dIJK[3], d000[3] = { 0,0,0 }; double o[3] = { _grid->_coords[0][0], _grid->_coords[1][0], @@ -2397,9 +2501,11 @@ namespace if ( iDirZ == 0 ) { ip._point = p1; + ip._shapeID = _grid->_shapes.Add( v1 ); _grid->_edgeIntP.push_back( ip ); if ( !addIntersection( _grid->_edgeIntP.back(), hexes, ijk, d000 )) _grid->_edgeIntP.pop_back(); + ip._shapeID = edgeID; } for ( int iP = 2; iP <= discret.NbPoints(); ++iP ) { @@ -2407,32 +2513,35 @@ namespace gp_XYZ p2 = discret.Value( iP ).XYZ(); double u2 = discret.Parameter( iP ); double zProj2 = planes._zNorm * ( p2 - _grid->_origin ); - int iZ2 = iZ1; - locateValue( iZ2, zProj2, planes._zProjs, dIJK[ iDirZ ], tol ); - - // treat intersections with planes between 2 end points of a segment - int dZ = ( iZ1 <= iZ2 ) ? +1 : -1; - int iZ = iZ1 + ( iZ1 < iZ2 ); - for ( int i = 0, nb = Abs( iZ1 - iZ2 ); i < nb; ++i, iZ += dZ ) + int iZ2 = iZ1; + if ( Abs( zProj2 - zProj1 ) > std::numeric_limits::min() ) { - ip._point = findIntPoint( u1, zProj1, u2, zProj2, - planes._zProjs[ iZ ], - curve, planes._zNorm, _grid->_origin ); - _grid->ComputeUVW( ip._point.XYZ(), ip._uvw ); - locateValue( ijk[iDirX], ip._uvw[iDirX], _grid->_coords[iDirX], dIJK[iDirX], tol ); - locateValue( ijk[iDirY], ip._uvw[iDirY], _grid->_coords[iDirY], dIJK[iDirY], tol ); - ijk[ iDirZ ] = iZ; - - // add ip to hex "above" the plane - _grid->_edgeIntP.push_back( ip ); - dIJK[ iDirZ ] = 0; - bool added = addIntersection(_grid->_edgeIntP.back(), hexes, ijk, dIJK); - - // add ip to hex "below" the plane - ijk[ iDirZ ] = iZ-1; - if ( !addIntersection( _grid->_edgeIntP.back(), hexes, ijk, dIJK ) && - !added) - _grid->_edgeIntP.pop_back(); + locateValue( iZ2, zProj2, planes._zProjs, dIJK[ iDirZ ], tol ); + + // treat intersections with planes between 2 end points of a segment + int dZ = ( iZ1 <= iZ2 ) ? +1 : -1; + int iZ = iZ1 + ( iZ1 < iZ2 ); + for ( int i = 0, nb = Abs( iZ1 - iZ2 ); i < nb; ++i, iZ += dZ ) + { + ip._point = findIntPoint( u1, zProj1, u2, zProj2, + planes._zProjs[ iZ ], + curve, planes._zNorm, _grid->_origin ); + _grid->ComputeUVW( ip._point.XYZ(), ip._uvw ); + locateValue( ijk[iDirX], ip._uvw[iDirX], _grid->_coords[iDirX], dIJK[iDirX], tol ); + locateValue( ijk[iDirY], ip._uvw[iDirY], _grid->_coords[iDirY], dIJK[iDirY], tol ); + ijk[ iDirZ ] = iZ; + + // add ip to hex "above" the plane + _grid->_edgeIntP.push_back( ip ); + dIJK[ iDirZ ] = 0; + bool added = addIntersection(_grid->_edgeIntP.back(), hexes, ijk, dIJK); + + // add ip to hex "below" the plane + ijk[ iDirZ ] = iZ-1; + if ( !addIntersection( _grid->_edgeIntP.back(), hexes, ijk, dIJK ) && + !added) + _grid->_edgeIntP.pop_back(); + } } iZ1 = iZ2; p1 = p2; @@ -2442,6 +2551,7 @@ namespace // add the 2nd vertex point to a hexahedron if ( iDirZ == 0 ) { + ip._shapeID = _grid->_shapes.Add( v2 ); ip._point = p1; _grid->ComputeUVW( p1, ip._uvw ); locateValue( ijk[iDirX], ip._uvw[iDirX], _grid->_coords[iDirX], dIJK[iDirX], tol ); @@ -2450,32 +2560,11 @@ namespace _grid->_edgeIntP.push_back( ip ); if ( !addIntersection( _grid->_edgeIntP.back(), hexes, ijk, d000 )) _grid->_edgeIntP.pop_back(); + ip._shapeID = edgeID; } } // loop on 3 grid directions } // loop on EDGEs - // Create nodes at found intersections - // const E_IntersectPoint* eip; - // for ( size_t i = 0; i < hexes.size(); ++i ) - // { - // Hexahedron* h = hexes[i]; - // if ( !h ) continue; - // for ( int iF = 0; iF < 6; ++iF ) - // { - // _Face& quad = h->_hexQuads[ iF ]; - // for ( size_t iP = 0; iP < quad._edgeNodes.size(); ++iP ) - // if ( !quad._edgeNodes[ iP ]._node ) - // if (( eip = quad._edgeNodes[ iP ].EdgeIntPnt() )) - // quad._edgeNodes[ iP ]._intPoint->_node = helper.AddNode( eip->_point.X(), - // eip->_point.Y(), - // eip->_point.Z() ); - // } - // for ( size_t iP = 0; iP < hexes[i]->_vertexNodes.size(); ++iP ) - // if (( eip = h->_vertexNodes[ iP ].EdgeIntPnt() )) - // h->_vertexNodes[ iP ]._intPoint->_node = helper.AddNode( eip->_point.X(), - // eip->_point.Y(), - // eip->_point.Z() ); - // } } //================================================================================ @@ -2524,34 +2613,34 @@ namespace { enum { X = 1, Y = 2, Z = 4 }; // == 001, 010, 100 int nbFacets = 0; - int vertex = 0, egdeMask = 0; + int vertex = 0, edgeMask = 0; if ( Abs( _grid->_coords[0][ _i ] - ip->_uvw[0] ) < _grid->_tol ) { facets[ nbFacets++ ] = SMESH_Block::ID_F0yz; - egdeMask |= X; + edgeMask |= X; } else if ( Abs( _grid->_coords[0][ _i+1 ] - ip->_uvw[0] ) < _grid->_tol ) { facets[ nbFacets++ ] = SMESH_Block::ID_F1yz; vertex |= X; - egdeMask |= X; + edgeMask |= X; } if ( Abs( _grid->_coords[1][ _j ] - ip->_uvw[1] ) < _grid->_tol ) { facets[ nbFacets++ ] = SMESH_Block::ID_Fx0z; - egdeMask |= Y; + edgeMask |= Y; } else if ( Abs( _grid->_coords[1][ _j+1 ] - ip->_uvw[1] ) < _grid->_tol ) { facets[ nbFacets++ ] = SMESH_Block::ID_Fx1z; vertex |= Y; - egdeMask |= Y; + edgeMask |= Y; } if ( Abs( _grid->_coords[2][ _k ] - ip->_uvw[2] ) < _grid->_tol ) { facets[ nbFacets++ ] = SMESH_Block::ID_Fxy0; - egdeMask |= Z; + edgeMask |= Z; } else if ( Abs( _grid->_coords[2][ _k+1 ] - ip->_uvw[2] ) < _grid->_tol ) { facets[ nbFacets++ ] = SMESH_Block::ID_Fxy1; vertex |= Z; - egdeMask |= Z; + edgeMask |= Z; } switch ( nbFacets ) @@ -2567,7 +2656,7 @@ namespace { SMESH_Block::ID_Ex00, 0, SMESH_Block::ID_Ex10, 0, SMESH_Block::ID_Ex01, 0, SMESH_Block::ID_Ex11 } }; - switch ( egdeMask ) { + switch ( edgeMask ) { case X | Y: sub = edge[ 0 ][ vertex ]; break; case X | Z: sub = edge[ 1 ][ vertex ]; break; default: sub = edge[ 2 ][ vertex ]; @@ -2604,15 +2693,10 @@ namespace Hexahedron* h = hexes[ hexIndex[i] ]; // check if ip is really inside the hex #ifdef _DEBUG_ - if (( _grid->_coords[0][ h->_i ] - _grid->_tol > ip._uvw[0] ) || - ( _grid->_coords[0][ h->_i+1 ] + _grid->_tol < ip._uvw[0] ) || - ( _grid->_coords[1][ h->_j ] - _grid->_tol > ip._uvw[1] ) || - ( _grid->_coords[1][ h->_j+1 ] + _grid->_tol < ip._uvw[1] ) || - ( _grid->_coords[2][ h->_k ] - _grid->_tol > ip._uvw[2] ) || - ( _grid->_coords[2][ h->_k+1 ] + _grid->_tol < ip._uvw[2] )) + if ( h->isOutParam( ip._uvw )) throw SALOME_Exception("ip outside a hex"); #endif - h->_edgeIntPnts.push_back( & ip ); + h->_eIntPoints.push_back( & ip ); added = true; } } @@ -2629,26 +2713,26 @@ namespace { chn.clear(); chn.push_back( n1 ); - for ( size_t iP = 0; iP < quad._edgeNodes.size(); ++iP ) - if ( !quad._edgeNodes[ iP ]._isUsedInFace && - n1->IsLinked( quad._edgeNodes[ iP ]._intPoint ) && - n2->IsLinked( quad._edgeNodes[ iP ]._intPoint )) + for ( size_t iP = 0; iP < quad._eIntNodes.size(); ++iP ) + if ( !quad._eIntNodes[ iP ]->IsUsedInFace( &quad ) && + n1->IsLinked( quad._eIntNodes[ iP ]->_intPoint ) && + n2->IsLinked( quad._eIntNodes[ iP ]->_intPoint )) { - chn.push_back( & quad._edgeNodes[ iP ]); + chn.push_back( quad._eIntNodes[ iP ]); chn.push_back( n2 ); - quad._edgeNodes[ iP ]._isUsedInFace = true; + quad._eIntNodes[ iP ]->_usedInFace = &quad; return true; } bool found; do { found = false; - for ( size_t iP = 0; iP < quad._edgeNodes.size(); ++iP ) - if ( !quad._edgeNodes[ iP ]._isUsedInFace && - chn.back()->IsLinked( quad._edgeNodes[ iP ]._intPoint )) + for ( size_t iP = 0; iP < quad._eIntNodes.size(); ++iP ) + if ( !quad._eIntNodes[ iP ]->IsUsedInFace( &quad ) && + chn.back()->IsLinked( quad._eIntNodes[ iP ]->_intPoint )) { - chn.push_back( & quad._edgeNodes[ iP ]); - found = quad._edgeNodes[ iP ]._isUsedInFace = true; + chn.push_back( quad._eIntNodes[ iP ]); + found = ( quad._eIntNodes[ iP ]->_usedInFace = &quad ); break; } } while ( found && ! chn.back()->IsLinked( n2->_intPoint ) ); @@ -2703,50 +2787,298 @@ namespace } //================================================================================ /*! - * \brief Checks transition at the 1st node of a link + * \brief Finds nodes on the same EDGE as the first node of avoidSplit. + * + * This function is for a case where an EDGE lies on a quad which lies on a FACE + * so that a part of quad in ON and another part in IN */ - bool Hexahedron::is1stNodeOut( int iLink ) const + bool Hexahedron::findChainOnEdge( const vector< _OrientedLink >& splits, + const _OrientedLink& prevSplit, + const _OrientedLink& avoidSplit, + size_t & iS, + _Face& quad, + vector<_Node*>& chn ) { - if ( !_hexLinks[ iLink ]._nodes[0]->Node() ) // no node - return true; - if ( !_hexLinks[ iLink ]._nodes[0]->_intPoint ) // no intersection with geometry + if ( !isImplementEdges() ) + return false; + + _Node* pn1 = prevSplit.FirstNode(); + _Node* pn2 = prevSplit.LastNode(); + int avoidFace = pn1->IsLinked( pn2->_intPoint ); // FACE under the quad + if ( avoidFace < 1 && pn1->_intPoint ) return false; - switch ( _hexLinks[ iLink ]._nodes[0]->FaceIntPnt()->_transition ) { - case Trans_OUT: return true; - case Trans_IN : return false; - default: ; // tangent transition - } - - // ijk of a GridLine corresponding to the link - int iDir = iLink / 4; - int indSub = iLink % 4; - LineIndexer li = _grid->GetLineIndexer( iDir ); - li.SetIJK( _i,_j,_k ); - size_t lineIndex[4] = { li.LineIndex (), - li.LineIndex10(), - li.LineIndex01(), - li.LineIndex11() }; - GridLine& line = _grid->_lines[ iDir ][ lineIndex[ indSub ]]; - - // analyze transition of previous ip - bool isOut = true; - multiset< F_IntersectPoint >::const_iterator ip = line._intPoints.begin(); - for ( ; ip != line._intPoints.end(); ++ip ) - { - if ( &(*ip) == _hexLinks[ iLink ]._nodes[0]->_intPoint ) + + _Node* n, *stopNode = avoidSplit.LastNode(); + + chn.clear(); + if ( !quad._eIntNodes.empty() ) + { + chn.push_back( pn2 ); + bool found; + do + { + found = false; + for ( size_t iP = 0; iP < quad._eIntNodes.size(); ++iP ) + if (( !quad._eIntNodes[ iP ]->IsUsedInFace( &quad )) && + ( chn.back()->IsLinked( quad._eIntNodes[ iP ]->_intPoint, avoidFace )) && + ( !avoidFace || quad._eIntNodes[ iP ]->IsOnFace( avoidFace ))) + { + chn.push_back( quad._eIntNodes[ iP ]); + found = ( quad._eIntNodes[ iP ]->_usedInFace = &quad ); + break; + } + } while ( found ); + pn2 = chn.back(); + } + + int i; + for ( i = splits.size()-1; i >= 0; --i ) + { + if ( !splits[i] ) + continue; + + n = splits[i].LastNode(); + if ( n == stopNode ) break; - switch ( ip->_transition ) { - case Trans_OUT: isOut = true; - case Trans_IN : isOut = false; - default:; + if (( n != pn1 ) && + ( n->IsLinked( pn2->_intPoint, avoidFace )) && + ( !avoidFace || n->IsOnFace( avoidFace ))) + break; + + n = splits[i].FirstNode(); + if ( n == stopNode ) + break; + if (( n->IsLinked( pn2->_intPoint, avoidFace )) && + ( !avoidFace || n->IsOnFace( avoidFace ))) + break; + n = 0; + } + if ( n && n != stopNode) + { + if ( chn.empty() ) + chn.push_back( pn2 ); + chn.push_back( n ); + iS = i-1; + return true; + } + return false; + } + //================================================================================ + /*! + * \brief Checks transition at the ginen intersection node of a link + */ + bool Hexahedron::isOutPoint( _Link& link, int iP, SMESH_MesherHelper& helper ) const + { + bool isOut = false; + + const bool moreIntPoints = ( iP+1 < (int) link._fIntPoints.size() ); + + // get 2 _Node's + _Node* n1 = link._fIntNodes[ iP ]; + if ( !n1->Node() ) + n1 = link._nodes[0]; + _Node* n2 = moreIntPoints ? link._fIntNodes[ iP+1 ] : 0; + if ( !n2 || !n2->Node() ) + n2 = link._nodes[1]; + if ( !n2->Node() ) + return true; + + // get all FACEs under n1 and n2 + set< TGeomID > faceIDs; + if ( moreIntPoints ) faceIDs.insert( link._fIntPoints[iP+1]->_faceIDs.begin(), + link._fIntPoints[iP+1]->_faceIDs.end() ); + if ( n2->_intPoint ) faceIDs.insert( n2->_intPoint->_faceIDs.begin(), + n2->_intPoint->_faceIDs.end() ); + if ( faceIDs.empty() ) + return false; // n2 is inside + if ( n1->_intPoint ) faceIDs.insert( n1->_intPoint->_faceIDs.begin(), + n1->_intPoint->_faceIDs.end() ); + faceIDs.insert( link._fIntPoints[iP]->_faceIDs.begin(), + link._fIntPoints[iP]->_faceIDs.end() ); + + // get a point between 2 nodes + gp_Pnt p1 = n1->Point(); + gp_Pnt p2 = n2->Point(); + gp_Pnt pOnLink = 0.8 * p1.XYZ() + 0.2 * p2.XYZ(); + + TopLoc_Location loc; + + set< TGeomID >::iterator faceID = faceIDs.begin(); + for ( ; faceID != faceIDs.end(); ++faceID ) + { + // project pOnLink on a FACE + if ( *faceID < 1 ) continue; + const TopoDS_Face& face = TopoDS::Face( _grid->_shapes( *faceID )); + GeomAPI_ProjectPointOnSurf& proj = + helper.GetProjector( face, loc, 0.1*_grid->_tol ); + gp_Pnt testPnt = pOnLink.Transformed( loc.Transformation().Inverted() ); + proj.Perform( testPnt ); + if ( proj.IsDone() && proj.NbPoints() > 0 ) + { + Standard_Real u,v; + proj.LowerDistanceParameters( u,v ); + + if ( proj.LowerDistance() <= 0.1 * _grid->_tol ) + { + isOut = false; + } + else + { + // find isOut by normals + gp_Dir normal; + if ( GeomLib::NormEstim( BRep_Tool::Surface( face, loc ), + gp_Pnt2d( u,v ), + 0.1*_grid->_tol, + normal ) < 3 ) + { + if ( face.Orientation() == TopAbs_REVERSED ) + normal.Reverse(); + gp_Vec v( proj.NearestPoint(), testPnt ); + isOut = ( v * normal > 0 ); + } + } + if ( !isOut ) + { + // classify a projection + if ( !n1->IsOnFace( *faceID ) || !n2->IsOnFace( *faceID )) + { + BRepTopAdaptor_FClass2d cls( face, Precision::Confusion() ); + TopAbs_State state = cls.Perform( gp_Pnt2d( u,v )); + if ( state == TopAbs_OUT ) + { + isOut = true; + continue; + } + } + return false; + } } } -#ifdef _DEBUG_ - if ( ip == line._intPoints.end() ) - cout << "BUG: Wrong GridLine. IKJ = ( "<< _i << " " << _j << " " << _k << " )" << endl; -#endif return isOut; } + //================================================================================ + /*! + * \brief Sort nodes on a FACE + */ + void Hexahedron::sortVertexNodes(vector<_Node*>& nodes, _Node* curNode, TGeomID faceID) + { + if ( nodes.size() > 20 ) return; + + // get shapes under nodes + TGeomID nShapeIds[20], *nShapeIdsEnd = &nShapeIds[0] + nodes.size(); + for ( size_t i = 0; i < nodes.size(); ++i ) + if ( !( nShapeIds[i] = nodes[i]->ShapeID() )) + return; + + // get shapes of the FACE + const TopoDS_Face& face = TopoDS::Face( _grid->_shapes( faceID )); + list< TopoDS_Edge > edges; + list< int > nbEdges; + int nbW = SMESH_Block::GetOrderedEdges (face, edges, nbEdges); + if ( nbW > 1 ) { + // select a WIRE - remove EDGEs of irrelevant WIREs from edges + list< TopoDS_Edge >::iterator e = edges.begin(), eEnd = e; + list< int >::iterator nE = nbEdges.begin(); + for ( ; nbW > 0; ++nE, --nbW ) + { + std::advance( eEnd, *nE ); + for ( ; e != eEnd; ++e ) + for ( int i = 0; i < 2; ++i ) + { + TGeomID id = i==0 ? + _grid->_shapes.FindIndex( *e ) : + _grid->_shapes.FindIndex( SMESH_MesherHelper::IthVertex( 0, *e )); + if (( id > 0 ) && + ( std::find( &nShapeIds[0], nShapeIdsEnd, id ) != nShapeIdsEnd )) + { + edges.erase( eEnd, edges.end() ); // remove rest wires + e = eEnd = edges.end(); + --e; + nbW = 0; + break; + } + } + if ( nbW > 0 ) + edges.erase( edges.begin(), eEnd ); // remove a current irrelevant wire + } + } + // rotate edges to have the first one at least partially out of the hexa + list< TopoDS_Edge >::iterator e = edges.begin(), eMidOut = edges.end(); + for ( ; e != edges.end(); ++e ) + { + if ( !_grid->_shapes.FindIndex( *e )) + continue; + bool isOut = false; + gp_Pnt p; + double uvw[3], f,l; + for ( int i = 0; i < 2 && !isOut; ++i ) + { + if ( i == 0 ) + { + TopoDS_Vertex v = SMESH_MesherHelper::IthVertex( 0, *e ); + p = BRep_Tool::Pnt( v ); + } + else if ( eMidOut == edges.end() ) + { + TopLoc_Location loc; + Handle(Geom_Curve) c = BRep_Tool::Curve( *e, loc, f, l); + if ( c.IsNull() ) break; + p = c->Value( 0.5 * ( f + l )).Transformed( loc ); + } + else + { + continue; + } + + _grid->ComputeUVW( p.XYZ(), uvw ); + if ( isOutParam( uvw )) + { + if ( i == 0 ) + isOut = true; + else + eMidOut = e; + } + } + if ( isOut ) + break; + } + if ( e != edges.end() ) + edges.splice( edges.end(), edges, edges.begin(), e ); + else if ( eMidOut != edges.end() ) + edges.splice( edges.end(), edges, edges.begin(), eMidOut ); + + // sort nodes according to the order of edges + _Node* orderNodes [20]; + //TGeomID orderShapeIDs[20]; + size_t nbN = 0; + TGeomID id, *pID = 0; + for ( e = edges.begin(); e != edges.end(); ++e ) + { + if (( id = _grid->_shapes.FindIndex( SMESH_MesherHelper::IthVertex( 0, *e ))) && + (( pID = std::find( &nShapeIds[0], nShapeIdsEnd, id )) != nShapeIdsEnd )) + { + //orderShapeIDs[ nbN ] = id; + orderNodes [ nbN++ ] = nodes[ pID - &nShapeIds[0] ]; + *pID = -1; + } + if (( id = _grid->_shapes.FindIndex( *e )) && + (( pID = std::find( &nShapeIds[0], nShapeIdsEnd, id )) != nShapeIdsEnd )) + { + //orderShapeIDs[ nbN ] = id; + orderNodes [ nbN++ ] = nodes[ pID - &nShapeIds[0] ]; + *pID = -1; + } + } + if ( nbN != nodes.size() ) + return; + + bool reverse = ( orderNodes[0 ]->Point().SquareDistance( curNode->Point() ) > + orderNodes[nbN-1]->Point().SquareDistance( curNode->Point() )); + + for ( size_t i = 0; i < nodes.size(); ++i ) + nodes[ i ] = orderNodes[ reverse ? nbN-1-i : i ]; + } + //================================================================================ /*! * \brief Adds computed elements to the mesh @@ -2801,10 +3133,10 @@ namespace */ bool Hexahedron::isInHole() const { - if ( !_vertexNodes.empty() ) + if ( !_vIntNodes.empty() ) return false; - const int ijk[3] = { _i, _j, _k }; + const size_t ijk[3] = { _i, _j, _k }; F_IntersectPoint curIntPnt; // consider a cell to be in a hole if all links in any direction @@ -2826,16 +3158,16 @@ namespace const F_IntersectPoint* firstIntPnt = 0; if ( link._nodes[0]->Node() ) // 1st node is a hexa corner { - curIntPnt._paramOnLine = coords[ ijk[ iDir ]] - coords[0]; + curIntPnt._paramOnLine = coords[ ijk[ iDir ]] - coords[0] + _grid->_tol; const GridLine& line = _grid->_lines[ iDir ][ lineIndex[ iL ]]; multiset< F_IntersectPoint >::const_iterator ip = line._intPoints.upper_bound( curIntPnt ); --ip; firstIntPnt = &(*ip); } - else if ( !link._intNodes.empty() ) + else if ( !link._fIntPoints.empty() ) { - firstIntPnt = link._intNodes[0].FaceIntPnt(); + firstIntPnt = link._fIntPoints[0]; } if ( firstIntPnt ) @@ -2860,6 +3192,8 @@ namespace for ( size_t iP = 0; iP < _polygons.size(); ++iP ) { const _Face& polygon = _polygons[iP]; + if ( polygon._links.empty() ) + continue; gp_XYZ area (0,0,0); gp_XYZ p1 = polygon._links[ 0 ].FirstNode()->Point().XYZ(); for ( size_t iL = 0; iL < polygon._links.size(); ++iL ) @@ -2882,28 +3216,34 @@ namespace */ bool Hexahedron::addHexa() { - if ( _polygons[0]._links.size() != 4 || - _polygons[1]._links.size() != 4 || - _polygons[2]._links.size() != 4 || - _polygons[3]._links.size() != 4 || - _polygons[4]._links.size() != 4 || - _polygons[5]._links.size() != 4 ) + int nbQuad = 0, iQuad = -1; + for ( size_t i = 0; i < _polygons.size(); ++i ) + { + if ( _polygons[i]._links.empty() ) + continue; + if ( _polygons[i]._links.size() != 4 ) + return false; + ++nbQuad; + if ( iQuad < 0 ) + iQuad = i; + } + if ( nbQuad != 6 ) return false; + _Node* nodes[8]; int nbN = 0; for ( int iL = 0; iL < 4; ++iL ) { // a base node - nodes[iL] = _polygons[0]._links[iL].FirstNode(); + nodes[iL] = _polygons[iQuad]._links[iL].FirstNode(); ++nbN; // find a top node above the base node - _Link* link = _polygons[0]._links[iL]._link; - //ASSERT( link->_faces.size() > 1 ); - if ( link->_faces.size() < 2 ) + _Link* link = _polygons[iQuad]._links[iL]._link; + if ( !link->_faces[0] || !link->_faces[1] ) return debugDumpLink( link ); - // a quadrangle sharing with _polygons[0] - _Face* quad = link->_faces[ bool( link->_faces[0] == & _polygons[0] )]; + // a quadrangle sharing with _polygons[iQuad] + _Face* quad = link->_faces[ bool( link->_faces[0] == & _polygons[iQuad] )]; for ( int i = 0; i < 4; ++i ) if ( quad->_links[i]._link == link ) { @@ -2914,7 +3254,7 @@ namespace } } if ( nbN == 8 ) - _volumeDefs.set( vector< _Node* >( nodes, nodes+8 )); + _volumeDefs.set( &nodes[0], 8 ); return nbN == 8; } @@ -2924,23 +3264,29 @@ namespace */ bool Hexahedron::addTetra() { + int iTria = -1; + for ( size_t i = 0; i < _polygons.size() && iTria < 0; ++i ) + if ( _polygons[i]._links.size() == 3 ) + iTria = i; + if ( iTria < 0 ) + return false; + _Node* nodes[4]; - nodes[0] = _polygons[0]._links[0].FirstNode(); - nodes[1] = _polygons[0]._links[1].FirstNode(); - nodes[2] = _polygons[0]._links[2].FirstNode(); + nodes[0] = _polygons[iTria]._links[0].FirstNode(); + nodes[1] = _polygons[iTria]._links[1].FirstNode(); + nodes[2] = _polygons[iTria]._links[2].FirstNode(); - _Link* link = _polygons[0]._links[0]._link; - //ASSERT( link->_faces.size() > 1 ); - if ( link->_faces.size() < 2 ) + _Link* link = _polygons[iTria]._links[0]._link; + if ( !link->_faces[0] || !link->_faces[1] ) return debugDumpLink( link ); // a triangle sharing with _polygons[0] - _Face* tria = link->_faces[ bool( link->_faces[0] == & _polygons[0] )]; + _Face* tria = link->_faces[ bool( link->_faces[0] == & _polygons[iTria] )]; for ( int i = 0; i < 3; ++i ) if ( tria->_links[i]._link == link ) { nodes[3] = tria->_links[(i+1)%3].LastNode(); - _volumeDefs.set( vector< _Node* >( nodes, nodes+4 )); + _volumeDefs.set( &nodes[0], 4 ); return true; } @@ -2970,8 +3316,7 @@ namespace // find a top node above the base node _Link* link = _polygons[ iTri ]._links[iL]._link; - //ASSERT( link->_faces.size() > 1 ); - if ( link->_faces.size() < 2 ) + if ( !link->_faces[0] || !link->_faces[1] ) return debugDumpLink( link ); // a quadrangle sharing with a base triangle _Face* quad = link->_faces[ bool( link->_faces[0] == & _polygons[ iTri ] )]; @@ -2986,7 +3331,7 @@ namespace } } if ( nbN == 6 ) - _volumeDefs.set( vector< _Node* >( nodes, nodes+6 )); + _volumeDefs.set( &nodes[0], 6 ); return ( nbN == 6 ); } @@ -3011,8 +3356,7 @@ namespace nodes[3] = _polygons[iQuad]._links[3].FirstNode(); _Link* link = _polygons[iQuad]._links[0]._link; - ASSERT( link->_faces.size() > 1 ); - if ( link->_faces.size() < 2 ) + if ( !link->_faces[0] || !link->_faces[1] ) return debugDumpLink( link ); // a triangle sharing with a base quadrangle @@ -3022,7 +3366,7 @@ namespace if ( tria->_links[i]._link == link ) { nodes[4] = tria->_links[(i+1)%3].LastNode(); - _volumeDefs.set( vector< _Node* >( nodes, nodes+5 )); + _volumeDefs.set( &nodes[0], 5 ); return true; } @@ -3042,6 +3386,19 @@ namespace #endif return false; } + //================================================================================ + /*! + * \brief Classify a point by grid parameters + */ + bool Hexahedron::isOutParam(const double uvw[3]) const + { + return (( _grid->_coords[0][ _i ] - _grid->_tol > uvw[0] ) || + ( _grid->_coords[0][ _i+1 ] + _grid->_tol < uvw[0] ) || + ( _grid->_coords[1][ _j ] - _grid->_tol > uvw[1] ) || + ( _grid->_coords[1][ _j+1 ] + _grid->_tol < uvw[1] ) || + ( _grid->_coords[2][ _k ] - _grid->_tol > uvw[2] ) || + ( _grid->_coords[2][ _k+1 ] + _grid->_tol < uvw[2] )); + } //================================================================================ /*! @@ -3162,6 +3519,7 @@ bool StdMeshers_Cartesian_3D::Compute(SMESH_Mesh & theMesh, try { Grid grid; + grid._helper = &helper; vector< TopoDS_Shape > faceVec; { @@ -3179,7 +3537,7 @@ bool StdMeshers_Cartesian_3D::Compute(SMESH_Mesh & theMesh, map< TGeomID, vector< TGeomID > > edge2faceIDsMap; TopExp_Explorer eExp; Bnd_Box shapeBox; - for ( int i = 0; i < faceVec.size(); ++i ) + for ( size_t i = 0; i < faceVec.size(); ++i ) { facesItersectors[i]._face = TopoDS::Face ( faceVec[i] ); facesItersectors[i]._faceID = grid._shapes.Add( faceVec[i] ); @@ -3230,7 +3588,7 @@ bool StdMeshers_Cartesian_3D::Compute(SMESH_Mesh & theMesh, facesItersectors[i].Intersect(); #endif - // put interesection points onto the GridLine's; this is done after intersection + // put intersection points onto the GridLine's; this is done after intersection // to avoid contention of facesItersectors for writing into the same GridLine // in case of parallel work of facesItersectors for ( size_t i = 0; i < facesItersectors.size(); ++i ) @@ -3261,7 +3619,7 @@ bool StdMeshers_Cartesian_3D::Compute(SMESH_Mesh & theMesh, for ( ; solidExp.More() && volIt->more(); solidExp.Next() ) { const SMDS_MeshElement* vol = volIt->next(); - sm1->RemoveElement( vol, /*isElemDeleted=*/false ); + sm1->RemoveElement( vol ); meshDS->SetMeshElementOnShape( vol, solidExp.Current() ); } }