licornea_tools
ply_importer.h
Go to the documentation of this file.
1 #ifndef LICORNEA_PLY_IMPORTER_H_
2 #define LICORNEA_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 #include <stdexcept>
16 
17 #include "common.h"
18 #include "io.h"
19 #include "point.h"
20 
21 namespace tlz {
22 
23 class ply_importer_error : public std::runtime_error {
24 public:
25  using std::runtime_error::runtime_error;
26 };
27 
28 
30 
33 class ply_importer {
34 private:
35  const static std::size_t maximal_ascii_element_line_length_ = 256;
36 
37  enum property_type {
38  none, int8, uint8, int16, uint16, int32, uint32, float32, float64, list
39  };
40 
41  struct property {
42  property_type type = none;
43  std::ptrdiff_t offset;
44  std::ptrdiff_t index;
45 
46  explicit operator bool() const { return (type != none); }
47  };
48 
49  std::ifstream file_;
50  line_delimitor line_delimitor_;
51  std::ifstream::pos_type vertex_data_start_;
52  std::size_t number_of_vertices_;
53  enum { binary_big_endian, binary_little_endian, ascii } format_;
54  std::size_t vertex_length_;
55  std::size_t number_of_properties_;
56  bool has_rgb_;
57  bool has_normal_;
58  bool has_weight_;
59  property x_, y_, z_, r_, g_, b_, nx_, ny_, nz_, w_;
60 
61  std::ptrdiff_t current_element_;
62 
63 
64  void skip_lines_(std::size_t n) {
65  while(n--) skip_line(file_, line_delimitor_);
66  }
67 
68  void read_line_(std::string& ln) {
69  read_line(file_, ln, line_delimitor_);
70  }
71 
72  bool is_host_endian_binary_() const {
73  if(host_is_little_endian) return (format_ == binary_little_endian);
74  else return (format_ == binary_big_endian);
75  }
76 
77  property* identify_property_(const std::string& nm);
78  static property_type identify_property_type_(const std::string& nm);
79  static std::size_t property_type_size_(property_type t);
80  void read_header_();
81 
82  template<typename Point> void read_ascii_(Point* buffer, std::size_t n);
83  template<typename Point> void read_binary_(Point* buffer, std::size_t n);
84 
85  void read_ascii_point_(point_xyz& out_point, const char* props[]) const;
86  void read_ascii_point_(point_full& out_point, const char* props[]) const;
87  void read_binary_point_(point_xyz& out_point, byte* data) const;
88  void read_binary_point_(point_full& out_point, byte* data) const;
89  template<typename T> T read_binary_property_(const property& prop, byte* data) const;
90 
91 public:
92  explicit ply_importer(const std::string& filename, line_delimitor ld = line_delimitor::unknown);
93 
94  std::size_t size() const;
95 
96  void rewind();
97  std::ptrdiff_t tell() const;
98  void read(point_xyz*, std::size_t sz);
99  void read(point_full*, std::size_t sz);
100 
101  bool is_binary() const { return format_ != ascii; }
102  bool is_ascii() const { return format_ == ascii; }
103 };
104 
105 }
106 
107 #endif
108 
const bool host_is_little_endian
Definition: io.cc:30
bool is_ascii() const
Definition: ply_importer.h:102
void skip_line(std::istream &str, line_delimitor ld)
Definition: io.cc:68
void read_line(std::istream &str, std::string &line, line_delimitor ld)
Definition: io.cc:52
bool is_binary() const
Definition: ply_importer.h:101
Imports point cloud from PLY file.
Definition: ply_importer.h:33
line_delimitor
Definition: io.h:11
std::uint8_t byte
Definition: common.h:17