forked from lucasw/vimjay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.cpp
More file actions
2053 lines (1657 loc) · 54 KB
/
Copy pathnodes.cpp
File metadata and controls
2053 lines (1657 loc) · 54 KB
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2012 Lucas Walter
This file is part of Vimjay.
Vimjay is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Vimjay is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vimjay. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <boost/thread.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/lexical_cast.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <glog/logging.h>
#include <deque>
//#include <pair>
#include "nodes.h"
#include "config.h"
using namespace cv;
using namespace std;
namespace bm {
unsigned long
hashdjb2(const char *str)
{
unsigned long hash = 5381;
int c;
while (c = (unsigned char)(*str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
cv::Scalar hashStringColor(const string str) {
unsigned long val = hashdjb2(str.c_str());
int val2 = val % 0xffffff;
int r = (val2 & 0xff0000) >> 16;
int g = (val2 & 0x00ff00) >> 8;
int b = (val2 & 0x0000ff) >> 0;
VLOG(6) << "hash color 0x" << std::hex << val << " 0x" << std::hex << val2
<< " : 0x" << std::hex << r << " 0x" << std::hex << g << " 0x" << std::hex << b;
return cv::Scalar(r,g,b);
}
//////////////////////////////////////////////////////////////////////////////////////
//Elem::Elem() : name("undefined"), highlight(false), highlight2(false)
//{
//}
Elem::Elem(const string name) :
name(name),
highlight(false),
highlight2(false),
do_update(false)
{
}
bool Elem::isDirty(const void* caller, const int ind, const bool clear)
{
VLOG(4) << name << " " << this << " isDirty " << caller
<< " " << ind << " " << clear;
// first stage
map<const void*, map<int, bool> >::iterator caller_map;
caller_map = dirty_hash.find(caller);
if (caller_map == dirty_hash.end()) {
dirty_hash[caller][ind] = false;
return true;
}
// second stage
map<int, bool>::iterator is_dirty;
is_dirty = caller_map->second.find(ind);
if (is_dirty == caller_map->second.end()) {
dirty_hash[caller][ind] = false;
return true;
}
const bool rv = is_dirty->second;
if (clear) {
dirty_hash[caller][ind] = false;
}
return rv;
}
bool Elem::setDirty()
{
for (map<const void*, map<int, bool> >::iterator it = dirty_hash.begin(); it != dirty_hash.end(); it++) {
for (map<int,bool>::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++) {
it2->second = true;
}
}
}
bool Elem::update()
{
if (!do_update) return false;
VLOG(3) << name << " update";
do_update = false;
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Connector::Connector(const std::string name) :
Elem(name),
parent(NULL),
src(NULL),
dst(NULL),
//writeable(true),
type(SIGNAL),
saturate(0),
val_min(0.0),
val_max(1.0),
value(0),
internally_set(false)
{
}
bool Connector::update()
{
if (!Elem::update()) return false;
// This will frequently already have been run
if (parent) {
VLOG(3) << parent->name << " : " << name << " update";
parent->update();
}
if (src) { // && src->parent) {
src->update();
//}
//if (dst) {
//src->parent->update(); // why not src->update()?
// TBD this overlooks the case where the connector has changed inputs
// but with a src that isn't dirty- the image still needs to propagate,
// w:w
// here does the dirtiness properly belong to force the im = src->im or value = src->value?
if ( src->isDirty(this, 0)
//if ( isDirty(this, 0)
//|| (src->parent == parent) // TBD special loop detection, TBD not sure why this is necessary
) {
VLOG(3) << "src " << src->name << " : " << name << " update";
// the reason we can't forward propagate is that dst isn't a vector
// of every dst, there is a one to many possible mapping
if (type == SIGNAL) setSignal(src->getSignal());
else if (type == IMAGE) setImage(src->getImage());
else if (type == STRING) setString(src->getString());
//if (type == SIGNAL) dst->setSignal(value);
//else if (type == IMAGE) dst->setImage(im);
//else if (type == STRING) dst->setString(str);
// this ought to err on the side of keeping last images before a disconnection
// though that may have already been happening or not happening for reasons
// outside of this assignment? Tested it and it looks like the image
// is kept properly so the check here is unnecessary
// if a connector has a src then it can't be an output
// (at least until it gets overridden as an output)
//internally_set = false;
// TBD get copy of sigbuf
// sigbuf = src->sigbuf;
}
}
return true;
}
bool Connector::setDirty()
{
Elem::setDirty();
if (!internally_set && parent) parent->setDirty();
}
std::string typeToString(const conType type)
{
if (type == IMAGE) {
return "Image";
} else if (type == SIGNAL) {
return "Signal";
} else if (type == BUFFER) {
return "Buffer";
} else if (type == SIGBUF) {
return "SigBuf";
}
return "Unknown";
}
bool Connector::setImage(cv::Mat im)
{
if (im.empty()) return false;
boost::mutex::scoped_lock l(im_mutex);
this->im = im;
setDirty();
return true;
}
bool Connector::setSignal(float val)
{
if (this->value == val) return true;
if (saturate == ROLL) {
//const float span = val_max - val_min;
if (val < val_min) val = val_max;
if (val > val_max) val = val_min;
} else if (saturate == SATURATE) {
if (val < val_min) val = val_min;
if (val > val_max) val = val_max;
} // saturate
if (this->value == val) return true;
VLOG(5) << name << " " << val << " " << val_min << " " << val_max;
this->value = val;
setDirty();
return true;
}
bool Connector::setString(const std::string new_str)
{
VLOG(1) << "setString " << CLTXT << name << " " << CLVAL << new_str << CLNRM;
//if (str.compare(new_str) == 0) return true;
if (str == new_str) return true;
str = new_str;
setDirty();
VLOG(1) << new_str;
return true;
}
cv::Mat Connector::getImage()
{
return im;
}
void Connector::draw(cv::Mat graph_ui, cv::Point2f ui_offset)
{
cv::Scalar hash_col = hashStringColor(/*parent->name +*/ typeToString(type) + name);
VLOG(6) << name << " " << hash_col[0] << " " << hash_col[1] << " " << hash_col[2];
bool is_dirty = isDirty(this, 1);
const int bval = 150 + is_dirty * 105;
hash_col *= (is_dirty ? 1.0 :0.6);
// draw a box around the port
cv::Scalar rect_col = cv::Scalar(40, 50, 40);
if (highlight) {
rect_col = cv::Scalar(140, 80, 80);
}
cv::rectangle(graph_ui,
parent->loc + loc + ui_offset,
parent->loc + loc + ui_offset + cv::Point2f(name.size()*10, -10),
rect_col,
CV_FILLED);
cv::rectangle(graph_ui,
parent->loc + loc + ui_offset,
parent->loc + loc + ui_offset + cv::Point2f(name.size()*10, -10),
hash_col);
if (internally_set) {
cv::rectangle(graph_ui,
parent->loc + loc + ui_offset,
parent->loc + loc + ui_offset - cv::Point2f(5, 5),
cv::Scalar(255, 255, 255),
CV_FILLED);
}
// draw connecting line if there is a connector connected to this one
if (src) {
cv::Scalar dst_hash_col = hashStringColor(/*src->parent->name +*/ typeToString(src->type) + src->name);
vector<cv::Point2f> control_points;
control_points.resize(4);
control_points[0] = src->parent->loc + src->loc + cv::Point2f(src->name.size()*10, -5);
control_points[3] = parent->loc + loc + cv::Point2f(0,-5);
// TBD if control_points dirty
{
cv::Point2f diff = control_points[3] - control_points[0];
float dist = abs(diff.x) + abs(diff.y);
// don't want lines going
float y_off = 0;
if ((control_points[3].x < control_points[0].x) &&
(abs(control_points[3].y - control_points[0].y) < 100)) y_off = 100;
control_points[1] = control_points[0] + cv::Point2f(dist/3.0, y_off);
control_points[2] = control_points[3] - cv::Point2f(dist/3.0, -y_off);
getBezier(control_points, connector_points, 32);
}
// draw dark outline around curve
for (int i = 1; i < connector_points.size(); i++) {
cv::Scalar outline_col = cv::Scalar(10,10,10);
if (highlight2) outline_col *= 4;
cv::line(graph_ui,
connector_points[i-1] + ui_offset,
connector_points[i] + ui_offset,
outline_col, 4, CV_AA );
}
// now draw a colored interior curve
for (int i = 1; i < connector_points.size(); i++) {
cv::Scalar col;
const float fr = (float)i/(float)connector_points.size();
// colorize the lines so that starts and ends are distinct, and
// use the hash colors so similarly named connector ends are similarly colored.
float wt1, wt2;
if (fr < 0.5) {
wt1 = 1.0 * (1.0-fr*fr);
wt2 = 0.0;
} else {
wt1 = ((1.0-fr)*(1.0-fr));
wt2 = 1.0 * (fr);
}
cv::Scalar hc2 = dst_hash_col * cv::Scalar(wt1);
//cv::Scalar hc1 = hash_col *( 1.0-(1.0-fr));
cv::Scalar hc1 = hash_col * cv::Scalar(wt2);
col = hc1 + hc2;
if (highlight2) col *= 3;
if (src && src->highlight2) col *= 2;
cv::line(graph_ui,
connector_points[i-1] + ui_offset,
connector_points[i] + ui_offset,
col, 2, CV_AA );
}
}
stringstream port_info;
port_info << name;
// color based on type late
cv::Scalar col = cv::Scalar(200,200,200);
const int cval = 75;
if (type == SIGNAL) {
col = cv::Scalar(cval, bval, bval);
port_info << " " << setprecision(4) << value;
} else if (type == IMAGE) {
col = cv::Scalar(bval, cval+20, bval);
} else if (type == BUFFER) {
col = cv::Scalar(bval, bval, cval);
} else if (type == SIGBUF) {
col = cv::Scalar(bval, bval/2 + cval/2, bval/2 + cval/2);
} else if (type == STRING) {
col = cv::Scalar(3*bval/4, bval/2, bval + cval/2);
port_info << " " << str;
}
port_info << " " << description;
cv::putText(graph_ui, port_info.str(), parent->loc + loc + ui_offset, 1, 1, col, 1);
}
// Buffers are different than Signals and Images because it isn't desirable to copy them (though maybe the overhead isn't that bad for small buffers?)
// especially the image types. Signal buffers may need different treatment, or some master templating scheme
// needs to take care of it.
#if 0
Buffer* Connector::getBuffer()
{
Buffer* im_in = NULL;
if (
//dynamic_cast<Buffer*> (con->src->parent);
}
#endif
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
Node::Node(const string name) :
Elem(name),
selected_type(NONE),
selected_port_ind(-1),
selected_port("")
{
vcol = cv::Scalar(200,200,200);
setSignal("enable", 1.0, false, SATURATE, 0,1);
setSignal("force_update", 0.0);
}
/*
Node::Node(string name, cv::Point loc, cv::Mat graph_ui ) :
selected_type(NONE),
selected_port_ind(-1),
name(name),
loc(loc),
graph_ui(graph_ui),
do_update(false)
{
//is_dirty = true;
vcol = cv::Scalar(0,128,255);
}
*/
// this finds all the nodes that are connected to this node and sets them to be updated
bool Node::setUpdate()
{
// if do_update is already set return, this prevents infinite loops
if (do_update) return false;
do_update = true;
//boost::mutex::scoped_lock l(port_mutex);
for (int i = 0; i < ports.size(); i++) {
// TBD look for per-port enable here
// TBD ports[i]->setUpdate() could provide both of these
ports[i]->do_update = true;
if (ports[i]->src) ports[i]->src->parent->setUpdate();
}
return true;
}
// the rv is so that an inheriting function will know whether to
// process or not
bool Node::update()
{
if (!Elem::update()) return false;
//boost::mutex::scoped_lock l(port_mutex);
// need to update enable no matter if it is false
for (int i = 0; i < ports.size(); i++)
{
if (ports[i]->name == "enable")
ports[i]->update();
}
// TBD should this occur before updating the inputs?
if (!(getSignal("enable") >= 1.0))
return false;
bool inputs_dirty = false;
for (int i = 0; i < ports.size(); i++)
{
// already updated if named enable
if ((ports[i]->name != "enable") ) { // TBD provide flag to disable updating
ports[i]->update();
}
#if 0
if (!ports[i]->internally_set) {
const bool is_dirty = ports[i]->isDirty(this, 2);
VLOG(2) << name << " " << CLTXT << ports[i]->name << " " << CLNRM << is_dirty;
if (is_dirty) {
inputs_dirty = true;
}
}
#endif
}
#if 0
// the inheriting object needs to setDirty() as appropriate if it
// isn't sufficient to have inputs determine it(e.g. it is sourcing change)
if ( inputs_dirty ) {
setDirty();
}
#endif
// TBD loop through ports and run getImage on each
// or could that be combined with looping through the getInputVector above?
VLOG(3) << name << " in sz " << ports.size() << " inputs dirty " << inputs_dirty;
return true;
}
bool Node::posUpdate()
{
// pos update
vel += acc;
acc = cv::Point2f(0,0);
loc += vel;
vel *= 0.9;
#if 0
if (loc.x < 0) {
loc.x = 0;
vel.x = abs(vel.x)*0.5;
}
if (loc.x > graph_ui.cols) {
loc.x = graph_ui.cols;
vel.x = -abs(vel.x)*0.5;
}
if (loc.y < 0) {
loc.y = 0;
vel.y = abs(vel.y)*0.5;
}
if (loc.y > graph_ui.rows) {
loc.y = graph_ui.rows;
vel.y = -abs(vel.y)*0.5;
}
#endif
}
bool Node::draw(cv::Point2f ui_offset)
{
posUpdate();
if (graph_ui.empty()) {
LOG(ERROR) << "graph empty";
return false;
}
int fr = 1;
if (!isDirty(this,1)) fr = 5;
//cv::Scalar col = cv::Scalar(vcol/fr);
cv::Scalar col = vcol * (1.0/fr); //cv::Scalar(vcol/fr);
if (!(getSignal("enable") >= 1.0))
cv::circle(graph_ui, loc + ui_offset, 10, cv::Scalar(0,0,100), -1);
cv::circle(graph_ui, loc + ui_offset, 24, col, 4);
const int ht = 10;
int j = 0;
if (highlight) {
cv::Scalar selected_color = cv::Scalar(0,220,1);
// draw green circle on selected node
#if 0
cv::circle(graph_ui,
loc + ui_offset,
18,
selected_color*0.8,
-1);
#endif
cv::rectangle(graph_ui,
loc + cv::Point2f(0, -10) + ui_offset,
loc + cv::Point2f(135, ports.size()*10 - 3) + ui_offset,
selected_color,
3);
}
{
boost::mutex::scoped_lock l(port_mutex);
// draw rectangle around entire node
cv::rectangle(graph_ui,
loc + cv::Point2f(-5, -15) + ui_offset,
loc + cv::Point2f(140, ports.size()*10 + 2) + ui_offset,
vcol*0.2, //cv::Scalar(255,0,0),
2);
int max_width = 0;
for (int i = 0; i < ports.size(); i++) {
if (i == selected_port_ind) {
ports[i]->highlight = true;
if (highlight) {
ports[i]->highlight2 = true;
} else {
ports[i]->highlight2 = false;
}
} else {
ports[i]->highlight = false;
ports[i]->highlight2 = false;
}
ports[i]->draw(graph_ui, ui_offset);
}
}
cv::putText(graph_ui, name, loc - cv::Point2f(9, ht) + ui_offset, 1, 1, cv::Scalar(115,115,115));
cv::putText(graph_ui, name, loc - cv::Point2f(10, ht) + ui_offset, 1, 1, cv::Scalar(255,255,255));
return true;
}
bool Node::load(cv::FileNodeIterator nd)
{
// TBD name, loc?
#if 0
// blanket loading of all svals
FileNode nd_in = (*nd)["inputs"];
if (nd_in.type() != FileNode::SEQ) {
//LOG(ERROR) << "no nodes";
//return false;
}
for (cv::FileNodeIterator it = nd_in.begin(); it != nd_in.end(); ++it) {
float val;
const string sval_name = (*it)["name"];
(*it)["value"] >> val;
setSignal( sval_name, val);
LOG(INFO) << name << " : " << sval_name << " = " << val;
/* this doesn't work, name returns blank
* though there are a matching number of entries to sval items
* */
/*
const string sval_name = (*it).name();
(*it)[sval_name] >> val;
setSignal( sval_name, val);
LOG(INFO) << name << " " << (*it) << " " << sval_name << " " << val;
*/
}
#endif
}
bool Node::save(cv::FileStorage& fs)
{
string type = getId(this);
fs << "typeid" << type;
//fs << "typeid_mangled" << typeid(*all_nodes[i]).name();
fs << "name" << name;
fs << "loc" << loc;
//fs << "vcol" << p->vcol ;
}
bool Node::handleKey(int key)
{
bool valid_key = true;
VLOG(2) << selected_type << " \"" << selected_port << "\"";
if ((selected_type == SIGNAL) && (selected_port != "")) {
float value = getSignal(selected_port);
if (key == '.') {
value *= 0.9;
}
else if (key == '/') {
value *= 1.1;
}
else if (key == ',') {
value += 1;
}
else if (key == 'm') {
value -= 1;
}
else if (key == 'n') {
value = 0;
}
else {
valid_key = false;
}
if (valid_key) {
VLOG(2) << value;
setSignal(selected_port, value);
setDirty();
}
return valid_key;
}
const float acc_step = 3.0;
// TBD alternatively could handle loc as a Signal
if (key == '8') { // UP
acc.y -= acc_step;
// LOG(INFO) << "acc.y " << acc.y;
} else if (key == '2') { // DOWN
acc.y += acc_step;
} else if (key == '4') { // LEFT
acc.x -= acc_step;
} else if (key == '6') { // RIGHT
acc.x += acc_step;
}
else {
valid_key = false;
}
return valid_key;
}
//////////////////////////////////////////////////////
int Node::getIndFromPointer(Connector* con)
{
if (con == NULL) return -1;
boost::mutex::scoped_lock l(port_mutex);
for (int i = 0; i < ports.size(); i++) {
if (con == ports[i]) {
VLOG(3) << con->name << " " << i;
return i;
}
}
return -1;
}
// TBD selectPortByPointer
bool Node::selectPortByInd(const int ind)
{
if (ind < 0) return false;
boost::mutex::scoped_lock l(port_mutex);
if (ind >= ports.size()) return false;
selected_port_ind = ind;
selected_port = ports[ind]->name;
selected_type = ports[ind]->type;
return true;
}
bool Node::getPrevPort(const conType type)
{
boost::mutex::scoped_lock l(port_mutex);
for (int i = ports.size()-1; i >= 0; i--) {
int ind = (i + selected_port_ind) % ports.size();
VLOG(3) << ind << " : " << type << " " << ports[ind]->type << ", "
<< ports[ind]->name;
if ((type == NONE) || (type == ports[ind]->type)) {
selected_port_ind = ind;
selected_port = ports[ind]->name;
selected_type = ports[ind]->type;
return true;
}
}
return false;
}
bool Node::getNextPort(const conType type)
{
boost::mutex::scoped_lock l(port_mutex);
for (int i = 0; i < ports.size(); i++) {
int ind = (i + 1 + selected_port_ind) % ports.size();
VLOG(3) << ind << " : " << type << " " << ports[ind]->type << ", "
<< ports[ind]->name;
if ((type == NONE) || (type == ports[ind]->type)) {
selected_port_ind = ind;
selected_port = ports[ind]->name;
selected_type = ports[ind]->type;
return true;
}
}
return false;
}
/// TBD
/*
get a pointer to the port connected to this nodes port
*/
bool Node::getInputPort(
const conType type,
const string port,
Connector*& con,
string& src_port)
{
con = NULL;
boost::mutex::scoped_lock l(port_mutex);
for (int i = 0; i < ports.size(); i++) {
VLOG(6) << i << " " << ports[i]->type << " " << ports[i]->name << ", " << type << " " << port;
if ((ports[i]->type == type) && (ports[i]->name == port)) {
con = ports[i];
if (ports[i]->src) {
src_port = ports[i]->src->name;
}
return true;
}
}
return false;
}
/*
Connect a source connector port to this port
if src_node is NULL then any existing connecting port is disconnected.
*/
void Node::setInputPort(
const conType type,
const std::string port,
Node* src_node,
const std::string src_port
)
{
// this will create the entries if they don't alread exists, without clobbering their values
// TBD only do this if requested?
Connector* con;
string existing_port;
bool con_exists = getInputPort(type, port, con, existing_port);
if (!con_exists) {
// TBD may not always want to create the connector if it doesn't exist
con = new Connector(port);
con->parent = this;
con->type = type;
boost::mutex::scoped_lock l(port_mutex);
con->loc = cv::Point2f(0, ports.size()*10);
ports.push_back(con);
VLOG(2) << "\"" << con->parent->name << "\"" <<CLTX2 << " new connector " << CLNRM
<< type << " \"" << con->name << "\"";
}
// blank out existing dst connection if it exists, it will be overwritten next
if (con->src) {
con->src->dst = NULL;
}
Connector* src_con = NULL;
if (src_node) {
bool src_con_exists = src_node->getInputPort(type, src_port, src_con, existing_port);
if (!src_con_exists) {
// this will produce connectors in the src parent in an order determined
// by the way the earlier connections use them as ports
src_con = new Connector(src_port);
src_con->parent = src_node;
src_con->type = type;
boost::mutex::scoped_lock l(src_node->port_mutex);
src_con->loc = cv::Point2f(0, src_node->ports.size()*10);
src_node->ports.push_back(src_con);
VLOG(2) << con->parent->name << " new src Connector " << type << " " << src_con->name;
}
//if (src_con->dst != con) con->setDirty();
//src_con->setDirty();
src_con->dst = con;
VLOG(1) << "\"" << name << "\" setInputPort: " << type << " "
<< CLTXT << "\"" << port << "\"" << CLNRM << " from "
<< CLTXT /*<< inputs[type][port].first->name << " "*/ // not necessarily non-NULL
<< "\"" << src_port << "\"" << CLNRM;
} else {
VLOG(1) << "\"" << name << "\" setInputPort: " << type << " " << port;
}
con->src = src_con;
}
bool Node::setImage(const std::string port, cv::Mat& im, const bool internally_set)
{
// can't set the image if it is controlled by an input node
//if (port.substr(0,3) == "out") {
// type = "ImageOut";
//}
Connector* con = NULL;
string src_port;
if (!getInputPort(IMAGE, port, con, src_port)) {
// create it since it doesn't exist
setInputPort(IMAGE, port, NULL, "");
// now get it again TBD actually check rv
if (!getInputPort(IMAGE, port, con, src_port)) {
LOG(ERROR) << "still can't get port";
return false;
}
con->internally_set = internally_set;
}
if (!con) {
LOG(ERROR) << "no connector";
return false;
}
// can't set signal if it is controlled by src port
if (con->src) return false;
con->setImage(im);
return true;
}
bool Node::setSignal(
const std::string port,
const float val,
const bool internally_set,
const int saturate,
const float min,
const float max
)
{
Connector* con = NULL;
string src_port;
if (!getInputPort(SIGNAL, port, con, src_port)) {
// create it if it doesn't exist
setInputPort(SIGNAL, port);
if (!getInputPort(SIGNAL, port, con, src_port)) {
LOG(ERROR) << name << " still can't get connector " << CLTXT << port << CLNRM;
return false;
}
// only use this setting if the port is new (TBD)
con->internally_set = internally_set;
}
// can't set signal if it is controlled by src port
if (con->src) return false;
if (saturate > 0) {
VLOG(1) << name << " - " << port << ((saturate == 1) ? " - saturating " : " - rolling over ")
<< min << " " << max;
con->saturate = saturate;
con->val_min = min;
con->val_max = max;
}
con->setSignal(val);
return true;
}
// get an imagenode image from this nodes imagenode inputs
cv::Mat Node::getImage(
const string port,
bool& valid,
bool& is_dirty,
const int is_dirty_ind
)
{
/*
string type = "ImageNode";
if (port.substr(0,3) == "out") {
type = "ImageOut";
}
*/
valid = false;
cv::Mat im;
Connector* con = NULL;
string src_port = "";
if (!getInputPort(IMAGE, port, con, src_port)) {
// create it if it doesn't exist