/// <summary> /// Multiplies transformation matrix /// </summary> /// <param name="matrix">Matrix to multiply with</param> /// <returns>Resulting matrix</returns> public TransformMatrix Multiply(TransformMatrix matrix) => new TransformMatrix( (M11 * matrix.M11) + (M12 * matrix.M21) + (M13 * matrix.M31) + (M14 * matrix.M41), (M11 * matrix.M12) + (M12 * matrix.M22) + (M13 * matrix.M32) + (M14 * matrix.M42), (M11 * matrix.M13) + (M12 * matrix.M23) + (M13 * matrix.M33) + (M14 * matrix.M43), (M11 * matrix.M14) + (M12 * matrix.M24) + (M13 * matrix.M34) + (M14 * matrix.M44), (M21 * matrix.M11) + (M22 * matrix.M21) + (M23 * matrix.M31) + (M24 * matrix.M41), (M21 * matrix.M12) + (M22 * matrix.M22) + (M23 * matrix.M32) + (M24 * matrix.M42), (M21 * matrix.M13) + (M22 * matrix.M23) + (M23 * matrix.M33) + (M24 * matrix.M43), (M21 * matrix.M14) + (M22 * matrix.M24) + (M23 * matrix.M34) + (M24 * matrix.M44), (M31 * matrix.M11) + (M32 * matrix.M21) + (M33 * matrix.M31) + (M34 * matrix.M41), (M31 * matrix.M12) + (M32 * matrix.M22) + (M33 * matrix.M32) + (M34 * matrix.M42), (M31 * matrix.M13) + (M32 * matrix.M23) + (M33 * matrix.M33) + (M34 * matrix.M43), (M31 * matrix.M14) + (M32 * matrix.M24) + (M33 * matrix.M34) + (M34 * matrix.M44), (M41 * matrix.M11) + (M42 * matrix.M21) + (M43 * matrix.M31) + (M44 * matrix.M41), (M41 * matrix.M12) + (M42 * matrix.M22) + (M43 * matrix.M32) + (M44 * matrix.M42), (M41 * matrix.M13) + (M42 * matrix.M23) + (M43 * matrix.M33) + (M44 * matrix.M43), (M41 * matrix.M14) + (M42 * matrix.M24) + (M43 * matrix.M34) + (M44 * matrix.M44));
/// <summary> /// Create rotation transformation around axis /// </summary> /// <param name="axis">Axis to generate rotation about</param> /// <param name="angle">Angle in radians</param> /// <param name="point">Anchor point of the rotation</param> /// <returns>Transformation matrix</returns> public static TransformMatrix CreateFromRotationAroundAxis(Vector axis, double angle, Point point) { var s = Math.Sin(angle); var c = Math.Cos(angle); var normVec = axis.Normalize(); var x = normVec.X; var y = normVec.Y; var z = normVec.Z; var xx = x * x; var yy = y * y; var zz = z * z; var xy = x * y; var xz = x * z; var yz = y * z; var matrix = new TransformMatrix( xx + c * (1.0 - xx), xy - c * xy + s * z, xz - c * xz - s * y, 0.0, xy - c * xy - s * z, yy + c * (1.0 - yy), yz - c * yz + s * x, 0.0, xz - c * xz + s * y, yz - c * yz - s * x, zz + c * (1.0 - zz), 0.0, 0.0, 0.0, 0.0, 1.0); if (Math.Abs(point.X) > double.Epsilon || Math.Abs(point.Y) > double.Epsilon || Math.Abs(point.Z) > double.Epsilon) { var translateMatrix = CreateFromTranslation(point.ToVector()); var translateMatrixInv = CreateFromTranslation(point.ToVector() * -1); matrix = translateMatrixInv.Multiply(matrix).Multiply(translateMatrix); } return(matrix); }
/// <summary> /// Transforms this vector with the transformation matrix /// </summary> /// <param name="matrix">Transformation matrix</param> /// <returns>Transformed vector</returns> public new Vector Transform(TransformMatrix matrix) => new Vector( X * matrix.M11 + Y * matrix.M21 + Z * matrix.M31, X * matrix.M12 + Y * matrix.M22 + Z * matrix.M32, X * matrix.M13 + Y * matrix.M23 + Z * matrix.M33);
/// <summary> /// Gets the transformation of this plane relative to the global XYZ /// </summary> /// <param name="plane">Plane</param> /// <returns>Transformation matrix</returns> public static TransformMatrix GetTransformation(this Plane plane) => TransformMatrix.Compose(plane.Direction, plane.Reference, plane.Normal, plane.Point);