3 # include <simgear_config.h>
6 #include <simgear/compiler.h>
12 #include <simgear/misc/sg_dir.hxx>
14 #include "sg_binobj.hxx"
21 #define COMPARE(a, b) \
23 cerr << "failed:" << #a << " != " << #b << endl; \
24 cerr << "\tgot:" << a << endl; \
30 cerr << "failed:" << #a << endl; \
34 void generate_points(int count, std::vector<SGVec3d>& vec)
36 for (int i=0; i<count; ++i) {
37 vec.push_back(SGVec3d(i * 0.5, i * i, i * 4));
41 void generate_normals(int count, std::vector<SGVec3f>& vec)
43 for (int i=0; i<count; ++i) {
44 vec.push_back(normalize(SGVec3f(i, i * 2, i * -4)));
48 void generate_tcs(int count, std::vector<SGVec2f>& vec)
50 for (int i=0; i<count; ++i) {
51 vec.push_back(SGVec2f(1.0 / i, 16.0 / i));
58 SGPath path(simgear::Dir::current().file("empty.btg.gz"));
59 bool ok = empty.write_bin_file(path);
62 ok = rd.read_bin(path.str()) ;
65 COMPARE(rd.get_wgs84_nodes().size(), 0);
66 VERIFY(rd.get_pt_materials().empty());
69 void comparePoints(const SGBinObject& rd, const std::vector<SGVec3d>& b)
71 for (unsigned int i=1; i<b.size(); i += 10) {
72 SGVec3d pos = rd.get_wgs84_nodes()[i];
73 pos += rd.get_gbs_center();
75 if (!equivalent(pos, b[i], 0.1)) {
76 cout << "i=" << i << endl;
81 VERIFY(equivalent(pos, b[i], 0.1));
85 void compareTexCoords(const SGBinObject& rd, const std::vector<SGVec2f>& b)
87 for (unsigned int i=1; i<b.size(); i += 10) {
88 SGVec2f pos = rd.get_texcoords()[i];
89 VERIFY(equivalent(pos, b[i], 0.001f));
93 int_list make_tri(int maxIndex)
96 r.push_back(random() % maxIndex);
97 r.push_back(random() % maxIndex);
98 r.push_back(random() % maxIndex);
102 void compareTris(const SGBinObject& a, const SGBinObject& b)
104 unsigned int count = a.get_tri_materials().size();
105 for (unsigned int i=0; i<count; i += 39) {
106 const int_list& vA(a.get_tris_v()[i]);
107 const int_list& vB(b.get_tris_v()[i]);
110 COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
112 const int_list& tA(a.get_tris_tc()[i]);
113 const int_list& tB(b.get_tris_tc()[i]);
118 void generate_tris(SGBinObject& b, int count)
121 string_list materials;
123 int maxVertices = b.get_wgs84_nodes().size();
124 int maxNormals = b.get_normals().size();
125 int maxTCs = b.get_texcoords().size();
127 for (int t=0; t<count; ++t) {
128 v.push_back(make_tri(maxVertices));
129 n.push_back(make_tri(maxNormals));
130 tc.push_back(make_tri(maxTCs));
131 materials.push_back("material1");
137 b.set_tri_materials(materials);
143 SGPath path(simgear::Dir::current().file("basic.btg.gz"));
145 SGVec3d center(1, 2, 3);
146 basic.set_gbs_center(center);
147 basic.set_gbs_radius(12345);
149 std::vector<SGVec3d> points;
150 generate_points(1024, points);
151 std::vector<SGVec3f> normals;
152 generate_normals(1024, normals);
153 std::vector<SGVec2f> texCoords;
154 generate_tcs(10000, texCoords);
156 basic.set_wgs84_nodes(points);
157 basic.set_normals(normals);
158 basic.set_texcoords(texCoords);
160 bool ok = basic.write_bin_file(path);
164 ok = rd.read_bin(path.str()) ;
166 COMPARE(rd.get_version(), 7); // should be version 7 since indices are < 2^16
167 COMPARE(rd.get_gbs_center(), center);
168 COMPARE(rd.get_gbs_radius(), 12345);
169 COMPARE(rd.get_wgs84_nodes().size(), points.size());
171 comparePoints(rd, points);
172 compareTexCoords(rd, texCoords);
178 SGPath path(simgear::Dir::current().file("many_tex.btg.gz"));
180 SGVec3d center(1, 2, 3);
181 basic.set_gbs_center(center);
182 basic.set_gbs_radius(12345);
184 std::vector<SGVec3d> points;
185 generate_points(10000, points);
186 std::vector<SGVec3f> normals;
187 generate_normals(1024, normals);
188 std::vector<SGVec2f> texCoords;
189 generate_tcs(100000, texCoords);
191 basic.set_wgs84_nodes(points);
192 basic.set_normals(normals);
193 basic.set_texcoords(texCoords);
195 generate_tris(basic, 20000);
197 bool ok = basic.write_bin_file(path);
201 ok = rd.read_bin(path.str()) ;
203 COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
204 COMPARE(rd.get_wgs84_nodes().size(), points.size());
205 COMPARE(rd.get_texcoords().size(), texCoords.size());
207 comparePoints(rd, points);
208 compareTexCoords(rd, texCoords);
209 compareTris(basic, rd);
215 SGPath path(simgear::Dir::current().file("big.btg.gz"));
217 SGVec3d center(1, 2, 3);
218 basic.set_gbs_center(center);
219 basic.set_gbs_radius(12345);
221 std::vector<SGVec3d> points;
222 generate_points(200000, points);
223 std::vector<SGVec3f> normals;
224 generate_normals(1024, normals);
225 std::vector<SGVec2f> texCoords;
226 generate_tcs(300000, texCoords);
228 basic.set_wgs84_nodes(points);
229 basic.set_normals(normals);
230 basic.set_texcoords(texCoords);
232 generate_tris(basic, 200000);
234 bool ok = basic.write_bin_file(path);
238 ok = rd.read_bin(path.str()) ;
240 COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
241 COMPARE(rd.get_wgs84_nodes().size(), points.size());
242 COMPARE(rd.get_texcoords().size(), texCoords.size());
244 comparePoints(rd, points);
245 compareTexCoords(rd, texCoords);
246 compareTris(basic, rd);
249 int main(int argc, char* argv[])