Skip to content

Commit

Permalink
feat(api): adds helper methods to Vec2 class
Browse files Browse the repository at this point in the history
  • Loading branch information
wpumacay committed Apr 30, 2023
1 parent ddf774c commit 8816439
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
28 changes: 28 additions & 0 deletions include/math/vec2_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace math {

// ***************************************************************************//
// Vector2 helper functions and operators //
// ***************************************************************************//

template <typename T>
using SFINAE_VEC2_GUARD = typename std::enable_if<IsScalar<T>::value>::type*;

Expand Down Expand Up @@ -269,4 +273,28 @@ auto operator>>(std::istream& input_stream, Vector2<T>& dst) -> std::istream& {
return input_stream;
}

// ***************************************************************************//
// Vector2-type methods //
// ***************************************************************************//

template <typename T>
auto Vector2<T>::lengthSquare() const -> T {
return ::math::squareNorm<T>(*this);
}

template <typename T>
auto Vector2<T>::length() const -> T {
return ::math::norm<T>(*this);
}

template <typename T>
auto Vector2<T>::normalize() -> void {
::math::normalize_in_place<T>(*this);
}

template <typename T>
auto Vector2<T>::normalized() const -> Vector2<T> {
return ::math::normalize<T>(*this);
}

} // namespace math
20 changes: 16 additions & 4 deletions include/math/vec2_t_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ struct Vector2 {
/// Number of dimensions of this vector (as in numpy.ndarray.ndim)
static constexpr uint32_t VECTOR_NDIM = 1;

/// Type alias of the vector
// Some handy type aliases used throught the codebase
using Type = Vector2<T>;
/// Type alias of the scalar used for this vector (float32|64)
using ElementType = T;
/// Type alias of the internal storage used for the vector (i.e. std::array)
using BufferType = std::array<T, BUFFER_SIZE>;

/// Constructs a zero-initialized vector
Expand All @@ -58,13 +56,27 @@ struct Vector2 {
m_Elements[1] = y_coord;
}

// cppcheck-suppress noExplicitConstructor
/// Constructs a vector from an initializer list of the form {x, y}
Vector2(const std::initializer_list<T>& values) {
assert(values.size() == Vector2<T>::VECTOR_SIZE);
std::copy(values.begin(), values.end(), m_Elements.data());
}

/// Returns the square of the length of this vector
auto lengthSquare() const -> T;

/// Returns the length of this vector
auto length() const -> T;

/// Normalizes this vector in place
auto normalize() -> void;

/// Returns the normalized version of this vector
auto normalized() const -> Vector2<T>;

/// Returns the dot product of this vector with the given vector
auto dot(const Vector2<T>& rhs) const -> T;

/// Returns a mutable reference to the x-component of the vector
auto x() -> T& { return m_Elements[0]; }

Expand Down
16 changes: 15 additions & 1 deletion python/math3d/bindings/vec2_py.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,22 @@ auto bindings_vector2(py::module& m, const char* class_name) -> void {
VECTOR_PROPERTY(x)
VECTOR_PROPERTY(y)
VECTOR_OPERATORS(T)
VECTOR_METHODS(T)
VECTOR_GETSET_ITEM(2, T)
.def("dot", [](const Class& self, const Class& other) -> T {
return self.dot(other);
})
.def("norm", [](const Class& self) -> T {
return self.length();
})
.def("squareNorm", [](const Class& self) -> T {
return self.lengthSquare();
})
.def("normalize", [](const Class& self) -> Class {
return self.normalized();
})
.def("normalize_", [](Class& self) -> void {
self.normalize();
})
// clant-format on
.def("numpy", [](const Class& self) -> py::array_t<T> {
return ::math::vec2_to_nparray<T>(self);
Expand Down

0 comments on commit 8816439

Please sign in to comment.