|
| 1 | +# ------------------------------------------------------------------ |
| 2 | +# Licensed under the MIT License. See LICENSE in the project root. |
| 3 | +# ------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | + centroid(geometry) |
| 7 | +
|
| 8 | +The centroid of the `geometry`. |
| 9 | +""" |
| 10 | +centroid(g::Geometry) = center(g) # some geometries have a natural center |
| 11 | + |
| 12 | +centroid(p::Polytope) = withcrs(p, sum(to, vertices(p)) / nvertices(p)) |
| 13 | + |
| 14 | +centroid(p::Point) = p |
| 15 | + |
| 16 | +centroid(p::Polygon) = centroid(first(rings(p))) |
| 17 | + |
| 18 | +centroid(b::Box) = withcrs(b, sum(to, extrema(b)) / 2) |
| 19 | + |
| 20 | +centroid(p::Plane) = p(0, 0) |
| 21 | + |
| 22 | +centroid(c::Cylinder) = centroid(boundary(c)) |
| 23 | + |
| 24 | +function centroid(c::CylinderSurface) |
| 25 | + a = centroid(bottom(c)) |
| 26 | + b = centroid(top(c)) |
| 27 | + withcrs(c, (to(a) + to(b)) / 2) |
| 28 | +end |
| 29 | + |
| 30 | +function centroid(p::ParaboloidSurface) |
| 31 | + c = apex(p) |
| 32 | + r = radius(p) |
| 33 | + f = focallength(p) |
| 34 | + z = r^2 / 4f |
| 35 | + x = zero(z) |
| 36 | + y = zero(z) |
| 37 | + c + Vec(x, y, z / 2) |
| 38 | +end |
| 39 | + |
| 40 | +centroid(m::Multi) = centroid(GeometrySet(parent(m))) |
| 41 | + |
| 42 | +centroid(g::TransformedGeometry) = transform(g)(centroid(parent(g))) |
| 43 | + |
| 44 | +""" |
| 45 | + centroid(domain) |
| 46 | +
|
| 47 | +The centroid of the `domain`. |
| 48 | +""" |
| 49 | +function centroid(d::Domain) |
| 50 | + vector(i) = to(centroid(d, i)) |
| 51 | + volume(i) = measure(element(d, i)) |
| 52 | + n = nelements(d) |
| 53 | + x = vector.(1:n) |
| 54 | + w = volume.(1:n) |
| 55 | + all(iszero, w) && (w = ones(eltype(w), n)) |
| 56 | + withcrs(d, sum(w .* x) / sum(w)) |
| 57 | +end |
| 58 | + |
| 59 | +""" |
| 60 | + centroid(domain, ind) |
| 61 | +
|
| 62 | +The centroid of the `ind`-th element of the `domain`. |
| 63 | +""" |
| 64 | +centroid(d::Domain, ind::Int) = centroid(d[ind]) |
| 65 | + |
| 66 | +centroid(d::SubDomain, ind::Int) = centroid(parent(d), parentindices(d)[ind]) |
| 67 | + |
| 68 | +function centroid(g::CartesianGrid, ind::Int) |
| 69 | + ijk = elem2cart(topology(g), ind) |
| 70 | + vertex(g, ijk) + Vec(spacing(g) ./ 2) |
| 71 | +end |
| 72 | + |
| 73 | +function centroid(g::RectilinearGrid, ind::Int) |
| 74 | + ijk = elem2cart(topology(g), ind) |
| 75 | + p1 = vertex(g, ijk) |
| 76 | + p2 = vertex(g, ijk .+ 1) |
| 77 | + withcrs(g, (to(p1) + to(p2)) / 2) |
| 78 | +end |
| 79 | + |
| 80 | +centroid(m::TransformedMesh, ind::Int) = transform(m)(centroid(parent(m), ind)) |
0 commit comments