-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fc8191
commit 351fe77
Showing
10 changed files
with
171 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// | ||
// Boundable.swift | ||
// SwiftGeo | ||
// | ||
// Created by Rémi Bardon on 04/02/2022. | ||
// Copyright © 2022 Rémi Bardon. All rights reserved. | ||
// | ||
|
||
import GeoModels | ||
|
||
public protocol Boundable { | ||
|
||
associatedtype BoundingBox: GeoModels.BoundingBox | ||
|
||
var _bbox: BoundingBox { get } | ||
|
||
} | ||
|
||
extension Boundable { | ||
|
||
public var bbox: BoundingBox { _bbox } | ||
|
||
} | ||
|
||
extension Boundable where Self: Hashable { | ||
|
||
public var bbox: BoundingBox { BoundingBoxCache.shared.bbox(for: self) } | ||
|
||
} | ||
|
||
extension Coordinate2D { | ||
|
||
public var _bbox: BoundingBox2D { | ||
BoundingBox2D(southWest: self, width: .zero, height: .zero) | ||
} | ||
|
||
} | ||
|
||
extension Coordinate3D: Boundable { | ||
|
||
public var _bbox: BoundingBox3D { | ||
BoundingBox3D(southWestLow: self, width: .zero, height: .zero, zHeight: .zero) | ||
} | ||
|
||
} | ||
|
||
extension Line2D: Boundable { | ||
|
||
public var _bbox: BoundingBox2D { | ||
Turf.bbox(for: [start, end])! | ||
} | ||
|
||
} | ||
|
||
extension Line3D: Boundable { | ||
|
||
public var _bbox: BoundingBox3D { | ||
Turf.bbox(for: [start, end])! | ||
} | ||
|
||
} | ||
|
||
extension BoundingBox2D: Boundable { | ||
|
||
public var _bbox: BoundingBox2D { self } | ||
|
||
} | ||
|
||
extension BoundingBox3D: Boundable { | ||
|
||
public var _bbox: BoundingBox3D { self } | ||
|
||
} | ||
|
||
// Extension of protocol 'Collection' cannot have an inheritance clause | ||
//extension Collection: Boundable where Element: Boundable { | ||
// | ||
// public var _bbox: Element.BoundingBox { | ||
// self.reduce(.zero, { $0.union($1.bbox) }) | ||
// } | ||
// | ||
//} | ||
|
||
extension Array: Boundable where Element: Boundable { | ||
|
||
public var _bbox: Element.BoundingBox { | ||
self.reduce(.zero, { $0.union($1.bbox) }) | ||
} | ||
|
||
} | ||
|
||
extension Set: Boundable where Element: Boundable { | ||
|
||
public var _bbox: Element.BoundingBox { | ||
self.reduce(.zero, { $0.union($1.bbox) }) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// BoundingBoxCache.swift | ||
// SwiftGeo | ||
// | ||
// Created by Rémi Bardon on 10/02/2022. | ||
// Copyright © 2022 Rémi Bardon. All rights reserved. | ||
// | ||
|
||
import GeoModels | ||
|
||
public class BoundingBoxCache { | ||
|
||
public static let shared = BoundingBoxCache() | ||
|
||
private var values = [AnyHashable: Any]() | ||
|
||
private init() {} | ||
|
||
internal func store<B: BoundingBox, K: Hashable>(_ value: B, forKey key: K) { | ||
values[AnyHashable(key)] = value | ||
} | ||
|
||
internal func get<B: BoundingBox, K: Hashable>(_ type: B.Type, forKey key: K) -> B? { | ||
values[AnyHashable(key)] as? B | ||
} | ||
|
||
public func bbox<B: Boundable & Hashable>(for boundable: B) -> B.BoundingBox { | ||
if let cachedValue = self.get(B.BoundingBox.self, forKey: boundable) { | ||
return cachedValue | ||
} else { | ||
let bbox = boundable._bbox | ||
self.store(bbox, forKey: boundable) | ||
return bbox | ||
} | ||
} | ||
|
||
public func removeCache<K: Hashable>(for key: K) { | ||
values.removeValue(forKey: AnyHashable(key)) | ||
} | ||
|
||
} |