From 4d3f1efd30ca480cf37cb16e44487f2fcac4d0c9 Mon Sep 17 00:00:00 2001
From: Christian Schmitt <chris@ilovelinux.de>
Date: Tue, 9 Oct 2012 20:25:00 +0200
Subject: [PATCH] Convert Rectangle function to SGGeod

---
 src/Airports/GenAirports850/main.cxx | 16 +++++------
 src/Lib/Geometry/line.cxx            | 22 +++++++--------
 src/Lib/Geometry/rectangle.cxx       | 40 ++++++++++++++--------------
 src/Lib/Geometry/rectangle.hxx       | 23 ++++++++--------
 src/Lib/Geometry/util.cxx            |  6 ++---
 5 files changed, 53 insertions(+), 54 deletions(-)

diff --git a/src/Airports/GenAirports850/main.cxx b/src/Airports/GenAirports850/main.cxx
index 53251dae..7559c5e3 100644
--- a/src/Airports/GenAirports850/main.cxx
+++ b/src/Airports/GenAirports850/main.cxx
@@ -213,18 +213,18 @@ int main(int argc, char **argv)
         else if ( arg.find("--chunk=") == 0 ) 
         {
             tg::Rectangle rectangle = tg::parseChunk(arg.substr(8).c_str(), 10.0);
-            min_lon = rectangle.getMin().x();
-            min_lat = rectangle.getMin().y();
-            max_lon = rectangle.getMax().x();
-            max_lat = rectangle.getMax().y();
+            min_lon = rectangle.getMin().getLongitudeDeg();
+            min_lat = rectangle.getMin().getLatitudeDeg();
+            max_lon = rectangle.getMax().getLongitudeDeg();
+            max_lat = rectangle.getMax().getLatitudeDeg();
         } 
         else if ( arg.find("--tile=") == 0 ) 
         {
             tg::Rectangle rectangle = tg::parseTile(arg.substr(7).c_str());
-            min_lon = rectangle.getMin().x();
-            min_lat = rectangle.getMin().y();
-            max_lon = rectangle.getMax().x();
-            max_lat = rectangle.getMax().y();
+            min_lon = rectangle.getMin().getLongitudeDeg();
+            min_lat = rectangle.getMin().getLatitudeDeg();
+            max_lon = rectangle.getMax().getLongitudeDeg();
+            max_lat = rectangle.getMax().getLatitudeDeg();
     	} 
         else if ( arg.find("--airport=") == 0 ) 
         {
diff --git a/src/Lib/Geometry/line.cxx b/src/Lib/Geometry/line.cxx
index 0d2f2b9b..675ccee9 100644
--- a/src/Lib/Geometry/line.cxx
+++ b/src/Lib/Geometry/line.cxx
@@ -51,22 +51,22 @@ Line::addPoint (const Point3D &point)
 Rectangle
 Line::getBounds () const
 {
-  Point3D min;
-  Point3D max;
+  SGGeod min;
+  SGGeod max;
 
   int nPoints = _points.size();
   for (int i = 0; i < nPoints; i++) {
     if (i == 0) {
-      min = max = _points[i];
+      min = max = _points[i].toSGGeod();
     } else {
-      if (_points[i].x() < min.x())
-	min.setx(_points[i].x());
-      if (_points[i].x() > max.x())
-	max.setx(_points[i].x());
-      if (_points[i].y() < min.y())
-	min.sety(_points[i].y());
-      if (_points[i].y() > max.y())
-	max.sety(_points[i].y());
+      if (_points[i].x() < min.getLongitudeDeg())
+	min.setLongitudeDeg(_points[i].x());
+      if (_points[i].x() > max.getLongitudeDeg())
+	max.setLongitudeDeg(_points[i].x());
+      if (_points[i].y() < min.getLatitudeDeg())
+	min.setLatitudeDeg(_points[i].y());
+      if (_points[i].y() > max.getLatitudeDeg())
+	max.setLatitudeDeg(_points[i].y());
     }
   }
 
diff --git a/src/Lib/Geometry/rectangle.cxx b/src/Lib/Geometry/rectangle.cxx
index 110d28e7..75a1d1ed 100644
--- a/src/Lib/Geometry/rectangle.cxx
+++ b/src/Lib/Geometry/rectangle.cxx
@@ -18,7 +18,7 @@ Rectangle::Rectangle (const Rectangle &r)
 {
 }
 
-Rectangle::Rectangle (const Point3D &min, const Point3D &max)
+Rectangle::Rectangle (const SGGeod &min, const SGGeod &max)
   : _min(min),
     _max(max)
 {
@@ -29,13 +29,13 @@ Rectangle::~Rectangle ()
 }
 
 void
-Rectangle::setMin (const Point3D &p)
+Rectangle::setMin (const SGGeod &p)
 {
   _min = p;
 }
 
 void
-Rectangle::setMax (const Point3D &p)
+Rectangle::setMax (const SGGeod &p)
 {
   _max = p;
 }
@@ -44,35 +44,35 @@ void
 Rectangle::sanify ()
 {
   double tmp;
-  if (_min.x() > _max.x()) {
-    tmp = _min.x();
-    _min.setx(_max.x());
-    _max.setx(tmp);
+  if (_min.getLongitudeDeg() > _max.getLongitudeDeg()) {
+    tmp = _min.getLongitudeDeg();
+    _min.setLongitudeDeg(_max.getLongitudeDeg());
+    _max.setLongitudeDeg(tmp);
   }
-  if (_min.y() > _max.y()) {
-    tmp = _min.y();
-    _min.sety(_max.y());
-    _max.sety(tmp);
+  if (_min.getLatitudeDeg() > _max.getLatitudeDeg()) {
+    tmp = _min.getLatitudeDeg();
+    _min.setLatitudeDeg(_max.getLatitudeDeg());
+    _max.setLatitudeDeg(tmp);
   }
 }
 
 bool
-Rectangle::isInside (const Point3D &p) const
+Rectangle::isInside (const SGGeod &p) const
 {
-  return ((p.x() >= _min.x() && p.x() <= _max.x()) &&
-	  (p.y() >= _min.y() && p.y() <= _max.y()));
+    return ((p.getLongitudeDeg() >= _min.getLongitudeDeg() && p.getLongitudeDeg() <= _max.getLongitudeDeg()) &&
+    (p.getLatitudeDeg() >= _min.getLatitudeDeg() && p.getLatitudeDeg() <= _max.getLatitudeDeg()));
 }
 
 bool
 Rectangle::isOverlapping (const Rectangle &r) const
 {
-  const Point3D &min = r.getMin();
-  const Point3D &max = r.getMax();
-  return ((max.x() >= _min.x()) && (min.x() <= _max.x()) &&
-	  (max.y() >= _min.y()) && (min.y() <= _max.y()));
+  const SGGeod &min = r.getMin();
+  const SGGeod &max = r.getMax();
+  return ((max.getLongitudeDeg() >= _min.getLongitudeDeg()) && (min.getLongitudeDeg() <= _max.getLongitudeDeg()) &&
+  (max.getLatitudeDeg() >= _min.getLatitudeDeg()) && (min.getLatitudeDeg() <= _max.getLatitudeDeg()));
 }
 
-const TGPolygon
+/*const TGPolygon
 Rectangle::toPoly () const
 {
   TGPolygon poly;
@@ -81,7 +81,7 @@ Rectangle::toPoly () const
   poly.add_node(0, _max);
   poly.add_node(0, Point3D(_min.x(), _max.y(), 0));
   return poly;
-}
+}*/
 
 };
 
diff --git a/src/Lib/Geometry/rectangle.hxx b/src/Lib/Geometry/rectangle.hxx
index 30ae53c1..daa66a23 100644
--- a/src/Lib/Geometry/rectangle.hxx
+++ b/src/Lib/Geometry/rectangle.hxx
@@ -12,7 +12,6 @@
 #endif  
 
 #include <simgear/compiler.h>
-#include <Geometry/point3d.hxx>
 
 #include <Polygon/polygon.hxx>
 
@@ -45,7 +44,7 @@ public:
   /**
    * Convenience constructor.
    */
-  Rectangle (const Point3D &min, const Point3D &max);
+  Rectangle (const SGGeod& min, const SGGeod& max);
 
   /**
    * Destructor.
@@ -57,42 +56,42 @@ public:
    *
    * @return The top-left vertex.
    */
-  virtual const Point3D &getMin () const { return _min; }
+  virtual const SGGeod &getMin () const { return _min; }
 
   /**
    * Get the maximum (bottom right) corner of the rectangle.
    *
    * @return The bottom-right vertex.
    */
-  virtual const Point3D &getMax () const { return _max; }
+  virtual const SGGeod &getMax () const { return _max; }
 
   /**
    * Get the minimum (top left) corner of the rectangle.
    *
    * @return The top-left vertex.
    */
-  virtual Point3D &getMin () { return _min; }
+  virtual SGGeod &getMin () { return _min; }
 
   /**
    * Get the maximum (bottom right) corner of the rectangle.
    *
    * @return The bottom-right vertex.
    */
-  virtual Point3D &getMax () { return _max; }
+  virtual SGGeod &getMax () { return _max; }
 
   /**
    * Set the minimum (top-left) corner of the rectangle.
    *
    * @param p The top-left vertex.
    */
-  virtual void setMin (const Point3D &p);
+  virtual void setMin (const SGGeod& p);
 
   /**
    * Set the maximum (bottom-right) corner of the rectangle.
    *
    * @param p The bottom-right vertex.
    */
-  virtual void setMax (const Point3D &p);
+  virtual void setMax (const SGGeod& p);
 
   /**
    * Make the rectangle sane.
@@ -110,7 +109,7 @@ public:
    * @return true if the point is inside or on the boundary of the
    * rectangle, false if it is outside.
    */
-  virtual bool isInside (const Point3D &p) const;
+  virtual bool isInside (const SGGeod& p) const;
 
   /**
    * Test whether this rectangle overlaps with another one.
@@ -126,11 +125,11 @@ public:
    *
    * @return A four-vertex polygon representing this rectangle.
    */
-  virtual const TGPolygon toPoly () const;
+//  virtual const TGPolygon toPoly () const;
 
 private:
-  Point3D _min;
-  Point3D _max;
+  SGGeod _min;
+  SGGeod _max;
 };
 
 };
diff --git a/src/Lib/Geometry/util.cxx b/src/Lib/Geometry/util.cxx
index 5866c9ad..5e770c23 100644
--- a/src/Lib/Geometry/util.cxx
+++ b/src/Lib/Geometry/util.cxx
@@ -804,7 +804,7 @@ makeBounds (const TGPolygon &polygon)
             }
         }
     }
-    return Rectangle(Point3D(min_x, min_y, 0), Point3D(max_x, max_y, 0));
+    return Rectangle(SGGeod::fromDeg(min_x, min_y), SGGeod::fromDeg(max_x, max_y));
 }
 
 
@@ -836,8 +836,8 @@ parseChunk (const string &s, double delta)
   
   double x = atoi(s.substr(1,3).c_str()) * x_factor;
   double y = atoi(s.substr(5).c_str()) * y_factor;
-  bounds.setMin(Point3D(x, y, 0));
-  bounds.setMax(Point3D(x + delta, y + delta, 0));
+  bounds.setMin(SGGeod::fromDeg(x, y));
+  bounds.setMax(SGGeod::fromDeg(x + delta, y + delta));
 
   return bounds;
 }