Clustering
C++20 header-only: DBSCAN, HDBSCAN, k-means.
Loading...
Searching...
No Matches
aabb.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <span>
5#include <type_traits>
6
8#ifdef CLUSTERING_USE_AVX2
9#include "clustering/math/detail/aabb_gap_avx2.h"
10#endif
11
13
43template <class T>
44[[nodiscard]] inline T pointAabbGapSq(const T *point, std::span<const T> boxMin,
45 std::span<const T> boxMax) noexcept {
46 static_assert(std::is_same_v<T, float>,
47 "pointAabbGapSq: T must be float; a double specialization is out of scope.");
48 CLUSTERING_ALWAYS_ASSERT(boxMin.size() == boxMax.size());
49 const std::size_t d = boxMin.size();
50#ifdef CLUSTERING_USE_AVX2
51 return detail::pointAabbGapSqAvx2F32(point, boxMin.data(), boxMax.data(), d);
52#else
53 T sum = T{0};
54 for (std::size_t j = 0; j < d; ++j) {
55 T gap = T{0};
56 if (point[j] < boxMin[j]) {
57 gap = boxMin[j] - point[j];
58 } else if (point[j] > boxMax[j]) {
59 gap = point[j] - boxMax[j];
60 }
61 sum += gap * gap;
62 }
63 return sum;
64#endif
65}
66
82template <class T>
83[[nodiscard]] inline T aabbAabbGapSq(std::span<const T> minA, std::span<const T> maxA,
84 std::span<const T> minB, std::span<const T> maxB) noexcept {
85 CLUSTERING_ALWAYS_ASSERT(minA.size() == maxA.size() && minB.size() == maxB.size() &&
86 minA.size() == minB.size());
87 T sum = T{0};
88 for (std::size_t j = 0; j < minA.size(); ++j) {
89 T gap = T{0};
90 if (minA[j] > maxB[j]) {
91 gap = minA[j] - maxB[j];
92 } else if (minB[j] > maxA[j]) {
93 gap = minB[j] - maxA[j];
94 }
95 sum += gap * gap;
96 }
97 return sum;
98}
99
113template <class T>
114[[nodiscard]] inline T pointAabbFarthestSq(const T *point, std::span<const T> boxMin,
115 std::span<const T> boxMax) noexcept {
116 CLUSTERING_ALWAYS_ASSERT(boxMin.size() == boxMax.size());
117 T sum = T{0};
118 for (std::size_t j = 0; j < boxMin.size(); ++j) {
119 const T lo = point[j] - boxMin[j];
120 const T hi = boxMax[j] - point[j];
121 const T loMag = lo < T{0} ? -lo : lo;
122 const T hiMag = hi < T{0} ? -hi : hi;
123 const T farSide = loMag > hiMag ? loMag : hiMag;
124 sum += farSide * farSide;
125 }
126 return sum;
127}
128
129} // namespace clustering::math
#define CLUSTERING_ALWAYS_ASSERT(cond)
Release-active assertion: evaluates cond in every build configuration.
T pointAabbGapSq(const T *point, std::span< const T > boxMin, std::span< const T > boxMax) noexcept
Squared gap distance between a point and an axis-aligned bounding box.
Definition aabb.h:44
T aabbAabbGapSq(std::span< const T > minA, std::span< const T > maxA, std::span< const T > minB, std::span< const T > maxB) noexcept
Squared gap distance between two axis-aligned bounding boxes.
Definition aabb.h:83
T sum(const NDArray< T, 1, L > &x) noexcept
Naive single-pass sum of a rank-1 array.
Definition reduce.h:25
T pointAabbFarthestSq(const T *point, std::span< const T > boxMin, std::span< const T > boxMax) noexcept
Squared farthest distance from a point to any point of an axis-aligned bounding box.
Definition aabb.h:114