From cddedad752e93556fed4f96a2f1027a091d63e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Ne=C4=8Dada?= Date: Sat, 25 Aug 2018 12:13:10 +0300 Subject: [PATCH] hexagonal lattice generator (gives correct results, but valgrind issues some warnings). Former-commit-id: ea5ced6206d1a75dccb5e72ebb4775ca3d77bfc3 --- qpms/lattices.h | 16 ++++++-- qpms/lattices2d.c | 54 +++++++++++++++++++++++++++ tests/lattice/2d_triangular_lattice.c | 13 +++++++ 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/qpms/lattices.h b/qpms/lattices.h index c275613..df0d053 100644 --- a/qpms/lattices.h +++ b/qpms/lattices.h @@ -94,16 +94,26 @@ int triangular_lattice_gen_extend_to_r(triangular_lattice_gen_t *g, double r); int triangular_lattice_gen_extend_to_steps(triangular_lattice_gen_t *g, int maxsteps); void triangular_lattice_gen_free(triangular_lattice_gen_t *g); -#if 0 /* * HONEYCOMB LATTICE */ typedef struct { + // public: + points2d_rordered_t ps; + TriangularLatticeOrientation orientation; + double a; + double h; -} honeycomb_lattice_generator_t; + // private: + triangular_lattice_gen_t *tg; +} honeycomb_lattice_gen_t; -#endif +honeycomb_lattice_gen_t *honeycomb_lattice_gen_init_h(double h, TriangularLatticeOrientation ori); +honeycomb_lattice_gen_t *honeycomb_lattice_gen_init_a(double a, TriangularLatticeOrientation ori); +int honeycomb_lattice_gen_extend_to_steps(honeycomb_lattice_gen_t *g, int maxsteps); +int honeycomb_lattice_gen_extend_to_r(honeycomb_lattice_gen_t *g, double r); +void honeycomb_lattice_gen_free(honeycomb_lattice_gen_t *g); #endif // LATTICES_H diff --git a/qpms/lattices2d.c b/qpms/lattices2d.c index 51b6d28..7a9cda7 100644 --- a/qpms/lattices2d.c +++ b/qpms/lattices2d.c @@ -443,3 +443,57 @@ int triangular_lattice_gen_extend_to_steps(triangular_lattice_gen_t * g, int max return 0; } +honeycomb_lattice_gen_t *honeycomb_lattice_gen_init_h(double h, TriangularLatticeOrientation ori) { + double a = M_SQRT3 * h; + honeycomb_lattice_gen_t *g = honeycomb_lattice_gen_init_a(a, ori); + g->h = h; // maybe it's not necessary as sqrt is "exact" + return g; +} + +honeycomb_lattice_gen_t *honeycomb_lattice_gen_init_a(double a, TriangularLatticeOrientation ori) { + honeycomb_lattice_gen_t *g = calloc(1, sizeof(honeycomb_lattice_gen_t)); // this already inits g->ps to zeros + g->a = a; + g->h = a * M_1_SQRT3; + g->tg = triangular_lattice_gen_init(a, ori, true, 1); + return g; +} + +void honeycomb_lattice_gen_free(honeycomb_lattice_gen_t *g) { + free(g->ps.rs); + free(g->ps.base); + free(g->ps.r_offsets); + triangular_lattice_gen_free(g->tg); + free(g); +} + +int honeycomb_lattice_gen_extend_to_steps(honeycomb_lattice_gen_t *g, const int maxsteps) { + if (maxsteps <= g->tg->priv->maxs) // nothing needed + return 0; + triangular_lattice_gen_extend_to_steps(g->tg, maxsteps); + + double *newmem = realloc(g->ps.rs, g->tg->ps.nrs * sizeof(double)); + if (NULL != newmem) + g->ps.rs = newmem; + else abort(); + ptrdiff_t *newmem2 = realloc(g->ps.r_offsets, (g->tg->ps.nrs+1) * sizeof(ptrdiff_t)); + if (NULL != newmem2) + g->ps.r_offsets = newmem2; + else abort(); + point2d *newmem3 = realloc(g->ps.base, 2 * (g->tg->ps.r_offsets[g->tg->ps.nrs]) * sizeof(point2d)); + if (NULL != newmem3) + g->ps.base = newmem3; + else abort(); + + // Now copy (new) contents of g->tg->ps into g->ps, but with inverse copy of each point + for (size_t ri = g->ps.nrs; ri <= g->tg->ps.nrs; ++ri) + g->ps.r_offsets[ri] = g->tg->ps.r_offsets[ri] * 2; + for (ptrdiff_t i_orig = g->tg->ps.r_offsets[g->ps.nrs]; i_orig < g->tg->ps.r_offsets[g->tg->ps.nrs]; ++i_orig) { + point2d p = g->tg->ps.base[i_orig]; + g->ps.base[2*i_orig] = p; + p.x *= -1; p.y *= -1; + g->ps.base[2*i_orig + 1] = p; + } + g->ps.nrs = g->tg->ps.nrs; + return 0; +} + diff --git a/tests/lattice/2d_triangular_lattice.c b/tests/lattice/2d_triangular_lattice.c index ee7079d..1d36f7b 100644 --- a/tests/lattice/2d_triangular_lattice.c +++ b/tests/lattice/2d_triangular_lattice.c @@ -51,6 +51,19 @@ int main() { dump_points2d_rordered(p, "triang_v_minus_s7_scaled_out"); points2d_rordered_free(p); + honeycomb_lattice_gen_t *h = honeycomb_lattice_gen_init_h(1, TRIANGULAR_HORIZONTAL); + dump_points2d_rordered(&(h->ps), "hex_h_empty.out"); + honeycomb_lattice_gen_extend_to_steps(h, 7); + dump_points2d_rordered(&(h->ps), "hex_h_s7.out"); + honeycomb_lattice_gen_extend_to_steps(h, 120); + dump_points2d_rordered(&(h->ps), "hex_h_s120.out"); + honeycomb_lattice_gen_free(h); + + h = honeycomb_lattice_gen_init_a(1, TRIANGULAR_VERTICAL); + honeycomb_lattice_gen_extend_to_steps(h, 5); + dump_points2d_rordered(&(h->ps), "hex_v_s5.out"); + honeycomb_lattice_gen_free(h); + return 0; }