Skip to content

4. Signed distance ​

With the dragon placed in the domain, the first question a grid asks is how far each point is from the wall, and on which side. This chapter queries single points: five on an axis, three with every sign algorithm, one in full detail.

The distance of a point ​

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

implicit none
type(surface_stl_object) :: surface
type(vector_R8P)         :: p(0:4), foot(0:4)
real(R8P)                :: d_pn, d_wn
logical                  :: inside(0:4)
integer(I4P)             :: k, u
character(16)            :: label

call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
print '(A)', '    y      d (pseudo-n.)  d (winding n.) inside'
do k = 0, 4
   p(k) = 0._R8P * ex_R8P + (-0.6_R8P + 0.3_R8P * k) * ey_R8P + 0._R8P * ez_R8P
   d_pn = surface%distance(point=p(k), is_signed=.true., is_square_root=.true.)
   d_wn = surface%distance(point=p(k), is_signed=.true., is_square_root=.true., sign_algorithm=SIGN_FAST_WINDING_NUMBER)
   inside(k) = surface%is_point_inside(point=p(k), sign_algorithm=SIGN_FAST_WINDING_NUMBER)
   print '(F7.3,2F15.6,L5)', p(k)%y, d_pn, d_wn, inside(k)
enddo
  • distance(point=) returns the distance from a point to the closest facet, found by a best-first traversal of the AABB tree. The defaults are unsigned and squared: pass is_square_root=.true. for the distance itself and is_signed=.true. for a sign, negative inside.
  • sign_algorithm= chooses how the sign is decided (next section); the default is SIGN_PSEUDO_NORMAL.
  • is_point_inside(point=, sign_algorithm=) returns the inside test alone, with the same algorithms and the same default.
$ tut04_a_distance
    y      d (pseudo-n.)  d (winding n.) inside
 -0.600       0.150398       0.150398    F
 -0.300      -0.000289      -0.000289    T
  0.000      -0.015419      -0.015419    T
  0.300      -0.002330      -0.002330    T
  0.600       0.114892       0.114892    F

The five points on the y axis go from outside (0.150398) through the body to outside again (0.114892). The three inner points are inside by 0.000289, 0.015419 and 0.002330: along this axis the body is thin, and the point at y = -0.3 lies almost on the wall. The two sign algorithms agree at these points.

the placed dragon drawn translucent, seen nearly along x, with the y axis from -0.6 to 0.6 and its five points: the two ends outside in ember, the three inside in teal, each joined by a gold segment to its closest point on the wall

A probe moved around the body shows the two answers together. In the animation it travels an ellipse in the plane x = 0; the segment joins it to its closest point, and its colour gives the side.

a probe travelling an ellipse in the plane x = 0 through the translucent dragon, 24 frames: the probe in teal inside and ember outside, a gold segment to its closest point on the wall, the signed distance in the caption, from 0.0953 outside to -0.0930 inside

Inside or outside: four algorithms ​

The sign is a separate question from the distance, and FOSSIL answers it four ways:

  • SIGN_PSEUDO_NORMAL (the default): the angle-weighted pseudo-normal of the closest face, edge or vertex, read in the same traversal. Exact on a closed, outward-oriented, manifold surface.
  • SIGN_FAST_WINDING_NUMBER: inside when the winding number of the surface around the point exceeds 0.5, evaluated hierarchically on the tree. It tolerates holes and gaps.
  • SIGN_RAY_INTERSECTIONS and SIGN_SOLID_ANGLE: ray parity and the solid-angle sum, O(N) per query.
f90
use fossil,     only : surface_stl_object, SIGN_PSEUDO_NORMAL, SIGN_FAST_WINDING_NUMBER, SIGN_RAY_INTERSECTIONS, &
                       SIGN_SOLID_ANGLE
use penf,       only : I4P, R8P
use vecfor,     only : vector_R8P, ex_R8P, ey_R8P, ez_R8P

implicit none
integer(I4P), parameter  :: ALG(4) = [SIGN_PSEUDO_NORMAL, SIGN_FAST_WINDING_NUMBER, SIGN_RAY_INTERSECTIONS, &
                                      SIGN_SOLID_ANGLE]
type(surface_stl_object) :: surface
type(vector_R8P)         :: p(3), foot, normal
real(R8P)                :: d
integer(I4P)             :: k, a, u

call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
p(1) = 0._R8P * ex_R8P
p(2) = 0.6_R8P * ey_R8P
p(3) = 0.36_R8P * ex_R8P - 0.2_R8P * ey_R8P + 0.2_R8P * ez_R8P
print '(A)', '        point          pseudo-n.  winding n.  rays  solid angle'
do k = 1, 3
   print '(3F7.3,4L10)', p(k)%x, p(k)%y, p(k)%z, &
                         (surface%is_point_inside(point=p(k), sign_algorithm=ALG(a)), a = 1, 4)
enddo
$ tut04_b_signs
        point          pseudo-n.  winding n.  rays  solid angle
  0.000  0.000  0.000         T         T         T         T
  0.000  0.600  0.000         F         F         F         F
  0.360 -0.200  0.200         T         F         F         F
point 3: pseudo-normal d  -0.2833, vertex pseudo-normal  -0.6886 -0.0309  0.7245

The origin is inside and the point beyond the head outside by every algorithm. Point 3, (0.36, -0.2, 0.2), lies 0.2833 outside the dragon by three of them, and inside by the pseudo-normal. Its closest feature is a vertex, and the pseudo-normal of that vertex, (-0.6886, -0.0309, 0.7245), points away from the point: the dragon is still not a closed manifold after chapter 2, and a sign read from one feature inherits its defects. The winding number and the two O(N) methods look at the whole surface instead. Chapter 5 counts how often this happens on a grid.

the placed dragon drawn translucent with point 3 in ember, a gold segment to its closest point (a vertex, gold), and the vertex pseudo-normal in teal, pointing away from the point: the pseudo-normal sign calls the point inside

The closest feature ​

f90
use fossil,              only : surface_stl_object
use fossil_facet_object, only : facet_object
use penf,                only : I4P, R8P
use vecfor,              only : vector_R8P, ex_R8P, ey_R8P, ez_R8P

implicit none
type(surface_stl_object)    :: surface
type(facet_object), pointer :: f
type(vector_R8P)            :: p, closest, normal
real(R8P)                   :: d
integer(I4P)                :: facet, edge, vertex, i, u

call surface%load_from_file(file_name='dragon-placed.stl', guess_format=.true.)
p = 0._R8P * ex_R8P + 0.6_R8P * ey_R8P + 0._R8P * ez_R8P
call surface%compute_distance(point=p, distance=d, is_signed=.true., is_square_root=.true., &
                              facet_index=facet, edge_index=edge, vertex_index=vertex, closest_point=closest, normal=normal)
print '(A,F10.6,A,I0,A,I0,A,I0)', 'd ', d, '  facet ', facet, '  edge ', edge, '  vertex ', vertex
print '(A,3F10.6)', 'closest point ', closest%x, closest%y, closest%z
print '(A,3F10.6)', 'normal        ', normal%x, normal%y, normal%z
f => surface%facet_at(facet)
print '(A,3F10.6)', 'facet normal  ', f%normal%x, f%normal%y, f%normal%z
  • compute_distance is the subroutine form with the details of the closest feature: facet_index, the local edge_index (1 to 3, 0 when not on an edge) and vertex_index (1 to 3, 0 when not at a vertex), the closest_point and the pseudo-normal normal of that feature. Chapter 8 gets the same for many points at once.
$ tut04_c_closest
d   0.114892  facet 1289  edge 0  vertex 3
closest point  -0.017632  0.500000  0.053752
normal         -0.014229  0.824610 -0.565522
facet normal   -0.430060 -0.071374 -0.899975

The point (0, 0.6, 0) is closest to vertex 3 of facet 1289, at the tip of the tail, so its normal is the vertex pseudo-normal (-0.0142, 0.8246, -0.5655), not the normal of facet 1289, (-0.4301, -0.0714, -0.9000). The pseudo-normal is the angle-weighted mean of the normals of every facet at the vertex; it points nearly at the query point, which is why the sign it gives is right on a closed surface.

the tip of the dragon's tail close up at the closest point, a vertex (gold): the direction to the query point in gold, the vertex pseudo-normal in teal close to it, and the normal of facet 1289 in grey, pointing down

What you learned

distance is unsigned and squared unless asked otherwise; sign_algorithm picks the inside test, and the pseudo-normal one needs a closed manifold; compute_distance adds the closest facet, feature, point and normal. Reference: Generalized winding number, Sign algorithms at sharp features.

Next: 5. Batched queries.