main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.mkdspf
  4. * AUTHOR(S): Helena Mitasova and Bill Brown (original contributor)
  5. * Roberto Flor <flor itc.it>, Bernhard Reiter <bernhard intevation.de>,
  6. * Brad Douglas <rez touchofmadness.com>, Glynn Clements <glynn gclements.plus.com>,
  7. * Radim Blazek <radim.blazek gmail.com>, Markus Neteler <neteler itc.it>
  8. * PURPOSE:
  9. * COPYRIGHT: (C) 2000-2006 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. /* This program implements the marching cubes surface tiler described by
  17. * Lorenson & Cline in the Siggraph 87 Conference Proceedings.
  18. *
  19. * This program reads in data from a grid3 formatted file containing 3D data
  20. * and creates a display file.
  21. *
  22. * The display file consists of cell_info structures.
  23. * The cell_info structure:
  24. * threshold (specified on commandline)
  25. * number of polygons
  26. * polygon vertice coordinates
  27. * surface or vertex normals (depending on lighting model specified)
  28. *
  29. * The user must specify the data file name and the thresholds and lighting
  30. * model desired.
  31. *
  32. * Based on a program outline written by Mike Krogh, NCSA, Feb.,1990
  33. *
  34. * written by Jan Moorman for D. Lawrance, Mednet NCSA.
  35. * rewritten for CERL, August 1990
  36. *
  37. * rewritten by Bill Brown
  38. * for UI Geographic Modeling Systems Laboratory February 1996
  39. * to use new GRASS 3dgrid data files & API
  40. */
  41. #include <stdlib.h>
  42. #include <math.h>
  43. #include "vizual.h"
  44. #include <grass/gis.h>
  45. #include <grass/raster3d.h>
  46. #include "local_proto.h"
  47. #include <grass/glocale.h>
  48. file_info Headfax; /* contains info about command line */
  49. Cube_data CUBE; /* and the data for a single cube */
  50. int NTHRESH;
  51. int main(int argc, char *argv[])
  52. {
  53. char element[GNAME_MAX+10];
  54. const char *dspout;
  55. void *g3map;
  56. RASTER3D_Region g3reg;
  57. const char *mapset;
  58. double dmin, dmax;
  59. struct GModule *module;
  60. struct Option *levels;
  61. struct Option *out;
  62. struct Option *min;
  63. struct Option *max;
  64. struct Option *step;
  65. struct Option *tnum;
  66. struct Option *name;
  67. struct Flag *shade;
  68. struct Flag *quiet;
  69. G_gisinit(argv[0]);
  70. module = G_define_module();
  71. G_add_keyword(_("raster3d"));
  72. G_add_keyword(_("voxel"));
  73. module->description =
  74. _("Creates a display file from an existing grid3 file according to specified threshold levels.");
  75. name = G_define_option();
  76. name->key = "input";
  77. name->type = TYPE_STRING;
  78. name->required = YES;
  79. name->gisprompt = "old,grid3,3dcell";
  80. /* should still find the DIRECTORY */
  81. name->description = _("Name of an existing 3d raster map");
  82. out = G_define_option();
  83. out->key = "dspf";
  84. out->type = TYPE_STRING;
  85. out->required = YES;
  86. out->gisprompt = "new_file,file,output";
  87. out->description = _("Name of output display file");
  88. levels = G_define_option();
  89. levels->key = "levels";
  90. levels->type = TYPE_DOUBLE;
  91. levels->required = NO;
  92. levels->multiple = YES;
  93. levels->description = _("List of thresholds for isosurfaces");
  94. min = G_define_option();
  95. min->key = "min";
  96. min->type = TYPE_DOUBLE;
  97. min->required = NO;
  98. min->description = _("Minimum isosurface level");
  99. max = G_define_option();
  100. max->key = "max";
  101. max->type = TYPE_DOUBLE;
  102. max->required = NO;
  103. max->description = _("Maximum isosurface level");
  104. step = G_define_option();
  105. step->key = "step";
  106. step->type = TYPE_DOUBLE;
  107. step->required = NO;
  108. step->description = _("Positive increment between isosurface levels");
  109. tnum = G_define_option();
  110. tnum->key = "tnum";
  111. tnum->type = TYPE_INTEGER;
  112. tnum->required = NO;
  113. tnum->answer = "7";
  114. tnum->description = _("Number of isosurface threshold levels");
  115. quiet = G_define_flag();
  116. quiet->key = 'q';
  117. quiet->description = _("Suppress progress report & min/max information");
  118. shade = G_define_flag();
  119. shade->key = 'f';
  120. shade->description = _("Use flat shading rather than gradient");
  121. if (G_parser(argc, argv))
  122. exit(EXIT_FAILURE);
  123. Rast3d_init_defaults();
  124. Rast3d_get_window(&g3reg);
  125. G_message(_("Region from getWindow: %d %d %d"),
  126. g3reg.rows, g3reg.cols, g3reg.depths);
  127. if (NULL ==
  128. (dspout =
  129. check_get_any_dspname(out->answer, name->answer, G_mapset())))
  130. exit(EXIT_FAILURE);
  131. Rast3d_set_error_fun(Rast3d_print_error);
  132. /* open g3 file for reading and writing */
  133. if (NULL == (mapset = G_find_file2("grid3", name->answer, "")))
  134. G_fatal_error(_("Not able to find grid3 file for [%s]"),
  135. name->answer);
  136. g3map = Rast3d_open_cell_old(name->answer, mapset, &g3reg,
  137. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
  138. /*
  139. g3map = Rast3d_open_cell_old (name->answer, mapset, RASTER3D_DEFAULT_WINDOW,
  140. RASTER3D_TILE_SAME_AS_FILE,
  141. RASTER3D_USE_CACHE_DEFAULT);
  142. */
  143. if (NULL == g3map)
  144. G_fatal_error(_("Error opening grid3 file [%s]"), name->answer);
  145. if (0 == Rast3d_range_load(g3map))
  146. G_fatal_error(_("Error reading range for [%s]"), name->answer);
  147. /* TODO: look at this - should use current 3dregion rather than
  148. region represented by original 3dgrid file */
  149. /*
  150. Rast3d_get_region_struct_map (g3map, &g3reg);
  151. */
  152. /* DONT USE Headfax any more ?
  153. g3read_header(&Headfax);
  154. */
  155. Rast3d_range_min_max(g3map, &dmin, &dmax);
  156. viz_make_header(&Headfax, dmin, dmax, &g3reg);
  157. /* puts command line options into cmndln_info structure */
  158. /* need to send it answers */
  159. viz_calc_tvals(&Headfax.linefax, levels->answers, min->answer,
  160. max->answer, step->answer, tnum->answer,
  161. quiet->answer ? 1 : 0);
  162. if (shade->answer) {
  163. Headfax.linefax.litmodel = 1; /* flat */
  164. }
  165. else
  166. Headfax.linefax.litmodel = 2; /* gradient */
  167. /* open display file for writing */
  168. sprintf(element, "grid3/%s/dsp", name->answer);
  169. if ((Headfax.dspfoutfp = G_fopen_new(element, dspout)) == NULL)
  170. G_fatal_error(_("Error opening display file [%s]"), dspout);
  171. /* write display file header info */
  172. /* have to adjust dimensions -dpg */
  173. {
  174. Headfax.xdim -= 1;
  175. Headfax.ydim -= 1;
  176. Headfax.zdim -= 1;
  177. G_message("DSPF DIMS: %d %d %d", Headfax.ydim + 1, Headfax.xdim + 1,
  178. Headfax.zdim + 1);
  179. if (dfwrite_header(&Headfax) < 0) {
  180. fclose(Headfax.dspfoutfp);
  181. exit(EXIT_FAILURE);
  182. }
  183. Headfax.xdim += 1;
  184. Headfax.ydim += 1;
  185. Headfax.zdim += 1;
  186. }
  187. if (!quiet->answer)
  188. G_message(_("Writing %s from %s..."), dspout, name->answer);
  189. viz_iso_surface(g3map, &g3reg, &Headfax.linefax, quiet->answer ? 1 : 0);
  190. if (!quiet->answer)
  191. fprintf(stderr, "\n");
  192. /* tries to write a header! */
  193. Rast3d_close(g3map);
  194. fclose(Headfax.dspfoutfp);
  195. exit(EXIT_SUCCESS);
  196. }