Open3D (C++ API)
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) 2019 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 <array>
30 #include <cstdint>
31 #include <functional>
32 
33 #define FMT_HEADER_ONLY 1
34 #define FMT_STRING_ALIAS 1
35 // Including windows.h causes all kinds of #defines like "OPAQUE", "near",
36 // "far", causes compile errors with Filament includes, and generally wrecks
37 // havoc.
38 #ifndef FMT_USE_WINDOWS_H
39 #define FMT_USE_WINDOWS_H 0
40 #endif
41 #include <fmt/format.h>
42 
43 namespace open3d {
44 
45 namespace visualization {
46 
47 // If you add entry here, don't forget to update TypeToString!
48 enum class EntityType : std::uint16_t {
49  None = 0,
50 
51  View,
52  Scene,
53 
54  Geometry,
55  Light,
57  Skybox,
58  Camera,
59  Material,
61  Texture,
62 
65 
66  Count
67 };
68 
69 // RenderEntityHandle - handle type for entities inside Renderer
70 // Can be used in STL containers as key
72  static const char* TypeToString(EntityType type);
73 
74  static const std::uint16_t kBadId = 0;
76 
77  inline size_t Hash() const {
78  return static_cast<std::uint16_t>(type) << 16 | id;
79  }
80 
81  bool operator==(const REHandle_abstract& other) const {
82  return id == other.id && type == other.type;
83  }
84 
85  bool operator<(const REHandle_abstract& other) const {
86  return Hash() < other.Hash();
87  }
88 
89  explicit operator bool() const { return id != kBadId; }
90 
91  REHandle_abstract() : type(EntityType::None), id(kBadId) {}
92 
93  std::uint16_t GetId() const { return id; }
94 
95 protected:
96  REHandle_abstract(const EntityType aType, const std::uint16_t aId)
97  : type(aType), id(aId) {}
98 
99  static std::array<std::uint16_t, static_cast<size_t>(EntityType::Count)>
100  uid_table;
101 
102  std::uint16_t id = kBadId;
103 };
104 
105 std::ostream& operator<<(std::ostream& os, const REHandle_abstract& uid);
106 
107 // REHandle is used for specification of handle types to prevent
108 // errors with passing, assigning or comparison of different kinds of handles
109 template <EntityType entityType>
110 struct REHandle : public REHandle_abstract {
111  static const REHandle kBad;
112 
113  static REHandle Next() {
114  const auto index = static_cast<std::uint16_t>(entityType);
115  auto id = ++uid_table[index];
116  if (id == REHandle_abstract::kBadId) {
117  uid_table[index] = REHandle_abstract::kBadId + 1;
118  id = REHandle_abstract::kBadId + 1;
119  }
120 
121  return std::move(REHandle(id));
122  }
123 
124  static REHandle Concretize(const REHandle_abstract& abstract) {
125  if (abstract.type != entityType) {
126  // assert("Incompatible render uid types!\n");
127  return REHandle();
128  }
129 
130  return REHandle(abstract.GetId());
131  }
132 
133  REHandle() : REHandle_abstract(entityType, REHandle_abstract::kBadId) {}
134  REHandle(const REHandle& other) : REHandle_abstract(entityType, other.id) {}
135 
136  REHandle& operator=(const REHandle& other) {
137  id = other.id;
138  return *this;
139  }
140 
141 private:
142  explicit REHandle(const std::uint16_t aId)
143  : REHandle_abstract(entityType, aId) {}
144 };
145 
146 template <EntityType entityType>
148 
161 
162 } // namespace visualization
163 } // namespace open3d
164 
165 namespace std {
166 template <>
167 class hash<open3d::visualization::REHandle_abstract> {
168 public:
169  size_t operator()(
170  const open3d::visualization::REHandle_abstract& uid) const {
171  return uid.Hash();
172  }
173 };
174 } // namespace std
175 
176 namespace fmt {
177 using namespace open3d::visualization;
178 template <>
179 struct formatter<open3d::visualization::REHandle_abstract> {
180  template <typename FormatContext>
182  FormatContext& ctx) {
183  return format_to(ctx.out(), "[{}, {}, hash: {}]",
185  uid.type),
186  uid.GetId(), uid.Hash());
187  }
188 
189  template <typename ParseContext>
190  constexpr auto parse(ParseContext& ctx) {
191  return ctx.begin();
192  }
193 };
194 } // namespace fmt
REHandle< EntityType::Texture > TextureHandle
Definition: RendererHandle.h:158
auto format(const open3d::visualization::REHandle_abstract &uid, FormatContext &ctx)
Definition: RendererHandle.h:181
Definition: ImguiFilamentBridge.h:61
EntityType
Definition: RendererHandle.h:48
const EntityType type
Definition: RendererHandle.h:75
Definition: RendererHandle.h:165
std::uint16_t id
Definition: RendererHandle.h:102
REHandle< EntityType::MaterialInstance > MaterialInstanceHandle
Definition: RendererHandle.h:157
constexpr auto parse(ParseContext &ctx)
Definition: RendererHandle.h:190
REHandle< EntityType::Camera > CameraHandle
Definition: RendererHandle.h:155
static const std::uint16_t kBadId
Definition: RendererHandle.h:74
REHandle< EntityType::VertexBuffer > VertexBufferHandle
Definition: RendererHandle.h:159
std::uint16_t GetId() const
Definition: RendererHandle.h:93
REHandle(const REHandle &other)
Definition: RendererHandle.h:134
size_t Hash() const
Definition: RendererHandle.h:77
REHandle()
Definition: RendererHandle.h:133
REHandle< EntityType::Skybox > SkyboxHandle
Definition: RendererHandle.h:154
REHandle_abstract()
Definition: RendererHandle.h:91
std::ostream & operator<<(std::ostream &os, const REHandle_abstract &uid)
Definition: RendererHandle.cpp:37
static const REHandle kBad
Definition: RendererHandle.h:111
char type
Definition: FilePCD.cpp:58
REHandle< EntityType::Material > MaterialHandle
Definition: RendererHandle.h:156
bool operator<(const REHandle_abstract &other) const
Definition: RendererHandle.h:85
Definition: Open3DViewer.h:29
Definition: RendererHandle.h:110
REHandle< EntityType::View > ViewHandle
Definition: RendererHandle.h:149
REHandle< EntityType::IndirectLight > IndirectLightHandle
Definition: RendererHandle.h:153
static const char * TypeToString(EntityType type)
Definition: RendererHandle.cpp:43
static REHandle Concretize(const REHandle_abstract &abstract)
Definition: RendererHandle.h:124
static REHandle Next()
Definition: RendererHandle.h:113
REHandle< EntityType::Scene > SceneHandle
Definition: RendererHandle.h:150
Definition: RendererHandle.h:176
REHandle< EntityType::IndexBuffer > IndexBufferHandle
Definition: RendererHandle.h:160
bool operator==(const REHandle_abstract &other) const
Definition: RendererHandle.h:81
REHandle_abstract(const EntityType aType, const std::uint16_t aId)
Definition: RendererHandle.h:96
REHandle & operator=(const REHandle &other)
Definition: RendererHandle.h:136
Definition: RendererHandle.h:71
REHandle< EntityType::Light > LightHandle
Definition: RendererHandle.h:152
REHandle< EntityType::Geometry > GeometryHandle
Definition: RendererHandle.h:151
size_t operator()(const open3d::visualization::REHandle_abstract &uid) const
Definition: RendererHandle.h:169