From e5028b5637b9f3def22694c4d24625cde86f7562 Mon Sep 17 00:00:00 2001
From: curt <curt>
Date: Tue, 18 Sep 2001 21:27:54 +0000
Subject: [PATCH] - added snap() function to snap a polygon to a grid

---
 src/Lib/Geometry/poly_support.cxx | 33 +++++++++++++++++++++++++++++++
 src/Lib/Geometry/poly_support.hxx |  3 +++
 2 files changed, 36 insertions(+)

diff --git a/src/Lib/Geometry/poly_support.cxx b/src/Lib/Geometry/poly_support.cxx
index e613687a..07d8c360 100644
--- a/src/Lib/Geometry/poly_support.cxx
+++ b/src/Lib/Geometry/poly_support.cxx
@@ -994,6 +994,39 @@ FGPolygon remove_dups( const FGPolygon &poly ) {
 }
 
 
+static inline double
+snap (double value, double grid_size)
+{
+				// I have no idea if this really works.
+  double factor = 1.0 / grid_size;
+  return double(int(value * factor)) / factor;
+}
+
+static inline Point3D
+snap (const Point3D &p, double grid_size)
+{
+  Point3D result;
+  result.setx(snap(p.x(), grid_size));
+  result.sety(snap(p.y(), grid_size));
+  result.setz(snap(p.z(), grid_size));
+  cout << result << endl;
+  return result;
+}
+
+// snap all points in a polygon to the given grid size.
+FGPolygon snap (const FGPolygon &poly, double grid_size)
+{
+  FGPolygon result;
+  for (int contour = 0; contour < poly.contours(); contour++) {
+    for (int i = 0; i < poly.contour_size(contour); i++) {
+      result.add_node(contour, snap(poly.get_pt(contour, i), grid_size));
+    }
+    result.set_hole_flag(contour, poly.get_hole_flag(contour));
+  }
+  return result;
+}
+
+
 // static const double tgAirportEpsilon = SG_EPSILON / 10.0;
 static const double tgAirportEpsilon = SG_EPSILON;
 
diff --git a/src/Lib/Geometry/poly_support.hxx b/src/Lib/Geometry/poly_support.hxx
index 88eec7f9..5cb03ce5 100644
--- a/src/Lib/Geometry/poly_support.hxx
+++ b/src/Lib/Geometry/poly_support.hxx
@@ -73,6 +73,9 @@ Point3D calc_point_inside_old( const FGPolygon& p, const int contour,
 // calculate some "arbitrary" point inside each of the polygons contours
 void calc_points_inside( FGPolygon& p );
 
+// snap all points to a grid
+FGPolygon snap( const FGPolygon &poly, double grid_size );
+
 // remove duplicate nodes in a polygon should they exist.  Returns the
 // fixed polygon
 FGPolygon remove_dups( const FGPolygon &poly );