File: tilemath.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (386 lines) | stat: -rw-r--r-- 10,194 bytes parent folder | download | duplicates (2)
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
386
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "raster3d_intern.h"

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

/*!
 * \brief
 *
 *  Converts index <em>tileIndex</em> into tile-coordinates
 * <em>(xTile, yTile, zTile)</em>.
 *
 *  \param map
 *  \param tileIndex
 *  \param xTile
 *  \param yTile
 *  \param zTile
 *  \return void
 */

void Rast3d_tile_index2tile(RASTER3D_Map *map, int tileIndex, int *xTile,
                            int *yTile, int *zTile)
{
    int tileIndex2d;

    *zTile = tileIndex / map->nxy;
    tileIndex2d = tileIndex % map->nxy;
    *yTile = tileIndex2d / map->nx;
    *xTile = tileIndex2d % map->nx;
}

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

/*!
 * \brief
 *
 * Returns tile-index corresponding to tile-coordinates <em>(xTile,
 * yTile, zTile)</em>.
 *
 *  \param map
 *  \param xTile
 *  \param yTile
 *  \param zTile
 *  \return int
 */

int Rast3d_tile2tile_index(RASTER3D_Map *map, int xTile, int yTile, int zTile)
{
    return map->nxy * zTile + map->nx * yTile + xTile;
}

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

/*!
 * \brief
 *
 *  Computes the cell-coordinates <em>(x, y, z)</em>
 * which correspond to the origin of the tile with tile-coordinates <em>(xTile,
 * yTile, zTile)</em>.
 *
 *  \param map
 *  \param xTile
 *  \param yTile
 *  \param zTile
 *  \param x
 *  \param y
 *  \param z
 *  \return void
 */

void Rast3d_tile_coord_origin(RASTER3D_Map *map, int xTile, int yTile,
                              int zTile, int *x, int *y, int *z)
{
    *x = map->tileX * xTile;
    *y = map->tileY * yTile;
    *z = map->tileZ * zTile;
}

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

/*!
 * \brief
 *
 *  Computes the cell-coordinates <em>(x, y, z)</em> which correspond to
 * the origin of the tile with <em>tileIndex</em>.
 *
 *  \param map
 *  \param tileIndex
 *  \param x
 *  \param y
 *  \param z
 *  \return void
 */

void Rast3d_tile_index_origin(RASTER3D_Map *map, int tileIndex, int *x, int *y,
                              int *z)
{
    int xTile, yTile, zTile;

    Rast3d_tile_index2tile(map, tileIndex, &xTile, &yTile, &zTile);
    Rast3d_tile_coord_origin(map, xTile, yTile, zTile, x, y, z);
}

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

/*!
 * \brief
 *
 *  Converts
 * cell-coordinates <em>(x, y, z)</em> into tile-coordinates <em>(xTile, yTile,
 * zTile)</em> and the coordinate of the cell <em>(xOffs, yOffs, zOffs)</em>
 * within the tile.
 *
 *  \param map
 *  \param x
 *  \param y
 *  \param z
 *  \param xTile
 *  \param yTile
 *  \param zTile
 *  \param xOffs
 *  \param yOffs
 *  \param zOffs
 *  \return void
 */

void Rast3d_coord2tile_coord(RASTER3D_Map *map, int x, int y, int z, int *xTile,
                             int *yTile, int *zTile, int *xOffs, int *yOffs,
                             int *zOffs)
{
    *xTile = x / map->tileX;
    *xOffs = x % map->tileX;
    *yTile = y / map->tileY;
    *yOffs = y % map->tileY;
    *zTile = z / map->tileZ;
    *zOffs = z % map->tileZ;
}

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

/*!
 * \brief
 *
 *  Converts cell-coordinates <em>(x, y, z)</em> into
 * <em>tileIndex</em> and the <em>offset</em> of the cell within the tile.
 *
 *  \param map
 *  \param x
 *  \param y
 *  \param z
 *  \param tileIndex
 *  \param offset
 *  \return void
 */

void Rast3d_coord2tile_index(RASTER3D_Map *map, int x, int y, int z,
                             int *tileIndex, int *offset)
{
    int xTile, yTile, zTile, xOffs, yOffs, zOffs;

    Rast3d_coord2tile_coord(map, x, y, z, &xTile, &yTile, &zTile, &xOffs,
                            &yOffs, &zOffs);
    *tileIndex = Rast3d_tile2tile_index(map, xTile, yTile, zTile);
    *offset = zOffs * map->tileXY + yOffs * map->tileX + xOffs;
}

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

/*!
 * \brief
 *
 *  Returns 1 if
 * cell-coordinate <em>(x, y, z)</em> is a coordinate inside the region. Returns
 * 0 otherwise.
 *
 *  \param map
 *  \param x
 *  \param y
 *  \param z
 *  \return int
 */

int Rast3d_coord_in_range(RASTER3D_Map *map, int x, int y, int z)
{
    return (x >= 0) && (x < map->region.cols) && (y >= 0) &&
           (y < map->region.rows) && (z >= 0) && (z < map->region.depths);
}

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

/*!
 * \brief
 *
 *  Returns 1 if <em>tileIndex</em> is a valid index for <em>map</em>.
 *  Returns 0 otherwise.
 *
 *  \param map
 *  \param tileIndex
 *  \return int
 */

int Rast3d_tile_index_in_range(RASTER3D_Map *map, int tileIndex)
{
    return (tileIndex < map->nTiles) && (tileIndex >= 0);
}

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

/*!
 * \brief
 *
 *  Returns 1 if
 * tile-coordinate <em>(x, y, z)</em> is a coordinate inside tile cube. Returns
 * 0 otherwise.
 *
 *  \param map
 *  \param x
 *  \param y
 *  \param z
 *  \return int
 */

int Rast3d_tile_in_range(RASTER3D_Map *map, int x, int y, int z)
{
    return (x >= 0) && (x < map->nx) && (y >= 0) && (y < map->ny) && (z >= 0) &&
           (z < map->nz);
}

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

/*!
 * \brief
 *
 *  Computes the dimensions of the tile when clipped to fit the
 * region of <em>map</em>. The clipped dimensions are returned in <em>rows</em>,
 * <em>cols</em>, <em>depths</em>.  The complement is returned in
 * <em>xRedundant</em>, <em>yRedundant</em>, and <em>zRedundant</em>. This
 * function returns the number of cells in the clipped tile.
 *
 *  \param map
 *  \param tileIndex
 *  \param rows
 *  \param cols
 *  \param depths
 *  \param xRedundant
 *  \param yRedundant
 *  \param zRedundant
 *  \return int
 */

int Rast3d_compute_clipped_tile_dimensions(RASTER3D_Map *map, int tileIndex,
                                           int *rows, int *cols, int *depths,
                                           int *xRedundant, int *yRedundant,
                                           int *zRedundant)
{
    int x, y, z;

    Rast3d_tile_index2tile(map, tileIndex, &x, &y, &z);

    if ((x != map->clipX) && (y != map->clipY) && (z != map->clipZ)) {
        return map->tileSize;
    }

    if (x != map->clipX) {
        *cols = map->tileX;
        *xRedundant = 0;
    }
    else {
        *cols = (map->region.cols - 1) % map->tileX + 1;
        *xRedundant = map->tileX - *cols;
    }
    if (y != map->clipY) {
        *rows = map->tileY;
        *yRedundant = 0;
    }
    else {
        *rows = (map->region.rows - 1) % map->tileY + 1;
        *yRedundant = map->tileY - *rows;
    }
    if (z != map->clipZ) {
        *depths = map->tileZ;
        *zRedundant = 0;
    }
    else {
        *depths = (map->region.depths - 1) % map->tileZ + 1;
        *zRedundant = map->tileZ - *depths;
    }

    /* printf ("%d (%d %d %d): (%d %d) (%d %d) (%d %d), %d\n", */
    /*      tileIndex, x, y, z, *rows, *xRedundant, *cols, *yRedundant,  */
    /*      *depths, *zRedundant, *depths * *cols * *rows); */

    return *depths * *cols * *rows;
}

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

/*!
 * \brief Compute the optimal tile size.
 *
 * This function computes tile sizes with an optimal ratio between tile
 * dimensions and minimized border tile overlapping. Large dimensions (in most
 * cases x and y) will be reduced more often than small dimensions to fit the
 * maxSize criteria.
 *
 *  \param region The region of the map
 *  \param type The type of the map (FCELL_TYPE or DCELL_TYPE)
 *  \param tileX Pointer of the tile size in x direction for result storage
 *  \param tileY Pointer of the tile size in y direction for result storage
 *  \param tileZ Pointer of the tile size in z direction for result storage
 *  \param maxSize The max size of the tile in kilo bytes
 *  \return void
 */

void Rast3d_compute_optimal_tile_dimension(RASTER3D_Region *region, int type,
                                           int *tileX, int *tileY, int *tileZ,
                                           int maxSize)
{
    unsigned long size = 0;
    unsigned long x, y, z;
    unsigned long i = 0;
    unsigned long tileSize;
    unsigned long divx = 2;
    unsigned long divy = 2;
    unsigned long divz = 2;

    if (type == FCELL_TYPE)
        size = sizeof(FCELL);

    if (type == DCELL_TYPE)
        size = sizeof(DCELL);

    x = region->cols;
    y = region->rows;
    z = region->depths;

    while (1) {
        tileSize = size * x * y * z;

        G_debug(2,
                "Rast3d_compute_optimal_tile_dimension: tilesize %li x %li y "
                "%li z %li\n",
                tileSize, x, y, z);

        if (maxSize < 0 || tileSize <= (unsigned int)maxSize * 1024)
            break;

        /* Compute weighted tile sizes. Take care that the tile size is computed
           based on the dimension ratio and reduce the border tile overlapping.
           In case one dimension is much larger than the other, reduce
           the large dimension by a factor till the maxSize is reached or till
           the the other dimensions are only by factor 2 smaller. */
        if ((y / x) <= 2 && (z / x) <= 2) {
            if (region->cols % divx != 0)
                x = region->cols / divx + 1;
            else
                x = region->cols / divx;
            divx += 1;
        }
        if ((x / y) <= 2 && (z / y) <= 2) {
            if (region->rows % divy != 0)
                y = region->rows / divy + 1;
            else
                y = region->rows / divy;
            divy += 1;
        }
        if ((x / z) <= 2 && (y / z) <= 2) {
            if (region->depths % divz != 0)
                z = region->depths / divz + 1;
            else
                z = region->depths / divz;
            divz += 1;
        }

        /* Avoid infinite loop */
        i++;
        if (i > 10000)
            break;
    }

    *tileX = (int)x;
    *tileY = (int)y;
    *tileZ = (int)z;
}