Rumba C++ SDK
WeightedComponent.h
Go to the documentation of this file.
1 /*
2 
3  *
4  ***
5  *****
6  ********************* Mercenaries Engineering SARL
7  ***************** Copyright (C) 2018
8  *************
9  ********* http://www.mercenaries-engineering.com
10  ***********
11  **** ****
12  ** **
13 
14 */
15 #pragma once
16 
17 #include "Points.h"
18 #include "SparseBuffer.h"
19 
20  namespace maquina
21  {
23 
58  {
59  public:
62  const Points &inputGeometry,
63  const char *weights_name,
64  float weight,
65  Shape::Topology topology
66  ) : _weight(weight)
67  {
68  const Value attribute = inputGeometry.read_attribute(weights_name, topology);
69  _indexed = *weights_name != '\0' && SparseBufferConstFloat::can_cast(attribute);
70  if(_indexed)
71  {
72  const SparseBufferConstFloat weights(attribute);
73  _indices = weights.read_indices();
74  _weights = weights.read_values();
75  _size = uint32_t(_indices.size());
76  }
77  else
78  {
79  _size = uint32_t(inputGeometry.read_points().size());
80  }
81  }
82 
85  const Points& inputGeometry,
86  float weight
87  ) : _weight(weight)
88  {
89  _indexed = false;
90  _size = uint32_t(inputGeometry.read_points().size());
91  }
92 
95  const SparseBufferConstFloat& weights,
96  float weight
97  ) : _weight(weight)
98  {
99  _indexed = true;
100  _indices = weights.read_indices();
101  _weights = weights.read_values();
102  _size = uint32_t(_indices.size());
103  }
104 
105  bool has_weights() const
106  {
107  return _indexed;
108  }
109 
111  class Iterator
112  {
113  public:
114  Iterator(uint32_t index, const WeightedComponent *wc) : _index(index), _wc(wc) {}
115 
116  std::pair<uint32_t, float> operator*() const
117  {
118  if(_wc->_indexed)
119  return {_wc->_indices[_index], _wc->_weights[_index]*_wc->_weight};
120  else
121  return {_index, _wc->_weight};
122  }
123 
125  uint32_t index() const
126  {
127  if(_wc->_indexed)
128  return _wc->_indices[_index];
129  else
130  return _index;
131  }
132 
134  float weight() const
135  {
136  if(_wc->_indexed)
137  return _wc->_weights[_index]*_wc->_weight;
138  else
139  return _wc->_weight;
140  }
141 
143  {
144  assert(_index < _wc->_size);
145  _index++;
146  return *this;
147  }
148 
149  Iterator &operator+(uint32_t d)
150  {
151  assert(_index+d <= _wc->_size);
152  _index += d;
153  return *this;
154  }
155 
156  bool operator!=(const Iterator &o) const
157  {
158  return _index != o._index;
159  }
160 
161  private:
162  uint32_t _index;
163  const WeightedComponent *_wc;
164  };
165 
167  Iterator begin() const
168  {
169  return {0, this};
170  }
171 
173  Iterator end() const
174  {
175  return {_size, this};
176  }
177 
179  uint32_t size() const
180  {
181  return _size;
182  }
183  private:
184  uint32_t _size;
185  float _weight;
186  bool _indexed;
187  gsl::span<const uint32_t> _indices;
188  gsl::span<const float> _weights;
189  };
190  }
This class is a helper to iterate over a geometry component, with an weight per component element...
Definition: WeightedComponent.h:57
Iterator begin() const
The begin iterator.
Definition: WeightedComponent.h:167
const Value read_attribute(const char *attribute_name, Topology topology) const
Get a read access to an attribute.
Topology
The different attribute topologies.
Definition: Shape.h:38
std::pair< uint32_t, float > operator*() const
Definition: WeightedComponent.h:116
WeightedComponent(const Points &inputGeometry, float weight)
Build a WeightedComponent object from a geometry.
Definition: WeightedComponent.h:84
Iterator & operator++()
Definition: WeightedComponent.h:142
This version of the SDK is unstable, i-e, it may change with no warning.
Definition: AddCurveAction.h:20
float weight() const
Return the component element weight, including the global weight.
Definition: WeightedComponent.h:134
const BufferConstV3f read_points() const
Return the points buffer.
Iterator end() const
The end iterator.
Definition: WeightedComponent.h:173
gsl::span< const value_type > read_values() const
Return a readable accessor on the buffer indices.
bool operator!=(const Iterator &o) const
Definition: WeightedComponent.h:156
Iterator(uint32_t index, const WeightedComponent *wc)
Definition: WeightedComponent.h:114
static bool can_cast(const Value &v)
WeightedComponent(const SparseBufferConstFloat &weights, float weight)
Build a WeightedComponent object from a weight mask.
Definition: WeightedComponent.h:94
The component iterator.
Definition: WeightedComponent.h:111
size_t size() const
Return the number of element in the buffer.
WeightedComponent(const Points &inputGeometry, const char *weights_name, float weight, Shape::Topology topology)
Build a WeightedComponent object using a points geometry with an optional weight mask.
Definition: WeightedComponent.h:61
Iterator & operator+(uint32_t d)
Definition: WeightedComponent.h:149
uint32_t size() const
Return the number of component element to iterate.
Definition: WeightedComponent.h:179
uint32_t index() const
Return the component element index.
Definition: WeightedComponent.h:125
A set of 3d points.
Definition: Points.h:25
bool has_weights() const
Definition: WeightedComponent.h:105
gsl::span< const uint32_t > read_indices() const
Return a readable accessor on the indices buffer.
Base class of all Rumba values.
Definition: Value.h:34