Open3D (C++ API)  0.11.0
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 namespace rendering {
47 
48 // If you add entry here, don't forget to update TypeToString!
49 enum class EntityType : std::uint16_t {
50  None = 0,
51 
52  View,
53  Scene,
54 
55  Geometry,
56  Light,
58  Skybox,
59  Camera,
60  Material,
62  Texture,
63 
66 
67  Count
68 };
69 
70 // RenderEntityHandle - handle type for entities inside Renderer
71 // Can be used in STL containers as key
73  static const char* TypeToString(EntityType type);
74 
75  static const std::uint16_t kBadId = 0;
77 
78  inline size_t Hash() const {
79  return static_cast<std::uint16_t>(type) << 16 | id;
80  }
81 
82  bool operator==(const REHandle_abstract& other) const {
83  return id == other.id && type == other.type;
84  }
85 
86  bool operator!=(const REHandle_abstract& other) const {
87  return !operator==(other);
88  }
89 
90  bool operator<(const REHandle_abstract& other) const {
91  return Hash() < other.Hash();
92  }
93 
94  explicit operator bool() const { return id != kBadId; }
95 
96  REHandle_abstract() : type(EntityType::None), id(kBadId) {}
97 
98  std::uint16_t GetId() const { return id; }
99 
100 protected:
101  REHandle_abstract(const EntityType aType, const std::uint16_t aId)
102  : type(aType), id(aId) {}
103 
104  static std::array<std::uint16_t, static_cast<size_t>(EntityType::Count)>
105  uid_table;
106 
107  std::uint16_t id = kBadId;
108 };
109 
110 std::ostream& operator<<(std::ostream& os, const REHandle_abstract& uid);
111 
112 // REHandle is used for specification of handle types to prevent
113 // errors with passing, assigning or comparison of different kinds of handles
114 template <EntityType entityType>
115 struct REHandle : public REHandle_abstract {
116  static const REHandle kBad;
117 
118  static REHandle Next() {
119  const auto index = static_cast<std::uint16_t>(entityType);
120  auto id = ++uid_table[index];
121  if (id == REHandle_abstract::kBadId) {
122  uid_table[index] = REHandle_abstract::kBadId + 1;
123  id = REHandle_abstract::kBadId + 1;
124  }
125 
126  return std::move(REHandle(id));
127  }
128 
129  static REHandle Concretize(const REHandle_abstract& abstract) {
130  if (abstract.type != entityType) {
131  // assert("Incompatible render uid types!\n");
132  return REHandle();
133  }
134 
135  return REHandle(abstract.GetId());
136  }
137 
138  REHandle() : REHandle_abstract(entityType, REHandle_abstract::kBadId) {}
139  REHandle(const REHandle& other) : REHandle_abstract(entityType, other.id) {}
140  // Don't use this constructor unless you know what you are doing
141  explicit REHandle(std::uint16_t id) : REHandle_abstract(entityType, id) {}
142 
143  REHandle& operator=(const REHandle& other) {
144  id = other.id;
145  return *this;
146  }
147 };
148 
149 template <EntityType entityType>
151 
164 
165 } // namespace rendering
166 } // namespace visualization
167 } // namespace open3d
168 
170 namespace std {
171 template <>
173 public:
175  uid) const {
176  return uid.Hash();
177  }
178 };
179 } // namespace std
180 
181 namespace fmt {
182 using namespace open3d::visualization;
183 template <>
184 struct formatter<open3d::visualization::rendering::REHandle_abstract> {
185  template <typename FormatContext>
187  FormatContext& ctx) {
188  return format_to(ctx.out(), "[{}, {}, hash: {}]",
190  TypeToString(uid.type),
191  uid.GetId(), uid.Hash());
192  }
193 
194  template <typename ParseContext>
195  constexpr auto parse(ParseContext& ctx) {
196  return ctx.begin();
197  }
198 };
199 } // namespace fmt
REHandle< EntityType::Geometry > GeometryHandle
Definition: RendererHandle.h:154
std::ostream & operator<<(std::ostream &os, const REHandle_abstract &uid)
Definition: RendererHandle.cpp:38
REHandle< EntityType::Camera > CameraHandle
Definition: RendererHandle.h:158
REHandle_abstract(const EntityType aType, const std::uint16_t aId)
Definition: RendererHandle.h:101
Definition: ModelIO.h:32
REHandle_abstract()
Definition: RendererHandle.h:96
static REHandle Concretize(const REHandle_abstract &abstract)
Definition: RendererHandle.h:129
REHandle()
Definition: RendererHandle.h:138
REHandle & operator=(const REHandle &other)
Definition: RendererHandle.h:143
REHandle(std::uint16_t id)
Definition: RendererHandle.h:141
Definition: RendererHandle.h:115
Definition: Optional.h:922
bool operator==(const PointXYZ A, const PointXYZ B)
Definition: Cloud.h:151
REHandle< EntityType::Material > MaterialHandle
Definition: RendererHandle.h:159
std::uint16_t GetId() const
Definition: RendererHandle.h:98
REHandle< EntityType::MaterialInstance > MaterialInstanceHandle
Definition: RendererHandle.h:160
const EntityType type
Definition: RendererHandle.h:76
bool operator<(const REHandle_abstract &other) const
Definition: RendererHandle.h:90
REHandle< EntityType::Scene > SceneHandle
Definition: RendererHandle.h:153
EntityType
Definition: RendererHandle.h:49
REHandle< EntityType::Texture > TextureHandle
Definition: RendererHandle.h:161
static REHandle Next()
Definition: RendererHandle.h:118
static const char * TypeToString(EntityType type)
Definition: RendererHandle.cpp:44
char type
Definition: FilePCD.cpp:60
REHandle< EntityType::IndirectLight > IndirectLightHandle
Definition: RendererHandle.h:156
bool operator==(const REHandle_abstract &other) const
Definition: RendererHandle.h:82
REHandle< EntityType::Light > LightHandle
Definition: RendererHandle.h:155
Definition: PinholeCameraIntrinsic.cpp:35
REHandle(const REHandle &other)
Definition: RendererHandle.h:139
filament::Texture::InternalFormat format
Definition: FilamentResourceManager.cpp:191
REHandle< EntityType::View > ViewHandle
Definition: RendererHandle.h:152
static const std::uint16_t kBadId
Definition: RendererHandle.h:75
REHandle< EntityType::Skybox > SkyboxHandle
Definition: RendererHandle.h:157
std::uint16_t id
Definition: RendererHandle.h:107
REHandle< EntityType::VertexBuffer > VertexBufferHandle
Definition: RendererHandle.h:162
REHandle< EntityType::IndexBuffer > IndexBufferHandle
Definition: RendererHandle.h:163
size_t Hash() const
Definition: RendererHandle.h:78
bool operator!=(const REHandle_abstract &other) const
Definition: RendererHandle.h:86
static const REHandle kBad
Definition: RendererHandle.h:116