Commit | Line | Data |
---|---|---|
fd8a39b0 SG |
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; | |
ee1f21c7 | 10 | |
fd8a39b0 SG |
11 | Line(PVector pt, PVector v) { |
12 | origin = pt; | |
13 | vector = v.get(); | |
14 | vector.normalize(); | |
15 | } | |
ee1f21c7 | 16 | |
fd8a39b0 SG |
17 | PVector getPoint() { |
18 | return origin; | |
19 | } | |
ee1f21c7 | 20 | |
fd8a39b0 SG |
21 | PVector getVector() { |
22 | return vector; | |
23 | } | |
ee1f21c7 SG |
24 | |
25 | PVector getPointAt(final float t) { | |
26 | return PVector.add(origin, PVector.mult(vector, t)); | |
fd8a39b0 | 27 | } |
ee1f21c7 SG |
28 | |
29 | boolean isColinear(final PVector pt) { | |
30 | PVector projected = projectPoint(pt); | |
fd8a39b0 SG |
31 | return projected.x==pt.x && projected.y==pt.y && projected.z==pt.z; |
32 | } | |
ee1f21c7 SG |
33 | |
34 | float getTValue(final PVector pt) { | |
fd8a39b0 SG |
35 | PVector subtraction = PVector.sub(pt, origin); |
36 | return subtraction.dot(vector); | |
ee1f21c7 SG |
37 | } |
38 | ||
39 | PVector projectPoint(final PVector pt) { | |
fd8a39b0 SG |
40 | return getPointAt(getTValue(pt)); |
41 | } | |
ee1f21c7 SG |
42 | |
43 | PVector rotatePoint(final PVector pt, final float rads) { | |
fd8a39b0 | 44 | Vec3D axisVec3D = new Vec3D(vector.x, vector.y, vector.z); |
ffb4b60e SG |
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); | |
fd8a39b0 SG |
50 | return new PVector(rotatedPt.x, rotatedPt.y, rotatedPt.z); |
51 | } | |
52 | } | |
53 | ||
54 | private class Helix { | |
55 | private final Line axis; | |
ee1f21c7 SG |
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 | |
fd8a39b0 SG |
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; | |
fd8a39b0 | 83 | |
ee1f21c7 SG |
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); | |
fd8a39b0 SG |
87 | phaseNormal.normalize(); |
88 | phaseNormal.mult(radius); | |
89 | } | |
ee1f21c7 | 90 | |
fd8a39b0 SG |
91 | private void setPhase(float phase) { |
92 | this.phase = phase; | |
93 | setPhaseNormalFromPhase(); | |
94 | } | |
ee1f21c7 SG |
95 | |
96 | Line getAxis() { | |
97 | return axis; | |
98 | } | |
99 | ||
fd8a39b0 | 100 | void step(int deltaMs) { |
ee1f21c7 SG |
101 | // Rotate |
102 | if (rotationPeriod != 0) { | |
103 | setPhase(phase + (deltaMs / rotationPeriod) * TWO_PI); | |
104 | } | |
fd8a39b0 | 105 | } |
ee1f21c7 | 106 | |
fd8a39b0 SG |
107 | PVector pointOnToroidalAxis(float t) { |
108 | PVector p = axis.getPointAt(t); | |
109 | PVector middle = PVector.add(p, phaseNormal); | |
110 | return axis.rotatePoint(middle, (t / period) * TWO_PI); | |
111 | } | |
ee1f21c7 SG |
112 | |
113 | color colorOfPoint(final PVector p) { | |
fd8a39b0 SG |
114 | // Find the appropriate point for the current rotation |
115 | // of the helix. | |
116 | float t = axis.getTValue(projectedPoint); | |
117 | PVector toroidPoint = pointOnToroidalAxis(t); | |
ee1f21c7 | 118 | |
fd8a39b0 SG |
119 | // The rotated point represents the middle of the girth of |
120 | // the helix. Figure out if the current point is inside that | |
121 | // region. | |
122 | float d = PVector.dist(p, toroidPoint); | |
c27cb078 | 123 | boolean inToroid = d < girth; |
ee1f21c7 | 124 | |
c27cb078 SG |
125 | // Soften edges by fading brightness |
126 | float b = constrain(100*(1 - ((d-.5*girth)/(girth*.5))), 0, 100); | |
127 | return color((lx.getBaseHuef() + (360*(phase / TWO_PI)))%360, (inToroid ? 80 : 0), b); | |
fd8a39b0 SG |
128 | } |
129 | } | |
130 | ||
131 | private final Helix h1; | |
132 | private final Helix h2; | |
ee1f21c7 | 133 | |
fd8a39b0 SG |
134 | private final BasicParameter helix1On = new BasicParameter("H1ON", 1); |
135 | private final BasicParameter helix2On = new BasicParameter("H2ON", 1); | |
ee1f21c7 | 136 | |
fd8a39b0 SG |
137 | public HelixPattern(GLucose glucose) { |
138 | super(glucose); | |
ee1f21c7 | 139 | |
fd8a39b0 SG |
140 | addParameter(helix1On); |
141 | addParameter(helix2On); | |
ee1f21c7 | 142 | |
fd8a39b0 SG |
143 | h1 = new Helix( |
144 | new Line(new PVector(100, 50, 70), new PVector(1,0,0)), | |
145 | 700, // period | |
146 | 50, // radius | |
147 | 30, // girth | |
148 | 0, // phase | |
149 | 10000); // rotation period (ms) | |
150 | h2 = new Helix( | |
151 | new Line(new PVector(100, 50, 70), new PVector(1,0,0)), | |
152 | 700, | |
153 | 50, | |
154 | 30, | |
155 | PI, | |
156 | 10000); | |
157 | ||
158 | // TODO(shaheen) calculate line segments between | |
159 | // toroidal points selected by stepping the | |
160 | // parameterized t value. select base pairs and | |
161 | // associated colors. lerp between colors for each | |
162 | // base pair to produce a DNA effect. | |
ee1f21c7 | 163 | |
fd8a39b0 | 164 | } |
ee1f21c7 | 165 | |
fd8a39b0 SG |
166 | void run(int deltaMs) { |
167 | boolean h1on = helix1On.getValue() > 0.5; | |
168 | boolean h2on = helix2On.getValue() > 0.5; | |
ee1f21c7 | 169 | |
fd8a39b0 SG |
170 | h1.step(deltaMs); |
171 | h2.step(deltaMs); | |
ee1f21c7 | 172 | |
fd8a39b0 SG |
173 | for (Point p : model.points) { |
174 | color h1c = color(0,0,0); | |
175 | color h2c = color(0,0,0); | |
ee1f21c7 | 176 | |
fd8a39b0 SG |
177 | if (h1on) { |
178 | h1c = h1.colorOfPoint(new PVector(p.x,p.y,p.z)); | |
179 | } | |
ee1f21c7 | 180 | |
fd8a39b0 SG |
181 | if (h2on) { |
182 | h2c = h2.colorOfPoint(new PVector(p.x,p.y,p.z)); | |
183 | } | |
ee1f21c7 | 184 | |
fd8a39b0 SG |
185 | // The helices are positioned to not overlap. If that changes, |
186 | // a better blending formula is probably needed. | |
187 | colors[p.index] = blendColor(h1c, h2c, ADD); | |
188 | } | |
189 | } | |
190 | } | |
191 |