💿🐜 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.

102 lines
2.6 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_HYPERPLANE_HPP
#define ANTKEEPER_GEOM_PRIMITIVES_HYPERPLANE_HPP
#include <engine/math/vector.hpp>
namespace geom {
namespace primitives {
/**
* *n*-dimensional plane.
*
* @tparam T Real type.
* @tparam N Number of dimensions.
*/
template <class T, std::size_t N>
struct hyperplane
{
/// Vector type.
using vector_type = math::vector<T, N>;
/// Hyperplane normal.
vector_type normal;
/// Hyperplane constant.
T constant;
/// Constructs a hyperplane.
constexpr hyperplane() noexcept = default;
/**
* Constructs a hyperplane given a normal and constant.
*
* @param normal Plane normal.
* @param constant Plane constant.
*/
inline constexpr hyperplane(const vector_type& normal, float constant) noexcept:
normal{normal},
constant{constant}
{}
/**
* Constructs a hyperplane given a normal and an offset point.
*
* @param normal Plane normal.
* @param offset Offset from the origin.
*/
inline constexpr hyperplane(const vector_type& normal, const vector_type& offset) noexcept:
normal{normal},
constant{-math::dot(normal, offset)}
{}
/**
* Calculates the signed distance from the hyperplane to a point.
*
* @param point Input point.
*
* @return Signed distance from the hyperplane to @p point.
*/
[[nodiscard]] inline constexpr T distance(const vector_type& point) const noexcept
{
return math::dot(normal, point) + constant;
}
/**
* Calculates the closest point on the hyperplane to a point.
*
* @param point Input point.
*
* @return Closest point on the hyperplane to @p point.
*/
[[nodiscard]] inline constexpr vector_type closest_point(const vector_type& point) const noexcept
{
return point - normal * distance(point);
}
};
} // namespace primitives
using namespace primitives;
} // namespace geom
#endif // ANTKEEPER_GEOM_PRIMITIVES_HYPERPLANE_HPP