Index: src/OFMatrix4x4.h ================================================================== --- src/OFMatrix4x4.h +++ src/OFMatrix4x4.h @@ -68,8 +68,22 @@ * and the receiver on the right side. * * @param matrix The matrix to multiply the receiver with */ - (void)multiplyWithMatrix: (OFMatrix4x4 *)matrix; + +/** + * @brief Transforms the specified 3D vector according to the matrix. + * + * As multiplying a 4x4 matrix with a 3D vector is not defined, this extends + * the 3D vector to a 4D vector with its `w` value being set to 0 and just + * discards the `w` value of the resulting 4D vector for the returned 3D + * vector. This allows reducing the number number of calculations performed and + * is mostly useful for 3D graphics. + * + * @param vector The 3D vector to transform + * @return The transformed 3D vector + */ +- (OFVector3D)transformedVector3D: (OFVector3D)vector; @end OF_ASSUME_NONNULL_END Index: src/OFMatrix4x4.m ================================================================== --- src/OFMatrix4x4.m +++ src/OFMatrix4x4.m @@ -181,10 +181,21 @@ _values[15] = matrix->_values[3] * copy[12] + matrix->_values[7] * copy[13] + matrix->_values[11] * copy[14] + matrix->_values[15] * copy[15]; } + +- (OFVector3D)transformedVector3D: (OFVector3D)vector +{ + return OFMakeVector3D( + _values[0] * vector.x + _values[4] * vector.y + + _values[8] * vector.z, + _values[1] * vector.x + _values[5] * vector.y + + _values[9] * vector.z, + _values[2] * vector.x + _values[6] * vector.y + + _values[10] * vector.z); +} - (OFString *)description { return [OFString stringWithFormat: @"