-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvgen_ptf.h
164 lines (154 loc) · 5.52 KB
/
vgen_ptf.h
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
// .___
// ___ __ ____ ___ ___ ____ __| _/
// \ \/ // __ \\ \/ // __ \ / __ |
// \ /\ ___/ > <\ ___// /_/ |
// \_/ \___ >__/\_ \\___ >____ |
// \/ \/ \/ \/ __ .__
// ____ ____ ____ ________________ _/ |_|__| ____ ____
// / ___\_/ __ \ / \_/ __ \_ __ \__ \\ __\ |/ _ \ / \
// / /_/ > ___/| | \ ___/| | \// __ \| | | ( <_> ) | \
// \___ / \___ >___| /\___ >__| (____ /__| |__|\____/|___| /
// /_____/ \/ \/ \/ \/ \/
//
// (c) 2016 - 2020 Karsten Schmidt // ASL 2.0 licensed
#ifndef __vgen_ptf_h__
#define __vgen_ptf_h__
#include <vgen_attribs.h>
#include <vgen_tessel.h>
/**
* Parallel Transport Frames implementation based on
* https://github.com/thi-ng/geom/blob/develop/src/types/ptf.org
*
* ```
* vgPTF ptf;
* ptf->init(0, @primnum, 0);
* ptf->sweep_tube(vg_point_positions(1, 0), "rscale", chf("twist"), chf("twistPhase"), 1);
* ```
*/
struct vgPTF {
int geo;
int closed;
int maxPoints;
vector points[];
vector tangents[];
vector normals[];
vector binormals[];
void init(int opinput, primnum, close) {
geo = opinput;
closed = close;
points = vg_point_positions(geo, primnum);
int nump = len(points);
if (nump > 1) {
resize(tangents, nump);
resize(normals, nump);
resize(binormals, nump);
maxPoints = nump - 1;
vector t = normalize(points[1] - points[0]);
vector ta = abs(t);
vector bn = {0, 0, 0};
int j = ta.x < ta.y ? 0 : 1;
j = ta.z < ta[j] ? 2 : j;
bn[j] = 1;
vector n = cross(t, normalize(cross(t, bn)));
tangents[0] = t;
normals[0] = n;
binormals[0] = cross(t, n);
for (int i = 1; i < nump; i++) {
vector s = i < nump - 1
? normalize(points[i + 1] - points[i])
: closed ? normalize(points[0] - points[i]) : t;
vector a = cross(t, s);
if (abs(length2(a)) > 1e-6) {
float theta = acos(clamp(dot(t, s), 0, 1));
matrix m = ident();
rotate(m, theta, normalize(a));
n = m * n;
}
tangents[i] = s;
normals[i] = n;
binormals[i] = cross(s, n);
t = s;
}
}
}
int add_sweep_point(int idx; const vector profile; string scaleRamp;
float twistAmp, twistPhase) {
float t = float(idx) / maxPoints;
vector p = profile * chramp(scaleRamp, t);
matrix3 m = ident();
rotate(m, twistPhase + twistAmp * t, {0.0, 0.0, 1.0});
p = m * p;
vector q = points[idx];
vector n = normals[idx];
vector bn = binormals[idx];
vector cp =
set(p.x * n.x + p.y * bn.x + q.x, p.x * n.y + p.y * bn.y + q.y,
p.x * n.z + p.y * bn.z + q.z);
return addpoint(0, cp);
}
void sweep_lines(const vector profile[]; string scaleRamp;
float twistAmp, twistPhase) {
int numProfile = len(profile);
for (int i = 0; i < numProfile; i++) {
int prim = addprim(geo, "polyline");
int first;
for (int j = 0; j <= maxPoints; j++) {
int p = this->add_sweep_point(j, profile[i], scaleRamp,
twistAmp, twistPhase);
addvertex(geo, prim, p);
if (j == 0) {
first = p;
}
}
if (closed) {
addvertex(geo, prim, first);
}
}
}
void sweep_tube(const vector profile[]; string scaleRamp;
float twistAmp, twistPhase;
int close) {
int numProfile = len(profile);
int prev[], first[];
for (int j = 0; j <= maxPoints; j++) {
int curr[];
resize(curr, numProfile);
for (int i = 0; i < numProfile; i++) {
curr[i] = this->add_sweep_point(j, profile[i], scaleRamp,
twistAmp, twistPhase);
}
if (j > 0) {
vg_quad_strip(geo, prev, curr, numProfile, close);
} else {
first = curr;
}
prev = curr;
}
if (closed) {
vg_quad_strip(geo, prev, first, numProfile, close);
}
}
void sweep_discs(const vector profile[]; string scaleRamp;
float twistAmp, twistPhase;
int close) {
int numProfile = len(profile);
for (int j = 0; j <= maxPoints; j++) {
int prev, first;
int c = addpoint(geo, points[j]);
for (int i = 0; i < numProfile; i++) {
int curr = this->add_sweep_point(j, profile[i], scaleRamp,
twistAmp, twistPhase);
if (i > 0) {
vg_add_triangle(geo, c, prev, curr);
} else {
first = curr;
}
prev = curr;
}
if (close) {
vg_add_triangle(geo, c, prev, first);
}
}
}
}
#endif