Update PLY IO for Point_set_3

This commit is contained in:
Simon Giraudot 2017-01-24 12:59:17 +01:00
parent 9ff95550e9
commit d30b7a4ef8
2 changed files with 84 additions and 57 deletions

View File

@ -29,28 +29,12 @@
namespace CGAL
{
/*!
\ingroup PkgPointSet3
\brief PLY interpreter designed to fill a `CGAL::Point_set_3` object.
This interpreter will instanciate any number of property needed to
store all PLY properties read in the header:
- points and normals are stored as usual `CGAL::Point_set_3`
properties (property "point" of type `CGAL::Point_3` and property
"normal" of type `CGAL::Vector_3`)
- other PLY properties are stored on point set properties with the
name and type given by the PLY header
\tparam Point Point type.
\tparam Vector Normal vector type.
\cgalModels `PlyInterpreter`
*/
namespace Ply
{
namespace internal
{
template <typename Point,
typename Vector = typename Kernel_traits<Point>::Kernel::Vector_3>
class Ply_interpreter_point_set_3
@ -100,7 +84,7 @@ public:
: m_point_set (point_set), m_use_floats (false)
{ }
bool is_applicable (Ply_reader& reader)
void instantiate_properties (Ply_reader& reader)
{
const std::vector<internal::Ply_read_number*>& readers
= reader.readers();
@ -187,11 +171,6 @@ public:
if (has_normal[0] && has_normal[1] && has_normal[2])
m_point_set.add_normal_map();
return (reader.does_tag_exist<float> ("x") || reader.does_tag_exist<double> ("x"))
&& (reader.does_tag_exist<float> ("y") || reader.does_tag_exist<double> ("y"))
&& (reader.does_tag_exist<float> ("z") || reader.does_tag_exist<double> ("z"));
}
void process_line (Ply_reader& reader)
@ -229,6 +208,10 @@ public:
}
};
}
}
}
/// \endcond

View File

@ -110,11 +110,33 @@ read_ply_point_set(
std::istream& stream, ///< input stream.
CGAL::Point_set_3<Point, Vector>& point_set) ///< point set
{
CGAL::Ply_interpreter_point_set_3<Point, Vector> interpreter (point_set);
if(!stream)
{
std::cerr << "Error: cannot open file" << std::endl;
return false;
}
return CGAL::read_ply_custom_points
(stream, interpreter,
typename Kernel_traits<Point>::Kernel());
Ply::internal::Ply_reader reader;
if (!(reader.init (stream)))
return false;
Ply::internal::Ply_interpreter_point_set_3<Point, Vector> interpreter (point_set);
interpreter.instantiate_properties (reader);
std::size_t points_read = 0;
while (!(stream.eof()) && points_read < reader.m_nb_points)
{
for (std::size_t i = 0; i < reader.readers().size (); ++ i)
reader.readers()[i]->get (stream);
interpreter.process_line (reader);
++ points_read;
}
return (points_read == reader.m_nb_points);
}
/*!
@ -167,14 +189,6 @@ write_ply_point_set(
stream << point_set;
return true;
// if (point_set.has_normal_map())
// return CGAL::write_ply_points_and_normals
// (stream, point_set.begin(), point_set.end(),
// point_set.point_map(), point_set.normal_map());
// return CGAL::write_ply_points
// (stream, point_set.begin(), point_set.end(),
// point_set.point_map());
}
@ -221,7 +235,7 @@ namespace internal
{
public:
virtual ~Abstract_property_printer() { }
virtual std::string get_string(const typename CGAL::Point_set_3<Point,Vector>::Index& index) = 0;
virtual void print (std::ostream& stream, const typename CGAL::Point_set_3<Point,Vector>::Index& index) = 0;
};
template <typename Point, typename Vector, typename Type>
@ -236,12 +250,33 @@ namespace internal
}
virtual std::string get_string(const typename CGAL::Point_set_3<Point,Vector>::Index& index)
virtual void print(std::ostream& stream, const typename CGAL::Point_set_3<Point,Vector>::Index& index)
{
std::ostringstream oss;
oss.precision (std::numeric_limits<double>::digits10 + 2);
oss << get(m_pmap, index);
return oss.str();
stream << get(m_pmap, index);
}
};
template <typename Point, typename Vector, typename Type>
class Simple_property_printer : public Abstract_property_printer<Point, Vector>
{
typedef typename CGAL::Point_set_3<Point, Vector> Point_set;
typedef typename Point_set::template Property_map<Type> Pmap;
Pmap m_pmap;
public:
Simple_property_printer (const Pmap& pmap) : m_pmap (pmap)
{
}
virtual void print(std::ostream& stream, const typename CGAL::Point_set_3<Point,Vector>::Index& index)
{
if (get_mode(stream) == IO::ASCII)
stream << get(m_pmap, index);
else
{
Type t = get (m_pmap, index);
stream.write (reinterpret_cast<char*>(&t), sizeof(t));
}
}
};
@ -257,11 +292,15 @@ namespace internal
}
virtual std::string get_string(const typename CGAL::Point_set_3<Point,Vector>::Index& index)
virtual void print(std::ostream& stream, const typename CGAL::Point_set_3<Point,Vector>::Index& index)
{
std::ostringstream oss;
oss << (int)(get(m_pmap, index));
return oss.str();
if (get_mode(stream) == IO::ASCII)
stream << int(get(m_pmap, index));
else
{
Type t = get (m_pmap, index);
stream.write (reinterpret_cast<char*>(&t), sizeof(t));
}
}
};
@ -284,7 +323,7 @@ std::ostream& operator<<(std::ostream& os,
typedef CGAL::Point_set_3<Point, Vector> Point_set;
os << "ply" << std::endl
<< "format ascii 1.0" << std::endl
<< ((get_mode(os) == IO::BINARY) ? "format binary_little_endian 1.0" : "format ascii 1.0") << std::endl
<< "comment Generated by the CGAL library" << std::endl
<< "element vertex " << ps.number_of_points() << std::endl;
@ -340,7 +379,7 @@ std::ostream& operator<<(std::ostream& os,
if (okay)
{
os << "property short " << prop[i] << std::endl;
printers.push_back (new internal::Property_printer<Point,Vector,boost::int16_t>(pmap));
printers.push_back (new internal::Simple_property_printer<Point,Vector,boost::int16_t>(pmap));
continue;
}
}
@ -350,7 +389,7 @@ std::ostream& operator<<(std::ostream& os,
if (okay)
{
os << "property ushort " << prop[i] << std::endl;
printers.push_back (new internal::Property_printer<Point,Vector,boost::uint16_t>(pmap));
printers.push_back (new internal::Simple_property_printer<Point,Vector,boost::uint16_t>(pmap));
continue;
}
}
@ -360,7 +399,7 @@ std::ostream& operator<<(std::ostream& os,
if (okay)
{
os << "property int " << prop[i] << std::endl;
printers.push_back (new internal::Property_printer<Point,Vector,boost::int32_t>(pmap));
printers.push_back (new internal::Simple_property_printer<Point,Vector,boost::int32_t>(pmap));
continue;
}
}
@ -370,7 +409,7 @@ std::ostream& operator<<(std::ostream& os,
if (okay)
{
os << "property float " << prop[i] << std::endl;
printers.push_back (new internal::Property_printer<Point,Vector,float>(pmap));
printers.push_back (new internal::Simple_property_printer<Point,Vector,float>(pmap));
continue;
}
}
@ -380,7 +419,7 @@ std::ostream& operator<<(std::ostream& os,
if (okay)
{
os << "property double " << prop[i] << std::endl;
printers.push_back (new internal::Property_printer<Point,Vector,double>(pmap));
printers.push_back (new internal::Simple_property_printer<Point,Vector,double>(pmap));
continue;
}
}
@ -391,8 +430,13 @@ std::ostream& operator<<(std::ostream& os,
for (typename Point_set::const_iterator it = ps.begin(); it != ps.end(); ++ it)
{
for (std::size_t i = 0; i < printers.size(); ++ i)
os << printers[i]->get_string(*it) << " ";
os << std::endl;
{
printers[i]->print(os, *it);
if (get_mode (os) == IO::ASCII)
os << " ";
}
if (get_mode (os) == IO::ASCII)
os << std::endl;
}
return os;
}