COGS
Computer Graphics Structures library
PointCloud.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef COGS_POINT_CLOUD_H
7 #define COGS_POINT_CLOUD_H
8 #include <vector>
9 #include <set>
10 #include <unordered_map>
11 #include <optional>
12 #include <variant>
13 
14 #include <glm/glm.hpp>
15 #include <Utils/GeometryStructures.h>
16 #include <Utils/SpaceAndUnits.h>
17 
18 #include <COGS/DataType.h>
19 #include <COGS/PointCloudProperty.h>
20 
21 
22 namespace cogs
23 {
32  class COGS_API PointCloud
33  {
34  public:
36  using PointNormalPredicate = std::function<bool(const glm::vec3 &, const glm::vec3 &)>;
37 
48 
50  PointCloud();
52  virtual ~PointCloud();
53 
55  PointCloud(const PointCloud &source);
57  PointCloud &operator=(const PointCloud &source);
59  PointCloud(PointCloud &&) noexcept = default;
61  PointCloud &operator=(PointCloud &&) noexcept = default;
62 
64  virtual bool Import(const std::string &filename);
66  virtual bool Export(const std::string &filename) const;
67 
69  virtual bool Resize(uint32_t new_size);
71  [[nodiscard]] virtual uint32_t GetSize() const;
73  virtual void Reserve(uint32_t new_capacity);
75  [[nodiscard]] uint32_t GetCapacity() const;
77  virtual void ShrinkToFit();
79  virtual void Clear();
80 
96  virtual bool Erase(std::vector<uint32_t> indices_to_erase, std::vector<int> *replacement_map = nullptr);
97 
113  virtual bool Erase(const PointNormalPredicate &Predicate, std::vector<int> *replacement_map = nullptr);
114 
123  virtual bool Append(const PointCloud &pc);
124 
131  [[nodiscard]] glm::vec3 *GetPositions();
132 
139  [[nodiscard]] const glm::vec3 *GetPositions() const;
140 
147  [[nodiscard]] bool HasNormals() const;
148 
155  [[nodiscard]] glm::vec3 *GetNormals();
156 
163  [[nodiscard]] const glm::vec3 *GetNormals() const;
164 
171  const PointCloudProperty &AddNormals();
172 
179  [[nodiscard]] bool HasColors() const;
180 
187  [[nodiscard]] struct Color3f *GetColors();
188 
195  [[nodiscard]] const struct Color3f *GetColors() const;
196 
203  const PointCloudProperty &AddColors ();
204 
211  [[nodiscard]] bool HasIntensities() const;
212 
219  [[nodiscard]] float *GetIntensities();
220 
227  [[nodiscard]] const float *GetIntensities() const;
228 
235  const PointCloudProperty &AddIntensities();
236 
238  [[nodiscard]] const std::vector<PointCloudProperty> &GetProperties() const;
239 
250  const PointCloudProperty &AddProperty(const PointCloudProperty::Key &key, DataType type);
251 
262  const PointCloudProperty &AddProperty(const PointCloudProperty::Key &key, size_t bytes_per_point);
264  void RemoveProperty(const PointCloudProperty::Key &key);
266  void ClonePropertiesOf(const PointCloud &pc);
268  [[nodiscard]] bool HasProperty(const PointCloudProperty::Key &key) const;
270  [[nodiscard]] std::optional<PointCloudProperty> GetProperty(const PointCloudProperty::Key &key);
272  [[nodiscard]] std::optional<const PointCloudProperty> GetProperty(const PointCloudProperty::Key &key) const;
273 
275  [[nodiscard]] virtual void *GetVoidData(const PointCloudProperty::Key &key);
277  [[nodiscard]] const void *GetVoidData(const PointCloudProperty::Key &key) const;
278 
280  template <typename T> [[nodiscard]] T *GetData(const PointCloudProperty::Key &key);
282  template <typename T> [[nodiscard]] const T *GetData(const PointCloudProperty::Key &key) const;
283 
289  virtual void Transform(
290  const glm::mat4 &transformation,
291  const std::optional<utils::SpaceDefinition> &resulting_space = std::nullopt);
292 
294  bool HasSpace() const;
296  const std::optional<utils::SpaceDefinition> &GetSpace() const;
298  std::string GetSpaceId() const;
300  void SetSpace(const std::optional<utils::SpaceDefinition> &new_space);
302  bool TransformToSpace(const utils::SpaceDefinition &new_space);
303 
304  protected:
305 
307  struct CopyCommand
308  {
309  uint32_t source;
310  uint32_t target;
311  uint32_t length;
312  };
313 
315  void MakeCloneOf(const PointCloud &source);
317  virtual void ExecuteCopyCommands(const std::vector<CopyCommand> &copy_info);
319  virtual void TruncateSize(size_t delta_size);
320 
321  private:
322 
324  struct Chunk
325  {
326  uint32_t first;
327  uint32_t last;
328  Chunk(uint32_t first, uint32_t last);
330  [[nodiscard]] bool IsValid() const;
332  [[nodiscard]] uint32_t GetSize() const;
333  };
334 
335  uint32_t capacity_;
336  uint32_t size_;
337  std::vector<PointCloudProperty> properties_;
338  std::unordered_map<PointCloudProperty::Key, size_t> property_map_;
339  std::optional<utils::SpaceDefinition> space_definition_;
340 
342  PointCloudProperty &AddProperty(
343  const PointCloudProperty::Key &key,
344  const std::variant<DataType, size_t> type_or_size);
346  [[nodiscard]] std::vector<Chunk> IndicesToChunks(const std::vector<uint32_t> &indices) const;
348  [[nodiscard]] std::vector<CopyCommand> CreateEraseCopyCommands(std::vector<Chunk> chunks_to_erases) const;
349  };
350 
351 
352 
353  template <typename T>
355  {
356  return reinterpret_cast<const T *>(GetVoidData(key));
357  }
358 
359  template <typename T>
361  {
362  return reinterpret_cast<T *>(GetVoidData(key));
363  }
364 
365  [[nodiscard]] COGS_API geom::Aabb3 GetPointCloudAabb(const PointCloud &cloud);
366  COGS_API void ErasePointsOutsideAabb(const geom::Aabb3 &aabb, PointCloud &cloud);
367 
368 }
369 
370 
371 #endif /* !COGS_POINT_CLOUD_H */
static const cogs::PointCloudProperty::Key INTENSITIES
Common identifier for point intensities.
Definition: PointCloud.h:47
Namespace of "Computer Graphic Structures" library, which implement many core classes and structures...
Definition: AabbHierarchy.h:8
A command for copying points.
Definition: PointCloud.h:307
Holds three color channels in float format in range 0-1.
Definition: Color.h:57
Class used for effective management of transformation matrix by handling translation, rotation and scaling separately.
Definition: Transform.h:26
uint32_t length
Number of sequential points to copy.
Definition: PointCloud.h:311
Management class for a set of data points in a 3D space.
Definition: PointCloud.h:32
std::function< bool(const glm::vec3 &, const glm::vec3 &)> PointNormalPredicate
A predicate over points and normals. A function that returns true or false.
Definition: PointCloud.h:36
uint32_t target
Index of the first point that should be replaced.
Definition: PointCloud.h:310
uint32_t source
Index of the first point that should be copied.
Definition: PointCloud.h:309
static const cogs::PointCloudProperty::Key POSITIONS
Common identifier for point positions.
Definition: PointCloud.h:39
static const cogs::PointCloudProperty::Key UVS
Common identifier for point texture coordinates.
Definition: PointCloud.h:45
Type used to store information about property.
Definition: PointCloudProperty.h:14
static const cogs::PointCloudProperty::Key COLORS
Common identifier for point colors.
Definition: PointCloud.h:43
std::string Key
Type used to identify cloud property record.
Definition: PointCloudProperty.h:17
T * GetData(const PointCloudProperty::Key &key)
When property exists, returns the pointer to data of a template type, nullptr otherwise.
Definition: PointCloud.h:360
DataType
Identifier of data types.
Definition: DataType.h:11
static const cogs::PointCloudProperty::Key NORMALS
Common identifier for point normals.
Definition: PointCloud.h:41