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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
|
#include "ofApp.h"
#include "glew.h"
const ofPoint outputWindowSize=ofPoint(1200,900);
const float guiScale=560.0f/4096.0f;
//--------------------------------------------------------------
void ofApp::setup(){
warpframe[0]=glm::vec2(0,0);
warpframe[1]=glm::vec2(outputWindowSize.x,0);
warpframe[2]=glm::vec2(outputWindowSize.x,outputWindowSize.y);
warpframe[3]=glm::vec2(0,outputWindowSize.y);
select_warpframe=-1;
bDrawFrame=false;
gui.setup();
source=SVG_outlines;
//==================================================== ofxNDI
senderName[0] = 0; // The sender name used for display
nSenders = 0; // Total number of NDI senders
senderWidth = 0; // Sender width
senderHeight = 0; // Sender height
bNDIreceiver = false; // Receiver creation
// Create an intial receiving image
ndiImage.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR_ALPHA);
// For received frame fps calculations - independent of the rendering rate
startTime = lastTime = frameTime = 0;
fps = frameRate = 1; // starting value
//============================ GUI
gui.add(contour_threshold.setup("threshold", 140, 0, 255));
gui.add(contour_simplify.setup("simplify", 0.2, 0.0, 1.0));
gui.add(contour_useColour.setup("use colour", true));
gui.add(laser_R.setup("red", 140, 0, 255));
gui.add(laser_G.setup("green", 140, 0, 255));
gui.add(laser_B.setup("blue", 140, 0, 255));
gui.add(laser_intensity.setup("intensity", 30, 0, 255));
gui.add(laser_points.setup("points", 20000, 0, 40000));
gui.add(shapes_randomise.setup("randomise shapes", true));
gui.add(shapes_amount.setup("shapes amount", 0.2, 0.0, 0.8));
gui.add(shapes_duration.setup("shape duration", 5, 0, 25));
framecounter=0;
//======================================= //positioning interface
bShowPositionInterface=false;
bOutputSelected=false;
outputPosition=ofPoint(0,0);
outputScale=1.0f;
outputOffsetScale=1.0f;
commandPressed=false;
}
//--------------------------------------------------------------
void ofApp::updateOutput(ofEventArgs & args){
laser.set_pts(laser_points);
laser.set_intensity(laser_intensity);
}
void ofApp::update(){
// Update the NDI sender list to find new senders
// There is no delay if no new senders are found
nSenders = ndiReceiver.FindSenders();
if(nSenders > 0) {
// Has the user changed the sender index ?
if(ndiReceiver.SenderSelected()) {
// Release the current receiver.
// A new one is then created from the selected sender index.
ndiReceiver.ReleaseReceiver();
bNDIreceiver = false;
}
// Create a new receiver if one does not exist.
// We don't know the sender dimensions until a frame is received.
if(!bNDIreceiver) {
// The receiver will detect which format a sender is using and convert
// the pixel buffer to BGRA for return. However, we can specify here
// that RGBA is the preferred format.
bNDIreceiver = ndiReceiver.CreateReceiver(NDIlib_recv_color_format_e_RGBX_RGBA);
// bNDIreceiver = ndiReceiver.CreateReceiver(); // default is BRRA
//
// A receiver is created from an index into a list of sender names.
// The current user selected index is saved in the NDIreceiver class
// and is used to create the receiver unless you specify a particular index.
//
// The name of the sender can also be retrieved if you need it.
// If you specified a particular sender index to create the receiver
// use that index to retrieve it's name.
//
// In this application we use it to display the sender name.
//
ndiReceiver.GetSenderName(senderName);
if(bNDIreceiver)
cout << "Created NDI receiver for " << senderName << endl;
// Reset the starting values for frame rate calulations
fps = frameRate = 1;
}
}
if(bNDIreceiver) {
unsigned int width = 0;
unsigned int height = 0;
// If the NDI sender uses BGRA format, the received buffer is converted to rgba by ReceiveImage.
// Optionally you can flip the image if necessary.
// !CHECKME MACOS
// ReceiveImage needs an unsigned char* so changed
// ndiImage.getPixels() to ndiImage.getPixels().getData()
// to avoid type error
if(ndiReceiver.ReceiveImage(ndiImage.getPixels().getData(), width, height, false)) { // receives as rgba
ndiImage.update();
// ----------------------------
// Calculate received frame fps
lastTime = startTime;
startTime = ofGetElapsedTimeMicros();
frameTime = (startTime - lastTime)/1000000; // seconds
if( frameTime > 0.01) {
frameRate = floor(1.0/frameTime + 0.5);
// damping from a starting fps value
fps *= 0.95;
fps += 0.05*frameRate;
}
// ----------------------------
// Have the NDI sender dimensions changed ?
if(senderWidth != width || senderHeight != height) {
// Update the sender dimensions
senderWidth = width;
senderHeight = height;
// Update the receiving image size
ndiImage.allocate(senderWidth, senderHeight, OF_IMAGE_COLOR_ALPHA);
}
}
}
if (movie.isLoaded()){
movie.update();
}
}
const ofPoint previewframesize=ofPoint(320,240);
//-------------------------------------------------------------- GUI
void ofApp::draw(){
ofBackground(0);
ofSetColor(255);
ofNoFill();
if (bShowPositionInterface){
glPushMatrix();
glTranslatef(20,20,0);
ofDrawRectangle(0,0,560,560);
glTranslatef(outputOffset.x,outputOffset.y,0);
glScalef(guiScale,guiScale,guiScale );
glTranslatef(2048.0f+outputPosition.x,2048.0f+outputPosition.y,0);
if (bOutputSelected) {
if (commandPressed) {
ofSetColor(0,255,0);
}
else {
ofSetColor(255,0,0);
}
}
ofDrawRectangle(
(-outputWindowSize.x/2)*outputScale*outputOffsetScale,
(-outputWindowSize.y/2)*outputScale*outputOffsetScale,
outputWindowSize.x*outputScale*outputOffsetScale,
outputWindowSize.y*outputScale*outputOffsetScale);
glPopMatrix();
}
else {
gui.draw();
//================================== NDI
/*
glPushMatrix();
glTranslatef(230,0,0);
ofDrawRectangle(20,20,previewframesize.x+4,previewframesize.y+4);
char str[256];
if(bNDIreceiver) {
ndiImage.draw(22, 22, previewframesize.x, previewframesize.y);
// Show fps etc.
if(nSenders > 0) {
if(bNDIreceiver) {
#ifdef _MSC_VER
sprintf_s(str, 256, "[%s] (%dx%d) - fps %2.0f", senderName, senderWidth, senderHeight, fps);
#else
// !CHECK MACOS
snprintf(str, 256, "[%s] (%dx%d) - fps %2.0f", senderName, senderWidth, senderHeight, fps);
str[255] = 0;
#endif
ofDrawBitmapString(str, 20, previewframesize.y+42);
}
if(nSenders == 1) {
ofDrawBitmapString("1 network source", 25, 32);
}
else {
#ifdef _MSC_VER
sprintf_s(str, 256, "%d network sources", nSenders);
#else
// !CHECK MACOS
snprintf(str, 256, "%d network sources", nSenders);
str[255] = 0;
#endif
ofDrawBitmapString(str, 25, 32);
//ofDrawBitmapString("'SPACE' to list senders or RH click to open sender dialog", 20, ofGetHeight()-20);
}
}
}
else {
ofDrawBitmapString("Connecting . . .", 25, 32);
}
glPopMatrix();
//================================== NDI
glPushMatrix();
glTranslatef(230,300,0);
*/
//================================== video
glPushMatrix();
glTranslatef(230,0,0);
ofDrawRectangle(20,20,previewframesize.x+4,previewframesize.y+4);
if (movie.isLoaded()){
movie.draw(22, 22, previewframesize.x, previewframesize.y);
}
glPopMatrix();
//================================== SVG
glPushMatrix();
float scale=previewframesize.x/outputWindowSize.x;
glTranslatef(230,300,0);
ofDrawRectangle(20,20,previewframesize.x+4,previewframesize.y+4);
glTranslatef(22,22,0);
glScalef(scale,scale,scale);
for (auto shape=segmenters.begin();shape!=segmenters.end();shape++){
shape->getPoly().draw();
}
glPopMatrix();
}
}
void ofApp::drawOutput(ofEventArgs & args){
ofBackground(0);
//composite output window
ofSetColor(255,255,255);
vector <colourPolyline> laserOutput;
switch (source){
case TEST:{
ofMatrix4x4 m = ofMatrix4x4::newIdentityMatrix();
m.rotateRad(ofGetElapsedTimef(),0,0,1);
m.translate(ofGetWidth()/2,ofGetHeight()/2,0);
glm::vec2 src[]={
glm::vec2(0,0),
glm::vec2(ofGetWidth(),0),
glm::vec2(ofGetWidth(),ofGetHeight()),
glm::vec2(0,ofGetHeight())
};
ofMatrix4x4 warp =lineTransformer::getPerspectiveTransformMatrix(src,warpframe);
//drawPoly(polyLineTransform(makePolygon(4,200),m),200,200);
//drawPoly(polyLineTransform(makePolygon(5,200),m),-200,200);
//drawPoly(polyLineTransform(makePolygon(6,200),m),-200,-200);
ofPolyline poly=lineTransformer::polyLineTransform(warp,
lineTransformer::polyLineTransform(m,
lineTransformer::makePolygon(6,200)
)
);
laserOutput.push_back(colourPolyline(poly,ofColor(laser_R,laser_G,laser_B)));
}
case NDI:{
ofPoint scale=ofPoint(outputWindowSize.x/ndiImage.getWidth(),outputWindowSize.x/ndiImage.getHeight());
//does not work no matter what the fuck you do
//grayImage.setFromPixels(pixels.getData(),ndiImage.getWidth(),ndiImage.getHeight());
//grayImage.draw(0,0,outputWindowSize.x,outputWindowSize.y);
grayImage = colorImg;
grayImage.threshold(contour_threshold);
//virtual int findContours( ofxCvGrayscaleImage& input,
// int minArea, int maxArea,
// int nConsidered, bool bFindHoles,
// bool bUseApproximation = true);
contourFinder.findContours(grayImage, 20, (340*240)/3, 10, true);
for (int i = 0; i < contourFinder.nBlobs; i++){
colourPolyline shape;
for (auto& point:contourFinder.blobs[i].pts){
ofVec3f p=point*scale;
ofColor c=colorImg.getPixels().getColor(point.x,point.y);
shape.addVertex(p,contour_useColour?c:ofColor(laser_R,laser_G,laser_B));
}
shape.simplify(contour_simplify);
laserOutput.push_back(shape);
}
break;
}
case Player:{
if (movie.isLoaded()){
ofPoint scale=ofPoint(outputWindowSize.x/movie.getWidth(),outputWindowSize.y/movie.getHeight());
colorImg.setFromPixels(movie.getPixels());
grayImage = colorImg;
grayImage.threshold(contour_threshold);
contourFinder.findContours(grayImage, 20, (340*240)/3, 10, true);
for (int i = 0; i < contourFinder.nBlobs; i++){
colourPolyline shape;
for (auto& point:contourFinder.blobs[i].pts){
ofVec3f p=point*scale;
ofColor c=colorImg.getPixels().getColor(point.x,point.y);
shape.addVertex(p,contour_useColour?c:ofColor(laser_R,laser_G,laser_B));
}
shape.simplify(contour_simplify);
laserOutput.push_back(shape);
}
}
break;
}
case SVG_outlines:{
if (shapes_randomise){
if (framecounter==0){
select_random_shapes();
framecounter=shapes_duration;
}
for (auto s:shape_selection){
laserOutput.push_back(colourPolyline(segmenters[s].getPoly(),ofColor(laser_R,laser_G,laser_B)));
}
framecounter--;
}
else {
for (auto shape=segmenters.begin();shape!=segmenters.end();shape++){
laserOutput.push_back(colourPolyline(shape->getPoly(),ofColor(laser_R,laser_G,laser_B)));
}
}
break;
}
}
int num = 0;
if (laserOutput.size()){
num=laser.draw(laserOutput);
for (auto& shape:laserOutput){
shape.draw();
}
}
if (bDrawFrame){
lineTransformer::drawWarpFrame(warpframe);
}
if (num>0){
ofSetWindowTitle(ofToString(ofGetFrameRate(), 2)+" fps laser points: "+ofToString(num));
}
else {
ofSetWindowTitle(ofToString(ofGetFrameRate(), 2)+" fps laser error ");
}
}
//--------------------------------------------------------------
void ofApp::exit() {
}
//--------------------------------------------------------------
void ofApp::outputKeyPressed(ofKeyEventArgs &args){
keyPressed(args);
}
void ofApp::keyPressed(ofKeyEventArgs &args){
if (args.key==OF_KEY_COMMAND){
commandPressed=true;
}
switch(args.key){
case '`':{
bShowPositionInterface=!bShowPositionInterface;
break;
}
case 'q':{
source--;
if (source<0){
source=Source_end-1;
}
break;
}
case 'p':{
source=(source+1)%Source_end;
break;
}
case 'w':{
bDrawFrame=!bDrawFrame;
break;
}
case OF_KEY_COMMAND:{
commandPressed=true;
}
}
}
//--------------------------------------------------------------
void ofApp::outputKeyReleased(ofKeyEventArgs &args){
outputKeyReleased(args);
}
void ofApp::keyReleased(ofKeyEventArgs &args){
if (args.key==OF_KEY_COMMAND){
commandPressed=false;
}
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::outputMouseDragged(ofMouseEventArgs & args){
if (select_warpframe>-1){
warpframe[select_warpframe]=glm::vec2(args.x,args.y);
}
}
void ofApp::mouseDragged(int x, int y, int button){
if (bOutputSelected){
if (commandPressed){
float startDistance=((outputPosition*guiScale)+ofPoint(300,300)).distance(outputSelectionPoint);
float currentDistance=((outputPosition*guiScale)+ofPoint(300,300)).distance(ofPoint(x,y));
outputOffsetScale=currentDistance/startDistance;
}
else {
outputOffset=ofPoint(x,y)-outputSelectionPoint;
laser.set_centre(ofPoint(
outputPosition.x+(outputOffset.x/guiScale),
outputPosition.y+(outputOffset.y/guiScale)
));
}
}
}
//--------------------------------------------------------------
void ofApp::outputMousePressed(ofMouseEventArgs & args){
for (int i=0;i<4;i++){
if (ofPoint(args.x,args.y).distance(warpframe[i])<25){
select_warpframe=i;
}
}
}
void ofApp::mousePressed(int x, int y, int button){
if (bShowPositionInterface){
if (x>(300+((outputPosition.x-((outputWindowSize.x/2)*outputScale))*guiScale))&&
x<(300+((outputPosition.x+((outputWindowSize.x/2)*outputScale))*guiScale))&&
y>(300+((outputPosition.y-((outputWindowSize.y/2)*outputScale))*guiScale))&&
y<(300+((outputPosition.y+((outputWindowSize.y/2)*outputScale))*guiScale))
){
outputSelectionPoint=ofPoint(x,y);
bOutputSelected=true;
}
}
}
//--------------------------------------------------------------
void ofApp::outputMouseReleased(ofMouseEventArgs & args){
select_warpframe=-1;
}
void ofApp::mouseReleased(int x, int y, int button){
if (bOutputSelected){
if (commandPressed){
outputScale*=outputOffsetScale;
}
else {
outputPosition+=outputOffset/guiScale;
}
bOutputSelected=false;
outputOffset=ofPoint(0,0);
outputOffsetScale=1.0f;
}
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::outputWindowResized(ofResizeEventArgs &resizeargs){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::select_random_shapes(){
shape_selection.clear();
while (shape_selection.size()<(segmenters.size()*shapes_amount)){
int selection=rand()%segmenters.size();
if (shape_selection.find(selection)==shape_selection.end()){
shape_selection.insert(selection);
}
}
std::stringstream strm;
for (auto& s:shape_selection){
strm << s <<" ";
}
//cout << "randomly selected (" << strm.str() <<") \n";
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
if (dragInfo.position.x>250&&dragInfo.position.x<570&&dragInfo.position.y>20&&dragInfo.position.y<266){
std::string filename= *dragInfo.files.begin();
if (movie.load(filename)){
cout << "loaded "<< filename<<std::endl;
movie.setLoopState(OF_LOOP_NORMAL);
movie.play();
}
else {
cout << "failed to load"<<std::endl;
}
}
if (dragInfo.position.x>250&&dragInfo.position.x<570&&dragInfo.position.y>320&&dragInfo.position.y<566){
std::string filename= *dragInfo.files.begin();
svg.load(filename);
vector <ofPath> imagepaths= svg.getPaths();
std::stringstream strm;
if (imagepaths.size()){
segmenters.clear();
for (auto& path:imagepaths){
path.setPolyWindingMode(OF_POLY_WINDING_ODD);
vector <ofPolyline> outlines= path.getOutline();
for (auto& outline:outlines){
strm << outline.size() << "->";
outline.simplify(contour_simplify);
strm << outline.size() << " ";
segmenters.push_back(lineSegmenter(outline));
}
strm << " , ";
}
cout << "SVG: found " << imagepaths.size() << " paths with " << segmenters.size() << " shapes [ " << strm.str() << " ]" <<std::endl;
select_random_shapes();
}
}
}
//printf("%s at %i %i \n",dragInfo.files.begin(),dragInfo.position.x,dragInfo.position.y);
//std::string filenames;
//cout << dragInfo.files.begin()->c_str()<<","<<dragInfo.position.x<<","<<dragInfo.position.y<<std::endl;
//for (auto f = dragInfo.files.begin(); f != dragInfo.files.end(); f++){
// cout << f->c_str()<<","<<dragInfo.position.x<<","<<dragInfo.position.y<<std::endl;
//if (f!=dragInfo.files.begin()){
// filenames=filenames+", ";
//}
//}
// filenames=filenames+*f;
// if (dragInfo.position.y<200){
// sets[0].addfiles(dragInfo.files); //,dragInfo.position);
// }
// else sets[1].addfiles(dragInfo.files); //,dragInfo.position);
//}
|