Skip to content

Vector API

Markdown

The Vector API provides structures representing commonly-used vector types as well as routines implementing various mathematical operations on them. Every operation exists per dimension (shz_vec2_*, shz_vec3_*, shz_vec4_*), and C code can call the type-generic shz_vec_* macros, which forward to the proper routine based on the argument type. Dot products and magnitudes are routed through the SH4 FIPR instruction, and normalization uses FSRRA. In C++, shz::vec2, shz::vec3 and shz::vec4 derive from the C structs and expose the same routines as methods and operators.

Suffixes used in this module. The full list is in Naming and Suffixes.

  • _safe: Guards degenerate input (zero-length vectors, zero denominators) at a small cost.
  • _fsrra: Uses FSRRA (fast approximate 1/sqrt(x)). The argument or denominator must be positive. Faster, less precise.
  • _deg: Angle argument is in degrees. The default is radians.
  • dot2/dot3: Computes 2 or 3 dot products of one left-hand side against several right-hand sides in one pipelined FIPR sequence.

Macros for converting between SH4ZAM and other compatible formats.

SymbolDescription
shz_vec2_derefDereferences the given pointer to a sequence of 2 floats as a shz_vec2_t.
shz_vec3_derefDereferences the given pointer to a sequence of 3 floats as a shz_vec3_t.
shz_vec4_derefDereferences the given pointer to a sequence of 4 floats as a shz_vec4_t.
shz_vec2_fromConverts the given value or expression to the equivalent 2D SH4ZAM vector value.
shz_vec3_fromConverts the given value or expression to the equivalent 3D SH4ZAM vector value.
shz_vec4_fromConverts the given value or expression to the equivalent 4D SH4ZAM vector value.
shz_vec2_toConverts the given 2D vector into a value of the given type.
shz_vec3_toConverts the given 3D vector into a value of the given type.
shz_vec4_toConverts the given 4D vector into a value of the given type.

Generalized vector routines for C and C++.

SymbolDescription
shz_vec_absC type-generic vector absolute value.
shz_vec_negC type-generic vector negation.
shz_vec_invC type-generic vector inversion.
shz_vec_maxC type-generic vector maximum value.
shz_vec_minC type-generic vector minimum value.
shz_vec_clampC type-generic vector minimum value.
shz_vec_equalC type-generic vector equals.
shz_vec_addC type-generic vector addition.
shz_vec_subC type-generic vector subtraction.
shz_vec_mulC type-generic vector multiplication.
shz_vec_divC type-generic vector division.
shz_vec_scaleC type-generic vector scaling.
shz_vec_dotC type-generic vector dot product.
shz_vec_dot2C type-generic vector chained double dot product.
shz_vec_dot3C type-generic vector chained triple dot product.
shz_vec_magnitude_sqrC type-generic vector squared magnitude.
shz_vec_magnitudeC type-generic vector magnitude.
shz_vec_magnitude_invC type-generic vector inverse magnitude.
shz_vec_normalizeC type-generic vector normalization.
shz_vec_normalize_safeC type-generic safe vector normalization.
shz_vec_distanceC type-generic vector distance.
shz_vec_moveC type-generic vector move.
shz_vec_distance_sqrC type-generic vector squared distance.
shz_vec_lerpC type-generic linear interpolation between two vectors.
shz_vec_reflectC type-generic vector reflection.
shz_vec_refractC type-generic vector refraction.
shz_vec_crossC type-generic vector cross-product.
shz_vec_projectC type-generic vector projection.
shz_vec_project_safeC type-generic safe vector projection.
shz_vec_anglesC type-generic angle-from-vector extraction.
shz_vec_angle_betweenC type-generic angle between two vectors.
shz_vec_swizzleC type-generic vector swizzling.
shz_vec_floorC type-generic component-wise floor.
shz_vec_ceilC type-generic component-wise ceil.
shz_vec_roundC type-generic component-wise round.
shz_vec_fractC type-generic component-wise fract.
shz_vec_signC type-generic component-wise sign.
shz_vec_saturateC type-generic component-wise saturate.
shz_vec_minvC type-generic pairwise minimum.
shz_vec_maxvC type-generic pairwise maximum.
shz_vec_inv_sqrtfC type-generic inv_sqrtf.
shz_vec_sqrtfC type-generic sqrtf.
shz_vec_stepvC type-generic component-wise step.
shz_vec_stepC type-generic step.
shz_vec_smoothstepvC type-generic component-wise smoothstep.
shz_vec_smoothstepC type-generic smoothstep.
shz_vec_smoothstepv_safeC type-generic component-wise smoothstep_safe.
shz_vec_smoothstep_safeC type-generic smoothstep_safe.

Component-based initialization routines.

SymbolDescription
shz_vec2_initReturns a 2D vector with the given x, and y coordinates.
shz_vec3_initReturns a 3D vector with the given x, y, and z coordinates.
shz_vec4_initReturns a 4D vector with the given x, y, z, and w coordinates.
shz_vec2_fillReturns a 2D vector with the value of each component equal to v.
shz_vec3_fillReturns a 3D vector with the value of each compoonent equal to v.
shz_vec4_fillReturns a 4D vector with the value of each component equal to v.

Routines which apply to each vector component.

SymbolDescription
shz_vec2_absReturns a 2D vector whose components are the absolute values of the given vector's components.
shz_vec3_absReturns a 3D vector whose components are the absolute values of the given vector's components.
shz_vec4_absReturns a 4D vector whose components are the absolute values of the given vector's components.
shz_vec2_negReturns a 2D vector whose components are the negative values of the given vector's components.
shz_vec3_negReturns a 3D vector whose components are the negative values of the given vector's components.
shz_vec4_negReturns a 4D vector whose components are the negative values of the given vector's components.
shz_vec2_invReturns the 2D vector whose components have been inverted or reciprocated.
shz_vec3_invReturns the 3D vector whose components have been inverted or reciprocated.
shz_vec4_invReturns the 4D vector whose components have been inverted or reciprocated.
shz_vec2_maxReturns the maximum value of both of the given vector's components.
shz_vec3_maxReturns the maximum value of the given vector's 3 components.
shz_vec4_maxReturns the maximum value of the given vector's 4 componetns.
shz_vec2_minRetuns the minimum value of both of the given vector's components.
shz_vec3_minReturns the minimum value of the given vector's 3 components.
shz_vec4_minReturns the minimum value of the given vector's 4 components.
shz_vec2_clampClamps the values of the given 2D vec between min and max, returning a new vector.
shz_vec3_clampClamps the values of the given 3D vec between min and max, returning a new vector.
shz_vec4_clampClamps the values of the given 4D vec between min and max, returning a new vector.
shz_vec2_floorReturns a 2D vector whose components are the floor of the given vector's components.
shz_vec3_floorReturns a 3D vector whose components are the floor of the given vector's components.
shz_vec4_floorReturns a 4D vector whose components are the floor of the given vector's components.
shz_vec2_ceilReturns a 2D vector whose components are the ceil of the given vector's components.
shz_vec3_ceilReturns a 3D vector whose components are the ceil of the given vector's components.
shz_vec4_ceilReturns a 4D vector whose components are the ceil of the given vector's components.
shz_vec2_roundReturns a 2D vector whose components are the rounded values of the given vector's components.
shz_vec3_roundReturns a 3D vector whose components are the rounded values of the given vector's components.
shz_vec4_roundReturns a 4D vector whose components are the rounded values of the given vector's components.
shz_vec2_fractReturns a 2D vector whose components are the fractional parts of the given vector's components.
shz_vec3_fractReturns a 3D vector whose components are the fractional parts of the given vector's components.
shz_vec4_fractReturns a 4D vector whose components are the fractional parts of the given vector's components.
shz_vec2_signReturns a 2D vector whose components are the signs (-1, 0, or 1) of the given vector's components.
shz_vec3_signReturns a 3D vector whose components are the signs (-1, 0, or 1) of the given vector's components.
shz_vec4_signReturns a 4D vector whose components are the signs (-1, 0, or 1) of the given vector's components.
shz_vec2_saturateReturns a 2D vector whose components are saturated (clamped to [0, 1]) values of the given vector's components.
shz_vec3_saturateReturns a 3D vector whose components are saturated (clamped to [0, 1]) values of the given vector's components.
shz_vec4_saturateReturns a 4D vector whose components are saturated (clamped to [0, 1]) values of the given vector's components.
shz_vec2_minvReturns a 2D vector whose components are the pairwise minimums of the two given vectors' components.
shz_vec3_minvReturns a 3D vector whose components are the pairwise minimums of the two given vectors' components.
shz_vec4_minvReturns a 4D vector whose components are the pairwise minimums of the two given vectors' components.
shz_vec2_maxvReturns a 2D vector whose components are the pairwise maximums of the two given vectors' components.
shz_vec3_maxvReturns a 3D vector whose components are the pairwise maximums of the two given vectors' components.
shz_vec4_maxvReturns a 4D vector whose components are the pairwise maximums of the two given vectors' components.
shz_vec2_equalReturns true if the values of each element within the two 2D vectors are approximately equal based on relative or absolute tolerance.
shz_vec3_equalReturns true if the values of each element within the two 3D vectors are approximately equal based on relative or absolute tolerance.
shz_vec4_equalReturns true if the values of each element within the two 4D vectors are approximately equal based on relative or absolute tolerance.
shz_vec2_stepvFor each component: returns 0.0f if vec[i] < edge[i], otherwise 1.0f.
shz_vec3_stepvFor each component: returns 0.0f if vec[i] < edge[i], otherwise 1.0f.
shz_vec4_stepvFor each component: returns 0.0f if vec[i] < edge[i], otherwise 1.0f.
shz_vec2_stepFor each component: returns 0.0f if vec[i] < edge, otherwise 1.0f.
shz_vec3_stepFor each component: returns 0.0f if vec[i] < edge, otherwise 1.0f.
shz_vec4_stepFor each component: returns 0.0f if vec[i] < edge, otherwise 1.0f.
shz_vec2_smoothstepFor each component: returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between.
shz_vec3_smoothstepFor each component: returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between.
shz_vec4_smoothstepFor each component: returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between.
shz_vec2_smoothstepvFor each component: returns 0.0f at/below edge0[i], 1.0f at/above edge1[i], smoothly varying in-between.
shz_vec3_smoothstepvFor each component i: returns 0.0f at/below edge0[i], 1.0f at/above edge1[i], smoothly varying in-between.
shz_vec4_smoothstepvFor each component i: returns 0.0f at/below edge0[i], 1.0f at/above edge1[i], smoothly varying in-between.
shz_vec2_smoothstep_safeFor each component: returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. Accepts inverse edges.
shz_vec3_smoothstep_safeFor each component: returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. Accepts inverse edges.
shz_vec4_smoothstep_safeFor each component: returns 0.0f at/below edge0, 1.0f at/above edge1, smoothly varying in-between. Accepts inverse edges.
shz_vec2_smoothstepv_safeFor each component: returns 0.0f at/below edge0[i], 1.0f at/above edge1[i], smoothly varying in-between. Accepts inverse edges.
shz_vec3_smoothstepv_safeFor each component i: returns 0.0f at/below edge0[i], 1.0f at/above edge1[i], smoothly varying in-between. Accepts inverse edges.
shz_vec4_smoothstepv_safeFor each component i: returns 0.0f at/below edge0[i], 1.0f at/above edge1[i], smoothly varying in-between. Accepts inverse edges.
shz_vec2_inv_sqrtfReturns a 2D vector whose components are the inverse square root of the given vector's components.
shz_vec3_inv_sqrtfReturns a 3D vector whose components are the inverse square root of the given vector's components.
shz_vec4_inv_sqrtfReturns a 4D vector whose components are the inverse square root of the given vector's components.
shz_vec2_sqrtfReturns a 2D vector whose components are the square root of the given vector's components.
shz_vec3_sqrtfReturns a 3D vector whose components are the square root of the given vector's components.
shz_vec4_sqrtfReturns a 4D vector whose components are the square root of the given vector's components.

Routines for basic vector arithmetic operations.

SymbolDescription
shz_vec2_addReturns a 2D vector whose components are the sums of the given vectors' components.
shz_vec3_addReturns a 3D vector whose components are the sums of the given vectors' components.
shz_vec4_addReturns a 4D vector whose components are the sums of the given vectors' components.
shz_vec2_subReturns a 2D vector whose components are equal to the values of vec1 minus vec2.
shz_vec3_subReturns a 3D vector whose components are equal to the values of vec1 minus vec2.
shz_vec4_subReturns a 4D vector whose components are equal to the values of vec1 minus vec2.
shz_vec2_mulReturns a 2D vector whose component values are those of vec1 times vec2.
shz_vec3_mulReturns a 3D vector whose component values are those of vec1 times vec2.
shz_vec4_mulReturns a 4D vector whose component values are those of vec1 times vec2.
shz_vec2_divReturns a 2D vector whose component values are those of vec1 divided by vec2.
shz_vec3_divReturns a 3D vector whose component values are those of vec1 divided by vec2.
shz_vec4_divReturns a 4D vector whose component values are those of vec1 divided by vec2.
shz_vec2_scaleReturns a 2D vector whose component values are those of the given vector multiplied by a factor.
shz_vec3_scaleReturns a 3D vector whose component values are those of the given vector multiplied by a factor.
shz_vec4_scaleReturns a 4D vector whose component values are those of the given vector multiplied by a factor.

Math routines for vector length and normalization.

SymbolDescription
shz_vec2_magnitude_sqrReturns the squared magnitude of the given 2D vector.
shz_vec4_magnitude_sqrReturns the squared magnitude of the given 4D vector.
shz_vec3_magnitude_sqrReturns the squared magnitude of the given 3D vector.
shz_vec2_magnitudeReturns the magnitude of the given 2D vector.
shz_vec3_magnitudeReturns the magnitude of the given 3D vector.
shz_vec4_magnitudeReturns the magnitude of the given 4D vector.
shz_vec2_magnitude_invReturns the inverse magnitude of the given 2D vector.
shz_vec3_magnitude_invReturns the inverse magnitude of the given 3D vector.
shz_vec4_magnitude_invReturns the inverse magnitude of the given 4D vector.
shz_vec2_normalizeReturns a normalized unit vector from the given 2D vector.
shz_vec3_normalizeReturns a normalized unit vector from the given 3D vector.
shz_vec4_normalizeReturns a normalized unit vector from the given 4D vector.
shz_vec2_normalize_safeSAFELY returns a normalized unit vector from the given 2D vector.
shz_vec3_normalize_safeSAFELY returns a normalized unit vector from the given 3D vector.
shz_vec4_normalize_safeSAFELY returns a normalized unit vector from the given 4D vector.

Linear algebra operations performed with multiple vectors.

SymbolDescription
shz_vec2_dotReturns the dot product between the two given 2D vectors.
shz_vec2_dot2Returns the two dot products taken between the 2D vector l and 2D vectors r1 and r2.
shz_vec2_dot3Returns the three dot products taken between the 2D vector l and 2D vectors r1, r2, and r3.
shz_vec3_dotReturns the dot product between the two given 3D vectors.
shz_vec3_dot2Returns the two dot products taken between the 3D vector l and 3D vectors r1 and r2.
shz_vec3_dot3Returns the three dot products taken between the 3D vector l and 3D vectors r1, r2, and r3.
shz_vec4_dotReturns the dot product between the two given 4D vectors.
shz_vec4_dot2Returns the two dot products taken between the 4D vector l and 4D vectors r1 and r2.
shz_vec4_dot3Returns the three dot products taken between the 4D vector l and 4D vectors r1, r2, and r3.
shz_vec2_distanceReturns the distance between the two given 2D vectors.
shz_vec3_distanceReturns the distance between the two given 3D vectors.
shz_vec4_distanceReturns the distance between the two given 4D vectors.
shz_vec2_distance_sqrReturns the squared-distance between the two given 2D vectors.
shz_vec3_distance_sqrReturns the squared-distance between the two given 3D vectors.
shz_vec4_distance_sqrReturns the squared-distance between the two given 4D vectors.
shz_vec2_moveReturns the given 2D vector, translated towards the target by the given max_distance.
shz_vec3_moveReturns the given 3D vector, translated towards the target by the given max_distance.
shz_vec4_moveReturns the given 4D vector, translated towards the target by the given max_distance.
shz_vec2_lerpReturns a 2D vector that is linearly interpolated from a to b by the given 0.0f-1.0f factor, t.
shz_vec3_lerpReturns a 3D vector that is linearly interpolated from a to b by the given 0.0f-1.0f factor, t.
shz_vec4_lerpReturns a 4D vector that is linearly interpolated from a to b by the given 0.0f-1.0f factor, t.
shz_vec2_reflectReflects the given 2D incidence vector against a surface with the given normal, returning the result.
shz_vec3_reflectReflects the given 3D incidence vector against a surface with the given normal, returning the result.
shz_vec4_reflectReflects the given 4D incidence vector against a surface with the given normal, returning the result.
shz_vec2_refractRefracts the given 2D incidence vector against a surface with the given normal using the given refraction index ratio, eta.
shz_vec3_refractRefracts the given 3D incidence vector against a surface with the given normal using the given refraction index ratio, eta.
shz_vec4_refractRefracts the given 4D incidence vector against a surface with the given normal using the given refraction index ratio, eta.
shz_vec2_crossReturns the cross product, as a scalar, between two 2D vectors.
shz_vec3_crossReturns the vector produced by taking the cross-product of the two given 3D vectors.
shz_vec2_projectReturns the resulting vector from projecting the given 2D vector along the given (unit) axis.
shz_vec3_projectReturns the resulting vector from projecting the given 3D vector along the given (unit) axis.
shz_vec4_projectReturns the resulting vector from projecting the given 4D vector along the given (unit) axis.
shz_vec2_project_safeReturns the resulting vector from projecting the given 2D vector along the given (unit) axis.
shz_vec3_project_safeReturns the resulting vector from projecting the given 3D vector along the given (unit) axis.
shz_vec4_project_safeReturns the resulting vector from projecting the given 4D vector along the given (unit) axis.
shz_vec3_rejectReturns the rejection of the given vector, vec, onto another vector, onto.

Other specialized vector routines.

SymbolDescription
shz_vec3_tripleReturns the 3D vector "triple product" between vector's a, b, and c.
shz_vec3_perpReturns a vector which is perpendicular to the given vector.
shz_vec3_barycenterComputes barycentric coordinates <u, v, w> for point p, within the plane of the triangle with vertices a, b, and c.
shz_vec3_orthonormalizeReturns 2 3D vectors which are normalized and orthogonal to the two input vectors.
shz_vec3_cubic_hermiteCalculates the cubic hermite interpolation between two vectors and their tangents.

Routines for working with vectors and angles.

SymbolDescription
shz_vec2_angle_betweenReturns the angle formed between the given 2D vectors in radians.
shz_vec3_angle_betweenReturns the angle formed between the given 3D vectors in radians.
shz_vec2_angleReturns the angle formed between the positive X axis and the given 2D vector, in radians.
shz_vec3_anglesReturns the angles formed between the positive X axis and the given 3D vector, in radians.
shz_vec2_from_sincosReturns the 2D unit vector representing a rotation from the positive X axis.
shz_vec3_from_sincosReturns the 3D unit vector representing the given rotation angles relative to the positive X axis.
shz_vec2_from_angleReturns the 2D unit vector representing a rotation from the positive X axis in radians.
shz_vec3_from_anglesReturns the 3D unit vector representing the given rotation angles relative to the positive X axis in radians.
shz_vec2_from_angle_degReturns the 2D unit vector representing a rotation from the positive X axis in degrees.
shz_vec3_from_angles_degReturns the 3D unit vector representing the given rotation angles relative to the positive X axis in degrees.
shz_vec2_rotateRotates the given 2D vector about the Z axis by the given angle in radians.
shz_vec3_rotate_xRotates the given 3D vector about the X axis by the given angle in radians.
shz_vec3_rotate_yRotates the given 3D vector about the Y axis by the given angle in radians.
shz_vec3_rotate_zRotates the given 3D vector about the Z axis by the given angle in radians.

Routines for extending vectors into other dimensions.

SymbolDescription
shz_vec2_vec3Extends a 2D vector to 3D, using z as the value of the Z component.
shz_vec2_vec4Extends a 2D vector to 4D, using z and w as the values of the Z and W components.
shz_vec3_vec4Extends a 3D vector to 4D, using w as the value of the W component.

Routines for swizzling the order of a vector's components.

SymbolDescription
shz_vec2_swizzleReturns a 2D vector whose elements are equal to the source vector's values at the given indices.
shz_vec3_swizzleReturns a 3D vector whose elements are equal to the source vector's values at the given indices.
shz_vec4_swizzleReturns a new 2D vector whose elements are equal to the source vector's values at the given indices.
SymbolDescription
shz_vec2_t2D Vector type
shz_vec2Alternate typedef for the shz_vec2 struct for those who hate POSIX-style.
shz_vec3_t3D Vector type
shz_vec3Alternate typedef for the shz_vec3 struct for those who hate POSIX-style.
shz_vec4_t4D Vector type
shz_vec4Alternate typedef for the shz_vec4 struct for those who hate POSIX-style.