Lloyd / Laplacian Relaxation Post-processing

Implemented function: lloyd_relax()  |  Method key: "poisson_disk_relaxed"

1. Motivation

The Bridson Poisson disk algorithm produces a distribution that satisfies the hard exclusion constraint (d_{ij} ≥ h), but the resulting node set is not as uniform as possible: large empty regions and irregular inter-node distances are common, especially for small k values. This non-uniformity can lead to:

Lloyd's algorithm (Lloyd 1982, also known as k-means smoothing or centroidal Voronoi tessellation optimization) is an iterative post-processing technique that improves node uniformity by repeatedly moving each interior node toward the centroid of its Voronoi cell. Applied to a Poisson disk point set, it produces a centroidal Voronoi tessellation (CVT) — a distribution where each node is the centroid of its own Voronoi cell, which is provably the most uniform node arrangement for a given number of points.

The lloyd_relax() function implements a simplified, computationally efficient variant: instead of computing Voronoi cells explicitly, it uses the Delaunay neighbours of each interior node as a proxy for the Voronoi cell vertices, and moves the node toward the centroid of its Delaunay neighbours (Laplacian smoothing). This approximation is much faster than full CVT optimization while still significantly improving node uniformity.

2. Mathematical Formulation

Let I = {x1, …, xNint} be the set of interior node positions, and B be the set of (fixed) boundary nodes. The Delaunay triangulation T is computed on all nodes.

At each iteration t, each interior node xi is updated by:

xi(t+1) = xi(t) + ω · (¯xi(t) - xi(t))

where:

Boundary nodes are held fixed throughout the iteration. The Delaunay triangulation is recomputed at each iteration to account for the updated node positions.

2.1 Convergence

The iteration stops when either:

3. Implementation Details

For planar (2D) clouds, the algorithm projects the 3D node positions onto the dominant 2D plane (by discarding the axis with minimum variance) before computing the Delaunay triangulation. This ensures numerical stability and correct connectivity even when the plane is not aligned with the coordinate axes.

Note on minimum distance: Lloyd/Laplacian relaxation does not guarantee that the minimum exclusion distance h is preserved after smoothing. In practice, for ω = 0.5 and few iterations, the minimum distance remains very close to h. For aggressive smoothing (high ω, many iterations), a small number of node pairs may end up slightly closer than h.

4. Geometry Containment: Two-Layer Strategy

Laplacian smoothing moves each interior node toward the centroid of its Delaunay neighbours. For nodes near hole edges or the outer boundary, this displacement may be directed toward — or even through — a geometric boundary, causing the node to end up inside a hole or outside the outer perimeter. The magnitude of the drift is typically ω · δ per iteration, where δ is the distance to the centroid, but in thin regions (narrow material strips between holes) it is possible for nodes to cross a hole boundary in a single step.

GearMaster uses a two-layer strategy to handle geometry containment robustly: an in-loop step-rejection constraint (active during iteration) and a post-relaxation cleanup filter (applied once after all iterations complete).

4.1 In-Loop Step Rejection (Per-Iteration Constraint)

When lloyd_relax() is called with an entity argument, the function builds a set of geometry constraints from the CAD shape before entering the iteration loop. Each constraint encodes the 2D boundary polygon and hole polygons of one planar face of the entity, expressed in that face's local 2D coordinate frame.

At each iteration, after computing the displacement step si = ω · (¯xi − xi) for each interior node, the tentative new position

xitentative = xi(t) + si

is projected into the 2D frame of each matching face and checked against the face's boundary polygon and holes using contains_points_with_holes(). If the tentative position is not contained in any valid face region, the step is rejected:

si = 0    if   xitentative ∉ domain

The node simply remains at its current position for that iteration and may be displaced in a future iteration if the Delaunay connectivity changes.

Generic design: The geometry constraints are built directly from the OCC CAD shape, not from any prior knowledge of the geometry type (e.g. whether holes are present or not). This makes the step-rejection constraint applicable to any planar face shape: rectangular, circular, with or without holes, with straight or curved (spline) edges. No geometry-specific code is required.

This constraint is the primary mechanism that prevents nodes from accumulating in invalid regions over multiple iterations. Without it, even a modest number of iterations (5–10) can push a significant fraction of boundary-adjacent interior nodes into holes in geometries with thin material strips between holes.

4.2 Post-Relaxation Cleanup Filter (_filter_post_relax)

After lloyd_relax() completes all iterations, the PointCloudGenerator applies a final containment filter (_filter_post_relax). The filter:

  1. Projects each relaxed interior node into the local 2D coordinate system of its face using the OCC plane normal and origin.
  2. Checks containment against the outer boundary polygon and all inner hole polygons using matplotlib.path.Path.contains_points() (ray casting).
  3. Removes any interior node that fails the containment test. Boundary nodes are always kept unchanged.
Role of the post-relax filter: When the in-loop constraint (Section 4.1) is active, the post-relax filter acts as a safety net for the rare cases where floating-point rounding or the discrete nature of the step-rejection check leaves a node marginally outside the domain after the final iteration. When the in-loop constraint is not active (no entity passed), the post-relax filter is the sole containment mechanism and may need to remove a larger number of drifted nodes.
Design choice — removal vs. projection: The filter removes drifted nodes rather than projecting them back to the nearest valid position. This is simpler and avoids introducing artificially clustered nodes near hole edges. In practice, the number of removed nodes is small, and the remaining distribution remains sufficiently uniform.

5. Parameters

When using interior_method="poisson_disk_relaxed", these parameters are applied at fixed default values. The n_relax_iter parameter of PointCloudGenerator controls the maximum number of iterations.

6. When to Use

Use "poisson_disk_relaxed" when improved node uniformity is desired over the raw Bridson output, without the overhead of a full Delaunay-based mesher. Typical use cases:

7. References