]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_binobj.cxx
Remove deprecated vector classes - finally!
[simgear.git] / simgear / io / sg_binobj.cxx
index d2da18ef92ae3a250e256d56842a47ee72472f8d..faed1e68f067ce9bb8e6e5087c11548208c75e0b 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Written by Curtis Olson, started January 2000.
 //
-// Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
+// Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -16,7 +16,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 //
 #endif
 
 #include <simgear/compiler.h>
+#include <simgear/debug/logstream.hxx>
 
 #include <stdio.h>
 #include <time.h>
 
 #include <vector>
-#include STL_STRING
+#include <string>
+#include <iostream>
 
 #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;
 
-#if !defined (SG_HAVE_NATIVE_SGI_COMPILERS)
-SG_USING_STD( cout );
-SG_USING_STD( endl );
-#endif
-
-
-enum {
+enum sgObjectTypes {
     SG_BOUNDING_SPHERE = 0,
 
     SG_VERTEX_LIST = 1,
@@ -62,20 +62,19 @@ enum {
     SG_TRIANGLE_FACES = 10,
     SG_TRIANGLE_STRIPS = 11,
     SG_TRIANGLE_FANS = 12
-} sgObjectTypes;
+};
 
-enum {
+enum sgIndexTypes {
     SG_IDX_VERTICES =  0x01,
     SG_IDX_NORMALS =   0x02,
     SG_IDX_COLORS =    0x04,
     SG_IDX_TEXCOORDS = 0x08
-} sgIndexTypes;
+};
 
-enum {
+enum sgPropertyTypes {
     SG_MATERIAL = 0,
     SG_INDEX_TYPES = 1
-} sgPropertyTypes;
-
+};
 
 
 class sgSimpleBuffer {
@@ -93,7 +92,7 @@ public:
        while ( size < s ) {
            size *= 2;
        }
-       cout << "Creating a new buffer of size = " << size << endl;
+        SG_LOG(SG_EVENT, SG_DEBUG, "Creating a new buffer of size = " << size);
        ptr = new char[size];
     }
 
@@ -111,57 +110,12 @@ public:
            while ( size < s ) {
                size *= 2;
            }
-           cout << "resizing buffer to size = " << size << endl;
+            SG_LOG(SG_EVENT, SG_DEBUG, "resizing buffer to size = " << size);
            ptr = new char[size];
        }
     }
 };
 
-
-// calculate the center of a list of points, by taking the halfway
-// point between the min and max points.
-static Point3D calc_center( 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,
@@ -178,7 +132,7 @@ static void read_object( gzFile fp,
     int idx_size;
     bool do_vertices, do_normals, do_colors, do_texcoords;
     int j, k, idx;
-    static sgSimpleBuffer buf( 32768 );  // 32 Kb
+    sgSimpleBuffer buf( 32768 );  // 32 Kb
     char material[256];
 
     // default values
@@ -244,8 +198,8 @@ static void read_object( gzFile fp,
        if ( nbytes > buf.get_size() ) { buf.resize( nbytes ); }
        char *ptr = buf.get_ptr();
        sgReadBytes( fp, nbytes, ptr );
-       int count = nbytes / (idx_size * sizeof(short));
-       short *sptr = (short *)ptr;
+       int count = nbytes / (idx_size * sizeof(unsigned short));
+       unsigned short *sptr = (unsigned short *)ptr;
        int_list vs; vs.clear();
        int_list ns; ns.clear();
        int_list cs; cs.clear();
@@ -253,7 +207,7 @@ static void read_object( gzFile fp,
        for ( k = 0; k < count; ++k ) {
            if ( sgIsBigEndian() ) {
                for ( idx = 0; idx < idx_size; ++idx ) {
-                   sgEndianSwap( (unsigned short *)&(sptr[idx]) );
+                   sgEndianSwap( (uint16_t *)&(sptr[idx]) );
                }
            }
            idx = 0;
@@ -284,13 +238,13 @@ static void read_object( gzFile fp,
 
 // read a binary file and populate the provided structures.
 bool SGBinObject::read_bin( const string& file ) {
-    Point3D p;
+    SGVec3d p;
     int i, j, k;
     unsigned int nbytes;
-    static sgSimpleBuffer buf( 32768 );  // 32 Kb
+    sgSimpleBuffer buf( 32768 );  // 32 Kb
 
     // zero out structures
-    gbs_center = Point3D( 0 );
+    gbs_center = SGVec3d(0, 0, 0);
     gbs_radius = 0.0;
 
     wgs84_nodes.clear();
@@ -325,8 +279,8 @@ bool SGBinObject::read_bin( const string& file ) {
     if ( (fp = gzopen( file.c_str(), "rb" )) == NULL ) {
        string filegz = file + ".gz";
        if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
-           cout << "ERROR: opening " << file << " or " << filegz
-                << "for reading!" << endl;
+            SG_LOG( SG_EVENT, SG_ALERT,
+               "ERROR: opening " << file << " or " << filegz << " for reading!");
 
            return false;
        }
@@ -364,22 +318,36 @@ bool SGBinObject::read_bin( const string& file ) {
     local_tm = localtime( &calendar_time );
     char time_str[256];
     strftime( time_str, 256, "%a %b %d %H:%M:%S %Z %Y", local_tm);
-    cout << "File created on " << time_str << endl;
+    SG_LOG( SG_EVENT, SG_DEBUG, "File created on " << time_str);
 #endif
 
     // read number of top level objects
-    short nobjects;
-    sgReadShort( fp, &nobjects );
+    unsigned short nobjects;
+    if ( version >= 7 ) {
+        sgReadUShort( fp, &nobjects );
+    } else {
+        short tmp;
+        sgReadShort( fp, &tmp );
+        nobjects = tmp;
+    }
     // cout << "Total objects to read = " << nobjects << endl;
 
     // read in objects
     for ( i = 0; i < nobjects; ++i ) {
        // read object header
        char obj_type;
-       short nproperties, nelements;
+       unsigned short nproperties, nelements;
        sgReadChar( fp, &obj_type );
-       sgReadShort( fp, &nproperties );
-       sgReadShort( fp, &nelements );
+        if ( version >= 7 ) {
+            sgReadUShort( fp, &nproperties );
+            sgReadUShort( fp, &nelements );
+        } else {
+            short tmp;
+            sgReadShort( fp, &tmp );
+            nproperties = tmp;
+            sgReadShort( fp, &tmp );
+            nelements = tmp;
+        }
 
        // cout << "object " << i << " = " << (int)obj_type << " props = "
        //      << nproperties << " elements = " << nelements << endl;
@@ -407,17 +375,17 @@ bool SGBinObject::read_bin( const string& file ) {
 
                double *dptr = (double *)ptr;
                if ( sgIsBigEndian() ) {
-                   sgEndianSwap( (uint64 *)&(dptr[0]) );
-                   sgEndianSwap( (uint64 *)&(dptr[1]) );
-                   sgEndianSwap( (uint64 *)&(dptr[2]) );
+                   sgEndianSwap( (uint64_t *)&(dptr[0]) );
+                   sgEndianSwap( (uint64_t *)&(dptr[1]) );
+                   sgEndianSwap( (uint64_t *)&(dptr[2]) );
                }
-               gbs_center = Point3D( dptr[0], dptr[1], dptr[2] );
+               gbs_center = SGVec3d( dptr[0], dptr[1], dptr[2] );
                // cout << "Center = " << gbs_center << endl;
                ptr += sizeof(double) * 3;
                
                float *fptr = (float *)ptr;
                if ( sgIsBigEndian() ) {
-                   sgEndianSwap( (unsigned int *)fptr );
+                   sgEndianSwap( (uint32_t *)fptr );
                }
                gbs_radius = fptr[0];
                // cout << "Bounding radius = " << gbs_radius << endl;
@@ -447,11 +415,11 @@ bool SGBinObject::read_bin( const string& file ) {
                wgs84_nodes.reserve( count );
                for ( k = 0; k < count; ++k ) {
                    if ( sgIsBigEndian() ) {
-                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[2]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[0]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[1]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[2]) );
                    }
-                   wgs84_nodes.push_back( Point3D(fptr[0], fptr[1], fptr[2]) );
+                   wgs84_nodes.push_back( SGVec3d(fptr[0], fptr[1], fptr[2]) );
                    fptr += 3;
                }
            }
@@ -480,12 +448,13 @@ bool SGBinObject::read_bin( const string& file ) {
                colors.reserve(count);
                for ( k = 0; k < count; ++k ) {
                    if ( sgIsBigEndian() ) {
-                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[2]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[3]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[0]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[1]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[2]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[3]) );
                    }
-                   colors.push_back( Point3D( fptr[0], fptr[1], fptr[2] ) );
+                   SGVec4f color( fptr[0], fptr[1], fptr[2], fptr[3] );
+                   colors.push_back( color );
                    fptr += 4;
                }
            }
@@ -512,14 +481,11 @@ bool SGBinObject::read_bin( const string& file ) {
                int count = nbytes / 3;
                normals.reserve( count );
                for ( k = 0; k < count; ++k ) {
-                    sgdVec3 normal;
-                    sgdSetVec3( normal,
-                               (ptr[0]) / 127.5 - 1.0,
-                               (ptr[1]) / 127.5 - 1.0,
-                               (ptr[2]) / 127.5 - 1.0 );
-                    sgdNormalizeVec3( normal );
-
-                   normals.push_back(Point3D(normal[0], normal[1], normal[2]));
+                    SGVec3f normal((ptr[0]) / 127.5 - 1.0,
+                                   (ptr[1]) / 127.5 - 1.0,
+                                   (ptr[2]) / 127.5 - 1.0);
+
+                   normals.push_back(normalize(normal));
                    ptr += 3;
                }
            }
@@ -548,10 +514,10 @@ bool SGBinObject::read_bin( const string& file ) {
                texcoords.reserve(count);
                for ( k = 0; k < count; ++k ) {
                    if ( sgIsBigEndian() ) {
-                       sgEndianSwap( (unsigned int *)&(fptr[0]) );
-                       sgEndianSwap( (unsigned int *)&(fptr[1]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[0]) );
+                       sgEndianSwap( (uint32_t *)&(fptr[1]) );
                    }
-                   texcoords.push_back( Point3D( fptr[0], fptr[1], 0 ) );
+                   texcoords.push_back( SGVec2f( fptr[0], fptr[1] ) );
                    fptr += 2;
                }
            }
@@ -616,28 +582,17 @@ bool SGBinObject::read_bin( const string& file ) {
 bool SGBinObject::write_bin( const string& base, const string& name,
                             const SGBucket& b )
 {
-    Point3D p;
-    sgVec2 t;
-    sgVec3 pt;
-    sgVec4 color;
     int i, j;
     unsigned char idx_mask;
     int idx_size;
 
-    string dir = base + "/" + b.gen_base_path();
-    string command = "mkdir -p " + dir;
-#if defined(_MSC_VER) || defined(__MINGW32__)
-    system( (string("mkdir ") + dir).c_str() );
-#else
-    system(command.c_str());
-#endif
-
-    string file = dir + "/" + name + ".gz";
-    cout << "Output file = " << file << endl;
+    SGPath file = base + "/" + b.gen_base_path() + "/" + name + ".gz";
+    file.create_dir( 0755 );
+    cout << "Output file = " << file.str() << endl;
 
     gzFile fp;
     if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
-       cout << "ERROR: opening " << file << " for writing!" << endl;
+       cout << "ERROR: opening " << file.str() << " for writing!" << endl;
        return false;
     }
 
@@ -660,13 +615,13 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     // write header magic
     sgWriteUInt( fp, SG_FILE_MAGIC_NUMBER );
     time_t calendar_time = time(NULL);
-    sgWriteLong( fp, (long int)calendar_time );
+    sgWriteLong( fp, (int32_t)calendar_time );
 
     // calculate and write number of top level objects
     string material;
     int start;
     int end;
-    short nobjects = 0;
+    unsigned short nobjects = 0;
     nobjects++;                        // for gbs
     nobjects++;                        // for vertices
     nobjects++;                        // for colors
@@ -674,7 +629,7 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     nobjects++;                        // for texcoords
 
     // points
-    short npts = 0;
+    unsigned short npts = 0;
     start = 0; end = 1;
     while ( start < (int)pt_materials.size() ) {
        material = pt_materials[start];
@@ -688,7 +643,7 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     nobjects += npts;
 
     // tris
-    short ntris = 0;
+    unsigned short ntris = 0;
     start = 0; end = 1;
     while ( start < (int)tri_materials.size() ) {
        material = tri_materials[start];
@@ -702,7 +657,7 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     nobjects += ntris;
 
     // strips
-    short nstrips = 0;
+    unsigned short nstrips = 0;
     start = 0; end = 1;
     while ( start < (int)strip_materials.size() ) {
        material = strip_materials[start];
@@ -716,7 +671,7 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     nobjects += nstrips;
 
     // fans
-    short nfans = 0;
+    unsigned short nfans = 0;
     start = 0; end = 1;
     while ( start < (int)fan_materials.size() ) {
        material = fan_materials[start];
@@ -730,12 +685,12 @@ bool SGBinObject::write_bin( const string& base, const string& name,
     nobjects += nfans;
 
     cout << "total top level objects = " << nobjects << endl;
-    sgWriteShort( fp, nobjects );
+    sgWriteUShort( fp, nobjects );
 
     // write bounding sphere
     sgWriteChar( fp, (char)SG_BOUNDING_SPHERE );        // type
-    sgWriteShort( fp, 0 );                             // nproperties
-    sgWriteShort( fp, 1 );                             // nelements
+    sgWriteUShort( fp, 0 );                            // nproperties
+    sgWriteUShort( fp, 1 );                            // nelements
 
     sgWriteUInt( fp, sizeof(double) * 3 + sizeof(float) ); // nbytes
     sgdVec3 center;
@@ -745,37 +700,31 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
     // dump vertex list
     sgWriteChar( fp, (char)SG_VERTEX_LIST );             // type
-    sgWriteShort( fp, 0 );                              // nproperties
-    sgWriteShort( fp, 1 );                              // nelements
+    sgWriteUShort( fp, 0 );                             // nproperties
+    sgWriteUShort( fp, 1 );                             // nelements
     sgWriteUInt( fp, wgs84_nodes.size() * sizeof(float) * 3 ); // nbytes
     for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
-       p = wgs84_nodes[i] - gbs_center;
-       sgSetVec3( pt, p.x(), p.y(), p.z() );
-       sgWriteVec3( fp, pt );
+        SGVec3f p = toVec3f(wgs84_nodes[i] - gbs_center);
+       sgWriteVec3( fp, p.data() );
     }
 
     // dump vertex color list
     sgWriteChar( fp, (char)SG_COLOR_LIST );             // type
-    sgWriteShort( fp, 0 );                              // nproperties
-    sgWriteShort( fp, 1 );                              // nelements
+    sgWriteUShort( fp, 0 );                             // nproperties
+    sgWriteUShort( fp, 1 );                             // nelements
     sgWriteUInt( fp, colors.size() * sizeof(float) * 4 ); // nbytes
     for ( i = 0; i < (int)colors.size(); ++i ) {
-       p = colors[i];
-        // Right now we have a place holder for color alpha but we
-        // need to update the interface so the calling program can
-        // provide the info.
-       sgSetVec4( color, p.x(), p.y(), p.z(), 1.0 );
-       sgWriteVec4( fp, color );
+       sgWriteVec4( fp, colors[i].data() );
     }
 
     // dump vertex normal list
     sgWriteChar( fp, (char)SG_NORMAL_LIST );            // type
-    sgWriteShort( fp, 0 );                             // nproperties
-    sgWriteShort( fp, 1 );                             // nelements
+    sgWriteUShort( fp, 0 );                            // nproperties
+    sgWriteUShort( fp, 1 );                            // nelements
     sgWriteUInt( fp, normals.size() * 3 );              // nbytes
     char normal[3];
     for ( i = 0; i < (int)normals.size(); ++i ) {
-       p = normals[i];
+        SGVec3f p = normals[i];
        normal[0] = (unsigned char)((p.x() + 1.0) * 127.5);
        normal[1] = (unsigned char)((p.y() + 1.0) * 127.5);
        normal[2] = (unsigned char)((p.z() + 1.0) * 127.5);
@@ -784,13 +733,11 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
     // dump texture coordinates
     sgWriteChar( fp, (char)SG_TEXCOORD_LIST );          // type
-    sgWriteShort( fp, 0 );                             // nproperties
-    sgWriteShort( fp, 1 );                             // nelements
+    sgWriteUShort( fp, 0 );                            // nproperties
+    sgWriteUShort( fp, 1 );                            // nelements
     sgWriteUInt( fp, texcoords.size() * sizeof(float) * 2 ); // nbytes
     for ( i = 0; i < (int)texcoords.size(); ++i ) {
-       p = texcoords[i];
-       sgSetVec2( t, p.x(), p.y() );
-       sgWriteVec2( fp, t );
+       sgWriteVec2( fp, texcoords[i].data() );
     }
 
     // dump point groups if they exist
@@ -811,8 +758,8 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
            // write group headers
            sgWriteChar( fp, (char)SG_POINTS );          // type
-           sgWriteShort( fp, 2 );                       // nproperties
-           sgWriteShort( fp, end - start );             // nelements
+           sgWriteUShort( fp, 2 );                      // nproperties
+           sgWriteUShort( fp, end - start );             // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );        // property
            sgWriteUInt( fp, material.length() );        // nbytes
@@ -831,19 +778,20 @@ bool SGBinObject::write_bin( const string& base, const string& name,
            // write strips
            for ( i = start; i < end; ++i ) {
                // nbytes
-               sgWriteUInt( fp, pts_v[i].size() * idx_size * sizeof(short) );
+               sgWriteUInt( fp, pts_v[i].size() * idx_size
+                                 * sizeof(unsigned short) );
                for ( j = 0; j < (int)pts_v[i].size(); ++j ) {
                    if ( pts_v.size() ) { 
-                       sgWriteShort( fp, (short)pts_v[i][j] );
+                       sgWriteUShort( fp, (unsigned short)pts_v[i][j] );
                    }
                    if ( pts_n.size() ) { 
-                       sgWriteShort( fp, (short)pts_n[i][j] );
+                       sgWriteUShort( fp, (unsigned short)pts_n[i][j] );
                    }
                    if ( pts_c.size() ) { 
-                       sgWriteShort( fp, (short)pts_c[i][j] );
+                       sgWriteUShort( fp, (unsigned short)pts_c[i][j] );
                    }
                    if ( pts_tc.size() ) { 
-                       sgWriteShort( fp, (short)pts_tc[i][j] );
+                       sgWriteUShort( fp, (unsigned short)pts_tc[i][j] );
                    }
                }
            }
@@ -862,7 +810,8 @@ bool SGBinObject::write_bin( const string& base, const string& name,
            // find next group
            material = tri_materials[start];
            while ( (end < (int)tri_materials.size()) && 
-                   (material == tri_materials[end]) )
+                   (material == tri_materials[end]) &&
+                   3*(end-start) < 32760 )
            {
                // cout << "end = " << end << endl;
                end++;
@@ -871,8 +820,8 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
            // write group headers
            sgWriteChar( fp, (char)SG_TRIANGLE_FACES ); // type
-           sgWriteShort( fp, 2 );                      // nproperties
-           sgWriteShort( fp, 1 );                      // nelements
+           sgWriteUShort( fp, 2 );                     // nproperties
+           sgWriteUShort( fp, 1 );                      // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );       // property
            sgWriteUInt( fp, material.length() );        // nbytes
@@ -889,22 +838,23 @@ bool SGBinObject::write_bin( const string& base, const string& name,
            sgWriteChar( fp, idx_mask );
 
            // nbytes
-           sgWriteUInt( fp, (end - start) * 3 * idx_size * sizeof(short) );
+           sgWriteUInt( fp, (end - start) * 3 * idx_size
+                             * sizeof(unsigned short) );
 
            // write group
            for ( i = start; i < end; ++i ) {
                for ( j = 0; j < 3; ++j ) {
                    if ( tris_v.size() ) {
-                       sgWriteShort( fp, (short)tris_v[i][j] );
+                       sgWriteUShort( fp, (unsigned short)tris_v[i][j] );
                    }
                    if ( tris_n.size() ) {
-                       sgWriteShort( fp, (short)tris_n[i][j] );
+                       sgWriteUShort( fp, (unsigned short)tris_n[i][j] );
                    }
                    if ( tris_c.size() ) {
-                       sgWriteShort( fp, (short)tris_c[i][j] );
+                       sgWriteUShort( fp, (unsigned short)tris_c[i][j] );
                    }
                    if ( tris_tc.size() ) {
-                       sgWriteShort( fp, (short)tris_tc[i][j] );
+                       sgWriteUShort( fp, (unsigned short)tris_tc[i][j] );
                    }
                }
            }
@@ -932,8 +882,8 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
            // write group headers
            sgWriteChar( fp, (char)SG_TRIANGLE_STRIPS ); // type
-           sgWriteShort( fp, 2 );                       // nproperties
-           sgWriteShort( fp, end - start );             // nelements
+           sgWriteUShort( fp, 2 );                      // nproperties
+           sgWriteUShort( fp, end - start );             // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );        // property
            sgWriteUInt( fp, material.length() );        // nbytes
@@ -952,19 +902,20 @@ bool SGBinObject::write_bin( const string& base, const string& name,
            // write strips
            for ( i = start; i < end; ++i ) {
                // nbytes
-               sgWriteUInt( fp, strips_v[i].size() * idx_size * sizeof(short));
+               sgWriteUInt( fp, strips_v[i].size() * idx_size
+                                 * sizeof(unsigned short));
                for ( j = 0; j < (int)strips_v[i].size(); ++j ) {
                    if ( strips_v.size() ) { 
-                       sgWriteShort( fp, (short)strips_v[i][j] );
+                       sgWriteUShort( fp, (unsigned short)strips_v[i][j] );
                    }
                    if ( strips_n.size() ) { 
-                       sgWriteShort( fp, (short)strips_n[i][j] );
+                       sgWriteUShort( fp, (unsigned short)strips_n[i][j] );
                    }
                    if ( strips_c.size() ) { 
-                       sgWriteShort( fp, (short)strips_c[i][j] );
+                       sgWriteUShort( fp, (unsigned short)strips_c[i][j] );
                    }
                    if ( strips_tc.size() ) { 
-                       sgWriteShort( fp, (short)strips_tc[i][j] );
+                       sgWriteUShort( fp, (unsigned short)strips_tc[i][j] );
                    }
                }
            }
@@ -992,8 +943,8 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 
            // write group headers
            sgWriteChar( fp, (char)SG_TRIANGLE_FANS );   // type
-           sgWriteShort( fp, 2 );                       // nproperties
-           sgWriteShort( fp, end - start );             // nelements
+           sgWriteUShort( fp, 2 );                      // nproperties
+           sgWriteUShort( fp, end - start );             // nelements
 
            sgWriteChar( fp, (char)SG_MATERIAL );       // property
            sgWriteUInt( fp, material.length() );        // nbytes
@@ -1012,19 +963,20 @@ bool SGBinObject::write_bin( const string& base, const string& name,
            // write fans
            for ( i = start; i < end; ++i ) {
                // nbytes
-               sgWriteUInt( fp, fans_v[i].size() * idx_size * sizeof(short) );
+               sgWriteUInt( fp, fans_v[i].size() * idx_size
+                                 * sizeof(unsigned short) );
                for ( j = 0; j < (int)fans_v[i].size(); ++j ) {
                    if ( fans_v.size() ) {
-                       sgWriteShort( fp, (short)fans_v[i][j] );
+                       sgWriteUShort( fp, (unsigned short)fans_v[i][j] );
                    }
                    if ( fans_n.size() ) {
-                       sgWriteShort( fp, (short)fans_n[i][j] );
+                       sgWriteUShort( fp, (unsigned short)fans_n[i][j] );
                    }
                    if ( fans_c.size() ) {
-                       sgWriteShort( fp, (short)fans_c[i][j] );
+                       sgWriteUShort( fp, (unsigned short)fans_c[i][j] );
                    }
                    if ( fans_tc.size() ) {
-                       sgWriteShort( fp, (short)fans_tc[i][j] );
+                       sgWriteUShort( fp, (unsigned short)fans_tc[i][j] );
                    }
                }
            }
@@ -1052,24 +1004,15 @@ bool SGBinObject::write_bin( const string& base, const string& name,
 bool SGBinObject::write_ascii( const string& base, const string& name,
                               const SGBucket& b )
 {
-    Point3D p;
     int i, j;
 
-    string dir = base + "/" + b.gen_base_path();
-    string command = "mkdir -p " + dir;
-#if defined(_MSC_VER) || defined(__MINGW32__)
-    system( (string("mkdir ") + dir).c_str() );
-#else
-    system(command.c_str());
-#endif
-
-    // string file = dir + "/" + b.gen_index_str();
-    string file = dir + "/" + name;
-    cout << "Output file = " << file << endl;
+    SGPath file = base + "/" + b.gen_base_path() + "/" + name;
+    file.create_dir( 0755 );
+    cout << "Output file = " << file.str() << endl;
 
     FILE *fp;
     if ( (fp = fopen( file.c_str(), "w" )) == NULL ) {
-       cout << "ERROR: opening " << file << " for writing!" << endl;
+       cout << "ERROR: opening " << file.str() << " for writing!" << endl;
        return false;
     }
 
@@ -1102,7 +1045,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
     // dump vertex list
     fprintf(fp, "# vertex list\n");
     for ( i = 0; i < (int)wgs84_nodes.size(); ++i ) {
-       p = wgs84_nodes[i] - gbs_center;
+        SGVec3d p = wgs84_nodes[i] - gbs_center;
        
        fprintf(fp,  "v %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
     }
@@ -1110,7 +1053,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
 
     fprintf(fp, "# vertex normal list\n");
     for ( i = 0; i < (int)normals.size(); ++i ) {
-       p = normals[i];
+        SGVec3f p = normals[i];
        fprintf(fp,  "vn %.5f %.5f %.5f\n", p.x(), p.y(), p.z() );
     }
     fprintf(fp, "\n");
@@ -1118,7 +1061,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
     // dump texture coordinates
     fprintf(fp, "# texture coordinate list\n");
     for ( i = 0; i < (int)texcoords.size(); ++i ) {
-       p = texcoords[i];
+        SGVec2f p = texcoords[i];
        fprintf(fp,  "vt %.5f %.5f\n", p.x(), p.y() );
     }
     fprintf(fp, "\n");
@@ -1141,19 +1084,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();
-           Point3D 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( wgs84_nodes[ tris_v[i][j] ] );
-                   bs_center = calc_center( group_nodes );
-                   bs_radius = sgCalcBoundingRadius( 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());
@@ -1192,18 +1132,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();
-           Point3D 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( wgs84_nodes[ strips_v[i][j] ] );
-                   bs_center = calc_center( group_nodes );
-                   bs_radius = sgCalcBoundingRadius( 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");
@@ -1228,7 +1166,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
     // close the file
     fclose(fp);
 
-    command = "gzip --force --best " + file;
+    string command = "gzip --force --best " + file.str();
     system(command.c_str());
 
     return true;