-
Notifications
You must be signed in to change notification settings - Fork 7
/
NL_projection_and_synapse.cpp
2470 lines (2120 loc) · 97.3 KB
/
NL_projection_and_synapse.cpp
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
/***************************************************************************
** **
** This file is part of SpineCreator, an easy to use GUI for **
** describing spiking neural network models. **
** Copyright (C) 2013-2014 Alex Cope, Paul Richmond, Seb James **
** **
** This program 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. **
** **
** This program 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 this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Authors: Alex Cope, Seb James **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include "NL_projection_and_synapse.h"
#include "NL_genericinput.h"
#include "NL_connection.h"
#include "EL_experiment.h"
#include "SC_projectobject.h"
#include <sstream>
#include <iomanip>
#include "globalHeader.h"
synapse::synapse(QSharedPointer <projection> proj, projectObject * data, bool dontAddInputs)
{
this->postSynapseCmpt = QSharedPointer<ComponentInstance>(new ComponentInstance(data->catalogPS[0]));
this->postSynapseCmpt->owner = proj;
this->weightUpdateCmpt = QSharedPointer<ComponentInstance>(new ComponentInstance(data->catalogWU[0]));
this->weightUpdateCmpt->owner = proj;
this->connectionType = new alltoAll_connection;
if (!dontAddInputs) {
// add the inputs:
// source -> synapse
this->weightUpdateCmpt->addInput(proj->source->neuronType, true);
// synapse -> PSP
this->postSynapseCmpt->addInput(this->weightUpdateCmpt, true);
// PSP -> destination
if (proj->destination != NULL) {
proj->destination->neuronType->addInput(this->postSynapseCmpt, true);
}
}
//attach to the projection (shared pointers mean this must be done elsewhere)
//proj->synapses.push_back(QSharedPointer<synapse>(this));
this->proj = proj;
this->type = synapseObject;
this->isVisualised = false;
this->connectionTypeStr = "";
}
synapse::synapse(QSharedPointer <projection> proj, nl_rootdata * data, bool dontAddInputs)
{
this->postSynapseCmpt = QSharedPointer<ComponentInstance>(new ComponentInstance(data->catalogPS[0]));
this->postSynapseCmpt->owner = proj;
this->weightUpdateCmpt = QSharedPointer<ComponentInstance>(new ComponentInstance(data->catalogWU[0]));
this->weightUpdateCmpt->owner = proj;
this->connectionType = new alltoAll_connection;
if (!dontAddInputs) {
// add the inputs:
// source -> synapse
this->weightUpdateCmpt->addInput(proj->source->neuronType, true);
// synapse -> PSP
this->postSynapseCmpt->addInput(this->weightUpdateCmpt, true);
// PSP -> destination
if (proj->destination != NULL) {
proj->destination->neuronType->addInput(this->postSynapseCmpt, true);
}
}
this->proj = proj;
this->type = synapseObject;
this->isVisualised = false;
this->connectionTypeStr = "";
}
synapse::~synapse()
{
// Note: postsynapseType and weightUpdateType are QSharedPointers
// do don't need to be deleted.
this->postSynapseCmpt.clear();
this->weightUpdateCmpt.clear();
delete this->connectionType;
}
void synapse::delAll(nl_rootdata *)
{
// remove components (they will clean up their inputs themselves)
this->postSynapseCmpt->removeReferences();
this->weightUpdateCmpt->removeReferences();
this->postSynapseCmpt.clear();
this->weightUpdateCmpt.clear();
this->connectionTypeStr = "";
delete this->connectionType;
}
QString synapse::getName()
{
int index = -1;
if ((index = this->getIndex()) == -1) {
DBG() << "Can't find synapse! In synapse::getName()";
return "Err";
}
return this->proj->getName() + ": Synapse " + QString::number(index);
}
int synapse::getIndex (void)
{
int index = -1;
for (int i = 0; i < this->proj->synapses.size(); ++i) {
if (this->proj->synapses[i].data() == this) {
index = i;
}
}
return index;
}
QString synapse::getWeightUpdateName (void)
{
int index = -1;
if ((index = this->getIndex()) == -1) {
DBG() << "Can't find synapse! In synapse::getName()";
return "Err";
}
return this->proj->getName() + " Synapse " + QString::number(index) + " weight_update";
}
QString synapse::getPostSynapseName()
{
int index = -1;
if ((index = this->getIndex()) == -1) {
DBG() << "Can't find synapse! In synapse::getName()";
return "Err";
}
return this->proj->getName() + " Synapse " + QString::number(index) + " postsynapse";
}
int synapse::getSynapseIndex()
{
int index = -1;
for (int i = 0; i < this->proj->synapses.size(); ++i) {
if (this->proj->synapses[i].data() == this) {
index = i;
}
}
return index;
}
QSharedPointer < systemObject > synapse::newFromExisting(QMap<systemObject *, QSharedPointer<systemObject> > &objectMap)
{
// create a new, identical, synapse
QSharedPointer <synapse> newSyn = QSharedPointer <synapse>(new synapse());
newSyn->weightUpdateCmpt = QSharedPointer<ComponentInstance>(new ComponentInstance(this->weightUpdateCmpt, true/*copy inputs / outputs*/));
newSyn->postSynapseCmpt = QSharedPointer<ComponentInstance>(new ComponentInstance(this->postSynapseCmpt, true/*copy inputs / outputs*/));
newSyn->connectionType = this->connectionType->newFromExisting();
newSyn->connectionType->setParent(newSyn);
newSyn->isVisualised = this->isVisualised;
objectMap.insert(this, newSyn);
// now we must create copies of all the projInput GenericInputs:
// we only do inputs for weightupdates, but inputs AND outputs for
// postsynapses. This is because the input to the postsynapse
// is the same as the output from the weightupdate,
// and they'll be remapped when we sort out the pointers in the
// second copy step...
for (int i = 0; i < this->weightUpdateCmpt->inputs.size(); ++i) {
if (this->weightUpdateCmpt->inputs[i]->projInput) {
// create a new copy
QSharedPointer <genericInput> in = qSharedPointerDynamicCast <genericInput> (this->weightUpdateCmpt->inputs[i]->newFromExisting(objectMap));
// add it to the pointer map!
objectMap.insert(this->weightUpdateCmpt->inputs[i].data(),in);
}
}
for (int i = 0; i < this->postSynapseCmpt->inputs.size(); ++i) {
if (this->postSynapseCmpt->inputs[i]->projInput) {
// create a new copy
QSharedPointer <genericInput> in = qSharedPointerDynamicCast <genericInput> (this->postSynapseCmpt->inputs[i]->newFromExisting(objectMap));
// add it to the pointer map!
objectMap.insert(this->postSynapseCmpt->inputs[i].data(),in);
}
}
for (int i = 0; i < this->postSynapseCmpt->outputs.size(); ++i) {
if (this->postSynapseCmpt->outputs[i]->projInput) {
// create a new copy
QSharedPointer <genericInput> in = qSharedPointerDynamicCast <genericInput> (this->postSynapseCmpt->outputs[i]->newFromExisting(objectMap));
// add it to the pointer map!
objectMap.insert(this->postSynapseCmpt->outputs[i].data(),in);
}
}
return qSharedPointerCast <systemObject> (newSyn);
}
void synapse::remapSharedPointers(QMap <systemObject *, QSharedPointer <systemObject> > objectMap)
{
this->weightUpdateCmpt->remapPointers(objectMap);
this->postSynapseCmpt->remapPointers(objectMap);
// we must also manually call remap on the projInputs:
for (int i = 0; i < this->weightUpdateCmpt->inputs.size(); ++i) {
if (this->weightUpdateCmpt->inputs[i]->projInput) {
this->weightUpdateCmpt->inputs[i]->remapSharedPointers(objectMap);
}
}
for (int i = 0; i < this->postSynapseCmpt->inputs.size(); ++i) {
if (this->postSynapseCmpt->inputs[i]->projInput) {
this->postSynapseCmpt->inputs[i]->remapSharedPointers(objectMap);
}
}
for (int i = 0; i < this->postSynapseCmpt->outputs.size(); ++i) {
if (this->postSynapseCmpt->outputs[i]->projInput) {
this->postSynapseCmpt->outputs[i]->remapSharedPointers(objectMap);
}
}
// connection, if it has a generator
if (this->connectionType->type == CSV) {
csv_connection * c = dynamic_cast < csv_connection * > (this->connectionType);
if (c && c->generator != NULL) {
pythonscript_connection * g = dynamic_cast < pythonscript_connection * > (c->generator);
if (g) {
g->srcPop = qSharedPointerDynamicCast <population> (objectMap[g->srcPop.data()]);
g->dstPop = qSharedPointerDynamicCast <population> (objectMap[g->dstPop.data()]);
if (!g->srcPop || !g->dstPop) {
DBG() << "Error casting objectMap lookup to population in synapse::remapSharedPointers";
exit(-1);
}
}
}
}
}
void
synapse::passDownSrcAndDst (void)
{
if (this->connectionType != NULL && !this->proj.isNull()) {
this->connectionType->srcPop = this->proj->source;
this->connectionType->dstPop = this->proj->destination;
if (!this->proj->source.isNull()) {
this->connectionType->setSrcName (this->proj->source->name);
}
if (!this->proj->destination.isNull()) {
this->connectionType->setDstName (this->proj->destination->name);
}
}
}
projection::projection()
{
this->type = projectionObject;
this->destination.clear();
this->source.clear();
this->currTarg = 0;
this->start = QPointF(0,0);
this->tempTrans.GLscale = 100;
this->tempTrans.height = 1;
this->tempTrans.width = 1;
this->tempTrans.viewX = 0;
this->tempTrans.viewY = 0;
this->selectedControlPoint.ind = -1;
this->selectedControlPoint.start = false;
this->selectedControlPoint.type = C1;
this->projDrawStyle = standardDrawStyleExcitatory;
this->showLabel = false;
}
projection::~projection()
{
}
void projection::connect(QSharedPointer<projection> in)
{
// connect might be called multiple times due to the nature of Undo
for (int i = 0; i < destination->reverseProjections.size(); ++i) {
if (destination->reverseProjections[i].data() == in.data()) {
// already there - give up
return;
}
}
for (int i = 0; i < source->projections.size(); ++i) {
if (source->projections[i].data() == in.data()) {
// already there - give up
return;
}
}
destination->reverseProjections.push_back(in);
source->projections.push_back(in);
}
void projection::disconnect()
{
if (destination != NULL) {
// remove projection
for (int i = 0; i < destination->reverseProjections.size(); ++i) {
if (destination->reverseProjections[i].data() == this) {
destination->reverseProjections.erase(destination->reverseProjections.begin()+i);
dstPos = i;
}
}
}
if (source != NULL) {
for (int i = 0; i < source->projections.size(); ++i) {
if (source->projections[i].data() == this) {
source->projections.erase(source->projections.begin()+i);
srcPos = i;
}
}
}
}
void projection::remove(nl_rootdata * data)
{
// remove from experiment
for (int j = 0; j < data->experiments.size(); ++j) {
// data->experiments[j]->purgeBadPointer(this);
}
}
void projection::delAll(nl_rootdata *)
{
// remove other references so we don't get deleted twice!
this->disconnect();
}
void projection::delAll(projectObject *)
{
// remove other references so we don't get deleted twice!
this->disconnect();
}
QPointF projection::currentLocation()
{
if (curves.size() > 0) {
return this->curves.back().end;
}
return start;
}
QPointF projection::selectedControlPointLocation()
{
QPointF rtn(0,0);
if (this->selectedControlPoint.start == true || this->selectedControlPoint.ind == -1) {
rtn = this->start;
} else {
if (this->selectedControlPoint.type == C1) {
rtn = this->curves[this->selectedControlPoint.ind].C1;
} else if (this->selectedControlPoint.type == C2) {
rtn = this->curves[this->selectedControlPoint.ind].C2;
} else if (this->selectedControlPoint.type == p_end) {
rtn = this->curves[this->selectedControlPoint.ind].end;
} else {
// error.
}
}
return rtn;
}
void projection::move(float x, float y)
{
if (curves.size() > 1) {
// move mid points:
this->curves[0].C2 = (this->curves[0].C2 - this->start) + QPointF(x,y) + locationOffset;
this->curves[0].end = (this->curves[0].end - this->start) + QPointF(x,y) + locationOffset;
for (int i = 1; i < this->curves.size() -1; ++i) {
this->curves[i].C1 = (this->curves[i].C1 - this->start) + QPointF(x,y) + locationOffset;
this->curves[i].C2 = (this->curves[i].C2 - this->start) + QPointF(x,y) + locationOffset;
this->curves[i].end = (this->curves[i].end - this->start) + QPointF(x,y) + locationOffset;
}
this->curves.back().C1 = (this->curves.back().C1 - this->start) + QPointF(x,y) + locationOffset;
}
}
void projection::animate(QSharedPointer<systemObject>movingObj, QPointF delta, QSharedPointer<projection>thisSharedPointer)
{
QSharedPointer <population> movingPop;
if (movingObj->type == populationObject) {
movingPop = qSharedPointerDynamicCast <population>(movingObj);
} else {
DBG() << "Incorrect object fed to projection animation";
return;
}
// if we are a self connection we get moved twice, so only move half as much each time
if (!(this->destination.isNull())) {
if (this->source->name == this->destination->name) {
delta = delta / 2;
}
}
// crash avoidance
if (this->curves.size() == 0) {
DBG() << "Projection created with no curves or bad access";
return;
}
// source is moving
if (movingPop->name == this->source->name) {
this->start = this->start + delta;
this->curves.front().C1 = this->curves.front().C1 + delta;
}
// if destination is set:
if (!(this->destination.isNull())) {
// destination is moving
if (movingPop->name == this->destination->name) {
this->curves.back().end = this->curves.back().end + delta;
this->curves.back().C2 = this->curves.back().C2 + delta;
// update inputs:
for (int i = 0; i < this->synapses.size(); ++i) {
for (int j = 0; j < this->synapses[i]->weightUpdateCmpt->inputs.size(); ++j) {
this->synapses[i]->weightUpdateCmpt->inputs[j]->animate(thisSharedPointer, delta);
}
for (int j = 0; j < this->synapses[i]->postSynapseCmpt->inputs.size(); ++j) {
this->synapses[i]->postSynapseCmpt->inputs[j]->animate(thisSharedPointer, delta);
}
}
}
}
}
void projection::setStyle(drawStyle style)
{
this->projDrawStyle = style;
}
drawStyle projection::style()
{
return this->projDrawStyle;
}
//@}
/*!
* Width Factors for the projection lines.
*/
//@{
#define WIDTHFACTOR_MULTIPLESYNAPSES 1.5f
#define WIDTHFACTOR_MULTIPLE 1.5f
#define WIDTHFACTOR_PYTHONCONN 1.5f
#define WIDTHFACTOR_ALLTOALL 1.8f
#define WIDTHFACTOR_ONETOONE 1.0f
#define WIDTHFACTOR_FIXEDPROB 1.5f
#define WIDTHFACTOR_CSV 1.5f
#define WIDTHFACTOR_KERNEL 1.5f
#define WIDTHFACTOR_OTHER 1.0f
//@}
void projection::draw(QPainter *painter, float GLscale,
float viewX, float viewY, int width, int height, QImage, drawStyle style)
{
// GLscale = 200 * scale from the UI
float scale = GLscale/200.0;
// Enforce a lower limit to scale, to ensure we don't try to draw
// lines too small for the UI to draw them.
if (scale < 0.4f) {
scale = 0.4f;
}
// setup for drawing curves
this->setupTrans(GLscale, viewX, viewY, width, height);
bool saveNetworkImage = false;
// switch if we have standardDrawStyle or saveNetworkImageDrawStyle
if (style == saveNetworkImageDrawStyle) {
// This draw is for a "Save Image" request, rather than an on-screen draw.
saveNetworkImage = true;
style = this->projDrawStyle;
} else if (style == standardDrawStyle) {
// standardDrawStyle for inhibitory,
// standardDrawStyleExcitatory for excitatory projections.
style = this->projDrawStyle;
}
if (this->curves.size() > 0) {
// Colour definitions and line width factor for this
// projection, dependent upon the connection type.
QColor colour = QCOL_BASICBLUE;
float connTypeWidthFactor = 1.0;
if (this->multipleConnTypes()) {
colour = QCOL_PURPLE1;
connTypeWidthFactor = WIDTHFACTOR_MULTIPLE;
} else {
// Set colour based on first synapse connection type.
colour = QCOL_BASICBLUE;
QString ctype("");
if (!this->synapses.isEmpty() && !this->synapses[0]->connectionTypeStr.isEmpty()) {
if (this->synapses[0]->connectionType->hasGenerator()) {
csv_connection* cn = (csv_connection*)this->synapses[0]->connectionType;
ctype += cn->generator->scriptText;
} else {
// Make colour vary based on md5sum of the text in ctype:
ctype += this->synapses[0]->connectionTypeStr;
}
QString result(QCryptographicHash::hash(ctype.toStdString().c_str(),
QCryptographicHash::Md5).toHex());
QByteArray r2(result.toStdString().c_str(),2);
bool ok = false;
// Vary the hue in the colour
colour.setHsl(r2.toInt(&ok, 16),0xff,0x40);
connTypeWidthFactor = WIDTHFACTOR_PYTHONCONN;
} else if (!this->synapses.isEmpty()) {
// No connectionTypeStr, so use type
switch (this->synapses[0]->connectionType->type) {
case AlltoAll:
colour = QCOL_BLUE1;
connTypeWidthFactor = WIDTHFACTOR_ALLTOALL;
break;
case OnetoOne:
colour = QCOL_RED1;
connTypeWidthFactor = WIDTHFACTOR_ONETOONE;
break;
case FixedProb:
colour = QCOL_GREEN1;
connTypeWidthFactor = WIDTHFACTOR_FIXEDPROB;
break;
case CSV:
// if it has a Script Annotation, then need to colour it later based on this information:
if (this->synapses[0]->connectionType->hasGenerator()) {
// Make colour vary based on md5sum of the text in ctype:
csv_connection* cn = (csv_connection*)this->synapses[0]->connectionType;
ctype += cn->generator->scriptText;
QString result(QCryptographicHash::hash(ctype.toStdString().c_str(),
QCryptographicHash::Md5).toHex());
QByteArray r2(result.toStdString().c_str(),2);
bool ok = false;
// Vary the hue in the colour
colour.setHsl(r2.toInt(&ok, 16),0xff,0x40);
connTypeWidthFactor = WIDTHFACTOR_PYTHONCONN;
} else {
colour = QCOL_GREEN3;
connTypeWidthFactor = WIDTHFACTOR_CSV;
}
break;
case Python:
case CSA:
default:
colour = QCOL_BLACK;
connTypeWidthFactor = WIDTHFACTOR_OTHER;
break;
}
} else {
// No Synapses?
}
}
// In some cases, the colour for the projection is passed
// in. In most cases we want to choose the colour here. This
// is a bit hacky, but when we get passed in blue, we reckon
// that we can override the colour scheme, but otherwise, we
// have to set the linePen colour to the passed in pen colour.
QPen oldPen = painter->pen();
if (saveNetworkImage == true || oldPen.color() == QCOL_BASICBLUE) {
//DBG() << "We can override colours";
} else {
//DBG() << "We've been passed in a specified colour, set linePen to this colour";
colour = oldPen.color();
}
QColor ptrColour = QCOL_GREY1;
QColor labelColour = colour;
QPointF start;
QPointF end;
switch (style) {
case microcircuitDrawStyle:
case spikeSourceDrawStyle:
{
if (source != NULL) {
QLineF temp = QLineF(QPointF(source->x, source->y), this->curves.front().C1);
temp.setLength(0.501);
start = temp.p2();
} else {
start = this->start;
}
if (destination != NULL) {
QLineF temp = QLineF(QPointF(destination->x, destination->y), this->curves.back().C2);
temp.setLength(0.501);
end = temp.p2();
} else {
end = this->curves.back().end;
}
// set pen width
QPen pen2 = painter->pen();
pen2.setWidthF((pen2.widthF()+1.0)*2*scale);
pen2.setColor(colour);
painter->setPen(pen2);
QPainterPath path;
path.moveTo(this->transformPoint(start));
for (int i = 0; i < this->curves.size(); ++i) {
if (this->curves.size()-1 == i) {
path.cubicTo(this->transformPoint(this->curves[i].C1),
this->transformPoint(this->curves[i].C2),
this->transformPoint(end));
} else {
path.cubicTo(this->transformPoint(this->curves[i].C1),
this->transformPoint(this->curves[i].C2),
this->transformPoint(this->curves[i].end));
}
}
// draw start and end markers
QPainterPath endPoint;
endPoint.addPolygon(this->makeArrowHead(path, GLscale));
painter->fillPath(endPoint, colour);
// Show number of synapses with dashes
{
QPen pen = painter->pen();
QVector<qreal> dash;
dash.push_back(4);
for (int syn = 1; syn < this->synapses.size(); ++syn) {
dash.push_back(2.0);
dash.push_back(1.0);
}
if (synapses.size() > 1) {
dash.push_back(2.0);
dash.push_back(1.0);
dash.push_back(2.0);
pen.setWidthF((pen.widthF()+1.0) * 1.5);
} else {
dash.push_back(0.0);
}
dash.push_back(100000.0);
pen.setDashPattern(dash);
painter->setPen(pen);
}
// DRAW
painter->drawPath(path);
painter->setPen(oldPen);
break;
}
case layersDrawStyle:
{
return;
}
case standardDrawStyle: // Used to draw inhibitory projections.
case standardDrawStyleExcitatory:
default:
{
start = this->start;
end = this->curves.back().end;
QSettings settings;
float dpi_ratio = settings.value("dpi", 1.0).toFloat();
if (saveNetworkImage) {
// Ensure image output isn't affected by dpi_ratio:
dpi_ratio = 1;
}
// account for hidpi in line width
QPen linePen = painter->pen();
linePen.setCapStyle(Qt::FlatCap); // Would like Qt::RoundCap, but not in the dashes.
// This sets the "base width" for the lines. We'll then
// modify that base width based on the connection type.
linePen.setWidthF(2*connTypeWidthFactor*scale*linePen.widthF()*dpi_ratio);
// Another pen for the pointer line
QPen pointerLinePen(ptrColour, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
pointerLinePen.setWidthF(scale*dpi_ratio);
QPen labelPen(labelColour, 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
//labelPen.setWidthF(2*scale*linePen.widthF()*dpi_ratio);
labelPen.setWidthF(linePen.widthF());
if (saveNetworkImage) {
// Wider lines for image output
linePen.setWidthF(linePen.widthF()*2);
}
linePen.setColor(colour);
painter->setPen(linePen);
QPainterPath path;
// start curve drawing
path.moveTo(this->transformPoint(start));
// draw curves
for (int i = 0; i < this->curves.size(); ++i) {
if (this->curves.size()-1 == i) {
path.cubicTo(this->transformPoint(this->curves[i].C1),
this->transformPoint(this->curves[i].C2),
this->transformPoint(end));
} else {
path.cubicTo(this->transformPoint(this->curves[i].C1),
this->transformPoint(this->curves[i].C2),
this->transformPoint(this->curves[i].end));
}
}
// only draw number of synapses for Projections
if (this->type == projectionObject) {
QPen pen = painter->pen();
QVector<qreal> dash;
dash.push_back(5);
for (int syn = 1; syn < this->synapses.size(); ++syn) {
dash.push_back(1.0);
dash.push_back(1.5);
}
if (synapses.size() > 1) {
dash.push_back(1.0);
dash.push_back(1.5);
dash.push_back(1.0);
pen.setWidthF(pen.widthF() * WIDTHFACTOR_MULTIPLESYNAPSES);
} else {
dash.push_back(0.0);
}
dash.push_back(100000.0);
dash.push_back(0.0);
pen.setDashPattern(dash);
painter->setPen(pen);
}
// Draw the line before the end marker.
painter->drawPath(path);
QPainterPath endPoint;
if (style == standardDrawStyle) {
// Connections marked by user as "inhibitory" get a little circle.
endPoint.addEllipse(this->transformPoint(this->curves.back().end),
0.025*dpi_ratio*GLscale,0.025*dpi_ratio*GLscale);
painter->drawPath(endPoint);
painter->fillPath(endPoint, colour);
} else if (style == standardDrawStyleExcitatory) {
endPoint.addPolygon(this->makeArrowHead(path, GLscale));
painter->fillPath(endPoint, colour);
}
if (this->showLabel) {
this->drawLabel(painter, linePen, pointerLinePen, labelPen, GLscale, scale);
}
painter->setPen(oldPen);
break;
}
} // switch
}
}
QPolygonF
projection::makeArrowHead (QPainterPath& path, const float GLscale)
{
QPolygonF arrow_head;
//calculate arrow head polygon
QPointF end_point = path.pointAtPercent(1.0);
QPointF temp_end_point = path.pointAtPercent(0.995);
QLineF line = QLineF(end_point, temp_end_point).unitVector();
QLineF line2 = QLineF(line.p2(), line.p1());
line2.setLength(line2.length()+0.05*GLscale/2.0);
end_point = line2.p2();
line.setLength(0.1*GLscale);
QPointF t = line.p2() - line.p1();
QLineF normal = line.normalVector();
normal.setLength(normal.length()*0.8);
QPointF a1 = normal.p2() + t;
normal.setLength(-normal.length());
QPointF a2 = normal.p2() + t;
arrow_head.clear();
arrow_head << end_point << a1 << a2 << end_point;
return arrow_head;
}
bool
projection::multipleConnTypes(void)
{
QString currentCtype("");
bool manyConnTypes = false;
// Quick return if there's only one synapse:
if (this->synapses.size() == 1) {
return manyConnTypes;
}
for (int i = 0; i < this->synapses.size(); ++i) {
QString ctype("");
if (this->synapses[i]->connectionTypeStr.isEmpty()) {
ctype = this->synapses[i]->connectionType->getTypeStr();
} else {
ctype = this->synapses[i]->connectionTypeStr;
}
if (!currentCtype.isEmpty() && ctype != currentCtype) {
// At least two connection types within this one projection
manyConnTypes = true;
break;
} else {
currentCtype = ctype;
}
}
return manyConnTypes;
}
void
projection::drawLabel (QPainter* painter, QPen& linePen, QPen& pointerLinePen, QPen& labelPen,
const float GLscale, const float scale)
{
// Are all synapse connection types the same? If so we
// don't need to list them all.
bool manyConnTypes = this->multipleConnTypes();
// Now draw the synapse labels
for (int i = 0; i < this->synapses.size(); ++i) {
QString ctype("");
if (manyConnTypes) {
ctype += "Syn" + QString::number(i) + QString(": ");
} else {
// If we already did the first connection, break; all synapses have same connectivity.
if (i > 0) { break; }
if (this->synapses.size() == 1) {
// Add nothing to label
} else {
ctype += QString::number(this->synapses.size()) + " synapses: ";
}
}
if (this->synapses[i]->connectionTypeStr.isEmpty()) {
ctype += this->synapses[i]->connectionType->getTypeStr();
} else {
ctype += this->synapses[i]->connectionTypeStr;
}
// Set a suitable font for the projection labels
QFont oldFont = painter->font();
QFont font = painter->font();
font.setPointSizeF(1.6*GLscale/20.0);
painter->setFont(font);
// Call getLabelPos for the position of the label and
// its "pointer line". Note I'm passing the *unscaled*
// font to this.
QPointF startLinePos(0,0);
QPointF labelPos = this->transformPoint(this->getLabelPos (font, i, ctype, scale, startLinePos));
startLinePos = this->transformPoint(startLinePos);
// Find a point for the end of the pointer line:
QPointF endLinePos = this->transformPoint(this->getBezierPos (this->curves.size()-1, 0.95f));
// Text first in same colour as projection line
painter->setPen(labelPen);
painter->drawText(labelPos, ctype);
if (i == 0) { // only one pointer line per projection
painter->setPen(pointerLinePen);
QPainterPath pointerLine;
pointerLine.moveTo(startLinePos);
pointerLine.lineTo(endLinePos);
painter->drawPath(pointerLine);
}
painter->setFont(oldFont);
painter->setPen(linePen);
}
}
QPointF
projection::getBezierPos (int curveIndex, float t)
{
QPointF startPoint = this->start;
if (curveIndex > 0) {
startPoint = this->curves[curveIndex-1].end;
}
if (t < 0.0 || t > 1.0) {
DBG() << "Warning, Bezier curve defined between 0 and 1";
}
// Cubic Bezier formula
QPointF B = powf(1.0-t,3)*startPoint
+ 3*powf(1.0-t,2)*t*this->curves[curveIndex].C1
+ 3*(1.0-t)*powf(t,2)*this->curves[curveIndex].C2
+ powf(t,3)*this->curves[curveIndex].end;
return B;
}
QPointF
projection::getLabelPos (QFont& f, int syn, const QString& text, const float scale,
QPointF& startLinePos)
{
QPointF curveMiddle(0,0);
for (int i = 0; i < this->curves.size(); ++i) {
// Get the vector average of the bezier curve control points
// and vector sum them:
curveMiddle += (this->curves[i].C1 + this->curves[i].C2)/2.0;
}
// Finish up the vector average of the control point means for
// each curve section:
curveMiddle /= this->curves.size();
QPointF projEnd = this->curves.back().end;
QPointF centre = (projEnd + this->start)/2.0;
//DBG() << "start: " << this->start << ", end: " << projEnd;
//DBG() << "centre: " << centre << ", curveMiddle: " << curveMiddle;
// Info about the size of the text in the label
QFontMetrics qf(f);
float factor = 0.01f;
float stringwidth = (float)qf.width (text)*factor/scale;
float xheight = (float)qf.xHeight()*factor/scale;
float maxWidth = (float)qf.maxWidth()*factor/scale;
QPointF labelPos = curveMiddle;
// Find out if the line goes up or down on the diagram - this will
// affect the startLinePos.
bool uptrending = false;
if (start.y() < projEnd.y()) {
uptrending = true;
}
// May need left trending and right trending also?
// Site the label near the end of the last curve in the
// projection, rather than the "curveMiddle". Find a reference
// point on the last curve to do this:
QPointF labelRef = this->getBezierPos (this->curves.size()-1, 0.85f);
// Return info: Vertical and Left or Right OR Horizontal and Up or down.
QPointF diff = projEnd - this->start;
float diffVert = fabs(diff.y());
float diffHorz = fabs(diff.x());
if (diffVert*3 < diffHorz) {
// Say it's horizontal. Figure out up/downness. Does that
// matter? Need some offset so the label doesn't sit on the
// line, and up/downness for direction of that offset (which
// is size give by the current font).
if (curveMiddle.y() < centre.y()) {
// DBG() << "Down curvy";
labelPos.setY(curveMiddle.y() - 2*xheight - (syn*1.8*xheight));
startLinePos.setY(labelPos.y() + xheight*1.6);
} else {
// DBG() << "Up curvy";
labelPos.setY(curveMiddle.y() + 2*xheight + (syn*1.8*xheight));
startLinePos.setY(labelPos.y() - xheight/4.0);
}
// Always shift text left if it's a horizontal projection:
labelPos.setX(curveMiddle.x() - stringwidth/2.0);
startLinePos.setX(labelPos.x() + stringwidth/4.0);