Subversion Repositories gelsvn

Rev

Rev 149 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#if defined(_MSC_VER)
#include <io.h>

#pragma message("Note: including lib: expat.lib\n")
#pragma comment(lib, "expat.lib")

#else
#include <unistd.h>
#endif
#include <expat.h>

#include "Util/Timer.h"
#include "Util/Parse.h"
#include "HMesh/Manifold.h"
#include "build_manifold.h"
#include <iostream>

#include "x3d_load.h"

using namespace CGLA;
using namespace Util;
using namespace std;
using namespace HMesh;

namespace HMesh
{

                namespace
                {
                                vector<int> faces;
                                vector<int> indices;
                                vector<Vec3f> vertices;

                                void coord_index_to_face_vec(const vector<int>& coord_index, 
                                                                                                                                                 vector<int>& faces,
                                                                                                                                                 vector<int>& indices)
                                {
                                                Face face;
                                                size_t k=0;
                                                for(size_t i=0;i<coord_index.size();i++) 
                                                {
                                                                int idx = coord_index[i];
                                                                if (idx==-1) 
                                                                {
                                                                                faces.push_back(k);
                                                                                k=0;
                                                                }
                                                                else
                                                                {
                                                                                indices.push_back(idx);
                                                                                ++k;
                                                                }
                                                }
                                }

                }
        
                void startElement(void *userData, const char *name, const char **atts) 
                {
                                if (strcmp(name,"IndexedFaceSet")==0)
                                {
                                                int j=0;
                                                while (atts[j]!=0) {
                                                                if (strcmp(atts[j],"coordIndex")==0) 
                                                                { 
                                                                                vector<int> coord_index;
                                                                                parse(atts[++j],coord_index);
                                                                                coord_index_to_face_vec(coord_index, faces, indices);
                                                                } 
                                                                j++;
                                                }
                                } 
                                else if (strcmp(name,"Shape")==0) 
                                {
                                                cout << "Found shape" << endl;
                                }
                                else if (strcmp(name,"Coordinate")==0) 
                                { 
                                                int j=0;
                                                while (atts[j]!=0) 
                                                {
                                                                if (strcmp(atts[j],"point")==0) { 
                                                                                parse(atts[++j],vertices);
                                                                } 
                                                                j++;
                                                }
                                }
                }

                void endElement(void *userData, const char *name) 
                {
                                if (strcmp(name,"Shape")==0) 
                                {
                                                cout << "Shape ends" << endl;                           
                                } 
                }

                int find_last_of(const std::string& F, const std::string& C)
                {
                                size_t pos = F.find_last_of(C);
                                if (pos == string::npos) 
                                                return -1;
                                return pos;
                }

                bool x3d_load(const std::string& filename, Manifold& mani) 
                {
                                faces.clear();
                                indices.clear();
                                vertices.clear();

                                Timer tim;
                                tim.start();

                                std::string baseurl;
                                int idx = s_max(find_last_of(filename, "\\"), 
                                                                                                find_last_of(filename, "/"));


                                if(idx != -1)
                                                baseurl = std::string(filename.substr(0, idx+1));


                                XML_Parser parser = XML_ParserCreate(NULL);
                                int done=0;
                                XML_SetElementHandler(parser, startElement, endElement);

                                int in;
                                if ((in=open(filename.data(),O_RDONLY)) == -1) {
                                                cout << "Error. "<<  filename << " not found" << endl;
                                                assert(0);
                                                return false;
                                }

                                struct stat stat_buf;
                                fstat(in, &stat_buf);

                                const size_t BUF_SIZE = s_min(5000000, static_cast<int>(stat_buf.st_size));
                                char* buf2 = new char[BUF_SIZE];
                                size_t len;
                                do {
                                                len = read(in, buf2, BUF_SIZE);
                                                if (len!=BUF_SIZE)
                                                                done=1;
                                                if (!XML_Parse(parser, buf2, len, done)) {
                                                                cerr << "%s at line %d\n" 
                                                                                 << XML_ErrorString(XML_GetErrorCode(parser))
                                                                                 << XML_GetCurrentLineNumber(parser);
                                                                assert(0);
                                                                return false;
                                                }
                                } while (!done);
                                close(in);
                                XML_ParserFree(parser);
                                delete buf2;
                
                                build_manifold(mani, vertices.size(), &vertices[0], faces.size(),
                                                                                         &faces[0], &indices[0]);

                                cout << " Loading took " << tim.get_secs() << endl;
                                return false;
                }
}