class MAGES::MeshDeformations::Utilities::Math

Overview

A collection of math utility functions. More…

class Math
{
public:
    // methods

    static float3x3 OuterProduct(float3 a, float3 b);

    static float TetrahedronVolume(
        in float3 p1,
        in float3 p2,
        in float3 p3,
        in float3 p4
    );

    static bool IsPointInsideTetrahedron(
        in float3 point,
        in float3 p0,
        in float3 p1,
        in float3 p2,
        in float3 p3
    );

    static bool TetrahedronBarycentricCoordinates(
        in float3 point,
        in float3 t0,
        in float3 t1,
        in float3 t2,
        in float3 t3,
        out float4 barycentricCoordinates
    );

    static float3 TetrahedronBarycentricInterpolation(
        in float3 p0,
        in float3 p1,
        in float3 p2,
        in float3 p3,
        float4 barycentricCoordinates
    );

    static float3 TriangleBarycentricCoordinates(
        float3 point,
        float3 triA,
        float3 triB,
        float3 triC
    );

    static float3 TriangleBarycentricInterpolation(
        in float3 p0,
        in float3 p1,
        in float3 p2,
        in float3 barycentricCoordinates
    );

    static bool ArePointsOnTheSameSideOfTriangle(
        float3 p1,
        float3 p2,
        float3 tri1,
        float3 tri2,
        float3 tri3
    );

    static bool GetSideOfTriangle(
        float3 point,
        float3 tri1,
        float3 tri2,
        float3 tri3
    );

    static bool IsPointInsideTriangle(float3 point, float3 v0, float3 v1, float3 v2);

    static float3 LineSegmentPlaneIntersection(
        float3 lineSegmentStart,
        float3 lineSegmentEnd,
        float3 planePoint,
        float3 planeNormal
    );

    static float3 RayPlaneIntersection(
        Ray ray,
        float3 planePoint,
        float3 planeNormal
    );

    static bool LineSegmentTriangleIntersection(
        in float3 triA,
        in float3 triB,
        in float3 triC,
        in float3 lineSegmentPointA,
        in float3 lineSegmentPointB,
        out float3 barycentricCoords
    );

    static bool RayTriangleIntersection(
        in Ray ray,
        in float3 triA,
        in float3 triB,
        in float3 triC,
        out float3 intersectionPoint
    );

    static bool RayTetrahedronIntersection(
        in Ray ray,
        in float3 tetA,
        in float3 tetB,
        in float3 tetC,
        in float3 tetD,
        out float3 intersectionPoint
    );

    static float3 TriangleNormal(in float3 p1, in float3 p2, in float3 p3);
    static quaternion ExtractRotation(this Matrix4x4 matrix);
    static float3 ExtractPosition(this Matrix4x4 matrix);
    static float3 ExtractScale(this Matrix4x4 matrix);
    static bool IsNaN(in this float3 val);
    static bool IsInfinity(in this float3 val);

    static float3 CatmullRomSpline(
        in float3 p0,
        in float3 p1,
        in float3 p2,
        in float3 p3,
        float t
    );

    static bool Approximately(in float3 vec, in float3 comp);
    static float3 ArbitraryPerpendicularVector(in float3 v);
};

Detailed Documentation

A collection of math utility functions.

Methods

static float3x3 OuterProduct(float3 a, float3 b)

Calculates the outer product of two vectors.

Parameters:

a

The first vector.

b

The second vector.

Returns:

The outer product matrix.

static float TetrahedronVolume(
    in float3 p1,
    in float3 p2,
    in float3 p3,
    in float3 p4
)

Calculates the volume of a tetrahedron defined by four points.

Parameters:

p1

The first point of the tetrahedron.

p2

The second point of the tetrahedron.

p3

The third point of the tetrahedron.

p4

The fourth point of the tetrahedron.

Returns:

The volume of the tetrahedron.

static bool IsPointInsideTetrahedron(
    in float3 point,
    in float3 p0,
    in float3 p1,
    in float3 p2,
    in float3 p3
)

Determines if a point is inside a tetrahedron.

Parameters:

point

The point to test.

p0

The first vertex of the tetrahedron.

p1

The second vertex of the tetrahedron.

p2

The third vertex of the tetrahedron.

p3

The fourth vertex of the tetrahedron.

Returns:

True if the point is inside the tetrahedron; otherwise, false.

static bool TetrahedronBarycentricCoordinates(
    in float3 point,
    in float3 t0,
    in float3 t1,
    in float3 t2,
    in float3 t3,
    out float4 barycentricCoordinates
)

Calculates the barycentric coordinates of a point within a tetrahedron.

Parameters:

point

The point to test.

t0

The first vertex of the tetrahedron.

t1

The second vertex of the tetrahedron.

t2

The third vertex of the tetrahedron.

t3

The fourth vertex of the tetrahedron.

barycentricCoordinates

The barycentric coordinates of the point within the tetrahedron.

Returns:

true if barycentric coordinates were calculated successfully.

static float3 TetrahedronBarycentricInterpolation(
    in float3 p0,
    in float3 p1,
    in float3 p2,
    in float3 p3,
    float4 barycentricCoordinates
)

Interpolates a point within a tetrahedron using barycentric coordinates.

Parameters:

p0

The first vertex of the tetrahedron.

p1

The second vertex of the tetrahedron.

p2

The third vertex of the tetrahedron.

p3

The fourth vertex of the tetrahedron.

barycentricCoordinates

The barycentric coordinates.

Returns:

The interpolated point within the tetrahedron.

static float3 TriangleBarycentricCoordinates(
    float3 point,
    float3 triA,
    float3 triB,
    float3 triC
)

Calculates the barycentric coordinates of a point within a triangle.

Parameters:

point

The point to test.

triA

The first vertex of the triangle.

triB

The second vertex of the triangle.

triC

The third vertex of the triangle.

Returns:

The barycentric coordinates of the point within the triangle.

static float3 TriangleBarycentricInterpolation(
    in float3 p0,
    in float3 p1,
    in float3 p2,
    in float3 barycentricCoordinates
)

Interpolates a point within a triangle using barycentric coordinates.

Parameters:

p0

The first vertex of the triangle.

p1

The second vertex of the triangle.

p2

The third vertex of the triangle.

barycentricCoordinates

The barycentric coordinates.

Returns:

The interpolated point within the triangle.

static bool ArePointsOnTheSameSideOfTriangle(
    float3 p1,
    float3 p2,
    float3 tri1,
    float3 tri2,
    float3 tri3
)

Checks if two points are on the same side of a triangle defined by three vertices.

Parameters:

p1

The first point to test.

p2

The second point to test.

tri1

The first vertex of the triangle.

tri2

The second vertex of the triangle.

tri3

The third vertex of the triangle.

Returns:

True if both points are on the same side of the triangle; otherwise, false.

static bool GetSideOfTriangle(
    float3 point,
    float3 tri1,
    float3 tri2,
    float3 tri3
)

Determines which side of a triangle a point is on.

Parameters:

point

The point to test.

tri1

The first vertex of the triangle.

tri2

The second vertex of the triangle.

tri3

The third vertex of the triangle.

Returns:

True if the point is on the same side as the normal of the triangle; otherwise, false.

static bool IsPointInsideTriangle(float3 point, float3 v0, float3 v1, float3 v2)

Determines if a point is inside the triangle defined by three vertices.

Parameters:

point

The point to test.

v0

The first vertex of the triangle.

v1

The second vertex of the triangle.

v2

The third vertex of the triangle.

Returns:

True if the point is inside the triangle; otherwise, false.

static float3 LineSegmentPlaneIntersection(
    float3 lineSegmentStart,
    float3 lineSegmentEnd,
    float3 planePoint,
    float3 planeNormal
)

Calculates the intersection point of a line segment and a plane.

Parameters:

lineSegmentStart

The start point of the line segment.

lineSegmentEnd

The end point of the line segment.

planePoint

A point on the plane.

planeNormal

The normal vector of the plane.

Returns:

The intersection point as a float3, if it exists.

static float3 RayPlaneIntersection(
    Ray ray,
    float3 planePoint,
    float3 planeNormal
)

Calculates the intersection point of a line segment and a plane.

Parameters:

ray

The ray.

planePoint

A point on the plane.

planeNormal

The normal vector of the plane.

Returns:

The intersection point as a float3, if it exists.

static bool LineSegmentTriangleIntersection(
    in float3 triA,
    in float3 triB,
    in float3 triC,
    in float3 lineSegmentPointA,
    in float3 lineSegmentPointB,
    out float3 barycentricCoords
)

Determines if a line segment intersects with a triangle and calculates the barycentric coordinates of the intersection point.

Parameters:

triA

The first vertex of the triangle.

triB

The second vertex of the triangle.

triC

The third vertex of the triangle.

lineSegmentPointA

The start point of the line segment.

lineSegmentPointB

The end point of the line segment.

barycentricCoords

The barycentric coordinates of the intersection point, if it exists.

Returns:

True if the line segment intersects the triangle; otherwise, false.

static bool RayTriangleIntersection(
    in Ray ray,
    in float3 triA,
    in float3 triB,
    in float3 triC,
    out float3 intersectionPoint
)

Checks if a ray intersects a triangle.

Parameters:

ray

The ray.

triA

The first vertex of the triangle.

triB

The second vertex of the triangle.

triC

The third vertex of the triangle.

intersectionPoint

The intersection point.

Returns:

True if intersects.

static bool RayTetrahedronIntersection(
    in Ray ray,
    in float3 tetA,
    in float3 tetB,
    in float3 tetC,
    in float3 tetD,
    out float3 intersectionPoint
)

Gets the intersection point of a ray and a tetrahedron.

Parameters:

ray

triA

triB

triC

intersectionPoint

Returns:

static float3 TriangleNormal(in float3 p1, in float3 p2, in float3 p3)

Calculates the normal vector of a triangle defined by three points.

Parameters:

p1

The first point of the triangle.

p2

The second point of the triangle.

p3

The third point of the triangle.

Returns:

The normal vector of the triangle.

static quaternion ExtractRotation(this Matrix4x4 matrix)

Extracts the rotation component from a transformation matrix as a quaternion.

Parameters:

matrix

The transformation matrix.

Returns:

The rotation as a quaternion.

static float3 ExtractPosition(this Matrix4x4 matrix)

Extracts the position component from a transformation matrix.

Parameters:

matrix

The transformation matrix.

Returns:

The position vector.

static float3 ExtractScale(this Matrix4x4 matrix)

Extracts the scale component from a transformation matrix.

Parameters:

matrix

The transformation matrix.

Returns:

The scale vector.

static bool IsNaN(in this float3 val)

Returns true if the val contains NaN values.

Parameters:

val

The float3 to check.

Returns:

True if val contains NaN values.

static bool IsInfinity(in this float3 val)

Returns true if at least one of the components of the val is infinity.

Parameters:

val

The value to check for inifinity.

Returns:

Bool if at least one of the val values is infinity.

static float3 CatmullRomSpline(
    in float3 p0,
    in float3 p1,
    in float3 p2,
    in float3 p3,
    float t
)

Calculates a point along a catmull-rom spline.

Parameters:

p0

Control point P0.

p1

Control point P1.

p2

Control point P2.

p3

Control point P3.

t

Interpolation Factor.

Returns:

The interpolated point along the spline.

static bool Approximately(in float3 vec, in float3 comp)

Tests if two float3 vectors are approximately equal.

Parameters:

vec

The vector1.

comp

The vector2.

Returns:

true if values are approximately the same.

static float3 ArbitraryPerpendicularVector(in float3 v)

Finds a vector perpendicular to v.

Parameters:

v

The input vector.

Returns:

Returns one of the perpendicular vectors to the input vector