ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
RendererHandle.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <fmt/format.h>
11 
12 #include <array>
13 #include <cstdint>
14 #include <functional>
15 #include <type_traits>
16 
17 namespace cloudViewer {
18 
19 namespace visualization {
20 namespace rendering {
21 
22 // If you add entry here, don't forget to update TypeToString!
23 enum class EntityType : std::uint16_t {
24  None = 0,
25 
26  View,
27  Scene,
28 
29  Geometry,
30  Light,
32  Skybox,
33  Camera,
34  Material,
36  Texture,
38 
41 
42  Count
43 };
44 
45 // RenderEntityHandle - handle type for entities inside Renderer
46 // Can be used in STL containers as key
48  static const char* TypeToString(EntityType type);
49 
50  static const std::uint16_t kBadId = 0;
52 
53  inline size_t Hash() const {
54  return static_cast<std::uint16_t>(type) << 16 | id;
55  }
56 
57  bool operator==(const REHandle_abstract& other) const {
58  return id == other.id && type == other.type;
59  }
60 
61  bool operator!=(const REHandle_abstract& other) const {
62  return !operator==(other);
63  }
64 
65  bool operator<(const REHandle_abstract& other) const {
66  return Hash() < other.Hash();
67  }
68 
69  explicit operator bool() const { return id != kBadId; }
70 
72 
73  std::uint16_t GetId() const { return id; }
74 
75 protected:
76  REHandle_abstract(const EntityType aType, const std::uint16_t aId)
77  : type(aType), id(aId) {}
78 
79  static std::array<std::uint16_t, static_cast<size_t>(EntityType::Count)>
81 
82  std::uint16_t id = kBadId;
83 };
84 
85 std::ostream& operator<<(std::ostream& os, const REHandle_abstract& uid);
86 
87 // REHandle is used for specification of handle types to prevent
88 // errors with passing, assigning or comparison of different kinds of handles
89 template <EntityType entityType>
90 struct REHandle : public REHandle_abstract {
91  static const REHandle kBad;
92 
93  static REHandle Next() {
94  const auto index = static_cast<std::uint16_t>(entityType);
95  auto id = ++uid_table[index];
96  if (id == REHandle_abstract::kBadId) {
99  }
100 
101  return std::move(REHandle(id));
102  }
103 
104  static REHandle Concretize(const REHandle_abstract& abstract) {
105  if (abstract.type != entityType) {
106  // assert("Incompatible render uid types!\n");
107  return REHandle();
108  }
109 
110  return REHandle(abstract.GetId());
111  }
112 
114  REHandle(const REHandle& other) : REHandle_abstract(entityType, other.id) {}
115  // Don't use this constructor unless you know what you are doing
116  explicit REHandle(std::uint16_t id) : REHandle_abstract(entityType, id) {}
117 
118  REHandle& operator=(const REHandle& other) {
119  id = other.id;
120  return *this;
121  }
122 };
123 
124 template <EntityType entityType>
125 const REHandle<entityType> REHandle<entityType>::kBad;
126 
140 
141 } // namespace rendering
142 } // namespace visualization
143 } // namespace cloudViewer
144 
146 namespace std {
147 template <>
148 class hash<cloudViewer::visualization::rendering::REHandle_abstract> {
149 public:
150  size_t operator()(
152  const {
153  return uid.Hash();
154  }
155 };
156 } // namespace std
157 
158 namespace fmt {
159 template <typename T>
160 struct formatter<
161  T,
162  std::enable_if_t<std::is_base_of<cloudViewer::visualization::rendering::
163  REHandle_abstract,
164  T>::value,
165  char>> {
166  template <typename FormatContext>
167  auto format(
169  FormatContext& ctx) const -> decltype(ctx.out()) {
170  return format_to(ctx.out(), "[{}, {}, hash: {}]",
172  REHandle_abstract::TypeToString(uid.type),
173  uid.GetId(), uid.Hash());
174  }
175 
176  template <typename ParseContext>
177  constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
178  return ctx.begin();
179  }
180 };
181 } // namespace fmt
filament::Texture::InternalFormat format
REHandle< EntityType::Camera > CameraHandle
REHandle< EntityType::Scene > SceneHandle
REHandle< EntityType::Geometry > GeometryHandle
REHandle< EntityType::Texture > TextureHandle
REHandle< EntityType::Material > MaterialHandle
REHandle< EntityType::VertexBuffer > VertexBufferHandle
REHandle< EntityType::RenderTarget > RenderTargetHandle
REHandle< EntityType::IndirectLight > IndirectLightHandle
REHandle< EntityType::Light > LightHandle
REHandle< EntityType::Skybox > SkyboxHandle
REHandle< EntityType::IndexBuffer > IndexBufferHandle
REHandle< EntityType::View > ViewHandle
std::ostream & operator<<(std::ostream &os, const REHandle_abstract &uid)
REHandle< EntityType::MaterialInstance > MaterialInstanceHandle
Generic file read and write utility for python interface.
Definition: Eigen.h:85
bool operator<(const REHandle_abstract &other) const
static const char * TypeToString(EntityType type)
REHandle_abstract(const EntityType aType, const std::uint16_t aId)
bool operator!=(const REHandle_abstract &other) const
bool operator==(const REHandle_abstract &other) const
static std::array< std::uint16_t, static_cast< size_t >EntityType::Count)> uid_table
REHandle & operator=(const REHandle &other)
static REHandle Concretize(const REHandle_abstract &abstract)