ElementVisualizer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <RobotAPI/interface/ArViz/Elements.h>
4 
5 #include <Inventor/nodes/SoSeparator.h>
6 #include <Inventor/nodes/SoSwitch.h>
7 
8 #include <memory>
9 
10 namespace armarx::viz::data
11 {
12  class Element;
13 }
14 
15 class SoUnits;
16 class SoTransform;
17 class SoMaterial;
18 
19 namespace armarx::viz::coin
20 {
22  {
24 
25  virtual ~ElementVisualization() = default;
26 
27  void updateBase(data::Element const& element);
28 
29  SoSeparator* separator;
30  SoUnits* units;
31  SoTransform* transform;
32  SoMaterial* material;
33  SoSwitch* switch_;
34  // TODO: Transform to flag system
35  bool wasUpdated = true;
36  bool visible = true;
37  };
38 
40  {
41  public:
42  virtual ~ElementVisualizer() = default;
43 
44  std::unique_ptr<ElementVisualization> create(data::Element const& element);
45  bool update(data::Element const& element, ElementVisualization* visu);
46 
47  virtual ElementVisualization* createDerived() = 0;
48  virtual bool updateDerived(data::Element const& element, ElementVisualization* data) = 0;
49  };
50 
51 
52  template <typename NodeT>
54  {
55  using NodeType = NodeT;
56 
58  {
59  node = new NodeType;
60  node->ref();
61  }
62 
64  {
65  node->unref();
66  }
67 
69  };
70 
71  template <typename DataT>
73  {
74  public:
75  using DataType = DataT;
77 
78 
80  {
81  DataType* result = new DataType;
82  result->switch_->addChild(result->node);
83  return result;
84  }
85 
86  bool updateDerived(data::Element const& element_, ElementVisualization* data_) final
87  {
88  auto const& element = static_cast<ElementType const&>(element_);
89  auto* data = dynamic_cast<DataType*>(data_);
90 
91  if (data)
92  {
93  bool hidden = (element.flags & armarx::viz::data::ElementFlags::HIDDEN);
94  if (hidden)
95  {
96  if (data->visible)
97  {
98  data->separator->removeChild(data->node);
99  data->visible = false;
100  }
101  }
102  else if (!data->visible)
103  {
104  data->visible = true;
105  data->separator->addChild(data->node);
106  }
107 
108  // We want to call update with the correctly downcasted arguments
109  return data->update(element);
110  }
111  return false;
112  }
113  };
114 }
armarx::viz::coin::ElementVisualization::~ElementVisualization
virtual ~ElementVisualization()=default
armarx::viz::coin::ElementVisualization
Definition: ElementVisualizer.h:21
armarx::viz::coin::TypedElementVisualization::TypedElementVisualization
TypedElementVisualization()
Definition: ElementVisualizer.h:57
armarx::viz::coin::ElementVisualizer::createDerived
virtual ElementVisualization * createDerived()=0
armarx::viz::coin::TypedElementVisualization
Definition: ElementVisualizer.h:53
armarx::viz::coin::TypedElementVisualizer::DataType
DataT DataType
Definition: ElementVisualizer.h:75
armarx::viz::coin::TypedElementVisualizer::ElementType
typename DataType::ElementType ElementType
Definition: ElementVisualizer.h:76
armarx::viz::coin::ElementVisualizer::update
bool update(data::Element const &element, ElementVisualization *visu)
Definition: ElementVisualizer.cpp:59
armarx::viz::coin::ElementVisualization::updateBase
void updateBase(data::Element const &element)
Definition: ElementVisualizer.cpp:32
armarx::viz::coin::ElementVisualization::switch_
SoSwitch * switch_
Definition: ElementVisualizer.h:33
armarx::viz::coin::ElementVisualization::transform
SoTransform * transform
Definition: ElementVisualizer.h:31
armarx::viz::coin::TypedElementVisualizer::updateDerived
bool updateDerived(data::Element const &element_, ElementVisualization *data_) final
Definition: ElementVisualizer.h:86
armarx::viz::coin::ElementVisualization::material
SoMaterial * material
Definition: ElementVisualizer.h:32
armarx::viz::coin::TypedElementVisualization::node
NodeType * node
Definition: ElementVisualizer.h:68
armarx::viz::data
Definition: ArVizStorage.cpp:416
armarx::viz::coin::ElementVisualizer::updateDerived
virtual bool updateDerived(data::Element const &element, ElementVisualization *data)=0
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::viz::coin::TypedElementVisualizer
Definition: ElementVisualizer.h:72
armarx::ElementTypes::ElementType
ElementType
Definition: AbstractObjectSerializer.h:32
armarx::viz::coin::ElementVisualizer::~ElementVisualizer
virtual ~ElementVisualizer()=default
armarx::viz::coin::ElementVisualization::units
SoUnits * units
Definition: ElementVisualizer.h:30
armarx::viz::coin::TypedElementVisualizer::createDerived
DataType * createDerived() final
Definition: ElementVisualizer.h:79
armarx::viz::coin::ElementVisualization::visible
bool visible
Definition: ElementVisualizer.h:36
armarx::viz::coin
Definition: ElementVisualizer.cpp:11
armarx::viz::coin::ElementVisualizer
Definition: ElementVisualizer.h:39
armarx::viz::coin::ElementVisualization::wasUpdated
bool wasUpdated
Definition: ElementVisualizer.h:35
armarx::viz::coin::ElementVisualizer::create
std::unique_ptr< ElementVisualization > create(data::Element const &element)
Definition: ElementVisualizer.cpp:51
armarx::viz::coin::ElementVisualization::separator
SoSeparator * separator
Definition: ElementVisualizer.h:29
armarx::viz::coin::TypedElementVisualization< SoSeparator >::NodeType
SoSeparator NodeType
Definition: ElementVisualizer.h:55
armarx::viz::coin::ElementVisualization::ElementVisualization
ElementVisualization()
Definition: ElementVisualizer.cpp:13
armarx::viz::coin::TypedElementVisualization::~TypedElementVisualization
~TypedElementVisualization()
Definition: ElementVisualizer.h:63