summaryrefslogtreecommitdiff
path: root/meme/mesh.c
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2014-09-12 02:54:30 (GMT)
committerWerner Almesberger <werner@almesberger.net>2014-09-12 03:13:49 (GMT)
commit2bc5b2f3ab23a4f75bbb8c23cf437519bb332808 (patch)
treeae092628f305c449572ce3f5304e6b3164e77e68 /meme/mesh.c
parente41c8e654bbe68360aec22b35b424a8849947a98 (diff)
downloadmisc-2bc5b2f3ab23a4f75bbb8c23cf437519bb332808.zip
misc-2bc5b2f3ab23a4f75bbb8c23cf437519bb332808.tar.gz
misc-2bc5b2f3ab23a4f75bbb8c23cf437519bb332808.tar.bz2
measmesh/: renamed to meme/
Too much typing ;-)
Diffstat (limited to 'meme/mesh.c')
-rw-r--r--meme/mesh.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/meme/mesh.c b/meme/mesh.c
new file mode 100644
index 0000000..7f1d37c
--- /dev/null
+++ b/meme/mesh.c
@@ -0,0 +1,137 @@
+/*
+ * mesh.c - 3D mesh data structures
+ *
+ * Written 2014 by Werner Almesberger
+ * Copyright 2014 by 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 "util.h"
+#include "mesh.h"
+
+
+#define VHASH_TABLE (1 << 20)
+#define EHASH_TABLE (1 << 20)
+
+
+struct v {
+ struct vertex v;
+ struct v *next;
+};
+
+struct e {
+ struct edge e;
+ struct e *next;
+};
+
+
+static struct v *vhash[VHASH_TABLE];
+static struct e *ehash[EHASH_TABLE];
+
+
+struct edge *edges = NULL;
+struct facet *facets = NULL;
+
+
+#define VHASH(x, y, z) ((x) % VHASH_TABLE)
+#define EHASH(a, b) ((long) (a) % EHASH_TABLE)
+
+
+const struct vertex *vertex_add(int x, int y, int z)
+{
+ struct v **h = &vhash[VHASH(x, y, z)];
+ struct v *v;
+
+ for (v = *h; v; v = v->next)
+ if (v->v.x == x && v->v.y == y && v->v.z == z)
+ return &v->v;
+
+ v = alloc_type(struct v);
+ v->v.x = x;
+ v->v.y = y;
+ v->v.z = z;
+ v->next = *h;
+ *h = v;
+ return &v->v;
+}
+
+
+void vertex_foreach(bool (*fn)(struct vertex *v, void *user), void *user)
+{
+ struct v *v;
+ int i;
+
+ for (i = 0; i != VHASH_TABLE; i++)
+ for (v = vhash[i]; v; v = v->next)
+ if (fn(&v->v, user))
+ return;
+}
+
+
+void vertex_foreach_const(bool (*fn)(const struct vertex *v, void *user),
+ void *user)
+{
+ const struct v *v;
+ int i;
+
+ for (i = 0; i != VHASH_TABLE; i++)
+ for (v = vhash[i]; v; v = v->next)
+ if (fn(&v->v, user))
+ return;
+}
+
+
+static void edge_add(const struct vertex *a, const struct vertex *b)
+{
+ const struct vertex *tmp;
+ struct e **h;
+ struct e *e;
+
+ if (a > b) {
+ tmp = a;
+ a = b;
+ b = tmp;
+ }
+ h = &ehash[EHASH(a, b)];
+ for (e = *h; e; e = e->next)
+ if (e->e.a == a && e->e.b == b)
+ return;
+
+ e = alloc_type(struct e);
+ e->e.a = a;
+ e->e.b = b;
+ e->e.next = edges;
+ edges = &e->e;
+ e->next = *h;
+ *h = e;
+}
+
+
+const struct facet *facet_add(const struct vertex *a, const struct vertex *b,
+ const struct vertex *c)
+{
+ struct facet *f;
+
+ f = alloc_type(struct facet);
+ f->v[0] = a;
+ f->v[1] = b;
+ f->v[2] = c;
+
+ edge_add(a, b);
+ edge_add(b, c);
+ edge_add(c, a);
+
+ f->next = facets;
+ facets = f;
+ return f;
+}
+
+
+void mesh_init(void)
+{
+}