diff --git a/examples/demos/gfx_mapslab.py b/examples/demos/gfx_mapslab.py index de59b2e5d1df8ee8386ae75921543139bddefde6..8cc8ca334d2abf5f4dd7911bcb0b77f422da70b1 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 76a513dc732613a3951a94c729690be5bab6f29e..ea4b58e89e23aba759f4c3fc51e1a130105ad4ee 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 9bec738a57c931ba729f9e9dc708911a1d294950..aff9564750228dbfd9e79552dbd2db846f1b4a2a 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 c3acaddc618d1ce7a394a04fce3349cb0c379195..1f0abadc4f0664044f1e94b597b4f0b0e40ab40c 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 7df5ee56cd5704234f5d99595044e2d28fa2ccd2..34e2e033aeb50b462cca4d7087787d5b416e9b87 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 361b9a2f28a3665fe606c7f798b04df2734347db..8959d3c8957c610ff49b56a4431f43ecba91669c 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()); }