-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathrepair.jl
231 lines (180 loc) · 5.02 KB
/
repair.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------
"""
Repair(K)
Perform repairing operation with code `K`.
## Available operations
- K = 0: duplicated vertices and faces are removed
- K = 1: unused vertices are removed
- K = 2: non-manifold faces are removed
- K = 3: degenerate faces are removed
- K = 4: non-manifold vertices are removed
- K = 5: non-manifold vertices are split by threshold
- K = 6: close vertices are merged (given a radius)
- K = 7: faces are coherently oriented
- K = 8: zero-area ears are removed
- K = 9: rings of polygon are sorted
- K = 10: outer rings of polygon are expanded
- K = 11: rings of polygon are coherently oriented
- K = 12: degenerate rings of polygon are removed
## Examples
```
# remove duplicates and degenerates
mesh |> Repair(0) |> Repair(3)
```
"""
struct Repair{K} <: GeometricTransform end
Repair(K) = Repair{K}()
# --------------
# OPERATION (0)
# --------------
apply(::Repair{0}, geom::Polytope) = unique(geom), nothing
apply(::Repair{0}, mesh::Mesh) = error("not implemented")
# --------------
# OPERATION (1)
# --------------
function apply(::Repair{1}, mesh::Mesh)
count = 0
seen = Int[]
inds = Dict{Int,Int}()
topo = topology(mesh)
elems = map(elements(topo)) do e
elem = indices(e)
for v in elem
if v ∉ seen
push!(seen, v)
count += 1
inds[v] = count
end
end
ntuple(i -> inds[elem[i]], length(elem))
end
points = [vertex(mesh, ind) for ind in seen]
connec = connect.(elems)
rmesh = SimpleMesh(points, connec)
rmesh, nothing
end
# --------------
# OPERATION (7)
# --------------
# HalfEdgeTopology constructor performs orientation of faces
apply(::Repair{7}, mesh::Mesh) = topoconvert(HalfEdgeTopology, mesh), nothing
# --------------
# OPERATION (8)
# --------------
function apply(::Repair{8}, poly::PolyArea)
v = poly |> rings .|> vertices .|> repair8
PolyArea(v), nothing
end
function apply(::Repair{8}, poly::Ngon)
v = poly |> vertices |> repair8
Ngon(ntuple(i -> @inbounds(v[i]), length(v))), nothing
end
function apply(::Repair{8}, ring::Ring)
v = ring |> vertices |> repair8
Ring(v), nothing
end
repair8(v::AbstractVector) = repair8(CircularVector(v))
function repair8(v::CircularVector{<:Point})
n = length(v)
keep = Int[]
for i in 1:n
t = Triangle(v[i - 1], v[i], v[i + 1])
a = area(t)
a > atol(a) && push!(keep, i)
end
isempty(keep) ? v[begin] : v[keep]
end
# --------------
# OPERATION (9)
# --------------
function apply(::Repair{9}, poly::PolyArea)
newrings, indices = poly |> rings |> repair9
PolyArea(newrings), indices
end
apply(::Repair{9}, poly::Ngon) = poly, []
function repair9(r::AbstractVector{<:Ring})
# sort vertices lexicographically
verts = vertices.(r)
coord = to.(reduce(vcat, verts))
vperm = sortperm(sortperm(coord))
# each ring has its own set of indices
offset = 0
indices = Vector{Int}[]
for vert in verts
nvert = length(vert)
range = (offset + 1):(offset + nvert)
push!(indices, vperm[range])
offset += nvert
end
# sort rings based on leftmost vertex
leftmost = argmin.(indices)
minimums = getindex.(indices, leftmost)
neworder = sortperm(minimums)
newverts = verts[neworder]
newinds = indices[neworder]
Ring.(newverts), newinds
end
# ---------------
# OPERATION (10)
# ---------------
function apply(::Repair{10}, poly::PolyArea)
t = _stretch10(poly)
r = rings(poly)
n, c = apply(t, first(r))
PolyArea([n; r[2:end]]), (t, c)
end
function revert(::Repair{10}, poly::PolyArea, c)
r = rings(poly)
o = revert(c[1], first(r), c[2])
PolyArea([o; r[2:end]])
end
function _stretch10(g::Geometry)
T = numtype(lentype(g))
Stretch(ntuple(i -> one(T) + 10atol(T), embeddim(g)))
end
# ---------------
# OPERATION (11)
# ---------------
function apply(::Repair{11}, poly::PolyArea)
r = rings(poly)
# fix orientation
ofix(r, o) = orientation(r) == o ? r : reverse(r)
outer = ofix(first(r), CCW)
inners = ofix.(r[2:end], CW)
PolyArea([outer; inners]), nothing
end
# ---------------
# OPERATION (12)
# ---------------
function apply(::Repair{12}, poly::PolyArea)
r = rings(poly)
# fix degeneracy
oring = first(r)
outer = if nvertices(oring) == 2
A, B = vertices(oring)
P = centroid(Segment(A, B))
Ring(A, P, B)
else
oring
end
# remove degenerated rings
inners = filter(r -> nvertices(r) > 2, r[2:end])
PolyArea([outer; inners]), nothing
end
# ----------
# FALLBACKS
# ----------
apply(::Repair, geom::Geometry) = geom, nothing
apply(t::Repair, multi::Multi) = Multi([t(g) for g in parent(multi)]), nothing
apply(t::Repair, dom::Domain) = GeometrySet([t(g) for g in dom]), nothing
# -----------
# IO METHODS
# -----------
Base.show(io::IO, ::Repair{K}) where {K} = print(io, "Repair(K: $K)")
function Base.show(io::IO, ::MIME"text/plain", t::Repair{K}) where {K}
summary(io, t)
println(io)
print(io, "└─ K: $K")
end