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