Closed JobLeonard closed 5 years ago
That's a reasonable request — I'll look into it.
I looked at the source-code for d3-delaunay
, which is used in that notebook. The code wrapping Delaunator also does this superfluous allocatoin to some extent. Maybe it makes more sense for me to take the source code of both, manually inline, and then add the update function?
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 update
is called at least once?
The last approach could be done like this:
update
method, if the cached arrays are null
or too small for the number of points, (re)allocate them. Otherwise reuse previous arraysthis.update(coords)
, then set all caches to null
to release memoryDoes 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?