7 #pragma warning(disable:4996) 78 #if defined(__APPLE__) || defined(__CELLOS_LV2__) 79 #define stricmp(a, b) strcasecmp((a), (b)) 88 virtual int ParseLine(
int lineno,
int argc,
const char **argv) =0;
110 SetSourceData(data,len);
127 for (
int i=0; i<256; i++)
130 mHardString[i*2] = i;
131 mHardString[i*2+1] = 0;
140 void SetFile(
const char *fname);
153 const char ** GetArglist(
char *source,
int &count);
177 void DefaultSymbols(
void);
196 inline char * AddHard(
int &argc,
const char **argv,
char *foo);
197 inline bool IsHard(
char c);
198 inline char * SkipSpaces(
char *foo);
199 inline bool IsWhiteSpace(
char c);
200 inline bool IsNonSeparator(
char c);
206 char mHardString[256*2];
224 FILE *fph = fopen(fname,
"rb");
227 fseek(fph,0L,SEEK_END);
229 fseek(fph,0L,SEEK_SET);
232 mData = (
char *) malloc(
sizeof(
char)*(mLen+1));
233 int ok = fread(mData, mLen, 1, fph);
266 while ( IsHard(*foo) )
268 const char *hard = &mHardString[*foo*2];
285 while ( !EOS(*foo) && IsWhiteSpace(*foo) ) foo++;
291 if ( !IsHard(c) && !IsWhiteSpace(c) && c != 0 )
return true;
305 while ( !EOS(*foo) && argc <
MAXARGS )
308 foo = SkipSpaces(foo);
310 if ( EOS(*foo) )
break;
312 if ( *foo == mQuoteChar )
319 while ( !EOS(*foo) && *foo != mQuoteChar ) foo++;
329 foo = AddHard(argc,argv,foo);
331 if ( IsNonSeparator(*foo) )
334 if ( *foo == mQuoteChar )
347 while (*foo && *foo != mQuoteChar ) foo++;
348 if ( *foo ) *foo = 32;
354 if ( IsWhiteSpace(*foo) )
360 else if ( IsHard(*foo) )
362 const char *hard = &mHardString[*foo*2];
379 ret = callback->
ParseLine(lineno, argc, argv );
388 if ( !mData )
return 0;
400 if ( *foo == 10 || *foo == 13 )
407 int v = ProcessLine(lineno,begin,callback);
412 if ( *foo == 10 ) foo++;
423 int v = ProcessLine(lineno,begin,callback);
431 SetHardSeparator(
',');
432 SetHardSeparator(
'(');
433 SetHardSeparator(
')');
434 SetHardSeparator(
'=');
435 SetHardSeparator(
'[');
436 SetHardSeparator(
']');
437 SetHardSeparator(
'{');
438 SetHardSeparator(
'}');
439 SetCommentSymbol(
'#');
445 const char **ret = 0;
447 static const char *argv[
MAXARGS];
452 while ( !EOS(*foo) && argc <
MAXARGS )
455 foo = SkipSpaces(foo);
457 if ( EOS(*foo) )
break;
459 if ( *foo == mQuoteChar )
466 while ( !EOS(*foo) && *foo != mQuoteChar ) foo++;
476 foo = AddHard(argc,argv,foo);
478 if ( IsNonSeparator(*foo) )
481 if ( *foo == mQuoteChar )
494 while (*foo && *foo != mQuoteChar ) foo++;
495 if ( *foo ) *foo = 32;
501 if ( IsWhiteSpace(*foo) )
507 else if ( IsHard(*foo) )
509 const char *hard = &mHardString[*foo*2];
566 int ParseLine(
int lineno,
int argc,
const char **argv);
602 static const char *
GetArg(
const char **argv,
int i,
int argc)
604 const char * ret = 0;
605 if ( i < argc ) ret = argv[i];
622 int index = atoi( face )-1;
624 const char *texel = strstr(face,
"/");
628 int tindex = atoi( texel+1) - 1;
630 if ( tindex >=0 && tindex < (
int)(mTexels.size()/2) )
632 const double *t = &mTexels[tindex*2];
639 const char *normal = strstr(texel+1,
"/");
642 int nindex = atoi( normal+1 ) - 1;
644 if (nindex >= 0 && nindex < (
int)(mNormals.size()/3) )
646 const double *n = &mNormals[nindex*3];
655 if ( index >= 0 && index < (
int)(mVerts.size()/3) )
658 const double *p = &mVerts[index*3];
673 const char *foo = argv[0];
676 if ( strcmp(argv[0],
"v") == 0 && argc == 4 )
678 double vx = (double) atof( argv[1] );
679 double vy = (double) atof( argv[2] );
680 double vz = (double) atof( argv[3] );
681 mVerts.push_back(vx);
682 mVerts.push_back(vy);
683 mVerts.push_back(vz);
685 else if ( strcmp(argv[0],
"vt") == 0 && argc == 3 )
687 double tx = (double) atof( argv[1] );
688 double ty = (double) atof( argv[2] );
689 mTexels.push_back(tx);
690 mTexels.push_back(ty);
692 else if ( strcmp(argv[0],
"vn") == 0 && argc == 4 )
694 double normalx = (double) atof(argv[1]);
695 double normaly = (double) atof(argv[2]);
696 double normalz = (double) atof(argv[3]);
697 mNormals.push_back(normalx);
698 mNormals.push_back(normaly);
699 mNormals.push_back(normalz);
701 else if ( strcmp(argv[0],
"f") == 0 && argc >= 4 )
707 for (
int i=1; i<argc; i++)
709 GetVertex(v[i-1],argv[i] );
713 #if 0 // not currently implemented 714 if ( mNormals.empty() )
723 for (
int i=0; i<vcount; i++)
733 mCallback->NodeTriangle(&v[0],&v[1],&v[2]);
737 for (
int i=2; i<(vcount-1); i++)
739 mCallback->NodeTriangle(&v[0],&v[i],&v[i+1]);
760 int vcount = mVertices.size()/3;
765 const double *v = &mVertices[0];
767 for (
int i=0; i<vcount; i++)
769 if ( v[0] == p[0] && v[1] == p[1] && v[2] == p[2] )
return i;
774 mVertices.push_back( p[0] );
775 mVertices.push_back( p[1] );
776 mVertices.push_back( p[2] );
783 mIndices.push_back( GetIndex(v1->
mPos) );
784 mIndices.push_back( GetIndex(v2->
mPos) );
785 mIndices.push_back( GetIndex(v3->
mPos) );
789 const IntVector& GetIndices(
void)
const {
return mIndices; };
814 unsigned int ret = 0;
835 mVertexCount = vlist.size()/3;
836 mVertices =
new double[mVertexCount*3];
837 memcpy( mVertices, &vlist[0],
sizeof(
double)*mVertexCount*3 );
838 mTriCount = indices.size()/3;
839 mIndices =
new int[mTriCount*3*
sizeof(int)];
840 memcpy(mIndices, &indices[0],
sizeof(
int)*mTriCount*3);
843 else if( obj.
mVerts.size() > 0 ) {
845 mVertexCount = obj.
mVerts.size()/3;
846 mVertices =
new double[mVertexCount*3];
847 memcpy( mVertices, &obj.
mVerts[0],
sizeof(
double)*mVertexCount*3 );
848 mTriCount = mVertexCount/3;
849 mIndices =
new int[mTriCount*3*
sizeof(int)];
850 for(
int i = 0; i < mVertexCount; ++i)
void SetSourceData(char *data, int len)
GeometryInterface * mCallback
virtual void NodeTriangle(const GeometryVertex *v1, const GeometryVertex *v2, const GeometryVertex *v3)
bool IsWhiteSpace(char c)
int LoadMesh(const char *fname, GeometryInterface *callback)
char * SkipSpaces(char *foo)
void SetQuoteChar(char c)
int ParseLine(int lineno, int argc, const char **argv)
virtual void NodeTriangle(const GeometryVertex *v1, const GeometryVertex *v2, const GeometryVertex *v3)
char * AddHard(int &argc, const char **argv, char *foo)
int GetIndex(const double *p)
void DefaultSymbols(void)
static const char * GetArg(const char **argv, int i, int argc)
const char ** GetArglist(char *source, int &count)
InPlaceParser(char *data, int len)
bool IsNonSeparator(char c)
InPlaceParser(const char *fname)
void SetHardSeparator(char c)
unsigned int loadObj(const char *fname)
virtual int ParseLine(int lineno, int argc, const char **argv)=0
void SetFile(const char *fname)
std::vector< double > FloatVector
void SetCommentSymbol(char c)
std::vector< int > IntVector
void GetVertex(GeometryVertex &v, const char *face) const
int Parse(InPlaceParserInterface *callback)
const IntVector & GetIndices(void) const
const FloatVector & GetVertices(void) const
void ClearHardSeparator(char c)
double ComputeNormal(const Vector3d< double > &A, const Vector3d< double > &B, const Vector3d< double > &C)
int ProcessLine(int lineno, char *line, InPlaceParserInterface *callback)