From ca454669d1984c0bb6729b4d9fab9c1730b8d11a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ne=C4=8Dada?= Date: Wed, 22 Aug 2018 19:22:11 +0300 Subject: [PATCH] Dudom doopravdy Former-commit-id: bc76e84f71750f47f07a3a08bf49044dc9f1ae70 --- qpms/lattices2d.c | 82 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 75 insertions(+), 7 deletions(-) diff --git a/qpms/lattices2d.c b/qpms/lattices2d.c index 07c3c68..da2e875 100644 --- a/qpms/lattices2d.c +++ b/qpms/lattices2d.c @@ -3,7 +3,7 @@ typedef struct { int i, j; -} intcoord2d; +} intcoord2_t; static inline int sqi(int x) { return x*x; } @@ -77,6 +77,15 @@ static inline int trilat_r2_ij(const int i, const int j) { return sqi(i) + sqi(j) + i*j; } +static inline int trilat_r2_coord(const intcoord2_t c) { + return trilat_r2_ij(c.i, c.j); +} + +static int trilat_cmp_intcoord2_by_r2(const void *p1, const void *p2) { + // CHECK the sign is right + return trilat_r2_coord(*(const intcoord2_t *)p1) - trilat_r2_coord(*(const intcoord2_t *)p2); +} + // Classify points into sextants (variant [a]) static int trilat_sextant_ij_a(const int i, const int j) { const int w = i + j; @@ -90,10 +99,18 @@ static int trilat_sextant_ij_a(const int i, const int j) { assert(0); // other options should be impossible } - - typedef struct { - TODO; + intcoord2_t *pointlist_base; // allocated memory for the point "buffer" + size_t pointlist_capacity; + // beginning and end of the point "buffer" + // not 100% sure what type should I use here + // (these are both relative to pointlist_base, due to possible realloc's) + ptrdiff_t pointlist_beg, pointlist_end; + int maxs; // the highest layer of the spider web generated (-1 by init, 0 is only origin (if applicable)) + // capacities of the arrays in ps + size_t ps_rs_capacity; + size_t ps_points_capacity; // this is the "base" array + // TODO anything else? } triangular_lattice_gen_t_privstuff_t; triangular_lattice_gen_t * triangular_lattice_gen_init(double a, TriangularLatticeOrientation ori, bool include_origin) @@ -103,11 +120,62 @@ triangular_lattice_gen_t * triangular_lattice_gen_init(double a, TriangularLatti g->includes_origin = include_origin; g->ps.nrs = 0; g->ps.rs = NULL; - g->ps.points_at_r = NULL; + g->ps.base = NULL; + g->ps.r_offsets = NULL; g->priv = malloc(sizeof(triangular_lattice_gen_privstuff_t)); - TODO; - + g->priv->maxs = -1; + g->priv->pointlist_capacity = 0; + g->priv->pointlist_base = NULL; + g->priv->pointlist_beg = 0; + g->priv->pointlist_end = 0; + g->priv->ps_rs_capacity = 0; + g->priv->ps_points_capacity = 0; return g; } +void triangular_lattice_gen_free(triangular_lattice_get_t *g) { + free(g->ps.rs); + free(g->ps.base); + free(g->ps.r_offsets); + free(g->priv->pointlist_base); + free(g->priv); + free(g); +} + +const points2d_reordered_t * triangular_lattice_gen_getpoints(const triangular_lattice_generator_t *g) { + return &(g->ps); +} + +int triangular_lattice_gen_extend_to_steps(triangular_lattice_generator_t * g, int maxsteps) { + if (maxsteps <= g->priv->maxs) // nothing needed + return 0; + // TODO FIXME: check for maximum possible maxsteps (not sure what it is) + int err; + err = trilatgen_ensure_pointlist_capacity(g, maxsteps); + if(err) return err; + err = trilatgen_ensure_ps_rs_capacity(g, maxsteps); + if(err) return err; + err = trilatgen_ensure_ps_points_capacity(g, maxsteps); + if(err) return err; + + if(g->includes_origin && g->priv->maxs < 0) // Add origin if not there yet + trilatgen_append_ij(g, 0, 0); + + for (int s = g->priv->maxs + 1; s <= maxsteps; ++s) { + int i, j; + // now go along the spider web layer as indicated in the lenghthy comment above + for (i = s, j = 0; i > 0; --i; ++j) trilatgen_append_ij(g,i,j); + for (i = 0, j = s; i + j > 0; --i) trilatgen_append_ij(g,i,j); + for (i = -s, j = s; j > 0; --j) trilatgen_append_ij(g,i,j); + for (i = -s, j = 0; i < 0; ++i, --j) trilatgen_append_ij(g,i,j); + for (i = 0, j = -s; i + j < 0; ++i) trilatgen_append_ij(g,i,j); + for (i = s, j = -s; j < 0; ++j) trilatgen_append_ij(g,i,j); + } + + trilatgen_sort_pointlist(g); + + // TODO doma a ted je potřeba vytahat potřebný počet bodů z fronty a naflákat je do ps. + + +