Subversion Repositories gelsvn

Rev

Rev 595 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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

Generated by GNU Enscript 1.6.6.
82

Generated by GNU Enscript 1.6.6.
83
 
83
 
84
 
84
 
85
 
85