From: eap Date: Fri, 14 Aug 2015 17:14:27 +0000 (+0300) Subject: 52780: Wrong mesh after STL re-import of a concave polygonal element X-Git-Tag: V7_7_0a1~10 X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=commitdiff_plain;h=741a54331a691355c38e17548fbc14475cfe9b6b 52780: Wrong mesh after STL re-import of a concave polygonal element --- diff --git a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx index 78a068e57..cd33b0253 100644 --- a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx +++ b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx @@ -33,12 +33,15 @@ #include "SMDS_Mesh.hxx" #include "SMDS_MeshElement.hxx" #include "SMDS_MeshNode.hxx" +#include "SMDS_PolygonalFaceOfNodes.hxx" #include "SMDS_SetIterator.hxx" #include "SMDS_VolumeTool.hxx" #include "SMESH_File.hxx" #include "SMESH_TypeDefs.hxx" -//#include "utilities.h" +#include +#include +#include #include @@ -74,6 +77,7 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform() return aResult; } + //================================================================================ /*! * \brief Destructor deletes temporary faces @@ -82,8 +86,8 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform() DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh() { - for ( unsigned i = 0; i < myVolumeTrias.size(); ++i ) - delete myVolumeTrias[i]; + for ( unsigned i = 0; i < myVolumeFacets.size(); ++i ) + delete myVolumeFacets[i]; } //================================================================================ @@ -94,6 +98,8 @@ DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh() void DriverSTL_W_SMDS_Mesh::findVolumeTriangles() { + myNbVolumeTrias = 0; + SMDS_VolumeTool theVolume; SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator(); std::vector< const SMDS_MeshNode*> nodes; @@ -106,19 +112,20 @@ void DriverSTL_W_SMDS_Mesh::findVolumeTriangles() const SMDS_MeshNode** n = theVolume.GetFaceNodes(iF); int nbN = theVolume.NbFaceNodes(iF); nodes.assign( n, n+nbN ); - if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false)) + if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*noMedium=*/false)) { - if ( nbN == 9 && !theVolume.IsPoly() ) // facet is SMDSEntity_BiQuad_Quadrangle + if (( nbN == 9 || nbN == 7 ) && + ( !theVolume.IsPoly() )) // facet is bi-quaratic { int nbTria = nbN - 1; for ( int iT = 0; iT < nbTria; ++iT ) - myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[8], n[0+iT], n[1+iT] )); + myVolumeFacets.push_back( new SMDS_FaceOfNodes( n[8], n[0+iT], n[1+iT] )); + myNbVolumeTrias += nbTria; } else { - int nbTria = nbN - 2; - for ( int iT = 0; iT < nbTria; ++iT ) - myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[0], n[1+iT], n[2+iT] )); + myVolumeFacets.push_back( new SMDS_PolygonalFaceOfNodes( nodes )); + myNbVolumeTrias += nbN - 2; } } } @@ -134,8 +141,8 @@ void DriverSTL_W_SMDS_Mesh::findVolumeTriangles() SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const { SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face); - SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeTrias.begin(), - myVolumeTrias.end())); + SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeFacets.begin(), + myVolumeFacets.end())); typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector; TElemIterVector iters(2); iters[0] = facesIter; @@ -201,6 +208,141 @@ static gp_XYZ getNormale( const SMDS_MeshNode* n1, return n; } +namespace +{ + /*! + * \brief Vertex of a polygon. Together with 2 neighbor Vertices represents a triangle + */ + struct PolyVertex + { + SMESH_TNodeXYZ _nxyz; + gp_XY _xy; + PolyVertex* _prev; + PolyVertex* _next; + + void SetNodeAndNext( const SMDS_MeshNode* n, PolyVertex& v ) + { + _nxyz.Set( n ); + _next = &v; + v._prev = this; + } + PolyVertex* Delete() + { + _prev->_next = _next; + _next->_prev = _prev; + return _next; + } + void GetTriaNodes( const SMDS_MeshNode** nodes) const + { + nodes[0] = _prev->_nxyz._node; + nodes[1] = this->_nxyz._node; + nodes[2] = _next->_nxyz._node; + } + + inline static double Area( const PolyVertex* v0, const PolyVertex* v1, const PolyVertex* v2 ) + { + gp_XY vPrev = v0->_xy - v1->_xy; + gp_XY vNext = v2->_xy - v1->_xy; + return vNext ^ vPrev; + } + double TriaArea() const { return Area( _prev, this, _next ); } + + bool IsInsideTria( const PolyVertex* v ) + { + gp_XY p = _prev->_xy - v->_xy; + gp_XY t = this->_xy - v->_xy; + gp_XY n = _next->_xy - v->_xy; + return (( p ^ t ) > 0 && + ( t ^ n ) > 0 && + ( n ^ p ) > 0 ); + // return ( Area( _prev, this, v ) > 0 && + // Area( this, _next, v ) > 0 && + // Area( _next, _prev, v ) > 0 ); + } + }; + + //================================================================================ + /*! + * \brief Triangulate a polygon. Assure correct orientation for concave polygons + */ + //================================================================================ + + bool triangulate( std::vector< const SMDS_MeshNode*>& nodes, const size_t nbNodes ) + { + // connect nodes into a ring + std::vector< PolyVertex > pv( nbNodes ); + for ( size_t i = 1; i < nbNodes; ++i ) + pv[i-1].SetNodeAndNext( nodes[i-1], pv[i] ); + pv[ nbNodes-1 ].SetNodeAndNext( nodes[ nbNodes-1 ], pv[0] ); + + // get a polygon normal + gp_XYZ normal(0,0,0), p0,v01,v02; + p0 = pv[0]._nxyz; + v01 = pv[1]._nxyz - p0; + for ( size_t i = 2; i < nbNodes; ++i ) + { + v02 = pv[i]._nxyz - p0; + normal += v01 ^ v02; + v01 = v02; + } + // project nodes to the found plane + gp_Ax2 axes; + try { + axes = gp_Ax2( p0, normal, v01 ); + } + catch ( Standard_Failure ) { + return false; + } + for ( size_t i = 0; i < nbNodes; ++i ) + { + gp_XYZ p = pv[i]._nxyz - p0; + pv[i]._xy.SetX( axes.XDirection().XYZ() * p ); + pv[i]._xy.SetY( axes.YDirection().XYZ() * p ); + } + + // in a loop, find triangles with positive area and having no vertices inside + int iN = 0, nbTria = nbNodes - 2; + nodes.reserve( nbTria * 3 ); + const double minArea = 1e-6; + PolyVertex* v = &pv[0], *vi; + int nbVertices = nbNodes, nbBadTria = 0, isGoodTria; + while ( nbBadTria < nbVertices ) + { + if (( isGoodTria = v->TriaArea() > minArea )) + { + for ( vi = v->_next->_next; + vi != v->_prev; + vi = vi->_next ) + { + if ( v->IsInsideTria( vi )) + break; + } + isGoodTria = ( vi == v->_prev ); + } + if ( isGoodTria ) + { + v->GetTriaNodes( &nodes[ iN ] ); + iN += 3; + v = v->Delete(); + if ( --nbVertices == 3 ) + { + // last triangle remains + v->GetTriaNodes( &nodes[ iN ] ); + return true; + } + nbBadTria = 0; + } + else + { + v = v->_next; + ++nbBadTria; + } + } + return false; + + } // triangulate() +} // namespace + //================================================================================ /*! * \brief Return nb triangles in a decomposed mesh face @@ -235,12 +377,13 @@ static int getNbTriangles( const SMDS_MeshElement* face) */ //================================================================================ -static int getTriangles( const SMDS_MeshElement* face, - const SMDS_MeshNode** nodes) +static int getTriangles( const SMDS_MeshElement* face, + std::vector< const SMDS_MeshNode*>& nodes) { // WARNING: decomposing into triangles must be coherent with getNbTriangles() - int nbTria, i = 0; + int nbTria, i = 0, nbNodes = face->NbNodes(); SMDS_NodeIteratorPtr nIt = face->interlacedNodesIterator(); + nodes.resize( nbNodes * 3 ); nodes[ i++ ] = nIt->next(); nodes[ i++ ] = nIt->next(); @@ -261,20 +404,32 @@ static int getTriangles( const SMDS_MeshElement* face, nodes[ i++ ] = nodes[ 0 ]; nodes[ i++ ] = nodes[ 2 ]; break; + case SMDSEntity_Triangle: + nbTria = 1; + nodes[ i++ ] = nIt->next(); + break; default: - // case SMDSEntity_Triangle: // case SMDSEntity_Quad_Triangle: // case SMDSEntity_Quadrangle: // case SMDSEntity_Quad_Quadrangle: // case SMDSEntity_Polygon: // case SMDSEntity_Quad_Polygon: - nbTria = face->NbNodes() - 2; - nodes[ i++ ] = nIt->next(); - while ( i < 3*nbTria ) - { - nodes[ i++ ] = nodes[ 0 ]; - nodes[ i++ ] = nodes[ i-2 ]; + nbTria = nbNodes - 2; + while ( nIt->more() ) nodes[ i++ ] = nIt->next(); + + if ( !triangulate( nodes, nbNodes )) + { + nIt = face->interlacedNodesIterator(); + nodes[ 0 ] = nIt->next(); + nodes[ 1 ] = nIt->next(); + nodes[ 2 ] = nIt->next(); + for ( i = 3; i < 3*nbTria; i += 3 ) + { + nodes[ i+0 ] = nodes[ 0 ]; + nodes[ i+1 ] = nodes[ i-1 ]; + nodes[ i+2 ] = nIt->next(); + } } break; } @@ -298,7 +453,7 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const aFile.writeRaw( buf.c_str(), buf.size() ); char sval[128]; - const SMDS_MeshNode* triaNodes[2048]; + std::vector< const SMDS_MeshNode* > triaNodes; SMDS_ElemIteratorPtr itFaces = getFaces(); while ( itFaces->more() ) @@ -354,7 +509,7 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const aFile.openForWriting(); // we first count the number of triangles - int nbTri = myVolumeTrias.size(); + int nbTri = myNbVolumeTrias; { SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator(); while ( itFaces->more() ) { @@ -372,7 +527,7 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const int dum=0; - const SMDS_MeshNode* triaNodes[2048]; + std::vector< const SMDS_MeshNode* > triaNodes; SMDS_ElemIteratorPtr itFaces = getFaces(); while ( itFaces->more() ) diff --git a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.h b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.h index d5fbabfa4..472610f0b 100644 --- a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.h +++ b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.h @@ -57,7 +57,8 @@ class MESHDRIVERSTL_EXPORT DriverSTL_W_SMDS_Mesh: public Driver_SMDS_Mesh private: // PRIVATE FIELDS bool myIsAscii; - std::vector myVolumeTrias; // tmp triangles + int myNbVolumeTrias; + std::vector myVolumeFacets; // tmp faces }; #endif diff --git a/src/SMDS/SMDS_PolygonalFaceOfNodes.cxx b/src/SMDS/SMDS_PolygonalFaceOfNodes.cxx index 5bc6aa1e9..f291b0bff 100644 --- a/src/SMDS/SMDS_PolygonalFaceOfNodes.cxx +++ b/src/SMDS/SMDS_PolygonalFaceOfNodes.cxx @@ -41,7 +41,7 @@ using namespace std; //purpose : //======================================================================= SMDS_PolygonalFaceOfNodes::SMDS_PolygonalFaceOfNodes - (std::vector nodes) + (const std::vector& nodes) { //MESSAGE("******************************************** SMDS_PolygonalFaceOfNodes"); myNodes = nodes; diff --git a/src/SMDS/SMDS_PolygonalFaceOfNodes.hxx b/src/SMDS/SMDS_PolygonalFaceOfNodes.hxx index ba15f67ac..bf6a1a5ff 100644 --- a/src/SMDS/SMDS_PolygonalFaceOfNodes.hxx +++ b/src/SMDS/SMDS_PolygonalFaceOfNodes.hxx @@ -38,7 +38,7 @@ class SMDS_EXPORT SMDS_PolygonalFaceOfNodes:public SMDS_MeshFace { public: - SMDS_PolygonalFaceOfNodes (std::vector nodes); + SMDS_PolygonalFaceOfNodes (const std::vector& nodes); virtual SMDSAbs_ElementType GetType() const; virtual SMDSAbs_EntityType GetEntityType() const { return SMDSEntity_Polygon; } diff --git a/src/SMESHUtils/SMESH_MeshAlgos.cxx b/src/SMESHUtils/SMESH_MeshAlgos.cxx index f3face96a..a16379b00 100644 --- a/src/SMESHUtils/SMESH_MeshAlgos.cxx +++ b/src/SMESHUtils/SMESH_MeshAlgos.cxx @@ -1599,7 +1599,7 @@ bool SMESH_MeshAlgos::FaceNormal(const SMDS_MeshElement* F, gp_XYZ& normal, bool return false; normal.SetCoord(0,0,0); - int nbNodes = F->IsQuadratic() ? F->NbNodes()/2 : F->NbNodes(); + int nbNodes = F->NbCornerNodes(); for ( int i = 0; i < nbNodes-2; ++i ) { gp_XYZ p[3]; diff --git a/src/SMESHUtils/SMESH_TypeDefs.hxx b/src/SMESHUtils/SMESH_TypeDefs.hxx index f5c44e79a..afef61b90 100644 --- a/src/SMESHUtils/SMESH_TypeDefs.hxx +++ b/src/SMESHUtils/SMESH_TypeDefs.hxx @@ -137,13 +137,20 @@ struct SMESH_TNodeXYZ : public gp_XYZ { const SMDS_MeshNode* _node; double _xyz[3]; - SMESH_TNodeXYZ( const SMDS_MeshElement* e=0):gp_XYZ(0,0,0),_node(0) { + SMESH_TNodeXYZ( const SMDS_MeshElement* e=0):gp_XYZ(0,0,0),_node(0) + { + Set(e); + } + bool Set( const SMDS_MeshElement* e=0 ) + { if (e) { assert( e->GetType() == SMDSAbs_Node ); _node = static_cast(e); _node->GetXYZ(_xyz); // - thread safe getting coords SetCoord( _xyz[0], _xyz[1], _xyz[2] ); + return true; } + return false; } double Distance(const SMDS_MeshNode* n) const { return (SMESH_TNodeXYZ( n )-*this).Modulus(); } double SquareDistance(const SMDS_MeshNode* n) const { return (SMESH_TNodeXYZ( n )-*this).SquareModulus(); } diff --git a/src/SMESH_I/SMESH_MeshEditor_i.cxx b/src/SMESH_I/SMESH_MeshEditor_i.cxx index a3fe510c7..206c73da4 100644 --- a/src/SMESH_I/SMESH_MeshEditor_i.cxx +++ b/src/SMESH_I/SMESH_MeshEditor_i.cxx @@ -1063,7 +1063,8 @@ CORBA::Long SMESH_MeshEditor_i::AddPolygonalFace (const SMESH::long_array & IDsO int NbNodes = IDsOfNodes.length(); std::vector nodes (NbNodes); for (int i = 0; i < NbNodes; i++) - nodes[i] = getMeshDS()->FindNode(IDsOfNodes[i]); + if ( ! ( nodes[i] = getMeshDS()->FindNode( IDsOfNodes[i] ))) + return 0; const SMDS_MeshElement* elem = getMeshDS()->AddPolygonalFace(nodes);