|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the ISC License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + PointPattern(coords) |
| 7 | +
|
| 8 | +A set of points with coordinate matrix `coords`. The number of rows |
| 9 | +of the matrix is the dimensionality of the pattern whereas the number |
| 10 | +of columns is the number of points in the pattern. Alternatively, `coords` |
| 11 | +can be a vector of tuples (i.e. points). |
| 12 | +""" |
| 13 | +struct PointPattern{T,N} |
| 14 | + coords::Matrix{T} |
| 15 | +end |
| 16 | + |
| 17 | +PointPattern(coords::AbstractMatrix{T}) where {T} = |
| 18 | + PointPattern{T,size(coords,1)}(coords) |
| 19 | + |
| 20 | +PointPattern(coords::AbstractVector{NTuple{N,T}}) where {N,T} = |
| 21 | + PointPattern([c[i] for i in 1:N, c in coords]) |
| 22 | + |
| 23 | +npoints(pp::PointPattern) = size(pp.coords, 2) |
| 24 | + |
| 25 | +""" |
| 26 | + coordinates(pp, ind) |
| 27 | +
|
| 28 | +Return the coordinates of the `ind`-th point in the point pattern `pp`. |
| 29 | +""" |
| 30 | +function coordinates(pp::PointPattern{T,N}, ind::Int) where {N,T} |
| 31 | + x = MVector{N,T}(undef) |
| 32 | + coordinates!(x, pp, ind) |
| 33 | + x |
| 34 | +end |
| 35 | + |
| 36 | +""" |
| 37 | + coordinates(pp, inds) |
| 38 | +
|
| 39 | +Return the coordinates of the points `inds` in the point pattern `pp`. |
| 40 | +""" |
| 41 | +function coordinates(pp::PointPattern{T,N}, inds::AbstractVector{Int}) where {N,T} |
| 42 | + X = Matrix{T}(undef, N, length(inds)) |
| 43 | + coordinates!(X, pp, inds) |
| 44 | + X |
| 45 | +end |
| 46 | + |
| 47 | +""" |
| 48 | + coordinates(pp) |
| 49 | +
|
| 50 | +Return the coordinates of all points in point pattern `pp`. |
| 51 | +""" |
| 52 | +coordinates(pp::PointPattern) = coordinates(pp, 1:npoints(pp)) |
| 53 | + |
| 54 | +""" |
| 55 | + coordinates!(buff, pp, inds) |
| 56 | +
|
| 57 | +Non-allocating version of [`coordinates`](@ref) |
| 58 | +""" |
| 59 | +function coordinates!(buff::AbstractMatrix, pp::PointPattern, |
| 60 | + inds::AbstractVector{Int}) |
| 61 | + for j in 1:length(inds) |
| 62 | + coordinates!(view(buff,:,j), pp, inds[j]) |
| 63 | + end |
| 64 | +end |
| 65 | + |
| 66 | +""" |
| 67 | + coordinates!(buff, pp, ind) |
| 68 | +
|
| 69 | +Non-allocating version of [`coordinates`](@ref). |
| 70 | +""" |
| 71 | +function coordinates!(buff::AbstractVector{T}, pp::PointPattern{T,N}, |
| 72 | + ind::Int) where {N,T} |
| 73 | + for i in 1:N |
| 74 | + @inbounds buff[i] = pp.coords[i,ind] |
| 75 | + end |
| 76 | +end |
| 77 | + |
| 78 | +# ------------ |
| 79 | +# IO methods |
| 80 | +# ------------ |
| 81 | +function Base.show(io::IO, pp::PointPattern{T,N}) where {N,T} |
| 82 | + npts = size(pp.coords, 2) |
| 83 | + print(io, "$npts PointPattern{$T,$N}") |
| 84 | +end |
| 85 | + |
| 86 | +function Base.show(io::IO, ::MIME"text/plain", pp::PointPattern{T,N}) where {N,T} |
| 87 | + println(io, pp) |
| 88 | + Base.print_array(io, pp.coords) |
| 89 | +end |
0 commit comments