-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcentroid.jl
80 lines (60 loc) · 1.93 KB
/
centroid.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
75
76
77
78
79
80
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
centroid(geometry)
The centroid of the `geometry`.
"""
centroid(g::Geometry) = center(g) # some geometries have a natural center
centroid(p::Point) = p
centroid(p::Polygon) = centroid(first(rings(p)))
centroid(p::Polytope) = withcrs(p, sum(to, vertices(p)) / nvertices(p))
centroid(b::Box) = withcrs(b, sum(to, extrema(b)) / 2)
centroid(p::Plane) = p(0, 0)
centroid(c::Cylinder) = centroid(boundary(c))
function centroid(c::CylinderSurface)
a = centroid(bottom(c))
b = centroid(top(c))
withcrs(c, (to(a) + to(b)) / 2)
end
function centroid(p::ParaboloidSurface)
c = apex(p)
r = radius(p)
f = focallength(p)
z = r^2 / 4f
x = zero(z)
y = zero(z)
c + Vec(x, y, z / 2)
end
centroid(m::Multi) = centroid(GeometrySet(parent(m)))
centroid(g::TransformedGeometry) = transform(g)(centroid(parent(g)))
"""
centroid(domain)
The centroid of the `domain`.
"""
function centroid(d::Domain)
vector(i) = to(centroid(d, i))
volume(i) = measure(element(d, i))
n = nelements(d)
x = vector.(1:n)
w = volume.(1:n)
all(iszero, w) && (w = ones(eltype(w), n))
withcrs(d, sum(w .* x) / sum(w))
end
"""
centroid(domain, ind)
The centroid of the `ind`-th element of the `domain`.
"""
centroid(d::Domain, ind::Int) = centroid(d[ind])
centroid(d::SubDomain, ind::Int) = centroid(parent(d), parentindices(d)[ind])
function centroid(g::OrthoRegularGrid, ind::Int)
ijk = elem2cart(topology(g), ind)
vertex(g, ijk) + Vec(spacing(g) ./ 2)
end
function centroid(g::RectilinearGrid{<:𝔼,<:CartesianOrProjected}, ind::Int)
ijk = elem2cart(topology(g), ind)
p1 = vertex(g, ijk)
p2 = vertex(g, ijk .+ 1)
withcrs(g, (to(p1) + to(p2)) / 2)
end
centroid(m::TransformedMesh, ind::Int) = transform(m)(centroid(parent(m), ind))