summaryrefslogtreecommitdiff
path: root/meme/overlay.c
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2015-03-13 06:53:53 (GMT)
committerWerner Almesberger <werner@almesberger.net>2015-03-13 06:53:53 (GMT)
commit55e2604f9cf7145d3aa4456658260ace68bc260c (patch)
treed69e89401dd64a247425821a53a975385bb8f81d /meme/overlay.c
parent6a7b99db7751a7b7bceb2e79d7b11f68a2ad226a (diff)
downloadmisc-55e2604f9cf7145d3aa4456658260ace68bc260c.zip
misc-55e2604f9cf7145d3aa4456658260ace68bc260c.tar.gz
misc-55e2604f9cf7145d3aa4456658260ace68bc260c.tar.bz2
meme/: add overlays (option -o, key O); clean up log file implementation
Diffstat (limited to 'meme/overlay.c')
-rw-r--r--meme/overlay.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/meme/overlay.c b/meme/overlay.c
new file mode 100644
index 0000000..df3f3cf
--- /dev/null
+++ b/meme/overlay.c
@@ -0,0 +1,104 @@
+/*
+ * overlay.c - Overlay in Gnuplot format
+ *
+ * Written 2015 by Werner Almesberger
+ * Copyright 2015 Werner Almesberger
+ *
+ * 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 2 of the License, or
+ * (at your option) any later version.
+ */
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+
+#include "util.h"
+#include "overlay.h"
+
+
+struct point {
+ float x, y;
+ struct point *next;
+};
+
+struct path {
+ struct point *points;
+ struct path *next;
+};
+
+
+static struct path *paths = NULL;
+
+
+void overlay_draw(void (*moveto)(void *user, int x, int y),
+ void (*lineto)(void *user, int x, int y), void *user)
+{
+ const struct path *path;
+ const struct point *p;
+
+ for (path = paths; path; path = path->next) {
+ p = path->points;
+ moveto(user, p->x, p->y);
+ for (p = p->next; p; p = p->next)
+ lineto(user, p->x, p->y);
+ }
+}
+
+
+static void overlay_load_file(FILE *file)
+{
+ struct path *path = NULL;
+ struct point *p;
+ int lineno = 0;
+ char buf[1024];
+ int n;
+ double x, y, z;
+
+ while (fgets(buf, sizeof(buf), file)) {
+ lineno++;
+ if (*buf == '#')
+ continue;
+ n = sscanf(buf, "%lf %lf %lf\n", &x, &y, &z);
+ switch (n) {
+ case -1:
+ path = NULL;
+ continue;
+ case 2:
+ /* fall through */
+ case 3:
+ break;
+ default:
+ fprintf(stderr, "invalid data at line %d\n", lineno);
+ exit(1);
+ }
+
+ if (!path) {
+ path = alloc_type(struct path);
+ path->next = paths;
+ path->points = NULL;
+ paths = path;
+ }
+ p = alloc_type(struct point);
+ p->x = round(x * 1000);
+ p->y = round(y * 1000);
+ p->next = path->points;
+ path->points = p;
+ }
+}
+
+
+void overlay_load(const char *name)
+{
+ FILE *file;
+
+ file = fopen(name, "r");
+ if (!file) {
+ perror(name);
+ exit(1);
+ }
+ overlay_load_file(file);
+ fclose(file);
+}