summaryrefslogtreecommitdiff
path: root/header.h
blob: 2a6754670edd4cc10b5d827e97585f44c1e72532 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#ifndef DEBUG_IO
#define DEBUG_IO 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 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;

  for (int 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;
}