vasturiano / d3-force-3d

Force-directed graph layout in 1D, 2D or 3D using velocity Verlet integration.
https://observablehq.com/@vasturiano/multi-dimensional-d3-force-simulation
MIT License
373 stars 54 forks source link

Z is NaN #6

Closed NagRock closed 5 years ago

NagRock commented 5 years ago

Hi, when I try to do simple simulation with 3 dimension I get z as NaN:

d3Force3d.forceSimulation(data).numDimensions(3)

Do I need to do something special to get z defined?

Is there any as simple as possible example with 3d simulation?

NagRock commented 5 years ago

Ok, I've debugged this and looks like I need to have defined 4 variables to get this working:

x = 0;
y = 0;
z = 0;
vz = 0;

Is this a bug?

vasturiano commented 5 years ago

@NagRock you should define numDimensions simultaneously when you include the nodes data, like so: d3Force3d.forceSimulation(data, 3).

I've adjusted the docs to reflect this more clearly.

NagRock commented 5 years ago

@vasturiano thanks, yes documentation didn't point it clearly. Nice that you've fixed it. But, looks like I'm getting different results when I use d3Force3d.forceSimulation(data, 3) and when I set default zeros for data elements. I can create sample project if you want to take a look at it.

vasturiano commented 5 years ago

@NagRock that is expected, because internally the nodes are not initialized to 0,0,0, but in a 3D phyllotaxis arrangement, so that they spread out evenly.

From the docs:

The position ⟨x[,y[,z]]⟩ and velocity ⟨vx[,vy[,vz]]⟩ may be subsequently modified by forces and by the simulation. If either applicable vx, vy or vz is NaN, the velocity is initialized to ⟨0,0,0⟩. If either applicable x, y or z is NaN, the 2D position is initialized in a phyllotaxis arrangement, so chosen to ensure a deterministic, uniform distribution around the origin.

That initial arrangement influences the eventual nodes topology. So yes, you would expect a difference, but hopefully is for the best. 😃

NagRock commented 5 years ago

Thanks for explanation!