Skip to content

Commit 8f42d4d

Browse files
authored
Refactor centroid (#1004)
1 parent 7e08d7a commit 8f42d4d

27 files changed

+128
-137
lines changed

src/Meshes.jl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ include("neighborsearch.jl")
9898
include("predicates.jl")
9999

100100
# operations
101+
include("centroid.jl")
102+
include("measures.jl")
103+
include("boundary.jl")
101104
include("winding.jl")
102105
include("sideof.jl")
103106
include("orientation.jl")
104-
include("measures.jl")
105-
include("boundary.jl")
106107
include("merging.jl")
107108
include("clipping.jl")
108109
include("clamping.jl")
@@ -148,8 +149,6 @@ export
148149
paramdim,
149150
crs,
150151
manifold,
151-
center,
152-
centroid,
153152

154153
# primitives
155154
Primitive,
@@ -179,6 +178,7 @@ export
179178
DeCasteljau,
180179
coords,
181180
to,
181+
center,
182182
radius,
183183
radii,
184184
plane,
@@ -396,6 +396,18 @@ export
396396
,
397397
,
398398

399+
# centroids
400+
centroid,
401+
402+
# measures
403+
measure,
404+
area,
405+
volume,
406+
perimeter,
407+
408+
# boundary
409+
boundary,
410+
399411
# winding number
400412
winding,
401413

@@ -414,15 +426,6 @@ export
414426
CW,
415427
CCW,
416428

417-
# measures
418-
measure,
419-
area,
420-
volume,
421-
perimeter,
422-
423-
# boundary
424-
boundary,
425-
426429
# clipping
427430
ClippingMethod,
428431
SutherlandHodgman,

src/centroid.jl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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))

src/complement.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ respect to its bounding box.
1313
function _boxboundary(g)
1414
= lentype(g)
1515
b = boundingbox(g)
16-
c = to(center(b))
16+
c = to(centroid(b))
1717
l = sides(b)
1818
α = (l .+ 2atol(ℒ)) ./ l
1919
t = Translate(-c...) Scale(α) Translate(c...)

src/domains.jl

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,6 @@ Return the length type of the `domain`.
9797
lentype(::Type{<:Domain{M,CRS}}) where {M,CRS} = lentype(CRS)
9898
lentype(d::Domain) = lentype(typeof(d))
9999

100-
"""
101-
centroid(domain, ind)
102-
103-
Return the centroid of the `ind`-th element in the `domain`.
104-
"""
105-
centroid(d::Domain, ind::Int) = centroid(d[ind])
106-
107-
"""
108-
centroid(domain)
109-
110-
Return the centroid of the `domain`, i.e. the centroid of all
111-
its element's centroids.
112-
"""
113-
function centroid(d::Domain)
114-
vector(i) = to(centroid(d, i))
115-
volume(i) = measure(element(d, i))
116-
n = nelements(d)
117-
x = vector.(1:n)
118-
w = volume.(1:n)
119-
all(iszero, w) && (w = ones(eltype(w), n))
120-
withcrs(d, sum(w .* x) / sum(w))
121-
end
122-
123100
"""
124101
extrema(domain)
125102

src/domains/meshes/cartesiangrid.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ end
150150

151151
XYZ(g::CartesianGrid) = XYZ(xyz(g))
152152

153-
function centroid(g::CartesianGrid, ind::Int)
154-
ijk = elem2cart(topology(g), ind)
155-
vertex(g, ijk) + Vec(spacing(g) ./ 2)
156-
end
157-
158153
function Base.getindex(g::CartesianGrid, I::CartesianIndices)
159154
@boundscheck _checkbounds(g, I)
160155
dims = size(I)

src/domains/meshes/rectilineargrid.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,6 @@ xyz(g::RectilinearGrid) = g.xyz
5353

5454
XYZ(g::RectilinearGrid) = XYZ(xyz(g))
5555

56-
function centroid(g::RectilinearGrid, ind::Int)
57-
ijk = elem2cart(topology(g), ind)
58-
p1 = vertex(g, ijk)
59-
p2 = vertex(g, ijk .+ 1)
60-
withcrs(g, (to(p1) + to(p2)) / 2)
61-
end
62-
6356
function Base.getindex(g::RectilinearGrid{Datum}, I::CartesianIndices) where {Datum}
6457
@boundscheck _checkbounds(g, I)
6558
dims = size(I)

src/domains/meshes/transformedmesh.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ topology(m::TransformedMesh) = topology(m.mesh)
3333

3434
vertex(m::TransformedMesh, ind::Int) = m.transform(vertex(m.mesh, ind))
3535

36-
centroid(m::TransformedMesh, ind::Int) = m.transform(centroid(m.mesh, ind))
37-
3836
==(m₁::TransformedMesh, m₂::TransformedMesh) = m₁.transform == m₂.transform && m₁.mesh == m₂.mesh
3937

4038
# alias to improve readability in IO methods

src/domains/sets.jl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,3 @@ PointSet(coords::Vararg{TP}) where {TP<:Tuple} = PointSet(collect(coords))
6262

6363
# constructor with iterator of points
6464
PointSet(points) = PointSet(map(identity, points))
65-
66-
centroid(d::PointSet, ind::Int) = d[ind]
67-
68-
centroid(d::PointSet) = withcrs(d, sum(to, d) / nelements(d))

src/domains/subdomains.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ end
1919
# specialize constructor to avoid infinite loops
2020
SubDomain(d::SubDomain, inds::AbstractVector{Int}) = SubDomain(d.domain, d.inds[inds])
2121

22+
Base.parent(d::SubDomain) = d.domain
23+
24+
Base.parentindices(d::SubDomain) = d.inds
25+
2226
"""
2327
SubGrid{M,CRS,Dim}
2428
@@ -35,8 +39,6 @@ element(d::SubDomain, ind::Int) = element(d.domain, d.inds[ind])
3539

3640
nelements(d::SubDomain) = length(d.inds)
3741

38-
centroid(d::SubDomain, ind::Int) = centroid(d.domain, d.inds[ind])
39-
4042
# specializations
4143
Base.eltype(d::SubDomain) = eltype(d.domain)
4244

src/domains/trajecs.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,24 @@ function element(t::CylindricalTrajectory, ind::Int)
4040

4141
if ind == 1 # head of trajectory
4242
# points at cylinder planes
43-
p₂ = center(Segment(c[ind], c[ind + 1]))
43+
p₂ = centroid(Segment(c[ind], c[ind + 1]))
4444
p₁ = p₂ - 2 * (p₂ - c[ind])
4545

4646
# normals to cylinder planes
4747
n₂ = c[ind + 1] - c[ind]
4848
n₁ = n₂
4949
elseif ind == n # tail of trajectory
5050
# points at cylinder planes
51-
p₁ = center(Segment(c[ind - 1], c[ind]))
51+
p₁ = centroid(Segment(c[ind - 1], c[ind]))
5252
p₂ = p₁ + 2 * (c[ind] - p₁)
5353

5454
# normals to cylinder planes
5555
n₁ = c[ind] - c[ind - 1]
5656
n₂ = n₁
5757
else # middle of trajectory
5858
# points at cylinder planes
59-
p₁ = center(Segment(c[ind - 1], c[ind]))
60-
p₂ = center(Segment(c[ind], c[ind + 1]))
59+
p₁ = centroid(Segment(c[ind - 1], c[ind]))
60+
p₂ = centroid(Segment(c[ind], c[ind + 1]))
6161

6262
# normals to cylinder planes
6363
n₁ = c[ind] - c[ind - 1]

0 commit comments

Comments
 (0)