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=d0239cfe14a2906b2aa3bece58d65b037fd7375d;hp=6d572ca0e5fbaf13c7224a6793ebed27b67f6044;hb=HEAD;hpb=b0a908c0d20341651771d0249fb10882f54b2aad diff --git a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx index 6d572ca0e..1dee48d8b 100644 --- a/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx +++ b/src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -20,28 +20,26 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#include -#include - #include "DriverSTL_W_SMDS_Mesh.h" +#ifdef WIN32 +#define NOMINMAX +#endif + +#include + #include "SMDS_FaceOfNodes.hxx" #include "SMDS_IteratorOnIterators.hxx" #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_MeshAlgos.hxx" #include "SMESH_TypeDefs.hxx" -#include -#include -#include -#include -#include -#include +#include -#include "utilities.h" // definition des constantes static const int LABEL_SIZE = 80; @@ -74,6 +72,7 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform() return aResult; } + //================================================================================ /*! * \brief Destructor deletes temporary faces @@ -82,8 +81,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 +93,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 +107,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 +136,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; @@ -147,7 +149,7 @@ SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const // static methods -static void writeInteger( const Standard_Integer& theVal, OSD_File& ofile ) +static void writeInteger( const Standard_Integer& theVal, SMESH_File& ofile ) { union { Standard_Integer i; @@ -162,16 +164,15 @@ static void writeInteger( const Standard_Integer& theVal, OSD_File& ofile ) entier |= (u.c[2] & 0xFF) << 0x10; entier |= (u.c[3] & 0xFF) << 0x18; - ofile.Write((char *)&entier,sizeof(u.c)); + ofile.write( entier ); } -static void writeFloat ( const Standard_ShortReal& theVal, - OSD_File& ofile) +static void writeFloat( const Standard_ShortReal& theVal, SMESH_File& ofile) { union { Standard_ShortReal f; - char c[4]; - }u; + char c[4]; + } u; u.f = theVal; @@ -182,7 +183,7 @@ static void writeFloat ( const Standard_ShortReal& theVal, entier |= (u.c[2] & 0xFF) << 0x10; entier |= (u.c[3] & 0xFF) << 0x18; - ofile.Write((char *)&entier,sizeof(u.c)); + ofile.write( entier ); } static gp_XYZ getNormale( const SMDS_MeshNode* n1, @@ -202,156 +203,59 @@ static gp_XYZ getNormale( const SMDS_MeshNode* n1, 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 { Status aResult = DRS_OK; - TCollection_AsciiString aFileName( (char *)myFile.c_str() ); - if ( aFileName.IsEmpty() ) { + if ( myFile.empty() ) { fprintf(stderr, ">> ERREOR : invalid file name \n"); return DRS_FAIL; } - OSD_File aFile = OSD_File(OSD_Path(aFileName)); - aFile.Build(OSD_WriteOnly,OSD_Protection()); + SMESH_File aFile( myFile, /*openForReading=*/false ); + aFile.openForWriting(); + + std::string buf("solid "); + buf += myName + "\n"; + aFile.writeRaw( buf.c_str(), buf.size() ); - char sval[16]; - TCollection_AsciiString buf = TCollection_AsciiString ("solid\n"); - aFile.Write (buf,buf.Length());buf.Clear(); + char sval[128]; + std::vector< const SMDS_MeshNode* > triaNodes; - const SMDS_MeshNode* triaNodes[2048]; + SMESH_MeshAlgos::Triangulate triangulator; SMDS_ElemIteratorPtr itFaces = getFaces(); while ( itFaces->more() ) { const SMDS_MeshElement* aFace = itFaces->next(); - int nbTria = getTriangles( aFace, triaNodes ); - + int nbTria = triangulator.GetTriangles( aFace, triaNodes ); + for ( int iT = 0, iN = 0; iT < nbTria; ++iT ) { gp_XYZ normale = getNormale( triaNodes[iN], triaNodes[iN+1], triaNodes[iN+2] ); + sprintf (sval, + " facet normal % 12e % 12e % 12e\n" + " outer loop\n" , + normale.X(), normale.Y(), normale.Z()); + aFile.writeRaw ( sval, 70 + strlen( sval + 70 )); // at least 70 but can be more (WIN) - buf += " facet normal "; - sprintf (sval,"% 12e",normale.X()); - buf += sval; - buf += " "; - sprintf (sval,"% 12e",normale.Y()); - buf += sval; - buf += " "; - sprintf (sval,"% 12e",normale.Z()); - buf += sval; - buf += '\n'; - aFile.Write (buf,buf.Length());buf.Clear(); - buf += " outer loop\n"; - aFile.Write (buf,buf.Length());buf.Clear(); - - for ( int jN = 0; jN < 3; ++jN, ++iN ) { - const SMDS_MeshNode* node = triaNodes[iN]; - buf += " vertex "; - sprintf (sval,"% 12e",node->X()); - buf += sval; - buf += " "; - sprintf (sval,"% 12e",node->Y()); - buf += sval; - buf += " "; - sprintf (sval,"% 12e",node->Z()); - buf += sval; - buf += '\n'; - aFile.Write (buf,buf.Length());buf.Clear(); + for ( int jN = 0; jN < 3; ++jN, ++iN ) + { + SMESH_TNodeXYZ node = triaNodes[iN]; + sprintf (sval, + " vertex % 12e % 12e % 12e\n", + node.X(), node.Y(), node.Z() ); + aFile.writeRaw ( sval, 54 + strlen( sval + 54 )); } - buf += " endloop\n"; - aFile.Write (buf,buf.Length());buf.Clear(); - buf += " endfacet\n"; - aFile.Write (buf,buf.Length());buf.Clear(); - } + aFile.writeRaw (" endloop\n" + " endfacet\n", 21 ); + } } - buf += "endsolid\n"; - aFile.Write (buf,buf.Length());buf.Clear(); - - aFile.Close (); + buf = "endsolid " + myName + "\n"; + aFile.writeRaw( buf.c_str(), buf.size() ); return aResult; } @@ -366,42 +270,48 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const { Status aResult = DRS_OK; - TCollection_AsciiString aFileName( (char *)myFile.c_str() ); - if ( aFileName.IsEmpty() ) { + + if ( myFile.empty() ) { fprintf(stderr, ">> ERREOR : invalid filename \n"); return DRS_FAIL; } - OSD_File aFile = OSD_File(OSD_Path(aFileName)); - aFile.Build(OSD_WriteOnly,OSD_Protection()); + SMESH_File aFile( myFile ); + aFile.openForWriting(); + + SMESH_MeshAlgos::Triangulate triangulator; // we first count the number of triangles - int nbTri = myVolumeTrias.size(); + int nbTri = myNbVolumeTrias; { SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator(); while ( itFaces->more() ) { const SMDS_MeshElement* aFace = itFaces->next(); - nbTri += getNbTriangles( aFace ); + nbTri += triangulator.GetNbTriangles( aFace ); } } - // char sval[80]; -- avoid writing not initialized memory - TCollection_AsciiString sval(LABEL_SIZE-1,' '); - aFile.Write((Standard_Address)sval.ToCString(),LABEL_SIZE); + std::string sval( LABEL_SIZE, ' ' ); + if ( !myName.empty() ) + { + sval = "name: " + myName; + sval.resize( LABEL_SIZE, ' ' ); + } + aFile.write( sval.c_str(), LABEL_SIZE ); // write number of triangles - writeInteger(nbTri,aFile); + writeInteger( nbTri, aFile ); // Loop writing nodes int dum=0; - const SMDS_MeshNode* triaNodes[2048]; + std::vector< const SMDS_MeshNode* > triaNodes; SMDS_ElemIteratorPtr itFaces = getFaces(); while ( itFaces->more() ) { const SMDS_MeshElement* aFace = itFaces->next(); - int nbTria = getTriangles( aFace, triaNodes ); + int nbTria = triangulator.GetTriangles( aFace, triaNodes ); for ( int iT = 0, iN = 0; iT < nbTria; ++iT ) { @@ -419,10 +329,9 @@ Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const writeFloat(node->Y(),aFile); writeFloat(node->Z(),aFile); } - aFile.Write (&dum,2); - } + aFile.writeRaw ( &dum, 2 ); + } } - aFile.Close (); return aResult; }