]> git.mxchange.org Git - simgear.git/blob - simgear/io/test_binobj.cxx
Fix VS2010 lack of fminf
[simgear.git] / simgear / io / test_binobj.cxx
1
2 #ifdef HAVE_CONFIG_H
3 #  include <simgear_config.h>
4 #endif
5
6 #include <simgear/compiler.h>
7
8 #include <iostream>
9 #include <cstdlib>
10 #include <cstdio>
11
12 #if defined _MSC_VER || defined _WIN32_WINNT
13 #   define  random  rand
14 #endif
15
16 #include <simgear/misc/sg_dir.hxx>
17
18 #include "sg_binobj.hxx"
19
20 using std::cout;
21 using std::cerr;
22 using std::endl;
23 using std::string;
24
25 #define COMPARE(a, b) \
26     if ((a) != (b))  { \
27         cerr << "failed:" << #a << " != " << #b << endl; \
28         cerr << "\tgot:" << a << endl; \
29         exit(1); \
30     }
31
32 #define VERIFY(a) \
33     if (!(a))  { \
34         cerr << "failed:" << #a << endl; \
35         exit(1); \
36     }
37     
38 void generate_points(int count, std::vector<SGVec3d>& vec)
39 {
40     for (int i=0; i<count; ++i) {
41         vec.push_back(SGVec3d(i * 0.5, i * i, i * 4));
42     }
43 }
44
45 void generate_normals(int count, std::vector<SGVec3f>& vec)
46 {
47     for (int i=0; i<count; ++i) {
48         vec.push_back(normalize(SGVec3f(i, i * 2, i * -4)));
49     }
50 }
51
52 void generate_tcs(int count, std::vector<SGVec2f>& vec)
53 {
54     for (int i=0; i<count; ++i) {
55         vec.push_back(SGVec2f(1.0 / i, 16.0 / i));
56     }
57 }
58     
59 void test_empty()
60 {
61     SGBinObject empty;
62     SGPath path(simgear::Dir::current().file("empty.btg.gz"));
63     bool ok = empty.write_bin_file(path);
64     VERIFY( ok );
65     SGBinObject rd;
66    ok = rd.read_bin(path.str()) ;
67    VERIFY( ok);
68
69    COMPARE(rd.get_wgs84_nodes().size(), 0);
70    VERIFY(rd.get_pt_materials().empty());
71 }   
72  
73 void comparePoints(const SGBinObject& rd, const std::vector<SGVec3d>& b)
74 {
75     for (unsigned int i=1; i<b.size(); i += 10) {
76         SGVec3d pos = rd.get_wgs84_nodes()[i];
77         pos += rd.get_gbs_center();
78      
79        if (!equivalent(pos, b[i], 0.1)) {
80             cout << "i=" << i << endl;
81             cout << b[i] << endl;
82             cout << pos << endl;
83         }
84         
85         VERIFY(equivalent(pos, b[i], 0.1));        
86     }
87 }
88
89 void compareTexCoords(const SGBinObject& rd, const std::vector<SGVec2f>& b)
90 {
91     for (unsigned int i=1; i<b.size(); i += 10) {
92         SGVec2f pos = rd.get_texcoords()[i];
93         VERIFY(equivalent(pos, b[i], 0.001f));        
94     }
95 }
96  
97 int_list make_tri(int maxIndex)
98 {
99     int_list r;
100     r.push_back(random() % maxIndex);
101     r.push_back(random() % maxIndex);
102     r.push_back(random() % maxIndex);
103     return r;
104 }
105
106 tci_list make_tri_tcs(int maxIndex)
107 {
108     tci_list tci;
109     tci[0].push_back(random() % maxIndex);
110     tci[0].push_back(random() % maxIndex);
111     tci[0].push_back(random() % maxIndex);
112     return tci;
113 }
114
115 void compareTris(const SGBinObject& a, const SGBinObject& b)
116 {
117     unsigned int count = a.get_tri_materials().size();
118     for (unsigned int i=0; i<count; i += 39) {
119         const int_list& vA(a.get_tris_v()[i]);
120         const int_list& vB(b.get_tris_v()[i]);
121         VERIFY(vA == vB);
122
123         COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
124         
125         const int_list& tA(a.get_tris_tcs()[i][0]);
126         const int_list& tB(b.get_tris_tcs()[i][0]);
127         VERIFY(tA == tB);
128     }
129 }
130
131 void generate_tris(SGBinObject& b, int count)
132 {
133     group_list v, n;
134     group_tci_list tc;
135     string_list materials;
136
137     int maxVertices = b.get_wgs84_nodes().size();
138     int maxNormals = b.get_normals().size();
139     int maxTCs = b.get_texcoords().size();
140     
141     SGBinObjectTriangle sgboTri;
142     for (int t=0; t<count; ++t) {
143         sgboTri.material = "material1";    
144         sgboTri.v_list = make_tri(maxVertices);
145         sgboTri.n_list = make_tri(maxNormals);
146         sgboTri.tc_list[0] = make_tri(maxTCs);
147
148         b.add_triangle( sgboTri );
149     }
150 }
151  
152 void test_basic()
153 {
154     SGBinObject basic;
155     SGPath path(simgear::Dir::current().file("basic.btg.gz"));
156     
157     SGVec3d center(1, 2, 3);
158     basic.set_gbs_center(center);
159     basic.set_gbs_radius(12345);
160     
161     std::vector<SGVec3d> points;
162     generate_points(1024, points);
163     std::vector<SGVec3f> normals;
164     generate_normals(1024, normals);
165     std::vector<SGVec2f> texCoords;
166     generate_tcs(10000, texCoords);
167     
168     basic.set_wgs84_nodes(points);
169     basic.set_normals(normals);
170     basic.set_texcoords(texCoords);
171     
172     bool ok = basic.write_bin_file(path);
173     VERIFY( ok );
174     
175     SGBinObject rd;
176    ok = rd.read_bin(path.str()) ;
177    VERIFY( ok);
178    COMPARE(rd.get_version(), 7); // should be version 7 since indices are < 2^16
179    COMPARE(rd.get_gbs_center(), center);
180    COMPARE(rd.get_gbs_radius(), 12345);
181    COMPARE(rd.get_wgs84_nodes().size(), points.size());
182    
183    comparePoints(rd, points);
184    compareTexCoords(rd, texCoords);
185 }
186         
187 void test_many_tcs()
188 {
189     SGBinObject basic;
190     SGPath path(simgear::Dir::current().file("many_tex.btg.gz"));
191     
192     SGVec3d center(1, 2, 3);
193     basic.set_gbs_center(center);
194     basic.set_gbs_radius(12345);
195     
196     std::vector<SGVec3d> points;
197     generate_points(10000, points);
198     std::vector<SGVec3f> normals;
199     generate_normals(1024, normals);
200     std::vector<SGVec2f> texCoords;
201     generate_tcs(100000, texCoords);
202     
203     basic.set_wgs84_nodes(points);
204     basic.set_normals(normals);
205     basic.set_texcoords(texCoords);
206     
207     generate_tris(basic, 20000);
208     
209     bool ok = basic.write_bin_file(path);
210     VERIFY( ok );
211     
212     SGBinObject rd;
213    ok = rd.read_bin(path.str()) ;
214    VERIFY( ok);
215    COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
216    COMPARE(rd.get_wgs84_nodes().size(), points.size());
217    COMPARE(rd.get_texcoords().size(), texCoords.size());
218    
219    comparePoints(rd, points);
220    compareTexCoords(rd, texCoords);
221    compareTris(basic, rd);
222 }
223
224 void test_big()
225 {
226     SGBinObject basic;
227     SGPath path(simgear::Dir::current().file("big.btg.gz"));
228     
229     SGVec3d center(1, 2, 3);
230     basic.set_gbs_center(center);
231     basic.set_gbs_radius(12345);
232     
233     std::vector<SGVec3d> points;
234     generate_points(200000, points);
235     std::vector<SGVec3f> normals;
236     generate_normals(1024, normals);
237     std::vector<SGVec2f> texCoords;
238     generate_tcs(300000, texCoords);
239     
240     basic.set_wgs84_nodes(points);
241     basic.set_normals(normals);
242     basic.set_texcoords(texCoords);
243     
244     generate_tris(basic, 200000);
245     
246     bool ok = basic.write_bin_file(path);
247     VERIFY( ok );
248     
249     SGBinObject rd;
250    ok = rd.read_bin(path.str()) ;
251    VERIFY( ok);
252    COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
253    COMPARE(rd.get_wgs84_nodes().size(), points.size());
254    COMPARE(rd.get_texcoords().size(), texCoords.size());
255    
256    comparePoints(rd, points);
257    compareTexCoords(rd, texCoords);
258    compareTris(basic, rd);
259 }
260
261 void test_some_objects()
262 {
263     SGBinObject basic;
264     SGPath path(simgear::Dir::current().file("some_objects.btg.gz"));
265     
266     SGVec3d center(1, 2, 3);
267     basic.set_gbs_center(center);
268     basic.set_gbs_radius(12345);
269     
270     std::vector<SGVec3d> points;
271     generate_points(10000, points);
272     std::vector<SGVec3f> normals;
273     generate_normals(1024, normals);
274     std::vector<SGVec2f> texCoords;
275     generate_tcs(20000, texCoords);
276     
277     basic.set_wgs84_nodes(points);
278     basic.set_normals(normals);
279     basic.set_texcoords(texCoords);
280     
281     generate_tris(basic, 30000); // a number smaller than 2^15!
282     
283     bool ok = basic.write_bin_file(path);
284     VERIFY( ok );
285     
286     SGBinObject rd;
287     ok = rd.read_bin(path.str()) ;
288     VERIFY( ok);
289     COMPARE(rd.get_version(), 7); // since we have less than 2^15 tris
290     COMPARE(rd.get_wgs84_nodes().size(), points.size());
291     COMPARE(rd.get_texcoords().size(), texCoords.size());
292    
293     comparePoints(rd, points);
294     compareTexCoords(rd, texCoords);
295     compareTris(basic, rd);
296 }
297
298 void test_many_objects()
299 {
300     SGBinObject basic;
301     SGPath path(simgear::Dir::current().file("many_objects.btg.gz"));
302     
303     SGVec3d center(1, 2, 3);
304     basic.set_gbs_center(center);
305     basic.set_gbs_radius(12345);
306     
307     std::vector<SGVec3d> points;
308     generate_points(10000, points);
309     std::vector<SGVec3f> normals;
310     generate_normals(1024, normals);
311     std::vector<SGVec2f> texCoords;
312     generate_tcs(20000, texCoords);
313     
314     basic.set_wgs84_nodes(points);
315     basic.set_normals(normals);
316     basic.set_texcoords(texCoords);
317     
318     generate_tris(basic, 200000);
319     
320     bool ok = basic.write_bin_file(path);
321     VERIFY( ok );
322     
323     SGBinObject rd;
324     ok = rd.read_bin(path.str()) ;
325     VERIFY( ok);
326     COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
327     COMPARE(rd.get_wgs84_nodes().size(), points.size());
328     COMPARE(rd.get_texcoords().size(), texCoords.size());
329    
330     comparePoints(rd, points);
331     compareTexCoords(rd, texCoords);
332     compareTris(basic, rd);
333 }
334
335 int main(int argc, char* argv[])
336 {
337     test_empty();
338     test_basic();
339     test_many_tcs();
340     test_big();
341     test_some_objects();
342     test_many_objects();
343     
344     return 0;
345 }