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 "SparseBuffer.h"
18 
19  namespace maquina
20  {
22 
57  {
58  public:
61  const Points &inputGeometry,
62  const char *weights_name,
63  float weight,
64  Shape::Topology topology
65  ) : _weight(weight)
66  {
67  const Value attribute = inputGeometry.read_attribute(weights_name, topology);
68  _indexed = *weights_name != '\0' && SparseBufferConstFloat::can_cast(attribute);
69  if(_indexed)
70  {
71  const SparseBufferConstFloat weights(attribute);
72  _indices = weights.read_indices();
73  _weights = weights.read_values();
74  _size = uint32_t(_indices.size());
75  }
76  else
77  {
78  _size = uint32_t(inputGeometry.read_points().size());
79  }
80  }
81 
82  bool has_weights() const
83  {
84  return _indexed;
85  }
86 
88  class Iterator
89  {
90  public:
91  Iterator(uint32_t index, const WeightedComponent *wc) : _index(index), _wc(wc) {}
92 
93  std::pair<uint32_t, float> operator*() const
94  {
95  if(_wc->_indexed)
96  return {_wc->_indices[_index], _wc->_weights[_index]*_wc->_weight};
97  else
98  return {_index, _wc->_weight};
99  }
100 
102  uint32_t index() const
103  {
104  if(_wc->_indexed)
105  return _wc->_indices[_index];
106  else
107  return _index;
108  }
109 
111  float weight() const
112  {
113  if(_wc->_indexed)
114  return _wc->_weights[_index]*_wc->_weight;
115  else
116  return _wc->_weight;
117  }
118 
120  {
121  assert(_index < _wc->_size);
122  _index++;
123  return *this;
124  }
125 
126  Iterator &operator+(uint32_t d)
127  {
128  assert(_index+d <= _wc->_size);
129  _index += d;
130  return *this;
131  }
132 
133  bool operator!=(const Iterator &o) const
134  {
135  return _index != o._index;
136  }
137 
138  private:
139  uint32_t _index;
140  const WeightedComponent *_wc;
141  };
142 
144  Iterator begin() const
145  {
146  return {0, this};
147  }
148 
150  Iterator end() const
151  {
152  return {_size, this};
153  }
154 
156  uint32_t size() const
157  {
158  return _size;
159  }
160  private:
161  uint32_t _size;
162  float _weight;
163  bool _indexed;
164  gsl::span<const uint32_t> _indices;
165  gsl::span<const float> _weights;
166  };
167  }
This class is a helper to iterate over a geometry component, with an weight per component element...
Definition: WeightedComponent.h:56
Iterator begin() const
The begin iterator.
Definition: WeightedComponent.h:144
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:36
std::pair< uint32_t, float > operator*() const
Definition: WeightedComponent.h:93
Iterator & operator++()
Definition: WeightedComponent.h:119
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:111
const BufferConstV3f read_points() const
Return the points buffer.
Iterator end() const
The end iterator.
Definition: WeightedComponent.h:150
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:133
Iterator(uint32_t index, const WeightedComponent *wc)
Definition: WeightedComponent.h:91
static bool can_cast(const Value &v)
The component iterator.
Definition: WeightedComponent.h:88
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.
Definition: WeightedComponent.h:60
Iterator & operator+(uint32_t d)
Definition: WeightedComponent.h:126
uint32_t size() const
Return the number of component element to iterate.
Definition: WeightedComponent.h:156
uint32_t index() const
Return the component element index.
Definition: WeightedComponent.h:102
A set of 3d points.
Definition: Points.h:26
bool has_weights() const
Definition: WeightedComponent.h:82
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
A read only buffer of sparse values. It is composed of a buffer of values and a buffer of the index i...
Definition: SparseBuffer.h:23