Skip to content

Commit

Permalink
VOXEDIT: import node hierarchy on drag and drop import and directory …
Browse files Browse the repository at this point in the history
…import, too
  • Loading branch information
mgerhardy committed Jan 13, 2025
1 parent 61770af commit 879a160
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
16 changes: 10 additions & 6 deletions src/modules/scenegraph/SceneGraphUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,22 @@ int copyNodeToSceneGraph(SceneGraph &sceneGraph, const SceneGraphNode &node, int
return nodeId;
}

int moveNodeToSceneGraph(SceneGraph &sceneGraph, SceneGraphNode &node, int parent) {
int moveNodeToSceneGraph(SceneGraph &sceneGraph, SceneGraphNode &node, int parent, const std::function<void(int)> &onNodeAdded) {
SceneGraphNode newNode(node.type(), node.uuid());
copy(node, newNode);
if (newNode.type() == SceneGraphNodeType::Model) {
core_assert(node.owns());
newNode.setVolume(node.volume(), true);
node.releaseOwnership();
}
return addToGraph(sceneGraph, core::move(newNode), parent);
int newNodeId = addToGraph(sceneGraph, core::move(newNode), parent);
if (onNodeAdded && newNodeId != InvalidNodeId) {
onNodeAdded(newNodeId);
}
return newNodeId;
}

static int addSceneGraphNode_r(SceneGraph &target, const SceneGraph &source, SceneGraphNode &sourceNode, int parent) {
static int addSceneGraphNode_r(SceneGraph &target, const SceneGraph &source, SceneGraphNode &sourceNode, int parent, const std::function<void(int)> &onNodeAdded) {
const int newNodeId = moveNodeToSceneGraph(target, sourceNode, parent);
if (newNodeId == InvalidNodeId) {
Log::error("Failed to add node to the scene graph");
Expand All @@ -141,18 +145,18 @@ static int addSceneGraphNode_r(SceneGraph &target, const SceneGraph &source, Sce
for (int sourceNodeIdx : sourceNode.children()) {
core_assert(source.hasNode(sourceNodeIdx));
SceneGraphNode &sourceChildNode = source.node(sourceNodeIdx);
nodesAdded += addSceneGraphNode_r(target, source, sourceChildNode, newNodeId);
nodesAdded += addSceneGraphNode_r(target, source, sourceChildNode, newNodeId, onNodeAdded);
}

return nodesAdded;
}

int addSceneGraphNodes(SceneGraph &target, SceneGraph &source, int parent) {
int addSceneGraphNodes(SceneGraph &target, SceneGraph &source, int parent, const std::function<void(int)> &onNodeAdded) {
const SceneGraphNode &sourceRoot = source.root();
int nodesAdded = 0;
target.node(parent).addProperties(sourceRoot.properties());
for (int sourceNodeId : sourceRoot.children()) {
nodesAdded += addSceneGraphNode_r(target, source, source.node(sourceNodeId), parent);
nodesAdded += addSceneGraphNode_r(target, source, source.node(sourceNodeId), parent, onNodeAdded);
}
return nodesAdded;
}
Expand Down
5 changes: 3 additions & 2 deletions src/modules/scenegraph/SceneGraphUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "scenegraph/SceneGraph.h"
#include <functional>

namespace scenegraph {

Expand All @@ -19,9 +20,9 @@ int copyNodeToSceneGraph(SceneGraph &sceneGraph, const SceneGraphNode &node, int
/**
* @brief this doesn't copy but transfer the volume ownership
*/
int moveNodeToSceneGraph(SceneGraph &sceneGraph, SceneGraphNode &node, int parent);
int moveNodeToSceneGraph(SceneGraph &sceneGraph, SceneGraphNode &node, int parent, const std::function<void(int)> &onNodeAdded = {});

int addSceneGraphNodes(SceneGraph &target, SceneGraph &source, int parent);
int addSceneGraphNodes(SceneGraph &target, SceneGraph &source, int parent, const std::function<void(int)> &onNodeAdded = {});

core::DynamicArray<int> copySceneGraph(SceneGraph &target, const SceneGraph &source, int parent = 0);

Expand Down
19 changes: 7 additions & 12 deletions src/tools/voxedit/modules/voxedit-util/SceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,10 @@ bool SceneManager::import(const core::String& file) {

scenegraph::SceneGraphNode groupNode(scenegraph::SceneGraphNodeType::Group);
groupNode.setName(core::string::extractFilename(file));
int newNodeId = _sceneGraph.emplace(core::move(groupNode), activeNode());
bool state = false;
for (auto iter = newSceneGraph.beginAllModels(); iter != newSceneGraph.end(); ++iter) {
scenegraph::SceneGraphNode &node = *iter;
state |= moveNodeToSceneGraph(node, newNodeId) != InvalidNodeId;
}

return state;
int importGroupNodeId = _sceneGraph.emplace(core::move(groupNode), activeNode());
return scenegraph::addSceneGraphNodes(_sceneGraph, newSceneGraph, importGroupNodeId, [this] (int nodeId) {
onNewNodeAdded(nodeId, false);
}) > 0;
}

bool SceneManager::importDirectory(const core::String& directory, const io::FormatDescription *format, int depth) {
Expand Down Expand Up @@ -449,10 +445,9 @@ bool SceneManager::importDirectory(const core::String& directory, const io::Form
Log::error("Failed to load %s", e.fullPath.c_str());
} else {
mergeIfNeeded(newSceneGraph);
for (auto iter = newSceneGraph.beginModel(); iter != newSceneGraph.end(); ++iter) {
scenegraph::SceneGraphNode &node = *iter;
state |= moveNodeToSceneGraph(node, importGroupNodeId) != InvalidNodeId;
}
state |= scenegraph::addSceneGraphNodes(_sceneGraph, newSceneGraph, importGroupNodeId, [this] (int nodeId) {
onNewNodeAdded(nodeId, false);
}) > 0;
}
}
return state;
Expand Down

0 comments on commit 879a160

Please sign in to comment.