Class MeshSignedDistanceGrid
  
  Compute discretely-sampled (ie gridded) signed distance field for a mesh
The basic approach is, first compute exact distances in a narrow band, and then
extend out to rest of grid using fast "sweeping" (ie like a distance transform).
The resulting unsigned grid is then signed using ray-intersection counting, which
is also computed on the grid, so no BVH is necessary
If you set ComputeMode to NarrowBandOnly, result is a narrow-band signed distance field.
This is quite a bit faster as the sweeping is the most computationally-intensive step.
Caveats:
- the "narrow band" is based on triangle bounding boxes, so it is not necessarily
that "narrow" if you have large triangles on a diagonal to grid axes
 
Potential optimizations:
- Often we have a spatial data structure that would allow faster computation of the
narrow-band distances (which become quite expensive if we want a wider band!)
Not clear how to take advantage of this though. Perhaps we could have a binary
grid that, in first pass, we set bits inside triangle bboxes to 1? Or perhaps
same as current code, but we use spatial-dist, and so for each ijk we only compute once?
(then have to test for computed value at each cell of each triangle...)
 
This code is based on the C++ implementation found at https://github.com/christopherbatty/SDFGen
Original license was public domain. 
Permission granted by Christopher Batty to include C# port under Boost license.
 
  
  
    Inheritance
    
    MeshSignedDistanceGrid
   
  
  Namespace: g3
  Assembly: cs.temp.dll.dll
  Syntax
  
    public class MeshSignedDistanceGrid
   
  Constructors
  
  
  
  
  MeshSignedDistanceGrid(DMesh3, Double, DMeshAABBTree3)
  
  
  Declaration
  
    public MeshSignedDistanceGrid(DMesh3 mesh, double cellSize, DMeshAABBTree3 spatial = null)
   
  Parameters
  
  Fields
  
  
  
  CancelF
  if this function returns true, we should abort calculation 
 
  
  Declaration
  
    public Func<bool> CancelF
   
  Field Value
  
  
  
  CellSize
  
  
  Declaration
  
  Field Value
  
  
  
  ComputeMode
  
  
  Declaration
  
    public MeshSignedDistanceGrid.ComputeModes ComputeMode
   
  Field Value
  
  
  
  ComputeSigns
  
  
  Declaration
  
  Field Value
  
  
  
  DebugPrint
  
  
  Declaration
  
  Field Value
  
  
  
  ExactBandWidth
  
  
  Declaration
  
    public int ExactBandWidth
   
  Field Value
  
  
  
  ExpandBounds
  
  
  Declaration
  
    public Vector3d ExpandBounds
   
  Field Value
  
  
  
  InsideMode
  
  
  Declaration
  
    public MeshSignedDistanceGrid.InsideModes InsideMode
   
  Field Value
  
  
  
  Mesh
  
  
  Declaration
  
  Field Value
  
  
  
  NarrowBandMaxDistance
  
  
  Declaration
  
    public double NarrowBandMaxDistance
   
  Field Value
  
  
  
  Spatial
  
  
  Declaration
  
    public DMeshAABBTree3 Spatial
   
  Field Value
  
  
  
  UseParallel
  
  
  Declaration
  
  Field Value
  
  
  
  WantClosestTriGrid
  
  
  Declaration
  
    public bool WantClosestTriGrid
   
  Field Value
  
  
  
  WantIntersectionsGrid
  
  
  Declaration
  
    public bool WantIntersectionsGrid
   
  Field Value
  
  Properties
  
  
  
  
  ClosestTriGrid
  
  
  Declaration
  
    public DenseGrid3i ClosestTriGrid { get; }
   
  Property Value
  
  
  
  
  Dimensions
  
  
  Declaration
  
    public Vector3i Dimensions { get; }
   
  Property Value
  
  
  
  
  Grid
  SDF grid available after calling Compute()
 
  
  Declaration
  
    public DenseGrid3f Grid { get; }
   
  Property Value
  
  
  
  
  GridOrigin
  Origin of the SDF grid, in same coordinates as mesh
 
  
  Declaration
  
    public Vector3f GridOrigin { get; }
   
  Property Value
  
  
  
  
  IntersectionsGrid
  
  
  Declaration
  
    public DenseGrid3i IntersectionsGrid { get; }
   
  Property Value
  
  
  
  
  Item[Vector3i]
  
  
  Declaration
  
    public float this[Vector3i idx] { get; }
   
  Parameters
  
  Property Value
  
  
  
  
  Item[Int32, Int32, Int32]
  
  
  Declaration
  
    public float this[int i, int j, int k] { get; }
   
  Parameters
  
  Property Value
  
  Methods
  
  
  
  
  CellCenter(Int32, Int32, Int32)
  
  
  Declaration
  
    public Vector3f CellCenter(int i, int j, int k)
   
  Parameters
  
  Returns
  
  
  
  
  Compute()
  
  
  Declaration
  
  
  
  
  orientation(Double, Double, Double, Double, out Double)
  
  
  Declaration
  
    public static int orientation(double x1, double y1, double x2, double y2, out double twice_signed_area)
   
  Parameters
  
  Returns
  
  
  
  
  point_in_triangle_2d(Double, Double, Double, Double, Double, Double, Double, Double, out Double, out Double, out Double)
  
  
  Declaration
  
    public static bool point_in_triangle_2d(double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, out double a, out double b, out double c)
   
  Parameters
  
  Returns
  
  
  
  
  point_segment_distance(ref Vector3d, ref Vector3d, ref Vector3d)
  
  
  Declaration
  
    public static double point_segment_distance(ref Vector3d x0, ref Vector3d x1, ref Vector3d x2)
   
  Parameters
  
  Returns
  
  
  
  
  point_segment_distance(ref Vector3f, ref Vector3f, ref Vector3f)
  
  
  Declaration
  
    public static float point_segment_distance(ref Vector3f x0, ref Vector3f x1, ref Vector3f x2)
   
  Parameters
  
  Returns
  
  
  
  
  point_triangle_distance(ref Vector3d, ref Vector3d, ref Vector3d, ref Vector3d)
  
  
  Declaration
  
    public static double point_triangle_distance(ref Vector3d x0, ref Vector3d x1, ref Vector3d x2, ref Vector3d x3)
   
  Parameters
  
  Returns
  
  
  
  
  point_triangle_distance(ref Vector3f, ref Vector3f, ref Vector3f, ref Vector3f)
  
  
  Declaration
  
    public static float point_triangle_distance(ref Vector3f x0, ref Vector3f x1, ref Vector3f x2, ref Vector3f x3)
   
  Parameters
  
  Returns