From: Thomas Geymayer Date: Tue, 15 Apr 2014 08:04:37 +0000 (+0200) Subject: math/nasal: Add more SGRect members and nasal helper. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=77955e5c3cd6e27dcf97b694440ee7b2df51f3b7;p=simgear.git math/nasal: Add more SGRect members and nasal helper. --- diff --git a/simgear/math/SGMathTest.cxx b/simgear/math/SGMathTest.cxx index ef1f5074..3308a009 100644 --- a/simgear/math/SGMathTest.cxx +++ b/simgear/math/SGMathTest.cxx @@ -19,10 +19,13 @@ # include #endif +#include + #include #include #include "SGMath.hxx" +#include "SGRect.hxx" #include "sg_random.h" template @@ -268,6 +271,33 @@ MatrixTest(void) return true; } +template +void doRectTest() +{ + SGRect rect(10, 15, 20, 25); + + COMPARE(rect.x(), 10) + COMPARE(rect.y(), 15) + COMPARE(rect.width(), 20) + COMPARE(rect.height(), 25) + + COMPARE(rect.pos(), SGVec2(10, 15)) + COMPARE(rect.size(), SGVec2(20, 25)) + + COMPARE(rect.l(), 10) + COMPARE(rect.t(), 15) + COMPARE(rect.r(), 30) + COMPARE(rect.b(), 40) + + VERIFY(rect == rect) + VERIFY(rect == SGRect(10, 15, 20, 25)) + VERIFY(rect != SGRect(11, 15, 20, 25)) + + VERIFY(rect.contains(10, 15)) + VERIFY(!rect.contains(9, 15)) + VERIFY(rect.contains(9, 15, 1)) +} + bool GeodesyTest(void) { @@ -351,6 +381,10 @@ main(void) if (!MatrixTest()) return EXIT_FAILURE; + // Do rect tests + doRectTest(); + doRectTest(); + // Check geodetic/geocentric/cartesian conversions if (!GeodesyTest()) return EXIT_FAILURE; diff --git a/simgear/math/SGRect.hxx b/simgear/math/SGRect.hxx index ad5771e9..9c51f091 100644 --- a/simgear/math/SGRect.hxx +++ b/simgear/math/SGRect.hxx @@ -88,11 +88,15 @@ class SGRect T y() const { return _min.y(); } T width() const { return _max.x() - _min.x(); } T height() const { return _max.y() - _min.y(); } + SGVec2 const& pos() const { return _min; } + SGVec2 size() const { return SGVec2(width(), height()); } void setX(T x) { T w = width(); _min.x() = x; _max.x() = x + w; } void setY(T y) { T h = height(); _min.y() = y; _max.y() = y + h; } void setWidth(T w) { _max.x() = _min.x() + w; } void setHeight(T h) { _max.y() = _min.y() + h; } + void setPos(const SGVec2& p) { setX(p.x()); setY(p.y()); } + void setSize(const SGVec2& s) { setWidth(s.x()); setHeight(s.y()); } T l() const { return _min.x(); } T r() const { return _max.x(); } @@ -129,6 +133,17 @@ class SGRect return *this; } + bool operator==(const SGRect& rhs) const + { + return _min == rhs._min + && _max == rhs._max; + } + + bool operator!=(const SGRect& rhs) const + { + return !(*this == rhs); + } + bool contains(T x, T y) const { return _min.x() <= x && x <= _max.x() @@ -176,4 +191,8 @@ std::basic_ostream& operator<<(std::basic_ostream& s, const SGRect& rect) { return s << "min = " << rect.getMin() << ", max = " << rect.getMax(); } +typedef SGRect SGRecti; +typedef SGRect SGRectf; +typedef SGRect SGRectd; + #endif /* SG_RECT_HXX_ */ diff --git a/simgear/nasal/cppbind/detail/to_nasal_helper.hxx b/simgear/nasal/cppbind/detail/to_nasal_helper.hxx index e6c8a6e4..a0173ff6 100644 --- a/simgear/nasal/cppbind/detail/to_nasal_helper.hxx +++ b/simgear/nasal/cppbind/detail/to_nasal_helper.hxx @@ -22,6 +22,8 @@ #include "nasal_traits.hxx" +#include +#include #include #include @@ -146,6 +148,19 @@ namespace nasal return to_nasal_helper(c, nasal_vec); } + //---------------------------------------------------------------------------- + template + naRef to_nasal_helper(naContext c, const SGRect& rect) + { + std::vector vec(4); + vec[0] = rect.l(); + vec[1] = rect.t(); + vec[2] = rect.r(); + vec[3] = rect.b(); + + return to_nasal_helper(c, vec); + } + //---------------------------------------------------------------------------- template naRef to_nasal_helper(naContext c, const std::map& map)