ElementProxy.hpp
Go to the documentation of this file.
1 
28 #pragma once
29 
30 #ifndef LVR2_TYPES_ELEMENTPROXY
31 #define LVR2_TYPES_ELEMENTPROXY
32 
33 #include <array>
34 #include <iostream>
35 #include <boost/optional.hpp>
36 #include <memory>
38 
39 namespace lvr2 {
40 
41 // forward declaration for ElementProxyPtr
42 template<typename T>
44 
52 template<typename T>
54 {
55  public:
56  ElementProxyPtr(T* ptr = nullptr, size_t w = 0) : m_ptr(ptr), m_w(w) {}
57 
58  // "Pointer" arithmetic
59  ElementProxyPtr operator+(const ElementProxyPtr&) = delete;
60 
61  ssize_t operator-(const ElementProxyPtr& p)
62  {
63  return (this->m_ptr - p.m_ptr) / m_w;
64  }
65 
67  {
68  m_ptr += m_w;
69  return *this;
70  }
71 
73  {
74  ElementProxyPtr tmp(*this);
75  tmp += i;
76  return tmp;
77  }
78 
80  {
81  ElementProxyPtr tmp(*this);
82  operator++();
83  return tmp;
84  }
85 
87  {
88  m_ptr += (m_w * i);
89  return *this;
90  }
91 
93  {
94  m_ptr -= (m_w * i);
95  }
96 
97  // comparison operators
98  bool operator< (const ElementProxyPtr& rhs) const { return (*this).m_ptr < rhs.m_ptr; }
99  bool operator> (const ElementProxyPtr& rhs) const { return rhs < (*this); }
100  bool operator<=(const ElementProxyPtr& rhs) const { return !((*this) > rhs); }
101  bool operator>=(const ElementProxyPtr& rhs) const { return !((*this) < rhs); }
102  bool operator==(const ElementProxyPtr& rhs) const { return (*this).m_ptr == rhs.m_ptr; }
103  bool operator!=(const ElementProxyPtr& rhs) const { return !(*this == rhs); }
104 
105  // member access.
107 
108  // TODO [] operator ?!
109 
110  // I don't think we are able to return a useful raw pointer.
111  // Array pointer breaks the abstraction.
112  // Pointer to Elementproxy is pointless because we would need to create one with new.
113  // We cannot overload the -> operator in ElementProxy for the same reasons.
114  ElementProxy<T>* operator->() = delete;
115 
116  private:
117  T* m_ptr;
118  size_t m_w;
119 
120 };
121 
122 template<typename T>
123 class ElementProxy
124 {
125 public:
126  friend class ElementProxyPtr<T>;
133  {
134  return ElementProxyPtr<T>(m_ptr, m_w);
135  }
136 
138  {
139  if( m_ptr && (m_w == 1))
140  m_ptr[0] = v;
141  return *this;
142  }
143 
144  template<typename type, size_t size>
145  ElementProxy operator=(const std::array<type, size>& array)
146  {
147  if( m_ptr && (m_w == size))
148  {
149  for(int i=0; i<size; i++){
150  m_ptr[i] = array[i];
151  }
152  }
153  return *this;
154  }
155 
156  template<typename BaseVecT>
157  ElementProxy operator=(const BaseVecT& v)
158  {
159  if( m_ptr && (m_w > 2))
160  {
161  m_ptr[0] = v.x;
162  m_ptr[1] = v.y;
163  m_ptr[2] = v.z;
164  }
165  return *this;
166  }
167 
168  template<typename BaseVecT>
169  ElementProxy operator+=(const BaseVecT& v)
170  {
171  if( m_ptr && (m_w > 2))
172  {
173  m_ptr[0] += v.x;
174  m_ptr[1] += v.y;
175  m_ptr[2] += v.z;
176  }
177  return *this;
178  }
179 
180  template<typename BaseVecT>
181  ElementProxy operator-=(const BaseVecT& v)
182  {
183  if( m_ptr && (m_w > 2))
184  {
185  m_ptr[0] -= v.x;
186  m_ptr[1] -= v.y;
187  m_ptr[2] -= v.z;
188  }
189  return *this;
190  }
191 
192 
193  template<typename BaseVecT>
194  BaseVecT operator+(const BaseVecT& v)
195  {
196  if(m_w > 2)
197  {
198  *this += v;
199  return BaseVecT(m_ptr[0], m_ptr[1], m_ptr[2]);
200  }
201  throw std::range_error("Element Proxy: Width to small for BaseVec addition");
202  }
203 
204  template<typename BaseVecT>
205  BaseVecT operator-(const BaseVecT& v)
206  {
207  if(m_w > 2)
208  {
209  *this -= v;
210  return BaseVecT(m_ptr[0], m_ptr[1], m_ptr[2]);
211  }
212  throw std::range_error("Element Proxy: Width to small for BaseVec subtraction");
213  }
214 
215  ElementProxy(T* pos = nullptr, unsigned w = 0) : m_ptr(pos), m_w(w) {}
216 
217  T& operator[](int i)
218  {
219  if(m_ptr && (i < m_w))
220  {
221  return m_ptr[i];
222  }
223  throw std::range_error("Element Proxy: Index larger than width");
224  }
225 
226  const T& operator[](int i) const
227  {
228  if(m_ptr && (i < m_w))
229  {
230  return m_ptr[i];
231  }
232  throw std::range_error("Element Proxy: Index out of Bounds");
233  }
234 
236  template<typename BaseVecT>
237  operator BaseVecT() const
238  {
239  if(m_w == 3)
240  {
241  return BaseVecT(m_ptr[0], m_ptr[1], m_ptr[2]);
242  }
243  throw std::range_error("Element Proxy: Width != 3 in BaseVecT conversion");
244  }
245 
246  template <typename type, size_t size>
247  operator std::array<type, size>() const
248  {
249  if (size == m_w){
250  std::array<type, size> arr;
251  for(int i=0; i<size; i++){
252  arr[i] = m_ptr[i];
253  };
254  return arr;
255  }
256  throw std::range_error("Element Proxy: array size differs from channel with in std::array conversion.");
257  }
258 
259  operator std::array<VertexHandle, 3>() const
260  {
261  if(m_w == 3)
262  {
263  std::array<VertexHandle, 3> arr = {VertexHandle(m_ptr[0]), VertexHandle(m_ptr[1]), VertexHandle(m_ptr[2])};
264  return arr;
265  }
266  throw std::range_error("Element Proxy: Width != 3 in std::array conversion.");
267  }
268 
269  operator EdgeHandle() const
270  {
271  if(m_w == 1)
272  {
273  return EdgeHandle(m_ptr[0]);
274  }
275  throw std::range_error("Element Proxy: Width != 1 in EdgeHandle conversion.");
276  }
277 
278  operator FaceHandle() const
279  {
280  if(m_w == 1)
281  {
282  return FaceHandle(m_ptr[0]);
283  }
284  throw std::range_error("Element Proxy: Width != 1 in FaceHandle conversion.");
285  }
286 
287  operator T() const
288  {
289  if(m_w == 1)
290  {
291  return m_ptr[0];
292  }
293  throw std::range_error("Element Proxy: Width != 1 in content type conversion.");
294  }
295 
296 private:
297 
298  T* m_ptr;
299  unsigned m_w;
300 };
301 
302 } // namespace lvr2
303 
304 #endif // LVR2_TYPES_ELEMENTPROXY
lvr2::ElementProxy::operator=
ElementProxy operator=(const T &v)
Definition: ElementProxy.hpp:137
Handles.hpp
lvr2::ElementProxyPtr::operator++
ElementProxyPtr operator++(int)
Definition: ElementProxy.hpp:79
lvr2::ElementProxy
Definition: ElementProxy.hpp:43
lvr2::ElementProxyPtr::operator<
bool operator<(const ElementProxyPtr &rhs) const
Definition: ElementProxy.hpp:98
lvr2::ElementProxyPtr::operator>
bool operator>(const ElementProxyPtr &rhs) const
Definition: ElementProxy.hpp:99
lvr2::ElementProxy::operator=
ElementProxy operator=(const std::array< type, size > &array)
Definition: ElementProxy.hpp:145
lvr2::FaceHandle
Handle to access faces of the mesh.
Definition: Handles.hpp:140
lvr2::ElementProxy::operator=
ElementProxy operator=(const BaseVecT &v)
Definition: ElementProxy.hpp:157
lvr2::ElementProxyPtr::m_w
size_t m_w
Definition: ElementProxy.hpp:118
lvr2::ElementProxy::ElementProxy
ElementProxy(T *pos=nullptr, unsigned w=0)
Definition: ElementProxy.hpp:215
p
SharedPointer p
Definition: ConvertShared.hpp:42
lvr2::ElementProxy::operator+=
ElementProxy operator+=(const BaseVecT &v)
Definition: ElementProxy.hpp:169
lvr2::ElementProxyPtr::m_ptr
T * m_ptr
Definition: ElementProxy.hpp:117
lvr2::EdgeHandle
Handle to access edges of the mesh.
Definition: Handles.hpp:134
lvr2::ElementProxy::operator[]
T & operator[](int i)
Definition: ElementProxy.hpp:217
lvr2::ElementProxyPtr::operator<=
bool operator<=(const ElementProxyPtr &rhs) const
Definition: ElementProxy.hpp:100
lvr2::ElementProxyPtr::operator+=
ElementProxyPtr & operator+=(size_t i)
Definition: ElementProxy.hpp:86
lvr2::ElementProxy::m_ptr
T * m_ptr
Definition: ElementProxy.hpp:298
lvr2::ElementProxy::operator&
ElementProxyPtr< T > operator&()
look at ElementProxyPtr documentation.
Definition: ElementProxy.hpp:132
lvr2::ElementProxyPtr::operator>=
bool operator>=(const ElementProxyPtr &rhs) const
Definition: ElementProxy.hpp:101
lvr2::VertexHandle
Handle to access vertices of the mesh.
Definition: Handles.hpp:146
lvr2::ElementProxyPtr::operator++
ElementProxyPtr & operator++()
Definition: ElementProxy.hpp:66
lvr2::ElementProxyPtr::operator-
ssize_t operator-(const ElementProxyPtr &p)
Definition: ElementProxy.hpp:61
lvr2::ElementProxy::operator-=
ElementProxy operator-=(const BaseVecT &v)
Definition: ElementProxy.hpp:181
lvr2::ElementProxyPtr::operator+
ElementProxyPtr operator+(const ElementProxyPtr &)=delete
lvr2::ElementProxy::operator[]
const T & operator[](int i) const
Definition: ElementProxy.hpp:226
lvr2::ElementProxy::operator-
BaseVecT operator-(const BaseVecT &v)
Definition: ElementProxy.hpp:205
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::ElementProxyPtr
This class emulates a Pointer behaviour for an ElementProxy if its & operator is used....
Definition: ElementProxy.hpp:53
lvr2::ElementProxy::m_w
unsigned m_w
Definition: ElementProxy.hpp:299
lvr2::ElementProxy::operator+
BaseVecT operator+(const BaseVecT &v)
Definition: ElementProxy.hpp:194
lvr2::ElementProxy::ElementProxyPtr< T >
friend class ElementProxyPtr< T >
Definition: ElementProxy.hpp:126
lvr2::ElementProxyPtr::operator!=
bool operator!=(const ElementProxyPtr &rhs) const
Definition: ElementProxy.hpp:103
lvr2::ElementProxyPtr::operator->
ElementProxy< T > * operator->()=delete
lvr2::ElementProxyPtr::operator-=
ElementProxyPtr operator-=(size_t i)
Definition: ElementProxy.hpp:92
lvr2::ElementProxyPtr::operator*
ElementProxy< T > operator*()
Definition: ElementProxy.hpp:106
lvr2::ElementProxyPtr::ElementProxyPtr
ElementProxyPtr(T *ptr=nullptr, size_t w=0)
Definition: ElementProxy.hpp:56
lvr2::ElementProxyPtr::operator+
ElementProxyPtr operator+(size_t i)
Definition: ElementProxy.hpp:72
lvr2::ElementProxyPtr::operator==
bool operator==(const ElementProxyPtr &rhs) const
Definition: ElementProxy.hpp:102


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:23