-
Notifications
You must be signed in to change notification settings - Fork 1
/
nbody-vis.c
1780 lines (1619 loc) · 46.7 KB
/
nbody-vis.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
/* x86_64-pc-mingw32-gcc -static -s -std=c11 -O2 -Wall -DNDEBUG -Wextra -pedantic -Wno-unused-parameter -I/usr/x86_64-pc-mingw32/usr/include/{SDL,cairo} -D_GNU_SOURCE=1 -o nbody.exe nbody-vis.c ring-buf.c -mwindows -lmingw32 -lSDLmain -lSDL -ldxguid -lcairo -lws2_32 -lfreetype -lpixman-1 -lz -lwinmm */
#define _XOPEN_SOURCE 500 /* M_PI, usleep() */
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* size_t, etc. */
#include <math.h> /* sin()/cos() */
#include <cairo.h>
#include <SDL.h>
#include <sys/time.h> /* gettimeofday() */
#include <fcntl.h> /* fcntl() */
#include <errno.h> /* errno, EAGAIN */
#include <assert.h>
#if defined(_WIN64) || defined(_WIN32)
# include <winsock2.h> /* select() et al. */
#endif
#include "ring-buf.h"
#define TRAIL_QUANTIZED 1
#define TRAIL_MESHED 2
#define PROG_TITLE "n-Body Visualizer"
#define INPUT_BUFFER_RESERVE (1 << 22) /* byte */
#define BODY_RADIUS (1.0/6.0) /* input distance unit */
#define TRAIL_WIDTH (BODY_RADIUS/4) /* input distance unit */
#define CENTER_X 0 /* input distance unit */
#define CENTER_Y 0 /* input distance unit */
#define WIDTH 800 /* px */
#define HEIGHT 800 /* px */
#define SCALE 72 /* px/(input distance unit) */
#define VIEW_SCROLL_RATIO (1.0/6.0)
#define VIEW_SCALE_RATIO (2.0/3.0)
#define TIME_SCALE_RATIO (2.0/3.0)
#define EVENT_LOOP_SLEEP 500 /* micro seconds */
//#define TRAIL_QUANT_STEPS 256
//#define TRAIL_METHOD TRAIL_MESHED
#define TRAIL_MAX_SEGMENTS 20000
#define TRAIL_MAX_DIST 4.5 /* px */
#define GAMMA 2.2
#define AUTO_ZOOM_FRAME_BORDER 20
#define LIGHT_COLOR_R 255
#define LIGHT_COLOR_G 255
#define LIGHT_COLOR_B 255
#define COLOR_MAX 255
#define FRAME_CAIRO_FORMAT CAIRO_FORMAT_RGB24
#define FRAME_CAIRO_ASHIFT 24
#define FRAME_CAIRO_RSHIFT 16
#define FRAME_CAIRO_GSHIFT 8
#define FRAME_CAIRO_BSHIFT 0
#define FRAME_CAIRO_AMASK (0x00 << FRAME_CAIRO_ASHIFT)
#define FRAME_CAIRO_RMASK (0xff << FRAME_CAIRO_RSHIFT)
#define FRAME_CAIRO_GMASK (0xff << FRAME_CAIRO_GSHIFT)
#define FRAME_CAIRO_BMASK (0xff << FRAME_CAIRO_BSHIFT)
/* check the above settings and define some macros used by the code later on */
#ifndef TRAIL_METHOD
# define TRAIL_METHOD TRAIL_QUANTIZED
#endif
#if TRAIL_METHOD == TRAIL_QUANTIZED
# ifndef TRAIL_QUANT
# ifdef TRAIL_QUANT_STEPS
# define TRAIL_QUANT(v) (int)((v) * (TRAIL_QUANT_STEPS - 1.0) + .5)
# else
# define TRAIL_QUANT(v) (v)
# endif
# endif
#elif TRAIL_METHOD == TRAIL_MESHED
# if !(CAIRO_VERSION_MAJOR == 1 && CAIRO_VERSION_MINOR >= 12)
# error TRAIL_METHOD == TRAIL_MESHED requires cairo >= 1.12
# endif
#else
# error unknown TRAIL_METHOD, supported are TRAIL_QUANTIZED, TRAIL_MESHED
#endif
#ifndef FRAME_GRAY
# if LIGHT_COLOR_R == LIGHT_COLOR_G && LIGHT_COLOR_G == LIGHT_COLOR_B
# define FRAME_GRAY 1
# else
# define FRAME_GRAY 0
# endif
#endif
#ifndef M_PI
# define M_PI atan2(+0.0, -0.0)
#endif
struct input {
struct ring_buf buf;
struct ring_buf line;
const unsigned *field_ids;
unsigned n_fields;
int fd;
};
#define INPUT_INIT { RING_BUF_INIT, RING_BUF_INIT, NULL, 0, -1 }
static int parse_record(
const char *line, unsigned n_fields, const unsigned *field_ids,
double *values
) {
unsigned id;
unsigned valid_fields = 0;
int a, n;
for (id = 1; *line && valid_fields < n_fields; id++) {
unsigned id_idx;
line += strspn(line, " \t");
if (line[0] == '#') /* remaining line is comment */
return 1;
a = strcspn(line, " \t");
for (id_idx = 0; id_idx < n_fields; id_idx++) {
if (field_ids[id_idx] != id)
continue;
if (sscanf(line, "%lf%n", values + id_idx, &n) < 1 ||
n != a) {
fprintf(stderr,
"cannot interpret %u:'%.*s' as "
"double; ignoring line\n",
id, (int)a, line);
return 1;
}
valid_fields++;
}
line += a;
}
if (valid_fields >= n_fields)
return 0;
fprintf(stderr, "not enough fields on line, ignoring\n");
return 1;
}
static int read_record(struct input *in, double *values)
{
struct ring_buf *buf = &in->buf;
struct ring_buf *line = &in->line;
size_t newline = 0;
if (rb_read(buf, in->fd) == -1 && errno != EAGAIN)
return -1;
while (1) {
ssize_t rd;
char *q = rb_chr(buf, '\n');
if (q) {
#if 1
newline = rb_ptrdiff(buf, q);
#else
size_t p = q - in->buf.buf;
newline = p >= buf->idx ? p - buf->idx
: buf->sz + p - buf->idx;
#endif
}
if (newline) {
#if 1
rb_reset(line);
#else
line->idx = line->valid = 0;
#endif
rb_ensure_sz(line, newline+1);
rb_tfer(line, buf, newline);
rb_putc(line, '\0');
rb_getc(buf); /* eat '\n' (if available) */
/* fprintf(stderr, "'%s'\n", line->buf); */
if (!parse_record(line->buf, in->n_fields, in->field_ids, values))
return 1;
newline = 0;
if (buf->valid)
continue;
}
if (buf->valid == buf->sz)
rb_ensure_sz(buf, buf->sz < INPUT_BUFFER_RESERVE
? INPUT_BUFFER_RESERVE
: 2 * buf->sz);
rd = rb_read(buf, in->fd);/*
fprintf(stderr,
"buf after read %zd: idx: %zu, valid: %zu, sz: %zu, "
"newline: %zu\n",
rd, buf->idx, buf->valid, buf->sz, newline);*/
if (rd < 0)
return errno == EAGAIN ? 0 : -1;
if (rd == 0) {
if (!buf->valid)
return -1;
newline = buf->valid;
} else {
newline = 0;
}
}
}
struct pt {
double x, y;
};
static inline struct pt * pt_add(struct pt *a, const struct pt *b)
{
a->x += b->x;
a->y += b->y;
return a;
}
static inline struct pt * pt_neg(struct pt *a)
{
a->x = -a->x;
a->y = -a->y;
return a;
}
static inline struct pt * pt_sub(struct pt *a, const struct pt *b)
{
a->x -= b->x;
a->y -= b->y;
return a;
}
static inline struct pt * pt_muld(struct pt *a, double s)
{
a->x *= s;
a->y *= s;
return a;
}
static inline double pt_norm_square(const struct pt *a)
{
return a->x * a->x + a->y * a->y;
}
static inline double pt_norm(const struct pt *a)
{
return hypot(a->x, a->y);
}
static inline struct pt * pt_min(struct pt *min, const struct pt *b)
{
if (b->x < min->x) min->x = b->x;
if (b->y < min->y) min->y = b->y;
return min;
}
static inline struct pt * pt_max(struct pt *max, const struct pt *b)
{
if (b->x > max->x) max->x = b->x;
if (b->y > max->y) max->y = b->y;
return max;
}
/* returns determinant d of [1 ax ay; 1 bx by; 1 cx cy]:
* d < 0 => abc cw, d > 0 => abc ccw, d == 0 => abc collinear */
static double pt_orientation(
const struct pt *a, const struct pt *b, const struct pt *c
) {
return (b->x - a->x)*(c->y - a->y) - (c->x - a->x)*(b->y - a->y);
}
static void paint_bobbel(cairo_t *cr, const struct pt *d, double r)
{
cairo_pattern_t *pat;
pat = cairo_pattern_create_radial(
d->x - .3 * r, d->y - .3 * r, 0.1 * r,
d->x - .3 * r, d->y - .3 * r, 1.3 * r);
cairo_pattern_add_color_stop_rgb(pat, 0,
(double)LIGHT_COLOR_R / COLOR_MAX,
(double)LIGHT_COLOR_G / COLOR_MAX,
(double)LIGHT_COLOR_B / COLOR_MAX);
cairo_pattern_add_color_stop_rgb(pat, 1, 0, 0, 0);
cairo_save(cr);
cairo_set_source(cr, pat);
cairo_arc(cr, d->x, d->y, r, 0, 2 * M_PI);
cairo_fill(cr);
cairo_restore(cr);
cairo_pattern_destroy(pat);
}
__attribute__((format(printf,2,3)))
static int cr_printf(cairo_t *cr, const char *fmt, ...)
{
va_list ap;
int n;
va_start(ap, fmt);
n = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (n < 0)
return n;
char buf[n+1];
va_start(ap, fmt);
vsnprintf(buf, n+1, fmt, ap);
va_end(ap);
cairo_show_text(cr, buf);
return n;
}
struct frame {
unsigned w, h;
cairo_surface_t *sf;
SDL_Surface *sdl_sf;
int sdl_flags;
};
struct pos_it {
struct pos_it *next;
double t;
struct pt pts[];
};
struct pos_queue {
struct pos_it *head;
struct pos_it *tail;
size_t n;
};
static void pos_queue_update(
struct pos_queue *q, unsigned n_fields, const double *values,
double trail_fade_t
) {
struct pos_it *it = NULL;
unsigned i;
while (q->head && values[0] - q->head->t > trail_fade_t) {
free(it);
it = q->head;
q->head = it->next;
if (!q->head)
q->tail = NULL;
q->n--;
}
if (!it)
it = malloc(sizeof(struct pos_it) +
sizeof(struct pt) * (n_fields/2));
it->next = NULL;
it->t = values[0];
for (i=0; i<n_fields/2; i++) {
it->pts[i].x = values[1+i*2+0];
it->pts[i].y = values[1+i*2+1];
}
if (q->tail)
q->tail->next = it;
else
q->head = it;
q->tail = it;
q->n++;
}
/* cw */
static struct pt orth(const struct pt *s, const struct pt *t, double scale)
{
struct pt r = { -(t->y - s->y), (t->x - s->x) };
scale /= hypot(r.x, r.y);
r.x *= scale;
r.y *= scale;
return r;
}
#define CR_PATH_OBJS_CLOSE (1+0)
#define CR_PATH_OBJS_MOVE_TO (1+1)
#define CR_PATH_OBJS_LINE_TO (1+1)
#define CR_PATH_OBJS_CURVE_TO (1+3)
static void cr_path_close(cairo_path_data_t *d)
{
d->header.type = CAIRO_PATH_CLOSE_PATH;
d->header.length = 1;
}
static void cr_path_move_to(cairo_path_data_t *d, const struct pt *p)
{
d->header.type = CAIRO_PATH_MOVE_TO;
d->header.length = 2;
d++;
d->point.x = p->x;
d->point.y = p->y;
}
static void cr_path_line_to(cairo_path_data_t *d, const struct pt *p)
{
d->header.type = CAIRO_PATH_LINE_TO;
d->header.length = 2;
d++;
d->point.x = p->x;
d->point.y = p->y;
}
#if TRAIL_METHOD == TRAIL_QUANTIZED
/* FIXME: endpoints of adjoining line segments get more ink due to painting
* those twice */
static int paint_trail(
cairo_t *cr,
double t, unsigned n_pts, const struct pos_queue *q, double trail_fade_t
) {
cairo_save(cr);
cairo_push_group(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_line_width(cr, TRAIL_WIDTH);
/*cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);*/
cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
const struct pos_it *prev, *it = q->head;
unsigned iterations = 0;
while ((prev = it) && (it = it->next)) {
iterations++;
double trail_fraction = (t - it->t) / trail_fade_t;
double c = 1.0 - pow(trail_fraction, 1.0/GAMMA);
unsigned n = 1, i;
double d = c;
while (it->next) {
double next_trail_fraction = (t - it->next->t) / trail_fade_t;
d = 1.0 - pow(next_trail_fraction, 1.0/GAMMA);
if (TRAIL_QUANT(c) != TRAIL_QUANT(d))
break;
it = it->next;
n++;
}
#if 0
for (i=0; i<n_pts; i++) {
cairo_pattern_t *pat = cairo_pattern_create_linear(
prev->pts[i].x,
prev->pts[i].y,
(it->next ? it->next : it)->pts[i].x,
(it->next ? it->next : it)->pts[i].y);
cairo_pattern_add_color_stop_rgb(pat, 0.0, c, c, c);
cairo_pattern_add_color_stop_rgb(pat, 1.0, d, d, d);
cairo_set_source(cr, pat);
cairo_pattern_destroy(pat);
cairo_move_to(cr, prev->pts[i].x, prev->pts[i].y);
const struct pos_it *it2 = prev->next;
for (unsigned j=0; j<=n && it2; j++, it2 = it2->next)
cairo_line_to(cr, it2->pts[i].x, it2->pts[i].y);
cairo_stroke(cr);
}
#elif 1
cairo_set_source_rgba(cr,0,0,0,c);
for (i=0; i<n_pts; i++) {
const struct pos_it *it2 = prev->next;
unsigned j;
cairo_move_to(cr, prev->pts[i].x, prev->pts[i].y);
for (j=0; j<=n && it2; j++, it2 = it2->next)
cairo_line_to(cr, it2->pts[i].x, it2->pts[i].y);
}
cairo_stroke(cr);
#else
cairo_set_source_rgba(cr,0,0,0,c);
for (i=0; i<n_pts; i++) {
cairo_path_data_t path[n*4*(1+1)];
unsigned i0 = 0, i1 = n*4*(1+1);
unsigned j;
const struct pos_it *it0 = prev;
const struct pos_it *it1 = it0->next;
const struct pos_it *it2 = it1->next;
struct pt o = orth(it0->pts+i, it1->pts+i, TRAIL_WIDTH/2.0);
struct pt p = orth(it1->pts+i, it2->pts+i, TRAIL_WIDTH/2.0);
struct pt q = o;
pt_muld(pt_add(&q, &p), .5);
struct pt p1 = it0->pts[i];
struct pt p2 = it1->pts[i];
struct pt p3 = it1->pts[i];
struct pt p4 = it0->pts[i];
pt_add(&p1, &o);
pt_sub(&p4, &o)
// cairo_move_to(cr, p1.x, p1.y);
cr_path_move_to(&path[i0], &p1), i0 += CR_PATH_OBJS_MOVE_TO;
cr_path_close(&path[i1 -= CR_PATH_OBJS_CLOSE]);
cr_path_line_to(&path[i1 -= CR_PATH_OBJS_LINE_TO], &p4);
double orient = pt_orientation(it0->pts+i, it1->pts+i, it2->pts+i);
if (orient > 0.0) { /* ccw */
} else if (orient < -0.0) { /* cw */
} else { /* collinear */
}
}
cairo_fill(cr);
#endif
}
cairo_pop_group_to_source(cr);
cairo_paint(cr);
cairo_restore(cr);
return iterations;
}
#elif TRAIL_METHOD == TRAIL_MESHED
static void cr_mesh_patch(
cairo_pattern_t *mesh,
const struct pt *side0_center, const struct pt *side0_off, double a0,
const struct pt *side2_center, const struct pt *side2_off, double a2
) {
const struct pt *s = side0_center;
const struct pt *t = side2_center;
const struct pt *o = side0_off;
const struct pt *p = side2_off;
cairo_mesh_pattern_begin_patch(mesh);
cairo_mesh_pattern_move_to(mesh, s->x + o->x, s->y + o->y);
cairo_mesh_pattern_line_to(mesh, s->x - o->x, s->y - o->y);
cairo_mesh_pattern_line_to(mesh, t->x - p->x, t->y - p->y);
cairo_mesh_pattern_line_to(mesh, t->x + p->x, t->y + p->y);
cairo_mesh_pattern_set_corner_color_rgba(mesh, 0, 0, 0, 0, a0);
cairo_mesh_pattern_set_corner_color_rgba(mesh, 1, 0, 0, 0, a0);
cairo_mesh_pattern_set_corner_color_rgba(mesh, 2, 0, 0, 0, a2);
cairo_mesh_pattern_set_corner_color_rgba(mesh, 3, 0, 0, 0, a2);
cairo_mesh_pattern_end_patch(mesh);
}
/* FIXME: intersections between parts of the same path are painted
* non-antialiased, because mesh patches have non-AA borders */
static int paint_trail(
cairo_t *cr,
double t, unsigned n_pts, const struct pos_queue *q, double trail_fade_t
) {
const struct pos_it *prev, *it, *next;
unsigned iterations = 0, i;
cairo_save(cr);
cairo_set_line_width(cr, TRAIL_WIDTH);
/*cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);*/
cairo_set_line_join(cr, CAIRO_LINE_JOIN_BEVEL);
for (i=0; i<n_pts; i++) {
it = q->head;
if (!it || !(next = it->next))
break;
cairo_pattern_t *mesh = cairo_pattern_create_mesh();
cairo_move_to(cr, it->pts[i].x, it->pts[i].y);
while ((prev = it, it = it->next) != NULL) {
double col0 = 1.0 - pow((t - prev->t) / trail_fade_t, 1.0/GAMMA);
double col1 = 1.0 - pow((t - it->t) / trail_fade_t, 1.0/GAMMA);
const struct pt *s = prev->pts + i;
const struct pt *t = it->pts + i;
cairo_line_to(cr, t->x, t->y);
/* orth on (s -> t) with length TRAIL_WIDTH/2.0 */
struct pt o = orth(s, t, TRAIL_WIDTH / 2.0);
/* define a col0-to-col1 gradient patch that completely
* covers the current line segment */
cr_mesh_patch(mesh, s, &o, col0, t, &o, col1);
if (!it->next)
continue;
/* orth on (t -> u) with length TRAIL_WIDTH/2.0 */
struct pt p = orth(t, it->next->pts+i, TRAIL_WIDTH/2.0);
/* define a solid col1 patch that completely covers the
* (bevel) join of the current line with the next one */
cr_mesh_patch(mesh, t, &o, col1, t, &p, col1);
}
cairo_set_source(cr, mesh);
cairo_stroke(cr);
cairo_pattern_destroy(mesh);
}
cairo_restore(cr);
return iterations;
}
#endif
#include <inttypes.h>
static void paint_frame(
const struct frame *f, const cairo_matrix_t *pt2view,
unsigned n_fields, const double *values, unsigned rec,
const struct pos_queue *q, double trail_fade_t, double rb_filled,
double exec_delta_t, int nrec
) {
unsigned n_pts = n_fields/2, i;
struct timeval tv, tw;
double x, y, w, h;
int trail_n = 0;
gettimeofday(&tv, NULL);
cairo_t *cr = cairo_create(f->sf);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
cairo_paint(cr);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_matrix(cr, pt2view);
if (q)
trail_n = paint_trail(cr, values[0], n_pts, q, trail_fade_t);
for (i=0; i<n_pts; i++) {
x = values[1+2*i+0];
y = values[1+2*i+1];
paint_bobbel(cr, &(struct pt){ x, y }, BODY_RADIUS);
}
x = f->w / 2.0;
y = f->h / 2.0;
w = f->w / 2.0;
h = f->h / 2.0;
cairo_device_to_user(cr, &x, &y);
cairo_device_to_user_distance(cr, &w, &h);
cairo_identity_matrix(cr);
/* draw some record infos on the frame */
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_move_to(cr, 5, f->h - 5);
cr_printf(cr, "Step %u, time: %.6e", rec, values[0]);
for (i=0; i<n_pts; i++)
cr_printf(cr, ", x%u: %.6e y%u: %.6e",
i, values[1+2*i+0], i, values[1+2*i+1]);
cairo_move_to(cr, 5, 13);
cr_printf(cr, "view: [%.6e +/- %.6e] x [%.6e +/- %.6e]", x,w, y,h);
gettimeofday(&tw, NULL);
cr_printf(cr,
", frame creation: %02.1f ms, loop: %02.1f ms, queue: %" PRIuMAX ":%u"
", buf: %4.1f %%, nrec: %d",
(tw.tv_sec - tv.tv_sec) * 1e3 + (tw.tv_usec - tv.tv_usec) / 1e3,
exec_delta_t * 1e3,
(uintmax_t)q->n, trail_n, rb_filled * 100, nrec);
cairo_destroy(cr);
cairo_surface_flush(f->sf);
}
static void frame_set_surface(struct frame *f)
{
cairo_format_t format = FRAME_CAIRO_FORMAT;
if (f->sf) {
if ((unsigned)cairo_image_surface_get_width(f->sf) == f->w &&
(unsigned)cairo_image_surface_get_height(f->sf) == f->h &&
cairo_image_surface_get_format(f->sf) == format)
return;
cairo_surface_destroy(f->sf);
}
f->sf = cairo_image_surface_create(format, f->w, f->h);
}
static int sdl_set_video_mode(struct frame *f)
{
if (!SDL_SetVideoMode(f->w, f->h, 0, f->sdl_flags))
return 1;
if (f->sdl_sf)
SDL_FreeSurface(f->sdl_sf);
frame_set_surface(f);
/* these SDL surfaces never need locking */
f->sdl_sf = SDL_CreateRGBSurfaceFrom(
cairo_image_surface_get_data(f->sf),
f->w, f->h, 32, cairo_image_surface_get_stride(f->sf),
FRAME_CAIRO_RMASK, FRAME_CAIRO_GMASK, FRAME_CAIRO_BMASK,
FRAME_CAIRO_AMASK);
return 0;
}
static void sdl_update_display(const struct frame *f)
{
SDL_Surface *screen = SDL_GetVideoSurface();
SDL_BlitSurface(f->sdl_sf, NULL, screen, NULL);
SDL_Flip(screen);
}
struct at {
double x0;
double s;
};
static double affine_transform(const struct at *a, double p)
{
return a->x0 + p * a->s;
}
static double affine_inverse_transform(const struct at *a, double q)
{
return (q - a->x0) / a->s;
}
#define EV_QUIT (1 << 0)
#define EV_REPAINT (1 << 1)
static int process_event(
const SDL_Event *ev, struct frame *f, cairo_matrix_t *a, double t,
struct at *time_scale, int *auto_zoom
) {
struct pt tmp = { 0, 0 };
cairo_matrix_t inv_a;
double v0;
memcpy(&inv_a, a, sizeof(inv_a));
cairo_matrix_invert(&inv_a);
switch (ev->type) {
case SDL_QUIT:
return EV_QUIT;
case SDL_KEYDOWN:
switch (ev->key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
return EV_QUIT;
case SDLK_a:
*auto_zoom = !*auto_zoom;
return EV_REPAINT;
case SDLK_PLUS:
case SDLK_KP_PLUS:
if (!isinf(t)) {
v0 = affine_inverse_transform(time_scale, t);
time_scale->s *= TIME_SCALE_RATIO;
time_scale->x0 = t - time_scale->s * v0;
} else {
time_scale->s *= TIME_SCALE_RATIO;
}
break;
case SDLK_MINUS:
case SDLK_KP_MINUS:
if (!isinf(t)) {
v0 = affine_inverse_transform(time_scale, t);
time_scale->s /= TIME_SCALE_RATIO;
time_scale->x0 = t - time_scale->s * v0;
} else {
time_scale->s /= TIME_SCALE_RATIO;
}
break;
case SDLK_0:
case SDLK_KP0:
if (!isinf(t)) {
v0 = affine_inverse_transform(time_scale, t);
time_scale->s = 1;
time_scale->x0 = t - time_scale->s * v0;
} else {
time_scale->s = 1;
}
break;
case SDLK_LEFT:
tmp.x = f->w * VIEW_SCROLL_RATIO;
cairo_matrix_transform_distance(&inv_a, &tmp.x, &tmp.y);
cairo_matrix_translate(a, tmp.x, tmp.y);
*auto_zoom = 0;
return EV_REPAINT;
case SDLK_RIGHT:
tmp.x = f->w * -VIEW_SCROLL_RATIO;
cairo_matrix_transform_distance(&inv_a, &tmp.x, &tmp.y);
cairo_matrix_translate(a, tmp.x, tmp.y);
*auto_zoom = 0;
return EV_REPAINT;
case SDLK_UP:
tmp.y = f->h * VIEW_SCROLL_RATIO;
cairo_matrix_transform_distance(&inv_a, &tmp.x, &tmp.y);
cairo_matrix_translate(a, tmp.x, tmp.y);
*auto_zoom = 0;
return EV_REPAINT;
case SDLK_DOWN:
tmp.y = f->h * -VIEW_SCROLL_RATIO;
cairo_matrix_transform_distance(&inv_a, &tmp.x, &tmp.y);
cairo_matrix_translate(a, tmp.x, tmp.y);
*auto_zoom = 0;
return EV_REPAINT;
default:
break;
}
break;
case SDL_MOUSEMOTION:
if (ev->motion.state & SDL_BUTTON(1)) {
tmp.x = ev->motion.xrel;
tmp.y = ev->motion.yrel;
cairo_matrix_transform_distance(&inv_a, &tmp.x, &tmp.y);
cairo_matrix_translate(a, tmp.x, tmp.y);
*auto_zoom = 0;
return EV_REPAINT;
}
break;
case SDL_MOUSEBUTTONDOWN:
switch (ev->button.button) {
case 4: /* wheel up */
tmp.x = ev->button.x;
tmp.y = ev->button.y;
cairo_matrix_transform_point(&inv_a, &tmp.x, &tmp.y);
cairo_matrix_translate(a, tmp.x, tmp.y);
cairo_matrix_scale(a, 1.0/VIEW_SCALE_RATIO,
1.0/VIEW_SCALE_RATIO);
cairo_matrix_translate(a, -tmp.x, -tmp.y);
*auto_zoom = 0;
return EV_REPAINT;
case 5: /* wheel down */
tmp.x = ev->button.x;
tmp.y = ev->button.y;
cairo_matrix_transform_point(&inv_a,
&tmp.x, &tmp.y);
cairo_matrix_translate(a, tmp.x, tmp.y);
cairo_matrix_scale(a, VIEW_SCALE_RATIO,
VIEW_SCALE_RATIO);
cairo_matrix_translate(a, -tmp.x, -tmp.y);
*auto_zoom = 0;
return EV_REPAINT;
}
break;
case SDL_VIDEORESIZE:
f->w = ev->resize.w;
f->h = ev->resize.h;
if (sdl_set_video_mode(f)) {
fprintf(stderr,
"error resizing surface to %dx%d: %s\n",
f->w, f->h, SDL_GetError());
exit(1);
}
return EV_REPAINT;
default:
break;
}
return 0;
}
static struct timeval * tv_norm(struct timeval *a)
{
ldiv_t n = ldiv(a->tv_usec, 1000000);
if (n.rem < 0) {
n.quot--;
n.rem += 1000000;
}
a->tv_sec += n.quot;
a->tv_usec = n.rem;
return a;
}
#if defined(_WIN64) || defined(_WIN32)
# define suseconds_t signed long
#endif
static inline struct timeval * tv_addi(struct timeval *a, time_t sec, suseconds_t usec)
{
a->tv_sec += sec;
a->tv_usec += usec;
return tv_norm(a);
}
static inline struct timeval * tv_add(struct timeval *a, const struct timeval *b)
{
return tv_addi(a, b->tv_sec, b->tv_usec);
}
static inline struct timeval * tv_subi(struct timeval *a, time_t sec, suseconds_t usec)
{
a->tv_sec -= sec;
a->tv_usec -= usec;
return tv_norm(a);
}
static inline struct timeval * tv_sub(struct timeval *a, const struct timeval *b)
{
return tv_subi(a, b->tv_sec, b->tv_usec);
}
static inline int tv_test(const struct timeval *a)
{
if (a->tv_sec < 0) return -1;
if (a->tv_sec > 0) return +1;
if (a->tv_usec < 0) return -1;
if (a->tv_usec > 0) return +1;
return 0;
}
static int tv_rem(struct timeval *limit)
{
struct timeval now;
gettimeofday(&now, NULL);
return tv_test(tv_sub(limit, &now));
}
/* calc n-1 finite difference values; store n values in fd
* 0: point
* 1: speed
* 2: acceleration
* ... */
static void fdiff_update(double *fd, unsigned n, double y, double inv_dt)
{
unsigned i;
for (i=0; i<n; i++) {
double x = fd[i];
fd[i] = i ? (fd[i-1] - y) * inv_dt : y;
y = x;
}
/*
struct fdiff2 r;
r.x = y;
r.dx = (r.x - fd2->x ) * inv_dt;
r.ddx = (r.dx - fd2->dx) * inv_dt;
*fd2 = r;*/
/*
fd->s = *pt_muld(pt_sub(&d, &fd->b), inv_dt);
fd->b = *c;*/
/*
fd->s.x = (c->x - fd->b.x) / dt;
fd->s.y = (c->y - fd->b.y) / dt;
fd->a = fd->b;
fd->b = *c;
fd->dt = dt;
fd->ds_square = fd->s.x * fd->s.x + fd->s.y * fd->s.y;*/
}
struct zoom_control_state {
double x0[3], y0[3], x1[3], y1[3]; /* fdiffs: required bbox */
double scale[3]; /* derived from bbox, needed? */
};
struct bbox {
struct pt min, max;
};
#define BBOX_INIT { { NAN, NAN }, { NAN, NAN } }
/* returns whether bbox a entirely contains bbox b */
static int bbox_contains(const struct bbox *a, const struct bbox *b)
{
if (b->min.x < a->min.x) return 0;
if (b->min.y < a->min.y) return 0;
if (b->max.x > a->max.x) return 0;
if (b->max.y > a->max.y) return 0;
return 1;
}
static inline struct pt * bbox_dim(struct pt *ret, const struct bbox *b)
{
*ret = b->max;
return pt_sub(ret, &b->min);
}
#if 0
static double bbox_scale(
const struct pt *a_dim, const struct pt *b_dim
) {
double sx = a_dim->x / b_dim->x;
double sy = a_dim->y / b_dim->y;
return sx < sy ? sx : sy;
}
#endif
static inline void bbox_span_pt(struct bbox *b, const struct pt *p, int empty)
{
if (empty) {
memcpy(&b->min, p, sizeof(b->min));
memcpy(&b->max, p, sizeof(b->max));
} else {
pt_min(&b->min, p);
pt_max(&b->max, p);
}
}
static inline void bbox_span(struct bbox *a, const struct bbox *b, int empty)
{
if (empty) {
memcpy(a, b, sizeof(*a));
} else {
pt_min(&a->min, &b->min);
pt_max(&a->max, &b->max);
}
}
static inline int bbox_is_empty(const struct bbox *b)
{
return !(b->min.x <= b->max.x && b->min.y <= b->max.y);
}
static void scene_bbox(
struct bbox *bbox_device, const cairo_matrix_t *a, const double *values,
unsigned n, double body_r, const struct pos_it *it, double trail_r
) {
unsigned i, j;
if (!n)
return;
/* find bbox for center points of all objects in device space */
for (i=0; i<n; i++) {
struct pt p = { values[i+i+0], values[i+i+1] };
cairo_matrix_transform_point(a, &p.x, &p.y);
bbox_span_pt(bbox_device, &p, !i);
}
/* adjust for the radius; this is in user space, so a transformation of
* the device unit vectors to user space is done to determine the
* appropriate scaling factors for r regarding horizontal and vertical
* boundaries;
* are these only valid for the current scaling factor of the matrix, so
* body_r_device needs to be rescaled accordingly later??? */
struct pt hori = { 1, 0 }, vert = { 0, 1 };
cairo_matrix_t inv_a = *a;
cairo_matrix_invert(&inv_a);
cairo_matrix_transform_distance(&inv_a, &hori.x, &hori.y);
cairo_matrix_transform_distance(&inv_a, &vert.x, &vert.y);
double inv_hori_norm = 1.0 / pt_norm(&hori);
double inv_vert_norm = 1.0 / pt_norm(&vert);
struct pt body_r_device = {
.x = body_r * inv_hori_norm,
.y = body_r * inv_vert_norm,
};
pt_sub(&bbox_device->min, &body_r_device);
pt_add(&bbox_device->max, &body_r_device);
if (!it)
return;
struct bbox trail_bbox_device;
for (j=0; it; it = it->next)