pioneer/src/vector2.h

130 lines
3.1 KiB
C
Raw Permalink Normal View History

2020-12-31 07:32:16 -08:00
// Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2012-09-15 17:59:15 -07:00
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
2012-09-12 04:38:30 -07:00
#pragma once
2012-02-03 23:17:05 -08:00
#ifndef _VECTOR2_H
#define _VECTOR2_H
#include "FloatComparison.h"
#include <math.h>
2012-02-03 23:17:05 -08:00
Speedup pilua (#4548) * Add a Rotate function to vector2 and copy-pasted some specializations to be used by Lua Copy-Paste vector3 Lua interface in LuaVector2 so it works for vector2 Expose LuaVector (vector3) and LuaVector2 (vector2) to LVM (example in 'pigui.lua') Add some utility function (used in PiGui) to LuaVector2 * mainmenu.lua use LuaVector2 Return vector2 for screen position of an object Pull a vector2d from Lua instead of a size_v * Port ui.pointOnClock in C++; Store last 'center' in ui.pointOnClock; Provide two pointOnClock overrided; Provide lineOnClock; Use pointOnClock and lineOnClock in Lua; * Eradicate dependencies of C++ from Vector.lua (see LuaPiGui); vectors in Lua are either LuaVector or LuaVector2 Remove Vector.lua Remove remaining dependencies from Vector.lua * Use length() instead of magnitude() Use length instead of magnitude in order to re-enable radar; Pass vector2 instead of vector3 for aep; Fix some calcultations because of length * Add '__newindex' function to LuaVector&LuaVector2; CheckFromLua don't return a 'const'; Use vector.x|y|z where useful; Implement some improvement here and there * New ctor for LuaTable; Avoid rehashing in 'l_pigui_get_projected_bodies'; Use new ctor of LuaTable, caching results; Some little explanation on new ctor in LuaTable; * Remove remaining '&' in Space.* * Use 'lua_typename' in LuaVector and LuaVector2; Move CalcTextAlignment in C++; Cache variable in 'displayDirectionalMarkers' * Remove 'ImVec2' push/pull, use vector2 instead * Use __index in game.lua * Bugfixes: seems vector2 assign value as reference; use 'new_index'" * Group bodies in C++ using 'Hierarchical Agglomerative Clustering'; Pass results in Lua; Print them Use reverse iterator in 'l_pigui_get_projected_bodies_grouped'; Reserve memory ahead of time; use group head to check nearest; use std::accumulate Reduce memory waste; Use std::for_each Use grouped bodies in game.lua; 'typedef' grouped bodies Avoid 'pass-the-end' error trying to group the last one * Add Print to vector2 * Use '__call' metamethod in order to initialize vectors in Lua; Name for vectors are now Vector2 and Vector3 * Split 'is_more_important_than' in a C-to-Lua part and a C++ function and use last directly in 'l_pigui_get_projected_bodies_grouped' * Use intensively Vector2 in Lua * Use forward iterator in reverse * Remove LuaMatrix.* because aren't used
2019-05-05 03:42:29 -07:00
template <typename T>
struct other_floating_type {};
template <>
struct other_floating_type<float> {
typedef double type;
};
template <>
struct other_floating_type<double> {
typedef float type;
};
template <typename T>
class vector2 {
2012-02-03 23:17:05 -08:00
public:
T x, y;
vector2() :
x(0.0f),
y(0.0f) {}
vector2(T _x, T _y) :
x(_x),
y(_y) {}
explicit vector2(int v) :
x(T(v)),
y(T(v)) {}
explicit vector2(unsigned int v) :
x(T(v)),
y(T(v)) {}
explicit vector2(T v) :
x(v),
y(v) {}
explicit vector2(const T v[2]) :
x(v[0]),
y(v[1]) {}
Speedup pilua (#4548) * Add a Rotate function to vector2 and copy-pasted some specializations to be used by Lua Copy-Paste vector3 Lua interface in LuaVector2 so it works for vector2 Expose LuaVector (vector3) and LuaVector2 (vector2) to LVM (example in 'pigui.lua') Add some utility function (used in PiGui) to LuaVector2 * mainmenu.lua use LuaVector2 Return vector2 for screen position of an object Pull a vector2d from Lua instead of a size_v * Port ui.pointOnClock in C++; Store last 'center' in ui.pointOnClock; Provide two pointOnClock overrided; Provide lineOnClock; Use pointOnClock and lineOnClock in Lua; * Eradicate dependencies of C++ from Vector.lua (see LuaPiGui); vectors in Lua are either LuaVector or LuaVector2 Remove Vector.lua Remove remaining dependencies from Vector.lua * Use length() instead of magnitude() Use length instead of magnitude in order to re-enable radar; Pass vector2 instead of vector3 for aep; Fix some calcultations because of length * Add '__newindex' function to LuaVector&LuaVector2; CheckFromLua don't return a 'const'; Use vector.x|y|z where useful; Implement some improvement here and there * New ctor for LuaTable; Avoid rehashing in 'l_pigui_get_projected_bodies'; Use new ctor of LuaTable, caching results; Some little explanation on new ctor in LuaTable; * Remove remaining '&' in Space.* * Use 'lua_typename' in LuaVector and LuaVector2; Move CalcTextAlignment in C++; Cache variable in 'displayDirectionalMarkers' * Remove 'ImVec2' push/pull, use vector2 instead * Use __index in game.lua * Bugfixes: seems vector2 assign value as reference; use 'new_index'" * Group bodies in C++ using 'Hierarchical Agglomerative Clustering'; Pass results in Lua; Print them Use reverse iterator in 'l_pigui_get_projected_bodies_grouped'; Reserve memory ahead of time; use group head to check nearest; use std::accumulate Reduce memory waste; Use std::for_each Use grouped bodies in game.lua; 'typedef' grouped bodies Avoid 'pass-the-end' error trying to group the last one * Add Print to vector2 * Use '__call' metamethod in order to initialize vectors in Lua; Name for vectors are now Vector2 and Vector3 * Split 'is_more_important_than' in a C-to-Lua part and a C++ function and use last directly in 'l_pigui_get_projected_bodies_grouped' * Use intensively Vector2 in Lua * Use forward iterator in reverse * Remove LuaMatrix.* because aren't used
2019-05-05 03:42:29 -07:00
// disallow implicit conversion between floating point sizes
explicit vector2(const vector2<typename other_floating_type<T>::type> &v);
explicit vector2(const typename other_floating_type<T>::type vals[2]);
vector2 operator+(const vector2 &v) const { return vector2(x + v.x, y + v.y); }
vector2 operator-(const vector2 &v) const { return vector2(x - v.x, y - v.y); }
vector2 &operator+=(const vector2 &v)
{
x += v.x;
y += v.y;
return *this;
}
vector2 &operator-=(const vector2 &v)
{
x -= v.x;
y -= v.y;
return *this;
}
vector2 &operator*=(const T &a)
{
x *= a;
y *= a;
return *this;
}
vector2 operator-() const { return vector2(-x, -y); }
bool operator==(const vector2 &a) const
{
2015-01-13 03:25:44 -08:00
return is_equal_exact(a.x, x) && is_equal_exact(a.y, y);
}
bool ExactlyEqual(const vector2 &a) const
{
return is_equal_exact(a.x, x) && is_equal_exact(a.y, y);
2012-02-03 23:17:05 -08:00
}
friend vector2 operator*(const vector2 &v, const T &a) { return vector2(v.x * a, v.y * a); }
friend vector2 operator*(const T &a, const vector2 &v) { return v * a; }
2020-03-13 11:50:14 -07:00
friend vector2 operator*(const vector2 &va, const vector2 &vb) { return vector2(va.x * vb.x, va.y * vb.y); }
friend vector2 operator/(const vector2 &v, const T &a) { return vector2(v.x / a, v.y / a); }
friend bool operator<(const vector2 &va, const vector2 &vb) { return va.LengthSqr() < vb.LengthSqr(); }
T Length() const { return sqrt(x * x + y * y); }
T LengthSqr() const { return x * x + y * y; }
vector2 Normalized() const
{
const T invlen = 1.0f / sqrt(x * x + y * y);
return vector2(x * invlen, y * invlen);
}
vector2 NormalizedSafe() const
{
const T lenSqr = x * x + y * y;
if (lenSqr < 1e-18) // sqrt(lenSqr) < 1e-9
return vector2(1, 0);
else {
const T invlen = sqrt(lenSqr);
return vector2(x / invlen, y / invlen);
}
}
Speedup pilua (#4548) * Add a Rotate function to vector2 and copy-pasted some specializations to be used by Lua Copy-Paste vector3 Lua interface in LuaVector2 so it works for vector2 Expose LuaVector (vector3) and LuaVector2 (vector2) to LVM (example in 'pigui.lua') Add some utility function (used in PiGui) to LuaVector2 * mainmenu.lua use LuaVector2 Return vector2 for screen position of an object Pull a vector2d from Lua instead of a size_v * Port ui.pointOnClock in C++; Store last 'center' in ui.pointOnClock; Provide two pointOnClock overrided; Provide lineOnClock; Use pointOnClock and lineOnClock in Lua; * Eradicate dependencies of C++ from Vector.lua (see LuaPiGui); vectors in Lua are either LuaVector or LuaVector2 Remove Vector.lua Remove remaining dependencies from Vector.lua * Use length() instead of magnitude() Use length instead of magnitude in order to re-enable radar; Pass vector2 instead of vector3 for aep; Fix some calcultations because of length * Add '__newindex' function to LuaVector&LuaVector2; CheckFromLua don't return a 'const'; Use vector.x|y|z where useful; Implement some improvement here and there * New ctor for LuaTable; Avoid rehashing in 'l_pigui_get_projected_bodies'; Use new ctor of LuaTable, caching results; Some little explanation on new ctor in LuaTable; * Remove remaining '&' in Space.* * Use 'lua_typename' in LuaVector and LuaVector2; Move CalcTextAlignment in C++; Cache variable in 'displayDirectionalMarkers' * Remove 'ImVec2' push/pull, use vector2 instead * Use __index in game.lua * Bugfixes: seems vector2 assign value as reference; use 'new_index'" * Group bodies in C++ using 'Hierarchical Agglomerative Clustering'; Pass results in Lua; Print them Use reverse iterator in 'l_pigui_get_projected_bodies_grouped'; Reserve memory ahead of time; use group head to check nearest; use std::accumulate Reduce memory waste; Use std::for_each Use grouped bodies in game.lua; 'typedef' grouped bodies Avoid 'pass-the-end' error trying to group the last one * Add Print to vector2 * Use '__call' metamethod in order to initialize vectors in Lua; Name for vectors are now Vector2 and Vector3 * Split 'is_more_important_than' in a C-to-Lua part and a C++ function and use last directly in 'l_pigui_get_projected_bodies_grouped' * Use intensively Vector2 in Lua * Use forward iterator in reverse * Remove LuaMatrix.* because aren't used
2019-05-05 03:42:29 -07:00
vector2 Rotate(T alpha) // Rotate around center
{
2020-03-13 11:50:14 -07:00
return vector2(x * cos(alpha) - y * sin(alpha), y * cos(alpha) + x * sin(alpha));
Speedup pilua (#4548) * Add a Rotate function to vector2 and copy-pasted some specializations to be used by Lua Copy-Paste vector3 Lua interface in LuaVector2 so it works for vector2 Expose LuaVector (vector3) and LuaVector2 (vector2) to LVM (example in 'pigui.lua') Add some utility function (used in PiGui) to LuaVector2 * mainmenu.lua use LuaVector2 Return vector2 for screen position of an object Pull a vector2d from Lua instead of a size_v * Port ui.pointOnClock in C++; Store last 'center' in ui.pointOnClock; Provide two pointOnClock overrided; Provide lineOnClock; Use pointOnClock and lineOnClock in Lua; * Eradicate dependencies of C++ from Vector.lua (see LuaPiGui); vectors in Lua are either LuaVector or LuaVector2 Remove Vector.lua Remove remaining dependencies from Vector.lua * Use length() instead of magnitude() Use length instead of magnitude in order to re-enable radar; Pass vector2 instead of vector3 for aep; Fix some calcultations because of length * Add '__newindex' function to LuaVector&LuaVector2; CheckFromLua don't return a 'const'; Use vector.x|y|z where useful; Implement some improvement here and there * New ctor for LuaTable; Avoid rehashing in 'l_pigui_get_projected_bodies'; Use new ctor of LuaTable, caching results; Some little explanation on new ctor in LuaTable; * Remove remaining '&' in Space.* * Use 'lua_typename' in LuaVector and LuaVector2; Move CalcTextAlignment in C++; Cache variable in 'displayDirectionalMarkers' * Remove 'ImVec2' push/pull, use vector2 instead * Use __index in game.lua * Bugfixes: seems vector2 assign value as reference; use 'new_index'" * Group bodies in C++ using 'Hierarchical Agglomerative Clustering'; Pass results in Lua; Print them Use reverse iterator in 'l_pigui_get_projected_bodies_grouped'; Reserve memory ahead of time; use group head to check nearest; use std::accumulate Reduce memory waste; Use std::for_each Use grouped bodies in game.lua; 'typedef' grouped bodies Avoid 'pass-the-end' error trying to group the last one * Add Print to vector2 * Use '__call' metamethod in order to initialize vectors in Lua; Name for vectors are now Vector2 and Vector3 * Split 'is_more_important_than' in a C-to-Lua part and a C++ function and use last directly in 'l_pigui_get_projected_bodies_grouped' * Use intensively Vector2 in Lua * Use forward iterator in reverse * Remove LuaMatrix.* because aren't used
2019-05-05 03:42:29 -07:00
}
void Print() const { printf("v(%f,%f)\n", x, y); }
2012-02-03 23:17:05 -08:00
};
Speedup pilua (#4548) * Add a Rotate function to vector2 and copy-pasted some specializations to be used by Lua Copy-Paste vector3 Lua interface in LuaVector2 so it works for vector2 Expose LuaVector (vector3) and LuaVector2 (vector2) to LVM (example in 'pigui.lua') Add some utility function (used in PiGui) to LuaVector2 * mainmenu.lua use LuaVector2 Return vector2 for screen position of an object Pull a vector2d from Lua instead of a size_v * Port ui.pointOnClock in C++; Store last 'center' in ui.pointOnClock; Provide two pointOnClock overrided; Provide lineOnClock; Use pointOnClock and lineOnClock in Lua; * Eradicate dependencies of C++ from Vector.lua (see LuaPiGui); vectors in Lua are either LuaVector or LuaVector2 Remove Vector.lua Remove remaining dependencies from Vector.lua * Use length() instead of magnitude() Use length instead of magnitude in order to re-enable radar; Pass vector2 instead of vector3 for aep; Fix some calcultations because of length * Add '__newindex' function to LuaVector&LuaVector2; CheckFromLua don't return a 'const'; Use vector.x|y|z where useful; Implement some improvement here and there * New ctor for LuaTable; Avoid rehashing in 'l_pigui_get_projected_bodies'; Use new ctor of LuaTable, caching results; Some little explanation on new ctor in LuaTable; * Remove remaining '&' in Space.* * Use 'lua_typename' in LuaVector and LuaVector2; Move CalcTextAlignment in C++; Cache variable in 'displayDirectionalMarkers' * Remove 'ImVec2' push/pull, use vector2 instead * Use __index in game.lua * Bugfixes: seems vector2 assign value as reference; use 'new_index'" * Group bodies in C++ using 'Hierarchical Agglomerative Clustering'; Pass results in Lua; Print them Use reverse iterator in 'l_pigui_get_projected_bodies_grouped'; Reserve memory ahead of time; use group head to check nearest; use std::accumulate Reduce memory waste; Use std::for_each Use grouped bodies in game.lua; 'typedef' grouped bodies Avoid 'pass-the-end' error trying to group the last one * Add Print to vector2 * Use '__call' metamethod in order to initialize vectors in Lua; Name for vectors are now Vector2 and Vector3 * Split 'is_more_important_than' in a C-to-Lua part and a C++ function and use last directly in 'l_pigui_get_projected_bodies_grouped' * Use intensively Vector2 in Lua * Use forward iterator in reverse * Remove LuaMatrix.* because aren't used
2019-05-05 03:42:29 -07:00
template <>
inline vector2<float>::vector2() {}
template <>
inline vector2<double>::vector2() {}
template <>
inline vector2<float>::vector2(const vector2<double> &v) :
x(float(v.x)),
y(float(v.y)) {}
template <>
inline vector2<double>::vector2(const vector2<float> &v) :
x(v.x),
y(v.y) {}
typedef vector2<float> vector2f;
typedef vector2<double> vector2d;
2012-02-03 23:17:05 -08:00
#endif