Skip to content

Commit

Permalink
Fixed cleaning up anchor when removing anchored prim
Browse files Browse the repository at this point in the history
  • Loading branch information
weegeekps committed Oct 4, 2023
1 parent f280996 commit c7c9b7d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/core/include/cesium/omniverse/GlobeAnchorRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ class GlobeAnchorRegistry {
GlobeAnchorRegistry& operator=(GlobeAnchorRegistry) = delete;

void clear();
bool anchorExists(pxr::SdfPath path);
std::shared_ptr<OmniGlobeAnchor> createAnchor(pxr::SdfPath path, glm::dmat4 anchorToFixed);
std::optional<std::shared_ptr<OmniGlobeAnchor>> getAnchor(const pxr::SdfPath& path);
std::vector<std::shared_ptr<OmniGlobeAnchor>> getAllAnchors();
bool removeAnchor(const pxr::SdfPath& path);

protected:
GlobeAnchorRegistry() = default;
Expand Down
34 changes: 22 additions & 12 deletions src/core/src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/core/src/GlobeAnchorRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

namespace cesium::omniverse {

bool GlobeAnchorRegistry::anchorExists(pxr::SdfPath path) {
return _anchors.count(path.GetText()) > 0;
}

void GlobeAnchorRegistry::clear() {
_anchors.clear();
}
Expand Down Expand Up @@ -36,4 +40,8 @@ std::vector<std::shared_ptr<OmniGlobeAnchor>> GlobeAnchorRegistry::getAllAnchors
return result;
}

bool GlobeAnchorRegistry::removeAnchor(const pxr::SdfPath& path) {
return _anchors.erase(path.GetText()) > 0;
}

} // namespace cesium::omniverse
12 changes: 12 additions & 0 deletions src/core/src/UsdNotificationHandler.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c7c9b7d

Please sign in to comment.