Root/
| 1 | /* |
| 2 | * slice.c - Generate slices |
| 3 | * |
| 4 | * Written 2015 by Werner Almesberger |
| 5 | * Copyright 2015 by Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdbool.h> |
| 15 | #include <math.h> |
| 16 | #include <glib.h> |
| 17 | |
| 18 | #include "util.h" |
| 19 | #include "stl.h" |
| 20 | #include "slice.h" |
| 21 | |
| 22 | |
| 23 | struct facet { |
| 24 | struct v a, b, c; |
| 25 | struct facet *next; |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | struct z { |
| 30 | float z; |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | static GTree *tree; |
| 35 | static struct facet *facets = NULL; |
| 36 | |
| 37 | |
| 38 | /* ----- Bounding box ------------------------------------------------------ */ |
| 39 | |
| 40 | |
| 41 | static bool first = 1; |
| 42 | static float min_x, max_x, min_y, max_y; |
| 43 | |
| 44 | |
| 45 | static void bbox(float x, float y) |
| 46 | { |
| 47 | if (first) { |
| 48 | min_x = max_x = x; |
| 49 | min_y = max_y = y; |
| 50 | first = 0; |
| 51 | } else { |
| 52 | if (x < min_x) |
| 53 | min_x = x; |
| 54 | if (y < min_y) |
| 55 | min_y = y; |
| 56 | if (x > max_x) |
| 57 | max_x = x; |
| 58 | if (y > max_y) |
| 59 | max_y = y; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* ----- Data collection --------------------------------------------------- */ |
| 65 | |
| 66 | |
| 67 | static inline bool eq(float a, float b) |
| 68 | { |
| 69 | return fabsf(a - b) < 1e-5; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static void mark_z(float z) |
| 74 | { |
| 75 | struct z *e; |
| 76 | |
| 77 | e = g_tree_lookup(tree, &z); |
| 78 | if (e) |
| 79 | return; |
| 80 | e = alloc_type(struct z); |
| 81 | e->z = z; |
| 82 | g_tree_insert(tree, &e->z, e); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static void add(const struct v *a, const struct v *b, const struct v *c) |
| 87 | { |
| 88 | float cross; |
| 89 | struct facet *f; |
| 90 | |
| 91 | cross = (b->x - a->x) * (c->y - a->y) - (b->y - a->y) * (c->x - a->x); |
| 92 | if (!eq(cross, 0)) { |
| 93 | fprintf(stderr, |
| 94 | "inclined facet\n\t%f %f %f\n\t%f %f %f\n\t%f %f %f\n", |
| 95 | a->x, a->y, a->z, b->x, b->y, b->z, c->x, c->y, c->z); |
| 96 | exit(1); |
| 97 | } |
| 98 | |
| 99 | bbox(a->x, a->y); |
| 100 | bbox(b->x, b->y); |
| 101 | bbox(c->x, c->y); |
| 102 | |
| 103 | f = alloc_type(struct facet); |
| 104 | f->a = *a; |
| 105 | f->b = *b; |
| 106 | f->c = *c; |
| 107 | f->next = facets; |
| 108 | facets = f; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | void slice(struct v f[3]) |
| 113 | { |
| 114 | bool ab = eq(f[0].z, f[1].z); |
| 115 | bool bc = eq(f[1].z, f[2].z); |
| 116 | |
| 117 | if (ab && bc) { |
| 118 | mark_z(f[0].z); |
| 119 | return; |
| 120 | } |
| 121 | add(f + 0, f + 1, f + 2); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /* ----- Insert intermediate layers ---------------------------------------- */ |
| 126 | |
| 127 | |
| 128 | struct ctx { |
| 129 | float z_step; |
| 130 | bool first; |
| 131 | float z0; /* only defined if !first */ |
| 132 | bool more; /* returned due to having made changes */ |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | static gboolean check_z(gpointer key, gpointer value, gpointer data) |
| 137 | { |
| 138 | const struct z *z = value; |
| 139 | struct ctx *ctx = data; |
| 140 | float d; |
| 141 | int n, i; |
| 142 | |
| 143 | if (ctx->first) { |
| 144 | ctx->first = 0; |
| 145 | ctx->z0 = z->z; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | d = z->z - ctx->z0; |
| 150 | if (d <= ctx->z_step) { |
| 151 | ctx->z0 = z->z; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | n = (d / ctx->z_step) + 1; |
| 156 | for (i = 1; i < n; i++) |
| 157 | mark_z(ctx->z0 + d / n * i); |
| 158 | |
| 159 | ctx->more = 1; |
| 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | void slice_intermediate(float z_step) |
| 165 | { |
| 166 | struct ctx ctx = { |
| 167 | .z_step = z_step, |
| 168 | }; |
| 169 | |
| 170 | do { |
| 171 | ctx.first = 1; |
| 172 | ctx.more = 0; |
| 173 | g_tree_foreach(tree, check_z, &ctx); |
| 174 | } |
| 175 | while (ctx.more); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* ----- Dumping ----------------------------------------------------------- */ |
| 180 | |
| 181 | |
| 182 | static bool cut(const struct v *a, const struct v *b, float z, struct v *res) |
| 183 | { |
| 184 | float dx, dy, dz; |
| 185 | float f; |
| 186 | |
| 187 | if (a->z > b->z) |
| 188 | return cut(b, a, z, res); |
| 189 | if (eq(a->z, b->z)) |
| 190 | return 0; |
| 191 | if (a->z > z || b->z < z || eq(b->z, z)) |
| 192 | return 0; |
| 193 | |
| 194 | dx = b->x - a->x; |
| 195 | dy = b->y - a->y; |
| 196 | dz = b->z - a->z; |
| 197 | |
| 198 | f = (z - a->z) / dz; |
| 199 | |
| 200 | res->x = f * dx + a->x; |
| 201 | res->y = f * dy + a->y; |
| 202 | res->z = z; |
| 203 | |
| 204 | return 1; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | static gboolean dump_layer(gpointer key, gpointer value, gpointer data) |
| 209 | { |
| 210 | const struct z *z = value; |
| 211 | const struct facet *f; |
| 212 | struct v p1, p2; |
| 213 | |
| 214 | for (f = facets; f; f = f->next) { |
| 215 | if (cut(&f->a, &f->b, z->z, &p1)) { |
| 216 | if (!cut(&f->a, &f->c, z->z, &p2) && |
| 217 | !cut(&f->b, &f->c, z->z, &p2)) |
| 218 | continue; |
| 219 | } else if (!cut(&f->a, &f->c, z->z, &p1) || |
| 220 | !cut(&f->b, &f->c, z->z, &p2)) { |
| 221 | continue; |
| 222 | } |
| 223 | printf("%f %f %f\n%f %f %f\n\n\n", |
| 224 | p1.x, p1.y, z->z, p2.x, p2.y, z->z); |
| 225 | |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | static gboolean dump_box(gpointer key, gpointer value, gpointer data) |
| 232 | { |
| 233 | const struct z *z = value; |
| 234 | float box = *(float *) data; |
| 235 | |
| 236 | printf("%f %f %f\n%f %f %f\n%f %f %f\n%f %f %f\n%f %f %f\n\n\n", |
| 237 | min_x - box, min_y - box, z->z, |
| 238 | max_x + box, min_y - box, z->z, |
| 239 | max_x + box, max_y + box, z->z, |
| 240 | min_x - box, max_y + box, z->z, |
| 241 | min_x - box, min_y - box, z->z); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void slice_dump(float box) |
| 248 | { |
| 249 | g_tree_foreach(tree, dump_layer, NULL); |
| 250 | if (box && !first) |
| 251 | g_tree_foreach(tree, dump_box, &box); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* ----- Initialization ---------------------------------------------------- */ |
| 256 | |
| 257 | |
| 258 | static gint comp(gconstpointer a, gconstpointer b) |
| 259 | { |
| 260 | const float *za = a; |
| 261 | const float *zb = b; |
| 262 | |
| 263 | return eq(*za, *zb) ? 0 : *za < *zb ? -1 : 1; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | void slice_init(void) |
| 268 | { |
| 269 | tree = g_tree_new(comp); |
| 270 | } |
| 271 |
Branches:
master
