mirror of https://github.com/CGAL/cgal
Update PLY IO for Point_set_3
This commit is contained in:
parent
9ff95550e9
commit
d30b7a4ef8
|
|
@ -29,27 +29,11 @@
|
||||||
namespace CGAL
|
namespace CGAL
|
||||||
{
|
{
|
||||||
|
|
||||||
/*!
|
namespace Ply
|
||||||
|
{
|
||||||
|
|
||||||
\ingroup PkgPointSet3
|
namespace internal
|
||||||
|
{
|
||||||
\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`
|
|
||||||
*/
|
|
||||||
|
|
||||||
template <typename Point,
|
template <typename Point,
|
||||||
typename Vector = typename Kernel_traits<Point>::Kernel::Vector_3>
|
typename Vector = typename Kernel_traits<Point>::Kernel::Vector_3>
|
||||||
|
|
@ -100,7 +84,7 @@ public:
|
||||||
: m_point_set (point_set), m_use_floats (false)
|
: 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
|
const std::vector<internal::Ply_read_number*>& readers
|
||||||
= reader.readers();
|
= reader.readers();
|
||||||
|
|
@ -187,11 +171,6 @@ public:
|
||||||
|
|
||||||
if (has_normal[0] && has_normal[1] && has_normal[2])
|
if (has_normal[0] && has_normal[1] && has_normal[2])
|
||||||
m_point_set.add_normal_map();
|
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)
|
void process_line (Ply_reader& reader)
|
||||||
|
|
@ -229,6 +208,10 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
/// \endcond
|
/// \endcond
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -110,11 +110,33 @@ read_ply_point_set(
|
||||||
std::istream& stream, ///< input stream.
|
std::istream& stream, ///< input stream.
|
||||||
CGAL::Point_set_3<Point, Vector>& point_set) ///< point set
|
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
|
Ply::internal::Ply_reader reader;
|
||||||
(stream, interpreter,
|
|
||||||
typename Kernel_traits<Point>::Kernel());
|
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;
|
stream << point_set;
|
||||||
return true;
|
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:
|
public:
|
||||||
virtual ~Abstract_property_printer() { }
|
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>
|
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;
|
stream << get(m_pmap, index);
|
||||||
oss.precision (std::numeric_limits<double>::digits10 + 2);
|
}
|
||||||
oss << get(m_pmap, index);
|
};
|
||||||
return oss.str();
|
|
||||||
|
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;
|
if (get_mode(stream) == IO::ASCII)
|
||||||
oss << (int)(get(m_pmap, index));
|
stream << int(get(m_pmap, index));
|
||||||
return oss.str();
|
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;
|
typedef CGAL::Point_set_3<Point, Vector> Point_set;
|
||||||
|
|
||||||
os << "ply" << std::endl
|
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
|
<< "comment Generated by the CGAL library" << std::endl
|
||||||
<< "element vertex " << ps.number_of_points() << std::endl;
|
<< "element vertex " << ps.number_of_points() << std::endl;
|
||||||
|
|
||||||
|
|
@ -340,7 +379,7 @@ std::ostream& operator<<(std::ostream& os,
|
||||||
if (okay)
|
if (okay)
|
||||||
{
|
{
|
||||||
os << "property short " << prop[i] << std::endl;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -350,7 +389,7 @@ std::ostream& operator<<(std::ostream& os,
|
||||||
if (okay)
|
if (okay)
|
||||||
{
|
{
|
||||||
os << "property ushort " << prop[i] << std::endl;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -360,7 +399,7 @@ std::ostream& operator<<(std::ostream& os,
|
||||||
if (okay)
|
if (okay)
|
||||||
{
|
{
|
||||||
os << "property int " << prop[i] << std::endl;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -370,7 +409,7 @@ std::ostream& operator<<(std::ostream& os,
|
||||||
if (okay)
|
if (okay)
|
||||||
{
|
{
|
||||||
os << "property float " << prop[i] << std::endl;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -380,7 +419,7 @@ std::ostream& operator<<(std::ostream& os,
|
||||||
if (okay)
|
if (okay)
|
||||||
{
|
{
|
||||||
os << "property double " << prop[i] << std::endl;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -391,7 +430,12 @@ std::ostream& operator<<(std::ostream& os,
|
||||||
for (typename Point_set::const_iterator it = ps.begin(); it != ps.end(); ++ it)
|
for (typename Point_set::const_iterator it = ps.begin(); it != ps.end(); ++ it)
|
||||||
{
|
{
|
||||||
for (std::size_t i = 0; i < printers.size(); ++ i)
|
for (std::size_t i = 0; i < printers.size(); ++ i)
|
||||||
os << printers[i]->get_string(*it) << " ";
|
{
|
||||||
|
printers[i]->print(os, *it);
|
||||||
|
if (get_mode (os) == IO::ASCII)
|
||||||
|
os << " ";
|
||||||
|
}
|
||||||
|
if (get_mode (os) == IO::ASCII)
|
||||||
os << std::endl;
|
os << std::endl;
|
||||||
}
|
}
|
||||||
return os;
|
return os;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue