diff --git a/src/geometry.h b/src/geometry.h index e808694c6..c415221e1 100644 --- a/src/geometry.h +++ b/src/geometry.h @@ -94,9 +94,10 @@ std::array resize_array(std::array value) { } class _no_z_member {}; -template ::value)>> +template struct _has_z_member { + static_assert(std::is_arithmetic_v, + "vector z member type (T) must be a number"); using ComponentRef = _member_access; /// @brief vec z value. @@ -106,9 +107,10 @@ struct _has_z_member { }; class _no_w_member {}; -template ::value)>> +template struct _has_w_member { + static_assert(std::is_arithmetic_v, + "vector w member type (T) must be a number"); using ComponentRef = _member_access; /// @brief vec w value. @@ -198,14 +200,14 @@ struct vec inline T get_y() const { return this->at(1); } /// @brief vec z component. /// @return z value of this vec. - template = 3)>> inline T get_z() const { + static_assert(Length >= 3, "vector doesn't have a z component"); return this->at(2); } /// @brief vec w component. /// @return w value of this vec. - template = 4)>> inline T get_w() const { + static_assert(Length >= 4, "vector doesn't have a w component"); return this->at(3); } @@ -213,12 +215,12 @@ struct vec inline void set_x(T new_value) { this->set(0, new_value); } inline void set_y(T new_value) { this->set(1, new_value); } - template = 3)>> inline void set_z(T new_value) { + static_assert(Length >= 3, "vector doesn't have a z component"); this->set(2, new_value); } - template = 4)>> inline void set_w(T new_value) { + static_assert(Length >= 4, "vector doesn't have a w component"); this->set(3, new_value); } @@ -247,7 +249,7 @@ struct vec inline const ComponentRef &operator[](size_t index) { assert_print(index < std::min(Length, static_cast(4)), - "reference index out of bounds"); + "index out of bounds"); switch (index) { case 0: return this->x; @@ -257,6 +259,8 @@ struct vec return this->z; case 3: return this->w; + default: + UNREACHABLE(); } } @@ -277,15 +281,15 @@ struct vec return vec(buffer); } /// @brief Z unit vector value. - template = 3), bool>> static vec UnitZ() { + static_assert(Length >= 3, "vector doesn't have a z component"); Data buffer{0}; buffer[2] = static_cast(1); return vec(buffer); } /// @brief W unit vector value. - template = 4), bool>> static vec UnitW() { + static_assert(Length >= 4, "vector doesn't have a w component"); Data buffer{0}; buffer[3] = static_cast(1); return vec(buffer); @@ -388,8 +392,8 @@ struct vec } inline T magnitude() const { return std::sqrt(this->magnitude_squared()); } - template > T surface() const { + static_assert(Length == 2, "surface computable only for 2D vectors"); return this->get_x() * this->get_y(); } }; @@ -398,23 +402,24 @@ template using vec2 = vec; using vec2f = vec2; using vec2d = vec2; -using vec2i = vec2; +using vec2i = vec2; template using vec3 = vec; using vec3f = vec3; using vec3d = vec3; -using vec3i = vec3; +using vec3i = vec3; template using vec4 = vec; using vec4f = vec4; using vec4d = vec4; -using vec4i = vec4; +using vec4i = vec4; /// @brief 2D rectangle representation using position and size vectors. /// @tparam T component number type. -template ::value>> +template struct rect { + static_assert(std::is_arithmetic_v, "T must be a number"); + using Component = T; using ComponentRef = _priv_geom::_member_access, T>; @@ -441,6 +446,7 @@ struct rect { /// @param index component index. /// @return component at `index` of this rectangle. T at(size_t index) const { + assert_print(index < static_cast(4), "index out of bounds"); switch (index) { case 0: return this->get_x(); @@ -461,6 +467,7 @@ struct rect { void set_height(T value) { this->size.set_y(value); } void set(size_t index, T value) { + assert_print(index < static_cast(4), "index out of bounds"); switch (index) { case 0: return this->set_x(value); @@ -533,6 +540,7 @@ struct rect { } constexpr const ComponentRef &operator[](size_t index) { + assert_print(index < static_cast(4), "index out of bounds"); switch (index) { case 0: return this->x;