From 359ebc325ec243eb24807cc73ad638ed38088a89 Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 7 Mar 2026 07:52:38 -0800 Subject: [PATCH] [offset] add offsetting --- adze/__init__.py | 1 + adze/offset.py | 91 ++++++++++++++++++++++++++++++++++++++++ adze/test/test_offset.py | 46 ++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 adze/offset.py create mode 100644 adze/test/test_offset.py diff --git a/adze/__init__.py b/adze/__init__.py index fe8a1f3..070145e 100644 --- a/adze/__init__.py +++ b/adze/__init__.py @@ -97,6 +97,7 @@ from .boolean import ( difference_convex as difference_convex, union_convex as union_convex, ) +from .offset import offset as offset from .util import is_inside as is_inside __author__ = 'Jan Petykiewicz' diff --git a/adze/offset.py b/adze/offset.py new file mode 100644 index 0000000..b7c4de2 --- /dev/null +++ b/adze/offset.py @@ -0,0 +1,91 @@ +import numpy +from . import poly_t +from .util import find_faces +from .boundary import planes_from_boundary + +def offset(poly: poly_t, delta: float, miter_limit: float = 10.0) -> poly_t: + """ + Offset a polyhedron by a distance delta. + Positive delta expands the shape, negative delta shrinks (etches) it. + + Args: + poly: Input polyhedron in Schlegel representation. + delta: Offset distance. + miter_limit: Maximum allowed multiplier for displacement at sharp corners. + + Returns: + Offsetted polyhedron. + """ + if poly['vertices'].shape[0] == 0: + return poly + + vertices = poly['vertices'] + neighbors = poly['neighbors'] + num_verts = vertices.shape[0] + + # 1. Find all faces and their unit normals + faces = find_faces(neighbors) + + # Map vertex to which faces it belongs to + vertex_to_faces: list[list[int]] = [[] for _ in range(num_verts)] + face_normals = [] + + for face_idx, face in enumerate(faces): + # planes_from_boundary handles robust normal calculation via Newell's method. + # Note: adze convention for boundary representation usually has + # INWARD facing normals for clipping. + plane = planes_from_boundary(vertices, [face])[0] + face_normals.append(plane[0]) # plane[0] is unit normal + for v_idx in face: + vertex_to_faces[v_idx].append(face_idx) + + # 2. For each vertex, solve the 3x3 system + new_vertices = numpy.zeros_like(vertices) + + for i in range(num_verts): + f_indices = vertex_to_faces[i] + if len(f_indices) != 3: + # Fallback if topology is non-manifold + new_vertices[i] = vertices[i] + continue + + # Normal matrix A + A = numpy.array([face_normals[fi] for fi in f_indices]) + + # If n is INWARD normal, then to EXPAND (positive delta) we want to move + # AGAINST the normal. + # n . (V' - V) = -delta + # Let d = V' - V + # A d = -delta * [1, 1, 1]^T + + try: + # solve A d = -delta + rhs = numpy.full(3, -delta) + d = numpy.linalg.solve(A, rhs) + + # Check miter limit + d_norm = numpy.linalg.norm(d) + if abs(delta) > 1e-15 and d_norm > miter_limit * abs(delta): + # Clamp displacement + avg_n = numpy.mean(A, axis=0) + avg_n_norm = numpy.linalg.norm(avg_n) + if avg_n_norm > 1e-12: + # Move along average inward normal (negative for expansion) + new_vertices[i] = vertices[i] - (avg_n / avg_n_norm) * delta + else: + new_vertices[i] = vertices[i] + d * (miter_limit * abs(delta) / d_norm) + else: + new_vertices[i] = vertices[i] + d + except numpy.linalg.LinAlgError: + # Singular matrix + avg_n = numpy.mean(A, axis=0) + avg_n_norm = numpy.linalg.norm(avg_n) + if avg_n_norm > 1e-12: + new_vertices[i] = vertices[i] - (avg_n / avg_n_norm) * delta + else: + new_vertices[i] = vertices[i] + + return { + 'vertices': new_vertices, + 'neighbors': neighbors.copy() + } diff --git a/adze/test/test_offset.py b/adze/test/test_offset.py new file mode 100644 index 0000000..8a59e0a --- /dev/null +++ b/adze/test/test_offset.py @@ -0,0 +1,46 @@ +import numpy +import adze +from adze.shapes import build_box +from adze.offset import offset + +def test_offset_cube_expand() -> None: + # Unit cube: [0,1]x[0,1]x[0,1], vol=1 + cube = build_box([[0, 0, 0], [1, 1, 1]]) + + # Expand by 0.1 on all sides + # New cube: [-0.1, 1.1] on all axes + # New side length = 1.2 + # Expected volume = 1.2^3 = 1.728 + expanded = offset(cube, 0.1) + + vol = adze.get_volume(expanded) + assert numpy.isclose(vol, 1.728) + + # Check bounds + vmin, vmax = adze.util.get_aabb(expanded) + assert numpy.allclose(vmin, [-0.1, -0.1, -0.1]) + assert numpy.allclose(vmax, [1.1, 1.1, 1.1]) + +def test_offset_cube_shrink() -> None: + # Unit cube: [0,1]x[0,1]x[0,1], vol=1 + cube = build_box([[0, 0, 0], [1, 1, 1]]) + + # Shrink by 0.1 on all sides + # New cube: [0.1, 0.9] on all axes + # New side length = 0.8 + # Expected volume = 0.8^3 = 0.512 + shrunk = offset(cube, -0.1) + + vol = adze.get_volume(shrunk) + assert numpy.isclose(vol, 0.512) + + # Check bounds + vmin, vmax = adze.util.get_aabb(shrunk) + assert numpy.allclose(vmin, [0.1, 0.1, 0.1]) + assert numpy.allclose(vmax, [0.9, 0.9, 0.9]) + +def test_offset_zero() -> None: + cube = build_box([[0, 0, 0], [1, 1, 1]]) + same = offset(cube, 0.0) + assert numpy.isclose(adze.get_volume(same), 1.0) + assert numpy.allclose(cube['vertices'], same['vertices'])