Skip to content

Commit

Permalink
Auto-update from libvoxelstorm: latest vectorstorm improvements from …
Browse files Browse the repository at this point in the history
…Armchair planet engine (ae38361)
  • Loading branch information
slowriot committed Jan 9, 2025
1 parent 9dcdf82 commit 2d5e343
Show file tree
Hide file tree
Showing 9 changed files with 484 additions and 298 deletions.
31 changes: 18 additions & 13 deletions vectorstorm/aabb/aabb2.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class aabb2 {
* bounding box becomes valid and contains solely the source point or bounding-box respectively.
* @return True if box is valid, otherwise false
*/
[[nodiscard]]
inline bool constexpr valid() const noexcept __attribute__((__always_inline__)) {
return min.x <= max.x && min.y <= max.y;
}
Expand Down Expand Up @@ -187,7 +188,7 @@ class aabb2 {
* @param point A point to extend the box by.
* @return Copy of extended bounding-box.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline constexpr aabb2<T> extended(vector2<SrcT> const &point) const noexcept {
aabb2<T> ret(*this);
ret.extend(point);
Expand All @@ -199,7 +200,7 @@ class aabb2 {
* @param box A box to extend the copy by.
* @return Copy of extended bounding-box.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline constexpr aabb2<T> extended(aabb2<SrcT> const &box) const noexcept {
aabb2<T> ret(*this);
ret.extend(box);
Expand All @@ -211,7 +212,7 @@ class aabb2 {
* @param point A point to be tested.
* @return True if point @a point lies within bounding-box, otherwise false.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr intersects(vector2<SrcT> const &point) const noexcept {
return min.x <= point.x && point.x <= max.x &&
min.y <= point.y && point.y <= max.y;
Expand All @@ -222,7 +223,7 @@ class aabb2 {
* @param box A box to be tested for intersection.
* @return True if there's intersection between boxes, otherwise false.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr intersects(aabb2<SrcT> const &box) const noexcept {
return max.x >= box.min.x && min.x <= box.max.x &&
max.y >= box.min.y && min.y <= box.max.y;
Expand All @@ -235,7 +236,7 @@ class aabb2 {
* @return Result of intersection.
* @see valid() method for more information on invalid bounding-boxes.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline constexpr aabb2<T> intersection(aabb2<SrcT> const &other) const noexcept {
return intersects(other) ? aabb2<T>(std::max(min, other.min), std::min(max, other.max)) : aabb2<T>{};
}
Expand All @@ -246,7 +247,7 @@ class aabb2 {
* @param ray The intersecting ray; does not need to be normalised
* @return True if the ray intersects the box, otherwise false.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr ray_intersects(vector2<SrcT> const &ray, vector2<SrcT> const &origin = vector2<SrcT>()) const noexcept {
// adapted from http://tavianator.com/2011/05/fast-branchless-raybounding-box-intersections/
/*
Expand Down Expand Up @@ -277,6 +278,7 @@ class aabb2 {
* Gets centre point of bounding box.
* @return The centre point of the bounding box.
*/
[[nodiscard]]
inline constexpr vector2<T> centre() const noexcept __attribute__((__always_inline__)) {
return (min + max) / static_cast<T>(2);
}
Expand All @@ -285,6 +287,7 @@ class aabb2 {
* Gets extent of bounding-box.
* @return Extent of bounding-box.
*/
[[nodiscard]]
inline constexpr vector2<T> extent() const noexcept __attribute__((__always_inline__)) {
return (max - min) / static_cast<T>(2);
}
Expand All @@ -293,6 +296,7 @@ class aabb2 {
* Gets diagonal size of bounding-box
* @return Sizes for particular dimensions.
*/
[[nodiscard]]
inline constexpr vector2<T> size() const noexcept __attribute__((__always_inline__)) {
return max - min;
}
Expand All @@ -308,6 +312,7 @@ class aabb2 {
* 4. (@c - @c - @c +)
*
*/
[[nodiscard]]
inline constexpr vector2<T> point(unsigned int i) const noexcept __attribute__((__always_inline__)) {
return vector2<T>((i & 1) ? min.x : max.x,
(i & 2) ? min.y : max.y);
Expand All @@ -321,7 +326,7 @@ class aabb2 {
* @param rhs Right-hand side.
* @return True if @a rhs and this bounding-boxes are equal, otherwise false.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr operator==(aabb2<RhsT> const &rhs) const noexcept {
return min == rhs.min && max == rhs.max;
}
Expand All @@ -331,7 +336,7 @@ class aabb2 {
* @param rhs Right-hand side.
* @return True if @a rhs and this bounding-boxes are not equal, otherwise false.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr operator!=(aabb2<RhsT> const &rhs) const noexcept {
return min != rhs.min || max != rhs.max;
}
Expand All @@ -341,7 +346,7 @@ class aabb2 {
* @param rhs A vector to move this bounding-box by.
* @return A resulting moved bounding-box.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb2<T> constexpr operator+(vector2<RhsT> const &rhs) const noexcept {
return aabb2(
vector2<T>{min.x + rhs.x, min.y + rhs.y},
Expand All @@ -354,7 +359,7 @@ class aabb2 {
* @param rhs A vector to move this bounding-box by the inverse of.
* @return A resulting moved bounding-box.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb2<T> constexpr operator-(vector2<RhsT> const &rhs) const noexcept {
return aabb2(
vector2<T>{min.x - rhs.x, min.y - rhs.y},
Expand All @@ -367,7 +372,7 @@ class aabb2 {
* @param rhs A vector to move this bounding-box by.
* @return Reference to this.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb2<T> constexpr &operator+=(vector2<RhsT> const &rhs) noexcept {
min.x += rhs.x;
min.y += rhs.y;
Expand Down Expand Up @@ -417,7 +422,7 @@ class aabb2 {
* @param rhs Right-hand side of union.
* @return A resulting bounding-box representing union.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb2<T> constexpr operator|(aabb2<RhsT> const &rhs) const noexcept {
return extended(rhs);
}
Expand All @@ -427,7 +432,7 @@ class aabb2 {
* @param rhs Right-hand side.
* @return Resulting bounding-box representing the intersection.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb2<T> constexpr operator&(aabb2<RhsT> const &rhs) const noexcept {
return intersection(rhs);
}
Expand Down
33 changes: 20 additions & 13 deletions vectorstorm/aabb/aabb3.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class aabb3 {
* bounding box becomes valid and contains solely the source point or bounding-box respectively.
* @return True if box is valid, otherwise false
*/
[[nodiscard]]
inline bool constexpr valid() const noexcept __attribute__((__always_inline__)) {
return min.x <= max.x && min.y <= max.y && min.z <= max.z;
}
Expand Down Expand Up @@ -192,7 +193,7 @@ class aabb3 {
* @param point A point to extend the box by.
* @return Copy of extended bounding-box.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline constexpr aabb3<T> extended(vector3<SrcT> const &point) const noexcept {
aabb3<T> ret(*this);
ret.extend(point);
Expand All @@ -204,7 +205,7 @@ class aabb3 {
* @param box A box to extend the copy by.
* @return Copy of extended bounding-box.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline constexpr aabb3<T> extended(aabb3<SrcT> const &box) const noexcept {
aabb3<T> ret(*this);
ret.extend(box);
Expand All @@ -216,7 +217,7 @@ class aabb3 {
* @param point A point to be tested.
* @return True if point @a point lies within bounding-box, otherwise false.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr intersects(vector3<SrcT> const &point) const noexcept {
return min.x <= point.x && point.x <= max.x &&
min.y <= point.y && point.y <= max.y &&
Expand All @@ -228,7 +229,7 @@ class aabb3 {
* @param box A box to be tested for intersection.
* @return True if there's intersection between boxes, otherwise false.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr intersects(aabb3<SrcT> const &box) const noexcept {
return max.x >= box.min.x && min.x <= box.max.x &&
max.y >= box.min.y && min.y <= box.max.y &&
Expand All @@ -242,7 +243,7 @@ class aabb3 {
* @return Result of intersection.
* @see valid() method for more information on invalid bounding-boxes.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline constexpr aabb3<T> intersection(aabb3<SrcT> const &other) const noexcept {
return intersects(other) ? aabb3<T>(std::max(min, other.min), std::min(max, other.max)) : aabb3<T>{};
}
Expand All @@ -253,7 +254,7 @@ class aabb3 {
* @param origin Origin of the intersecting ray
* @return True if the ray intersects the box, otherwise false.
*/
template<typename SrcT> __attribute__((__always_inline__))
template<typename SrcT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr ray_intersects(vector3<SrcT> const &ray, vector3<SrcT> const &origin = vector3<SrcT>()) const noexcept {
// adapted from http://tavianator.com/2011/05/fast-branchless-raybounding-box-intersections/
/*
Expand Down Expand Up @@ -293,6 +294,7 @@ class aabb3 {
* Gets centre point of bounding box.
* @return The centre point of the bounding box.
*/
[[nodiscard]]
inline vector3<T> constexpr centre() const noexcept __attribute__((__always_inline__)) {
return (min + max) / static_cast<T>(2);
}
Expand All @@ -301,6 +303,7 @@ class aabb3 {
* Gets extent of bounding-box.
* @return Extent of bounding-box.
*/
[[nodiscard]]
inline vector3<T> constexpr extent() const noexcept __attribute__((__always_inline__)) {
return (max - min) / static_cast<T>(2);
}
Expand All @@ -309,6 +312,7 @@ class aabb3 {
* Gets diagonal size of bounding-box
* @return Sizes for particular dimensions.
*/
[[nodiscard]]
inline vector3<T> constexpr size() const noexcept __attribute__((__always_inline__)) {
return max - min;
}
Expand All @@ -328,6 +332,7 @@ class aabb3 {
* 8. (@c - @c - @c -)
*
*/
[[nodiscard]]
inline vector3<T> constexpr point(unsigned int i) const noexcept __attribute__((__always_inline__)) {
return vector3<T>((i & 1) ? min.x : max.x,
(i & 2) ? min.y : max.y,
Expand All @@ -339,9 +344,10 @@ class aabb3 {
* @param t A transform matrix
* @return Transformed bounding-box
*/
[[nodiscard]]
inline aabb3<T> constexpr transformed(matrix4<T> const &t) const noexcept __attribute__((__always_inline__)) {
aabb3<T> ret;
for(unsigned int i = 0; i != 8; ++i) {
for(unsigned int i{0}; i != 8; ++i) {
const vector4<T> p(point(i), 1);
ret.extend((t * p).xyz());
}
Expand All @@ -356,7 +362,7 @@ class aabb3 {
* @param rhs Right-hand side
* @return True if @a rhs and this bounding-boxes are equal, otherwise false
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr operator==(aabb3<RhsT> const &rhs) const noexcept {
return min == rhs.min && max == rhs.max;
}
Expand All @@ -366,7 +372,7 @@ class aabb3 {
* @param rhs Right-hand side
* @return True if @a rhs and this bounding-boxes are not equal, otherwise false
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline bool constexpr operator!=(aabb3<RhsT> const &rhs) const noexcept {
return min != rhs.min || max != rhs.max;
}
Expand All @@ -376,7 +382,7 @@ class aabb3 {
* @param rhs A vector to move this bounding-box by.
* @return A resulting moved bounding-box.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb3<T> constexpr operator+(vector3<RhsT> const &rhs) const noexcept {
return aabb3(
vector3<T>{min.x + rhs.x, min.y + rhs.y, min.z + rhs.z},
Expand All @@ -389,7 +395,7 @@ class aabb3 {
* @param rhs A vector to move this bounding-box by the inverse of.
* @return A resulting moved bounding-box.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb3<T> constexpr operator-(vector3<RhsT> const &rhs) const noexcept {
return aabb3(
vector3<T>{min.x - rhs.x, min.y - rhs.y, min.z - rhs.z},
Expand Down Expand Up @@ -434,6 +440,7 @@ class aabb3 {
* @param rhs matrix 4x4 representing the transform.
* @return Transformed bounding-box.
*/
[[nodiscard]]
inline aabb3<T> constexpr operator*(matrix4<T> const &rhs) const noexcept __attribute__((__always_inline__)) {
return transformed(rhs);
}
Expand Down Expand Up @@ -475,7 +482,7 @@ class aabb3 {
* @param rhs Right-hand side of union.
* @return A resulting bounding-box representing union.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb3<T> constexpr operator|(aabb3<RhsT> const &rhs) const noexcept {
return extended(rhs);
}
Expand All @@ -485,7 +492,7 @@ class aabb3 {
* @param rhs Right-hand side.
* @return Resulting bounding-box representing the intersection.
*/
template<typename RhsT> __attribute__((__always_inline__))
template<typename RhsT> [[nodiscard]] __attribute__((__always_inline__))
inline aabb3<T> constexpr operator&(aabb3<RhsT> const &rhs) const noexcept {
return intersection(rhs);
}
Expand Down
Loading

0 comments on commit 2d5e343

Please sign in to comment.