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 tci_list make_tri_tcs(int maxIndex)
109 tci[0].push_back(random() % maxIndex);
110 tci[0].push_back(random() % maxIndex);
111 tci[0].push_back(random() % maxIndex);
115 void compareTris(const SGBinObject& a, const SGBinObject& b)
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]);
123 COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
125 const int_list& tA(a.get_tris_tcs()[i][0]);
126 const int_list& tB(b.get_tris_tcs()[i][0]);
131 void generate_tris(SGBinObject& b, int count)
135 string_list materials;
137 int maxVertices = b.get_wgs84_nodes().size();
138 int maxNormals = b.get_normals().size();
139 int maxTCs = b.get_texcoords().size();
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);
148 b.add_triangle( sgboTri );
155 SGPath path(simgear::Dir::current().file("basic.btg.gz"));
157 SGVec3d center(1, 2, 3);
158 basic.set_gbs_center(center);
159 basic.set_gbs_radius(12345);
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);
168 basic.set_wgs84_nodes(points);
169 basic.set_normals(normals);
170 basic.set_texcoords(texCoords);
172 bool ok = basic.write_bin_file(path);
176 ok = rd.read_bin(path.str()) ;
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());
183 comparePoints(rd, points);
184 compareTexCoords(rd, texCoords);
190 SGPath path(simgear::Dir::current().file("many_tex.btg.gz"));
192 SGVec3d center(1, 2, 3);
193 basic.set_gbs_center(center);
194 basic.set_gbs_radius(12345);
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);
203 basic.set_wgs84_nodes(points);
204 basic.set_normals(normals);
205 basic.set_texcoords(texCoords);
207 generate_tris(basic, 20000);
209 bool ok = basic.write_bin_file(path);
213 ok = rd.read_bin(path.str()) ;
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());
219 comparePoints(rd, points);
220 compareTexCoords(rd, texCoords);
221 compareTris(basic, rd);
227 SGPath path(simgear::Dir::current().file("big.btg.gz"));
229 SGVec3d center(1, 2, 3);
230 basic.set_gbs_center(center);
231 basic.set_gbs_radius(12345);
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);
240 basic.set_wgs84_nodes(points);
241 basic.set_normals(normals);
242 basic.set_texcoords(texCoords);
244 generate_tris(basic, 200000);
246 bool ok = basic.write_bin_file(path);
250 ok = rd.read_bin(path.str()) ;
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());
256 comparePoints(rd, points);
257 compareTexCoords(rd, texCoords);
258 compareTris(basic, rd);
261 void test_some_objects()
264 SGPath path(simgear::Dir::current().file("some_objects.btg.gz"));
266 SGVec3d center(1, 2, 3);
267 basic.set_gbs_center(center);
268 basic.set_gbs_radius(12345);
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);
277 basic.set_wgs84_nodes(points);
278 basic.set_normals(normals);
279 basic.set_texcoords(texCoords);
281 generate_tris(basic, 30000); // a number smaller than 2^15!
283 bool ok = basic.write_bin_file(path);
287 ok = rd.read_bin(path.str()) ;
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());
293 comparePoints(rd, points);
294 compareTexCoords(rd, texCoords);
295 compareTris(basic, rd);
298 void test_many_objects()
301 SGPath path(simgear::Dir::current().file("many_objects.btg.gz"));
303 SGVec3d center(1, 2, 3);
304 basic.set_gbs_center(center);
305 basic.set_gbs_radius(12345);
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);
314 basic.set_wgs84_nodes(points);
315 basic.set_normals(normals);
316 basic.set_texcoords(texCoords);
318 generate_tris(basic, 200000);
320 bool ok = basic.write_bin_file(path);
324 ok = rd.read_bin(path.str()) ;
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());
330 comparePoints(rd, points);
331 compareTexCoords(rd, texCoords);
332 compareTris(basic, rd);
335 int main(int argc, char* argv[])