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