20#include <grass/N_pde.h>
21#include <grass/raster.h>
22#include <grass/glocale.h>
79 if (rows < 1 || cols < 1)
80 G_fatal_error(
"N_alloc_array_2d: cols and rows should be > 0");
82 if (type != CELL_TYPE && type != FCELL_TYPE && type != DCELL_TYPE)
83 G_fatal_error(
"N_alloc_array_2d: Wrong data type, should be CELL_TYPE, "
84 "FCELL_TYPE or DCELL_TYPE");
91 data->offset = offset;
92 data->rows_intern = rows + 2 * offset;
93 data->cols_intern = cols + 2 * offset;
94 data->cell_array =
NULL;
95 data->fcell_array =
NULL;
96 data->dcell_array =
NULL;
98 if (data->type == CELL_TYPE) {
99 data->cell_array = (CELL *)G_calloc(
100 (
size_t)data->rows_intern * data->cols_intern,
sizeof(CELL));
102 "N_alloc_array_2d: CELL array allocated rows_intern %i "
103 "cols_intern %i offset %i",
104 data->rows_intern, data->cols_intern, data->offset = offset);
106 else if (data->type == FCELL_TYPE) {
107 data->fcell_array = (FCELL *)G_calloc(
108 (
size_t)data->rows_intern * data->cols_intern,
sizeof(FCELL));
110 "N_alloc_array_2d: FCELL array allocated rows_intern %i "
111 "cols_intern %i offset %i",
112 data->rows_intern, data->cols_intern, data->offset = offset);
114 else if (data->type == DCELL_TYPE) {
115 data->dcell_array = (DCELL *)G_calloc(
116 (
size_t)data->rows_intern * data->cols_intern,
sizeof(DCELL));
118 "N_alloc_array_2d: DCELL array allocated rows_intern %i "
119 "cols_intern %i offset %i",
120 data->rows_intern, data->cols_intern, data->offset = offset);
136 G_debug(3,
"N_free_array_2d: free N_array_2d");
138 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
141 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
142 G_free(data->fcell_array);
144 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
145 G_free(data->dcell_array);
185 if (data->offset == 0) {
186 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
187 *((CELL *)value) = data->cell_array[row * data->cols_intern + col];
189 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
191 data->fcell_array[row * data->cols_intern + col];
193 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
195 data->dcell_array[row * data->cols_intern + col];
199 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
201 data->cell_array[(row + data->offset) * data->cols_intern +
204 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
206 data->fcell_array[(row + data->offset) * data->cols_intern +
209 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
211 data->dcell_array[(row + data->offset) * data->cols_intern +
234 if (data->offset == 0) {
235 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
237 "N_is_array_2d_value_null: null value is of type CELL at "
240 return Rast_is_null_value(
241 (
void *)&(data->cell_array[row * data->cols_intern + col]),
244 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
246 "N_is_array_2d_value_null: null value is of type FCELL at "
249 return Rast_is_null_value(
250 (
void *)&(data->fcell_array[row * data->cols_intern + col]),
253 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
255 "N_is_array_2d_value_null: null value is of type DCELL at "
258 return Rast_is_null_value(
259 (
void *)&(data->dcell_array[row * data->cols_intern + col]),
264 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
266 "N_is_array_2d_value_null: null value is of type CELL at "
269 return Rast_is_null_value(
271 data->cell_array[(row + data->offset) * data->cols_intern +
272 col + data->offset]),
275 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
277 "N_is_array_2d_value_null: null value is of type FCELL at "
280 return Rast_is_null_value(
282 data->fcell_array[(row + data->offset) * data->cols_intern +
283 col + data->offset]),
286 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
288 "N_is_array_2d_value_null: null value is of type DCELL at "
291 return Rast_is_null_value(
293 data->dcell_array[(row + data->offset) * data->cols_intern +
294 col + data->offset]),
320 switch (data->type) {
353 switch (data->type) {
359 return (FCELL)fvalue;
362 return (FCELL)dvalue;
386 switch (data->type) {
392 return (DCELL)fvalue;
395 return (DCELL)dvalue;
415 G_debug(6,
"N_put_array_2d_value: put value to array");
417 if (data->offset == 0) {
418 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
419 data->cell_array[row * data->cols_intern + col] = *((CELL *)value);
421 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
422 data->fcell_array[row * data->cols_intern + col] =
425 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
426 data->dcell_array[row * data->cols_intern + col] =
431 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
432 data->cell_array[(row + data->offset) * data->cols_intern + col +
433 data->offset] = *((CELL *)value);
435 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
436 data->fcell_array[(row + data->offset) * data->cols_intern + col +
437 data->offset] = *((FCELL *)value);
439 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
440 data->dcell_array[(row + data->offset) * data->cols_intern + col +
441 data->offset] = *((DCELL *)value);
463 "N_put_array_2d_value_null: put null value to array pos [%i][%i]",
466 if (data->offset == 0) {
467 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
468 Rast_set_c_null_value(
469 (
void *)&(data->cell_array[row * data->cols_intern + col]), 1);
471 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
472 Rast_set_f_null_value(
473 (
void *)&(data->fcell_array[row * data->cols_intern + col]), 1);
475 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
476 Rast_set_d_null_value(
477 (
void *)&(data->dcell_array[row * data->cols_intern + col]), 1);
481 if (data->type == CELL_TYPE && data->cell_array !=
NULL) {
482 Rast_set_c_null_value(
484 data->cell_array[(row + data->offset) * data->cols_intern +
485 col + data->offset]),
488 else if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
489 Rast_set_f_null_value(
491 data->fcell_array[(row + data->offset) * data->cols_intern +
492 col + data->offset]),
495 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
496 Rast_set_d_null_value(
498 data->dcell_array[(row + data->offset) * data->cols_intern +
499 col + data->offset]),
521 switch (data->type) {
523 fvalue = (FCELL)value;
527 dvalue = (DCELL)value;
551 switch (data->type) {
553 cvalue = (CELL)value;
557 dvalue = (DCELL)value;
581 switch (data->type) {
583 cvalue = (CELL)value;
587 fvalue = (FCELL)value;
606 fprintf(stdout,
"N_array_2d \n");
607 fprintf(stdout,
"Cols %i\n", data->cols);
608 fprintf(stdout,
"Rows: %i\n", data->rows);
609 fprintf(stdout,
"Array type: %i\n", data->type);
610 fprintf(stdout,
"Offset: %i\n", data->offset);
611 fprintf(stdout,
"Internal cols: %i\n", data->cols_intern);
612 fprintf(stdout,
"Internal rows: %i\n", data->rows_intern);
613 fprintf(stdout,
"CELL array pointer: %p\n", (
void *)data->cell_array);
614 fprintf(stdout,
"FCELL array pointer: %p\n", (
void *)data->fcell_array);
615 fprintf(stdout,
"DCELL array pointer: %p\n", (
void *)data->dcell_array);
634 for (j = 0 - data->offset; j < data->rows + data->offset; j++) {
635 for (i = 0 - data->offset; i < data->cols + data->offset; i++) {
636 if (data->type == CELL_TYPE)
638 else if (data->type == FCELL_TYPE)
640 else if (data->type == DCELL_TYPE)
643 fprintf(stdout,
"\n");
645 fprintf(stdout,
"\n");
724 if (rows < 1 || cols < 1 || depths < 1)
725 G_fatal_error(
"N_alloc_array_3d: depths, cols and rows should be > 0");
727 if (type != DCELL_TYPE && type != FCELL_TYPE)
728 G_fatal_error(
"N_alloc_array_3d: Wrong data type, should be FCELL_TYPE "
735 data->depths = depths;
737 data->offset = offset;
738 data->rows_intern = rows + 2 * offset;
739 data->cols_intern = cols + 2 * offset;
740 data->depths_intern = depths + 2 * offset;
741 data->fcell_array =
NULL;
742 data->dcell_array =
NULL;
744 if (data->type == FCELL_TYPE) {
745 data->fcell_array = (
float *)G_calloc(
746 (
size_t)data->depths_intern * data->rows_intern * data->cols_intern,
749 "N_alloc_array_3d: float array allocated rows_intern %i "
750 "cols_intern %i depths_intern %i offset %i",
751 data->rows_intern, data->cols_intern, data->depths_intern,
752 data->offset = offset);
754 else if (data->type == DCELL_TYPE) {
755 data->dcell_array = (
double *)G_calloc(
756 (
size_t)data->depths_intern * data->rows_intern * data->cols_intern,
759 "N_alloc_array_3d: double array allocated rows_intern %i "
760 "cols_intern %i depths_intern %i offset %i",
761 data->rows_intern, data->cols_intern, data->depths_intern,
762 data->offset = offset);
778 G_debug(3,
"N_free_array_3d: free N_array_3d");
780 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
781 G_free(data->fcell_array);
783 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
784 G_free(data->dcell_array);
826 if (data->offset == 0) {
827 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
829 data->fcell_array[depth *
830 (data->rows_intern * data->cols_intern) +
831 row * data->cols_intern + col];
833 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
835 data->dcell_array[depth *
836 (data->rows_intern * data->cols_intern) +
837 row * data->cols_intern + col];
841 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
843 data->fcell_array[(depth + data->offset) *
844 (data->rows_intern * data->cols_intern) +
845 (row + data->offset) * data->cols_intern +
846 (col + data->offset)];
848 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
850 data->dcell_array[(depth + data->offset) *
851 (data->rows_intern * data->cols_intern) +
852 (row + data->offset) * data->cols_intern +
853 (col + data->offset)];
876 if (data->offset == 0) {
877 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
879 "N_is_array_3d_value_null: null value is of type "
880 "DCELL_TYPE at pos [%i][%i][%i]",
882 return Rast3d_is_null_value_num(
883 (
void *)&(data->fcell_array[depth * (data->rows_intern *
885 row * data->cols_intern + col]),
888 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
890 "N_is_array_3d_value_null: null value is of type "
891 "DCELL_TYPE at pos [%i][%i][%i]",
893 return Rast3d_is_null_value_num(
894 (
void *)&(data->dcell_array[depth * (data->rows_intern *
896 row * data->cols_intern + col]),
901 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
903 "N_is_array_3d_value_null: null value is of type "
904 "DCELL_TYPE at pos [%i][%i][%i]",
906 return Rast3d_is_null_value_num(
908 data->fcell_array[(depth + data->offset) *
911 (row + data->offset) * data->cols_intern +
912 (col + data->offset)]),
915 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
917 "N_is_array_3d_value_null: null value is of type "
918 "DCELL_TYPE at pos [%i][%i][%i]",
920 return Rast3d_is_null_value_num(
922 data->dcell_array[(depth + data->offset) *
925 (row + data->offset) * data->cols_intern +
926 (col + data->offset)]),
953 switch (data->type) {
956 return (
float)fvalue;
959 return (
float)dvalue;
984 switch (data->type) {
988 return (
double)fvalue;
991 return (
double)dvalue;
1014 G_debug(6,
"N_put_array_3d_value: put value to array at pos [%i][%i][%i]",
1017 if (data->offset == 0) {
1018 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
1019 data->fcell_array[depth * (data->rows_intern * data->cols_intern) +
1020 row * data->cols_intern + col] =
1023 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
1025 data->dcell_array[depth * (data->rows_intern * data->cols_intern) +
1026 row * data->cols_intern + col] =
1031 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
1032 data->fcell_array[(depth + data->offset) *
1033 (data->rows_intern * data->cols_intern) +
1034 (row + data->offset) * data->cols_intern +
1035 (col + data->offset)] = *((
float *)value);
1037 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
1038 data->dcell_array[(depth + data->offset) *
1039 (data->rows_intern * data->cols_intern) +
1040 (row + data->offset) * data->cols_intern +
1041 (col + data->offset)] = *((
double *)value);
1064 "N_put_array_3d_value_null: put null value to array at pos "
1068 if (data->offset == 0) {
1069 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
1070 Rast3d_set_null_value(
1071 (
void *)&(data->fcell_array[depth * (data->rows_intern *
1072 data->cols_intern) +
1073 row * data->cols_intern + col]),
1076 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
1077 Rast3d_set_null_value(
1078 (
void *)&(data->dcell_array[depth * (data->rows_intern *
1079 data->cols_intern) +
1080 row * data->cols_intern + col]),
1085 if (data->type == FCELL_TYPE && data->fcell_array !=
NULL) {
1086 Rast3d_set_null_value(
1088 data->fcell_array[(depth + data->offset) *
1089 (data->rows_intern *
1090 data->cols_intern) +
1091 (row + data->offset) * data->cols_intern +
1092 (col + data->offset)]),
1095 else if (data->type == DCELL_TYPE && data->dcell_array !=
NULL) {
1096 Rast3d_set_null_value(
1098 data->dcell_array[(depth + data->offset) *
1099 (data->rows_intern *
1100 data->cols_intern) +
1101 (row + data->offset) * data->cols_intern +
1102 (col + data->offset)]),
1126 if (data->type == DCELL_TYPE) {
1127 dval = (double)value;
1153 if (data->type == FCELL_TYPE) {
1154 fval = (double)value;
1173 fprintf(stdout,
"N_array_3d \n");
1174 fprintf(stdout,
"Cols %i\n", data->cols);
1175 fprintf(stdout,
"Rows: %i\n", data->rows);
1176 fprintf(stdout,
"Depths: %i\n", data->depths);
1177 fprintf(stdout,
"Array type: %i\n", data->type);
1178 fprintf(stdout,
"Offset: %i\n", data->offset);
1179 fprintf(stdout,
"Internal cols: %i\n", data->cols_intern);
1180 fprintf(stdout,
"Internal rows: %i\n", data->rows_intern);
1181 fprintf(stdout,
"Internal depths: %i\n", data->depths_intern);
1182 fprintf(stdout,
"FCELL array pointer: %p\n", (
void *)data->fcell_array);
1183 fprintf(stdout,
"DCELL array pointer: %p\n", (
void *)data->dcell_array);
1202 for (k = 0; k < data->depths; k++) {
1203 for (j = 0; j < data->rows; j++) {
1204 for (i = 0; i < data->cols; i++) {
1205 if (data->type == FCELL_TYPE)
1207 else if (data->type == DCELL_TYPE)
void G_free(void *buf)
Free allocated memory.
int G_debug(int level, const char *msg,...)
Print debugging message.
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
FCELL N_get_array_2d_f_value(N_array_2d *data, int col, int row)
Returns the value of type FCELL at position col, row.
void N_put_array_3d_value_null(N_array_3d *data, int col, int row, int depth)
This function writes a null value to the N_array_3d data at position col, row, depth.
void N_print_array_3d_info(N_array_3d *data)
Write the info of the array to stdout.
N_array_3d * N_alloc_array_3d(int cols, int rows, int depths, int offset, int type)
Allocate memory for a N_array_3d data structure.
CELL N_get_array_2d_c_value(N_array_2d *data, int col, int row)
Returns the value of type CELL at position col, row.
void N_put_array_2d_f_value(N_array_2d *data, int col, int row, FCELL value)
Writes a FCELL value to the N_array_2d struct at position col, row.
float N_get_array_3d_f_value(N_array_3d *data, int col, int row, int depth)
This function returns the value of type float at position col, row, depth.
void N_free_array_3d(N_array_3d *data)
Release the memory of a N_array_3d.
void N_put_array_3d_f_value(N_array_3d *data, int col, int row, int depth, float value)
This function writes a float value to the N_array_3d data at position col, row, depth.
DCELL N_get_array_2d_d_value(N_array_2d *data, int col, int row)
Returns the value of type DCELL at position col, row.
int N_get_array_2d_type(N_array_2d *array)
Return the data type of the N_array_2d struct.
void N_put_array_3d_value(N_array_3d *data, int col, int row, int depth, char *value)
This function writes a value to the N_array_3d data at position col, row, depth.
void N_print_array_2d(N_array_2d *data)
Write info and content of the N_array_2d struct to stdout.
int N_is_array_3d_value_null(N_array_3d *data, int col, int row, int depth)
This function returns 1 if value of N_array_3d data at position col, row, depth is of type null,...
void N_get_array_2d_value(N_array_2d *data, int col, int row, void *value)
Write the value of the N_array_2d struct at position col, row to value.
void N_put_array_2d_value(N_array_2d *data, int col, int row, char *value)
Writes a value to the N_array_2d struct at position col, row.
void N_print_array_2d_info(N_array_2d *data)
This function writes the data info of the array data to stdout.
int N_is_array_2d_value_null(N_array_2d *data, int col, int row)
Returns 1 if the value of N_array_2d struct at position col, row is of type null, otherwise 0.
void N_free_array_2d(N_array_2d *data)
Release the memory of a N_array_2d structure.
void N_put_array_3d_d_value(N_array_3d *data, int col, int row, int depth, double value)
Writes a double value to the N_array_3d struct at position col, row, depth.
N_array_2d * N_alloc_array_2d(int cols, int rows, int offset, int type)
Allocate memory for a N_array_2d data structure.
double N_get_array_3d_d_value(N_array_3d *data, int col, int row, int depth)
This function returns the value of type float at position col, row, depth.
void N_get_array_3d_value(N_array_3d *data, int col, int row, int depth, void *value)
This function writes the value of N_array_3d data at position col, row, depth to the variable value.
void N_put_array_2d_value_null(N_array_2d *data, int col, int row)
Writes the null value to the N_array_2d struct at position col, row.
void N_put_array_2d_c_value(N_array_2d *data, int col, int row, CELL value)
Writes a CELL value to the N_array_2d struct at position col, row.
void N_put_array_2d_d_value(N_array_2d *data, int col, int row, DCELL value)
Writes a DCELL value to the N_array_2d struct at position col, row.
int N_get_array_3d_type(N_array_3d *array)
Return the data type of the N_array_3d.
void N_print_array_3d(N_array_3d *data)
Write info and content of the array data to stdout.