CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
cifti::MathFunctions Class Reference

#include <MathFunctions.h>

Static Public Member Functions

static int64_t combinations (const int64_t n, const int64_t k)
 
static int64_t permutations (const int64_t n, const int64_t k)
 
static int64_t factorial (const int64_t n)
 
static bool normalVector (const float v1[3], const float v2[3], const float v3[3], float normalVectorOut[3])
 
static bool normalVector (const double v1[3], const double v2[3], const double v3[3], double normalVectorOut[3])
 
static void normalVectorDirection (const float v1[3], const float v2[3], const float v3[3], float directionOut[3])
 
static void crossProduct (const float v1[], const float v2[], float resultOut[])
 
static void crossProduct (const double v1[], const double v2[], double resultOut[])
 
static void normalizedCrossProduct (const float x1[], const float x2[], float resultOut[])
 
static float normalizeVector (float vectorsAll[], const int32_t offset)
 
static float normalizeVector (float vectorInOut[3])
 
static double normalizeVector (double vectorInOut[3])
 
static float vectorLength (const float vector[3])
 
static float vectorLength (const float vectorsAll[], const int32_t offset)
 
static double vectorLength (const double vector[3])
 
static float distanceSquared3D (const float p1[3], const float p2[3])
 
static float distanceSquared3D (const float xyzAll[], const int32_t offsetCoord1, const int32_t offsetCoord2)
 
static float distance3D (const float p1[3], const float p2[3])
 
static double distanceSquared3D (const double p1[3], const double p2[3])
 
static double distance3D (const double p1[3], const double p2[3])
 
static double distanceSquared2D (const double x1, const double y1, const double x2, const double y2)
 
static void subtractVectors (const float v1[3], const float v2[3], float resultOut[3])
 
static void addVectors (const float v1[3], const float v2[3], float resultOut[3])
 
static void createUnitVector (const float startXYZ[3], const float endXYZ[3], float unitVectorOut[3])
 
static void createUnitVector (const double startXYZ[3], const double endXYZ[3], double unitVectorOut[3])
 
static float dotProduct (const float p1[3], const float p2[3])
 
static double dotProduct (const double p1[3], const double p2[3])
 
static float triangleArea (const float v1[3], const float v2[3], const float v3[3])
 
static float triangleArea (const double v1[3], const double v2[3], const double v3[3])
 
static float triangleArea (const float xyzAll[], const int32_t offsetCoord1, const int32_t offsetCoord2, const int32_t offsetCoord3)
 
static float triangleAreaSigned2D (const float p1[3], const float p2[3], const float p3[3])
 
static float triangleAreaSigned3D (const float referenceNormal[3], const float p1[3], const float p2[3], const float p3[3])
 
static bool lineIntersection2D (const float p1[3], const float p2[3], const float q1[3], const float q2[3], const float tolerance, float intersectionOut[3])
 
static bool rayIntersectPlane (const float p1[3], const float p2[3], const float p3[3], const float rayOrigin[3], const float rayVector[3], float intersectionXYZandDistance[3])
 
static void projectPoint (const float pt[3], const float origin[3], const float normal[3], float projectedPointOut[3])
 
static float signedDistanceFromPlane (const float planeNormal[3], const float pointInPlane[3], const float queryPoint[3])
 
static int32_t limitRange (const int32_t value, const int32_t minimumValue, const int32_t maximumValue)
 
static float limitRange (const float value, const float minimumValue, const float maximumValue)
 
static double limitRange (const double value, const double minimumValue, const double maximumValue)
 
static float distanceToLine3D (const float p1[3], const float p2[3], const float point[3])
 
static bool arraysEqual (const float a[], const float b[], const int32_t numElements)
 
static void averageOfThreeCoordinates (const float c1[3], const float c2[3], const float c3[3], float outputAverage[3])
 
static void averageOfThreeCoordinates (const float xyzAll[], const int32_t offsetCoord1, const int32_t offsetCoord2, const int32_t offsetCoord3, float outputAverage[], const int32_t outputOffset)
 
static float angle (const float p1[3], const float p2[3], const float p3[3])
 
static float signedAngle (const float pi[3], const float pj[3], const float pk[3], const float n[3])
 
static bool isOddNumber (const int32_t number)
 
static bool isEvenNumber (const int32_t number)
 
static bool isNaN (const float number)
 
static bool isPosInf (const float number)
 
static bool isNegInf (const float number)
 
static bool isInf (const float number)
 true if either inf or -inf
 
static bool isNumeric (const float number)
 true only if not NaN, inf, or -inf
 
static bool compareArrays (const float a1[], const float a2[], const int32_t numElements, const float tolerance)
 
static int32_t clamp (const int32_t value, const int32_t minimum, const int32_t maximum)
 
static float clamp (const float value, const float minimum, const float maximum)
 
static uint32_t gcd (uint32_t num1, uint32_t num2)
 greatest common divisor
 
static void quaternToMatrix (const float cijk[4], float matrix[3][3])
 convert quaternion to rotation matrix
 
static void quaternToMatrix (const double cijk[4], double matrix[3][3])
 convert quaternion to rotation matrix
 
static bool matrixToQuatern (const float matrix[3][3], float cijk[4])
 try to convert 3x3 matrix to quaternion (return false if not a rotation matrix)
 
static bool matrixToQuatern (const double matrix[3][3], double cijk[4])
 try to convert 3x3 matrix to quaternion (return false if not a rotation matrix)
 
static double remainder (const double numerator, const double denominator)
 
static double round (const double value)
 

Detailed Description

Various mathematical functions.

Member Function Documentation

◆ angle()

float MathFunctions::angle ( const float p1[3],
const float p2[3],
const float p3[3] )
static

Angle formed by p1, p2, p3 (angle at p2). Returned angle is in radians. This method uses Java Math.acos() and produces highly accurate results.

Parameters
p1- point.
p2- point.
p3- point.
Returns
Angle formed by points.

◆ arraysEqual()

bool MathFunctions::arraysEqual ( const float a[],
const float b[],
const int32_t numElements )
static

Determine if two arrays are equal, same number of elements and corresponding elements equal.

Parameters
a- first array.
b- second array.
Returns
true if arrays are equal, else false.

◆ averageOfThreeCoordinates() [1/2]

void MathFunctions::averageOfThreeCoordinates ( const float c1[3],
const float c2[3],
const float c3[3],
float outputAverage[3] )
static

Get the average of three coordinates.

Parameters
c1- coordinate 1
c2- coordinate 2
c3- coordinate 3
outputAverageA three-dimensional array into which the average of the three coordinates is placed.

◆ averageOfThreeCoordinates() [2/2]

void MathFunctions::averageOfThreeCoordinates ( const float xyzAll[],
const int32_t offsetCoord1,
const int32_t offsetCoord2,
const int32_t offsetCoord3,
float outputAverage[],
const int32_t outputOffset )
static

Calculate the average of 3 coordinates.

Parameters
xyzAllOne-dimensional array containing the XYZ coordinates.
offsetCoord1Offset of node 1's X-coordinate which is followed by the Y- and Z-coordinates.
offsetCoord2Offset of node 2's X-coordinate which is followed by the Y- and Z-coordinates.
offsetCoord3Offset of node 3's X-coordinate which is followed by the Y- and Z-coordinates.
outputAverage3 dimensional array passed in, into which the average is placed.
outputOffsetOffset of average into outputAverage array.

◆ clamp() [1/2]

float MathFunctions::clamp ( const float value,
const float minimum,
const float maximum )
static

Clamp a value to the range minimum to maximum.

Parameters
valueValue for clamping.
minimumMinimum allowed value.
maximumMaximum allowed value.
Returns
Value clamped to minimum and maximum.

◆ clamp() [2/2]

int32_t MathFunctions::clamp ( const int32_t value,
const int32_t minimum,
const int32_t maximum )
static

Clamp a value to the range minimum to maximum.

Parameters
valueValue for clamping.
minimumMinimum allowed value.
maximumMaximum allowed value.
Returns
Value clamped to minimum and maximum.

◆ combinations()

int64_t MathFunctions::combinations ( const int64_t n,
const int64_t k )
static

Calulate the number of combinations for choosing "k" elements from a total of "n" elements.

Formula: [n!/(k!*(n-k!)) If k > (n-k): [n(n-1)(n-2)...(k+1)] / (n-k)! If k < (n-k): [n(n-1)(n-2)...(n-k+1)] / k!

Parameters
n- total number of elements.
k- number of elements to choose.
Returns
- number of combinations.

◆ compareArrays()

bool MathFunctions::compareArrays ( const float a1[],
const float a2[],
const int32_t numElements,
const float tolerance )
static

Determine if two arrays are equal.

Parameters
a1First array.
a2Second array.
toleranceAllowable difference in elements at same index.
Returns
true if arrays are of same length and corresponding elements have a difference less than tolerance.

◆ createUnitVector() [1/2]

void MathFunctions::createUnitVector ( const double startXYZ[3],
const double endXYZ[3],
double unitVectorOut[3] )
static

Create the unit vector for a vector that starts at startXYZ and ends at endXYZ.

Parameters
startXYZ- Starting position of vector.
endXYZ- Ending position of vector.
unitVectorOut- output, vector starting at startXYZ and pointing to endXYZ.

◆ createUnitVector() [2/2]

void MathFunctions::createUnitVector ( const float startXYZ[3],
const float endXYZ[3],
float unitVectorOut[3] )
static

Create the unit vector for a vector that starts at startXYZ and ends at endXYZ.

Parameters
startXYZ- Starting position of vector.
endXYZ- Ending position of vector.
unitVectorOut- output, vector starting at startXYZ and pointing to endXYZ.

◆ crossProduct() [1/2]

void MathFunctions::crossProduct ( const double v1[],
const double v2[],
double resultOut[] )
static

Cross product of two 3D vectors.

Parameters
v1The first vector, an array of three floats.
v2The first vector, an array of three floats.
resultOutOutput containing the cross product.

◆ crossProduct() [2/2]

void MathFunctions::crossProduct ( const float v1[],
const float v2[],
float resultOut[] )
static

Cross product of two 3D vectors.

Parameters
v1The first vector, an array of three floats.
v2The first vector, an array of three floats.
resultOutOutput containing the cross product.

◆ distance3D() [1/2]

double MathFunctions::distance3D ( const double p1[3],
const double p2[3] )
static

Get the distance between two 3D points.

Parameters
p1Point 1 (3 element array)
p2Point 2 (3 element array)
Returns
Distance between the two points.

◆ distance3D() [2/2]

float MathFunctions::distance3D ( const float p1[3],
const float p2[3] )
static

Get the distance between two 3D points.

Parameters
p1Point 1 (3 element array)
p2Point 2 (3 element array)
Returns
Distance between the two points.

◆ distanceSquared2D()

double MathFunctions::distanceSquared2D ( const double x1,
const double y1,
const double x2,
const double y2 )
static

Distance SQUARED from (x1, y1) to (x2, y2)

Parameters
X-coordinateof first point.
Y-coordinateof first point.
X-coordinateof second point.
Y-coordinateof second point.
Returns
Distance squared between the points.

◆ distanceSquared3D() [1/3]

double MathFunctions::distanceSquared3D ( const double p1[3],
const double p2[3] )
static

Get the squared distance between two 3D points.

Parameters
p1Point 1 (3 element array)
p2Point 2 (3 element array)
Returns
Distance squared between the two points.

◆ distanceSquared3D() [2/3]

float MathFunctions::distanceSquared3D ( const float p1[3],
const float p2[3] )
static

Get the squared distance between two 3D points.

Parameters
p1Point 1 (3 element array)
p2Point 2 (3 element array)
Returns
Distance squared between the two points.

◆ distanceSquared3D() [3/3]

float MathFunctions::distanceSquared3D ( const float xyzAll[],
const int32_t offsetCoord1,
const int32_t offsetCoord2 )
static

Get the squared distance between two 3D coordinates.

Parameters
xyzAllArray containing all of the XYZ coordinates.
offsetCoord1Offset of the first coordinates X-coordinate.
offsetCoord2Offset of the second coordinates X-coordinate.
Returns
Distance squared between the two coordinates.

◆ distanceToLine3D()

float MathFunctions::distanceToLine3D ( const float p1[3],
const float p2[3],
const float point[3] )
static

Find the distance from the point to the line defined by p1 and p2. Formula is from "http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html".

Parameters
p1- First coordinate in line.
p2- Second coordinate in line.
point- coordinate for which distance to line is sought.
Returns
Distance from point to the line (p1, p2).

◆ dotProduct() [1/2]

double MathFunctions::dotProduct ( const double p1[3],
const double p2[3] )
static

Dot produce of three dimensional vectors.

Parameters
p1vector 1
p2vector 2
Returns
Dot product of the two vectors.

◆ dotProduct() [2/2]

float MathFunctions::dotProduct ( const float p1[3],
const float p2[3] )
static

Dot produce of three dimensional vectors.

Parameters
p1vector 1
p2vector 2
Returns
Dot product of the two vectors.

◆ factorial()

int64_t MathFunctions::factorial ( const int64_t n)
static

Calculate the factorial for a number.

Parameters
n- the number.
Returns
- its factiorial

◆ isEvenNumber()

bool MathFunctions::isEvenNumber ( const int32_t number)
static

Determine if an integer is an odd number.

Parameters
numberInteger to test.
Returns
true if integer is odd, else false.

◆ isOddNumber()

bool MathFunctions::isOddNumber ( const int32_t number)
static

Determine if an integer is an odd number.

Parameters
numberInteger to test.
Returns
true if integer is odd, else false.

◆ limitRange() [1/3]

double MathFunctions::limitRange ( const double value,
const double minimumValue,
const double maximumValue )
static

Limit the "value" to be inclusively between the minimum and maximum.

Parameters
value- Value for testing.
minimumValue- Minimum inclusive value.
maximumValue- Maximum inclusive value.
Returns
Value limited inclusively to the minimum and maximum values.

◆ limitRange() [2/3]

float MathFunctions::limitRange ( const float value,
const float minimumValue,
const float maximumValue )
static

Limit the "value" to be inclusively between the minimum and maximum.

Parameters
value- Value for testing.
minimumValue- Minimum inclusive value.
maximumValue- Maximum inclusive value.
Returns
Value limited inclusively to the minimum and maximum values.

◆ limitRange() [3/3]

int32_t MathFunctions::limitRange ( const int32_t value,
const int32_t minimumValue,
const int32_t maximumValue )
static

Limit the "value" to be inclusively between the minimum and maximum.

Parameters
value- Value for testing.
minimumValue- Minimum inclusive value.
maximumValue- Maximum inclusive value.
Returns
Value limited inclusively to the minimum and maximum values.

◆ lineIntersection2D()

bool MathFunctions::lineIntersection2D ( const float p1[3],
const float p2[3],
const float q1[3],
const float q2[3],
const float tolerance,
float intersectionOut[3] )
static

Determine if 2D line segments intersect. Algorithm from http://mathworld.wolfram.com/Line-LineIntersection.html

Parameters
p1Line 1 end point 1.
p2Line 1 end point 2.
q1Line 2 end point 1.
q2Line 2 end point 2.
toleranceTolerance around the vertices (essentially lengthens lines by this quantity). Caret5 set this parameter to 0.01.
intersectionOutLocation of intersection.
Returns
true if the line segments intersect else false.

◆ normalizedCrossProduct()

void MathFunctions::normalizedCrossProduct ( const float x1[],
const float x2[],
float resultOut[] )
static

Cross product of two 3D vectors with normalizing both the input and output vectors.

Parameters
x1The first vector, an array of three floats.
x2The first vector, an array of three floats.
Returns
The cross product, an array of three floats.

◆ normalizeVector() [1/3]

double MathFunctions::normalizeVector ( double vector[3])
static

Normalize a 3D vector (make its length 1.0).

Parameters
vectorInOutvector that is normalized.
Returns
The length of the vector prior to normalization.

◆ normalizeVector() [2/3]

float MathFunctions::normalizeVector ( float vector[3])
static

Normalize a 3D vector (make its length 1.0).

Parameters
vectorInOutvector that is normalized.
Returns
The length of the vector prior to normalization.

◆ normalizeVector() [3/3]

float MathFunctions::normalizeVector ( float vectorsAll[],
const int32_t offset )
static

Normalize a 3D vector (make its length 1.0).

Parameters
vectorsAllArray containing the XYZ components of the vectors.
offsetOffset of the vector's X-component in the vectorsAll array.
Returns
The length of the vector prior to normalization.

◆ normalVector() [1/2]

bool MathFunctions::normalVector ( const double v1[3],
const double v2[3],
const double v3[3],
double normalVectorOut[3] )
static

Compute a normal vector from three vertices and make it a unit vector.

Parameters
v1the first vertex, an array of three floats.
v2the first vertex, an array of three floats.
v3the first vertex, an array of three floats.
normalVectorOutA three-dimensional array passed into which the normal vector is loaded.
Returns
true if vector is valid (non-zero length).

◆ normalVector() [2/2]

bool MathFunctions::normalVector ( const float v1[3],
const float v2[3],
const float v3[3],
float normalVectorOut[3] )
static

Compute a normal vector from three vertices and make it a unit vector.

Parameters
v1the first vertex, an array of three floats.
v2the first vertex, an array of three floats.
v3the first vertex, an array of three floats.
normalVectorOutA three-dimensional array passed into which the normal vector is loaded.
Returns
true if vector is valid (non-zero length).

◆ normalVectorDirection()

void MathFunctions::normalVectorDirection ( const float v1[3],
const float v2[3],
const float v3[3],
float directionOut[3] )
static

Compute a normal vector from three vertices and but the returned vector IS NOT a unit vector.

Parameters
v1the first vertex, an array of three floats.
v2the first vertex, an array of three floats.
v3the first vertex, an array of three floats.
Returns
The normal vector, an array of three floats.

◆ permutations()

int64_t MathFunctions::permutations ( const int64_t n,
const int64_t k )
static

Calulate the number of permuations for choosing "k" elements from a total of "n" elements.

Formula: [n!/(n-m)!] = n(n-1)(n-2)...(n-m+1).

Parameters
n- total number of elements.
k- number of elements to choose.
Returns
- number of combinations.

◆ projectPoint()

void MathFunctions::projectPoint ( const float pt[3],
const float origin[3],
const float normal[3],
float projectedPointOut[3] )
static

Project a point to a plane.

Parameters
pt- the point to project.
origin- point in the plane.
normal- normal vector of plane.
projectedPointOut- output, the projected position of "pt" on the plane.

◆ rayIntersectPlane()

bool MathFunctions::rayIntersectPlane ( const float p1[3],
const float p2[3],
const float p3[3],
const float rayOrigin[3],
const float rayVector[3],
float intersectionXYZandDistance[3] )
static

Determine if a ray intersects a plane.

Parameters
p1- 1st point defining the plane
p2- 2nd point defining the plane
p3- 3rd point defining the plane
rayOrigin- origin of the ray
rayVector- vector defining the ray
intersectionXYZandDistance- An array of four that will contain the XYZ or the intersection point and the distance from the plane.
Returns
true if the ray intersects the plane, else false.

◆ remainder()

double MathFunctions::remainder ( const double numerator,
const double denominator )
static

Return the remainder from the resulting division using the given values.

This method is written to match the result produced by the remainder() function that is part of C99 but not supported on all platforms.

Code may appear verbose but it avoid functions calls to fabs(), ceil(), and floor().

Note: X is the numerator.
      Y is the denominator.

The remainder() functions compute the value r such that r = x - n*y,
where n is the integer nearest the exact value of x/y.

If there are two integers closest to x/y, n shall be the even one. 
Parameters
numeratorThe numerator.
denominatorThe denominator.
Returns
The remainder from numerator divided by denominator.

◆ round()

double MathFunctions::round ( const double value)
static

Return the value rounded to the nearest integral (integer) value.

Parameters
valueValue that is rounded.
Returns
Value rounded to nearest integral value.

◆ signedAngle()

float MathFunctions::signedAngle ( const float pi[3],
const float pj[3],
const float pk[3],
const float n[3] )
static

Signed angle for "jik".

Parameters
pi- point.
pj- point.
pk- point.
n- normal
Returns
signed angle formed by the points.

◆ signedDistanceFromPlane()

float MathFunctions::signedDistanceFromPlane ( const float planeNormal[3],
const float pointInPlane[3],
const float queryPoint[3] )
static

Get the signed distance from the plane to the "queryPoint".
A positive distance indicates "queryPoint" is above the plane and a negative distance indicates "queryPoint" is below the plane.

Parameters
planeNormal- plane's normal vector.
pointInPlane- point on the plane.
queryPoint- the query point for which distance to plane is sought.
Returns
Distance from the plane to the query point.

◆ subtractVectors()

void MathFunctions::subtractVectors ( const float v1[3],
const float v2[3],
float resultOut[3] )
static

subtract vectors (3d) result = v1 - v2.

Parameters
v11st vector input
v22nd vector input
resultOutoutput, 3D vector containing result of subtraction.

◆ triangleArea() [1/3]

float MathFunctions::triangleArea ( const double v1[3],
const double v2[3],
const double v3[3] )
static

Calculate the area for a triangle (with doubles)

Parameters
v1- XYZ coordinates for vertex 1
v2- XYZ coordinates for vertex 2
v3- XYZ coordinates for vertex 3
Returns
Area of triangle.

◆ triangleArea() [2/3]

float MathFunctions::triangleArea ( const float v1[3],
const float v2[3],
const float v3[3] )
static

Calculate the area for a triangle.

Parameters
v1- XYZ coordinates for vertex 1
v2- XYZ coordinates for vertex 2
v3- XYZ coordinates for vertex 3
Returns
Area of triangle.

◆ triangleArea() [3/3]

float MathFunctions::triangleArea ( const float xyzAll[],
const int32_t offsetCoord1,
const int32_t offsetCoord2,
const int32_t offsetCoord3 )
static

Calculate the area of a triangle formed by 3 coordinates.

Parameters
xyzAllOne-dimensional array containing the XYZ coordinates.
offsetCoord1Offset of node 1's X-coordinate which is followed by the Y- and Z-coordinates.
offsetCoord2Offset of node 2's X-coordinate which is followed by the Y- and Z-coordinates.
offsetCoord3Offset of node 3's X-coordinate which is followed by the Y- and Z-coordinates.
Returns
Area of the triangle formed by the coordinates.

◆ triangleAreaSigned2D()

float MathFunctions::triangleAreaSigned2D ( const float p1[3],
const float p2[3],
const float p3[3] )
static

Compute the signed area of a triangle in 2D.

Parameters
p1- 1st coordinate of triangle
p2- 2nd coordinate of triangle
p3- 3rd coordinate of triangle
Returns
Signed area of triangle which is positive if the vertices are in counter-clockwise orientation or negative if the vertices are in clockwise orientation.

◆ triangleAreaSigned3D()

float MathFunctions::triangleAreaSigned3D ( const float referenceNormal[3],
const float p1[3],
const float p2[3],
const float p3[3] )
static

Compute the signed area of a triangle in 3D.

Parameters
referenceNormal- Normal vector.
p1- 1st coordinate of triangle
p2- 2nd coordinate of triangle
p3- 3rd coordinate of triangle
Returns
Signed area of triangle which is positive if the vertices are in counter-clockwise orientation or negative if the vertices are in clockwise orientation.

◆ vectorLength() [1/3]

double MathFunctions::vectorLength ( const double vector[3])
static

Get length of vector.

Parameters
vectorVector whose length is computed.
Returns
The length of the vector.

◆ vectorLength() [2/3]

float MathFunctions::vectorLength ( const float vector[3])
static

Get length of vector.

Parameters
vectorVector whose length is computed.
Returns
The length of the vector.

◆ vectorLength() [3/3]

float MathFunctions::vectorLength ( const float vectorsAll[],
const int32_t offset )
static

Get length of vector.

Parameters
vectorsAllArray containing three-dimensional vectors.
offsetOffset of vector's X-component in vectorsAll array.
Returns
The length of the vector.

The documentation for this class was generated from the following files: