Loading [MathJax]/extensions/TeX/AMSmath.js
Radium Engine  1.5.28
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
geometryData.cpp
1#include "Core/Geometry/StandardAttribNames.hpp"
2#include <Core/Asset/GeometryData.hpp>
3#include <catch2/catch_test_macros.hpp>
4
5TEST_CASE( "Core/Asset/GeometryData", "[unittests][Core][Core/Asset][GeometryData]" ) {
6
7 using namespace Ra::Core::Asset;
8 using namespace Ra::Core::Geometry;
9
10 SECTION( "Normal test" ) {
11 auto geom = new GeometryData();
12 auto& coreGeom = geom->getGeometry();
13 auto& normal = coreGeom.normalsWithLock();
14 auto& name = Ra::Core::Geometry::getAttribName( MeshAttrib::VERTEX_NORMAL );
15
16 REQUIRE( normal.empty() );
17 REQUIRE( coreGeom.vertexAttribs().getAttribBase( name )->isLocked() );
18
19 normal.resize( 1, Ra::Core::Vector3::Zero() );
20 normal[0] = Ra::Core::Vector3().setRandom();
21 auto save = normal[0];
22 coreGeom.normalsUnlock();
23
24 REQUIRE( !normal.empty() );
25 REQUIRE( !coreGeom.vertexAttribs().getAttribBase( name )->isLocked() );
26
27 auto attriHandler = coreGeom.vertexAttribs().findAttrib<Ra::Core::Vector3>( name );
28 const auto& data = coreGeom.vertexAttribs().getData( attriHandler );
29 REQUIRE( save == data[0] );
30
31 auto geometry2 = new GeometryData();
32 auto& coreGeom2 = geometry2->getGeometry();
33 coreGeom2.setNormals( normal );
34 auto attriHandler2 = coreGeom2.vertexAttribs().findAttrib<Ra::Core::Vector3>( name );
35 const auto& d = coreGeom2.vertexAttribs().getData( attriHandler2 );
36 REQUIRE( d.size() == 1 );
37 REQUIRE( d[0] == save );
38 }
39
40 SECTION( "Vertices test" ) {
41 auto geom = new GeometryData();
42 auto& coreGeom = geom->getGeometry();
43 auto& vertex = coreGeom.verticesWithLock();
44 auto& name = Ra::Core::Geometry::getAttribName( MeshAttrib::VERTEX_POSITION );
45
46 REQUIRE( vertex.empty() );
47 REQUIRE( coreGeom.vertexAttribs().getAttribBase( name )->isLocked() );
48
49 vertex.resize( 1, Ra::Core::Vector3::Zero() );
50 vertex[0] = Ra::Core::Vector3().setRandom();
51 auto save = vertex[0];
52 coreGeom.verticesUnlock();
53
54 REQUIRE( !vertex.empty() );
55 REQUIRE( !coreGeom.vertexAttribs().getAttribBase( name )->isLocked() );
56
57 auto attriHandle = coreGeom.vertexAttribs().findAttrib<Ra::Core::Vector3>( name );
58 const auto& data = coreGeom.vertexAttribs().getData( attriHandle );
59 REQUIRE( save == data[0] );
60
61 auto geom2 = new GeometryData();
62 auto& coreGeom2 = geom2->getGeometry();
63 coreGeom2.setVertices( geom->getGeometry().vertices() );
64 auto attriHandle2 = coreGeom2.vertexAttribs().findAttrib<Ra::Core::Vector3>( name );
65 const auto& d = coreGeom2.vertexAttribs().getData( attriHandle2 );
66 REQUIRE( d.size() == 1 );
67 REQUIRE( d[0] == save );
68 }
69
70 SECTION( "Custom Attrib" ) {
71 auto geom = new GeometryData();
72 auto& coreGeom = geom->getGeometry();
73 auto& vertAttrib = coreGeom.vertexAttribs();
74
75 auto intHandle = vertAttrib.addAttrib<int>( "testInt" );
76 auto longHandle = vertAttrib.addAttrib<long>( "testLong" );
77 auto longlongHandle = vertAttrib.addAttrib<long long>( "testLongLong" );
78 auto charHandle = vertAttrib.addAttrib<char>( "testChar" );
79 auto stringHandle = vertAttrib.addAttrib<std::string>( "testString" );
80
81 {
82 size_t ssize = 20;
83
84 auto& intData = vertAttrib.getDataWithLock( intHandle );
85 auto& longData = vertAttrib.getDataWithLock( longHandle );
86 auto& longlongData = vertAttrib.getDataWithLock( longlongHandle );
87 auto& charData = vertAttrib.getDataWithLock( charHandle );
88 auto& stringData = vertAttrib.getDataWithLock( stringHandle );
89 intData.resize( ssize );
90 longData.resize( ssize );
91 longlongData.resize( ssize );
92 charData.resize( ssize );
93 stringData.resize( ssize );
94 intData[0] = 0;
95 longData[1] = 1l;
96 longlongData[2] = 2ll;
97 charData[3] = 'f';
98 stringData[4] = "foo";
99 vertAttrib.unlock( intHandle );
100 vertAttrib.unlock( longHandle );
101 vertAttrib.unlock( longlongHandle );
102 vertAttrib.unlock( charHandle );
103 vertAttrib.unlock( stringHandle );
104 REQUIRE( vertAttrib.getAttrib<int>( "testInt" ).getSize() == ssize );
105 REQUIRE( vertAttrib.getAttrib<long>( "testLong" ).getSize() == ssize );
106 REQUIRE( vertAttrib.getAttrib<long long>( "testLongLong" ).getSize() == ssize );
107 REQUIRE( vertAttrib.getAttrib<char>( "testChar" ).getSize() == ssize );
108 REQUIRE( vertAttrib.getAttrib<std::string>( "testString" ).getSize() == ssize );
109
110 REQUIRE( vertAttrib.getAttrib<int>( "testInt" ).data()[0] == 0 );
111 REQUIRE( vertAttrib.getAttrib<long>( "testLong" ).data()[1] == 1l );
112 REQUIRE( vertAttrib.getAttrib<long long>( "testLongLong" ).data()[2] == 2ll );
113 REQUIRE( vertAttrib.getAttrib<char>( "testChar" ).data()[3] == 'f' );
114 REQUIRE( vertAttrib.getAttrib<std::string>( "testString" ).data()[4] == "foo" );
115 }
116 }
117
118 SECTION( "Tangent, BiTangent, TexCoord tests" ) {
119 auto geom = new GeometryData();
120 auto& coreGeom = geom->getGeometry();
121 auto& name = getAttribName( Ra::Core::Geometry::MeshAttrib::VERTEX_TANGENT );
122 auto attribHandle = coreGeom.addAttrib<Ra::Core::Vector3>( name );
123 auto& attribData = coreGeom.vertexAttribs().getDataWithLock( attribHandle );
124
125 REQUIRE( attribData.empty() );
126 REQUIRE( coreGeom.vertexAttribs().getAttribBase( name )->isLocked() );
127
128 attribData.resize( 1, Ra::Core::Vector3::Zero() );
129 attribData[0] = Ra::Core::Vector3().setRandom();
130 auto saveTan = attribData[0];
131 coreGeom.vertexAttribs().getAttribBase( name )->unlock();
132
133 REQUIRE( !attribData.empty() );
134 REQUIRE( !coreGeom.vertexAttribs().getAttribBase( name )->isLocked() );
135
136 auto tangentAttribHandle2 = coreGeom.vertexAttribs().findAttrib<Ra::Core::Vector3>( name );
137 const auto& dataTan = coreGeom.vertexAttribs().getData( tangentAttribHandle2 );
138 REQUIRE( saveTan == dataTan[0] );
139
140 auto geom2 = new GeometryData();
141 auto& coreGeom2 = geom2->getGeometry();
142 auto attribHandle2 = coreGeom2.addAttrib<Ra::Core::Vector3>( name );
143 coreGeom2.getAttrib<Ra::Core::Vector3>( attribHandle2 ).setData( dataTan );
144
145 auto attribHandle3 = coreGeom2.vertexAttribs().findAttrib<Ra::Core::Vector3>( name );
146 const auto& d = coreGeom2.vertexAttribs().getData( attribHandle3 );
147
148 REQUIRE( d.size() == 1 );
149 REQUIRE( d[0] == saveTan );
150 }
151
152 SECTION( "Type test " ) {
153 auto geometry = GeometryData();
154
155 geometry.setType( GeometryData::GeometryType::LINE_MESH );
156 REQUIRE( geometry.isLineMesh() );
157
158 geometry.setType( GeometryData::GeometryType::TRI_MESH );
159 REQUIRE( geometry.isTriMesh() );
160
161 geometry.setType( GeometryData::GeometryType::QUAD_MESH );
162 REQUIRE( geometry.isQuadMesh() );
163
164 geometry.setType( GeometryData::GeometryType::POLY_MESH );
165 REQUIRE( geometry.isPolyMesh() );
166
167 geometry.setType( GeometryData::GeometryType::HEX_MESH );
168 REQUIRE( geometry.isHexMesh() );
169
170 geometry.setType( GeometryData::GeometryType::TETRA_MESH );
171 REQUIRE( geometry.isTetraMesh() );
172
173 geometry.setType( GeometryData::GeometryType::POINT_CLOUD );
174 REQUIRE( geometry.isPointCloud() );
175
176 geometry.setType( GeometryData::GeometryType::UNKNOWN );
177 REQUIRE( !geometry.isLineMesh() );
178 REQUIRE( !geometry.isTriMesh() );
179 REQUIRE( !geometry.isQuadMesh() );
180 REQUIRE( !geometry.isPolyMesh() );
181 REQUIRE( !geometry.isHexMesh() );
182 REQUIRE( !geometry.isTetraMesh() );
183 REQUIRE( !geometry.isPointCloud() );
184 }
185}
T data(T... args)