Skip to content

5. Batched queries ​

A grid asks for the distance of every one of its points, not of five. distance_many takes the whole batch in one call. This chapter runs it on 16³ points over the domain, then on both kinds of AABB tree, then with both device-ready sign algorithms.

A grid in one call ​

f90
use fossil,     only : surface_stl_object, SIGN_FAST_WINDING_NUMBER, STATUS_OK
use penf,       only : I4P, R8P
use vecfor,     only : vector_R8P, ex_R8P, ey_R8P, ez_R8P

implicit none
integer(I4P), parameter       :: n = 16                      ! points per direction
type(surface_stl_object)      :: surface, light
type(vector_R8P), allocatable :: points(:)
real(R8P),        allocatable :: d(:)
real(R8P)                     :: h
integer(I4P)                  :: i, j, k, p, status, u
character(80)                 :: caption

allocate(points(n**3), d(n**3))
h = 1.2_R8P / (n - 1)                                        ! the cube [-0.6, 0.6]^3
p = 0
do k = 1, n ; do j = 1, n ; do i = 1, n
   p = p + 1
   points(p) = (-0.6_R8P + (i - 1) * h) * ex_R8P + (-0.6_R8P + (j - 1) * h) * ey_R8P + (-0.6_R8P + (k - 1) * h) * ez_R8P
enddo ; enddo ; enddo
call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
call surface%distance_many(points=points, distances=d, is_signed=.true., is_square_root=.true., &
                           sign_algorithm=SIGN_FAST_WINDING_NUMBER, status=status)
if (status /= STATUS_OK) error stop 'distance_many failed'
print '(A,I0)',     'points  ', size(points)
print '(A,I0)',     'inside  ', count(d < 0._R8P)
print '(A,2F10.6)', 'min, max d ', minval(d), maxval(d)
  • distance_many(points=, distances=) is a loop of compute_distance over points, with the same is_signed, sign_algorithm and is_square_root for every point (unsigned and squared by default, as distance). distances is allocated by the caller, with the size of points; a size mismatch gives status = STATUS_INVALID_INPUT and leaves distances untouched.
  • The loop is an OpenMP parallel do: the tree is read-only and each iteration writes its own slot. Built with -fopenmp it runs on all threads, without it serially, with the same results.
  • The open dragon needs SIGN_FAST_WINDING_NUMBER (chapter 4); the last section shows why on this grid.
$ tut05_a_grid
points  4096
inside  108
min, max d  -0.069887  0.776508

108 of the 4096 points are inside, the deepest 0.069887 from the wall; the farthest point is 0.776508 away. The animation walks the batch plane by plane, each point drawn as a small square coloured by its distance.

the 16 by 16 by 16 batch drawn one z-plane per frame inside the domain cube, over the translucent dragon: each grid point a small square coloured by its signed distance, teal inside, cream on the wall, ember far outside; the caption gives the plane and the range of points of the batch it holds

Two kinds of tree ​

aabb_tree_kind= on load_from_file (also on analyze and adopt_facets) selects the tree that every query traverses:

  • AABB_TREE_SAH_BVH (the default): a binary tree built by splitting the facets, at each node, where the surface-area heuristic says a traversal is cheapest. Its boxes are tight and overlap.
  • AABB_TREE_OCTREE: the older 8-way partition of space into equal cubes, to the depth refinement_levels sets. It is kept for comparison; the BVH is the faster one and the only one with a device path (chapter 16).
f90
use fossil,                  only : surface_stl_object, AABB_TREE_OCTREE, AABB_TREE_SAH_BVH
use penf,                    only : I4P, R8P
use vecfor,                  only : vector_R8P, ex_R8P, ey_R8P, ez_R8P

implicit none
integer(I4P), parameter       :: n = 16                      ! points per direction
type(surface_stl_object)      :: bvh, octree
type(vector_R8P), allocatable :: points(:)
real(R8P),        allocatable :: d_bvh(:), d_oct(:)
real(R8P)                     :: h
integer(I4P)                  :: i, j, k, p

allocate(points(n**3), d_bvh(n**3), d_oct(n**3))
h = 1.2_R8P / (n - 1)                                        ! the cube [-0.6, 0.6]^3
p = 0
do k = 1, n ; do j = 1, n ; do i = 1, n
   p = p + 1
   points(p) = (-0.6_R8P + (i - 1) * h) * ex_R8P + (-0.6_R8P + (j - 1) * h) * ey_R8P + (-0.6_R8P + (k - 1) * h) * ez_R8P
enddo ; enddo ; enddo
call bvh%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)       ! default AABB_TREE_SAH_BVH
call octree%load_from_file(file_name='dragon-placed.stl', guess_format=.true., aabb_tree_kind=AABB_TREE_OCTREE)
call bvh%distance_many(points=points, distances=d_bvh, is_signed=.true., is_square_root=.true.)
call octree%distance_many(points=points, distances=d_oct, is_signed=.true., is_square_root=.true.)
print '(A,I0)',     'points                ', size(points)
print '(A,ES10.3)', 'max ||bvh| - |oct||  ', maxval(abs(abs(d_bvh) - abs(d_oct)))
print '(A,I0)',     'bvh, oct signs differ ', count(d_bvh * d_oct < 0._R8P)
$ tut05_b_trees
points                4096
max ||bvh| - |oct||   0.000E+00
bvh, oct signs differ 1
SAH BVH: 16 boxes 4 levels below the root, 313 nodes in all
octree: 50 boxes 2 levels below the root, 59 nodes in all

The two trees give the same distance magnitudes bit for bit: the traversal never prunes the true closest facet. One sign differs. At that point several facets are equally close, the trees report different ones, and on a non-manifold surface their pseudo-normals disagree.

The trees are private to the surface, but tree_boxes reads them: the boxes of every node down to max_depth, breadth first from the root, with their depth (and, optionally, the leaf flags, the parents and the facets of the leaves). The figures draw each surface's own tree with it:

f90
call draw(bvh, 'tut05-bvh', 'SAH BVH', 4_I4P)
call draw(octree, 'tut05-octree', 'octree', 2_I4P)
contains
   subroutine draw(s, name, title, depth)
   !< The boxes of the tree of s `depth` levels below the root, over the dragon.
   type(surface_stl_object), intent(in) :: s           !< Surface.
   character(*),             intent(in) :: name, title !< Figure name, tree name.
   integer(I4P),             intent(in) :: depth       !< Levels below the root.
   type(vector_R8P), allocatable        :: bmin(:), bmax(:)
   integer(I4P),     allocatable        :: level(:)
   integer(I4P)                         :: c, u, all
   character(96)                        :: caption

   call s%tree_boxes(bmin=bmin, bmax=bmax)                    ! the whole tree
   all = size(bmin)
   call s%tree_boxes(bmin=bmin, bmax=bmax, depth=level, max_depth=depth)
   write(caption, '(A,A,I0,A,I0,A,I0,A)') title, ': ', count(level == depth), ' boxes ', depth, &
                                          ' levels below the root, ', all, ' nodes in all'
   print '(A)', trim(caption)

the SAH BVH of the placed dragon: the 16 boxes four levels below the root, each wrapping a group of facets tightly, overlapping where the body folds; 313 nodes in all

the octree of the placed dragon: the 50 non-empty cells two levels below the root, equal cubes of the space around the body whether it fills them or not; 59 nodes in all

The timing is not printed: it depends on the machine. The figures are in GPU distance queries (the BVH is about 110 times faster than the octree on a mesh of 24k facets).

Pseudo-normal or winding number ​

f90
call surface%distance_many(points=points, distances=d_pn, is_signed=.true., is_square_root=.true.)  ! SIGN_PSEUDO_NORMAL
call surface%distance_many(points=points, distances=d_wn, is_signed=.true., is_square_root=.true., &
                           sign_algorithm=SIGN_FAST_WINDING_NUMBER)
print '(A,I5,A,F10.6)', 'pseudo-normal:  inside ', count(d_pn < 0._R8P), '  min d ', minval(d_pn)
print '(A,I5,A,F10.6)', 'winding number: inside ', count(d_wn < 0._R8P), '  min d ', minval(d_wn)
print '(A,I0,A,ES10.3)', 'signs differ at ', count(d_pn * d_wn < 0._R8P), ' points, max ||d_pn| - |d_wn|| ', &
                        maxval(abs(abs(d_pn) - abs(d_wn)))
$ tut05_c_signs
pseudo-normal:  inside   152  min d  -0.647299
winding number: inside   108  min d  -0.069887
signs differ at 50 points, max ||d_pn| - |d_wn||  0.000E+00

The two sign algorithms give the same magnitudes and different signs at 50 of the 4096 points. The pseudo-normal sign puts 152 points inside, one of them at d = -0.647, deeper than the dragon is wide: a sign read from the closest feature is wrong where the surface is not a closed manifold. The fast winding number puts 108 points inside, none deeper than 0.070. On a surface that is not closed, use SIGN_FAST_WINDING_NUMBER; the rest of the tutorial does.

the grid points the pseudo-normal sign puts inside the dragon (a translucent ghost in the domain cube): teal where the fast winding number agrees, ember for the 47 it puts outside, most of them in clusters far from the body

the grid points the fast winding number puts inside: 108, all within the body, three of them in ember where the pseudo-normal disagrees

Most ember points of the left figure lie far from the body, near the corners of the domain. Each of the 47 takes its sign from the pseudo-normal of a vertex or an edge of the dragon, like point 3 of chapter 4; one wrong pseudo-normal flips every point whose closest feature it is.

What you learned

distance_many answers a whole grid in one OpenMP call; the SAH BVH splits facets, the octree splits space, and they agree on the distances; the pseudo-normal sign needs a closed surface, the fast winding number does not. Reference: surface_stl_object.

Next: 6. A distance field.