SuperDex Physics C++ API
Loading...
Searching...
No Matches
math_utils.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
25
26#include <type_traits>
27
28namespace superdex {
29
30/**************************************************************************************************
31 ArgMin, ArgMax
32*/
33
34// Returns the index of the lesser entry of an array.
35template <typename T, size_t N>
36[[nodiscard]] MOCHI_FORCE_INLINE constexpr size_t ArgMin(NdArray<T, N> const& a);
37
38// Returns the index of the greater entry of an array.
39template <typename T, size_t N>
40[[nodiscard]] MOCHI_FORCE_INLINE constexpr size_t ArgMax(NdArray<T, N> const& a);
41
42/**************************************************************************************************
43 NearEqual: (abs(a-b) <= epsilon)
44*/
45
46template <typename T, typename Eps = T, size_t D0, size_t... DIMS>
47[[nodiscard]] MOCHI_FORCE_INLINE constexpr bool NearEqual(
50 Eps epsilon = kDefaultNearEqualEpsilon<T>);
51
52template <typename T, size_t D0, int D1>
54 NdArray<Simd<T, D1>, D0> const& a,
55 NdArray<Simd<T, D1>, D0> const& b,
57
58template <typename T, size_t D0, int D1>
59[[nodiscard]] MOCHI_FORCE_INLINE bool NearEqual(
60 NdArray<Simd<T, D1>, D0> const& a,
61 NdArray<Simd<T, D1>, D0> const& b,
62 T epsilon = kDefaultNearEqualEpsilon<T>);
63
64/**************************************************************************************************
65 Euclidean Norm (e.g. vector magnitude)
66*/
67
68template <typename T, size_t N>
69[[nodiscard]] MOCHI_FORCE_INLINE constexpr T NormSqr(NdArray<T, N> const& a);
70
71template <typename T, size_t N>
72[[nodiscard]] MOCHI_FORCE_INLINE T Norm(NdArray<T, N> const& a);
73
74/**************************************************************************************************
75 Basis vector
76*/
77
78template <typename T, int N>
79[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> BasisVector(int axis);
80
81/**************************************************************************************************
82 Normalize (make unit length)
83*/
84
85template <typename T, size_t N>
86[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Normalize(NdArray<T, N> const& a);
87
88template <typename T, size_t N>
89[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Normalize(
90 NdArray<T, N> const& a,
91 T sqrNorm);
92
93/**************************************************************************************************
94 Sum, Prod and Mean
95*/
96
97// Sum of 1D array
98template <typename T, size_t N>
99[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Sum(NdArray<T, N> const& a);
100
101// Product of 1D array
102template <typename T, size_t N>
103[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Prod(NdArray<T, N> const& a);
104
105// Mean of 1D array
106template <typename T, size_t N>
107[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Mean(NdArray<T, N> const& a);
108
109// Max of 1D array
110template <typename T, size_t N>
111[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Max(NdArray<T, N> const& a);
112
113// Min of 1D array
114template <typename T, size_t N>
115[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Min(NdArray<T, N> const& a);
116
117/**************************************************************************************************
118 Floor, ceil, round
119*/
120
121// Floor of 1D array
122template <typename T, size_t N>
123[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Floor(NdArray<T, N> const& a);
124
125// Ceil of 1D array
126template <typename T, size_t N>
127[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Ceil(NdArray<T, N> const& a);
128
129// Round of 1D array
130template <typename T, size_t N>
131[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Round(NdArray<T, N> const& a);
132
133// out[i] = Clamp(a[i], min, max)
134template <typename T, size_t N>
135[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N>
136Clamp(NdArray<T, N> const& a, T min, T max);
137
138// out[i] = max(a[i], max)
139template <typename T, size_t N>
140[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Max(NdArray<T, N> const& a, T max);
141
142// out[i] = min(a[i], min)
143template <typename T, size_t N>
144[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Min(NdArray<T, N> const& a, T min);
145
146// out[i] = Clamp(a[i], min[i], max[i])
147template <typename T, size_t N>
148[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N>
149Clamp(NdArray<T, N> const& a, NdArray<T, N> const& min, NdArray<T, N> const& max);
150
151// out[i] = Rect(a[i], min[i], max[i])
152template <typename T, size_t N, bool MinInclusiveT, bool MaxInclusiveT>
153[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Rect(
154 NdArray<T, N> const& a,
155 NdArray<T, N> const& min,
156 NdArray<T, N> const& max,
157 std::integral_constant<bool, MinInclusiveT> minInclusive = std::true_type{},
158 std::integral_constant<bool, MaxInclusiveT> maxInclusive = std::false_type{});
159
160// out[i] = max(a[i], max[i])
161template <typename T, size_t N>
162[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Max(
163 NdArray<T, N> const& a,
164 NdArray<T, N> const& max);
165
166// out[i] = min(a[i], min[i])
167template <typename T, size_t N>
168[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, N> Min(
169 NdArray<T, N> const& a,
170 NdArray<T, N> const& min);
171
172/**************************************************************************************************
173 Dot Product
174*/
175
176template <typename T, size_t N>
177[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Dot(NdArray<T, N> const& a, NdArray<T, N> const& b);
178
179template <typename T, size_t N>
180[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Dot(Span<T const> a, NdArray<T, N> const& b);
181
182template <typename T>
183[[nodiscard]] MOCHI_FORCE_INLINE constexpr T Dot(Span<T const> a, Span<T const> b);
184
185/**************************************************************************************************
186 Cross Product
187*/
188
189template <typename T>
190[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, 3> Cross(
191 NdArray<T, 3> const& a,
192 NdArray<T, 3> const& b);
193
194/**************************************************************************************************
195 Triple Product
196*/
197
198template <typename T>
199[[nodiscard]] MOCHI_FORCE_INLINE constexpr T
200TripleProduct(NdArray<T, 3> const& a, NdArray<T, 3> const& b, NdArray<T, 3> const& c);
201
202/**************************************************************************************************
203 Other Geometric Utilities
204*/
205
206// Builds an arbitrary vector that is orthogonal to the given one.
207template <typename T>
208[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, 2> OrthogonalVector(NdArray<T, 2> const& vec);
209
210// Builds an arbitrary vector that is orthogonal to the given one.
211template <typename T>
212[[nodiscard]] MOCHI_FORCE_INLINE constexpr NdArray<T, 3> OrthogonalVector(NdArray<T, 3> const& vec);
213
214/**************************************************************************************************
215 IsFinite
216*/
217
218/// @brief Per-lane finiteness check on a SIMD-typed NdArray. Returns a per-lane SIMD mask.
219///
220/// @note Use when you need per-lane finiteness instead of the whole-array @ref IsFinite, which
221/// collapses the result to a single bool.
222template <class T, int DN, size_t D0, size_t... DIMS>
223[[nodiscard]] MOCHI_FORCE_INLINE Simd<T, DN> VIsFinite(NdArray<Simd<T, DN>, D0, DIMS...> const& a);
224
225// Return true if all elements of the NdArray are finite.
226template <class T, int DN, size_t D0, size_t... DIMS>
227[[nodiscard]] MOCHI_FORCE_INLINE bool IsFinite(NdArray<Simd<T, DN>, D0, DIMS...> const& a);
228
229template <class T, size_t D0, size_t... DIMS>
230[[nodiscard]] MOCHI_FORCE_INLINE bool IsFinite(NdArray<T, D0, DIMS...> const& a);
231
232} // namespace superdex
233
234#include "math_utils_inl.h"
#define MOCHI_FORCE_INLINE
T Dot(Simd< T, N > a, Simd< T, N > b)
Definition simd.h:666
Simd< T, N > VIsFinite(Simd< T, N > a)
Definition simd_inl.h:755
constexpr T const & Min(T const &a, T const &b)
T NormSqr(Simd< T, N > a)
Definition simd_inl.h:849
constexpr T TripleProduct(NdArray< T, 3 > const &a, NdArray< T, 3 > const &b, NdArray< T, 3 > const &c)
constexpr T kDefaultNearEqualEpsilon
constexpr ValT Rect(ValT value, MinT min, MaxT max, std::integral_constant< bool, kMinInclusive >=std::true_type{}, std::integral_constant< bool, kMaxInclusive >=std::false_type{})
T Norm(Simd< T, N > a)
Definition simd_inl.h:854
V VNearEqual(V a, V b, V epsilon)
Definition simd_inl.h:733
constexpr NdArray< T, 2 > OrthogonalVector(NdArray< T, 2 > const &vec)
Simd< T, N > Normalize(Simd< T, N > a)
Definition simd_inl.h:859
constexpr size_t ArgMin(NdArray< T, N > const &a)
constexpr NdArray< T, N > BasisVector(int axis)
constexpr T Mean(NdArray< T, N > const &a)
constexpr T Floor(T a)
constexpr T Ceil(T a)
constexpr T Sum(NdArray< T, N > const &a)
constexpr size_t ArgMax(NdArray< T, N > const &a)
constexpr ValT Clamp(ValT value, MinT min, MaxT max)
constexpr T const & Max(T const &a, T const &b)
constexpr T Prod(NdArray< T, N > const &a)
bool NearEqual(TransformRT const &a, TransformRT const &b, real epsilon=kDefaultNearEqualEpsilon< real >)
bool IsFinite(TransformRT const &a)
constexpr T Round(T a)
constexpr NdArray< T, 3 > Cross(NdArray< T, 3 > const &a, NdArray< T, 3 > const &b)