li3zhen1 / Grape

A Swift library for graph visualization and efficient force simulation.
https://li3zhen1.github.io/Grape/Grape/documentation/grape/
MIT License
262 stars 11 forks source link

No way to lookup original node IDs #60

Open andrewtheis opened 2 weeks ago

andrewtheis commented 2 weeks ago

First, let me say this is a fantastic library! However, there's a shortcoming when it comes to the ForceDescriptor types.

For example, it's not possible to lookup the original source / target IDs in the Kinetics2D.LinkStiffness.varied closure, as they're type-erased to EdgeID<Int> internally (for performance reasons I assume).

A workaround could be to carefully track the expected Int id value in some way as they're added to Series, but obviously this is a very fragile solution.

To solve this, ideally nodeIndexLookup would be exposed in some way, possibly by updating the the force: closure on ForceDirectedGraph init to include the lookup hash table:

ForceDirectedGraph(states: states) {
   ...
} force: { nodeIdLookup in
   LinkForce(stiffness: .varied { edge, _ in
      let sourceId = nodeIdLookup[edge.source]
      ...
   })
}

Alternatively, NodeMark / LinkMark could include values for setting stiffness, etc and then the lookup would continue to happen internally:

LinkMark(...)
   .stiffness(10)

If I can find time, I might take a crack at one of these solutions, but I'm sure this is something you've already thought of implementing!

li3zhen1 commented 2 weeks ago

Hi, thanks for the interest!

I think currently there's no way to get nodeIdLookup from View API, because the force parameter is designed to work without the knowledge of the graph structure. This is a nice gotcha but probably need some rework on View APIs. I'll get back to you later.