Skip to content

Commit

Permalink
implement all free and member functions for our vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii committed Nov 1, 2023
1 parent d1eab51 commit 72c9ab6
Show file tree
Hide file tree
Showing 12 changed files with 637 additions and 87 deletions.
54 changes: 54 additions & 0 deletions include/mim/detail/func/func_vector1.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace mim
{
/// Member Functions

template <typename T>
constexpr T vec<1, T>::length() const
Expand Down Expand Up @@ -68,9 +69,62 @@ namespace mim
return (x - v.x) * (x - v.x);
}

template <typename T>
constexpr vec<1, T> vec<1, T>::hadamard(const vec<1, T> & v) const
{
static_assert(std::is_arithmetic<T>::value, "Cannot use hadamard() on a non-arithmetic vector.");
return this->x * v.x;
}

/// Free functions

template <typename T>
constexpr T length(vec<1, T> const& v)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use length() on a non-arithmetic vector.");
return v.length();
}

template <typename T>
constexpr T length_squared(vec<1, T> const& v)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use length_squared() on a non-arithmetic vector.");
return v.length_squared();
}

template <typename T>
constexpr vec<1, T> normalized(vec<1, T> const& v)
{
static_assert(std::is_floating_point<T>::value, "Cannot normalize a non-floating-point vector.");
return v.normalized();
}

template <typename T>
constexpr bool is_normalized(vec<1, T> const& v)
{
static_assert(std::is_floating_point<T>::value, "Cannot normalize a non-floating-point vector.");
return v.is_normalized();
}

template <typename T>
constexpr T distance(vec<1, T> const& v1, vec<1, T> const& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use distance() on a non-arithmetic vector.");
return v1.distance(v2);
}

template <typename T>
constexpr T distance_squared(vec<1, T> const& v1, vec<1, T> const& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use distance_squared() on a non-arithmetic vector.");
return v1.distance_squared(v2);
}

template <typename T>
constexpr vec<1, T> hadamard(vec<1, T> const& v1, vec<1, T> const& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use hadamard() on a non-arithmetic vector.");
return v1.hadamard(v2);
}

}
124 changes: 121 additions & 3 deletions include/mim/detail/func/func_vector2.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace mim
{
/// Member Functions

template <typename T>
constexpr T vec<2, T>::length() const
Expand Down Expand Up @@ -69,16 +70,31 @@ namespace mim
return (x - v.x) * (x - v.x) + (y - v.y) * (y - v.y);
}


/// Functions

template <typename T>
constexpr T vec<2, T>::dot(const vec<2, T>& v) const
{
static_assert(std::is_arithmetic<T>::value, "Cannot use the dot product on a non-arithmetic vector.");
return x * v.x + y * v.y;
}

template <typename T>
constexpr vec<2, T> vec<2, T>::cross(const vec<2, T>& v) const
{
static_assert(std::is_arithmetic<T>::value, "Cannot use the cross product on a non-arithmetic vector.");

// Mathematically a cross product is normally only valid for 3D vectors.
// However, we can still compute a cross product for 2D vectors using the wedge product.
// This is particularly useful for game development thus we allow it.
return vec<2, T>(this->x * v.y - this->y * v.x);
}

template <typename T>
constexpr vec<2, T> vec<2, T>::hadamard(const vec<2, T>& v) const
{
static_assert(std::is_arithmetic<T>::value, "Cannot use hadamard() on a non-arithmetic vector.");
return vec<2, T>(x * v.x, y * v.y);
}

// TODO: Implement rotate()?

// TODO: Make this constexpr once sine and cosine are constexpr.
Expand Down Expand Up @@ -132,4 +148,106 @@ namespace mim
}


/// Free functions

template <typename T>
constexpr T length(const vec<2, T>& v)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use length() on a non-arithmetic vector.");
return v.length();
}

template <typename T>
constexpr T length_squared(const vec<2, T>& v)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use length_squared() on a non-arithmetic vector.");
return v.length_squared();
}

template <typename T>
constexpr vec<2, T> normalized(const vec<2, T>& v)
{
static_assert(std::is_floating_point<T>::value, "Cannot normalize a non-floating-point vector.");
return v.normalized();
}

template <typename T>
constexpr bool is_normalized(const vec<2, T>& v)
{
static_assert(std::is_floating_point<T>::value, "Cannot normalize a non-floating-point vector.");
return v.is_normalized();
}

template <typename T>
constexpr T distance(const vec<2, T>& v1, const vec<2, T>& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use distance() on a non-arithmetic vector.");
return v1.distance(v2);
}

template <typename T>
constexpr T distance_squared(const vec<2, T>& v1, const vec<2, T>& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use distance_squared() on a non-arithmetic vector.");
return v1.distance_squared(v2);
}

template <typename T>
constexpr T dot(const vec<2, T>& v1, const vec<2, T>& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use the dot product on a non-arithmetic vector.");
return v1.dot(v2);
}

template <typename T>
constexpr vec<2, T> cross(const vec<2, T>& v1, const vec<2, T>& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use the cross product on a non-arithmetic vector.");
return v1.cross(v2);
}

template <typename T>
constexpr vec<2, T> hadamard(const vec<2, T>& v1, const vec<2, T>& v2)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use hadamard() on a non-arithmetic vector.");
return v1.hadamard(v2);
}

template <typename T>
constexpr vec<2, T> rotated(const vec<2, T>& v, T angle)
{
static_assert(std::is_arithmetic<T>::value, "Cannot rotate a non-arithmetic vector.");
return v.rotated(angle);
}

template <typename T>
constexpr vec<2, T> clamp(const vec<2, T>& v, const vec<2, T>& min, const vec<2, T>& max)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use clamp() on a non-arithmetic vector.");
return v.clamp(min, max);
}

template <typename T>
constexpr vec<2, T> reflect(const vec<2, T>& v, const vec<2, T>& normal)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use reflect() on a non-arithmetic vector.");
return v.reflect(normal);
}

template <typename T>
constexpr vec<2, T> refract(const vec<2, T>& v, const vec<2, T>& normal, T eta)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use refract() on a non-arithmetic vector.");
return v.refract(normal, eta);
}

template <typename T>
constexpr vec<2, T> project(const vec<2, T>& v, const vec<2, T>& to)
{
static_assert(std::is_arithmetic<T>::value, "Cannot use project() on a non-arithmetic vector.");
return v.project(to);
}



}
Loading

0 comments on commit 72c9ab6

Please sign in to comment.