Skip to content

Commit

Permalink
Add: Unary vector operators, vector reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed May 4, 2024
1 parent 1054baf commit ea85d9a
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions include/fastgltf/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ namespace fastgltf::math {
return *this;
}

constexpr auto operator+() const noexcept {
vec<T, N> ret;
for (std::size_t i = 0; i < N; ++i)
ret[i] = +(*this)[i];
return ret;
}
constexpr auto operator+(T scalar) const noexcept {
return vec<T, N>(*this) += scalar;
}
Expand All @@ -207,6 +213,12 @@ namespace fastgltf::math {
return ret;
}

constexpr auto operator-() const noexcept {
vec<T, N> ret;
for (std::size_t i = 0; i < N; ++i)
ret[i] = -(*this)[i];
return ret;
}
constexpr auto operator-(T scalar) const noexcept {
return vec<T, N>(*this) -= scalar;
}
Expand Down Expand Up @@ -242,15 +254,21 @@ namespace fastgltf::math {
}
};

/** Computes the dot product of two vectors */
/** Reduction sum of a single vector */
template <typename T, std::size_t N>
[[nodiscard]] auto dot(const vec<T, N>& a, const vec<T, N>& b) noexcept {
T ret = a.x() * b.x();
[[nodiscard]] auto sum(const vec<T, N>& a) noexcept {
T ret = a.x();
for (std::size_t i = 1; i < N; ++i)
ret += a[i] * b[i];
ret += a[i];
return ret;
}

/** Computes the dot product of two vectors */
template <typename T, std::size_t N>
[[nodiscard]] auto dot(const vec<T, N>& a, const vec<T, N>& b) noexcept {
return sum(a * b);
}

/** Computes the 3D cross product of two vectors */
template <typename T>
[[nodiscard]] auto cross(const vec<T, 3>& a, const vec<T, 3>& b) noexcept {
Expand Down Expand Up @@ -297,9 +315,15 @@ namespace fastgltf::math {
using fvec2 = vec<float, 2>;
using fvec3 = vec<float, 3>;
using fvec4 = vec<float, 4>;
using f32vec2 = vec<float, 2>;
using f32vec3 = vec<float, 3>;
using f32vec4 = vec<float, 4>;
using dvec2 = vec<double, 2>;
using dvec3 = vec<double, 3>;
using dvec4 = vec<double, 4>;
using f64vec2 = vec<double, 2>;
using f64vec3 = vec<double, 3>;
using f64vec4 = vec<double, 4>;

/** A quaternion */
template <typename T>
Expand Down

0 comments on commit ea85d9a

Please sign in to comment.