123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- #ifndef CORE_VECTOR_HPP
- #define CORE_VECTOR_HPP
- #include "math/Math.hpp"
- #include "utils/ArrayString.hpp"
- namespace Core {
- template<int N, typename T>
- class alignas(sizeof(T) * (Math::isPowerOf2(N) ? N : 1)) Vector final {
- static_assert(N > 0, "Vector size must be positive");
- T values[static_cast<unsigned int>(N)];
- public:
- Vector() {
- for(int i = 0; i < N; i++) {
- values[i] = static_cast<T>(0);
- }
- }
- template<typename OT, typename... Args>
- Vector(OT a, Args&&... args) {
- init<0>(a, args...);
- }
- private:
- template<int I>
- void init() {
- static_assert(I == N, "vector parameters do not match its size");
- }
- template<int I, typename OT, typename... Args>
- void init(OT a, Args&&... args) {
- values[I] = static_cast<T>(a);
- init<I + 1>(args...);
- }
- public:
- Vector& setAngles(float, float) = delete;
- Vector cross(const Vector&) const = delete;
- Vector& operator+=(const Vector& other) {
- for(int i = 0; i < N; i++) {
- values[i] += other.values[i];
- }
- return *this;
- }
- Vector operator+(const Vector& other) const {
- Vector v = *this;
- v += other;
- return v;
- }
- Vector& operator-=(const Vector& other) {
- for(int i = 0; i < N; i++) {
- values[i] -= other.values[i];
- }
- return *this;
- }
- Vector operator-() const {
- Vector v = *this;
- for(int i = 0; i < N; i++) {
- v.values[i] = -v.values[i];
- }
- return v;
- }
- Vector operator-(const Vector& other) const {
- Vector v = *this;
- v -= other;
- return v;
- }
- Vector& operator*=(T factor) {
- for(int i = 0; i < N; i++) {
- values[i] *= factor;
- }
- return *this;
- }
- Vector& operator*=(const Vector& other) {
- for(int i = 0; i < N; i++) {
- values[i] *= other.values[i];
- }
- return *this;
- }
- Vector operator*(T factor) const {
- Vector v = *this;
- v *= factor;
- return v;
- }
- Vector operator*(const Vector& other) const {
- Vector v = *this;
- v *= other;
- return v;
- }
- Vector& operator/=(T factor) {
- for(int i = 0; i < N; i++) {
- values[i] /= factor;
- }
- return *this;
- }
- Vector& operator/=(const Vector& other) {
- for(int i = 0; i < N; i++) {
- values[i] /= other.values[i];
- }
- return *this;
- }
- Vector operator/(T factor) const {
- Vector v = *this;
- v /= factor;
- return v;
- }
- Vector operator/(const Vector& other) const {
- Vector v = *this;
- v /= other;
- return v;
- }
- T dot(const Vector& v) const {
- T length = 0.0f;
- for(int i = 0; i < N; i++) {
- length += values[i] * v.values[i];
- }
- return length;
- }
- T squareLength() const {
- return dot(*this);
- }
- float length() const {
- return Math::squareRoot(squareLength());
- }
- Vector& normalize() {
- *this *= 1.0f / length();
- return *this;
- }
- T& operator[](int index) {
- return values[index];
- }
- const T& operator[](int index) const {
- return values[index];
- }
- Vector<N, int> toInt() const {
- Vector<N, int> cast;
- for(int i = 0; i < N; i++) {
- cast[i] = static_cast<int>(values[i]);
- }
- return cast;
- }
- Vector<N, float> toFloat() const {
- Vector<N, float> cast;
- for(int i = 0; i < N; i++) {
- cast[i] = static_cast<float>(values[i]);
- }
- return cast;
- }
- template<typename String>
- check_return Error toString(String& s) const {
- CORE_RETURN_ERROR(s.append("["));
- for(int i = 0; i < N - 1; i++) {
- CORE_RETURN_ERROR(s.append(values[i]));
- CORE_RETURN_ERROR(s.append(", "));
- }
- if(N > 0) {
- CORE_RETURN_ERROR(s.append(values[N - 1]));
- }
- return s.append("]");
- }
- };
- using Vector4 = Vector<4, float>;
- using Vector3 = Vector<3, float>;
- using Vector2 = Vector<2, float>;
- static_assert(alignof(Vector4) == sizeof(float) * 4,
- "invalid Vector4 alignment");
- static_assert(alignof(Vector3) == sizeof(float),
- "invalid Vector3 alignment");
- static_assert(alignof(Vector2) == sizeof(float) * 2,
- "invalid Vector2 alignment");
- using IntVector4 = Vector<4, int>;
- using IntVector3 = Vector<3, int>;
- using IntVector2 = Vector<2, int>;
- static_assert(alignof(IntVector4) == sizeof(int) * 4,
- "invalid IntVector4 alignment");
- static_assert(alignof(IntVector3) == sizeof(int),
- "invalid IntVector3 alignment");
- static_assert(alignof(IntVector2) == sizeof(int) * 2,
- "invalid IntVector2 alignment");
- template<>
- Vector3& Vector3::setAngles(float lengthAngle, float widthAngle);
- template<>
- Vector3 Vector3::cross(const Vector3& other) const;
- }
- template<int N, typename T>
- Core::Vector<N, T> operator*(T factor, const Core::Vector<N, T>& v) {
- return v * factor;
- }
- #endif
|