Skip to content
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ used in the Julia ecosystem. Notable changes will be documented in this file for
zero-pressure particles instead of retaining them in the termination condition (#1215).
- Fixed the viscous force on structures in fluid-structure interaction, which had the wrong
sign (#1348).
- Fixed matrix point inputs, including non-contiguous views, for winding-number algorithms
and `SignedDistanceField`. `WindingNumberJacobson()` now defaults to non-hierarchical
winding when no geometry is provided (#1188).

### Documentation

Expand Down
6 changes: 5 additions & 1 deletion src/preprocessing/particle_packing/signed_distance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ to this surface.
distance of `abs(max_signed_distance)` to the surface of the shape
will be sampled.
- `points`: Points on which the signed distance is computed.
Pass a collection of static vectors or an `NDIMS`-by-`N` matrix with
one point per column.
When set to `nothing` (default), the bounding box of the shape will be
sampled with a uniform grid of points.
- `use_for_boundary_packing`: Set to `true` if [`SignedDistanceField`] is used to pack
Expand Down Expand Up @@ -62,9 +64,11 @@ function SignedDistanceField(geometry, particle_spacing;
min_corner; place_on_shell=true)

points = reinterpret(reshape, SVector{NDIMS, eltype(grid)}, grid)
elseif points isa AbstractMatrix
points = reinterpret(reshape, SVector{NDIMS, eltype(points)}, points)
end

positions = copy(points)
positions = collect(points)

# This gives a performance boost for large geometries
delete_positions_in_empty_cells!(positions, nhs)
Expand Down
4 changes: 4 additions & 0 deletions src/preprocessing/point_in_poly/winding_number_hormann.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ struct WindingNumberHormann end
function (point_in_poly::WindingNumberHormann)(geometry, points; store_winding_number=false)
(; edge_vertices) = geometry

if points isa AbstractMatrix
points = reinterpret(reshape, SVector{ndims(geometry), eltype(points)}, points)
end

# We cannot use a `BitVector` here, as writing to a `BitVector` is not thread-safe
inpoly = fill(false, length(points))

Expand Down
13 changes: 10 additions & 3 deletions src/preprocessing/point_in_poly/winding_number_jacobson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ end

"""
WindingNumberJacobson(; geometry=nothing, winding_number_factor=sqrt(eps()),
hierarchical_winding=false)
hierarchical_winding=!isnothing(geometry))
Algorithm for inside-outside segmentation of a complex geometry proposed by [Jacobson2013](@cite).

# Keywords
- `geometry`: Complex geometry returned by [`load_geometry`](@ref) and is only required when using
`hierarchical_winding=true`.
- `hierarchical_winding`: If set to `true`, an optimized hierarchical approach will be used,
which gives a significant speedup. For further information see [Hierarchical Winding](@ref hierarchical_winding).
which gives a significant speedup. It is only supported with a `geometry`,
so it defaults to `true` when `geometry` is passed and to `false` otherwise.
For further information see
[Hierarchical Winding](@ref hierarchical_winding).
- `winding_number_factor`: For leaky geometries, a factor of `0.4` will give a better inside-outside segmentation.

!!! warning "Experimental Implementation"
Expand All @@ -69,7 +72,7 @@ struct WindingNumberJacobson{ELTYPE, W}
winding :: W

function WindingNumberJacobson(; geometry=nothing, winding_number_factor=sqrt(eps()),
hierarchical_winding=true)
hierarchical_winding=(!isnothing(geometry)))
if hierarchical_winding && geometry isa Nothing
throw(ArgumentError("`geometry` must be of type `Polygon` (2D) or `TriangleMesh` (3D) when using hierarchical winding"))
end
Expand Down Expand Up @@ -105,6 +108,10 @@ function (point_in_poly::WindingNumberJacobson)(geometry, points;
store_winding_number=false)
(; winding_number_factor, winding) = point_in_poly

if points isa AbstractMatrix
points = reinterpret(reshape, SVector{ndims(geometry), eltype(points)}, points)
end

# We cannot use a `BitVector` here, as writing to a `BitVector` is not thread-safe
inpoly = fill(false, length(points))

Expand Down
11 changes: 11 additions & 0 deletions test/preprocessing/packing/signed_distance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,15 @@
end
end
end

@testset verbose=true "Point Matrix Input" begin
data_dir = pkgdir(TrixiParticles, "examples", "preprocessing", "data")
geometry = load_geometry(joinpath(data_dir, "hexagon.asc"))

point = first(geometry.vertices)
signed_distance_field = SignedDistanceField(geometry, 0.1; points=hcat(point))

@test signed_distance_field.positions == [point]
@test signed_distance_field.distances == [0.0]
end
end
21 changes: 21 additions & 0 deletions test/preprocessing/point_in_poly/winding_number_jacobson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
data_dir = pkgdir(TrixiParticles, "examples", "preprocessing", "data")
geometry = load_geometry(joinpath(data_dir, "circle.asc"))

winding = WindingNumberJacobson()

show_compact = "WindingNumberJacobson{NaiveWinding}()"
@test repr(winding) == show_compact

winding = WindingNumberJacobson(; hierarchical_winding=false)

show_compact = "WindingNumberJacobson{NaiveWinding}()"
Expand Down Expand Up @@ -30,4 +35,20 @@
└──────────────────────────────────────────────────────────────────────────────────────────────────┘"""
@test repr("text/plain", winding) == show_box
end

@testset verbose=true "Point Matrix Input" begin
geometry = TrixiParticles.Polygon([0.0 1.0 1.0 0.0 0.0;
0.0 0.0 1.0 1.0 0.0])
point_storage = [0.5 0.0 1.5;
0.5 0.0 1.5]
points = @view point_storage[:, 1:2:3]

expected = Bool[true, false]

inpoly_jacobson, _ = WindingNumberJacobson()(geometry, points)
inpoly_hormann, _ = WindingNumberHormann()(geometry, points)

@test inpoly_jacobson == expected
@test inpoly_hormann == expected
end
end
Loading