|
|
@@ -29,367 +29,250 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
#include "Assert.h"
|
|
|
#include "Types.h"
|
|
|
#include "MathUtils.h"
|
|
|
+#include "MathTypes.h"
|
|
|
|
|
|
namespace crown
|
|
|
{
|
|
|
|
|
|
-/// 2D column vector.
|
|
|
-struct Vector2
|
|
|
-{
|
|
|
-public:
|
|
|
-
|
|
|
- float x, y;
|
|
|
-
|
|
|
- /// Does nothing for efficiency.
|
|
|
- Vector2();
|
|
|
-
|
|
|
- /// Initializes all the components to val
|
|
|
- Vector2(float val);
|
|
|
-
|
|
|
- /// Constructs from two components
|
|
|
- Vector2(float nx, float ny);
|
|
|
-
|
|
|
- /// Constructs from array
|
|
|
- Vector2(const float v[2]);
|
|
|
- Vector2(const Vector2& a);
|
|
|
+/// Negates @a a and returns the result.
|
|
|
+Vector2 operator-(const Vector2& a);
|
|
|
|
|
|
- /// Random access by index
|
|
|
- float operator[](uint32_t i) const;
|
|
|
+/// Adds the vector @a to @b and returns the result.
|
|
|
+Vector2 operator+(Vector2 a, const Vector2& b);
|
|
|
|
|
|
- /// Random access by index
|
|
|
- float& operator[](uint32_t i);
|
|
|
+/// Subtracts the vector @a b from @a a and returns the result.
|
|
|
+Vector2 operator-(Vector2 a, const Vector2& b);
|
|
|
|
|
|
- Vector2 operator+(const Vector2& a) const;
|
|
|
- Vector2& operator+=(const Vector2& a);
|
|
|
- Vector2 operator-(const Vector2& a) const;
|
|
|
- Vector2& operator-=(const Vector2& a);
|
|
|
- Vector2 operator*(float k) const;
|
|
|
- Vector2& operator*=(float k);
|
|
|
- Vector2 operator/(float k) const;
|
|
|
- Vector2& operator/=(float k);
|
|
|
+/// Multiplies the vector @a a by the scalar @a k and returns the result.
|
|
|
+Vector2 operator*(Vector2 a, float k);
|
|
|
|
|
|
- /// Dot product
|
|
|
- float dot(const Vector2& a) const;
|
|
|
+/// @copydoc operator*(Vector2, float)
|
|
|
+Vector2 operator*(float k, Vector2 a);
|
|
|
|
|
|
- /// For simmetry
|
|
|
- friend Vector2 operator*(float k, const Vector2& a);
|
|
|
+/// Divides the vector @a a by the scalar @a k and returns the result.
|
|
|
+Vector2 operator/(Vector2 a, float k);
|
|
|
|
|
|
- bool operator==(const Vector2& other) const;
|
|
|
- bool operator!=(const Vector2& other) const;
|
|
|
+/// Returns true whether the vectors @a a and @a b are equal.
|
|
|
+bool operator==(const Vector2& a, const Vector2& b);
|
|
|
|
|
|
- /// Returns whether all the components of this vector are smaller than all of the @a other vector
|
|
|
- bool operator<(const Vector2& other) const;
|
|
|
+namespace vector2
|
|
|
+{
|
|
|
+ const Vector2 ZERO = Vector2(0, 0);
|
|
|
|
|
|
- /// Returns whether all the components of this vector are greater than all of the @a other vector
|
|
|
- bool operator>(const Vector2& other) const;
|
|
|
+ /// Dot product
|
|
|
+ float dot(const Vector2& a, const Vector2& b);
|
|
|
|
|
|
/// Returns the vector's length
|
|
|
- float length() const;
|
|
|
+ float length(const Vector2& a);
|
|
|
|
|
|
/// Returns the vector's squared length
|
|
|
- float squared_length() const;
|
|
|
+ float squared_length(const Vector2& a);
|
|
|
|
|
|
/// Sets the vector's length
|
|
|
- void set_length(float len);
|
|
|
- float get_angle() const;
|
|
|
- float get_angle_2d() const;
|
|
|
+ void set_length(Vector2& a, float len);
|
|
|
|
|
|
/// Normalizes the vector
|
|
|
- Vector2& normalize();
|
|
|
-
|
|
|
- /// Returns the normalized vector
|
|
|
- Vector2 get_normalized() const;
|
|
|
-
|
|
|
- /// Negates the vector (i.e. builds the inverse)
|
|
|
- Vector2& negate();
|
|
|
-
|
|
|
- /// Negates the vector (i.e. builds the inverse)
|
|
|
- Vector2 operator-() const;
|
|
|
+ Vector2 normalize(Vector2& a);
|
|
|
|
|
|
/// Returns the distance
|
|
|
- float get_distance_to(const Vector2& a) const;
|
|
|
-
|
|
|
- /// Returns the angle in radian
|
|
|
- float get_angle_between(const Vector2& a) const;
|
|
|
+ float distance(const Vector2& a, const Vector2& b);
|
|
|
|
|
|
- /// Sets all components to zero
|
|
|
- void zero();
|
|
|
+ /// Returns the angle between
|
|
|
+ float angle(const Vector2& a, const Vector2& b);
|
|
|
|
|
|
/// Returns the pointer to the vector's data
|
|
|
- float* to_float_ptr();
|
|
|
+ float* to_float_ptr(Vector2& a);
|
|
|
|
|
|
- /// Returns the pointer to the vector's data
|
|
|
- const float* to_float_ptr() const;
|
|
|
-
|
|
|
- static const Vector2 ZERO;
|
|
|
- static const Vector2 ONE;
|
|
|
- static const Vector2 XAXIS;
|
|
|
- static const Vector2 YAXIS;
|
|
|
-};
|
|
|
+ /// Returns the pointer to the vector's data
|
|
|
+ const float* to_float_ptr(const Vector2& a);
|
|
|
+} // namespace vector2
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2::Vector2()
|
|
|
+inline Vector2 operator-(const Vector2& a)
|
|
|
{
|
|
|
+ return Vector2(-a.x, -a.y);
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2::Vector2(float val) : x(val), y(val)
|
|
|
+inline Vector2 operator+(Vector2 a, const Vector2& b)
|
|
|
{
|
|
|
+ a += b;
|
|
|
+ return a;
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2::Vector2(float nx, float ny) : x(nx), y(ny)
|
|
|
+inline Vector2 operator-(Vector2 a, const Vector2& b)
|
|
|
{
|
|
|
+ a -= b;
|
|
|
+ return a;
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2::Vector2(const float a[2]) : x(a[0]), y(a[1])
|
|
|
+inline Vector2 operator*(Vector2 a, float k)
|
|
|
{
|
|
|
+ a *= k;
|
|
|
+ return a;
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2::Vector2(const Vector2& a) : x(a.x), y(a.y)
|
|
|
+inline Vector2 operator*(float k, Vector2 a)
|
|
|
{
|
|
|
+ a *= k;
|
|
|
+ return a;
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::operator[](uint32_t i) const
|
|
|
+inline Vector2 operator/(Vector2 a, float k)
|
|
|
{
|
|
|
- CE_ASSERT(i < 2, "Index must be < 2");
|
|
|
-
|
|
|
- return (&x)[i];
|
|
|
+ a /= k;
|
|
|
+ return a;
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline float& Vector2::operator[](uint32_t i)
|
|
|
+inline bool operator==(const Vector2& a, const Vector2& b)
|
|
|
{
|
|
|
- CE_ASSERT(i < 2, "Index must be < 2");
|
|
|
-
|
|
|
- return (&x)[i];
|
|
|
+ return math::equals(a.x, b.x) && math::equals(a.y, b.y);
|
|
|
}
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 Vector2::operator+(const Vector2& a) const
|
|
|
-{
|
|
|
- return Vector2(x + a.x, y + a.y);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2& Vector2::operator+=(const Vector2& a)
|
|
|
+namespace vector2
|
|
|
{
|
|
|
- x += a.x;
|
|
|
- y += a.y;
|
|
|
|
|
|
- return *this;
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 Vector2::operator-(const Vector2& a) const
|
|
|
-{
|
|
|
- return Vector2(x - a.x, y - a.y);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2& Vector2::operator-=(const Vector2& a)
|
|
|
-{
|
|
|
- x -= a.x;
|
|
|
- y -= a.y;
|
|
|
-
|
|
|
- return *this;
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 Vector2::operator*(float k) const
|
|
|
-{
|
|
|
- return Vector2(x * k, y * k);
|
|
|
-}
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline float dot(const Vector2& a, const Vector2& b)
|
|
|
+ {
|
|
|
+ return a.x * b.x + a.y * b.y;
|
|
|
+ }
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2& Vector2::operator*=(float k)
|
|
|
-{
|
|
|
- x *= k;
|
|
|
- y *= k;
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline float length(const Vector2& a)
|
|
|
+ {
|
|
|
+ return math::sqrt(a.x * a.x + a.y * a.y);
|
|
|
+ }
|
|
|
|
|
|
- return *this;
|
|
|
-}
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline float squared_length(const Vector2& a)
|
|
|
+ {
|
|
|
+ return a.x * a.x + a.y * a.y;
|
|
|
+ }
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 Vector2::operator/(float k) const
|
|
|
-{
|
|
|
- CE_ASSERT(k != (float)0.0, "Division by zero");
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline void set_length(Vector2& a, float len)
|
|
|
+ {
|
|
|
+ normalize(a);
|
|
|
|
|
|
- float inv = (float)(1.0 / k);
|
|
|
+ a.x *= len;
|
|
|
+ a.y *= len;
|
|
|
+ }
|
|
|
|
|
|
- return Vector2(x * inv, y * inv);
|
|
|
-}
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline Vector2 normalize(Vector2& a)
|
|
|
+ {
|
|
|
+ float inv_len = 1.0 / length(a);
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2& Vector2::operator/=(float k)
|
|
|
-{
|
|
|
- CE_ASSERT(k != (float)0.0, "Division by zero");
|
|
|
+ a.x *= inv_len;
|
|
|
+ a.y *= inv_len;
|
|
|
|
|
|
- float inv = (float)(1.0 / k);
|
|
|
+ return a;
|
|
|
+ }
|
|
|
|
|
|
- x *= inv;
|
|
|
- y *= inv;
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline float distance(const Vector2& a, const Vector2& b)
|
|
|
+ {
|
|
|
+ return length(b - a);
|
|
|
+ }
|
|
|
|
|
|
- return *this;
|
|
|
-}
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline float angle(const Vector2& a, const Vector2& b)
|
|
|
+ {
|
|
|
+ return math::acos(dot(a, b) / (length(a) * length(b)));
|
|
|
+ }
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::dot(const Vector2& a) const
|
|
|
-{
|
|
|
- return x * a.x + y * a.y;
|
|
|
-}
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline float* to_float_ptr(Vector2& a)
|
|
|
+ {
|
|
|
+ return &a.x;
|
|
|
+ }
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline bool Vector2::operator==(const Vector2& other) const
|
|
|
-{
|
|
|
- return math::equals(x, other.x) && math::equals(y, other.y);
|
|
|
-}
|
|
|
+ //-----------------------------------------------------------------------------
|
|
|
+ inline const float* to_float_ptr(const Vector2& a)
|
|
|
+ {
|
|
|
+ return &a.x;
|
|
|
+ }
|
|
|
+} // namespace vector2
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline bool Vector2::operator!=(const Vector2& other) const
|
|
|
+inline Vector2::Vector2()
|
|
|
{
|
|
|
- return !math::equals(x, other.x) || !math::equals(y, other.y);
|
|
|
+ // Do not initialize
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline bool Vector2::operator<(const Vector2& other) const
|
|
|
+inline Vector2::Vector2(float val) : x(val), y(val)
|
|
|
{
|
|
|
- return ((x < other.x) && (y < other.y));
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline bool Vector2::operator>(const Vector2& other) const
|
|
|
+inline Vector2::Vector2(float nx, float ny) : x(nx), y(ny)
|
|
|
{
|
|
|
- return ((x > other.x) && (y > other.y));
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::length() const
|
|
|
+inline Vector2::Vector2(const float a[2]) : x(a[0]), y(a[1])
|
|
|
{
|
|
|
- return math::sqrt(x * x + y * y);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::squared_length() const
|
|
|
+inline Vector2::Vector2(const Vector2& a) : x(a.x), y(a.y)
|
|
|
{
|
|
|
- return x * x + y * y;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline void Vector2::set_length(float len)
|
|
|
+inline float Vector2::operator[](uint32_t i) const
|
|
|
{
|
|
|
- normalize();
|
|
|
+ CE_ASSERT(i < 2, "Index must be < 2");
|
|
|
|
|
|
- x *= len;
|
|
|
- y *= len;
|
|
|
+ return (&x)[i];
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::get_angle() const
|
|
|
+inline float& Vector2::operator[](uint32_t i)
|
|
|
{
|
|
|
- return math::atan2(y, x);
|
|
|
-}
|
|
|
+ CE_ASSERT(i < 2, "Index must be < 2");
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::get_angle_2d() const
|
|
|
-{
|
|
|
- return math::atan2(-y, x);
|
|
|
+ return (&x)[i];
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline Vector2& Vector2::normalize()
|
|
|
+inline Vector2& Vector2::operator+=(const Vector2& a)
|
|
|
{
|
|
|
- float len = length();
|
|
|
-
|
|
|
- if (math::equals(len, (float)0.0))
|
|
|
- {
|
|
|
- return *this;
|
|
|
- }
|
|
|
-
|
|
|
- x /= len;
|
|
|
- y /= len;
|
|
|
+ x += a.x;
|
|
|
+ y += a.y;
|
|
|
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 Vector2::get_normalized() const
|
|
|
-{
|
|
|
- Vector2 tmp(x, y);
|
|
|
-
|
|
|
- return tmp.normalize();
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2& Vector2::negate()
|
|
|
+inline Vector2& Vector2::operator-=(const Vector2& a)
|
|
|
{
|
|
|
- x = -x;
|
|
|
- y = -y;
|
|
|
+ x -= a.x;
|
|
|
+ y -= a.y;
|
|
|
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 Vector2::operator-() const
|
|
|
-{
|
|
|
- return Vector2(-x, -y);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::get_distance_to(const Vector2& a) const
|
|
|
-{
|
|
|
- return (*this - a).length();
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline float Vector2::get_angle_between(const Vector2& a) const
|
|
|
-{
|
|
|
- return math::acos(this->dot(a) / (this->length() * a.length()));
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline void Vector2::zero()
|
|
|
-{
|
|
|
- x = 0.0;
|
|
|
- y = 0.0;
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline float* Vector2::to_float_ptr()
|
|
|
+inline Vector2& Vector2::operator*=(float k)
|
|
|
{
|
|
|
- return &x;
|
|
|
-}
|
|
|
+ x *= k;
|
|
|
+ y *= k;
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline const float* Vector2::to_float_ptr() const
|
|
|
-{
|
|
|
- return &x;
|
|
|
+ return *this;
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 get_projected_parallel(const Vector2& v, const Vector2& n)
|
|
|
+inline Vector2& Vector2::operator/=(float k)
|
|
|
{
|
|
|
- float n_len_q;
|
|
|
- n_len_q = n.length();
|
|
|
- n_len_q = n_len_q * n_len_q;
|
|
|
+ CE_ASSERT(k != (float)0.0, "Division by zero");
|
|
|
|
|
|
- return n * (v.dot(n) / n_len_q);
|
|
|
-}
|
|
|
+ float inv = (float)(1.0 / k);
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 get_projected_perpendicular(const Vector2& v, const Vector2& n)
|
|
|
-{
|
|
|
- return v - get_projected_parallel(v, n);
|
|
|
-}
|
|
|
+ x *= inv;
|
|
|
+ y *= inv;
|
|
|
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-inline Vector2 operator*(float k, const Vector2& a)
|
|
|
-{
|
|
|
- return a * k;
|
|
|
+ return *this;
|
|
|
}
|
|
|
|
|
|
} // namespace crown
|
|
|
-
|