n_arrays.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass PDE Numerical Library
  4. * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
  5. * soerengebbert <at> gmx <dot> de
  6. *
  7. * PURPOSE: Array management functions
  8. * part of the gpde library
  9. *
  10. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General Public
  13. * License (>=v2). Read the file COPYING that comes with GRASS
  14. * for details.
  15. *
  16. *****************************************************************************/
  17. #include <math.h>
  18. #include <grass/N_pde.h>
  19. #include <grass/raster.h>
  20. #include <grass/glocale.h>
  21. /* ******************** 2D ARRAY FUNCTIONS *********************** */
  22. /*!
  23. * \brief Allocate memory for a N_array_2d data structure.
  24. *
  25. * This function allocates memory for an array of type N_array_2d
  26. * and returns the pointer to the new allocated memory.
  27. * <br><br>
  28. * The data type of this array is set by "type" and must be
  29. * CELL_TYPE, FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
  30. * The offset sets the number of boundary cols and rows.
  31. * This option is useful to generate homogeneous Neumann boundary conditions around
  32. * an array or to establish overlapping boundaries. The array is initialized with 0 by default.
  33. * <br><br>
  34. * If the offset is greater then 0, negative indices are possible.
  35. * <br><br>
  36. *
  37. * The data structure of a array with 3 rows and cols and an offset of 1
  38. * will looks like this:
  39. * <br><br>
  40. *
  41. \verbatim
  42. 0 0 0 0 0
  43. 0 0 1 2 0
  44. 0 3 4 5 0
  45. 0 6 7 8 0
  46. 0 0 0 0 0
  47. \endverbatim
  48. *
  49. * 0 is the boundary.
  50. * <br><br>
  51. * Internal a one dimensional array is allocated to save memory and to speed up the memory access.
  52. * To access the one dimensional array with a two dimensional index use the provided
  53. * get and put functions. The internal representation of the above data will look like this:
  54. *
  55. \verbatim
  56. 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
  57. \endverbatim
  58. *
  59. * \param cols int
  60. * \param rows int
  61. * \param offset int
  62. * \param type int
  63. * \return N_array_2d *
  64. *
  65. * */
  66. N_array_2d *N_alloc_array_2d(int cols, int rows, int offset, int type)
  67. {
  68. N_array_2d *data = NULL;
  69. if (rows < 1 || cols < 1)
  70. G_fatal_error("N_alloc_array_2d: cols and rows should be > 0");
  71. if (type != CELL_TYPE && type != FCELL_TYPE && type != DCELL_TYPE)
  72. G_fatal_error
  73. ("N_alloc_array_2d: Wrong data type, should be CELL_TYPE, FCELL_TYPE or DCELL_TYPE");
  74. data = (N_array_2d *) G_calloc(1, sizeof(N_array_2d));
  75. data->cols = cols;
  76. data->rows = rows;
  77. data->type = type;
  78. data->offset = offset;
  79. data->rows_intern = rows + 2 * offset; /*offset position at booth sides */
  80. data->cols_intern = cols + 2 * offset; /*offset position at booth sides */
  81. data->cell_array = NULL;
  82. data->fcell_array = NULL;
  83. data->dcell_array = NULL;
  84. if (data->type == CELL_TYPE) {
  85. data->cell_array =
  86. (CELL *) G_calloc((size_t) data->rows_intern * data->cols_intern,
  87. sizeof(CELL));
  88. G_debug(3,
  89. "N_alloc_array_2d: CELL array allocated rows_intern %i cols_intern %i offset %i",
  90. data->rows_intern, data->cols_intern, data->offset = offset);
  91. }
  92. else if (data->type == FCELL_TYPE) {
  93. data->fcell_array =
  94. (FCELL *) G_calloc((size_t) data->rows_intern * data->cols_intern,
  95. sizeof(FCELL));
  96. G_debug(3,
  97. "N_alloc_array_2d: FCELL array allocated rows_intern %i cols_intern %i offset %i",
  98. data->rows_intern, data->cols_intern, data->offset = offset);
  99. }
  100. else if (data->type == DCELL_TYPE) {
  101. data->dcell_array =
  102. (DCELL *) G_calloc((size_t) data->rows_intern * data->cols_intern,
  103. sizeof(DCELL));
  104. G_debug(3,
  105. "N_alloc_array_2d: DCELL array allocated rows_intern %i cols_intern %i offset %i",
  106. data->rows_intern, data->cols_intern, data->offset = offset);
  107. }
  108. return data;
  109. }
  110. /*!
  111. * \brief Release the memory of a N_array_2d structure
  112. *
  113. * \param data N_array_2d *
  114. * \return void
  115. * */
  116. void N_free_array_2d(N_array_2d * data)
  117. {
  118. if (data != NULL) {
  119. G_debug(3, "N_free_array_2d: free N_array_2d");
  120. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  121. G_free(data->cell_array);
  122. }
  123. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  124. G_free(data->fcell_array);
  125. }
  126. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  127. G_free(data->dcell_array);
  128. }
  129. G_free(data);
  130. data = NULL;
  131. }
  132. return;
  133. }
  134. /*!
  135. * \brief Return the data type of the N_array_2d struct
  136. *
  137. * The data type can be CELL_TYPE, FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
  138. *
  139. * \param array N_array_2d *
  140. * \return type int
  141. * */
  142. int N_get_array_2d_type(N_array_2d * array)
  143. {
  144. return array->type;
  145. }
  146. /*!
  147. * \brief Write the value of the N_array_2d struct at position col, row to value
  148. *
  149. * The value must be of the same type as the array. Otherwise you will risk data losses.
  150. *
  151. * \param data N_array_2d *
  152. * \param col int
  153. * \param row int
  154. * \param value void * - this variable contains the array value at col, row position
  155. * \return void
  156. * */
  157. void N_get_array_2d_value(N_array_2d * data, int col, int row, void *value)
  158. {
  159. if (data->offset == 0) {
  160. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  161. *((CELL *) value) =
  162. data->cell_array[row * data->cols_intern + col];
  163. }
  164. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  165. *((FCELL *) value) =
  166. data->fcell_array[row * data->cols_intern + col];
  167. }
  168. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  169. *((DCELL *) value) =
  170. data->dcell_array[row * data->cols_intern + col];
  171. }
  172. }
  173. else {
  174. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  175. *((CELL *) value) =
  176. data->cell_array[(row + data->offset) * data->cols_intern +
  177. col + data->offset];
  178. }
  179. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  180. *((FCELL *) value) =
  181. data->fcell_array[(row + data->offset) * data->cols_intern +
  182. col + data->offset];
  183. }
  184. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  185. *((DCELL *) value) =
  186. data->dcell_array[(row + data->offset) * data->cols_intern +
  187. col + data->offset];
  188. }
  189. }
  190. return;
  191. }
  192. /*!
  193. * \brief Returns 1 if the value of N_array_2d struct at position col, row
  194. * is of type null, otherwise 0
  195. *
  196. * This function checks automatically the type of the array and checks for the
  197. * data type null value.
  198. *
  199. * \param data N_array_2d *
  200. * \param col int
  201. * \param row int
  202. * \return int - 1 = is null, 0 otherwise
  203. * */
  204. int N_is_array_2d_value_null(N_array_2d * data, int col, int row)
  205. {
  206. if (data->offset == 0) {
  207. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  208. G_debug(6,
  209. "N_is_array_2d_value_null: null value is of type CELL at pos [%i][%i]",
  210. col, row);
  211. return Rast_is_null_value((void *)
  212. &(data->
  213. cell_array[row * data->cols_intern +
  214. col]), CELL_TYPE);
  215. }
  216. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  217. G_debug(6,
  218. "N_is_array_2d_value_null: null value is of type FCELL at pos [%i][%i]",
  219. col, row);
  220. return Rast_is_null_value((void *)
  221. &(data->
  222. fcell_array[row * data->cols_intern +
  223. col]), FCELL_TYPE);
  224. }
  225. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  226. G_debug(6,
  227. "N_is_array_2d_value_null: null value is of type DCELL at pos [%i][%i]",
  228. col, row);
  229. return Rast_is_null_value((void *)
  230. &(data->
  231. dcell_array[row * data->cols_intern +
  232. col]), DCELL_TYPE);
  233. }
  234. }
  235. else {
  236. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  237. G_debug(6,
  238. "N_is_array_2d_value_null: null value is of type CELL at pos [%i][%i]",
  239. col, row);
  240. return Rast_is_null_value((void *)
  241. &(data->
  242. cell_array[(row +
  243. data->offset) *
  244. data->cols_intern + col +
  245. data->offset]), CELL_TYPE);
  246. }
  247. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  248. G_debug(6,
  249. "N_is_array_2d_value_null: null value is of type FCELL at pos [%i][%i]",
  250. col, row);
  251. return Rast_is_null_value((void *)
  252. &(data->
  253. fcell_array[(row +
  254. data->offset) *
  255. data->cols_intern + col +
  256. data->offset]), FCELL_TYPE);
  257. }
  258. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  259. G_debug(6,
  260. "N_is_array_2d_value_null: null value is of type DCELL at pos [%i][%i]",
  261. col, row);
  262. return Rast_is_null_value((void *)
  263. &(data->
  264. dcell_array[(row +
  265. data->offset) *
  266. data->cols_intern + col +
  267. data->offset]), DCELL_TYPE);
  268. }
  269. }
  270. return 0;
  271. }
  272. /*!
  273. * \brief Returns the value of type CELL at position col, row
  274. *
  275. * The data array can be of type CELL, FCELL or DCELL, the value will be casted to the CELL type.
  276. *
  277. * \param data N_array_2d *
  278. * \param col int
  279. * \param row int
  280. * \return CELL
  281. *
  282. * */
  283. CELL N_get_array_2d_c_value(N_array_2d * data, int col, int row)
  284. {
  285. CELL value = 0;
  286. FCELL fvalue = 0.0;
  287. DCELL dvalue = 0.0;
  288. switch (data->type) {
  289. case CELL_TYPE:
  290. N_get_array_2d_value(data, col, row, (void *)&value);
  291. return (CELL) value;
  292. case FCELL_TYPE:
  293. N_get_array_2d_value(data, col, row, (void *)&fvalue);
  294. return (CELL) fvalue;
  295. case DCELL_TYPE:
  296. N_get_array_2d_value(data, col, row, (void *)&dvalue);
  297. return (CELL) dvalue;
  298. }
  299. return value;
  300. }
  301. /*!
  302. * \brief Returns the value of type FCELL at position col, row
  303. *
  304. * The data array can be of type CELL, FCELL or DCELL, the value will be casted to the FCELL type.
  305. *
  306. * \param data N_array_2d *
  307. * \param col int
  308. * \param row int
  309. * \return FCELL
  310. * */
  311. FCELL N_get_array_2d_f_value(N_array_2d * data, int col, int row)
  312. {
  313. CELL value = 0;
  314. FCELL fvalue = 0.0;
  315. DCELL dvalue = 0.0;
  316. switch (data->type) {
  317. case CELL_TYPE:
  318. N_get_array_2d_value(data, col, row, (void *)&value);
  319. return (FCELL) value;
  320. case FCELL_TYPE:
  321. N_get_array_2d_value(data, col, row, (void *)&fvalue);
  322. return (FCELL) fvalue;
  323. case DCELL_TYPE:
  324. N_get_array_2d_value(data, col, row, (void *)&dvalue);
  325. return (FCELL) dvalue;
  326. }
  327. return fvalue;
  328. }
  329. /*!
  330. * \brief Returns the value of type DCELL at position col, row
  331. *
  332. * The data array can be of type CELL, FCELL or DCELL, the value will be casted to the DCELL type.
  333. *
  334. * \param data N_array_2d *
  335. * \param col int
  336. * \param row int
  337. * \return DCELL
  338. *
  339. * */
  340. DCELL N_get_array_2d_d_value(N_array_2d * data, int col, int row)
  341. {
  342. CELL value = 0;
  343. FCELL fvalue = 0.0;
  344. DCELL dvalue = 0.0;
  345. switch (data->type) {
  346. case CELL_TYPE:
  347. N_get_array_2d_value(data, col, row, (void *)&value);
  348. return (DCELL) value;
  349. case FCELL_TYPE:
  350. N_get_array_2d_value(data, col, row, (void *)&fvalue);
  351. return (DCELL) fvalue;
  352. case DCELL_TYPE:
  353. N_get_array_2d_value(data, col, row, (void *)&dvalue);
  354. return (DCELL) dvalue;
  355. }
  356. return dvalue;
  357. }
  358. /*!
  359. * \brief Writes a value to the N_array_2d struct at position col, row
  360. *
  361. * The value will be automatically cast to the array type.
  362. *
  363. * \param data N_array_2d *
  364. * \param col int
  365. * \param row int
  366. * \param value char *
  367. * \return void
  368. * */
  369. void N_put_array_2d_value(N_array_2d * data, int col, int row, char *value)
  370. {
  371. G_debug(6, "N_put_array_2d_value: put value to array");
  372. if (data->offset == 0) {
  373. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  374. data->cell_array[row * data->cols_intern + col] =
  375. *((CELL *) value);
  376. }
  377. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  378. data->fcell_array[row * data->cols_intern + col] =
  379. *((FCELL *) value);
  380. }
  381. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  382. data->dcell_array[row * data->cols_intern + col] =
  383. *((DCELL *) value);
  384. }
  385. }
  386. else {
  387. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  388. data->cell_array[(row + data->offset) * data->cols_intern + col +
  389. data->offset] = *((CELL *) value);
  390. }
  391. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  392. data->fcell_array[(row + data->offset) * data->cols_intern + col +
  393. data->offset] = *((FCELL *) value);
  394. }
  395. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  396. data->dcell_array[(row + data->offset) * data->cols_intern + col +
  397. data->offset] = *((DCELL *) value);
  398. }
  399. }
  400. return;
  401. }
  402. /*!
  403. * \brief Writes the null value to the N_array_2d struct at position col, row
  404. *
  405. * The null value will be automatically set to the array data type (CELL, FCELL or DCELL).
  406. *
  407. * \param data N_array_2d *
  408. * \param col int
  409. * \param row int
  410. * \return void
  411. * */
  412. void N_put_array_2d_value_null(N_array_2d * data, int col, int row)
  413. {
  414. G_debug(6,
  415. "N_put_array_2d_value_null: put null value to array pos [%i][%i]",
  416. col, row);
  417. if (data->offset == 0) {
  418. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  419. Rast_set_c_null_value((void *)
  420. &(data->
  421. cell_array[row * data->cols_intern + col]),
  422. 1);
  423. }
  424. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  425. Rast_set_f_null_value((void *)
  426. &(data->
  427. fcell_array[row * data->cols_intern + col]),
  428. 1);
  429. }
  430. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  431. Rast_set_d_null_value((void *)
  432. &(data->
  433. dcell_array[row * data->cols_intern + col]),
  434. 1);
  435. }
  436. }
  437. else {
  438. if (data->type == CELL_TYPE && data->cell_array != NULL) {
  439. Rast_set_c_null_value((void *)
  440. &(data->
  441. cell_array[(row +
  442. data->offset) *
  443. data->cols_intern + col +
  444. data->offset]), 1);
  445. }
  446. else if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  447. Rast_set_f_null_value((void *)
  448. &(data->
  449. fcell_array[(row +
  450. data->offset) *
  451. data->cols_intern + col +
  452. data->offset]), 1);
  453. }
  454. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  455. Rast_set_d_null_value((void *)
  456. &(data->
  457. dcell_array[(row +
  458. data->offset) *
  459. data->cols_intern + col +
  460. data->offset]), 1);
  461. }
  462. }
  463. return;
  464. }
  465. /*!
  466. * \brief Writes a CELL value to the N_array_2d struct at position col, row
  467. *
  468. * \param data N_array_2d *
  469. * \param col int
  470. * \param row int
  471. * \param value CELL
  472. * \return void
  473. * */
  474. void N_put_array_2d_c_value(N_array_2d * data, int col, int row, CELL value)
  475. {
  476. FCELL fvalue;
  477. DCELL dvalue;
  478. switch (data->type) {
  479. case FCELL_TYPE:
  480. fvalue = (FCELL) value;
  481. N_put_array_2d_value(data, col, row, (char *)&fvalue);
  482. return;
  483. case DCELL_TYPE:
  484. dvalue = (DCELL) value;
  485. N_put_array_2d_value(data, col, row, (char *)&dvalue);
  486. return;
  487. }
  488. N_put_array_2d_value(data, col, row, (char *)&value);
  489. return;
  490. }
  491. /*!
  492. * \brief Writes a FCELL value to the N_array_2d struct at position col, row
  493. *
  494. * \param data N_array_2d *
  495. * \param col int
  496. * \param row int
  497. * \param value FCELL
  498. * \return void
  499. * */
  500. void N_put_array_2d_f_value(N_array_2d * data, int col, int row, FCELL value)
  501. {
  502. CELL cvalue;
  503. DCELL dvalue;
  504. switch (data->type) {
  505. case CELL_TYPE:
  506. cvalue = (CELL) value;
  507. N_put_array_2d_value(data, col, row, (char *)&cvalue);
  508. return;
  509. case DCELL_TYPE:
  510. dvalue = (DCELL) value;
  511. N_put_array_2d_value(data, col, row, (char *)&dvalue);
  512. return;
  513. }
  514. N_put_array_2d_value(data, col, row, (char *)&value);
  515. return;
  516. }
  517. /*!
  518. * \brief Writes a DCELL value to the N_array_2d struct at position col, row
  519. *
  520. * \param data N_array_2d *
  521. * \param col int
  522. * \param row int
  523. * \param value DCELL
  524. * \return void
  525. * */
  526. void N_put_array_2d_d_value(N_array_2d * data, int col, int row, DCELL value)
  527. {
  528. CELL cvalue;
  529. FCELL fvalue;
  530. switch (data->type) {
  531. case CELL_TYPE:
  532. cvalue = (CELL) value;
  533. N_put_array_2d_value(data, col, row, (char *)&cvalue);
  534. return;
  535. case FCELL_TYPE:
  536. fvalue = (FCELL) value;
  537. N_put_array_2d_value(data, col, row, (char *)&fvalue);
  538. return;
  539. }
  540. N_put_array_2d_value(data, col, row, (char *)&value);
  541. return;
  542. }
  543. /*!
  544. * \brief This function writes the data info of the array data to stdout
  545. *
  546. * \param data N_array_2d *
  547. * \return void
  548. * */
  549. void N_print_array_2d_info(N_array_2d * data)
  550. {
  551. fprintf(stdout, "N_array_2d \n");
  552. fprintf(stdout, "Cols %i\n", data->cols);
  553. fprintf(stdout, "Rows: %i\n", data->rows);
  554. fprintf(stdout, "Array type: %i\n", data->type);
  555. fprintf(stdout, "Offset: %i\n", data->offset);
  556. fprintf(stdout, "Internal cols: %i\n", data->cols_intern);
  557. fprintf(stdout, "Internal rows: %i\n", data->rows_intern);
  558. fprintf(stdout, "CELL array pointer: %p\n", data->cell_array);
  559. fprintf(stdout, "FCELL array pointer: %p\n", data->fcell_array);
  560. fprintf(stdout, "DCELL array pointer: %p\n", data->dcell_array);
  561. return;
  562. }
  563. /*!
  564. * \brief Write info and content of the N_array_2d struct to stdout
  565. *
  566. * Offsets are ignored
  567. *
  568. * \param data N_array_2d *
  569. * \return void
  570. * */
  571. void N_print_array_2d(N_array_2d * data)
  572. {
  573. int i, j;
  574. N_print_array_2d_info(data);
  575. for (j = 0 - data->offset; j < data->rows + data->offset; j++) {
  576. for (i = 0 - data->offset; i < data->cols + data->offset; i++) {
  577. if (data->type == CELL_TYPE)
  578. fprintf(stdout, "%6d ", N_get_array_2d_c_value(data, i, j));
  579. else if (data->type == FCELL_TYPE)
  580. fprintf(stdout, "%6.6f ", N_get_array_2d_f_value(data, i, j));
  581. else if (data->type == DCELL_TYPE)
  582. printf("%6.6f ", N_get_array_2d_d_value(data, i, j));
  583. }
  584. fprintf(stdout, "\n");
  585. }
  586. fprintf(stdout, "\n");
  587. return;
  588. }
  589. /* ******************** 3D ARRAY FUNCTIONS *********************** */
  590. /*!
  591. * \brief Allocate memory for a N_array_3d data structure.
  592. *
  593. * This functions allocates an array of type N_array_3d and returns a pointer
  594. * to the new allocated memory.
  595. * <br><br>
  596. * The data type of this array set by "type" must be
  597. * FCELL_TYPE or DCELL_TYPE accordingly to the raster3d map data types.
  598. * The offsets sets the number of boundary cols, rows and depths.
  599. * This option is useful to generate homogeneous Neumann boundary conditions around
  600. * an array or to establish overlapping boundaries. The arrays are initialized with 0 by default.
  601. * <br><br>
  602. * If the offset is greater then 0, negative indices are possible.
  603. * The data structure of a array with 3 depths, rows and cols and an offset of 1
  604. * will looks like this:
  605. *
  606. \verbatim
  607. 0 0 0 0 0
  608. 0 0 0 0 0
  609. 0 0 0 0 0
  610. 0 0 0 0 0
  611. 0 0 0 0 0
  612. 0 0 0 0 0
  613. 0 0 1 2 0
  614. 0 3 4 5 0
  615. 0 6 7 8 0
  616. 0 0 0 0 0
  617. 0 0 0 0 0
  618. 0 9 10 11 0
  619. 0 12 13 14 0
  620. 0 15 16 17 0
  621. 0 0 0 0 0
  622. 0 0 0 0 0
  623. 0 18 19 20 0
  624. 0 21 22 23 0
  625. 0 24 25 26 0
  626. 0 0 0 0 0
  627. 0 0 0 0 0
  628. 0 0 0 0 0
  629. 0 0 0 0 0
  630. 0 0 0 0 0
  631. 0 0 0 0 0
  632. \endverbatim
  633. The depth counts from the bottom to the top.
  634. * <br><br>
  635. * Internal a one dimensional array is allocated to speed up the memory access.
  636. * To access the dimensional array with a three dimensional indexing use the provided
  637. * get and put functions.
  638. *
  639. * \param cols int
  640. * \param rows int
  641. * \param depths int
  642. * \param offset int
  643. * \param type int
  644. * \return N_array_3d *
  645. *
  646. * */
  647. N_array_3d *N_alloc_array_3d(int cols, int rows, int depths, int offset,
  648. int type)
  649. {
  650. N_array_3d *data = NULL;
  651. if (rows < 1 || cols < 1 || depths < 1)
  652. G_fatal_error
  653. ("N_alloc_array_3d: depths, cols and rows should be > 0");
  654. if (type != DCELL_TYPE && type != FCELL_TYPE)
  655. G_fatal_error
  656. ("N_alloc_array_3d: Wrong data type, should be FCELL_TYPE or DCELL_TYPE");
  657. data = (N_array_3d *) G_calloc(1, sizeof(N_array_3d));
  658. data->cols = cols;
  659. data->rows = rows;
  660. data->depths = depths;
  661. data->type = type;
  662. data->offset = offset;
  663. data->rows_intern = rows + 2 * offset;
  664. data->cols_intern = cols + 2 * offset;
  665. data->depths_intern = depths + 2 * offset;
  666. data->fcell_array = NULL;
  667. data->dcell_array = NULL;
  668. if (data->type == FCELL_TYPE) {
  669. data->fcell_array =
  670. (float *)G_calloc((size_t) data->depths_intern * data->rows_intern *
  671. data->cols_intern, sizeof(float));
  672. G_debug(3,
  673. "N_alloc_array_3d: float array allocated rows_intern %i cols_intern %i depths_intern %i offset %i",
  674. data->rows_intern, data->cols_intern, data->depths_intern,
  675. data->offset = offset);
  676. }
  677. else if (data->type == DCELL_TYPE) {
  678. data->dcell_array =
  679. (double *)G_calloc((size_t) data->depths_intern * data->rows_intern *
  680. data->cols_intern, sizeof(double));
  681. G_debug(3,
  682. "N_alloc_array_3d: double array allocated rows_intern %i cols_intern %i depths_intern %i offset %i",
  683. data->rows_intern, data->cols_intern, data->depths_intern,
  684. data->offset = offset);
  685. }
  686. return data;
  687. }
  688. /*!
  689. * \brief Release the memory of a N_array_3d
  690. *
  691. * \param data N_array_3d *
  692. * \return void
  693. * */
  694. void N_free_array_3d(N_array_3d * data)
  695. {
  696. if (data != NULL) {
  697. G_debug(3, "N_free_array_3d: free N_array_3d");
  698. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  699. G_free(data->fcell_array);
  700. }
  701. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  702. G_free(data->dcell_array);
  703. }
  704. G_free(data);
  705. data = NULL;
  706. }
  707. return;
  708. }
  709. /*!
  710. * \brief Return the data type of the N_array_3d
  711. *
  712. * The data type can be FCELL_TYPE and DCELL_TYPE accordingly to the raster map data types.
  713. *
  714. * \param array N_array_3d *
  715. * \return type int -- FCELL_TYPE or DCELL_TYPE
  716. * */
  717. int N_get_array_3d_type(N_array_3d * array)
  718. {
  719. return array->type;
  720. }
  721. /*!
  722. * \brief This function writes the value of N_array_3d data at position col, row, depth
  723. * to the variable value
  724. *
  725. * The value must be from the same type as the array. Otherwise you will risk data losses.
  726. *
  727. * \param data N_array_3d *
  728. * \param col int
  729. * \param row int
  730. * \param depth int
  731. * \param value void *
  732. * \return void
  733. * */
  734. void
  735. N_get_array_3d_value(N_array_3d * data, int col, int row, int depth,
  736. void *value)
  737. {
  738. if (data->offset == 0) {
  739. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  740. *((float *)value) =
  741. data->fcell_array[depth *
  742. (data->rows_intern * data->cols_intern) +
  743. row * data->cols_intern + col];
  744. }
  745. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  746. *((double *)value) =
  747. data->dcell_array[depth *
  748. (data->rows_intern * data->cols_intern) +
  749. row * data->cols_intern + col];
  750. }
  751. }
  752. else {
  753. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  754. *((float *)value) =
  755. data->fcell_array[(depth + data->offset) *
  756. (data->rows_intern * data->cols_intern) +
  757. (row + data->offset) * data->cols_intern +
  758. (col + data->offset)];
  759. }
  760. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  761. *((double *)value) =
  762. data->dcell_array[(depth + data->offset) *
  763. (data->rows_intern * data->cols_intern) +
  764. (row + data->offset) * data->cols_intern +
  765. (col + data->offset)];
  766. }
  767. }
  768. return;
  769. }
  770. /*!
  771. * \brief This function returns 1 if value of N_array_3d data at position col, row, depth
  772. * is of type null, otherwise 0
  773. *
  774. * This function checks automatically the type of the array and checks for the
  775. * data type null value.
  776. *
  777. * \param data N_array_3d *
  778. * \param col int
  779. * \param row int
  780. * \param depth int
  781. * \return void
  782. * */
  783. int N_is_array_3d_value_null(N_array_3d * data, int col, int row, int depth)
  784. {
  785. if (data->offset == 0) {
  786. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  787. G_debug(6,
  788. "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
  789. depth, row, col);
  790. return Rast3d_is_null_value_num((void *)
  791. &(data->
  792. fcell_array[depth *
  793. (data->rows_intern *
  794. data->cols_intern) +
  795. row * data->cols_intern +
  796. col]), FCELL_TYPE);
  797. }
  798. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  799. G_debug(6,
  800. "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
  801. depth, row, col);
  802. return Rast3d_is_null_value_num((void *)
  803. &(data->
  804. dcell_array[depth *
  805. (data->rows_intern *
  806. data->cols_intern) +
  807. row * data->cols_intern +
  808. col]), DCELL_TYPE);
  809. }
  810. }
  811. else {
  812. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  813. G_debug(6,
  814. "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
  815. depth, row, col);
  816. return Rast3d_is_null_value_num((void *)
  817. &(data->
  818. fcell_array[(depth +
  819. data->offset) *
  820. (data->rows_intern *
  821. data->cols_intern) +
  822. (row + data->offset)
  823. * data->cols_intern +
  824. (col + data->offset)]),
  825. FCELL_TYPE);
  826. }
  827. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  828. G_debug(6,
  829. "N_is_array_3d_value_null: null value is of type DCELL_TYPE at pos [%i][%i][%i]",
  830. depth, row, col);
  831. return Rast3d_is_null_value_num((void *)
  832. &(data->
  833. dcell_array[(depth +
  834. data->offset) *
  835. (data->rows_intern *
  836. data->cols_intern) +
  837. (row +
  838. data->offset) *
  839. data->cols_intern + (col +
  840. data->
  841. offset)]),
  842. DCELL_TYPE);
  843. }
  844. }
  845. return 0;
  846. }
  847. /*!
  848. * \brief This function returns the value of type float at position col, row, depth
  849. *
  850. * The data type can be FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
  851. *
  852. * \param data N_array_3d *
  853. * \param col int
  854. * \param row int
  855. * \param depth int
  856. * \return float
  857. *
  858. * */
  859. float N_get_array_3d_f_value(N_array_3d * data, int col, int row, int depth)
  860. {
  861. float fvalue = 0.0;
  862. double dvalue = 0.0;
  863. switch (data->type) {
  864. case FCELL_TYPE:
  865. N_get_array_3d_value(data, col, row, depth, (void *)&fvalue);
  866. return (float)fvalue;
  867. case DCELL_TYPE:
  868. N_get_array_3d_value(data, col, row, depth, (void *)&dvalue);
  869. return (float)dvalue;
  870. }
  871. return fvalue;
  872. }
  873. /*!
  874. * \brief This function returns the value of type float at position col, row, depth
  875. *
  876. * The data type can be FCELL_TYPE or DCELL_TYPE accordingly to the raster map data types.
  877. *
  878. * \param data N_array_3d *
  879. * \param col int
  880. * \param row int
  881. * \param depth int
  882. * \return double
  883. *
  884. * */
  885. double N_get_array_3d_d_value(N_array_3d * data, int col, int row, int depth)
  886. {
  887. float fvalue = 0.0;
  888. double dvalue = 0.0;
  889. switch (data->type) {
  890. case FCELL_TYPE:
  891. N_get_array_3d_value(data, col, row, depth, (void *)&fvalue);
  892. return (double)fvalue;
  893. case DCELL_TYPE:
  894. N_get_array_3d_value(data, col, row, depth, (void *)&dvalue);
  895. return (double)dvalue;
  896. }
  897. return dvalue;
  898. }
  899. /*!
  900. * \brief This function writes a value to the N_array_3d data at position col, row, depth
  901. *
  902. * The value will be automatically cast to the array type.
  903. *
  904. * \param data N_array_3d *
  905. * \param col int
  906. * \param row int
  907. * \param depth int
  908. * \param value cahr *
  909. * \return void
  910. * */
  911. void
  912. N_put_array_3d_value(N_array_3d * data, int col, int row, int depth,
  913. char *value)
  914. {
  915. G_debug(6, "N_put_array_3d_value: put value to array at pos [%i][%i][%i]",
  916. depth, row, col);
  917. if (data->offset == 0) {
  918. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  919. data->fcell_array[depth *
  920. (data->rows_intern * data->cols_intern) +
  921. row * data->cols_intern + col]
  922. = *((float *)value);
  923. }
  924. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  925. data->dcell_array[depth *
  926. (data->rows_intern * data->cols_intern) +
  927. row * data->cols_intern + col]
  928. = *((double *)value);
  929. }
  930. }
  931. else {
  932. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  933. data->fcell_array[(depth + data->offset) *
  934. (data->rows_intern * data->cols_intern) + (row +
  935. data->
  936. offset)
  937. * data->cols_intern + (col + data->offset)] =
  938. *((float *)value);
  939. }
  940. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  941. data->dcell_array[(depth + data->offset) *
  942. (data->rows_intern * data->cols_intern) + (row +
  943. data->
  944. offset)
  945. * data->cols_intern + (col + data->offset)] =
  946. *((double *)value);
  947. }
  948. }
  949. return;
  950. }
  951. /*!
  952. * \brief This function writes a null value to the N_array_3d data at position col, row, depth
  953. *
  954. * The null value will be automatically set to the array type.
  955. *
  956. * \param data N_array_3d *
  957. * \param col int
  958. * \param row int
  959. * \param depth int
  960. * \return void
  961. * */
  962. void N_put_array_3d_value_null(N_array_3d * data, int col, int row, int depth)
  963. {
  964. G_debug(6,
  965. "N_put_array_3d_value_null: put null value to array at pos [%i][%i][%i]",
  966. depth, row, col);
  967. if (data->offset == 0) {
  968. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  969. Rast3d_set_null_value((void *)
  970. &(data->
  971. fcell_array[depth *
  972. (data->rows_intern *
  973. data->cols_intern) +
  974. row * data->cols_intern + col]), 1,
  975. FCELL_TYPE);
  976. }
  977. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  978. Rast3d_set_null_value((void *)
  979. &(data->
  980. dcell_array[depth *
  981. (data->rows_intern *
  982. data->cols_intern) +
  983. row * data->cols_intern + col]), 1,
  984. DCELL_TYPE);
  985. }
  986. }
  987. else {
  988. if (data->type == FCELL_TYPE && data->fcell_array != NULL) {
  989. Rast3d_set_null_value((void *)
  990. &(data->
  991. fcell_array[(depth +
  992. data->offset) *
  993. (data->rows_intern *
  994. data->cols_intern) + (row +
  995. data->
  996. offset) *
  997. data->cols_intern + (col +
  998. data->
  999. offset)]), 1,
  1000. FCELL_TYPE);
  1001. }
  1002. else if (data->type == DCELL_TYPE && data->dcell_array != NULL) {
  1003. Rast3d_set_null_value((void *)
  1004. &(data->
  1005. dcell_array[(depth +
  1006. data->offset) *
  1007. (data->rows_intern *
  1008. data->cols_intern) + (row +
  1009. data->
  1010. offset) *
  1011. data->cols_intern + (col +
  1012. data->
  1013. offset)]), 1,
  1014. DCELL_TYPE);
  1015. }
  1016. }
  1017. return;
  1018. }
  1019. /*!
  1020. * \brief This function writes a float value to the N_array_3d data at position col, row, depth
  1021. *
  1022. * \param data N_array_3d *
  1023. * \param col int
  1024. * \param row int
  1025. * \param depth int
  1026. * \param value float
  1027. * \return void
  1028. * */
  1029. void
  1030. N_put_array_3d_f_value(N_array_3d * data, int col, int row, int depth,
  1031. float value)
  1032. {
  1033. double dval;
  1034. if (data->type == DCELL_TYPE) {
  1035. dval = (double)value;
  1036. N_put_array_3d_value(data, col, row, depth, (void *)&dval);
  1037. }
  1038. else {
  1039. N_put_array_3d_value(data, col, row, depth, (void *)&value);
  1040. }
  1041. return;
  1042. }
  1043. /*!
  1044. * \brief Writes a double value to the N_array_3d struct at position col, row, depth
  1045. *
  1046. * \param data N_array_3d *
  1047. * \param col int
  1048. * \param row int
  1049. * \param depth int
  1050. * \param value double
  1051. * \return void
  1052. * */
  1053. void
  1054. N_put_array_3d_d_value(N_array_3d * data, int col, int row, int depth,
  1055. double value)
  1056. {
  1057. float fval;
  1058. if (data->type == FCELL_TYPE) {
  1059. fval = (double)value;
  1060. N_put_array_3d_value(data, col, row, depth, (void *)&fval);
  1061. }
  1062. else {
  1063. N_put_array_3d_value(data, col, row, depth, (void *)&value);
  1064. }
  1065. return;
  1066. }
  1067. /*!
  1068. * \brief Write the info of the array to stdout
  1069. *
  1070. * \param data N_array_3d *
  1071. * \return void
  1072. * */
  1073. void N_print_array_3d_info(N_array_3d * data)
  1074. {
  1075. fprintf(stdout, "N_array_3d \n");
  1076. fprintf(stdout, "Cols %i\n", data->cols);
  1077. fprintf(stdout, "Rows: %i\n", data->rows);
  1078. fprintf(stdout, "Depths: %i\n", data->depths);
  1079. fprintf(stdout, "Array type: %i\n", data->type);
  1080. fprintf(stdout, "Offset: %i\n", data->offset);
  1081. fprintf(stdout, "Internal cols: %i\n", data->cols_intern);
  1082. fprintf(stdout, "Internal rows: %i\n", data->rows_intern);
  1083. fprintf(stdout, "Internal depths: %i\n", data->depths_intern);
  1084. fprintf(stdout, "FCELL array pointer: %p\n", data->fcell_array);
  1085. fprintf(stdout, "DCELL array pointer: %p\n", data->dcell_array);
  1086. return;
  1087. }
  1088. /*!
  1089. * \brief Write info and content of the array data to stdout
  1090. *
  1091. * Offsets are ignored
  1092. *
  1093. * \param data N_array_2d *
  1094. * \return void
  1095. * */
  1096. void N_print_array_3d(N_array_3d * data)
  1097. {
  1098. int i, j, k;
  1099. N_print_array_3d_info(data);
  1100. for (k = 0; k < data->depths; k++) {
  1101. for (j = 0; j < data->rows; j++) {
  1102. for (i = 0; i < data->cols; i++) {
  1103. if (data->type == FCELL_TYPE)
  1104. printf("%6.6f ", N_get_array_3d_f_value(data, i, j, k));
  1105. else if (data->type == DCELL_TYPE)
  1106. printf("%6.6f ", N_get_array_3d_d_value(data, i, j, k));
  1107. }
  1108. printf("\n");
  1109. }
  1110. printf("\n");
  1111. }
  1112. printf("\n");
  1113. return;
  1114. }