From f8e4de2d13c81f9f8d6e77da228c43436e296eba Mon Sep 17 00:00:00 2001
From: curt <curt>
Date: Mon, 9 Nov 1998 23:40:46 +0000
Subject: [PATCH] Bernie Bright <bbright@c031.aone.net.au> writes: I've made
 some changes to the Scenery handling.  Basically just tidy ups. The main
 difference is in tile.[ch]xx where I've changed list<fgFRAGMENT> to
 vector<fgFRAGMENT>.  Studying our usage patterns this seems reasonable. Lists
 are good if you need to insert/delete elements randomly but we don't do that.
  All access seems to be sequential.  Two additional benefits are smaller
 memory usage - each list element requires pointers to the next and previous
 elements, and faster access - vector iterators are smaller and faster than
 list iterators.  This should also help Charlie Hotchkiss' problem when
 compiling with Borland and STLport.

./Lib/Bucket/bucketutils.hxx
  Convenience functions for fgBUCKET.

./Simulator/Scenery/tile.cxx
./Simulator/Scenery/tile.hxx
  Changed fragment list to a vector.
  Added some convenience member functions.

./Simulator/Scenery/tilecache.cxx
./Simulator/Scenery/tilecache.hxx
  use const fgBUCKET& instead of fgBUCKET* where appropriate.

./Simulator/Scenery/tilemgr.cxx
./Simulator/Scenery/tilemgr.hxx
  uses all the new convenience functions.
---
 Scenery/tile.cxx      | 463 +++++-------------------------------------
 Scenery/tile.hxx      |  98 +++++++--
 Scenery/tilecache.cxx | 120 +++++------
 Scenery/tilecache.hxx |  61 ++++--
 Scenery/tilemgr.cxx   |  98 +++++----
 5 files changed, 281 insertions(+), 559 deletions(-)

diff --git a/Scenery/tile.cxx b/Scenery/tile.cxx
index 2b55082f8..4efe70697 100644
--- a/Scenery/tile.cxx
+++ b/Scenery/tile.cxx
@@ -22,432 +22,73 @@
 // (Log is kept at end of this file)
 
 
-#include <Include/fg_constants.h>
-#include <Math/mat3.h>
+// #include <Include/fg_constants.h>
+// #include <Math/mat3.h>
+#include <Debug/logstream.hxx>
+#include <Scenery/tile.hxx>
+#include "Bucket/bucketutils.hxx"
 
 #include "tile.hxx"
 
 
-#if 0
-// return the sign of a value
-#define FG_SIGN( x )  ((x) < 0 ? -1 : 1)
-
-// return min or max of two values
-#define FG_MIN(A,B)	((A) < (B) ? (A) :  (B))
-#define FG_MAX(A,B)	((A) > (B) ? (A) :  (B))
-
-
-fgFACE :: fgFACE () : 
-    n1(0), n2(0), n3(0)
-{
-}
-
-fgFACE :: ~fgFACE()
-{
-}
-
-fgFACE :: fgFACE( const fgFACE & image ) :
-    n1( image.n1), n2( image.n2), n3( image.n3)
-{
-}
-
-bool fgFACE :: operator < (const fgFACE & rhs )
-{
-    return ( n1 < rhs.n1 ? true : false);
-}
-
-bool fgFACE :: operator == (const fgFACE & rhs )
-{
-    return ((n1 == rhs.n1) && (n2 == rhs.n2) && ( n3 == rhs.n3));
-}
-
-
 // Constructor
-fgFRAGMENT::fgFRAGMENT ( void ) {
-}
-
-
-// Copy constructor
-fgFRAGMENT ::   fgFRAGMENT ( const fgFRAGMENT & rhs ) :
-    center         ( rhs.center          ),
-    bounding_radius( rhs.bounding_radius ),
-    material_ptr   ( rhs.material_ptr    ),
-    tile_ptr       ( rhs.tile_ptr        ),
-    display_list   ( rhs.display_list    ),
-    faces          ( rhs.faces           ),
-    num_faces      ( rhs.num_faces       )
+fgTILE::fgTILE ( void )
+    : nodes(new double[MAX_NODES][3]),
+      ncount(0),
+      used(false)
 {
 }
 
-fgFRAGMENT & fgFRAGMENT :: operator = ( const fgFRAGMENT & rhs )
-{
-    if(!(this == &rhs )) {
-	center          = rhs.center;
-	bounding_radius = rhs.bounding_radius;
-	material_ptr    = rhs.material_ptr;
-	tile_ptr        = rhs.tile_ptr;
-	// display_list    = rhs.display_list;
-	faces           = rhs.faces;
-    }
-    return *this;
-}
-
-
-// Add a face to the face list
-void fgFRAGMENT::add_face(int n1, int n2, int n3) {
-    fgFACE face;
-
-    face.n1 = n1;
-    face.n2 = n2;
-    face.n3 = n3;
-
-    faces.push_back(face);
-    num_faces++;
-}
-
-
-/*
-// return the sign of a value
-static int fg_sign( double x ) {
-    if ( x >= 0 ) {
-	return(1);
-    } else {
-	return(-1);
-    }
-}
-
-
-// return the minimum of the three values
-static double fg_min( double a, double b, double c ) {
-    double result;
-    result = a;
-    if (result > b) result = b;
-    if (result > c) result = c;
-
-    return(result);
-}
-
-
-// return the maximum of the three values
-static double fg_max( double a, double b, double c ) {
-    double result;
-    result = a;
-    if (result < b) result = b;
-    if (result < c) result = c;
-
-    return(result);
-}
-*/
-
-
-// return the minimum of the three values
-static double fg_min3 (double a, double b, double c)
-{
-    return (a > b ? FG_MIN (b, c) : FG_MIN (a, c));
-}
-
-
-// return the maximum of the three values
-static double fg_max3 (double a, double b, double c)
-{
-  return (a < b ? FG_MAX (b, c) : FG_MAX (a, c));
-}
-
-
-// test if line intesects with this fragment.  p0 and p1 are the two
-// line end points of the line.  If side_flag is true, check to see
-// that end points are on opposite sides of face.  Returns 1 if it
-// intersection found, 0 otherwise.  If it intesects, result is the
-// point of intersection
-
-int fgFRAGMENT::intersect( const Point3D& end0, Point3D& end1, int side_flag,
-			   Point3D& result)
-{
-    fgTILE *t;
-    fgFACE face;
-    MAT3vec v1, v2, n, center;
-    double p1[3], p2[3], p3[3];
-    double x, y, z;  // temporary holding spot for result
-    double a, b, c, d;
-    double x0, y0, z0, x1, y1, z1, a1, b1, c1;
-    double t1, t2, t3;
-    double xmin, xmax, ymin, ymax, zmin, zmax;
-    double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
-    int side1, side2;
-    list < fgFACE > :: iterator current;
-    list < fgFACE > :: iterator last;
-
-    // find the associated tile
-    t = tile_ptr;
-
-    // printf("Intersecting\n");
-
-    // traverse the face list for this fragment
-    current = faces.begin();
-    last = faces.end();
-    while ( current != last ) {
-	face = *current;
-	current++;
-
-	// printf(".");
-
-	// get face vertex coordinates
-	center[0] = t->center.x;
-	center[1] = t->center.y;
-	center[2] = t->center.z;
-
-	MAT3_ADD_VEC(p1, t->nodes[face.n1], center);
-	MAT3_ADD_VEC(p2, t->nodes[face.n2], center);
-	MAT3_ADD_VEC(p3, t->nodes[face.n3], center);
-
-	// printf("point 1 = %.2f %.2f %.2f\n", p1[0], p1[1], p1[2]);
-	// printf("point 2 = %.2f %.2f %.2f\n", p2[0], p2[1], p2[2]);
-	// printf("point 3 = %.2f %.2f %.2f\n", p3[0], p3[1], p3[2]);
-
-	// calculate two edge vectors, and the face normal
-	MAT3_SUB_VEC(v1, p2, p1);
-	MAT3_SUB_VEC(v2, p3, p1);
-	MAT3cross_product(n, v1, v2);
-
-	// calculate the plane coefficients for the plane defined by
-	// this face.  If n is the normal vector, n = (a, b, c) and p1
-	// is a point on the plane, p1 = (x0, y0, z0), then the
-	// equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
-	a = n[0];
-	b = n[1];
-	c = n[2];
-	d = a * p1[0] + b * p1[1] + c * p1[2];
-	// printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
-
-	// printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
-	// printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
-	// printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
-
-	// calculate the line coefficients for the specified line
-	x0 = end0.x();  x1 = end1.x();
-	y0 = end0.y();  y1 = end1.y();
-	z0 = end0.z();  z1 = end1.z();
-
-	if ( fabs(x1 - x0) > FG_EPSILON ) {
-	    a1 = 1.0 / (x1 - x0);
-	} else {
-	    // we got a big divide by zero problem here
-	    a1 = 0.0;
-	}
-	b1 = y1 - y0;
-	c1 = z1 - z0;
-
-	// intersect the specified line with this plane
-	t1 = b * b1 * a1;
-	t2 = c * c1 * a1;
-
-	// printf("a = %.2f  t1 = %.2f  t2 = %.2f\n", a, t1, t2);
-
-	if ( fabs(a + t1 + t2) > FG_EPSILON ) {
-	    x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
-	    t3 = a1 * (x - x0);
-	    y = b1 * t3 + y0;
-	    z = c1 * t3 + z0;	    
-	    // printf("result(d) = %.2f\n", a * x + b * y + c * z);
-	} else {
-	    // no intersection point
-	    continue;
-	}
-
-	if ( side_flag ) {
-	    // check to see if end0 and end1 are on opposite sides of
-	    // plane
-	    if ( (x - x0) > FG_EPSILON ) {
-		t1 = x;
-		t2 = x0;
-		t3 = x1;
-	    } else if ( (y - y0) > FG_EPSILON ) {
-		t1 = y;
-		t2 = y0;
-		t3 = y1;
-	    } else if ( (z - z0) > FG_EPSILON ) {
-		t1 = z;
-		t2 = z0;
-		t3 = z1;
-	    } else {
-		// everything is too close together to tell the difference
-		// so the current intersection point should work as good
-		// as any
-		result->x = x;
-		result->y = y;
-		result->z = z;
-		return(1);
-	    }
-	    side1 = FG_SIGN (t1 - t2);
-	    side2 = FG_SIGN (t1 - t3);
-	    if ( side1 == side2 ) {
-		// same side, punt
-		continue;
-	    }
-	}
-
-	// check to see if intersection point is in the bounding
-	// cube of the face
-#ifdef XTRA_DEBUG_STUFF
-	xmin = fg_min3 (p1[0], p2[0], p3[0]);
-	xmax = fg_max3 (p1[0], p2[0], p3[0]);
-	ymin = fg_min3 (p1[1], p2[1], p3[1]);
-	ymax = fg_max3 (p1[1], p2[1], p3[1]);
-	zmin = fg_min3 (p1[2], p2[2], p3[2]);
-	zmax = fg_max3 (p1[2], p2[2], p3[2]);
-	printf("bounding cube = %.2f,%.2f,%.2f  %.2f,%.2f,%.2f\n",
-	       xmin, ymin, zmin, xmax, ymax, zmax);
-#endif
-	// punt if outside bouding cube
-	if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
-	    continue;
-	} else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
-	    continue;
-	} else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
-	    continue;
-	} else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
-	    continue;
-	} else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
-	    continue;
-	} else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
-	    continue;
-	}
-
-	// (finally) check to see if the intersection point is
-	// actually inside this face
-
-	//first, drop the smallest dimension so we only have to work
-	//in 2d.
-	dx = xmax - xmin;
-	dy = ymax - ymin;
-	dz = zmax - zmin;
-	min_dim = fg_min3 (dx, dy, dz);
-	if ( fabs(min_dim - dx) <= FG_EPSILON ) {
-	    // x is the smallest dimension
-	    x1 = p1[1];
-	    y1 = p1[2];
-	    x2 = p2[1];
-	    y2 = p2[2];
-	    x3 = p3[1];
-	    y3 = p3[2];
-	    rx = y;
-	    ry = z;
-	} else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
-	    // y is the smallest dimension
-	    x1 = p1[0];
-	    y1 = p1[2];
-	    x2 = p2[0];
-	    y2 = p2[2];
-	    x3 = p3[0];
-	    y3 = p3[2];
-	    rx = x;
-	    ry = z;
-	} else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
-	    // z is the smallest dimension
-	    x1 = p1[0];
-	    y1 = p1[1];
-	    x2 = p2[0];
-	    y2 = p2[1];
-	    x3 = p3[0];
-	    y3 = p3[1];
-	    rx = x;
-	    ry = y;
-	} else {
-	    // all dimensions are really small so lets call it close
-	    // enough and return a successful match
-	    result->x = x;
-	    result->y = y;
-	    result->z = z;
-	    return(1);
-	}
-
-	// check if intersection point is on the same side of p1 <-> p2 as p3
-	t1 = (y1 - y2) / (x1 - x2);
-	side1 = FG_SIGN (t1 * ((x3) - x2) + y2 - (y3));
-	side2 = FG_SIGN (t1 * ((rx) - x2) + y2 - (ry));
-	if ( side1 != side2 ) {
-	    // printf("failed side 1 check\n");
-	    continue;
-	}
-
-	// check if intersection point is on correct side of p2 <-> p3 as p1
-	t1 = (y2 - y3) / (x2 - x3);
-	side1 = FG_SIGN (t1 * ((x1) - x3) + y3 - (y1));
-	side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
-	if ( side1 != side2 ) {
-	    // printf("failed side 2 check\n");
-	    continue;
-	}
-
-	// check if intersection point is on correct side of p1 <-> p3 as p2
-	t1 = (y1 - y3) / (x1 - x3);
-	side1 = FG_SIGN (t1 * ((x2) - x3) + y3 - (y2));
-	side2 = FG_SIGN (t1 * ((rx) - x3) + y3 - (ry));
-	if ( side1 != side2 ) {
-	    // printf("failed side 3  check\n");
-	    continue;
-	}
-
-	// printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
-	result->x = x;
-	result->y = y;
-	result->z = z;
-	return(1);
-    }
-
-    // printf("\n");
-
-    return(0);
-}
-
-
-// Destructor
-fgFRAGMENT::~fgFRAGMENT ( void ) {
-    // Step through the face list deleting the items until the list is
-    // empty
-
-    // printf("destructing a fragment with %d faces\n", faces.size());
-
-    faces.erase( faces.begin(), faces.end() );
-}
-
-
-// equality operator
-bool  fgFRAGMENT :: operator == ( const fgFRAGMENT & rhs)
-{
-    if(( center.x - rhs.center.x ) < FG_EPSILON) {
-	if(( center.y - rhs.center.y) < FG_EPSILON) {
-	    if(( center.z - rhs.center.z) < FG_EPSILON) {
-		return true;
-	    }
-	}
-    }
-    return false;
-}
-
-// comparison operator
-bool  fgFRAGMENT :: operator < ( const fgFRAGMENT &rhs)
-{
-    // This is completely arbitrary. It satisfies RW's STL implementation
-
-    return bounding_radius < rhs.bounding_radius;
-}
-#endif
-
-
-// Constructor
-fgTILE::fgTILE ( void ) {
-    nodes = new double[MAX_NODES][3];
-}
-
 
 // Destructor
 fgTILE::~fgTILE ( void ) {
-    free(nodes);
+//     free(nodes);
+    delete[] nodes;
+}
+
+// Step through the fragment list, deleting the display list, then
+// the fragment, until the list is empty.
+void
+fgTILE::release_fragments()
+{
+    FG_LOG( FG_TERRAIN, FG_DEBUG,
+	    "FREEING TILE = (" << tile_bucket << ")" );
+    for_each( begin(), end(),
+	      mem_fun_ref( &fgFRAGMENT::deleteDisplayList ));
+    fragment_list.erase( begin(), end() );
+    used = false;
 }
 
 
 // $Log$
+// Revision 1.13  1998/11/09 23:40:46  curt
+// Bernie Bright <bbright@c031.aone.net.au> writes:
+// I've made some changes to the Scenery handling.  Basically just tidy ups.
+// The main difference is in tile.[ch]xx where I've changed list<fgFRAGMENT> to
+// vector<fgFRAGMENT>.  Studying our usage patterns this seems reasonable.
+// Lists are good if you need to insert/delete elements randomly but we
+// don't do that.  All access seems to be sequential.  Two additional
+// benefits are smaller memory usage - each list element requires pointers
+// to the next and previous elements, and faster access - vector iterators
+// are smaller and faster than list iterators.  This should also help
+// Charlie Hotchkiss' problem when compiling with Borland and STLport.
+//
+// ./Lib/Bucket/bucketutils.hxx
+//   Convenience functions for fgBUCKET.
+//
+// ./Simulator/Scenery/tile.cxx
+// ./Simulator/Scenery/tile.hxx
+//   Changed fragment list to a vector.
+//   Added some convenience member functions.
+//
+// ./Simulator/Scenery/tilecache.cxx
+// ./Simulator/Scenery/tilecache.hxx
+//   use const fgBUCKET& instead of fgBUCKET* where appropriate.
+//
+// ./Simulator/Scenery/tilemgr.cxx
+// ./Simulator/Scenery/tilemgr.hxx
+//   uses all the new convenience functions.
+//
 // Revision 1.12  1998/10/16 00:55:45  curt
 // Converted to Point3D class.
 //
diff --git a/Scenery/tile.hxx b/Scenery/tile.hxx
index d0bd74281..c3a2c1237 100644
--- a/Scenery/tile.hxx
+++ b/Scenery/tile.hxx
@@ -41,29 +41,32 @@
 #include <GL/glut.h>
 #include <XGL/xgl.h>
 
-#if defined ( __sun__ )
-extern "C" void *memmove(void *, const void *, size_t);
-extern "C" void *memset(void *, int, size_t);
-#endif
+#include <vector>
+#include <string>
 
-#include <list>         // STL list
+#include "Include/compiler.h"
+#include STL_FUNCTIONAL
+#include STL_ALGORITHM
+FG_USING_NAMESPACE(std);
+// FG_USING_STD(string);
+// FG_USING_STD(vector);
 
 #include <Bucket/bucketutils.h>
-// #include <Include/fg_types.h>
 #include <Math/mat3.h>
 #include <Math/point3d.hxx>
 #include <Objects/fragment.hxx>
 
-#ifdef NEEDNAMESPACESTD
-using namespace std;
-#endif
-
 
 // Scenery tile class
 class fgTILE {
 
 public:
 
+    typedef vector < fgFRAGMENT > container;
+    typedef container::iterator FragmentIterator;
+    typedef container::const_iterator FragmentConstIterator;
+
+public:
     // node list (the per fragment face lists reference this node list)
     double (*nodes)[3];
     int ncount;
@@ -78,9 +81,33 @@ public:
     fgBUCKET tile_bucket;
 
     // the tile cache will mark here if the tile is being used
-    int used;
+    bool used;
 
-    list < fgFRAGMENT > fragment_list;
+    container fragment_list;
+
+public:
+
+    FragmentIterator begin() { return fragment_list.begin(); }
+    FragmentConstIterator begin() const { return fragment_list.begin(); }
+
+    FragmentIterator end() { return fragment_list.end(); }
+    FragmentConstIterator end() const { return fragment_list.end(); }
+
+    void add_fragment( fgFRAGMENT& frag ) {
+ 	frag.tile_ptr = this;
+	fragment_list.push_back( frag );
+    }
+
+    //
+    size_t num_fragments() const {
+	return fragment_list.size();
+    }
+
+    // Step through the fragment list, deleting the display list, then
+    // the fragment, until the list is empty.
+    void release_fragments();
+
+//     int ObjLoad( const string& path, const fgBUCKET& p );
 
     // Constructor
     fgTILE ( void );
@@ -89,8 +116,7 @@ public:
     ~fgTILE ( void );
 
     // Calculate this tile's offset
-    void
-    fgTILE::SetOffset( const Point3D& off)
+    void SetOffset( const Point3D& off)
     {
 	offset = center - off;
     }
@@ -98,7 +124,7 @@ public:
 
     // Calculate the model_view transformation matrix for this tile
     inline void
-    fgTILE::UpdateViewMatrix(GLdouble *MODEL_VIEW)
+    UpdateViewMatrix(GLdouble *MODEL_VIEW)
     {
 
 #ifdef WIN32
@@ -108,17 +134,25 @@ public:
 #endif
 	
 	// This is equivalent to doing a glTranslatef(x, y, z);
-	model_view[12] += (model_view[0]*offset.x() + model_view[4]*offset.y() +
+	model_view[12] += (model_view[0]*offset.x() +
+			   model_view[4]*offset.y() +
 			   model_view[8]*offset.z());
-	model_view[13] += (model_view[1]*offset.x() + model_view[5]*offset.y() +
+	model_view[13] += (model_view[1]*offset.x() +
+			   model_view[5]*offset.y() +
 			   model_view[9]*offset.z());
-	model_view[14] += (model_view[2]*offset.x() + model_view[6]*offset.y() +
+	model_view[14] += (model_view[2]*offset.x() +
+			   model_view[6]*offset.y() +
 			   model_view[10]*offset.z() );
 	// m[15] += (m[3]*x + m[7]*y + m[11]*z);
 	// m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
 	// so m[15] is unchanged
     }
 
+private:
+
+    // not defined
+    fgTILE( const fgTILE& );
+    fgTILE& operator = ( const fgTILE& );
 };
 
 
@@ -126,6 +160,34 @@ public:
 
 
 // $Log$
+// Revision 1.21  1998/11/09 23:40:47  curt
+// Bernie Bright <bbright@c031.aone.net.au> writes:
+// I've made some changes to the Scenery handling.  Basically just tidy ups.
+// The main difference is in tile.[ch]xx where I've changed list<fgFRAGMENT> to
+// vector<fgFRAGMENT>.  Studying our usage patterns this seems reasonable.
+// Lists are good if you need to insert/delete elements randomly but we
+// don't do that.  All access seems to be sequential.  Two additional
+// benefits are smaller memory usage - each list element requires pointers
+// to the next and previous elements, and faster access - vector iterators
+// are smaller and faster than list iterators.  This should also help
+// Charlie Hotchkiss' problem when compiling with Borland and STLport.
+//
+// ./Lib/Bucket/bucketutils.hxx
+//   Convenience functions for fgBUCKET.
+//
+// ./Simulator/Scenery/tile.cxx
+// ./Simulator/Scenery/tile.hxx
+//   Changed fragment list to a vector.
+//   Added some convenience member functions.
+//
+// ./Simulator/Scenery/tilecache.cxx
+// ./Simulator/Scenery/tilecache.hxx
+//   use const fgBUCKET& instead of fgBUCKET* where appropriate.
+//
+// ./Simulator/Scenery/tilemgr.cxx
+// ./Simulator/Scenery/tilemgr.hxx
+//   uses all the new convenience functions.
+//
 // Revision 1.20  1998/10/16 00:55:46  curt
 // Converted to Point3D class.
 //
diff --git a/Scenery/tilecache.cxx b/Scenery/tilecache.cxx
index 993e28551..10157e45b 100644
--- a/Scenery/tilecache.cxx
+++ b/Scenery/tilecache.cxx
@@ -34,7 +34,7 @@
 #include <XGL/xgl.h>
 
 #include <Airports/genapt.hxx>
-#include <Bucket/bucketutils.h>
+#include <Bucket/bucketutils.hxx>
 #include <Debug/logstream.hxx>
 #include <Main/options.hxx>
 #include <Main/views.hxx>
@@ -69,21 +69,15 @@ fgTILECACHE::init( void )
 
 // Search for the specified "bucket" in the cache
 int
-fgTILECACHE::exists( fgBUCKET *p )
+fgTILECACHE::exists( const fgBUCKET& p )
 {
     int i;
 
     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
-	if ( tile_cache[i].tile_bucket.lon == p->lon ) {
-	    if ( tile_cache[i].tile_bucket.lat == p->lat ) {
-		if ( tile_cache[i].tile_bucket.x == p->x ) {
-		    if ( tile_cache[i].tile_bucket.y == p->y ) {
-			FG_LOG( FG_TERRAIN, FG_DEBUG, 
-				"TILE EXISTS in cache ... index = " << i );
-			return( i );
-		    }
-		}
-	    }
+	if ( tile_cache[i].tile_bucket == p ) {
+	    FG_LOG( FG_TERRAIN, FG_DEBUG, 
+		    "TILE EXISTS in cache ... index = " << i );
+	    return( i );
 	}
     }
     
@@ -93,34 +87,22 @@ fgTILECACHE::exists( fgBUCKET *p )
 
 // Fill in a tile cache entry with real data for the specified bucket
 void
-fgTILECACHE::fill_in( int index, fgBUCKET *p )
+fgTILECACHE::fill_in( int index, const fgBUCKET& p )
 {
-    string root, tile_path, apt_path;
-    char index_str[256];
-    char base_path[256];
+    // Load the appropriate data file and build tile fragment list
+    string tile_path = current_options.get_fg_root() +
+	"/Scenery/" + fgBucketGenBasePath(p) + "/" + fgBucketGenIndex(p);
 
-    // Mark this cache entry as used
-    tile_cache[index].used = 1;
-
-    // Update the bucket
-    tile_cache[index].tile_bucket.lon = p->lon;
-    tile_cache[index].tile_bucket.lat = p->lat;
-    tile_cache[index].tile_bucket.x = p->x;
-    tile_cache[index].tile_bucket.y = p->y;
-
-    // Load the appropriate data file and built tile fragment list
-    fgBucketGenBasePath(p, base_path);
-    root = current_options.get_fg_root();
-    sprintf( index_str, "%ld", fgBucketGenIndex(p) );
-
-    tile_path = root + "/Scenery/" + base_path + "/" + index_str;
-    fgObjLoad( tile_path.c_str(), &tile_cache[index] );
+    tile_cache[index].used = true;
+    tile_cache[index].tile_bucket = p;
+    fgObjLoad( tile_path, &tile_cache[index] );
+//     tile_cache[ index ].ObjLoad( tile_path, p );
 
     // cout << " ncount before = " << tile_cache[index].ncount << "\n";
     // cout << " fragments before = " << tile_cache[index].fragment_list.size()
     //      << "\n";
 
-    apt_path = tile_path + ".apt";
+    string apt_path = tile_path + ".apt";
     fgAptGenerate( apt_path, &tile_cache[index] );
 
     // cout << " ncount after = " << tile_cache[index].ncount << "\n";
@@ -133,37 +115,7 @@ fgTILECACHE::fill_in( int index, fgBUCKET *p )
 void
 fgTILECACHE::entry_free( int index )
 {
-    fgFRAGMENT *fragment;
-
-    // Mark this cache entry as un-used
-    tile_cache[index].used = 0;
-
-    // Update the bucket
-    FG_LOG( FG_TERRAIN, FG_DEBUG, 
-	    "FREEING TILE = ("
-	    << tile_cache[index].tile_bucket.lon << " "
-	    << tile_cache[index].tile_bucket.lat << " "
-	    << tile_cache[index].tile_bucket.x << " "
-	    << tile_cache[index].tile_bucket.y << ")" );
-
-    // Step through the fragment list, deleting the display list, then
-    // the fragment, until the list is empty.
-    while ( tile_cache[index].fragment_list.size() ) {
-	list < fgFRAGMENT > :: iterator current =
-	    tile_cache[index].fragment_list.begin();
-	fragment = &(*current);
-	xglDeleteLists( fragment->display_list, 1 );
-
-	tile_cache[index].fragment_list.pop_front();
-    }
-}
-
-
-// Return the specified tile cache entry 
-fgTILE *
-fgTILECACHE::get_tile( int index )
-{
-    return ( &tile_cache[index] );
+    tile_cache[index].release_fragments();
 }
 
 
@@ -184,20 +136,14 @@ fgTILECACHE::next_avail( void )
     max_index = 0;
 
     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
-	if ( tile_cache[i].used == 0 ) {
+	if ( ! tile_cache[i].used ) {
 	    return(i);
 	} else {
 	    // calculate approximate distance from view point
 	    FG_LOG( FG_TERRAIN, FG_DEBUG,
-		    "DIST Abs view pos = "
-		    << v->abs_view_pos.x() << ", "
-		    << v->abs_view_pos.y() << ", "
-		    << v->abs_view_pos.z() );
+		    "DIST Abs view pos = " << v->abs_view_pos );
 	    FG_LOG( FG_TERRAIN, FG_DEBUG,
-		    "    ref point = "
-		    << tile_cache[i].center.x() << ", "
-		    << tile_cache[i].center.y() << ", "
-		    << tile_cache[i].center.z() );
+		    "    ref point = " << tile_cache[i].center );
 
 	    delta.setx( fabs(tile_cache[i].center.x() - v->abs_view_pos.x() ) );
 	    delta.sety( fabs(tile_cache[i].center.y() - v->abs_view_pos.y() ) );
@@ -236,6 +182,34 @@ fgTILECACHE::~fgTILECACHE( void ) {
 
 
 // $Log$
+// Revision 1.20  1998/11/09 23:40:49  curt
+// Bernie Bright <bbright@c031.aone.net.au> writes:
+// I've made some changes to the Scenery handling.  Basically just tidy ups.
+// The main difference is in tile.[ch]xx where I've changed list<fgFRAGMENT> to
+// vector<fgFRAGMENT>.  Studying our usage patterns this seems reasonable.
+// Lists are good if you need to insert/delete elements randomly but we
+// don't do that.  All access seems to be sequential.  Two additional
+// benefits are smaller memory usage - each list element requires pointers
+// to the next and previous elements, and faster access - vector iterators
+// are smaller and faster than list iterators.  This should also help
+// Charlie Hotchkiss' problem when compiling with Borland and STLport.
+//
+// ./Lib/Bucket/bucketutils.hxx
+//   Convenience functions for fgBUCKET.
+//
+// ./Simulator/Scenery/tile.cxx
+// ./Simulator/Scenery/tile.hxx
+//   Changed fragment list to a vector.
+//   Added some convenience member functions.
+//
+// ./Simulator/Scenery/tilecache.cxx
+// ./Simulator/Scenery/tilecache.hxx
+//   use const fgBUCKET& instead of fgBUCKET* where appropriate.
+//
+// ./Simulator/Scenery/tilemgr.cxx
+// ./Simulator/Scenery/tilemgr.hxx
+//   uses all the new convenience functions.
+//
 // Revision 1.19  1998/11/06 21:18:21  curt
 // Converted to new logstream debugging facility.  This allows release
 // builds with no messages at all (and no performance impact) by using
diff --git a/Scenery/tilecache.hxx b/Scenery/tilecache.hxx
index 473ed1b51..742921e3e 100644
--- a/Scenery/tilecache.hxx
+++ b/Scenery/tilecache.hxx
@@ -43,7 +43,6 @@
 #include <XGL/xgl.h>
 
 #include <Bucket/bucketutils.h>
-// #include <Include/fg_types.h>
 #include <Math/point3d.hxx>
 
 #include "tile.hxx"
@@ -54,24 +53,18 @@
 // FG_TILE_CACHE_SIZE >= (o->tile_diameter + 1) ** 2 
 #define FG_TILE_CACHE_SIZE 121
 
-
-/*
-// Tile cache record 
-typedef struct {
-    fgBUCKET tile_bucket;
-    GLint display_list;
-    fgCartesianPoint3d local_ref;
-    double bounding_radius;
-    int used;
-    int priority;
-} fgTILE;
-*/
-
-
 // A class to store and manage a pile of tiles
 class fgTILECACHE {
+
+//     enum
+//     {
+// 	// For best results... i.e. to avoid tile load problems and blank areas
+// 	// FG_TILE_CACHE_SIZE >= (o->tile_diameter + 1) ** 2 
+// 	FG_TILE_CACHE_SIZE = 121
+//     };
+
     // cache storage space
-    fgTILE tile_cache[FG_TILE_CACHE_SIZE];
+    fgTILE tile_cache[ FG_TILE_CACHE_SIZE ];
 
 public:
 
@@ -82,7 +75,7 @@ public:
     void init( void );
 
     // Search for the specified "bucket" in the cache 
-    int exists( fgBUCKET *p );
+    int exists( const fgBUCKET& p );
 
     // Return index of next available slot in tile cache 
     int next_avail( void );
@@ -91,10 +84,12 @@ public:
     void entry_free( int index );
 
     // Fill in a tile cache entry with real data for the specified bucket 
-    void fill_in( int index, fgBUCKET *p );
+    void fill_in( int index, const fgBUCKET& p );
 
     // Return a pointer to the specified tile cache entry 
-    fgTILE *get_tile( int index );
+    fgTILE *get_tile( int index ) {
+	return &tile_cache[index];
+    }
 
     // Destructor
     ~fgTILECACHE( void );
@@ -109,6 +104,34 @@ extern fgTILECACHE global_tile_cache;
 
 
 // $Log$
+// Revision 1.13  1998/11/09 23:40:51  curt
+// Bernie Bright <bbright@c031.aone.net.au> writes:
+// I've made some changes to the Scenery handling.  Basically just tidy ups.
+// The main difference is in tile.[ch]xx where I've changed list<fgFRAGMENT> to
+// vector<fgFRAGMENT>.  Studying our usage patterns this seems reasonable.
+// Lists are good if you need to insert/delete elements randomly but we
+// don't do that.  All access seems to be sequential.  Two additional
+// benefits are smaller memory usage - each list element requires pointers
+// to the next and previous elements, and faster access - vector iterators
+// are smaller and faster than list iterators.  This should also help
+// Charlie Hotchkiss' problem when compiling with Borland and STLport.
+//
+// ./Lib/Bucket/bucketutils.hxx
+//   Convenience functions for fgBUCKET.
+//
+// ./Simulator/Scenery/tile.cxx
+// ./Simulator/Scenery/tile.hxx
+//   Changed fragment list to a vector.
+//   Added some convenience member functions.
+//
+// ./Simulator/Scenery/tilecache.cxx
+// ./Simulator/Scenery/tilecache.hxx
+//   use const fgBUCKET& instead of fgBUCKET* where appropriate.
+//
+// ./Simulator/Scenery/tilemgr.cxx
+// ./Simulator/Scenery/tilemgr.hxx
+//   uses all the new convenience functions.
+//
 // Revision 1.12  1998/10/16 00:55:49  curt
 // Converted to Point3D class.
 //
diff --git a/Scenery/tilemgr.cxx b/Scenery/tilemgr.cxx
index f3aa2d3b4..12d8952a5 100644
--- a/Scenery/tilemgr.cxx
+++ b/Scenery/tilemgr.cxx
@@ -35,7 +35,7 @@
 
 #include <Aircraft/aircraft.hxx>
 
-#include <Bucket/bucketutils.h>
+#include <Bucket/bucketutils.hxx>
 #include <Debug/logstream.hxx>
 #include <Include/fg_constants.h>
 #include <Main/options.hxx>
@@ -88,22 +88,21 @@ int fgTileMgrInit( void ) {
 
 
 // load a tile
-void fgTileMgrLoadTile( fgBUCKET *p, int *index) {
+void fgTileMgrLoadTile( const fgBUCKET& p, int *index) {
     fgTILECACHE *c;
 
     c = &global_tile_cache;
 
-    FG_LOG( FG_TERRAIN, FG_DEBUG, 
-	    "Updating for bucket "
-	    << p->lon << " " << p->lat << " " << p->x << " " << p->y );
+    FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating for bucket " << p );
     
     // if not in cache, load tile into the next available slot
-    if ( (*index = c->exists(p)) < 0 ) {
+    *index = c->exists(p);
+    if ( *index < 0 ) {
 	*index = c->next_avail();
 	c->fill_in(*index, p);
     }
 
-    FG_LOG( FG_TERRAIN, FG_DEBUG, "Selected cache index of " << *index);
+    FG_LOG( FG_TERRAIN, FG_DEBUG, "Selected cache index: " << *index );
 }
 
 
@@ -126,8 +125,7 @@ int fgTileMgrUpdate( void ) {
     dw = tile_diameter / 2;
     dh = tile_diameter / 2;
 
-    if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
-	 (p1.x == p_last.x) && (p1.y == p_last.y) ) {
+    if ( p1 == p_last ) {
 	// same bucket as last time
 	FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
     } else if ( p_last.lon == -1000 ) {
@@ -135,12 +133,9 @@ int fgTileMgrUpdate( void ) {
 	// relavant tiles
 
 	FG_LOG( FG_TERRAIN, FG_INFO, "  First time through ... " );
-	FG_LOG( FG_TERRAIN, FG_INFO, 
-		"  Updating Tile list for "
-		<< p1.lon << "," << p1.lat << " " << p1.x << "," << p1.y );
+	FG_LOG( FG_TERRAIN, FG_INFO, "  Updating Tile list for " << p1 );
 	FG_LOG( FG_TERRAIN, FG_INFO, "  Loading " 
-		<< tile_diameter * tile_diameter
-		<< " tiles" );
+		<< tile_diameter * tile_diameter << " tiles" );
 
 	// wipe/initialize tile cache
 	c->init();
@@ -149,7 +144,7 @@ int fgTileMgrUpdate( void ) {
 	for ( j = 0; j < tile_diameter; j++ ) {
 	    for ( i = 0; i < tile_diameter; i++ ) {
 		fgBucketOffset(&p1, &p2, i - dw, j - dh);
-		fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + i]);
+		fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + i]);
 	    }
 	}
     } else {
@@ -160,9 +155,7 @@ int fgTileMgrUpdate( void ) {
 	// AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
 	// THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
 
-	FG_LOG( FG_TERRAIN, FG_INFO, 
-		"Updating Tile list for "
-		<< p1.lon << "," << p1.lat << " " << p1.x << "," << p1.y );
+	FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
 
 	if ( (p1.lon > p_last.lon) ||
 	     ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
@@ -176,7 +169,7 @@ int fgTileMgrUpdate( void ) {
 		}
 		// load in new column
 		fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
-		fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + 
+		fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + 
 					     tile_diameter - 1]);
 	    }
 	} else if ( (p1.lon < p_last.lon) ||
@@ -191,7 +184,7 @@ int fgTileMgrUpdate( void ) {
 		}
 		// load in new column
 		fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
-		fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + 0]);
+		fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + 0]);
 	    }
 	}
 
@@ -207,7 +200,7 @@ int fgTileMgrUpdate( void ) {
 		}
 		// load in new column
 		fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
-		fgTileMgrLoadTile(&p2, &tiles[((tile_diameter-1) * 
+		fgTileMgrLoadTile( p2, &tiles[((tile_diameter-1) * 
 					       tile_diameter) + i]);
 	    }
 	} else if ( (p1.lat < p_last.lat) ||
@@ -222,7 +215,7 @@ int fgTileMgrUpdate( void ) {
 		}
 		// load in new column
 		fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
-		fgTileMgrLoadTile(&p2, &tiles[0 + i]);
+		fgTileMgrLoadTile( p2, &tiles[0 + i]);
 	    }
 	}
     }
@@ -409,17 +402,16 @@ inrange( const double radius, const Point3D& center, const Point3D& vp,
 // render the scene, but we'd also like to be able to do this
 // explicitely.  lat & lon are in radians.  abs_view_pos in meters.
 // Returns result in meters.
-double fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
+double
+fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
     fgTILECACHE *c;
     fgTILE *t;
     // fgVIEW *v;
     fgFRAGMENT *frag_ptr;
     fgBUCKET p;
-    Point3D earth_center, result;
-    Point3D pp;
+    Point3D earth_center(0.0);
+    Point3D result;
     MAT3vec local_up;
-    list < fgFRAGMENT > :: iterator current;
-    list < fgFRAGMENT > :: iterator last;
     double dist, lat_geod, alt, sea_level_r;
     // double x, y, z;
     int index;
@@ -433,17 +425,21 @@ double fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
 
     // Find current translation offset
     fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
-    index = c->exists(&p);
+    index = c->exists(p);
+    if ( index < 0 ) {
+	FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
+	return 0.0;
+    }
+
     t = c->get_tile(index);
 
     scenery.next_center = t->center;
     
-    earth_center = Point3D(0.0, 0.0, 0.0);
+    // earth_center = Point3D(0.0, 0.0, 0.0);
 
     FG_LOG( FG_TERRAIN, FG_DEBUG, 
 	    "Pos = (" << lon * RAD_TO_DEG << ", " << lat * RAD_TO_DEG
-	    << "  Current bucket = " 
-	    << p.lon << " " << p.lat << " " << p.x << " " << p.y
+	    << ")  Current bucket = " << p 
 	    << "  Index = " << fgBucketGenIndex(&p) );
 
     // calculate tile offset
@@ -463,8 +459,8 @@ double fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
     if ( dist < FG_SQUARE(t->bounding_radius) ) {
 
 	// traverse fragment list for tile
-	current = t->fragment_list.begin();
-	last = t->fragment_list.end();
+        fgTILE::FragmentIterator current = t->begin();
+        fgTILE::FragmentIterator last = t->end();
 
 	for ( ; current != last; ++current ) {
 	    frag_ptr = &(*current);
@@ -480,7 +476,7 @@ double fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
 		if ( frag_ptr->intersect( abs_view_pos, 
 					  earth_center, 0, result ) ) {
 		    // compute geocentric coordinates of tile center
-		    pp = fgCartToPolar3d(result);
+		    Point3D pp = fgCartToPolar3d(result);
 		    // convert to geodetic coordinates
 		    fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod, 
 				 &alt, &sea_level_r);
@@ -538,8 +534,6 @@ void fgTileMgrRender( void ) {
     Point3D frag_offset;
     fgFRAGMENT *frag_ptr;
     fgMATERIAL *mtl_ptr;
-    list < fgFRAGMENT > :: iterator current;
-    list < fgFRAGMENT > :: iterator last;
     int i;
     int tile_diameter;
     int index;
@@ -580,8 +574,8 @@ void fgTileMgrRender( void ) {
 	    // xglTranslatef(t->offset.x, t->offset.y, t->offset.z);
 
 	    // traverse fragment list for tile
-	    current = t->fragment_list.begin();
-	    last = t->fragment_list.end();
+            fgTILE::FragmentIterator current = t->begin();
+            fgTILE::FragmentIterator last = t->end();
 
 	    for ( ; current != last; ++current ) {
 		frag_ptr = &(*current);
@@ -638,6 +632,34 @@ void fgTileMgrRender( void ) {
 
 
 // $Log$
+// Revision 1.43  1998/11/09 23:40:52  curt
+// Bernie Bright <bbright@c031.aone.net.au> writes:
+// I've made some changes to the Scenery handling.  Basically just tidy ups.
+// The main difference is in tile.[ch]xx where I've changed list<fgFRAGMENT> to
+// vector<fgFRAGMENT>.  Studying our usage patterns this seems reasonable.
+// Lists are good if you need to insert/delete elements randomly but we
+// don't do that.  All access seems to be sequential.  Two additional
+// benefits are smaller memory usage - each list element requires pointers
+// to the next and previous elements, and faster access - vector iterators
+// are smaller and faster than list iterators.  This should also help
+// Charlie Hotchkiss' problem when compiling with Borland and STLport.
+//
+// ./Lib/Bucket/bucketutils.hxx
+//   Convenience functions for fgBUCKET.
+//
+// ./Simulator/Scenery/tile.cxx
+// ./Simulator/Scenery/tile.hxx
+//   Changed fragment list to a vector.
+//   Added some convenience member functions.
+//
+// ./Simulator/Scenery/tilecache.cxx
+// ./Simulator/Scenery/tilecache.hxx
+//   use const fgBUCKET& instead of fgBUCKET* where appropriate.
+//
+// ./Simulator/Scenery/tilemgr.cxx
+// ./Simulator/Scenery/tilemgr.hxx
+//   uses all the new convenience functions.
+//
 // Revision 1.42  1998/11/06 21:18:23  curt
 // Converted to new logstream debugging facility.  This allows release
 // builds with no messages at all (and no performance impact) by using