Subversion Repositories gelsvn

Rev

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