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 "x3d_save.h"

#include <fstream>
#include <vector>
#include <CGLA/Vec3f.h>

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


namespace HMesh
{
        using namespace std;
        using namespace CGLA;


        namespace
        {
                const string X3D_BEGIN = 
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                "<!DOCTYPE X3D PUBLIC \"http://www.web3D.org/TaskGroups/x3d/translation/x3d-compact.dtd\"\n"
                "\"/www.web3d.org/TaskGroups/x3d/translation/x3d-compact.dtd\">\n"
                "<X3D>\n"
                "  <head>\n"
                "  </head>\n"
                "  <Scene>\n"
                "               <Viewpoint description=\"Pyramid\" orientation=\"0 1 0 .2\" position=\"4 0 25\"/>\n"
                "    <NavigationInfo type=\"EXAMINE ANY\"/>\n";
        
                const string X3D_END = 
                "  </Scene>\n"
                "</X3D>\n";

        }

        bool x3d_save(const std::string& filename, Manifold& m)
        {
                ofstream os(filename.data());
        if(os.bad()){ 
            return false;
        }
                os << X3D_BEGIN << "\n";

                os << "<Shape>\n"
                         << "<Appearance>\n"
                         << "<Material diffuseColor=\"0.8 0.8 0.2\" specularColor=\"0 0 0.5\"/>\n"
                         << "</Appearance>\n";

                os <<  "<Coordinate point=\"" << "\n";

                VertexAttributeVector<IndexType> vmap(m);

                IndexType k = 0;
        for(VertexIDIterator v = m.vertices_begin(); v != m.vertices_end(); ++v){
                                Vec3f p = m.pos(*v);
                                os << p[0] << " " << p[1] << " " << p[2] << "\n";
                                vmap[*v] = k++;
                        }
                os << "\"/>" << "\n";
        
                os << "<IndexedFaceSet coordIndex=\"" << endl;
        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());
                                verts.push_back(idx);
                        }
                    //assert(verts.size()==3);
            for(IndexType i = 0; i < verts.size(); ++i){
                                os << verts[i] << " ";
            }
                        os << " -1\n";
                }
                os << "\">" << "\n";
                os << "</IndexedFaceSet>\n"
                         << "</Shape>\n";
                os << X3D_END << "\n";

                return true;
        }
}