e325f1753ec4c343bb0e3df4566bcd1110afed49
[platform/core/uifw/dali-core.git] / automated-tests / TET / dali-test-suite / geometry / utc-Dali-MeshData.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23
24 #include <dali-test-suite-utils.h>
25
26 using namespace Dali;
27
28 #include <mesh-builder.h>
29
30 static void Startup();
31 static void Cleanup();
32
33 extern "C" {
34   void (*tet_startup)() = Startup;
35   void (*tet_cleanup)() = Cleanup;
36 }
37
38 enum {
39   POSITIVE_TC_IDX = 0x01,
40   NEGATIVE_TC_IDX,
41 };
42
43 #define MAX_NUMBER_OF_TESTS 10000
44 extern "C" {
45   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
46 }
47
48 // Add test functionality for all APIs in the class (Positive and Negative)
49 TEST_FUNCTION( UtcDaliMeshDataNew, POSITIVE_TC_IDX );
50 TEST_FUNCTION( UtcDaliMeshDataSetData, POSITIVE_TC_IDX );
51 TEST_FUNCTION( UtcDaliMeshDataAddToBoundingVolume, POSITIVE_TC_IDX );
52 TEST_FUNCTION( UtcDaliMeshDataBoundingBox, POSITIVE_TC_IDX );
53 TEST_FUNCTION( UtcDaliMeshDataGetVertexCount, POSITIVE_TC_IDX );
54 TEST_FUNCTION( UtcDaliMeshDataGetVertices, POSITIVE_TC_IDX );
55 TEST_FUNCTION( UtcDaliMeshDataGetFaceCount, POSITIVE_TC_IDX );
56 TEST_FUNCTION( UtcDaliMeshDataGetFaces, POSITIVE_TC_IDX );
57 TEST_FUNCTION( UtcDaliMeshDataTextureCoords, POSITIVE_TC_IDX );
58 TEST_FUNCTION( UtcDaliMeshDataNormals, POSITIVE_TC_IDX );
59 TEST_FUNCTION( UtcDaliMeshDataGetMaterial, POSITIVE_TC_IDX );
60 TEST_FUNCTION( UtcDaliMeshDataSetMaterial, POSITIVE_TC_IDX );
61 TEST_FUNCTION( UtcDaliMeshDataGetBoneCount, POSITIVE_TC_IDX );
62 TEST_FUNCTION( UtcDaliMeshDataGetBones, POSITIVE_TC_IDX );
63
64 // Called only once before first test is run.
65 static void Startup()
66 {
67 }
68
69 // Called only once after last test is run
70 static void Cleanup()
71 {
72 }
73
74
75
76 static void UtcDaliMeshDataNew()
77 {
78   TestApplication application;
79   MeshData meshData;
80
81   DALI_TEST_EQUALS(meshData.HasNormals(), false, TEST_LOCATION);
82   DALI_TEST_EQUALS(meshData.HasTextureCoords(), false, TEST_LOCATION);
83 }
84
85
86 static void UtcDaliMeshDataSetData()
87 {
88   TestApplication application;
89
90   MeshData meshData;
91   MeshData::VertexContainer    vertices;
92   MeshData::FaceIndices        faces;
93   BoneContainer                bones;
94   ConstructVertices(vertices, 60);
95   ConstructFaces(vertices, faces);
96   Material customMaterial = ConstructMaterial();
97   meshData.SetData(vertices, faces, bones, customMaterial);
98
99   DALI_TEST_GREATER(meshData.GetVertexCount(), 0u, TEST_LOCATION);
100   DALI_TEST_GREATER(meshData.GetFaceCount(), 0u, TEST_LOCATION);
101
102   const MeshData::FaceIndices& faces2 = meshData.GetFaces();
103   const MeshData::VertexContainer& verts2 = meshData.GetVertices();
104   DALI_TEST_EQUALS(faces.at(0), faces2.at(0), TEST_LOCATION);
105   DALI_TEST_EQUALS(vertices.at(1).y, verts2.at(1).y, TEST_LOCATION);
106   DALI_TEST_EQUALS(meshData.GetBoneCount(), static_cast<size_t>(0), TEST_LOCATION);
107 }
108
109 static void UtcDaliMeshDataAddToBoundingVolume()
110 {
111   TestApplication application;
112
113   float sz=40.0f;
114
115   MeshData meshData;
116   MeshData::VertexContainer    vertices;
117   MeshData::FaceIndices        faces;
118   BoneContainer                bones;
119   ConstructVertices(vertices, sz);
120   ConstructFaces(vertices, faces);
121   Material customMaterial = ConstructMaterial();
122   meshData.SetData(vertices, faces, bones, customMaterial);
123
124   Vector4 upper(-1e10f, -1e10f, -1e10f, 0.0f);
125   Vector4 lower(1e10f, 1e10f, 1e10f, 0.0f);
126   Matrix f(false);
127   f.SetIdentityAndScale(Vector3(2.0f, 2.0f, 2.0f));
128   meshData.AddToBoundingVolume(lower, upper, f);
129
130   Vector4 min(-sz*0.5f, -sz,      -sz*0.7f, 0.0f);
131   Vector4 max( sz*0.5f,  sz*0.3f,  sz*0.5f, 0.0f);
132
133   // Test that upper and lower bounds are set and transformed
134   DALI_TEST_EQUALS(lower, min*2.0f, 0.001, TEST_LOCATION);
135   DALI_TEST_EQUALS(upper, max*2.0f, 0.001, TEST_LOCATION);
136
137   // Test that mesh's upper and lower bounds are set and not transformed
138   DALI_TEST_EQUALS(meshData.GetBoundingBoxMin(), min, 0.001, TEST_LOCATION);
139   DALI_TEST_EQUALS(meshData.GetBoundingBoxMax(), max, 0.001, TEST_LOCATION);
140 }
141
142 static void UtcDaliMeshDataBoundingBox()
143 {
144   TestApplication application;
145
146   float sz=40.0f;
147   MeshData meshData;
148   Vector4 min(-1.0f, -2.0f, -3.0f, 0.0f);
149   Vector4 max(1.0f, 2.0f, 3.0f, 0.0f);
150   meshData.SetBoundingBoxMin(min);
151   meshData.SetBoundingBoxMax(max);
152   DALI_TEST_EQUALS(meshData.GetBoundingBoxMin(), min, 0.001, TEST_LOCATION);
153   DALI_TEST_EQUALS(meshData.GetBoundingBoxMax(), max, 0.001, TEST_LOCATION);
154
155   MeshData::VertexContainer    vertices;
156   MeshData::FaceIndices        faces;
157   BoneContainer                bones;
158   ConstructVertices(vertices, sz);
159   ConstructFaces(vertices, faces);
160   Material customMaterial = ConstructMaterial();
161   meshData.SetData(vertices, faces, bones, customMaterial);
162
163   // Check bounding box hasn't changed
164   DALI_TEST_EQUALS(meshData.GetBoundingBoxMin(), min, 0.001, TEST_LOCATION);
165   DALI_TEST_EQUALS(meshData.GetBoundingBoxMax(), max, 0.001, TEST_LOCATION);
166
167   Vector4 upper(-1e10f, -1e10f, -1e10f, 0.0f);
168   Vector4 lower(1e10f, 1e10f, 1e10f, 0.0f);
169   meshData.AddToBoundingVolume(lower, upper, Matrix::IDENTITY);
170
171   // Bounding box should have been update
172   Vector4 bbMin(-sz*0.5f, -sz,      -sz*0.7f, 0.0f);
173   Vector4 bbMax( sz*0.5f,  sz*0.3f,  sz*0.5f, 0.0f);
174
175   // Test that upper and lower bounds are set and transformed
176   DALI_TEST_EQUALS(lower, bbMin, 0.001, TEST_LOCATION);
177   DALI_TEST_EQUALS(upper, bbMax, 0.001, TEST_LOCATION);
178
179   // Test that mesh's upper and lower bounds are set and not transformed
180   DALI_TEST_EQUALS(meshData.GetBoundingBoxMin(), bbMin, 0.001, TEST_LOCATION);
181   DALI_TEST_EQUALS(meshData.GetBoundingBoxMax(), bbMax, 0.001, TEST_LOCATION);
182 }
183
184 static void UtcDaliMeshDataGetVertexCount ()
185 {
186   TestApplication application;
187
188   MeshData meshData;
189   MeshData::VertexContainer    vertices;
190   MeshData::FaceIndices        faces;
191   BoneContainer                bones;
192   ConstructVertices(vertices, 30);
193   ConstructFaces(vertices, faces);
194   Material customMaterial = ConstructMaterial();
195
196   DALI_TEST_EQUALS(meshData.GetVertexCount(), static_cast<size_t>(0), TEST_LOCATION);
197
198   meshData.SetData(vertices, faces, bones, customMaterial);
199   DALI_TEST_EQUALS(meshData.GetVertexCount(), vertices.size(), TEST_LOCATION);
200
201 }
202
203 static void UtcDaliMeshDataGetVertices ()
204 {
205   TestApplication application;
206   MeshData meshData;
207   const Dali::MeshData::VertexContainer& verts1 = meshData.GetVertices();
208   DALI_TEST_CHECK(verts1.size() == 0);
209
210   MeshData::VertexContainer    vertices;
211   MeshData::FaceIndices        faces;
212   BoneContainer                bones;
213   ConstructVertices(vertices, 30);
214   ConstructFaces(vertices, faces);
215   Material customMaterial = ConstructMaterial();
216   meshData.SetData(vertices, faces, bones, customMaterial);
217
218   const Dali::MeshData::VertexContainer& verts2 = meshData.GetVertices();
219   DALI_TEST_CHECK(verts2.size() != 0);
220   DALI_TEST_CHECK(verts2.size() == meshData.GetVertexCount());
221 }
222
223 static void UtcDaliMeshDataGetFaceCount ()
224 {
225   TestApplication application;
226   MeshData meshData;
227   DALI_TEST_EQUALS(meshData.GetFaceCount(), static_cast<size_t>(0), TEST_LOCATION);
228
229   MeshData::VertexContainer    vertices;
230   MeshData::FaceIndices        faces;
231   BoneContainer                bones;
232   ConstructVertices(vertices, 30);
233   ConstructFaces(vertices, faces);
234   Material customMaterial = ConstructMaterial();
235   meshData.SetData(vertices, faces, bones, customMaterial);
236
237   DALI_TEST_EQUALS(meshData.GetFaceCount(), faces.size() / 3, TEST_LOCATION);
238 }
239
240 static void UtcDaliMeshDataGetFaces ()
241 {
242   TestApplication application;
243   MeshData meshData;
244   const Dali::MeshData::FaceIndices& faces1 = meshData.GetFaces();
245   DALI_TEST_CHECK(faces1.size() == 0);
246
247   MeshData::VertexContainer    vertices;
248   MeshData::FaceIndices        faces;
249   BoneContainer                bones;
250   ConstructVertices(vertices, 30);
251   ConstructFaces(vertices, faces);
252   Material customMaterial = ConstructMaterial();
253   meshData.SetData(vertices, faces, bones, customMaterial);
254
255   const Dali::MeshData::FaceIndices& faces2 = meshData.GetFaces();
256   DALI_TEST_CHECK(faces2.size() != 0);
257 }
258
259 static void UtcDaliMeshDataTextureCoords ()
260 {
261   TestApplication application;
262   MeshData meshData;
263   DALI_TEST_EQUALS(meshData.HasTextureCoords(), false, TEST_LOCATION);
264   meshData.SetHasTextureCoords(true);
265   DALI_TEST_EQUALS(meshData.HasTextureCoords(), true, TEST_LOCATION);
266 }
267
268 static void UtcDaliMeshDataNormals ()
269 {
270   TestApplication application;
271   MeshData meshData;
272   DALI_TEST_EQUALS(meshData.HasNormals(), false, TEST_LOCATION);
273   meshData.SetHasNormals(true);
274   DALI_TEST_EQUALS(meshData.HasNormals(), true, TEST_LOCATION);
275 }
276
277 static void UtcDaliMeshDataGetMaterial ()
278 {
279   TestApplication application;
280   MeshData meshData;
281   Material aMat = meshData.GetMaterial();
282   DALI_TEST_CHECK(!aMat);
283
284   MeshData::VertexContainer    vertices;
285   MeshData::FaceIndices        faces;
286   BoneContainer                bones;
287   ConstructVertices(vertices, 30);
288   ConstructFaces(vertices, faces);
289   Material customMaterial = ConstructMaterial();
290   meshData.SetData(vertices, faces, bones, customMaterial);
291
292   aMat = meshData.GetMaterial();
293   DALI_TEST_CHECK(aMat);
294 }
295
296 static void UtcDaliMeshDataSetMaterial ()
297 {
298   TestApplication application;
299   MeshData meshData;
300
301   Material aMat = meshData.GetMaterial();
302   DALI_TEST_CHECK(!aMat);
303
304   Material mat1 = ConstructMaterial();
305   meshData.SetMaterial(mat1);
306   aMat = meshData.GetMaterial();
307   DALI_TEST_CHECK(mat1 == aMat);
308
309   MeshData::VertexContainer    vertices;
310   MeshData::FaceIndices        faces;
311   BoneContainer                bones;
312   ConstructVertices(vertices, 30);
313   ConstructFaces(vertices, faces);
314   Material customMaterial = ConstructMaterial();
315   meshData.SetData(vertices, faces, bones, customMaterial);
316
317   aMat = meshData.GetMaterial();
318
319   DALI_TEST_CHECK(aMat == customMaterial);
320   DALI_TEST_CHECK(aMat != mat1);
321
322 }
323
324 static void UtcDaliMeshDataGetBoneCount ()
325 {
326   TestApplication application;
327   MeshData meshData;
328   DALI_TEST_EQUALS(meshData.GetBoneCount(), static_cast<size_t>(0), TEST_LOCATION);
329   DALI_TEST_EQUALS(meshData.HasBones(), false, TEST_LOCATION);
330
331   MeshData::VertexContainer    vertices;
332   MeshData::FaceIndices        faces;
333   BoneContainer                bones;
334   ConstructVertices(vertices, 30);
335   ConstructFaces(vertices, faces);
336   ConstructBones(bones);
337   Material customMaterial = ConstructMaterial();
338   meshData.SetData(vertices, faces, bones, customMaterial);
339
340   DALI_TEST_EQUALS(meshData.GetBoneCount(), static_cast<size_t>(3), TEST_LOCATION);
341   DALI_TEST_EQUALS(meshData.HasBones(), true, TEST_LOCATION);
342 }
343
344
345 static void UtcDaliMeshDataGetBones ()
346 {
347   TestApplication application;
348   MeshData meshData;
349   DALI_TEST_EQUALS(meshData.GetBoneCount(), static_cast<size_t>(0), TEST_LOCATION);
350   const BoneContainer& bones1 = meshData.GetBones();
351   DALI_TEST_CHECK(bones1.empty());
352
353   MeshData::VertexContainer    vertices;
354   MeshData::FaceIndices        faces;
355   BoneContainer                bones;
356   ConstructVertices(vertices, 30);
357   ConstructFaces(vertices, faces);
358   ConstructBones(bones);
359   Material customMaterial = ConstructMaterial();
360   meshData.SetData(vertices, faces, bones, customMaterial);
361   const BoneContainer& bones3 = meshData.GetBones();
362   DALI_TEST_CHECK( ! bones3.empty() );
363 }