math/nasal: Add more SGRect members and nasal helper.

This commit is contained in:
Thomas Geymayer
2014-04-15 10:04:37 +02:00
parent 19481983e5
commit 77955e5c3c
3 changed files with 68 additions and 0 deletions

View File

@@ -19,10 +19,13 @@
# include <simgear_config.h>
#endif
#include <simgear/misc/test_macros.hxx>
#include <cstdlib>
#include <iostream>
#include "SGMath.hxx"
#include "SGRect.hxx"
#include "sg_random.h"
template<typename T>
@@ -268,6 +271,33 @@ MatrixTest(void)
return true;
}
template<typename T>
void doRectTest()
{
SGRect<T> 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<T>(10, 15))
COMPARE(rect.size(), SGVec2<T>(20, 25))
COMPARE(rect.l(), 10)
COMPARE(rect.t(), 15)
COMPARE(rect.r(), 30)
COMPARE(rect.b(), 40)
VERIFY(rect == rect)
VERIFY(rect == SGRect<T>(10, 15, 20, 25))
VERIFY(rect != SGRect<T>(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<double>())
return EXIT_FAILURE;
// Do rect tests
doRectTest<int>();
doRectTest<double>();
// Check geodetic/geocentric/cartesian conversions
if (!GeodesyTest())
return EXIT_FAILURE;

View File

@@ -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<T> const& pos() const { return _min; }
SGVec2<T> size() const { return SGVec2<T>(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<T>& p) { setX(p.x()); setY(p.y()); }
void setSize(const SGVec2<T>& 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<T>& rhs) const
{
return _min == rhs._min
&& _max == rhs._max;
}
bool operator!=(const SGRect<T>& 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<char_type, traits_type>&
operator<<(std::basic_ostream<char_type, traits_type>& s, const SGRect<T>& rect)
{ return s << "min = " << rect.getMin() << ", max = " << rect.getMax(); }
typedef SGRect<int> SGRecti;
typedef SGRect<float> SGRectf;
typedef SGRect<double> SGRectd;
#endif /* SG_RECT_HXX_ */

View File

@@ -22,6 +22,8 @@
#include "nasal_traits.hxx"
#include <simgear/math/SGMath.hxx>
#include <simgear/math/SGRect.hxx>
#include <simgear/nasal/nasal.h>
#include <boost/function/function_fwd.hpp>
@@ -146,6 +148,19 @@ namespace nasal
return to_nasal_helper(c, nasal_vec);
}
//----------------------------------------------------------------------------
template<class T>
naRef to_nasal_helper(naContext c, const SGRect<T>& rect)
{
std::vector<float> 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<class Value>
naRef to_nasal_helper(naContext c, const std::map<std::string, Value>& map)