Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | 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
 
149 jab 7
#include "x3d_save.h"
8
 
595 jab 9
#include <fstream>
10
#include <vector>
601 jab 11
#include "../CGLA/Vec3f.h"
595 jab 12
 
13
#include "Manifold.h"
14
#include "AttributeVector.h"
15
 
16
 
150 jab 17
namespace HMesh
149 jab 18
{
595 jab 19
    using namespace std;
20
    using namespace CGLA;
149 jab 21
 
22
 
595 jab 23
    namespace
24
    {
25
        const string X3D_BEGIN = 
26
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
27
            "<!DOCTYPE X3D PUBLIC \"http://www.web3D.org/TaskGroups/x3d/translation/x3d-compact.dtd\"\n"
28
            "\"/www.web3d.org/TaskGroups/x3d/translation/x3d-compact.dtd\">\n"
29
            "<X3D>\n"
30
            "  <head>\n"
31
            "  </head>\n"
32
            "  <Scene>\n"
33
            "		<Viewpoint description=\"Pyramid\" orientation=\"0 1 0 .2\" position=\"4 0 25\"/>\n"
34
            "    <NavigationInfo type=\"EXAMINE ANY\"/>\n";
149 jab 35
 
595 jab 36
        const string X3D_END = 
37
            "  </Scene>\n"
38
            "</X3D>\n";
149 jab 39
 
595 jab 40
    }
149 jab 41
 
595 jab 42
    bool x3d_save(const std::string& filename, Manifold& m)
43
    {
44
        ofstream os(filename.data());
45
        if(os.bad()){ 
46
            return false;
47
        }
48
        os << X3D_BEGIN << "\n";
149 jab 49
 
595 jab 50
        os << "<Shape>\n"
51
            << "<Appearance>\n"
52
            << "<Material diffuseColor=\"0.8 0.8 0.2\" specularColor=\"0 0 0.5\"/>\n"
53
            << "</Appearance>\n";
149 jab 54
 
595 jab 55
        os <<  "<Coordinate point=\"" << "\n";
149 jab 56
 
595 jab 57
        VertexAttributeVector<int> vmap(m.allocated_vertices());
58
 
59
        int k = 0;
60
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
61
            Vec3d p = m.pos(*v);
62
            os << p[0] << " " << p[1] << " " << p[2] << "\n";
63
            vmap[*v] = k++;
64
        }
65
        os << "\"/>" << "\n";
66
 
67
        os << "<IndexedFaceSet coordIndex=\"" << endl;
68
        for(FaceIDIterator f =  m.faces_begin(); f != m.faces_end(); ++f){
69
            vector<int> verts;
70
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
71
                int idx = vmap[w.vertex()];
72
                assert(static_cast<size_t>(idx) < m.no_vertices());
73
                verts.push_back(idx);
74
            }
75
            //assert(verts.size()==3);
76
            for(size_t i = 0; i < verts.size(); ++i){
77
                os << verts[i] << " ";
78
            }
79
            os << " -1\n";
80
        }
81
        os << "\">" << "\n";
82
        os << "</IndexedFaceSet>\n"
83
            << "</Shape>\n";
84
        os << X3D_END << "\n";
85
 
86
        return true;
87
    }
149 jab 88
}