Burst compatible miscellaneous math related utility functions.
Currently, the package focuses on 2d related utilities, but functions for 3d math will be added as well.
- BurstMathUtils
- Table od Contents
- Getting started
- Features
- Algebra
- float Angle(float2 a, float2 b)
- float Cross(float2 a, float2 b)
- void EigenDecomposition(float2x2 matrix, out float2 eigval, out float2x2 eigvec)
- float2 Max(float2 a, float2 b, float2 c)
- float2 Min(float2 a, float2 b, float2 c)
- float2x2 OuterProduct(float2 a, float2 b)
- void PolarDecomposition(float2x2 A, out float2x2 U)
- void PolarDecomposition(float2x2 A, out float2x2 U, out float2x2 P)
- float2 Right()
- float2 Rotate90CCW(this float2 a)
- float2 Rotate90CW(this float2 a)
- float2x2 ToDiag(this float2 a)
- float2x2 Transform(this float2x2 M, float2x2 A)
- float2 Up()
- Primitives
- Geometry
- float2 Barycentric(float2 a, float2 b, float2 p)
- float3 Barycentric(float2 a, float2 b, float2 c, float2 p)
- float2 BarycentricSafe(float2 a, float2 b, float2 p, float2 @default = default)
- float3 BarycentricSafe(float2 a, float2 b, float2 c, float2 p, float3 @default = default)
- float CCW(float2 a, float2 b, float2 c)
- bool IsConvexQuadrilateral(float2 a, float2 b, float2 c, float2 d)
- void PointClosestPointOnLineSegment(float2 a, float2 b0, float2 b1, out float2 p)
- bool PointInsideTriangle(float2 p, float2 a, float2 b, float2 c)
- bool PointInsideTriangle(float2 p, float2 a, float2 b, float2 c, out float3 bar)
- float PointLineSignedDistance(float2 p, float2 n, float2 a)
- void ShortestLineSegmentBetweenLineSegments(float2 a0, float2 a1, float2 b0, float2 b1, out float2 pA, out float2 pB)
- Misc
- Complex
- Complex Conjugate(Complex z)
- Complex LookRotation(float2 direction)
- Complex LookRotationSafe(float2 direction, float2 @default = default)
- Complex Normalize(Complex z)
- Complex NormalizeSafe(Complex z, Complex @default = default)
- Complex Polar(float r, float phi)
- Complex PolarUnit(float phi)
- Complex Pow(Complex z, float x)
- Complex Reciprocal(Complex z)
- Dependencies
Install the package using one of the following methods
Using scoped registry (recommended)
Use OpenUPM CLI or add corresponding entries to the project'smanifest.json
manually.
Add or modify scoped registries in the manifest
"scopedRegistries": [ { "name": "OpenUPM", "url": "https://package.openupm.com/", "scopes": [ "com.andywiecko" ] } ]and in the dependencies provide selected version of the package
"dependencies": { "com.andywiecko.burst.mathutils": "1.3.1", ...See Unity docs for more details https://docs.unity3d.com/2021.1/Documentation/Manual/upm-scoped.html
git
install
Use package manager via git install: https://github.com/andywiecko/BurstMathUtils.git#v1.3.1
Manual instalation
Clone or download this repository and then selectpackage.json
using Package Manager (Window/Package Manager
).
Package contains a static class with utilities (and extensions), i.e. MathUtils
which includes many
useful functions related to the following categories
Algebra,
Primitives,
Geometry, and
Misc.
Additionally, the package introduces struct Complex struct, a burst-friendly complex number representation.
Angle (in radias) between vectors a and b.
Two-dimensional cross product between vectors a and b.
Procedure solves eigen problem for symmetric matrix.
Componentwise maximum of three vectors.
Componentwise minimum of three vectors.
Outer product of two vectors, i.e. a · bᵀ.
Procedure solves polar decomposition problem for matrix A, formulated as A = U · P, where U a is unitary matrix and P is a positive semi-definite Hermitian matrix.
Procedure solves polar decomposition problem for matrix A, formulated as A = U · P, where U a is unitary matrix and P is a positive semi-definite Hermitian matrix.
Right axis (1, 0).
Rotated vector a by 90° (counter-clockwise).
Rotated vector a by 90° (clockwise).
Diagonal matrix with values a placed in the diagonal.
Transformed matrix M, with given transformation A, i.e. A · M · Aᵀ.
Up axis (0, 1).
Bounding circle at point p and radius r of the triangle (a, b, c)
Circumcenter at point p and radius r of the triangle (a, b, c)
Signed area of triangle (a, b, c).
Doubled signed area of triangle (a, b, c).
Position p expressed in barycentric coordinate system defined within line segment (a, b).
Position p expressed in barycentric coordinate system defined within triangle (a, b, c).
Position p expressed in barycentric coordinate system defined within line segment (a, b). If the result is not finite, then returns default.
Position p expressed in barycentric coordinate system defined within triangle (a, b, c). If the result is not finite, then returns default.
Triangle (a, b, c) counterclockwise check.
Returns:
- +1 triangle is counterclockwise.
- 0 triangle is degenerate (colinear points).
- -1 triangle is clockwise.
true if quadrilateral (a, b, c, d) is convex, false otherwise.
Procedure finds the closest point p on the line segment (b0, b1) to point a.
true if p is inside triangle (a, b, c), false otherwise.
true if p is inside triangle (a, b, c), false otherwise. bar corresponds to barycentric coordinates of p in triangle (a, b, c).
Point–line signed distance.
void ShortestLineSegmentBetweenLineSegments(float2 a0, float2 a1, float2 b0, float2 b1, out float2 pA, out float2 pB)
Procedure finds the shortest line segment (pA, pB), between line segments (a0, a1) and (b0, b1)
Utility function for id enumeration in bilateral interleaving order, e.g. sequence of id = 0, 1, 2, 3, 4, 5 (count = 6), will be enumerated in the following way: 0, 5, 1, 4, 2, 3.
Utility function for converting linear index into 2-d index for lower triangular matrix, stored in a row-wise fashion.
Based on the paper1.
Utility function for converting 2-d index into linear index for lower triangular matrix, stored in a row-wise fashion.
Rounds value with respect to given digits.
Rounds value with respect to given digits.
Rounds value with respect to given digits.
Struct complex is a burst-friendly complex number representation.
It supports basic arthimetic operators +, -, *, /
.
Complex struct implementation is based on Unity.Collections.float2
so all functions/operations should be optimized using Burst SIMD intrinsics.
Small demo how Complex
can be used is presented below
Complex z = (1.2f, 5.7f);
z += 1f; // Expected: z = (2.2f, 5.7f)
z = (0, 1);
z *= z; // Expected: z = (-1, 0)
z = Complex.Polar(r: 2, phi: math.PI); // Returns 2 * exp(i * PI)
Additional utility functions for complex numbers can be found below.
Conjugated number z, i.e. z*.
Complex number which represent view in the given direction.
Complex number which represent view in the given direction. If rotation cannot be represented by the finite number, then returns default.
Normalized complex number z.
Normalized complex number z. If normalized number cannot be represented by the finite number, then returns default.
Complex number via polar construction: r · eⁱᵠ.
Complex number via (unit length) polar construction: r · eⁱᵠ.
z raised to the power x, i.e. zˣ.
Repciprocal of z, i.e. z⁻¹.
Footnotes
-
M. Angeletti, J-M. Bonny, and J. Koko. "Parallel Euclidean distance matrix computation on big datasets." (2019). ↩