From 2b1e3648f49c665bae6abfdb57033a9230d94f1d Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Wed, 28 Oct 2015 20:56:21 -0700 Subject: [PATCH] [Math] Sort out Vector3, make it conform to Vector protocol --- Math/Math.xcodeproj/project.pbxproj | 9 ++ Math/Math/Matrices.swift | 169 ++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 Math/Math/Matrices.swift diff --git a/Math/Math.xcodeproj/project.pbxproj b/Math/Math.xcodeproj/project.pbxproj index dbb6afe..940d201 100644 --- a/Math/Math.xcodeproj/project.pbxproj +++ b/Math/Math.xcodeproj/project.pbxproj @@ -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 = ""; }; C005E00B1BE1CBA800F1BD3C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + C005E0381BE1CC7A00F1BD3C /* Matrices.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrices.swift; sourceTree = ""; }; /* 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 */ }; diff --git a/Math/Math/Matrices.swift b/Math/Math/Matrices.swift new file mode 100644 index 0000000..d3a673d --- /dev/null +++ b/Math/Math/Matrices.swift @@ -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: 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 { + 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 *(matrix: Matrix4, scalar: T) -> Matrix4 { +// var out: Matrix4 +// for i in 0..(matrix: Matrix4, vector: Vector4) -> Vector4 { +// return Vector4() +//} \ No newline at end of file