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>;
132  ElementProxyPtr<T> operator&()
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
ElementProxy operator=(const BaseVecT &v)
Handle to access vertices of the mesh.
Definition: Handles.hpp:146
ElementProxy operator-=(const BaseVecT &v)
ElementProxy operator=(const T &v)
bool operator>(const ElementProxyPtr &rhs) const
BaseVecT operator+(const BaseVecT &v)
Handle to access edges of the mesh.
Definition: Handles.hpp:134
bool operator<=(const ElementProxyPtr &rhs) const
ElementProxyPtr operator++(int)
ElementProxy operator=(const std::array< type, size > &array)
ssize_t operator-(const ElementProxyPtr &p)
bool operator==(const ElementProxyPtr &rhs) const
bool operator>=(const ElementProxyPtr &rhs) const
ElementProxyPtr & operator+=(size_t i)
SharedPointer p
ElementProxyPtr & operator++()
ElementProxyPtr operator-=(size_t i)
bool operator!=(const ElementProxyPtr &rhs) const
ElementProxy< T > operator*()
ElementProxyPtr operator+(size_t i)
bool operator<(const ElementProxyPtr &rhs) const
ElementProxy(T *pos=nullptr, unsigned w=0)
ElementProxyPtr(T *ptr=nullptr, size_t w=0)
ElementProxyPtr operator+(const ElementProxyPtr &)=delete
ElementProxy< T > * operator->()=delete
This class emulates a Pointer behaviour for an ElementProxy if its & operator is used. The arithmetic is based on the width of an ElementProxy. It was necessary for the Octree. USE WITH CARE.
BaseVecT operator-(const BaseVecT &v)
const T & operator[](int i) const
Handle to access faces of the mesh.
Definition: Handles.hpp:140
ElementProxy operator+=(const BaseVecT &v)


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 Mon Feb 28 2022 22:46:06