Module

Hylograph.Forces.ThreeD.Octree

Package
purescript-hylograph-forces
Repository
afcondon/purescript-hylograph-forces

The part of d3-octree that d3-force-3d uses: build a tree from a set of points, once, and hand it to a force to walk.

Ported from d3-octree 1.1.0 (MIT, Copyright 2017 Vasco Asturiano; itself from d3-quadtree, ISC, Mike Bostock); see LICENSE.

Built the way Hylograph.Forces.Quadtree builds its tree, from the top down and without mutation, and for the same reason: d3-octree splits a cube exactly when a second location lands in it, so the tree does not depend on the order of insertion. What does depend on it is kept: a leaf's points run newest first, and the extent starts from the unit cube at the floor of the smallest coordinates and doubles until it covers the largest. d3-octree's cover measures the side by x alone (t = x1 - x0 || 1) and doubles all three axes together, so the extent is always a cube.

The eight octants of a branch are in d3-octree's order, the index being deep << 2 | bottom << 1 | right, where right is x >= middle, bottom y >= middle and deep z >= middle. Its visit takes them in that order.

One thing d3-quadtree does not do. addAll keeps the coordinates in Float64Arrays, and a point with a NaN coordinate is skipped when they are filled, so its slots stay 0: it is left out of the extent, and then inserted all the same, at (0, 0, 0). (d3-quadtree's plain arrays leave the slot undefined, and add drops the point.) Such a point descends the tree as if it were at the origin, but where it is the leaf that a later point is compared with or split from, its own coordinates are read again (xp = +tree._x.call(null, node.data)), and they are NaN. Where it lands then depends on the order of insertion, so when any point has a NaN coordinate this module builds the tree as d3-octree does, one point at a time (insert).

#Point Source

type Point item = { item :: item, x :: Number, y :: Number, z :: Number }

A location and whatever the force keeps there.

#Octree Source

data Octree item

An octree. A Leaf holds its newest point and then the older points at the same location, newest first. A Branch holds its eight octants in d3-octree's order (see the module's head).

Constructors

#Extent Source

type Extent = { x0 :: Number, x1 :: Number, y0 :: Number, y1 :: Number, z0 :: Number, z1 :: Number }

The cube a tree covers.

#Planted Source

type Planted item = { extent :: Extent, root :: Octree item }

A tree and the cube its root covers.

#build Source

build :: forall item. List (Point item) -> Planted item

d3-octree's octree(nodes, x, y, z): the tree of the given points, listed newest first (the reverse of d3's insertion order). A point with any coordinate NaN is left out, as d3-octree leaves it out.