Source code for pyvcad_metamaterials.fields

"""Scalar attribute coercion used by metamaterial builders."""

# OpenVCAD
import pyvcad as pv


def _coerce_attribute(value):
    # One conversion path for every scalar control: numbers, FloatAttributes, ScalarParameters,
    # numba cfuncs, and plain callables compiled with numba (see pv.as_float_attribute).
    return pv.as_float_attribute(value)


[docs] def distance_field(body, voxel_size=0.5, bandwidth=10.0, signed=True): """Return a fast native ``pv.FloatAttribute`` giving the distance in mm to a body node. This is ``pv.FloatAttribute.distance_to(body, ...)``, the core spelling, kept here so lattice scripts can stay within ``mm``. Use it to grade lattices by distance from another part, for example strut radius or wall thickness falling off with distance from a head form:: radius = mm.distance_field(head, bandwidth=30.0).map_range(0.0, 30.0, 1.2, 0.5).clamp(0.5, 1.2) The body (any node, including meshes and CAD) is cloned and prepared with ``voxel_size``; your node is not modified. The value is signed (negative inside) unless ``signed=False``. It is accurate within ``bandwidth`` of the body's bounding box; farther away a monotone lower bound is returned, so choose ``bandwidth`` at least as large as the distances you grade over. """ return pv.FloatAttribute.distance_to(body, voxel_size=float(voxel_size), bandwidth=float(bandwidth), signed=bool(signed))