-
Notifications
You must be signed in to change notification settings - Fork 141
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
Request: a mutable variant with an update method, for Voronoi relaxation purposes #36
Comments
That's a reasonable request — I'll look into it. |
I looked at the source-code for |
Would this work? https://gist.github.com/JobLeonard/b5dccf0ebacc06f30baf20029f206fb5 |
@JobLeonard ideally we would want to make this work while avoiding code duplication, while making sure we're not breaking the public API — it's tricky, but it's definitely possible. I had a stab at this some months ago but didn't finish — might need to revisit some time soon. |
Can't we just split out the main code into another method that is called from both the constructor and the update function? |
I suppose one question is that we don't want to cache the arrays by default. How about not doing so until |
The last approach could be done like this:
Does that sound like an elegant solution? |
PR here: #48 |
Lloyd's Algorithm works by repeatedly moving generators of a Voronoi diagram to their center of mass and re-computing the diagram.
This is is used in many contexts, but a fun one is Secord's stippling algorithm - Mike Bostock has a notebook here.
This requires creating a new diagram from scratch each time. So far so good. However, that also comes with allocating nine typed arrays each iteration:
Now, the
this.hull
one is pretty small, so let's just pretend it compensates for the -5 in themaxTriangles
calculation. Then forn
points, we allocate the equivalent ofUint32Array(10*n) + Int32Array(6*n + Math.sqrt(n)) + Float64Array(n)
. For a single allocation this is not a big deal, but I want to hook up that Voronoi stippling algorithm to my webcam. At 60FPS, you start to notice this!And when using Lloyd's Algorithm the number of coordinates remains the same anyway, so it would make a lot more sense to just re-use those arrays.
A hypothetical
update()
function would be almost identical to the currentt contructor, except it would have to check if the newcoords
is the same length as the old one, and then it would reset all typed arrays withTypedArray.prototype.fill(0)
.Perhaps there is a preference for
Delaunator
to remain immutable. Well, then one could make aMutableDelaunator
class, no?The text was updated successfully, but these errors were encountered: