Skip to content

src/core/vector.h

Namespaces

Name
dakku
dakku namespace

Classes

Name
class dakku::Vector
vector
class dakku::Point
point
class dakku::Normal
normal

Source code

#ifndef DAKKU_CORE_VECTOR_H_
#define DAKKU_CORE_VECTOR_H_
#include <core/vector_base.h>
#include <core/lua.h>

namespace dakku {

template <typename T, size_t S>
class Vector : public VectorBase<T, S, Vector<T, S>> {
 public:
  using VectorBase<T, S, Vector<T, S>>::VectorBase;

  Vector operator-() const {
    Vector ret = *this;
    for (size_t i = 0; i < S; ++i) ret[i] = -ret[i];
    return ret;
  }

  Vector operator-(const Vector &rhs) const {
    Vector ret = *this;
    ret -= rhs;
    return ret;
  }

  template <ArithmeticType V>
  Vector operator-(V rhs) const {
    Vector ret = *this;
    ret -= rhs;
    return ret;
  }
};

using Vector2i = Vector<int, 2>;
using Vector2f = Vector<float, 2>;
using Vector3i = Vector<int, 3>;
using Vector3f = Vector<float, 3>;

DAKKU_DECLARE_LUA_OBJECT(Vector2i, DAKKU_EXPORT_CORE);
DAKKU_DECLARE_LUA_OBJECT(Vector2f, DAKKU_EXPORT_CORE);
DAKKU_DECLARE_LUA_OBJECT(Vector3i, DAKKU_EXPORT_CORE);
DAKKU_DECLARE_LUA_OBJECT(Vector3f, DAKKU_EXPORT_CORE);

template <typename T, size_t S>
class Point : public VectorBase<T, S, Point<T, S>> {
 public:
  using VectorBase<T, S, Point<T, S>>::VectorBase;

  Point operator-() const {
    Point ret = *this;
    for (size_t i = 0; i < S; ++i) ret[i] = -ret[i];
    return ret;
  }

  template <ArithmeticType V>
  Point operator-(V rhs) const {
    Point ret = *this;
    ret -= rhs;
    return ret;
  }

  Vector<T, S> operator-(const Point &rhs) const {
    Point ret = *this;
    ret -= rhs;
    return Vector<T, S>{ret};
  }

  Point operator-(const Vector<T, S> &rhs) const {
    Point ret = *this;
    ret -= Point(rhs);
    return ret;
  }

  friend Point operator+(const Point &a, const Vector<T, S> &b) {
    return Point(Vector<T, S>(a) + b);
  }
};

using Point3f = Point<float, 3>;
using Point3i = Point<int, 3>;
using Point2f = Point<float, 2>;
using Point2i = Point<int, 2>;
DAKKU_DECLARE_LUA_OBJECT(Point3f, DAKKU_EXPORT_CORE);
DAKKU_DECLARE_LUA_OBJECT(Point3i, DAKKU_EXPORT_CORE);
DAKKU_DECLARE_LUA_OBJECT(Point2f, DAKKU_EXPORT_CORE);
DAKKU_DECLARE_LUA_OBJECT(Point2i, DAKKU_EXPORT_CORE);

template <typename T, size_t S>
class Normal : public VectorBase<T, S, Normal<T, S>> {
 public:
  using VectorBase<T, S, Normal<T, S>>::VectorBase;

  Normal operator-() const {
    Normal ret = *this;
    for (size_t i = 0; i < S; ++i) ret[i] = -ret[i];
    return ret;
  }

  Normal operator-(const Normal &rhs) const {
    Normal ret = *this;
    ret -= rhs;
    return ret;
  }

  template <ArithmeticType V>
  Normal operator-(V rhs) const {
    Normal ret = *this;
    ret -= rhs;
    return ret;
  }
};

using Normal3f = Normal<float, 3>;
DAKKU_DECLARE_LUA_OBJECT(Normal3f, DAKKU_EXPORT_CORE);
}  // namespace dakku
#endif

Updated on 2022-04-30 at 15:46:11 +0000