[Math] Sort out Vector3, make it conform to Vector protocol
This commit is contained in:
		
							parent
							
								
									84f7d6bc2c
								
							
						
					
					
						commit
						2b1e3648f4
					
				
					 2 changed files with 178 additions and 0 deletions
				
			
		| 
						 | 
				
			
			@ -10,6 +10,7 @@
 | 
			
		|||
		C005DFFE1BE1CBA700F1BD3C /* Math.h in Headers */ = {isa = PBXBuildFile; fileRef = C005DFFD1BE1CBA700F1BD3C /* Math.h */; settings = {ATTRIBUTES = (Public, ); }; };
 | 
			
		||||
		C005E0051BE1CBA800F1BD3C /* Math.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C005DFFA1BE1CBA700F1BD3C /* Math.framework */; };
 | 
			
		||||
		C005E00A1BE1CBA800F1BD3C /* MathTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C005E0091BE1CBA800F1BD3C /* MathTests.swift */; };
 | 
			
		||||
		C005E0391BE1CC7A00F1BD3C /* Matrices.swift in Sources */ = {isa = PBXBuildFile; fileRef = C005E0381BE1CC7A00F1BD3C /* Matrices.swift */; };
 | 
			
		||||
/* End PBXBuildFile section */
 | 
			
		||||
 | 
			
		||||
/* Begin PBXContainerItemProxy section */
 | 
			
		||||
| 
						 | 
				
			
			@ -29,6 +30,7 @@
 | 
			
		|||
		C005E0041BE1CBA800F1BD3C /* MathTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MathTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
 | 
			
		||||
		C005E0091BE1CBA800F1BD3C /* MathTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MathTests.swift; sourceTree = "<group>"; };
 | 
			
		||||
		C005E00B1BE1CBA800F1BD3C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
 | 
			
		||||
		C005E0381BE1CC7A00F1BD3C /* Matrices.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrices.swift; sourceTree = "<group>"; };
 | 
			
		||||
/* End PBXFileReference section */
 | 
			
		||||
 | 
			
		||||
/* Begin PBXFrameworksBuildPhase section */
 | 
			
		||||
| 
						 | 
				
			
			@ -72,6 +74,7 @@
 | 
			
		|||
			isa = PBXGroup;
 | 
			
		||||
			children = (
 | 
			
		||||
				C005DFFD1BE1CBA700F1BD3C /* Math.h */,
 | 
			
		||||
				C005E0381BE1CC7A00F1BD3C /* Matrices.swift */,
 | 
			
		||||
				C005DFFF1BE1CBA700F1BD3C /* Info.plist */,
 | 
			
		||||
			);
 | 
			
		||||
			path = Math;
 | 
			
		||||
| 
						 | 
				
			
			@ -194,6 +197,7 @@
 | 
			
		|||
			isa = PBXSourcesBuildPhase;
 | 
			
		||||
			buildActionMask = 2147483647;
 | 
			
		||||
			files = (
 | 
			
		||||
				C005E0391BE1CC7A00F1BD3C /* Matrices.swift in Sources */,
 | 
			
		||||
			);
 | 
			
		||||
			runOnlyForDeploymentPostprocessing = 0;
 | 
			
		||||
		};
 | 
			
		||||
| 
						 | 
				
			
			@ -305,6 +309,7 @@
 | 
			
		|||
		C005E00F1BE1CBA800F1BD3C /* Debug */ = {
 | 
			
		||||
			isa = XCBuildConfiguration;
 | 
			
		||||
			buildSettings = {
 | 
			
		||||
				CLANG_ENABLE_MODULES = YES;
 | 
			
		||||
				COMBINE_HIDPI_IMAGES = YES;
 | 
			
		||||
				DEFINES_MODULE = YES;
 | 
			
		||||
				DYLIB_COMPATIBILITY_VERSION = 1;
 | 
			
		||||
| 
						 | 
				
			
			@ -317,12 +322,14 @@
 | 
			
		|||
				PRODUCT_BUNDLE_IDENTIFIER = me.erynwells.graphics.Math;
 | 
			
		||||
				PRODUCT_NAME = "$(TARGET_NAME)";
 | 
			
		||||
				SKIP_INSTALL = YES;
 | 
			
		||||
				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
 | 
			
		||||
			};
 | 
			
		||||
			name = Debug;
 | 
			
		||||
		};
 | 
			
		||||
		C005E0101BE1CBA800F1BD3C /* Release */ = {
 | 
			
		||||
			isa = XCBuildConfiguration;
 | 
			
		||||
			buildSettings = {
 | 
			
		||||
				CLANG_ENABLE_MODULES = YES;
 | 
			
		||||
				COMBINE_HIDPI_IMAGES = YES;
 | 
			
		||||
				DEFINES_MODULE = YES;
 | 
			
		||||
				DYLIB_COMPATIBILITY_VERSION = 1;
 | 
			
		||||
| 
						 | 
				
			
			@ -379,6 +386,7 @@
 | 
			
		|||
				C005E0101BE1CBA800F1BD3C /* Release */,
 | 
			
		||||
			);
 | 
			
		||||
			defaultConfigurationIsVisible = 0;
 | 
			
		||||
			defaultConfigurationName = Release;
 | 
			
		||||
		};
 | 
			
		||||
		C005E0111BE1CBA800F1BD3C /* Build configuration list for PBXNativeTarget "MathTests" */ = {
 | 
			
		||||
			isa = XCConfigurationList;
 | 
			
		||||
| 
						 | 
				
			
			@ -387,6 +395,7 @@
 | 
			
		|||
				C005E0131BE1CBA800F1BD3C /* Release */,
 | 
			
		||||
			);
 | 
			
		||||
			defaultConfigurationIsVisible = 0;
 | 
			
		||||
			defaultConfigurationName = Release;
 | 
			
		||||
		};
 | 
			
		||||
/* End XCConfigurationList section */
 | 
			
		||||
	};
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										169
									
								
								Math/Math/Matrices.swift
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										169
									
								
								Math/Math/Matrices.swift
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,169 @@
 | 
			
		|||
//
 | 
			
		||||
//  Matrices.swift
 | 
			
		||||
//  Game
 | 
			
		||||
//
 | 
			
		||||
//  Created by Eryn Wells on 2015-10-24.
 | 
			
		||||
//  Copyright © 2015 Eryn Wells. All rights reserved.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
/** A square matrix. */
 | 
			
		||||
protocol Matrix {
 | 
			
		||||
    typealias ElementType
 | 
			
		||||
 | 
			
		||||
    static func dimension() -> Int
 | 
			
		||||
    static func size() -> Int
 | 
			
		||||
 | 
			
		||||
    init()
 | 
			
		||||
    init?(values: [ElementType])
 | 
			
		||||
 | 
			
		||||
    subscript(idx: Int) -> ElementType { get set }
 | 
			
		||||
    subscript(row: Int, col: Int) -> ElementType { get set }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** A vector. */
 | 
			
		||||
protocol Vector {
 | 
			
		||||
    init()
 | 
			
		||||
 | 
			
		||||
    /** Number of elements in the vector. */
 | 
			
		||||
    var count: Int { get }
 | 
			
		||||
    /** Element access by index. */
 | 
			
		||||
    subscript(idx: Int) -> Float { get set }
 | 
			
		||||
 | 
			
		||||
    /** Length of the vector. */
 | 
			
		||||
    var length2: Float { get }
 | 
			
		||||
    /** Length-squared of the vector. */
 | 
			
		||||
    var length: Float { get }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MARK: - Matrices
 | 
			
		||||
 | 
			
		||||
public struct Matrix4<T: FloatingPointType>: Matrix {
 | 
			
		||||
    static func dimension() -> Int {
 | 
			
		||||
        return 4
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static func size() -> Int {
 | 
			
		||||
        return dimension() * dimension()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public private(set) var data: [T]
 | 
			
		||||
 | 
			
		||||
    public init() {
 | 
			
		||||
        data = [T](count: Matrix4.size(), repeatedValue: T(0))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public init?(values: [T]) {
 | 
			
		||||
        guard values.count == Matrix4.size() else { return nil }
 | 
			
		||||
        data = values
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public subscript(idx: Int) -> T {
 | 
			
		||||
        get {
 | 
			
		||||
            return data[idx]
 | 
			
		||||
        }
 | 
			
		||||
        set(value) {
 | 
			
		||||
            data[idx] = value
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public subscript(row: Int, col: Int) -> T {
 | 
			
		||||
        get {
 | 
			
		||||
            return data[indexFromCoordinates(row, col)]
 | 
			
		||||
        }
 | 
			
		||||
        set(value) {
 | 
			
		||||
            data[indexFromCoordinates(row, col)] = value
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /** Convert a (row, col) pair into an index into the data array. */
 | 
			
		||||
    private func indexFromCoordinates(row: Int, _ col: Int) -> Int {
 | 
			
		||||
        return row * Matrix4.dimension() + col
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
struct Matrix3 {
 | 
			
		||||
    static let Rows = 3
 | 
			
		||||
    static let Cols = 3
 | 
			
		||||
    static let Size = Matrix3.Rows * Matrix3.Cols
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MARK: Vectors
 | 
			
		||||
 | 
			
		||||
public struct Vector4<T: FloatingPointType> {
 | 
			
		||||
    static func size() -> Int {
 | 
			
		||||
        return 4
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public private(set) var data: [T]
 | 
			
		||||
 | 
			
		||||
    init() {
 | 
			
		||||
        self.init(x: T(0), y: T(0), z: T(0), w: T(0))
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    init(x: T, y: T, z: T, w: T = T(1)) {
 | 
			
		||||
        data = [x, y, z, w]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    subscript(idx: Int) -> T {
 | 
			
		||||
        get {
 | 
			
		||||
            return data[idx]
 | 
			
		||||
        }
 | 
			
		||||
        set(value) {
 | 
			
		||||
            data[idx] = value
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
public struct Vector3: Vector {
 | 
			
		||||
    private var data: [Float]
 | 
			
		||||
 | 
			
		||||
    init() {
 | 
			
		||||
        self.init(x: 0.0, y: 0.0, z: 0.0)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    init(x: Float, y: Float, z: Float) {
 | 
			
		||||
        data = [x, y, z]
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var length: Float {
 | 
			
		||||
        return sqrtf(length2)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    var length2: Float {
 | 
			
		||||
        return data.reduce(0.0) { $0 + $1 * $1 }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // MARK: Sequence-ish
 | 
			
		||||
 | 
			
		||||
    var count: Int {
 | 
			
		||||
        return 3
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public subscript(idx: Int) -> Float {
 | 
			
		||||
        get {
 | 
			
		||||
            return data[idx]
 | 
			
		||||
        }
 | 
			
		||||
        set(value) {
 | 
			
		||||
            data[idx] = value
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MARK: - Matrix-Scalar Multiplication
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//func *<T: FloatingPointType>(matrix: Matrix4<T>, scalar: T) -> Matrix4<T> {
 | 
			
		||||
//    var out: Matrix4<T>
 | 
			
		||||
//    for i in 0..<out.data.count {
 | 
			
		||||
//        out[i] = matrix.data[i] * scalar
 | 
			
		||||
//    }
 | 
			
		||||
//    return out
 | 
			
		||||
//}
 | 
			
		||||
 | 
			
		||||
// MARK: - Matrix-Vector Multiplication
 | 
			
		||||
 | 
			
		||||
//func *<T: FloatingPointType>(matrix: Matrix4<T>, vector: Vector4<T>) -> Vector4<T> {
 | 
			
		||||
//    return Vector4<T>()
 | 
			
		||||
//}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue