PLYwoot
Header-only C++17 library for parsing and writing PLY files
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1/*
2 This file is part of PLYwoot, a header-only PLY parser.
3
4 Copyright (C) 2023-2025, Ton van den Heuvel
5
6 PLYwoot is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef PLYWOOT_TYPES_HPP
21#define PLYWOOT_TYPES_HPP
22
24
25#include <algorithm>
26#include <cstdint>
27#include <optional>
28#include <ostream>
29#include <string>
30#include <utility>
31#include <vector>
32
33namespace plywoot {
34
36enum class PlyDataType { Char, UChar, Short, UShort, Int, UInt, Float, Double };
37
44inline std::ostream &operator<<(std::ostream &os, PlyDataType type)
45{
46 switch (type)
47 {
48 case PlyDataType::Char:
49 return os << "char";
50 case PlyDataType::UChar:
51 return os << "uchar";
52 case PlyDataType::Short:
53 return os << "short";
54 case PlyDataType::UShort:
55 return os << "ushort";
56 case PlyDataType::Int:
57 return os << "int";
58 case PlyDataType::UInt:
59 return os << "uint";
60 case PlyDataType::Float:
61 return os << "float";
62 case PlyDataType::Double:
63 return os << "double";
64 }
65
66 return os;
67}
68
72enum class PlyFormat {
73 Ascii,
74 BinaryBigEndian,
75 BinaryLittleEndian,
76};
77
81{
83 PlyProperty() = default;
88 PlyProperty(std::string name, PlyDataType type) : name_{std::move(name)}, type_{type} {}
96 : name_{std::move(name)}, type_{type}, isList_{true}, sizeType_{sizeType}
97 {
98 }
99
103 const std::string &name() const { return name_; }
107 PlyDataType type() const { return type_; }
112 bool isList() const { return isList_; }
116 PlyDataType sizeType() const { return sizeType_; }
117
123 inline friend bool operator==(const PlyProperty &x, const PlyProperty &y)
124 {
125 return x.type_ == y.type_ && x.isList_ == y.isList_ && x.sizeType_ == y.sizeType_ && x.name_ == y.name_;
126 }
127
133 inline friend bool operator!=(const PlyProperty &x, const PlyProperty &y) { return !(x == y); }
134
135private:
136 std::string name_;
137 PlyDataType type_;
138
139 bool isList_{false};
140 PlyDataType sizeType_{PlyDataType::Char};
141};
142
145using PlyPropertyConstIterator = std::vector<PlyProperty>::const_iterator;
146
150{
152 PlyElement() = default;
157 PlyElement(std::string name, std::size_t size) : name_{std::move(name)}, size_{size} {}
165 PlyElement(std::string name, std::size_t size, std::vector<PlyProperty> properties)
166 : name_{std::move(name)}, size_{size}, properties_{std::move(properties)}
167 {
168 }
169
173 const std::string &name() const { return name_; }
177 std::size_t size() const { return size_; }
181 const std::vector<PlyProperty> &properties() const { return properties_; }
182
193 std::optional<PlyProperty> property(const std::string &propertyName) const
194 {
195 const auto it = std::find_if(properties_.begin(), properties_.end(), [&](const PlyProperty &p) {
196 return p.name() == propertyName;
197 });
198 return it != properties_.end() ? std::optional(*it) : std::nullopt;
199 }
200
207 template<typename... Args>
208 PlyProperty &addProperty(Args &&...args)
209 {
210 properties_.emplace_back(std::forward<Args>(args)...);
211 return properties_.back();
212 }
213
221 inline friend bool operator==(const PlyElement &x, const PlyElement &y)
222 {
223 return x.size_ == y.size_ && x.name_ == y.name_ && x.properties_ == y.properties_;
224 }
232 inline friend bool operator!=(const PlyElement &x, const PlyElement &y) { return !(x == y); }
233
234private:
236 std::string name_;
238 std::size_t size_;
240 std::vector<PlyProperty> properties_;
241};
242
246{
248 std::uint32_t line;
250 std::string text;
251
256 inline friend bool operator==(const Comment &x, const Comment &y)
257 {
258 return x.line == y.line && x.text == y.text;
259 }
264 inline friend bool operator!=(const Comment &x, const Comment &y) { return !(x == y); }
265};
266
267}
268
269#endif
std::string text
The comment text.
Definition types.hpp:250
std::uint32_t line
Line number in the PLY header where this comment originates from.
Definition types.hpp:248
friend bool operator!=(const Comment &x, const Comment &y)
Definition types.hpp:264
friend bool operator==(const Comment &x, const Comment &y)
Definition types.hpp:256
std::optional< PlyProperty > property(const std::string &propertyName) const
Definition types.hpp:193
const std::vector< PlyProperty > & properties() const
Definition types.hpp:181
PlyElement(std::string name, std::size_t size)
Definition types.hpp:157
PlyProperty & addProperty(Args &&...args)
Definition types.hpp:208
friend bool operator==(const PlyElement &x, const PlyElement &y)
Definition types.hpp:221
const std::string & name() const
Definition types.hpp:173
PlyElement()=default
Default constructor.
std::size_t size() const
Definition types.hpp:177
friend bool operator!=(const PlyElement &x, const PlyElement &y)
Definition types.hpp:232
PlyElement(std::string name, std::size_t size, std::vector< PlyProperty > properties)
Definition types.hpp:165
PlyProperty()=default
Default constructor.
PlyDataType type() const
Definition types.hpp:107
bool isList() const
Definition types.hpp:112
const std::string & name() const
Definition types.hpp:103
PlyDataType sizeType() const
Definition types.hpp:116
friend bool operator==(const PlyProperty &x, const PlyProperty &y)
Definition types.hpp:123
PlyProperty(std::string name, PlyDataType type)
Definition types.hpp:88
friend bool operator!=(const PlyProperty &x, const PlyProperty &y)
Definition types.hpp:133
PlyProperty(std::string name, PlyDataType type, PlyDataType sizeType)
Definition types.hpp:95
std::vector< PlyProperty >::const_iterator PlyPropertyConstIterator
Definition types.hpp:145
PlyFormat
Definition types.hpp:72
PlyDataType
Enumeration of data types supported by the PLY format.
Definition types.hpp:36