/* See pz_i2_chain.h */ /* Last edited on 2023-02-12 07:49:24 by stolfi */ #include #include #include #include #include #include pz_i2_chain_t pz_i2_chain_cut (pz_i2_chain_t *c, int start, int length) { pz_i2_chain_t x = i2_vec_new(length); pz_i2_chain_do_cut(c, start, length, &x); return x; } void pz_i2_chain_do_cut (pz_i2_chain_t *c, int start, int length, pz_i2_chain_t *x) { int N = (c->nel); int ini = start % N; int fin = ini + length - 1; int i; if (fin < N) { for(i = 0; i < length; i++) { x->el[i] = c->el[ini+i]; } } else { int NA = N-ini, NB = (fin+1) % N; for(i = 0; i < NA; i++) { x->el[i] = c->el[ini+i]; } for(i = 0; i < NB; i++) { x->el[N-ini+i] = c->el[i]; } } } void pz_i2_chain_reverse (pz_i2_chain_t *c) { i2_t aux; int last = c->nel - 1, mid = last/2; int i; for (i = 0; i <= mid; i++) { aux = c->el[i]; c->el[i] = c->el[last-i]; c->el[last-i] = aux; }; } pz_i2_chain_t pz_i2_chain_extract_segment (pz_i2_chain_t *c, pz_segment_t *s) { affirm(c->nel == s->tot, "bad seg"); pz_i2_chain_t x = i2_vec_new(s->ns); pz_i2_chain_do_extract_segment(c, s, &x); return x; } void pz_i2_chain_do_extract_segment (pz_i2_chain_t *c, pz_segment_t *s, pz_i2_chain_t *x) { affirm(c->nel == s->tot, "bad seg"); pz_i2_chain_do_cut(c, s->ini, s->ns, x); if (s->rev){ pz_i2_chain_reverse(x); } } #define FileVersion "97-01-21" pz_i2_chain_read_data pz_i2_chain_read (FILE *rd) { pz_i2_chain_read_data data; filefmt_read_header(rd, "pz_i2_chain_t", FileVersion); data.cmt = filefmt_read_comment(rd, '|'); int N = nget_int32(rd, "points"); fget_eol(rd); data.c = i2_vec_new(N); int i; for (i = 0; i < N ; i++) { int x = fget_int32(rd); fget_skip_spaces(rd); int y = fget_int32(rd); fget_skip_spaces(rd); data.c.el[i] = (i2_t){{x, y}}; fget_eol(rd); } filefmt_read_footer(rd, "pz_i2_chain_t"); return data; } void pz_i2_chain_write (FILE *wr, char *cmt, pz_i2_chain_t *c) { filefmt_write_header(wr, "pz_i2_chain_t", FileVersion); int ind = 0; /* Comment indentation. */ filefmt_write_comment(wr, cmt, ind, '|'); fprintf(wr, "length = %d\n", c->nel); int i; for (i = 0; i < c->nel ; i++) { i2_t *pi = &(c->el[i]); fprintf(wr, " %d %d\n", pi->c[0], pi->c[1]); } filefmt_write_footer(wr, "pz_i2_chain_t"); fflush(wr); } /* Copyright © 2001 Universidade Estadual de Campinas (UNICAMP). Authors: Helena C. G. Leitão and Jorge Stolfi. This file can be freely distributed, used, and modified, provided that this copyright and authorship notice is preserved, and that any modified versions are clearly marked as such. This software has NO WARRANTY of correctness or applicability for any purpose. Neither the authors nor their employers chall be held responsible for any losses or damages that may result from its use. */