Skip to main content

SdfGrid

Struct SdfGrid 

Source
pub struct SdfGrid {
    pub nx: usize,
    pub ny: usize,
    pub nz: usize,
    pub dx: f64,
    pub origin: [f64; 3],
    pub values: Vec<f64>,
}
Expand description

A 3-D signed distance field stored on a uniform Cartesian grid.

Fields§

§nx: usize

Number of cells in the x direction.

§ny: usize

Number of cells in the y direction.

§nz: usize

Number of cells in the z direction.

§dx: f64

Uniform cell spacing (same in all directions).

§origin: [f64; 3]

World-space coordinate of the (0,0,0) grid corner.

§values: Vec<f64>

Flat storage: index = i*ny*nz + j*nz + k.

Implementations§

Source§

impl SdfGrid

Source

pub fn new(nx: usize, ny: usize, nz: usize, dx: f64, origin: [f64; 3]) -> Self

Create a new grid filled with f64::MAX.

Source

pub fn index(&self, i: usize, j: usize, k: usize) -> usize

Flat index for cell (i, j, k).

Source

pub fn world_pos(&self, i: usize, j: usize, k: usize) -> [f64; 3]

World-space centre of cell (i, j, k).

Source

pub fn get(&self, i: usize, j: usize, k: usize) -> f64

Read the SDF value at (i, j, k).

Source

pub fn set(&mut self, i: usize, j: usize, k: usize, v: f64)

Write the SDF value at (i, j, k).

Source

pub fn compute_sphere_sdf(&mut self, center: [f64; 3], radius: f64)

Fill the grid with the signed distance to a sphere.

Source

pub fn compute_box_sdf(&mut self, box_center: [f64; 3], half_extents: [f64; 3])

Fill the grid with the signed distance to an axis-aligned box.

Source

pub fn compute_cylinder_sdf( &mut self, center: [f64; 2], radius: f64, half_height: f64, )

Fill the grid with the signed distance to an infinite cylinder aligned along the z-axis.

Source

pub fn compute_torus_sdf( &mut self, center: [f64; 3], major_radius: f64, minor_radius: f64, )

Fill the grid with the signed distance to a torus centred at the origin in the xz-plane.

Source

pub fn gradient_at(&self, i: usize, j: usize, k: usize) -> [f64; 3]

Estimate the gradient of the SDF at (i, j, k) using central differences.

Source

pub fn total_cells(&self) -> usize

Total number of cells in the grid.

Source

pub fn sample(&self, pos: [f64; 3]) -> Option<f64>

Trilinear interpolation of the SDF at an arbitrary world-space point.

Returns None if the point is outside the grid.

Source

pub fn gradient_at_point(&self, pos: [f64; 3]) -> Option<[f64; 3]>

Compute the gradient at an arbitrary point using trilinear interpolation of gradient values.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.