-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcinterplot.c
3512 lines (3059 loc) · 108 KB
/
cinterplot.c
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
#include "cinterplot_common.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdatomic.h>
#include <sys/stat.h>
#include "cinterplot.h"
#include "font.c"
#include "oklab.h"
#include "savepng.h"
#define LOG101_VALUE 0.0099503308531681
#define LOG101_VALUE_INV (1.0 / LOG101_VALUE)
#define log101(x) (log (x) * LOG101_VALUE_INV)
#define exp101(x) exp ((x) * LOG101_VALUE)
#define LOGFUN log101
#define EXPFUN exp101
double cx = 0;
double cy = 0;
double cz = 0;
typedef struct CipState
{
uint32_t crosshairEnabled : 1;
uint32_t trackingMode : 2;
uint32_t statuslineEnabled : 1;
uint32_t zoomEnabled : 1;
uint32_t fullscreen : 1;
uint32_t redraw : 1;
uint32_t redrawing : 1;
uint32_t running : 1;
uint32_t bordered : 1;
uint32_t forceRefresh : 1;
uint32_t margin : 8;
uint32_t showHelp : 1;
uint32_t stopped : 1;
int (*app_on_keyboard) (CipState *cs, int key, int mod, int pressed, int repeat);
int (*app_on_mouse_motion) (CipState *cs, int windowIndex, double x, double y);
int mouseState;
CipPosition mouseWindowPos;
CipMouse mouse;
float bgShade;
uint32_t numSubWindows;
CipSubWindow *subWindows;
CipSubWindow *activeSw;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
uint32_t windowWidth;
uint32_t windowHeight;
uint32_t graphOrder;
int frameCounter;
int pressedModifiers;
int (*on_mouse_pressed) (struct CipState *cs, int xi, int yi, int button, int clicks);
int (*on_mouse_released) (struct CipState *cs, int xi, int yi);
int (*on_mouse_motion) (struct CipState *cs, int xi, int yi);
int (*on_mouse_wheel) (struct CipState *cs, float xf, float yf);
int (*on_keyboard) (struct CipState *cs, int key, int mod, int pressed, int repeat);
void (*plot_data) (struct CipState *cs, uint32_t *pixels);
} CipState;
#define STATUSLINE_HEIGHT 20
enum
{
MOUSE_HOVER_MODE_NONE,
MOUSE_HOVER_MODE_WINDOW,
MOUSE_HOVER_MODE_BORDER_L,
MOUSE_HOVER_MODE_BORDER_R,
MOUSE_HOVER_MODE_BORDER_T,
MOUSE_HOVER_MODE_BORDER_B,
MOUSE_HOVER_MODE_CORNER_TL,
MOUSE_HOVER_MODE_CORNER_TR,
MOUSE_HOVER_MODE_CORNER_BL,
MOUSE_HOVER_MODE_CORNER_BR,
};
enum
{
MOUSE_STATE_NONE,
MOUSE_STATE_MOVING,
MOUSE_STATE_SELECTING,
MOUSE_STATE_RESIZING_L,
MOUSE_STATE_RESIZING_R,
MOUSE_STATE_RESIZING_T,
MOUSE_STATE_RESIZING_B,
MOUSE_STATE_RESIZING_TL,
MOUSE_STATE_RESIZING_TR,
MOUSE_STATE_RESIZING_BL,
MOUSE_STATE_RESIZING_BR,
};
extern const unsigned int font[256][8];
static int interrupted = 0;
static int paused = 0;
static CipArea storedDataRanges[10] = {0};
static uint64_t make_histogram_2d (CipHistogram *hist, CipGraph *graph, uint32_t logMode, char plotType);
static uint64_t make_histogram_3d (CipHistogram *hist, CipGraph *graph, uint32_t logMode, char plotType);
//static void *safe_malloc (size_t size)
//{
// void *ptr = malloc (size);
// if (!ptr)
// exit_error ("can't malloc %ld", size);
// return ptr;
//}
static void *safe_calloc (size_t count, size_t size)
{
void *ptr = calloc (count, size);
if (!ptr)
exit_error ("can't calloc %ld %ld", count, size);
return ptr;
}
static char *parse_csv (char *str, int *argc, char ***argv, char sep, int inplace)
{
if (!str)
return NULL;
char *modStr = inplace ? str : strdup (str);
assert (modStr);
int cnt = 1;
int len = (int) strlen (modStr);
for (int i=0; i<len; i++)
if (modStr[i] == sep)
cnt++;
char **strings = (char**) malloc ((uint32_t) cnt * sizeof (char*));
assert (strings);
int argi=0, i=0;
strings[argi++] = modStr;
while (modStr[i])
{
if (modStr[i] == sep)
{
modStr[i++] = '\0';
strings[argi++] = & modStr[i];
}
else
{
i++;
}
}
*argc = cnt;
*argv = strings;
return modStr;
}
static int file_exists (const char* file)
{
struct stat s;
return stat (file, & s) == 0;
}
static double get_time (void)
{
struct timeval t;
gettimeofday (& t, 0);
double timestamp = (double) t.tv_sec + (double) t.tv_usec / 1000000;
return timestamp;
}
void wait_for_access (atomic_flag* accessFlag)
{
while (atomic_flag_test_and_set(accessFlag))
continue;
}
void release_access (atomic_flag* accessFlag)
{
atomic_flag_clear (accessFlag);
}
static uint32_t rgb2color (RGB *rgb)
{
float r = rgb->r;
float g = rgb->g;
float b = rgb->b;
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
if (r > 1) r = 1;
if (g > 1) g = 1;
if (b > 1) b = 1;
int ri = (int) (255 * r);
int gi = (int) (255 * g);
int bi = (int) (255 * b);
return MAKE_COLOR (ri,gi,bi);
}
static uint32_t make_gray (float a)
{
RGB rgb = {a,a,a};
return rgb2color (& rgb);
}
typedef struct RGBNames
{
const char *name;
const int r;
const int g;
const int b;
} RGBNames;
static const RGBNames rgbNameList[] =
{
{"red", 255, 0, 0},
{"green", 0, 255, 0},
{"blue", 0, 0, 255},
{"black", 0, 0, 0},
{"white", 255, 255, 255},
{"cyan", 0, 255, 255},
{"magenta", 255, 0, 255},
{"yellow", 255, 255, 0},
{"orange", 255, 165, 0},
{"purple", 128, 0, 128},
{"pink", 255, 192, 203},
{"brown", 165, 42, 42},
{"gray", 128, 128, 128},
{"darkgray", 169, 169, 169},
{"lightgray", 211, 211, 211},
{"lime", 191, 255, 0},
{"indigo", 75, 0, 130},
{"violet", 238, 130, 238},
{"silver", 192, 192, 192},
{"gold", 255, 215, 0},
{"beige", 245, 245, 220},
{"navy", 0, 0, 128},
{"maroon", 128, 0, 0},
{"olive", 128, 128, 0},
{"teal", 0, 128, 128},
{"lime", 0, 255, 0},
{"aquamarine", 127, 255, 212},
{"turquoise", 64, 224, 208},
{"royalblue", 70, 130, 180},
{"slategray", 112, 128, 144},
{"plum", 221, 160, 221},
{"orchid", 218, 112, 214},
{"salmon", 250, 128, 114},
{"coral", 255, 127, 80},
{"wheat", 245, 222, 179},
{"peru", 205, 133, 63},
{"sienna", 160, 82, 45},
{"chocolate", 210, 105, 30}
};
static int get_color_by_name (char *str, int *r, int *g, int *b)
{
int n = sizeof (rgbNameList) / sizeof (rgbNameList[0]);
for (int i=0; i<n; i++)
{
if (equal (str, rgbNameList[i].name))
{
*r = rgbNameList[i].r;
*g = rgbNameList[i].g;
*b = rgbNameList[i].b;
return 0;
}
}
return -1;
}
static void delete_color_scheme (CipColorScheme *scheme)
{
if (scheme)
{
free (scheme->colors);
free (scheme);
}
}
static CipColorScheme *make_color_scheme (char *spec, uint32_t nLevels)
{
CipColorScheme *scheme = safe_calloc (1, sizeof (*scheme));
scheme->colors = safe_calloc (nLevels, sizeof (scheme->colors[0]));
scheme->nLevels = nLevels;
int argc;
char **argv;
char *modStr = parse_csv (spec, & argc, & argv, ' ', 0);
if (!modStr)
exit_error ("bug");
uint32_t nVertices = (uint32_t) argc;
if (nVertices > MAX_NUM_VERTICES)
exit_error ("nVertices(%u) > MAX_NUM_VERTICES(%u) at '%s'", nVertices, MAX_NUM_VERTICES, spec);
RGB vertices[MAX_NUM_VERTICES];
for (int i=0; i<argc; i++)
{
char *str = argv[i];
if (str[0] == '#')
{
unsigned int r,g,b;
if (sscanf (& str[1], "%02x%02x%02x", & r, & g, & b) != 3)
exit_error ("parse error at '%s' in str spec '%s'", str, spec);
float s = 1.0f / 255.0f;
vertices[i].r = (float) r * s;
vertices[i].g = (float) g * s;
vertices[i].b = (float) b * s;
}
else if ('0' <= str[0] && str[0] <= '9')
{
int colorIndex, numColors;
if (sscanf (str, "%d/%d", & colorIndex, & numColors) != 2)
exit_error ("parse error at '%s' in str spec '%s'", str, spec);
Lab oklab;
oklab.L = 0.7f;
float C = 0.5f;
float h = (float) ((2 * M_PI * colorIndex) / numColors);
oklab.a = C * cosf(h);
oklab.b = C * sinf(h);
oklab_to_srgb (& oklab, & vertices[i]);
}
else
{
int r,g,b;
if (get_color_by_name (str, & r, & g, & b) < 0)
exit_error ("parse error at '%s' in color spec '%s'", str, spec);
float s = 1.0f / 255.0f;
vertices[i].r = (float) r * s;
vertices[i].g = (float) g * s;
vertices[i].b = (float) b * s;
}
}
free (modStr);
free (argv);
if (nVertices == 1)
{
uint32_t color = rgb2color (& vertices[0]);
for (uint32_t i=0; i<nLevels; i++)
scheme->colors[i] = color;
}
else
{
uint32_t offset = 0;
for (int v=0; v<nVertices-1; v++)
{
Lab c0, c1;
srgb_to_oklab (& vertices[v], & c0);
srgb_to_oklab (& vertices[v+1], & c1);
uint32_t len = nLevels / (nVertices - 1);
if (v == 0)
{
uint32_t rest = nLevels - len * (nVertices - 1);
len += rest;
}
for (uint32_t i=0; i<len; i++)
{
float w1;
if (v+1 == nVertices-1)
w1 = (float) i / (float) (len - 1);
else
w1 = (float) i / (float) len;
float w0 = 1 - w1;
Lab c =
{
w0 * c0.L + w1 * c1.L,
w0 * c0.a + w1 * c1.a,
w0 * c0.b + w1 * c1.b,
};
RGB rgb;
oklab_to_srgb (& c, & rgb);
if (offset + i >= nLevels)
exit_error ("bug");
scheme->colors[offset + i] = rgb2color (& rgb);
}
offset += len;
}
if (offset != nLevels)
exit_error ("bug");
}
return scheme;
}
void cip_update_color_scheme (CipState *cs, GraphAttacher *attacher, char *spec, uint32_t nLevels)
{
CipColorScheme *oldColorScheme = attacher->colorScheme;
attacher->colorScheme = make_color_scheme (spec, nLevels);
if (oldColorScheme)
{
while (cs->redrawing)
usleep (10);
if (oldColorScheme->colors)
free (oldColorScheme->colors);
free (oldColorScheme);
}
}
static void matrix_vector_multiply (double mtx[3][3], double src[3], double dst[3])
{
dst[0] = mtx[0][0] * src[0] + mtx[0][1] * src[1] + mtx[0][2] * src[2];
dst[1] = mtx[1][0] * src[0] + mtx[1][1] * src[1] + mtx[1][2] * src[2];
dst[2] = mtx[2][0] * src[0] + mtx[2][1] * src[1] + mtx[2][2] * src[2];
}
double (*rotMatrix)[3][3]; // FIXME: should not be needed
double perspectiveFactor = 0.15;
static void cycle_graph_order (CipState *cs)
{
cs->graphOrder++;
//print_debug ("graph order %u", cs->graphOrder);
}
int cip_autoscale_sw (CipSubWindow *sw)
{
if (!sw)
return 0;
double xmin = DBL_MAX;
double xmax = -DBL_MAX;
double ymin = DBL_MAX;
double ymax = -DBL_MAX;
GraphAttacher **ag = sw->attachedGraphs;
for (int i=0; i<sw->numAttachedGraphs; i++)
{
CipGraph *graph = ag[i]->graph;
wait_for_access (& graph->readAccess);
wait_for_access (& graph->insertAccess);
int is3d = graph->sb->itemSize == sizeof (double) * 3;
if (is3d)
{
double (*xyzs)[3];
uint32_t len;
stream_buffer_get (graph->sb, & xyzs, & len);
int firstIdx = (int) len - (int) graph->len;
if (firstIdx < 0 || graph->len == 0)
firstIdx = 0;
for (uint32_t i=firstIdx; i<len; i++)
{
double xyz[3];
matrix_vector_multiply (*rotMatrix, xyzs[i], xyz);
double x2 = xyz[0];
double y2 = xyz[1];
double z2 = xyz[2];
double scale = z2 * perspectiveFactor + 1;
if (scale < 0)
continue;
x2 /= scale;
y2 /= scale;
if (isnan (x2) || isnan (y2) || isnan (z2) || isinf (x2) || isinf (y2) || isinf (z2))
continue;
if (sw->logMode & 1) x2 = LOGFUN (x2);
if (sw->logMode & 2) y2 = LOGFUN (y2);
if (isnan (x2) || isnan (y2)) continue;
if (isinf (x2) || isinf (y2)) continue;
if (xmin > x2) xmin = x2;
if (xmax < x2) xmax = x2;
if (ymin > y2) ymin = y2;
if (ymax < y2) ymax = y2;
}
}
else
{
double (*xys)[2];
uint32_t len;
stream_buffer_get (graph->sb, & xys, & len);
int firstIdx = (int) len - (int) graph->len;
if (firstIdx < 0 || graph->len == 0)
firstIdx = 0;
for (uint32_t i=firstIdx; i<len; i++)
{
double x = xys[i][0];
double y = xys[i][1];
if (sw->logMode & 1) x = LOGFUN (x);
if (sw->logMode & 2) y = LOGFUN (y);
if (isnan (x) || isnan (y)) continue;
if (isinf (x) || isinf (y)) continue;
if (xmin > x) xmin = x;
if (xmax < x) xmax = x;
if (ymin > y) ymin = y;
if (ymax < y) ymax = y;
}
}
release_access (& graph->insertAccess);
release_access (& graph->readAccess);
}
if (xmin == DBL_MAX || xmax == -DBL_MAX || ymin == DBL_MAX || ymax == -DBL_MAX)
return 0;
double dx = xmax - xmin;
double dy = ymax - ymin;
double mx = dx * 0.0;
double my = dy * 0.05;
sw->dataRange.x0 = xmin - mx;
sw->dataRange.y0 = ymax + my;
sw->dataRange.x1 = xmax + mx;
sw->dataRange.y1 = ymin - my;
return 1;
}
int cip_autoscale (CipState *cs, uint32_t windowIndex)
{
return cip_autoscale_sw (cip_get_sub_window (cs, windowIndex));
}
void cip_set_range (CipSubWindow *sw, double xmin, double ymin, double xmax, double ymax, int setAsDefault)
{
if (!sw)
exit_error ("bug");
sw->dataRange.x0 = xmin;
sw->dataRange.y0 = ymax;
sw->dataRange.x1 = xmax;
sw->dataRange.y1 = ymin;
if (setAsDefault)
memcpy (& sw->defaultDataRange, & sw->dataRange, sizeof (CipArea));
}
void cip_set_x_range (CipState *cs, uint32_t windowIndex, double xmin, double xmax, int setAsDefault)
{
CipSubWindow *sw = cip_get_sub_window (cs, windowIndex);
if (!sw)
exit_error ("bug");
sw->dataRange.x0 = xmin;
sw->dataRange.x1 = xmax;
if (setAsDefault)
memcpy (& sw->defaultDataRange, & sw->dataRange, sizeof (CipArea));
}
void cip_set_y_range (CipState *cs, uint32_t windowIndex, double ymin, double ymax, int setAsDefault)
{
CipSubWindow *sw = cip_get_sub_window (cs, windowIndex);
if (!sw)
exit_error ("bug");
sw->dataRange.y0 = ymax;
sw->dataRange.y1 = ymin;
if (setAsDefault)
memcpy (& sw->defaultDataRange, & sw->dataRange, sizeof (CipArea));
}
int cip_continuous_scroll_update (CipSubWindow *sw)
{
double xmin = DBL_MAX;
double xmax = -DBL_MAX;
GraphAttacher **ag = sw->attachedGraphs;
for (int i=0; i<sw->numAttachedGraphs; i++)
{
CipGraph *graph = ag[i]->graph;
wait_for_access (& graph->readAccess);
wait_for_access (& graph->insertAccess);
double (*xys)[2];
uint32_t len;
stream_buffer_get (graph->sb, & xys, & len);
if (len)
{
int firstIdx = (int) len - (int) graph->len;
if (firstIdx < 0)
firstIdx = 0;
double x0 = xys[firstIdx][0];
double x1 = xys[len-1][0];
if (xmin > x0) xmin = x0;
if (xmax < x1) xmax = x1;
}
release_access (& graph->insertAccess);
release_access (& graph->readAccess);
}
if (xmin == DBL_MAX || xmax == -DBL_MAX)
return 0;
double width = sw->dataRange.x1 - sw->dataRange.x0;
sw->dataRange.x1 = xmax;
sw->dataRange.x0 = sw->dataRange.x1 - width;
return 1;
}
void cip_continuous_scroll_enable (CipState *cs, uint32_t windowIndex) { CipSubWindow *sw = cip_get_sub_window (cs, windowIndex); if (sw) sw->continuousScroll=1; }
void cip_continuous_scroll_disable (CipState *cs, uint32_t windowIndex) { CipSubWindow *sw = cip_get_sub_window (cs, windowIndex); if (sw) sw->continuousScroll=0; }
int cip_set_grid_mode (CipState *cs, uint32_t windowIndex, uint32_t mode) { CipSubWindow *sw = cip_get_sub_window (cs, windowIndex); return cip_set_grid_mode_sw (sw, mode); }
int cip_set_grid_mode_sw (CipSubWindow *sw, uint32_t mode) { if (sw) sw->gridMode = mode & 3; return 1; }
int cip_force_refresh (CipState *cs) { cs->forceRefresh = 1; return 1; }
int cip_is_running (CipState *cs) { return cs->running; }
void cip_redraw_async(CipState *cs) { cs->redraw=1; }
void cip_set_bg_shade (CipState *cs, float bgShade) { cs->bgShade = bgShade; }
int cip_set_crosshair_enabled (CipState *cs, uint32_t enabled) { cs->crosshairEnabled = enabled & 1; return 1; }
int cip_set_statusline_enabled (CipState *cs, uint32_t enabled) { cs->statuslineEnabled = enabled & 1; return 1; }
int cip_set_tracking_mode (CipState *cs, uint32_t mode) { cs->trackingMode = mode & 3; return 1; }
int cip_toggle_paused (CipState *cs) { paused ^= 1; return 1; }
int cip_quit (CipState *cs) { cs->running = 0; paused=0; return 0; }
static int toggle_help (CipState *cs) { cs->showHelp ^= 1; return 1; }
static int cycle_selected_graph (CipSubWindow *sw, uint32_t step)
{
sw->selectedGraph = sw->numAttachedGraphs ? (sw->selectedGraph + step) % sw->numAttachedGraphs : 0;
return 1;
}
static void undo_zooming (CipSubWindow *sw)
{
if (!sw)
return;
if (sw->defaultDataRange.x0 == sw->defaultDataRange.x1)
return;
if (sw->defaultDataRange.y0 == sw->defaultDataRange.y1)
return;
memcpy (& sw->dataRange, & sw->defaultDataRange, sizeof (CipArea));
}
int cip_set_log_mode_sw (CipSubWindow *sw, uint32_t mode)
{
if (!sw)
return 0;
mode &= 3;
int xIsLog = sw->logMode & 1;
int yIsLog = sw->logMode & 2;
int xWantsLog = mode & 1;
int yWantsLog = mode & 2;
if (xIsLog != xWantsLog)
{
if (xWantsLog)
{
sw->dataRange.x0 = LOGFUN (sw->dataRange.x0);
sw->dataRange.x1 = LOGFUN (sw->dataRange.x1);
sw->mouseDataPos.x = LOGFUN (sw->mouseDataPos.x);
}
else
{
sw->dataRange.x0 = EXPFUN (sw->dataRange.x0);
sw->dataRange.x1 = EXPFUN (sw->dataRange.x1);
sw->mouseDataPos.x = EXPFUN (sw->mouseDataPos.x);
}
}
if (yIsLog != yWantsLog)
{
if (yWantsLog)
{
sw->dataRange.y0 = LOGFUN (sw->dataRange.y0);
sw->dataRange.y1 = LOGFUN (sw->dataRange.y1);
sw->mouseDataPos.y = LOGFUN (sw->mouseDataPos.y);
}
else
{
sw->dataRange.y0 = EXPFUN (sw->dataRange.y0);
sw->dataRange.y1 = EXPFUN (sw->dataRange.y1);
sw->mouseDataPos.y = EXPFUN (sw->mouseDataPos.y);
}
}
sw->logMode = mode & 3;
return 1;
}
int cip_set_log_mode (CipState *cs, uint32_t windowIndex, uint32_t mode)
{
return cip_set_log_mode_sw (cip_get_sub_window (cs, windowIndex), mode);
}
static void transform_pos (const CipArea *srcArea, const CipPosition *srcPos, const CipArea *dstArea, CipPosition *dstPos)
{
double xf = (srcPos->x - srcArea->x0) / (srcArea->x1 - srcArea->x0);
double yf = (srcPos->y - srcArea->y0) / (srcArea->y1 - srcArea->y0);
dstPos->x = xf * (dstArea->x1 - dstArea->x0) + dstArea->x0;
dstPos->y = yf * (dstArea->y1 - dstArea->y0) + dstArea->y0;
}
static void get_active_area (CipState *cs, CipArea *src, CipArea *dst)
{
uint32_t w = cs->windowWidth;
uint32_t h = cs->windowHeight - cs->statuslineEnabled * STATUSLINE_HEIGHT;
double xp = (double) (cs->bordered + cs->margin) / w;
double yp = (double) (cs->bordered + cs->margin) / h;
double epsw = 1.0 / cs->windowWidth;
double epsh = 1.0 / cs->windowHeight;
dst->x0 = src->x0 + xp;
dst->y0 = src->y0 + yp;
dst->x1 = src->x1 - xp - epsw;
dst->y1 = src->y1 - yp - epsh;
}
static void sub_window_change (CipState *cs, int dir)
{
int index = (int) (cs->activeSw - cs->subWindows);
index += dir;
if (index < 0)
index = 0;
if (index > (int) cs->numSubWindows - 1)
index = (int) cs->numSubWindows - 1;
if (cs->zoomEnabled)
{
CipSubWindow *sw0 = cs->activeSw;
CipSubWindow *sw1 = & cs->subWindows[index];
CipArea activeArea;
CipArea zoomWindowArea = {0,0,1,1};
get_active_area (cs, & zoomWindowArea, & activeArea);
CipPosition winPos;
transform_pos (& sw0->dataRange, & sw0->mouseDataPos, & activeArea, & winPos);
transform_pos (& activeArea, & winPos, & sw1->dataRange, & sw1->mouseDataPos);
}
cs->activeSw = & cs->subWindows[index];
}
int cip_zoom (CipSubWindow *sw, double xf, double yf)
{
if (!sw)
return 0;
CipArea *dr = & sw->dataRange;
double dx = (dr->x1 - dr->x0) * xf;
double dy = (dr->y1 - dr->y0) * yf;
dr->x0 += dx;
dr->x1 -= dx;
dr->y0 += dy;
dr->y1 -= dy;
return 1;
}
int cip_move (CipSubWindow *sw, double xf, double yf)
{
if (!sw)
return 0;
CipArea *dr = & sw->dataRange;
double dx = (dr->x1 - dr->x0) * xf;
double dy = (dr->y1 - dr->y0) * yf;
dr->x0 -= dx;
dr->x1 -= dx;
dr->y0 -= dy;
dr->y1 -= dy;
return 1;
}
static void reinitialise_sdl_context (CipState *cs, int reinitWindow)
{
if (cs->texture)
SDL_DestroyTexture (cs->texture);
if (cs->renderer)
SDL_DestroyRenderer (cs->renderer);
if (reinitWindow)
{
if (cs->window)
SDL_DestroyWindow (cs->window);
cs->window = SDL_CreateWindow (CINTERPLOT_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
(int) cs->windowWidth, (int) cs->windowHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if (!cs->window)
exit_error ("Window could not be created: SDL Error: %s\n", SDL_GetError ());
}
cs->renderer = SDL_CreateRenderer (cs->window, -1, SDL_RENDERER_ACCELERATED);
if (!cs->renderer)
exit_error ("Renderer could not be created! SDL Error: %s\n", SDL_GetError ());
cs->texture = SDL_CreateTexture (cs->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
(int) cs->windowWidth, (int) cs->windowHeight);
if (!cs->texture)
exit_error ("Texture could not be created: SDL Error: %s\n", SDL_GetError ());
SDL_SetWindowFullscreen (cs->window, cs->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
}
int cip_set_fullscreen (CipState *cs, uint32_t fullscreen)
{
cs->fullscreen = fullscreen & 1;
if (cs->fullscreen)
{
SDL_DisplayMode displayMode;
SDL_GetCurrentDisplayMode (0, & displayMode);
assert (displayMode.w > 0);
assert (displayMode.h > 0);
print_debug ("fullscreen res: %u %u", displayMode.w, displayMode.h);
cs->windowWidth = (uint32_t) displayMode.w;
cs->windowHeight = (uint32_t) displayMode.h;
}
else
{
cs->windowWidth = CINTERPLOT_INIT_WIDTH;
cs->windowHeight = CINTERPLOT_INIT_HEIGHT;
}
reinitialise_sdl_context (cs, 1);
return 1;
}
static void lineRGBA (uint32_t *pixels, uint32_t _w, uint32_t _h, uint32_t _x0, uint32_t _y0, uint32_t _x1, uint32_t _y1, uint32_t color)
{
if (!pixels)
return;
int w = (int) _w;
int h = (int) _h;
int x0 = (int) _x0;
int y0 = (int) _y0;
int x1 = (int) _x1;
int y1 = (int) _y1;
int xabs = (x1 > x0) ? x1 - x0 : x0 - x1;
int yabs = (y1 > y0) ? y1 - y0 : y0 - y1;
if (xabs > yabs)
{
int xstart = ((x0 < x1) ? x0 : x1);
int xstop = xstart + xabs;
if (xstart < 0) xstart = 0;
if (xstart > w-1) xstart = w-1;
if (xstop < 0) xstop = 0;
if (xstop > w-1) xstop = w-1;
for (int x=xstart; x<=xstop; x++)
{
int y = (int) (y0 + ((double) (x - x0) / (x1 - x0) * (y1 - y0) + 0.5));
if (x>=0 && y>=0 && x<w && y<h)
pixels[y*w+x] = color;
}
}
else if (yabs >= xabs && y0 != y1)
{
int ystart = ((y0 < y1) ? y0 : y1);
int ystop = ystart + yabs;
if (ystart < 0) ystart = 0;
if (ystart > h-1) ystart = h-1;
if (ystop < 0) ystop = 0;
if (ystop > h-1) ystop = h-1;
for (int y=ystart; y<=ystop; y++)
{
int x = (int) (x0 + ((double) (y - y0) / (y1 - y0) * (x1 - x0) + 0.5));
if (x>=0 && y>=0 && x<w && y<h)
pixels[y*w+x] = color;
}
}
else
{
int x = x0;
int y = y0;
if (x>=0 && y>=0 && x<w && y<h)
pixels[y*w+x] = color;
}
}
void cip_histogram_line (CipHistogram *hist, int x0, int y0, int x1, int y1)
{
int *bins = hist->bins;
int w = (int) hist->w;
int h = (int) hist->h;
if ((x0 < 0 && x1 < 0) ||
(y0 < 0 && y1 < 0) ||
(x0 > w && x1 > w) ||
(y0 > h && y1 > h)
)
return;
int xabs = (x1 > x0) ? x1 - x0 : x0 - x1;
int yabs = (y1 > y0) ? y1 - y0 : y0 - y1;
if (xabs > yabs)
{
int xstart = ((x0 < x1) ? x0 : x1);
int xstop = xstart + xabs;
if (xstart < 0) xstart = 0;
if (xstart > w-1) xstart = w-1;
if (xstop < 0) xstop = 0;
if (xstop > w-1) xstop = w-1;
for (int x=xstart; x<=xstop; x++)
{
int y = (int) (y0 + ((double) (x - x0) / (x1 - x0) * (y1 - y0) + 0.5));
if (x>=0 && y>=0 && x<w && y<h)
bins[y*w+x]++;
}
}
else if (yabs >= xabs && y0 != y1)
{
int ystart = ((y0 < y1) ? y0 : y1);
int ystop = ystart + yabs;
if (ystart < 0) ystart = 0;
if (ystart > h-1) ystart = h-1;
if (ystop < 0) ystop = 0;
if (ystop > h-1) ystop = h-1;
for (int y=ystart; y<=ystop; y++)
{
int x = (int) (x0 + ((double) (y - y0) / (y1 - y0) * (x1 - x0) + 0.5));
if (x>=0 && y>=1 && x<w && y<h)
bins[y*w+x]++;
}
}
else
{
int x = x0;
int y = y0;
if (x>=0 && y>=0 && x<w && y<h)
bins[y*w+x]++;
}
}
static void draw_rect (uint32_t* pixels, uint32_t w, uint32_t h, uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint32_t color)
{
if (x1 < x0)
{
x1 ^= x0;
x0 ^= x1;
x1 ^= x0;
}
if (y1 < y0)
{
y1 ^= y0;
y0 ^= y1;
y1 ^= y0;
}
if (!pixels)
return;
for (uint32_t yi=y0; yi<=y1 && yi<h; yi++)
{
pixels[yi*w + x0] = color;
pixels[yi*w + x1] = color;
}
for (uint32_t xi=x0; xi<=x1 && xi<w; xi++)
{
pixels[y0*w + xi] = color;
pixels[y1*w + xi] = color;
}