From 598500edb0b62ba3190c879dd4b60ddd78088fd1 Mon Sep 17 00:00:00 2001 From: david <david> Date: Tue, 23 Jul 2002 14:32:34 +0000 Subject: [PATCH] Added a parseChunk function for parsing chunk strings like w080n40. --- src/Lib/Geometry/util.cxx | 35 +++++++++++++++++++++++++++++++++++ src/Lib/Geometry/util.hxx | 12 ++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Lib/Geometry/util.cxx b/src/Lib/Geometry/util.cxx index 1fe270c9..716c7753 100644 --- a/src/Lib/Geometry/util.cxx +++ b/src/Lib/Geometry/util.cxx @@ -8,6 +8,7 @@ #include <simgear/debug/logstream.hxx> #include <simgear/math/sg_geodesy.hxx> +#include <simgear/misc/exception.hxx> #include <Polygon/polygon.hxx> @@ -144,4 +145,38 @@ makePolygon (const Line &line, int width, FGPolygon &polygon) } +Rectangle +parseChunk (const string &s) +{ + Rectangle bounds; + int x_factor; + int y_factor; + + if (s.size() != 7) + throw sg_exception(string("Bad length for chunk specifier: ") + s); + + if (s[0] == 'w') + x_factor = -1; + else if (s[0] == 'e') + x_factor = 1; + else + throw sg_exception(string("Chunk specifier must begin with 'e' or 'w': " + + s)); + + if (s[4] == 's') + y_factor = -1; + else if (s[4] == 'n') + y_factor = 1; + else + throw sg_exception("Second part of chunk specifier must begin with 's' or 'n': " + s); + + + 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 + 10, y + 10, 0)); + + return bounds; +} + // end of util.cxx diff --git a/src/Lib/Geometry/util.hxx b/src/Lib/Geometry/util.hxx index 56b4562c..c1cfbdfe 100644 --- a/src/Lib/Geometry/util.hxx +++ b/src/Lib/Geometry/util.hxx @@ -14,6 +14,9 @@ #include <simgear/compiler.h> #include <simgear/math/point3d.hxx> +#include <string> +SG_USING_STD(string); + #include <Polygon/polygon.hxx> #include "line.hxx" @@ -84,4 +87,13 @@ void makePolygon (const Point3D &p, int width, FGPolygon &polygon); void makePolygon (const Line &line, int width, FGPolygon &polygon); +/** + * Parse a chunk string (like "w080n40") into a rectangle. + * + * @param s The string. + * @return A rectangle containing the bounds. + */ +Rectangle parseChunk (const string &s); + + #endif // __UTIL_HXX