Subversion Repositories gelsvn

Rev

Rev 507 | Rev 512 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
* Written by Christian Thode Larsen 2009-2010
* Contact: thode2d@gmail.com
* Based on original work by J. Andreas Baerentzen
* Inspired by OpenMesh (www.openmesh.org)
*/

#include "obj_save.h"

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#include <CGLA/Vec3f.h>

#include "Manifold.h"
#include "AttributeVector.h"

namespace HMesh
{
    using namespace std;
    using namespace CGLA;
        
        bool obj_save(const string& filename, Manifold& m)
        {
                ofstream os(filename.data());
        if(os.bad())
            return false;
        
                VertexAttributeVector<IndexType> vmap(m);

                IndexType k = 0;
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
                        Vec3f p = m.pos(*v);
                        os << "v "<< p[0] << " " << p[1] << " " << p[2] << "\n";
                        vmap[*v] = k++;
                }

        for(FaceIDIterator f = m.faces_begin(); f != m.faces_end(); ++f){        
                    vector<IndexType> verts;
                        for(HalfEdgeWalker w = m.halfedgewalker(*f); !w.full_circle(); w = w.circulate_face_cw()){
                            IndexType idx = vmap[w.vertex()];                   
                assert(idx < m.no_vertices());
                // move subscript range from 0..size-1 to 1..size according to OBJ standards
                                verts.push_back(idx + 1);
                    }
                    for(IndexType i = 1; i < (verts.size()-1) ; ++i){
                            os << "f ";
                            os << verts[0] << " " << verts[i] << " " << verts[i+1] << "\n";
                    }
                }

                return true;
        }
        
}