Loading [MathJax]/extensions/TeX/AMSsymbols.js
Radium Engine  1.5.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
AssimpGeometryDataLoader.hpp
1 #pragma once
2 
3 #include <Core/Asset/DataLoader.hpp>
4 #include <Core/Asset/GeometryData.hpp>
5 #include <Core/Geometry/StandardAttribNames.hpp>
6 #include <Core/Types.hpp>
7 #include <IO/AssimpLoader/AssimpWrapper.hpp>
8 #include <IO/RaIO.hpp>
9 
10 #include <assimp/mesh.h>
11 
12 #include <memory>
13 #include <set>
14 
15 struct aiScene;
16 struct aiMesh;
17 struct aiFace;
18 struct aiNode;
19 struct aiMaterial;
20 
21 namespace Ra {
22 namespace Core {
23 namespace Asset {
24 class GeometryData;
25 }
26 } // namespace Core
27 
28 namespace IO {
29 
32 class RA_IO_API AssimpGeometryDataLoader : public Core::Asset::DataLoader<Core::Asset::GeometryData>
33 {
34  public:
35  explicit AssimpGeometryDataLoader( const std::string& filepath,
36  const bool VERBOSE_MODE = false );
37 
38  ~AssimpGeometryDataLoader() override;
39 
41  void loadData( const aiScene* scene,
42  std::vector<std::unique_ptr<Core::Asset::GeometryData>>& data ) override;
43 
44  protected:
46  inline bool sceneHasGeometry( const aiScene* scene ) const;
47 
49  uint sceneGeometrySize( const aiScene* scene ) const;
50 
52  void loadGeometryData( const aiScene* scene,
53  std::vector<std::unique_ptr<Core::Asset::GeometryData>>& data );
54 
56  void loadMaterial( const aiMaterial& material, Core::Asset::GeometryData& data ) const;
57 
59  void loadMeshFrame( const aiNode* node,
60  const Core::Transform& parentFrame,
61  const std::map<uint, size_t>& indexTable,
62  std::vector<std::unique_ptr<Core::Asset::GeometryData>>& data ) const;
63 
64  private:
66  void loadMeshAttrib( const aiMesh& mesh,
68  std::set<std::string>& usedNames );
69 
72  void fetchName( const aiMesh& mesh,
74  std::set<std::string>& usedNames ) const;
75 
77  void fetchType( const aiMesh& mesh, Core::Asset::GeometryData& data ) const;
78 
80  void fetchPolyhedron( const aiMesh& mesh, Core::Asset::GeometryData& data ) const;
81 
83  template <typename T>
84  void fetchAttribute( T* aiData,
85  int size,
87  Core::Geometry::MeshAttrib a ) const;
88 
90  template <typename T>
91  void fetchIndexLayer( aiFace* faces,
92  int numFaces,
94 
96  std::string m_filepath;
97 };
98 
99 template <typename T>
100 void AssimpGeometryDataLoader::fetchAttribute( T* aiData,
101  int size,
103  Core::Geometry::MeshAttrib a ) const {
104  auto attribHandle = data.addAttrib<typename AssimpTypeWrapper<T>::Type>( getAttribName( a ) );
105  auto& attribData = data.vertexAttribs().getDataWithLock( attribHandle );
106  attribData.resize( size );
107 #pragma omp parallel for
108  for ( int i = 0; i < size; ++i ) {
109  attribData.at( i ) = assimpToCore( aiData[i] );
110  }
111  data.vertexAttribs().unlock( attribHandle );
112 }
113 
114 template <typename T>
115 void AssimpGeometryDataLoader::fetchIndexLayer( aiFace* faces,
116  int numFaces,
117  Core::Geometry::MultiIndexedGeometry& data ) const {
118  auto layer = std::make_unique<T>();
119  auto& indices = layer->collection();
120  indices.resize( numFaces );
121 #pragma omp parallel for
122  for ( int i = 0; i < numFaces; ++i ) {
123  indices[i] = assimpToCore<typename T::IndexType>( faces[i].mIndices, faces[i].mNumIndices );
124  }
125  data.addLayer( std::move( layer ), false, "indices" );
126 }
127 
128 } // namespace IO
129 } // namespace Ra
Utils::AttribHandle< T > addAttrib(const std::string &name)
Utils::AttribManager & vertexAttribs()
AbstractGeometry with per-vertex attributes and layers of indices. Each layer represents a different ...
Attrib< T >::Container & getDataWithLock(const AttribHandle< T > &h)
Get the locked data container from the attrib handle.
Definition: Attribs.hpp:634
void unlock(const AttribHandle< T > &h)
Unlock the handle data.
Definition: Attribs.hpp:644
Definition: Cage.cpp:3