2-D geometry (curves)
What it does
A 2-D solver on a quadtree needs the same geometry queries as a 3-D one on an octree: distance, inside/outside, a signed-distance field. FOSSIL answers them natively in 2-D with curve_object, a set of closed polylines (loops) in an axis-aligned plane.
It does not extrude the curve into an STL. That would make every 2-D query pay for 3-D triangles. It would also put the query plane along the extrusion's facet edges, where sign and closest-feature decisions are most fragile.
What it covers:
- curves: loops, metrics, validity, an ASCII format;
- queries: signed distance with closest point, segment and vertex; generalized winding number; inside test; exact self-intersections. They run on a segment BVH;
- slicing an STL by an axis-aligned plane, so a 2-D and a 3-D run share one body;
- repair: joining and closing open chains, merging vertices, orienting loops;
- batches of distances on the CPU (OpenMP) or the device (OpenACC);
- fields: 2-D signed-distance fields with
distance_field_object, one quadtree block at a time.
Two bodies, three loops, area 4.27099: distance_many gives the 5000 cell-centre distances, from −0.575 to 1.330, 1712 of them inside; compute_distance the six feet, e.g. the point (0.10, 0.05) in the hole at d = 0.25 from the hole's top side (fig_ref_2d_geometry_queries.f90).
Conventions
- Plane. A curve lives in the plane normal to
normal_axis(1, 2 or 3; default 3). Its in-plane coordinates (u, v) are the other two axes in cyclic order: (y, z), (z, x) or (x, y), so u × v points along +normal_axis. Points are passed asp(2) = [u, v]. - Orientation. The solid is on the left of every segment. Outer loops run counter-clockwise, holes clockwise, and islands inside holes counter-clockwise again. The signed distance is negative inside, as in 3-D.
- Open loops. A loop may be open: a polyline, such as a slice of a mesh with holes. An open loop has no closing segment and no area, and
check_validityreports it. The distance and the generalized winding number still handle it, and the fast winding number sign is meant for it. - Validity.
check_validityreturns the first defect loop by loop (open loop, too few vertices, zero-length segment, zero area, wrong orientation for the nesting depth). By default it does not look for crossings;check_validity(check_self_intersections=.true.)also returnsCURVE_SELF_INTERSECTINGwhen two segments cross, touch or overlap (self_intersectionslists them, decided exactly). A curve can pass the default check and still cross itself, a section of a non-manifold mesh for example; the cut cells and the swept geometry assume it does not. - Exactness. Decisions that must agree from one query to the next use the exact
orient2d: nesting depth, self-intersections, slicing, repair. The point queries are plain floating point, as in 3-D, so they run unchanged on the device.
The nesting depths are 0, 1, 2 and the signed areas 12, −6 and 0.5: the hole's is negative because it runs clockwise. check_validity returns CURVE_OPEN_LOOP at loop 4, whose area is 0. The cells are filled by is_point_inside(sign_algorithm=SIGN_FAST_WINDING_NUMBER), which closes the open loop by the chord between its ends (fig_ref_2d_geometry_conventions.f90).
Example
Tutorial 7 slices the dragon and computes a field from the section; the cookbook cuts a section of a wing and measures the distance to a polygon. This program builds a curve from vertex arrays with two defects, checks it, repairs it and queries it:
use fossil, only : curve_object, CURVE_VALID, SIGN_FAST_WINDING_NUMBER
use penf, only : I4P, R8P
implicit none
real(R8P), parameter :: PI = acos(-1._R8P)
type(curve_object) :: c
real(R8P) :: u(16), v(16), d, foot(2), p(2, 3)
integer(I4P) :: k, l, defect, seg, vtx, gaps, reversed, still_open
u(1:4) = [0._R8P, 2._R8P, 2._R8P, 0._R8P] ; v(1:4) = [0._R8P, 0._R8P, 1._R8P, 1._R8P] ! outer plate, counter-clockwise
do k = 0, 7 ! hole, an octagon: counter-clockwise,
u(5 + k) = 0.6_R8P + 0.25_R8P * cos(PI * k / 4) ; v(5 + k) = 0.5_R8P + 0.25_R8P * sin(PI * k / 4) ! the wrong way
enddo
u(13:16) = [3._R8P, 4._R8P, 3._R8P, 3._R8P] ; v(13:16) = [0._R8P, 0._R8P, 1._R8P, 1.e-7_R8P] ! a triangle that misses
call c%adopt_loops(u=u, v=v, loop_sizes=[4_I4P, 8_I4P, 4_I4P], closed=[.true., .true., .false.]) ! closing: open chaindefect = c%check_validity(loop=l)
print '(A,I0,A,I0)', 'before repair: defect ', defect, ' at loop ', l
call c%repair(tolerance=1.e-6_R8P, gaps_closed=gaps, loops_reversed=reversed, loops_open=still_open)
print '(A,I0,A,I0,A,I0)', 'repair: gaps closed ', gaps, ', loops reversed ', reversed, ', still open ', still_open
defect = c%check_validity(loop=l, check_self_intersections=.true.)
print '(A,L1,A,I0,A,I0,A,F8.5)', 'after repair: valid ', defect == CURVE_VALID, ', loops ', c%get_loops_number(), &
', bodies ', c%get_bodies_number(), ', area ', c%get_area()
do l = 1, c%get_loops_number()
print '(A,I0,A,I0,A,F9.5)', ' loop ', l, ': nesting depth ', c%nesting_depth(l), ', signed area ', c%get_loop_area(l)
enddop(:, 1) = [1.5_R8P, 0.5_R8P] ; p(:, 2) = [0.6_R8P, 0.5_R8P] ; p(:, 3) = [2.3_R8P, 0.2_R8P] ! solid, hole, outside
do k = 1, 3
call c%compute_distance(p(:, k), distance=d, closest_point=foot, closest_segment=seg, closest_vertex=vtx)
print '(A,2F5.2,A,F8.5,A,2F8.5,A,I0,A,I0)', 'p = ', p(:, k), ': d ', d, ', foot ', foot, ', segment ', seg, &
', vertex ', vtx
print '(A,F8.5,A,F8.5,A,I0,A,L1,A,L1)', ' w fast ', c%winding_number(p(:, k)), ', w sum ', &
c%winding_number(p(:, k), use_brute_force=.true.), ', w exact ', c%winding_number_exact(p(:, k)), &
', inside ', c%is_point_inside(p(:, k)), ', inside (fwn) ', &
c%is_point_inside(p(:, k), sign_algorithm=SIGN_FAST_WINDING_NUMBER)
enddo$ ref_2d_geometry
before repair: defect 4 at loop 2
repair: gaps closed 1, loops reversed 1, still open 0
after repair: valid T, loops 3, bodies 2, area 2.32322
loop 1: nesting depth 0, signed area 2.00000
loop 2: nesting depth 1, signed area -0.17678
loop 3: nesting depth 0, signed area 0.50000
p = 1.50 0.50: d -0.50000, foot 1.50000 0.00000, segment 1, vertex 0
w fast 1.00000, w sum 1.00000, w exact 1, inside T, inside (fwn) T
p = 0.60 0.50: d 0.23097, foot 0.38661 0.58839, segment 8, vertex 0
w fast 0.00000, w sum 0.00000, w exact 0, inside F, inside (fwn) F
p = 2.30 0.20: d 0.30000, foot 2.00000 0.20000, segment 2, vertex 0
w fast 0.00096, w sum 0.00000, w exact 0, inside F, inside (fwn) F- Building.
adopt_loopstakes the vertices of every loop one after the other,loop_sizesand, per loop,closed. The third loop is passed open: its last vertex lies 1e-7 from its first, as a chain that missed closing by round-off would. - Checking.
check_validityreturns the first defect, hereCURVE_WRONG_ORIENTATION(4) at loop 2: the octagon lies inside the plate (nesting depth 1), so it is a hole and must run clockwise. The open loop 3 is a second defect, not reported: only the first is. - Repairing.
repair(tolerance=1.e-6)closes the open chain onto its own start (one gap) and reverses the hole. The curve is then valid, self-intersections included, with two bodies: the plate with its hole and the triangle. The hole's signed area is negative, −0.17678 = −2√2 r² for the octagon of radius 0.25, and the total 2.32322 is 2 − 0.17678 + 0.5. - Querying.
compute_distancereturns the foot point, the segment and, if the foot is a vertex, the vertex (0 in a segment interior). The point in the hole is outside (d > 0). The fast winding number is 1 or 0 to round-off here, except at the last point, 0.3 from the plate's edge, where the far-field expansion leaves 0.00096; the brute-force sum (use_brute_force=.true.) is exact to round-off, andwinding_number_exactis the integer from exactorient2ddecisions. Both inside tests agree with it.
How it works
Segment BVH (
fossil_segment_bvh). The 2-D twin of the SAH BVH: binary, 16 buckets per axis on box half-perimeters, with median splits below depth 48 so the fixed traversal stack cannot overflow. Everything is stored in flat arrays. Queries are iterative and deterministic: the nearer child first, pruning on squared distance, and a candidate wins only if strictly closer.Sign. The sign comes from the closest feature: the side of the segment, or the vertex pseudo-normal. In 2-D the angle-weighted pseudo-normal of Bærentzen and Aanæs reduces to the bisector of the two segment normals. At a vertex the distance is computed from the vertex itself, so the two segments sharing it give the same bits.
Near an open end the two signs differ. The fast winding number closes the gap by the straight chord between the ends, where w crosses 0.5; the pseudo-normal takes the side of the nearest feature, an end vertex for the points beyond the gap. On the open dragon section they disagree on 39 cells of 0.05, all in a wedge outside the chord:
Winding number. w = (1/2π) Σ (angle subtended by each segment): 1 inside, 0 outside or in a hole, fractional near an open end.
winding_numberevaluates it hierarchically, the 2-D analogue of the fast winding number (Barill et al. 2018). A node far enough away contributes the second-order expansion of (1/2π) ∫ n·(x − p)/|x − p|² ds from its dipole and second moments, which are exact for straight segments.On the open section of the dragon above, 645 of the 11400 cells have 0.05 < w < 0.95: all of them in the fan around the gap of 0.957 between the two open ends. Elsewhere w is 0 or 1, as for closed loops.
Slicing.
- A vertex on the plane counts as above it. A facet is therefore crossed along exactly two edges or not at all, and a facet lying in the plane is not crossed.
- Crossing points are built from the edge endpoints in lexicographic order, so the two facets sharing an edge produce the same bits. Loops close by exact equality, without tolerances.
- Each segment's direction comes from the facet's vertex order, so outward-oriented facets put the solid on the left.
Device. The traversals are free
_oackernels (!$acc routine seq) on the flat arrays, and the CPU queries call them too.distance_many_devicefollows the GPU rules of #20.
Results
| Case | Result |
|---|---|
near-collinear orient2d grid (2048 points) | naive formula wrong on 482, orient2d on 0 |
| 2000 random points (NACA 0012, cusp, 64-gon) | BVH distance = brute force bit for bit; sign = exact winding number |
| fast winding number, NACA 0012 | inside decisions exact; max error 1.4e-2, 2.0e-3, 2.3e-4 at β = 2, 4, 8 |
| slice of the unit cube at z = 0.4, 0.5, 0.25, 1 | the exact unit square after merging, area exactly 1 |
| slice of an icosphere at z = 0.3, levels 2-4 | area error 6.9e-2, 1.8e-2, 4.7e-3 (O(h²)) |
distance_many / device path vs compute_distance | bit for bit (4000 points, both signs) |
| 2-D field, circle, 32², 64², 128² | band bit-exact; far-field error 2.8e-2, 2.0e-2, 1.3e-2 (O(h |log h|)) |
Limitations
- GPU not validated yet. The device path is written and runs bit-exact on the host, through the same kernel, in the
tests-gnubuild. The GPU check (fobis build --mode tests-nvidia-curve --varset local_nvf, thenfossil_test_curve_bench) is pending. - Coarse fast winding number near thin bodies. It is about 1e-2 at the default β = 2. The inside decision has a margin of 0.5; raise β where the value itself matters.
- No curved segments. Curves are polylines: arcs and splines must be sampled first.
- Slicing with planes along axes only. An arbitrary plane needs a rotation of the surface first.
- A coarse join rule in repair. It joins open chains greedily by nearest end within the tolerance. Ambiguous gaps (several ends within the tolerance) take the nearest.
- One tolerance for joins and merges.
repairalso merges consecutive vertices closer thantolerance. A tolerance large enough to bridge a real gap (a missing facet, as in the figures above) also drops every segment shorter than the gap and coarsens the curve. It is meant for round-off gaps, as in the example; a real gap needs the open-loop queries (fast winding number sign) or a repaired surface.
API
See curve_object for every method and argument.