From 33a6756d24c4191f01431d7f106f65174a77d12c Mon Sep 17 00:00:00 2001
From: Raphael Grimm <ufdrv@student.kit.edu>
Date: Fri, 6 May 2016 09:42:42 +0200
Subject: [PATCH] now a sequence of add clear add operations does not result in
 an empty layer (now the last add is executed)

---
 .../DebugDrawer/DebugDrawerComponent.cpp      | 326 +++++-------------
 .../DebugDrawer/DebugDrawerComponent.h        |   7 +-
 2 files changed, 82 insertions(+), 251 deletions(-)

diff --git a/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.cpp b/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.cpp
index c8659d536..9c0a8d0cf 100644
--- a/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.cpp
+++ b/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.cpp
@@ -1782,14 +1782,12 @@ namespace armarx
     void DebugDrawerComponent::clearLayer(const std::string& layerName, const Ice::Current&)
     {
         ScopedRecursiveLockPtr lockData = getScopedAccumulatedDataLock();
+        //updates should only contain elements to add for each layer after the last clear for this layer was executed
+        //this prevents a sequence add,clear,add to result in an empty layer
+        //=>remove all objects pending for this layer
+        removeAccumulatedData(layerName);
+        accumulatedUpdateData.clearLayers.emplace(layerName);
 
-        // check if already requested
-        if (std::find(accumulatedUpdateData.clearLayers.begin(), accumulatedUpdateData.clearLayers.end(), layerName) != accumulatedUpdateData.clearLayers.end())
-        {
-            return;
-        }
-
-        accumulatedUpdateData.clearLayers.push_back(layerName);
     }
 
     void DebugDrawerComponent::clearLayerQt(const std::string& layerName)
@@ -1816,8 +1814,6 @@ namespace armarx
 
         selectionNode->deselectAll();
 
-        removeAccumulatedData(layerName);
-
         Layer& layer = layers.at(layerName);
 
         for (const auto & i : layer.addedCoordVisualizations)
@@ -1950,113 +1946,103 @@ namespace armarx
         ScopedRecursiveLockPtr lockData = getScopedAccumulatedDataLock();
         ScopedRecursiveLockPtr lockVisu = getScopedVisuLock();
 
-        for (auto i = accumulatedUpdateData.coord.begin(); i != accumulatedUpdateData.coord.end(); i++)
+        // check for clear&remove
+        //(updates only contain elements to add for each layer after the last clear for this layer was executed)
+        //this prevents a sequence add,clear,add to result in an empty layer
+        for (const auto & layer : accumulatedUpdateData.clearLayers)
         {
-            drawCoordSystem(i->second);
+            clearLayerQt(layer);
         }
+        accumulatedUpdateData.clearLayers.clear();
 
-        accumulatedUpdateData.coord.clear();
+        for (const auto & layer : accumulatedUpdateData.removeLayers)
+        {
+            removeLayerQt(layer);
+        }
+        accumulatedUpdateData.removeLayers.clear();
 
-        for (auto i = accumulatedUpdateData.box.begin(); i != accumulatedUpdateData.box.end(); i++)
+        //add elements
+        for (auto & e : accumulatedUpdateData.coord)
         {
-            drawBox(i->second);
+            drawCoordSystem(e.second);
         }
+        accumulatedUpdateData.coord.clear();
 
+        for (auto & e : accumulatedUpdateData.box)
+        {
+            drawBox(e.second);
+        }
         accumulatedUpdateData.box.clear();
 
-        for (auto i = accumulatedUpdateData.line.begin(); i != accumulatedUpdateData.line.end(); i++)
+        for (auto & e : accumulatedUpdateData.line)
         {
-            drawLine(i->second);
+            drawLine(e.second);
         }
-
         accumulatedUpdateData.line.clear();
 
-        for (auto i = accumulatedUpdateData.lineSet.begin(); i != accumulatedUpdateData.lineSet.end(); i++)
+        for (auto & e : accumulatedUpdateData.lineSet)
         {
-            drawLineSet(i->second);
+            drawLineSet(e.second);
         }
-
         accumulatedUpdateData.lineSet.clear();
 
-        for (auto i = accumulatedUpdateData.sphere.begin(); i != accumulatedUpdateData.sphere.end(); i++)
+        for (auto & e : accumulatedUpdateData.sphere)
         {
-            drawSphere(i->second);
+            drawSphere(e.second);
         }
-
         accumulatedUpdateData.sphere.clear();
 
-        for (auto i = accumulatedUpdateData.cylinder.begin(); i != accumulatedUpdateData.cylinder.end(); i++)
+        for (auto & e : accumulatedUpdateData.cylinder)
         {
-            drawCylinder(i->second);
+            drawCylinder(e.second);
         }
-
         accumulatedUpdateData.cylinder.clear();
 
-        for (auto i = accumulatedUpdateData.text.begin(); i != accumulatedUpdateData.text.end(); i++)
+        for (auto & e : accumulatedUpdateData.text)
         {
-            drawText(i->second);
+            drawText(e.second);
         }
-
         accumulatedUpdateData.text.clear();
 
-        for (auto i = accumulatedUpdateData.pointcloud.begin(); i != accumulatedUpdateData.pointcloud.end(); i++)
+        for (auto & e : accumulatedUpdateData.pointcloud)
         {
-            drawPointCloud(i->second);
+            drawPointCloud(e.second);
         }
-
         accumulatedUpdateData.pointcloud.clear();
 
-        for (auto i = accumulatedUpdateData.polygons.begin(); i != accumulatedUpdateData.polygons.end(); i++)
+        for (auto & e : accumulatedUpdateData.polygons)
         {
-            drawPolygon(i->second);
+            drawPolygon(e.second);
         }
-
         accumulatedUpdateData.polygons.clear();
 
-        for (auto i = accumulatedUpdateData.arrows.begin(); i != accumulatedUpdateData.arrows.end(); i++)
+        for (auto & e : accumulatedUpdateData.arrows)
         {
-            drawArrow(i->second);
+            drawArrow(e.second);
         }
-
         accumulatedUpdateData.arrows.clear();
 
-        for (auto i = accumulatedUpdateData.robots.begin(); i != accumulatedUpdateData.robots.end(); i++)
+        for (auto & e : accumulatedUpdateData.robots)
         {
-            ARMARX_DEBUG << "update visu / drawRobot for robot " << i->first;
-            ensureExistingRobotNodes(i->second);
+            ARMARX_DEBUG << "update visu / drawRobot for robot " << e.first;
+            ensureExistingRobotNodes(e.second);
 
-            drawRobot(i->second);
+            drawRobot(e.second);
         }
-
         accumulatedUpdateData.robots.clear();
 
-        for (auto i = accumulatedUpdateData.coloredpointcloud.begin(); i != accumulatedUpdateData.coloredpointcloud.end(); i++)
+        for (auto & e : accumulatedUpdateData.coloredpointcloud)
         {
-            drawColoredPointCloud(i->second);
+            drawColoredPointCloud(e.second);
         }
-
         accumulatedUpdateData.coloredpointcloud.clear();
 
-        for (auto i = accumulatedUpdateData.colored24Bitpointcloud.begin(); i != accumulatedUpdateData.colored24Bitpointcloud.end(); i++)
+        for (auto & e : accumulatedUpdateData.colored24Bitpointcloud)
         {
-            draw24BitColoredPointCloud(i->second);
+            draw24BitColoredPointCloud(e.second);
         }
-
         accumulatedUpdateData.colored24Bitpointcloud.clear();
 
-        // check for clear and remove
-        for (auto i = accumulatedUpdateData.clearLayers.begin(); i != accumulatedUpdateData.clearLayers.end(); i++)
-        {
-            clearLayerQt(*i);
-        }
-        accumulatedUpdateData.clearLayers.clear();
-
-        for (auto i = accumulatedUpdateData.removeLayers.begin(); i != accumulatedUpdateData.removeLayers.end(); i++)
-        {
-            removeLayerQt(*i);
-        }
-        accumulatedUpdateData.removeLayers.clear();
-
         onUpdateVisualization();
     }
 
@@ -2069,13 +2055,11 @@ namespace armarx
     void DebugDrawerComponent::removeLayer(const std::string& layerName, const ::Ice::Current&)
     {
         ScopedRecursiveLockPtr lockData = getScopedAccumulatedDataLock();
-        // check if already requested
-        if (std::find(accumulatedUpdateData.removeLayers.begin(), accumulatedUpdateData.removeLayers.end(), layerName) != accumulatedUpdateData.removeLayers.end())
-        {
-            return;
-        }
-
-        accumulatedUpdateData.removeLayers.push_back(layerName);
+        //updates should only contain elements to add for each layer after the last clear/remove for this layer was executed
+        //this prevents a sequence add,clear,add to result in an empty layer
+        //=>remove all objects pending for this layer
+        removeAccumulatedData(layerName);
+        accumulatedUpdateData.removeLayers.emplace(layerName);
     }
 
     void DebugDrawerComponent::removeLayerQt(const std::string& layerName)
@@ -2106,192 +2090,37 @@ namespace armarx
         }
     }
 
-    void DebugDrawerComponent::removeAccumulatedData(const std::string& layerName)
+    template<class UpdateDataType>
+    void removeUpdateDataFromMap(const std::string& layerName, std::map<std::string, UpdateDataType>& map)
     {
-        ScopedRecursiveLockPtr lockData = getScopedAccumulatedDataLock();
-        std::string entryName = "__" + layerName + "__";
+        auto it = map.begin();
+        while (it != map.end())
         {
-            auto i1 = accumulatedUpdateData.coord.begin();
-
-            while (i1 != accumulatedUpdateData.coord.end())
+            auto curr = it++;
+            if (curr->second.layerName == layerName)
             {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.coord.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
+                map.erase(curr);
             }
         }
+    }
 
-        {
-            auto i1 = accumulatedUpdateData.box.begin();
-
-            while (i1 != accumulatedUpdateData.box.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.box.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.line.begin();
-
-            while (i1 != accumulatedUpdateData.line.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.line.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.lineSet.begin();
-
-            while (i1 != accumulatedUpdateData.lineSet.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.lineSet.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.sphere.begin();
-
-            while (i1 != accumulatedUpdateData.sphere.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.sphere.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.cylinder.begin();
-
-            while (i1 != accumulatedUpdateData.cylinder.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.cylinder.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.text.begin();
-
-            while (i1 != accumulatedUpdateData.text.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.text.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.pointcloud.begin();
-
-            while (i1 != accumulatedUpdateData.pointcloud.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.pointcloud.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.polygons.begin();
-
-            while (i1 != accumulatedUpdateData.polygons.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.polygons.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            auto i1 = accumulatedUpdateData.arrows.begin();
-
-            while (i1 != accumulatedUpdateData.arrows.end())
-            {
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    i1 = accumulatedUpdateData.arrows.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
-        }
-
-        {
-            ARMARX_DEBUG << "Removing all accumulated data for robot, nr of robots:" << accumulatedUpdateData.robots.size();
-
-            auto i1 = accumulatedUpdateData.robots.begin();
-
-            while (i1 != accumulatedUpdateData.robots.end())
-            {
-                ARMARX_DEBUG << "checking accumulated data " << i1->first;
-
-                if (boost::starts_with(i1->first, entryName))
-                {
-                    ARMARX_DEBUG << "removing accumulated data for " << i1->first;
-                    i1 = accumulatedUpdateData.robots.erase(i1);
-                }
-                else
-                {
-                    i1++;
-                }
-            }
+    void DebugDrawerComponent::removeAccumulatedData(const std::string& layerName)
+    {
+        ScopedRecursiveLockPtr lockData = getScopedAccumulatedDataLock();
 
-            ARMARX_DEBUG << "end, nr of robots:" << accumulatedUpdateData.robots.size();
-        }
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.coord);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.line);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.lineSet);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.box);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.text);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.sphere);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.cylinder);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.pointcloud);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.coloredpointcloud);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.colored24Bitpointcloud);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.polygons);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.arrows);
+        removeUpdateDataFromMap(layerName, accumulatedUpdateData.robots);
 
         onRemoveAccumulatedData(layerName);
 
@@ -2574,5 +2403,4 @@ namespace armarx
     {
         remove24BitColoredPointCloudVisu(DEBUG_LAYER_NAME, pointCloudName);
     }
-
 }//namespace armarx
diff --git a/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.h b/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.h
index f7e64a70a..cffcb8e07 100644
--- a/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.h
+++ b/source/RobotAPI/components/DebugDrawer/DebugDrawerComponent.h
@@ -293,6 +293,9 @@ namespace armarx
             std::string layerName;
             std::string name;
             bool active;
+            /**
+             * @brief Whether an existing visu should be updated
+             */
             bool update;
         };
 
@@ -407,8 +410,8 @@ namespace armarx
             std::map<std::string, ArrowData> arrows;
             std::map<std::string, RobotData> robots;
 
-            std::vector< std::string > clearLayers;
-            std::vector< std::string > removeLayers;
+            std::set< std::string > clearLayers;
+            std::set< std::string > removeLayers;
         };
 
         UpdateData accumulatedUpdateData;
-- 
GitLab