licornea_tools
ply_exporter.cc
Go to the documentation of this file.
1 #include "ply_exporter.h"
2 #include "common.h"
3 #include <iomanip>
4 #include <stdexcept>
5 #include <cassert>
6 
7 namespace tlz {
8 
9 namespace {
10  constexpr std::size_t vertex_count_string_length_ = 15;
11 }
12 
13 ply_exporter::ply_exporter(const std::string& filename, bool full, bool ascii, line_delimitor ld) :
14  file_(filename, std::ios_base::out | std::ios_base::trunc | std::ios_base::binary),
15  line_delimitor_(ld),
16  full_(full),
17  ascii_(ascii)
18 {
19  write_line_("ply");
20  if(ascii_) write_line_("format ascii 1.0");
21  else if(host_is_little_endian) write_line_("format binary_little_endian 1.0");
22  else write_line_("format binary_big_endian 1.0");
23 
24  write_line_("comment PLY file generated using tlz::ply_exporter");
25 
26  file_ << "element vertex ";
27  file_ << std::flush;
28  vertex_count_string_position_ = file_.tellp();
29  file_ << std::setfill(' ') << std::left << std::setw(vertex_count_string_length_) << 0;
30  write_line_("");
31 
32  write_line_("property float x");
33  write_line_("property float y");
34  write_line_("property float z");
35  if(full_) {
36  write_line_("property uchar red");
37  write_line_("property uchar green");
38  write_line_("property uchar blue");
39  }
40 
41  write_line_("end_header");
42 }
43 
44 
46  close();
47 }
48 
49 
51  // write vertex count to file
52  file_.seekp(vertex_count_string_position_);
53  file_ << std::setfill(' ') << std::left << std::setw(vertex_count_string_length_) << count_;
54 
55  // close
56  file_.close();
57 }
58 
59 
60 void ply_exporter::write_line_(const std::string& ln) {
61  write_line(file_, ln, line_delimitor_);
62 }
63 
64 
65 void ply_exporter::write_binary_(const point_xyz& p) {
66  float pos[3] = { p.x, p.y, p.z };
67  file_.write(reinterpret_cast<const char*>(&pos), 3 * sizeof(float));
68  count_++;
69 }
70 
71 
72 void ply_exporter::write_ascii_(const point_xyz& p) {
73  file_ << p.x << ' ' << p.y << ' ' << p.z;
74  end_line(file_, line_delimitor_);
75  count_++;
76 }
77 
78 
79 void ply_exporter::write_full_binary_(const point_full& p) {
80  float pos[3] = { p.x, p.y, p.z };
81  rgb_color col = p.color;
82 
83  file_.write(reinterpret_cast<const char*>(&pos), 3 * sizeof(float));
84  file_.write(reinterpret_cast<const char*>(&col), 3);
85  count_++;
86 }
87 
88 
89 void ply_exporter::write_full_ascii_(const point_full& p) {
90  file_ << p.x << ' ' << p.y << ' ' << p.z
91  << ' ' << (unsigned)p.color.r << ' ' << (unsigned)p.color.g << ' ' << (unsigned)p.color.b;
92  end_line(file_, line_delimitor_);
93  count_++;
94 }
95 
96 }
const bool host_is_little_endian
Definition: io.cc:30
void end_line(std::ostream &str, line_delimitor ld)
Definition: io.cc:88
float y
Definition: point.h:15
std::uint8_t b
Definition: color.h:21
std::uint8_t r
Definition: color.h:19
rgb_color color
Definition: point.h:42
std::uint8_t g
Definition: color.h:20
ply_exporter(const std::string &filename, bool full=true, bool ascii=false, line_delimitor ld=line_delimitor::LF)
Definition: ply_exporter.cc:13
float x
Definition: point.h:15
RGB color, 8 bit.
Definition: color.h:11
line_delimitor
Definition: io.h:11
float z
Definition: point.h:15
void write_line(std::ostream &str, const std::string &line, line_delimitor ld)
Definition: io.cc:82