]> git.mxchange.org Git - simgear.git/blob - simgear/io/test_binobj.cxx
Remove automake ignores.
[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 #ifdef _MSC_VER
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 void compareTris(const SGBinObject& a, const SGBinObject& b)
107 {
108     unsigned int count = a.get_tri_materials().size();
109     for (unsigned int i=0; i<count; i += 39) {
110         const int_list& vA(a.get_tris_v()[i]);
111         const int_list& vB(b.get_tris_v()[i]);
112         VERIFY(vA == vB);
113
114         COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
115         
116         const int_list& tA(a.get_tris_tc()[i]);
117         const int_list& tB(b.get_tris_tc()[i]);
118         VERIFY(tA == tB);
119     }
120 }
121
122 void generate_tris(SGBinObject& b, int count)
123 {
124     group_list v, n, tc;
125     string_list materials;
126
127     int maxVertices = b.get_wgs84_nodes().size();
128     int maxNormals = b.get_normals().size();
129     int maxTCs = b.get_texcoords().size();
130     
131     for (int t=0; t<count; ++t) {
132         v.push_back(make_tri(maxVertices));
133         n.push_back(make_tri(maxNormals));
134         tc.push_back(make_tri(maxTCs));
135         materials.push_back("material1");
136     }
137     
138     b.set_tris_v(v);
139     b.set_tris_n(n);
140     b.set_tris_tc(tc);
141     b.set_tri_materials(materials);
142 }
143  
144 void test_basic()
145 {
146     SGBinObject basic;
147     SGPath path(simgear::Dir::current().file("basic.btg.gz"));
148     
149     SGVec3d center(1, 2, 3);
150     basic.set_gbs_center(center);
151     basic.set_gbs_radius(12345);
152     
153     std::vector<SGVec3d> points;
154     generate_points(1024, points);
155     std::vector<SGVec3f> normals;
156     generate_normals(1024, normals);
157     std::vector<SGVec2f> texCoords;
158     generate_tcs(10000, texCoords);
159     
160     basic.set_wgs84_nodes(points);
161     basic.set_normals(normals);
162     basic.set_texcoords(texCoords);
163     
164     bool ok = basic.write_bin_file(path);
165     VERIFY( ok );
166     
167     SGBinObject rd;
168    ok = rd.read_bin(path.str()) ;
169    VERIFY( ok);
170    COMPARE(rd.get_version(), 7); // should be version 7 since indices are < 2^16
171    COMPARE(rd.get_gbs_center(), center);
172    COMPARE(rd.get_gbs_radius(), 12345);
173    COMPARE(rd.get_wgs84_nodes().size(), points.size());
174    
175    comparePoints(rd, points);
176    compareTexCoords(rd, texCoords);
177 }
178         
179 void test_many_tcs()
180 {
181     SGBinObject basic;
182     SGPath path(simgear::Dir::current().file("many_tex.btg.gz"));
183     
184     SGVec3d center(1, 2, 3);
185     basic.set_gbs_center(center);
186     basic.set_gbs_radius(12345);
187     
188     std::vector<SGVec3d> points;
189     generate_points(10000, points);
190     std::vector<SGVec3f> normals;
191     generate_normals(1024, normals);
192     std::vector<SGVec2f> texCoords;
193     generate_tcs(100000, texCoords);
194     
195     basic.set_wgs84_nodes(points);
196     basic.set_normals(normals);
197     basic.set_texcoords(texCoords);
198     
199     generate_tris(basic, 20000);
200     
201     bool ok = basic.write_bin_file(path);
202     VERIFY( ok );
203     
204     SGBinObject rd;
205    ok = rd.read_bin(path.str()) ;
206    VERIFY( ok);
207    COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
208    COMPARE(rd.get_wgs84_nodes().size(), points.size());
209    COMPARE(rd.get_texcoords().size(), texCoords.size());
210    
211    comparePoints(rd, points);
212    compareTexCoords(rd, texCoords);
213    compareTris(basic, rd);
214 }
215
216 void test_big()
217 {
218     SGBinObject basic;
219     SGPath path(simgear::Dir::current().file("big.btg.gz"));
220     
221     SGVec3d center(1, 2, 3);
222     basic.set_gbs_center(center);
223     basic.set_gbs_radius(12345);
224     
225     std::vector<SGVec3d> points;
226     generate_points(200000, points);
227     std::vector<SGVec3f> normals;
228     generate_normals(1024, normals);
229     std::vector<SGVec2f> texCoords;
230     generate_tcs(300000, texCoords);
231     
232     basic.set_wgs84_nodes(points);
233     basic.set_normals(normals);
234     basic.set_texcoords(texCoords);
235     
236     generate_tris(basic, 200000);
237     
238     bool ok = basic.write_bin_file(path);
239     VERIFY( ok );
240     
241     SGBinObject rd;
242    ok = rd.read_bin(path.str()) ;
243    VERIFY( ok);
244    COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
245    COMPARE(rd.get_wgs84_nodes().size(), points.size());
246    COMPARE(rd.get_texcoords().size(), texCoords.size());
247    
248    comparePoints(rd, points);
249    compareTexCoords(rd, texCoords);
250    compareTris(basic, rd);
251 }
252
253 int main(int argc, char* argv[])
254 {
255     test_empty();
256     test_basic();
257     test_many_tcs();
258     test_big();
259     
260     return 0;
261 }