Source code for upxo.meshing.gbconformant.d3v2p1.surface_intersections

"""Parallel triangle-intersection search with the same result as d3v2p0.

The candidate rule and the pair test are d3v2p0's. With numba the whole
search runs as one compiled kernel on numba threads (numba_intersections);
otherwise query triangles are split into chunks that worker processes search
independently, or the search runs serially. Every pair is decided by the
same test, so the reported pairs do not depend on the tier, the chunking or
the worker count.
"""
import numpy as np
from scipy.spatial import cKDTree
from ..d3v2p0.surface_intersections import _intersecting_pairs
from .backend import plan, numba_threads
from .parallel import run_chunks
from .mesh_tables import unique_rows


def _search(task):
    """Worker: d3v2p0's batched search for the query ids of one chunk."""
    points, triangles, tolerance, batch_size, query_ids, full = task
    xyz = points[triangles]
    centres = xyz.mean(axis=1)
    radii = np.linalg.norm(xyz - centres[:, None], axis=2).max(axis=1)
    lo, hi = xyz.min(axis=1), xyz.max(axis=1)
    bands = np.floor(np.log2(np.maximum(radii, max(tolerance, 1e-30)))).astype(int)
    trees = []
    for band in np.unique(bands):
        members = np.flatnonzero(bands == band)
        trees.append((members, cKDTree(centres[members]), float(radii[members].max())))
    found = []
    for start in range(0, len(query_ids), batch_size):
        ids = query_ids[start:start + batch_size]
        for members, tree, maximum_radius in trees:
            neighbours = tree.query_ball_point(centres[ids], radii[ids] + maximum_radius + tolerance)
            sizes = np.array([len(n) for n in neighbours])
            if not sizes.sum():
                continue
            a = np.repeat(ids, sizes)
            b = members[np.concatenate(neighbours).astype(int)]
            keep = ((a < b) if full else (a != b)) & np.all(lo[a] <= hi[b] + tolerance, axis=1) \
                & np.all(lo[b] <= hi[a] + tolerance, axis=1)
            candidates = unique_rows(np.sort(np.column_stack((a[keep], b[keep])), axis=1))
            found.append(candidates[_intersecting_pairs(points, triangles, candidates, tolerance)])
    return np.vstack(found) if found else np.empty((0, 2), int)


[docs] def find_surface_intersections(points, triangles, tolerance=None, batch_size=1024, triangle_ids=None, n_workers=None, backend='auto'): """d3v2p0.surface_intersections.find_surface_intersections searched in parallel. backend / n_workers: see d3v2p1.backend (None = automatic; searches under 4096 query triangles per worker run serially). The numba tier runs the whole search (candidate grid and pair test) as a compiled kernel on numba threads. Returns the same intersecting pairs as d3v2p0 for every tier and worker count.""" points = np.asarray(points) triangles = np.asarray(triangles) if not len(triangles): return np.empty((0, 2), int) tolerance = 1e-9 * max(1., float(np.ptp(points, axis=0).max())) if tolerance is None else tolerance full = triangle_ids is None query_ids = np.arange(len(triangles)) if full else np.unique(triangle_ids) # small searches are not worth starting processes (or many threads) for chosen = plan(backend, n_workers, items=len(query_ids), min_items_per_worker=4096, numba_kernels=True) if chosen.numba: from .numba_intersections import find_pairs # d3v2p0's radius bands: each triangle's band maximum enters the candidate rule xyz = points[triangles] radii = np.linalg.norm(xyz - xyz.mean(axis=1)[:, None], axis=2).max(axis=1) bands = np.floor(np.log2(np.maximum(radii, max(tolerance, 1e-30)))).astype(int) _, band_of = np.unique(bands, return_inverse=True) band_max = np.zeros(band_of.max() + 1) np.maximum.at(band_max, band_of, radii) with numba_threads(chosen.workers): pairs = find_pairs(points, triangles, query_ids, full, tolerance, band_max[band_of]) found = [pairs] if len(pairs) else [] else: chunks = [query_ids[k::chosen.workers] for k in range(chosen.workers)] tasks = [(points, triangles, tolerance, batch_size, c, full) for c in chunks if len(c)] found = [r for r in run_chunks(_search, tasks, chosen) if len(r)] return unique_rows(np.vstack(found)) if found else np.empty((0, 2), int)