From 230758660d142090388344903a26674154f6ab6e Mon Sep 17 00:00:00 2001
From: Fabian Paus <fabian.paus@kit.edu>
Date: Wed, 12 Jan 2022 13:14:48 +0100
Subject: [PATCH] ArViz: Add flag for hiding selected object during
 manipulation

---
 .../ArViz/Client/elements/ElementOps.h        | 10 ++++---
 .../ArViz/Coin/ElementVisualizer.cpp          |  4 +++
 .../components/ArViz/Coin/ElementVisualizer.h |  4 ++-
 .../ArViz/Coin/RegisterVisualizationTypes.cpp |  1 +
 .../components/ArViz/Coin/Visualizer.cpp      | 27 ++++++++++++++++---
 .../ArViz/Example/ArVizInteractExample.cpp    |  9 ++++---
 source/RobotAPI/interface/ArViz/Elements.ice  | 18 ++++++++-----
 7 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h b/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h
index 66ee0b5ad..28c974451 100644
--- a/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h
+++ b/source/RobotAPI/components/ArViz/Client/elements/ElementOps.h
@@ -84,13 +84,17 @@ namespace armarx::viz
             return selection();
         }
 
-
-
-        Self& fullTransform()
+        Self& transform()
         {
             return translation().rotation();
         }
 
+        Self& hideDuringTransform()
+        {
+            data_.enableFlags |= data::InteractionEnableFlags::TRANSFORM_HIDE;
+            return *this;
+        }
+
         data::InteractionDescription data_;
     };
 
diff --git a/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.cpp b/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.cpp
index f16ae2ac1..e4eb6d93f 100644
--- a/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.cpp
+++ b/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.cpp
@@ -19,10 +19,14 @@ namespace armarx::viz::coin
         transform = new SoTransform;
         material = new SoMaterial;
 
+        switch_ = new SoSwitch;
+        switch_->whichChild = SO_SWITCH_ALL;
+
         separator = new SoSeparator;
         separator->addChild(units);
         separator->addChild(transform);
         separator->addChild(material);
+        separator->addChild(switch_);
     }
 
     void ElementVisualization::updateBase(data::Element const& element)
diff --git a/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h b/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h
index 595db2893..c29794f2c 100644
--- a/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h
+++ b/source/RobotAPI/components/ArViz/Coin/ElementVisualizer.h
@@ -3,6 +3,7 @@
 #include <RobotAPI/interface/ArViz/Elements.h>
 
 #include <Inventor/nodes/SoSeparator.h>
+#include <Inventor/nodes/SoSwitch.h>
 
 #include <memory>
 
@@ -29,6 +30,7 @@ namespace armarx::viz::coin
         SoUnits* units;
         SoTransform* transform;
         SoMaterial* material;
+        SoSwitch* switch_;
         // TODO: Transform to flag system
         bool wasUpdated = true;
         bool visible = true;
@@ -77,7 +79,7 @@ namespace armarx::viz::coin
         DataType* createDerived() final
         {
             DataType* result = new DataType;
-            result->separator->addChild(result->node);
+            result->switch_->addChild(result->node);
             return result;
         }
 
diff --git a/source/RobotAPI/components/ArViz/Coin/RegisterVisualizationTypes.cpp b/source/RobotAPI/components/ArViz/Coin/RegisterVisualizationTypes.cpp
index 5ca14001a..743f0f5a5 100644
--- a/source/RobotAPI/components/ArViz/Coin/RegisterVisualizationTypes.cpp
+++ b/source/RobotAPI/components/ArViz/Coin/RegisterVisualizationTypes.cpp
@@ -22,6 +22,7 @@ void armarx::viz::CoinVisualizer::registerVisualizationTypes()
 {
     using namespace armarx::viz::coin;
 
+    elementVisualizersTypes.reserve(16);
     elementVisualizers.reserve(16);
 
     registerVisualizerFor<VisualizationBox>();
diff --git a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
index 76aff32f8..5bf05e33c 100644
--- a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
+++ b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
@@ -308,7 +308,7 @@ namespace coin
                 std::string typeName = armarx::GetTypeString(elementType);
                 ARMARX_WARNING << deactivateSpam(typeName, 1)
                                << "CoinElementVisualizer returned null for type: " << typeName << "\n"
-                               << "You need to register a visualizer for each type in ArViz/Coin/Visualizer.cpp";
+                               << "You need to register a visualizer for each type in ArViz/Coin/RegisterVisualizationTypes.cpp";
             }
         }
     }
@@ -651,13 +651,34 @@ namespace coin
                 manipulatorGroup->addChild(manipulator);
 
                 // We add the same visualization node again
-                manipulatorGroup->addChild(id->visu->separator);
-
+                SoSeparator* newSep = new SoSeparator();
+                int childNum = id->visu->separator->getNumChildren();
+                for (int i = 0; i < childNum; ++i)
+                {
+                    SoNode* child = id->visu->separator->getChild(i);
+                    if (SoSwitch* switch_ = dynamic_cast<SoSwitch*>(child))
+                    {
+                        child = switch_->copy();
+                    }
+                    newSep->addChild(child);
+                }
+                manipulatorGroup->addChild(newSep);
                 manipulator->unsquishKnobs();
+
+                if (enableFlags & data::InteractionEnableFlags::TRANSFORM_HIDE)
+                {
+                    id->visu->switch_->whichChild = SO_SWITCH_NONE;
+                }
+
             }
         }
         else
         {
+            if (enableFlags & data::InteractionEnableFlags::TRANSFORM_HIDE)
+            {
+                id->visu->switch_->whichChild = SO_SWITCH_ALL;
+            }
+
             selectedElement = nullptr;
             manipulatorGroup->removeAllChildren();
         }
diff --git a/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp b/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp
index 9d13f31d7..375c513d2 100644
--- a/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp
+++ b/source/RobotAPI/components/ArViz/Example/ArVizInteractExample.cpp
@@ -64,21 +64,24 @@ namespace armarx
                  .color(x.color)
                  .size(boxSize)
                  .enable(viz::interaction()
-                         .translation(viz::AXES_X));
+                         .translation(viz::AXES_X)
+                         .hideDuringTransform());
 
             y.initial = origin + Eigen::Vector3f(0.0f, 0.5f * ARROW_LENGTH, 0.0f);
             y.box.position(y.initial)
                  .color(y.color)
                  .size(boxSize)
                  .enable(viz::interaction()
-                         .translation(viz::AXES_Y));
+                         .translation(viz::AXES_Y)
+                         .hideDuringTransform());
 
             z.initial = origin + Eigen::Vector3f(0.0f, 0.0f, 0.5f * ARROW_LENGTH);
             z.box.position(z.initial)
                  .color(z.color)
                  .size(boxSize)
                  .enable(viz::interaction()
-                         .translation(viz::AXES_Z));
+                         .translation(viz::AXES_Z)
+                         .hideDuringTransform());
 
             sphere.position(origin + 0.5f * ARROW_LENGTH * Eigen::Vector3f(1.0f, 1.0f, 1.0f))
                   .color(viz::Color::orange())
diff --git a/source/RobotAPI/interface/ArViz/Elements.ice b/source/RobotAPI/interface/ArViz/Elements.ice
index 785b960d2..e22e5219f 100644
--- a/source/RobotAPI/interface/ArViz/Elements.ice
+++ b/source/RobotAPI/interface/ArViz/Elements.ice
@@ -42,16 +42,22 @@ module data
         const int TRANSLATION_X  = 4;
         const int TRANSLATION_Y  = 8;
         const int TRANSLATION_Z  = 16;
+        const int TRANSLATION_LOCAL = 32;
 
         // Enable rotation along the three axes
-        const int ROTATION_X     = 32;
-        const int ROTATION_Y     = 64;
-        const int ROTATION_Z     = 128;
+        const int ROTATION_X     = 64;
+        const int ROTATION_Y     = 128;
+        const int ROTATION_Z     = 256;
+        const int ROTATION_LOCAL = 512;
 
         // Enable scaling along the three axes
-        const int SCALING_X       = 256;
-        const int SCALING_Y       = 512;
-        const int SCALING_Z       = 1024;
+        const int SCALING_X       = 1024;
+        const int SCALING_Y       = 2048;
+        const int SCALING_Z       = 4096;
+        const int SCALING_LOCAL   = 8192;
+
+        // Hide the original object during the transformation
+        const int TRANSFORM_HIDE = 16384;
     };
 
     struct InteractionDescription
-- 
GitLab