Class MathUtilities
Math Utilities class.
Inherited Members
Namespace: Microsoft.MixedReality.Toolkit.Utilities
Assembly: cs.temp.dll.dll
Syntax
public static class MathUtilities
Methods
Add(Matrix4x4, Matrix4x4)
Element-wise addition of two Matrix4x4s - extension method
Declaration
public static Matrix4x4 Add(Matrix4x4 a, Matrix4x4 b)
Parameters
Type | Name | Description |
---|---|---|
Matrix4x4 | a | matrix |
Matrix4x4 | b | matrix |
Returns
Type | Description |
---|---|
Matrix4x4 | element-wise (a+b) |
AngularScaleFromDistance(Single, Single)
Declaration
[Obsolete("Method obsolete. Use ScaleFromAngularSizeAndDistance instead")]
public static float AngularScaleFromDistance(float angle, float distance)
Parameters
Type | Name | Description |
---|---|---|
Single | angle | |
Single | distance |
Returns
Type | Description |
---|---|
Single |
CLampLerp(Single, Single, Single, Single)
Clamps via a lerp for a "soft" clamp effect
Declaration
public static float CLampLerp(float pos, float min, float max, float clampFactor)
Parameters
Type | Name | Description |
---|---|---|
Single | pos | number to clamp |
Single | min | if pos is less than min, then lerp clamps to this value |
Single | max | if pos is more than max, lerp clamps to this value |
Single | clampFactor | Range from 0.0f to 1.0f of how close to snap to min and max |
Returns
Type | Description |
---|---|
Single | A soft clamped value |
ClosestPointOnLineSegmentToPoint(Vector3, Vector3, Vector3)
Declaration
public static Vector3 ClosestPointOnLineSegmentToPoint(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | |
Vector3 | lineStart | |
Vector3 | lineEnd |
Returns
Type | Description |
---|---|
Vector3 |
ClosestPointOnLineToPoint(Vector3, Vector3, Vector3)
Declaration
public static Vector3 ClosestPointOnLineToPoint(Vector3 point, Vector3 linePointA, Vector3 linePointB)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | |
Vector3 | linePointA | |
Vector3 | linePointB |
Returns
Type | Description |
---|---|
Vector3 |
DegreesToRadians(Double)
Convert degrees to radians.
Declaration
public static float DegreesToRadians(double degrees)
Parameters
Type | Name | Description |
---|---|---|
Double | degrees | Angle, in degrees. |
Returns
Type | Description |
---|---|
Single | Angle, in radians. |
DistanceOfPointToLine(Ray, Vector3)
find unsigned distance of 3D point to an infinite line
Declaration
public static float DistanceOfPointToLine(Ray ray, Vector3 point)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | ray that specifies an infinite line |
Vector3 | point | 3D point |
Returns
Type | Description |
---|---|
Single | unsigned perpendicular distance from point to line |
DistanceOfPointToLine(Vector3, Vector3, Vector3)
Returns the distance between a point and an infinite line defined by two points; linePointA and linePointB
Declaration
public static float DistanceOfPointToLine(Vector3 point, Vector3 linePointA, Vector3 linePointB)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | |
Vector3 | linePointA | |
Vector3 | linePointB |
Returns
Type | Description |
---|---|
Single |
DistanceOfPointToLineSegment(Vector3, Vector3, Vector3)
Declaration
public static float DistanceOfPointToLineSegment(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | point | |
Vector3 | lineStart | |
Vector3 | lineEnd |
Returns
Type | Description |
---|---|
Single |
GetAngleBetween(Vector2, Vector2)
Calculates the angle (at pointA) between two, two-dimensional points.
Declaration
public static float GetAngleBetween(Vector2 pointA, Vector2 pointB)
Parameters
Type | Name | Description |
---|---|---|
Vector2 | pointA | The first point. |
Vector2 | pointB | The second point. |
Returns
Type | Description |
---|---|
Single | The angle between the two points. |
GetDirection(Quaternion)
Calculates the direction vector from a rotation.
Declaration
public static Vector3 GetDirection(Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
Quaternion | rotation | Quaternion representing the rotation of the object. |
Returns
Type | Description |
---|---|
Vector3 | Normalized Vector3 representing the direction vector. |
InRange(Vector2, Vector2, Vector2)
Tests component-wise if a Vector2 is in a given range
Declaration
public static bool InRange(Vector2 vec, Vector2 lower, Vector2 upper)
Parameters
Type | Name | Description |
---|---|---|
Vector2 | vec | The vector to test |
Vector2 | lower | The lower bounds |
Vector2 | upper | The upper bounds |
Returns
Type | Description |
---|---|
Boolean | true if in range, otherwise false |
InRange(Vector3, Vector3, Vector3)
Tests component-wise if a Vector3 is in a given range
Declaration
public static bool InRange(Vector3 vec, Vector3 lower, Vector3 upper)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | vec | The vector to test |
Vector3 | lower | The lower bounds |
Vector3 | upper | The upper bounds |
Returns
Type | Description |
---|---|
Boolean | true if in range, otherwise false |
IsInFOV(Vector3, Transform, Single, Single, Single, Single)
Returns if a point lies within a frame of reference view as defined by arguments
Declaration
public static bool IsInFOV(Vector3 testPosition, Transform frameOfReference, float verticalFOV, float horizontalFOV, float minPlaneDistance, float maxPlaneDistance)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | testPosition | |
Transform | frameOfReference | |
Single | verticalFOV | |
Single | horizontalFOV | |
Single | minPlaneDistance | |
Single | maxPlaneDistance |
Returns
Type | Description |
---|---|
Boolean |
Remarks
Field of view parameters are in degrees and plane distances are in meters
IsInFOVCone(Transform, Vector3, Single, Single, Single)
Returns true if a point lies inside the cone described with given parameters, false otherwise. The cone is inscribed to a radius equal to the vertical height of the provided FOV. The test also ensures the distance from the point to the cone lies within the given range.
Declaration
public static bool IsInFOVCone(Transform cone, Vector3 point, float fieldOfView, float minDist = 0.05F, float maxDist = 100F)
Parameters
Type | Name | Description |
---|---|---|
Transform | cone | The transform that defines the orientation and position of the cone |
Vector3 | point | The point to test if it lies within the cone FOV |
Single | fieldOfView | Field of view for the cone which calculates its radius |
Single | minDist | Point must be at least this far away (along direction forward) from the cone |
Single | maxDist | Point must be at most this far away (along direction forward) from the cone. |
Returns
Type | Description |
---|---|
Boolean |
Remarks
Field of view parameter is in degrees and distances are in meters.
NearestPointToLines(Ray, Ray)
Find 3D point that minimizes distance to 2 lines, midpoint of the shortest perpendicular line segment between them
Declaration
public static Vector3 NearestPointToLines(Ray p, Ray q)
Parameters
Type | Name | Description |
---|---|---|
Ray | p | ray that specifies a line |
Ray | q | ray that specifies a line |
Returns
Type | Description |
---|---|
Vector3 | point nearest to the lines |
NearestPointToLinesLeastSquares(IEnumerable<Ray>)
Find 3D point that minimizes distance to a set of 2 or more lines
Declaration
public static Vector3 NearestPointToLinesLeastSquares(IEnumerable<Ray> rays)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Ray> | rays | each ray specifies an infinite line |
Returns
Type | Description |
---|---|
Vector3 | point nearest to the set of lines |
NearestPointToLinesRANSAC(List<Ray>, Int32, Single, out Int32)
Find 3D point that minimizes distance to a set of 2 or more lines, ignoring outliers
Declaration
public static Vector3 NearestPointToLinesRANSAC(List<Ray> rays, int ransac_iterations, float ransac_threshold, out int numActualInliers)
Parameters
Type | Name | Description |
---|---|---|
List<Ray> | rays | list of rays, each specifying a line, must have at least 1 |
Int32 | ransac_iterations | number of iterations: log(1-p)/log(1-(1-E)^s) where p is probability of at least one sample containing s points is all inliers E is proportion of outliers (1-ransac_ratio) e.g. p=0.999, ransac_ratio=0.54, s=2 ==> log(0.001)/(log(1-0.54^2) = 20 |
Single | ransac_threshold | minimum distance from point to line for a line to be considered an inlier |
Int32 | numActualInliers | return number of inliers: lines that are within ransac_threshold of nearest point |
Returns
Type | Description |
---|---|
Vector3 | point nearest to the set of lines, ignoring outliers |
QuaternionFromMatrix(Matrix4x4)
Creates a quaternion containing the rotation from the input matrix.
Declaration
public static Quaternion QuaternionFromMatrix(Matrix4x4 m)
Parameters
Type | Name | Description |
---|---|---|
Matrix4x4 | m | Input matrix to convert to quaternion |
Returns
Type | Description |
---|---|
Quaternion |
RadiansToDegrees(Single)
Convert radians to degrees.
Declaration
public static float RadiansToDegrees(float radians)
Parameters
Type | Name | Description |
---|---|---|
Single | radians | Angle, in radians. |
Returns
Type | Description |
---|---|
Single | Angle, in degrees. |
ScaleFromAngularSizeAndDistance(Single, Single)
Retrieve angular measurement describing how large a sphere or circle appears from a given point of view. Takes an angle (at given point of view) and a distance and returns the actual diameter of the object.
Declaration
public static float ScaleFromAngularSizeAndDistance(float angle, float distance)
Parameters
Type | Name | Description |
---|---|---|
Single | angle | |
Single | distance |
Returns
Type | Description |
---|---|
Single |
Subtract(Matrix4x4, Matrix4x4)
Element-wise subtraction of two Matrix4x4s - extension method
Declaration
public static Matrix4x4 Subtract(Matrix4x4 a, Matrix4x4 b)
Parameters
Type | Name | Description |
---|---|---|
Matrix4x4 | a | matrix |
Matrix4x4 | b | matrix |
Returns
Type | Description |
---|---|
Matrix4x4 | element-wise (a-b) |
TestPlanesAABB(Plane[], Int32, Bounds, out Boolean)
Declaration
public static bool TestPlanesAABB(Plane[] planes, int planeMask, Bounds bounds, out bool entirelyInside)
Parameters
Type | Name | Description |
---|---|---|
Plane[] | planes | |
Int32 | planeMask | |
Bounds | bounds | |
Boolean | entirelyInside |
Returns
Type | Description |
---|---|
Boolean |
ToTranslationRotation(Matrix4x4, out Vector3, out Quaternion)
Extract the translation and rotation components of a Unity matrix
Declaration
public static void ToTranslationRotation(Matrix4x4 unityMtx, out Vector3 translation, out Quaternion rotation)
Parameters
Type | Name | Description |
---|---|---|
Matrix4x4 | unityMtx | |
Vector3 | translation | |
Quaternion | rotation |
TransformDirectionFromTo(Transform, Transform, Vector3)
Takes a direction in the coordinate space specified by the "from" transform and transforms it to be the correct direction in the coordinate space specified by the "to" transform applies rotation only, no translation or scale
Declaration
public static Vector3 TransformDirectionFromTo(Transform from, Transform to, Vector3 fromDirection)
Parameters
Type | Name | Description |
---|---|---|
Transform | from | |
Transform | to | |
Vector3 | fromDirection |
Returns
Type | Description |
---|---|
Vector3 | Direction to. |
TransformPointFromTo(Transform, Transform, Vector3)
Takes a point in the coordinate space specified by the "from" transform and transforms it to be the correct point in the coordinate space specified by the "to" transform applies rotation, scale and translation.
Declaration
public static Vector3 TransformPointFromTo(Transform from, Transform to, Vector3 fromPoint)
Parameters
Type | Name | Description |
---|---|---|
Transform | from | |
Transform | to | |
Vector3 | fromPoint |
Returns
Type | Description |
---|---|
Vector3 | Point to. |
TransformRayFromTo(Transform, Transform, Ray)
Takes a ray in the coordinate space specified by the "from" transform and transforms it to be the correct ray in the coordinate space specified by the "to" transform
Declaration
public static Ray TransformRayFromTo(Transform from, Transform to, Ray rayToConvert)
Parameters
Type | Name | Description |
---|---|---|
Transform | from | |
Transform | to | |
Ray | rayToConvert |
Returns
Type | Description |
---|---|
Ray |
TransformVectorFromTo(Transform, Transform, Vector3)
Takes a vector in the coordinate space specified by the "from" transform and transforms it to be the correct direction in the coordinate space specified by the "to" transform applies rotation and scale, no translation
Declaration
public static Vector3 TransformVectorFromTo(Transform from, Transform to, Vector3 vecInFrom)
Parameters
Type | Name | Description |
---|---|---|
Transform | from | |
Transform | to | |
Vector3 | vecInFrom |
Returns
Type | Description |
---|---|
Vector3 |
XYProject(Vector3)
Project vector onto XY plane
Declaration
public static Vector3 XYProject(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v |
Returns
Type | Description |
---|---|
Vector3 | result of projecting v onto XY plane |
XZProject(Vector3)
Project vector onto XZ plane
Declaration
public static Vector3 XZProject(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v |
Returns
Type | Description |
---|---|
Vector3 | result of projecting v onto XZ plane |
YZProject(Vector3)
Project vector onto YZ plane
Declaration
public static Vector3 YZProject(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v |
Returns
Type | Description |
---|---|
Vector3 | result of projecting v onto YZ plane |