#include <stdio.h>
#include <time.h>
+#include <cstring>
+#include <cstdlib> // for system()
#include <vector>
#include <string>
#include <simgear/bucket/newbucket.hxx>
#include <simgear/misc/sg_path.hxx>
+#include <simgear/math/SGGeometry.hxx>
#include "lowlevel.hxx"
#include "sg_binobj.hxx"
-SG_USING_STD( string );
-SG_USING_STD( vector );
-
+using std::string;
+using std::vector;
using std::cout;
using std::endl;
}
};
-
-// calculate the center of a list of points, by taking the halfway
-// point between the min and max points.
-Point3D sgCalcCenter( point_list& wgs84_nodes ) {
- Point3D p, min, max;
-
- if ( wgs84_nodes.size() ) {
- min = max = wgs84_nodes[0];
- } else {
- min = max = Point3D( 0 );
- }
-
- for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
- p = wgs84_nodes[i];
-
- if ( p.x() < min.x() ) { min.setx( p.x() ); }
- if ( p.y() < min.y() ) { min.sety( p.y() ); }
- if ( p.z() < min.z() ) { min.setz( p.z() ); }
-
- if ( p.x() > max.x() ) { max.setx( p.x() ); }
- if ( p.y() > max.y() ) { max.sety( p.y() ); }
- if ( p.z() > max.z() ) { max.setz( p.z() ); }
- }
-
- return ( min + max ) / 2.0;
-}
-
-// calculate the bounding sphere. Center is the center of the
-// tile and zero elevation
-double sgCalcBoundingRadius( Point3D center, point_list& wgs84_nodes ) {
- double dist_squared;
- double radius_squared = 0;
-
- for ( int i = 0; i < (int)wgs84_nodes.size(); ++i ) {
- dist_squared = center.distance3Dsquared( wgs84_nodes[i] );
- if ( dist_squared > radius_squared ) {
- radius_squared = dist_squared;
- }
- }
-
- return sqrt(radius_squared);
-}
-
-
-
// read object properties
static void read_object( gzFile fp,
int obj_type,
sgWriteUShort( fp, 1 ); // nelements
sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
- sgdVec3 center;
- sgdSetVec3( center, gbs_center.x(), gbs_center.y(), gbs_center.z() );
- sgWritedVec3( fp, center );
+ sgWritedVec3( fp, gbs_center );
sgWriteFloat( fp, gbs_radius );
// dump vertex list
sgWriteUShort( fp, 1 ); // nelements
sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
- SGVec3f p = toVec3f(wgs84_nodes[i] - gbs_center);
- sgWriteVec3( fp, p.data() );
+ sgWriteVec3( fp, toVec3f(wgs84_nodes[i] - gbs_center));
}
// dump vertex color list
sgWriteUShort( fp, 1 ); // nelements
sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
for ( i = 0; i < (int)colors.size(); ++i ) {
- sgWriteVec4( fp, colors[i].data() );
+ sgWriteVec4( fp, colors[i]);
}
// dump vertex normal list
sgWriteUShort( fp, 1 ); // nelements
sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
for ( i = 0; i < (int)texcoords.size(); ++i ) {
- sgWriteVec2( fp, texcoords[i].data() );
+ sgWriteVec2( fp, texcoords[i]);
}
// dump point groups if they exist
}
// cout << "group = " << start << " to " << end - 1 << endl;
- // make a list of points for the group
- point_list group_nodes;
- group_nodes.clear();
- SGVec3d bs_center;
- double bs_radius = 0;
- for ( i = start; i < end; ++i ) {
- for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
- group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ tris_v[i][j] ]) );
- bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
- bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
- }
- }
-
+ SGSphered d;
+ for ( i = start; i < end; ++i ) {
+ for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
+ d.expandBy(wgs84_nodes[ tris_v[i][j] ]);
+ }
+ }
+
+ SGVec3d bs_center = d.getCenter();
+ double bs_radius = d.getRadius();
+
// write group headers
fprintf(fp, "\n");
fprintf(fp, "# usemtl %s\n", material.c_str());
}
// cout << "group = " << start << " to " << end - 1 << endl;
- // make a list of points for the group
- point_list group_nodes;
- group_nodes.clear();
- SGVec3d bs_center;
- double bs_radius = 0;
- for ( i = start; i < end; ++i ) {
- for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
- group_nodes.push_back( Point3D::fromSGVec3(wgs84_nodes[ strips_v[i][j] ]) );
- bs_center = sgCalcCenter( group_nodes ).toSGVec3d();
- bs_radius = sgCalcBoundingRadius( Point3D::fromSGVec3(bs_center), group_nodes );
- }
- }
+
+ SGSphered d;
+ for ( i = start; i < end; ++i ) {
+ for ( j = 0; j < (int)tris_v[i].size(); ++j ) {
+ d.expandBy(wgs84_nodes[ tris_v[i][j] ]);
+ }
+ }
+
+ SGVec3d bs_center = d.getCenter();
+ double bs_radius = d.getRadius();
// write group headers
fprintf(fp, "\n");