X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FDriverSTL%2FDriverSTL_R_SMDS_Mesh.cxx;h=ee4da4a6f295ffa4cf7582e054b2d69bab977518;hp=0f01bff01838a43d120e5214f8260fd31157b6a8;hb=e09cefb492091eed2987a366e9d607d0078e8cb9;hpb=f90e1ea49e27e4188ba8bff184ec45f28fd3574a diff --git a/src/DriverSTL/DriverSTL_R_SMDS_Mesh.cxx b/src/DriverSTL/DriverSTL_R_SMDS_Mesh.cxx index 0f01bff01..ee4da4a6f 100644 --- a/src/DriverSTL/DriverSTL_R_SMDS_Mesh.cxx +++ b/src/DriverSTL/DriverSTL_R_SMDS_Mesh.cxx @@ -20,69 +20,65 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -#include -#include -#include - -//======================================================================= -//function : HashCode -//purpose : -//======================================================================= -inline Standard_Integer HashCode - (const gp_Pnt& point, Standard_Integer Upper) -{ - union - { - Standard_Real R[3]; - Standard_Integer I[6]; - } U; - - point.Coord(U.R[0],U.R[1],U.R[2]); - - return ::HashCode(U.I[0]/23+U.I[1]/19+U.I[2]/17+U.I[3]/13+U.I[4]/11+U.I[5]/7,Upper); -} -static Standard_Real tab1[3]; -static Standard_Real tab2[3]; -//======================================================================= -//function : IsEqual -//purpose : -//======================================================================= -inline Standard_Boolean IsEqual - (const gp_Pnt& point1, const gp_Pnt& point2) -{ - point1.Coord(tab1[0],tab1[1],tab1[2]); - point2.Coord(tab2[0],tab2[1],tab2[2]); - return (memcmp(tab1,tab2,sizeof(tab1)) == 0); -} #include "DriverSTL_R_SMDS_Mesh.h" #include "SMDS_Mesh.hxx" #include "SMDS_MeshElement.hxx" #include "SMDS_MeshNode.hxx" +#include "SMESH_File.hxx" -#include -#include -#include -#include -#include -#include - -#include "utilities.h" +#include +#include -static const int HEADER_SIZE = 84; -static const int SIZEOF_STL_FACET = 50; -//static const int STL_MIN_FILE_SIZE = 284; -static const int ASCII_LINES_PER_FACET = 7; +#include +#include +namespace +{ + struct Hasher + { + //======================================================================= + //function : HashCode + //purpose : + //======================================================================= + inline static Standard_Integer HashCode + (const gp_Pnt& point, Standard_Integer Upper) + { + union + { + Standard_Real R[3]; + Standard_Integer I[6]; + } U; -//typedef NCollection_BaseCollection DriverSTL_ColOfNodePtr; + point.Coord( U.R[0], U.R[1], U.R[2] ); + return ::HashCode(U.I[0]/23+U.I[1]/19+U.I[2]/17+U.I[3]/13+U.I[4]/11+U.I[5]/7,Upper); + } + //======================================================================= + //function : IsEqual + //purpose : + //======================================================================= + inline static Standard_Boolean IsEqual + (const gp_Pnt& point1, const gp_Pnt& point2) + { + static Standard_Real tab1[3], tab2[3]; + point1.Coord(tab1[0],tab1[1],tab1[2]); + point2.Coord(tab2[0],tab2[1],tab2[2]); + return (memcmp(tab1,tab2,sizeof(tab1)) == 0); + } + }; + typedef NCollection_DataMap TDataMapOfPntNodePtr; + + const int HEADER_SIZE = 84; + const int SIZEOF_STL_FACET = 50; + const int ASCII_LINES_PER_FACET = 7; + const int SIZE_OF_FLOAT = 4; + // const int STL_MIN_FILE_SIZE = 284; +} -#include -typedef NCollection_DataMap DriverSTL_DataMapOfPntNodePtr; //======================================================================= //function : DriverSTL_R_SMDS_Mesh -//purpose : +//purpose : //======================================================================= DriverSTL_R_SMDS_Mesh::DriverSTL_R_SMDS_Mesh() @@ -110,42 +106,29 @@ Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform() 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; } - filebuf fic; - Standard_IStream is(&fic); - if (!fic.open(aFileName.ToCString(),ios::in)) { - fprintf(stderr, ">> ERROR : cannot open file %s \n", aFileName.ToCString()); + SMESH_File file( myFile, /*open=*/false ); + if ( !file.open() ) { + fprintf(stderr, ">> ERROR : cannot open file %s \n", myFile.c_str()); return DRS_FAIL; } - - OSD_Path aPath( aFileName ); - OSD_File file = OSD_File( aPath ); - file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD)); - unsigned char str[128]; - Standard_Integer lread,i; - Standard_Address ach; - ach = (Standard_Address)str; // we skip the header which is in Ascii for both modes - file.Read(ach,HEADER_SIZE,lread); + const char* data = file; + data += HEADER_SIZE; - // we read 128 characters to detect if we have a non-ascii char - file.Read(ach,sizeof(str),lread); - + // we check 128 characters to detect if we have a non-ascii char myIsAscii = Standard_True; - for (i = 0; i < lread; ++i) { - if (str[i] > '~') { + for (int i = 0; i < 128; ++i, ++data) { + if ( !isascii( *data ) && data < file.end() ) { myIsAscii = Standard_False; break; } } - - file.Close(); if ( !myMesh ) { fprintf(stderr, ">> ERREOR : cannot create mesh \n"); @@ -153,37 +136,34 @@ Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform() } if ( myIsAscii ) - aResult = readAscii(); + aResult = readAscii( file ); else - aResult = readBinary(); + aResult = readBinary( file ); return aResult; } // static methods -static Standard_Real readFloat(OSD_File& theFile) +static Standard_Real readFloat(SMESH_File& theFile) { union { - Standard_Boolean i; + Standard_Boolean i; Standard_ShortReal f; - }u; + } u; - char c[4]; - Standard_Address adr; - adr = (Standard_Address)c; - Standard_Integer lread; - theFile.Read(adr,4,lread); + const char* c = theFile; u.i = c[0] & 0xFF; u.i |= (c[1] & 0xFF) << 0x08; u.i |= (c[2] & 0xFF) << 0x10; u.i |= (c[3] & 0xFF) << 0x18; + theFile += SIZE_OF_FLOAT; return u.f; } static SMDS_MeshNode* addNode(const gp_Pnt& P, - DriverSTL_DataMapOfPntNodePtr& uniqnodes, + TDataMapOfPntNodePtr& uniqnodes, SMDS_Mesh* theMesh) { SMDS_MeshNode* node = 0; @@ -198,7 +178,7 @@ static SMDS_MeshNode* addNode(const gp_Pnt& P, } static SMDS_MeshNode* readNode(FILE* file, - DriverSTL_DataMapOfPntNodePtr& uniqnodes, + TDataMapOfPntNodePtr& uniqnodes, SMDS_Mesh* theMesh) { Standard_ShortReal coord[3]; @@ -209,17 +189,16 @@ static SMDS_MeshNode* readNode(FILE* file, return addNode( P, uniqnodes, theMesh ); } -static SMDS_MeshNode* readNode(OSD_File& theFile, - DriverSTL_DataMapOfPntNodePtr& uniqnodes, +static SMDS_MeshNode* readNode(SMESH_File& theFile, + TDataMapOfPntNodePtr& uniqnodes, SMDS_Mesh* theMesh) { - Standard_ShortReal coord[3]; - coord[0] = readFloat(theFile); - coord[1] = readFloat(theFile); - coord[2] = readFloat(theFile); + gp_Pnt coord; + coord.SetX( readFloat(theFile)); + coord.SetY( readFloat(theFile)); + coord.SetZ( readFloat(theFile)); - gp_Pnt P(coord[0],coord[1],coord[2]); - return addNode( P, uniqnodes, theMesh ); + return addNode( coord, uniqnodes, theMesh ); } //======================================================================= @@ -227,35 +206,30 @@ static SMDS_MeshNode* readNode(OSD_File& theFile, //purpose : //======================================================================= -Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii() const +Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii(SMESH_File& theFile) const { Status aResult = DRS_OK; - long ipos; - Standard_Integer nbLines = 0; - // Open the file - TCollection_AsciiString aFileName( (char *)myFile.c_str() ); - FILE* file = fopen(aFileName.ToCString(),"r"); - fseek(file,0L,SEEK_END); // get the file size - long filesize = ftell(file); - fclose(file); - file = fopen(aFileName.ToCString(),"r"); - + long filesize = theFile.size(); + theFile.close(); + + // Open the file + FILE* file = fopen( myFile.c_str(),"r"); + // count the number of lines - for (ipos = 0; ipos < filesize; ++ipos) { + Standard_Integer nbLines = 0; + for (long ipos = 0; ipos < filesize; ++ipos) { if (getc(file) == '\n') nbLines++; } // go back to the beginning of the file -// fclose(file); -// file = fopen(aFileName.ToCString(),"r"); rewind(file); Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET); - DriverSTL_DataMapOfPntNodePtr uniqnodes; + TDataMapOfPntNodePtr uniqnodes; // skip header while (getc(file) != '\n'); @@ -293,23 +267,14 @@ Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii() const //purpose : //======================================================================= -Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary() const +Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary(SMESH_File& file) const { Status aResult = DRS_OK; - char buftest[5]; - Standard_Address adr; - adr = (Standard_Address)buftest; - - TCollection_AsciiString aFileName( (char *)myFile.c_str() ); - OSD_File aFile = OSD_File(OSD_Path( aFileName )); - aFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD)); - // the size of the file (minus the header size) // must be a multiple of SIZEOF_STL_FACET - // compute file size - Standard_Integer filesize = aFile.Size(); + long filesize = file.size(); if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0 // Commented to allow reading small files (ex: 1 face) @@ -322,29 +287,25 @@ Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary() const Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET); // skip the header - aFile.Seek(HEADER_SIZE,OSD_FromBeginning); + file += HEADER_SIZE; - DriverSTL_DataMapOfPntNodePtr uniqnodes; - Standard_Integer lread; + TDataMapOfPntNodePtr uniqnodes; for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) { // ignore normals - readFloat(aFile); - readFloat(aFile); - readFloat(aFile); + file += 3 * SIZE_OF_FLOAT; // read vertices - SMDS_MeshNode* node1 = readNode( aFile, uniqnodes, myMesh ); - SMDS_MeshNode* node2 = readNode( aFile, uniqnodes, myMesh ); - SMDS_MeshNode* node3 = readNode( aFile, uniqnodes, myMesh ); + SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh ); + SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh ); + SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh ); if (myIsCreateFaces) myMesh->AddFace(node1,node2,node3); // skip extra bytes - aFile.Read(adr,2,lread); + file += 2; } - aFile.Close(); return aResult; }