1 import toxi.geom.Vec3D;
2 import toxi.geom.Matrix4x4;
4 class HelixPattern extends SCPattern {
6 // Stores a line in point + vector form
8 private final PVector origin;
9 private final PVector vector;
11 Line(PVector pt, PVector v) {
25 PVector getPointAt(final float t) {
26 return PVector.add(origin, PVector.mult(vector, t));
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;
34 float getTValue(final PVector pt) {
35 PVector subtraction = PVector.sub(pt, origin);
36 return subtraction.dot(vector);
39 PVector projectPoint(final PVector pt) {
40 return getPointAt(getTValue(pt));
43 PVector rotatePoint(final PVector p, final float t) {
44 final PVector o = origin;
45 final PVector v = vector;
47 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 - cos(t)) + p.x*cos(t) + (-o.z*v.y + o.y*v.z - v.z*p.y + v.y*p.z)*sin(t);
48 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 - cos(t)) + p.y*cos(t) + (o.z*v.x - o.x*v.z + v.z*p.x - v.x*p.z)*sin(t);
49 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 - cos(t)) + p.z*cos(t) + (-o.y*v.x + o.x*v.y - v.y*p.x + v.x*p.y)*sin(t);
50 return new PVector(x, y, z);
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;
62 private PVector phaseNormal;
64 Helix(Line axis, float period, float radius, float girth, float phase, float rotationPeriod) {
70 this.rotationPeriod = rotationPeriod;
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);
82 this.referencePoint = pt;
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);
95 void step(int deltaMs) {
97 if (rotationPeriod != 0) {
98 this.phase = (phase + ((float)deltaMs / (float)rotationPeriod) * TWO_PI);
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);
108 color colorOfPoint(final PVector p) {
109 float t = axis.getTValue(p);
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) {
117 // Find the appropriate point for the current rotation
119 PVector toroidPoint = pointOnToroidalAxis(t);
121 // The rotated point represents the middle of the girth of
122 // the helix. Figure out if the current point is inside that
124 float d = PVector.dist(p, toroidPoint);
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);
132 private final Helix h1;
133 private final Helix h2;
135 private final BasicParameter helix1On = new BasicParameter("H1ON", 1);
136 private final BasicParameter helix2On = new BasicParameter("H2ON", 1);
137 private final BasicParameter basePairsOn = new BasicParameter("BPON", 1);
139 private static final float helixCoilPeriod = 100;
140 private static final float helixCoilRadius = 45;
141 private static final float helixCoilGirth = 20;
142 private static final float helixCoilRotationPeriod = 10000;
144 private static final float spokePeriod = 40;
145 private static final float spokeGirth = 10;
146 private static final float spokePhase = 10;
147 private static final float spokeRadius = 35; // helixCoilRadius - helixCoilGirth*.5f;
149 public HelixPattern(GLucose glucose) {
152 addParameter(helix1On);
153 addParameter(helix2On);
154 addParameter(basePairsOn);
156 PVector origin = new PVector(100, 50, 45);
157 PVector axis = new PVector(1,0,0);
160 new Line(origin, axis),
165 helixCoilRotationPeriod);
167 new Line(origin, axis),
172 helixCoilRotationPeriod);
175 private color calculateSpokeColor(final color h1c, final color h2c, final PVector pt) {
176 // Find the closest spoke's t-value and calculate its
177 // axis. Until everything animates in the model reference
178 // frame, this has to be calculated at every step because
179 // the helices rotate.
180 float t = h1.getAxis().getTValue(pt) + spokePhase;
181 float spokeAxisTValue = floor(((t + spokePeriod/2) / spokePeriod)) * spokePeriod;
182 PVector h1point = h1.pointOnToroidalAxis(spokeAxisTValue);
183 PVector h2point = h2.pointOnToroidalAxis(spokeAxisTValue);
184 PVector spokeVector = PVector.sub(h2point, h1point);
185 spokeVector.normalize();
186 Line spokeLine = new Line(h1point, spokeVector);
187 float spokeLength = PVector.dist(h1point, h2point);
188 // TODO(shaheen) investigate why h1.getAxis().getPointAt(spokeAxisTValue) doesn't quite
189 // have the same value.
190 PVector spokeCenter = PVector.add(h1point, PVector.mult(spokeVector, spokeLength/2.f));
191 PVector pointOnSpoke = spokeLine.projectPoint(pt);
192 float b = ((PVector.dist(pt, pointOnSpoke) < spokeGirth) && (PVector.dist(pointOnSpoke, spokeCenter) < spokeRadius)) ? 100.f : 0.f;
193 return color(100, 80.f, b);
196 void run(int deltaMs) {
197 boolean h1on = helix1On.getValue() > 0.5;
198 boolean h2on = helix2On.getValue() > 0.5;
199 boolean spokesOn = (float)basePairsOn.getValue() > 0.5;
204 for (Point p : model.points) {
205 PVector pt = new PVector(p.x,p.y,p.z);
206 color h1c = h1.colorOfPoint(pt);
207 color h2c = h2.colorOfPoint(pt);
208 color spokeColor = calculateSpokeColor(h1c, h2c, pt);
219 spokeColor = color(0,0,0);
222 // The helices are positioned to not overlap. If that changes,
223 // a better blending formula is probably needed.
224 colors[p.index] = blendColor(blendColor(h1c, h2c, ADD), spokeColor, ADD);