Skip to content

Commit

Permalink
Merge pull request #38 from li3zhen1/dev
Browse files Browse the repository at this point in the history
Add conditional import for simd
  • Loading branch information
li3zhen1 authored Dec 12, 2023
2 parents 4dd5c50 + eaad997 commit 1a6e3e1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 99 deletions.
9 changes: 2 additions & 7 deletions Sources/ForceSimulation/AttributeDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,8 @@ extension AttributeDescriptor {
initialValue: m
)
case .varied(let valueProvider):
let array = UnsafeArray<T>.createBuffer(
withHeader: count,
count: count,
initialValue: .zero
)
for i in 0..<count {
array[i] = valueProvider(i)
let array = UnsafeArray<T>.createBuffer(withHeader: count, count: count) {
valueProvider($0)
}
return array
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/ForceSimulation/EdgeID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ public struct EdgeID<NodeID: Hashable>: Hashable {
self.target = target
}
}

extension EdgeID {
public init(_ source: NodeID, _ target: NodeID) {
self.source = source
self.target = target
}
}
67 changes: 35 additions & 32 deletions Sources/ForceSimulation/KDTree/BufferedKDTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ where

@inlinable
init(
nodeIndices: NodeIndex?,
childrenBufferPointer: UnsafeMutablePointer<KDTreeNode>?,
nodeIndices: consuming NodeIndex?,
childrenBufferPointer: consuming UnsafeMutablePointer<KDTreeNode>?,
delegate: consuming Delegate,
box: consuming KDBox<Vector>
box: consuming KDBox<Vector>,
nodePosition: consuming Vector = .zero
) {
self.childrenBufferPointer = childrenBufferPointer
self.nodeIndices = nodeIndices
self.childrenBufferPointer = consume childrenBufferPointer
self.nodeIndices = consume nodeIndices
self.delegate = consume delegate
self.box = consume box
self.nodePosition = .zero
self.nodePosition = consume nodePosition
}

}
Expand All @@ -44,7 +45,9 @@ where
public typealias TreeNode = KDTreeNode<Vector, Delegate>

@usableFromInline
internal var rootPointer: UnsafeMutablePointer<TreeNode>
internal var rootPointer: UnsafeMutablePointer<TreeNode> {
treeNodeBuffer.mutablePointer
}

@usableFromInline
internal var validCount: Int = 0
Expand Down Expand Up @@ -83,7 +86,7 @@ where
count: maxBufferCount,
initialValue: zeroNode
)
rootPointer = treeNodeBuffer.withUnsafeMutablePointerToElements { $0 }
// rootPointer = treeNodeBuffer.withUnsafeMutablePointerToElements { $0 }

rootPointer.pointee = .init(
nodeIndices: nil,
Expand Down Expand Up @@ -136,36 +139,28 @@ where
}
}

self.rootPointer = newRootPointer
// self.rootPointer = newRootPointer
self.treeNodeBuffer = newTreeNodeBuffer

// UnsafeArray<TreeNode>.createBuffer(
// withHeader: newTreeNodeBufferSize,
// count: newTreeNodeBufferSize,
// initialValue: .init(
// nodeIndices: nil,
// childrenBufferPointer: nil,
// delegate: root.delegate,
// box: root.box
// )
// )
// newTreeNodeBuffer.withUnsafeMutablePointerToElements {
// $0.moveInitialize(
// from: treeNodeBuffer.withUnsafeMutablePointerToElements { $0 }, count: validCount)
// }

#if DEBUG
assert(rootCopy.box == root.box)
assert(oldRootPointer != rootPointer)
#endif
}

/// Extend the size of the buffer.
/// - Parameter count: the number of elements to extend
/// - Returns: true if the buffer is resized
@inlinable
@discardableResult
internal mutating func resizeIfNeededBeforeAllocation(for count: Int) -> Bool {
if validCount + count > treeNodeBuffer.count {
let factor = (count / self.treeNodeBuffer.count) + 2
assert(treeNodeBuffer.count * factor > validCount + count)

resize(to: treeNodeBuffer.count * factor)

assert(treeNodeBuffer.count >= validCount + count)

return true
}
return false
Expand Down Expand Up @@ -204,10 +199,10 @@ where
> Self.clusterDistanceSquared
{

// let __treeNode = treeNode
// let __rootPointer = rootPointer
// let __treeNode = copy treeNode
// let __rootPointer = copy rootPointer
let treeNodeOffset = (consume treeNode) - rootPointer
let resized = resizeIfNeededBeforeAllocation(for: Self.directionCount)
resizeIfNeededBeforeAllocation(for: Self.directionCount)

let spawnedDelegate = treeNode.pointee.delegate.spawn()
let center = treeNode.pointee.box.center
Expand Down Expand Up @@ -252,9 +247,18 @@ where
relativeTo: center
)

childrenBufferPointer[direction].nodeIndices = newTreeNode.pointee.nodeIndices
childrenBufferPointer[direction].nodePosition = newTreeNode.pointee.nodePosition
childrenBufferPointer[direction].delegate = newTreeNode.pointee.delegate
childrenBufferPointer[direction] = .init(
nodeIndices: newTreeNode.pointee.nodeIndices,
childrenBufferPointer: childrenBufferPointer[direction].childrenBufferPointer,
delegate: newTreeNode.pointee.delegate,
box: childrenBufferPointer[direction].box,
nodePosition: newTreeNode.pointee.nodePosition
)
// childrenBufferPointer[direction].nodePosition = newTreeNode.pointee.nodePosition

// childrenBufferPointer[direction].nodeIndices = newTreeNode.pointee.nodeIndices
// childrenBufferPointer[direction].nodePosition = newTreeNode.pointee.nodePosition
// childrenBufferPointer[direction].delegate = newTreeNode.pointee.delegate
newTreeNode.pointee.nodeIndices = nil
newTreeNode.pointee.nodePosition = .zero
}
Expand Down Expand Up @@ -364,7 +368,6 @@ where
@inlinable
internal func getIndexInChildren(_ point: Vector, relativeTo originalPoint: Vector) -> Int {
var index = 0

let mask = point .>= originalPoint

for i in 0..<Vector.scalarCount {
Expand Down
5 changes: 0 additions & 5 deletions Sources/ForceSimulation/KDTree/KDTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ where
public var nodePosition: Vector?
public var nodeIndices: [NodeIndex]

// public let clusterDistance: Vector.Scalar
@inlinable var clusterDistanceSquared: Vector.Scalar {
return Vector.clusterDistanceSquared
}
Expand All @@ -30,20 +29,17 @@ where
spawnedDelegateBeingConsumed: consuming Delegate
) {
self.box = box
// self.clusterDistanceSquared = clusterDistanceSquared
self.nodeIndices = []
self.delegate = consume spawnedDelegateBeingConsumed
}

@inlinable
init(
box: Box,
// clusterDistanceSquared: Vector.Scalar,
spawnedDelegateBeingConsumed: consuming Delegate,
childrenBeingConsumed: consuming [KDTree<Vector, Delegate>]
) {
self.box = box
// self.clusterDistanceSquared = clusterDistanceSquared
self.nodeIndices = []
self.delegate = consume spawnedDelegateBeingConsumed
self.children = consume childrenBeingConsumed
Expand Down Expand Up @@ -127,7 +123,6 @@ where

self = Self(
box: newRootBox,
// clusterDistanceSquared: clusterDistanceSquared,
spawnedDelegateBeingConsumed: self.delegate,
childrenBeingConsumed: consume result
)
Expand Down
7 changes: 6 additions & 1 deletion Sources/ForceSimulation/SimulatableVector.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import simd

/// A protocol for vectors that can be jiggled, and has a certain precision for
/// simulation — so zero vectors could be altered
Expand Down Expand Up @@ -85,6 +84,10 @@ extension SIMD3: SimulatableVector where Scalar: FloatingPoint & HasDeterministi
}
}

#if canImport(simd)
import simd


extension SIMD2: L2NormCalculatable where Scalar == Double {
@inlinable
public func distanceSquared(to point: SIMD2<Scalar>) -> Scalar {
Expand Down Expand Up @@ -128,3 +131,5 @@ extension SIMD3: L2NormCalculatable where Scalar == Float {
return simd_length(self)
}
}

#endif
54 changes: 0 additions & 54 deletions Sources/Grape/Utils/SequenceDiff.swift

This file was deleted.

56 changes: 56 additions & 0 deletions Sources/Grape/Utils/Viewport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import simd

protocol ViewportPoint<Scalar> {
associatedtype Scalar where Scalar: FloatingPoint
var x: Scalar { get }
var y: Scalar { get }
}

extension SIMD2: ViewportPoint where Scalar == Double {}

public struct ViewportTransform {
public typealias Scalar = Double
@usableFromInline
internal var data: SIMD3<Scalar>

@inlinable
public init(data: SIMD3<Scalar>) {
self.data = data
}
}

extension ViewportTransform: ExpressibleByArrayLiteral {
@inlinable
public init(arrayLiteral elements: Scalar...) {
self.init(data: SIMD3<Scalar>(elements))
}
}

extension ViewportTransform {
@inlinable
public var k: Scalar {
return self.data.x
}

@inlinable
public var x: Scalar {
return self.data.y
}

@inlinable
public var y: Scalar {
return self.data.z
}
}

extension ViewportTransform {
@inlinable
public func scale(by factor: Scalar) -> ViewportTransform {
return ViewportTransform(data: self.data * [1, factor, factor])
}

@inlinable
public func translate(by vector: SIMD2<Scalar>) -> ViewportTransform {
return ViewportTransform(data: self.data + [0, vector.x, vector.y])
}
}

0 comments on commit 1a6e3e1

Please sign in to comment.