[Helix][perf] Don't call trig functions more than needed
[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 void step(int deltaMs) {
99 // Rotate
100 if (rotationPeriod != 0) {
101 this.phase = (phase + ((float)deltaMs / (float)rotationPeriod) * TWO_PI);
102 }
103 }
104
105 PVector pointOnToroidalAxis(float t) {
106 PVector p = axis.getPointAt(t);
107 PVector middle = PVector.add(p, phaseNormal);
108 return axis.rotatePoint(middle, (t / period) * TWO_PI + phase);
109 }
110
111 color colorOfPoint(final PVector p) {
112 float t = axis.getTValue(p);
113
114 // For performance reasons, cut out points that are outside of
115 // the tube where the toroidal coil lives.
116 if (abs(PVector.dist(p, axis.getPointAt(t)) - radius) > girth*.5f) {
117 return color(0,0,0);
118 }
119
120 // Find the appropriate point for the current rotation
121 // of the helix.
122 PVector toroidPoint = pointOnToroidalAxis(t);
123
124 // The rotated point represents the middle of the girth of
125 // the helix. Figure out if the current point is inside that
126 // region.
127 float d = PVector.dist(p, toroidPoint);
128
129 // Soften edges by fading brightness.
130 float b = constrain(100*(1 - ((d-.5*girth)/(girth*.5))), 0, 100);
131 return color((lx.getBaseHuef() + (360*(phase / TWO_PI)))%360, 80, b);
132 }
133 }
134
135 private final Helix h1;
136 private final Helix h2;
137
138 private final BasicParameter helix1On = new BasicParameter("H1ON", 1);
139 private final BasicParameter helix2On = new BasicParameter("H2ON", 1);
140 private final BasicParameter basePairsOn = new BasicParameter("BPON", 1);
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 private static final float spokePeriod = 40;
148 private static final float spokeGirth = 10;
149 private static final float spokePhase = 10;
150 private static final float spokeRadius = 35; // helixCoilRadius - helixCoilGirth*.5f;
151
152 public HelixPattern(GLucose glucose) {
153 super(glucose);
154
155 addParameter(helix1On);
156 addParameter(helix2On);
157 addParameter(basePairsOn);
158
159 PVector origin = new PVector(100, 50, 45);
160 PVector axis = new PVector(1,0,0);
161
162 h1 = new Helix(
163 new Line(origin, axis),
164 helixCoilPeriod,
165 helixCoilRadius,
166 helixCoilGirth,
167 0,
168 helixCoilRotationPeriod);
169 h2 = new Helix(
170 new Line(origin, axis),
171 helixCoilPeriod,
172 helixCoilRadius,
173 helixCoilGirth,
174 PI,
175 helixCoilRotationPeriod);
176 }
177
178 private color calculateSpokeColor(final color h1c, final color h2c, final PVector pt) {
179 // Find the closest spoke's t-value and calculate its
180 // axis. Until everything animates in the model reference
181 // frame, this has to be calculated at every step because
182 // the helices rotate.
183 float t = h1.getAxis().getTValue(pt) + spokePhase;
184 float spokeAxisTValue = floor(((t + spokePeriod/2) / spokePeriod)) * spokePeriod;
185 PVector h1point = h1.pointOnToroidalAxis(spokeAxisTValue);
186 // TODO(shaheen) investigate why h1.getAxis().getPointAt(spokeAxisTValue) doesn't quite
187 // have the same value as finding the middle between h1point and h2point.
188 PVector spokeCenter = h1.getAxis().getPointAt(spokeAxisTValue);
189 PVector spokeVector = PVector.sub(h1point, spokeCenter);
190 spokeVector.normalize();
191 Line spokeLine = new Line(h1point, spokeVector);
192 PVector pointOnSpoke = spokeLine.projectPoint(pt);
193 float b = ((PVector.dist(pt, pointOnSpoke) < spokeGirth) && (PVector.dist(pointOnSpoke, spokeCenter) < spokeRadius)) ? 100.f : 0.f;
194 return color(100, 80.f, b);
195 }
196
197 void run(int deltaMs) {
198 boolean h1on = helix1On.getValue() > 0.5;
199 boolean h2on = helix2On.getValue() > 0.5;
200 boolean spokesOn = (float)basePairsOn.getValue() > 0.5;
201
202 h1.step(deltaMs);
203 h2.step(deltaMs);
204
205 for (Point p : model.points) {
206 PVector pt = new PVector(p.x,p.y,p.z);
207 color h1c = h1.colorOfPoint(pt);
208 color h2c = h2.colorOfPoint(pt);
209 color spokeColor = calculateSpokeColor(h1c, h2c, pt);
210
211 if (!h1on) {
212 h1c = color(0,0,0);
213 }
214
215 if (!h2on) {
216 h2c = color(0,0,0);
217 }
218
219 if (!spokesOn) {
220 spokeColor = color(0,0,0);
221 }
222
223 // The helices are positioned to not overlap. If that changes,
224 // a better blending formula is probably needed.
225 colors[p.index] = blendColor(blendColor(h1c, h2c, ADD), spokeColor, ADD);
226 }
227 }
228 }
229