[Helix][perf] Reduce number of point calculations.
[SugarCubes.git] / ShaheenGandhi.pde
index 816bd10d14cec76edd6fc8d2f7f6dce913657c4f..d562e3bd3fed0b12357611d46d4bb8072c76ebd6 100644 (file)
@@ -94,6 +94,14 @@ class HelixPattern extends SCPattern {
     Line getAxis() {
       return axis;
     }
+    
+    PVector getPhaseNormal() {
+      return phaseNormal;
+    }
+    
+    float getPhase() {
+      return phase;
+    }
 
     void step(int deltaMs) {
       // Rotate
@@ -109,17 +117,20 @@ class HelixPattern extends SCPattern {
     }
 
     color colorOfPoint(final PVector p) {
-      float t = axis.getTValue(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(PVector.dist(p, axis.getPointAt(t)) - radius) > girth*.5f) {
+      if (abs(PVector.dist(p, axisPoint) - radius) > girth*.5f) {
         return color(0,0,0);
       }
 
       // Find the appropriate point for the current rotation
       // of the helix.
-      PVector toroidPoint = pointOnToroidalAxis(t);
+      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
@@ -180,9 +191,12 @@ class HelixPattern extends SCPattern {
     // axis.  Until everything animates in the model reference
     // frame, this has to be calculated at every step because
     // the helices rotate.
-    float t = h1.getAxis().getTValue(pt) + spokePhase;
+    Line axis = h1.getAxis();
+    float t = axis.getTValue(pt) + spokePhase;
     float spokeAxisTValue = floor(((t + spokePeriod/2) / spokePeriod)) * spokePeriod;
-    PVector h1point = h1.pointOnToroidalAxis(spokeAxisTValue);
+    PVector h1point = axis.getPointAt(t);
+    h1point.add(h1.getPhaseNormal());
+    h1point = axis.rotatePoint(h1point, (t / helixCoilPeriod) * TWO_PI + h1.getPhase());
     // TODO(shaheen) investigate why h1.getAxis().getPointAt(spokeAxisTValue) doesn't quite
     // have the same value as finding the middle between h1point and h2point.
     PVector spokeCenter = h1.getAxis().getPointAt(spokeAxisTValue);