💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.0 KiB

/*
* Copyright (C) 2023 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_GEOM_PRIMITIVES_HYPERRECTANGLE_HPP
#define ANTKEEPER_GEOM_PRIMITIVES_HYPERRECTANGLE_HPP
#include <engine/math/vector.hpp>
#include <algorithm>
namespace geom {
namespace primitives {
/**
* *n*-dimensional axis-aligned rectangle.
*
* @tparam T Real type.
* @tparam N Number of dimensions.
*/
template <class T, std::size_t N>
struct hyperrectangle
{
/// Vector type.
using vector_type = math::vector<T, N>;
/// Minimum extent of the hyperrectangle.
vector_type min;
/// Maximum extent of the hyperrectangle.
vector_type max;
/**
* Tests whether a point is contained within this hyperrectangle.
*
* @param point Point to test for containment.
*
* @return `true` if the point is contained within this hyperrectangle, `false` otherwise.
*/
[[nodiscard]] constexpr bool contains(const vector_type& point) const noexcept
{
for (std::size_t i = 0; i < N; ++i)
{
if (point[i] < min[i] || point[i] > max[i])
{
return false;
}
}
return true;
}
/**
* Tests whether another hyperrectangle is contained within this hyperrectangle.
*
* @param other Hyperrectangle to test for containment.
*
* @return `true` if the hyperrectangle is contained within this hyperrectangle, `false` otherwise.
*/
[[nodiscard]] constexpr bool contains(const hyperrectangle& other) const noexcept
{
for (std::size_t i = 0; i < N; ++i)
{
if (other.min[i] < min[i] || other.max[i] > max[i])
{
return false;
}
}
return true;
}
/// Returns the center position of the hyperrectangle.
[[nodiscard]] inline constexpr vector_type center() const noexcept
{
return (min + max) / T{2};
}
/**
* Calculates the signed distance from the hyperrectangle to a point.
*
* @param point Input point.
*
* @return Signed distance from the hyperrectangle to @p point.
*/
[[nodiscard]] T distance(const vector_type& point) const noexcept
{
const vector_type d = math::abs(point - center()) - extents();
return math::length(math::max(vector_type::zero(), d)) + std::min<T>(T{0}, math::max(d));
}
/**
* Extends the hyperrectangle to include a point.
*
* @param point Point to include in the hyperrectangle.
*/
void extend(const vector_type& point) noexcept
{
min = math::min(min, point);
max = math::max(max, point);
}
/**
* Extends the hyperrectangle to include another hyperrectangle.
*
* @param other Hyperrectangle to include in this hyperrectangle.
*/
void extend(const hyperrectangle& other) noexcept
{
min = math::min(min, other.min);
max = math::max(max, other.max);
}
/**
* Tests whether another hyperrectangle intersects this hyperrectangle.
*
* @param other Hyperrectangle to test for intersection.
*
* @return `true` if the hyperrectangle intersects this hyperrectangle, `false` otherwise.
*/
[[nodiscard]] constexpr bool intersects(const hyperrectangle& other) const noexcept
{
for (std::size_t i = 0; i < N; ++i)
{
if (other.min[i] > max[i] || other.max[i] < min[i])
{
return false;
}
}
return true;
}
/// Calculates the size of the hyperrectangle.
[[nodiscard]] inline constexpr vector_type size() const noexcept
{
return max - min;
}
/// Calculates the extents of the hyperrectangle.
[[nodiscard]] inline constexpr vector_type extents() const noexcept
{
return size() / T{2};
}
/**
* Returns `true` if any coordinates of `min` are greater than `max`, `false` otherwise.
*/
[[nodiscard]] constexpr bool degenerate() const noexcept
{
for (std::size_t i = 0; i < N; ++i)
{
if (min[i] > max[i])
{
return true;
}
}
return false;
}
/// Calculates the volume of the hyperrectangle.
[[nodiscard]] constexpr T volume() const noexcept
{
T v = max[0] - min[0];
for (std::size_t i = 1; i < N; ++i)
{
v *= max[i] - min[i];
}
return v;
}
/**
* Returns the nth corner of the hyperrectangle.
*
* @param index Index of a corner.
*/
[[nodiscard]] constexpr vector_type corner(std::size_t index) const noexcept
{
vector_type p;
for (std::size_t i = 0; i < N; ++i)
{
p[i] = ((index >> ((N - 1) - i)) & 1) ? max[i] : min[i];
}
return p;
}
};
} // namespace primitives
using namespace primitives;
} // namespace geom
#endif // ANTKEEPER_GEOM_PRIMITIVES_HYPERRECTANGLE_HPP