From 37b4005d3e117ac9b65d7a83fa69bd29e8092e63 Mon Sep 17 00:00:00 2001
From: ehofman <ehofman>
Date: Tue, 25 Oct 2005 13:48:58 +0000
Subject: [PATCH] Alex Romosan:

* Use "const string&" rather than "string" in function calls when appropriate.
* Use "const Point3D&" instead of "Pint3D" in function calls when appropriate.
* Improved course calculation in calc_gc_course_dist()
* Safer thread handling code.

Vassilii Khachaturov:

Dont use "const Point3D&" for return types unless you're absolutely sure.

Erik Hofman:

* Use SGD_(2)PI(_[24]) as defined in simgear/constants.h rather than
  calculating it by hand every time.
---
 simgear/bucket/newbucket.cxx  |   4 +-
 simgear/environment/metar.hxx |  18 +--
 simgear/ephemeris/moonpos.cxx |   2 +-
 simgear/math/fastmath.cxx     |   2 +-
 simgear/math/point3d.hxx      |   6 +-
 simgear/math/polar3d.cxx      | 232 +++++++++++++++++++++++++++++++++-
 simgear/math/polar3d.hxx      | 170 ++-----------------------
 simgear/misc/texcoord.cxx     |   4 +-
 simgear/route/waypoint.hxx    |   4 +-
 simgear/scene/sky/sky.cxx     |   4 +-
 simgear/scene/sky/sphere.cxx  |   3 +-
 simgear/scene/sky/stars.cxx   |  15 ++-
 simgear/threads/SGThread.hxx  |  13 +-
 simgear/timing/sg_time.cxx    |   2 +-
 14 files changed, 282 insertions(+), 197 deletions(-)

diff --git a/simgear/bucket/newbucket.cxx b/simgear/bucket/newbucket.cxx
index e3c34387..9a913628 100644
--- a/simgear/bucket/newbucket.cxx
+++ b/simgear/bucket/newbucket.cxx
@@ -208,7 +208,7 @@ double SGBucket::get_width_m() const {
     double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
     double cos_lat = cos( clat_rad );
     double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
-    double local_perimeter = 2.0 * local_radius * SGD_PI;
+    double local_perimeter = local_radius * SGD_2PI;
     double degree_width = local_perimeter / 360.0;
 
     return sg_bucket_span( get_center_lat() ) * degree_width;
@@ -217,7 +217,7 @@ double SGBucket::get_width_m() const {
 
 // return height of the tile in meters
 double SGBucket::get_height_m() const {
-    double perimeter = 2.0 * SG_EQUATORIAL_RADIUS_M * SGD_PI;
+    double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
     double degree_height = perimeter / 360.0;
 
     return SG_BUCKET_SPAN * degree_height;
diff --git a/simgear/environment/metar.hxx b/simgear/environment/metar.hxx
index 631d3e31..a446152d 100644
--- a/simgear/environment/metar.hxx
+++ b/simgear/environment/metar.hxx
@@ -108,8 +108,8 @@ public:
 	inline const char		*getFrictionString()	const { return _friction_string; }
 	inline const char		*getComment()		const { return _comment; }
 	inline const bool		getWindShear()		const { return _wind_shear; }
-	inline SGMetarVisibility	getMinVisibility()	const { return _min_visibility; }
-	inline SGMetarVisibility	getMaxVisibility()	const { return _max_visibility; }
+	inline const SGMetarVisibility&	getMinVisibility()	const { return _min_visibility; }
+	inline const SGMetarVisibility&	getMaxVisibility()	const { return _max_visibility; }
 
 protected:
 	SGMetarVisibility _min_visibility;
@@ -190,10 +190,10 @@ public:
 	inline int	getWindRangeFrom()	const { return _wind_range_from; }
 	inline int	getWindRangeTo()	const { return _wind_range_to; }
 
-	inline SGMetarVisibility& getMinVisibility()	{ return _min_visibility; }
-	inline SGMetarVisibility& getMaxVisibility()	{ return _max_visibility; }
-	inline SGMetarVisibility& getVertVisibility()	{ return _vert_visibility; }
-	inline SGMetarVisibility *getDirVisibility()	{ return _dir_visibility; }
+	inline const SGMetarVisibility& getMinVisibility()	const { return _min_visibility; }
+	inline const SGMetarVisibility& getMaxVisibility()	const { return _max_visibility; }
+	inline const SGMetarVisibility& getVertVisibility()	const { return _vert_visibility; }
+	inline const SGMetarVisibility *getDirVisibility()	const { return _dir_visibility; }
 
 	inline double	getTemperature_C()	const { return _temp; }
 	inline double	getTemperature_F()	const { return _temp == NaN ? NaN : 1.8 * _temp + 32; }
@@ -209,9 +209,9 @@ public:
 
 	double		getRelHumidity()	const;
 
-	inline vector<SGMetarCloud>& getClouds()	{ return _clouds; }
-	inline map<string, SGMetarRunway>& getRunways()	{ return _runways; }
-	inline vector<string>& getWeather()		{ return _weather; }
+	inline const vector<SGMetarCloud>& getClouds()	const	{ return _clouds; }
+	inline const map<string, SGMetarRunway>& getRunways()	const	{ return _runways; }
+	inline const vector<string>& getWeather()		const	{ return _weather; }
 
 protected:
 	string	_url;
diff --git a/simgear/ephemeris/moonpos.cxx b/simgear/ephemeris/moonpos.cxx
index 47696927..9ff25518 100644
--- a/simgear/ephemeris/moonpos.cxx
+++ b/simgear/ephemeris/moonpos.cxx
@@ -172,7 +172,7 @@ void MoonPos::updatePosition(double mjd, double lst, double lat, Star *ourSun)
   // SG_LOG( SG_GENERAL, SG_INFO, "rho = " << rho );
   
   if (geoRa < 0)
-    geoRa += (2*SGD_PI);
+    geoRa += SGD_2PI;
   
   HA = lst - (3.8197186 * geoRa);
   /* SG_LOG( SG_GENERAL, SG_INFO, "t->getLst() = " << t->getLst() 
diff --git a/simgear/math/fastmath.cxx b/simgear/math/fastmath.cxx
index 8f0eed29..f7379bd5 100644
--- a/simgear/math/fastmath.cxx
+++ b/simgear/math/fastmath.cxx
@@ -27,9 +27,9 @@
  */
 
 
+#include <simgear/constants.h>
 
 #include "fastmath.hxx"
-#define  SGD_PI_2  1.57079632679489661923
 
 /**
  * This function is on avarage 9 times faster than the system exp() function
diff --git a/simgear/math/point3d.hxx b/simgear/math/point3d.hxx
index 77fd4a5b..c5b72c93 100644
--- a/simgear/math/point3d.hxx
+++ b/simgear/math/point3d.hxx
@@ -185,7 +185,11 @@ operator<< ( ostream& out, const Point3D& p )
 
 // CONSTRUCTORS
 
-inline Point3D::Point3D() {}
+inline Point3D::Point3D()
+{
+   n[PX] = n[PY] = 0.0;
+   n[PZ] = -9999.0;
+}
 
 inline Point3D::Point3D(const double x, const double y, const double z)
 {
diff --git a/simgear/math/polar3d.cxx b/simgear/math/polar3d.cxx
index 1502f3a8..6917fe12 100644
--- a/simgear/math/polar3d.cxx
+++ b/simgear/math/polar3d.cxx
@@ -30,10 +30,14 @@
 #include "polar3d.hxx"
 
 
-// Find the Altitude above the Ellipsoid (WGS84) given the Earth
-// Centered Cartesian coordinate vector Distances are specified in
-// meters.
-double fgGeodAltFromCart(const Point3D& cp)
+/**
+ * Find the Altitude above the Ellipsoid (WGS84) given the Earth
+ * Centered Cartesian coordinate vector Distances are specified in
+ * meters.
+ * @param cp point specified in cartesian coordinates
+ * @return altitude above the (wgs84) earth in meters
+ */
+double sgGeodAltFromCart(const Point3D& cp)
 {
     double t_lat, x_alpha, mu_alpha;
     double lat_geoc, radius;
@@ -59,4 +63,224 @@ double fgGeodAltFromCart(const Point3D& cp)
     return(result);
 }
 
+/**
+ * Convert a polar coordinate to a cartesian coordinate.  Lon and Lat
+ * must be specified in radians.  The SG convention is for distances
+ * to be specified in meters
+ * @param p point specified in polar coordinates
+ * @return the same point in cartesian coordinates
+ */
+Point3D sgPolarToCart3d(const Point3D& p) {
+    double tmp = cos( p.lat() ) * p.radius();
 
+    return Point3D( cos( p.lon() ) * tmp,
+                    sin( p.lon() ) * tmp,
+                    sin( p.lat() ) * p.radius() );
+}
+
+/**
+ * Convert a cartesian coordinate to polar coordinates (lon/lat
+ * specified in radians.  Distances are specified in meters.
+ * @param cp point specified in cartesian coordinates
+ * @return the same point in polar coordinates
+ */
+Point3D sgCartToPolar3d(const Point3D& cp) {
+    return Point3D( atan2( cp.y(), cp.x() ),
+                    SGD_PI_2 -
+                    atan2( sqrt(cp.x()*cp.x() + cp.y()*cp.y()), cp.z() ),
+                    sqrt(cp.x()*cp.x() + cp.y()*cp.y() + cp.z()*cp.z()) );
+}
+
+/**
+ * Calculate new lon/lat given starting lon/lat, and offset radial, and
+ * distance.  NOTE: starting point is specifed in radians, distance is
+ * specified in meters (and converted internally to radians)
+ * ... assumes a spherical world.
+ * @param orig specified in polar coordinates
+ * @param course offset radial
+ * @param dist offset distance
+ * @return destination point in polar coordinates
+ */
+Point3D calc_gc_lon_lat( const Point3D& orig, double course,
+                                double dist ) {
+    Point3D result;
+
+    // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
+    // IF (cos(lat)=0)
+    //   lon=lon1      // endpoint a pole
+    // ELSE
+    //   lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
+    // ENDIF
+
+    // printf("calc_lon_lat()  offset.theta = %.2f offset.dist = %.2f\n",
+    //        offset.theta, offset.dist);
+
+    dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
+
+    result.sety( asin( sin(orig.y()) * cos(dist) +
+                       cos(orig.y()) * sin(dist) * cos(course) ) );
+
+    if ( cos(result.y()) < SG_EPSILON ) {
+        result.setx( orig.x() );      // endpoint a pole
+    } else {
+        result.setx(
+            fmod(orig.x() - asin( sin(course) * sin(dist) /
+                                  cos(result.y()) )
+                 + SGD_PI, SGD_2PI) - SGD_PI );
+    }
+
+    return result;
+}
+
+/**
+ * Calculate course/dist given two spherical points.
+ * @param start starting point
+ * @param dest ending point
+ * @param course resulting course
+ * @param dist resulting distance
+ */
+void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
+                                 double *course, double *dist )
+{
+    if ( start == dest) {
+            *dist=0;
+            *course=0;
+            return;
+    }
+    // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
+    //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
+    double cos_start_y = cos( start.y() );
+    double tmp1 = sin( (start.y() - dest.y()) * 0.5 );
+    double tmp2 = sin( (start.x() - dest.x()) * 0.5 );
+    double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
+                                 cos_start_y * cos(dest.y()) * tmp2 * tmp2));
+
+    *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
+
+#if 1
+    double c1 = atan2(
+                cos(dest.y())*sin(dest.x()-start.x()),
+                cos(start.y())*sin(dest.y())-
+                sin(start.y())*cos(dest.y())*cos(dest.x()-start.x()));
+    if (c1 >= 0)
+      *course = SGD_2PI-c1;
+    else
+      *course = -c1;
+#else
+    // We obtain the initial course, tc1, (at point 1) from point 1 to
+    // point 2 by the following. The formula fails if the initial
+    // point is a pole. We can special case this with:
+    //
+    // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
+    //   IF (lat1 > 0)
+    //     tc1= pi        //  starting from N pole
+    //   ELSE
+    //     tc1= 0         //  starting from S pole
+    //   ENDIF
+    // ENDIF
+    //
+    // For starting points other than the poles:
+    //
+    // IF sin(lon2-lon1)<0
+    //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
+    // ELSE
+    //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
+    // ENDIF
+
+    // if ( cos(start.y()) < SG_EPSILON ) {
+    // doing it this way saves a transcendental call
+    double sin_start_y = sin( start.y() );
+    if ( fabs(1.0-sin_start_y) < SG_EPSILON ) {
+        // EPS a small number ~ machine precision
+        if ( start.y() > 0 ) {
+            *course = SGD_PI;   // starting from N pole
+        } else {
+            *course = 0;        // starting from S pole
+        }
+    } else {
+        // For starting points other than the poles:
+        // double tmp3 = sin(d)*cos_start_y);
+        // double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
+        // double tmp5 = acos(tmp4/tmp3);
+
+        // Doing this way gaurentees that the temps are
+        // not stored into memory
+        double tmp5 = acos( (sin(dest.y()) - sin_start_y * cos(d)) /
+                            (sin(d) * cos_start_y) );
+
+        // if ( sin( dest.x() - start.x() ) < 0 ) {
+        // the sin of the negative angle is just the opposite sign
+        // of the sin of the angle  so tmp2 will have the opposite
+        // sign of sin( dest.x() - start.x() )
+        if ( tmp2 >= 0 ) {
+            *course = tmp5;
+        } else {
+            *course = SGD_2PI - tmp5;
+        }
+    }
+#endif
+}
+
+
+#if 0
+/**
+ * Calculate course/dist given two spherical points.
+ * @param start starting point
+ * @param dest ending point
+ * @param course resulting course
+ * @param dist resulting distance
+ */
+void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
+                                 double *course, double *dist ) {
+    // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 +
+    //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
+    double tmp1 = sin( (start.y() - dest.y()) / 2 );
+    double tmp2 = sin( (start.x() - dest.x()) / 2 );
+    double d = 2.0 * asin( sqrt( tmp1 * tmp1 +
+                                 cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
+    // We obtain the initial course, tc1, (at point 1) from point 1 to
+    // point 2 by the following. The formula fails if the initial
+    // point is a pole. We can special case this with:
+    //
+    // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
+    //   IF (lat1 > 0)
+    //     tc1= pi        //  starting from N pole
+    //   ELSE
+    //     tc1= 0         //  starting from S pole
+    //   ENDIF
+    // ENDIF
+    //
+    // For starting points other than the poles:
+    //
+    // IF sin(lon2-lon1)<0
+    //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
+    // ELSE
+    //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))
+    // ENDIF
+
+    double tc1;
+
+    if ( cos(start.y()) < SG_EPSILON ) {
+        // EPS a small number ~ machine precision
+        if ( start.y() > 0 ) {
+            tc1 = SGD_PI;        // starting from N pole
+        } else {
+            tc1 = 0;            // starting from S pole
+        }
+    }
+
+    // For starting points other than the poles:
+
+    double tmp3 = sin(d)*cos(start.y());
+    double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
+    double tmp5 = acos(tmp4/tmp3);
+    if ( sin( dest.x() - start.x() ) < 0 ) {
+         tc1 = tmp5;
+    } else {
+         tc1 = SGD_2PI - tmp5;
+    }
+
+    *course = tc1;
+    *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
+}
+#endif // 0
diff --git a/simgear/math/polar3d.hxx b/simgear/math/polar3d.hxx
index b9d5a66b..4a0c7ed7 100644
--- a/simgear/math/polar3d.hxx
+++ b/simgear/math/polar3d.hxx
@@ -57,13 +57,7 @@ double sgGeodAltFromCart(const Point3D& cp);
  * @param p point specified in polar coordinates
  * @return the same point in cartesian coordinates
  */
-inline Point3D sgPolarToCart3d(const Point3D& p) {
-    double tmp = cos( p.lat() ) * p.radius();
-
-    return Point3D( cos( p.lon() ) * tmp,
-		    sin( p.lon() ) * tmp,
-		    sin( p.lat() ) * p.radius() );
-}
+Point3D sgPolarToCart3d(const Point3D& p);
 
 
 /**
@@ -72,12 +66,7 @@ inline Point3D sgPolarToCart3d(const Point3D& p) {
  * @param cp point specified in cartesian coordinates
  * @return the same point in polar coordinates
  */
-inline Point3D sgCartToPolar3d(const Point3D& cp) {
-    return Point3D( atan2( cp.y(), cp.x() ),
-		    SGD_PI_2 - 
-		    atan2( sqrt(cp.x()*cp.x() + cp.y()*cp.y()), cp.z() ),
-		    sqrt(cp.x()*cp.x() + cp.y()*cp.y() + cp.z()*cp.z()) );
-}
+Point3D sgCartToPolar3d(const Point3D& cp);
 
 
 /**
@@ -90,36 +79,7 @@ inline Point3D sgCartToPolar3d(const Point3D& cp) {
  * @param dist offset distance
  * @return destination point in polar coordinates
  */
-inline Point3D calc_gc_lon_lat( const Point3D& orig, double course,
-				double dist ) {
-    Point3D result;
-
-    // lat=asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc))
-    // IF (cos(lat)=0)
-    //   lon=lon1      // endpoint a pole
-    // ELSE
-    //   lon=mod(lon1-asin(sin(tc)*sin(d)/cos(lat))+pi,2*pi)-pi
-    // ENDIF
-
-    // printf("calc_lon_lat()  offset.theta = %.2f offset.dist = %.2f\n",
-    //        offset.theta, offset.dist);
-
-    dist *= SG_METER_TO_NM * SG_NM_TO_RAD;
-    
-    result.sety( asin( sin(orig.y()) * cos(dist) + 
-		       cos(orig.y()) * sin(dist) * cos(course) ) );
-
-    if ( cos(result.y()) < SG_EPSILON ) {
-        result.setx( orig.x() );      // endpoint a pole
-    } else {
-        result.setx( 
-	    fmod(orig.x() - asin( sin(course) * sin(dist) / 
-				  cos(result.y()) )
-                 + SGD_PI, SGD_2PI) - SGD_PI );
-    }
-
-    return result;
-}
+Point3D calc_gc_lon_lat( const Point3D& orig, double course, double dist );
 
 
 /**
@@ -129,71 +89,8 @@ inline Point3D calc_gc_lon_lat( const Point3D& orig, double course,
  * @param course resulting course
  * @param dist resulting distance
  */
-inline void calc_gc_course_dist( const Point3D& start, const Point3D& dest, 
-                                 double *course, double *dist )
-{
-    // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 + 
-    //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
-    double cos_start_y = cos( start.y() );
-    volatile double tmp1 = sin( (start.y() - dest.y()) * 0.5 );
-    volatile double tmp2 = sin( (start.x() - dest.x()) * 0.5 );
-    double d = 2.0 * asin( sqrt( tmp1 * tmp1 + 
-                                 cos_start_y * cos(dest.y()) * tmp2 * tmp2));
-
-    *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
-
-    // We obtain the initial course, tc1, (at point 1) from point 1 to
-    // point 2 by the following. The formula fails if the initial
-    // point is a pole. We can special case this with:
-    //
-    // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
-    //   IF (lat1 > 0)
-    //     tc1= pi        //  starting from N pole
-    //   ELSE
-    //     tc1= 0         //  starting from S pole
-    //   ENDIF
-    // ENDIF
-    //
-    // For starting points other than the poles: 
-    // 
-    // IF sin(lon2-lon1)<0       
-    //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))    
-    // ELSE       
-    //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))    
-    // ENDIF 
-
-    // if ( cos(start.y()) < SG_EPSILON ) {
-    // doing it this way saves a transcendental call
-    double sin_start_y = sin( start.y() );
-    if ( fabs(1.0-sin_start_y) < SG_EPSILON ) {
-        // EPS a small number ~ machine precision
-        if ( start.y() > 0 ) {
-            *course = SGD_PI;   // starting from N pole
-        } else {
-            *course = 0;        // starting from S pole
-        }
-    } else {
-        // For starting points other than the poles: 
-        // double tmp3 = sin(d)*cos_start_y);
-        // double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
-        // double tmp5 = acos(tmp4/tmp3);
-
-        // Doing this way gaurentees that the temps are
-        // not stored into memory
-        double tmp5 = acos( (sin(dest.y()) - sin_start_y * cos(d)) /
-                            (sin(d) * cos_start_y) );
-
-        // if ( sin( dest.x() - start.x() ) < 0 ) {
-        // the sin of the negative angle is just the opposite sign
-        // of the sin of the angle  so tmp2 will have the opposite
-        // sign of sin( dest.x() - start.x() )
-        if ( tmp2 >= 0 ) {
-            *course = tmp5;
-        } else {
-            *course = 2 * SGD_PI - tmp5;
-        }
-    }
-}
+void calc_gc_course_dist( const Point3D& start, const Point3D& dest, 
+                                 double *course, double *dist );
 
 #if 0
 /**
@@ -203,60 +100,9 @@ inline void calc_gc_course_dist( const Point3D& start, const Point3D& dest,
  * @param course resulting course
  * @param dist resulting distance
  */
-inline void calc_gc_course_dist( const Point3D& start, const Point3D& dest, 
-				 double *course, double *dist ) {
-    // d = 2*asin(sqrt((sin((lat1-lat2)/2))^2 + 
-    //            cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
-    double tmp1 = sin( (start.y() - dest.y()) / 2 );
-    double tmp2 = sin( (start.x() - dest.x()) / 2 );
-    double d = 2.0 * asin( sqrt( tmp1 * tmp1 + 
-				 cos(start.y()) * cos(dest.y()) * tmp2 * tmp2));
-
-    // We obtain the initial course, tc1, (at point 1) from point 1 to
-    // point 2 by the following. The formula fails if the initial
-    // point is a pole. We can special case this with:
-    //
-    // IF (cos(lat1) < EPS)   // EPS a small number ~ machine precision
-    //   IF (lat1 > 0)
-    //     tc1= pi        //  starting from N pole
-    //   ELSE
-    //     tc1= 0         //  starting from S pole
-    //   ENDIF
-    // ENDIF
-    //
-    // For starting points other than the poles: 
-    // 
-    // IF sin(lon2-lon1)<0       
-    //   tc1=acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))    
-    // ELSE       
-    //   tc1=2*pi-acos((sin(lat2)-sin(lat1)*cos(d))/(sin(d)*cos(lat1)))    
-    // ENDIF 
-
-    double tc1;
-
-    if ( cos(start.y()) < SG_EPSILON ) {
-	// EPS a small number ~ machine precision
-	if ( start.y() > 0 ) {
-	    tc1 = SGD_PI;        // starting from N pole
-	} else {
-	    tc1 = 0;		// starting from S pole
-	}
-    }
-
-    // For starting points other than the poles: 
-
-    double tmp3 = sin(d)*cos(start.y());
-    double tmp4 = sin(dest.y())-sin(start.y())*cos(d);
-    double tmp5 = acos(tmp4/tmp3);
-    if ( sin( dest.x() - start.x() ) < 0 ) {
-	 tc1 = tmp5;
-    } else {
-	 tc1 = 2 * SGD_PI - tmp5;
-    }
-
-    *course = tc1;
-    *dist = d * SG_RAD_TO_NM * SG_NM_TO_METER;
-}
+void calc_gc_course_dist( const Point3D& start, const Point3D& dest, 
+				 double *course, double *dist );
 #endif // 0
 
 #endif // _POLAR3D_HXX
+
diff --git a/simgear/misc/texcoord.cxx b/simgear/misc/texcoord.cxx
index d4874a7d..7f01d1ee 100644
--- a/simgear/misc/texcoord.cxx
+++ b/simgear/misc/texcoord.cxx
@@ -189,7 +189,7 @@ point_list sgCalcTexCoords( const SGBucket& b, const point_list& geod_nodes,
     double clat_rad = clat * SGD_DEGREES_TO_RADIANS;
     double cos_lat = cos( clat_rad );
     double local_radius = cos_lat * SG_EQUATORIAL_RADIUS_M;
-    double local_perimeter = 2.0 * local_radius * SGD_PI;
+    double local_perimeter = local_radius * SGD_2PI;
     double degree_width = local_perimeter / 360.0;
 
     // cout << "clat = " << clat << endl;
@@ -199,7 +199,7 @@ point_list sgCalcTexCoords( const SGBucket& b, const point_list& geod_nodes,
     // cout << "local_perimeter = " << local_perimeter << endl;
     // cout << "degree_width = " << degree_width << endl;
 
-    double perimeter = 2.0 * SG_EQUATORIAL_RADIUS_M * SGD_PI;
+    double perimeter = SG_EQUATORIAL_RADIUS_M * SGD_2PI;
     double degree_height = perimeter / 360.0;
     // cout << "degree_height = " << degree_height << endl;
 
diff --git a/simgear/route/waypoint.hxx b/simgear/route/waypoint.hxx
index a95b030f..508b1e4f 100644
--- a/simgear/route/waypoint.hxx
+++ b/simgear/route/waypoint.hxx
@@ -153,10 +153,10 @@ public:
     inline void set_distance( double d ) { distance = d; }
 
     /** @return waypoint id */
-    inline string get_id() const { return id; }
+    inline const string& get_id() const { return id; }
 
     /** @return waypoint name */
-    inline string get_name() const { return name; }
+    inline const string& get_name() const { return name; }
 
 };
 
diff --git a/simgear/scene/sky/sky.cxx b/simgear/scene/sky/sky.cxx
index 5100635a..42a50f48 100644
--- a/simgear/scene/sky/sky.cxx
+++ b/simgear/scene/sky/sky.cxx
@@ -334,13 +334,13 @@ void SGSky::modify_vis( float alt, float time_factor ) {
 		// modify actual_visibility based on puff envelope
 
 		if ( puff_progression <= ramp_up ) {
-		    double x = 0.5 * SGD_PI * puff_progression / ramp_up;
+		    double x = SGD_PI_2 * puff_progression / ramp_up;
 		    double factor = 1.0 - sin( x );
 		    // cout << "ramp up = " << puff_progression
 		    //      << "  factor = " << factor << endl;
 		    effvis = effvis * factor;
 		} else if ( puff_progression >= ramp_up + puff_length ) {
-		    double x = 0.5 * SGD_PI * 
+		    double x = SGD_PI_2 * 
 			(puff_progression - (ramp_up + puff_length)) /
 			ramp_down;
 		    double factor = sin( x );
diff --git a/simgear/scene/sky/sphere.cxx b/simgear/scene/sky/sphere.cxx
index d464f891..0a045ac1 100644
--- a/simgear/scene/sky/sphere.cxx
+++ b/simgear/scene/sky/sphere.cxx
@@ -24,6 +24,7 @@
 
 
 #include <simgear/compiler.h>
+#include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 
 #include STL_IOSTREAM
@@ -47,7 +48,7 @@ ssgBranch *ssgMakeSphere( ssgSimpleState *state, ssgColourArray *cl,
     sgVec3 vec3;
 
     drho = SGD_PI / (float) stacks;
-    dtheta = 2.0 * SGD_PI / (float) slices;
+    dtheta = SGD_2PI / (float) slices;
 
     /* texturing: s goes from 0.0/0.25/0.5/0.75/1.0 at +y/+x/-y/-x/+y
        axis t goes from -1.0/+1.0 at z = -radius/+radius (linear along
diff --git a/simgear/scene/sky/stars.cxx b/simgear/scene/sky/stars.cxx
index 47c7f8f1..eb9691ad 100644
--- a/simgear/scene/sky/stars.cxx
+++ b/simgear/scene/sky/stars.cxx
@@ -26,6 +26,7 @@
 
 
 #include <simgear/compiler.h>
+#include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 
 #include <stdio.h>
@@ -153,32 +154,32 @@ bool SGStars::repaint( double sun_angle, int num, sgdVec3 *star_data ) {
     int phase;
 
     // determine which star structure to draw
-    if ( sun_angle > (0.5 * SGD_PI + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
+    if ( sun_angle > (SGD_PI_2 + 10.0 * SGD_DEGREES_TO_RADIANS ) ) {
         // deep night
         factor = 1.0;
         cutoff = 4.5;
         phase = 0;
-    } else if ( sun_angle > (0.5 * SGD_PI + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 8.8 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 1.0;
         cutoff = 3.8;
         phase = 1;
-    } else if ( sun_angle > (0.5 * SGD_PI + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 7.5 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.95;
         cutoff = 3.1;
         phase = 2;
-    } else if ( sun_angle > (0.5 * SGD_PI + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 7.0 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.9;
         cutoff = 2.4;
         phase = 3;
-    } else if ( sun_angle > (0.5 * SGD_PI + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 6.5 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.85;
         cutoff = 1.8;
         phase = 4;
-    } else if ( sun_angle > (0.5 * SGD_PI + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 6.0 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.8;
         cutoff = 1.2;
         phase = 5;
-    } else if ( sun_angle > (0.5 * SGD_PI + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
+    } else if ( sun_angle > (SGD_PI_2 + 5.5 * SGD_DEGREES_TO_RADIANS ) ) {
         factor = 0.75;
         cutoff = 0.6;
         phase = 6;
diff --git a/simgear/threads/SGThread.hxx b/simgear/threads/SGThread.hxx
index fcbc0cb5..5c52607a 100644
--- a/simgear/threads/SGThread.hxx
+++ b/simgear/threads/SGThread.hxx
@@ -134,8 +134,13 @@ SGThread::~SGThread()
 inline int
 SGThread::start( unsigned cpu )
 {
-    int status = pthread_create( &tid, 0, start_handler, this );
+    pthread_attr_t attr;
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+	  
+    int status = pthread_create( &tid, &attr, start_handler, this );
     assert( status == 0 );
+    pthread_attr_destroy(&attr);
 #if defined( sgi )
     if ( !status && !cpu )
         pthread_setrunon_np( cpu );
@@ -217,8 +222,12 @@ protected:
 
 inline SGMutex::SGMutex()
 {
-    int status = pthread_mutex_init( &mutex, 0 );
+    pthread_mutexattr_t    mutex_attr;
+    pthread_mutexattr_init(&mutex_attr);
+    pthread_mutexattr_setpshared(&mutex_attr, PTHREAD_PROCESS_SHARED);
+    int status = pthread_mutex_init( &mutex, &mutex_attr );
     assert( status == 0 );
+    pthread_mutexattr_destroy(&mutex_attr);
 }
 
 inline SGMutex::~SGMutex()
diff --git a/simgear/timing/sg_time.cxx b/simgear/timing/sg_time.cxx
index efa59c7d..12afd26c 100644
--- a/simgear/timing/sg_time.cxx
+++ b/simgear/timing/sg_time.cxx
@@ -279,7 +279,7 @@ void SGTime::updateLocal( double lon_rad, double lat_rad, const string& root ) {
         // not within -180 ... 180
         lon_rad = 0.0;
     }
-    if ( lat_rad < -SGD_PI * 0.5 || lat_rad > SGD_PI * 0.5 ) {
+    if ( lat_rad < -SGD_PI_2 || lat_rad > SGD_PI_2 ) {
         // not within -90 ... 90
         lat_rad = 0.0;
     }
-- 
2.39.5