3 # include <simgear_config.h>
6 #include <simgear/compiler.h>
12 #if defined _MSC_VER || defined _WIN32_WINNT
16 #include <simgear/misc/sg_dir.hxx>
18 #include "sg_binobj.hxx"
25 #define COMPARE(a, b) \
27 cerr << "failed:" << #a << " != " << #b << endl; \
28 cerr << "\tgot:" << a << endl; \
34 cerr << "failed:" << #a << endl; \
38 void generate_points(int count, std::vector<SGVec3d>& vec)
40 for (int i=0; i<count; ++i) {
41 vec.push_back(SGVec3d(i * 0.5, i * i, i * 4));
45 void generate_normals(int count, std::vector<SGVec3f>& vec)
47 for (int i=0; i<count; ++i) {
48 vec.push_back(normalize(SGVec3f(i, i * 2, i * -4)));
52 void generate_tcs(int count, std::vector<SGVec2f>& vec)
54 for (int i=0; i<count; ++i) {
55 vec.push_back(SGVec2f(1.0 / i, 16.0 / i));
62 SGPath path(simgear::Dir::current().file("empty.btg.gz"));
63 bool ok = empty.write_bin_file(path);
66 ok = rd.read_bin(path.str()) ;
69 COMPARE(rd.get_wgs84_nodes().size(), 0);
70 VERIFY(rd.get_pt_materials().empty());
73 void comparePoints(const SGBinObject& rd, const std::vector<SGVec3d>& b)
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();
79 if (!equivalent(pos, b[i], 0.1)) {
80 cout << "i=" << i << endl;
85 VERIFY(equivalent(pos, b[i], 0.1));
89 void compareTexCoords(const SGBinObject& rd, const std::vector<SGVec2f>& b)
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));
97 int_list make_tri(int maxIndex)
105 while(!valid && retry--) {
106 a = (random() % maxIndex);
107 b = (random() % maxIndex);
108 c = (random() % maxIndex);
110 valid = ( (a!=b) && (b!=c) && (c!=a) );
114 cerr << "can't generate valid triangle" << endl;
125 tci_list make_tri_tcs(int maxIndex)
128 tci[0].push_back(random() % maxIndex);
129 tci[0].push_back(random() % maxIndex);
130 tci[0].push_back(random() % maxIndex);
134 void compareTris(const SGBinObject& a, const SGBinObject& b)
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]);
142 COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
144 const int_list& tA(a.get_tris_tcs()[i][0]);
145 const int_list& tB(b.get_tris_tcs()[i][0]);
150 void generate_tris(SGBinObject& b, int count)
154 string_list materials;
156 int maxVertices = b.get_wgs84_nodes().size();
157 int maxNormals = b.get_normals().size();
158 int maxTCs = b.get_texcoords().size();
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);
167 b.add_triangle( sgboTri );
174 SGPath path(simgear::Dir::current().file("basic.btg.gz"));
176 SGVec3d center(1, 2, 3);
177 basic.set_gbs_center(center);
178 basic.set_gbs_radius(12345);
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);
187 basic.set_wgs84_nodes(points);
188 basic.set_normals(normals);
189 basic.set_texcoords(texCoords);
191 bool ok = basic.write_bin_file(path);
195 ok = rd.read_bin(path.str()) ;
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());
202 comparePoints(rd, points);
203 compareTexCoords(rd, texCoords);
209 SGPath path(simgear::Dir::current().file("many_tex.btg.gz"));
211 SGVec3d center(1, 2, 3);
212 basic.set_gbs_center(center);
213 basic.set_gbs_radius(12345);
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);
222 basic.set_wgs84_nodes(points);
223 basic.set_normals(normals);
224 basic.set_texcoords(texCoords);
226 generate_tris(basic, 20000);
228 bool ok = basic.write_bin_file(path);
232 ok = rd.read_bin(path.str()) ;
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());
238 comparePoints(rd, points);
239 compareTexCoords(rd, texCoords);
240 compareTris(basic, rd);
246 SGPath path(simgear::Dir::current().file("big.btg.gz"));
248 SGVec3d center(1, 2, 3);
249 basic.set_gbs_center(center);
250 basic.set_gbs_radius(12345);
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);
259 basic.set_wgs84_nodes(points);
260 basic.set_normals(normals);
261 basic.set_texcoords(texCoords);
263 generate_tris(basic, 200000);
265 bool ok = basic.write_bin_file(path);
269 ok = rd.read_bin(path.str()) ;
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());
275 comparePoints(rd, points);
276 compareTexCoords(rd, texCoords);
277 compareTris(basic, rd);
280 void test_some_objects()
283 SGPath path(simgear::Dir::current().file("some_objects.btg.gz"));
285 SGVec3d center(1, 2, 3);
286 basic.set_gbs_center(center);
287 basic.set_gbs_radius(12345);
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);
296 basic.set_wgs84_nodes(points);
297 basic.set_normals(normals);
298 basic.set_texcoords(texCoords);
300 generate_tris(basic, 30000); // a number smaller than 2^15!
302 bool ok = basic.write_bin_file(path);
306 ok = rd.read_bin(path.str()) ;
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());
312 comparePoints(rd, points);
313 compareTexCoords(rd, texCoords);
314 compareTris(basic, rd);
317 void test_many_objects()
320 SGPath path(simgear::Dir::current().file("many_objects.btg.gz"));
322 SGVec3d center(1, 2, 3);
323 basic.set_gbs_center(center);
324 basic.set_gbs_radius(12345);
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);
333 basic.set_wgs84_nodes(points);
334 basic.set_normals(normals);
335 basic.set_texcoords(texCoords);
337 generate_tris(basic, 200000);
339 bool ok = basic.write_bin_file(path);
343 ok = rd.read_bin(path.str()) ;
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());
349 comparePoints(rd, points);
350 compareTexCoords(rd, texCoords);
351 compareTris(basic, rd);
354 int main(int argc, char* argv[])