Skip to content

Commit

Permalink
Version 4.
Browse files Browse the repository at this point in the history
Revised API to remove Integer support as most algorithms required Float/Double. This reduces the need for global functions and they have been now been made into static functions on the type itself to try to improve compile times.
  • Loading branch information
jkolb committed Apr 26, 2018
1 parent b6f4f03 commit 57175b5
Show file tree
Hide file tree
Showing 31 changed files with 1,197 additions and 1,646 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
#### Changelog

### Version 4.0.0
* Simplified API by removing Integer support, most useful functions require trigonometry or not truncating division.
* Made functions follow Swift conventions better by making them static methods on the type instead of global functions.
* Removed the Angle type as it made things more complicated than the benefits it may have provided.
* Removed the Rectangle type as it was not used in any algorithms and was mostly desinged for use with Integers which were removed.

### Version 3.1.2
* zubco Make sure Float80 is only compiled for Intel

Expand Down
139 changes: 0 additions & 139 deletions Sources/Swiftish/Angle.swift

This file was deleted.

10 changes: 3 additions & 7 deletions Sources/Swiftish/Bounds2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
*/

public struct Bounds2<T: SignedVectorable> : Equatable {
public struct Bounds2<T: Vectorable> : Equatable {
public var center: Vector2<T>
public var extents: Vector2<T>

Expand Down Expand Up @@ -64,8 +64,8 @@ public struct Bounds2<T: SignedVectorable> : Equatable {
return
}

var minimum = Vector2<T>(T.max, T.max)
var maximum = Vector2<T>(T.min, T.min)
var minimum = Vector2<T>(+T.greatestFiniteMagnitude, +T.greatestFiniteMagnitude)
var maximum = Vector2<T>(-T.greatestFiniteMagnitude, -T.greatestFiniteMagnitude)

for point in points {
if point.x < minimum.x {
Expand Down Expand Up @@ -187,7 +187,3 @@ public struct Bounds2<T: SignedVectorable> : Equatable {
return a.center == b.center && a.extents == b.extents
}
}

public func circle<T: FloatingPointVectorable>(_ a: Bounds2<T>) -> Circle<T> {
return Circle<T>(center: a.center, radius: length(a.extents))
}
90 changes: 65 additions & 25 deletions Sources/Swiftish/Bounds3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
SOFTWARE.
*/

public struct Bounds3<T: SignedVectorable> : Equatable {
public struct Bounds3<T: Vectorable> : Equatable {
public var center: Vector3<T>
public var extents: Vector3<T>

Expand Down Expand Up @@ -66,8 +66,8 @@ public struct Bounds3<T: SignedVectorable> : Equatable {
return
}

var minimum = Vector3<T>(T.max, T.max, T.max)
var maximum = Vector3<T>(T.min, T.min, T.min)
var minimum = Vector3<T>(+T.greatestFiniteMagnitude, +T.greatestFiniteMagnitude, +T.greatestFiniteMagnitude)
var maximum = Vector3<T>(-T.greatestFiniteMagnitude, -T.greatestFiniteMagnitude, -T.greatestFiniteMagnitude)

for point in points {
if point.x < minimum.x {
Expand Down Expand Up @@ -206,32 +206,72 @@ public struct Bounds3<T: SignedVectorable> : Equatable {
public static func ==(a: Bounds3<T>, b: Bounds3<T>) -> Bool {
return a.center == b.center && a.extents == b.extents
}
}

public func sphere<T: FloatingPointVectorable>(_ a: Bounds3<T>) -> Sphere<T> {
return Sphere<T>(center: a.center, radius: length(a.extents))
}

public func distance2<T>(_ point: Vector3<T>, _ bounds: Bounds3<T>) -> T {
var distanceSquared: T = 0
let minimum = bounds.minimum
let maximum = bounds.maximum

for index in 0..<3 {
let p = point[index]
let bMin = minimum[index]
let bMax = maximum[index]

if p < bMin {
let delta = bMin - p
distanceSquared = distanceSquared + delta * delta
public static func distance2<T>(_ point: Vector3<T>, _ bounds: Bounds3<T>) -> T {
var distanceSquared: T = 0
let minimum = bounds.minimum
let maximum = bounds.maximum

for index in 0..<3 {
let p = point[index]
let bMin = minimum[index]
let bMax = maximum[index]

if p < bMin {
let delta = bMin - p
distanceSquared = distanceSquared + delta * delta
}

if p > bMax {
let delta = p - bMax
distanceSquared = distanceSquared + delta * delta
}
}

if p > bMax {
let delta = p - bMax
distanceSquared = distanceSquared + delta * delta
return distanceSquared
}

public func intersectsOrIsInside(_ plane: Plane<T>) -> Bool {
let projectionRadiusOfBox = Vector3<T>.sum(extents * Vector3<T>.abs(plane.normal))
let distanceOfBoxCenterFromPlane = Vector3<T>.dot(plane.normal, center) - plane.distance
let intersects = abs(distanceOfBoxCenterFromPlane) <= projectionRadiusOfBox
let isInside = projectionRadiusOfBox <= distanceOfBoxCenterFromPlane

return intersects || isInside
}

public func intersectsOrIsInside(_ frustum: Frustum<T>) -> Bool {
if isNull {
return false
}

// In order of most likely to cause early exit
if !intersectsOrIsInside(frustum.near) { return false }
if !intersectsOrIsInside(frustum.left) { return false }
if !intersectsOrIsInside(frustum.right) { return false }
if !intersectsOrIsInside(frustum.top) { return false }
if !intersectsOrIsInside(frustum.bottom) { return false }
if !intersectsOrIsInside(frustum.far) { return false }

return true
}

public func intersects(_ plane: Plane<T>) -> Bool {
let projectionRadiusOfBox = Vector3<T>.sum(extents * Vector3<T>.abs(plane.normal))
let distanceOfBoxCenterFromPlane = Swift.abs(Vector3<T>.dot(plane.normal, center) - plane.distance)

return distanceOfBoxCenterFromPlane <= projectionRadiusOfBox
}

public func intersects(_ bounds: Bounds3<T>) -> Bool {
if Swift.abs(center.x - bounds.center.x) > (extents.x + bounds.extents.x) { return false }
if Swift.abs(center.y - bounds.center.y) > (extents.y + bounds.extents.y) { return false }
if Swift.abs(center.z - bounds.center.z) > (extents.z + bounds.extents.z) { return false }

return true
}

return distanceSquared
public func transform(_ t: Transform3<T>) -> Bounds3<T> {
return Bounds3<T>(containingPoints: corners.map({ $0.transform(t) }))
}
}
5 changes: 5 additions & 0 deletions Sources/Swiftish/Circle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public struct Circle<T: Vectorable> : Equatable, CustomStringConvertible {
self.radius = radius
}

public init(_ bounds: Bounds2<T>) {
self.center = bounds.center
self.radius = Vector2<T>.length(bounds.extents)
}

public var description: String {
return "{\(center), \(radius)}"
}
Expand Down
Loading

0 comments on commit 57175b5

Please sign in to comment.