Subversion Repositories gelsvn

Rev

Rev 394 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 394 Rev 595
Line -... Line 1...
-
 
1
/* ----------------------------------------------------------------------- *
-
 
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
1
#include <fstream>
3
 * Copyright (C) the authors and DTU Informatics
2
#include <string>
4
 * For license and list of authors, see ../../doc/intro.pdf
-
 
5
 * ----------------------------------------------------------------------- */
-
 
6
 
3
#include "x3d_save.h"
7
#include "x3d_save.h"
-
 
8
 
-
 
9
#include <fstream>
-
 
10
#include <vector>
-
 
11
#include <CGLA/Vec3f.h>
-
 
12
 
-
 
13
#include "Manifold.h"
4
#include "HMesh/FaceCirculator.h"
14
#include "AttributeVector.h"
-
 
15
 
5
 
16
 
6
namespace HMesh
17
namespace HMesh
7
{
18
{
8
	using namespace std;
19
    using namespace std;
9
	using namespace CGLA;
20
    using namespace CGLA;
10
	using namespace HMesh;
-
 
11
 
21
 
12
 
22
 
13
	namespace
23
    namespace
14
	{
24
    {
15
		const string X3D_BEGIN = 
25
        const string X3D_BEGIN = 
16
		"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
26
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
17
		"<!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"
18
		"\"/www.web3d.org/TaskGroups/x3d/translation/x3d-compact.dtd\">\n"
28
            "\"/www.web3d.org/TaskGroups/x3d/translation/x3d-compact.dtd\">\n"
19
		"<X3D>\n"
29
            "<X3D>\n"
20
		"  <head>\n"
30
            "  <head>\n"
21
		"  </head>\n"
31
            "  </head>\n"
22
		"  <Scene>\n"
32
            "  <Scene>\n"
23
		"		<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"
24
		"    <NavigationInfo type=\"EXAMINE ANY\"/>\n";
34
            "    <NavigationInfo type=\"EXAMINE ANY\"/>\n";
25
	
35
 
26
		const string X3D_END = 
36
        const string X3D_END = 
27
		"  </Scene>\n"
37
            "  </Scene>\n"
28
		"</X3D>\n";
38
            "</X3D>\n";
29
 
39
 
30
	}
40
    }
31
 
41
 
32
	bool x3d_save(const std::string& filename, Manifold& m)
42
    bool x3d_save(const std::string& filename, Manifold& m)
33
	{
43
    {
34
		ofstream os(filename.data());
44
        ofstream os(filename.data());
-
 
45
        if(os.bad()){ 
35
		if(os.bad()) return false;
46
            return false;
-
 
47
        }
36
		os << X3D_BEGIN << endl;
48
        os << X3D_BEGIN << "\n";
37
 
49
 
38
		os << "<Shape>\n"
50
        os << "<Shape>\n"
39
			 << "<Appearance>\n"
51
            << "<Appearance>\n"
40
			 << "<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"
41
			 << "</Appearance>\n";
53
            << "</Appearance>\n";
42
 
54
 
43
		os <<  "<Coordinate point=\"" << endl;
55
        os <<  "<Coordinate point=\"" << "\n";
-
 
56
 
44
		vector<int> touch(m.no_vertices());
57
        VertexAttributeVector<int> vmap(m.allocated_vertices());
-
 
58
 
45
		int max_vertex_idx=0;
59
        int k = 0;
46
		for(VertexIter vi = m.vertices_begin(); vi != m.vertices_end(); 
60
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
47
				++vi,++max_vertex_idx)
-
 
48
			{
-
 
49
				touch[max_vertex_idx] = vi->touched;
-
 
50
				vi->touched = max_vertex_idx;
-
 
51
				Vec3f p = vi->pos;
61
            Vec3d p = m.pos(*v);
52
				os << p[0] << " " << p[1] << " " << p[2] << "\n";
62
            os << p[0] << " " << p[1] << " " << p[2] << "\n";
-
 
63
            vmap[*v] = k++;
53
			}
64
        }
54
		os << "\"/>" << endl;
65
        os << "\"/>" << "\n";
55
	
66
 
56
		os << "<IndexedFaceSet coordIndex=\"" << endl;
67
        os << "<IndexedFaceSet coordIndex=\"" << endl;
57
		for(FaceIter fi = m.faces_begin();fi != m.faces_end(); ++fi)
68
        for(FaceIDIterator f =  m.faces_begin(); f != m.faces_end(); ++f){
58
			{
-
 
59
				std::vector<int> verts;
69
            vector<int> verts;
60
				for(FaceCirculator fc(fi);!fc.end();++fc)
70
            for(Walker w = m.walker(*f); !w.full_circle(); w = w.circulate_face_ccw()){
61
					{
-
 
62
						int vertex_idx = fc.get_vertex()->touched;
-
 
63
						if(vertex_idx >= max_vertex_idx)
71
                int idx = vmap[w.vertex()];
64
							{
-
 
65
								cout << vertex_idx  << " " << max_vertex_idx << endl;
-
 
66
								verts.push_back(0);
-
 
67
							}
-
 
68
						else
-
 
69
							{
-
 
70
								assert(vertex_idx < max_vertex_idx);
72
                assert(static_cast<size_t>(idx) < m.no_vertices());
71
								verts.push_back(vertex_idx);
73
                verts.push_back(idx);
72
							}
74
            }
73
					}
-
 
74
				//assert(verts.size()==3);
75
            //assert(verts.size()==3);
75
				for(size_t i=0;i<verts.size();++i)
76
            for(size_t i = 0; i < verts.size(); ++i){
76
					os << verts[i] << " ";
77
                os << verts[i] << " ";
-
 
78
            }
77
				os << " -1\n";
79
            os << " -1\n";
78
			}
80
        }
79
		os << "\">" << endl;
81
        os << "\">" << "\n";
80
		os << "</IndexedFaceSet>\n"
82
        os << "</IndexedFaceSet>\n"
81
			 << "</Shape>\n";
83
            << "</Shape>\n";
82
		os << X3D_END << endl;
84
        os << X3D_END << "\n";
83
 
-
 
84
		max_vertex_idx=0;
-
 
85
		for(VertexIter vi = m.vertices_begin(); vi != m.vertices_end(); ++vi,
-
 
86
					++max_vertex_idx)
-
 
87
			vi->touched = touch[max_vertex_idx];
-
 
88
 
85
 
89
		return false;
86
        return true;
90
	}
87
    }
91
}
88
}