main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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(_("export"));
  312. G_add_keyword(_("voxel"));
  313. module->description =
  314. _("Converts 3D raster maps into the VTK-ASCII format.");
  315. /* Get parameters from user */
  316. set_params();
  317. /* Have GRASS get inputs */
  318. if (G_parser(argc, argv))
  319. exit(EXIT_FAILURE);
  320. /*The precision of the output */
  321. if (param.decimals->answer) {
  322. if (sscanf(param.decimals->answer, "%d", &dp) != 1)
  323. G_fatal_error(_("failed to interpret dp as an integer"));
  324. if (dp > 20 || dp < 0)
  325. G_fatal_error(_("dp has to be from 0 to 20"));
  326. }
  327. else {
  328. dp = 8; /*This value is taken from the lib settings in G_format_easting */
  329. }
  330. /*Check the input */
  331. check_input_maps();
  332. /*Correct the coordinates, so the precision of VTK is not hurt :( */
  333. if (param.coorcorr->answer) {
  334. /*Get the default region for coordiante correction */
  335. G_get_default_window(&default_region);
  336. /*Use the center of the current region as extent */
  337. y_extent = (default_region.north + default_region.south) / 2;
  338. x_extent = (default_region.west + default_region.east) / 2;
  339. }
  340. else {
  341. x_extent = 0;
  342. y_extent = 0;
  343. }
  344. /*open the output */
  345. if (param.output->answer) {
  346. fp = fopen(param.output->answer, "w");
  347. if (fp == NULL) {
  348. perror(param.output->answer);
  349. G_usage();
  350. exit(EXIT_FAILURE);
  351. }
  352. }
  353. else
  354. fp = stdout;
  355. /* Figure out the region from the map */
  356. G3d_initDefaults();
  357. G3d_getWindow(&region);
  358. /*initiate the input mpas structure */
  359. in = create_input_maps_struct();
  360. /* read and compute the scale factor */
  361. sscanf(param.elevscale->answer, "%lf", &scale);
  362. /*if LL projection, convert the elevation values to degrees */
  363. if (region.proj == PROJECTION_LL) {
  364. llscale = M_PI / (180) * 6378137;
  365. scale /= llscale;
  366. }
  367. /*Open the top and bottom file */
  368. if (param.structgrid->answer) {
  369. /*Check if the g3d-region is equal to the 2d rows and cols */
  370. rows = Rast_window_rows();
  371. cols = Rast_window_cols();
  372. /*If not equal, set the 2D windows correct */
  373. if (rows != region.rows || cols != region.cols) {
  374. G_message(_("The 2d and 3d region settings are different. The g3d settings are used to adjust the 2d region."));
  375. G_get_set_window(&window2d);
  376. window2d.ns_res = region.ns_res;
  377. window2d.ew_res = region.ew_res;
  378. window2d.rows = region.rows;
  379. window2d.cols = region.cols;
  380. Rast_set_window(&window2d);
  381. }
  382. /*open top */
  383. mapset = NULL;
  384. name = NULL;
  385. name = param.top->answer;
  386. mapset = G_find_raster2(name, "");
  387. in->top = open_input_map(name, mapset);
  388. in->topMapType = Rast_get_map_type(in->top);
  389. /*open bottom */
  390. mapset = NULL;
  391. name = NULL;
  392. name = param.bottom->answer;
  393. mapset = G_find_raster2(name, "");
  394. in->bottom = open_input_map(name, mapset);
  395. in->bottomMapType = Rast_get_map_type(in->bottom);
  396. /* Write the vtk-header and the points */
  397. if (param.point->answer) {
  398. write_vtk_structured_grid_header(fp, output, region);
  399. write_vtk_points(in, fp, region, dp, 1, scale);
  400. }
  401. else {
  402. write_vtk_unstructured_grid_header(fp, output, region);
  403. write_vtk_points(in, fp, region, dp, 0, scale);
  404. write_vtk_unstructured_grid_cells(fp, region);
  405. }
  406. Rast_close(in->top);
  407. in->top = -1;
  408. Rast_close(in->bottom);
  409. in->bottom = -1;
  410. }
  411. else {
  412. /* Write the structured point vtk-header */
  413. write_vtk_structured_point_header(fp, output, region, dp, scale);
  414. }
  415. /*Write the normal VTK data (cell or point data) */
  416. /*Loop over all 3d input maps! */
  417. if (param.input->answers != NULL) {
  418. for (i = 0; param.input->answers[i] != NULL; i++) {
  419. G_debug(3, "Open 3d raster map %s", param.input->answers[i]);
  420. /*Open the map */
  421. in->map =
  422. G3d_openCellOld(param.input->answers[i],
  423. G_find_grid3(param.input->answers[i], ""),
  424. &region, G3D_TILE_SAME_AS_FILE,
  425. G3D_USE_CACHE_DEFAULT);
  426. if (in->map == NULL) {
  427. G_warning(_("Error opening 3d raster map <%s>"),
  428. param.input->answers[i]);
  429. fatal_error(" ", in);
  430. }
  431. /*if requested set the Mask on */
  432. if (param.mask->answer) {
  433. if (G3d_maskFileExists()) {
  434. changemask = 0;
  435. if (G3d_maskIsOff(in->map)) {
  436. G3d_maskOn(in->map);
  437. changemask = 1;
  438. }
  439. }
  440. }
  441. /* Write the point or cell data */
  442. write_vtk_data(fp, in->map, region, param.input->answers[i], dp);
  443. /*We set the Mask off, if it was off before */
  444. if (param.mask->answer) {
  445. if (G3d_maskFileExists())
  446. if (G3d_maskIsOn(in->map) && changemask)
  447. G3d_maskOff(in->map);
  448. }
  449. /* Close the 3d raster map */
  450. if (!G3d_closeCell(in->map)) {
  451. in->map = NULL;
  452. fatal_error(_("Error closing 3d raster map, the VTK file may be incomplete."),
  453. in);
  454. }
  455. in->map = NULL;
  456. }
  457. }
  458. /*Write the RGB voxel data */
  459. open_write_rgb_maps(in, region, fp, dp);
  460. open_write_vector_maps(in, region, fp, dp);
  461. /*Close the output file */
  462. if (param.output->answer && fp != NULL)
  463. if (fclose(fp))
  464. fatal_error(_("Error closing VTK-ASCII file"), in);
  465. /*close all open maps and free memory */
  466. release_input_maps_struct(in);
  467. return 0;
  468. }