]> git.mxchange.org Git - simgear.git/blob - simgear/io/test_binobj.cxx
Drop explicit SDK setting on Mac
[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     int a, b, c;
101         
102     bool valid = false;
103     int retry  = 10;
104     
105     while(!valid && retry--) {
106         a = (random() % maxIndex);
107         b = (random() % maxIndex);
108         c = (random() % maxIndex);
109     
110         valid = ( (a!=b) && (b!=c) && (c!=a) );
111     }
112     
113     if (!valid) {
114         cerr << "can't generate valid triangle" << endl;
115         exit(1);
116     }
117     
118     r.push_back(a);
119     r.push_back(b);
120     r.push_back(c);
121     
122     return r;
123 }
124
125 tci_list make_tri_tcs(int maxIndex)
126 {
127     tci_list tci;
128     tci[0].push_back(random() % maxIndex);
129     tci[0].push_back(random() % maxIndex);
130     tci[0].push_back(random() % maxIndex);
131     return tci;
132 }
133
134 void compareTris(const SGBinObject& a, const SGBinObject& b)
135 {
136     unsigned int count = a.get_tri_materials().size();
137     for (unsigned int i=0; i<count; i += 39) {
138         const int_list& vA(a.get_tris_v()[i]);
139         const int_list& vB(b.get_tris_v()[i]);
140         VERIFY(vA == vB);
141
142         COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
143         
144         const int_list& tA(a.get_tris_tcs()[i][0]);
145         const int_list& tB(b.get_tris_tcs()[i][0]);
146         VERIFY(tA == tB);
147     }
148 }
149
150 void generate_tris(SGBinObject& b, int count)
151 {
152     group_list v, n;
153     group_tci_list tc;
154     string_list materials;
155
156     int maxVertices = b.get_wgs84_nodes().size();
157     int maxNormals = b.get_normals().size();
158     int maxTCs = b.get_texcoords().size();
159     
160     SGBinObjectTriangle sgboTri;
161     for (int t=0; t<count; ++t) {
162         sgboTri.material = "material1";    
163         sgboTri.v_list = make_tri(maxVertices);
164         sgboTri.n_list = make_tri(maxNormals);
165         sgboTri.tc_list[0] = make_tri(maxTCs);
166
167         b.add_triangle( sgboTri );
168     }
169 }
170  
171 void test_basic()
172 {
173     SGBinObject basic;
174     SGPath path(simgear::Dir::current().file("basic.btg.gz"));
175     
176     SGVec3d center(1, 2, 3);
177     basic.set_gbs_center(center);
178     basic.set_gbs_radius(12345);
179     
180     std::vector<SGVec3d> points;
181     generate_points(1024, points);
182     std::vector<SGVec3f> normals;
183     generate_normals(1024, normals);
184     std::vector<SGVec2f> texCoords;
185     generate_tcs(10000, texCoords);
186     
187     basic.set_wgs84_nodes(points);
188     basic.set_normals(normals);
189     basic.set_texcoords(texCoords);
190     
191     bool ok = basic.write_bin_file(path);
192     VERIFY( ok );
193     
194     SGBinObject rd;
195    ok = rd.read_bin(path.str()) ;
196    VERIFY( ok);
197    COMPARE(rd.get_version(), 7); // should be version 7 since indices are < 2^16
198    COMPARE(rd.get_gbs_center(), center);
199    COMPARE(rd.get_gbs_radius(), 12345);
200    COMPARE(rd.get_wgs84_nodes().size(), points.size());
201    
202    comparePoints(rd, points);
203    compareTexCoords(rd, texCoords);
204 }
205         
206 void test_many_tcs()
207 {
208     SGBinObject basic;
209     SGPath path(simgear::Dir::current().file("many_tex.btg.gz"));
210     
211     SGVec3d center(1, 2, 3);
212     basic.set_gbs_center(center);
213     basic.set_gbs_radius(12345);
214     
215     std::vector<SGVec3d> points;
216     generate_points(10000, points);
217     std::vector<SGVec3f> normals;
218     generate_normals(1024, normals);
219     std::vector<SGVec2f> texCoords;
220     generate_tcs(100000, texCoords);
221     
222     basic.set_wgs84_nodes(points);
223     basic.set_normals(normals);
224     basic.set_texcoords(texCoords);
225     
226     generate_tris(basic, 20000);
227     
228     bool ok = basic.write_bin_file(path);
229     VERIFY( ok );
230     
231     SGBinObject rd;
232    ok = rd.read_bin(path.str()) ;
233    VERIFY( ok);
234    COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
235    COMPARE(rd.get_wgs84_nodes().size(), points.size());
236    COMPARE(rd.get_texcoords().size(), texCoords.size());
237    
238    comparePoints(rd, points);
239    compareTexCoords(rd, texCoords);
240    compareTris(basic, rd);
241 }
242
243 void test_big()
244 {
245     SGBinObject basic;
246     SGPath path(simgear::Dir::current().file("big.btg.gz"));
247     
248     SGVec3d center(1, 2, 3);
249     basic.set_gbs_center(center);
250     basic.set_gbs_radius(12345);
251     
252     std::vector<SGVec3d> points;
253     generate_points(200000, points);
254     std::vector<SGVec3f> normals;
255     generate_normals(1024, normals);
256     std::vector<SGVec2f> texCoords;
257     generate_tcs(300000, texCoords);
258     
259     basic.set_wgs84_nodes(points);
260     basic.set_normals(normals);
261     basic.set_texcoords(texCoords);
262     
263     generate_tris(basic, 200000);
264     
265     bool ok = basic.write_bin_file(path);
266     VERIFY( ok );
267     
268     SGBinObject rd;
269    ok = rd.read_bin(path.str()) ;
270    VERIFY( ok);
271    COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
272    COMPARE(rd.get_wgs84_nodes().size(), points.size());
273    COMPARE(rd.get_texcoords().size(), texCoords.size());
274    
275    comparePoints(rd, points);
276    compareTexCoords(rd, texCoords);
277    compareTris(basic, rd);
278 }
279
280 void test_some_objects()
281 {
282     SGBinObject basic;
283     SGPath path(simgear::Dir::current().file("some_objects.btg.gz"));
284     
285     SGVec3d center(1, 2, 3);
286     basic.set_gbs_center(center);
287     basic.set_gbs_radius(12345);
288     
289     std::vector<SGVec3d> points;
290     generate_points(10000, points);
291     std::vector<SGVec3f> normals;
292     generate_normals(1024, normals);
293     std::vector<SGVec2f> texCoords;
294     generate_tcs(20000, texCoords);
295     
296     basic.set_wgs84_nodes(points);
297     basic.set_normals(normals);
298     basic.set_texcoords(texCoords);
299     
300     generate_tris(basic, 30000); // a number smaller than 2^15!
301     
302     bool ok = basic.write_bin_file(path);
303     VERIFY( ok );
304     
305     SGBinObject rd;
306     ok = rd.read_bin(path.str()) ;
307     VERIFY( ok);
308     COMPARE(rd.get_version(), 7); // since we have less than 2^15 tris
309     COMPARE(rd.get_wgs84_nodes().size(), points.size());
310     COMPARE(rd.get_texcoords().size(), texCoords.size());
311    
312     comparePoints(rd, points);
313     compareTexCoords(rd, texCoords);
314     compareTris(basic, rd);
315 }
316
317 void test_many_objects()
318 {
319     SGBinObject basic;
320     SGPath path(simgear::Dir::current().file("many_objects.btg.gz"));
321     
322     SGVec3d center(1, 2, 3);
323     basic.set_gbs_center(center);
324     basic.set_gbs_radius(12345);
325     
326     std::vector<SGVec3d> points;
327     generate_points(10000, points);
328     std::vector<SGVec3f> normals;
329     generate_normals(1024, normals);
330     std::vector<SGVec2f> texCoords;
331     generate_tcs(20000, texCoords);
332     
333     basic.set_wgs84_nodes(points);
334     basic.set_normals(normals);
335     basic.set_texcoords(texCoords);
336     
337     generate_tris(basic, 200000);
338     
339     bool ok = basic.write_bin_file(path);
340     VERIFY( ok );
341     
342     SGBinObject rd;
343     ok = rd.read_bin(path.str()) ;
344     VERIFY( ok);
345     COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
346     COMPARE(rd.get_wgs84_nodes().size(), points.size());
347     COMPARE(rd.get_texcoords().size(), texCoords.size());
348    
349     comparePoints(rd, points);
350     compareTexCoords(rd, texCoords);
351     compareTris(basic, rd);
352 }
353
354 int main(int argc, char* argv[])
355 {
356     test_empty();
357     test_basic();
358     test_many_tcs();
359     test_big();
360     test_some_objects();
361     test_many_objects();
362     
363     return 0;
364 }