123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245 |
- /*****************************************************************************
- *
- * MODULE: Grass PDE Numerical Library
- * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
- * soerengebbert <at> gmx <dot> de
- *
- * PURPOSE: Array management functions
- * part of the gpde library
- *
- * COPYRIGHT: (C) 2000 by the GRASS Development Team
- *
- * This program is free software under the GNU General Public
- * License (>=v2). Read the file COPYING that comes with GRASS
- * for details.
- *
- *****************************************************************************/
- #include <math.h>
- #include <grass/N_pde.h>
- #include <grass/raster.h>
- #include <grass/glocale.h>
- /* ******************** 2D ARRAY FUNCTIONS *********************** */
- /*!
- * \brief Allocate memory for a N_array_2d data structure.
- *
- * This function allocates memory for an array of type N_array_2d
- * and returns the pointer to the new allocated memory.
- * <br><br>
- * The data type of this array is set by "type" and must be
- * CELL_TYPE, FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
- * The offset sets the number of boundary cols and rows.
- * This option is useful to generate homogeneous Neumann boundary conditions around
- * an array or to establish overlapping boundaries. The array is initialized with 0 by default.
- * <br><br>
- * If the offset is greater then 0, negative indices are possible.
- * <br><br>
- *
- * The data structure of a array with 3 rows and cols and an offset of 1
- * will looks like this:
- * <br><br>
- *
- \verbatim
- 0 0 0 0 0
- 0 0 1 2 0
- 0 3 4 5 0
- 0 6 7 8 0
- 0 0 0 0 0
- \endverbatim
- *
- * 0 is the boundary.
- * <br><br>
- * Internal a one dimensional array is allocated to save memory and to speed up the memory access.
- * To access the one dimensional array with a two dimensional index use the provided
- * get and put functions. The internal representation of the above data will look like this:
- *
- \verbatim
- 0 0 0 0 0 0 0 1 2 0 0 3 4 5 0 0 6 7 8 0 0 0 0 0 0
- \endverbatim
- *
- * \param cols int
- * \param rows int
- * \param offset int
- * \param type int
- * \return N_array_2d *
- *
- * */
- N_array_2d *N_alloc_array_2d(int cols, int rows, int offset, int type)
- {
- N_array_2d *data = NULL;
- if (rows < 1 || cols < 1)
- G_fatal_error("N_alloc_array_2d: cols and rows should be > 0");
- if (type != CELL_TYPE && type != FCELL_TYPE && type != DCELL_TYPE)
- G_fatal_error
- ("N_alloc_array_2d: Wrong data type, should be CELL_TYPE, FCELL_TYPE or DCELL_TYPE");
- data = (N_array_2d *) G_calloc(1, sizeof(N_array_2d));
- data->cols = cols;
- data->rows = rows;
- data->type = type;
- data->offset = offset;
- data->rows_intern = rows + 2 * offset; /*offset position at booth sides */
- data->cols_intern = cols + 2 * offset; /*offset position at booth sides */
- data->cell_array = NULL;
- data->fcell_array = NULL;
- data->dcell_array = NULL;
- if (data->type == CELL_TYPE) {
- data->cell_array =
- (CELL *) G_calloc((size_t) data->rows_intern * data->cols_intern,
- sizeof(CELL));
- G_debug(3,
- "N_alloc_array_2d: CELL array allocated rows_intern %i cols_intern %i offset %i",
- data->rows_intern, data->cols_intern, data->offset = offset);
- }
- else if (data->type == FCELL_TYPE) {
- data->fcell_array =
- (FCELL *) G_calloc((size_t) data->rows_intern * data->cols_intern,
- sizeof(FCELL));
- G_debug(3,
- "N_alloc_array_2d: FCELL array allocated rows_intern %i cols_intern %i offset %i",
- data->rows_intern, data->cols_intern, data->offset = offset);
- }
- else if (data->type == DCELL_TYPE) {
- data->dcell_array =
- (DCELL *) G_calloc((size_t) data->rows_intern * data->cols_intern,
- sizeof(DCELL));
- G_debug(3,
- "N_alloc_array_2d: DCELL array allocated rows_intern %i cols_intern %i offset %i",
- data->rows_intern, data->cols_intern, data->offset = offset);
- }
- return data;
- }
- /*!
- * \brief Release the memory of a N_array_2d structure
- *
- * \param data N_array_2d *
- * \return void
- * */
- void N_free_array_2d(N_array_2d * data)
- {
- if (data != NULL) {
- G_debug(3, "N_free_array_2d: free N_array_2d");
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- G_free(data->cell_array);
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- G_free(data->fcell_array);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- G_free(data->dcell_array);
- }
- G_free(data);
- data = NULL;
- }
- return;
- }
- /*!
- * \brief Return the data type of the N_array_2d struct
- *
- * The data type can be CELL_TYPE, FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
- *
- * \param array N_array_2d *
- * \return type int
- * */
- int N_get_array_2d_type(N_array_2d * array)
- {
- return array->type;
- }
- /*!
- * \brief Write the value of the N_array_2d struct at position col, row to value
- *
- * The value must be of the same type as the array. Otherwise you will risk data losses.
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \param value void * - this variable contains the array value at col, row position
- * \return void
- * */
- void N_get_array_2d_value(N_array_2d * data, int col, int row, void *value)
- {
- if (data->offset == 0) {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- *((CELL *) value) =
- data->cell_array[row * data->cols_intern + col];
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- *((FCELL *) value) =
- data->fcell_array[row * data->cols_intern + col];
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- *((DCELL *) value) =
- data->dcell_array[row * data->cols_intern + col];
- }
- }
- else {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- *((CELL *) value) =
- data->cell_array[(row + data->offset) * data->cols_intern +
- col + data->offset];
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- *((FCELL *) value) =
- data->fcell_array[(row + data->offset) * data->cols_intern +
- col + data->offset];
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- *((DCELL *) value) =
- data->dcell_array[(row + data->offset) * data->cols_intern +
- col + data->offset];
- }
- }
- return;
- }
- /*!
- * \brief Returns 1 if the value of N_array_2d struct at position col, row
- * is of type null, otherwise 0
- *
- * This function checks automatically the type of the array and checks for the
- * data type null value.
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \return int - 1 = is null, 0 otherwise
- * */
- int N_is_array_2d_value_null(N_array_2d * data, int col, int row)
- {
- if (data->offset == 0) {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- G_debug(6,
- "N_is_array_2d_value_null: null value is of type CELL at pos [%i][%i]",
- col, row);
- return Rast_is_null_value((void *)
- &(data->
- cell_array[row * data->cols_intern +
- col]), CELL_TYPE);
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- G_debug(6,
- "N_is_array_2d_value_null: null value is of type FCELL at pos [%i][%i]",
- col, row);
- return Rast_is_null_value((void *)
- &(data->
- fcell_array[row * data->cols_intern +
- col]), FCELL_TYPE);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- G_debug(6,
- "N_is_array_2d_value_null: null value is of type DCELL at pos [%i][%i]",
- col, row);
- return Rast_is_null_value((void *)
- &(data->
- dcell_array[row * data->cols_intern +
- col]), DCELL_TYPE);
- }
- }
- else {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- G_debug(6,
- "N_is_array_2d_value_null: null value is of type CELL at pos [%i][%i]",
- col, row);
- return Rast_is_null_value((void *)
- &(data->
- cell_array[(row +
- data->offset) *
- data->cols_intern + col +
- data->offset]), CELL_TYPE);
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- G_debug(6,
- "N_is_array_2d_value_null: null value is of type FCELL at pos [%i][%i]",
- col, row);
- return Rast_is_null_value((void *)
- &(data->
- fcell_array[(row +
- data->offset) *
- data->cols_intern + col +
- data->offset]), FCELL_TYPE);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- G_debug(6,
- "N_is_array_2d_value_null: null value is of type DCELL at pos [%i][%i]",
- col, row);
- return Rast_is_null_value((void *)
- &(data->
- dcell_array[(row +
- data->offset) *
- data->cols_intern + col +
- data->offset]), DCELL_TYPE);
- }
- }
- return 0;
- }
- /*!
- * \brief Returns the value of type CELL at position col, row
- *
- * The data array can be of type CELL, FCELL or DCELL, the value will be casted to the CELL type.
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \return CELL
- *
- * */
- CELL N_get_array_2d_c_value(N_array_2d * data, int col, int row)
- {
- CELL value = 0;
- FCELL fvalue = 0.0;
- DCELL dvalue = 0.0;
- switch (data->type) {
- case CELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&value);
- return (CELL) value;
- case FCELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&fvalue);
- return (CELL) fvalue;
- case DCELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&dvalue);
- return (CELL) dvalue;
- }
- return value;
- }
- /*!
- * \brief Returns the value of type FCELL at position col, row
- *
- * The data array can be of type CELL, FCELL or DCELL, the value will be casted to the FCELL type.
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \return FCELL
- * */
- FCELL N_get_array_2d_f_value(N_array_2d * data, int col, int row)
- {
- CELL value = 0;
- FCELL fvalue = 0.0;
- DCELL dvalue = 0.0;
- switch (data->type) {
- case CELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&value);
- return (FCELL) value;
- case FCELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&fvalue);
- return (FCELL) fvalue;
- case DCELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&dvalue);
- return (FCELL) dvalue;
- }
- return fvalue;
- }
- /*!
- * \brief Returns the value of type DCELL at position col, row
- *
- * The data array can be of type CELL, FCELL or DCELL, the value will be casted to the DCELL type.
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \return DCELL
- *
- * */
- DCELL N_get_array_2d_d_value(N_array_2d * data, int col, int row)
- {
- CELL value = 0;
- FCELL fvalue = 0.0;
- DCELL dvalue = 0.0;
- switch (data->type) {
- case CELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&value);
- return (DCELL) value;
- case FCELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&fvalue);
- return (DCELL) fvalue;
- case DCELL_TYPE:
- N_get_array_2d_value(data, col, row, (void *)&dvalue);
- return (DCELL) dvalue;
- }
- return dvalue;
- }
- /*!
- * \brief Writes a value to the N_array_2d struct at position col, row
- *
- * The value will be automatically cast to the array type.
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \param value char *
- * \return void
- * */
- void N_put_array_2d_value(N_array_2d * data, int col, int row, char *value)
- {
- G_debug(6, "N_put_array_2d_value: put value to array");
- if (data->offset == 0) {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- data->cell_array[row * data->cols_intern + col] =
- *((CELL *) value);
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- data->fcell_array[row * data->cols_intern + col] =
- *((FCELL *) value);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- data->dcell_array[row * data->cols_intern + col] =
- *((DCELL *) value);
- }
- }
- else {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- data->cell_array[(row + data->offset) * data->cols_intern + col +
- data->offset] = *((CELL *) value);
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- data->fcell_array[(row + data->offset) * data->cols_intern + col +
- data->offset] = *((FCELL *) value);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- data->dcell_array[(row + data->offset) * data->cols_intern + col +
- data->offset] = *((DCELL *) value);
- }
- }
- return;
- }
- /*!
- * \brief Writes the null value to the N_array_2d struct at position col, row
- *
- * The null value will be automatically set to the array data type (CELL, FCELL or DCELL).
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \return void
- * */
- void N_put_array_2d_value_null(N_array_2d * data, int col, int row)
- {
- G_debug(6,
- "N_put_array_2d_value_null: put null value to array pos [%i][%i]",
- col, row);
- if (data->offset == 0) {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- Rast_set_c_null_value((void *)
- &(data->
- cell_array[row * data->cols_intern + col]),
- 1);
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- Rast_set_f_null_value((void *)
- &(data->
- fcell_array[row * data->cols_intern + col]),
- 1);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- Rast_set_d_null_value((void *)
- &(data->
- dcell_array[row * data->cols_intern + col]),
- 1);
- }
- }
- else {
- if (data->type == CELL_TYPE && data->cell_array != NULL) {
- Rast_set_c_null_value((void *)
- &(data->
- cell_array[(row +
- data->offset) *
- data->cols_intern + col +
- data->offset]), 1);
- }
- else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- Rast_set_f_null_value((void *)
- &(data->
- fcell_array[(row +
- data->offset) *
- data->cols_intern + col +
- data->offset]), 1);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- Rast_set_d_null_value((void *)
- &(data->
- dcell_array[(row +
- data->offset) *
- data->cols_intern + col +
- data->offset]), 1);
- }
- }
- return;
- }
- /*!
- * \brief Writes a CELL value to the N_array_2d struct at position col, row
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \param value CELL
- * \return void
- * */
- void N_put_array_2d_c_value(N_array_2d * data, int col, int row, CELL value)
- {
- FCELL fvalue;
- DCELL dvalue;
- switch (data->type) {
- case FCELL_TYPE:
- fvalue = (FCELL) value;
- N_put_array_2d_value(data, col, row, (char *)&fvalue);
- return;
- case DCELL_TYPE:
- dvalue = (DCELL) value;
- N_put_array_2d_value(data, col, row, (char *)&dvalue);
- return;
- }
- N_put_array_2d_value(data, col, row, (char *)&value);
- return;
- }
- /*!
- * \brief Writes a FCELL value to the N_array_2d struct at position col, row
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \param value FCELL
- * \return void
- * */
- void N_put_array_2d_f_value(N_array_2d * data, int col, int row, FCELL value)
- {
- CELL cvalue;
- DCELL dvalue;
- switch (data->type) {
- case CELL_TYPE:
- cvalue = (CELL) value;
- N_put_array_2d_value(data, col, row, (char *)&cvalue);
- return;
- case DCELL_TYPE:
- dvalue = (DCELL) value;
- N_put_array_2d_value(data, col, row, (char *)&dvalue);
- return;
- }
- N_put_array_2d_value(data, col, row, (char *)&value);
- return;
- }
- /*!
- * \brief Writes a DCELL value to the N_array_2d struct at position col, row
- *
- * \param data N_array_2d *
- * \param col int
- * \param row int
- * \param value DCELL
- * \return void
- * */
- void N_put_array_2d_d_value(N_array_2d * data, int col, int row, DCELL value)
- {
- CELL cvalue;
- FCELL fvalue;
- switch (data->type) {
- case CELL_TYPE:
- cvalue = (CELL) value;
- N_put_array_2d_value(data, col, row, (char *)&cvalue);
- return;
- case FCELL_TYPE:
- fvalue = (FCELL) value;
- N_put_array_2d_value(data, col, row, (char *)&fvalue);
- return;
- }
- N_put_array_2d_value(data, col, row, (char *)&value);
- return;
- }
- /*!
- * \brief This function writes the data info of the array data to stdout
- *
- * \param data N_array_2d *
- * \return void
- * */
- void N_print_array_2d_info(N_array_2d * data)
- {
- fprintf(stdout, "N_array_2d \n");
- fprintf(stdout, "Cols %i\n", data->cols);
- fprintf(stdout, "Rows: %i\n", data->rows);
- fprintf(stdout, "Array type: %i\n", data->type);
- fprintf(stdout, "Offset: %i\n", data->offset);
- fprintf(stdout, "Internal cols: %i\n", data->cols_intern);
- fprintf(stdout, "Internal rows: %i\n", data->rows_intern);
- fprintf(stdout, "CELL array pointer: %p\n", data->cell_array);
- fprintf(stdout, "FCELL array pointer: %p\n", data->fcell_array);
- fprintf(stdout, "DCELL array pointer: %p\n", data->dcell_array);
- return;
- }
- /*!
- * \brief Write info and content of the N_array_2d struct to stdout
- *
- * Offsets are ignored
- *
- * \param data N_array_2d *
- * \return void
- * */
- void N_print_array_2d(N_array_2d * data)
- {
- int i, j;
- N_print_array_2d_info(data);
- for (j = 0 - data->offset; j < data->rows + data->offset; j++) {
- for (i = 0 - data->offset; i < data->cols + data->offset; i++) {
- if (data->type == CELL_TYPE)
- fprintf(stdout, "%6d ", N_get_array_2d_c_value(data, i, j));
- else if (data->type == FCELL_TYPE)
- fprintf(stdout, "%6.6f ", N_get_array_2d_f_value(data, i, j));
- else if (data->type == DCELL_TYPE)
- printf("%6.6f ", N_get_array_2d_d_value(data, i, j));
- }
- fprintf(stdout, "\n");
- }
- fprintf(stdout, "\n");
- return;
- }
- /* ******************** 3D ARRAY FUNCTIONS *********************** */
- /*!
- * \brief Allocate memory for a N_array_3d data structure.
- *
- * This functions allocates an array of type N_array_3d and returns a pointer
- * to the new allocated memory.
- * <br><br>
- * The data type of this array set by "type" must be
- * FCELL_TYPE or DCELL_TYPE accordingly to the raster3d map data types.
- * The offsets sets the number of boundary cols, rows and depths.
- * This option is useful to generate homogeneous Neumann boundary conditions around
- * an array or to establish overlapping boundaries. The arrays are initialized with 0 by default.
- * <br><br>
- * If the offset is greater then 0, negative indices are possible.
- * The data structure of a array with 3 depths, rows and cols and an offset of 1
- * will looks like this:
- *
- \verbatim
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 1 2 0
- 0 3 4 5 0
- 0 6 7 8 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 9 10 11 0
- 0 12 13 14 0
- 0 15 16 17 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 18 19 20 0
- 0 21 22 23 0
- 0 24 25 26 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- 0 0 0 0 0
- \endverbatim
- The depth counts from the bottom to the top.
- * <br><br>
- * Internal a one dimensional array is allocated to speed up the memory access.
- * To access the dimensional array with a three dimensional indexing use the provided
- * get and put functions.
- *
- * \param cols int
- * \param rows int
- * \param depths int
- * \param offset int
- * \param type int
- * \return N_array_3d *
- *
- * */
- N_array_3d *N_alloc_array_3d(int cols, int rows, int depths, int offset,
- int type)
- {
- N_array_3d *data = NULL;
- if (rows < 1 || cols < 1 || depths < 1)
- G_fatal_error
- ("N_alloc_array_3d: depths, cols and rows should be > 0");
- if (type != DCELL_TYPE && type != FCELL_TYPE)
- G_fatal_error
- ("N_alloc_array_3d: Wrong data type, should be FCELL_TYPE or DCELL_TYPE");
- data = (N_array_3d *) G_calloc(1, sizeof(N_array_3d));
- data->cols = cols;
- data->rows = rows;
- data->depths = depths;
- data->type = type;
- data->offset = offset;
- data->rows_intern = rows + 2 * offset;
- data->cols_intern = cols + 2 * offset;
- data->depths_intern = depths + 2 * offset;
- data->fcell_array = NULL;
- data->dcell_array = NULL;
- if (data->type == FCELL_TYPE) {
- data->fcell_array =
- (float *)G_calloc((size_t) data->depths_intern * data->rows_intern *
- data->cols_intern, sizeof(float));
- G_debug(3,
- "N_alloc_array_3d: float array allocated rows_intern %i cols_intern %i depths_intern %i offset %i",
- data->rows_intern, data->cols_intern, data->depths_intern,
- data->offset = offset);
- }
- else if (data->type == DCELL_TYPE) {
- data->dcell_array =
- (double *)G_calloc((size_t) data->depths_intern * data->rows_intern *
- data->cols_intern, sizeof(double));
- G_debug(3,
- "N_alloc_array_3d: double array allocated rows_intern %i cols_intern %i depths_intern %i offset %i",
- data->rows_intern, data->cols_intern, data->depths_intern,
- data->offset = offset);
- }
- return data;
- }
- /*!
- * \brief Release the memory of a N_array_3d
- *
- * \param data N_array_3d *
- * \return void
- * */
- void N_free_array_3d(N_array_3d * data)
- {
- if (data != NULL) {
- G_debug(3, "N_free_array_3d: free N_array_3d");
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- G_free(data->fcell_array);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- G_free(data->dcell_array);
- }
- G_free(data);
- data = NULL;
- }
- return;
- }
- /*!
- * \brief Return the data type of the N_array_3d
- *
- * The data type can be FCELL_TYPE and DCELL_TYPE accordingly to the raster map data types.
- *
- * \param array N_array_3d *
- * \return type int -- FCELL_TYPE or DCELL_TYPE
- * */
- int N_get_array_3d_type(N_array_3d * array)
- {
- return array->type;
- }
- /*!
- * \brief This function writes the value of N_array_3d data at position col, row, depth
- * to the variable value
- *
- * The value must be from the same type as the array. Otherwise you will risk data losses.
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \param value void *
- * \return void
- * */
- void
- N_get_array_3d_value(N_array_3d * data, int col, int row, int depth,
- void *value)
- {
- if (data->offset == 0) {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- *((float *)value) =
- data->fcell_array[depth *
- (data->rows_intern * data->cols_intern) +
- row * data->cols_intern + col];
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- *((double *)value) =
- data->dcell_array[depth *
- (data->rows_intern * data->cols_intern) +
- row * data->cols_intern + col];
- }
- }
- else {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- *((float *)value) =
- data->fcell_array[(depth + data->offset) *
- (data->rows_intern * data->cols_intern) +
- (row + data->offset) * data->cols_intern +
- (col + data->offset)];
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- *((double *)value) =
- data->dcell_array[(depth + data->offset) *
- (data->rows_intern * data->cols_intern) +
- (row + data->offset) * data->cols_intern +
- (col + data->offset)];
- }
- }
- return;
- }
- /*!
- * \brief This function returns 1 if value of N_array_3d data at position col, row, depth
- * is of type null, otherwise 0
- *
- * This function checks automatically the type of the array and checks for the
- * data type null value.
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \return void
- * */
- int N_is_array_3d_value_null(N_array_3d * data, int col, int row, int depth)
- {
- if (data->offset == 0) {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- G_debug(6,
- "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
- depth, row, col);
- return Rast3d_is_null_value_num((void *)
- &(data->
- fcell_array[depth *
- (data->rows_intern *
- data->cols_intern) +
- row * data->cols_intern +
- col]), FCELL_TYPE);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- G_debug(6,
- "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
- depth, row, col);
- return Rast3d_is_null_value_num((void *)
- &(data->
- dcell_array[depth *
- (data->rows_intern *
- data->cols_intern) +
- row * data->cols_intern +
- col]), DCELL_TYPE);
- }
- }
- else {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- G_debug(6,
- "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
- depth, row, col);
- return Rast3d_is_null_value_num((void *)
- &(data->
- fcell_array[(depth +
- data->offset) *
- (data->rows_intern *
- data->cols_intern) +
- (row + data->offset)
- * data->cols_intern +
- (col + data->offset)]),
- FCELL_TYPE);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- G_debug(6,
- "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
- depth, row, col);
- return Rast3d_is_null_value_num((void *)
- &(data->
- dcell_array[(depth +
- data->offset) *
- (data->rows_intern *
- data->cols_intern) +
- (row +
- data->offset) *
- data->cols_intern + (col +
- data->
- offset)]),
- DCELL_TYPE);
- }
- }
- return 0;
- }
- /*!
- * \brief This function returns the value of type float at position col, row, depth
- *
- * The data type can be FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \return float
- *
- * */
- float N_get_array_3d_f_value(N_array_3d * data, int col, int row, int depth)
- {
- float fvalue = 0.0;
- double dvalue = 0.0;
- switch (data->type) {
- case FCELL_TYPE:
- N_get_array_3d_value(data, col, row, depth, (void *)&fvalue);
- return (float)fvalue;
- case DCELL_TYPE:
- N_get_array_3d_value(data, col, row, depth, (void *)&dvalue);
- return (float)dvalue;
- }
- return fvalue;
- }
- /*!
- * \brief This function returns the value of type float at position col, row, depth
- *
- * The data type can be FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \return double
- *
- * */
- double N_get_array_3d_d_value(N_array_3d * data, int col, int row, int depth)
- {
- float fvalue = 0.0;
- double dvalue = 0.0;
- switch (data->type) {
- case FCELL_TYPE:
- N_get_array_3d_value(data, col, row, depth, (void *)&fvalue);
- return (double)fvalue;
- case DCELL_TYPE:
- N_get_array_3d_value(data, col, row, depth, (void *)&dvalue);
- return (double)dvalue;
- }
- return dvalue;
- }
- /*!
- * \brief This function writes a value to the N_array_3d data at position col, row, depth
- *
- * The value will be automatically cast to the array type.
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \param value cahr *
- * \return void
- * */
- void
- N_put_array_3d_value(N_array_3d * data, int col, int row, int depth,
- char *value)
- {
- G_debug(6, "N_put_array_3d_value: put value to array at pos [%i][%i][%i]",
- depth, row, col);
- if (data->offset == 0) {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- data->fcell_array[depth *
- (data->rows_intern * data->cols_intern) +
- row * data->cols_intern + col]
- = *((float *)value);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- data->dcell_array[depth *
- (data->rows_intern * data->cols_intern) +
- row * data->cols_intern + col]
- = *((double *)value);
- }
- }
- else {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- data->fcell_array[(depth + data->offset) *
- (data->rows_intern * data->cols_intern) + (row +
- data->
- offset)
- * data->cols_intern + (col + data->offset)] =
- *((float *)value);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- data->dcell_array[(depth + data->offset) *
- (data->rows_intern * data->cols_intern) + (row +
- data->
- offset)
- * data->cols_intern + (col + data->offset)] =
- *((double *)value);
- }
- }
- return;
- }
- /*!
- * \brief This function writes a null value to the N_array_3d data at position col, row, depth
- *
- * The null value will be automatically set to the array type.
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \return void
- * */
- void N_put_array_3d_value_null(N_array_3d * data, int col, int row, int depth)
- {
- G_debug(6,
- "N_put_array_3d_value_null: put null value to array at pos [%i][%i][%i]",
- depth, row, col);
- if (data->offset == 0) {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- Rast3d_set_null_value((void *)
- &(data->
- fcell_array[depth *
- (data->rows_intern *
- data->cols_intern) +
- row * data->cols_intern + col]), 1,
- FCELL_TYPE);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- Rast3d_set_null_value((void *)
- &(data->
- dcell_array[depth *
- (data->rows_intern *
- data->cols_intern) +
- row * data->cols_intern + col]), 1,
- DCELL_TYPE);
- }
- }
- else {
- if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
- Rast3d_set_null_value((void *)
- &(data->
- fcell_array[(depth +
- data->offset) *
- (data->rows_intern *
- data->cols_intern) + (row +
- data->
- offset) *
- data->cols_intern + (col +
- data->
- offset)]), 1,
- FCELL_TYPE);
- }
- else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
- Rast3d_set_null_value((void *)
- &(data->
- dcell_array[(depth +
- data->offset) *
- (data->rows_intern *
- data->cols_intern) + (row +
- data->
- offset) *
- data->cols_intern + (col +
- data->
- offset)]), 1,
- DCELL_TYPE);
- }
- }
- return;
- }
- /*!
- * \brief This function writes a float value to the N_array_3d data at position col, row, depth
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \param value float
- * \return void
- * */
- void
- N_put_array_3d_f_value(N_array_3d * data, int col, int row, int depth,
- float value)
- {
- double dval;
- if (data->type == DCELL_TYPE) {
- dval = (double)value;
- N_put_array_3d_value(data, col, row, depth, (void *)&dval);
- }
- else {
- N_put_array_3d_value(data, col, row, depth, (void *)&value);
- }
- return;
- }
- /*!
- * \brief Writes a double value to the N_array_3d struct at position col, row, depth
- *
- * \param data N_array_3d *
- * \param col int
- * \param row int
- * \param depth int
- * \param value double
- * \return void
- * */
- void
- N_put_array_3d_d_value(N_array_3d * data, int col, int row, int depth,
- double value)
- {
- float fval;
- if (data->type == FCELL_TYPE) {
- fval = (double)value;
- N_put_array_3d_value(data, col, row, depth, (void *)&fval);
- }
- else {
- N_put_array_3d_value(data, col, row, depth, (void *)&value);
- }
- return;
- }
- /*!
- * \brief Write the info of the array to stdout
- *
- * \param data N_array_3d *
- * \return void
- * */
- void N_print_array_3d_info(N_array_3d * data)
- {
- fprintf(stdout, "N_array_3d \n");
- fprintf(stdout, "Cols %i\n", data->cols);
- fprintf(stdout, "Rows: %i\n", data->rows);
- fprintf(stdout, "Depths: %i\n", data->depths);
- fprintf(stdout, "Array type: %i\n", data->type);
- fprintf(stdout, "Offset: %i\n", data->offset);
- fprintf(stdout, "Internal cols: %i\n", data->cols_intern);
- fprintf(stdout, "Internal rows: %i\n", data->rows_intern);
- fprintf(stdout, "Internal depths: %i\n", data->depths_intern);
- fprintf(stdout, "FCELL array pointer: %p\n", data->fcell_array);
- fprintf(stdout, "DCELL array pointer: %p\n", data->dcell_array);
- return;
- }
- /*!
- * \brief Write info and content of the array data to stdout
- *
- * Offsets are ignored
- *
- * \param data N_array_2d *
- * \return void
- * */
- void N_print_array_3d(N_array_3d * data)
- {
- int i, j, k;
- N_print_array_3d_info(data);
- for (k = 0; k < data->depths; k++) {
- for (j = 0; j < data->rows; j++) {
- for (i = 0; i < data->cols; i++) {
- if (data->type == FCELL_TYPE)
- printf("%6.6f ", N_get_array_3d_f_value(data, i, j, k));
- else if (data->type == DCELL_TYPE)
- printf("%6.6f ", N_get_array_3d_d_value(data, i, j, k));
- }
- printf("\n");
- }
- printf("\n");
- }
- printf("\n");
- return;
- }
|