Commit 869271ce authored by Romain Pacanowski's avatar Romain Pacanowski

more robust functions to parse data depending on the floating point precision required

parent 685d26c0
......@@ -14,8 +14,117 @@
#include "tinyply/tinyply.h"
namespace mrf
{namespace io
{
namespace io
{
inline unsigned int sizeOfPlyType(
tinyply::PlyData const * data)
{
switch( data->t )
{
case tinyply::Type::INVALID:
{
return 0;
}
case tinyply::Type::INT8:
{
return sizeof(int) / 4;
}
case tinyply::Type::UINT8:
{
return sizeof(unsigned int) / 4;
}
case tinyply::Type::INT16:
{
return sizeof(int)/2;
}
case tinyply::Type::UINT16:
{
return sizeof(unsigned int)/2;
}
case tinyply::Type::INT32:
{
return sizeof(int);
}
case tinyply::Type::UINT32:
{
return sizeof(unsigned int);
}
case tinyply::Type::FLOAT32:
{
return sizeof(float);
}
case tinyply::Type::FLOAT64:
{
return sizeof(double);
}
default:
return 0;
}
}
inline std::string PlyTypeToString( tinyply::PlyData const * data)
{
switch( data->t )
{
case tinyply::Type::INVALID:
{
return std::string("INVALID");
}
case tinyply::Type::INT8:
{
return std::string("INT8");
}
case tinyply::Type::UINT8:
{
return std::string("UINT8");
}
case tinyply::Type::INT16:
{
return std::string("INT16");
}
case tinyply::Type::UINT16:
{
return std::string("UINT16");
}
case tinyply::Type::INT32:
{
return std::string("INT32");
}
case tinyply::Type::UINT32:
{
return std::string("UINT32");
}
case tinyply::Type::FLOAT32:
{
return std::string("FLOAT32");
}
case tinyply::Type::FLOAT64:
{
return std::string("FLOAT64");
}
default:
return std::string("");
}
}
template<class VEC3_TYPE>
inline void loadPLY( std::string const & ply_filename,
......@@ -26,6 +135,8 @@ inline void loadPLY( std::string const & ply_filename,
std::vector<unsigned int> & index_faces )
{
std::cout << " PLY FILENAME = " << ply_filename << std::endl;
try
{
std::ifstream ss(ply_filename, std::ios::binary);
......@@ -54,6 +165,7 @@ inline void loadPLY( std::string const & ply_filename,
// Tinyply treats parsed data as untyped byte buffers. See below for examples.
std::shared_ptr<tinyply::PlyData> tiny_vertices, tiny_normals, tiny_faces, tiny_texcoords, tiny_color_per_vertex;
// The header information can be used to programmatically extract properties on elements
// known to exist in the header prior to reading the data. For brevity of this sample, properties
// like vertex position are hard-coded:
......@@ -115,21 +227,83 @@ inline void loadPLY( std::string const & ply_filename,
file.read(ss);
if (tiny_vertices)
std::cout << "\tRead vertices:" << tiny_vertices->count;
std::cout << "\tRead vertices:" << tiny_vertices->count << std::endl;
if (tiny_color_per_vertex)
std::cout << "\tRead color per verterx:" << tiny_color_per_vertex->count << std::endl;
if (tiny_texcoords)
std::cout << "\tRead texcoords:" << tiny_texcoords->count << std::endl;;
std::cout << "\tRead texcoords:" << tiny_texcoords->count << std::endl;
if (tiny_faces)
std::cout << "\tRead faces(triangles):" << tiny_faces->count << std::endl;
std::cout << " Vertices = " << PlyTypeToString( tiny_vertices.get() ) << " Count " << tiny_vertices->count << " " << std::endl;
std::cout << " VEC3 sizeof underlyint type " << VEC3_TYPE::sizeOfT() << std::endl;
if( (tiny_vertices->t != tinyply::Type::FLOAT32) && (tiny_vertices->t != tinyply::Type::FLOAT64 ) )
{
std::cerr << " Unsupported Type for vertices. Got " << PlyTypeToString(tiny_vertices.get()) << " Expecting FLOAT32 or FLOAT64" << std::endl;
throw std::runtime_error("Unsupported vertex Types for PLY file");
}
// Prepare Copy
vertices.resize(tiny_vertices->count);
if (sizeOfPlyType( tiny_vertices.get() ) == VEC3_TYPE::sizeOfT()) // Direct copy
{
const size_t numVerticesBytes = tiny_vertices->buffer.size_bytes();
std::cout << " numVertics BYTES = " << numVerticesBytes << " for " << tiny_vertices->count << " vertices " << std::endl;
std::memcpy((void *)vertices.data(), (const void *)tiny_vertices->buffer.get(), (size_t)numVerticesBytes);
}
else
{
std::cout << " Reading Ply. Slow copy " << std::endl;
if (sizeOfPlyType( tiny_vertices.get() ) > VEC3_TYPE::sizeOfT() )
{
std::cout << " Precision of vertices positions will be lost " << std::endl;
}
if( tinyply::Type::FLOAT32 == tiny_vertices->t )
{
float* ply_raw_vertices = reinterpret_cast<float*>(tiny_vertices->buffer.get() );
// Todo Openmp
for (unsigned int i = 0; i < tiny_vertices->count; i++)
{
float const x = ply_raw_vertices[3*i];
float const y = ply_raw_vertices[3*i+1];
float const z = ply_raw_vertices[3*i+2];
vertices[i] = VEC3_TYPE(x, y, z);
}
}
else
{
double *ply_raw_vertices = reinterpret_cast<double *>(tiny_vertices->buffer.get());
// TODO OpenMP
for (unsigned int i = 0; i < tiny_vertices->count; i++)
{
double const x = ply_raw_vertices[3 * i];
double const y = ply_raw_vertices[3 * i + 1];
double const z = ply_raw_vertices[3 * i + 2];
vertices[i] = VEC3_TYPE(x, y, z);
}
}
}
const size_t numVerticesBytes = tiny_vertices->buffer.size_bytes();
vertices.resize(tiny_vertices->count);
std::memcpy((void *)vertices.data(), (const void *)tiny_vertices->buffer.get(), (size_t)numVerticesBytes);
std::cout << " INSIDE LOAD PLY vertices[0] = " << vertices[0] << " " << std::endl;
if (tiny_texcoords)
{
......@@ -181,9 +355,6 @@ inline void loadPLY( std::string const & ply_filename,
color_per_vertex.resize(tiny_color_per_vertex->count);
std::memcpy((void *)color_per_vertex.data(), (const void *)tiny_color_per_vertex->buffer.get(), (size_t)num_tcpv_Bytes);
}
}
catch (const std::exception &e)
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment