MeshLoaderPLY.cpp

Go to the documentation of this file.
00001 /**<!-------------------------------------------------------------------->
00002    @file   MeshLoaderPLY.cpp
00003    @author Travis Fischer (fisch0920@gmail.com)
00004    @author Matthew Jacobs (jacobs.mh@gmail.com)
00005    @date   Spring 2008
00006    
00007    @brief
00008       Loads PLY meshes from an external source ('.ply' file extension).
00009    Wrapper around ply-parser-0.1
00010    <!-------------------------------------------------------------------->**/
00011 
00012 #include "MeshLoaderPLY.h"
00013 #include "MeshTriangle.h"
00014 #include "Mesh.h"
00015 
00016 #include <cstdlib>
00017 #include <cstring>
00018 #include <fstream>
00019 #include <iostream>
00020 
00021 bool MeshLoaderPLY::_load(std::istream &istream, MeshData &data) {
00022    _data = data;
00023    
00024    ply::ply_parser::flags_type ply_parser_flags = 1; // triangulate??
00025    ply::ply_parser ply_parser(ply_parser_flags);
00026    
00027    ply_parser.info_callback(std::tr1::bind(&MeshLoaderPLY::info_callback, this, data.fileName, _1, _2));
00028    ply_parser.warning_callback(std::tr1::bind(&MeshLoaderPLY::warning_callback, this, data.fileName, _1, _2));
00029    ply_parser.error_callback(std::tr1::bind(&MeshLoaderPLY::error_callback, this, data.fileName, _1, _2));
00030    
00031    ply_parser.element_definition_callback(std::tr1::bind(&MeshLoaderPLY::element_definition_callback, this, _1, _2));
00032    
00033    ply::ply_parser::scalar_property_definition_callbacks_type scalar_property_definition_callbacks;
00034    ply::at<ply::float32>(scalar_property_definition_callbacks) = std::tr1::bind(&MeshLoaderPLY::scalar_property_definition_callback, this, _1, _2);
00035    ply_parser.scalar_property_definition_callbacks(scalar_property_definition_callbacks);
00036    
00037    ply::ply_parser::list_property_definition_callbacks_type list_property_definition_callbacks;
00038    ply::at<ply::uint8, ply::int32>(list_property_definition_callbacks) = std::tr1::bind(&MeshLoaderPLY::list_property_definition_callback, this, _1, _2);
00039    ply_parser.list_property_definition_callbacks(list_property_definition_callbacks);
00040    
00041    return ply_parser.parse(istream);
00042 }
00043 
00044 void MeshLoaderPLY::info_callback(const std::string &filename, 
00045                                   std::size_t line_number, 
00046                                   const std::string &message)
00047 {
00048    std::cerr << filename << ":" << line_number << ": " << "info: " << message << std::endl;
00049 }
00050 
00051 void MeshLoaderPLY::warning_callback(const std::string &filename, 
00052                                      std::size_t line_number, 
00053                                      const std::string &message)
00054 {
00055    std::cerr << filename << ":" << line_number << ": " << "warning: " << message << std::endl;
00056 }
00057 
00058 void MeshLoaderPLY::error_callback(const std::string &filename, 
00059                                    std::size_t line_number, 
00060                                    const std::string &message)
00061 {
00062    std::cerr << filename << ":" << line_number << ": " << "error: " << message << std::endl;
00063 }
00064 
00065 std::tr1::tuple<std::tr1::function<void()>, std::tr1::function<void()> > 
00066 MeshLoaderPLY::element_definition_callback(const std::string& element_name, 
00067                                            std::size_t count)
00068 {
00069    if (element_name == "vertex") {
00070       return std::tr1::tuple<std::tr1::function<void()>, std::tr1::function<void()> >(
00071          std::tr1::bind(&MeshLoaderPLY::vertex_begin, this),
00072          std::tr1::bind(&MeshLoaderPLY::vertex_end, this)
00073          );
00074    }
00075    else if (element_name == "face") {
00076       return std::tr1::tuple<std::tr1::function<void()>, std::tr1::function<void()> >(
00077          std::tr1::bind(&MeshLoaderPLY::face_begin, this),
00078          std::tr1::bind(&MeshLoaderPLY::face_end, this)
00079          );
00080    }
00081    else {
00082       return std::tr1::tuple<std::tr1::function<void()>, std::tr1::function<void()> >(0, 0);
00083    }
00084 }
00085 
00086 std::tr1::function<void (ply::float32)> 
00087 MeshLoaderPLY::scalar_property_definition_callback(const std::string& element_name, 
00088                                                    const std::string& property_name)
00089 {
00090    if (element_name == "vertex") {
00091       if (property_name == "x") {
00092          return std::tr1::bind(&MeshLoaderPLY::vertex_x, this, _1);
00093       }
00094       else if (property_name == "y") {
00095          return std::tr1::bind(&MeshLoaderPLY::vertex_y, this, _1);
00096       }
00097       else if (property_name == "z") {
00098          return std::tr1::bind(&MeshLoaderPLY::vertex_z, this, _1);
00099       }
00100       else {
00101          return 0;
00102       }
00103    }
00104    else {
00105       return 0;
00106    }
00107 }
00108 
00109 std::tr1::tuple<std::tr1::function<void (ply::uint8)>, std::tr1::function<void (ply::int32)>, std::tr1::function<void ()> > 
00110 MeshLoaderPLY::list_property_definition_callback(const std::string& element_name, 
00111                                                  const std::string& property_name)
00112 {
00113    if ((element_name == "face") && (property_name == "vertex_indices")) {
00114       return std::tr1::tuple<std::tr1::function<void (ply::uint8)>, std::tr1::function<void (ply::int32)>, std::tr1::function<void ()> >(
00115          std::tr1::bind(&MeshLoaderPLY::face_vertex_indices_begin, this, _1),
00116          std::tr1::bind(&MeshLoaderPLY::face_vertex_indices_element, this, _1),
00117          std::tr1::bind(&MeshLoaderPLY::face_vertex_indices_end, this)
00118          );
00119    }
00120    else {
00121       return std::tr1::tuple<std::tr1::function<void (ply::uint8)>, std::tr1::function<void (ply::int32)>, std::tr1::function<void ()> >(0, 0, 0);
00122    }
00123 }
00124 
00125 void MeshLoaderPLY::vertex_begin()
00126 { }
00127 
00128 void MeshLoaderPLY::vertex_x(ply::float32 x)
00129 {
00130    m_x = x;
00131 }
00132 
00133 void MeshLoaderPLY::vertex_y(ply::float32 y)
00134 {
00135    m_y = y;
00136 }
00137 
00138 void MeshLoaderPLY::vertex_z(ply::float32 z)
00139 {
00140    m_z = z;
00141 }
00142 
00143 void MeshLoaderPLY::vertex_end() {
00144    //(*ostream_) << "v " << vertex_x_ << " " << vertex_y_ << " " << vertex_z_ << "\n";
00145    
00146    _data.vertices.push_back(Vertex(m_x, m_y, m_z));
00147 }
00148 
00149 void MeshLoaderPLY::face_begin()
00150 { }
00151 
00152 void MeshLoaderPLY::face_vertex_indices_begin(ply::uint8 size) {
00153    m_faceIndex = 0;
00154 }
00155 
00156 void MeshLoaderPLY::face_vertex_indices_element(ply::int32 vertex_index) {
00157    if (m_faceIndex == 0) {
00158       m_firstFaceIndex = vertex_index;
00159    } else {
00160       if (m_faceIndex > 1) {
00161          // completed another triangle face
00162          _data.triangles.push_back(
00163             MeshTriangle(m_firstFaceIndex, m_prevFaceIndex, vertex_index));
00164       }
00165       
00166       m_prevFaceIndex = vertex_index;
00167    }
00168    
00169    ++m_faceIndex;
00170 }
00171 
00172 void MeshLoaderPLY::face_vertex_indices_end() 
00173 { }
00174 
00175 void MeshLoaderPLY::face_end() 
00176 { }
00177 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6