-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from li3zhen1/dev
Add PositionForce
- Loading branch information
Showing
11 changed files
with
526 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
struct Node { | ||
float2 position; | ||
float2 velocity; | ||
float2 fixation; | ||
}; | ||
|
||
kernel void applyCenterForce( | ||
device Node* nodes [[ buffer(0) ]], | ||
constant float2& center [[ buffer(1) ]], | ||
constant float& strength [[ buffer(2) ]], | ||
uint id [[ thread_position_in_grid ]], | ||
uint nodeCount [[ threads_per_grid ]]) | ||
{ | ||
float2 meanPosition = float2(0.0, 0.0); | ||
for (int i = 0; i < nodeCount; ++i) { | ||
meanPosition += nodes[i].position; | ||
} | ||
meanPosition /= float(nodeCount); | ||
|
||
float2 delta = (meanPosition - center) * strength; | ||
nodes[id].position -= delta; | ||
} |
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,28 @@ | ||
import Foundation | ||
import Metal | ||
import simd | ||
import MetalPerformanceShaders | ||
|
||
|
||
final public class MPSSimulation { | ||
init() { | ||
guard let device = MTLCreateSystemDefaultDevice() else { | ||
fatalError("") | ||
} | ||
|
||
guard let commandQueue = device.makeCommandQueue() else { | ||
fatalError("") | ||
} | ||
|
||
let library = device.makeDefaultLibrary() | ||
|
||
let function = library?.makeFunction(name: "") | ||
|
||
var pipelineState: MTLComputePipelineState | ||
do { | ||
pipelineState = try device.makeComputePipelineState(function: function!) | ||
} catch { | ||
fatalError("") | ||
} | ||
} | ||
} |
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,158 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by li3zhen1 on 10/10/23. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol ComponentComparable { | ||
@inlinable static func <(lhs: Self, rhs: Self) -> Bool | ||
@inlinable static func <=(lhs: Self, rhs: Self) -> Bool | ||
@inlinable static func >(lhs: Self, rhs: Self) -> Bool | ||
@inlinable static func >=(lhs: Self, rhs: Self) -> Bool | ||
} | ||
|
||
public struct NdBox<Coordinate> where Coordinate: SIMD<Float>, Coordinate: ComponentComparable { | ||
public var p0: Coordinate | ||
public var p1: Coordinate | ||
|
||
@inlinable public init(p0:Coordinate, p1:Coordinate) { | ||
var p0 = p0 | ||
var p1 = p1 | ||
for i in p0.indices { | ||
if p1[i] < p0[i] { | ||
swap(&p0[i], &p1[i]) | ||
} | ||
} | ||
self.p0 = p0 | ||
self.p1 = p1 | ||
} | ||
|
||
public static var unit: Self { .init(p0: .zero, p1: .one) } | ||
} | ||
|
||
extension NdBox { | ||
var area: Float { | ||
var result: Float = 1 | ||
let delta = p1 - p0 | ||
for i in delta.indices { | ||
result *= delta[i] | ||
} | ||
return result | ||
} | ||
|
||
var vec: Coordinate { p1 - p0 } | ||
|
||
var center: Coordinate { (p1+p0) / 2 } | ||
|
||
@inlinable public func getCorner<Direction>( | ||
of direction: Direction | ||
) -> Coordinate where Direction: NdDirection, Direction.Coordinate == Coordinate { | ||
var result = Coordinate() | ||
var value = direction.rawValue | ||
|
||
// starting from the last element | ||
for i in (0..<Coordinate.scalarCount).reversed() { | ||
result[i] = value % 2 == 1 ? p1[i] : p0[i] | ||
value /= 2 | ||
} | ||
return result | ||
} | ||
|
||
@inlinable func contains(_ point: Coordinate) -> Bool { | ||
return (p0 <= point) && (point < p1) | ||
} | ||
} | ||
|
||
/// Reversed bit representation for nth dimension | ||
/// e.g. for 3d: 0b001 => (x:0, y:0, z:1) | ||
public protocol NdDirection: RawRepresentable { | ||
associatedtype Coordinate: SIMD<Float> | ||
var rawValue: Int { get } | ||
var reversed: Self { get } | ||
static var entryCount: Int { get } | ||
} | ||
|
||
//public extension SIMD<Float> { | ||
// @inlinable func direction<Direction>(originalPoint point: Self) -> Direction where Direction: NdDirection, Direction.Coordinate == Self { | ||
// | ||
// } | ||
//} | ||
|
||
struct OctDirection: NdDirection { | ||
typealias Coordinate = SIMD3<Float> | ||
let rawValue: Int | ||
var reversed: OctDirection { | ||
return OctDirection(rawValue: 7-rawValue) | ||
} | ||
static let entryCount: Int = 8 | ||
} | ||
|
||
public struct NdChildren<T, Coordinate> where Coordinate: SIMD<Float> { | ||
@usableFromInline internal let children: [T] | ||
|
||
@inlinable public subscript<Direction>( | ||
at direction: Direction | ||
) -> T where Direction: NdDirection, Direction.Coordinate == Coordinate { | ||
return children[direction.rawValue] | ||
} | ||
} | ||
|
||
|
||
public protocol TreeNodeDelegate { | ||
associatedtype Node | ||
associatedtype Index: Hashable | ||
mutating func didAddNode(_ node: Index, at position: Vector2f) | ||
mutating func didRemoveNode(_ node: Index, at position: Vector2f) | ||
func copy() -> Self | ||
func createNew() -> Self | ||
} | ||
|
||
|
||
public final class NdTree<C, TD> where C:SIMD<Float>, C:ComponentComparable, TD: TreeNodeDelegate { | ||
|
||
public typealias Box = NdBox<C> | ||
public typealias Children = NdChildren<NdTree<C, TD>, C> | ||
public typealias Index = Int | ||
public typealias Direction = Int | ||
|
||
public private(set) var box: NdBox<C> | ||
|
||
public var nodes: [Index] = [] | ||
public private(set) var children: Children? | ||
public let clusterDistance: Float | ||
public var delegate: TD | ||
|
||
init( | ||
box: Box, | ||
clusterDistance: Float, | ||
rootNodeDelegate: TD | ||
) { | ||
self.box = box | ||
self.clusterDistance = clusterDistance | ||
self.delegate = rootNodeDelegate.createNew() | ||
} | ||
|
||
public func add(_ nodeIndex: Index, at point: C) { | ||
|
||
} | ||
|
||
|
||
private func cover(_ point: C) { | ||
if box.contains(point) { return } | ||
|
||
repeat { | ||
|
||
// let direction: Direction = | ||
// | ||
// expand(towards: direction) | ||
|
||
} while !box.contains(point) | ||
} | ||
|
||
private func expand(towards direction: Direction) { | ||
|
||
} | ||
} |
Oops, something went wrong.