25#ifndef VULPES_GEOMETRY_DISTANCE_HPP_
26#define VULPES_GEOMETRY_DISTANCE_HPP_
64template <
typename FloatType,
int32_t Dim,
typename Distance>
82 static constexpr size_t size() {
return Distance::size(); }
97 static_cast<const Distance&
>(*this)(point, distance_buffer);
108 std::array<Point<FloatType, Dim>, 2>
bbox()
const {
109 return static_cast<const Distance*
>(
this)->
bbox();
123template <
typename FloatType>
137 static constexpr size_t size() {
return 5; }
158 distance_buffer[0] =
m_corner_a[0] - point[0];
159 distance_buffer[1] = point[0] -
m_corner_b[0];
160 distance_buffer[2] =
m_corner_a[1] - point[1];
161 distance_buffer[3] = point[1] -
m_corner_b[1];
163 *std::max_element(distance_buffer, distance_buffer + 4);
171 std::array<Point<FloatType, 2>, 2>
bbox()
const {
195template <
typename FloatType>
227 throw std::invalid_argument(
228 "A polygon must have at least three vertices");
231 throw std::invalid_argument(
"Polygon has too many edges");
234 FloatType signed_area = 0.;
235 for (
size_t i = 0; i <
m_vertices.size(); ++i) {
238 signed_area += start[0] * end[1] - end[0] * start[1];
240 if (signed_area == FloatType(0.)) {
241 throw std::invalid_argument(
"Polygon has zero area");
244 throw std::invalid_argument(
"Polygon has edges that cross or touch");
259 const FloatType infinity = std::numeric_limits<FloatType>::max();
260 FloatType min_distance = infinity;
261 bool point_on_boundary =
false;
262 bool point_inside =
false;
265 std::fill(distance_buffer, distance_buffer +
max_edges, infinity);
269 for (
size_t i = 0; i <
m_vertices.size(); ++i) {
272 const FloatType dx = end[0] - start[0];
273 const FloatType dy = end[1] - start[1];
274 const FloatType edge_length_squared = dx * dx + dy * dy;
278 FloatType projection = 0.;
279 if (edge_length_squared > 0.) {
280 projection = ((point[0] - start[0]) * dx + (point[1] - start[1]) * dy) /
282 projection = std::clamp(projection, FloatType(0.), FloatType(1.));
285 const FloatType closest_x = start[0] + projection * dx;
286 const FloatType closest_y = start[1] + projection * dy;
287 const FloatType diff_x = point[0] - closest_x;
288 const FloatType diff_y = point[1] - closest_y;
289 const FloatType edge_distance =
290 std::sqrt(diff_x * diff_x + diff_y * diff_y);
291 min_distance = std::min(min_distance, edge_distance);
294 const FloatType cross =
295 dx * (point[1] - start[1]) - dy * (point[0] - start[0]);
297 edge_length_squared > 0.
303 const FloatType scale = std::max(
304 {FloatType(1.), std::abs(point[0]), std::abs(point[1]),
305 std::abs(start[0]), std::abs(start[1]), std::abs(end[0]),
308 FloatType(8.) * std::numeric_limits<FloatType>::epsilon() * scale) {
309 point_on_boundary =
true;
314 if ((start[1] > point[1]) != (end[1] > point[1]) &&
315 point[0] < (end[0] - start[0]) * (point[1] - start[1]) /
316 (end[1] - start[1]) +
318 point_inside = !point_inside;
323 const FloatType signed_distance =
324 point_on_boundary || point_inside ? -min_distance : min_distance;
325 distance_buffer[
max_edges] = signed_distance;
333 std::array<Point<FloatType, 2>, 2>
bbox()
const {
337 lower[0] = std::min(lower[0], vertex[0]);
338 lower[1] = std::min(lower[1], vertex[1]);
339 upper[0] = std::max(upper[0], vertex[0]);
340 upper[1] = std::max(upper[1], vertex[1]);
342 return {lower, upper};
349 const auto orientation = [](
const Point2& a,
const Point2& b,
351 return (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
354 const auto onSegment = [](
const Point2& a,
const Point2& b,
356 return std::min(a[0], b[0]) <= c[0] && c[0] <= std::max(a[0], b[0]) &&
357 std::min(a[1], b[1]) <= c[1] && c[1] <= std::max(a[1], b[1]);
360 for (
size_t i = 0; i < n; ++i) {
363 for (
size_t j = i + 2; j < n; ++j) {
365 if (i == 0 && j == n - 1) {
370 const FloatType side_c = orientation(a, b, c);
371 const FloatType side_d = orientation(a, b, d);
372 const FloatType side_a = orientation(c, d, a);
373 const FloatType side_b = orientation(c, d, b);
374 if (side_c * side_d < 0. && side_a * side_b < 0.) {
377 if ((side_c == 0. && onSegment(a, b, c)) ||
378 (side_d == 0. && onSegment(a, b, d)) ||
379 (side_a == 0. && onSegment(c, d, a)) ||
380 (side_b == 0. && onSegment(c, d, b))) {
388 const std::vector<Point<FloatType, 2>>
401template <
typename FloatType>
416 static constexpr size_t size() {
return 2; }
436 const FloatType dx = point[0] -
m_center[0];
437 const FloatType dy = point[1] -
m_center[1];
438 distance_buffer[0] = std::sqrt(dx * dx + dy * dy) -
m_radius;
439 distance_buffer[1] = distance_buffer[0];
447 std::array<Point<FloatType, 2>, 2>
bbox()
const {
466template <
typename FloatType>
480 static constexpr size_t size() {
return 7; }
501 distance_buffer[0] =
m_corner_a[0] - point[0];
502 distance_buffer[1] = point[0] -
m_corner_b[0];
503 distance_buffer[2] =
m_corner_a[1] - point[1];
504 distance_buffer[3] = point[1] -
m_corner_b[1];
505 distance_buffer[4] =
m_corner_a[2] - point[2];
506 distance_buffer[5] = point[2] -
m_corner_b[2];
508 *std::max_element(distance_buffer, distance_buffer + 6);
516 std::array<Point<FloatType, 3>, 2>
bbox()
const {
533template <
typename FloatType>
547 static constexpr size_t size() {
return 2; }
567 const FloatType dx = point[0] -
m_center[0];
568 const FloatType dy = point[1] -
m_center[1];
569 const FloatType dz = point[2] -
m_center[2];
570 distance_buffer[0] = std::sqrt(dx * dx + dy * dy + dz * dz) -
m_radius;
571 distance_buffer[1] = distance_buffer[0];
579 std::array<Point<FloatType, 3>, 2>
bbox()
const {
602template <
typename FloatType,
int32_t Dim,
typename D1,
typename D2>
606 UnionDistance<FloatType, Dim, D1, D2>> {
609 typename std::conditional_t<D1::is_leaf, const D1&, const D1>
m_u;
612 typename std::conditional_t<D2::is_leaf, const D2&, const D2>
m_v;
633 static constexpr size_t size() {
634 return std::decay_t<
decltype(
m_v)>
::size() +
650 constexpr auto u_distance_size = std::decay_t<
decltype(
m_u)>
::size();
651 constexpr auto this_size = std::decay_t<
decltype(*this)>
::size();
652 m_u(point, distance_buffer);
653 FloatType tmp = distance_buffer[u_distance_size - 1];
654 m_v(point, distance_buffer + u_distance_size - 1);
655 distance_buffer[this_size - 1] =
656 std::min(tmp, distance_buffer[this_size - 1]);
666 std::array<Point<FloatType, Dim>, 2>
bbox()
const {
667 const auto bbox_u =
m_u.bbox();
668 const auto bbox_v =
m_v.bbox();
671 for (
size_t d = 0; d < Dim; ++d) {
672 corner_low[d] = std::min(bbox_u[0][d], bbox_v[0][d]);
673 corner_high[d] = std::max(bbox_u[1][d], bbox_v[1][d]);
675 return {corner_low, corner_high};
690template <
typename FloatType,
int32_t Dim,
typename D1,
typename D2>
695 *
static_cast<const D1*
>(&u), *
static_cast<const D2*
>(&v));
710template <
typename FloatType,
int32_t Dim,
typename D1,
typename D2>
714 IntersectDistance<FloatType, Dim, D1, D2>> {
717 typename std::conditional_t<D1::is_leaf, const D1&, const D1>
m_u;
720 typename std::conditional_t<D2::is_leaf, const D2&, const D2>
m_v;
745 static constexpr size_t size() {
746 return std::decay_t<
decltype(
m_v)>
::size() +
762 constexpr auto u_distance_size = std::decay_t<
decltype(
m_u)>
::size();
763 constexpr auto this_size = std::decay_t<
decltype(*this)>
::size();
764 m_u(point, distance_buffer);
765 FloatType tmp = distance_buffer[u_distance_size - 1];
766 m_v(point, distance_buffer + u_distance_size - 1);
767 distance_buffer[this_size - 1] =
768 std::max(tmp, distance_buffer[this_size - 1]);
776 std::array<Point<FloatType, Dim>, 2>
bbox()
const {
777 const auto bbox_u =
m_u.bbox();
778 const auto bbox_v =
m_v.bbox();
781 for (
size_t d = 0; d < Dim; ++d) {
782 corner_low[d] = std::max(bbox_u[0][d], bbox_v[0][d]);
783 corner_high[d] = std::min(bbox_u[1][d], bbox_v[1][d]);
785 return {corner_low, corner_high};
801template <
typename FloatType,
int32_t Dim,
typename D1,
typename D2>
806 *
static_cast<const D1*
>(&u), *
static_cast<const D2*
>(&v));
822template <
typename FloatType,
int32_t Dim,
typename D1,
typename D2>
826 DifferenceDistance<FloatType, Dim, D1, D2>> {
829 typename std::conditional_t<D1::is_leaf, const D1&, const D1>
m_u;
832 typename std::conditional_t<D2::is_leaf, const D2&, const D2>
m_v;
857 static constexpr size_t size() {
858 return std::decay_t<
decltype(
m_v)>
::size() +
875 constexpr auto u_distance_size = std::decay_t<
decltype(
m_u)>
::size();
876 constexpr auto this_size = std::decay_t<
decltype(*this)>
::size();
877 m_u(point, distance_buffer);
878 FloatType tmp = distance_buffer[u_distance_size - 1];
879 m_v(point, distance_buffer + u_distance_size - 1);
880 distance_buffer[this_size - 1] =
881 std::max(tmp, -distance_buffer[this_size - 1]);
891 std::array<Point<FloatType, Dim>, 2>
bbox()
const {
return m_u.bbox(); }
906template <
typename FloatType,
int32_t Dim,
typename D1,
typename D2>
911 *
static_cast<const D1*
>(&u), *
static_cast<const D2*
>(&v));
935template <
typename FloatType,
int32_t Dim,
typename D>
939 constexpr size_t distance_size =
941 std::array<FloatType, distance_size> distance_buffer;
942 distance_function(point, distance_buffer.data());
943 return distance_buffer;
Represents the difference of two signed distance functions.
Definition distance.hpp:826
static constexpr size_t size()
Get the size of the distance buffer for this difference operation.
Definition distance.hpp:857
std::conditional_t< D2::is_leaf, const D2 &, const D2 > m_v
Definition distance.hpp:832
std::array< Point< FloatType, Dim >, 2 > bbox() const
Get the bounding box of the difference of two shapes.
Definition distance.hpp:891
DifferenceDistance(D1 const &u, D2 const &v)
Constructor for the difference distance function.
Definition distance.hpp:847
static constexpr bool is_leaf
A flag indicating that this is a non-leaf node in a composite expression.
Definition distance.hpp:839
void operator()(const Point< FloatType, Dim > &point, FloatType *distance_buffer) const
Compute the distance between a point and the difference of two shapes.
Definition distance.hpp:873
std::conditional_t< D1::is_leaf, const D1 &, const D1 > m_u
Definition distance.hpp:829
Template class representing a generic signed distance expression.
Definition distance.hpp:65
void operator()(const Point< FloatType, Dim > &point, FloatType *distance_buffer) const
Evaluate the distance function for the given point.
Definition distance.hpp:95
static constexpr bool is_leaf
A flag indicating whether this is a leaf node in a composite expression.
Definition distance.hpp:71
std::array< Point< FloatType, Dim >, 2 > bbox() const
Get the bounding box (bbox) of the shape.
Definition distance.hpp:108
static constexpr size_t size()
Get the size of the distance buffer required for this expression.
Definition distance.hpp:82
Represents the intersection of two signed distance functions.
Definition distance.hpp:714
static constexpr bool is_leaf
A flag indicating that this is a non-leaf node in a composite expression.
Definition distance.hpp:727
std::array< Point< FloatType, Dim >, 2 > bbox() const
Get the bounding box of the intersection of two shapes.
Definition distance.hpp:776
static constexpr size_t size()
Get the size of the distance buffer for this intersection operation.
Definition distance.hpp:745
std::conditional_t< D2::is_leaf, const D2 &, const D2 > m_v
Definition distance.hpp:720
std::conditional_t< D1::is_leaf, const D1 &, const D1 > m_u
Definition distance.hpp:717
void operator()(const Point< FloatType, Dim > &point, FloatType *distance_buffer) const
Compute the signed distance between a point and the intersection of two shapes.
Definition distance.hpp:760
IntersectDistance(D1 const &u, D2 const &v)
Constructor for the intersection distance function.
Definition distance.hpp:735
Represents the union of two signed distance functions.
Definition distance.hpp:606
void operator()(const Point< FloatType, Dim > &point, FloatType *distance_buffer) const
Compute the signed distance between a point and the union of two shapes.
Definition distance.hpp:648
static constexpr bool is_leaf
Definition distance.hpp:615
UnionDistance(D1 const &u, D2 const &v)
Constructor for the union distance function.
Definition distance.hpp:623
static constexpr size_t size()
Get the size of the distance buffer for this union operation.
Definition distance.hpp:633
std::conditional_t< D2::is_leaf, const D2 &, const D2 > m_v
Definition distance.hpp:612
std::conditional_t< D1::is_leaf, const D1 &, const D1 > m_u
Definition distance.hpp:609
std::array< Point< FloatType, Dim >, 2 > bbox() const
Get the bounding box of the union of two shapes.
Definition distance.hpp:666
static constexpr bool is_leaf
A flag indicating that this is a leaf node in a composite expression. Indeed we give an explicit form...
Definition distance.hpp:409
static constexpr size_t size()
Get the size of the distance buffer for this distance function.
Definition distance.hpp:416
const FloatType m_radius
The radius of the circle.
Definition distance.hpp:453
const Point< FloatType, 2 > m_center
The center of the circle.
Definition distance.hpp:452
std::array< Point< FloatType, 2 >, 2 > bbox() const
Get the bounding box of the circle.
Definition distance.hpp:447
CircleDistance(const Point< FloatType, 2 > ¢er, FloatType radius)
Constructor for the circle distance function.
Definition distance.hpp:424
void operator()(const Point< FloatType, 2 > &point, FloatType *distance_buffer) const
Compute the signed distance between a point and the circle.
Definition distance.hpp:434
std::array< Point< FloatType, 2 >, 2 > bbox() const
Get the bounding box of the polygon.
Definition distance.hpp:333
static constexpr bool is_leaf
A flag indicating that this is a leaf node in a composite expression. Indeed we give an explicit form...
Definition distance.hpp:206
const std::vector< Point< FloatType, 2 > > m_vertices
The vertices of the polygon.
Definition distance.hpp:389
bool hasIntersectingEdges() const
True if two edges that are not consecutive cross or touch.
Definition distance.hpp:347
FloatType m_outward_sign
Makes the edge distances positive outward.
Definition distance.hpp:390
void operator()(const Point< FloatType, 2 > &point, FloatType *distance_buffer) const
Compute the signed distance between a point and the polygon.
Definition distance.hpp:256
static constexpr size_t max_edges
Largest number of edges: the buffer size is a compile-time constant.
Definition distance.hpp:200
static constexpr size_t size()
Get the size of the distance buffer for this distance function.
Definition distance.hpp:213
PolygonDistance(const std::vector< Point< FloatType, 2 > > &vertices)
Constructor for the polygon distance function.
Definition distance.hpp:224
const Point< FloatType, 2 > m_corner_b
Opposite corner of the rectangle.
Definition distance.hpp:177
std::array< Point< FloatType, 2 >, 2 > bbox() const
Get the bounding box of the rectangle.
Definition distance.hpp:171
RectangleDistance(const Point< FloatType, 2 > &corner_a, const Point< FloatType, 2 > &corner_b)
Constructor for the rectangle distance function.
Definition distance.hpp:145
static constexpr size_t size()
Get the size of the distance buffer for this distance function.
Definition distance.hpp:137
const Point< FloatType, 2 > m_corner_a
One corner of the rectangle.
Definition distance.hpp:176
void operator()(const Point< FloatType, 2 > &point, FloatType *distance_buffer) const
Compute the signed distance between a point and the rectangle.
Definition distance.hpp:156
static constexpr bool is_leaf
A flag indicating that this is a leaf node in a composite expression. Indeed we give an explicit form...
Definition distance.hpp:131
std::array< Point< FloatType, 3 >, 2 > bbox() const
Get the bounding box of the hexahedron.
Definition distance.hpp:516
static constexpr bool is_leaf
A flag indicating that this is a leaf node in a composite expression. Indeed we give an explicit form...
Definition distance.hpp:474
static constexpr size_t size()
Get the size of the distance buffer for this distance function.
Definition distance.hpp:480
const Point< FloatType, 3 > m_corner_a
Smallest corner of the hexahedron.
Definition distance.hpp:521
const Point< FloatType, 3 > m_corner_b
Largest corner of the hexahedron.
Definition distance.hpp:522
void operator()(const Point< FloatType, 3 > &point, FloatType *distance_buffer) const
Compute the signed distance between a point and the hexahedron.
Definition distance.hpp:499
HexahedronDistance(const Point< FloatType, 3 > &corner_a, const Point< FloatType, 3 > &corner_b)
Constructor for the hexahedron distance function.
Definition distance.hpp:488
void operator()(const Point< FloatType, 3 > &point, FloatType *distance_buffer) const
Compute the signed distance between a point and the sphere.
Definition distance.hpp:565
static constexpr size_t size()
Get the size of the distance buffer for this distance function.
Definition distance.hpp:547
static constexpr bool is_leaf
A flag indicating that this is a leaf node in a composite expression. Indeed we give an explicit form...
Definition distance.hpp:541
std::array< Point< FloatType, 3 >, 2 > bbox() const
Get the bounding box of the sphere.
Definition distance.hpp:579
const FloatType m_radius
The radius of the sphere.
Definition distance.hpp:585
const Point< FloatType, 3 > m_center
The center of the sphere.
Definition distance.hpp:584
SphereDistance(const Point< FloatType, 3 > ¢er, FloatType radius)
Constructor for the sphere distance function.
Definition distance.hpp:555
Definition distance.hpp:113
Definition distance.hpp:457
Definition abs_agg_model.cpp:40
Eigen::Matrix< DataType, Dim, 1, Eigen::ColMajor > Point
Definition traits.hpp:34
IntersectDistance< FloatType, Dim, D1, D2 > operator&(const DistanceExpression< FloatType, Dim, D1 > &u, const DistanceExpression< FloatType, Dim, D2 > &v)
Create an intersection of two signed distance functions.
Definition distance.hpp:802
DifferenceDistance< FloatType, Dim, D1, D2 > operator-(const DistanceExpression< FloatType, Dim, D1 > &u, const DistanceExpression< FloatType, Dim, D2 > &v)
Create a difference of two distance functions.
Definition distance.hpp:907
auto computeDistance(const Point< FloatType, Dim > &point, const DistanceExpression< FloatType, Dim, D > &distance_function)
Computes the signed distance between a point and a shape defined by a distance function.
Definition distance.hpp:936
UnionDistance< FloatType, Dim, D1, D2 > operator|(const DistanceExpression< FloatType, Dim, D1 > &u, const DistanceExpression< FloatType, Dim, D2 > &v)
Create a union of two signed distance functions.
Definition distance.hpp:691
Definition cell_manager.hpp:38