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)
100 r.push_back(random() % maxIndex);
101 r.push_back(random() % maxIndex);
102 r.push_back(random() % maxIndex);
106 void compareTris(const SGBinObject& a, const SGBinObject& b)
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]);
114 COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
116 const int_list& tA(a.get_tris_tc()[i]);
117 const int_list& tB(b.get_tris_tc()[i]);
122 void generate_tris(SGBinObject& b, int count)
125 string_list materials;
127 int maxVertices = b.get_wgs84_nodes().size();
128 int maxNormals = b.get_normals().size();
129 int maxTCs = b.get_texcoords().size();
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");
141 b.set_tri_materials(materials);
147 SGPath path(simgear::Dir::current().file("basic.btg.gz"));
149 SGVec3d center(1, 2, 3);
150 basic.set_gbs_center(center);
151 basic.set_gbs_radius(12345);
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);
160 basic.set_wgs84_nodes(points);
161 basic.set_normals(normals);
162 basic.set_texcoords(texCoords);
164 bool ok = basic.write_bin_file(path);
168 ok = rd.read_bin(path.str()) ;
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());
175 comparePoints(rd, points);
176 compareTexCoords(rd, texCoords);
182 SGPath path(simgear::Dir::current().file("many_tex.btg.gz"));
184 SGVec3d center(1, 2, 3);
185 basic.set_gbs_center(center);
186 basic.set_gbs_radius(12345);
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);
195 basic.set_wgs84_nodes(points);
196 basic.set_normals(normals);
197 basic.set_texcoords(texCoords);
199 generate_tris(basic, 20000);
201 bool ok = basic.write_bin_file(path);
205 ok = rd.read_bin(path.str()) ;
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());
211 comparePoints(rd, points);
212 compareTexCoords(rd, texCoords);
213 compareTris(basic, rd);
219 SGPath path(simgear::Dir::current().file("big.btg.gz"));
221 SGVec3d center(1, 2, 3);
222 basic.set_gbs_center(center);
223 basic.set_gbs_radius(12345);
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);
232 basic.set_wgs84_nodes(points);
233 basic.set_normals(normals);
234 basic.set_texcoords(texCoords);
236 generate_tris(basic, 200000);
238 bool ok = basic.write_bin_file(path);
242 ok = rd.read_bin(path.str()) ;
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());
248 comparePoints(rd, points);
249 compareTexCoords(rd, texCoords);
250 compareTris(basic, rd);
253 void test_some_objects()
256 SGPath path(simgear::Dir::current().file("some_objects.btg.gz"));
258 SGVec3d center(1, 2, 3);
259 basic.set_gbs_center(center);
260 basic.set_gbs_radius(12345);
262 std::vector<SGVec3d> points;
263 generate_points(10000, points);
264 std::vector<SGVec3f> normals;
265 generate_normals(1024, normals);
266 std::vector<SGVec2f> texCoords;
267 generate_tcs(20000, texCoords);
269 basic.set_wgs84_nodes(points);
270 basic.set_normals(normals);
271 basic.set_texcoords(texCoords);
273 generate_tris(basic, 30000); // a number smaller than 2^15!
275 bool ok = basic.write_bin_file(path);
279 ok = rd.read_bin(path.str()) ;
281 COMPARE(rd.get_version(), 7); // since we have less than 2^15 tris
282 COMPARE(rd.get_wgs84_nodes().size(), points.size());
283 COMPARE(rd.get_texcoords().size(), texCoords.size());
285 comparePoints(rd, points);
286 compareTexCoords(rd, texCoords);
287 compareTris(basic, rd);
290 void test_many_objects()
293 SGPath path(simgear::Dir::current().file("many_objects.btg.gz"));
295 SGVec3d center(1, 2, 3);
296 basic.set_gbs_center(center);
297 basic.set_gbs_radius(12345);
299 std::vector<SGVec3d> points;
300 generate_points(10000, points);
301 std::vector<SGVec3f> normals;
302 generate_normals(1024, normals);
303 std::vector<SGVec2f> texCoords;
304 generate_tcs(20000, texCoords);
306 basic.set_wgs84_nodes(points);
307 basic.set_normals(normals);
308 basic.set_texcoords(texCoords);
310 generate_tris(basic, 200000);
312 bool ok = basic.write_bin_file(path);
316 ok = rd.read_bin(path.str()) ;
318 COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
319 COMPARE(rd.get_wgs84_nodes().size(), points.size());
320 COMPARE(rd.get_texcoords().size(), texCoords.size());
322 comparePoints(rd, points);
323 compareTexCoords(rd, texCoords);
324 compareTris(basic, rd);
327 int main(int argc, char* argv[])