Subversion Repositories gelsvn

Rev

Rev 667 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 667 Rev 677
Line 1... Line 1...
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 "obj_save.h"
7
#include "obj_save.h"
8
 
8
 
9
#include <fstream>
9
#include <fstream>
10
#include <iostream>
10
#include <iostream>
11
#include <string>
11
#include <string>
12
#include <vector>
12
#include <vector>
13
 
13
 
14
#include "../CGLA/Vec3f.h"
14
#include "../CGLA/Vec3f.h"
15
 
15
 
16
#include "Manifold.h"
16
#include "Manifold.h"
17
#include "AttributeVector.h"
17
#include "AttributeVector.h"
18
 
18
 
19
namespace HMesh
19
namespace HMesh
20
{
20
{
21
    using namespace std;
21
    using namespace std;
22
    using namespace CGLA;
22
    using namespace CGLA;
23
 
23
 
24
    bool obj_save(const string& filename, Manifold& m)
24
    bool obj_save(const string& filename, Manifold& m)
25
    {
25
    {
26
        ofstream os(filename.data());
26
        ofstream os(filename.data());
27
        if(os.bad())
27
        if(os.bad())
28
            return false;
28
            return false;
29
 
29
 
30
        VertexAttributeVector<int> vmap;
30
        VertexAttributeVector<int> vmap;
31
        int k = 0;
31
        int k = 0;
32
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
32
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
33
            Vec3d p = m.pos(*v);
33
            Vec3d p = m.pos(*v);
34
            os << "v "<< p[0] << " " << p[1] << " " << p[2] << "\n";
34
            os << "v "<< p[0] << " " << p[1] << " " << p[2] << "\n";
35
            vmap[*v] = k++;
35
            vmap[*v] = k++;
36
        }
36
        }
37
 
37
 
38
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){        
38
        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){        
39
            vector<int> verts;
39
            vector<int> verts;
40
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
40
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
41
                int idx = vmap[w.vertex()];			
41
                int idx = vmap[w.vertex()];			
42
                assert(static_cast<size_t>(idx) < m.no_vertices());
42
                assert(static_cast<size_t>(idx) < m.no_vertices());
43
                // move subscript range from 0..size-1 to 1..size according to OBJ standards
43
                // move subscript range from 0..size-1 to 1..size according to OBJ standards
44
                verts.push_back(idx + 1);
44
                verts.push_back(idx + 1);
45
            }
45
            }
46
			os << "f ";
46
			os << "f ";
47
            for(size_t i = 0; i < verts.size() ; ++i){
47
            for(size_t i = 0; i < verts.size() ; ++i){
48
                os << verts[i] << " ";
48
                os << verts[i];
-
 
49
                if (i+1==verts.size())
-
 
50
                    break;
-
 
51
                os << " ";
49
            }
52
            }
50
			os<<endl;
53
			os<<endl;
51
        }
54
        }
52
 
55
 
53
        return true;
56
        return true;
54
    }
57
    }
55
 
58
 
56
}
59
}