]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_binobj.cxx
Removal of PLIB/SG from SimGear
[simgear.git] / simgear / io / sg_binobj.cxx
index 3c3213082ae7e033945b3325426439597af9c6c9..5f7a34d27c8639f376e0247bdff32213fee27b02 100644 (file)
@@ -31,6 +31,8 @@
 
 #include <stdio.h>
 #include <time.h>
+#include <cstring>
+#include <cstdlib> // for system()
 
 #include <vector>
 #include <string>
@@ -38,6 +40,7 @@
 
 #include <simgear/bucket/newbucket.hxx>
 #include <simgear/misc/sg_path.hxx>
+#include <simgear/math/SGGeometry.hxx>
 
 #include "lowlevel.hxx"
 #include "sg_binobj.hxx"
@@ -115,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,
@@ -737,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
@@ -748,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
@@ -758,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
@@ -781,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
@@ -1128,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());
@@ -1179,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");