Skip to content

Commit

Permalink
Fix complier crashing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
li3zhen1 committed Nov 5, 2023
1 parent efdb085 commit 45e96e6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import Grape
struct ForceDirectedGraphSwiftUIExample: View {
let graphController = ForceDirectedGraph2DController<Int>()
var body: some View {

ForceDirectedGraph(controller: graphController) {
NodeMark(id: 0)
for i in 1..<10 {
NodeMark(id: i)
}

for i in 0..<9 {
NodeMark(id: 0, fill: .green)
NodeMark(id: 1, fill: .blue)
NodeMark(id: 2, fill: .yellow)

for i in 0..<2 {
LinkMark(from: i, to: i+1)
}

Expand Down
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,48 @@ Source code: [ForceDirectedGraph3D/ContentView.swift](https://github.com/li3zhen

## Usage

Grape provides 2 kinds of classes, `NDTree` and `Simulation`.
### `Grape`

`Grape` provides a SwiftUI view `ForceDirectedGraph`:

```swift
struct ForceDirectedGraphSwiftUIExample: View {
let graphController = ForceDirectedGraph2DController<Int>()
var body: some View {
ForceDirectedGraph(controller: graphController) {
// Declare nodes and links like you would do in Swift Charts.
NodeMark(id: 0, fill: .green)
NodeMark(id: 1, fill: .blue)
NodeMark(id: 2, fill: .yellow)
for i in 0..<2 {
LinkMark(from: i, to: i+1)
}

} forceField: {
// Declare forces like you would do in D3.js.
LinkForce()
CenterForce()
ManyBodyForce()
}
.onAppear {
// Let's start moving!
graphController.start()
}

}
}
```
> [!NOTE]
> `ForceDirectedGraph` is only a minimal working example. Please refere to the next section if you need a view that really works.

### `ForceSimulation`

`ForceSimulation` module provides 2 kinds of classes, `NDTree` and `Simulation`.
- `NDTree` is a KD-Tree data structure, which is used to accelerate the force simulation with [Barnes-Hut Approximation](https://jheer.github.io/barnes-hut/).
- `Simulation` is a force simulation class, that enables you to create any dimensional simulation with velocity Verlet integration.

### Basic
#### Basic

The basic concepts of simulations and forces can be found here: [Force simulations - D3](https://d3js.org/d3-force/simulation). You can simply create 2D or 3D simulations by using `Simulation2D` or `Simulation3D`:

Expand Down Expand Up @@ -104,7 +141,7 @@ See [Example](https://github.com/li3zhen1/Grape/tree/main/Examples/ForceDirected

<br/>

### Advanced
#### Advanced

Grape provides a set of generic based types that works with any SIMD-like data structures. To integrate Grape into platforms where `import simd` isn't supported, or higher dimensions, you need to create a struct conforming to the `VectorLike` protocol. For ease of use, it's also recommended to add some type aliases. Here’s how you can do it:

Expand Down
3 changes: 1 addition & 2 deletions Sources/Grape/Models/ForceDirectedGraph2DLayoutEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ protocol LayoutEngine {
@Observable
public class ForceDirectedGraph2DLayoutEngine: LayoutEngine {

public var simulation: Simulation2D<Int>
var simulation: Simulation2D<Int>

@ObservationIgnored
let frameRate: Double = 60.0

@ObservationIgnored
var scheduledTimer: Timer? = nil

@inlinable
public init(initialSimulation: Simulation2D<Int>) {
self.simulation = initialSimulation
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/Grape/Views/ForceDirectedGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public struct ForceDirectedGraph<NodeID: Hashable>: View {
}
}

@inlinable
// @inlinable
func paintNodes(context: inout GraphicsContext, centerX: Double, centerY: Double) {

for i in model.simulation.nodePositions.indices {
Expand Down Expand Up @@ -98,9 +98,9 @@ public struct ForceDirectedGraph<NodeID: Hashable>: View {
}
}

self.nodeIdToIndexLookup = consume lookup
self.nodeIdToIndexLookup = lookup
let model = ForceDirectedGraph2DLayoutEngine(
initialSimulation: consume simulation
initialSimulation: simulation
)
controller.layoutEngine = model
self.model = model
Expand Down

0 comments on commit 45e96e6

Please sign in to comment.