mf
Media Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ply_importer.h
Go to the documentation of this file.
1 #ifndef MF_PLY_IMPORTER_H_
2 #define MF_PLY_IMPORTER_H_
3 
4 #include <fstream>
5 #include <string>
6 #include <cassert>
7 #include <vector>
8 #include <array>
9 #include <cstdlib>
10 #include <cstdint>
11 #include <limits>
12 #include <utility>
13 #include <memory>
14 #include <cctype>
15 
16 #include "../utility/io.h"
17 
18 namespace mf {
19 
21 
24 class ply_importer {
25 private:
26  const static std::size_t maximal_ascii_element_line_length_ = 256;
27 
28  enum property_type {
29  none, int8, uint8, int16, uint16, int32, uint32, float32, float64, list
30  };
31 
32  struct property {
33  property_type type = none;
34  std::ptrdiff_t offset;
35  std::ptrdiff_t index;
36 
37  explicit operator bool() const { return (type != none); }
38  };
39 
40  std::ifstream file_;
41  line_delimitor line_delimitor_;
42  std::ifstream::pos_type vertex_data_start_;
43  std::size_t number_of_vertices_;
44  enum { binary_big_endian, binary_little_endian, ascii } format_;
45  std::size_t vertex_length_;
46  std::size_t number_of_properties_;
47  bool has_rgb_;
48  bool has_normal_;
49  bool has_weight_;
50  property x_, y_, z_, r_, g_, b_, nx_, ny_, nz_, w_;
51 
52  std::ptrdiff_t current_element_;
53 
54 
55  void skip_lines_(std::size_t n) {
56  while(n--) skip_line(file_, line_delimitor_);
57  }
58 
59  void read_line_(std::string& ln) {
60  read_line(file_, ln, line_delimitor_);
61  }
62 
63  bool is_host_endian_binary_() const {
64  if(host_is_little_endian) return (format_ == binary_little_endian);
65  else return (format_ == binary_big_endian);
66  }
67 
68  property* identify_property_(const std::string& nm);
69  static property_type identify_property_type_(const std::string& nm);
70  static std::size_t property_type_size_(property_type t);
71  void read_header_();
72 
73  template<typename Point> void read_ascii_(Point* buffer, std::size_t n);
74  template<typename Point> void read_binary_(Point* buffer, std::size_t n);
75 
76  void read_ascii_point_(point_xyz& out_point, const char* props[]) const;
77  void read_ascii_point_(point_full& out_point, const char* props[]) const;
78  void read_binary_point_(point_xyz& out_point, char* data) const;
79  void read_binary_point_(point_full& out_point, char* data) const;
80  template<typename T> T read_binary_property_(const property& prop, char* data) const;
81 
82 public:
83  explicit ply_importer(const char* filename, line_delimitor ld = line_delimitor::unknown);
84  explicit ply_importer(const std::string& filename, line_delimitor ld = line_delimitor::unknown) :
85  ply_importer(filename.c_str(), ld) { }
86 
87  std::size_t size() const override;
88  bool all_valid() const override;
89 
90  void rewind() override;
91  std::ptrdiff_t tell() const override;
92  void read(point_xyz*, std::size_t sz) override;
93  void read(point_full*, std::size_t sz) override;
94 
95  bool is_binary() const { return format_ != ascii; }
96  bool is_ascii() const { return format_ == ascii; }
97 };
98 
99 }
100 
101 #endif
void read(point_xyz *, std::size_t sz) override
const bool host_is_little_endian
Definition: io.cc:30
std::ptrdiff_t tell() const override
void rewind() override
bool is_ascii() const
Definition: ply_importer.h:96
Point cloud point with only XYZ coordinates.
Definition: point.h:14
ply_importer(const char *filename, line_delimitor ld=line_delimitor::unknown)
Imports point cloud from PLY file.
Definition: ply_importer.h:24
Point cloud point with XYZ coordinates, normal vector, weight, and RGB color.
Definition: point.h:52
line_delimitor
Definition: io.h:11
bool all_valid() const override
ply_importer(const std::string &filename, line_delimitor ld=line_delimitor::unknown)
Definition: ply_importer.h:84
bool is_binary() const
Definition: ply_importer.h:95
void skip_line(std::istream &str, line_delimitor ld)
Definition: io.cc:68
std::size_t size() const override
void read_line(std::istream &str, std::string &line, line_delimitor ld)
Definition: io.cc:52