Fix the build; test box subdivisions
This commit is contained in:
parent
8e2a873edf
commit
cd48797fa9
3 changed files with 62 additions and 13 deletions
|
@ -591,6 +591,7 @@
|
|||
"$(inherited)",
|
||||
"@executable_path/../Frameworks",
|
||||
);
|
||||
MTL_HEADER_SEARCH_PATHS = Terrain2;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = me.erynwells.Terrain2;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
PROVISIONING_PROFILE_SPECIFIER = "";
|
||||
|
@ -613,6 +614,7 @@
|
|||
"$(inherited)",
|
||||
"@executable_path/../Frameworks",
|
||||
);
|
||||
MTL_HEADER_SEARCH_PATHS = Terrain2;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = me.erynwells.Terrain2;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
PROVISIONING_PROFILE_SPECIFIER = "";
|
||||
|
|
|
@ -132,54 +132,68 @@ public class DiamondSquareAlgorithm: Algorithm {
|
|||
let origin: Point
|
||||
let size: Size
|
||||
|
||||
public init(origin o: Point, size s: Size) {
|
||||
init(origin o: Point, size s: Size) {
|
||||
origin = o
|
||||
size = s
|
||||
}
|
||||
|
||||
public var corners: [Point] {
|
||||
var corners: [Point] {
|
||||
return [northwest, southwest, northeast, northwest]
|
||||
}
|
||||
|
||||
public var sideMidpoints: [Point] {
|
||||
var sideMidpoints: [Point] {
|
||||
return [north, west, south, east]
|
||||
}
|
||||
|
||||
public var north: Point {
|
||||
var north: Point {
|
||||
return (x: origin.x + (size.w / 2 + 1), y: origin.y)
|
||||
}
|
||||
|
||||
public var west: Point {
|
||||
var west: Point {
|
||||
return (x: origin.x, y: origin.y + (size.h / 2 + 1))
|
||||
}
|
||||
|
||||
public var south: Point {
|
||||
var south: Point {
|
||||
return (x: origin.x + (size.w / 2 + 1), y: origin.y + size.h)
|
||||
}
|
||||
|
||||
public var east: Point {
|
||||
var east: Point {
|
||||
return (x: origin.x + size.w, y: origin.y + (size.h / 2 + 1))
|
||||
}
|
||||
|
||||
public var northwest: Point {
|
||||
var northwest: Point {
|
||||
return origin
|
||||
}
|
||||
|
||||
public var southwest: Point {
|
||||
var southwest: Point {
|
||||
return (x: origin.x, y: origin.y + size.h)
|
||||
}
|
||||
|
||||
public var northeast: Point {
|
||||
var northeast: Point {
|
||||
return (x: origin.x + size.w, y: origin.y)
|
||||
}
|
||||
|
||||
public var southeast: Point {
|
||||
var southeast: Point {
|
||||
return (x: origin.x + size.w, y: origin.y + size.h)
|
||||
}
|
||||
|
||||
public var midpoint: Point {
|
||||
var midpoint: Point {
|
||||
return (x: origin.x + (size.w / 2 + 1), y: origin.y + (size.h / 2 + 1))
|
||||
}
|
||||
|
||||
var subdivisions: [Box] {
|
||||
guard size.w > 1 && size.h > 1 else {
|
||||
return []
|
||||
}
|
||||
let midp = midpoint
|
||||
let newSize = (w: midp.x - origin.x, h: midp.y - origin.y)
|
||||
return [
|
||||
Box(origin: origin, size: newSize),
|
||||
Box(origin: (origin.x + newSize.w, origin.y), size: newSize),
|
||||
Box(origin: (origin.x, origin.y + newSize.h), size: newSize),
|
||||
Box(origin: (origin.x + newSize.w, origin.y + newSize.h), size: newSize)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
let name = "Diamond-Square"
|
||||
|
@ -278,6 +292,15 @@ public class DiamondSquareAlgorithm: Algorithm {
|
|||
texture.replace(region: region, mipmapLevel: 0, withBytes: heightMap, bytesPerRow: MemoryLayout<Float>.stride * size.width)
|
||||
}
|
||||
|
||||
private func breadthFirstSearch(ofGridWithSize size: MTLSize, visit: (Box) -> (Void)) {
|
||||
var queue = [Box(origin: (0, 0), size: (size.width, size.height))]
|
||||
while queue.count > 0 {
|
||||
let box = queue.removeFirst()
|
||||
visit(box)
|
||||
queue.append(contentsOf: box.subdivisions)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Algorithm
|
||||
|
||||
var outTexture: MTLTexture {
|
||||
|
@ -291,6 +314,12 @@ public class DiamondSquareAlgorithm: Algorithm {
|
|||
}
|
||||
}
|
||||
|
||||
extension DiamondSquareAlgorithm.Box: Equatable {
|
||||
public static func == (lhs: DiamondSquareAlgorithm.Box, rhs: DiamondSquareAlgorithm.Box) -> Bool {
|
||||
return lhs.origin == rhs.origin && lhs.size == rhs.size
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of the Circles algorithm.
|
||||
//class CirclesAlgorithm: Algorithm {
|
||||
// static let name = "Circles"
|
||||
|
|
|
@ -7,7 +7,15 @@
|
|||
//
|
||||
|
||||
import XCTest
|
||||
import Terrain2
|
||||
@testable import Terrain2
|
||||
|
||||
public typealias Box = DiamondSquareAlgorithm.Box
|
||||
public typealias Point = DiamondSquareAlgorithm.Box.Point
|
||||
public typealias Size = DiamondSquareAlgorithm.Box.Size
|
||||
|
||||
class DiamondSquareBFSTests: XCTestCase {
|
||||
|
||||
}
|
||||
|
||||
class DiamondSquareBoxTests: XCTestCase {
|
||||
func testNorthwest() {
|
||||
|
@ -49,4 +57,14 @@ class DiamondSquareBoxTests: XCTestCase {
|
|||
XCTAssertEqual(midpoint.x, 6)
|
||||
XCTAssertEqual(midpoint.y, 7)
|
||||
}
|
||||
|
||||
func testSubdivision() {
|
||||
let box = Box(origin: (x: 3, y: 4), size: (w: 5, h: 5))
|
||||
let subdivs = box.subdivisions
|
||||
XCTAssertEqual(subdivs.count, 4)
|
||||
XCTAssertEqual(subdivs[0], Box(origin: (x: 3, y: 4), size: (w: 3, h: 3)))
|
||||
XCTAssertEqual(subdivs[1], Box(origin: (x: 6, y: 4), size: (w: 3, h: 3)))
|
||||
XCTAssertEqual(subdivs[2], Box(origin: (x: 3, y: 7), size: (w: 3, h: 3)))
|
||||
XCTAssertEqual(subdivs[3], Box(origin: (x: 6, y: 7), size: (w: 3, h: 3)))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue