summaryrefslogtreecommitdiff
path: root/header.h
blob: 172649c83be4b5478ab9f2b9cb2ecafb124f18e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#ifdef SINGLE
#define REAL float
#else
#define REAL double
#endif

#include "triangle.h"

#ifndef DEBUG_IO
#define DEBUG_IO 0
#endif

#ifndef VERBOSE
#define VERBOSE 0
#endif

/*
 * ----------------------------------------------------------------------
 * wavelet functions
 * ----------------------------------------------------------------------
 */

int get_metadata(char *datafilename, int *nrow, int *ncol, double *xllcorner, double *yllcorner, double *cellsize, double *nodata);

int get_data(char *datafilename, int nrow, int ncol, int ncol_padded, double *data);

int compress(double *data, double *m, int nrow, int ncol);

int decompress(double *m, int nrow, int ncol);

int hard_threshold(double *m, int *flag, int n, int nlevels, int nrow, int ncol, double threshold);

int multiresolution_analysis(int argc, char** argv);

int multiresolution_meshing(int argc, char** argv);

/*
 * ----------------------------------------------------------------------
 */

int sign(double x)
{
  int flag = (x > 0) ? 1 : -1;
  if (x == 0) {
    flag = 0;
  }
  return flag;
}

double max(double a, double b)
{
  double results = (a > b) ? a : b;
  return results;
}

double min(double a, double b)
{
  double results = (a < b) ? a : b;
  return results;
}

int ipow(int base, int exponent)
{
  int result = 1;
  for (int i = exponent; i > 0; i--) {
    result = result * base;
  }
  return result;
}

int get_wavelet_matrix(double *m, int dim)
{
  int len = (int) (dim / 2);
  
  int r1[len]; /* rows for low frequency */
  int r2[len]; /* rows for high frequency */
  int c1[len]; /* columns */
  int c2[len]; /* columns */

  for (int i = 0; i < len; i ++) {
    r1[i] = i;
    r2[i] = len + i;
    c1[i] = 2 * i;
    c2[i] = 2 * i + 1;
    
    m[c1[i] + dim * r1[i]] = 0.5 * sqrt(2.0);
    m[c2[i] + dim * r1[i]] = 0.5 * sqrt(2.0);
    
    m[c1[i] + dim * r2[i]] = 0.5 * sqrt(2.0);
    m[c2[i] + dim * r2[i]] = - 0.5 * sqrt(2.0);
  }
  
  return 0;
}

int matmul(double* matrix1, int nrow1, int ncol1, double* matrix2, int nrow2, int ncol2, double* result) {

  if (ncol1 != nrow2) {
    fscanf(stderr, "[ERR] Dimensions of matrices don't match: %d-by%d with %d-by%d\n", &nrow1, &ncol1, &nrow2, &ncol2);
    return -1;
  }
  
  double sum = 0.0;
  int i;

#pragma omp parallel
  {
#pragma omp for private(i) reduction(+:sum)
    for (i = 0; i < nrow1; i ++) {
      for (int j = 0; j < ncol2; j ++) {
	for (int k = 0; k < ncol1; k ++) {
	  sum += matrix1[k + i * ncol1] * matrix2[j + k * ncol2];
	}
	result[j + ncol2 * i] = sum;
	sum = 0.0;
      }
    }
  }

  return 0;
}

int transpose(double* matrix, int nrow, int ncol) {

  double *_tmp = (double*) calloc(ncol * nrow, sizeof(double));

  for (int j = 0; j < nrow; j ++) {
    for (int i = 0; i < ncol; i ++) {
      _tmp[j + nrow * i] = matrix[i + ncol * j];
    }
  }

  memcpy(matrix, _tmp, nrow * ncol * sizeof(double));

  free(_tmp);
  
  return 0;
}

/*
 * ----------------------------------------------------------------------
 * meshing functions
 * ----------------------------------------------------------------------
 */

int get_border(double *data, double nodata, int nrow, int ncol)
{
  /* Square tracing */

  FILE *fp = fopen("output/boundary.asc", "w");

  int i = 0; /* x direction */
  int j = 0; /* y direction */

  double val;

  /* Bootstrap */

  val = data[i + ncol * j];

  while (val <= nodata) {

    i = i + 1;
    if (i == ncol) {
      i = 0;
      j = j + 1;
    }
    
    val = data[i + ncol * j];
    
  }

  int i0 = i;
  int j0 = j;

  /*
   *        1
   *        ^
   *        |
   * 2 < - - - - > 0
   *        |
   *        v
   *        3
   */
  
  int dir = 0;
  
  /* Detected first pixel of the boundary */
  
  while (1) {
    
    if (i < 0 || j < 0 || i >= ncol || j >= nrow) {
      val = nodata;
    } else {
      val = data[i + ncol * j];
    }
    
    if (val > nodata) {
      dir = (dir + 1) % 4; /* go left */
      fprintf(fp, "%d %d %f\n", i, j, val);
    } else {
      dir = (dir + 3) % 4; /* go right */
    }

    switch (dir) {
    case 0:
      i = i + 1;
      break;
    case 1:
      j = j + 1;
      break;
    case 2:
      i = i - 1;
      break;
    case 3:
      j = j - 1;
    }
    
    if (i == i0 && j == j0) {
      break;
    }
    
  }

  fclose(fp);

  return 0;
}

void write_avs(struct triangulateio *io, int counter, double *elevations, int ncol, int nrow, double xllcorner, double yllcorner, double cellsize, double nodata)
{

  char fname[1024];
  sprintf(fname, "output/mesh%d.inp", counter);
  
  FILE *fp;
  fp = fopen(fname, "w");

  fprintf(fp, "%d %d %d %d %d\n", io->numberofpoints, io->numberoftriangles, 0, 0, 0);

  for (int i = 0; i < io->numberofpoints; ++ i) {

    /* map elevation to the grid */
    double x = io->pointlist[2*i];
    double y = io->pointlist[2*i+1];

    int ix = (int) floor((x - xllcorner) / cellsize);
    int iy = nrow - (int) floor((y - yllcorner) / cellsize);

    double z = elevations[ix + ncol * iy];
    if (z == nodata) {
      int ix0 = (ix + 1) < ncol ? ix + 1 : ix;
      int ix1 = (ix - 1) > 0 ? ix - 1 : ix;
      int iy0 = (iy + 1) < nrow ? iy + 1 : iy;
      int iy1 = (iy - 1) > 0 ? iy - 1 : iy;

      /* indices of von neumann neighborhood */
      int neumann[4] = {ix0 + ncol * iy, ix1 + ncol * iy, ix + ncol * iy0, ix + ncol * iy1};

      for (int j = 0; j < 4; j ++) {
	double znew = elevations[neumann[j]];
	if (znew != nodata) {
	  z = znew;
	  break;
	}
      }

      int moore[4] = {ix0 + ncol * iy0, ix0 + ncol * iy1, ix1 + ncol * iy0, ix1 + ncol * iy1};

      for (int j = 0; j < 4; j ++) {
	double znew = elevations[moore[j]];
	if (znew != nodata) {
	  z = znew;
	  break;
	}
      }
      
    }
    
    fprintf(fp, "%08d %f %f %f\n", i+1, io->pointlist[2*i], io->pointlist[2*i+1], z);

  }

  for (int i = 0; i < io->numberoftriangles; ++ i) {
    fprintf(fp, "%08d %8d tri %8d %8d %8d\n", i+1, 1, io->trianglelist[3*i], io->trianglelist[3*i+1], io->trianglelist[3*i+2]);
  }

  fclose(fp);
  
}

void report(struct triangulateio *io, int markers, int reporttriangles,
	    int reportneighbors, int reportsegments, int reportedges,
	    int reportnorms)
{
  
  int i, j;

  for (i = 0; i < io->numberofpoints; i++) {
    printf("Point %4d:", i);
    for (j = 0; j < 2; j++) {
      printf("  %.6g", io->pointlist[i * 2 + j]);
    }
    if (io->numberofpointattributes > 0) {
      printf("   attributes");
    }
    for (j = 0; j < io->numberofpointattributes; j++) {
      printf("  %.6g",
             io->pointattributelist[i * io->numberofpointattributes + j]);
    }
    if (markers) {
      printf("   marker %d\n", io->pointmarkerlist[i]);
    } else {
      printf("\n");
    }
  }
  printf("\n");

  if (reporttriangles || reportneighbors) {
    for (i = 0; i < io->numberoftriangles; i++) {
      if (reporttriangles) {
        printf("Triangle %4d points:", i);
        for (j = 0; j < io->numberofcorners; j++) {
          printf("  %4d", io->trianglelist[i * io->numberofcorners + j]);
        }
        if (io->numberoftriangleattributes > 0) {
          printf("   attributes");
        }
        for (j = 0; j < io->numberoftriangleattributes; j++) {
          printf("  %.6g", io->triangleattributelist[i *
                                         io->numberoftriangleattributes + j]);
        }
        printf("\n");
      }
      if (reportneighbors) {
        printf("Triangle %4d neighbors:", i);
        for (j = 0; j < 3; j++) {
          printf("  %4d", io->neighborlist[i * 3 + j]);
        }
        printf("\n");
      }
    }
    printf("\n");
  }

  if (reportsegments) {
    for (i = 0; i < io->numberofsegments; i++) {
      printf("Segment %4d points:", i);
      for (j = 0; j < 2; j++) {
        printf("  %4d", io->segmentlist[i * 2 + j]);
      }
      if (markers) {
        printf("   marker %d\n", io->segmentmarkerlist[i]);
      } else {
        printf("\n");
      }
    }
    printf("\n");
  }

  if (reportedges) {
    for (i = 0; i < io->numberofedges; i++) {
      printf("Edge %4d points:", i);
      for (j = 0; j < 2; j++) {
        printf("  %4d", io->edgelist[i * 2 + j]);
      }
      if (reportnorms && (io->edgelist[i * 2 + 1] == -1)) {
        for (j = 0; j < 2; j++) {
          printf("  %.6g", io->normlist[i * 2 + j]);
        }
      }
      if (markers) {
        printf("   marker %d\n", io->edgemarkerlist[i]);
      } else {
        printf("\n");
      }
    }
    printf("\n");
  }
}