Radium Engine  1.5.0
TriangleMesh.hpp
1 #pragma once
2 
3 #include <Core/Containers/VectorArray.hpp>
4 #include <Core/Geometry/AbstractGeometry.hpp>
5 #include <Core/Geometry/StandardAttribNames.hpp>
6 #include <Core/RaCore.hpp>
7 #include <Core/Types.hpp>
8 #include <Core/Utils/Attribs.hpp>
9 #include <Core/Utils/Color.hpp>
10 #include <Core/Utils/Observable.hpp>
11 
12 namespace Ra {
13 namespace Core {
14 namespace Geometry {
15 
25 class RA_CORE_API AttribArrayGeometry : public AbstractGeometry
26 {
27  public:
28  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29 
30  using Point = Vector3;
31  using Normal = Vector3;
32 
39 
41  inline AttribArrayGeometry() : AbstractGeometry() { initDefaultAttribs(); }
42 
45  inline explicit AttribArrayGeometry( const AttribArrayGeometry& other );
46 
49  inline explicit AttribArrayGeometry( AttribArrayGeometry&& other );
50 
53  inline AttribArrayGeometry& operator=( const AttribArrayGeometry& other );
54 
57  inline AttribArrayGeometry& operator=( AttribArrayGeometry&& other );
58 
59  ~AttribArrayGeometry() = default;
60 
64  bool append( const AttribArrayGeometry& other );
65 
67  void clear() override;
68 
70  inline void setVertices( PointAttribHandle::Container&& vertices );
72  inline void setVertices( const PointAttribHandle::Container& vertices );
73 
75  inline const PointAttribHandle::Container& vertices() const;
76 
78  inline void setNormals( PointAttribHandle::Container&& normals );
80  inline void setNormals( const PointAttribHandle::Container& normals );
81 
83  inline const NormalAttribHandle::Container& normals() const;
84 
88  template <typename T>
89  inline Utils::AttribHandle<T> getAttribHandle( const std::string& name ) const;
90 
92  template <typename T>
93  inline bool isValid( const Utils::AttribHandle<T>& h ) const;
94 
97 
99  template <typename T>
100  inline Utils::Attrib<T>& getAttrib( const Utils::AttribHandle<T>& h );
102  template <typename T>
103  inline const Utils::Attrib<T>& getAttrib( const Utils::AttribHandle<T>& h ) const;
104 
106  template <typename T>
107  inline Utils::Attrib<T>& getAttrib( const std::string& name );
109  template <typename T>
110  inline const Utils::Attrib<T>& getAttrib( const std::string& name ) const;
111 
113  template <typename T>
116  template <typename T>
117  inline const Utils::Attrib<T>* getAttribPtr( const Utils::AttribHandle<T>& h ) const;
118 
120  inline Utils::AttribBase* getAttribBase( const std::string& name );
122  inline const Utils::AttribBase* getAttribBase( const std::string& name ) const;
124 
127  inline bool hasAttrib( const std::string& name ) const;
128 
134  template <typename T>
135  inline Utils::AttribHandle<T> addAttrib( const std::string& name );
136 
141  template <typename T>
142  inline Utils::AttribHandle<T> addAttrib( const std::string& name,
143  const typename Core::VectorArray<T>& data );
144 
149  template <typename T>
150  inline Utils::AttribHandle<T> addAttrib( const std::string& name,
151  const typename Utils::Attrib<T>::Container&& data );
153 
156  template <typename T>
157  inline void removeAttrib( Utils::AttribHandle<T>& h );
158 
160  inline void clearAttributes();
161 
165  inline virtual void copyBaseGeometry( const AttribArrayGeometry& other );
166 
172  template <typename... Handles>
173  bool copyAttributes( const AttribArrayGeometry& input, Handles... attribs );
174 
180  inline bool copyAllAttributes( const AttribArrayGeometry& input );
181 
182  inline Aabb computeAabb() const override;
183 
186  void colorize( const Utils::Color& c );
187 
191  inline Utils::AttribManager& vertexAttribs();
192 
195  inline const Utils::AttribManager& vertexAttribs() const;
196 
199  inline PointAttribHandle::Container& verticesWithLock();
200 
202  inline void verticesUnlock();
203 
206  inline NormalAttribHandle::Container& normalsWithLock();
207 
209  inline void normalsUnlock();
210 
211  private:
213  inline void initDefaultAttribs();
214 
218  template <typename T>
219  void append_attrib( Utils::AttribBase* attr );
220 
222  Utils::AttribManager m_vertexAttribs;
223 
225  PointAttribHandle m_verticesHandle;
226 
228  NormalAttribHandle m_normalsHandle;
229 };
230 
231 class RA_CORE_API PointCloud : public AttribArrayGeometry
232 {};
233 
234 class RA_CORE_API LineStrip : public AttribArrayGeometry
235 {};
236 
238  AbstractGeometry( other ) {
239  m_vertexAttribs.copyAllAttributes( other.m_vertexAttribs );
240  m_verticesHandle = other.m_verticesHandle;
241  m_normalsHandle = other.m_normalsHandle;
242 }
243 
245  m_vertexAttribs( std::move( other.m_vertexAttribs ) ),
246  m_verticesHandle( std::move( other.m_verticesHandle ) ),
247  m_normalsHandle( std::move( other.m_normalsHandle ) ) {}
248 
250  if ( this != &other ) {
251  m_vertexAttribs.clear();
252  m_vertexAttribs.copyAllAttributes( other.m_vertexAttribs );
253  m_verticesHandle = other.m_verticesHandle;
254  m_normalsHandle = other.m_normalsHandle;
255 
256  invalidateAabb();
257  }
258  return *this;
259 }
260 
262  if ( this != &other ) {
263  m_vertexAttribs = std::move( other.m_vertexAttribs );
264  m_verticesHandle = std::move( other.m_verticesHandle );
265  m_normalsHandle = std::move( other.m_normalsHandle );
266 
267  invalidateAabb();
268  }
269  return *this;
270 }
271 
273  m_vertexAttribs.clear();
274  // restore the default attribs (empty though)
275  initDefaultAttribs();
276  invalidateAabb();
277 }
278 
280  clear();
281  m_vertexAttribs.copyAttributes(
282  other.m_vertexAttribs, other.m_verticesHandle, other.m_normalsHandle );
283  invalidateAabb();
284 }
285 
286 template <typename... Handles>
288  Handles... attribs ) {
289  if ( vertices().size() != input.vertices().size() ) return false;
290  // copy attribs
291  m_vertexAttribs.copyAttributes( input.m_vertexAttribs, attribs... );
292  invalidateAabb();
293  return true;
294 }
295 
297  if ( vertices().size() != input.vertices().size() ) return false;
298  // copy attribs
299  m_vertexAttribs.copyAllAttributes( input.m_vertexAttribs );
300  invalidateAabb();
301  return true;
302 }
303 
304 inline Aabb AttribArrayGeometry::computeAabb() const {
305  if ( !isAabbValid() ) {
306  Aabb aabb;
307  for ( const auto& v : vertices() ) {
308  aabb.extend( v );
309  }
310  setAabb( aabb );
311  }
312 
313  return getAabb();
314 }
315 
316 inline void AttribArrayGeometry::setVertices( PointAttribHandle::Container&& vertices ) {
317  m_vertexAttribs.setAttrib( m_verticesHandle, std::move( vertices ) );
318  invalidateAabb();
319 }
320 
321 inline void AttribArrayGeometry::setVertices( const PointAttribHandle::Container& vertices ) {
322  m_vertexAttribs.setAttrib<PointAttribHandle::value_type>( m_verticesHandle, vertices );
323  invalidateAabb();
324 }
325 
326 inline const AttribArrayGeometry::PointAttribHandle::Container&
328  return m_vertexAttribs.getAttrib( m_verticesHandle ).data();
329 }
330 
331 inline void AttribArrayGeometry::setNormals( PointAttribHandle::Container&& normals ) {
332  m_vertexAttribs.setAttrib( m_normalsHandle, std::move( normals ) );
333 }
334 inline void AttribArrayGeometry::setNormals( const PointAttribHandle::Container& normals ) {
335  m_vertexAttribs.setAttrib( m_normalsHandle, normals );
336 }
337 
338 inline const AttribArrayGeometry::NormalAttribHandle::Container&
340  return m_vertexAttribs.getAttrib( m_normalsHandle ).data();
341 }
342 
343 template <typename T>
345 AttribArrayGeometry::getAttribHandle( const std::string& name ) const {
346  return m_vertexAttribs.findAttrib<T>( name );
347 }
348 
349 template <typename T>
351  return m_vertexAttribs.isValid( h );
352 }
353 
354 template <typename T>
356  return m_vertexAttribs.getAttrib( h );
357 }
358 
359 template <typename T>
361  return m_vertexAttribs.getAttrib( h );
362 }
363 
364 inline Utils::AttribBase* AttribArrayGeometry::getAttribBase( const std::string& name ) {
365  return m_vertexAttribs.getAttribBase( name );
366 }
367 
368 inline const Utils::AttribBase*
369 AttribArrayGeometry::getAttribBase( const std::string& name ) const {
370  return m_vertexAttribs.getAttribBase( name );
371 }
372 
373 template <typename T>
374 inline const Utils::Attrib<T>& AttribArrayGeometry::getAttrib( const std::string& name ) const {
375  return m_vertexAttribs.getAttrib<T>( name );
376 }
377 
378 template <typename T>
379 inline Utils::Attrib<T>& AttribArrayGeometry::getAttrib( const std::string& name ) {
380  return m_vertexAttribs.getAttrib<T>( name );
381 }
382 
383 inline bool AttribArrayGeometry::hasAttrib( const std::string& name ) const {
384  return m_vertexAttribs.contains( name );
385 }
386 
387 template <typename T>
388 inline Utils::AttribHandle<T> AttribArrayGeometry::addAttrib( const std::string& name ) {
389  invalidateAabb();
390  return m_vertexAttribs.addAttrib<T>( name );
391 }
392 
393 template <typename T>
395 AttribArrayGeometry::addAttrib( const std::string& name,
396  const typename Core::VectorArray<T>& data ) {
397  auto handle = addAttrib<T>( name );
398  getAttrib( handle ).setData( data );
399  invalidateAabb();
400  return handle;
401 }
402 
403 template <typename T>
405 AttribArrayGeometry::addAttrib( const std::string& name,
406  const typename Utils::Attrib<T>::Container&& data ) {
407  auto handle = addAttrib<T>( name );
408  getAttrib( handle ).setData( std::move( data ) );
409  invalidateAabb();
410  return handle;
411 }
412 
413 template <typename T>
415  m_vertexAttribs.removeAttrib( h );
416  invalidateAabb();
417 }
418 
420  return m_vertexAttribs;
421 }
422 
424  return m_vertexAttribs;
425 }
426 
427 inline AttribArrayGeometry::PointAttribHandle::Container& AttribArrayGeometry::verticesWithLock() {
428  return m_vertexAttribs.getAttrib( m_verticesHandle ).getDataWithLock();
429 }
430 
432  return m_vertexAttribs.getAttrib( m_verticesHandle ).unlock();
433 }
434 
435 inline AttribArrayGeometry::NormalAttribHandle::Container& AttribArrayGeometry::normalsWithLock() {
436  return m_vertexAttribs.getAttrib( m_normalsHandle ).getDataWithLock();
437 }
438 
440  return m_vertexAttribs.getAttrib( m_normalsHandle ).unlock();
441 }
442 
443 inline void AttribArrayGeometry::initDefaultAttribs() {
444  m_verticesHandle = m_vertexAttribs.addAttrib<PointAttribHandle::value_type>(
445  getAttribName( MeshAttrib::VERTEX_POSITION ) );
446  m_normalsHandle = m_vertexAttribs.addAttrib<NormalAttribHandle::value_type>(
447  getAttribName( MeshAttrib::VERTEX_NORMAL ) );
448  invalidateAabb();
449 }
450 
451 template <typename T>
452 inline void AttribArrayGeometry::append_attrib( Utils::AttribBase* attr ) {
453  auto h = m_vertexAttribs.findAttrib<T>( attr->getName() );
454  auto& v0 = m_vertexAttribs.getAttrib( h ).getDataWithLock();
455  const auto& v1 = attr->cast<T>().data();
456  v0.insert( v0.end(), v1.cbegin(), v1.cend() );
457  m_vertexAttribs.getAttrib( h ).unlock();
458  invalidateAabb();
459 }
460 
461 } // namespace Geometry
462 } // namespace Core
463 } // namespace Ra
464 
465 #include "IndexedGeometry.hpp"
This class represents vertex + attributes per vertex. Toplogy is handled in MultiIndexedGeometry subc...
virtual void copyBaseGeometry(const AttribArrayGeometry &other)
Utils::AttribHandle< T > getAttribHandle(const std::string &name) const
bool isValid(const Utils::AttribHandle< T > &h) const
Return true if h refers to an existing attribute in *this.
NormalAttribHandle::Container & normalsWithLock()
bool copyAttributes(const AttribArrayGeometry &input, Handles... attribs)
Aabb computeAabb() const override
Compute bounding box.
Utils::Attrib< T > * getAttribPtr(const Utils::AttribHandle< T > &h)
void setNormals(PointAttribHandle::Container &&normals)
Set normals.
bool hasAttrib(const std::string &name) const
AttribArrayGeometry & operator=(const AttribArrayGeometry &other)
void normalsUnlock()
Release lock on vertices normals.
void clear() override
Erases all data, making the AttribArrayGeometry empty.
PointAttribHandle::Container & verticesWithLock()
void verticesUnlock()
Release lock on vertices positions.
void setVertices(PointAttribHandle::Container &&vertices)
Set vertices.
AttribArrayGeometry()
Create an empty geometry.
Utils::AttribBase * getAttribBase(const std::string &name)
bool copyAllAttributes(const AttribArrayGeometry &input)
const NormalAttribHandle::Container & normals() const
Access the vertices normals.
void removeAttrib(Utils::AttribHandle< T > &h)
const PointAttribHandle::Container & vertices() const
Access the vertices positions.
const Utils::Attrib< T > * getAttribPtr(const Utils::AttribHandle< T > &h) const
Utils::AttribHandle< T > addAttrib(const std::string &name)
Utils::Attrib< T > & getAttrib(const Utils::AttribHandle< T > &h)
Utils::AttribManager & vertexAttribs()
Attrib< T > & cast()
Downcast from AttribBase to Attrib<T>.
Definition: Attribs.hpp:462
std::string getName() const
Return the attribute's name.
Definition: Attribs.hpp:449
The AttribManager provides attributes management by handles.
Definition: Attribs.hpp:204
void setAttrib(const AttribHandle< T > &h, const typename AttribHandle< T >::Container &data)
Definition: Attribs.hpp:678
void copyAllAttributes(const AttribManager &m)
Definition: Attribs.cpp:23
AttribBase * getAttribBase(const std::string &name)
Definition: Attribs.hpp:689
AttribHandle< T > addAttrib(const std::string &name)
Definition: Attribs.hpp:712
void copyAttributes(const AttribManager &m)
Base copy, does nothing.
Definition: Attribs.hpp:592
bool contains(const std::string &name) const
contains Check if an attribute with the given name exists.
Definition: Attribs.hpp:618
Attrib< T > & getAttrib(const AttribHandle< T > &h)
Definition: Attribs.hpp:649
bool isValid(const AttribHandle< T > &h) const
Return true if h correspond to an existing attribute in *this.
Definition: Attribs.hpp:613
void clear()
clear all attribs, invalidate handles.
Definition: Attribs.cpp:17
void removeAttrib(AttribHandle< T > &h)
Definition: Attribs.hpp:740
AttribHandle< T > findAttrib(const std::string &name) const
findAttrib Grab an attribute handler by name.
Definition: Attribs.hpp:623
Definition: Cage.cpp:3