-
Notifications
You must be signed in to change notification settings - Fork 43
/
NurbsMeshCreator.java
103 lines (92 loc) · 3.26 KB
/
NurbsMeshCreator.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
package toxi.geom.nurbs;
import toxi.geom.Vec2D;
import toxi.geom.Vec3D;
import toxi.geom.mesh.Mesh3D;
import toxi.geom.mesh.TriangleMesh;
public class NurbsMeshCreator {
private NurbsSurface surf;
private Vec2D maxUV;
public NurbsMeshCreator(NurbsSurface surf) {
this(surf, new Vec2D(1, 1));
}
public NurbsMeshCreator(NurbsSurface surf, Vec2D maxUV) {
this.surf = surf;
this.maxUV = maxUV;
}
public Mesh3D createControlMesh(Mesh3D mesh) {
Vec3D[] prev = null;
if (mesh == null) {
mesh = new TriangleMesh();
}
int resU = surf.getControlNet().uLength();
int resV = surf.getControlNet().vLength();
Vec2D dUV = maxUV.scale(1f / resU, 1f / resV);
for (int u = 0; u < resU; u++) {
Vec3D[] curr = new Vec3D[resV + 1];
for (int v = 0; v < resV; v++) {
Vec3D vert = surf.getControlNet().get(u, v).to3D();
if (v > 0 && u > 0) {
mesh.addFace(curr[v - 1], vert, prev[v - 1],
dUV.scale(u, v - 1), dUV.scale(u, v),
dUV.scale(u - 1, v - 1));
mesh.addFace(vert, prev[v], prev[v - 1], dUV.scale(u, v),
dUV.scale(u - 1, v), dUV.scale(u - 1, v - 1));
}
curr[v] = vert;
}
prev = curr;
}
mesh.computeVertexNormals();
return mesh;
}
public Mesh3D createMesh(Mesh3D mesh, int resU, int resV, boolean isClosed) {
final KnotVector knotU = surf.getUKnotVector();
final KnotVector knotV = surf.getVKnotVector();
double iresU = knotU.get(knotU.length() - 1) / resU;
double iresV = knotV.get(knotV.length() - 1) / resV;
Vec3D[] prev = null;
Vec3D[] first = null;
if (mesh == null) {
mesh = new TriangleMesh();
}
Vec2D dUV = maxUV.scale(1f / resU, 1f / resV);
for (int u = 0; u <= resU; u++) {
Vec3D[] curr = new Vec3D[resV + 1];
for (int v = 0; v <= resV; v++) {
Vec3D vert = null;
if (isClosed) {
vert = u < resU ? surf.pointOnSurface(u * iresU, v * iresV)
: first[v];
} else {
vert = surf.pointOnSurface(u * iresU, v * iresV);
}
if (v > 0 && u > 0) {
mesh.addFace(curr[v - 1], vert, prev[v - 1],
dUV.scale(u, v - 1), dUV.scale(u, v),
dUV.scale(u - 1, v - 1));
mesh.addFace(vert, prev[v], prev[v - 1], dUV.scale(u, v),
dUV.scale(u - 1, v), dUV.scale(u - 1, v - 1));
}
curr[v] = vert;
}
prev = curr;
if (u == 0) {
first = curr;
}
}
mesh.computeVertexNormals();
return mesh;
}
public NurbsSurface getSurface() {
return surf;
}
public Vec2D getUVScale() {
return maxUV;
}
public void setSurface(NurbsSurface surf) {
this.surf = surf;
}
public void setUVScale(Vec2D maxUV) {
this.maxUV = maxUV;
}
}