Subversion Repositories gelsvn

Rev

Rev 489 | Rev 511 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "x3d_save.h"

#include <fstream>
#include <vector>

#include <CGLA/Vec3f.h>

#include "Manifold.h"
#include "VertexHandle.h"
#include "FaceHandle.h"
#include "FaceCirculator.h"

namespace HMesh
{
    using std::string;
    using std::ofstream;
    using std::cout;
    using std::endl;
    using std::vector;
    using CGLA::Vec3f;
        //using namespace HMesh;

        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 << endl;

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

                os <<  "<Coordinate point=\"" << endl;
        
        //easy way to avoid skipping indice numbers
        Manifold m1 = m;
        m1.cleanup();

        for(VertexHandle v = m1.vertices_begin(); v != m1.vertices_end(); ++v){
                                Vec3f p = v.get().pos;
                                os << p[0] << " " << p[1] << " " << p[2] << "\n";
                        }
                os << "\"/>" << endl;
        
                os << "<IndexedFaceSet coordIndex=\"" << endl;
        for(FaceHandle f =  m1.faces_begin(); f != m1.faces_end(); ++f){
                        vector<VertexIndex> verts;
                        for(FaceCirculator fc(f);!fc.end();++fc){
                VertexIndex vertex_idx = fc.vertex().get_idx();
                                assert(vertex_idx < m1.no_vertices());
                                verts.push_back(vertex_idx);
                        }
                    //assert(verts.size()==3);
            for(IndexType i = 0; i < verts.size(); ++i){
                                os << verts[i] << " ";
            }
                        os << " -1\n";
                }
                os << "\">" << endl;
                os << "</IndexedFaceSet>\n"
                         << "</Shape>\n";
                os << X3D_END << endl;

                return true;
        }
}