Basilisk CFD
Adaptive Cartesian mesh PDE framework
Loading...
Searching...
No Matches
xyz2kdt.c
Go to the documentation of this file.
1/** @file xyz2kdt.c
2 */
3/* Gerris - The GNU Flow Solver
4 * Copyright (C) 2010 National Institute of Water and Atmospheric Research
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <stdlib.h>
23#include <stdio.h>
24#include <assert.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <fcntl.h>
29#include <unistd.h>
30
31#if HAVE_GETOPT_H
32# include <getopt.h>
33#endif /* HAVE_GETOPT_H */
34
35#include "kdt.h"
36
38{
39 return 1;
40}
41
42static void progress (float complete, void * data)
43{
44#if 0 /* this doesn't work yet */
45 struct timeval * start = data, now;
47 double remaining = ((double) (now.tv_usec - start->tv_usec) +
48 1e6*(double) (now.tv_sec - start->tv_sec))*(1. - complete);
49 int hours = remaining/1e6/3600.;
50 int mins = remaining/1e6/60. - 60.*hours;
51 int secs = remaining/1e6 - 3600.*hours - 60.*mins;
52 fprintf (stderr, "\rxyz2kdt: %3.0f%% complete %02d:%02d:%02d remaining",
53 (complete > 1. ? 1. : complete)*100.,
54 hours, mins, secs);
55#else
56 fprintf (stderr, "\rxyz2kdt: %3.0f%% complete ",
57 (complete > 1. ? 1. : complete)*100.);
58#endif
59}
60
61int main (int argc, char * argv[])
62{
63 int c = 0, pagesize = 4096;
64 int verbose = 0;
65
66 /* parse options using getopt */
67 while (c != EOF) {
68#if HAVE_GETOPT_LONG
69 static struct option long_options[] = {
70 {"pagesize", required_argument, NULL, 'p'},
71 {"verbose", no_argument, NULL, 'v'},
72 {"help", no_argument, NULL, 'h'},
73 { NULL }
74 };
75 int option_index = 0;
76 switch ((c = getopt_long (argc, argv, "p:hv",
78#else /* not HAVE_GETOPT_LONG */
79 switch ((c = getopt (argc, argv, "p:hv"))) {
80#endif /* not HAVE_GETOPT_LONG */
81 case 'v': /* verbose */
82 verbose = 1;
83 break;
84 case 'p': /* pagesize */
86 break;
87 case 'h': /* help */
89 "Usage: xyz2kdt [OPTION] BASENAME\n"
90 "\n"
91 "Converts the x, y and z coordinates on standard input to a\n"
92 "2D-tree-indexed database suitable for use with the\n"
93 "terrain module of Gerris.\n"
94 "\n"
95 " -p N --pagesize=N sets the pagesize in bytes (default is 4096)\n"
96 " -v --verbose display progress bar\n"
97 " -h --help display this help and exit\n"
98 "\n"
99 "Report bugs to %s\n",
100 "popinet@users.sf.net");
101 return 0; /* success */
102 break;
103 case '?': /* wrong options */
104 fprintf (stderr, "Try `xyz2kdt -h' for more information.\n");
105 return 1; /* failure */
106 }
107 }
108
109 if (optind >= argc) { /* missing BASENAME */
110 fprintf (stderr,
111 "xyz2kdt: missing BASENAME\n"
112 "Try `xyz2kdt -h' for more information.\n");
113 return 1; /* failure */
114 }
115
116 if (verbose)
117 fprintf (stderr, "xyz2kdt: reading points...\r");
118 KdtHeap h;
119 kdt_heap_create (&h, kdt_tmpfile (), 0, -1, 1000000);
120 long n = 0;
121 KdtPoint p;
122 while (scanf ("%lf %lf %lf", &p.x, &p.y, &p.z) == 3) {
123 kdt_heap_put (&h, &p);
124 if (verbose && n % 3571 == 0)
125 fprintf (stderr, "xyz2kdt: reading points... %ld\r", n);
126 n++;
127 }
128 kdt_heap_flush (&h);
129 if (verbose)
130 fprintf (stderr, "xyz2kdt: 0%% complete ");
131
132 struct timeval start;
133 gettimeofday (&start, NULL);
134 Kdt * kdt = kdt_new ();
135 kdt_create (kdt, argv[optind], pagesize, &h, verbose ? progress : NULL, &start);
137
138 if (verbose) {
139 Kdt * kdt = kdt_new ();
140 KdtRect rect = {{-1e30,1e30},{-1e30,1e30}};
141 KdtSum sum;
142
143 kdt_sum_init (&sum);
145 long n = kdt_query_sum (kdt,
147 rect, &sum);
149 "\r%ld points Height min: %g average: %g max: %g\n",
150 n, sum.Hmin, sum.H0/sum.w, sum.Hmax);
152 }
153
154 return 0;
155}
scalar h[]
Definition atmosphere.h:6
int x
Definition common.h:76
#define p
Definition tree.h:320
#define assert(a)
Definition config.h:107
else return n
Definition curvature.h:101
FILE * kdt_tmpfile(void)
Definition kdt.c:42
void kdt_sum_init(KdtSum *s)
Definition kdt.c:1043
Kdt * kdt_new(void)
Definition kdt.c:632
long kdt_query_sum(const Kdt *kdt, KdtCheck includes, KdtCheck intersects, void *data, const KdtRect query, KdtSum *sum)
Definition kdt.c:1025
void kdt_destroy(Kdt *kdt)
Definition kdt.c:757
void kdt_heap_create(KdtHeap *h, FILE *fp, long start, long len, long buflen)
Definition kdt.c:134
int kdt_create(Kdt *kdt, const char *name, int blksize, KdtHeap *h, void(*progress)(float complete, void *data), void *data)
Definition kdt.c:686
void kdt_heap_flush(KdtHeap *h)
Definition kdt.c:233
int kdt_open(Kdt *kdt, const char *name)
Definition kdt.c:723
void kdt_heap_put(KdtHeap *h, KdtPoint *p)
Definition kdt.c:224
int(* KdtCheck)(const KdtRect rect, void *data)
Definition kdt.h:98
KdtInterval KdtRect[2]
Definition kdt.h:40
double * sum
#define data(k, l)
Definition linear.h:26
int progress
Definition qcc.c:60
Definition kdt.h:42
Definition kdt.h:32
Definition kdt.h:77
Definition kdt.c:358
scalar c
Definition vof.h:57
int main(int argc, char *argv[])
Definition xyz2kdt.c:61
static int includes_true(KdtRect rect)
Definition xyz2kdt.c:37