More constexpr

This commit is contained in:
Jonathan 2021-10-05 23:49:15 -04:00
parent 75bcccada1
commit cb9d90744f
2 changed files with 19 additions and 18 deletions

View File

@ -24,7 +24,7 @@
/** /**
* touching a side/edge/corner is considered touching * touching a side/edge/corner is considered touching
*/ */
template<int N, class V> template<size_t N, class V>
class aabb class aabb
{ {
public: public:
@ -49,7 +49,7 @@ private:
constexpr void fix() constexpr void fix()
{ {
for (int i = 0; i < N; i++) { for (size_t i = 0; i < N; i++) {
if (m_maxs[i] < m_mins[i]) { if (m_maxs[i] < m_mins[i]) {
m_maxs[i] = m_mins[i]; m_maxs[i] = m_mins[i];
} }
@ -78,7 +78,7 @@ public:
constexpr bool disjoint(const aabb<N, V> &other, const typename V::value_type &epsilon = 0) const constexpr bool disjoint(const aabb<N, V> &other, const typename V::value_type &epsilon = 0) const
{ {
for (int i = 0; i < N; i++) { for (size_t i = 0; i < N; i++) {
if (m_maxs[i] < (other.m_mins[i] - epsilon)) if (m_maxs[i] < (other.m_mins[i] - epsilon))
return true; return true;
if (m_mins[i] > (other.m_maxs[i] + epsilon)) if (m_mins[i] > (other.m_maxs[i] + epsilon))
@ -89,7 +89,7 @@ public:
constexpr bool contains(const aabb<N, V> &other) const constexpr bool contains(const aabb<N, V> &other) const
{ {
for (int i = 0; i < 3; i++) { for (size_t i = 0; i < 3; i++) {
if (other.m_mins[i] < m_mins[i]) if (other.m_mins[i] < m_mins[i])
return false; return false;
if (other.m_maxs[i] > m_maxs[i]) if (other.m_maxs[i] > m_maxs[i])
@ -100,7 +100,7 @@ public:
constexpr bool containsPoint(const V &p) const constexpr bool containsPoint(const V &p) const
{ {
for (int i = 0; i < N; i++) { for (size_t i = 0; i < N; i++) {
if (!(p[i] >= m_mins[i] && p[i] <= m_maxs[i])) if (!(p[i] >= m_mins[i] && p[i] <= m_maxs[i]))
return false; return false;
} }
@ -109,10 +109,10 @@ public:
constexpr aabb<N, V> expand(const V &pt) const constexpr aabb<N, V> expand(const V &pt) const
{ {
V mins, maxs; V mins = m_mins, maxs = m_maxs;
for (int i = 0; i < N; i++) { for (size_t i = 0; i < N; i++) {
mins[i] = qmin(m_mins[i], pt[i]); mins[i] = qmin(mins[i], pt[i]);
maxs[i] = qmax(m_maxs[i], pt[i]); maxs[i] = qmax(maxs[i], pt[i]);
} }
return aabb<N, V>(mins, maxs); return aabb<N, V>(mins, maxs);
} }
@ -129,10 +129,10 @@ public:
constexpr intersection_t intersectWith(const aabb<N, V> &other) const constexpr intersection_t intersectWith(const aabb<N, V> &other) const
{ {
V mins, maxs; V mins = m_mins, maxs = m_maxs;
for (int i = 0; i < N; i++) { for (size_t i = 0; i < N; i++) {
mins[i] = qmax(m_mins[i], other.m_mins[i]); mins[i] = qmax(mins[i], other.m_mins[i]);
maxs[i] = qmin(m_maxs[i], other.m_maxs[i]); maxs[i] = qmin(maxs[i], other.m_maxs[i]);
if (mins[i] > maxs[i]) { if (mins[i] > maxs[i]) {
// empty intersection // empty intersection
return intersection_t(); return intersection_t();
@ -145,12 +145,12 @@ public:
constexpr aabb<N, V> grow(const V &size) const { return aabb<N, V>(m_mins - size, m_maxs + size); } constexpr aabb<N, V> grow(const V &size) const { return aabb<N, V>(m_mins - size, m_maxs + size); }
constexpr V &operator[](const int32_t &index) constexpr V &operator[](const size_t &index)
{ {
return (index == 0 ? m_mins : index == 1 ? m_maxs : throw std::exception()); return (index == 0 ? m_mins : index == 1 ? m_maxs : throw std::exception());
} }
constexpr const V &operator[](const int32_t &index) const constexpr const V &operator[](const size_t &index) const
{ {
return (index == 0 ? m_mins : index == 1 ? m_maxs : throw std::exception()); return (index == 0 ? m_mins : index == 1 ? m_maxs : throw std::exception());
} }

View File

@ -25,6 +25,7 @@
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <fmt/format.h>
#define qmax std::max #define qmax std::max
#define qmin std::min #define qmin std::min
@ -537,7 +538,7 @@ public:
// multiplication by a vector // multiplication by a vector
[[nodiscard]] qvec<M, T> operator*(const qvec<N, T> &vec) const [[nodiscard]] constexpr qvec<M, T> operator*(const qvec<N, T> &vec) const
{ {
qvec<M, T> res{}; qvec<M, T> res{};
for (size_t i = 0; i < M; i++) { // for each row for (size_t i = 0; i < M; i++) { // for each row
@ -551,7 +552,7 @@ public:
// multiplication by a matrix // multiplication by a matrix
template<int P> template<int P>
[[nodiscard]] qmat<M, P, T> operator*(const qmat<N, P, T> &other) const [[nodiscard]] constexpr qmat<M, P, T> operator*(const qmat<N, P, T> &other) const
{ {
qmat<M, P, T> res; qmat<M, P, T> res;
for (size_t i = 0; i < M; i++) { for (size_t i = 0; i < M; i++) {
@ -568,7 +569,7 @@ public:
// multiplication by a scalar // multiplication by a scalar
[[nodiscard]] qmat<M, N, T> operator*(const T scalar) const [[nodiscard]] constexpr qmat<M, N, T> operator*(const T scalar) const
{ {
qmat<M, N, T> res(*this); qmat<M, N, T> res(*this);
for (size_t i = 0; i < M * N; i++) { for (size_t i = 0; i < M * N; i++) {