Radium Engine  1.5.20
Loading...
Searching...
No Matches
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
12namespace Ra {
13namespace Core {
14namespace Geometry {
15
25class RA_CORE_API AttribArrayGeometry : public AbstractGeometry
26{
27 public:
28 using Point = Vector3;
29 using Normal = Vector3;
30
37
39 inline AttribArrayGeometry() : AbstractGeometry() { initDefaultAttribs(); }
40
43 inline explicit AttribArrayGeometry( const AttribArrayGeometry& other );
44
47 inline explicit AttribArrayGeometry( AttribArrayGeometry&& other );
48
51 inline AttribArrayGeometry& operator=( const AttribArrayGeometry& other );
52
55 inline AttribArrayGeometry& operator=( AttribArrayGeometry&& other );
56
57 ~AttribArrayGeometry() = default;
58
62 bool append( const AttribArrayGeometry& other );
63
65 void clear() override;
66
68 inline void setVertices( PointAttribHandle::Container&& vertices );
70 inline void setVertices( const PointAttribHandle::Container& vertices );
71
73 inline const PointAttribHandle::Container& vertices() const;
74
76 inline void setNormals( PointAttribHandle::Container&& normals );
78 inline void setNormals( const PointAttribHandle::Container& normals );
79
81 inline const NormalAttribHandle::Container& normals() const;
82
86 template <typename T>
87 inline Utils::AttribHandle<T> getAttribHandle( const std::string& name ) const;
88
90 template <typename T>
91 inline bool isValid( const Utils::AttribHandle<T>& h ) const;
92
95
97 template <typename T>
98 inline Utils::Attrib<T>& getAttrib( const Utils::AttribHandle<T>& h );
100 template <typename T>
101 inline const Utils::Attrib<T>& getAttrib( const Utils::AttribHandle<T>& h ) const;
102
104 template <typename T>
105 inline Utils::Attrib<T>& getAttrib( const std::string& name );
107 template <typename T>
108 inline const Utils::Attrib<T>& getAttrib( const std::string& name ) const;
109
111 template <typename T>
114 template <typename T>
115 inline const Utils::Attrib<T>* getAttribPtr( const Utils::AttribHandle<T>& h ) const;
116
118 inline Utils::AttribBase* getAttribBase( const std::string& name );
120 inline const Utils::AttribBase* getAttribBase( const std::string& name ) const;
122
125 inline bool hasAttrib( const std::string& name ) const;
126
132 template <typename T>
133 inline Utils::AttribHandle<T> addAttrib( const std::string& name );
134
139 template <typename T>
140 inline Utils::AttribHandle<T> addAttrib( const std::string& name,
141 const typename Core::VectorArray<T>& data );
142
147 template <typename T>
148 inline Utils::AttribHandle<T> addAttrib( const std::string& name,
149 const typename Utils::Attrib<T>::Container&& data );
151
154 template <typename T>
155 inline void removeAttrib( Utils::AttribHandle<T>& h );
156
158 inline void clearAttributes();
159
163 inline virtual void copyBaseGeometry( const AttribArrayGeometry& other );
164
170 template <typename... Handles>
171 bool copyAttributes( const AttribArrayGeometry& input, Handles... attribs );
172
178 inline bool copyAllAttributes( const AttribArrayGeometry& input );
179
180 inline Aabb computeAabb() const override;
181
184 void colorize( const Utils::Color& c );
185
189 inline Utils::AttribManager& vertexAttribs();
190
193 inline const Utils::AttribManager& vertexAttribs() const;
194
197 inline PointAttribHandle::Container& verticesWithLock();
198
200 inline void verticesUnlock();
201
204 inline NormalAttribHandle::Container& normalsWithLock();
205
207 inline void normalsUnlock();
208
209 private:
211 inline void initDefaultAttribs();
212
216 template <typename T>
217 void append_attrib( Utils::AttribBase* attr );
218
220 Utils::AttribManager m_vertexAttribs;
221
223 PointAttribHandle m_verticesHandle;
224
226 NormalAttribHandle m_normalsHandle;
227};
228
229class RA_CORE_API PointCloud : public AttribArrayGeometry
230{};
231
232class RA_CORE_API LineStrip : public AttribArrayGeometry
233{};
234
235inline AttribArrayGeometry ::AttribArrayGeometry( const AttribArrayGeometry& other ) :
236 AbstractGeometry( other ) {
237 m_vertexAttribs.copyAllAttributes( other.m_vertexAttribs );
238 m_verticesHandle = other.m_verticesHandle;
239 m_normalsHandle = other.m_normalsHandle;
240}
241
243 m_vertexAttribs( std::move( other.m_vertexAttribs ) ),
244 m_verticesHandle( std::move( other.m_verticesHandle ) ),
245 m_normalsHandle( std::move( other.m_normalsHandle ) ) {}
246
248 if ( this != &other ) {
249 m_vertexAttribs.clear();
250 m_vertexAttribs.copyAllAttributes( other.m_vertexAttribs );
251 m_verticesHandle = other.m_verticesHandle;
252 m_normalsHandle = other.m_normalsHandle;
253
254 invalidateAabb();
255 }
256 return *this;
257}
258
260 if ( this != &other ) {
261 m_vertexAttribs = std::move( other.m_vertexAttribs );
262 m_verticesHandle = std::move( other.m_verticesHandle );
263 m_normalsHandle = std::move( other.m_normalsHandle );
264
265 invalidateAabb();
266 }
267 return *this;
268}
269
271 m_vertexAttribs.clear();
272 // restore the default attribs (empty though)
273 initDefaultAttribs();
274 invalidateAabb();
275}
276
278 clear();
279 m_vertexAttribs.copyAttributes(
280 other.m_vertexAttribs, other.m_verticesHandle, other.m_normalsHandle );
281 invalidateAabb();
282}
283
284template <typename... Handles>
286 Handles... attribs ) {
287 if ( vertices().size() != input.vertices().size() ) return false;
288 // copy attribs
289 m_vertexAttribs.copyAttributes( input.m_vertexAttribs, attribs... );
290 invalidateAabb();
291 return true;
292}
293
295 if ( vertices().size() != input.vertices().size() ) return false;
296 // copy attribs
297 m_vertexAttribs.copyAllAttributes( input.m_vertexAttribs );
298 invalidateAabb();
299 return true;
300}
301
303 if ( !isAabbValid() ) {
304 Aabb aabb;
305 for ( const auto& v : vertices() ) {
306 aabb.extend( v );
307 }
308 setAabb( aabb );
309 }
310
311 return getAabb();
312}
313
314inline void AttribArrayGeometry::setVertices( PointAttribHandle::Container&& vertices ) {
315 m_vertexAttribs.setAttrib( m_verticesHandle, std::move( vertices ) );
316 invalidateAabb();
317}
318
319inline void AttribArrayGeometry::setVertices( const PointAttribHandle::Container& vertices ) {
320 m_vertexAttribs.setAttrib<PointAttribHandle::value_type>( m_verticesHandle, vertices );
321 invalidateAabb();
322}
323
324inline const AttribArrayGeometry::PointAttribHandle::Container&
326 return m_vertexAttribs.getAttrib( m_verticesHandle ).data();
327}
328
329inline void AttribArrayGeometry::setNormals( PointAttribHandle::Container&& normals ) {
330 m_vertexAttribs.setAttrib( m_normalsHandle, std::move( normals ) );
331}
332inline void AttribArrayGeometry::setNormals( const PointAttribHandle::Container& normals ) {
333 m_vertexAttribs.setAttrib( m_normalsHandle, normals );
334}
335
336inline const AttribArrayGeometry::NormalAttribHandle::Container&
338 return m_vertexAttribs.getAttrib( m_normalsHandle ).data();
339}
340
341template <typename T>
344 return m_vertexAttribs.findAttrib<T>( name );
345}
346
347template <typename T>
349 return m_vertexAttribs.isValid( h );
350}
351
352template <typename T>
354 return m_vertexAttribs.getAttrib( h );
355}
356
357template <typename T>
359 return m_vertexAttribs.getAttrib( h );
360}
361
363 return m_vertexAttribs.getAttribBase( name );
364}
365
366inline const Utils::AttribBase*
368 return m_vertexAttribs.getAttribBase( name );
369}
370
371template <typename T>
373 return m_vertexAttribs.getAttrib<T>( name );
374}
375
376template <typename T>
378 return m_vertexAttribs.getAttrib<T>( name );
379}
380
381inline bool AttribArrayGeometry::hasAttrib( const std::string& name ) const {
382 return m_vertexAttribs.contains( name );
383}
384
385template <typename T>
387 invalidateAabb();
388 return m_vertexAttribs.addAttrib<T>( name );
389}
390
391template <typename T>
394 const typename Core::VectorArray<T>& data ) {
395 auto handle = addAttrib<T>( name );
396 getAttrib( handle ).setData( data );
397 invalidateAabb();
398 return handle;
399}
400
401template <typename T>
404 const typename Utils::Attrib<T>::Container&& data ) {
405 auto handle = addAttrib<T>( name );
406 getAttrib( handle ).setData( std::move( data ) );
407 invalidateAabb();
408 return handle;
409}
410
411template <typename T>
413 m_vertexAttribs.removeAttrib( h );
414 invalidateAabb();
415}
416
418 return m_vertexAttribs;
419}
420
422 return m_vertexAttribs;
423}
424
425inline AttribArrayGeometry::PointAttribHandle::Container& AttribArrayGeometry::verticesWithLock() {
426 return m_vertexAttribs.getAttrib( m_verticesHandle ).getDataWithLock();
427}
428
430 return m_vertexAttribs.getAttrib( m_verticesHandle ).unlock();
431}
432
433inline AttribArrayGeometry::NormalAttribHandle::Container& AttribArrayGeometry::normalsWithLock() {
434 return m_vertexAttribs.getAttrib( m_normalsHandle ).getDataWithLock();
435}
436
438 return m_vertexAttribs.getAttrib( m_normalsHandle ).unlock();
439}
440
441inline void AttribArrayGeometry::initDefaultAttribs() {
442 m_verticesHandle = m_vertexAttribs.addAttrib<PointAttribHandle::value_type>(
443 getAttribName( MeshAttrib::VERTEX_POSITION ) );
444 m_normalsHandle = m_vertexAttribs.addAttrib<NormalAttribHandle::value_type>(
445 getAttribName( MeshAttrib::VERTEX_NORMAL ) );
446 invalidateAabb();
447}
448
449template <typename T>
450inline void AttribArrayGeometry::append_attrib( Utils::AttribBase* attr ) {
451 auto h = m_vertexAttribs.findAttrib<T>( attr->getName() );
452 auto& v0 = m_vertexAttribs.getAttrib( h ).getDataWithLock();
453 const auto& v1 = attr->cast<T>().data();
454 v0.insert( v0.end(), v1.cbegin(), v1.cend() );
455 m_vertexAttribs.getAttrib( h ).unlock();
456 invalidateAabb();
457}
458
459} // namespace Geometry
460} // namespace Core
461} // namespace Ra
462
463#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.
const Utils::Attrib< T > * getAttribPtr(const Utils::AttribHandle< T > &h) const
NormalAttribHandle::Container & normalsWithLock()
bool copyAttributes(const AttribArrayGeometry &input, Handles... attribs)
Aabb computeAabb() const override
Compute bounding box.
void setNormals(PointAttribHandle::Container &&normals)
Set normals.
bool hasAttrib(const std::string &name) const
AttribArrayGeometry & operator=(const AttribArrayGeometry &other)
Utils::Attrib< T > * getAttribPtr(const Utils::AttribHandle< T > &h)
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.
Utils::AttribHandle< T > addAttrib(const std::string &name)
Utils::Attrib< T > & getAttrib(const Utils::AttribHandle< T > &h)
Attrib< T > & cast()
Downcast from AttribBase to Attrib<T>.
Definition Attribs.hpp:461
std::string getName() const
Return the attribute's name.
Definition Attribs.hpp:448
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:677
void copyAllAttributes(const AttribManager &m)
Definition Attribs.cpp:23
AttribBase * getAttribBase(const std::string &name)
Definition Attribs.hpp:688
AttribHandle< T > addAttrib(const std::string &name)
Definition Attribs.hpp:711
void copyAttributes(const AttribManager &m)
Base copy, does nothing.
Definition Attribs.hpp:591
bool contains(const std::string &name) const
contains Check if an attribute with the given name exists.
Definition Attribs.hpp:617
Attrib< T > & getAttrib(const AttribHandle< T > &h)
Definition Attribs.hpp:648
bool isValid(const AttribHandle< T > &h) const
Return true if h correspond to an existing attribute in *this.
Definition Attribs.hpp:612
void clear()
clear all attribs, invalidate handles.
Definition Attribs.cpp:17
void removeAttrib(AttribHandle< T > &h)
Definition Attribs.hpp:739
AttribHandle< T > findAttrib(const std::string &name) const
findAttrib Grab an attribute handler by name.
Definition Attribs.hpp:622
This class implements ContainerIntrospectionInterface for AlignedStdVector.
T move(T... args)
@ Geometry
"Geometry" render objects are those loaded using Radium::IO and generated by GeometrySystem
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
STL namespace.