summaryrefslogtreecommitdiff
path: root/meme
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2014-09-12 14:20:43 -0300
committerWerner Almesberger <werner@almesberger.net>2014-09-12 14:20:43 -0300
commit7b7435012f7e510e08662463b0865327824e2ab9 (patch)
tree374a4071ce178b07eb1c1030ed191d939307bf03 /meme
parent29baa2745d7408a8bbe6088807e3467a11120855 (diff)
downloadmisc-7b7435012f7e510e08662463b0865327824e2ab9.tar.gz
misc-7b7435012f7e510e08662463b0865327824e2ab9.tar.bz2
misc-7b7435012f7e510e08662463b0865327824e2ab9.zip
meme/: abstract iteration over edges (edge_foreach)
This way, we can later remove "struct edge".
Diffstat (limited to 'meme')
-rw-r--r--meme/gui.c34
-rw-r--r--meme/main.c28
-rw-r--r--meme/mesh.c13
-rw-r--r--meme/mesh.h5
4 files changed, 55 insertions, 25 deletions
diff --git a/meme/gui.c b/meme/gui.c
index 8b1476f..93a5ad4 100644
--- a/meme/gui.c
+++ b/meme/gui.c
@@ -113,23 +113,29 @@ static void surface_draw(SDL_Surface *s)
}
-static void edges_draw(SDL_Surface *s)
+static bool edge_draw(const struct vertex *a, const struct vertex *b,
+ void *user)
{
- const struct edge *e;
+ SDL_Surface *s = user;
Sint16 xa, ya, xb, yb;
- int a, b;
+ int res_a, res_b;
- for (e = edges; e; e = e->next) {
- a = coord(e->a->x, e->a->y, &xa, &ya);
- if (a < 0)
- continue;
- b = coord(e->b->x, e->b->y, &xb, &yb);
- if (b < 0)
- continue;
- if (!(a || b))
- continue;
- aalineColor(s, xa, ya, xb, yb, 0x000000ff);
- }
+ res_a = coord(a->x, a->y, &xa, &ya);
+ if (res_a < 0)
+ return 0;
+ res_b = coord(b->x, b->y, &xb, &yb);
+ if (res_b < 0)
+ return 0;
+ if (!(res_a || res_b))
+ return 0;
+ aalineColor(s, xa, ya, xb, yb, 0x000000ff);
+ return 0;
+}
+
+
+static void edges_draw(SDL_Surface *s)
+{
+ edge_foreach(edge_draw, s);
}
diff --git a/meme/main.c b/meme/main.c
index eccd692..3a70a56 100644
--- a/meme/main.c
+++ b/meme/main.c
@@ -22,6 +22,25 @@
extern void gui(void);
+static bool count_edge(const struct vertex *a, const struct vertex *b,
+ void *user)
+{
+ unsigned *count = user;
+
+ (*count)++;
+ return 0;
+}
+
+
+static unsigned count_edges(void)
+{
+ unsigned count = 0;
+
+ edge_foreach(count_edge, &count);
+ return count;
+}
+
+
static void usage(const char *name)
{
fprintf(stderr, "usage: %s\n", name);
@@ -44,16 +63,9 @@ int main(int argc, char **argv)
}
fprintf(stderr, "STL loaded in %.3f s\n", time_ms(&t_load)/1000.0);
+ fprintf(stderr, "%u edges\n", count_edges());
gui();
-{
- const struct edge *e;
- int n = 0;
-
- for (e = edges; e; e = e->next)
- n++;
- printf("%d edges\n", n);
-}
return 0;
}
diff --git a/meme/mesh.c b/meme/mesh.c
index 39ee10c..9195885 100644
--- a/meme/mesh.c
+++ b/meme/mesh.c
@@ -36,7 +36,7 @@ static struct v *vhash[VHASH_TABLE];
static struct e *ehash[EHASH_TABLE];
-struct edge *edges = NULL;
+static struct edge *edges = NULL;
struct facet *facets = NULL;
@@ -104,6 +104,17 @@ void vertex_foreach_const(bool (*fn)(const struct vertex *v, void *user),
}
+void edge_foreach(bool (*fn)(const struct vertex *a, const struct vertex *b,
+ void *user), void *user)
+{
+ const struct edge *e;
+
+ for (e = edges; e; e = e->next)
+ if (fn(e->a, e->b, user))
+ return;
+}
+
+
static void edge_add(const struct vertex *a, const struct vertex *b)
{
const struct vertex *tmp;
diff --git a/meme/mesh.h b/meme/mesh.h
index 1927147..d2efc73 100644
--- a/meme/mesh.h
+++ b/meme/mesh.h
@@ -25,7 +25,6 @@ struct vertex {
struct edge {
const struct vertex *a, *b;
- bool drawn;
struct edge *next;
};
@@ -36,7 +35,6 @@ struct facet {
};
-extern struct edge *edges;
extern struct facet *facets;
@@ -46,6 +44,9 @@ void vertex_foreach(bool (*fn)(struct vertex *v, void *user), void *user);
void vertex_foreach_const(bool (*fn)(const struct vertex *v, void *user),
void *user);
+void edge_foreach(bool (*fn)(const struct vertex *a, const struct vertex *b,
+ void *user), void *user);
+
const struct facet *facet_add(const struct vertex *a, const struct vertex *b,
const struct vertex *c);