Visualizer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ElementVisualizer.h"
4 
5 #include <RobotAPI/interface/ArViz/Component.h>
6 
7 #include <Inventor/manips/SoTransformerManip.h>
8 #include <Inventor/nodes/SoSelection.h>
9 #include <Inventor/nodes/SoSeparator.h>
10 
11 #include <IceUtil/Shared.h>
12 
13 #include <functional>
14 #include <typeindex>
15 #include <memory>
16 #include <string>
17 #include <mutex>
18 
19 namespace armarx::viz
20 {
21  using CoinLayerID = std::pair<std::string, std::string>;
22 
23 
25  {
26  data::ElementPtr data;
27  std::unique_ptr<coin::ElementVisualization> visu;
28  };
29 
30  inline bool isElementIdLess(CoinLayerElement const& left, CoinLayerElement const& right)
31  {
32  return left.data->id < right.data->id;
33  }
34 
35 
36  struct CoinLayer
37  {
38  CoinLayer() = default;
39 
40  CoinLayer(CoinLayerID const& id, SoSeparator* node)
41  : id(id)
42  , node(node)
43  {
44  // Increase ref-count, so we can add/remove this node without it being deleted
45  // This is needed for showing/hiding layers
46  node->ref();
47  pivot.data = new data::Element;
48  }
49 
51  : id(std::move(other.id))
52  , node(other.node)
53  , elements(std::move(other.elements))
54  , pivot(std::move(other.pivot))
55  {
56  other.node = nullptr;
57  }
58 
60  {
61  if (node)
62  {
63  node->unref();
64  }
65  }
66 
67  CoinLayer(CoinLayer const&) = delete;
68  void operator= (CoinLayer const&) = delete;
69 
70  void operator= (CoinLayer&& other)
71  {
72  id = std::move(other.id);
73  node = other.node;
74  other.node = nullptr;
75  elements = std::move(other.elements);
76  }
77 
78  auto lowerBound(std::string const& id)
79  {
80  pivot.data->id = id;
81  return std::lower_bound(elements.begin(), elements.end(), pivot, &isElementIdLess);
82  }
83 
84  CoinLayerElement* findElement(std::string const& id)
85  {
86  auto iter = lowerBound(id);
87  if (iter != elements.end() && iter->data->id == id)
88  {
89  return &*iter;
90  }
91  return nullptr;
92  }
93 
94  CoinLayerElement const* findElement(std::string const& id) const
95  {
96  return const_cast<CoinLayer*>(this)->findElement(id);
97  }
98 
100  SoSeparator* node = nullptr;
101  std::vector<CoinLayerElement> elements;
103  };
104 
105  inline bool isCoinLayerIdLess(CoinLayer const& left, CoinLayer const& right)
106  {
107  return left.id < right.id;
108  }
109 
111  {
112  auto lowerBound(CoinLayerID const& id)
113  {
114  pivot.id = id;
115  return std::lower_bound(data.begin(), data.end(), pivot, &isCoinLayerIdLess);
116  }
117 
119  {
120  auto iter = lowerBound(id);
121  if (iter != data.end() && iter->id == id)
122  {
123  return &*iter;
124  }
125  return nullptr;
126  }
127 
128  CoinLayer const* findLayer(CoinLayerID const& id) const
129  {
130  return const_cast<CoinLayerMap*>(this)->findLayer(id);
131  }
132 
133  std::vector<CoinLayer> data;
135  };
136 
138  {
139  STOPPED,
140  STARTING,
141  RUNNING,
142  STOPPING
143  };
144 
146  {
147  SUCCESS,
148  WAITING,
149  FAILURE,
150  };
151 
153  {
154  std::string layerName;
155 
156  IceUtil::Time addLayer = IceUtil::Time::seconds(0);
157  IceUtil::Time updateElements = IceUtil::Time::seconds(0);
158  IceUtil::Time removeElements = IceUtil::Time::seconds(0);
159  IceUtil::Time total = IceUtil::Time::seconds(0);
160 
161  void add(CoinVisualizer_ApplyTiming const& other)
162  {
163  addLayer += other.addLayer;
166  total += other.total;
167  }
168  };
169 
171  {
172  std::vector<CoinVisualizer_ApplyTiming> applies;
173 
175 
176  IceUtil::Time pull = IceUtil::Time::seconds(0);
177  IceUtil::Time layersChanged = IceUtil::Time::seconds(0);
178  IceUtil::Time total = IceUtil::Time::seconds(0);
179 
180  IceUtil::Time waitStart = IceUtil::Time::seconds(0);
181  IceUtil::Time waitDuration = IceUtil::Time::seconds(0);
182 
183  int updateToggle = 0;
184 
185  int counter = 0;
186  };
187 
188  struct CoinVisualizerWrapper;
189 
191  {
193  std::string element;
194  viz::data::InteractionDescription interaction;
196  };
197 
199  {
200  public:
201  CoinVisualizer();
202 
203  ~CoinVisualizer();
204 
205  void clearCache();
206 
208 
209  void startAsync(StorageInterfacePrx const& storage);
210 
211  void stop();
212 
213  CoinVisualizer_ApplyTiming apply(data::LayerUpdate const& update);
214 
215  void update();
216 
217  void exportToVRML(std::string const& exportFilePath);
218 
219  template <typename ElementVisuT>
221  {
222  using ElementT = typename ElementVisuT::ElementType;
223  using VisualizerT = coin::TypedElementVisualizer<ElementVisuT>;
224  // Map element type to visualizer
225  elementVisualizersTypes.emplace_back(typeid(ElementT));
226  elementVisualizers.push_back(std::make_unique<VisualizerT>());
227  }
228 
229  void showLayer(CoinLayerID const& id, bool visible);
230 
232 
233  CoinLayer& findOrAddLayer(CoinLayerID const& layerID);
234  void addOrUpdateElements(CoinLayer* layer, data::LayerUpdate const& update);
235  void addOrUpdateInteraction(CoinLayerID const& layerID, std::string const& elementID,
236  data::InteractionDescription const& interactionDesc,
239 
240  std::vector<CoinLayerID> getLayerIDs();
241  void emitLayersChanged(std::vector<CoinLayerID> const& layerIDs);
242  void emitLayerUpdated(CoinLayerID const& layerID, CoinLayer const& layer);
243 
244  void selectElement(int index);
245  void onSelectEvent(SoPath* path, int eventType);
246  void onManipulation(SoDragger* dragger, int eventType);
247  // These are selectable element IDs and need to be persistent in memory.
248  // We store a raw pointer to these into the SoSeperator objects of Coin.
249  // Later, we can retrieve the element ID from this pointer, if it has been selected.
250  std::vector<std::unique_ptr<ElementInteractionData>> elementInteractions;
251  std::vector<viz::data::InteractionFeedback> interactionFeedbackBuffer;
252  // The currently selected element. Maybe nullptr if no element is selected.
254 
255  void onUpdateSuccess(data::LayerUpdates const& updates);
256  void onUpdateFailure(Ice::Exception const& ex);
258  armarx::viz::Callback_StorageInterface_pullUpdatesSinceAndSendInteractionsPtr callback;
259 
260  std::mutex storageMutex;
261  viz::StorageInterfacePrx storage;
262 
264 
265  std::vector<std::type_index> elementVisualizersTypes;
266  std::vector<std::unique_ptr<coin::ElementVisualizer>> elementVisualizers;
267 
268  SoSelection* selection = nullptr;
269  SoSeparator* root = nullptr;
270  SoSeparator* manipulatorGroup = nullptr;
271  SoTransformerManip* manipulator = nullptr;
272 
273  std::atomic<CoinVisualizerUpdateResult> updateResult{CoinVisualizerUpdateResult::SUCCESS};
274  data::LayerUpdates pulledUpdates;
275 
276  std::mutex stateMutex;
277  std::atomic<CoinVisualizerState> state{CoinVisualizerState::STOPPED};
278  viz::StorageInterfacePrx stateStorage;
279 
280  std::function<void(std::vector<CoinLayerID> const&)> layersChangedCallback;
281 
282  /// A layer's data has changed.
283  std::vector<std::function<void(CoinLayerID const& layerID, CoinLayer const& layer)>> layerUpdatedCallbacks;
284 
285  std::mutex timingMutex;
287  };
288 }
armarx::viz::CoinVisualizer::selectedElement
ElementInteractionData * selectedElement
Definition: Visualizer.h:253
armarx::viz::CoinLayer
Definition: Visualizer.h:36
armarx::viz::CoinVisualizer_ApplyTiming::removeElements
IceUtil::Time removeElements
Definition: Visualizer.h:158
armarx::viz::CoinVisualizer::findOrAddLayer
CoinLayer & findOrAddLayer(CoinLayerID const &layerID)
Definition: Visualizer.cpp:204
armarx::viz::CoinVisualizer_UpdateTiming::waitDuration
IceUtil::Time waitDuration
Definition: Visualizer.h:181
armarx::viz::CoinLayerMap::findLayer
CoinLayer * findLayer(CoinLayerID const &id)
Definition: Visualizer.h:118
armarx::viz::CoinLayer::elements
std::vector< CoinLayerElement > elements
Definition: Visualizer.h:101
armarx::viz::CoinVisualizer::pulledUpdates
data::LayerUpdates pulledUpdates
Definition: Visualizer.h:274
armarx::viz::CoinVisualizer::state
std::atomic< CoinVisualizerState > state
Definition: Visualizer.h:277
armarx::viz::CoinVisualizer::selectElement
void selectElement(int index)
Definition: Visualizer.cpp:583
armarx::viz::CoinLayer::operator=
void operator=(CoinLayer const &)=delete
armarx::viz::coin::ElementVisualization
Definition: ElementVisualizer.h:21
armarx::viz::CoinVisualizer::elementVisualizers
std::vector< std::unique_ptr< coin::ElementVisualizer > > elementVisualizers
Definition: Visualizer.h:266
armarx::viz::CoinVisualizer_UpdateTiming::total
IceUtil::Time total
Definition: Visualizer.h:178
armarx::viz::CoinVisualizer::showLayer
void showLayer(CoinLayerID const &id, bool visible)
Definition: Visualizer.cpp:514
armarx::viz::CoinVisualizer::onSelectEvent
void onSelectEvent(SoPath *path, int eventType)
Definition: Visualizer.cpp:619
armarx::viz::ElementInteractionData::visu
coin::ElementVisualization * visu
Definition: Visualizer.h:195
ElementVisualizer.h
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::viz::CoinVisualizer::clearCache
void clearCache()
Definition: Visualizer.cpp:142
armarx::viz::CoinVisualizer::startAsync
void startAsync(StorageInterfacePrx const &storage)
Definition: Visualizer.cpp:149
armarx::viz::CoinVisualizer::stateStorage
viz::StorageInterfacePrx stateStorage
Definition: Visualizer.h:278
armarx::viz::CoinVisualizer::manipulator
SoTransformerManip * manipulator
Definition: Visualizer.h:271
armarx::viz::CoinVisualizer::selection
SoSelection * selection
Definition: Visualizer.h:268
armarx::viz::CoinLayerMap::pivot
CoinLayer pivot
Definition: Visualizer.h:134
armarx::viz::ElementInteractionData::layer
CoinLayerID layer
Definition: Visualizer.h:192
armarx::viz::CoinVisualizer::layerUpdatedCallbacks
std::vector< std::function< void(CoinLayerID const &layerID, CoinLayer const &layer)> > layerUpdatedCallbacks
A layer's data has changed.
Definition: Visualizer.h:283
armarx::viz::CoinVisualizer::getLayerIDs
std::vector< CoinLayerID > getLayerIDs()
Definition: Visualizer.cpp:553
armarx::viz::CoinVisualizer::onUpdateFailure
void onUpdateFailure(Ice::Exception const &ex)
Definition: Visualizer.cpp:507
armarx::viz::CoinVisualizerState::STOPPING
@ STOPPING
armarx::viz::CoinVisualizer::emitLayerUpdated
void emitLayerUpdated(CoinLayerID const &layerID, CoinLayer const &layer)
Definition: Visualizer.cpp:572
armarx::viz::CoinLayer::pivot
CoinLayerElement pivot
Definition: Visualizer.h:102
armarx::viz::CoinVisualizer::removeElementsIfNotUpdated
void removeElementsIfNotUpdated(CoinLayer *layer)
Definition: Visualizer.cpp:350
armarx::viz::CoinVisualizer::stateMutex
std::mutex stateMutex
Definition: Visualizer.h:276
armarx::viz::CoinVisualizer::onManipulation
void onManipulation(SoDragger *dragger, int eventType)
Definition: Visualizer.cpp:812
armarx::viz::CoinVisualizer_UpdateTiming::pull
IceUtil::Time pull
Definition: Visualizer.h:176
armarx::viz::CoinVisualizer::callback
armarx::viz::Callback_StorageInterface_pullUpdatesSinceAndSendInteractionsPtr callback
Definition: Visualizer.h:258
armarx::viz::CoinVisualizer_ApplyTiming::updateElements
IceUtil::Time updateElements
Definition: Visualizer.h:157
armarx::viz::CoinLayer::node
SoSeparator * node
Definition: Visualizer.h:100
armarx::viz::CoinVisualizerState::RUNNING
@ RUNNING
armarx::viz::CoinVisualizer_ApplyTiming::add
void add(CoinVisualizer_ApplyTiming const &other)
Definition: Visualizer.h:161
armarx::viz::CoinLayer::CoinLayer
CoinLayer(CoinLayer &&other)
Definition: Visualizer.h:50
armarx::viz::CoinVisualizerState::STARTING
@ STARTING
armarx::viz::CoinVisualizer_UpdateTiming::updateToggle
int updateToggle
Definition: Visualizer.h:183
armarx::viz::CoinVisualizer_UpdateTiming::counter
int counter
Definition: Visualizer.h:185
armarx::viz::CoinVisualizer_UpdateTiming
Definition: Visualizer.h:170
armarx::viz::CoinLayer::lowerBound
auto lowerBound(std::string const &id)
Definition: Visualizer.h:78
armarx::viz::CoinVisualizerUpdateResult::FAILURE
@ FAILURE
armarx::viz::ElementInteractionData::interaction
viz::data::InteractionDescription interaction
Definition: Visualizer.h:194
armarx::viz::ElementInteractionData
Definition: Visualizer.h:190
armarx::viz::CoinLayer::id
CoinLayerID id
Definition: Visualizer.h:99
armarx::viz::CoinVisualizer::callbackData
IceUtil::Handle< CoinVisualizerWrapper > callbackData
Definition: Visualizer.h:257
armarx::viz::CoinVisualizer::CoinVisualizer
CoinVisualizer()
Definition: Visualizer.cpp:112
armarx::viz::CoinVisualizerUpdateResult::WAITING
@ WAITING
armarx::viz::CoinLayerMap::data
std::vector< CoinLayer > data
Definition: Visualizer.h:133
armarx::viz::CoinVisualizer_UpdateTiming::waitStart
IceUtil::Time waitStart
Definition: Visualizer.h:180
armarx::viz::CoinVisualizer_UpdateTiming::layersChanged
IceUtil::Time layersChanged
Definition: Visualizer.h:177
armarx::viz::CoinVisualizer::registerVisualizationTypes
void registerVisualizationTypes()
Definition: RegisterVisualizationTypes.cpp:21
armarx::viz::CoinVisualizer::addOrUpdateInteraction
void addOrUpdateInteraction(CoinLayerID const &layerID, std::string const &elementID, data::InteractionDescription const &interactionDesc, coin::ElementVisualization *visu)
Definition: Visualizer.cpp:319
armarx::viz::CoinLayerMap::findLayer
CoinLayer const * findLayer(CoinLayerID const &id) const
Definition: Visualizer.h:128
armarx::viz::ElementInteractionData::element
std::string element
Definition: Visualizer.h:193
armarx::viz::CoinVisualizer::~CoinVisualizer
~CoinVisualizer()
Definition: Visualizer.cpp:138
armarx::viz::CoinVisualizer::emitLayersChanged
void emitLayersChanged(std::vector< CoinLayerID > const &layerIDs)
Definition: Visualizer.cpp:564
armarx::viz::CoinLayer::~CoinLayer
~CoinLayer()
Definition: Visualizer.h:59
armarx::viz::CoinVisualizer_ApplyTiming::total
IceUtil::Time total
Definition: Visualizer.h:159
armarx::viz::CoinVisualizer::layers
CoinLayerMap layers
Definition: Visualizer.h:263
armarx::viz::CoinLayerElement::data
data::ElementPtr data
Definition: Visualizer.h:26
armarx::viz::CoinLayerElement
Definition: Visualizer.h:24
armarx::viz::isElementIdLess
bool isElementIdLess(CoinLayerElement const &left, CoinLayerElement const &right)
Definition: Visualizer.h:30
armarx::viz::CoinVisualizer::stop
void stop()
Definition: Visualizer.cpp:163
armarx::viz::coin::TypedElementVisualizer
Definition: ElementVisualizer.h:72
armarx::viz::CoinVisualizer::exportToVRML
void exportToVRML(std::string const &exportFilePath)
Definition: Visualizer.cpp:942
armarx::viz::CoinVisualizer::addOrUpdateElements
void addOrUpdateElements(CoinLayer *layer, data::LayerUpdate const &update)
Definition: Visualizer.cpp:222
armarx::viz::CoinVisualizer::update
void update()
Definition: Visualizer.cpp:389
armarx::viz::CoinVisualizerState
CoinVisualizerState
Definition: Visualizer.h:137
armarx::viz::CoinVisualizer_ApplyTiming::layerName
std::string layerName
Definition: Visualizer.h:154
armarx::viz::CoinVisualizer_UpdateTiming::applyTotal
CoinVisualizer_ApplyTiming applyTotal
Definition: Visualizer.h:174
armarx::viz::CoinLayerElement::visu
std::unique_ptr< coin::ElementVisualization > visu
Definition: Visualizer.h:27
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::viz::CoinVisualizer::root
SoSeparator * root
Definition: Visualizer.h:269
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::viz::CoinVisualizer::lastTiming
CoinVisualizer_UpdateTiming lastTiming
Definition: Visualizer.h:286
armarx::viz::CoinVisualizer
Definition: Visualizer.h:198
armarx::viz::CoinVisualizer::getTiming
CoinVisualizer_UpdateTiming getTiming()
Definition: Visualizer.cpp:547
armarx::viz::CoinVisualizerState::STOPPED
@ STOPPED
armarx::viz::CoinVisualizer::storage
viz::StorageInterfacePrx storage
Definition: Visualizer.h:261
armarx::viz::CoinVisualizer::apply
CoinVisualizer_ApplyTiming apply(data::LayerUpdate const &update)
Definition: Visualizer.cpp:173
armarx::viz::CoinVisualizer::onUpdateSuccess
void onUpdateSuccess(data::LayerUpdates const &updates)
Definition: Visualizer.cpp:501
armarx::viz::CoinVisualizer_UpdateTiming::applies
std::vector< CoinVisualizer_ApplyTiming > applies
Definition: Visualizer.h:172
armarx::viz::CoinVisualizer::timingMutex
std::mutex timingMutex
Definition: Visualizer.h:285
armarx::viz::CoinLayer::findElement
CoinLayerElement * findElement(std::string const &id)
Definition: Visualizer.h:84
armarx::viz::CoinVisualizer::storageMutex
std::mutex storageMutex
Definition: Visualizer.h:260
armarx::viz::CoinVisualizer_ApplyTiming
Definition: Visualizer.h:152
armarx::viz::CoinLayer::CoinLayer
CoinLayer()=default
armarx::viz::CoinVisualizerWrapper
Definition: Visualizer.cpp:30
armarx::viz::CoinVisualizer::elementInteractions
std::vector< std::unique_ptr< ElementInteractionData > > elementInteractions
Definition: Visualizer.h:250
std
Definition: Application.h:66
armarx::viz::isCoinLayerIdLess
bool isCoinLayerIdLess(CoinLayer const &left, CoinLayer const &right)
Definition: Visualizer.h:105
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::viz::CoinVisualizer::updateResult
std::atomic< CoinVisualizerUpdateResult > updateResult
Definition: Visualizer.h:273
armarx::viz::CoinVisualizer::registerVisualizerFor
void registerVisualizerFor()
Definition: Visualizer.h:220
armarx::viz::CoinLayer::CoinLayer
CoinLayer(CoinLayerID const &id, SoSeparator *node)
Definition: Visualizer.h:40
armarx::viz::CoinLayer::findElement
CoinLayerElement const * findElement(std::string const &id) const
Definition: Visualizer.h:94
armarx::viz::CoinVisualizer::layersChangedCallback
std::function< void(std::vector< CoinLayerID > const &)> layersChangedCallback
Definition: Visualizer.h:280
armarx::viz::CoinVisualizerUpdateResult
CoinVisualizerUpdateResult
Definition: Visualizer.h:145
armarx::viz::CoinVisualizerUpdateResult::SUCCESS
@ SUCCESS
armarx::viz::CoinVisualizer::elementVisualizersTypes
std::vector< std::type_index > elementVisualizersTypes
Definition: Visualizer.h:265
armarx::viz::CoinLayerMap::lowerBound
auto lowerBound(CoinLayerID const &id)
Definition: Visualizer.h:112
armarx::viz::CoinLayerMap
Definition: Visualizer.h:110
armarx::viz
This file is part of ArmarX.
Definition: ArVizStorage.cpp:416
armarx::viz::CoinLayerID
std::pair< std::string, std::string > CoinLayerID
Definition: Visualizer.h:21
armarx::viz::CoinVisualizer_ApplyTiming::addLayer
IceUtil::Time addLayer
Definition: Visualizer.h:156
armarx::viz::CoinVisualizer::interactionFeedbackBuffer
std::vector< viz::data::InteractionFeedback > interactionFeedbackBuffer
Definition: Visualizer.h:251
armarx::viz::CoinVisualizer::manipulatorGroup
SoSeparator * manipulatorGroup
Definition: Visualizer.h:270