From 6b7cecaf5f58f5d564b4d2d91ffd178f1eb92074 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 8 Jan 2015 09:12:05 -0300 Subject: [PATCH] slicer/slice.c, slice.h: optionally draw a box around the object --- slicer/slice.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++-- slicer/slice.h | 2 +- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/slicer/slice.c b/slicer/slice.c index cdc05aa..465eea7 100644 --- a/slicer/slice.c +++ b/slicer/slice.c @@ -38,6 +38,35 @@ struct z { static GTree *tree; +/* ----- Bounding box ------------------------------------------------------ */ + + +static bool first = 1; +static float min_x, max_x, min_y, max_y; + + +static void bbox(float x, float y) +{ + if (first) { + min_x = max_x = x; + min_y = max_y = y; + first = 0; + } else { + if (x < min_x) + min_x = x; + if (y < min_y) + min_y = y; + if (x > max_x) + max_x = x; + if (y > max_y) + max_y = y; + } +} + + +/* ----- Data collection --------------------------------------------------- */ + + static inline bool eq(float a, float b) { return fabsf(a - b) < 1e-6; @@ -91,6 +120,9 @@ fprintf(stderr, "zero point\n"); exit(1); } + bbox(a->x, a->y); + bbox(b->x, b->y); + z = get_z(a->z); line = alloc_type(struct line); line->ax = a->x; @@ -152,9 +184,30 @@ static gboolean dump_range(gpointer key, gpointer value, gpointer data) } -void slice_dump(void) +static gboolean dump_box(gpointer key, gpointer value, gpointer data) +{ + const struct z *z = value; + float box = *(float *) data; + + if (!z->present) + return 0; + + printf("%f %f %f\n%f %f %f\n%f %f %f\n%f %f %f\n%f %f %f\n\n\n", + min_x - box, min_y - box, z->z, + max_x + box, min_y - box, z->z, + max_x + box, max_y + box, z->z, + min_x - box, max_y + box, z->z, + min_x - box, min_y - box, z->z); + + return 0; +} + + +void slice_dump(float box) { g_tree_foreach(tree, dump_range, NULL); + if (box && !first) + g_tree_foreach(tree, dump_box, &box); } @@ -167,7 +220,6 @@ static gint comp(gconstpointer a, gconstpointer b) const float *zb = b; return eq(*za, *zb) ? 0 : *za < *zb ? -1 : 1; - } diff --git a/slicer/slice.h b/slicer/slice.h index fbaa9ac..1772b61 100644 --- a/slicer/slice.h +++ b/slicer/slice.h @@ -18,7 +18,7 @@ void slice(struct v f[3]); -void slice_dump(void); +void slice_dump(float box); void slice_init(void); #endif /* !SLICE_H */