Skip to content

Math

Introduction

vtkMath provides methods to perform common math operations. These include providing constants such as Pi; conversion from degrees to radians; vector operations such as dot and cross products and vector norm; matrix determinant for 2x2 and 3x3 matrices; univariate polynomial solvers; and for random number generation (for backward compatibility only). Contrary to the rest of vtk.js, vtkMath is in row-major format (similar to VTK C++).

Methods

LUFactor3x3

LU Factorization of a 3x3 matrix.

ArgumentTypeRequiredDescription
mat_3x3Matrix3x3Yes
index_3Vector3Yes

LUSolve3x3

LU back substitution for a 3x3 matrix.

ArgumentTypeRequiredDescription
mat_3x3Matrix3x3Yes
index_3Vector3Yes
x_3Vector3Yes

Pi

Get the number π.

add

Add two 3-vectors.

ArgumentTypeRequiredDescription
aVector3YesThe first 3D vector.
bVector3YesThe second 3D vector.
outVector3YesThe output 3D vector.

angleBetweenVectors

Angle between 3D vectors.

ArgumentTypeRequiredDescription
v1Vector3YesThe first 3D vector.
v2Vector3YesThe second 3D vector.

areBoundsInitialized

ArgumentTypeRequiredDescription
boundsBoundsYesThe bounds to check.

areEquals

Returns true if elements of both arrays are equals.

ArgumentTypeRequiredDescription
aArray[Number]YesAn array of numbers (vector, point, matrix...)
bArray[Number]YesAn array of numbers (vector, point, matrix...)
epsNumberNoThe tolerance value.

arrayMax

Get the maximum of the array.

ArgumentTypeRequiredDescription
arrArray[Number]YesThe array.
offsetNumberYesThe offset.
strideNumberYesThe stride.

arrayMin

Get the minimum of the array.

ArgumentTypeRequiredDescription
arrArray[Number]YesThe array.
offsetNumberYesThe offset.
strideNumberYesThe stride.

arrayRange

ArgumentTypeRequiredDescription
arrArray[Number]YesThe array.
offsetNumberYesThe offset.
strideNumberYesThe stride.

beginCombination

Start iterating over "m choose n" objects.

ArgumentTypeRequiredDescription
mNumberNoThe first numeric expression.
nNumberNoThe second numeric expression.

binomial

The number of combinations of n objects from a pool of m objects (m>n).

ArgumentTypeRequiredDescription
mNumberYesThe first numeric expression.
nNumberYesThe second numeric expression.

boundsIsWithinOtherBounds

Check if first 3D bounds is within the second 3D bounds.

ArgumentTypeRequiredDescription
bounds1_6BoundsYesThe first bounds.
bounds2_6BoundsYesThe second bounds.
delta_3Vector3YesThe error margin along each axis.

ceil

Same as Math.ceil().

ArgumentTypeRequiredDescription
param1NumberYesA numeric expression.

ceilLog2

Gives the exponent of the lowest power of two not less than x.

clampAndNormalizeValue

ArgumentTypeRequiredDescription
valueNumberYes
rangeRangeYes

clampValue

Clamp some value against a range.

ArgumentTypeRequiredDescription
valueNumberYesThe value to clamp.
minValueNumberYesThe minimum value.
maxValueNumberYesThe maximum value.

clampVector

Clamp some vector against a range.

ArgumentTypeRequiredDescription
vectorVector3YesThe vector to clamp.
minVectorVector3YesThe minimum vector.
maxVectorVector3YesThe maximum vector.
outVector3YesThe output vector.

columnsToMat3

Fill a 3x3 matrix with the given column vectors

ArgumentTypeRequiredDescription
column0Vector3Yes
column1Vector3Yes
column2Vector3Yes
matMatrixYes

columnsToMat4

Fill a 4x4 matrix with the given column vectors

ArgumentTypeRequiredDescription
column0Vector4Yes
column1Vector4Yes
column2Vector4Yes
column3Vector4Yes
matMatrixYes

createArray

ArgumentTypeRequiredDescription
sizeNumberYesThe size of the array.

createUninitializedBounds

cross

Computes cross product of 3D vectors x and y.

ArgumentTypeRequiredDescription
xVector3YesThe first 3D vector.
yVector3YesThe second 3D vector.
outVector3YesThe output 3D vector.

degreesFromRadians

Convert radians to degrees.

ArgumentTypeRequiredDescription
radNumberYesThe value in radians.

determinant2x2

ArgumentTypeRequiredDescription
argsArray[Number]Yes

determinant3x3

Calculate the determinant of a 3x3 matrix.

ArgumentTypeRequiredDescription
mat_3x3Matrix3x3YesThe input 3x3 matrix.

diagonalize3x3

ArgumentTypeRequiredDescription
a_3x3Matrix3x3Yes
w_3Vector3Yes
v_3x3Matrix3x3Yes

distance2BetweenPoints

Compute distance squared between two points p1 and p2.

ArgumentTypeRequiredDescription
xVector3YesThe first 3D vector.
yVector3YesThe second 3D vector.

dot

Dot product of two 3-vectors.

ArgumentTypeRequiredDescription
xVector3Yes
yVector3Yes

dot2D

Compute the dot product of two 2D vectors.

ArgumentTypeRequiredDescription
xVector2Yes
yVector2Yes

estimateMatrixCondition

ArgumentTypeRequiredDescription
AMatrixYes
sizeNumberYes

extentIsWithinOtherExtent

Check if first 3D extent is within second 3D extent.

ArgumentTypeRequiredDescription
extent1ExtentYesThe first extent.
extent2ExtentYesThe second extent.

factorial

Compute N factorial, N! = N*(N-1) * (N-2)...32*1.

float2CssRGBA

Convert RGB or RGBA color array to CSS representation

ArgumentTypeRequiredDescription
rgbArrayRGBColor or RGBAColorYesThe color array.

floatRGB2HexCode

ArgumentTypeRequiredDescription
rgbArrayRGBColorYes
prefixstringNo

floatToHex2

ArgumentTypeRequiredDescription
valueNumberYesThe value to convert.

floor

Same as Math.floor().

ArgumentTypeRequiredDescription
param1NumberYesA numeric expression.

gaussian

Generate pseudo-random numbers distributed according to the standard normal distribution.

gaussianAmplitude

Compute the amplitude of a Gaussian function with mean=0 and specified variance.

ArgumentTypeRequiredDescription
meanNumberYesThe mean value.
varianceNumberYesThe variance value.
positionNumberYesThe position value.

gaussianWeight

Compute the amplitude of an unnormalized Gaussian function with mean=0 and specified variance.

ArgumentTypeRequiredDescription
meanNumberYesThe mean value.
varianceNumberYesThe variance value.
positionNumberYesThe position value.

getAdjustedScalarRange

getMajorAxisIndex

ArgumentTypeRequiredDescription
vectorArray[Number]Yes

getScalarTypeFittingRange

Get the scalar type that is most likely to have enough precision to store a given range of data once it has been scaled and shifted

getSeed

Return the current seed used by the random number generator.

getSparseOrthogonalMatrix

Return the closest orthogonal matrix of 1, -1 and 0 It works for both column major and row major matrices This function iteratively associate a column with a row by choosing the greatest absolute value from the remaining row and columns For each association, a -1 or a 1 is set in the output, depending on the sign of the value in the original matrix

ArgumentTypeRequiredDescription
matrixArray[Number]YesThe matrix of size nxn
nArray[Number]YesThe size of the square matrix, defaults to 3

hex2float

ArgumentTypeRequiredDescription
hexStrStringYes
outFloatArrayArray[Number]No

hsv2rgb

ArgumentTypeRequiredDescription
hsvHSVColorYesAn Array of the HSV color.
rgbRGBColorYesAn Array of the RGB color.

identity

Set mat to the identity matrix.

ArgumentTypeRequiredDescription
nNumberYesThe size of the matrix.
matArray[Number]YesThe output matrix.

identity3x3

Set mat_3x3 to the identity matrix.

ArgumentTypeRequiredDescription
mat_3x3Matrix3x3YesThe input 3x3 matrix.

invert3x3

Invert a 3x3 matrix.

ArgumentTypeRequiredDescription
in_3x3Matrix3x3YesThe input 3x3 matrix.
outI_3x3Matrix3x3YesThe output 3x3 matrix.

invertMatrix

ArgumentTypeRequiredDescription
AMatrixYesThe input matrix. It is modified during the inversion.
AIMatrixYesThe output inverse matrix. Can be the same as input matrix.
sizeNumberNoThe square size of the matrix to invert : 4 for a 4x4
indexArray[Number]No
columnArray[Number]No

Returns

TypeDescription
AI on success, null otherwise

isFinite

Determines whether the passed value is a finite number.

ArgumentTypeRequiredDescription
valueYesThe value to check.

isIdentity

Returns true if provided matrix is the identity matrix.

ArgumentTypeRequiredDescription
matArray[Number]YesThe 3x3 matrix to check
epsNumberNoThe tolerance value.

isIdentity3x3

Returns true if provided 3x3 matrix is the identity matrix.

ArgumentTypeRequiredDescription
matMatrix3x3YesThe 3x3 matrix to check
epsNumberNoThe tolerance value.

isInf

Determines whether the passed value is a infinite number.

ArgumentTypeRequiredDescription
valueNumberYesThe value to check.

isNaN

Determines whether the passed value is a NaN.

ArgumentTypeRequiredDescription
valueNumberYesThe value to check.

isNan

Determines whether the passed value is a NaN.

ArgumentTypeRequiredDescription
valueNumberYesThe value to check.

isPowerOfTwo

Returns true if integer is a power of two.

ArgumentTypeRequiredDescription
xNumberYesA numeric expression.

jacobi

ArgumentTypeRequiredDescription
a_3x3Matrix3x3Yes
wArray[Number]Yes
vArray[Number]Yes

jacobiN

Jacobi iteration for the solution of eigenvectors/eigenvalues. Input matrix a is modified (the upper triangle is filled with zeros)

ArgumentTypeRequiredDescription
aMatrixYesreal symetric nxn matrix
nNumberYesmatrix size
wArray[Number]Yesvector of size n to store eigenvalues (stored in decreasing order)
vArray[Number]Yesmatrix of size nxn to store eigenvectors (stored in decreasing order, normalized)

lab2rgb

ArgumentTypeRequiredDescription
labVector3Yes
rgbRGBColorYesAn Array of the RGB color.

lab2xyz

ArgumentTypeRequiredDescription
labVector3Yes
xyzVector3Yes

ldexp

Calculates x times (2 to the power of exponent).

linearSolve3x3

Solve mat_3x3y_3 = x_3 for y and place the result in y.

ArgumentTypeRequiredDescription
mat_3x3Matrix3x3Yes
x_3Vector3Yes
y_3Vector3Yes

luFactorLinearSystem

ArgumentTypeRequiredDescription
AMatrixYes
indexArray[Number]Yes
sizeNumberYes

luSolveLinearSystem

ArgumentTypeRequiredDescription
AMatrixYes
indexArray[Number]Yes
xArray[Number]Yes
sizeNumberYes

matrix3x3ToQuaternion

ArgumentTypeRequiredDescription
mat_3x3Matrix3x3Yes
quat_4Vector4Yes

max

Get the maximum of the two arguments provided. If either argument is NaN, the first argument will always be returned.

ArgumentTypeRequiredDescription
param1NumberYesThe first numeric expression.
param2NumberYesThe second numeric expression.

min

Get the minimum of the two arguments provided. If either argument is NaN, the first argument will always be returned.

ArgumentTypeRequiredDescription
param1NumberYesThe first numeric expression.
param2NumberYesThe second numeric expression.

multiply3x3_mat3

ArgumentTypeRequiredDescription
a_3x3Matrix3x3Yes
b_3x3Matrix3x3Yes
out_3x3Matrix3x3Yes

multiply3x3_vect3

ArgumentTypeRequiredDescription
mat_3x3Matrix3x3Yes
in_3Vector3Yes
out_3Vector3Yes

multiplyAccumulate

Multiply two 3-vectors.

ArgumentTypeRequiredDescription
aVector3Yes
bVector3Yes
scalarNumberYes
outVector3Yes

multiplyAccumulate2D

Multiply two 2-vectors.

ArgumentTypeRequiredDescription
aVector2Yes
bVector2Yes
scalarNumberYes
outVector2Yes

multiplyMatrix

Multiply two matrices.

ArgumentTypeRequiredDescription
aMatrixYes
bMatrixYes
rowANumberYes
colANumberYes
rowBNumberYes
colBNumberYes
outRowAColBMatrixYes

multiplyQuaternion

ArgumentTypeRequiredDescription
quat_1Vector4Yes
quat_2Vector4Yes
quat_outVector4Yes

multiplyScalar

Multiply a 3-vector by a scalar.

ArgumentTypeRequiredDescription
vecVector3Yes
scalarNumberYes

multiplyScalar2D

Multiply a 3-vector by a scalar.

ArgumentTypeRequiredDescription
vecVector2Yes
scalarNumberYes

nearestPowerOfTwo

Compute the nearest power of two that is not less than x.

ArgumentTypeRequiredDescription
xiNumberYesA numeric expression.

nextCombination

Given m, n, and a valid combination of n integers in the range [0,m[, this function alters the integers into the next combination in a sequence of all combinations of n items from a pool of m.

ArgumentTypeRequiredDescription
mNumberYesThe first numeric expression.
nNumberYesThe second numeric expression.
rArray[Number]Yes

norm

Compute the norm of a vector.

ArgumentTypeRequiredDescription
xArray[Number]YesThe vector to compute the norm of.
nNumberNoThe number of components to consider (default is 3).

norm2D

Compute the norm of a 2-vector.

ArgumentTypeRequiredDescription
x2DVector2Yesx The 2D vector.

normalize

Normalize in place. Returns norm.

ArgumentTypeRequiredDescription
xArray[Number]YesThe vector to normalize.

normalize2D

Normalize (in place) a 2-vector.

ArgumentTypeRequiredDescription
xVector2YesThe 2D vector.

normalize4D

Normalize in place. Returns norm.

ArgumentTypeRequiredDescription
xArray[Number]YesThe vector to normalize.

orthogonalize3x3

ArgumentTypeRequiredDescription
a_3x3Matrix3x3Yes
out_3x3Matrix3x3Yes

outer

Outer product of two 3-vectors.

ArgumentTypeRequiredDescription
xVector3YesThe first 3D vector.
yVector3YesThe second 3D vector.
out_3x3Matrix3x3YesThe output 3x3 matrix.

outer2D

Outer product of two 2-vectors.

ArgumentTypeRequiredDescription
xVector2YesThe first 2D vector.
yVector2YesThe second 2D vector.
out_2x2MatrixYesThe output 2x2 matrix.

perpendiculars

Given a unit vector v1, find two unit vectors v2 and v3 such that v1 cross v2 = v3

ArgumentTypeRequiredDescription
xVector3YesThe first vector.
yVector3YesThe second vector.
zVector3YesThe third vector.
thetaNumberYes

pointIsWithinBounds

Check if point is within the given 3D bounds.

ArgumentTypeRequiredDescription
point_3Vector3YesThe coordinate of the point.
bounds_6BoundsYesThe bounds.
delta_3Vector3YesThe error margin along each axis.

projectVector

Project vector a onto vector b and returns the result in projection vector.

ArgumentTypeRequiredDescription
aVector3YesThe first 3D vector.
bVector3YesThe second 3D vector.
projectionVector3YesThe projection 3D vector.

projectVector2D

Compute the projection of 2D vector a on 2D vector b and returns the result in projection vector.

ArgumentTypeRequiredDescription
aVector2YesThe first 2D vector.
bVector2YesThe second 2D vector.
projectionVector2YesThe projection 2D vector.

quaternionToMatrix3x3

ArgumentTypeRequiredDescription
quat_4Vector4Yes
mat_3x3Matrix3x3Yes

radiansFromDegrees

Convert degrees to radians.

ArgumentTypeRequiredDescription
degNumberYesThe value in degrees.

random

Generate pseudo-random numbers distributed according to the uniform distribution between min and max.

ArgumentTypeRequiredDescription
minValueNumberYes
maxValueNumberYes

randomSeed

Initialize seed value.

ArgumentTypeRequiredDescription
seedNumberYes

rgb2hsv

ArgumentTypeRequiredDescription
rgbRGBColorYesAn Array of the RGB color.
hsvHSVColorYesAn Array of the HSV color.

rgb2lab

ArgumentTypeRequiredDescription
rgbRGBColorYes
labVector3Yes

rgb2xyz

ArgumentTypeRequiredDescription
rgbRGBColorYesAn Array of the RGB color.
xyzVector3Yes

round

Same as Math.round().

ArgumentTypeRequiredDescription
param1NumberYesThe value to be rounded to the nearest integer.

roundNumber

ArgumentTypeRequiredDescription
numNumberYes
digitsNumberNo

roundVector

ArgumentTypeRequiredDescription
vectorVector3Yes
outVector3Yes

roundVector

ArgumentTypeRequiredDescription
vectorVector3Yes
outVector3No
digitsNumberNo

rowsToMat3

Fill a 3x3 matrix with the given row vectors

ArgumentTypeRequiredDescription
row0Vector3Yes
row1Vector3Yes
row2Vector3Yes
matMatrixYes

rowsToMat4

Fill a 4x4 matrix with the given row vectors

ArgumentTypeRequiredDescription
row0Vector4Yes
row1Vector4Yes
row2Vector4Yes
row3Vector4Yes
matMatrixYes

signedAngleBetweenVectors

Signed angle between v1 and v2 with regards to plane defined by normal vN. angle between v1 and v2 with regards to plane defined by normal vN.Signed angle between v1 and v2 with regards to plane defined by normal vN.t3(mat_3x3, in_3, out_3)

ArgumentTypeRequiredDescription
v1Vector3YesThe first 3D vector.
v2Vector3YesThe second 3D vector.
vNVector3Yes

singularValueDecomposition3x3

ArgumentTypeRequiredDescription
a_3x3Matrix3x3Yes
u_3x3Matrix3x3Yes
w_3Vector3Yes
vT_3x3Matrix3x3Yes

solve3PointCircle

In Euclidean space, there is a unique circle passing through any given three non-collinear points P1, P2, and P3.

Using Cartesian coordinates to represent these points as spatial vectors, it is possible to use the dot product and cross product to calculate the radius and center of the circle. See: http://en.wikipedia.org/wiki/Circumscribed_circle and more specifically the section Barycentric coordinates from cross- and dot-products

ArgumentTypeRequiredDescription
p1Vector3YesThe coordinate of the first point.
p2Vector3YesThe coordinate of the second point.
p3Vector3YesThe coordinate of the third point.
centerVector3YesThe coordinate of the center point.

solveHomogeneousLeastSquares

Solves for the least squares best fit matrix for the homogeneous equation X'M' = 0'. Uses the method described on pages 40-41 of Computer Vision by Forsyth and Ponce, which is that the solution is the eigenvector associated with the minimum eigenvalue of T(X)X, where T(X) is the transpose of X. The inputs and output are transposed matrices. Dimensions: X' is numberOfSamples by xOrder, M' dimension is xOrder by yOrder. M' should be pre-allocated. All matrices are row major. The resultant matrix M' should be pre-multiplied to X' to get 0', or transposed and then post multiplied to X to get 0

ArgumentTypeRequiredDescription
numberOfSamplesNumberYes
xtMatrixYes
xOrderNumberYes
mtMatrixYes

solveLeastSquares

Solves for the least squares best fit matrix for the equation X'M' = Y'. Uses pseudoinverse to get the ordinary least squares. The inputs and output are transposed matrices. Dimensions: X' is numberOfSamples by xOrder, Y' is numberOfSamples by yOrder, M' dimension is xOrder by yOrder. M' should be pre-allocated. All matrices are row major. The resultant matrix M' should be pre-multiplied to X' to get Y', or transposed and then post multiplied to X to get Y By default, this method checks for the homogeneous condition where Y==0, and if so, invokes SolveHomogeneousLeastSquares. For better performance when the system is known not to be homogeneous, invoke with checkHomogeneous=0.

ArgumentTypeRequiredDescription
numberOfSamplesNumberYes
xtMatrixYes
xOrderNumberYes
ytMatrixYes
yOrderNumberYes
mtMatrixYes
checkHomogeneousBooleanNo

solveLinearSystem

ArgumentTypeRequiredDescription
AMatrixYes
xArray[Number]Yes
sizeNumberYes

subtract

Substract two 3-vectors.

ArgumentTypeRequiredDescription
aVector3YesThe first 3D vector.
bVector3YesThe second 3D vector.
outVector3YesThe output 3D vector.

swapColumnsMatrix_nxn

Given two columns indices, swap the two columns of a nxn matrix

ArgumentTypeRequiredDescription
matrixArray[Number]YesThe n by n matrix in wich we want to swap the vectors.
nNumberYessize of the matrix.
column1NumberYesindex of first col to swap with the other.
column2NumberYesindex of second col to swap with the other.

swapRowsMatrix_nxn

Given two rows indices, swap the two rows of a nxn matrix

ArgumentTypeRequiredDescription
matrixArray[Number]YesThe n by n matrix in wich we want to swap the vectors.
nNumberYessize of the matrix.
row1NumberYesindex of first row to swap with the other.
row2NumberYesindex of second row to swap with the other.

transpose3x3

Transpose a 3x3 matrix.

ArgumentTypeRequiredDescription
in_3x3Matrix3x3YesThe input 3x3 matrix.
outT_3x3Matrix3x3YesThe output 3x3 matrix.

uninitializeBounds

Returns bounds.

ArgumentTypeRequiredDescription
boundsBoundsYesOutput array that hold bounds, optionally empty.

xyz2lab

ArgumentTypeRequiredDescription
xyzVector3Yes
labVector3Yes

xyz2rgb

ArgumentTypeRequiredDescription
xyzVector3Yes
rgbRGBColorYesAn Array of the RGB color.