From 601c6e763cdde8a3c0d9765175ce20ddfd6f646d Mon Sep 17 00:00:00 2001 From: linesight Date: Mon, 27 Oct 2025 21:30:27 -0700 Subject: [PATCH] fix const cy::TriMesh::Str to const char * conversion --- cyTriMesh.h | 356 ++++++++++++++++++++++++++-------------------------- 1 file changed, 178 insertions(+), 178 deletions(-) diff --git a/cyTriMesh.h b/cyTriMesh.h index db92ea4..bd9194c 100644 --- a/cyTriMesh.h +++ b/cyTriMesh.h @@ -66,7 +66,7 @@ class TriMesh Str() : data(nullptr) {} //!< Constructor Str( Str const &s ) : data(nullptr) { *this = s; } //!< Copy constructor ~Str() { if ( data ) delete [] data; } //!< Destructor - operator char const * () { return data; } //!< Implicit conversion to const char + operator char const * () const { return data; } //!< Implicit conversion to const char void operator = ( Str const &s ) { *this = s.data; } //!< Assignment operator void operator = ( char const *s ) { if (s) { size_t n=strlen(s); if (data) delete [] data; data=new char[n+1]; strncpy(data,s,n); data[n]='\0'; } else if (data) { delete [] data; data=nullptr; } } //!< Assignment operator }; @@ -192,14 +192,14 @@ class TriMesh static Vec3f Interpolate( int i, Vec3f const *v, TriFace const *f, Vec3f const &bc ) { return v[f[i].v[0]]*bc.x + v[f[i].v[1]]*bc.y + v[f[i].v[2]]*bc.z; } // Temporary structures - struct MtlData - { - std::string mtlName; - unsigned int firstFace; - unsigned int faceCount; - MtlData() { faceCount=0; firstFace=0; } - }; - struct MtlLibName { std::string filename; }; + struct MtlData + { + std::string mtlName; + unsigned int firstFace; + unsigned int faceCount; + MtlData() { faceCount=0; firstFace=0; } + }; + struct MtlLibName { std::string filename; }; }; //------------------------------------------------------------------------------- @@ -269,15 +269,15 @@ inline bool TriMesh::LoadFromFileObj( char const *filename, bool loadMtl, std::o return false; } - Clear(); - - class Buffer - { - char data[1024]; - int readLine; - public: - int ReadLine(FILE *fp) - { + Clear(); + + class Buffer + { + char data[1024]; + int readLine; + public: + int ReadLine(FILE *fp) + { int c = fgetc(fp); while ( !feof(fp) ) { while ( isspace(c) && ( !feof(fp) || c!='\0' ) ) c = fgetc(fp); // skip empty space @@ -299,53 +299,53 @@ inline bool TriMesh::LoadFromFileObj( char const *filename, bool loadMtl, std::o } data[i] = '\0'; readLine = i; - return i; - } - char& operator[](int i) { return data[i]; } - void ReadVertex( Vec3f &v ) const { v.Zero(); sscanf( data+2, "%f %f %f", &v.x, &v.y, &v.z ); } - void ReadFloat3( float f[3] ) const { f[2]=f[1]=f[0]=0; int n = sscanf( data+2, "%f %f %f", &f[0], &f[1], &f[2] ); if ( n==1 ) f[2]=f[1]=f[0]; } - void ReadFloat( float *f ) const { sscanf( data+2, "%f", f ); } - void ReadInt( int *i, int start ) const { sscanf( data+start, "%d", i ); } - bool IsCommand( char const *cmd ) const { - int i=0; - while ( cmd[i]!='\0' ) { - if ( cmd[i] != data[i] ) return false; - i++; - } - return (data[i]=='\0' || data[i]==' '); - } - char const * Data(int start=0) { return data+start; } - void Copy( Str &str, int start=0 ) - { - while ( data[start] != '\0' && data[start] <= ' ' ) start++; - str = Data(start); - } - }; - Buffer buffer; - - - struct MtlList { - std::vector mtlData; - int GetMtlIndex( char const *mtlName ) - { - for ( unsigned int i=0; i= 0 ) return i; - MtlData m; - m.mtlName = mtlName; - m.firstFace = firstFace; - mtlData.push_back(m); - return (int)mtlData.size()-1; - } - }; - MtlList mtlList; + return i; + } + char& operator[](int i) { return data[i]; } + void ReadVertex( Vec3f &v ) const { v.Zero(); sscanf( data+2, "%f %f %f", &v.x, &v.y, &v.z ); } + void ReadFloat3( float f[3] ) const { f[2]=f[1]=f[0]=0; int n = sscanf( data+2, "%f %f %f", &f[0], &f[1], &f[2] ); if ( n==1 ) f[2]=f[1]=f[0]; } + void ReadFloat( float *f ) const { sscanf( data+2, "%f", f ); } + void ReadInt( int *i, int start ) const { sscanf( data+start, "%d", i ); } + bool IsCommand( char const *cmd ) const { + int i=0; + while ( cmd[i]!='\0' ) { + if ( cmd[i] != data[i] ) return false; + i++; + } + return (data[i]=='\0' || data[i]==' '); + } + char const * Data(int start=0) { return data+start; } + void Copy( Str &str, int start=0 ) + { + while ( data[start] != '\0' && data[start] <= ' ' ) start++; + str = Data(start); + } + }; + Buffer buffer; + + + struct MtlList { + std::vector mtlData; + int GetMtlIndex( char const *mtlName ) + { + for ( unsigned int i=0; i= 0 ) return i; + MtlData m; + m.mtlName = mtlName; + m.firstFace = firstFace; + mtlData.push_back(m); + return (int)mtlData.size()-1; + } + }; + MtlList mtlList; std::vector _v; // vertices std::vector _f; // faces @@ -353,12 +353,12 @@ inline bool TriMesh::LoadFromFileObj( char const *filename, bool loadMtl, std::o std::vector _fn; // normal faces std::vector _vt; // texture vertices std::vector _ft; // texture faces - std::vector mtlFiles; - std::vector faceMtlIndex; - - int currentMtlIndex = -1; - bool hasTextures=false, hasNormals=false; - + std::vector mtlFiles; + std::vector faceMtlIndex; + + int currentMtlIndex = -1; + bool hasTextures=false, hasNormals=false; + while ( int rb = buffer.ReadLine(fp) ) { if ( buffer.IsCommand("v") ) { Vec3f vertex; @@ -378,65 +378,65 @@ inline bool TriMesh::LoadFromFileObj( char const *filename, bool loadMtl, std::o hasNormals = true; } else if ( buffer.IsCommand("f") ) { - int facevert = -1; - bool inspace = true; - bool negative = false; - int type = 0; - unsigned int index; - TriFace face, textureFace, normalFace; - unsigned int nFacesBefore = (unsigned int)_f.size(); - for ( int i=2; i= '0' && buffer[i] <= '9' ) { - index = index*10 + (buffer[i]-'0'); - switch ( type ) { - case 0: face.v [facevert] = negative ? (unsigned int)_v. size()-index : index-1; break; - case 1: textureFace.v[facevert] = negative ? (unsigned int)_vt.size()-index : index-1; hasTextures=true; break; - case 2: normalFace.v [facevert] = negative ? (unsigned int)_vn.size()-index : index-1; hasNormals =true; break; - } - } - } - } - _f.push_back(face); - if ( hasTextures ) _ft.push_back(textureFace); - if ( hasNormals ) _fn.push_back(normalFace); - faceMtlIndex.push_back(currentMtlIndex); - if ( currentMtlIndex>=0 ) mtlList.mtlData[currentMtlIndex].faceCount += (unsigned int)_f.size() - nFacesBefore; - } + int facevert = -1; + bool inspace = true; + bool negative = false; + int type = 0; + unsigned int index; + TriFace face, textureFace, normalFace; + unsigned int nFacesBefore = (unsigned int)_f.size(); + for ( int i=2; i= '0' && buffer[i] <= '9' ) { + index = index*10 + (buffer[i]-'0'); + switch ( type ) { + case 0: face.v [facevert] = negative ? (unsigned int)_v. size()-index : index-1; break; + case 1: textureFace.v[facevert] = negative ? (unsigned int)_vt.size()-index : index-1; hasTextures=true; break; + case 2: normalFace.v [facevert] = negative ? (unsigned int)_vn.size()-index : index-1; hasNormals =true; break; + } + } + } + } + _f.push_back(face); + if ( hasTextures ) _ft.push_back(textureFace); + if ( hasNormals ) _fn.push_back(normalFace); + faceMtlIndex.push_back(currentMtlIndex); + if ( currentMtlIndex>=0 ) mtlList.mtlData[currentMtlIndex].faceCount += (unsigned int)_f.size() - nFacesBefore; + } else if ( loadMtl ) { if ( buffer.IsCommand("usemtl") ) { currentMtlIndex = mtlList.CreateMtl(buffer.Data(7), (unsigned int)_f.size()); @@ -447,55 +447,55 @@ inline bool TriMesh::LoadFromFileObj( char const *filename, bool loadMtl, std::o mtlFiles.push_back(libName); } } - if ( feof(fp) ) break; - } - + if ( feof(fp) ) break; + } + fclose(fp); - - - if ( _f.size() == 0 ) return true; // No faces found - SetNumVertex((unsigned int)_v.size()); - SetNumFaces((unsigned int)_f.size()); - SetNumTexVerts((unsigned int)_vt.size()); - SetNumNormals((unsigned int)_vn.size()); - if ( loadMtl ) SetNumMtls((unsigned int)mtlList.mtlData.size()); - - // Copy data - memcpy(v, _v.data(), sizeof(Vec3f)*_v.size()); - if ( _vt.size() > 0 ) memcpy(vt, _vt.data(), sizeof(Vec3f)*_vt.size()); - if ( _vn.size() > 0 ) memcpy(vn, _vn.data(), sizeof(Vec3f)*_vn.size()); - - if ( mtlList.mtlData.size() > 0 ) { - unsigned int fid = 0; - for ( int mi=0; mi<(int)mtlList.mtlData.size(); mi++ ) { - for ( unsigned int i=mtlList.mtlData[mi].firstFace, j=0; j 0 ) memcpy(vt, _vt.data(), sizeof(Vec3f)*_vt.size()); + if ( _vn.size() > 0 ) memcpy(vn, _vn.data(), sizeof(Vec3f)*_vn.size()); + + if ( mtlList.mtlData.size() > 0 ) { + unsigned int fid = 0; + for ( int mi=0; mi<(int)mtlList.mtlData.size(); mi++ ) { + for ( unsigned int i=mtlList.mtlData[mi].firstFace, j=0; j