// ---------------------------------------------------------------------------- // - Open3D: www.open3d.org - // ---------------------------------------------------------------------------- // The MIT License (MIT) // // Copyright (c) 2019 www.open3d.org // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. // ---------------------------------------------------------------------------- #pragma once #include #include #include #define FMT_HEADER_ONLY 1 #define FMT_STRING_ALIAS 1 // Including windows.h causes all kinds of #defines like "OPAQUE", "near", // "far", causes compile errors with Filament includes, and generally wrecks // havoc. #ifndef FMT_USE_WINDOWS_H #define FMT_USE_WINDOWS_H 0 #endif #include namespace open3d { namespace visualization { // If you add entry here, don't forget to update TypeToString! enum class EntityType : std::uint16_t { None = 0, View, Scene, Geometry, Light, IndirectLight, Skybox, Camera, Material, MaterialInstance, Texture, VertexBuffer, IndexBuffer, Count }; // RenderEntityHandle - handle type for entities inside Renderer // Can be used in STL containers as key struct REHandle_abstract { static const char* TypeToString(EntityType type); static const std::uint16_t kBadId = 0; const EntityType type = EntityType::None; inline size_t Hash() const { return static_cast(type) << 16 | id; } bool operator==(const REHandle_abstract& other) const { return id == other.id && type == other.type; } bool operator<(const REHandle_abstract& other) const { return Hash() < other.Hash(); } explicit operator bool() const { return id != kBadId; } REHandle_abstract() : type(EntityType::None), id(kBadId) {} std::uint16_t GetId() const { return id; } protected: REHandle_abstract(const EntityType aType, const std::uint16_t aId) : type(aType), id(aId) {} static std::array(EntityType::Count)> uid_table; std::uint16_t id = kBadId; }; std::ostream& operator<<(std::ostream& os, const REHandle_abstract& uid); // REHandle is used for specification of handle types to prevent // errors with passing, assigning or comparison of different kinds of handles template struct REHandle : public REHandle_abstract { static const REHandle kBad; static REHandle Next() { const auto index = static_cast(entityType); auto id = ++uid_table[index]; if (id == REHandle_abstract::kBadId) { uid_table[index] = REHandle_abstract::kBadId + 1; id = REHandle_abstract::kBadId + 1; } return std::move(REHandle(id)); } static REHandle Concretize(const REHandle_abstract& abstract) { if (abstract.type != entityType) { // assert("Incompatible render uid types!\n"); return REHandle(); } return REHandle(abstract.GetId()); } REHandle() : REHandle_abstract(entityType, REHandle_abstract::kBadId) {} REHandle(const REHandle& other) : REHandle_abstract(entityType, other.id) {} REHandle& operator=(const REHandle& other) { id = other.id; return *this; } private: explicit REHandle(const std::uint16_t aId) : REHandle_abstract(entityType, aId) {} }; template const REHandle REHandle::kBad; typedef REHandle ViewHandle; typedef REHandle SceneHandle; typedef REHandle GeometryHandle; typedef REHandle LightHandle; typedef REHandle IndirectLightHandle; typedef REHandle SkyboxHandle; typedef REHandle CameraHandle; typedef REHandle MaterialHandle; typedef REHandle MaterialInstanceHandle; typedef REHandle TextureHandle; typedef REHandle VertexBufferHandle; typedef REHandle IndexBufferHandle; } // namespace visualization } // namespace open3d namespace std { template <> class hash { public: size_t operator()( const open3d::visualization::REHandle_abstract& uid) const { return uid.Hash(); } }; } // namespace std namespace fmt { using namespace open3d::visualization; template <> struct formatter { template auto format(const open3d::visualization::REHandle_abstract& uid, FormatContext& ctx) { return format_to(ctx.out(), "[{}, {}, hash: {}]", open3d::visualization::REHandle_abstract::TypeToString( uid.type), uid.GetId(), uid.Hash()); } template constexpr auto parse(ParseContext& ctx) { return ctx.begin(); } }; } // namespace fmt