diff --git a/.swiftformat b/.swiftformat index 458ae5a..eb07e68 100644 --- a/.swiftformat +++ b/.swiftformat @@ -3,6 +3,8 @@ --commas inline --disable opaqueGenericParameters --disable preferKeyPath +--disable redundantType +--disable wrapMultilineStatementBraces --exclude .build,.github,.swiftpm,Scripts --extensionacl on-declarations --header strip diff --git a/.swiftlint.yml b/.swiftlint.yml index fd10715..bf40b6c 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -25,7 +25,6 @@ opt_in_rules: #- required_deinit #- type_contents_order #- unowned_variable_capture - - anyobject_protocol - array_init - attributes - closure_body_length @@ -48,7 +47,6 @@ opt_in_rules: - enum_case_associated_values_count - expiring_todo - explicit_init - - explicit_self - explicit_top_level_acl - fallthrough - fatal_error_message @@ -95,7 +93,6 @@ opt_in_rules: - raw_value_for_camel_cased_codable_enum - reduce_into - redundant_nil_coalescing - - redundant_type_annotation - required_enum_case - single_test_class - sorted_first_last @@ -109,11 +106,14 @@ opt_in_rules: - unavailable_function - unneeded_parentheses_in_closure_argument - untyped_error_in_catch - - unused_declaration - - unused_import - vertical_parameter_alignment_on_call - vertical_whitespace_between_cases - vertical_whitespace_closing_braces - vertical_whitespace_opening_braces - xct_specific_matcher - yoda_condition + +analyzer_rules: + - explicit_self + - unused_declaration + - unused_import diff --git a/Sources/FirebladeMath/Constants.swift b/Sources/FirebladeMath/Constants.swift index 1c7b303..ca21d76 100644 --- a/Sources/FirebladeMath/Constants.swift +++ b/Sources/FirebladeMath/Constants.swift @@ -1,21 +1,35 @@ /// Factor to convert degrees to radians (Double). -@inlinable public var kDegreeToRadians64: Double { Double.pi / 180.0 } +@inlinable public var kDegreeToRadians64: Double { + Double.pi / 180.0 +} + /// Factor to convert degrees to radians (Float). -@inlinable public var kDegreeToRadians32: Float { Float(Double.pi / 180.0) } +@inlinable public var kDegreeToRadians32: Float { + Float(Double.pi / 180.0) +} /// Factor to convert radians to degrees (Double). -@inlinable public var kRadiansToDegree64: Double { 180.0 / Double.pi } +@inlinable public var kRadiansToDegree64: Double { + 180.0 / Double.pi +} + /// Factor to convert radians to degrees (Float). -@inlinable public var kRadiansToDegree32: Float { Float(180.0 / Double.pi) } +@inlinable public var kRadiansToDegree32: Float { + Float(180.0 / Double.pi) +} /// Extension to add constants to Float. extension Float { /// Half of Pi (π/2). - @inlinable public static var halfPi: Float { Float.pi * 0.5 } + @inlinable public static var halfPi: Float { + Float.pi * 0.5 + } } /// Extension to add constants to Double. extension Double { /// Half of Pi (π/2). - @inlinable public static var halfPi: Double { Double.pi * 0.5 } + @inlinable public static var halfPi: Double { + Double.pi * 0.5 + } } diff --git a/Sources/FirebladeMath/Functions/adjugate.swift b/Sources/FirebladeMath/Functions/adjugate.swift index bb125dd..048a2d4 100644 --- a/Sources/FirebladeMath/Functions/adjugate.swift +++ b/Sources/FirebladeMath/Functions/adjugate.swift @@ -41,62 +41,126 @@ public func adjugate(_ inMat: Mat3x3d) -> Mat3x3d { } /// Computes the adjugate of a 4x4 matrix. -/// - Parameter m: The input matrix. +/// - Parameter matrix: The input matrix. /// - Returns: The adjugate of the input matrix. -public func adjugate(_ m: Mat4x4f) -> Mat4x4f { +public func adjugate(_ matrix: Mat4x4f) -> Mat4x4f { // Adjugate is the transpose of the cofactor matrix. // cofactor(i, j) = (-1)^(i+j) * minor(i, j) - // Here we use m[col, row] indexing. + // Here we use matrix[col, row] indexing. var res = Mat4x4f.identity - res[0, 0] = m[1, 1] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 1] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) + m[3, 1] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2]) - res[1, 0] = -(m[1, 0] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 0] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) + m[3, 0] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2])) - res[2, 0] = m[1, 0] * (m[2, 1] * m[3, 3] - m[2, 3] * m[3, 1]) - m[2, 0] * (m[1, 1] * m[3, 3] - m[1, 3] * m[3, 1]) + m[3, 0] * (m[1, 1] * m[2, 3] - m[1, 3] * m[2, 1]) - res[3, 0] = -(m[1, 0] * (m[2, 1] * m[3, 2] - m[2, 2] * m[3, 1]) - m[2, 0] * (m[1, 1] * m[3, 2] - m[1, 2] * m[3, 1]) + m[3, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1])) - - res[0, 1] = -(m[0, 1] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 1] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 1] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2])) - res[1, 1] = m[0, 0] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 0] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 0] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2]) - res[2, 1] = -(m[0, 0] * (m[2, 1] * m[3, 3] - m[2, 3] * m[3, 1]) - m[2, 0] * (m[0, 1] * m[3, 3] - m[0, 3] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[2, 3] - m[0, 3] * m[2, 1])) - res[3, 1] = m[0, 0] * (m[2, 1] * m[3, 2] - m[2, 2] * m[3, 1]) - m[2, 0] * (m[0, 1] * m[3, 2] - m[0, 2] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[2, 2] - m[0, 2] * m[2, 1]) - - res[0, 2] = m[0, 1] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) - m[1, 1] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 1] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2]) - res[1, 2] = -(m[0, 0] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) - m[1, 0] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 0] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2])) - res[2, 2] = m[0, 0] * (m[1, 1] * m[3, 3] - m[1, 3] * m[3, 1]) - m[1, 0] * (m[0, 1] * m[3, 3] - m[0, 3] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[1, 3] - m[0, 3] * m[1, 1]) - res[3, 2] = -(m[0, 0] * (m[1, 1] * m[3, 2] - m[1, 2] * m[3, 1]) - m[1, 0] * (m[0, 1] * m[3, 2] - m[0, 2] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[1, 2] - m[0, 2] * m[1, 1])) - - res[0, 3] = -(m[0, 1] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2]) - m[1, 1] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2]) + m[2, 1] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2])) - res[1, 3] = m[0, 0] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2]) - m[1, 0] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2]) + m[2, 0] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2]) - res[2, 3] = -(m[0, 0] * (m[1, 1] * m[2, 3] - m[1, 3] * m[2, 1]) - m[1, 0] * (m[0, 1] * m[2, 3] - m[0, 3] * m[2, 1]) + m[2, 0] * (m[0, 1] * m[1, 3] - m[0, 3] * m[1, 1])) - res[3, 3] = m[0, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1]) - m[1, 0] * (m[0, 1] * m[2, 2] - m[0, 2] * m[2, 1]) + m[2, 0] * (m[0, 1] * m[1, 2] - m[0, 2] * m[1, 1]) + res[0, 0] = matrix[1, 1] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 1] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + + matrix[3, 1] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2]) + res[1, 0] = -(matrix[1, 0] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 0] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + + matrix[3, 0] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2])) + res[2, 0] = matrix[1, 0] * (matrix[2, 1] * matrix[3, 3] - matrix[2, 3] * matrix[3, 1]) + - matrix[2, 0] * (matrix[1, 1] * matrix[3, 3] - matrix[1, 3] * matrix[3, 1]) + + matrix[3, 0] * (matrix[1, 1] * matrix[2, 3] - matrix[1, 3] * matrix[2, 1]) + res[3, 0] = -(matrix[1, 0] * (matrix[2, 1] * matrix[3, 2] - matrix[2, 2] * matrix[3, 1]) + - matrix[2, 0] * (matrix[1, 1] * matrix[3, 2] - matrix[1, 2] * matrix[3, 1]) + + matrix[3, 0] * (matrix[1, 1] * matrix[2, 2] - matrix[1, 2] * matrix[2, 1])) + + res[0, 1] = -(matrix[0, 1] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 1] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 1] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2])) + res[1, 1] = matrix[0, 0] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 0] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 0] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2]) + res[2, 1] = -(matrix[0, 0] * (matrix[2, 1] * matrix[3, 3] - matrix[2, 3] * matrix[3, 1]) + - matrix[2, 0] * (matrix[0, 1] * matrix[3, 3] - matrix[0, 3] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[2, 3] - matrix[0, 3] * matrix[2, 1])) + res[3, 1] = matrix[0, 0] * (matrix[2, 1] * matrix[3, 2] - matrix[2, 2] * matrix[3, 1]) + - matrix[2, 0] * (matrix[0, 1] * matrix[3, 2] - matrix[0, 2] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[2, 2] - matrix[0, 2] * matrix[2, 1]) + + res[0, 2] = matrix[0, 1] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + - matrix[1, 1] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 1] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2]) + res[1, 2] = -(matrix[0, 0] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + - matrix[1, 0] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 0] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2])) + res[2, 2] = matrix[0, 0] * (matrix[1, 1] * matrix[3, 3] - matrix[1, 3] * matrix[3, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[3, 3] - matrix[0, 3] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[1, 3] - matrix[0, 3] * matrix[1, 1]) + res[3, 2] = -(matrix[0, 0] * (matrix[1, 1] * matrix[3, 2] - matrix[1, 2] * matrix[3, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[3, 2] - matrix[0, 2] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[1, 2] - matrix[0, 2] * matrix[1, 1])) + + res[0, 3] = -(matrix[0, 1] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2]) + - matrix[1, 1] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2]) + + matrix[2, 1] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2])) + res[1, 3] = matrix[0, 0] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2]) + - matrix[1, 0] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2]) + + matrix[2, 0] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2]) + res[2, 3] = -(matrix[0, 0] * (matrix[1, 1] * matrix[2, 3] - matrix[1, 3] * matrix[2, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[2, 3] - matrix[0, 3] * matrix[2, 1]) + + matrix[2, 0] * (matrix[0, 1] * matrix[1, 3] - matrix[0, 3] * matrix[1, 1])) + res[3, 3] = matrix[0, 0] * (matrix[1, 1] * matrix[2, 2] - matrix[1, 2] * matrix[2, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[2, 2] - matrix[0, 2] * matrix[2, 1]) + + matrix[2, 0] * (matrix[0, 1] * matrix[1, 2] - matrix[0, 2] * matrix[1, 1]) return res } /// Computes the adjugate of a 4x4 matrix. -/// - Parameter m: The input matrix. +/// - Parameter matrix: The input matrix. /// - Returns: The adjugate of the input matrix. -public func adjugate(_ m: Mat4x4d) -> Mat4x4d { +public func adjugate(_ matrix: Mat4x4d) -> Mat4x4d { var res = Mat4x4d.identity - res[0, 0] = m[1, 1] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 1] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) + m[3, 1] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2]) - res[1, 0] = -(m[1, 0] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 0] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) + m[3, 0] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2])) - res[2, 0] = m[1, 0] * (m[2, 1] * m[3, 3] - m[2, 3] * m[3, 1]) - m[2, 0] * (m[1, 1] * m[3, 3] - m[1, 3] * m[3, 1]) + m[3, 0] * (m[1, 1] * m[2, 3] - m[1, 3] * m[2, 1]) - res[3, 0] = -(m[1, 0] * (m[2, 1] * m[3, 2] - m[2, 2] * m[3, 1]) - m[2, 0] * (m[1, 1] * m[3, 2] - m[1, 2] * m[3, 1]) + m[3, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1])) - - res[0, 1] = -(m[0, 1] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 1] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 1] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2])) - res[1, 1] = m[0, 0] * (m[2, 2] * m[3, 3] - m[2, 3] * m[3, 2]) - m[2, 0] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 0] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2]) - res[2, 1] = -(m[0, 0] * (m[2, 1] * m[3, 3] - m[2, 3] * m[3, 1]) - m[2, 0] * (m[0, 1] * m[3, 3] - m[0, 3] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[2, 3] - m[0, 3] * m[2, 1])) - res[3, 1] = m[0, 0] * (m[2, 1] * m[3, 2] - m[2, 2] * m[3, 1]) - m[2, 0] * (m[0, 1] * m[3, 2] - m[0, 2] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[2, 2] - m[0, 2] * m[2, 1]) - - res[0, 2] = m[0, 1] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) - m[1, 1] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 1] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2]) - res[1, 2] = -(m[0, 0] * (m[1, 2] * m[3, 3] - m[1, 3] * m[3, 2]) - m[1, 0] * (m[0, 2] * m[3, 3] - m[0, 3] * m[3, 2]) + m[3, 0] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2])) - res[2, 2] = m[0, 0] * (m[1, 1] * m[3, 3] - m[1, 3] * m[3, 1]) - m[1, 0] * (m[0, 1] * m[3, 3] - m[0, 3] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[1, 3] - m[0, 3] * m[1, 1]) - res[3, 2] = -(m[0, 0] * (m[1, 1] * m[3, 2] - m[1, 2] * m[3, 1]) - m[1, 0] * (m[0, 1] * m[3, 2] - m[0, 2] * m[3, 1]) + m[3, 0] * (m[0, 1] * m[1, 2] - m[0, 2] * m[1, 1])) - - res[0, 3] = -(m[0, 1] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2]) - m[1, 1] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2]) + m[2, 1] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2])) - res[1, 3] = m[0, 0] * (m[1, 2] * m[2, 3] - m[1, 3] * m[2, 2]) - m[1, 0] * (m[0, 2] * m[2, 3] - m[0, 3] * m[2, 2]) + m[2, 0] * (m[0, 2] * m[1, 3] - m[0, 3] * m[1, 2]) - res[2, 3] = -(m[0, 0] * (m[1, 1] * m[2, 3] - m[1, 3] * m[2, 1]) - m[1, 0] * (m[0, 1] * m[2, 3] - m[0, 3] * m[2, 1]) + m[2, 0] * (m[0, 1] * m[1, 3] - m[0, 3] * m[1, 1])) - res[3, 3] = m[0, 0] * (m[1, 1] * m[2, 2] - m[1, 2] * m[2, 1]) - m[1, 0] * (m[0, 1] * m[2, 2] - m[0, 2] * m[2, 1]) + m[2, 0] * (m[0, 1] * m[1, 2] - m[0, 2] * m[1, 1]) + res[0, 0] = matrix[1, 1] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 1] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + + matrix[3, 1] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2]) + res[1, 0] = -(matrix[1, 0] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 0] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + + matrix[3, 0] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2])) + res[2, 0] = matrix[1, 0] * (matrix[2, 1] * matrix[3, 3] - matrix[2, 3] * matrix[3, 1]) + - matrix[2, 0] * (matrix[1, 1] * matrix[3, 3] - matrix[1, 3] * matrix[3, 1]) + + matrix[3, 0] * (matrix[1, 1] * matrix[2, 3] - matrix[1, 3] * matrix[2, 1]) + res[3, 0] = -(matrix[1, 0] * (matrix[2, 1] * matrix[3, 2] - matrix[2, 2] * matrix[3, 1]) + - matrix[2, 0] * (matrix[1, 1] * matrix[3, 2] - matrix[1, 2] * matrix[3, 1]) + + matrix[3, 0] * (matrix[1, 1] * matrix[2, 2] - matrix[1, 2] * matrix[2, 1])) + + res[0, 1] = -(matrix[0, 1] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 1] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 1] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2])) + res[1, 1] = matrix[0, 0] * (matrix[2, 2] * matrix[3, 3] - matrix[2, 3] * matrix[3, 2]) + - matrix[2, 0] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 0] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2]) + res[2, 1] = -(matrix[0, 0] * (matrix[2, 1] * matrix[3, 3] - matrix[2, 3] * matrix[3, 1]) + - matrix[2, 0] * (matrix[0, 1] * matrix[3, 3] - matrix[0, 3] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[2, 3] - matrix[0, 3] * matrix[2, 1])) + res[3, 1] = matrix[0, 0] * (matrix[2, 1] * matrix[3, 2] - matrix[2, 2] * matrix[3, 1]) + - matrix[2, 0] * (matrix[0, 1] * matrix[3, 2] - matrix[0, 2] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[2, 2] - matrix[0, 2] * matrix[2, 1]) + + res[0, 2] = matrix[0, 1] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + - matrix[1, 1] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 1] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2]) + res[1, 2] = -(matrix[0, 0] * (matrix[1, 2] * matrix[3, 3] - matrix[1, 3] * matrix[3, 2]) + - matrix[1, 0] * (matrix[0, 2] * matrix[3, 3] - matrix[0, 3] * matrix[3, 2]) + + matrix[3, 0] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2])) + res[2, 2] = matrix[0, 0] * (matrix[1, 1] * matrix[3, 3] - matrix[1, 3] * matrix[3, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[3, 3] - matrix[0, 3] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[1, 3] - matrix[0, 3] * matrix[1, 1]) + res[3, 2] = -(matrix[0, 0] * (matrix[1, 1] * matrix[3, 2] - matrix[1, 2] * matrix[3, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[3, 2] - matrix[0, 2] * matrix[3, 1]) + + matrix[3, 0] * (matrix[0, 1] * matrix[1, 2] - matrix[0, 2] * matrix[1, 1])) + + res[0, 3] = -(matrix[0, 1] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2]) + - matrix[1, 1] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2]) + + matrix[2, 1] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2])) + res[1, 3] = matrix[0, 0] * (matrix[1, 2] * matrix[2, 3] - matrix[1, 3] * matrix[2, 2]) + - matrix[1, 0] * (matrix[0, 2] * matrix[2, 3] - matrix[0, 3] * matrix[2, 2]) + + matrix[2, 0] * (matrix[0, 2] * matrix[1, 3] - matrix[0, 3] * matrix[1, 2]) + res[2, 3] = -(matrix[0, 0] * (matrix[1, 1] * matrix[2, 3] - matrix[1, 3] * matrix[2, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[2, 3] - matrix[0, 3] * matrix[2, 1]) + + matrix[2, 0] * (matrix[0, 1] * matrix[1, 3] - matrix[0, 3] * matrix[1, 1])) + res[3, 3] = matrix[0, 0] * (matrix[1, 1] * matrix[2, 2] - matrix[1, 2] * matrix[2, 1]) + - matrix[1, 0] * (matrix[0, 1] * matrix[2, 2] - matrix[0, 2] * matrix[2, 1]) + + matrix[2, 0] * (matrix[0, 1] * matrix[1, 2] - matrix[0, 2] * matrix[1, 1]) return res } diff --git a/Sources/FirebladeMath/Functions/determinant.swift b/Sources/FirebladeMath/Functions/determinant.swift index 1da9e3d..40aafaa 100644 --- a/Sources/FirebladeMath/Functions/determinant.swift +++ b/Sources/FirebladeMath/Functions/determinant.swift @@ -40,10 +40,10 @@ public func determinant(_ mat: Mat3x3f) -> Float { #if FRB_MATH_USE_SIMD return simd.simd_determinant(mat.storage) #else - let a = mat[0, 0] * (mat[1, 1] * mat[2, 2] - mat[1, 2] * mat[2, 1]) - let b = mat[0, 1] * (mat[1, 0] * mat[2, 2] - mat[1, 2] * mat[2, 0]) - let c = mat[0, 2] * (mat[1, 0] * mat[2, 1] - mat[1, 1] * mat[2, 0]) - return a - b + c + let termA = mat[0, 0] * (mat[1, 1] * mat[2, 2] - mat[1, 2] * mat[2, 1]) + let termB = mat[0, 1] * (mat[1, 0] * mat[2, 2] - mat[1, 2] * mat[2, 0]) + let termC = mat[0, 2] * (mat[1, 0] * mat[2, 1] - mat[1, 1] * mat[2, 0]) + return termA - termB + termC #endif } @@ -85,9 +85,9 @@ public func determinant(_ mat: Mat3x3d) -> Double { #if FRB_MATH_USE_SIMD return simd.simd_determinant(mat.storage) #else - let a = mat[0, 0] * (mat[1, 1] * mat[2, 2] - mat[1, 2] * mat[2, 1]) - let b = mat[0, 1] * (mat[1, 0] * mat[2, 2] - mat[1, 2] * mat[2, 0]) - let c = mat[0, 2] * (mat[1, 0] * mat[2, 1] - mat[1, 1] * mat[2, 0]) - return a - b + c + let termA = mat[0, 0] * (mat[1, 1] * mat[2, 2] - mat[1, 2] * mat[2, 1]) + let termB = mat[0, 1] * (mat[1, 0] * mat[2, 2] - mat[1, 2] * mat[2, 0]) + let termC = mat[0, 2] * (mat[1, 0] * mat[2, 1] - mat[1, 1] * mat[2, 0]) + return termA - termB + termC #endif } diff --git a/Sources/FirebladeMath/Functions/matrix.swift b/Sources/FirebladeMath/Functions/matrix.swift index d70ae63..7a83df6 100644 --- a/Sources/FirebladeMath/Functions/matrix.swift +++ b/Sources/FirebladeMath/Functions/matrix.swift @@ -56,15 +56,24 @@ public func matrix4x4(from quat: Quat4f) -> Mat4x4f { #else let v = Vec4f(quat) return Mat4x4f( - Vec4f(1 - 2 * (v.y * v.y + v.z * v.z), - 2 * (v.x * v.y + v.z * v.w), - 2 * (v.x * v.z - v.y * v.w), 0), - Vec4f(2 * (v.x * v.y - v.z * v.w), - 1 - 2 * (v.z * v.z + v.x * v.x), - 2 * (v.y * v.z + v.x * v.w), 0), - Vec4f(2 * (v.z * v.x + v.y * v.w), - 2 * (v.y * v.z - v.x * v.w), - 1 - 2 * (v.y * v.y + v.x * v.x), 0), + Vec4f( + 1 - 2 * (v.y * v.y + v.z * v.z), + 2 * (v.x * v.y + v.z * v.w), + 2 * (v.x * v.z - v.y * v.w), + 0 + ), + Vec4f( + 2 * (v.x * v.y - v.z * v.w), + 1 - 2 * (v.z * v.z + v.x * v.x), + 2 * (v.y * v.z + v.x * v.w), + 0 + ), + Vec4f( + 2 * (v.z * v.x + v.y * v.w), + 2 * (v.y * v.z - v.x * v.w), + 1 - 2 * (v.y * v.y + v.x * v.x), + 0 + ), Vec4f(0, 0, 0, 1) ) #endif @@ -79,15 +88,24 @@ public func matrix4x4(from quat: Quat4d) -> Mat4x4d { #else let v = Vec4d(quat) return Mat4x4d( - Vec4d(1 - 2 * (v.y * v.y + v.z * v.z), - 2 * (v.x * v.y + v.z * v.w), - 2 * (v.x * v.z - v.y * v.w), 0), - Vec4d(2 * (v.x * v.y - v.z * v.w), - 1 - 2 * (v.z * v.z + v.x * v.x), - 2 * (v.y * v.z + v.x * v.w), 0), - Vec4d(2 * (v.z * v.x + v.y * v.w), - 2 * (v.y * v.z - v.x * v.w), - 1 - 2 * (v.y * v.y + v.x * v.x), 0), + Vec4d( + 1 - 2 * (v.y * v.y + v.z * v.z), + 2 * (v.x * v.y + v.z * v.w), + 2 * (v.x * v.z - v.y * v.w), + 0 + ), + Vec4d( + 2 * (v.x * v.y - v.z * v.w), + 1 - 2 * (v.z * v.z + v.x * v.x), + 2 * (v.y * v.z + v.x * v.w), + 0 + ), + Vec4d( + 2 * (v.z * v.x + v.y * v.w), + 2 * (v.y * v.z - v.x * v.w), + 1 - 2 * (v.y * v.y + v.x * v.x), + 0 + ), Vec4d(0, 0, 0, 1) ) #endif diff --git a/Sources/FirebladeMath/Functions/sign.swift b/Sources/FirebladeMath/Functions/sign.swift index 72c9ee3..a71d190 100644 --- a/Sources/FirebladeMath/Functions/sign.swift +++ b/Sources/FirebladeMath/Functions/sign.swift @@ -9,7 +9,7 @@ public func sign(_ x: Double) -> Double { #if FRB_MATH_USE_SIMD return simd_sign(x) #else - return (x == 0 || x != x) ? 0 : copysign(1, x) + return (x == 0 || x.isNaN) ? 0 : copysign(1, x) #endif } @@ -20,6 +20,6 @@ public func sign(_ x: Float) -> Float { #if FRB_MATH_USE_SIMD return simd_sign(x) #else - return (x == 0 || x != x) ? 0 : copysign(1, x) + return (x == 0 || x.isNaN) ? 0 : copysign(1, x) #endif } diff --git a/Sources/FirebladeMath/Matrix/Mat4x4d.swift b/Sources/FirebladeMath/Matrix/Mat4x4d.swift index da2c1d6..1bf44b5 100644 --- a/Sources/FirebladeMath/Matrix/Mat4x4d.swift +++ b/Sources/FirebladeMath/Matrix/Mat4x4d.swift @@ -174,7 +174,8 @@ extension Mat4x4d { /// Returns the Euler angles (pitch, yaw, roll) for this matrix in radians. /// - Returns: A 3D vector where x is pitch, y is yaw, and z is roll. - @inline(__always) public var eulerAnglesXYZ: Vec3d { + @inline(__always) + public var eulerAnglesXYZ: Vec3d { let thetaX: Double let thetaY: Double let thetaZ: Double diff --git a/Sources/FirebladeMath/Matrix/Mat4x4f.swift b/Sources/FirebladeMath/Matrix/Mat4x4f.swift index 39f5d8d..2308763 100644 --- a/Sources/FirebladeMath/Matrix/Mat4x4f.swift +++ b/Sources/FirebladeMath/Matrix/Mat4x4f.swift @@ -273,7 +273,8 @@ extension Mat4x4f { /// Returns the Euler angles (pitch, yaw, roll) for this matrix in radians. /// - Returns: A 3D vector where x is pitch, y is yaw, and z is roll. - @inline(__always) public var eulerAnglesXYZ: Vec3f { + @inline(__always) + public var eulerAnglesXYZ: Vec3f { // /// https://www.geometrictools.com/Documentation/EulerAngles.pdf let thetaX: Float let thetaY: Float diff --git a/Sources/FirebladeMath/Matrix/Matrix+Multiplication.swift b/Sources/FirebladeMath/Matrix/Matrix+Multiplication.swift index e2ceb01..69b0a08 100644 --- a/Sources/FirebladeMath/Matrix/Matrix+Multiplication.swift +++ b/Sources/FirebladeMath/Matrix/Matrix+Multiplication.swift @@ -1,3 +1,4 @@ +// swiftlint:disable file_length #if FRB_MATH_USE_SIMD import func simd.simd_mul #endif diff --git a/Sources/FirebladeMath/Matrix/Matrix+Operators.swift b/Sources/FirebladeMath/Matrix/Matrix+Operators.swift index d32d959..2278b58 100644 --- a/Sources/FirebladeMath/Matrix/Matrix+Operators.swift +++ b/Sources/FirebladeMath/Matrix/Matrix+Operators.swift @@ -1,305 +1,317 @@ -// MARK: 4x4f - -/// Multiplies two 4x4 float matrices. -/// - Parameters: -/// - lhs: The left-hand side matrix. -/// - rhs: The right-hand side matrix. -/// - Returns: The product of the two matrices. -@inlinable -public func * (lhs: Mat4x4f, rhs: Mat4x4f) -> Mat4x4f { - FirebladeMath.multiply(lhs, rhs) +// MARK: - 4x4f + +extension Mat4x4f { + /// Multiplies two 4x4 float matrices. + /// - Parameters: + /// - lhs: The left-hand side matrix. + /// - rhs: The right-hand side matrix. + /// - Returns: The product of the two matrices. + @inlinable + public static func * (lhs: Mat4x4f, rhs: Mat4x4f) -> Mat4x4f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 4x4 float matrix by a scalar. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The matrix. + /// - Returns: The resulting matrix. + @inlinable + public static func * (lhs: Float, rhs: Mat4x4f) -> Mat4x4f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 4D float vector by a 4x4 float matrix. + /// - Parameters: + /// - lhs: The vector. + /// - rhs: The matrix. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Vec4f, rhs: Mat4x4f) -> Vec4f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 4x4 float matrix by a 4D float vector. + /// - Parameters: + /// - lhs: The matrix. + /// - rhs: The vector. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Mat4x4f, rhs: Vec4f) -> Vec4f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies two 4x4 float matrices and assigns the result to the first matrix. + /// - Parameters: + /// - lhs: The left-hand side matrix to be updated. + /// - rhs: The right-hand side matrix. + @inlinable + public static func *= (lhs: inout Mat4x4f, rhs: Mat4x4f) { + lhs = FirebladeMath.multiply(lhs, rhs) + } } -/// Multiplies a 4x4 float matrix by a scalar. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The matrix. -/// - Returns: The resulting matrix. -@inlinable -public func * (lhs: Float, rhs: Mat4x4f) -> Mat4x4f { - FirebladeMath.multiply(lhs, rhs) +// MARK: - 4x4d + +extension Mat4x4d { + /// Multiplies two 4x4 double matrices. + /// - Parameters: + /// - lhs: The left-hand side matrix. + /// - rhs: The right-hand side matrix. + /// - Returns: The product of the two matrices. + @inlinable + public static func * (lhs: Mat4x4d, rhs: Mat4x4d) -> Mat4x4d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 4x4 double matrix by a scalar. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The matrix. + /// - Returns: The resulting matrix. + @inlinable + public static func * (lhs: Double, rhs: Mat4x4d) -> Mat4x4d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 4D double vector by a 4x4 double matrix. + /// - Parameters: + /// - lhs: The vector. + /// - rhs: The matrix. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Vec4d, rhs: Mat4x4d) -> Vec4d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 4x4 double matrix by a 4D double vector. + /// - Parameters: + /// - lhs: The matrix. + /// - rhs: The vector. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Mat4x4d, rhs: Vec4d) -> Vec4d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies two 4x4 double matrices and assigns the result to the first matrix. + /// - Parameters: + /// - lhs: The left-hand side matrix to be updated. + /// - rhs: The right-hand side matrix. + @inlinable + public static func *= (lhs: inout Mat4x4d, rhs: Mat4x4d) { + lhs = FirebladeMath.multiply(lhs, rhs) + } } -/// Multiplies a 4D float vector by a 4x4 float matrix. -/// - Parameters: -/// - lhs: The vector. -/// - rhs: The matrix. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Vec4f, rhs: Mat4x4f) -> Vec4f { - FirebladeMath.multiply(lhs, rhs) +// MARK: - 3x3f + +extension Mat3x3f { + /// Multiplies two 3x3 float matrices. + /// - Parameters: + /// - lhs: The left-hand side matrix. + /// - rhs: The right-hand side matrix. + /// - Returns: The product of the two matrices. + @inlinable + public static func * (lhs: Mat3x3f, rhs: Mat3x3f) -> Mat3x3f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 3x3 float matrix by a scalar. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The matrix. + /// - Returns: The resulting matrix. + @inlinable + public static func * (lhs: Float, rhs: Mat3x3f) -> Mat3x3f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 3D float vector by a 3x3 float matrix. + /// - Parameters: + /// - lhs: The vector. + /// - rhs: The matrix. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Vec3f, rhs: Mat3x3f) -> Vec3f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 3x3 float matrix by a 3D float vector. + /// - Parameters: + /// - lhs: The matrix. + /// - rhs: The vector. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Mat3x3f, rhs: Vec3f) -> Vec3f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies two 3x3 float matrices and assigns the result to the first matrix. + /// - Parameters: + /// - lhs: The left-hand side matrix to be updated. + /// - rhs: The right-hand side matrix. + @inlinable + public static func *= (lhs: inout Mat3x3f, rhs: Mat3x3f) { + lhs = FirebladeMath.multiply(lhs, rhs) + } } -/// Multiplies a 4x4 float matrix by a 4D float vector. -/// - Parameters: -/// - lhs: The matrix. -/// - rhs: The vector. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Mat4x4f, rhs: Vec4f) -> Vec4f { - FirebladeMath.multiply(lhs, rhs) +// MARK: - 3x3d + +extension Mat3x3d { + /// Multiplies two 3x3 double matrices. + /// - Parameters: + /// - lhs: The left-hand side matrix. + /// - rhs: The right-hand side matrix. + /// - Returns: The product of the two matrices. + @inlinable + public static func * (lhs: Mat3x3d, rhs: Mat3x3d) -> Mat3x3d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 3x3 double matrix by a scalar. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The matrix. + /// - Returns: The resulting matrix. + @inlinable + public static func * (lhs: Double, rhs: Mat3x3d) -> Mat3x3d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 3D double vector by a 3x3 double matrix. + /// - Parameters: + /// - lhs: The vector. + /// - rhs: The matrix. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Vec3d, rhs: Mat3x3d) -> Vec3d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 3x3 double matrix by a 3D double vector. + /// - Parameters: + /// - lhs: The matrix. + /// - rhs: The vector. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Mat3x3d, rhs: Vec3d) -> Vec3d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies two 3x3 double matrices and assigns the result to the first matrix. + /// - Parameters: + /// - lhs: The left-hand side matrix to be updated. + /// - rhs: The right-hand side matrix. + @inlinable + public static func *= (lhs: inout Mat3x3d, rhs: Mat3x3d) { + lhs = FirebladeMath.multiply(lhs, rhs) + } } -/// Multiplies two 4x4 float matrices and assigns the result to the first matrix. -/// - Parameters: -/// - lhs: The left-hand side matrix to be updated. -/// - rhs: The right-hand side matrix. -@inlinable -public func *= (lhs: inout Mat4x4f, rhs: Mat4x4f) { - lhs = FirebladeMath.multiply(lhs, rhs) +// MARK: - 2x2f + +extension Mat2x2f { + /// Multiplies two 2x2 float matrices. + /// - Parameters: + /// - lhs: The left-hand side matrix. + /// - rhs: The right-hand side matrix. + /// - Returns: The product of the two matrices. + @inlinable + public static func * (lhs: Mat2x2f, rhs: Mat2x2f) -> Mat2x2f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 2x2 float matrix by a scalar. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The matrix. + /// - Returns: The resulting matrix. + @inlinable + public static func * (lhs: Float, rhs: Mat2x2f) -> Mat2x2f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 2D float vector by a 2x2 float matrix. + /// - Parameters: + /// - lhs: The vector. + /// - rhs: The matrix. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Vec2f, rhs: Mat2x2f) -> Vec2f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 2x2 float matrix by a 2D float vector. + /// - Parameters: + /// - lhs: The matrix. + /// - rhs: The vector. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Mat2x2f, rhs: Vec2f) -> Vec2f { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies two 2x2 float matrices and assigns the result to the first matrix. + /// - Parameters: + /// - lhs: The left-hand side matrix to be updated. + /// - rhs: The right-hand side matrix. + @inlinable + public static func *= (lhs: inout Mat2x2f, rhs: Mat2x2f) { + lhs = FirebladeMath.multiply(lhs, rhs) + } } -// MARK: 4x4d - -/// Multiplies two 4x4 double matrices. -/// - Parameters: -/// - lhs: The left-hand side matrix. -/// - rhs: The right-hand side matrix. -/// - Returns: The product of the two matrices. -@inlinable -public func * (lhs: Mat4x4d, rhs: Mat4x4d) -> Mat4x4d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 4x4 double matrix by a scalar. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The matrix. -/// - Returns: The resulting matrix. -@inlinable -public func * (lhs: Double, rhs: Mat4x4d) -> Mat4x4d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 4D double vector by a 4x4 double matrix. -/// - Parameters: -/// - lhs: The vector. -/// - rhs: The matrix. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Vec4d, rhs: Mat4x4d) -> Vec4d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 4x4 double matrix by a 4D double vector. -/// - Parameters: -/// - lhs: The matrix. -/// - rhs: The vector. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Mat4x4d, rhs: Vec4d) -> Vec4d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies two 4x4 double matrices and assigns the result to the first matrix. -/// - Parameters: -/// - lhs: The left-hand side matrix to be updated. -/// - rhs: The right-hand side matrix. -@inlinable -public func *= (lhs: inout Mat4x4d, rhs: Mat4x4d) { - lhs = FirebladeMath.multiply(lhs, rhs) -} - -// MARK: 3x3f - -/// Multiplies two 3x3 float matrices. -/// - Parameters: -/// - lhs: The left-hand side matrix. -/// - rhs: The right-hand side matrix. -/// - Returns: The product of the two matrices. -@inlinable -public func * (lhs: Mat3x3f, rhs: Mat3x3f) -> Mat3x3f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 3x3 float matrix by a scalar. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The matrix. -/// - Returns: The resulting matrix. -@inlinable -public func * (lhs: Float, rhs: Mat3x3f) -> Mat3x3f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 3D float vector by a 3x3 float matrix. -/// - Parameters: -/// - lhs: The vector. -/// - rhs: The matrix. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Vec3f, rhs: Mat3x3f) -> Vec3f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 3x3 float matrix by a 3D float vector. -/// - Parameters: -/// - lhs: The matrix. -/// - rhs: The vector. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Mat3x3f, rhs: Vec3f) -> Vec3f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies two 3x3 float matrices and assigns the result to the first matrix. -/// - Parameters: -/// - lhs: The left-hand side matrix to be updated. -/// - rhs: The right-hand side matrix. -@inlinable -public func *= (lhs: inout Mat3x3f, rhs: Mat3x3f) { - lhs = FirebladeMath.multiply(lhs, rhs) -} - -// MARK: 3x3d - -/// Multiplies two 3x3 double matrices. -/// - Parameters: -/// - lhs: The left-hand side matrix. -/// - rhs: The right-hand side matrix. -/// - Returns: The product of the two matrices. -@inlinable -public func * (lhs: Mat3x3d, rhs: Mat3x3d) -> Mat3x3d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 3x3 double matrix by a scalar. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The matrix. -/// - Returns: The resulting matrix. -@inlinable -public func * (lhs: Double, rhs: Mat3x3d) -> Mat3x3d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 3D double vector by a 3x3 double matrix. -/// - Parameters: -/// - lhs: The vector. -/// - rhs: The matrix. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Vec3d, rhs: Mat3x3d) -> Vec3d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 3x3 double matrix by a 3D double vector. -/// - Parameters: -/// - lhs: The matrix. -/// - rhs: The vector. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Mat3x3d, rhs: Vec3d) -> Vec3d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies two 3x3 double matrices and assigns the result to the first matrix. -/// - Parameters: -/// - lhs: The left-hand side matrix to be updated. -/// - rhs: The right-hand side matrix. -@inlinable -public func *= (lhs: inout Mat3x3d, rhs: Mat3x3d) { - lhs = FirebladeMath.multiply(lhs, rhs) -} - -// MARK: 2x2f - -/// Multiplies two 2x2 float matrices. -/// - Parameters: -/// - lhs: The left-hand side matrix. -/// - rhs: The right-hand side matrix. -/// - Returns: The product of the two matrices. -@inlinable -public func * (lhs: Mat2x2f, rhs: Mat2x2f) -> Mat2x2f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 2x2 float matrix by a scalar. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The matrix. -/// - Returns: The resulting matrix. -@inlinable -public func * (lhs: Float, rhs: Mat2x2f) -> Mat2x2f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 2D float vector by a 2x2 float matrix. -/// - Parameters: -/// - lhs: The vector. -/// - rhs: The matrix. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Vec2f, rhs: Mat2x2f) -> Vec2f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 2x2 float matrix by a 2D float vector. -/// - Parameters: -/// - lhs: The matrix. -/// - rhs: The vector. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Mat2x2f, rhs: Vec2f) -> Vec2f { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies two 2x2 float matrices and assigns the result to the first matrix. -/// - Parameters: -/// - lhs: The left-hand side matrix to be updated. -/// - rhs: The right-hand side matrix. -@inlinable -public func *= (lhs: inout Mat2x2f, rhs: Mat2x2f) { - lhs = FirebladeMath.multiply(lhs, rhs) -} - -// MARK: 2x2d - -/// Multiplies two 2x2 double matrices. -/// - Parameters: -/// - lhs: The left-hand side matrix. -/// - rhs: The right-hand side matrix. -/// - Returns: The product of the two matrices. -@inlinable -public func * (lhs: Mat2x2d, rhs: Mat2x2d) -> Mat2x2d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 2x2 double matrix by a scalar. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The matrix. -/// - Returns: The resulting matrix. -@inlinable -public func * (lhs: Double, rhs: Mat2x2d) -> Mat2x2d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 2D double vector by a 2x2 double matrix. -/// - Parameters: -/// - lhs: The vector. -/// - rhs: The matrix. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Vec2d, rhs: Mat2x2d) -> Vec2d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies a 2x2 double matrix by a 2D double vector. -/// - Parameters: -/// - lhs: The matrix. -/// - rhs: The vector. -/// - Returns: The resulting vector. -@inlinable -public func * (lhs: Mat2x2d, rhs: Vec2d) -> Vec2d { - FirebladeMath.multiply(lhs, rhs) -} - -/// Multiplies two 2x2 double matrices and assigns the result to the first matrix. -/// - Parameters: -/// - lhs: The left-hand side matrix to be updated. -/// - rhs: The right-hand side matrix. -@inlinable -public func *= (lhs: inout Mat2x2d, rhs: Mat2x2d) { - lhs = FirebladeMath.multiply(lhs, rhs) +// MARK: - 2x2d + +extension Mat2x2d { + /// Multiplies two 2x2 double matrices. + /// - Parameters: + /// - lhs: The left-hand side matrix. + /// - rhs: The right-hand side matrix. + /// - Returns: The product of the two matrices. + @inlinable + public static func * (lhs: Mat2x2d, rhs: Mat2x2d) -> Mat2x2d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 2x2 double matrix by a scalar. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The matrix. + /// - Returns: The resulting matrix. + @inlinable + public static func * (lhs: Double, rhs: Mat2x2d) -> Mat2x2d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 2D double vector by a 2x2 double matrix. + /// - Parameters: + /// - lhs: The vector. + /// - rhs: The matrix. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Vec2d, rhs: Mat2x2d) -> Vec2d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies a 2x2 double matrix by a 2D double vector. + /// - Parameters: + /// - lhs: The matrix. + /// - rhs: The vector. + /// - Returns: The resulting vector. + @inlinable + public static func * (lhs: Mat2x2d, rhs: Vec2d) -> Vec2d { + FirebladeMath.multiply(lhs, rhs) + } + + /// Multiplies two 2x2 double matrices and assigns the result to the first matrix. + /// - Parameters: + /// - lhs: The left-hand side matrix to be updated. + /// - rhs: The right-hand side matrix. + @inlinable + public static func *= (lhs: inout Mat2x2d, rhs: Mat2x2d) { + lhs = FirebladeMath.multiply(lhs, rhs) + } } diff --git a/Sources/FirebladeMath/Matrix/Matrix2x2.swift b/Sources/FirebladeMath/Matrix/Matrix2x2.swift index 84c8621..13fbcdd 100644 --- a/Sources/FirebladeMath/Matrix/Matrix2x2.swift +++ b/Sources/FirebladeMath/Matrix/Matrix2x2.swift @@ -33,13 +33,13 @@ public struct Matrix2x2: RandomAccessCollection, Mu storage.index(before: i) } - @usableFromInline init(storage: Storage) { + @usableFromInline + init(storage: Storage) { self.storage = storage } /// The 2x2 identity matrix. - @inlinable - public static var identity: Matrix2x2 { + @inlinable public static var identity: Matrix2x2 { Matrix2x2(diagonal: Vector(repeating: 1)) } @@ -109,7 +109,8 @@ public struct Matrix2x2: RandomAccessCollection, Mu } /// Provides access to the underlying memory as a contiguous buffer. - @inlinable public func withForcedContiguousStorage(_ body: (UnsafeBufferPointer) -> R) throws -> R? { + @inlinable + public func withForcedContiguousStorage(_ body: (UnsafeBufferPointer) -> R) throws -> R? { // https://forums.swift.org/t/se-0256-introduce-mutable-contiguouscollection-protocol/22569/7 if let result = withContiguousStorageIfAvailable(body) { return result @@ -119,7 +120,8 @@ public struct Matrix2x2: RandomAccessCollection, Mu } /// Provides mutable access to the underlying memory as a contiguous buffer. - @inlinable public mutating func withForcedContiguousMutableStorage(_ body: (inout UnsafeMutableBufferPointer) -> R) throws -> R? { + @inlinable + public mutating func withForcedContiguousMutableStorage(_ body: (inout UnsafeMutableBufferPointer) -> R) throws -> R? { // https://forums.swift.org/t/se-0256-introduce-mutable-contiguouscollection-protocol/22569/7 if let result = withContiguousMutableStorageIfAvailable(body) { return result diff --git a/Sources/FirebladeMath/Matrix/Matrix3x3.swift b/Sources/FirebladeMath/Matrix/Matrix3x3.swift index fc381e1..97dc27c 100644 --- a/Sources/FirebladeMath/Matrix/Matrix3x3.swift +++ b/Sources/FirebladeMath/Matrix/Matrix3x3.swift @@ -33,13 +33,13 @@ public struct Matrix3x3: RandomAccessCollection, Mu storage.index(before: i) } - @usableFromInline init(storage: Storage) { + @usableFromInline + init(storage: Storage) { self.storage = storage } /// The 3x3 identity matrix. - @inlinable - public static var identity: Matrix3x3 { + @inlinable public static var identity: Matrix3x3 { Matrix3x3(diagonal: Vector(repeating: 1)) } @@ -101,7 +101,7 @@ public struct Matrix3x3: RandomAccessCollection, Mu } /// The column vectors of the matrix. - @inlinable public var columns: (Vector, Vector, Vector) { + @inlinable public var columns: (Vector, Vector, Vector) { // swiftlint:disable:this large_tuple storage.columns } @@ -111,7 +111,8 @@ public struct Matrix3x3: RandomAccessCollection, Mu } /// Provides access to the underlying memory as a contiguous buffer. - @inlinable public func withForcedContiguousStorage(_ body: (UnsafeBufferPointer) -> R) throws -> R? { + @inlinable + public func withForcedContiguousStorage(_ body: (UnsafeBufferPointer) -> R) throws -> R? { // https://forums.swift.org/t/se-0256-introduce-mutable-contiguouscollection-protocol/22569/7 if let result = withContiguousStorageIfAvailable(body) { return result @@ -121,7 +122,8 @@ public struct Matrix3x3: RandomAccessCollection, Mu } /// Provides mutable access to the underlying memory as a contiguous buffer. - @inlinable public mutating func withForcedContiguousMutableStorage(_ body: (inout UnsafeMutableBufferPointer) -> R) throws -> R? { + @inlinable + public mutating func withForcedContiguousMutableStorage(_ body: (inout UnsafeMutableBufferPointer) -> R) throws -> R? { // https://forums.swift.org/t/se-0256-introduce-mutable-contiguouscollection-protocol/22569/7 if let result = withContiguousMutableStorageIfAvailable(body) { return result diff --git a/Sources/FirebladeMath/Matrix/Matrix4x4+Projections.swift b/Sources/FirebladeMath/Matrix/Matrix4x4+Projections.swift index a590c3e..a48eb51 100644 --- a/Sources/FirebladeMath/Matrix/Matrix4x4+Projections.swift +++ b/Sources/FirebladeMath/Matrix/Matrix4x4+Projections.swift @@ -66,7 +66,7 @@ extension Mat4x4f { /// - zNear: The distance to the near clipping plane. /// - zFar: The distance to the far clipping plane. /// - Returns: The orthographic projection matrix. - public static func orthographicRH(left: Float, right: Float, top: Float, bottom: Float, zNear: Float, zFar: Float) -> Self { + public static func orthographicRH(left: Float, right: Float, top: Float, bottom: Float, zNear: Float, zFar: Float) -> Self { // swiftlint:disable:this function_parameter_count let m00: Float = 2.0 / (right - left) let m03: Float = (left + right) / (left - right) let m11: Float = 2.0 / (top - bottom) @@ -92,7 +92,7 @@ extension Mat4x4f { /// - zNear: The distance to the near clipping plane. /// - zFar: The distance to the far clipping plane. /// - Returns: The orthographic projection matrix. - public static func orthographicLH(left: Float, right: Float, top: Float, bottom: Float, zNear: Float, zFar: Float) -> Self { + public static func orthographicLH(left: Float, right: Float, top: Float, bottom: Float, zNear: Float, zFar: Float) -> Self { // swiftlint:disable:this function_parameter_count let m00: Float = 2.0 / (right - left) let m03: Float = (left + right) / (left - right) let m11: Float = 2.0 / (top - bottom) @@ -174,7 +174,7 @@ extension Mat4x4d { /// - zNear: The distance to the near clipping plane. /// - zFar: The distance to the far clipping plane. /// - Returns: The orthographic projection matrix. - public static func orthographicRH(left: Double, right: Double, top: Double, bottom: Double, zNear: Double, zFar: Double) -> Self { + public static func orthographicRH(left: Double, right: Double, top: Double, bottom: Double, zNear: Double, zFar: Double) -> Self { // swiftlint:disable:this function_parameter_count let m00: Double = 2.0 / (right - left) let m03: Double = (left + right) / (left - right) let m11: Double = 2.0 / (top - bottom) @@ -200,7 +200,7 @@ extension Mat4x4d { /// - zNear: The distance to the near clipping plane. /// - zFar: The distance to the far clipping plane. /// - Returns: The orthographic projection matrix. - public static func orthographicLH(left: Double, right: Double, top: Double, bottom: Double, zNear: Double, zFar: Double) -> Self { + public static func orthographicLH(left: Double, right: Double, top: Double, bottom: Double, zNear: Double, zFar: Double) -> Self { // swiftlint:disable:this function_parameter_count let m00: Double = 2.0 / (right - left) let m03: Double = (left + right) / (left - right) let m11: Double = 2.0 / (top - bottom) diff --git a/Sources/FirebladeMath/Matrix/Matrix4x4.swift b/Sources/FirebladeMath/Matrix/Matrix4x4.swift index c840227..d992223 100644 --- a/Sources/FirebladeMath/Matrix/Matrix4x4.swift +++ b/Sources/FirebladeMath/Matrix/Matrix4x4.swift @@ -33,13 +33,13 @@ public struct Matrix4x4: RandomAccessCollection, Mu storage.index(before: i) } - @usableFromInline init(storage: Storage) { + @usableFromInline + init(storage: Storage) { self.storage = storage } /// The 4x4 identity matrix. - @inlinable - public static var identity: Matrix4x4 { + @inlinable public static var identity: Matrix4x4 { Matrix4x4(diagonal: Vector(repeating: 1)) } @@ -102,9 +102,8 @@ public struct Matrix4x4: RandomAccessCollection, Mu storage.replaceSubrange(subrange, with: newElements) } - // swiftlint:disable large_tuple /// The column vectors of the matrix. - @inlinable public var columns: (Vector, Vector, Vector, Vector) { + @inlinable public var columns: (Vector, Vector, Vector, Vector) { // swiftlint:disable:this large_tuple storage.columns } @@ -114,7 +113,8 @@ public struct Matrix4x4: RandomAccessCollection, Mu } /// Provides access to the underlying memory as a contiguous buffer. - @inlinable public func withForcedContiguousStorage(_ body: (UnsafeBufferPointer) -> R) throws -> R? { + @inlinable + public func withForcedContiguousStorage(_ body: (UnsafeBufferPointer) -> R) throws -> R? { // https://forums.swift.org/t/se-0256-introduce-mutable-contiguouscollection-protocol/22569/7 if let result = withContiguousStorageIfAvailable(body) { return result @@ -124,7 +124,8 @@ public struct Matrix4x4: RandomAccessCollection, Mu } /// Provides mutable access to the underlying memory as a contiguous buffer. - @inlinable public mutating func withForcedContiguousMutableStorage(_ body: (inout UnsafeMutableBufferPointer) -> R) throws -> R? { + @inlinable + public mutating func withForcedContiguousMutableStorage(_ body: (inout UnsafeMutableBufferPointer) -> R) throws -> R? { // https://forums.swift.org/t/se-0256-introduce-mutable-contiguouscollection-protocol/22569/7 if let result = withContiguousMutableStorageIfAvailable(body) { return result diff --git a/Sources/FirebladeMath/Matrix/MatrixStorage+NO_SIMD.swift b/Sources/FirebladeMath/Matrix/MatrixStorage+NO_SIMD.swift index 8b56f31..f4480f3 100644 --- a/Sources/FirebladeMath/Matrix/MatrixStorage+NO_SIMD.swift +++ b/Sources/FirebladeMath/Matrix/MatrixStorage+NO_SIMD.swift @@ -97,6 +97,7 @@ public struct Storage3x3: Storage3x3Protocol { @usableFromInline var column1: Column @usableFromInline var column2: Column + // swiftlint:disable:next large_tuple @inlinable public var columns: (Column, Column, Column) { (column0, column1, column2) } @@ -189,14 +190,14 @@ public struct Storage4x4: Storage4x4Protocol { public typealias Storage2x2Ref = Storage2x2 public typealias Storage3x3Ref = Storage3x3 - // TODO: we could use SIMD16 here + // NOTE: we could use SIMD16 here @usableFromInline var column0: Column @usableFromInline var column1: Column @usableFromInline var column2: Column @usableFromInline var column3: Column - // swiftlint:disable large_tuple + // swiftlint:disable:next large_tuple @inlinable public var columns: (Column, Column, Column, Column) { (column0, column1, column2, column3) } diff --git a/Sources/FirebladeMath/Matrix/MatrixStorage+SIMD.swift b/Sources/FirebladeMath/Matrix/MatrixStorage+SIMD.swift index c470fad..8cb7a08 100644 --- a/Sources/FirebladeMath/Matrix/MatrixStorage+SIMD.swift +++ b/Sources/FirebladeMath/Matrix/MatrixStorage+SIMD.swift @@ -11,8 +11,7 @@ import struct simd.matrix.simd_float4x4 /// Conformance to matrix storage protocols. extension simd_float2x2: Storage2x2Protocol, @retroactive @unchecked Sendable, @retroactive RandomAccessCollection, @retroactive MutableCollection, @retroactive RangeReplaceableCollection, @retroactive BidirectionalCollection, @retroactive Collection, - @retroactive Sequence -{ + @retroactive Sequence { /// The element type of the matrix storage. public typealias Element = Float /// The column vector type of the matrix. @@ -59,8 +58,7 @@ extension simd_float2x2: Storage2x2Protocol, @retroactive @unchecked Sendable, @ /// Conformance to matrix storage protocols. extension simd_double2x2: Storage2x2Protocol, @retroactive @unchecked Sendable, @retroactive RandomAccessCollection, @retroactive MutableCollection, @retroactive RangeReplaceableCollection, @retroactive BidirectionalCollection, @retroactive Collection, - @retroactive Sequence -{ + @retroactive Sequence { /// The element type of the matrix storage. public typealias Element = Double /// The column vector type of the matrix. @@ -109,8 +107,7 @@ extension simd_double2x2: Storage2x2Protocol, @retroactive @unchecked Sendable, /// Conformance to matrix storage protocols. extension simd_float3x3: Storage3x3Protocol, @retroactive @unchecked Sendable, @retroactive RandomAccessCollection, @retroactive MutableCollection, @retroactive RangeReplaceableCollection, @retroactive BidirectionalCollection, @retroactive Collection, - @retroactive Sequence -{ + @retroactive Sequence { /// The element type of the matrix storage. public typealias Element = Float /// The column vector type of the matrix. @@ -163,8 +160,7 @@ extension simd_float3x3: Storage3x3Protocol, @retroactive @unchecked Sendable, @ /// Conformance to matrix storage protocols. extension simd_double3x3: Storage3x3Protocol, @retroactive @unchecked Sendable, @retroactive RandomAccessCollection, @retroactive MutableCollection, @retroactive RangeReplaceableCollection, @retroactive BidirectionalCollection, @retroactive Collection, - @retroactive Sequence -{ + @retroactive Sequence { /// The element type of the matrix storage. public typealias Element = Double /// The column vector type of the matrix. @@ -219,8 +215,7 @@ extension simd_double3x3: Storage3x3Protocol, @retroactive @unchecked Sendable, /// Conformance to matrix storage protocols. extension simd_float4x4: Storage4x4Protocol, @retroactive @unchecked Sendable, @retroactive RandomAccessCollection, @retroactive MutableCollection, @retroactive RangeReplaceableCollection, @retroactive BidirectionalCollection, @retroactive Collection, - @retroactive Sequence -{ + @retroactive Sequence { /// The element type of the matrix storage. public typealias Element = Float /// The column vector type of the matrix. @@ -279,8 +274,7 @@ extension simd_float4x4: Storage4x4Protocol, @retroactive @unchecked Sendable, @ /// Conformance to matrix storage protocols. extension simd_double4x4: Storage4x4Protocol, @retroactive @unchecked Sendable, @retroactive RandomAccessCollection, @retroactive MutableCollection, @retroactive RangeReplaceableCollection, @retroactive BidirectionalCollection, @retroactive Collection, - @retroactive Sequence -{ + @retroactive Sequence { /// The element type of the matrix storage. public typealias Element = Double /// The column vector type of the matrix. diff --git a/Sources/FirebladeMath/Matrix/MatrixStorage.swift b/Sources/FirebladeMath/Matrix/MatrixStorage.swift index beef11e..c5cc463 100644 --- a/Sources/FirebladeMath/Matrix/MatrixStorage.swift +++ b/Sources/FirebladeMath/Matrix/MatrixStorage.swift @@ -11,9 +11,8 @@ public protocol Storage4x4Protocol: RandomAccessCollection, MutableCollection, R /// Creates a new storage with the specified diagonal values. init(diagonal: Column) - // swiftlint:disable large_tuple /// The column vectors of the matrix. - var columns: (Column, Column, Column, Column) { get } + var columns: (Column, Column, Column, Column) { get } // swiftlint:disable:this large_tuple /// Accesses the value at the specified column and row. subscript(column: Int, row: Int) -> Value { get set } @@ -65,7 +64,7 @@ public protocol Storage3x3Protocol: RandomAccessCollection, MutableCollection, R init(diagonal: Column) /// The column vectors of the matrix. - var columns: (Column, Column, Column) { get } + var columns: (Column, Column, Column) { get } // swiftlint:disable:this large_tuple /// Accesses the value at the specified column and row. subscript(column: Int, row: Int) -> Value { get set } diff --git a/Sources/FirebladeMath/Quat/Quat.swift b/Sources/FirebladeMath/Quat/Quat.swift index c297140..69bf7dc 100644 --- a/Sources/FirebladeMath/Quat/Quat.swift +++ b/Sources/FirebladeMath/Quat/Quat.swift @@ -5,6 +5,7 @@ public struct Quaternion: Sendable { /// The scalar type used in the quaternion. public typealias Value = Storage.Value + @usableFromInline var storage: Storage /// Creates a quaternion from a storage object. diff --git a/Sources/FirebladeMath/Quat/Quat4d.swift b/Sources/FirebladeMath/Quat/Quat4d.swift index 89b9c37..a64d0d1 100644 --- a/Sources/FirebladeMath/Quat/Quat4d.swift +++ b/Sources/FirebladeMath/Quat/Quat4d.swift @@ -1,7 +1,8 @@ extension Quat4d { /// Creates a double-precision quaternion from a single-precision quaternion. /// - Parameter quat: The single-precision quaternion. - @inlinable public init(_ quat: Quat4f) { + @inlinable + public init(_ quat: Quat4f) { self.init(Double(quat.x), Double(quat.y), Double(quat.z), Double(quat.w)) } diff --git a/Sources/FirebladeMath/Quat/Quat4f+Euler.swift b/Sources/FirebladeMath/Quat/Quat4f+Euler.swift index 65e60c1..2043d62 100644 --- a/Sources/FirebladeMath/Quat/Quat4f+Euler.swift +++ b/Sources/FirebladeMath/Quat/Quat4f+Euler.swift @@ -12,13 +12,13 @@ extension Quat4f { // return Quat4f(q1, q2, q3, q4) // } - public static func fromEulerAngles_123(_ e: Vec3f) -> Quat4f { - let c1 = cos(e.x / 2.0) - let s1 = sin(e.x / 2.0) - let c2 = cos(e.y / 2.0) - let s2 = sin(e.y / 2.0) - let c3 = cos(e.z / 2.0) - let s3 = sin(e.z / 2.0) + public static func fromEulerAngles_123(_ euler: Vec3f) -> Quat4f { + let c1 = cos(euler.x / 2.0) + let s1 = sin(euler.x / 2.0) + let c2 = cos(euler.y / 2.0) + let s2 = sin(euler.y / 2.0) + let c3 = cos(euler.z / 2.0) + let s3 = sin(euler.z / 2.0) let q4 = c1 * c2 * c3 - s1 * s2 * s3 let q3 = s1 * c2 * c3 + c1 * s2 * s3 @@ -41,13 +41,13 @@ extension Quat4f { // return Quat4f(q1, q2, q3, q4) // } - public static func fromEulerAngles_132(_ e: Vec3f) -> Quat4f { - let c1 = cos(e.x / 2.0) - let s1 = sin(e.x / 2.0) - let c2 = cos(e.y / 2.0) - let s2 = sin(e.y / 2.0) - let c3 = cos(e.z / 2.0) - let s3 = sin(e.z / 2.0) + public static func fromEulerAngles_132(_ euler: Vec3f) -> Quat4f { + let c1 = cos(euler.x / 2.0) + let s1 = sin(euler.x / 2.0) + let c2 = cos(euler.y / 2.0) + let s2 = sin(euler.y / 2.0) + let c3 = cos(euler.z / 2.0) + let s3 = sin(euler.z / 2.0) let q4 = c1 * c2 * c3 + s1 * s2 * s3 let q3 = s1 * c2 * c3 - c1 * s2 * s3 @@ -70,13 +70,13 @@ extension Quat4f { // return Quat4f(q1, q2, q3, q4) // } - public static func fromEulerAngles_213(_ e: Vec3f) -> Quat4f { - let c1 = cos(e.x / 2.0) - let s1 = sin(e.x / 2.0) - let c2 = cos(e.y / 2.0) - let s2 = sin(e.y / 2.0) - let c3 = cos(e.z / 2.0) - let s3 = sin(e.z / 2.0) + public static func fromEulerAngles_213(_ euler: Vec3f) -> Quat4f { + let c1 = cos(euler.x / 2.0) + let s1 = sin(euler.x / 2.0) + let c2 = cos(euler.y / 2.0) + let s2 = sin(euler.y / 2.0) + let c3 = cos(euler.z / 2.0) + let s3 = sin(euler.z / 2.0) let q4 = c1 * c2 * c3 + s1 * s2 * s3 let q3 = c1 * s2 * c3 + s1 * c2 * s3 @@ -86,13 +86,13 @@ extension Quat4f { return Quat4f(q1, q2, q3, q4) } - public static func fromEulerAngles_231(_ e: Vec3f) -> Quat4f { - let c1 = cos(e.x / 2.0) - let s1 = sin(e.x / 2.0) - let c2 = cos(e.y / 2.0) - let s2 = sin(e.y / 2.0) - let c3 = cos(e.z / 2.0) - let s3 = sin(e.z / 2.0) + public static func fromEulerAngles_231(_ euler: Vec3f) -> Quat4f { + let c1 = cos(euler.x / 2.0) + let s1 = sin(euler.x / 2.0) + let c2 = cos(euler.y / 2.0) + let s2 = sin(euler.y / 2.0) + let c3 = cos(euler.z / 2.0) + let s3 = sin(euler.z / 2.0) let q4 = c1 * c2 * c3 - s1 * s2 * s3 let q3 = c1 * c2 * s3 + s1 * s2 * c3 @@ -115,13 +115,13 @@ extension Quat4f { // return Quat4f(q1, q2, q3, q4) // } - public static func fromEulerAngles_312(_ e: Vec3f) -> Quat4f { - let c1 = cos(e.x / 2.0) - let s1 = sin(e.x / 2.0) - let c2 = cos(e.y / 2.0) - let s2 = sin(e.y / 2.0) - let c3 = cos(e.z / 2.0) - let s3 = sin(e.z / 2.0) + public static func fromEulerAngles_312(_ euler: Vec3f) -> Quat4f { + let c1 = cos(euler.x / 2.0) + let s1 = sin(euler.x / 2.0) + let c2 = cos(euler.y / 2.0) + let s2 = sin(euler.y / 2.0) + let c3 = cos(euler.z / 2.0) + let s3 = sin(euler.z / 2.0) let q4 = c1 * c2 * c3 - s1 * s2 * s3 let q3 = c1 * s2 * c3 - s1 * c2 * s3 @@ -144,13 +144,13 @@ extension Quat4f { // return Quat4f(q1, q2, q3, q4) // } - public static func fromEulerAngles_321(_ e: Vec3f) -> Quat4f { - let c1 = cos(e.x / 2.0) - let s1 = sin(e.x / 2.0) - let c2 = cos(e.y / 2.0) - let s2 = sin(e.y / 2.0) - let c3 = cos(e.z / 2.0) - let s3 = sin(e.z / 2.0) + public static func fromEulerAngles_321(_ euler: Vec3f) -> Quat4f { + let c1 = cos(euler.x / 2.0) + let s1 = sin(euler.x / 2.0) + let c2 = cos(euler.y / 2.0) + let s2 = sin(euler.y / 2.0) + let c3 = cos(euler.z / 2.0) + let s3 = sin(euler.z / 2.0) let q4 = c1 * c2 * c3 + s1 * s2 * s3 let q3 = c1 * c2 * s3 - s1 * s2 * c3 @@ -165,23 +165,19 @@ extension Quat4f { self = Quat4f.fromEulerAngles_321(Vec3f(pitch, yaw, roll)) } - @inlinable - public var eulerAngles: Vec3f { + @inlinable public var eulerAngles: Vec3f { quaternionToEulerAngles_321(self) } - @inlinable - public var pitch: Float { + @inlinable public var pitch: Float { eulerAngles.x } - @inlinable - public var yaw: Float { + @inlinable public var yaw: Float { eulerAngles.y } - @inlinable - public var roll: Float { + @inlinable public var roll: Float { eulerAngles.z } @@ -212,11 +208,11 @@ extension Quat4f { // return Vec3f(e1, e2, e3) // } -public func quaternionToEulerAngles_123(_ q: Quat4f) -> Vec3f { - let q0 = q.w - let q1 = q.z - let q2 = q.y - let q3 = q.x +public func quaternionToEulerAngles_123(_ quat: Quat4f) -> Vec3f { + let q0 = quat.w + let q1 = quat.z + let q2 = quat.y + let q3 = quat.x let e1 = atan2(-2.0 * (q2 * q3 - q0 * q1), q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3) let e2 = asin(2.0 * (q1 * q3 + q0 * q2)) @@ -235,11 +231,11 @@ public func quaternionToEulerAngles_123(_ q: Quat4f) -> Vec3f { // return Vec3f(e1, e2, e3) // } -public func quaternionToEulerAngles_132(_ q: Quat4f) -> Vec3f { - let q0 = q.w - let q1 = q.z - let q2 = q.y - let q3 = q.x +public func quaternionToEulerAngles_132(_ quat: Quat4f) -> Vec3f { + let q0 = quat.w + let q1 = quat.z + let q2 = quat.y + let q3 = quat.x let e1 = atan2(2.0 * (q2 * q3 + q0 * q1), q0 * q0 - q1 * q1 + q2 * q2 - q3 * q3) let e2 = asin(-2.0 * (q1 * q2 - q0 * q3)) @@ -259,11 +255,11 @@ public func quaternionToEulerAngles_132(_ q: Quat4f) -> Vec3f { // return Vec3f(e1, e2, e3) // } -public func quaternionToEulerAngles_213(_ q: Quat4f) -> Vec3f { - let q0 = q.w - let q1 = q.z - let q2 = q.y - let q3 = q.x +public func quaternionToEulerAngles_213(_ quat: Quat4f) -> Vec3f { + let q0 = quat.w + let q1 = quat.z + let q2 = quat.y + let q3 = quat.x let e1 = atan2(2.0 * (q1 * q3 + q0 * q2), q0 * q0 - q1 * q1 - q2 * q2 + q3 * q3) let e2 = asin(-2.0 * (q2 * q3 - q0 * q1)) @@ -272,11 +268,11 @@ public func quaternionToEulerAngles_213(_ q: Quat4f) -> Vec3f { return Vec3f(e1, e2, e3) } -public func quaternionToEulerAngles_231(_ q: Quat4f) -> Vec3f { - let q0 = q.w - let q1 = q.z - let q2 = q.y - let q3 = q.x +public func quaternionToEulerAngles_231(_ quat: Quat4f) -> Vec3f { + let q0 = quat.w + let q1 = quat.z + let q2 = quat.y + let q3 = quat.x let e1 = atan2(-2.0 * (q1 * q3 - q0 * q2), q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3) let e2 = asin(2.0 * (q1 * q2 + q0 * q3)) @@ -296,11 +292,11 @@ public func quaternionToEulerAngles_231(_ q: Quat4f) -> Vec3f { // return Vec3f(e1, e2, e3) // } -public func quaternionToEulerAngles_312(_ q: Quat4f) -> Vec3f { - let q0 = q.w - let q1 = q.z - let q2 = q.y - let q3 = q.x +public func quaternionToEulerAngles_312(_ quat: Quat4f) -> Vec3f { + let q0 = quat.w + let q1 = quat.z + let q2 = quat.y + let q3 = quat.x let e1 = atan2(-2.0 * (q1 * q2 - q0 * q3), q0 * q0 - q1 * q1 + q2 * q2 - q3 * q3) let e2 = asin(2.0 * (q2 * q3 + q0 * q1)) @@ -320,11 +316,11 @@ public func quaternionToEulerAngles_312(_ q: Quat4f) -> Vec3f { // return Vec3f(e1, e2, e3) // } -public func quaternionToEulerAngles_321(_ q: Quat4f) -> Vec3f { - let q0 = q.w - let q1 = q.z - let q2 = q.y - let q3 = q.x +public func quaternionToEulerAngles_321(_ quat: Quat4f) -> Vec3f { + let q0 = quat.w + let q1 = quat.z + let q2 = quat.y + let q3 = quat.x let e1 = atan2(2.0 * (q1 * q2 + q0 * q3), q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3) let e2 = asin(-2.0 * (q1 * q3 - q0 * q2)) diff --git a/Sources/FirebladeMath/Quat/Quaternion+Operators.swift b/Sources/FirebladeMath/Quat/Quaternion+Operators.swift index fa79e63..4940e5b 100644 --- a/Sources/FirebladeMath/Quat/Quaternion+Operators.swift +++ b/Sources/FirebladeMath/Quat/Quaternion+Operators.swift @@ -1,137 +1,141 @@ -/// Multiplies a scalar by a quaternion. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The quaternion. -/// - Returns: The scaled quaternion. -@inlinable -public func * (lhs: Float, rhs: Quat4f) -> Quat4f { - FirebladeMath.multiply(lhs, rhs) -} +extension Quat4f { + /// Multiplies a scalar by a quaternion. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The quaternion. + /// - Returns: The scaled quaternion. + @inlinable + public static func * (lhs: Float, rhs: Quat4f) -> Quat4f { + FirebladeMath.multiply(lhs, rhs) + } -/// Multiplies a quaternion by a scalar. -/// - Parameters: -/// - lhs: The quaternion. -/// - rhs: The scalar value. -/// - Returns: The scaled quaternion. -@inlinable -public func * (lhs: Quat4f, rhs: Float) -> Quat4f { - FirebladeMath.multiply(lhs, rhs) -} + /// Multiplies a quaternion by a scalar. + /// - Parameters: + /// - lhs: The quaternion. + /// - rhs: The scalar value. + /// - Returns: The scaled quaternion. + @inlinable + public static func * (lhs: Quat4f, rhs: Float) -> Quat4f { + FirebladeMath.multiply(lhs, rhs) + } -/// Returns the product of two quaternions. -/// - Parameters: -/// - lhs: The left-hand side quaternion. -/// - rhs: The right-hand side quaternion. -/// - Returns: The product of the two quaternions. -@inlinable -public func * (lhs: Quat4f, rhs: Quat4f) -> Quat4f { - FirebladeMath.multiply(lhs, rhs) -} + /// Returns the product of two quaternions. + /// - Parameters: + /// - lhs: The left-hand side quaternion. + /// - rhs: The right-hand side quaternion. + /// - Returns: The product of the two quaternions. + @inlinable + public static func * (lhs: Quat4f, rhs: Quat4f) -> Quat4f { + FirebladeMath.multiply(lhs, rhs) + } -/// Returns a vector rotated by a quaternion. -/// - Parameters: -/// - lhs: The quaternion. -/// - rhs: The vector to rotate. -/// - Returns: The rotated vector. -@inlinable -public func * (lhs: Quat4f, rhs: Vec3f) -> Vec3f { - FirebladeMath.act(lhs, rhs) -} + /// Returns a vector rotated by a quaternion. + /// - Parameters: + /// - lhs: The quaternion. + /// - rhs: The vector to rotate. + /// - Returns: The rotated vector. + @inlinable + public static func * (lhs: Quat4f, rhs: Vec3f) -> Vec3f { + FirebladeMath.act(lhs, rhs) + } -/// Multiplies two quaternions and assigns the result to the left-hand side. -/// - Parameters: -/// - lhs: The left-hand side quaternion to be modified. -/// - rhs: The right-hand side quaternion. -@inlinable -public func *= (lhs: inout Quat4f, rhs: Quat4f) { - lhs = FirebladeMath.multiply(lhs, rhs) -} + /// Multiplies two quaternions and assigns the result to the left-hand side. + /// - Parameters: + /// - lhs: The left-hand side quaternion to be modified. + /// - rhs: The right-hand side quaternion. + @inlinable + public static func *= (lhs: inout Quat4f, rhs: Quat4f) { + lhs = FirebladeMath.multiply(lhs, rhs) + } -/// Adds two quaternions component-wise. -/// - Parameters: -/// - lhs: The left-hand side quaternion. -/// - rhs: The right-hand side quaternion. -/// - Returns: The sum of the two quaternions. -@inlinable -public func + (lhs: Quat4f, rhs: Quat4f) -> Quat4f { - FirebladeMath.add(lhs, rhs) -} + /// Adds two quaternions component-wise. + /// - Parameters: + /// - lhs: The left-hand side quaternion. + /// - rhs: The right-hand side quaternion. + /// - Returns: The sum of the two quaternions. + @inlinable + public static func + (lhs: Quat4f, rhs: Quat4f) -> Quat4f { + FirebladeMath.add(lhs, rhs) + } -/// Subtracts the right-hand side quaternion from the left-hand side quaternion component-wise. -/// - Parameters: -/// - lhs: The left-hand side quaternion. -/// - rhs: The right-hand side quaternion. -/// - Returns: The difference of the two quaternions. -@inlinable -public func - (lhs: Quat4f, rhs: Quat4f) -> Quat4f { - FirebladeMath.subtract(lhs, rhs) + /// Subtracts the right-hand side quaternion from the left-hand side quaternion component-wise. + /// - Parameters: + /// - lhs: The left-hand side quaternion. + /// - rhs: The right-hand side quaternion. + /// - Returns: The difference of the two quaternions. + @inlinable + public static func - (lhs: Quat4f, rhs: Quat4f) -> Quat4f { + FirebladeMath.subtract(lhs, rhs) + } } -/// Multiplies a scalar by a quaternion. -/// - Parameters: -/// - lhs: The scalar value. -/// - rhs: The quaternion. -/// - Returns: The scaled quaternion. -@inlinable -public func * (lhs: Double, rhs: Quat4d) -> Quat4d { - FirebladeMath.multiply(lhs, rhs) -} +extension Quat4d { + /// Multiplies a scalar by a quaternion. + /// - Parameters: + /// - lhs: The scalar value. + /// - rhs: The quaternion. + /// - Returns: The scaled quaternion. + @inlinable + public static func * (lhs: Double, rhs: Quat4d) -> Quat4d { + FirebladeMath.multiply(lhs, rhs) + } -/// Multiplies a quaternion by a scalar. -/// - Parameters: -/// - lhs: The quaternion. -/// - rhs: The scalar value. -/// - Returns: The scaled quaternion. -@inlinable -public func * (lhs: Quat4d, rhs: Double) -> Quat4d { - FirebladeMath.multiply(lhs, rhs) -} + /// Multiplies a quaternion by a scalar. + /// - Parameters: + /// - lhs: The quaternion. + /// - rhs: The scalar value. + /// - Returns: The scaled quaternion. + @inlinable + public static func * (lhs: Quat4d, rhs: Double) -> Quat4d { + FirebladeMath.multiply(lhs, rhs) + } -/// Returns the product of two quaternions. -/// - Parameters: -/// - lhs: The left-hand side quaternion. -/// - rhs: The right-hand side quaternion. -/// - Returns: The product of the two quaternions. -@inlinable -public func * (lhs: Quat4d, rhs: Quat4d) -> Quat4d { - FirebladeMath.multiply(lhs, rhs) -} + /// Returns the product of two quaternions. + /// - Parameters: + /// - lhs: The left-hand side quaternion. + /// - rhs: The right-hand side quaternion. + /// - Returns: The product of the two quaternions. + @inlinable + public static func * (lhs: Quat4d, rhs: Quat4d) -> Quat4d { + FirebladeMath.multiply(lhs, rhs) + } -/// Returns a vector rotated by a quaternion. -/// - Parameters: -/// - lhs: The quaternion. -/// - rhs: The vector to rotate. -/// - Returns: The rotated vector. -@inlinable -public func * (lhs: Quat4d, rhs: Vec3d) -> Vec3d { - FirebladeMath.act(lhs, rhs) -} + /// Returns a vector rotated by a quaternion. + /// - Parameters: + /// - lhs: The quaternion. + /// - rhs: The vector to rotate. + /// - Returns: The rotated vector. + @inlinable + public static func * (lhs: Quat4d, rhs: Vec3d) -> Vec3d { + FirebladeMath.act(lhs, rhs) + } -/// Multiplies two quaternions and assigns the result to the left-hand side. -/// - Parameters: -/// - lhs: The left-hand side quaternion to be modified. -/// - rhs: The right-hand side quaternion. -@inlinable -public func *= (lhs: inout Quat4d, rhs: Quat4d) { - lhs = FirebladeMath.multiply(lhs, rhs) -} + /// Multiplies two quaternions and assigns the result to the left-hand side. + /// - Parameters: + /// - lhs: The left-hand side quaternion to be modified. + /// - rhs: The right-hand side quaternion. + @inlinable + public static func *= (lhs: inout Quat4d, rhs: Quat4d) { + lhs = FirebladeMath.multiply(lhs, rhs) + } -/// Adds two quaternions component-wise. -/// - Parameters: -/// - lhs: The left-hand side quaternion. -/// - rhs: The right-hand side quaternion. -/// - Returns: The sum of the two quaternions. -@inlinable -public func + (lhs: Quat4d, rhs: Quat4d) -> Quat4d { - FirebladeMath.add(lhs, rhs) -} + /// Adds two quaternions component-wise. + /// - Parameters: + /// - lhs: The left-hand side quaternion. + /// - rhs: The right-hand side quaternion. + /// - Returns: The sum of the two quaternions. + @inlinable + public static func + (lhs: Quat4d, rhs: Quat4d) -> Quat4d { + FirebladeMath.add(lhs, rhs) + } -/// Subtracts the right-hand side quaternion from the left-hand side quaternion component-wise. -/// - Parameters: -/// - lhs: The left-hand side quaternion. -/// - rhs: The right-hand side quaternion. -/// - Returns: The difference of the two quaternions. -@inlinable -public func - (lhs: Quat4d, rhs: Quat4d) -> Quat4d { - FirebladeMath.subtract(lhs, rhs) + /// Subtracts the right-hand side quaternion from the left-hand side quaternion component-wise. + /// - Parameters: + /// - lhs: The left-hand side quaternion. + /// - rhs: The right-hand side quaternion. + /// - Returns: The difference of the two quaternions. + @inlinable + public static func - (lhs: Quat4d, rhs: Quat4d) -> Quat4d { + FirebladeMath.subtract(lhs, rhs) + } } diff --git a/Sources/FirebladeMath/Quat/QuaternionStorage+NO_SIMD.swift b/Sources/FirebladeMath/Quat/QuaternionStorage+NO_SIMD.swift index 74559e6..0107a4d 100644 --- a/Sources/FirebladeMath/Quat/QuaternionStorage+NO_SIMD.swift +++ b/Sources/FirebladeMath/Quat/QuaternionStorage+NO_SIMD.swift @@ -14,23 +14,23 @@ public struct QuaternionStorage: QuaternionStorageProtocol } @inlinable public var x: Value { - set { storage.x = newValue } get { storage.x } + set { storage.x = newValue } } @inlinable public var y: Value { - set { storage.y = newValue } get { storage.y } + set { storage.y = newValue } } @inlinable public var z: Value { - set { storage.z = newValue } get { storage.z } + set { storage.z = newValue } } @inlinable public var w: Value { - set { storage.w = newValue } get { storage.w } + set { storage.w = newValue } } public func makeIterator() -> SIMDScalarIterator> {