From 7af4b777c7c834a31b06722cac6ae5a872686bdc Mon Sep 17 00:00:00 2001 From: Marco Biasini <marco.biasini@unibas.ch> Date: Thu, 17 Feb 2011 09:31:26 +0100 Subject: [PATCH] improve check for uniqueness of scene node names --- examples/demos/gfx_mapslab.py | 3 ++- modules/gfx/src/gfx_node.cc | 21 +++++++++++++++++++++ modules/gfx/src/gfx_node.hh | 8 +++++++- modules/gfx/src/scene.cc | 12 ++++-------- modules/gfx/src/scene.hh | 5 ++++- modules/gfx/tests/test_gfx_node.cc | 26 +++++++++++++++++--------- 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/examples/demos/gfx_mapslab.py b/examples/demos/gfx_mapslab.py index de59b2e5d..8cc8ca334 100644 --- a/examples/demos/gfx_mapslab.py +++ b/examples/demos/gfx_mapslab.py @@ -52,6 +52,7 @@ scene.SetCenter(go1.GetCenter()) go2 = gfx.MapSlab("slab",mh,geom.Plane(go1.GetCenter(),geom.Vec3(0.0,0.0,1.0))) scene.Add(go2) -go2.ColorBy(gfx.YELLOW,gfx.GREEN,0.2,0.8) + +go2.ColorBy('HEAT_MAP',0.2,0.8) print 'Demo 4: Projecting the density of a map onto a plane...' diff --git a/modules/gfx/src/gfx_node.cc b/modules/gfx/src/gfx_node.cc index 76a513dc7..ea4b58e89 100644 --- a/modules/gfx/src/gfx_node.cc +++ b/modules/gfx/src/gfx_node.cc @@ -59,6 +59,17 @@ void GfxNode::DeepSwap(GfxNode& n) std::swap(show_,n.show_); } +bool GfxNode::IsNameAvailable(const String& name) const +{ + for (GfxNodeVector::const_iterator it =node_vector_.begin(); + it!=node_vector_.end();++it) { + if ((*it)->GetName()==name) { + return false; + } + } + return true; +} + void GfxNode::Apply(GfxNodeVisitor& v,GfxNodeVisitor::Stack st) { if(!v.VisitNode(this,st)) return; @@ -155,6 +166,16 @@ void GfxNode::RemoveAll() void GfxNode::Add(GfxNodeP node) { + if (!node) { + return; + } + if (!this->IsNameAvailable(node->GetName())) { + std::stringstream ss; + ss << "node '" << this->GetName() << "' has already a node with name '" + << node->GetName() << "'"; + throw Error(ss.str()); + } + node_vector_.push_back(node); if (!node->parent_.expired()) { node->GetParent()->Remove(node); diff --git a/modules/gfx/src/gfx_node.hh b/modules/gfx/src/gfx_node.hh index 9bec738a5..aff956475 100644 --- a/modules/gfx/src/gfx_node.hh +++ b/modules/gfx/src/gfx_node.hh @@ -75,7 +75,13 @@ class DLLEXPORT_OST_GFX GfxNode: public boost::enable_shared_from_this<GfxNode> // add a graphical object - leaf void Add(GfxObjP obj); - + + + /// \brief returns true if no scene node of the given name is a child + /// of this node. + bool IsNameAvailable(const String& name) const; + + // remove given graphical object void Remove(GfxObjP obj); diff --git a/modules/gfx/src/scene.cc b/modules/gfx/src/scene.cc index c3acaddc6..1f0abadc4 100644 --- a/modules/gfx/src/scene.cc +++ b/modules/gfx/src/scene.cc @@ -756,6 +756,8 @@ size_t Scene::GetNodeCount() const void Scene::Add(const GfxNodeP& n, bool redraw) { if(!n) return; + // even though IsNameAvailable() is called in GfxNode::Add, check here + // as well to produce error message specific to adding a node to the scene. if(!this->IsNameAvailable(n->GetName())){ throw Error("Scene already has a node with name '"+n->GetName()+"'"); } @@ -776,15 +778,9 @@ void Scene::Add(const GfxNodeP& n, bool redraw) } } -bool Scene::IsNameAvailable(String name) +bool Scene::IsNameAvailable(const String& name) const { - FindNode fn(name); - Apply(fn); - if(fn.node) { - LOG_INFO("Scene: " << name << " already exists as a scene node"); - return false; - } - return true; + return root_node_->IsNameAvailable(name); } void Scene::NodeAdded(const GfxNodeP& node) diff --git a/modules/gfx/src/scene.hh b/modules/gfx/src/scene.hh index 7df5ee56c..34e2e033a 100644 --- a/modules/gfx/src/scene.hh +++ b/modules/gfx/src/scene.hh @@ -498,8 +498,11 @@ private: void render_scene(); void render_glow(); void render_stereo(); + void do_autoslab(); - bool IsNameAvailable(String name); + + bool IsNameAvailable(const String& name) const; + }; }} // ns diff --git a/modules/gfx/tests/test_gfx_node.cc b/modules/gfx/tests/test_gfx_node.cc index 361b9a2f2..8959d3c89 100644 --- a/modules/gfx/tests/test_gfx_node.cc +++ b/modules/gfx/tests/test_gfx_node.cc @@ -75,32 +75,40 @@ BOOST_AUTO_TEST_CASE(gfx_node_add) { GfxNodeP n1(new GfxNode("1")); BOOST_CHECK_EQUAL(n1->GetParent(), GfxNodeP()); - BOOST_CHECK_EQUAL(n1->GetChildCount(), 0); + BOOST_CHECK_EQUAL(n1->GetChildCount(), size_t(0)); GfxNodeP n2(new GfxNode("2")); n1->Add(n2); BOOST_CHECK_EQUAL(n1->GetParent(), GfxNodeP()); - BOOST_CHECK_EQUAL(n1->GetChildCount(), 1); + BOOST_CHECK_EQUAL(n1->GetChildCount(), size_t(1)); BOOST_CHECK_EQUAL(n2->GetParent(), n1); - BOOST_CHECK_EQUAL(n2->GetChildCount(), 0); + BOOST_CHECK_EQUAL(n2->GetChildCount(), size_t(0)); // "move" node 2 from 1 to 3 GfxNodeP n3(new GfxNode("3")); n3->Add(n2); - BOOST_CHECK_EQUAL(n1->GetChildCount(), 0); + BOOST_CHECK_EQUAL(n1->GetChildCount(), size_t(0)); BOOST_CHECK_EQUAL(n2->GetParent(), n3); - BOOST_CHECK_EQUAL(n3->GetChildCount(), 1); + BOOST_CHECK_EQUAL(n3->GetChildCount(), size_t(1)); } +BOOST_AUTO_TEST_CASE(gfx_node_add_duplicate) +{ + GfxNodeP n1(new GfxNode("1")); + GfxNodeP n2(new GfxNode("2")); + GfxNodeP n3(new GfxNode("2")); + BOOST_CHECK_NO_THROW(n1->Add(n2)); + BOOST_CHECK_THROW(n1->Add(n3), Error); +} BOOST_AUTO_TEST_CASE(gfx_node_remove) { GfxNodeP n1(new GfxNode("1")); BOOST_CHECK_EQUAL(n1->GetParent(), GfxNodeP()); - BOOST_CHECK_EQUAL(n1->GetChildCount(), 0); + BOOST_CHECK_EQUAL(n1->GetChildCount(), size_t(0)); GfxNodeP n2(new GfxNode("2")); n1->Add(n2); n1->Remove(n2); - BOOST_CHECK_EQUAL(n1->GetChildCount(), 0); + BOOST_CHECK_EQUAL(n1->GetChildCount(), size_t(0)); BOOST_CHECK_EQUAL(n2->GetParent(), GfxNodeP()); } @@ -108,7 +116,7 @@ BOOST_AUTO_TEST_CASE(gfx_node_remove_all) { GfxNodeP n1(new GfxNode("1")); BOOST_CHECK_EQUAL(n1->GetParent(), GfxNodeP()); - BOOST_CHECK_EQUAL(n1->GetChildCount(), 0); + BOOST_CHECK_EQUAL(n1->GetChildCount(), size_t(0)); GfxNodeP n2(new GfxNode("2")); GfxNodeP n3(new GfxNode("3")); GfxNodeP n4(new GfxNode("4")); @@ -116,7 +124,7 @@ BOOST_AUTO_TEST_CASE(gfx_node_remove_all) n1->Add(n3); n1->Add(n4); n1->RemoveAll(); - BOOST_CHECK_EQUAL(n1->GetChildCount(), 0); + BOOST_CHECK_EQUAL(n1->GetChildCount(), size_t(0)); BOOST_CHECK_EQUAL(n2->GetParent(), GfxNodeP()); } -- GitLab