-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaton_magique.pde
334 lines (268 loc) · 8.19 KB
/
baton_magique.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
// Baton magique
// program written for a workshop with children aged 6-8
// to draw letters with a torch and upload them to a laser cutter
// l’atelier des chercheurs - https://latelier-des-chercheurs.fr/
// - Super Fast Blur v1.1 by Mario Klingemann <http://incubator.quasimondo.com>
// - BlobDetection library
import processing.video.*;
import blobDetection.*;
import controlP5.*;
import java.text.*;
import java.util.*;
import processing.svg.PGraphicsSVG;
ControlFrame cf;
Capture cam;
BlobDetection theBlobDetection;
boolean recordSVG = false;
boolean isDebug = false;
PImage img;
boolean newFrame=false;
// set default in controlFrame.pde
float brightnessThreshold;
boolean showCamera, showBlobDetection;
int easeTraces;
String currentMode = "mouse";
PGraphics canvas;
int setEpaisseurBoitier = 0;
int defaultEpaisseurThickness = 5;
String[] modesAvailable = {"mouse"};
String[] allConnectedCameras = {};
// in lineCoords, x and y are used for position and z is used for the timestamp of the capture
ArrayList<PVector> lineCoords = new ArrayList();
PVector pointToTrace = new PVector(0, 0);
PVector currentPointPosition = new PVector(0, 0);
String setExportSuffix = "";
// for animation when exporting is done
int exportingAnimationMaxTime = 50;
int exportingAnimation = 0;
color[] exportAnimationColors = { color(27, 47, 129), color(75, 192, 180), color(255, 190, 50), color(255, 62, 81) };
color exportRectangleColor;
float lineTrace = 0;
// function necessary for controlFrame
void settings() {
size(1200, 800);
}
void setup()
{
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println("camera at i: " + i + cameras[i]);
}
// The camera can be initialized directly using an
// element from the array returned by list():
cam = new Capture(this, cameras[0]);
cam.start();
// BlobDetection
// img which will be sent to detection (a smaller copy of the cam frame);
img = new PImage(192, 108);
theBlobDetection = new BlobDetection(img.width, img.height);
theBlobDetection.setPosDiscrimination(true);
modesAvailable = append(modesAvailable, "camera");
allConnectedCameras = concat(allConnectedCameras, cameras);
}
cf = new ControlFrame(this, 480, 640, "Controls");
canvas = createGraphics(640, 480);
}
// ==================================================
// draw()
// ==================================================
void draw()
{
if (exportingAnimation > 0) {
exportingAnimation--;
println("export animation: " + exportingAnimation);
exportAnimation();
} else {
background(0);
// check if the coordinate is empty
if (pointToTrace.mag() == 0) {
recordCoordinates(currentPointPosition);
currentPointPosition = new PVector(0, 0);
} else if (PVector.dist(pointToTrace, currentPointPosition) > 0) {
//println("New point detected. Number of points in path : " + lineCoords.size());
if (currentPointPosition.mag() == 0) {
currentPointPosition = pointToTrace.copy();
}
currentPointPosition.lerp(pointToTrace, (easeTraces)/100.0f);
// show it, record it
recordCoordinates(currentPointPosition);
}
drawCoordinates();
if(recordSVG) {
exportShapeSVG();
recordSVG = false;
}
}
}
void recordCoordinates(PVector newVector) {
lineCoords.add(newVector.copy());
}
void drawCoordinates() {
ArrayList<PVector>[] lines = new ArrayList[20];
int idx = 0;
lines[idx] = new ArrayList();
boolean newLineCreated = true;
for (int i=0; i<lineCoords.size(); i++) {
if (lineCoords.get(i).mag() == 0) {
if (!newLineCreated && idx < lines.length-1) {
idx++;
lines[idx] = new ArrayList();
newLineCreated = true;
}
} else {
lines[idx].add(lineCoords.get(i));
newLineCreated = false;
}
}
if (idx == 0 && lines[idx].isEmpty()) {
//return;
}
for (int index=idx; index>=0; index--) {
if (lines[index].isEmpty())
continue;
PVector[] listOfPoints = new PVector[lines[index].size()];
listOfPoints = lines[index].toArray(listOfPoints);
// dessiner le trait gauche
noStroke();
fill(255);
strokeWeight(0);
if (isDebug) {
stroke(255, 0, 0);
fill(255,0,255);
noFill();
strokeWeight(2);
}
lineTrace = 0;
beginShape();
for (int i=2; i<listOfPoints.length; i++) {
PVector ninety = getNinetyAtPoint(listOfPoints, i);
PVector ninety2 = getNinetyAtPoint(listOfPoints, i-1);
vertex(ninety.x, ninety.y);
if(isDebug)
ellipse(ninety.x, ninety.y, 10, 10);
}
for (int i=listOfPoints.length-1; i>2; i--) {
PVector mninety = getMNinetyAtPoint(listOfPoints, i);
PVector mninety2 = getMNinetyAtPoint(listOfPoints, i-1);
vertex( mninety.x, mninety.y);
//vertex( mninety2.x, mninety2.y);
if(isDebug)
ellipse(mninety.x, mninety.y, 10, 10);
}
endShape(CLOSE);
stroke(255, 0, 0);
noFill();
strokeWeight(0);
if (isDebug) {
strokeWeight(2);
}
if(recordSVG || isDebug) {
stroke(0, 0, 255);
beginShape();
for (int i=0; i<listOfPoints.length; i++) {
PVector coord = listOfPoints[i];
vertex(coord.x, coord.y);
//print("-- i: " + i + " and x:" + coord.x);
}
endShape();
}
stroke(255, 255, 0);
noFill();
if (isDebug) {
point(currentPointPosition.x, currentPointPosition.y);
point(pointToTrace.x, pointToTrace.y);
}
}
stroke(255,0,0);
noFill();
strokeWeight(1);
int carreWidth = 600;
rect( width/2 - carreWidth/2, height/2 - carreWidth/2, carreWidth, carreWidth);
}
PVector getNinetyAtPoint(PVector[] listOfPoints, int i) {
PVector coord1 = listOfPoints[i-1];
PVector coord2 = listOfPoints[i];
PVector diff = PVector.sub(coord1, coord2);
lineTrace = lerp(lineTrace, diff.mag()*2, .4);
PVector ninety = PVector.fromAngle(diff.heading() - PI/2);
ninety
.normalize()
.setMag(lineTrace + 5 + setEpaisseurBoitier)
.limit(30 + setEpaisseurBoitier)
.add(coord2)
;
return ninety;
}
PVector getMNinetyAtPoint(PVector[] listOfPoints, int i) {
PVector coord1 = listOfPoints[i-1];
PVector coord2 = listOfPoints[i];
PVector diff = PVector.sub(coord1, coord2);
lineTrace = lerp(lineTrace, diff.mag()*2, .4);
PVector mninety = PVector.fromAngle(diff.heading() + PI/2);
mninety
.normalize()
.setMag(lineTrace + 5 + setEpaisseurBoitier)
.limit(30 + setEpaisseurBoitier)
.add(coord2)
;
return mninety;
}
void captureEvent(Capture cam)
{
cam.read();
newFrame = true;
}
public void startOver() {
lineCoords.clear();
}
public void exportSVG() {
println("EXPORT SVG");
recordSVG = true;
}
void exportShapeSVG() {
println("exportShapeSVG");
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss");
java.util.Date d = new java.util.Date();
String dateFichier = formatter.format(d);
String nomFichier = dateFichier + "_" + setExportSuffix + ".svg";
println("Check point");
beginRecord(SVG, "exports/" + nomFichier);
background(0);
fill(255);
drawCoordinates();
endRecord();
println("Export SVG done. Now animating");
exportingAnimation = exportingAnimationMaxTime;
exportRectangleColor = exportAnimationColors[int(random(exportAnimationColors.length))];
}
void mouseDragged() {
if (currentMode == "mouse") {
pointToTrace = new PVector(mouseX, mouseY);
}
}
void mouseReleased() {
pointToTrace = new PVector(0, 0);
}
// code duplicate with controlframe
void keyReleased() {
// dodoc box, blue arrow left
if (key == 'w') {
// reduce stroke weight
setEpaisseurBoitier = setEpaisseurBoitier <= 0 ? 0 : (setEpaisseurBoitier - 2);
// dodoc box, blue arrow right
} else if (key == 's') {
setEpaisseurBoitier = setEpaisseurBoitier >= 20 ? 20 : (setEpaisseurBoitier + 2);
// dodoc box, green button
} else if (key == 'a') {
recordSVG = true;
} else if(key==' '){
startOver();
} else {
setExportSuffix = key + "";
}
println("setEpaisseurBoitier ? " + setEpaisseurBoitier);
}