667 |
khor |
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 "x3d_save.h"
|
|
|
8 |
|
|
|
9 |
#include <fstream>
|
|
|
10 |
#include <vector>
|
|
|
11 |
#include "../CGLA/Vec3f.h"
|
|
|
12 |
|
|
|
13 |
#include "Manifold.h"
|
|
|
14 |
#include "AttributeVector.h"
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
namespace HMesh
|
|
|
18 |
{
|
|
|
19 |
using namespace std;
|
|
|
20 |
using namespace CGLA;
|
|
|
21 |
|
|
|
22 |
|
|
|
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";
|
|
|
35 |
|
|
|
36 |
const string X3D_END =
|
|
|
37 |
" </Scene>\n"
|
|
|
38 |
"</X3D>\n";
|
|
|
39 |
|
|
|
40 |
}
|
|
|
41 |
|
|
|
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";
|
|
|
49 |
|
|
|
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";
|
|
|
54 |
|
|
|
55 |
os << "<Coordinate point=\"" << "\n";
|
|
|
56 |
|
|
|
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 |
}
|
|
|
88 |
}
|