35#define MIN(a,b) ((a) < (b) ? (a) : (b))
36#define MAX(a,b) ((a) > (b) ? (a) : (b))
38#define VERSION 20120405
44 char name[] =
"kdtXXXXXX";
94 if (
h->len ==
h->buflen) {
108 if (
ftell (
h->fp) !=
h->current)
111 long maxlen =
h->start +
h->len -
h->current/
sizeof (
KdtPoint);
125 if (
ftell (
h->fp) !=
h->current)
138 if (len > 0 && len < buflen)
159 if (
h->len ==
h->buflen) {
173 if (
h->len ==
h->buflen &&
h->
i >=
h->len)
179 if (
h->end <
h->buflen)
189 if (
h1->len ==
h1->buflen) {
192 h2->len =
h1->len - len1;
193 h2->buflen =
h2->len;
195 h2->p = &
h1->p[len1];
205 if (len1 >
h1->buflen)
212 for (
i = 0;
i < len1;
i++) {
226 if (
h->
i ==
h->buflen) {
266 int (*
compar) (
const void *,
const void *),
298 return (
double) (end->tv_usec - start->tv_usec) +
299 1
e6*(
double) (end->tv_sec - start->tv_sec);
304 int (*
compar) (
const void *,
const void *),
311 if (
h->len ==
h->buflen) {
318 long buflen =
h->buflen;
335 while (len > buflen) {
370#if (!defined (__LP64__) && !defined (__64BIT__) && \
371 !defined (_LP64) && !(__WORDSIZE == 64))
372 long maxlen = (1 << 31)/
sizeof (
KdtPoint);
373 if (
kdt->h.len > maxlen) {
374 fprintf (
stderr,
"kdt: 32-bits systems are limited to %ld data points\n",
384 *nodes =
n->n1*
sizeof (
Node);
394 if (((
double)
rect[1].h) - ((
double)
rect[1].l) > *
h)
401 double p[3],
o[2],
h;
405 p[0] = (
a->
x -
o[0])/
h;
p[1] = (
a->
y -
o[1])/
h;
p[2] =
a->z;
409 if (
p[2] <
sum->Hmin)
sum->Hmin =
p[2];
410 if (
p[2] >
sum->Hmax)
sum->Hmax =
p[2];
434 if (
p[2] <
sum->Hmin)
sum->Hmin =
p[2];
435 if (
p[2] >
sum->Hmax)
sum->Hmax =
p[2];
447 return w >
h ?
w :
h;
452 fprintf (
fp,
"%f %f\n%f %f\n%f %f\n%f %f\n%f %f\n\n",
489 for (
i = 1;
i <
h->len;
i++) {
522 if (
kdt->progress &&
kdt->m > 0)
551 if (
h1->len >
kdt->h.np) {
553 int nindex = (bound[0].h - bound[0].l < bound[1].h - bound[1].l);
592 double a =
area (bound);
601 *coverage =
s.coverage;
641 char *
fname =
malloc (
sizeof(
char)*(len + len1 + 1));
677 kdt->h.bound[0].l =
kdt->h.bound[1].l = 1
e30;
678 kdt->h.bound[0].h =
kdt->h.bound[1].h = -1
e30;
698 kdt->h.bound[0] = bound[0];
699 kdt->h.bound[1] = bound[1];
707 while (len >
kdt->h.np) {
742 "kdt: incompatible version number. Use:\n"
744 "to convert to the new format.\n",
784 if (len >
kdt->h.np) {
822 for (
i = 0;
i < len;
i++)
924 if (
a->Hmin <
sum->Hmin)
sum->Hmin =
a->Hmin;
925 if (
a->Hmax >
sum->Hmax)
sum->Hmax =
a->Hmax;
938 if (len >
kdt->h.np) {
959 f->np +=
sizeof (
Node);
1005 for (
i = 0;
i < len;
i++,
a++) {
define m((k)==0 &&(l)==0 &&(m)==0) macro2 foreach_point(double _x=0.
scalar f[]
The primary fields are:
define sysmalloc malloc define syscalloc calloc define sysrealloc realloc define sysfree free define systrdup strdup define line calloc(n, s) @ define prealloc(p
define sysmalloc malloc define syscalloc calloc define sysrealloc realloc define sysfree free define systrdup strdup define line line line line op define op
#define intersection(a, b)
void includes(int argc, char **argv, char **grid1, int *default_grid, int *dim, int *bg, int *lyrs, int *gpus, const char *dir)
void kdt_heap_resize(KdtHeap *h, long len)
static void fwrite_check(const void *ptr, size_t size, size_t nmemb, FILE *stream)
static void relative(const KdtRect rect, double *o, double *h)
static void kdt_heap_sort(KdtHeap *h, int(*compar)(const void *, const void *), void(*progress)(void *), void *data)
void kdt_sum_init(KdtSum *s)
static void union_bound(KdtRect b, const KdtRect b1, const KdtRect b2)
static int kdt_init(Kdt *kdt, const char *name, int npmax, long len)
static Buffer * buffer_ref(Buffer *b)
static float area(const KdtRect rect)
long kdt_query_sum(const Kdt *kdt, KdtCheck includes, KdtCheck intersects, void *data, const KdtRect query, KdtSum *sum)
static long query_sum(const Kdt *kdt, KdtCheck includes, KdtCheck intersects, void *data, KdtRect bound, long len, FilePointers *f, const KdtRect query, KdtSum *sum)
static Buffer * buffer_new(long len)
void kdt_rect_write(const KdtRect rect, FILE *fp)
long kdt_query(const Kdt *kdt, const KdtRect rect)
void kdt_destroy(Kdt *kdt)
static void sum_add_sum(const KdtRect parent, KdtSum *sum, const KdtRect rect, const KdtSumCore *a)
void kdt_heap_split(KdtHeap *h1, long len1, KdtHeap *h2)
static int sort_y(const void *p1, const void *p2)
void kdt_heap_create(KdtHeap *h, FILE *fp, long start, long len, long buflen)
static int kdt_heap_sort_cost(long len, long buflen)
static int update_sum(const KdtRect rect, KdtSumCore *n, KdtHeap *h, int index)
static long update_bounds(KdtRect rect, KdtHeap *h)
int kdt_intersects(const KdtRect rect, const KdtRect query)
static void sizes(const Node *n, long *nodes, long *sums, long *leaves)
static int put(KdtHeap *h, KdtPoint *p, KdtHeap *merged)
int kdt_includes(const KdtRect rect, const KdtRect query)
static long query(const Kdt *kdt, const KdtRect rect, long len)
void kdt_heap_rewind(KdtHeap *h)
static FILE * open_ext(const char *name, const char *ext, const char *mode)
static void merge(KdtHeap *h1, KdtHeap *h2, int(*compar)(const void *, const void *), long buflen)
static long heap_read(KdtHeap *h, long len)
static int check_32_bits(const Kdt *kdt)
void kdt_write(KdtHeap *h, FILE *fp)
void kdt_heap_free(KdtHeap *h)
int kdt_create(Kdt *kdt, const char *name, int blksize, KdtHeap *h, void(*progress)(float complete, void *data), void *data)
int kdt_heap_get(KdtHeap *h, KdtPoint *p)
static float length(const KdtRect rect)
static void kdt_sum_core_init(KdtSumCore *s)
void kdt_heap_flush(KdtHeap *h)
static int sort_x(const void *p1, const void *p2)
int kdt_open(Kdt *kdt, const char *name)
void kdt_heap_put(KdtHeap *h, KdtPoint *p)
static int split(KdtHeap *h1, KdtRect bound, int index, Kdt *kdt, float *coverage)
static float intersection_area(const KdtRect rect1, const KdtRect rect2)
static void heap_write(KdtHeap *h, long len)
static void buffer_unref(Buffer *b)
static void sum_add_point(const KdtRect parent, KdtSumCore *sum, const KdtPoint *a, double w)
int(* KdtCheck)(const KdtRect rect, void *data)
void(* progress)(float complete, void *data)
static int intersects(KdtRect rect, Point *p)
define n sizeof(Cell))) @define fine(a