Debian Codegraph @

third_party/cgltf/test/test_conversion.cpp

raw
1 #define CGLTF_IMPLEMENTATION
2 #include "../cgltf.h"
3
4 #include <algorithm>
5 #include <cmath>
6 #include <cstdio>
7 #include <limits>
8
9 static bool is_near(cgltf_float a, cgltf_float b)
10 {
11 return std::abs(a - b) < 10 * std::numeric_limits<cgltf_float>::min();
12 }
13
14 int main(int argc, char** argv)
15 {
16 if (argc < 2)
17 {
18 printf("err\n");
19 return -1;
20 }
21
22 cgltf_options options = {};
23 cgltf_data* data = NULL;
24 cgltf_result result = cgltf_parse_file(&options, argv[1], &data);
25
26 if (result == cgltf_result_success)
27 result = cgltf_load_buffers(&options, data, argv[1]);
28
29 if (result != cgltf_result_success || strstr(argv[1], "Draco"))
30 return result;
31
32 //const cgltf_accessor* blobs = data->accessors;
33 cgltf_float element_float[16];
34 cgltf_uint element_uint[4];
35 for (cgltf_size blob_index = 0; blob_index < data->accessors_count; ++blob_index)
36 {
37 const cgltf_accessor* blob = data->accessors + blob_index;
38 if (blob->is_sparse)
39 {
40 cgltf_size nfloats = cgltf_num_components(blob->type) * blob->count;
41 cgltf_float* dense = (cgltf_float*) malloc(nfloats * sizeof(cgltf_float));
42 if (cgltf_accessor_unpack_floats(blob, dense, nfloats) < nfloats) {
43 printf("Unable to completely unpack a sparse accessor.\n");
44 return -1;
45 }
46 free(dense);
47 continue;
48 }
49 if (blob->has_max && blob->has_min)
50 {
51 cgltf_float min0 = std::numeric_limits<float>::max();
52 cgltf_float max0 = std::numeric_limits<float>::lowest();
53 for (cgltf_size index = 0; index < blob->count; index++)
54 {
55 cgltf_accessor_read_float(blob, index, element_float, 16);
56 min0 = std::min(min0, element_float[0]);
57 max0 = std::max(max0, element_float[0]);
58 }
59 if (!is_near(min0, blob->min[0]) || !is_near(max0, blob->max[0]))
60 {
61 printf("Computed [%f, %f] but expected [%f, %f]\n", min0, max0, blob->min[0], blob->max[0]);
62 return -1;
63 }
64 }
65 if (blob->has_max && blob->has_min && blob->component_type != cgltf_component_type_r_32f && blob->component_type != cgltf_component_type_invalid)
66 {
67 cgltf_uint min0 = std::numeric_limits<unsigned int>::max();
68 cgltf_uint max0 = std::numeric_limits<unsigned int>::lowest();
69 for ( cgltf_size index = 0; index < blob->count; index++ )
70 {
71 cgltf_accessor_read_uint( blob, index, element_uint, 4 );
72 min0 = std::min( min0, element_uint[0] );
73 max0 = std::max( max0, element_uint[0] );
74 }
75 if ( min0 != (unsigned int) blob->min[0] || max0 != (unsigned int) blob->max[0] )
76 {
77 printf( "Computed [%u, %u] but expected [%u, %u]\n", min0, max0, (unsigned int) blob->min[0], (unsigned int) blob->max[0] );
78 return -1;
79 }
80 }
81 }
82
83 cgltf_free(data);
84
85 return result;
86 }