-
Notifications
You must be signed in to change notification settings - Fork 6
/
Utils.pde
341 lines (303 loc) · 9.5 KB
/
Utils.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
String saveFilename(String prefix)
{
String sf = prefix + year() + "-" + month() + "-" + day() + "_" + hour() + "." + minute() + "." + second() + (svgMode? ".svg" : ".png");
return sf;
}
void saveSnapshot(String prefix)
{
String sf = saveFilename(prefix);
saveSnapshotAs(sf);
println("Frame saved as " + sf);
// Feedback
for (Gear g : activeGears) {
println("Gear " + g.nom + " has " + g.teeth + " teeth");
}
}
void saveSVGPathFragment() {
println("Saving Path Fragment");
svgPathFragments += String.format("<g stroke=\"#%06x\" fill=\"none\" stroke-width=\"%.1f\" >\n<path d=\"%s\" />\n</g>\n",((int)penColor) & 0x00FFFFFF,(float) penWidth,svgPath);
svgPath = "";
}
void saveSnapshotAs(String sf)
{
if (svgMode) {
// println("Saving Path to SVG " + sf);
saveSVGPathFragment();
PrintWriter fw;
fw = createWriter(sf);
fw.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
fw.println("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">");
fw.println("<svg width=\"100%\" height=\"100%\" viewBox=\"0 0 648 648\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xml:space=\"preserve\" xmlns:serif=\"http://www.serif.com/\" style=\"fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;\">");
fw.write(svgPathFragments);
//fw.println(String.format("<g stroke=\"#%06x\" fill=\"none\" stroke-width=\"%.1f\" >",((int)penColor) & 0x00FFFFFF,(float) penWidth));
//fw.println(String.format("<path d=\"%s\" />\n</g>",svgPath));
fw.println("</svg>");
println(svgPath);
fw.flush();
fw.close();
} else {
PGraphics tmp = createGraphics(paper.width, paper.height);
tmp.smooth();
tmp.beginDraw();
tmp.background(255);
tmp.image(paper, 0, 0);
tmp.endDraw();
tmp.save(sf);
}
}
String getSetupString()
{
String ss = "Setup\t" + ((char) (65+ setupMode)) + "\n";
ss += "Gear Teeth\t";
for (int i = 0; i < setupTeeth[setupMode].length; ++i) {
if (i > 0) ss += "\t";
ss += setupTeeth[setupMode][i];
}
ss += "\nMount Points\t";
for (int i = 0; i < setupMounts[setupMode].length; ++i) {
if (i > 0) ss += "\t";
ss += setupMounts[setupMode][i];
}
ss += "\n";
ss += "Pen\t" + penRig.len + "\t" + penRig.angle + "°" + "\n";
return ss;
}
int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
// Compute total turntable rotations for current drawing
int computeCyclicRotations() {
int a = 1; // running minimum
int idx = 0;
for (Gear g : activeGears) {
if (g.contributesToCycle && g != turnTable) {
int ratioNom = turnTable.teeth;
int ratioDenom = g.teeth;
if (g.isMoving) { // ! cheesy hack for our orbit configuration, assumes anchorTable,anchorHub,orbit configuration
ratioNom = turnTable.teeth * (activeGears.get(idx-1).teeth + g.teeth);
ratioDenom = activeGears.get(idx-2).teeth * g.teeth;
int gcd = GCD(ratioNom, ratioDenom);
ratioNom /= gcd;
ratioDenom /= gcd;
}
int b = min(ratioNom,ratioDenom) / GCD(ratioNom, ratioDenom);
a = max(a,max(a,b)*min(a,b)/ GCD(a, b));
}
idx += 1;
}
return a;
}
void invertConnectingRod()
{
if (selectedObject instanceof ConnectingRod) {
((ConnectingRod) selectedObject).invert();
} else if (activeConnectingRods.size() == 1) {
activeConnectingRods.get(0).invert();
} else {
println("Please select a connecting rod to invert");
}
}
void completeDrawing()
{
myFrameCount = 0;
penRaised = true;
int totalRotations = computeCyclicRotations();
if (!isTweening)
println("Total turntable cycles needed = " + totalRotations);
int framesPerRotation = int(TWO_PI / crankSpeed);
myLastFrame = framesPerRotation * totalRotations + 1;
passesPerFrame = isRecording? 10 : 360*2; // this sets up each pass to do 1 cycle
isMoving = true;
}
void clearPaper()
{
svgPath = "";
svgPathFragments = "";
paper.beginDraw();
paper.clear();
paper.endDraw();
}
void measureGears() {
float[] sav = {0,0,0,0,0,0,0,0,0};
int i = 0;
for (Gear g : activeGears) {
sav[i] = g.rotation;
i++;
}
myFrameCount += 1;
turnTable.crank(myFrameCount*crankSpeed); // The turntable is always the root of the propulsion chain, since it is the only required gear.
i = 0;
for (Gear g : activeGears) {
sav[i] = g.rotation;
i++;
// Turntable should be crankSpeed
println(g.teeth + ": " + (g.rotation - sav[i])/crankSpeed);
}
}
void nudge(int direction, int kc)
{
if (selectedObject != null) {
selectedObject.nudge(direction, kc);
}
}
void deselect() {
if (selectedObject != null) {
selectedObject.unselect();
selectedObject = null;
}
}
void advancePenColor(int direction) {
saveSVGPathFragment();
penColorIdx = (penColorIdx + penColors.length + direction) % penColors.length;
penColor = penColors[penColorIdx];
paper.beginDraw();
paper.stroke(penColor);
paper.endDraw();
println("Pen color changed");
}
void advancePenWidth(int direction) {
saveSVGPathFragment();
penWidthIdx = (penWidthIdx + penWidths.length + direction) % penWidths.length;
penWidth = penWidths[penWidthIdx];
paper.beginDraw();
paper.strokeWeight(penWidth);
paper.endDraw();
println("Pen width set to " + penWidth);
}
void toggleOutputmode()
{
svgMode = !svgMode;
if (svgMode) {
println("SVG ON - saved frames will be in SVG format");
} else {
println("SVG OFF - saved frames will be in PNG format");
}
}
void toggleHiresmode()
{
hiresMode = !hiresMode;
PGraphics oldPaper = paper;
if (hiresMode) {
paperScale = 2;
println("Hires ON - saved frames are twice the size, drawings are 4 times as slow");
}
else {
paperScale = 1;
println("Hires OFF");
}
paperWidth = 9*inchesToPoints*paperScale;
paper = createGraphics(int(paperWidth), int(paperWidth));
paper.smooth(8);
clearPaper();
paper.beginDraw();
paper.image(oldPaper,0,0,paper.width,paper.height);
paper.endDraw();
penRaised = true;
}
void saveSettings() {
// default filename "setup_" + (char)('A' + setupMode) + "_override.txt"
selectOutput("Select a file to save settings to:", "saveCallback");
// println("Oname = " + oName);
}
void saveCallback(File fileSelection)
{
if (fileSelection == null) {
println("Canceled");
return;
}
JSONObject settings = new JSONObject();
settings.setInt("layout", setupMode);
JSONArray gears = new JSONArray();
for (int i = 0; i < setupTeeth[setupMode].length; ++i) {
gears.setInt(i, setupTeeth[setupMode][i]);
}
settings.setJSONArray("gears", gears);
JSONArray mounts = new JSONArray();
for (int i = 0; i < setupMounts[setupMode].length; ++i) {
mounts.setFloat(i, setupMounts[setupMode][i]);
}
settings.setJSONArray("mounts", mounts);
JSONArray invs = new JSONArray();
for (int i = 0; i < setupInversions[setupMode].length; ++i) {
invs.setBoolean(i, setupInversions[setupMode][i]);
}
settings.setJSONArray("inversions", invs);
JSONObject penRigSetup = new JSONObject();
penRigSetup.setFloat("length", setupPens[setupMode][0]);
penRigSetup.setFloat("angle", setupPens[setupMode][1]);
settings.setJSONObject("penrig", penRigSetup);
saveJSONObject(settings, fileSelection.getAbsolutePath());
}
void loadSettings()
{
selectInput("Select a file to load settings from:", "loadCallback");
}
void loadCallback(File fileSelection)
{
if (fileSelection == null) {
println("Canceled");
return;
}
JSONObject settings = loadJSONObject(fileSelection.getAbsolutePath());
if (settings == null) {
println("Invalid settings file");
return;
}
setupMode = settings.getInt("layout");
JSONArray gears = settings.getJSONArray("gears");
if (gears != null) {
int ng = min(setupTeeth[setupMode].length, gears.size());
for (int i = 0; i < ng; ++i) {
setupTeeth[setupMode][i] = gears.getInt(i);
}
}
JSONArray mounts = settings.getJSONArray("mounts");
if (mounts != null) {
int nm = min(setupMounts[setupMode].length, mounts.size());
for (int i = 0; i < nm; ++i) {
setupMounts[setupMode][i] = mounts.getFloat(i);
}
}
JSONArray invs = settings.getJSONArray("inversions");
if (invs != null) {
int nm = min(setupInversions[setupMode].length, invs.size());
for (int i = 0; i < nm; ++i) {
setupInversions[setupMode][i] = invs.getBoolean(i);
}
}
JSONObject penRigSetup = settings.getJSONObject("penrig");
if (penRigSetup != null) {
setupPens[setupMode][0] = penRigSetup.getFloat("length");
setupPens[setupMode][1] = penRigSetup.getFloat("angle");
}
deselect();
drawingSetup(setupMode, false);
}
void drawFulcrumLabels() {
textFont(nFont);
textAlign(CENTER);
fill(64);
stroke(92);
strokeWeight(0.5);
pushMatrix();
translate(3.1*inchesToPoints, 10.23*inchesToPoints);
rotate(PI/2);
int nbrNotches = 39;
float startNotch = 0.25*inchesToPoints;
float notchIncr = 0.25*inchesToPoints;
float minNotch = 0.9*inchesToPoints;
float lilNotch = minNotch/2;
float widIncr = 1.722*inchesToPoints/nbrNotches;
float notchSize = minNotch;
float notchX = -startNotch;
for (int n = 0; n < 39; ++n) {
line(notchX,0,notchX,n % 2 == 1? notchSize : lilNotch);
if (n % 2 == 1) {
text("" + (n/2+1),notchX,lilNotch);
}
notchSize += widIncr;
notchX -= notchIncr;
}
popMatrix();
}