-
Notifications
You must be signed in to change notification settings - Fork 78
/
sets.jl
74 lines (54 loc) · 2.35 KB
/
sets.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
GeometrySet(geometries)
A set of `geometries` representing a [`Domain`](@ref).
## Examples
Set containing two balls centered at `(0.0, 0.0)` and `(1.0, 1.0)`:
```julia
julia> GeometrySet([Ball((0.0, 0.0)), Ball((1.0, 1.0))])
```
"""
struct GeometrySet{Dim,T,G<:Geometry{Dim,T}} <: Domain{Dim,T}
geoms::Vector{G}
end
# constructor with iterator of geometries
GeometrySet(geoms) = GeometrySet(map(identity, geoms))
element(d::GeometrySet, ind::Int) = d.geoms[ind]
nelements(d::GeometrySet) = length(d.geoms)
Base.parent(d::GeometrySet) = d.geoms
# specialized for efficiency
Base.vcat(d1::GeometrySet, d2::GeometrySet) = GeometrySet(vcat(d1.geoms, d2.geoms))
Base.vcat(d1::GeometrySet, d2::Domain) = GeometrySet(vcat(d1.geoms, collect(d2)))
Base.vcat(d1::Domain, d2::GeometrySet) = GeometrySet(vcat(collect(d1), d2.geoms))
# ------------------------
# SPECIAL CASE: POINT SET
# ------------------------
const PointSet{Dim,T} = GeometrySet{Dim,T,Point{Dim,T}}
"""
PointSet(points)
A set of `points` (a.k.a. point cloud) representing a [`Domain`](@ref).
## Examples
All point sets below are the same and contain two points in R³:
```julia
julia> PointSet([Point(1,2,3), Point(4,5,6)])
julia> PointSet(Point(1,2,3), Point(4,5,6))
julia> PointSet([(1,2,3), (4,5,6)])
julia> PointSet((1,2,3), (4,5,6))
julia> PointSet([[1,2,3], [4,5,6]])
julia> PointSet([1,2,3], [4,5,6])
julia> PointSet([1 4; 2 5; 3 6])
```
"""
PointSet(points::AbstractVector{P}) where {P<:Point} = PointSet{embeddim(P),coordtype(P)}(points)
PointSet(points::Vararg{P}) where {P<:Point} = PointSet(collect(points))
PointSet(coords::AbstractVector{TP}) where {TP<:Tuple} = PointSet(Point.(coords))
PointSet(coords::Vararg{TP}) where {TP<:Tuple} = PointSet(collect(coords))
PointSet(coords::AbstractVector{V}) where {V<:AbstractVector} = PointSet(Point.(coords))
PointSet(coords::Vararg{V}) where {V<:AbstractVector} = PointSet(collect(coords))
PointSet(coords::AbstractMatrix) = PointSet(Tuple.(eachcol(coords)))
# constructor with iterator of points
PointSet(points) = PointSet(map(identity, points))
centroid(d::PointSet, ind::Int) = d[ind]
centroid(d::PointSet) = Point(sum(coordinates, d) / nelements(d))