Open3D (C++ API)  0.15.1
RendererHandle.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <fmt/format.h>
30 
31 #include <array>
32 #include <cstdint>
33 #include <functional>
34 #include <type_traits>
35 
36 namespace open3d {
37 
38 namespace visualization {
39 namespace rendering {
40 
41 // If you add entry here, don't forget to update TypeToString!
42 enum class EntityType : std::uint16_t {
43  None = 0,
44 
45  View,
46  Scene,
47 
48  Geometry,
49  Light,
51  Skybox,
52  Camera,
53  Material,
55  Texture,
57 
60 
61  Count
62 };
63 
64 // RenderEntityHandle - handle type for entities inside Renderer
65 // Can be used in STL containers as key
67  static const char* TypeToString(EntityType type);
68 
69  static const std::uint16_t kBadId = 0;
71 
72  inline size_t Hash() const {
73  return static_cast<std::uint16_t>(type) << 16 | id;
74  }
75 
76  bool operator==(const REHandle_abstract& other) const {
77  return id == other.id && type == other.type;
78  }
79 
80  bool operator!=(const REHandle_abstract& other) const {
81  return !operator==(other);
82  }
83 
84  bool operator<(const REHandle_abstract& other) const {
85  return Hash() < other.Hash();
86  }
87 
88  explicit operator bool() const { return id != kBadId; }
89 
91 
92  std::uint16_t GetId() const { return id; }
93 
94 protected:
95  REHandle_abstract(const EntityType aType, const std::uint16_t aId)
96  : type(aType), id(aId) {}
97 
98  static std::array<std::uint16_t, static_cast<size_t>(EntityType::Count)>
100 
101  std::uint16_t id = kBadId;
102 };
103 
104 std::ostream& operator<<(std::ostream& os, const REHandle_abstract& uid);
105 
106 // REHandle is used for specification of handle types to prevent
107 // errors with passing, assigning or comparison of different kinds of handles
108 template <EntityType entityType>
109 struct REHandle : public REHandle_abstract {
110  static const REHandle kBad;
111 
112  static REHandle Next() {
113  const auto index = static_cast<std::uint16_t>(entityType);
114  auto id = ++uid_table[index];
115  if (id == REHandle_abstract::kBadId) {
117  id = REHandle_abstract::kBadId + 1;
118  }
119 
120  return std::move(REHandle(id));
121  }
122 
123  static REHandle Concretize(const REHandle_abstract& abstract) {
124  if (abstract.type != entityType) {
125  // assert("Incompatible render uid types!\n");
126  return REHandle();
127  }
128 
129  return REHandle(abstract.GetId());
130  }
131 
133  REHandle(const REHandle& other) : REHandle_abstract(entityType, other.id) {}
134  // Don't use this constructor unless you know what you are doing
135  explicit REHandle(std::uint16_t id) : REHandle_abstract(entityType, id) {}
136 
137  REHandle& operator=(const REHandle& other) {
138  id = other.id;
139  return *this;
140  }
141 };
142 
143 template <EntityType entityType>
144 const REHandle<entityType> REHandle<entityType>::kBad;
145 
159 
160 } // namespace rendering
161 } // namespace visualization
162 } // namespace open3d
163 
165 namespace std {
166 template <>
167 class hash<open3d::visualization::rendering::REHandle_abstract> {
168 public:
170  uid) const {
171  return uid.Hash();
172  }
173 };
174 } // namespace std
175 
176 namespace fmt {
177 template <typename T>
178 struct formatter<T, std::enable_if_t<std::is_base_of<open3d::visualization::rendering::REHandle_abstract, T>::value, char>> {
179  template <typename FormatContext>
181  FormatContext& ctx) -> decltype(ctx.out()) {
182  return format_to(ctx.out(), "[{}, {}, hash: {}]",
184  TypeToString(uid.type),
185  uid.GetId(), uid.Hash());
186  }
187 
188  template <typename ParseContext>
189  constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
190  return ctx.begin();
191  }
192 };
193 } // namespace fmt
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:214
REHandle< EntityType::IndexBuffer > IndexBufferHandle
Definition: RendererHandle.h:158
std::ostream & operator<<(std::ostream &os, const REHandle_abstract &uid)
Definition: RendererHandle.cpp:38
REHandle< EntityType::Material > MaterialHandle
Definition: RendererHandle.h:153
REHandle< EntityType::VertexBuffer > VertexBufferHandle
Definition: RendererHandle.h:157
REHandle< EntityType::Scene > SceneHandle
Definition: RendererHandle.h:147
EntityType
Definition: RendererHandle.h:42
REHandle< EntityType::MaterialInstance > MaterialInstanceHandle
Definition: RendererHandle.h:154
REHandle< EntityType::Geometry > GeometryHandle
Definition: RendererHandle.h:148
REHandle< EntityType::Camera > CameraHandle
Definition: RendererHandle.h:152
REHandle< EntityType::RenderTarget > RenderTargetHandle
Definition: RendererHandle.h:156
REHandle< EntityType::Light > LightHandle
Definition: RendererHandle.h:149
REHandle< EntityType::Texture > TextureHandle
Definition: RendererHandle.h:155
REHandle< EntityType::Skybox > SkyboxHandle
Definition: RendererHandle.h:151
REHandle< EntityType::View > ViewHandle
Definition: RendererHandle.h:146
REHandle< EntityType::IndirectLight > IndirectLightHandle
Definition: RendererHandle.h:150
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Device.h:138
bool operator!=(const REHandle_abstract &other) const
Definition: RendererHandle.h:80
std::uint16_t GetId() const
Definition: RendererHandle.h:92
bool operator<(const REHandle_abstract &other) const
Definition: RendererHandle.h:84
const EntityType type
Definition: RendererHandle.h:70
std::uint16_t id
Definition: RendererHandle.h:101
size_t Hash() const
Definition: RendererHandle.h:72
bool operator==(const REHandle_abstract &other) const
Definition: RendererHandle.h:76
REHandle_abstract(const EntityType aType, const std::uint16_t aId)
Definition: RendererHandle.h:95
REHandle_abstract()
Definition: RendererHandle.h:90
static const char * TypeToString(EntityType type)
Definition: RendererHandle.cpp:44
static std::array< std::uint16_t, static_cast< size_t >EntityType::Count)> uid_table
Definition: RendererHandle.h:99
static const std::uint16_t kBadId
Definition: RendererHandle.h:69
Definition: RendererHandle.h:109
REHandle(std::uint16_t id)
Definition: RendererHandle.h:135
static REHandle Next()
Definition: RendererHandle.h:112
REHandle(const REHandle &other)
Definition: RendererHandle.h:133
static REHandle Concretize(const REHandle_abstract &abstract)
Definition: RendererHandle.h:123
REHandle()
Definition: RendererHandle.h:132
REHandle & operator=(const REHandle &other)
Definition: RendererHandle.h:137
static const REHandle kBad
Definition: RendererHandle.h:110