forked from rivetTDA/rivet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizationwindow.cpp
1176 lines (892 loc) · 38.9 KB
/
visualizationwindow.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
/**********************************************************************
Copyright 2014-2016 The RIVET Developers. See the COPYRIGHT file at
the top-level directory of this distribution.
This file is part of RIVET.
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/>.
**********************************************************************/
#include "visualizationwindow.h"
#include "ui_visualizationwindow.h"
#include "dcel/arrangement_message.h"
#include "dcel/barcode.h"
#include "dcel/barcode_template.h"
#include "interface/config_parameters.h"
#include "interface/file_writer.h"
#include "numerics.h"
#include <QDateTime>
#include <QDebug>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QTime>
#include <algorithm>
#include <fstream>
#include <sstream>
const QString VisualizationWindow::DEFAULT_SAVE_DIR_KEY("default_save_dir");
VisualizationWindow::VisualizationWindow(InputParameters& params)
: QMainWindow()
, ui(new Ui::VisualizationWindow)
, verbosity(params.verbosity)
, data_selected(false)
, unsaved_data(false)
, input_params(params)
, config_params()
, ds_dialog(input_params, this)
, grades()
, angle_precise(0)
, offset_precise(0)
, x_reverse(false)
, y_reverse(false)
, template_points()
, degenerate_x(false)
, degenerate_y(false)
, line_selection_ready(false)
, slice_diagram(&config_params, grades.x, grades.y, this)
, slice_update_lock(false)
, p_diagram(&config_params, this)
, persistence_diagram_drawn(false)
, slice_diagram_initialized(false)
{
// create computation thread
cthread = new ComputationThread(input_params);
ui->setupUi(this);
//set up the slice diagram
ui->sliceView->setScene(&slice_diagram);
// ui->sliceView->setDragMode(QGraphicsView::ScrollHandDrag);
ui->sliceView->scale(1, -1);
ui->sliceView->setRenderHint(QPainter::Antialiasing);
ui->sliceView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
//set up the persistence diagram scene
ui->pdView->setScene(&p_diagram);
ui->pdView->scale(1, -1);
ui->pdView->setRenderHint(QPainter::Antialiasing);
//connect signal from DataSelectDialog to start the computation
QObject::connect(&ds_dialog, &DataSelectDialog::dataSelected, this, &VisualizationWindow::init);
//connect signals from ComputationThread to slots in VisualizationWindow
QObject::connect(cthread, &ComputationThread::templatePointsReady, this, &VisualizationWindow::paint_template_points);
QObject::connect(cthread, &ComputationThread::arrangementReady, this, &VisualizationWindow::augmented_arrangement_ready);
//connect signals and slots for the diagrams
QObject::connect(&slice_diagram, &SliceDiagram::set_line_control_elements, this, &VisualizationWindow::set_line_parameters);
QObject::connect(&slice_diagram, &SliceDiagram::persistence_bar_selected, &p_diagram, &PersistenceDiagram::receive_dot_selection);
QObject::connect(&slice_diagram, &SliceDiagram::persistence_bar_deselected, &p_diagram, &PersistenceDiagram::receive_dot_deselection);
QObject::connect(&p_diagram, &PersistenceDiagram::persistence_dot_selected, &slice_diagram, &SliceDiagram::receive_bar_selection);
QObject::connect(&p_diagram, &PersistenceDiagram::persistence_dot_secondary_selection, &slice_diagram, &SliceDiagram::receive_bar_secondary_selection);
QObject::connect(&p_diagram, &PersistenceDiagram::persistence_dot_deselected, &slice_diagram, &SliceDiagram::receive_bar_deselection);
}
VisualizationWindow::~VisualizationWindow()
{
delete ui;
}
void VisualizationWindow::redraw()
{
// redraw slice diagram and persistence diagram
// if config parameters are changed
if (line_selection_ready) {
slice_diagram.receive_parameter_change();
if (persistence_diagram_drawn)
p_diagram.receive_parameter_change();
}
// disconnect and delete configure dialog
QObject::disconnect(configBox, &ConfigureDialog::window_closed, this, &VisualizationWindow::redraw);
delete configBox;
}
// slot that resets data structures, variables and ui
void VisualizationWindow::init()
{
// disable visualization window parameters
// when computation is going on
ui->BettiLabel->setEnabled(false);
ui->xi0CheckBox->setEnabled(false);
ui->xi1CheckBox->setEnabled(false);
ui->xi2CheckBox->setEnabled(false);
ui->xi0CheckBox->setChecked(true);
ui->xi1CheckBox->setChecked(true);
ui->xi2CheckBox->setChecked(false);
ui->normCoordCheckBox->setEnabled(false);
ui->angleLabel->setEnabled(false);
ui->angleDoubleSpinBox->setEnabled(false);
ui->offsetLabel->setEnabled(false);
ui->offsetSpinBox->setEnabled(false);
ui->barcodeCheckBox->setEnabled(false);
// reset data structures associated with visualization window
if (template_points)
template_points = NULL;
if (arrangement)
arrangement = NULL;
if (barcode)
barcode.release();
template_points = std::shared_ptr<TemplatePointsMessage>();
arrangement = std::shared_ptr<ArrangementMessage>();
barcode = std::unique_ptr<Barcode>();
grades = Grades();
slice_diagram.reset();
p_diagram.reset();
// reset variables associated with visualization window
// Possible TODO: Remove intializers from constructor?
x_reverse = false;
y_reverse = false;
degenerate_x = false;
degenerate_y = false;
line_selection_ready = false;
persistence_diagram_drawn = false;
slice_diagram_initialized = false;
// create new progress dialog for computation
prog_dialog = new ProgressDialog(this);
//connect signals related to prog_dialog
QObject::connect(cthread, &ComputationThread::advanceProgressStage, prog_dialog, &ProgressDialog::advanceToNextStage);
QObject::connect(cthread, &ComputationThread::setProgressMaximum, prog_dialog, &ProgressDialog::setStageMaximum);
QObject::connect(cthread, &ComputationThread::setCurrentProgress, prog_dialog, &ProgressDialog::updateProgress);
// to stop computation midway - TODO: make it stop more gracefully
QObject::connect(prog_dialog, &ProgressDialog::stopComputation, cthread, &ComputationThread::terminate);
start_computation();
}
//slot that starts the persistent homology computation in a new thread
void VisualizationWindow::start_computation()
{
data_selected = true;
//show the progress box
prog_dialog->show();
prog_dialog->activateWindow();
prog_dialog->raise();
//start the computation in a new thread
cthread->compute();
//update text items
auto shortName = QString::fromStdString(input_params.shortName);
this->setWindowTitle("RIVET - " + shortName);
ui->filenameLabel->setText( QStringLiteral("Input file: ").append(shortName) );
ui->homdimLabel->setText( QStringLiteral("Homology dimension: %1").arg(input_params.hom_degree) );
} //end start_computation()
//this slot is signaled when the xi support points are ready to be drawn
void VisualizationWindow::paint_template_points(std::shared_ptr<TemplatePointsMessage> points)
{
qDebug() << "VisualizationWindow: Received template points";
x_reverse=points->x_reverse;
y_reverse=points->y_reverse;
template_points = points;
//first load our local copies of the data
grades = Grades(template_points->x_exact, template_points->y_exact);
//send xi support points to the SliceDiagram
slice_diagram.clear_points();
for (auto point : template_points->template_points)
slice_diagram.add_point(grades.x[point.x], grades.y[point.y], point.zero, point.one, point.two);
double initial_xmax, initial_ymax;
if(!slice_diagram_initialized)
{
double xrev_sign=x_reverse? -1: 1;
double yrev_sign=y_reverse? -1: 1;
double x_step=fmax(.0001, (grades.x.back()-grades.x.front())/20); //spin box has 4 digit precision, so shouldn't have step size be less than this
double y_step=fmax(.0001, (grades.y.back()-grades.y.front())/20);
//note that setting a negative step is the same as setting a 0 step, according to QT docs
ui->BottomCornerXSpinBox->setSingleStep(x_step);
ui->TopCornerXSpinBox->setSingleStep(x_step);
ui->BottomCornerYSpinBox->setSingleStep(y_step);
ui->TopCornerYSpinBox->setSingleStep(y_step);
origin_x=grades.x.front();
origin_y=grades.y.front();
degenerate_x=(grades.x.size()==1);
degenerate_y=(grades.y.size()==1);
//set the maximal values of the initial window
if(degenerate_x&& degenerate_y){
//doubly degenerate case-choose window scale arbitrarily
initial_xmax=grades.x.front()+1;
initial_ymax=grades.y.front()+1;
}
//in the singly degenerate case, choose the default window to be square
else if(degenerate_x){
initial_xmax=grades.x.front()+grades.y.back()-grades.y.front();
initial_ymax=grades.y.back();
}
else if(degenerate_y){
initial_xmax=grades.x.back();
initial_ymax=grades.y.front()+grades.x.back()-grades.x.front();
}
else{
initial_xmax=grades.x.back();
initial_ymax=grades.y.back();
}
//set absolute bounds on the maximum/minimum window scroll boxes, to prevent numerical issues
double max_x_length=10*(initial_xmax-grades.x.front());
double max_y_length=10*(initial_ymax-grades.y.front());
double xmin, xmax,ymin,ymax;
if (x_reverse){
xmin=-1*grades.x.back()-max_x_length;
xmax=-1*grades.x.front()+max_x_length;
}
else{
xmin=grades.x.front()-max_x_length;
xmax=grades.x.back()+max_x_length;
}
if (y_reverse){
ymin=-1*grades.y.back()-max_y_length;
ymax=-1*grades.y.front()+max_y_length;
}
else{
ymin=grades.y.front()-max_y_length;
ymax=grades.y.back()+max_y_length;
}
ui->BottomCornerXSpinBox->setMinimum(xmin);
ui->TopCornerXSpinBox->setMaximum(xmax);
ui->BottomCornerXSpinBox->setMaximum(xmax);
ui->TopCornerXSpinBox->setMinimum(xmin);
ui->BottomCornerYSpinBox->setMinimum(ymin);
ui->TopCornerYSpinBox->setMinimum(ymin);
ui->BottomCornerYSpinBox->setMaximum(ymax);
ui->TopCornerYSpinBox->setMaximum(ymax);
//set the iniital bounds of the slice window
//these calls to setValue will only update the internal value and then return (since slice_diagram_initialized is false)
ui->BottomCornerXSpinBox->setValue(grades.x.front()*xrev_sign);
ui->BottomCornerYSpinBox->setValue(grades.y.front()*yrev_sign);
ui->TopCornerXSpinBox->setValue(initial_xmax*xrev_sign);
ui->TopCornerYSpinBox->setValue(initial_ymax*yrev_sign);
std::vector<QDoubleSpinBox*> spin_boxes={ui->BottomCornerXSpinBox,ui->BottomCornerYSpinBox,
ui->TopCornerXSpinBox,ui->TopCornerYSpinBox};
for(auto spinbox: spin_boxes)
{
QSizePolicy sp_retain = spinbox->sizePolicy();
sp_retain.setRetainSizeWhenHidden(true);
spinbox->setSizePolicy(sp_retain);
//spinbox->setEnabled(false);
}
slice_diagram_initialized=true;
}
//create the SliceDiagram
config_params.xLabel = QString::fromStdString(template_points->x_label);
config_params.yLabel = QString::fromStdString(template_points->y_label);
if (!slice_diagram.is_created() && !grades.x.empty() && !grades.y.empty()) {
slice_diagram.create_diagram(
config_params.xLabel,
config_params.yLabel,
grades.x.front(), initial_xmax,
grades.y.front(), initial_ymax,
ui->normCoordCheckBox->isChecked(), template_points->homology_dimensions,
x_reverse, y_reverse);
}
//enable control items
ui->BettiLabel->setEnabled(true);
ui->xi0CheckBox->setEnabled(true);
ui->xi1CheckBox->setEnabled(true);
ui->xi2CheckBox->setEnabled(true);
ui->normCoordCheckBox->setEnabled(true);
//update offset extents
///TODO: maybe these extents should be updated dynamically, based on the slope of the slice line
ui->offsetSpinBox->setMinimum(grades.min_offset());
ui->offsetSpinBox->setMaximum(grades.max_offset());
//update status
line_selection_ready = true;
// ui->statusBar->showMessage("bigraded Betti number visualization ready"); STATUS BAR CURRENTLY DISABLED
}
//this slot is signaled when the augmented arrangement is ready
void VisualizationWindow::augmented_arrangement_ready(std::shared_ptr<ArrangementMessage> arrangement)
{
//receive the arrangement
this->arrangement = arrangement;
if(arrangement->is_empty()) { //e.g. the arrangement contains only Betti numbers and no barcode templates
return;
}
//TESTING: print arrangement info and verify consistency
// arrangement->print_stats();
// arrangement->test_consistency();
//create persistence diagram
p_diagram.create_diagram();
//get the barcode
BarcodeTemplate dbc = arrangement->get_barcode_template(angle_precise, offset_precise);
barcode = dbc.rescale(angle_precise, offset_precise, template_points->template_points, grades);
//TESTING
barcode->print();
if (!grades.x.empty() && !grades.y.empty()) {
//shift the barcode so that "zero" is where the selected line crosses the bottom or left side of the viewing window
double ll_corner = rivet::numeric::project_to_line(angle_precise, offset_precise, grades.x[0], grades.y[0]); //lower-left corner of line selection window
barcode = barcode->shift(-1*ll_corner);
//show slice line; necessary to do this here because successive functions use parameters of this diagram
//draw the barcode
p_diagram.set_barcode(*barcode);
p_diagram.resize_diagram(slice_diagram.get_slice_length(), slice_diagram.get_pd_scale());
//draws the diagram itself, but with incorrect parameter values
p_diagram.update_diagram(slice_diagram.get_slice_length(), slice_diagram.get_pd_scale(), 0, slice_diagram.get_line_visible(), *barcode);
//updates the diagram with the correct parameter values
//enable slice diagram control items
slice_diagram.enable_slice_line();
ui->angleLabel->setEnabled(true);
ui->angleDoubleSpinBox->setEnabled(true);
ui->offsetLabel->setEnabled(true);
ui->offsetSpinBox->setEnabled(true);
ui->barcodeCheckBox->setEnabled(true);
//draw the slice diagram
slice_diagram.zoom_diagram(angle_precise, offset_precise,0);
slice_diagram.draw_barcode(*barcode, ui->barcodeCheckBox->isChecked());
//update status
if (verbosity >= 2) {
qDebug() << "COMPUTATION FINISHED; READY FOR INTERACTIVITY.";
}
persistence_diagram_drawn = true;
// ui->statusBar->showMessage("ready for interactive barcode exploration"); STATUS BAR CURRENTLY DISABLED
//Enable save menu item
ui->actionSave->setEnabled(true);
//if an output file has been specified, then save the arrangement
if (!input_params.outputFile.empty())
save_arrangement(QString::fromStdString(input_params.outputFile));
//TODO: we don't have file reading tools here anymore, so we don't know what kind of file it was
//Have to rely on console to either a) always save (to tmp file if needed), or b) tell us filetype in the output.
// else if(input_params.raw_data)
// unsaved_data = true;
}
if (prog_dialog) {
prog_dialog->setComputationFinished();
delete prog_dialog;
}
} //end augmented_arrangement_ready()
void VisualizationWindow::on_BottomCornerXSpinBox_valueChanged(double x_bottom)
{
xmin_precise=x_bottom*double(1-2*x_reverse);
if(!slice_diagram_initialized)
{
return;
}
//set minimum value in other x spin box, to prevent overflow errors
double padding=degenerate_x? .01: std::min(.01, .5*(grades.x.back()-grades.x.front()));
if(x_reverse){
ui->TopCornerXSpinBox->setMaximum(x_bottom-padding);
}
else{
ui->TopCornerXSpinBox->setMinimum(x_bottom+padding);
}
update_origin();
double step=fmax(.0001,(xmax_precise-xmin_precise)/20);
ui->TopCornerXSpinBox->setSingleStep(step);
ui->BottomCornerXSpinBox->setSingleStep(step);
slice_diagram.update_BottomX(xmin_precise,dist_to_origin,is_visible);
slice_diagram.zoom_diagram(angle_precise, offset_precise,dist_to_origin);
//this needs to be done before calling update_persistence_diagram so that
//slice_diagram contains the updated slice length and scale;
//note that changing the value of xmin (or any other bound) does not change
//the value of origin_x or origin_y, so the updated barcode sent to slice_diagram
//in the call to update_persistence_diagram is identical to the barcode stored in slice_diagram
//at this point in the program, therefore this call to zoom_diagram draws the barcode in the right place
if(ui->actionAutomatically_reset_line->isChecked() && !slice_diagram.get_line_visible())
{
reset_line();
update_persistence_diagram();
}
else if(persistence_diagram_drawn)
{
update_persistence_diagram(false);
}
}
void VisualizationWindow::on_BottomCornerYSpinBox_valueChanged(double y_bottom)
{
ymin_precise=y_bottom*double(1-2*y_reverse);
if(!slice_diagram_initialized)
{
return;
}
double padding=degenerate_y? .01: std::min(.01, .5*(grades.y.back()-grades.y.front()));
if(y_reverse){
ui->TopCornerYSpinBox->setMaximum(y_bottom-padding);
}
else{
ui->TopCornerYSpinBox->setMinimum(y_bottom+padding);
}
update_origin();
double step=fmax(.0001,(ymax_precise-ymin_precise)/20);
ui->TopCornerYSpinBox->setSingleStep(step);
ui->BottomCornerYSpinBox->setSingleStep(step);
slice_diagram.update_BottomY(ymin_precise,dist_to_origin,is_visible);
slice_diagram.zoom_diagram(angle_precise, offset_precise,dist_to_origin);
if(ui->actionAutomatically_reset_line->isChecked() && !slice_diagram.get_line_visible())
{
reset_line();
update_persistence_diagram();
}
else if(persistence_diagram_drawn)
{
update_persistence_diagram(false);
}
}
void VisualizationWindow::on_TopCornerXSpinBox_valueChanged(double x_top)
{
xmax_precise=x_top*double(1-2*x_reverse);
if(!slice_diagram_initialized)
{
return;
}
double padding=degenerate_x? .01: std::min(.01, .5*(grades.x.back()-grades.x.front()));
if (x_reverse){
ui->BottomCornerXSpinBox->setMinimum(x_top+padding);
}
else{
ui->BottomCornerXSpinBox->setMaximum(x_top-padding);
}
update_origin();
double step=fmax(.0001,(xmax_precise-xmin_precise)/20);
ui->TopCornerXSpinBox->setSingleStep(step);
ui->BottomCornerXSpinBox->setSingleStep(step);
slice_diagram.update_TopX(xmax_precise,dist_to_origin,is_visible);
slice_diagram.zoom_diagram(angle_precise, offset_precise,dist_to_origin);
if(ui->actionAutomatically_reset_line->isChecked() && !slice_diagram.get_line_visible())
{
reset_line();
update_persistence_diagram();
}
else if(persistence_diagram_drawn)
{
update_persistence_diagram(false);
}
}
void VisualizationWindow::on_TopCornerYSpinBox_valueChanged(double y_top)
{
ymax_precise=y_top*double(1-2*y_reverse);
if(!slice_diagram_initialized)
{
return;
}
double padding=degenerate_y? .01: std::min(.01, .5*(grades.y.back()-grades.y.front()));
if (y_reverse){
ui->BottomCornerYSpinBox->setMinimum(y_top+padding);
}
else{
ui->BottomCornerYSpinBox->setMaximum(y_top-padding);
}
update_origin();
double step=fmax(.0001,(ymax_precise-ymin_precise)/20);
ui->TopCornerYSpinBox->setSingleStep(step);
ui->BottomCornerYSpinBox->setSingleStep(step);
slice_diagram.update_TopY(ymax_precise,dist_to_origin,is_visible);
slice_diagram.zoom_diagram(angle_precise, offset_precise,dist_to_origin);
if(ui->actionAutomatically_reset_line->isChecked() && !slice_diagram.get_line_visible())
{
reset_line();
update_persistence_diagram();
}
else if(persistence_diagram_drawn)
{
update_persistence_diagram(false);
}
}
void VisualizationWindow::on_angleDoubleSpinBox_valueChanged(double angle)
{
if (line_selection_ready && !slice_update_lock) {
angle_precise = angle;
update_origin();
if(!is_visible&& ui->actionAutomatically_reset_line->isChecked()){
reset_line(); //reset_line will call this function again with the updated angle value
return;
}
slice_diagram.update_line(angle_precise, ui->offsetSpinBox->value(), dist_to_origin);
update_persistence_diagram();//updates the barcode in the slice diagram
//note that the x and y scales of the diagram do not need to be updated, so the values
//passed to the pd in this function will be correct
slice_diagram.zoom_diagram(angle_precise, offset_precise,dist_to_origin); //draws the diagram
}
}
void VisualizationWindow::on_offsetSpinBox_valueChanged(double offset)
{
if (line_selection_ready && !slice_update_lock) {
offset_precise = offset;
update_origin();
if(!is_visible&& ui->actionAutomatically_reset_line->isChecked()){
reset_line(); //reset_line will call this function again with the updated offset value
return;
}
slice_diagram.update_line(ui->angleDoubleSpinBox->value(), offset_precise, dist_to_origin);
update_persistence_diagram(); //updates the barcode in the slice diagram
slice_diagram.zoom_diagram(angle_precise, offset_precise,dist_to_origin);//draws the diagram,with the updated barcode
}
}
void VisualizationWindow::on_normCoordCheckBox_clicked(bool checked)
{
if (line_selection_ready) {
slice_diagram.set_normalized_coords(checked);
slice_diagram.resize_diagram();
if (persistence_diagram_drawn)
p_diagram.resize_diagram(slice_diagram.get_slice_length(), slice_diagram.get_pd_scale());
}
}
void VisualizationWindow::on_barcodeCheckBox_clicked(bool checked)
{
if (line_selection_ready)
slice_diagram.toggle_barcode(checked);
}
void VisualizationWindow::on_xi0CheckBox_toggled(bool checked)
{
if (line_selection_ready)
slice_diagram.toggle_xi0_points(checked);
}
void VisualizationWindow::on_xi1CheckBox_toggled(bool checked)
{
if (line_selection_ready)
slice_diagram.toggle_xi1_points(checked);
}
void VisualizationWindow::on_xi2CheckBox_toggled(bool checked)
{
if (line_selection_ready)
slice_diagram.toggle_xi2_points(checked);
}
//updates the persistence diagram and barcode after a change in the slice line
void VisualizationWindow::update_persistence_diagram(bool line_changed)
{
if (persistence_diagram_drawn) {
//get the barcode
if(line_changed)
{
if (verbosity >= 0) {
qDebug() << " QUERY: angle =" << angle_precise << ", offset =" << offset_precise;
}
BarcodeTemplate dbc = arrangement->get_barcode_template(angle_precise, offset_precise);
barcode = dbc.rescale(angle_precise, offset_precise, template_points->template_points, grades);
//shift the barcode so that "zero" is where the selected line crosses the bottom or left side of the viewing window
double ll_corner = rivet::numeric::project_to_line(angle_precise, offset_precise, origin_x, origin_y); //lower-left corner of line selection window
barcode = barcode->shift(-1*ll_corner);
//TESTING
//qDebug() << " XI SUPPORT VECTOR:";
//for (unsigned i = 0; i < template_points->template_points.size(); i++) {
// TemplatePoint p = template_points->template_points[i];
// qDebug().nospace() << " [" << i << "]: (" << p.x << "," << p.y << ") --> (" << grades.x[p.x] << "," << grades.y[p.y] << ")";
//}
if (verbosity >= 0) {
dbc.print();
barcode->print();
}
slice_diagram.update_barcode(*barcode, ui->barcodeCheckBox->isChecked());
}
p_diagram.update_diagram(slice_diagram.get_slice_length(), slice_diagram.get_pd_scale(),dist_to_origin, is_visible,*barcode);
//slice_diagram.update_barcode(*barcode, ui->barcodeCheckBox->isChecked());
}
}
void VisualizationWindow::set_line_parameters(double angle, double offset)
{
slice_update_lock = true;
//correct for slight numerical errors that the interface might introduce
if (angle < 0 && angle > -45)
angle = 0;
if (angle > 90 || angle < -40)
angle = 90;
//store values internally
angle_precise = angle;
offset_precise = offset;
update_origin();
//update UI elements (values will be truncated)
ui->angleDoubleSpinBox->setValue(angle);//note these calls do NOT call update_origin or update_persistence diagram because slice_update_lock=true
ui->offsetSpinBox->setValue(offset);
update_persistence_diagram();
slice_diagram.zoom_diagram(angle_precise, offset_precise,dist_to_origin);
slice_update_lock = false;
}
void VisualizationWindow::showEvent(QShowEvent* event)
{
QMainWindow::showEvent(event);
if (!data_selected) {
ds_dialog.exec(); //show();
}
}
void VisualizationWindow::resizeEvent(QResizeEvent* /*unused*/)
{
if (line_selection_ready) {
slice_diagram.resize_diagram();
}
if (persistence_diagram_drawn)
{
p_diagram.resize_diagram(slice_diagram.get_slice_length(), slice_diagram.get_pd_scale());
}
}
void VisualizationWindow::closeEvent(QCloseEvent* event)
{
if (unsaved_data) {
QMessageBox::StandardButton reallyExit;
reallyExit = QMessageBox::question(this, "Exit?", "Are you sure you want to exit? Your augmented arrangement has not been saved and will be lost!", QMessageBox::Yes | QMessageBox::No);
if (reallyExit == QMessageBox::Yes) {
event->accept();
// qDebug() << "User has closed RIVET.";
} else
event->ignore();
} else {
event->accept();
}
}
void VisualizationWindow::on_actionExit_triggered()
{
close();
}
void VisualizationWindow::on_actionAbout_triggered()
{
aboutBox.show();
}
void VisualizationWindow::on_actionConfigure_triggered()
{
// create new configure dialog
configBox = new ConfigureDialog(config_params, input_params, this);
configBox->show();
// connect to receive config parameter change
QObject::connect(configBox, &ConfigureDialog::window_closed, this, &VisualizationWindow::redraw);
}
void VisualizationWindow::on_actionSave_persistence_diagram_as_image_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this, "Export persistence diagram as image",
suggestedName("persist_offset_" + QString::number(offset_precise)
+ "_angle_" + QString::number(angle_precise) + ".png"),
"PNG Image (*.png)");
if (!fileName.isNull()) {
QSettings settings;
settings.setValue(DEFAULT_SAVE_DIR_KEY, QFileInfo(fileName).absolutePath());
QPixmap pixMap = ui->pdView->grab();
pixMap.save(fileName, "PNG");
}
///TODO: error handling?
}
void VisualizationWindow::on_actionSave_line_selection_window_as_image_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this, "Export line selection window as image",
suggestedName("line_offset_" + QString::number(offset_precise)
+ "_angle_" + QString::number(angle_precise) + ".png"),
"PNG Image (*.png)");
if (!fileName.isNull()) {
QSettings settings;
settings.setValue(DEFAULT_SAVE_DIR_KEY, QFileInfo(fileName).absolutePath());
QPixmap pixMap = ui->sliceView->grab();
pixMap.save(fileName, "PNG");
}
///TODO: error handling?
}
void VisualizationWindow::on_actionSave_triggered()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save module invariants file", suggestedName("mif"));
if (!fileName.isNull()) {
QSettings settings;
settings.setValue(DEFAULT_SAVE_DIR_KEY, QFileInfo(fileName).absolutePath());
save_arrangement(fileName);
}
///TODO: error handling?
} //end on_actionSave_triggered()
void VisualizationWindow::save_arrangement(const QString& filename)
{
try {
write_msgpack_file(filename, input_params, *template_points, *arrangement);
} catch (std::exception& e) {
QMessageBox errorBox(QMessageBox::Warning, "Error",
QString("Unable to save arrangement: ").append(filename).append(": ").append(e.what()));
errorBox.exec();
}
} //end save_arrangement()
void VisualizationWindow::on_actionOpen_triggered()
{
///TODO: get user confirmation
// open dataselect dialog to get new data
ds_dialog.exec();
} //end on_actionOpen_triggered()
void VisualizationWindow::on_actionRestore_default_window_triggered()
{
//the internal values of the bounds
//e.g. xmin is always less than xmax.
//if xreverse=true, and the window goes from, e.g. 10 to 0
//then orig_xmin=-10 and orig_xmax=0
double orig_xmin=slice_diagram.get_original_xmin();
double orig_xmax=slice_diagram.get_original_xmax();
double orig_ymin=slice_diagram.get_original_ymin();
double orig_ymax=slice_diagram.get_original_ymax();
double xrev_sign=x_reverse? -1 :1;
double yrev_sign=y_reverse? -1 :1;
//this is necessary to correctly deal with the case where the current value is negative
//but the reset value is not
ui->BottomCornerXSpinBox->from_button=true;
ui->BottomCornerYSpinBox->from_button=true;
ui->TopCornerXSpinBox->from_button=true;
ui->TopCornerYSpinBox->from_button=true;
//set the values displayed in the controls
ui->BottomCornerXSpinBox->setValue(orig_xmin*xrev_sign);
ui->BottomCornerYSpinBox->setValue(orig_ymin*yrev_sign);
ui->TopCornerXSpinBox->setValue(orig_xmax*xrev_sign);
ui->TopCornerYSpinBox->setValue(orig_ymax*yrev_sign);
ui->BottomCornerXSpinBox->from_button=false;
ui->BottomCornerYSpinBox->from_button=false;
ui->TopCornerXSpinBox->from_button=false;
ui->TopCornerYSpinBox->from_button=false;
xmin_precise=orig_xmin; //overwrite the value set by the call to setValue to avoid rounding errors in reset_line()
xmax_precise=orig_xmax;
ymin_precise=orig_ymin;
ymax_precise=orig_ymax;
reset_line();
update_origin();
slice_diagram.zoom_diagram(angle_precise, offset_precise, dist_to_origin);
update_persistence_diagram();
}//end on_actionRestore_default_window_triggered()
//sets the window to be the smallest one containing all nonzero Betti numbers, with the slice line connecting the corners
void VisualizationWindow::on_actionBetti_number_window_triggered()
{
if(degenerate_x|| degenerate_y){
on_actionRestore_default_window_triggered();
return;
}
double xmin=slice_diagram.get_min_supp_xi_x();
double xmax=slice_diagram.get_max_supp_xi_x();
double ymin=slice_diagram.get_min_supp_xi_y();
double ymax=slice_diagram.get_max_supp_xi_y();
double xrev_sign=x_reverse? -1 :1;
double yrev_sign=y_reverse? -1 :1;
//this is necessary to correctly deal with the case where the current value is negative
//but the reset value is not
ui->BottomCornerXSpinBox->from_button=true;
ui->BottomCornerYSpinBox->from_button=true;
ui->TopCornerXSpinBox->from_button=true;
ui->TopCornerYSpinBox->from_button=true;
ui->BottomCornerXSpinBox->setValue(xmin*xrev_sign);
ui->BottomCornerYSpinBox->setValue(ymin*yrev_sign);
ui->TopCornerXSpinBox->setValue(xmax*xrev_sign);
ui->TopCornerYSpinBox->setValue(ymax*yrev_sign);
ui->BottomCornerXSpinBox->from_button=false;
ui->BottomCornerYSpinBox->from_button=false;
ui->TopCornerXSpinBox->from_button=false;
ui->TopCornerYSpinBox->from_button=false;
xmin_precise=xmin; //overwrite the value set by the call to setValue to avoid rounding errors in reset_line()
xmax_precise=xmax;
ymin_precise=ymin;
ymax_precise=ymax;
reset_line();
update_origin();
slice_diagram.zoom_diagram(angle_precise, offset_precise, dist_to_origin);
update_persistence_diagram();
}//end on_actionBetti_number_window_triggered()
//resets the line if it is not currently visible, and the option is set to true
void VisualizationWindow::on_actionAutomatically_reset_line_toggled()
{
if(ui->actionAutomatically_reset_line->isChecked()&& ! slice_diagram.get_line_visible())