3D vectors libraryVector3D.h
3D vectors library provide primitives for math calculations with three dimensional vectors. It implements basic functions of linear algebra in vector space and can be used in analytic geometry, engineering, physics, natural sciences, computer science, and the social sciences (particularly in economics). The library provides structures/classes with public components which represent projections of vector onto common axes: X, Y and Z. You may set these components directly or change them via the library functions.
Contents
- 3D vector structure
- Arithmetic operations
- Unary operations
- Normalization (direction cosines)
- Reflection of vector
- Reflection through the origin
- Reflection through the X axis
- Reflection through the Y axis
- Reflection through the Z axis
- Reflection through the YZ plane
- Reflection through the XZ plane
- Reflection through the XY plane
- Binary operations
- Addition of vectors
- Subtraction of vectors
- Multiplication by scalar value
- Division by scalar value
- Rotation of vector
- Rotation around the X axis
- Rotation around the Y axis
- Rotation around the Z axis
- Shearing of vector
- Shearing parallel to the YZ plane
- Shearing parallel to the XZ plane
- Shearing parallel to the XY plane
- Scaling of vector
- Products
- Vector product
- Scalar product
- Triple product
- Absolute value
- Cosine value of angle between the vectors
- Projection of the vector to another vector
- Checks
- Check for zero vector
- Check for equality of the vectors
- Check for negativity of the vectors
- Check for collinearity of the vectors
- Check for orthogonality of the vectors
- Check for coplanarity of the vectors
Function list
C function name | Functions | C++ function name | Functions |
---|---|---|---|
Abs | 2 functions | Abs | 2 functions |
Add | 2 functions | Add | 2 functions |
Cos | 2 functions | Cos | 2 functions |
Div | 2 functions | Div | 2 functions |
IsCollinear | 2 functions | IsCollinear | 2 functions |
IsCoplanar | 2 functions | IsCoplanar | 2 functions |
IsEqual | 2 functions | IsEqual | 2 functions |
IsNeg | 2 functions | IsNeg | 2 functions |
IsOrthogonal | 2 functions | IsOrthogonal | 2 functions |
IsZero | 2 functions | IsZero | 2 functions |
Mul | 2 functions | Mul | 2 functions |
Normalize | 2 functions | Normalize | 2 functions |
Projection | 2 functions | Projection | 2 functions |
ReflectOrigin | 2 functions | ReflectOrigin | 2 functions |
ReflectX | 2 functions | ReflectX | 2 functions |
ReflectXY | 2 functions | ReflectXY | 2 functions |
ReflectXZ | 2 functions | ReflectXZ | 2 functions |
ReflectY | 2 functions | ReflectY | 2 functions |
ReflectYZ | 2 functions | ReflectYZ | 2 functions |
ReflectZ | 2 functions | ReflectZ | 2 functions |
RotateX | 2 functions | RotateX | 2 functions |
RotateY | 2 functions | RotateY | 2 functions |
RotateZ | 2 functions | RotateZ | 2 functions |
ScalarProduct | 2 functions | ScalarProduct | 2 functions |
Scale | 2 functions | Scale | 2 functions |
ShearXY | 2 functions | ShearXY | 2 functions |
ShearXZ | 2 functions | ShearXZ | 2 functions |
ShearYZ | 2 functions | ShearYZ | 2 functions |
Sub | 2 functions | Sub | 2 functions |
TripleProduct | 2 functions | TripleProduct | 2 functions |
VectorProduct | 2 functions | VectorProduct | 2 functions |
C function name | Functions | C++ function name | Functions |
3D vector structure
Vector structure has three elements, which represent x, y and z components of vector. You may set these fields directly or change their values in your code. Vector structure is defined below.
C/C++Single precision 3D vector struct v3D32_t { flt32_t x; flt32_t y; flt32_t z; }; Double precision 3D vector struct v3D64_t { flt64_t x; flt64_t y; flt64_t z; };
Both 32-bit and 64-bit vector structures have following members:
- x - projection of vector onto X axis
- y - projection of vector onto Y axis
- z - projection of vector onto Z axis
Arithmetic operations
Algorithms of arithmetic operations with vectors implement standard math operations of vector algebra. They are divided onto two groups: unary and binary. Both operations change components of target vector and store the result into the same vector. I.e. they modify original vector (unary operations) and change target vector (binary operations).
Unary operations
Unary operations change vector components and store the result into the same vector. They implement basic manipulations with vectors like normalizations and reflections through the axes and planes.
Normalization (direction cosines)
Cvoid Vector3D_Normalize_flt32 (struct v3D32_t *vector); void Vector3D_Normalize_flt64 (struct v3D64_t *vector);
C++void Vector3D::Normalize (v3D32_t *vector); void Vector3D::Normalize (v3D64_t *vector);
Description: Normalize vector (find its direction cosines) by division the vector to its absolute value.
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection of vector
Reflection functions change sign of vector components to reflect vectors through origin, axes and planes.
Reflection through the origintop
Cvoid Vector3D_ReflectOrigin_flt32 (struct v3D32_t *vector); void Vector3D_ReflectOrigin_flt64 (struct v3D64_t *vector);
C++void Vector3D::ReflectOrigin (v3D32_t *vector); void Vector3D::ReflectOrigin (v3D64_t *vector);
Description: Reflect vector through the origin (change sign of X, Y and Z coordinates).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the X axistop
Cvoid Vector3D_ReflectX_flt32 (struct v3D32_t *vector); void Vector3D_ReflectX_flt64 (struct v3D64_t *vector);
C++void Vector3D::ReflectX (v3D32_t *vector); void Vector3D::ReflectX (v3D64_t *vector);
Description: Reflect vector through the X axis (change sign of Y and Z coordinates).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the Y axistop
Cvoid Vector3D_ReflectY_flt32 (struct v3D32_t *vector); void Vector3D_ReflectY_flt64 (struct v3D64_t *vector);
C++void Vector3D::ReflectY (v3D32_t *vector); void Vector3D::ReflectY (v3D64_t *vector);
Description: Reflect vector through the Y axis (change sign of X and Z coordinates).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the Z axistop
Cvoid Vector3D_ReflectZ_flt32 (struct v3D32_t *vector); void Vector3D_ReflectZ_flt64 (struct v3D64_t *vector);
C++void Vector3D::ReflectZ (v3D32_t *vector); void Vector3D::ReflectZ (v3D64_t *vector);
Description: Reflect vector through the Z axis (change sign of X and Y coordinates).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the YZ planetop
Cvoid Vector3D_ReflectYZ_flt32 (struct v3D32_t *vector); void Vector3D_ReflectYZ_flt64 (struct v3D64_t *vector);
C++void Vector3D::ReflectYZ (v3D32_t *vector); void Vector3D::ReflectYZ (v3D64_t *vector);
Description: Reflect vector through the YZ plane (change sign of X coordinate).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the XZ planetop
Cvoid Vector3D_ReflectXZ_flt32 (struct v3D32_t *vector); void Vector3D_ReflectXZ_flt64 (struct v3D64_t *vector);
C++void Vector3D::ReflectXZ (v3D32_t *vector); void Vector3D::ReflectXZ (v3D64_t *vector);
Description: Reflect vector through the XZ plane (change sign of Y coordinate).
Parameters:
- vector - pointer to the vector
Return value: None.
Reflection through the XY planetop
Cvoid Vector3D_ReflectXY_flt32 (struct v3D32_t *vector); void Vector3D_ReflectXY_flt64 (struct v3D64_t *vector);
C++void Vector3D::ReflectXY (v3D32_t *vector); void Vector3D::ReflectXY (v3D64_t *vector);
Description: Reflect vector through the XY plane (change sign of Z coordinate).
Parameters:
- vector - pointer to the vector
Return value: None.
Binary operations
Binary operations with vectors take two operands, which called target and source. Then they apply arithmetic operations to target vector, using source vector as second operand, and then store the result into target operand.
Addition of vectors
Cvoid Vector3D_Add_flt32 (struct v3D32_t *target, const struct v3D32_t *source); void Vector3D_Add_flt64 (struct v3D64_t *target, const struct v3D64_t *source);
C++void Vector3D::Add (v3D32_t *target, const v3D32_t *source); void Vector3D::Add (v3D64_t *target, const v3D64_t *source);
Description: Add source vector to target vector and store the result into the same place (into target vector).
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value: None.
Subtraction of vectors
Cvoid Vector3D_Sub_flt32 (struct v3D32_t *target, const struct v3D32_t *source); void Vector3D_Sub_flt64 (struct v3D64_t *target, const struct v3D64_t *source);
C++void Vector3D::Sub (v3D32_t *target, const v3D32_t *source); void Vector3D::Sub (v3D64_t *target, const v3D64_t *source);
Description: Subtract source vector from target vector and store the result into the same place (into target vector).
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value: None.
Multiplication by scalar value
Cvoid Vector3D_Mul_flt32 (struct v3D32_t *vector, flt32_t value); void Vector3D_Mul_flt64 (struct v3D64_t *vector, flt64_t value);
C++void Vector3D::Mul (v3D32_t *vector, flt32_t value); void Vector3D::Mul (v3D64_t *vector, flt64_t value);
Description: Multiply vector by scalar value and store the result into the same place.
Parameters:
- vector - pointer to the vector
- value - value to multiply
Return value: None.
Division by scalar value
Cvoid Vector3D_Div_flt32 (struct v3D32_t *vector, flt32_t value); void Vector3D_Div_flt64 (struct v3D64_t *vector, flt64_t value);
C++void Vector3D::Div (v3D32_t *vector, flt32_t value); void Vector3D::Div (v3D64_t *vector, flt64_t value);
Description: Divide vector by scalar value and store the result into the same place.
Parameters:
- vector - pointer to the vector
- value - value to divide
Return value: None.
Rotation of vector
Rotation functions change vector orientation by rotating it around common coordinate axes: X, Y, Z.
Rotation around the X axis
Cvoid Vector3D_RotateX_flt32 (struct v3D32_t *vector, flt32_t cos, flt32_t sin); void Vector3D_RotateX_flt64 (struct v3D64_t *vector, flt64_t cos, flt64_t sin);
C++void Vector3D::RotateX (v3D32_t *vector, flt32_t cos, flt32_t sin); void Vector3D::RotateX (v3D64_t *vector, flt64_t cos, flt64_t sin);
Description: Rotate vector around the X axis using cosine and sine value of rotation angle.
Parameters:
- vector - pointer to the vector
- cos - cosine value of angle to rotate
- sin - sine value of angle to rotate
Return value: None.
Rotation around the Y axis
Cvoid Vector3D_RotateY_flt32 (struct v3D32_t *vector, flt32_t cos, flt32_t sin); void Vector3D_RotateY_flt64 (struct v3D64_t *vector, flt64_t cos, flt64_t sin);
C++void Vector3D::RotateY (v3D32_t *vector, flt32_t cos, flt32_t sin); void Vector3D::RotateY (v3D64_t *vector, flt64_t cos, flt64_t sin);
Description: Rotate vector around the Y axis using cosine and sine value of rotation angle.
Parameters:
- vector - pointer to the vector
- cos - cosine value of angle to rotate
- sin - sine value of angle to rotate
Return value: None.
Rotation around the Z axis
Cvoid Vector3D_RotateZ_flt32 (struct v3D32_t *vector, flt32_t cos, flt32_t sin); void Vector3D_RotateZ_flt64 (struct v3D64_t *vector, flt64_t cos, flt64_t sin);
C++void Vector3D::RotateZ (v3D32_t *vector, flt32_t cos, flt32_t sin); void Vector3D::RotateZ (v3D64_t *vector, flt64_t cos, flt64_t sin);
Description: Rotate vector around the Z axis using cosine and sine value of rotation angle.
Parameters:
- vector - pointer to the vector
- cos - cosine value of angle to rotate
- sin - sine value of angle to rotate
Return value: None.
Shearing of vector
Shear functions do shear operations to vector in parallel to common planes. They use the value you provide to multiply it on third vector component, then add this value as shearing factor.
Shearing parallel to the YZ plane
Cvoid Vector3D_ShearYZ_flt32 (struct v3D32_t *vector, flt32_t value1, flt32_t value2); void Vector3D_ShearYZ_flt64 (struct v3D64_t *vector, flt64_t value1, flt64_t value2);
C++void Vector3D::ShearYZ (v3D32_t *vector, flt32_t value1, flt32_t value2); void Vector3D::ShearYZ (v3D64_t *vector, flt64_t value1, flt64_t value2);
Description: Shear vector parallel to the YZ plane.
Parameters:
- vector - pointer to the vector
- value1 - shear value by the Y axis
- value2 - shear value by the Z axis
Return value: None.
Shearing parallel to the XZ plane
Cvoid Vector3D_ShearXZ_flt32 (struct v3D32_t *vector, flt32_t value1, flt32_t value2); void Vector3D_ShearXZ_flt64 (struct v3D64_t *vector, flt64_t value1, flt64_t value2);
C++void Vector3D::ShearXZ (v3D32_t *vector, flt32_t value1, flt32_t value2); void Vector3D::ShearXZ (v3D64_t *vector, flt64_t value1, flt64_t value2);
Description: Shear vector parallel to the XZ plane.
Parameters:
- vector - pointer to the vector
- value1 - shear value by the X axis
- value2 - shear value by the Z axis
Return value: None.
Shearing parallel to the XY plane
Cvoid Vector3D_ShearXY_flt32 (struct v3D32_t *vector, flt32_t value1, flt32_t value2); void Vector3D_ShearXY_flt64 (struct v3D64_t *vector, flt64_t value1, flt64_t value2);
C++void Vector3D::ShearXY (v3D32_t *vector, flt32_t value1, flt32_t value2); void Vector3D::ShearXY (v3D64_t *vector, flt64_t value1, flt64_t value2);
Description: Shear vector parallel to the XY plane.
Parameters:
- vector - pointer to the vector
- value1 - shear value by the X axis
- value2 - shear value by the Y axis
Return value: None.
Scaling of vector
Cvoid Vector3D_Scale_flt32 (struct v3D32_t *target, const struct v3D32_t *source); void Vector3D_Scale_flt64 (struct v3D64_t *target, const struct v3D64_t *source);
C++void Vector3D::Scale (v3D32_t *target, const v3D32_t *source); void Vector3D::Scale (v3D64_t *target, const v3D64_t *source);
Description: Scale target vector using appropriate coordinates of source vector as scale values.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value: None.
Products
These functions compute standard vector products, are defined into vector algebra, and place the result into the same vector (vector product) or return product value as result of function (scalar and triple products).
Vector product
Cvoid Vector3D_VectorProduct_flt32 (struct v3D32_t *target, const struct v3D32_t *source); void Vector3D_VectorProduct_flt64 (struct v3D64_t *target, const struct v3D64_t *source);
C++void Vector3D::VectorProduct (v3D32_t *target, const v3D32_t *source); void Vector3D::VectorProduct (v3D64_t *target, const v3D64_t *source);
Description: Compute vector product of target and source vectors and store result into target vector.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value: None.
Scalar product
Cflt32_t Vector3D_ScalarProduct_flt32 (const struct v3D32_t *target, const struct v3D32_t *source); flt64_t Vector3D_ScalarProduct_flt64 (const struct v3D64_t *target, const struct v3D64_t *source);
C++flt32_t Vector3D::ScalarProduct (const v3D32_t *target, const v3D32_t *source); flt64_t Vector3D::ScalarProduct (const v3D64_t *target, const v3D64_t *source);
Description: Compute scalar product of target and source vectors.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- Scalar product of target and source vectors.
- NaN (not a number) if either target or source vector has NaN values.
Triple product
Cflt32_t Vector3D_TripleProduct_flt32 (const struct v3D32_t *target, const struct v3D32_t *source1, const struct v3D32_t *source2); flt64_t Vector3D_TripleProduct_flt64 (const struct v3D64_t *target, const struct v3D64_t *source1, const struct v3D64_t *source2);
C++flt32_t Vector3D::TripleProduct (const v3D32_t *target, const v3D32_t *source1, const v3D32_t *source2); flt64_t Vector3D::TripleProduct (const v3D64_t *target, const v3D64_t *source1, const v3D64_t *source2);
Description: Compute triple product of three of vectors.
Parameters:
- target - pointer to target vector
- source1 - pointer to first source vector
- source2 - pointer to second source vector
Return value:
- Triple product of all vectors.
- NaN (not a number) if any of vectors has NaN values.
Absolute value
Cflt32_t Vector3D_Abs_flt32 (const struct v3D32_t *vector); flt64_t Vector3D_Abs_flt64 (const struct v3D64_t *vector);
C++flt32_t Vector3D::Abs (const v3D32_t *vector, void); flt64_t Vector3D::Abs (const v3D64_t *vector, void);
Description: Calculate absolute value of vector.
Parameters:
- vector - pointer to the vector
Return value:
- Absolute value of vector.
- Inf (infinity) if absolute value is too big and cannot be represented in selected floating-point type.
- Subnormal value in case of underflow.
- 0 (zero) if vector absolute value can not be represented as subnormal value, or it is equal to zero.
Cosine value of angle between the vectors
Cflt32_t Vector3D_Cos_flt32 (const struct v3D32_t *target, const struct v3D32_t *source); flt64_t Vector3D_Cos_flt64 (const struct v3D64_t *target, const struct v3D64_t *source);
C++flt32_t Vector3D::Cos (const v3D32_t *target, const v3D32_t *source); flt64_t Vector3D::Cos (const v3D64_t *target, const v3D64_t *source);
Description: Find cosine value between the vectors.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- Cosine value of angle between the vectors.
- NaN (not a number) if either target or source vector is equal to zero.
Projection of the vector to another vector
Cflt32_t Vector3D_Projection_flt32 (const struct v3D32_t *target, const struct v3D32_t *source); flt64_t Vector3D_Projection_flt64 (const struct v3D64_t *target, const struct v3D64_t *source);
C++flt32_t Vector3D::Projection (const v3D32_t *target, const v3D32_t *source); flt64_t Vector3D::Projection (const v3D64_t *target, const v3D64_t *source);
Description: Compute projection of target vector onto source vector.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- Projection value of target vector onto source vector.
- Nan (not a number) if source vector is equal to zero.
Checks
Check functions compare vectors and return relation state between them. They also check vectors for special cases like zero vector.
Check for zero vector
Cbool Vector3D_IsZero_flt32 (const struct v3D32_t *vector); bool Vector3D_IsZero_flt64 (const struct v3D64_t *vector);
C++bool Vector3D::IsZero (const v3D32_t *vector, ); bool Vector3D::IsZero (const v3D64_t *vector, );
Description: Check vector for zero value.
Parameters:
- vector - pointer to the vector
Return value:
- TRUE (1) if vector has zero value.
- FALSE (0) in other case.
Check for equality of the vectors
Cbool Vector3D_IsEqual_flt32 (const struct v3D32_t *target, const struct v3D32_t *source); bool Vector3D_IsEqual_flt64 (const struct v3D64_t *target, const struct v3D64_t *source);
C++bool Vector3D::IsEqual (const v3D32_t *target, const v3D32_t *source); bool Vector3D::IsEqual (const v3D64_t *target, const v3D64_t *source);
Description: Check vectors for equality.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if all coordinates of target vector are equal to corresponding coordinates of source vector.
- FALSE (0) in other case.
Check for negativity of the vectors
Cbool Vector3D_IsNeg_flt32 (const struct v3D32_t *target, const struct v3D32_t *source); bool Vector3D_IsNeg_flt64 (const struct v3D64_t *target, const struct v3D64_t *source);
C++bool Vector3D::IsNeg (const v3D32_t *target, const v3D32_t *source); bool Vector3D::IsNeg (const v3D64_t *target, const v3D64_t *source);
Description: Check vectors for negativity (one vector is negative variant of another).
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if one vector is negative variant of another.
- FALSE (0) in other case.
Check for collinearity of the vectors
Cbool Vector3D_IsCollinear_flt32 (const struct v3D32_t *target, const struct v3D32_t *source); bool Vector3D_IsCollinear_flt64 (const struct v3D64_t *target, const struct v3D64_t *source);
C++bool Vector3D::IsCollinear (const v3D32_t *target, const v3D32_t *source); bool Vector3D::IsCollinear (const v3D64_t *target, const v3D64_t *source);
Description: Check if vectors are collinear.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if vectors are collinear.
- FALSE (0) in other case.
Check for orthogonality of the vectors
Cbool Vector3D_IsOrthogonal_flt32 (const struct v3D32_t *target, const struct v3D32_t *source); bool Vector3D_IsOrthogonal_flt64 (const struct v3D64_t *target, const struct v3D64_t *source);
C++bool Vector3D::IsOrthogonal (const v3D32_t *target, const v3D32_t *source); bool Vector3D::IsOrthogonal (const v3D64_t *target, const v3D64_t *source);
Description: Check if vectors are orthogonal.
Parameters:
- target - pointer to target vector
- source - pointer to source vector
Return value:
- TRUE (1) if vectors are orthogonal.
- FALSE (0) in other case.
Check for coplanarity of the vectors
Cbool Vector3D_IsCoplanar_flt32 (const struct v3D32_t *target, const struct v3D32_t *source1, const struct v3D32_t *source2); bool Vector3D_IsCoplanar_flt64 (const struct v3D64_t *target, const struct v3D64_t *source1, const struct v3D64_t *source2);
C++bool Vector3D::IsCoplanar (const v3D32_t *target, const v3D32_t *source1, const v3D32_t *source2); bool Vector3D::IsCoplanar (const v3D64_t *target, const v3D64_t *source1, const v3D64_t *source2);
Description: Check if three vectors are coplanar.
Parameters:
- target - pointer to first vector
- source1 - pointer to second vector
- source2 - pointer to third vector
Return value:
- TRUE (1) if vectors are coplanar.
- FALSE (0) in other case.