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