From 9f20937b16a1a62b5139e88fbc9013b1ce926462 Mon Sep 17 00:00:00 2001
From: Markus Grotz <markus.grotz@kit.edu>
Date: Mon, 28 Sep 2020 16:21:06 +0200
Subject: [PATCH] add visibility control to ArViz elements

- add Element::hide(), Element::show(), and Element::visibile() to
toggle an elements visibility

- update ArViz example
---
 .../ArViz/Client/elements/ElementOps.h        | 27 +++++++++++++++++++
 .../components/ArViz/Coin/ElementVisualizer.h | 26 ++++++++++++++++++
 .../components/ArViz/Example/ArVizExample.cpp |  6 +++++
 source/RobotAPI/interface/ArViz/Elements.ice  |  1 +
 4 files changed, 60 insertions(+)

diff --git a/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h b/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h
index e7b8f0542..4f7c222f5 100644
--- a/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h
+++ b/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h
@@ -140,6 +140,33 @@ namespace armarx::viz
             return *static_cast<DerivedT*>(this);
         }
 
+        DerivedT& hide()
+        {
+            data_->flags |= data::ElementFlags::HIDDEN;
+
+            return *static_cast<DerivedT*>(this);
+        }
+
+        DerivedT& show()
+        {
+            data_->flags &= ~data::ElementFlags::HIDDEN;
+
+            return *static_cast<DerivedT*>(this);
+        }
+
+        DerivedT& visible(bool visible)
+        {
+            if (visible)
+            {
+                return show();
+            }
+            else
+            {
+                return hide();
+            }
+        }
+
+
         IceInternal::Handle<ElementT> data_;
     };
 
diff --git a/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h b/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h
index 8c3f524fb..9be0fa5b5 100644
--- a/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h
+++ b/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h
@@ -7,6 +7,8 @@
 
 #include <memory>
 
+#include <RobotAPI/interface/ArViz/Elements.h>
+
 namespace armarx::viz::data
 {
     class Element;
@@ -27,6 +29,7 @@ namespace armarx::viz::coin
         SoTransform* transform;
         SoMaterial* material;
         bool wasUpdated = true;
+        bool visible = true;
     };
 
     class ElementVisualizer
@@ -50,6 +53,12 @@ namespace armarx::viz::coin
         TypedElementVisualization()
         {
             node = new NodeType;
+            node->ref();
+        }
+
+        ~TypedElementVisualization()
+        {
+            node->unref();
         }
 
         NodeType* node;
@@ -62,6 +71,7 @@ namespace armarx::viz::coin
         using DataType = DataT;
         using ElementType = typename DataType::ElementType;
 
+
         DataType* createDerived() final
         {
             DataType* result = new DataType;
@@ -73,8 +83,24 @@ namespace armarx::viz::coin
         {
             auto const& element = static_cast<ElementType const&>(element_);
             auto* data = dynamic_cast<DataType*>(data_);
+
             if (data)
             {
+                bool hidden = (element.flags & armarx::viz::data::ElementFlags::HIDDEN);
+                if (hidden)
+                {
+                    if (data->visible)
+                    {
+                        data->separator->removeChild(data->node);
+                        data->visible = false;
+                    }
+                }
+                else if (!data->visible)
+                {
+                    data->visible = true;
+                    data->separator->addChild(data->node);
+                }
+
                 // We want to call  update with the correctly downcasted arguments
                 return data->update(element);
             }
diff --git a/source/RobotAPI/components/ArViz/Example/ArVizExample.cpp b/source/RobotAPI/components/ArViz/Example/ArVizExample.cpp
index 9043aa8ae..6cb096403 100644
--- a/source/RobotAPI/components/ArViz/Example/ArVizExample.cpp
+++ b/source/RobotAPI/components/ArViz/Example/ArVizExample.cpp
@@ -69,6 +69,9 @@ namespace armarx
                            .color(viz::Color::red())
                            .size(Eigen::Vector3f(100.0f, 100.0f, 100.0f));
 
+            bool toggleVisibility = (static_cast<int>(timeInSeconds) % 2 == 0);
+            box.visible(toggleVisibility);
+
             layer.add(box);
         }
         {
@@ -125,6 +128,7 @@ namespace armarx
             Eigen::Vector3f direction = dirRot * Eigen::Vector3f::UnitX();
             arrow.direction(direction);
 
+
             Eigen::Vector3f pos = Eigen::Vector3f::Zero();
             pos.z() = +300.0f;
             pos.x() = -500.0f;
@@ -133,6 +137,8 @@ namespace armarx
 
             layer.add(arrow);
         }
+
+
     }
 
 
diff --git a/source/RobotAPI/interface/ArViz/Elements.ice b/source/RobotAPI/interface/ArViz/Elements.ice
index 03440c043..ac8a70dfe 100644
--- a/source/RobotAPI/interface/ArViz/Elements.ice
+++ b/source/RobotAPI/interface/ArViz/Elements.ice
@@ -34,6 +34,7 @@ module data
     {
         const int NONE = 0;
         const int OVERRIDE_MATERIAL = 1;
+        const int INVISIBLE = 2;
     };
 
     class Element
-- 
GitLab