writeVTKData.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.out.vtk
  4. *
  5. * AUTHOR(S): Original author
  6. * Soeren Gebbert soerengebbert at gmx de
  7. * 27 Feb 2006 Berlin
  8. * PURPOSE: Converts 3D raster maps (G3D) into the VTK-Ascii format
  9. *
  10. * COPYRIGHT: (C) 2005 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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/raster3d.h>
  23. #include <grass/glocale.h>
  24. #include "globalDefs.h"
  25. #include "writeVTKData.h"
  26. #include "parameters.h"
  27. #include "errorHandling.h"
  28. /*local prototypes */
  29. static double get_raster_value_as_double(int maptype, void *ptr,
  30. double nullval);
  31. static double get_g3d_raster_value_as_double(void *map, int x, int y, int z,
  32. int type, double nullval);
  33. /* ************************************************************************* */
  34. /* Get the value of the current raster pointer as double ******************* */
  35. /* ************************************************************************* */
  36. double get_raster_value_as_double(int MapType, void *ptr, double nullval)
  37. {
  38. double val = nullval;
  39. if (MapType == CELL_TYPE) {
  40. if (Rast_is_null_value(ptr, MapType)) {
  41. val = nullval;
  42. } else {
  43. val = *(CELL *) ptr;
  44. }
  45. }
  46. if (MapType == FCELL_TYPE) {
  47. if (Rast_is_null_value(ptr, MapType)) {
  48. val = nullval;
  49. } else {
  50. val = *(FCELL *) ptr;
  51. }
  52. }
  53. if (MapType == DCELL_TYPE) {
  54. if (Rast_is_null_value(ptr, MapType)) {
  55. val = nullval;
  56. } else {
  57. val = *(DCELL *) ptr;
  58. }
  59. }
  60. return val;
  61. }
  62. /* ************************************************************************* */
  63. /* Get the value of the 3d raster map as double *************************** */
  64. /* ************************************************************************* */
  65. double get_g3d_raster_value_as_double(void *map, int x, int y, int z,
  66. int type, double nullval)
  67. {
  68. double val = 0;
  69. float fvalue;
  70. double dvalue;
  71. if (type == FCELL_TYPE) {
  72. G3d_getValue(map, x, y, z, &fvalue, type);
  73. if (G3d_isNullValueNum(&fvalue, FCELL_TYPE))
  74. val = nullval;
  75. else
  76. val = (double) fvalue;
  77. } else {
  78. G3d_getValue(map, x, y, z, &dvalue, type);
  79. if (G3d_isNullValueNum(&dvalue, DCELL_TYPE))
  80. val = nullval;
  81. else
  82. val = dvalue;
  83. }
  84. return val;
  85. }
  86. /* ************************************************************************* */
  87. /* This function writes the point coordinates ****************************** */
  88. /* ************************************************************************* */
  89. void write_vtk_points(input_maps * in, FILE * fp, G3D_Region region, int dp,
  90. int type, double scale)
  91. {
  92. int x, y, z, percentage = 0;
  93. int rows, cols, depths;
  94. void *rast_top = NULL;
  95. void *rast_bottom = NULL;
  96. void *ptr_top = NULL;
  97. void *ptr_bottom = NULL;
  98. double topval = 0, bottomval = 0;
  99. double zcoor, ycoor, xcoor;
  100. double zcoor1, ycoor1, xcoor1;
  101. rows = region.rows;
  102. cols = region.cols;
  103. depths = region.depths;
  104. rast_top = Rast_allocate_buf(in->topMapType);
  105. rast_bottom = Rast_allocate_buf(in->bottomMapType);
  106. G_debug(3, _("write_vtk_points: Writing point coordinates"));
  107. for (z = 0; z < depths; z++) {
  108. for (y = 0; y < rows; y++) {
  109. G_percent(percentage, (rows * depths - 1), 10);
  110. percentage++;
  111. Rast_get_row(in->top, rast_top, y, in->topMapType);
  112. Rast_get_row(in->bottom, rast_bottom, y, in->bottomMapType);
  113. for (x = 0, ptr_top = rast_top, ptr_bottom = rast_bottom;
  114. x < cols;
  115. x++, ptr_top =
  116. G_incr_void_ptr(ptr_top, Rast_cell_size(in->topMapType)),
  117. ptr_bottom =
  118. G_incr_void_ptr(ptr_bottom,
  119. Rast_cell_size(in->bottomMapType))) {
  120. /*Get the values */
  121. topval =
  122. get_raster_value_as_double(in->topMapType, ptr_top, 0.0);
  123. bottomval =
  124. get_raster_value_as_double(in->bottomMapType, ptr_bottom,
  125. 0.0);
  126. if (type == 1) { /*Structured Grid */
  127. /*Calculate the coordinates */
  128. xcoor =
  129. region.west + (region.ew_res / 2 +
  130. region.ew_res * (x));
  131. /* Here the raster3d north->south coordinate system is used */
  132. ycoor =
  133. region.north - (region.ns_res / 2 +
  134. region.ns_res * (y));
  135. zcoor =
  136. (bottomval +
  137. z * (topval - bottomval) / (depths - 1)) * scale;
  138. xcoor -= x_extent;
  139. ycoor -= y_extent;
  140. fprintf(fp, "%.*f ", dp, xcoor);
  141. fprintf(fp, "%.*f ", dp, ycoor);
  142. fprintf(fp, "%.*f\n", dp, zcoor);
  143. } else { /*Unstructured Grid */
  144. /*Write for every cell the coordinates for a hexahedron -> 8 points */
  145. /*VTK Hexaeder */
  146. /* bottom
  147. * 3 --- 2
  148. * | |
  149. * 0 --- 1
  150. * top
  151. * 7 --- 6
  152. * | |
  153. * 4 --- 5
  154. */
  155. xcoor = region.west + (region.ew_res * (x)); /*0, 3, 4, 7 */
  156. /* Here the raster3d north->south coordinate system is used */
  157. ycoor = region.north - (region.ns_res * (y)); /*2, 3, 6, 7 */
  158. zcoor = (bottomval + z * (topval - bottomval) / (depths)) * scale; /*0, 1, 2, 3 */
  159. xcoor1 = region.west + (region.ew_res + region.ew_res * (x)); /*1, 2, 5, 6 */
  160. /* Here the raster3d north->south coordinate system is used */
  161. ycoor1 = region.north - (region.ns_res + region.ns_res * (y)); /*0, 1, 4, 5 */
  162. zcoor1 = (bottomval + z * (topval - bottomval) / (depths) + (topval - bottomval) / (depths)) * scale; /*4, 5, ,6 ,7 */
  163. xcoor -= x_extent;
  164. ycoor -= y_extent;
  165. xcoor1 -= x_extent;
  166. ycoor1 -= y_extent;
  167. /*0 */
  168. fprintf(fp, "%.*f ", dp, xcoor);
  169. fprintf(fp, "%.*f ", dp, ycoor1);
  170. fprintf(fp, "%.*f\n", dp, zcoor);
  171. /*1 */
  172. fprintf(fp, "%.*f ", dp, xcoor1);
  173. fprintf(fp, "%.*f ", dp, ycoor1);
  174. fprintf(fp, "%.*f\n", dp, zcoor);
  175. /*2 */
  176. fprintf(fp, "%.*f ", dp, xcoor1);
  177. fprintf(fp, "%.*f ", dp, ycoor);
  178. fprintf(fp, "%.*f\n", dp, zcoor);
  179. /*3 */
  180. fprintf(fp, "%.*f ", dp, xcoor);
  181. fprintf(fp, "%.*f ", dp, ycoor);
  182. fprintf(fp, "%.*f\n", dp, zcoor);
  183. /*4 */
  184. fprintf(fp, "%.*f ", dp, xcoor);
  185. fprintf(fp, "%.*f ", dp, ycoor1);
  186. fprintf(fp, "%.*f\n", dp, zcoor1);
  187. /*5 */
  188. fprintf(fp, "%.*f ", dp, xcoor1);
  189. fprintf(fp, "%.*f ", dp, ycoor1);
  190. fprintf(fp, "%.*f\n", dp, zcoor1);
  191. /*6 */
  192. fprintf(fp, "%.*f ", dp, xcoor1);
  193. fprintf(fp, "%.*f ", dp, ycoor);
  194. fprintf(fp, "%.*f\n", dp, zcoor1);
  195. /*7 */
  196. fprintf(fp, "%.*f ", dp, xcoor);
  197. fprintf(fp, "%.*f ", dp, ycoor);
  198. fprintf(fp, "%.*f\n", dp, zcoor1);
  199. }
  200. }
  201. }
  202. }
  203. if (type == 1)
  204. fprintf(fp, "POINT_DATA %i\n", region.cols * region.rows * region.depths); /*We have pointdata */
  205. return;
  206. }
  207. /* ************************************************************************* */
  208. /* This function writes the cell for the unstructured grid ***************** */
  209. /* ************************************************************************* */
  210. void write_vtk_unstructured_grid_cells(FILE * fp, G3D_Region region)
  211. {
  212. int x, y, z, percentage;
  213. int rows, cols, depths, count;
  214. rows = region.rows;
  215. cols = region.cols;
  216. depths = region.depths;
  217. G_debug(3, _("write_vtk_unstructured_grid_cells: Writing the cells"));
  218. fprintf(fp, "CELLS %i %i\n", region.cols * region.rows * region.depths,
  219. region.cols * region.rows * region.depths * 9);
  220. count = 0;
  221. percentage = 0;
  222. /*The point - cell links */
  223. for (z = 0; z < depths; z++) {
  224. for (y = 0; y < rows; y++) {
  225. G_percent(percentage, (rows * depths - 1), 10);
  226. percentage++;
  227. for (x = 0; x < cols; x++) {
  228. /*Voxel */
  229. fprintf(fp, "%i %i %i %i %i %i %i %i %i\n", 8,
  230. count * 8, count * 8 + 1, count * 8 + 3,
  231. count * 8 + 2, count * 8 + 4, count * 8 + 5,
  232. count * 8 + 7, count * 8 + 6);
  233. /*Hexaeder
  234. * fprintf(fp, "%i %i %i %i %i %i %i %i %i\n", 8,
  235. * count * 8, count * 8 + 1, count * 8 + 2, count * 8 + 3,
  236. * count * 8 + 4, count * 8 + 5, count * 8 + 6,
  237. * count * 8 + 7);
  238. */
  239. count++;
  240. }
  241. }
  242. }
  243. percentage = 0;
  244. fprintf(fp, "CELL_TYPES %i\n", region.cols * region.rows * region.depths);
  245. /*the cell types */
  246. for (z = 0; z < depths; z++) {
  247. for (y = 0; y < rows; y++) {
  248. G_percent(percentage, (rows * depths - 1), 10);
  249. percentage++;
  250. for (x = 0; x < cols; x++) {
  251. /*Voxel */
  252. fprintf(fp, "11\n");
  253. /*Hexaeder
  254. * fprintf(fp, "12\n");
  255. */
  256. }
  257. }
  258. }
  259. fprintf(fp, "CELL_DATA %i\n", region.cols * region.rows * region.depths); /*We have celldata */
  260. return;
  261. }
  262. /* ************************************************************************* */
  263. /* Write the VTK Cell or point data **************************************** */
  264. /* ************************************************************************* */
  265. void write_vtk_data(FILE * fp, void *map, G3D_Region region, char *varname,
  266. int dp)
  267. {
  268. double value;
  269. double nullvalue;
  270. int x, y, z, percentage;
  271. int rows, cols, depths, typeIntern;
  272. rows = region.rows;
  273. cols = region.cols;
  274. depths = region.depths;
  275. /*the nullvalue */
  276. if (!sscanf(param.null_val->answer, "%lf", &nullvalue)) {
  277. G_warning("Null value is not valid, using 0 instead.");
  278. nullvalue = 0;
  279. }
  280. G_debug(3,
  281. _("write_vtk_data: Writing Celldata %s with rows %i cols %i depths %i to vtk-ascii file"),
  282. varname, rows, cols, depths);
  283. fprintf(fp, "SCALARS %s float 1\n", varname);
  284. fprintf(fp, "LOOKUP_TABLE default\n");
  285. typeIntern = G3d_tileTypeMap(map);
  286. percentage = 0;
  287. for (z = 0; z < depths; z++) {
  288. /* In case of structured grid data, the point/cell coordinates
  289. are computed based on the default north->south raster3d coordinate system.
  290. We need to compute south -> north ordering for image data.
  291. */
  292. if (!param.structgrid->answer) {
  293. for (y = rows - 1; y >= 0; y--) {
  294. G_percent(percentage, (rows * depths - 1), 10);
  295. percentage++;
  296. for (x = 0; x < cols; x++) {
  297. value =
  298. get_g3d_raster_value_as_double(map, x, y, z,
  299. typeIntern, nullvalue);
  300. fprintf(fp, "%.*f ", dp, value);
  301. }
  302. fprintf(fp, "\n");
  303. }
  304. } else {
  305. for (y = 0; y < rows; y++) {
  306. G_percent(percentage, (rows * depths - 1), 10);
  307. percentage++;
  308. for (x = 0; x < cols; x++) {
  309. value =
  310. get_g3d_raster_value_as_double(map, x, y, z,
  311. typeIntern, nullvalue);
  312. fprintf(fp, "%.*f ", dp, value);
  313. }
  314. fprintf(fp, "\n");
  315. }
  316. }
  317. }
  318. }
  319. /* ************************************************************************* */
  320. /* Write the VTK RGB Voxel Data ******************************************** */
  321. /* ************************************************************************* */
  322. void write_vtk_rgb_data(void *map_r, void *map_g, void *map_b,
  323. FILE * fp, const char *varname,
  324. G3D_Region region, int dp)
  325. {
  326. double value = 0;
  327. int x, y, z, percentage, k;
  328. int rows, cols, depths;
  329. int typeIntern[3];
  330. void *maprgb = NULL;
  331. G_debug(3, _("write_vtk_rgb_data: writing rgb data"));
  332. rows = region.rows;
  333. cols = region.cols;
  334. depths = region.depths;
  335. typeIntern[0] = G3d_tileTypeMap(map_r);
  336. typeIntern[1] = G3d_tileTypeMap(map_g);
  337. typeIntern[2] = G3d_tileTypeMap(map_b);
  338. percentage = 0;
  339. /********************** WRITE RGB VOXEL DATA; CELL OR POINT ****************/
  340. fprintf(fp, "COLOR_SCALARS %s 3\n", varname);
  341. for (z = 0; z < depths; z++) {
  342. for (y = 0; y < rows; y++) {
  343. G_percent(percentage, (rows * depths - 1), 10);
  344. percentage++;
  345. for (x = 0; x < cols; x++) {
  346. for (k = 0; k < 3; k++) {
  347. if (k == 0)
  348. maprgb = map_r;
  349. if (k == 1)
  350. maprgb = map_g;
  351. if (k == 2)
  352. maprgb = map_b;
  353. /* In case of structured grid data, the point/cell coordinates
  354. are computed based on the default north->south raster3d coordinate system.
  355. We need to compute south -> north ordering for image data.
  356. */
  357. if (!param.structgrid->answer)
  358. value =
  359. get_g3d_raster_value_as_double(maprgb, x, rows - y - 1, z,
  360. typeIntern[k],
  361. 0.0);
  362. else
  363. value =
  364. get_g3d_raster_value_as_double(maprgb, x, y, z,
  365. typeIntern[k],
  366. 0.0);
  367. /*Test of value range, the data should be 1 byte gray values */
  368. if (value > 255 || value < 0) {
  369. G_warning(_("Wrong 3d raster map values! Values should in between 0 and 255!\n"));
  370. fprintf(fp, "0 ");
  371. } else {
  372. fprintf(fp, "%.*f ", dp, (value / 255));
  373. }
  374. }
  375. fprintf(fp, "\n");
  376. }
  377. }
  378. }
  379. return;
  380. }
  381. /* ************************************************************************* */
  382. /* Write the VTK vector Data *********************************************** */
  383. /* ************************************************************************* */
  384. void write_vtk_vector_data(void *map_x, void *map_y, void *map_z,
  385. FILE * fp, const char *varname,
  386. G3D_Region region, int dp)
  387. {
  388. double value = 0;
  389. int x, y, z, percentage, k;
  390. int rows, cols, depths;
  391. int typeIntern[3];
  392. void *mapvect = NULL;
  393. G_debug(3, _("write_vtk_vector_data: writing vector data"));
  394. rows = region.rows;
  395. cols = region.cols;
  396. depths = region.depths;
  397. typeIntern[0] = G3d_tileTypeMap(map_x);
  398. typeIntern[1] = G3d_tileTypeMap(map_y);
  399. typeIntern[2] = G3d_tileTypeMap(map_z);
  400. percentage = 0;
  401. /********************** WRITE VECTOR DATA; CELL OR POINT ****************/
  402. fprintf(fp, "VECTORS %s float\n", varname);
  403. for (z = 0; z < depths; z++) { /*From the bottom to the top */
  404. for (y = 0; y < rows; y++) {
  405. G_percent(percentage, (rows * depths - 1), 10);
  406. percentage++;
  407. for (x = 0; x < cols; x++) {
  408. for (k = 0; k < 3; k++) {
  409. if (k == 0)
  410. mapvect = map_x;
  411. if (k == 1)
  412. mapvect = map_y;
  413. if (k == 2)
  414. mapvect = map_z;
  415. /* In case of structured grid data, the point/cell coordinates
  416. are computed based on the default north->south raster3d coordinate system.
  417. We need to compute south -> north ordering for image data.
  418. */
  419. if (!param.structgrid->answer)
  420. value =
  421. get_g3d_raster_value_as_double(mapvect, x, rows - y - 1, z,
  422. typeIntern[k],
  423. 0.0);
  424. else
  425. value =
  426. get_g3d_raster_value_as_double(mapvect, x, y, z,
  427. typeIntern[k],
  428. 0.0);
  429. fprintf(fp, "%.*f ", dp, value);
  430. }
  431. fprintf(fp, "\n");
  432. }
  433. }
  434. }
  435. return;
  436. }