X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FDriverSTL%2FDriverSTL_W_SMDS_Mesh.cxx;h=ef66f22927371705f8bfb49c870466f5fed5544a;hp=ed7c48ebc4d4bdb191bf56151f243db8ff26bde5;hb=7868270178ffe3023bfe60b6499e906b9d812cc2;hpb=4ff5bd61540272713e48de1eee75625028c32155 diff --git a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx index ed7c48ebc..ef66f2292 100644 --- a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx +++ b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx @@ -1,42 +1,48 @@ -// Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, -// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS -// -// This library is free software; you can redistribute it and/or -// modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either -// version 2.1 of the License. -// -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -// Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -// -// See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org +// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE +// +// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, +// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// #include +#include #include "DriverSTL_W_SMDS_Mesh.h" +#include "SMDS_FaceOfNodes.hxx" +#include "SMDS_IteratorOnIterators.hxx" #include "SMDS_Mesh.hxx" #include "SMDS_MeshElement.hxx" #include "SMDS_MeshNode.hxx" -#include -#include +#include "SMDS_SetIterator.hxx" +#include "SMDS_VolumeTool.hxx" +#include "SMESH_TypeDefs.hxx" + #include -#include +#include #include -#include #include -#include +#include +#include #include "utilities.h" -//using namespace std; - // definition des constantes static const int LABEL_SIZE = 80; @@ -52,12 +58,15 @@ void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii ) Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform() { + Kernel_Utils::Localizer loc; + Status aResult = DRS_OK; if ( !myMesh ) { fprintf(stderr, ">> ERROR : Mesh is null \n"); return DRS_FAIL; } + findVolumeTriangles(); if ( myIsAscii ) aResult = writeAscii(); else @@ -65,16 +74,85 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform() return aResult; } +//================================================================================ +/*! + * \brief Destructor deletes temporary faces + */ +//================================================================================ + +DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh() +{ + for ( unsigned i = 0; i < myVolumeTrias.size(); ++i ) + delete myVolumeTrias[i]; +} + +//================================================================================ +/*! + * \brief Finds free facets of volumes for which faces are missing in the mesh + */ +//================================================================================ + +void DriverSTL_W_SMDS_Mesh::findVolumeTriangles() +{ + SMDS_VolumeTool theVolume; + SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator(); + std::vector< const SMDS_MeshNode*> nodes; + while ( vIt->more() ) + { + theVolume.Set( vIt->next(), /*ignoreCentralNodes=*/false ); + for ( int iF = 0; iF < theVolume.NbFaces(); ++iF ) + if ( theVolume.IsFreeFace( iF )) + { + 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 ( nbN == 9 && !theVolume.IsPoly() ) // facet is SMDSEntity_BiQuad_Quadrangle + { + 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] )); + } + 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] )); + } + } + } + } +} + +//================================================================================ +/*! + * \brief Return iterator on both faces in the mesh and on temporary faces + */ +//================================================================================ + +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())); + typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector; + TElemIterVector iters(2); + iters[0] = facesIter; + iters[1] = tmpTriaIter; + + typedef SMDS_IteratorOnIterators TItersIter; + return SMDS_ElemIteratorPtr( new TItersIter( iters )); +} // static methods -static void writeInteger( const Standard_Integer& theVal, - OSD_File& ofile ) +static void writeInteger( const Standard_Integer& theVal, OSD_File& ofile ) { union { Standard_Integer i; char c[4]; - }u; + } u; u.i = theVal; @@ -88,7 +166,7 @@ static void writeInteger( const Standard_Integer& theVal, } static void writeFloat ( const Standard_ShortReal& theVal, - OSD_File& ofile) + OSD_File& ofile) { union { Standard_ShortReal f; @@ -107,35 +185,103 @@ static void writeFloat ( const Standard_ShortReal& theVal, ofile.Write((char *)&entier,sizeof(u.c)); } -static gp_XYZ getNormale( const SMDS_MeshFace* theFace ) +static gp_XYZ getNormale( const SMDS_MeshNode* n1, + const SMDS_MeshNode* n2, + const SMDS_MeshNode* n3) { - gp_XYZ n; - int aNbNode = theFace->NbNodes(); - TColgp_Array1OfXYZ anArrOfXYZ(1,4); - gp_XYZ p1, p2, p3, p4; - SMDS_ElemIteratorPtr aNodeItr = theFace->nodesIterator(); - int i = 1; - for ( ; aNodeItr->more() && i <= 4; i++ ) - { - SMDS_MeshNode* aNode = (SMDS_MeshNode*)aNodeItr->next(); - anArrOfXYZ.SetValue(i, gp_XYZ( aNode->X(), aNode->Y(), aNode->Z() ) ); - } - - gp_XYZ q1 = anArrOfXYZ.Value(2) - anArrOfXYZ.Value(1); - gp_XYZ q2 = anArrOfXYZ.Value(3) - anArrOfXYZ.Value(1); - n = q1 ^ q2; - if ( aNbNode > 3 ) - { - gp_XYZ q3 = anArrOfXYZ.Value(4) - anArrOfXYZ.Value(1); - n += q2 ^ q3; - } + SMESH_TNodeXYZ xyz1( n1 ); + SMESH_TNodeXYZ xyz2( n2 ); + SMESH_TNodeXYZ xyz3( n3 ); + gp_XYZ q1 = xyz2 - xyz1; + gp_XYZ q2 = xyz3 - xyz1; + gp_XYZ n = q1 ^ q2; double len = n.Modulus(); - if ( len > 0 ) + if ( len > std::numeric_limits::min() ) n /= len; return n; } +//================================================================================ +/*! + * \brief Return nb triangles in a decomposed mesh face + * \retval int - number of triangles + */ +//================================================================================ + +static int getNbTriangles( const SMDS_MeshElement* face) +{ + // WARNING: counting triangles must be coherent with getTriangles() + switch ( face->GetEntityType() ) + { + case SMDSEntity_BiQuad_Triangle: + case SMDSEntity_BiQuad_Quadrangle: + return face->NbNodes() - 1; + // case SMDSEntity_Triangle: + // case SMDSEntity_Quad_Triangle: + // case SMDSEntity_Quadrangle: + // case SMDSEntity_Quad_Quadrangle: + // case SMDSEntity_Polygon: + // case SMDSEntity_Quad_Polygon: + default: + return face->NbNodes() - 2; + } + return 0; +} + +//================================================================================ +/*! + * \brief Decompose a mesh face into triangles + * \retval int - number of triangles + */ +//================================================================================ + +static int getTriangles( const SMDS_MeshElement* face, + const SMDS_MeshNode** nodes) +{ + // WARNING: decomposing into triangles must be coherent with getNbTriangles() + int nbTria, i = 0; + SMDS_NodeIteratorPtr nIt = face->interlacedNodesIterator(); + nodes[ i++ ] = nIt->next(); + nodes[ i++ ] = nIt->next(); + + const SMDSAbs_EntityType type = face->GetEntityType(); + switch ( type ) + { + case SMDSEntity_BiQuad_Triangle: + case SMDSEntity_BiQuad_Quadrangle: + nbTria = ( type == SMDSEntity_BiQuad_Triangle ) ? 6 : 8; + nodes[ i++ ] = face->GetNode( nbTria ); + while ( i < 3*(nbTria-1) ) + { + nodes[ i++ ] = nodes[ i-2 ]; + nodes[ i++ ] = nIt->next(); + nodes[ i++ ] = nodes[ 2 ]; + } + nodes[ i++ ] = nodes[ i-2 ]; + nodes[ i++ ] = nodes[ 0 ]; + nodes[ i++ ] = nodes[ 2 ]; + 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 ]; + nodes[ i++ ] = nIt->next(); + } + break; + } + return nbTria; +} + // private methods Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const @@ -149,17 +295,24 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const OSD_File aFile = OSD_File(OSD_Path(aFileName)); aFile.Build(OSD_WriteOnly,OSD_Protection()); + + char sval[16]; TCollection_AsciiString buf = TCollection_AsciiString ("solid\n"); aFile.Write (buf,buf.Length());buf.Clear(); - char sval[16]; - SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator(); - - for (; itFaces->more() ;) { - SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next(); + const SMDS_MeshNode* triaNodes[2048]; + + SMDS_ElemIteratorPtr itFaces = getFaces(); + while ( itFaces->more() ) + { + const SMDS_MeshElement* aFace = itFaces->next(); + int nbTria = getTriangles( aFace, triaNodes ); - if (aFace->NbNodes() == 3) { - gp_XYZ normale = getNormale( aFace ); + for ( int iT = 0, iN = 0; iT < nbTria; ++iT ) + { + gp_XYZ normale = getNormale( triaNodes[iN], + triaNodes[iN+1], + triaNodes[iN+2] ); buf += " facet normal "; sprintf (sval,"% 12e",normale.X()); @@ -175,9 +328,8 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const buf += " outer loop\n"; aFile.Write (buf,buf.Length());buf.Clear(); - SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator(); - for (; aNodeIter->more(); ) { - SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next(); + for ( int jN = 0; jN < 3; ++jN, ++iN ) { + const SMDS_MeshNode* node = triaNodes[iN]; buf += " vertex "; sprintf (sval,"% 12e",node->X()); buf += sval; @@ -204,6 +356,13 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const return aResult; } +//================================================================================ +/*! + * \brief Writes all triangles in binary format + * \return Driver_Mesh::Status - DRS_FAIL if no file name is provided + */ +//================================================================================ + Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const { Status aResult = DRS_OK; @@ -216,41 +375,49 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const OSD_File aFile = OSD_File(OSD_Path(aFileName)); aFile.Build(OSD_WriteOnly,OSD_Protection()); - char sval[80]; - Standard_Integer nbTri = 0; - SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator(); - // we first count the number of triangles - for (;itFaces->more();) { - SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next(); - if (aFace->NbNodes() == 3) - nbTri++; + int nbTri = myVolumeTrias.size(); + { + SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator(); + while ( itFaces->more() ) { + const SMDS_MeshElement* aFace = itFaces->next(); + nbTri += getNbTriangles( aFace ); + } } + // char sval[80]; -- avoid writing not initialized memory + TCollection_AsciiString sval(LABEL_SIZE-1,' '); + aFile.Write((Standard_Address)sval.ToCString(),LABEL_SIZE); // write number of triangles - //unsigned int NBT = nbTri; - aFile.Write((Standard_Address)sval,LABEL_SIZE); writeInteger(nbTri,aFile); - // loop writing nodes. take face iterator again + // Loop writing nodes + int dum=0; - itFaces = myMesh->facesIterator(); - - for (;itFaces->more();) { - SMDS_MeshFace* aFace = (SMDS_MeshFace*)itFaces->next(); + + const SMDS_MeshNode* triaNodes[2048]; + + SMDS_ElemIteratorPtr itFaces = getFaces(); + while ( itFaces->more() ) + { + const SMDS_MeshElement* aFace = itFaces->next(); + int nbTria = getTriangles( aFace, triaNodes ); - if (aFace->NbNodes() == 3) { - gp_XYZ aNorm = getNormale( aFace ); - writeFloat(aNorm.X(),aFile); - writeFloat(aNorm.Y(),aFile); - writeFloat(aNorm.Z(),aFile); - - SMDS_ElemIteratorPtr aNodeIter = aFace->nodesIterator(); - for (; aNodeIter->more(); ) { - SMDS_MeshNode* node = (SMDS_MeshNode*)aNodeIter->next(); - writeFloat(node->X(),aFile); - writeFloat(node->Y(),aFile); - writeFloat(node->Z(),aFile); + for ( int iT = 0, iN = 0; iT < nbTria; ++iT ) + { + gp_XYZ normale = getNormale( triaNodes[iN], + triaNodes[iN+1], + triaNodes[iN+2] ); + writeFloat(normale.X(),aFile); + writeFloat(normale.Y(),aFile); + writeFloat(normale.Z(),aFile); + + for ( int jN = 0; jN < 3; ++jN, ++iN ) + { + const SMDS_MeshNode* node = triaNodes[iN]; + writeFloat(node->X(),aFile); + writeFloat(node->Y(),aFile); + writeFloat(node->Z(),aFile); } aFile.Write (&dum,2); }