main.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/raster.h>
  22. #include <grass/G3d.h>
  23. #include <grass/glocale.h>
  24. #include "globalDefs.h"
  25. #include "parameters.h"
  26. #include "writeVTKData.h"
  27. #include "writeVTKHead.h"
  28. #include "errorHandling.h"
  29. paramType param; /*Parameters */
  30. double x_extent;
  31. double y_extent;
  32. /** prototypes ***************************************************************/
  33. /*Open the rgb voxel maps and write the data to the output */
  34. static void open_write_rgb_maps(input_maps * in, G3D_Region region, FILE * fp,
  35. int dp);
  36. /*Open the rgb voxel maps and write the data to the output */
  37. static void open_write_vector_maps(input_maps * in, G3D_Region region,
  38. FILE * fp, int dp);
  39. /*opens a raster input map */
  40. static int open_input_map(const char *name, const char *mapset);
  41. /*Check if all maps are available */
  42. static void check_input_maps(void);
  43. /*Initiate the input maps structure */
  44. static input_maps *create_input_maps_struct(void);
  45. /* ************************************************************************* */
  46. /* Open the raster input map *********************************************** */
  47. /* ************************************************************************* */
  48. input_maps *create_input_maps_struct(void)
  49. {
  50. input_maps *in;
  51. in = (input_maps *) calloc(1, sizeof(input_maps));
  52. in->map = NULL;
  53. in->map_r = NULL;
  54. in->map_g = NULL;
  55. in->map_b = NULL;
  56. in->map_x = NULL;
  57. in->map_y = NULL;
  58. in->map_z = NULL;
  59. in->top = -1;
  60. in->bottom = -1;
  61. in->topMapType = 0;
  62. in->bottomMapType = 0;
  63. in->elevmaps = NULL;
  64. in->elevmaptypes = NULL;
  65. in->numelevmaps = 0;
  66. return in;
  67. }
  68. /* ************************************************************************* */
  69. /* Open the raster input map *********************************************** */
  70. /* ************************************************************************* */
  71. int open_input_map(const char *name, const char *mapset)
  72. {
  73. G_debug(3, "Open Raster file %s in Mapset %s", name, mapset);
  74. /* open raster map */
  75. return Rast_open_old(name, mapset);
  76. }
  77. /* ************************************************************************* */
  78. /* Check the input maps **************************************************** */
  79. /* ************************************************************************* */
  80. void check_input_maps(void)
  81. {
  82. int i = 0;
  83. const char *mapset, *name;
  84. /*Check top and bottom if surface is requested */
  85. if (param.structgrid->answer) {
  86. if (!param.top->answer || !param.bottom->answer)
  87. G3d_fatalError(_("You have to specify top and bottom map"));
  88. mapset = NULL;
  89. name = NULL;
  90. name = param.top->answer;
  91. mapset = G_find_raster2(name, "");
  92. if (mapset == NULL) {
  93. G3d_fatalError(_("Top cell map <%s> not found"),
  94. param.top->answer);
  95. }
  96. mapset = NULL;
  97. name = NULL;
  98. name = param.bottom->answer;
  99. mapset = G_find_raster2(name, "");
  100. if (mapset == NULL) {
  101. G3d_fatalError(_("Bottom cell map <%s> not found"),
  102. param.bottom->answer);
  103. }
  104. }
  105. /*If input maps are provided, check them */
  106. if (param.input->answers != NULL) {
  107. for (i = 0; param.input->answers[i] != NULL; i++) {
  108. if (NULL == G_find_grid3(param.input->answers[i], ""))
  109. G3d_fatalError(_("Requested 3d raster map <%s> not found"),
  110. param.input->answers[i]);
  111. }
  112. }
  113. /*Check for rgb maps. */
  114. if (param.rgbmaps->answers != NULL) {
  115. for (i = 0; i < 3; i++) {
  116. if (param.rgbmaps->answers[i] != NULL) {
  117. if (NULL == G_find_grid3(param.rgbmaps->answers[i], ""))
  118. G3d_fatalError(_("Requested g3d RGB map <%s> not found"),
  119. param.rgbmaps->answers[i]);
  120. }
  121. else {
  122. G3d_fatalError(_("Please provide three g3d RGB maps"));
  123. }
  124. }
  125. }
  126. /*Check for vector maps. */
  127. if (param.vectormaps->answers != NULL) {
  128. for (i = 0; i < 3; i++) {
  129. if (param.vectormaps->answers[i] != NULL) {
  130. if (NULL == G_find_grid3(param.vectormaps->answers[i], ""))
  131. G3d_fatalError(_("Requested g3d vector map <%s> not found"),
  132. param.vectormaps->answers[i]);
  133. }
  134. else {
  135. G3d_fatalError(_("Please provide three g3d vector maps [x,y,z]"));
  136. }
  137. }
  138. }
  139. if (param.input->answers == NULL && param.rgbmaps->answers == NULL &&
  140. param.vectormaps->answers == NULL) {
  141. G_warning(_("No g3d data, RGB or xyz-vector maps are provided! Will only write the geometry."));
  142. }
  143. return;
  144. }
  145. /* ************************************************************************* */
  146. /* Prepare the VTK RGB voxel data for writing ****************************** */
  147. /* ************************************************************************* */
  148. void open_write_rgb_maps(input_maps * in, G3D_Region region, FILE * fp,
  149. int dp)
  150. {
  151. int i, changemask[3] = { 0, 0, 0 };
  152. void *maprgb = NULL;
  153. if (param.rgbmaps->answers != NULL) {
  154. /*Loop over all input maps! */
  155. for (i = 0; i < 3; i++) {
  156. G_debug(3, _("Open rgb 3d raster map %s"),
  157. param.rgbmaps->answers[i]);
  158. maprgb = NULL;
  159. /*Open the map */
  160. maprgb =
  161. G3d_openCellOld(param.rgbmaps->answers[i],
  162. G_find_grid3(param.rgbmaps->answers[i], ""),
  163. &region, G3D_TILE_SAME_AS_FILE,
  164. G3D_USE_CACHE_DEFAULT);
  165. if (maprgb == NULL) {
  166. G_warning(_("Error opening 3d raster map <%s>"),
  167. param.rgbmaps->answers[i]);
  168. fatal_error(_("No RGB Data will be created."), in);
  169. }
  170. /*if requested set the Mask on */
  171. if (param.mask->answer) {
  172. if (G3d_maskFileExists()) {
  173. changemask[i] = 0;
  174. if (G3d_maskIsOff(maprgb)) {
  175. G3d_maskOn(maprgb);
  176. changemask[i] = 1;
  177. }
  178. }
  179. }
  180. if (i == 0)
  181. in->map_r = maprgb;
  182. if (i == 1)
  183. in->map_g = maprgb;
  184. if (i == 2)
  185. in->map_b = maprgb;
  186. }
  187. G_debug(3, "Writing VTK VoxelData");
  188. write_vtk_rgb_data(in->map_r, in->map_g, in->map_b, fp, "RGB_Voxel",
  189. region, dp);
  190. for (i = 0; i < 3; i++) {
  191. if (i == 0)
  192. maprgb = in->map_r;
  193. if (i == 1)
  194. maprgb = in->map_g;
  195. if (i == 2)
  196. maprgb = in->map_b;
  197. /*We set the Mask off, if it was off before */
  198. if (param.mask->answer) {
  199. if (G3d_maskFileExists())
  200. if (G3d_maskIsOn(maprgb) && changemask[i])
  201. G3d_maskOff(maprgb);
  202. }
  203. /* Close the 3d raster map */
  204. if (!G3d_closeCell(maprgb)) {
  205. fatal_error(_("Error closing g3d rgb map."), in);
  206. }
  207. /*Set the pointer to null so we noe later that these files are already closed */
  208. if (i == 0)
  209. in->map_r = NULL;
  210. if (i == 1)
  211. in->map_g = NULL;
  212. if (i == 2)
  213. in->map_b = NULL;
  214. }
  215. }
  216. return;
  217. }
  218. /* ************************************************************************* */
  219. /* Prepare the VTK vector data for writing ********************************* */
  220. /* ************************************************************************* */
  221. void open_write_vector_maps(input_maps * in, G3D_Region region, FILE * fp,
  222. int dp)
  223. {
  224. int i, changemask[3] = { 0, 0, 0 };
  225. void *mapvect = NULL;
  226. if (param.vectormaps->answers != NULL) {
  227. /*Loop over all input maps! */
  228. for (i = 0; i < 3; i++) {
  229. G_debug(3, "Open vector 3d raster map %s",
  230. param.vectormaps->answers[i]);
  231. mapvect = NULL;
  232. /*Open the map */
  233. mapvect =
  234. G3d_openCellOld(param.vectormaps->answers[i],
  235. G_find_grid3(param.vectormaps->answers[i],
  236. ""), &region,
  237. G3D_TILE_SAME_AS_FILE, G3D_USE_CACHE_DEFAULT);
  238. if (mapvect == NULL) {
  239. G_warning(_("Error opening 3d raster map <%s>"),
  240. param.vectormaps->answers[i]);
  241. fatal_error(_("No vector data will be created."), in);
  242. }
  243. /*if requested set the Mask on */
  244. if (param.mask->answer) {
  245. if (G3d_maskFileExists()) {
  246. changemask[i] = 0;
  247. if (G3d_maskIsOff(mapvect)) {
  248. G3d_maskOn(mapvect);
  249. changemask[i] = 1;
  250. }
  251. }
  252. }
  253. if (i == 0)
  254. in->map_x = mapvect;
  255. if (i == 1)
  256. in->map_y = mapvect;
  257. if (i == 2)
  258. in->map_z = mapvect;
  259. }
  260. G_debug(3, "Writing VTK Vector Data");
  261. write_vtk_vector_data(in->map_x, in->map_y, in->map_z, fp,
  262. "Vector_Data", region, dp);
  263. for (i = 0; i < 3; i++) {
  264. if (i == 0)
  265. mapvect = in->map_x;
  266. if (i == 1)
  267. mapvect = in->map_y;
  268. if (i == 2)
  269. mapvect = in->map_z;
  270. /*We set the Mask off, if it was off before */
  271. if (param.mask->answer) {
  272. if (G3d_maskFileExists())
  273. if (G3d_maskIsOn(mapvect) && changemask[i])
  274. G3d_maskOff(mapvect);
  275. }
  276. /* Close the 3d raster map */
  277. if (!G3d_closeCell(mapvect)) {
  278. fatal_error(_("Error closing g3d vector map."), in);
  279. }
  280. /*Set the pointer to null so we noe later that these files are already closed */
  281. if (i == 0)
  282. in->map_x = NULL;
  283. if (i == 1)
  284. in->map_y = NULL;
  285. if (i == 2)
  286. in->map_z = NULL;
  287. }
  288. }
  289. return;
  290. }
  291. /* ************************************************************************* */
  292. /* Main function, opens most of the input and output files ***************** */
  293. /* ************************************************************************* */
  294. int main(int argc, char *argv[])
  295. {
  296. char *output = NULL;
  297. G3D_Region region;
  298. struct Cell_head window2d;
  299. struct Cell_head default_region;
  300. FILE *fp = NULL;
  301. struct GModule *module;
  302. int dp, i, changemask = 0;
  303. int rows, cols;
  304. const char *mapset, *name;
  305. double scale = 1.0, llscale = 1.0;
  306. input_maps *in;
  307. /* Initialize GRASS */
  308. G_gisinit(argv[0]);
  309. module = G_define_module();
  310. G_add_keyword(_("raster3d"));
  311. G_add_keyword(_("voxel"));
  312. module->description =
  313. _("Converts 3D raster maps (G3D) into the VTK-Ascii format");
  314. /* Get parameters from user */
  315. set_params();
  316. /* Have GRASS get inputs */
  317. if (G_parser(argc, argv))
  318. exit(EXIT_FAILURE);
  319. /*The precision of the output */
  320. if (param.decimals->answer) {
  321. if (sscanf(param.decimals->answer, "%d", &dp) != 1)
  322. G_fatal_error(_("failed to interpret dp as an integer"));
  323. if (dp > 20 || dp < 0)
  324. G_fatal_error(_("dp has to be from 0 to 20"));
  325. }
  326. else {
  327. dp = 8; /*This value is taken from the lib settings in G_format_easting */
  328. }
  329. /*Check the input */
  330. check_input_maps();
  331. /*Correct the coordinates, so the precision of VTK is not hurt :( */
  332. if (param.coorcorr->answer) {
  333. /*Get the default region for coordiante correction */
  334. G_get_default_window(&default_region);
  335. /*Use the center of the current region as extent */
  336. y_extent = (default_region.north + default_region.south) / 2;
  337. x_extent = (default_region.west + default_region.east) / 2;
  338. }
  339. else {
  340. x_extent = 0;
  341. y_extent = 0;
  342. }
  343. /*open the output */
  344. if (param.output->answer) {
  345. fp = fopen(param.output->answer, "w");
  346. if (fp == NULL) {
  347. perror(param.output->answer);
  348. G_usage();
  349. exit(EXIT_FAILURE);
  350. }
  351. }
  352. else
  353. fp = stdout;
  354. /* Figure out the region from the map */
  355. G3d_initDefaults();
  356. G3d_getWindow(&region);
  357. /*initiate the input mpas structure */
  358. in = create_input_maps_struct();
  359. /* read and compute the scale factor */
  360. sscanf(param.elevscale->answer, "%lf", &scale);
  361. /*if LL projection, convert the elevation values to degrees */
  362. if (region.proj == PROJECTION_LL) {
  363. llscale = M_PI / (180) * 6378137;
  364. scale /= llscale;
  365. }
  366. /*Open the top and bottom file */
  367. if (param.structgrid->answer) {
  368. /*Check if the g3d-region is equal to the 2d rows and cols */
  369. rows = G_window_rows();
  370. cols = G_window_cols();
  371. /*If not equal, set the 2D windows correct */
  372. if (rows != region.rows || cols != region.cols) {
  373. G_message(_("The 2d and 3d region settings are different. The g3d settings are used to adjust the 2d region."));
  374. G_get_set_window(&window2d);
  375. window2d.ns_res = region.ns_res;
  376. window2d.ew_res = region.ew_res;
  377. window2d.rows = region.rows;
  378. window2d.cols = region.cols;
  379. Rast_set_window(&window2d);
  380. }
  381. /*open top */
  382. mapset = NULL;
  383. name = NULL;
  384. name = param.top->answer;
  385. mapset = G_find_raster2(name, "");
  386. in->top = open_input_map(name, mapset);
  387. in->topMapType = Rast_get_map_type(in->top);
  388. /*open bottom */
  389. mapset = NULL;
  390. name = NULL;
  391. name = param.bottom->answer;
  392. mapset = G_find_raster2(name, "");
  393. in->bottom = open_input_map(name, mapset);
  394. in->bottomMapType = Rast_get_map_type(in->bottom);
  395. /* Write the vtk-header and the points */
  396. if (param.point->answer) {
  397. write_vtk_structured_grid_header(fp, output, region);
  398. write_vtk_points(in, fp, region, dp, 1, scale);
  399. }
  400. else {
  401. write_vtk_unstructured_grid_header(fp, output, region);
  402. write_vtk_points(in, fp, region, dp, 0, scale);
  403. write_vtk_unstructured_grid_cells(fp, region);
  404. }
  405. Rast_close(in->top);
  406. in->top = -1;
  407. Rast_close(in->bottom);
  408. in->bottom = -1;
  409. }
  410. else {
  411. /* Write the structured point vtk-header */
  412. write_vtk_structured_point_header(fp, output, region, dp, scale);
  413. }
  414. /*Write the normal VTK data (cell or point data) */
  415. /*Loop over all 3d input maps! */
  416. if (param.input->answers != NULL) {
  417. for (i = 0; param.input->answers[i] != NULL; i++) {
  418. G_debug(3, "Open 3d raster map %s", param.input->answers[i]);
  419. /*Open the map */
  420. in->map =
  421. G3d_openCellOld(param.input->answers[i],
  422. G_find_grid3(param.input->answers[i], ""),
  423. &region, G3D_TILE_SAME_AS_FILE,
  424. G3D_USE_CACHE_DEFAULT);
  425. if (in->map == NULL) {
  426. G_warning(_("Error opening 3d raster map <%s>"),
  427. param.input->answers[i]);
  428. fatal_error(" ", in);
  429. }
  430. /*if requested set the Mask on */
  431. if (param.mask->answer) {
  432. if (G3d_maskFileExists()) {
  433. changemask = 0;
  434. if (G3d_maskIsOff(in->map)) {
  435. G3d_maskOn(in->map);
  436. changemask = 1;
  437. }
  438. }
  439. }
  440. /* Write the point or cell data */
  441. write_vtk_data(fp, in->map, region, param.input->answers[i], dp);
  442. /*We set the Mask off, if it was off before */
  443. if (param.mask->answer) {
  444. if (G3d_maskFileExists())
  445. if (G3d_maskIsOn(in->map) && changemask)
  446. G3d_maskOff(in->map);
  447. }
  448. /* Close the 3d raster map */
  449. if (!G3d_closeCell(in->map)) {
  450. in->map = NULL;
  451. fatal_error(_("Error closing 3d raster map, the VTK file may be incomplete."),
  452. in);
  453. }
  454. in->map = NULL;
  455. }
  456. }
  457. /*Write the RGB voxel data */
  458. open_write_rgb_maps(in, region, fp, dp);
  459. open_write_vector_maps(in, region, fp, dp);
  460. /*Close the output file */
  461. if (param.output->answer && fp != NULL)
  462. if (fclose(fp))
  463. fatal_error(_("Error closing VTK-ASCII file"), in);
  464. /*close all open maps and free memory */
  465. release_input_maps_struct(in);
  466. return 0;
  467. }