writeVTKData.c 15 KB

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