X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_binobj.cxx;h=5f7a34d27c8639f376e0247bdff32213fee27b02;hb=d90647e9babf61a2fb6cef1ef994e360beb876a4;hp=692ee56abf26fabe94df0199f109337f7a93c333;hpb=733e6fa14ff507a1022ecab8d55cc9bf587bee40;p=simgear.git diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index 692ee56a..5f7a34d2 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -31,6 +31,8 @@ #include #include +#include +#include // for system() #include #include @@ -38,14 +40,14 @@ #include #include +#include #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; @@ -116,51 +118,6 @@ public: } }; - -// 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, @@ -738,9 +695,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, 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 @@ -749,8 +704,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, 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 @@ -759,7 +713,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, 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 @@ -782,7 +736,7 @@ bool SGBinObject::write_bin( const string& base, const string& name, 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 @@ -1129,19 +1083,16 @@ bool SGBinObject::write_ascii( const string& base, const string& name, } // 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()); @@ -1180,18 +1131,16 @@ bool SGBinObject::write_ascii( const string& base, const string& name, } // 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");