Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constraining nodes to a sphere #6

Open
jokroese opened this issue Jun 17, 2022 · 4 comments
Open

Constraining nodes to a sphere #6

jokroese opened this issue Jun 17, 2022 · 4 comments

Comments

@jokroese
Copy link

I'm trying to constrain some nodes in 3D to a sphere. I'm using 3d-force-graph and hoping to do the constraining with d3-force-limit.

My current naive approach is here: https://codepen.io/jokroese/pen/ExQMrpj.

The problems are:

  1. if a node gets outside the sphere, it doesn't always get back inside the sphere. (Try pulling one of the nodes out of the sphere in the codepen example, ~50% of the time it will stay outside.)
  2. (Smaller issue) Though the nodes are mostly spread over the sphere, there is a "belt" of higher density at $x=0$.

Do you have suggestions on next steps for moving forward with this?

Possible options:

  1. I see that @Fil suggests a neater approach for this in 2D in Remarks #1. I could try to extend this to 3D.
  2. Use a different library that is more suited to this use case.
  3. Use d3-force-limit, but in a more thoughtful way.
@Fil
Copy link

Fil commented Jun 19, 2022

Here's my suggestion:

Graph.d3Force("radius", forceRadius(gData.nodes, 300));

function forceRadius(nodes, R = 1) {
  return () => {
    for (const n of nodes) {
      const r = Math.hypot(n.x, n.y, n.z);
      const u = Math.pow(r ? Math.sqrt(R / r) : 1, 0.05);
      n.x *= u;
      n.y *= u;
      n.z *= u;
    }
  }
}

see https://observablehq.com/@fil/3d-graph-on-sphere

@jokroese
Copy link
Author

Thanks @Fil, that looks great!

The only unusual thing is that dragging a node seems to remove the force. Do you know why that is? And can you see an obvious way to keep the force applied while dragging?

@Fil
Copy link

Fil commented Jun 19, 2022

Dragging does not remove the force, but it pushes alpha up (the "temperature", if you will), making all the other forces stronger. You could change the parameter 0.05 to make it stronger with alpha if you wanted, but it would make the initial convergence less smooth.

@Bennyyy27
Copy link

I really like this approach! Thanks for sharing.
Do you know if there is there any way to "pythonize" this or a similar approach in python?
Or a way to import my own adjacency list to this and export the 3D coordinates?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants