Merge pull request #3 from visigoth/master
[SugarCubes.git] / ShaheenGandhi.pde
1 import toxi.geom.Vec3D;
2 import toxi.geom.Matrix4x4;
3
4 class HelixPattern extends SCPattern {
5
6 // Stores a line in point + vector form
7 private class Line {
8 private final PVector origin;
9 private final PVector vector;
10
11 Line(PVector pt, PVector v) {
12 origin = pt;
13 vector = v.get();
14 vector.normalize();
15 }
16
17 PVector getPoint() {
18 return origin;
19 }
20
21 PVector getVector() {
22 return vector;
23 }
24
25 PVector getPointAt(final float t) {
26 return PVector.add(origin, PVector.mult(vector, t));
27 }
28
29 boolean isColinear(final PVector pt) {
30 PVector projected = projectPoint(pt);
31 return projected.x==pt.x && projected.y==pt.y && projected.z==pt.z;
32 }
33
34 float getTValue(final PVector pt) {
35 PVector subtraction = PVector.sub(pt, origin);
36 return subtraction.dot(vector);
37 }
38
39 PVector projectPoint(final PVector pt) {
40 return getPointAt(getTValue(pt));
41 }
42
43 PVector rotatePoint(final PVector pt, final float rads) {
44 Vec3D axisVec3D = new Vec3D(vector.x, vector.y, vector.z);
45 Vec3D originVec3D = new Vec3D(origin.x, origin.y, origin.z);
46 Matrix4x4 mat = new Matrix4x4().identity()
47 .rotateAroundAxis(axisVec3D, rads);
48 Vec3D ptVec3D = new Vec3D(pt.x, pt.y, pt.z).sub(originVec3D);
49 Vec3D rotatedPt = mat.applyTo(ptVec3D).add(originVec3D);
50 return new PVector(rotatedPt.x, rotatedPt.y, rotatedPt.z);
51 }
52 }
53
54 private class Helix {
55 private final Line axis;
56 private final float period; // period of coil
57 private final float rotationPeriod; // animation period
58 private final float radius; // radius of coil
59 private final float girth; // girth of coil
60 private final PVector referencePoint;
61 private float phase;
62 private PVector phaseNormal;
63
64 Helix(Line axis, float period, float radius, float girth, float phase, float rotationPeriod) {
65 this.axis = axis;
66 this.period = period;
67 this.radius = radius;
68 this.girth = girth;
69 this.phase = phase;
70 this.rotationPeriod = rotationPeriod;
71
72 // Generate a normal that will rotate to
73 // produce the helical shape.
74 PVector pt = new PVector(0, 1, 0);
75 if (this.axis.isColinear(pt)) {
76 pt = new PVector(0, 0, 1);
77 if (this.axis.isColinear(pt)) {
78 pt = new PVector(0, 1, 1);
79 }
80 }
81
82 this.referencePoint = pt;
83
84 // The normal is calculated by the cross product of the axis
85 // and a random point that is not colinear with it.
86 phaseNormal = axis.getVector().cross(referencePoint);
87 phaseNormal.normalize();
88 phaseNormal.mult(radius);
89 }
90
91 Line getAxis() {
92 return axis;
93 }
94
95 void step(int deltaMs) {
96 // Rotate
97 if (rotationPeriod != 0) {
98 this.phase = (phase + ((float)deltaMs / (float)rotationPeriod) * TWO_PI);
99 }
100 }
101
102 PVector pointOnToroidalAxis(float t) {
103 PVector p = axis.getPointAt(t);
104 PVector middle = PVector.add(p, phaseNormal);
105 return axis.rotatePoint(middle, (t / period) * TWO_PI + phase);
106 }
107
108 color colorOfPoint(final PVector p) {
109 float t = axis.getTValue(p);
110
111 // For performance reasons, cut out points that are outside of
112 // the tube where the toroidal coil lives.
113 if (abs(PVector.dist(p, axis.getPointAt(t)) - radius) > girth*.5f) {
114 return color(0,0,0);
115 }
116
117 // Find the appropriate point for the current rotation
118 // of the helix.
119 PVector toroidPoint = pointOnToroidalAxis(t);
120
121 // The rotated point represents the middle of the girth of
122 // the helix. Figure out if the current point is inside that
123 // region.
124 float d = PVector.dist(p, toroidPoint);
125
126 // Soften edges by fading brightness.
127 float b = constrain(100*(1 - ((d-.5*girth)/(girth*.5))), 0, 100);
128 return color((lx.getBaseHuef() + (360*(phase / TWO_PI)))%360, 80, b);
129 }
130 }
131
132 private final Helix h1;
133 private final Helix h2;
134
135 private final BasicParameter helix1On = new BasicParameter("H1ON", 1);
136 private final BasicParameter helix2On = new BasicParameter("H2ON", 1);
137
138 private final BasicParameter basePairsOn = new BasicParameter("BPON", 1);
139 private final BasicParameter spokePeriodParam = new BasicParameter("SPPD", 0.40);
140 private final BasicParameter spokePhaseParam = new BasicParameter("SPPH", 0.25);
141
142 private static final float helixCoilPeriod = 100;
143 private static final float helixCoilRadius = 45;
144 private static final float helixCoilGirth = 20;
145 private static final float helixCoilRotationPeriod = 10000;
146
147 public HelixPattern(GLucose glucose) {
148 super(glucose);
149
150 addParameter(helix1On);
151 addParameter(helix2On);
152 addParameter(basePairsOn);
153 addParameter(spokePhaseParam);
154 addParameter(spokePeriodParam);
155
156 PVector origin = new PVector(100, 50, 45);
157 PVector axis = new PVector(1,0,0);
158
159 h1 = new Helix(
160 new Line(origin, axis),
161 helixCoilPeriod,
162 helixCoilRadius,
163 helixCoilGirth,
164 0,
165 helixCoilRotationPeriod);
166 h2 = new Helix(
167 new Line(origin, axis),
168 helixCoilPeriod,
169 helixCoilRadius,
170 helixCoilGirth,
171 PI,
172 helixCoilRotationPeriod);
173 }
174
175 void run(int deltaMs) {
176 boolean h1on = helix1On.getValue() > 0.5;
177 boolean h2on = helix2On.getValue() > 0.5;
178 boolean spokesOn = (float)basePairsOn.getValue() > 0.5;
179 float spokePeriod = (float)spokePeriodParam.getValue() * 100 + 1;
180 float spokeGirth = 10;
181 float spokePhase = (float)spokePhaseParam.getValue() * spokePeriod;
182 float spokeRadius = helixCoilRadius - helixCoilGirth*.5f;
183
184 h1.step(deltaMs);
185 h2.step(deltaMs);
186
187 for (Point p : model.points) {
188 PVector pt = new PVector(p.x,p.y,p.z);
189 color h1c = h1.colorOfPoint(pt);
190 color h2c = h2.colorOfPoint(pt);
191
192 // Find the closest spoke's t-value and calculate its
193 // axis. Until everything animates in the model reference
194 // frame, this has to be calculated at every step because
195 // the helices rotate.
196 float t = h1.getAxis().getTValue(pt) + spokePhase;
197 float spokeAxisTValue = floor(((t + spokePeriod/2) / spokePeriod)) * spokePeriod;
198 PVector h1point = h1.pointOnToroidalAxis(spokeAxisTValue);
199 PVector h2point = h2.pointOnToroidalAxis(spokeAxisTValue);
200 PVector spokeVector = PVector.sub(h2point, h1point);
201 spokeVector.normalize();
202 Line spokeLine = new Line(h1point, spokeVector);
203 float spokeLength = PVector.dist(h1point, h2point);
204 // TODO(shaheen) investigate why h1.getAxis().getPointAt(spokeAxisTValue) doesn't quite
205 // have the same value.
206 PVector spokeCenter = PVector.add(h1point, PVector.mult(spokeVector, spokeLength/2.f));
207 PVector spokeStart = PVector.add(spokeCenter, PVector.mult(spokeLine.getVector(), -spokeRadius));
208 PVector spokeEnd = PVector.add(spokeCenter, PVector.mult(spokeLine.getVector(), spokeRadius));
209 float spokeStartTValue = spokeLine.getTValue(spokeStart);
210 float spokeEndTValue = spokeLine.getTValue(spokeEnd);
211 PVector pointOnSpoke = spokeLine.projectPoint(pt);
212 float projectedTValue = spokeLine.getTValue(pointOnSpoke);
213 float percentage = constrain(PVector.dist(pointOnSpoke, spokeStart) / spokeLength, 0.f, 1.f);
214 float b = ((PVector.dist(pt, pointOnSpoke) < spokeGirth) && (PVector.dist(pointOnSpoke, spokeCenter) < spokeRadius)) ? 100.f : 0.f;
215
216 color spokeColor;
217
218 if (spokeStartTValue < spokeEndTValue) {
219 spokeColor = lerpColor(h1c, h2c, percentage);
220 } else {
221 spokeColor = lerpColor(h2c, h1c, percentage);
222 }
223
224 spokeColor = color(hue(spokeColor), 80.f, b);
225
226 if (!h1on) {
227 h1c = color(0,0,0);
228 }
229
230 if (!h2on) {
231 h2c = color(0,0,0);
232 }
233
234 if (!spokesOn) {
235 spokeColor = color(0,0,0);
236 }
237
238 // The helices are positioned to not overlap. If that changes,
239 // a better blending formula is probably needed.
240 colors[p.index] = blendColor(blendColor(h1c, h2c, ADD), spokeColor, ADD);
241 }
242 }
243 }
244