-
Notifications
You must be signed in to change notification settings - Fork 4
/
OriginalGCode.pde
297 lines (253 loc) · 10.9 KB
/
OriginalGCode.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
//-----------------------------------------------------------------------------------------
/*
This work is licensed under the Creative Commons Attribution-NonCommercial-Repurcusssions 3.0 Unported License.
To view a copy of this license, visit http://www.graffitiresearchlab.fr/?portfolio=attribution-noncommercial-repercussions-3-0-unported-cc-by-nc-3-0
ORIGINAL G-CODE
Graffiti Research Lab Germany
http://www.graffitiresearchlab.de/original-gcode/
Processing 1.5.1
BlobDetection Library by v3ga <http://processing.v3ga.net>
GML4U Library by Jérôme Saint-Clair <http://www.saint-clair.net/gml-gml4u/>
ControlP5 Library by Andreas Schlegel <http://www.sojamo.de/libraries/controlP5/>
Super Fast Blur v1.1 by Mario Klingemann <http://incubator.quasimondo.com>
*/
//-----------------------------------------------------------------------------------------
// LIBRARY IMPORTS
import blobDetection.*;
import processing.dxf.*;
import processing.pdf.*;
import processing.opengl.*;
import controlP5.*;
import processing.video.*;
import gml4u.drawing.GmlBrushManager;
import gml4u.events.GmlEvent;
import gml4u.model.GmlBrush;
import gml4u.brushes.*;
import gml4u.model.GmlConstants;
import gml4u.model.GmlStroke;
import gml4u.model.Gml;
import gml4u.recording.GmlRecorder;
import gml4u.utils.GmlSaver;
import toxi.geom.Vec3D;
// CLASS DECLARATION
BlobDetection theBlobDetection;
ControlP5 controlP5;
Capture cam;
PrintWriter output;
GmlRecorder recorder;
GmlSaver saver;
GmlBrushManager brushes = new GmlBrushManager();
GmlBrushManager brushManager;
XMLElement xml;
// VARIABLE DECLARATIONS
PFont font; // For drawign GUI
PImage img; // Detected Blobs
PImage logo; // Logo
boolean newFrame = false; // Video Capture
// GUI Variables for Controlling Export Options
boolean detect = false;
boolean export = false;
boolean clear = false;
boolean saveDXF = false;
boolean saveJPG = false;
boolean saveGMLf = false;
boolean saveGMLw = false;
boolean saveGCODE = false;
float gcFeed, gcZUP, gcZDN;
// An Array of Blobs that we can turn off
boolean[] blbs = {true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true,
true,true,true,true,true,true,true,true,true,true,true,true};
float thresh = 0.24; // Threshold of Blob Detection
int blurVal = 1; // Default Blur Amount
float scale; // To remap normalized GML values <screenScale></screenScale>
int exportCounter = 0; // To Iterate Exports, so we dont Overwrite
int id, camID; String name, attribute; // XML Variables from settings.xml
//-----------------------------------------------------------------------------------------
void setup() {
// Screen stuff
// Capture.list(); // OPENGL workaround
String[] devices = Capture.list();
// println(devices);
size(1600,800,OPENGL);
background(0);
// XML
xml = new XMLElement(this, "settings.xml"); // load our settings
int numTags = xml.getChildCount();
for(int i = 0; i < numTags; i++) {
XMLElement kid = xml.getChild(i);
int id = kid.getInt("id");
// Overly cautious, but making sure mistaked editing wont destroy parsing...
if(id == 0) { // 1st tag
name = kid.getString("name"); // 'camera'
}
if(name.equals("camera")) {
attribute = kid.getContent(); // number
}
}
int camID = int(attribute); // convert our parsed string to an int
// Capture
cam = new Capture(this, 640, 480, devices[camID]); // device id will change per machine
img = new PImage(320,240);
// Blob Detection
theBlobDetection = new BlobDetection(img.width, img.height);
theBlobDetection.setPosDiscrimination(true);
// Font
font = createFont("Verdana", 18);
// Writer
//output = createWriter("exports/GCode.txt"); // moved to control tab
// Draw Labels
labels();
// GML Recorder
Vec3D screen = new Vec3D(cam.width, cam.height, 0);
brushManager = new GmlBrushManager();
scale = 640;
recorder = new GmlRecorder(screen, 0.015f, 0.01f);
saver = new GmlSaver(500, "", this);
saver.start();
// CP5
controlP5 = new ControlP5(this);
// CTRL
controlP5.addSlider("THRESHOLD",0,1,0.14, 725, 100, 100, 20);
controlP5.addSlider("BLUR_VALUE",1,10,1, 725, 130, 100, 20);
// EXPORT OPTIONS
controlP5.addToggle("DETECT",false,725,200,30,30);
controlP5.addBang("EXPORT",785,200,30,30);
controlP5.addToggle("DXF", false,725,285,20,20);
controlP5.addToggle("JPG", false,785,285,20,20);
controlP5.addToggle("GML_FILE",false,725,325,20,20);
controlP5.addToggle("GML_WEB",false,785,325,20,20);
controlP5.addToggle("GCODE", false, 725,365,20,20);
controlP5.addSlider("FEEDRATE",50,500,120,725,400,100,20);
controlP5.addSlider("Z_UP",-5,5,0,725,430,100,20);
controlP5.addSlider("Z_DOWN",-5,5,0,725,460,100,20);
// BLOB TOGGLE
ControlGroup bl = controlP5.addGroup("BLOBS", 50, 575, 300);
controlP5.addToggle("_00",false,0,10,20,20).setGroup(bl);
controlP5.addToggle("_01",false,25,10,20,20).setGroup(bl);
controlP5.addToggle("_02",false,50,10,20,20).setGroup(bl);
controlP5.addToggle("_03",false,75,10,20,20).setGroup(bl);
controlP5.addToggle("_04",false,100,10,20,20).setGroup(bl);
controlP5.addToggle("_05",false,125,10,20,20).setGroup(bl);
controlP5.addToggle("_06",false,150,10,20,20).setGroup(bl);
controlP5.addToggle("_07",false,175,10,20,20).setGroup(bl);
controlP5.addToggle("_08",false,200,10,20,20).setGroup(bl);
controlP5.addToggle("_09",false,225,10,20,20).setGroup(bl);
controlP5.addToggle("_10",false,250,10,20,20).setGroup(bl);
controlP5.addToggle("_11",false,275,10,20,20).setGroup(bl);
controlP5.addToggle("_12",false,0,50,20,20).setGroup(bl);
controlP5.addToggle("_13",false,25,50,20,20).setGroup(bl);
controlP5.addToggle("_14",false,50,50,20,20).setGroup(bl);
controlP5.addToggle("_15",false,75,50,20,20).setGroup(bl);
controlP5.addToggle("_16",false,100,50,20,20).setGroup(bl);
controlP5.addToggle("_17",false,125,50,20,20).setGroup(bl);
controlP5.addToggle("_18",false,150,50,20,20).setGroup(bl);
controlP5.addToggle("_19",false,175,50,20,20).setGroup(bl);
controlP5.addToggle("_20",false,200,50,20,20).setGroup(bl);
controlP5.addToggle("_21",false,225,50,20,20).setGroup(bl);
controlP5.addToggle("_22",false,250,50,20,20).setGroup(bl);
controlP5.addToggle("_23",false,275,50,20,20).setGroup(bl);
controlP5.addToggle("_24",false,0,90,20,20).setGroup(bl);
controlP5.addToggle("_25",false,25,90,20,20).setGroup(bl);
controlP5.addToggle("_26",false,50,90,20,20).setGroup(bl);
controlP5.addToggle("_27",false,75,90,20,20).setGroup(bl);
controlP5.addToggle("_28",false,100,90,20,20).setGroup(bl);
controlP5.addToggle("_29",false,125,90,20,20).setGroup(bl);
controlP5.addToggle("_30",false,150,90,20,20).setGroup(bl);
controlP5.addToggle("_31",false,175,90,20,20).setGroup(bl);
controlP5.addToggle("_32",false,200,90,20,20).setGroup(bl);
controlP5.addToggle("_33",false,225,90,20,20).setGroup(bl);
controlP5.addToggle("_34",false,250,90,20,20).setGroup(bl);
controlP5.addToggle("_35",false,275,90,20,20).setGroup(bl);
} // end Setup
//-----------------------------------------------------------------------------------------
void captureEvent(Capture cam) {
cam.read(); // read a new frame from the camera
newFrame = true;
} // end captureEvent
//-----------------------------------------------------------------------------------------
void draw() {
background(0);
labels();
//int fps = round(frameRate); println(fps);
// GUI adjustments (the rest is in the control tab)
theBlobDetection.setThreshold(thresh);
// Video Windows
noFill();
strokeWeight(2);
stroke(255);
// Preview Window
pushMatrix();
scale(-1,1);
translate(-890-320,-50-241);
translate(-890-320,50+241);
rect(889, 49, 642, 482);
image(cam,890,50,640,480); // Preview
popMatrix();
// Effect Window
rect(50, 50, 640, 480);
img.copy(cam, 0, 0, cam.width, cam.height, 0,0, img.width, img.height); // Output
// Start Exporting
if (export == true) {
println("EXPORTING AT FRAME " + frameCount);
if(saveDXF == true) {
println("SAVING DXF FILE");
beginRaw(DXF, "exports/output_" + exportCounter + ".dxf");
}
}
// draw stuff
if (newFrame == true) {
newFrame = false;
fastblur(img, blurVal); // apply blur to soften the image
pushMatrix();
scale(-1,1); // mirror it for reference
translate(-50-320,-50-241);
translate(-50-320,50+241);
theBlobDetection.computeBlobs(img.pixels); // compute the blobs
translate(50,50); // offset from corner
drawBlobsAndEdges(false ,true); // draw the blobs (and not the bounding rects)
popMatrix();
}
// Start Exporting
if (export == true) {
println("EXPORTING AT FRAME " + frameCount);
if(saveJPG == true) {
println("SAVING JPG SCREENSHOT");
saveFrame("exports/output_" + exportCounter + ".jpg");
}
if(saveGMLf == true) {
println("SAVING GML FILE");
gml();
recorder.endStroke(0);
saver.save(recorder.getGml(), sketchPath + "/exports/gml_" + exportCounter + ".xml");
}
if(saveGMLw == true) {
println("SAVING GML TO 000000BOOK.COM");
// upload to 000000book.com
}
if(saveGCODE == true) {
println("SAVING GCODE");
gc();
output.flush();
output.close();
}
}
// Stop Exporting
if (export == true) {
endRaw();
export = false;
println("STOPPING AT FRAME " + frameCount);
}
controlP5.draw();
} // end Draw
//-----------------------------------------------------------------------------------------