Skip to content

Commit

Permalink
Merge pull request #4 from li3zhen1/dev
Browse files Browse the repository at this point in the history
Add collision force & update example
  • Loading branch information
li3zhen1 committed Oct 10, 2023
2 parents 20f7b64 + 4e9d773 commit 38a1cec
Show file tree
Hide file tree
Showing 21 changed files with 905 additions and 859 deletions.
69 changes: 69 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"fileScopedDeclarationPrivacy": {
"accessLevel": "private"
},
"indentation": {
"spaces": 4
},
"indentConditionalCompilationBlocks": true,
"indentSwitchCaseLabels": false,
"lineBreakAroundMultilineExpressionChainComponents": false,
"lineBreakBeforeControlFlowKeywords": false,
"lineBreakBeforeEachArgument": false,
"lineBreakBeforeEachGenericRequirement": false,
"lineLength": 100,
"maximumBlankLines": 1,
"multiElementCollectionTrailingCommas": true,
"noAssignmentInExpressions": {
"allowedFunctions": [
"XCTAssertNoThrow"
]
},
"prioritizeKeepingFunctionOutputTogether": false,
"respectsExistingLineBreaks": true,
"rules": {
"AllPublicDeclarationsHaveDocumentation": false,
"AlwaysUseLiteralForEmptyCollectionInit": false,
"AlwaysUseLowerCamelCase": true,
"AmbiguousTrailingClosureOverload": true,
"BeginDocumentationCommentWithOneLineSummary": false,
"DoNotUseSemicolons": true,
"DontRepeatTypeInStaticProperties": true,
"FileScopedDeclarationPrivacy": true,
"FullyIndirectEnum": true,
"GroupNumericLiterals": true,
"IdentifiersMustBeASCII": true,
"NeverForceUnwrap": false,
"NeverUseForceTry": false,
"NeverUseImplicitlyUnwrappedOptionals": false,
"NoAccessLevelOnExtensionDeclaration": true,
"NoAssignmentInExpressions": true,
"NoBlockComments": true,
"NoCasesWithOnlyFallthrough": true,
"NoEmptyTrailingClosureParentheses": true,
"NoLabelsInCasePatterns": true,
"NoLeadingUnderscores": false,
"NoParensAroundConditions": true,
"NoPlaygroundLiterals": true,
"NoVoidReturnOnFunctionSignature": true,
"OmitExplicitReturns": false,
"OneCasePerLine": true,
"OneVariableDeclarationPerLine": true,
"OnlyOneTrailingClosureArgument": true,
"OrderedImports": true,
"ReplaceForEachWithForLoop": true,
"ReturnVoidInsteadOfEmptyTuple": true,
"TypeNamesShouldBeCapitalized": true,
"UseEarlyExits": false,
"UseLetInEveryBoundCaseVariable": true,
"UseShorthandTypeNames": true,
"UseSingleLinePropertyGetter": true,
"UseSynthesizedInitializer": true,
"UseTripleSlashForDocumentationComments": true,
"UseWhereClausesInForLoops": false,
"ValidateDocumentationComments": false
},
"spacesAroundRangeFormationOperators": false,
"tabWidth": 8,
"version": 1
}
Binary file modified Assets/ForceDirectedGraph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/GrapeRecord.mp4
Binary file not shown.
23 changes: 15 additions & 8 deletions Examples/GrapeView/GrapeView/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ struct ContentView: View {

init() {
self.data = getData(miserables)
self.sim = Simulation(nodes: data.nodes, alphaDecay: 0.001)
self.sim = Simulation(nodes: data.nodes, alphaDecay: 0.0005)

sim.createManyBodyForce(name: "manybody1", strength: -2.5)
sim.createManyBodyForce(strength: -30)

self.linkForce = sim.createLinkForce(name: "link1", links: data.links.map({ l in (l.source, l.target) }), originalLength: .constant(50))
self.linkForce = sim.createLinkForce(links: data.links.map({ l in (l.source, l.target) }), originalLength: .constant(35))

sim.createCenterForce(name: "center1", center: .zero, strength: 0.3)
sim.createCenterForce(center: .zero, strength: 0.1)

sim.createCollideForce(radius: .constant(5))

}

Expand Down Expand Up @@ -79,14 +81,19 @@ struct ContentView: View {
let x = 300 + node.position.x
let y = 200 + node.position.y

let rect = CGRect(origin: .init(x: CGFloat(x-3.0), y: CGFloat(y-3.0)), size: CGSize(width: 6.0, height: 6.0))
let rect = CGRect(origin: .init(x: CGFloat(x-4.0), y: CGFloat(y-4.0)), size: CGSize(width: 8.0, height: 8.0))

context.fill(Path(ellipseIn: rect), with: colors[self.data.nodes[i].group % colors.count])

context.stroke(Path(ellipseIn: rect), with: .color(Color(nsColor: .windowBackgroundColor)), style: StrokeStyle(lineWidth: 1.5))

}
}
.onAppear {
self.sim.start(intervalPerTick: 1/120) { nodes in
self.simulationNodes = nodes
}
self.simulationNodes = sim.simulationNodes
// self.sim.start(intervalPerTick: 1/120) { nodes in
// self.simulationNodes = nodes
// }
}
.frame(width: 600, height: 400)
.navigationTitle("Force Directed Graph Example")
Expand Down
26 changes: 8 additions & 18 deletions Sources/ForceSimulation/Force.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@
// Created by li3zhen1 on 10/1/23.
//

import simd
import QuadTree
import simd

public struct EdgeID<VertexID>: Hashable where VertexID: Hashable{
public struct EdgeID<VertexID>: Hashable where VertexID: Hashable {
public let source: VertexID
public let target: VertexID

public init(_ source: VertexID, _ target: VertexID) {
self.source = source
self.target = target
}
}


public struct SimulationNode<VID>: Identifiable where VID: Hashable {
public internal(set) var id: VID
public var position: Vector2f
public var velocity: Vector2f
public var fixation: Vector2f?

public mutating func fix(_ position: Vector2f) {
self.fixation = position
}
Expand All @@ -34,9 +33,7 @@ public struct SimulationNode<VID>: Identifiable where VID: Hashable {
}

public var isFixed: Bool {
get {
return self.fixation != nil
}
return self.fixation != nil
}

public mutating func setVelocity(_ velocity: Vector2f) {
Expand All @@ -55,28 +52,21 @@ public struct SimulationNode<VID>: Identifiable where VID: Hashable {

public struct SimulationEdge<VID>: Identifiable where VID: Hashable {
public var id: EdgeID<VID> {
get {
return EdgeID(source, target)
}
return EdgeID(source, target)
}

public var source: VID
public var target: VID
}


public protocol Force {
associatedtype N: Identifiable

func apply(alpha: Float)
}




extension SimulationNode: CustomDebugStringConvertible {
public var debugDescription: String {
return "\(self.id) [\(self.position.x), \(self.position.y)]"
}

}
Loading

0 comments on commit 38a1cec

Please sign in to comment.