[Helix][perf] Precalculate spoke line segments
[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 p, final float t) {
44 final PVector o = origin;
45 final PVector v = vector;
46
47 final float cost = cos(t);
48 final float sint = sin(t);
49
50 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;
51 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;
52 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;
53 return new PVector(x, y, z);
54 }
55 }
56
57 private class Helix {
58 private final Line axis;
59 private final float period; // period of coil
60 private final float rotationPeriod; // animation period
61 private final float radius; // radius of coil
62 private final float girth; // girth of coil
63 private final PVector referencePoint;
64 private float phase;
65 private PVector phaseNormal;
66
67 Helix(Line axis, float period, float radius, float girth, float phase, float rotationPeriod) {
68 this.axis = axis;
69 this.period = period;
70 this.radius = radius;
71 this.girth = girth;
72 this.phase = phase;
73 this.rotationPeriod = rotationPeriod;
74
75 // Generate a normal that will rotate to
76 // produce the helical shape.
77 PVector pt = new PVector(0, 1, 0);
78 if (this.axis.isColinear(pt)) {
79 pt = new PVector(0, 0, 1);
80 if (this.axis.isColinear(pt)) {
81 pt = new PVector(0, 1, 1);
82 }
83 }
84
85 this.referencePoint = pt;
86
87 // The normal is calculated by the cross product of the axis
88 // and a random point that is not colinear with it.
89 phaseNormal = axis.getVector().cross(referencePoint);
90 phaseNormal.normalize();
91 phaseNormal.mult(radius);
92 }
93
94 Line getAxis() {
95 return axis;
96 }
97
98 PVector getPhaseNormal() {
99 return phaseNormal;
100 }
101
102 float getPhase() {
103 return phase;
104 }
105
106 void step(int deltaMs) {
107 // Rotate
108 if (rotationPeriod != 0) {
109 this.phase = (phase + ((float)deltaMs / (float)rotationPeriod) * TWO_PI);
110 }
111 }
112
113 PVector pointOnToroidalAxis(float t) {
114 PVector p = axis.getPointAt(t);
115 PVector middle = PVector.add(p, phaseNormal);
116 return axis.rotatePoint(middle, (t / period) * TWO_PI + phase);
117 }
118
119 private float myDist(PVector p1, PVector p2) {
120 final float x = p2.x-p1.x;
121 final float y = p2.y-p1.y;
122 final float z = p2.z-p1.z;
123 return sqrt(x*x + y*y + z*z);
124 }
125
126 color colorOfPoint(final PVector p) {
127 final float t = axis.getTValue(p);
128 final PVector axisPoint = axis.getPointAt(t);
129
130 // For performance reasons, cut out points that are outside of
131 // the tube where the toroidal coil lives.
132 if (abs(myDist(p, axisPoint) - radius) > girth*.5f) {
133 return color(0,0,0);
134 }
135
136 // Find the appropriate point for the current rotation
137 // of the helix.
138 PVector toroidPoint = axisPoint;
139 toroidPoint.add(phaseNormal);
140 toroidPoint = axis.rotatePoint(toroidPoint, (t / period) * TWO_PI + phase);
141
142 // The rotated point represents the middle of the girth of
143 // the helix. Figure out if the current point is inside that
144 // region.
145 float d = myDist(p, toroidPoint);
146
147 // Soften edges by fading brightness.
148 float b = constrain(100*(1 - ((d-.5*girth)/(girth*.5))), 0, 100);
149 return color((lx.getBaseHuef() + (360*(phase / TWO_PI)))%360, 80, b);
150 }
151 }
152
153 private final Helix h1;
154 private final Helix h2;
155 private final Line[] basePairs;
156
157 private final BasicParameter helix1On = new BasicParameter("H1ON", 1);
158 private final BasicParameter helix2On = new BasicParameter("H2ON", 1);
159 private final BasicParameter basePairsOn = new BasicParameter("BPON", 1);
160
161 private static final float helixCoilPeriod = 100;
162 private static final float helixCoilRadius = 45;
163 private static final float helixCoilGirth = 20;
164 private static final float helixCoilRotationPeriod = 10000;
165
166 private static final float spokePeriod = 40;
167 private static final float spokeGirth = 10;
168 private static final float spokePhase = 10;
169 private static final float spokeRadius = 35; // helixCoilRadius - helixCoilGirth*.5f;
170
171 private static final float tMin = -200;
172 private static final float tMax = 200;
173
174 public HelixPattern(GLucose glucose) {
175 super(glucose);
176
177 addParameter(helix1On);
178 addParameter(helix2On);
179 addParameter(basePairsOn);
180
181 PVector origin = new PVector(100, 50, 45);
182 PVector axis = new PVector(1,0,0);
183
184 h1 = new Helix(
185 new Line(origin, axis),
186 helixCoilPeriod,
187 helixCoilRadius,
188 helixCoilGirth,
189 0,
190 helixCoilRotationPeriod);
191 h2 = new Helix(
192 new Line(origin, axis),
193 helixCoilPeriod,
194 helixCoilRadius,
195 helixCoilGirth,
196 PI,
197 helixCoilRotationPeriod);
198
199 basePairs = new Line[(int)floor((tMax - tMin)/spokePeriod)];
200 }
201
202 private void calculateSpokes() {
203 for (float t = tMin + spokePhase; t < tMax; t += spokePeriod) {
204 int spokeIndex = (int)floor((t - tMin)/spokePeriod);
205 PVector h1point = h1.pointOnToroidalAxis(t);
206 PVector spokeCenter = h1.getAxis().getPointAt(t);
207 PVector spokeVector = PVector.sub(h1point, spokeCenter);
208 basePairs[spokeIndex] = new Line(spokeCenter, spokeVector);
209 }
210 }
211
212 private color calculateSpokeColor(final PVector pt) {
213 // Find the closest spoke's t-value and calculate its
214 // axis. Until everything animates in the model reference
215 // frame, this has to be calculated at every step because
216 // the helices rotate.
217 Line axis = h1.getAxis();
218 float t = axis.getTValue(pt) + spokePhase;
219 int spokeIndex = (int)floor((t - tMin + spokePeriod/2) / spokePeriod);
220 if (spokeIndex < 0 || spokeIndex >= basePairs.length) {
221 return color(0,0,0);
222 }
223 Line spokeLine = basePairs[spokeIndex];
224 PVector pointOnSpoke = spokeLine.projectPoint(pt);
225 float b = ((PVector.dist(pt, pointOnSpoke) < spokeGirth) && (PVector.dist(pointOnSpoke, spokeLine.getPoint()) < spokeRadius)) ? 100.f : 0.f;
226 return color(100, 80.f, b);
227 }
228
229 void run(int deltaMs) {
230 boolean h1on = helix1On.getValue() > 0.5;
231 boolean h2on = helix2On.getValue() > 0.5;
232 boolean spokesOn = (float)basePairsOn.getValue() > 0.5;
233
234 h1.step(deltaMs);
235 h2.step(deltaMs);
236 calculateSpokes();
237
238 for (Point p : model.points) {
239 PVector pt = new PVector(p.x,p.y,p.z);
240 color h1c = h1.colorOfPoint(pt);
241 color h2c = h2.colorOfPoint(pt);
242 color spokeColor = calculateSpokeColor(pt);
243
244 if (!h1on) {
245 h1c = color(0,0,0);
246 }
247
248 if (!h2on) {
249 h2c = color(0,0,0);
250 }
251
252 if (!spokesOn) {
253 spokeColor = color(0,0,0);
254 }
255
256 // The helices are positioned to not overlap. If that changes,
257 // a better blending formula is probably needed.
258 colors[p.index] = blendColor(blendColor(h1c, h2c, ADD), spokeColor, ADD);
259 }
260 }
261 }
262