-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMeshEditor.java
355 lines (316 loc) · 9.74 KB
/
MeshEditor.java
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*-
* #%L
* Fiji distribution of ImageJ for the life sciences.
* %%
* Copyright (C) 2010 - 2024 Fiji developers.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
/** Albert Cardona 2007
* Released under the terms of the latest edition of the General Public License.
*/
package isosurface;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.jogamp.vecmath.Point3f;
import customnode.CustomTriangleMesh;
public class MeshEditor {
/**
* If the Content instance wraps a mesh, smooth it by the fraction K (0, 1).
*/
static public void smooth(final CustomTriangleMesh c, final float K) {
final List triangles = c.getMesh();
if (0 != triangles.size() % 3) {
System.out
.println("MeshEditor.smooth: need a list of points multiple of 3.");
return;
}
// for each unique point, find which other points are linked by one edge to
// it.
// In the triangles List, there are only points, but each sequence of 3
// points makes a triangle.
final Hashtable ht = new Hashtable();
for (int i = 0; i < triangles.size(); i += 3) {
// process one triangle at a time
final Point3f p1 = (Point3f) triangles.get(i);
final Point3f p2 = (Point3f) triangles.get(i + 1);
final Point3f p3 = (Point3f) triangles.get(i + 2);
build(p1, p2, p3, ht);
build(p2, p3, p1, ht);
build(p3, p1, p2, ht);
}
/* // shrinkage correction works, but generates undesirably unsmooth edges
for (Iterator it = ht.values().iterator(); it.hasNext(); ) {
PointGroup pg = (PointGroup)it.next();
pg.computeVector(K);
}
for (Iterator it = ht.values().iterator(); it.hasNext(); ) {
PointGroup pg = (PointGroup)it.next();
pg.applyVector(ht);
}
*/
for (final Iterator it = ht.values().iterator(); it.hasNext();) {
final PointGroup pg = (PointGroup) it.next();
pg.smoothMembers(K);
}
c.update();
// done!
}
/**
* Represents one point in 3D space that appears in multiple instances within
* the triangles list.
*/
static private class PointGroup {
Point3f first;
HashSet edges = new HashSet();
ArrayList members = new ArrayList(); // can't be a HashSet, because points
// will compare as equal
float vx, vy, vz;
PointGroup(final Point3f first) {
this.first = first;
members.add(first);
}
void addMember(final Point3f p) {
members.add(p);
}
void addEdge(final Point3f p) {
edges.add(p);
}
void smoothMembers(final float K) {
// Compute a vector composed of all vectors to other points with which it
// shares an edge, and add some fraction of that vector to each member
// point.
vx = 0;
vy = 0;
vz = 0;
for (final Iterator it = edges.iterator(); it.hasNext();) {
final Point3f po = (Point3f) it.next();
vx += po.x;
vy += po.y;
vz += po.z;
}
final int size = edges.size();
vx = (vx / size - first.x) * K;
vy = (vy / size - first.y) * K;
vz = (vz / size - first.z) * K;
for (final Iterator it = members.iterator(); it.hasNext();) {
final Point3f m = (Point3f) it.next();
m.x += vx;
m.y += vy;
m.z += vz;
}
}
void computeVector(final float K) {
// Compute a vector composed of all vectors to other points with which it
// shares an edge, and add some fraction of that vector to each member
// point.
vx = 0;
vy = 0;
vz = 0;
for (final Iterator it = edges.iterator(); it.hasNext();) {
final Point3f po = (Point3f) it.next();
vx += po.x;
vy += po.y;
vz += po.z;
}
final int size = edges.size();
vx = (vx / size - first.x) * K;
vy = (vy / size - first.y) * K;
vz = (vz / size - first.z) * K;
}
void applyVector(final Hashtable ht) {
// compute average displacement vector for all neighbors, i.e. edge points
float ax = 0, ay = 0, az = 0;
int count = 0;
for (final Iterator it = edges.iterator(); it.hasNext();) {
final PointGroup pg = (PointGroup) ht.get(it.next());
if (null == pg) continue;
count++;
ax += pg.vx;
ay += pg.vy;
az += pg.vz;
}
ax += vx;
ay += vy;
az += vz;
count++; // so count can never be zero
ax /= count;
ay /= count;
az /= count;
// apply to each member the smoothing vector minus average neighborhood
// smoothing vector to avoid shrinking
for (final Iterator it = members.iterator(); it.hasNext();) {
final Point3f m = (Point3f) it.next();
m.x += vx;// - ax;
m.y += vy;// - ay;
m.z += vz;// - az;
}
}
}
/**
* Build a list of points that are just one edge away from p1, and store them
* in the Hashtable.
*/
static private void build(final Point3f p1, final Point3f p2,
final Point3f p3, final Hashtable ht)
{
PointGroup pg = (PointGroup) ht.get(p1);
if (null != pg) {
pg.addMember(p1);
}
else {
pg = new PointGroup(p1);
ht.put(p1, pg);
}
pg.addEdge(p2);
pg.addEdge(p3);
}
static private final class Vertex {
// The original
final private Point3f p;
// The collection of vertices from the mesh that equal this one
final private ArrayList<Point3f> copies = new ArrayList<Point3f>();
// The averaged and later on the smoothed result
final private Point3f tmp;
// The number of times it's been averaged
private int n;
Vertex(final Point3f p) {
this.p = p;
this.tmp = new Point3f(0, 0, 0);
this.copies.add(p);
}
private final void reset() {
this.tmp.set(0, 0, 0);
this.n = 0;
}
@Override
public final boolean equals(final Object ob) {
final Vertex v = (Vertex) ob;
return v.p == this.p ||
(v.p.x == this.p.x && v.p.y == this.p.y && v.p.z == this.p.z);
}
private final void average(final Vertex v) {
// Increment counter for both
++this.n;
++v.n;
// compute average of the original coordinates
final Point3f a =
new Point3f((this.p.x + v.p.x) / 2, (this.p.y + v.p.y) / 2,
(this.p.z + v.p.z) / 2);
// Add average to each vertices' tmp
this.tmp.add(a);
v.tmp.add(a);
}
private final void smooth() {
// Compute smoothed coordinates
final float f = 0.5f / n;
tmp.set(0.5f * p.x + f * tmp.x, 0.5f * p.y + f * tmp.y, 0.5f * p.z + f *
tmp.z);
// Apply them to all copies
// It doesn't matter if the copies are not unique.
for (final Point3f p : copies) {
p.set(tmp);
}
}
}
static private final class Edge {
private final Vertex v1, v2;
Edge(final Vertex v1, final Vertex v2) {
this.v1 = v1;
this.v2 = v2;
}
@Override
public final boolean equals(final Object ob) {
final Edge e = (Edge) ob;
return e == this || (e.v1 == this.v1 && e.v2 == this.v2) ||
(e.v1 == this.v2 && e.v2 == this.v1);
}
private final void averageVertices() {
this.v1.average(this.v2);
}
}
static private final Vertex uniqueVertex(final Point3f p,
final HashMap<Point3f, Vertex> verts)
{
Vertex v = verts.get(p);
if (null == v) {
v = new Vertex(p);
verts.put(p, v);
}
else {
v.copies.add(p);
}
return v;
}
/**
* Implemented Blender-style vertex smoothing. See Blender's file
* editmesh_mods.c, at function
* "static int smooth_vertex(bContext *C, wmOperator *op)" What it does: 1.
* For each unique edge, compute the average of both vertices and store it in
* a Point3f. Also increment a counter for each vertex indicating that it has
* been part of an averaging operation. If the vertex is again part of an
* averaging operation, just add the new average to the existing one. 2. For
* each unique vertex, computer a factor as 0.5/count, where count is the
* number of times that the vertex has been part of an averaging operation.
* Then set the value of the vertex to 0.5 times the original coordinates,
* plus the factor times the cumulative average coordinates. The result is
* beautifully smoothed meshes that don't shrink noticeably. All kudos to
* Blender's authors. Thanks for sharing with GPL license.
*/
static public void smooth2(final CustomTriangleMesh c, final int iterations) {
smooth2(c.getMesh(), iterations);
c.update();
}
static protected void smooth2(final List<Point3f> triangles,
final int iterations)
{
final HashMap<Point3f, Vertex> verts = new HashMap<Point3f, Vertex>();
final HashSet<Edge> edges = new HashSet<Edge>();
// Find unique edges made of unique vertices
for (int i = 0; i < triangles.size(); i += 3) {
// process one triangle at a time
final Vertex v1 = uniqueVertex(triangles.get(i), verts), v2 =
uniqueVertex(triangles.get(i + 1), verts), v3 =
uniqueVertex(triangles.get(i + 2), verts);
// Add unique edges only
edges.add(new Edge(v1, v2));
edges.add(new Edge(v2, v3));
edges.add(new Edge(v1, v3));
if (0 == i % 300 && Thread.currentThread().isInterrupted()) return;
}
for (int i = 0; i < iterations; ++i) {
if (Thread.currentThread().isInterrupted()) return;
// First pass: accumulate averages
for (final Edge e : edges) {
e.averageVertices();
}
// Second pass: compute the smoothed coordinates and apply them
for (final Vertex v : verts.values()) {
v.smooth();
}
// Prepare for next iteration
if (i + 1 < iterations) {
for (final Vertex v : verts.values()) {
v.reset();
}
}
}
}
}