From e1bf98d7efc3e5279b4895e41c4eb6710a298b06 Mon Sep 17 00:00:00 2001
From: Fabian Paus <fabian.paus@kit.edu>
Date: Wed, 16 Oct 2019 15:57:26 +0200
Subject: [PATCH] Fix draw style flag handling

---
 .../ArViz/Coin/VisualizationRobot.cpp         | 24 ++++++++++++--
 .../ArViz/Coin/VisualizationRobot.h           |  2 +-
 .../components/ArViz/Coin/Visualizer.cpp      | 33 ++++++++++++++++---
 .../components/ArVizExample/ArVizExample.cpp  | 11 +++----
 source/RobotAPI/interface/ArViz/Elements.ice  | 10 +++---
 5 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.cpp b/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.cpp
index 075f05b3c..58b14a230 100644
--- a/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.cpp
+++ b/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.cpp
@@ -49,6 +49,12 @@ namespace armarx::viz::coin
                 ARMARX_INFO << "Loading robot from " << fullFilename;
                 result = VirtualRobot::RobotIO::loadRobot(fullFilename, loadMode);
                 result->setThreadsafe(false);
+                // HACK: Disable visualization updates to time
+                for (auto& node : result->getRobotNodes())
+                {
+                    node->setUpdateVisualization(false);
+                    node->setUpdateCollisionModel(false);
+                }
             }
             catch (std::exception const& ex)
             {
@@ -56,6 +62,7 @@ namespace armarx::viz::coin
                                << "\nReason: " << ex.what();
             }
 
+
             return result;
         }
 
@@ -99,6 +106,8 @@ namespace armarx::viz::coin
 
     bool VisualizationRobot::update(ElementType const& element)
     {
+        IceUtil::Time time_start = IceUtil::Time::now();
+
         bool robotChanged = loaded.project != element.project || loaded.filename != element.filename;
         if (robotChanged)
         {
@@ -113,12 +122,15 @@ namespace armarx::viz::coin
                            << "\nFilename: " << element.filename;
             return true;
         }
+        IceUtil::Time time_load = IceUtil::Time::now();
+
         bool drawStyleChanged = loadedDrawStyle != element.drawStyle;
         if (robotChanged || drawStyleChanged)
         {
             recreateVisualizationNodes(element.drawStyle);
             loadedDrawStyle = element.drawStyle;
         }
+        IceUtil::Time time_style = IceUtil::Time::now();
 
         // Set pose, configuration and so on
 
@@ -132,8 +144,9 @@ namespace armarx::viz::coin
         // robot.setGlobalPose(pose, false);
 
         robot.setJointValues(element.jointValues);
+        IceUtil::Time time_set = IceUtil::Time::now();
 
-        if (loadedDrawStyle & ModelDrawStyle_OVERRIDE_COLOR)
+        if (loadedDrawStyle & ModelDrawStyle::OVERRIDE_COLOR)
         {
             int numChildren = node->getNumChildren();
             for (int i = 0; i < numChildren; i++)
@@ -157,6 +170,13 @@ namespace armarx::viz::coin
                 m->setOverride(true);
             }
         }
+        IceUtil::Time time_color = IceUtil::Time::now();
+
+        ARMARX_INFO << "Total:   " << (time_color - time_start).toMilliSecondsDouble()
+                    << "\nLoad:  " << (time_load - time_start).toMilliSecondsDouble()
+                    << "\nStyle: " << (time_style - time_load).toMilliSecondsDouble()
+                    << "\nSet:   " << (time_set - time_style).toMilliSecondsDouble()
+                    << "\nColor: " << (time_color - time_set).toMilliSecondsDouble();
 
         return true;
     }
@@ -164,7 +184,7 @@ namespace armarx::viz::coin
     void VisualizationRobot::recreateVisualizationNodes(int drawStyle)
     {
         VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full;
-        if (drawStyle & ModelDrawStyle_COLLISION)
+        if (drawStyle & ModelDrawStyle::COLLISION)
         {
             visuType = VirtualRobot::SceneObject::Collision;
         }
diff --git a/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.h b/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.h
index 15c81347a..0f4a49211 100644
--- a/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.h
+++ b/source/RobotAPI/components/ArViz/Coin/VisualizationRobot.h
@@ -24,7 +24,7 @@ namespace armarx::viz::coin
         void recreateVisualizationNodes(int drawStyle);
 
         LoadedRobot loaded;
-        int loadedDrawStyle = ModelDrawStyle_ORIGINAL;
+        int loadedDrawStyle = ModelDrawStyle::ORIGINAL;
     };
 
     void clearRobotCache();
diff --git a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
index 92135134d..6ae7956ce 100644
--- a/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
+++ b/source/RobotAPI/components/ArViz/Coin/Visualizer.cpp
@@ -125,6 +125,7 @@ namespace armarx::viz
 
         std::set<std::string> updatedIDs;
 
+        IceUtil::Time time_start = IceUtil::Time::now();
         // Add or update the elements in the update
         CoinLayer& layer = layerIt->second;
         for (auto& updatedElementPtr : update.elements)
@@ -145,9 +146,10 @@ namespace armarx::viz
             }
             if (visualizer == nullptr)
             {
-                ARMARX_WARNING << "No visualizer for element type found: "
+                ARMARX_WARNING << deactivateSpam(1)
+                               << "No visualizer for element type found: "
                                << boost::core::demangle(elementType.name());
-                return;
+                continue;
             }
 
             auto oldElementIter = layer.elements.find(updatedElement.id);
@@ -157,6 +159,12 @@ namespace armarx::viz
                 coin::ElementVisualization& oldElement = *oldElementIter->second;
 
                 bool updated = visualizer->update(updatedElement, &oldElement);
+                if (update.name == "Example")
+                {
+                    IceUtil::Time dur_elem = IceUtil::Time::now() - time_start;
+                    ARMARX_INFO << "Elem " << updatedElement.id
+                                << ": " << dur_elem.toMilliSecondsDouble();
+                }
                 if (updated)
                 {
                     continue;
@@ -182,6 +190,11 @@ namespace armarx::viz
                                << "You need to register a visualizer for each type in ArViz/Coin/Visualizer.cpp";
             }
         }
+        IceUtil::Time duration = IceUtil::Time::now() - time_start;
+        if (duration > IceUtil::Time::milliSeconds(2))
+        {
+            ARMARX_INFO << "Layer " << update.name << ": " << duration.toMilliSecondsDouble();
+        }
 
         // Remove the elements which were not contained in the update
         for (auto iter = layer.elements.begin(); iter != layer.elements.end();)
@@ -240,25 +253,34 @@ namespace armarx::viz
                 // We never block the GUI thread with Ice calls: Only get result when call is completed
                 if (pullUpdateResult->isCompleted())
                 {
-                    TimedBlock block("apply-updates");
+                    IceUtil::Time time_start = IceUtil::Time::now();
 
                     auto layerIDsBefore = getLayerIDs();
 
                     LayerUpdates pulledUpdates = storage->end_pullUpdatesSince(pullUpdateResult);
+                    IceUtil::Time time_afterPull = IceUtil::Time::now();
+
                     pullUpdateResult = nullptr;
                     for (LayerUpdate const& update : pulledUpdates.updates)
                     {
                         apply(update);
                     }
                     updateCounter = pulledUpdates.updateCounter;
+                    IceUtil::Time time_afterApply = IceUtil::Time::now();
 
                     auto layerIDsAfter = getLayerIDs();
                     if (layerIDsAfter != layerIDsBefore)
                     {
                         emitLayersChanged(layerIDsAfter);
                     }
-
-                    ARMARX_INFO << "Updates: " << pulledUpdates.updates.size();
+                    IceUtil::Time time_afterLayersChanged = IceUtil::Time::now();
+
+                    // Most of the time is spent in apply
+                    //                    ARMARX_INFO << "Updates:  " << pulledUpdates.updates.size()
+                    //                                << "\nTotal:  " << (time_afterLayersChanged - time_start).toMilliSecondsDouble()
+                    //                                << "\nPull:   " << (time_afterPull - time_start).toMilliSecondsDouble()
+                    //                                << "\nApply:  " << (time_afterApply - time_afterPull).toMilliSecondsDouble()
+                    //                                << "\nLayers: " << (time_afterLayersChanged - time_afterApply).toMilliSecondsDouble();
                 }
                 else
                 {
@@ -311,6 +333,7 @@ namespace armarx::viz
     std::vector<CoinLayerID> CoinVisualizer::getLayerIDs()
     {
         std::vector<CoinLayerID> result;
+        result.reserve(layers.size());
         for (auto& entry : layers)
         {
             result.push_back(entry.first);
diff --git a/source/RobotAPI/components/ArVizExample/ArVizExample.cpp b/source/RobotAPI/components/ArVizExample/ArVizExample.cpp
index 94e5ccc58..9e4d49e39 100644
--- a/source/RobotAPI/components/ArVizExample/ArVizExample.cpp
+++ b/source/RobotAPI/components/ArVizExample/ArVizExample.cpp
@@ -382,31 +382,30 @@ namespace armarx::viz
             return *this;
         }
 
-        // Encapsulate this
         Robot& useCollisionModel()
         {
-            data_->drawStyle |= ModelDrawStyle_COLLISION;
+            data_->drawStyle |= ModelDrawStyle::COLLISION;
 
             return *this;
         }
 
         Robot& useFullModel()
         {
-            data_->drawStyle &= ~ModelDrawStyle_COLLISION;
+            data_->drawStyle &= ~ModelDrawStyle::COLLISION;
 
             return *this;
         }
 
         Robot& overrideColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 255)
         {
-            data_->drawStyle |= ModelDrawStyle_OVERRIDE_COLOR;
+            data_->drawStyle |= ModelDrawStyle::OVERRIDE_COLOR;
 
             return color(r, g, b, a);
         }
 
         Robot& useOriginalColor()
         {
-            data_->drawStyle &= ~ModelDrawStyle_OVERRIDE_COLOR;
+            data_->drawStyle &= ~ModelDrawStyle::OVERRIDE_COLOR;
 
             return *this;
         }
@@ -770,5 +769,3 @@ armarx::PropertyDefinitionsPtr ArVizExample::createPropertyDefinitions()
     return armarx::PropertyDefinitionsPtr(new ArVizExamplePropertyDefinitions(
             getConfigIdentifier()));
 }
-
-
diff --git a/source/RobotAPI/interface/ArViz/Elements.ice b/source/RobotAPI/interface/ArViz/Elements.ice
index 6745ceb88..e9dc918a4 100644
--- a/source/RobotAPI/interface/ArViz/Elements.ice
+++ b/source/RobotAPI/interface/ArViz/Elements.ice
@@ -128,18 +128,18 @@ module viz
         FaceSeq faces;
     };
 
-    enum ModelDrawStyleFlags
+    module ModelDrawStyle
     {
-        ModelDrawStyle_ORIGINAL        = 0,
-        ModelDrawStyle_COLLISION       = 1,
-        ModelDrawStyle_OVERRIDE_COLOR  = 2,
+        const int ORIGINAL  = 0;
+        const int COLLISION = 1;
+        const int OVERRIDE_COLOR = 2;
     };
 
     class ElementRobot extends Element
     {
         string project;
         string filename;
-        int drawStyle = ModelDrawStyle_ORIGINAL;
+        int drawStyle = ModelDrawStyle::ORIGINAL;
 
         StringFloatDictionary jointValues;
     };
-- 
GitLab