-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.jl
187 lines (125 loc) · 3.87 KB
/
script.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
# https://juliageometry.github.io/MeshesDocs/stable/geometries/primitives.html
using Meshes, GeoIO
import GLMakie as gl
import DelimitedFiles
using ProgressMeter
using Graphs, SimpleWeightedGraphs
using MetricSpaces, Ripserer, PersistenceDiagrams
using Chain
# functions
function reduz_obj(arquivo, n_points=1000)
geotable = GeoIO.load(arquivo)
X_total = geotable.vertices .|> coordinates .|> Vector |> EuclideanSpace
ids = farthest_points_sample(X_total, n_points)
X = X_total[ids]
arquivo_novo = replace(arquivo, ".obj" => ".csv")
DelimitedFiles.writedlm(arquivo_novo, stack(X)' |> Matrix, ",")
X
end
function meshes_to_csv(dir_path)
for (root, dirs, files) ∈ collect(walkdir(dir_path))
for file ∈ files
if occursin(".obj", file)
arquivo = joinpath(root, file)
println(arquivo)
reduz_obj(arquivo)
end
end
end
end
read_mesh(arquivo) = GeoIO.load(arquivo).geometry
function mesh_to_graph(ms, X)
g = SimpleWeightedGraph()
n = length(X)
add_vertices!(g, n)
triangles = ms.topology.connec
@showprogress desc = "Adding vertices to graph..." for t ∈ triangles
v1, v2, v3 = t.indices
add_edge!(g, v1, v2, dist_euclidean(X[v1], X[v2]))
add_edge!(g, v1, v3, dist_euclidean(X[v1], X[v3]))
add_edge!(g, v2, v3, dist_euclidean(X[v2], X[v3]))
end
g
end
mesh_to_metric_space(ms) = ms.vertices .|> coordinates .|> Vector |> EuclideanSpace
function geodesic_distance_from_graph(g, ids)
n = length(ids)
D = zeros(n, n)
@showprogress desc = "Calculating geodesic distance..." Threads.@threads for (i, id) ∈ collect(enumerate(ids))
dts = dijkstra_shortest_paths(g, id)
D[i, :] = dts.dists[ids]
end
return D
end
plot_mesh(ms) = viz(ms)
# ----------------------
# script
ms = read_mesh("meshes/flamingo-poses/flam-01.obj")
# ms = GeoIO.load("meshes/flamingo-poses/flam-01.obj")
plot_mesh(ms)
# componentes
function barcode_from_mesh(ms, n_points=1000)
X_total = mesh_to_metric_space(ms)
g = mesh_to_graph(ms, X_total)
componentes_g = connected_components(g)
ids_maior_componente = componentes_g[findmax(length, componentes_g)[2]]
X_total = X_total[ids_maior_componente]
g = g[ids_maior_componente]
fts_sample = farthest_points_sample(X_total, n_points)
X = X_total[fts_sample]
D = geodesic_distance_from_graph(g, fts_sample)
# force simmetry on X
for i ∈ 1:n_points
for j ∈ i:n_points
D[i, j] = D[j, i]
end
end
max_dist = maximum(D)
D = D ./ max_dist
pd = ripserer(D, dim_max = 2, verbose=true, sparse = true, threshold = 0.9)
pd, D, X, g
end
pd, D, X, g = barcode_from_mesh(ms, 300)
exc = mapslices(sum, D, dims=2) |> vec
gl.scatter(X, color=exc)
import Plots
Plots.plot(pd)
barcode(pd)
# calcula pds
function list_files(path="", pattern="")
files =
@chain begin
map(walkdir(path)) do (root, dirs, files)
joinpath.(root, files)
end
reduce(vcat, _)
filter(x -> occursin(pattern, x), _)
end
files
end
arquivos = list_files("meshes/", ".obj")
arquivos = arquivos[1:5:80]
analises =
@showprogress desc="lendo arquivo..." map(arquivos) do file
ms = read_mesh(file)
pd, D, X, g = barcode_from_mesh(ms)
end
analises[1]
barcode(analises[1][1])
arquivos = [
"meshes/flamingo-poses/flam-01.obj"
,"meshes/elephant-poses/elephant-01.obj"
]
analises = map(arquivos) do f
ms = read_mesh(f)
barcode_from_mesh(ms, 350)
end
pd, D, X, g = analises[1]
exc = mapslices(sum, D, dims=2) |> vec
gl.scatter(X, color=exc)
barcode(pd)
pd, D, X, g = analises[2]
exc = mapslices(sum, D, dims=2) |> vec
gl.scatter(X, color=exc)
barcode(pd)
Bottleneck()(analises[1][1][2:3], analises[2][1][2:3])