Subversion Repositories gelsvn

Rev

Rev 595 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
#include <fstream>
149 jab 8
#include "obj_load.h"
9
 
601 jab 10
#include "../Geometry/obj_load.h"
11
#include "../Geometry/TriMesh.h"
149 jab 12
 
595 jab 13
#include "Manifold.h"
14
 
149 jab 15
using namespace std;
16
using namespace CGLA;
17
 
150 jab 18
namespace HMesh
149 jab 19
{
595 jab 20
    using std::string;
21
    using Geometry::TriMesh;
22
    using Geometry::obj_load;
23
 
24
    bool obj_load(const string& filename, Manifold& m)
25
    {
26
        ifstream ifs(filename.data());
27
 
28
        if(ifs)
29
        {
30
            vector<Vec3f> vertices;
31
            vector<int> faces;
32
            vector<int> indices;
33
            while(ifs.good() && !ifs.eof())
34
            {
35
                string tok;
36
                ifs >> tok;
37
                if(tok == "v")
38
                {
39
                    float x,y,z;
40
                    ifs >> x >> y >> z;
41
                    vertices.push_back(Vec3f(x,y,z));
42
                    char line[1000];
43
                    ifs.getline(line, 998);
44
                }
45
                else if(tok == "f")
46
                {
47
                    char line[1000];
48
                    ifs.getline(line, 998);
49
                    char* pch = strtok(line, " \t");
50
                    int ctr = 0;
51
                    while(pch != 0)
52
                    {
53
                        int v;
54
                        sscanf(pch, "%d", &v);
55
                        indices.push_back(v-1);
56
                        pch = strtok(0, " \t");
57
                        ++ctr;
58
                    }
59
                    if(ctr)
60
                        faces.push_back(ctr);
61
                }
62
                else
63
                {
64
                    char line[1000];
65
                    ifs.getline(line, 998);
66
                }
67
            }
68
            m.clear();
69
            m.build(vertices.size(),
70
                    reinterpret_cast<float*>(&vertices[0]),
71
                    faces.size(),
72
                    &faces[0],
73
                    &indices[0]);
74
 
75
            return true;
76
        }
77
        return false;
78
 
79
    }
80
}