writeVTKData.c 14 KB

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