From c7c9b7dfb6eace6239fa7fd6d9b8290a78172dd4 Mon Sep 17 00:00:00 2001 From: "Adam N. Morris" Date: Wed, 4 Oct 2023 17:21:04 -0500 Subject: [PATCH] Fixed cleaning up anchor when removing anchored prim --- .../cesium/omniverse/GlobeAnchorRegistry.h | 2 ++ src/core/src/Context.cpp | 34 ++++++++++++------- src/core/src/GlobeAnchorRegistry.cpp | 8 +++++ src/core/src/UsdNotificationHandler.cpp | 12 +++++++ 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/core/include/cesium/omniverse/GlobeAnchorRegistry.h b/src/core/include/cesium/omniverse/GlobeAnchorRegistry.h index 20a18e876..935306b6e 100644 --- a/src/core/include/cesium/omniverse/GlobeAnchorRegistry.h +++ b/src/core/include/cesium/omniverse/GlobeAnchorRegistry.h @@ -28,9 +28,11 @@ class GlobeAnchorRegistry { GlobeAnchorRegistry& operator=(GlobeAnchorRegistry) = delete; void clear(); + bool anchorExists(pxr::SdfPath path); std::shared_ptr createAnchor(pxr::SdfPath path, glm::dmat4 anchorToFixed); std::optional> getAnchor(const pxr::SdfPath& path); std::vector> getAllAnchors(); + bool removeAnchor(const pxr::SdfPath& path); protected: GlobeAnchorRegistry() = default; diff --git a/src/core/src/Context.cpp b/src/core/src/Context.cpp index 47ce6988d..f7c87f55e 100644 --- a/src/core/src/Context.cpp +++ b/src/core/src/Context.cpp @@ -366,18 +366,28 @@ void Context::processCesiumGlobeAnchorChanged(const cesium::omniverse::ChangedPr } void Context::processPrimRemoved(const ChangedPrim& changedPrim) { - // TODO: Remove prim from anchor registry if has anchor API. - - if (changedPrim.primType == ChangedPrimType::CESIUM_TILESET) { - // Remove the tileset from the asset registry - const auto tilesetPath = changedPrim.path; - AssetRegistry::getInstance().removeTileset(tilesetPath); - } else if (changedPrim.primType == ChangedPrimType::CESIUM_IMAGERY) { - // Remove the imagery from the asset registry and reload the tileset that the imagery was attached to - const auto imageryPath = changedPrim.path; - const auto tilesetPath = changedPrim.path.GetParentPath(); - AssetRegistry::getInstance().removeImagery(imageryPath); - reloadTileset(tilesetPath); + switch (changedPrim.primType) { + case ChangedPrimType::CESIUM_TILESET: { + // Remove the tileset from the asset registry + const auto tilesetPath = changedPrim.path; + AssetRegistry::getInstance().removeTileset(changedPrim.path); + } break; + case ChangedPrimType::CESIUM_IMAGERY: { + // Remove the imagery from the asset registry and reload the tileset that the imagery was attached to + const auto imageryPath = changedPrim.path; + const auto tilesetPath = changedPrim.path.GetParentPath(); + AssetRegistry::getInstance().removeImagery(imageryPath); + reloadTileset(tilesetPath); + } break; + case ChangedPrimType::CESIUM_GLOBE_ANCHOR: + if (!GlobeAnchorRegistry::getInstance().removeAnchor(changedPrim.path)) { + CESIUM_LOG_ERROR("Failed to remove anchor from registry: {}", changedPrim.path.GetString()); + } + break; + case ChangedPrimType::CESIUM_GEOREFERENCE: + case ChangedPrimType::CESIUM_DATA: + case ChangedPrimType::OTHER: + break; } } diff --git a/src/core/src/GlobeAnchorRegistry.cpp b/src/core/src/GlobeAnchorRegistry.cpp index 6944fb9f0..a5afffc18 100644 --- a/src/core/src/GlobeAnchorRegistry.cpp +++ b/src/core/src/GlobeAnchorRegistry.cpp @@ -5,6 +5,10 @@ namespace cesium::omniverse { +bool GlobeAnchorRegistry::anchorExists(pxr::SdfPath path) { + return _anchors.count(path.GetText()) > 0; +} + void GlobeAnchorRegistry::clear() { _anchors.clear(); } @@ -36,4 +40,8 @@ std::vector> GlobeAnchorRegistry::getAllAnchors return result; } +bool GlobeAnchorRegistry::removeAnchor(const pxr::SdfPath& path) { + return _anchors.erase(path.GetText()) > 0; +} + } // namespace cesium::omniverse diff --git a/src/core/src/UsdNotificationHandler.cpp b/src/core/src/UsdNotificationHandler.cpp index 742964ec2..07470643d 100644 --- a/src/core/src/UsdNotificationHandler.cpp +++ b/src/core/src/UsdNotificationHandler.cpp @@ -1,6 +1,7 @@ #include "cesium/omniverse/UsdNotificationHandler.h" #include "cesium/omniverse/AssetRegistry.h" +#include "cesium/omniverse/GlobeAnchorRegistry.h" #include "cesium/omniverse/LoggerSink.h" #include "cesium/omniverse/OmniImagery.h" #include "cesium/omniverse/OmniTileset.h" @@ -36,6 +37,11 @@ ChangedPrimType getType(const pxr::SdfPath& path) { default: break; } + + // If we still haven't found the prim type, it could be a globe anchor, and we should check if it exists in the anchor registry + if (GlobeAnchorRegistry::getInstance().anchorExists(path)) { + return ChangedPrimType::CESIUM_GLOBE_ANCHOR; + } } return ChangedPrimType::OTHER; @@ -161,6 +167,12 @@ void UsdNotificationHandler::onPrimRemoved(const pxr::SdfPath& primPath) { } } } + + const auto& type = getType(primPath); + if (type == ChangedPrimType::CESIUM_GLOBE_ANCHOR) { + _changedPrims.emplace_back(ChangedPrim{primPath, pxr::TfToken(), type, ChangeType::PRIM_REMOVED}); + CESIUM_LOG_INFO("Removed prim: {}", primPath.GetText()); + } } void UsdNotificationHandler::onPropertyChanged(const pxr::SdfPath& propertyPath) {