-
Notifications
You must be signed in to change notification settings - Fork 9
/
ShaheenGandhi.pde
279 lines (229 loc) · 8.64 KB
/
ShaheenGandhi.pde
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
import toxi.geom.Vec3D;
import toxi.geom.Matrix4x4;
class HelixPattern extends SCPattern {
// Stores a line in point + vector form
private class Line {
private final PVector origin;
private final PVector vector;
Line(PVector pt, PVector v) {
origin = pt;
vector = v.get();
vector.normalize();
}
PVector getPoint() {
return origin;
}
PVector getVector() {
return vector;
}
PVector getPointAt(final float t) {
return PVector.add(origin, PVector.mult(vector, t));
}
boolean isColinear(final PVector pt) {
PVector projected = projectPoint(pt);
return projected.x==pt.x && projected.y==pt.y && projected.z==pt.z;
}
float getTValue(final PVector pt) {
PVector subtraction = PVector.sub(pt, origin);
return subtraction.dot(vector);
}
PVector projectPoint(final PVector pt) {
return getPointAt(getTValue(pt));
}
PVector rotatePoint(final PVector p, final float t) {
final PVector o = origin;
final PVector v = vector;
final float cost = cos(t);
final float sint = sin(t);
float x = (o.x*(v.y*v.y + v.z*v.z) - v.x*(o.y*v.y + o.z*v.z - v.x*p.x - v.y*p.y - v.z*p.z))*(1 - cost) + p.x*cost + (-o.z*v.y + o.y*v.z - v.z*p.y + v.y*p.z)*sint;
float y = (o.y*(v.x*v.x + v.z*v.z) - v.y*(o.x*v.x + o.z*v.z - v.x*p.x - v.y*p.y - v.z*p.z))*(1 - cost) + p.y*cost + (o.z*v.x - o.x*v.z + v.z*p.x - v.x*p.z)*sint;
float z = (o.z*(v.x*v.x + v.y*v.y) - v.z*(o.x*v.x + o.y*v.y - v.x*p.x - v.y*p.y - v.z*p.z))*(1 - cost) + p.z*cost + (-o.y*v.x + o.x*v.y - v.y*p.x + v.x*p.y)*sint;
return new PVector(x, y, z);
}
}
private class Helix {
private final Line axis;
private final float period; // period of coil
private final float rotationPeriod; // animation period
private final float radius; // radius of coil
private final float girth; // girth of coil
private final PVector referencePoint;
private float phase;
private PVector phaseNormal;
Helix(Line axis, float period, float radius, float girth, float phase, float rotationPeriod) {
this.axis = axis;
this.period = period;
this.radius = radius;
this.girth = girth;
this.phase = phase;
this.rotationPeriod = rotationPeriod;
// Generate a normal that will rotate to
// produce the helical shape.
PVector pt = new PVector(0, 1, 0);
if (this.axis.isColinear(pt)) {
pt = new PVector(0, 0, 1);
if (this.axis.isColinear(pt)) {
pt = new PVector(0, 1, 1);
}
}
this.referencePoint = pt;
// The normal is calculated by the cross product of the axis
// and a random point that is not colinear with it.
phaseNormal = axis.getVector().cross(referencePoint);
phaseNormal.normalize();
phaseNormal.mult(radius);
}
Line getAxis() {
return axis;
}
PVector getPhaseNormal() {
return phaseNormal;
}
float getPhase() {
return phase;
}
void step(int deltaMs) {
// Rotate
if (rotationPeriod != 0) {
this.phase = (phase + ((float)deltaMs / (float)rotationPeriod) * TWO_PI);
}
}
PVector pointOnToroidalAxis(float t) {
PVector p = axis.getPointAt(t);
PVector middle = PVector.add(p, phaseNormal);
return axis.rotatePoint(middle, (t / period) * TWO_PI + phase);
}
private float myDist(PVector p1, PVector p2) {
final float x = p2.x-p1.x;
final float y = p2.y-p1.y;
final float z = p2.z-p1.z;
return sqrt(x*x + y*y + z*z);
}
color colorOfPoint(final PVector p) {
final float t = axis.getTValue(p);
final PVector axisPoint = axis.getPointAt(t);
// For performance reasons, cut out points that are outside of
// the tube where the toroidal coil lives.
if (abs(myDist(p, axisPoint) - radius) > girth*.5f) {
return color(0,0,0);
}
// Find the appropriate point for the current rotation
// of the helix.
PVector toroidPoint = axisPoint;
toroidPoint.add(phaseNormal);
toroidPoint = axis.rotatePoint(toroidPoint, (t / period) * TWO_PI + phase);
// The rotated point represents the middle of the girth of
// the helix. Figure out if the current point is inside that
// region.
float d = myDist(p, toroidPoint);
// Soften edges by fading brightness.
float b = constrain(100*(1 - ((d-.5*girth)/(girth*.5))), 0, 100);
return color((lx.getBaseHuef() + (360*(phase / TWO_PI)))%360, 80, b);
}
}
private class BasePairInfo {
Line line;
float colorPhase1;
float colorPhase2;
BasePairInfo(Line line, float colorPhase1, float colorPhase2) {
this.line = line;
this.colorPhase1 = colorPhase1;
this.colorPhase2 = colorPhase2;
}
}
private final Helix h1;
private final Helix h2;
private final BasePairInfo[] basePairs;
private final BasicParameter helix1On = new BasicParameter("H1ON", 1);
private final BasicParameter helix2On = new BasicParameter("H2ON", 1);
private final BasicParameter basePairsOn = new BasicParameter("BPON", 1);
private static final float helixCoilPeriod = 100;
private static final float helixCoilRadius = 50;
private static final float helixCoilGirth = 30;
private static final float helixCoilRotationPeriod = 5000;
private static final float spokePeriod = 40;
private static final float spokeGirth = 20;
private static final float spokePhase = 10;
private static final float spokeRadius = helixCoilRadius - helixCoilGirth*.5f;
private static final float tMin = -200;
private static final float tMax = 200;
public HelixPattern(GLucose glucose) {
super(glucose);
addParameter(helix1On);
addParameter(helix2On);
addParameter(basePairsOn);
PVector origin = new PVector(100, 50, 55);
PVector axis = new PVector(1,0,0);
h1 = new Helix(
new Line(origin, axis),
helixCoilPeriod,
helixCoilRadius,
helixCoilGirth,
0,
helixCoilRotationPeriod);
h2 = new Helix(
new Line(origin, axis),
helixCoilPeriod,
helixCoilRadius,
helixCoilGirth,
PI,
helixCoilRotationPeriod);
basePairs = new BasePairInfo[(int)floor((tMax - tMin)/spokePeriod)];
}
private void calculateSpokes() {
float colorPhase = PI/6;
for (float t = tMin + spokePhase; t < tMax; t += spokePeriod) {
int spokeIndex = (int)floor((t - tMin)/spokePeriod);
PVector h1point = h1.pointOnToroidalAxis(t);
PVector spokeCenter = h1.getAxis().getPointAt(t);
PVector spokeVector = PVector.sub(h1point, spokeCenter);
Line spokeLine = new Line(spokeCenter, spokeVector);
basePairs[spokeIndex] = new BasePairInfo(spokeLine, colorPhase * spokeIndex, colorPhase * (spokeIndex + 1));
}
}
private color calculateSpokeColor(final PVector pt) {
// Find the closest spoke's t-value and calculate its
// axis. Until everything animates in the model reference
// frame, this has to be calculated at every step because
// the helices rotate.
Line axis = h1.getAxis();
float t = axis.getTValue(pt) + spokePhase;
int spokeIndex = (int)floor((t - tMin + spokePeriod/2) / spokePeriod);
if (spokeIndex < 0 || spokeIndex >= basePairs.length) {
return color(0,0,0);
}
BasePairInfo basePair = basePairs[spokeIndex];
Line spokeLine = basePair.line;
PVector pointOnSpoke = spokeLine.projectPoint(pt);
float d = PVector.dist(pt, pointOnSpoke);
float b = (PVector.dist(pointOnSpoke, spokeLine.getPoint()) < spokeRadius) ? constrain(100*(1 - ((d-.5*spokeGirth)/(spokeGirth*.5))), 0, 100) : 0.f;
float phase = spokeLine.getTValue(pointOnSpoke) < 0 ? basePair.colorPhase1 : basePair.colorPhase2;
return color((lx.getBaseHuef() + (360*(phase / TWO_PI)))%360, 80.f, b);
}
void run(int deltaMs) {
boolean h1on = helix1On.getValue() > 0.5;
boolean h2on = helix2On.getValue() > 0.5;
boolean spokesOn = (float)basePairsOn.getValue() > 0.5;
h1.step(deltaMs);
h2.step(deltaMs);
calculateSpokes();
for (Point p : model.points) {
PVector pt = new PVector(p.x,p.y,p.z);
color h1c = h1.colorOfPoint(pt);
color h2c = h2.colorOfPoint(pt);
color spokeColor = calculateSpokeColor(pt);
if (!h1on) {
h1c = color(0,0,0);
}
if (!h2on) {
h2c = color(0,0,0);
}
if (!spokesOn) {
spokeColor = color(0,0,0);
}
// The helices are positioned to not overlap. If that changes,
// a better blending formula is probably needed.
colors[p.index] = blendColor(blendColor(h1c, h2c, ADD), spokeColor, ADD);
}
}
}