Skip to content

Commit

Permalink
Improve vector functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cjhoward committed Dec 15, 2023
1 parent f902df1 commit d2d2397
Show file tree
Hide file tree
Showing 13 changed files with 963 additions and 1,041 deletions.
12 changes: 6 additions & 6 deletions src/engine/math/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ namespace functions {
/// @{

/**
* Returns the minimum of two value.
* Returns the minimum of two values.
*
* @param a First value.
* @param b Second value.
*
* @return `a < b ? a : b`.
*/
template <std::floating_point T>
[[nodiscard]] inline constexpr T min(T a, T b) noexcept
[[nodiscard]] inline constexpr const T& min(const T& a, const T& b) noexcept
{
return a < b ? a : b;
}

/**
* Returns the maximum of two value.
* Returns the maximum of two values.
*
* @param a First value.
* @param b Second value.
*
* @return `a > b ? a : b`.
*/
template <std::floating_point T>
[[nodiscard]] inline constexpr T max(T a, T b) noexcept
[[nodiscard]] inline constexpr const T& max(const T& a, const T& b) noexcept
{
return a > b ? a : b;
}
Expand All @@ -60,7 +60,7 @@ template <std::floating_point T>
* @return @p x constrained to [`min_val`, `max_val`].
*/
template <std::floating_point T>
[[nodiscard]] inline constexpr T clamp(T x, T min_val, T max_val) noexcept
[[nodiscard]] inline constexpr const T& clamp(const T& x, const T& min_val, const T& max_val) noexcept
{
return min(max(x, min_val), max_val);
}
Expand Down Expand Up @@ -524,7 +524,7 @@ template <std::floating_point T>
}

/**
* Splits a value into its integer and fractional components.
* Splits a value into its fractional and integer components.
*
* @param x Value to split.
*
Expand Down
4 changes: 1 addition & 3 deletions src/engine/math/matrix-constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ namespace constants {
template <class T, std::size_t N, std::size_t M>
[[nodiscard]] consteval matrix<T, N, M> fill_matrix(const T& value) noexcept
{
vector<T, M> v;
v.fill(value);
matrix<T, N, M> m;
m.fill(v);
m.fill(value);
return m;
}

Expand Down
4 changes: 2 additions & 2 deletions src/engine/math/matrix-functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ constexpr matrix<T, 4, 4> inverse(const matrix<T, 4, 4>& m) noexcept
template <class T>
constexpr mat4<T> look_at_rh(const vec3<T>& position, const vec3<T>& target, const vec3<T>& up)
{
const auto f = normalize(sub(target, position));
const auto f = normalize(target - position);
const auto r = normalize(cross(f, up));
const auto u = cross(r, f);
const auto t = vec3<T>{dot(position, r), dot(position, u), dot(position, f)};
Expand All @@ -509,7 +509,7 @@ constexpr mat4<T> look_at_rh(const vec3<T>& position, const vec3<T>& target, con
template <class T>
constexpr std::tuple<mat4<T>, mat4<T>> look_at_rh_inv(const vec3<T>& position, const vec3<T>& target, const vec3<T>& up)
{
const auto f = normalize(sub(target, position));
const auto f = normalize(target - position);
const auto r = normalize(cross(f, up));
const auto u = cross(r, f);

Expand Down
31 changes: 25 additions & 6 deletions src/engine/math/matrix-types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,37 +304,37 @@ struct matrix
/// @{

/** Returns `true` if the matrix is empty, `false` otherwise. */
[[nodiscard]] inline constexpr bool empty() noexcept
[[nodiscard]] inline static consteval bool empty() noexcept
{
return element_count;
};

/** Returns the number of columns in the matrix. */
[[nodiscard]] inline constexpr size_type size() const noexcept
[[nodiscard]] inline static consteval size_type size() noexcept
{
return column_count;
};

/** Returns the number of columns in the matrix. */
[[nodiscard]] inline constexpr size_type max_size() const noexcept
[[nodiscard]] inline static consteval size_type max_size() noexcept
{
return column_count;
};

/** Returns the number of columns in the matrix. */
[[nodiscard]] inline constexpr size_type size_columns() const noexcept
[[nodiscard]] inline static consteval size_type size_columns() noexcept
{
return column_count;
};

/** Returns the number of rows in the matrix. */
[[nodiscard]] inline constexpr size_type size_rows() const noexcept
[[nodiscard]] inline static consteval size_type size_rows() noexcept
{
return row_count;
};

/** Returns the number of elements in the matrix. */
[[nodiscard]] inline constexpr size_type size_elements() const noexcept
[[nodiscard]] inline static consteval size_type size_elements() noexcept
{
return element_count;
};
Expand Down Expand Up @@ -381,6 +381,25 @@ struct matrix
};

/// @}

/// @name Comparison
/// @{

/**
* Tests two matrices for equality.
*
* @return `true` if the two matrices are equivalent, `false` otherwise.
*/
[[nodiscard]] inline constexpr friend bool operator==(const matrix&, const matrix&) noexcept = default;

/**
* Compares the columns of two matrices lexicographically.
*
* @return Lexicographical ordering of the two matrices.
*/
[[nodiscard]] inline constexpr friend auto operator<=>(const matrix&, const matrix&) noexcept = default;

/// @}
};

/// @name Tuple-like interface
Expand Down

0 comments on commit d2d2397

Please sign in to comment.