forked from jbum/CDMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.pde
351 lines (323 loc) · 9.86 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
342
343
344
345
346
347
348
349
350
351
String saveFilename(String prefix)
{
String sf = prefix + year() + "-" + month() + "-" + day() + "_" + hour() + "." + minute() + "." + second() + ".png";
return 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);
// println(g.teeth + " " + ratioNom + "/" + ratioDenom + " b = " + b);
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 {
// ignore it
// println("Please select a connecting rod to invert");
}
}
void completeDrawing()
{
myFrameCount = 0;
penRaised = true;
int totalRotations = computeCyclicRotations();
// println("Total turntable cycles needed = " + totalRotations);
int framesPerRotation = int(TWO_PI / crankSpeed);
myLastFrame = framesPerRotation * totalRotations + 1;
passesPerFrame = 360*2;
isMoving = true;
}
void clearPaper()
{
paper = createGraphics(paperWidth, paperWidth);
paper.beginDraw();
paper.smooth(8);
paper.noFill();
paper.stroke(penColor);
paper.strokeJoin(ROUND);
paper.strokeCap(ROUND);
paper.strokeWeight(penWidth);
paper.endDraw();
}
void nudge(int direction, int kc)
{
if (selectedObject != null) {
selectedObject.nudge(direction, kc);
}
doSaveSetup();
}
Boolean isDragging = false;
float startDragX = 0, startDragY= 0;
void drag() {
if (selectedObject != null) {
int direction=0, keycode=0;
if (!isDragging) {
startDragX = pmouseX;
startDragY = pmouseY;
isDragging = true;
}
//!! for ConnectingRod - use a similar system as for penrig to move it's mountpoint - do NOT do swaps (maybe do them by double-clicking on swivel?)
//
if (selectedObject instanceof Gear) {
Gear g = (Gear) selectedObject;
float dm = dist(mouseX, mouseY, g.x, g.y);
float ds = dist(startDragX, startDragY, g.x, g.y);
if (abs(dm-ds) > 10) {
direction = (dm > ds)? 1 : -1;
keycode = (direction == 1)? UP : DOWN;
startDragX = mouseX;
startDragY = mouseY;
}
} else if (selectedObject instanceof PenRig) {
// For pen arm, use startX, endX to get closest anchor point on pen arm. Then reposition/rotate so that anchorP is as close as possible to mouseX/mouseY
// using proper penarm quantization.
// we solve rotation first (using mouse -> arm pivot, translated for parent), then length is fairly easy.
//
PenRig pr = (PenRig) selectedObject;
float dm = dist(mouseX, mouseY, startDragX, startDragX);
if (abs(dm) > 10) {
PVector ap = pr.itsMP.getPosition(); // position of mount
PVector pp = pr.getPosition(); // position of pen
float startDragLen = dist(startDragX,startDragY,pp.x,pp.y);
float gPenAngle, lenScale;
if (startDragLen/(0.5*inchesToPoints) > pr.len) {
// We are on opposite side of mount point from pen
gPenAngle = atan2(ap.y-pp.y,ap.x-pp.x); // this is for moving pen when we're on the opposite side from pen
lenScale= -1;
} else {
gPenAngle = atan2(pp.y-ap.y,pp.x-ap.x); // this causes us to be moving pen arm if we're close to pen...
lenScale = 1;
}
float lAngleOffset = radians(pr.angle) - gPenAngle; // adjustment to stored angle, in radians
float desiredAngle = atan2(mouseY-ap.y,mouseX-ap.x);
pr.angle = degrees(desiredAngle+lAngleOffset);
pr.angle = round(pr.angle / 5)*5;
float oLen = dist(startDragX,startDragY,ap.x,ap.y);
float desLen = dist(mouseX, mouseY, ap.x, ap.y);
pr.len += lenScale*(desLen-oLen)/(0.5*inchesToPoints);
pr.len = round(pr.len / 0.125)*0.125;
setupPens[setupMode][1] = pr.angle;
setupPens[setupMode][0] = pr.len;
doSaveSetup();
startDragX = mouseX;
startDragY = mouseY;
}
} else {
float dm = dist(mouseX, mouseY, startDragX, startDragX);
if (abs(dm) > 10) {
float a = atan2(mouseY-startDragY, mouseX-startDragX);
if (a >= -PI/4 && a <= PI/4) {
direction = 1;
keycode = RIGHT;
} else if (a >= 3*PI/4 || a <= -3*PI/4) {
direction = -1;
keycode = LEFT;
} else if (a >= -3*PI/4 && a <= -PI/4) {
direction = 1;
keycode = UP;
} else if (a >= PI/4 && a <= 3*PI/4) {
direction = -1;
keycode = DOWN;
}
startDragX = mouseX;
startDragY = mouseY;
}
}
if (direction != 0)
nudge(direction, keycode);
}
}
void deselect() {
if (selectedObject != null) {
selectedObject.unselect();
selectedObject = null;
}
}
void advancePenColor(int direction) {
penColorIdx = (penColorIdx + penColors.length + direction) % penColors.length;
penColor = penColors[penColorIdx];
paper.beginDraw();
paper.stroke(penColor);
paper.endDraw();
if (direction != 0) {
doSaveSetup();
}
}
void advancePenWidth(int direction) {
penWidthIdx = (penWidthIdx + penWidths.length + direction) % penWidths.length;
penWidth = penWidths[penWidthIdx];
paper.beginDraw();
paper.strokeWeight(penWidth);
paper.endDraw();
if (direction != 0) {
doSaveSetup();
}
}
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("" + int(n/2+1),notchX,lilNotch);
}
notchSize += widIncr;
notchX -= notchIncr;
}
popMatrix();
}
class CDMSSetup {
int[][] setupTeeth;
float[][] setupMounts;
float[][] setupPens;
Boolean[][] setupInversions;
int penColorIdx, penWidthIdx;
CDMSSetup(int setupMode, int penColorIdx, int penWidthIdx, int[][] setupTeeth, float[][] setupMounts, float[][] setupPens, Boolean[][] setupInversions)
{
this.penColorIdx = penColorIdx;
this.penWidthIdx = penWidthIdx;
this.setupMode = setupMode;
this.setupTeeth = setupTeeth;
this.setupMounts = setupMounts;
this.setupPens = setupPens;
this.setupInversions = setupInversions;
}
};
void doSaveSetup()
{
CDMSSetup tsetup = new CDMSSetup(setupMode, penColorIdx, penWidthIdx, setupTeeth, setupMounts, setupPens, setupInversions);
jsSaveSetups(tsetup);
}
void doLoadSetup()
{
CDMSSetup tsetup = new CDMSSetup(setupMode, penColorIdx, penWidthIdx, setupTeeth, setupMounts, setupPens, setupInversions);
jsLoadSetups(tsetup);
setupTeeth = tsetup.setupTeeth;
setupMounts = tsetup.setupMounts;
setupPens = tsetup.setupPens;
setupInversions = tsetup.setupInversions;
setupMode = tsetup.setupMode;
penColorIdx = tsetup.penColorIdx;
penWidthIdx = tsetup.penWidthIdx;
advancePenColor(0);
advancePenWidth(0);
}
void doSnapshot()
{
//
// background(255);
// image(paper,0,0);
// save("untitled.png");
makeSnapshot(paper, turnTable.rotation, saveFilename("cdm_"));
}
void issueCmd(String cmd, String subcmd) {
if (cmd.equals("play")) {
passesPerFrame = 1;
isMoving = true;
drawDirection = 1;
myLastFrame = -1;
} else if (cmd.equals("pause")) {
isMoving = false;
drawDirection = 1;
myLastFrame = -1;
} else if (cmd.equals("ff")) {
passesPerFrame = 10;
drawDirection = 1;
isMoving = true;
} else if (cmd.equals("fff")) {
drawDirection = 1;
completeDrawing();
} else if (cmd.equals("rr")) {
drawDirection = -1;
passesPerFrame = 1;
isMoving = true;
} else if (cmd.equals("rrr")) {
drawDirection = -1;
passesPerFrame = 10;
isMoving = true;
} else if (cmd.equals("erase")) {
clearPaper();
} else if (cmd.equals("setup")) {
int setupMode = int(subcmd);
deselect();
drawingSetup(setupMode, false);
doSaveSetup();
} else if (cmd.equals("snapshot")) {
doSnapshot();
} else if (cmd.equals("help")) {
toggleHelp();
// alert("Help is coming soon...");
}
}
String getSetupMode()
{
return setupMode;
}
int getDrawDirection()
{
return drawDirection;
}
int getPassesPerFrame()
{
return passesPerFrame;
}
Boolean getIsMoving()
{
return isMoving;
}