main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* ***************************************************************
  2. * *
  3. * * MODULE: v.out.pov
  4. * *
  5. * * AUTHOR(S): Radim Blazek
  6. * * OGR support by Martin Landa <landa.martin gmail.com>
  7. * *
  8. * * PURPOSE: Export vector to renderers' format (PovRay)
  9. * *
  10. * * COPYRIGHT: (C) 2001-2012 by the GRASS Development Team
  11. * *
  12. * * This program is free software under the
  13. * * GNU General Public License (>=v2).
  14. * * Read the file COPYING that comes with GRASS
  15. * * for details.
  16. * *
  17. * **************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <grass/gis.h>
  21. #include <grass/vector.h>
  22. #include <grass/glocale.h>
  23. int main(int argc, char *argv[])
  24. {
  25. int i, j, centroid, otype, count;
  26. int nlines, nareas;
  27. int field;
  28. struct GModule *module;
  29. struct Option *in_opt, *field_opt, *out_opt, *type_opt;
  30. struct Option *size_opt, *zmod_opt, *objmod_opt;
  31. FILE *fd;
  32. /* Vector */
  33. struct Map_info In;
  34. struct line_pnts *Points;
  35. struct line_cats *Cats;
  36. int type;
  37. G_gisinit(argv[0]);
  38. /* Module options */
  39. module = G_define_module();
  40. G_add_keyword(_("vector"));
  41. G_add_keyword(_("export"));
  42. module->description =
  43. _("Converts GRASS x,y,z points to POV-Ray x,z,y format.");
  44. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  45. field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  46. type_opt = G_define_standard_option(G_OPT_V3_TYPE);
  47. type_opt->answer = "point,line,area,face";
  48. out_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  49. out_opt->required = YES;
  50. out_opt->description = _("Name for output POV file");
  51. size_opt = G_define_option();
  52. size_opt->key = "size";
  53. size_opt->type = TYPE_STRING;
  54. size_opt->required = NO;
  55. size_opt->answer = "10";
  56. size_opt->label = _("Radius of sphere for points and tube for lines");
  57. size_opt->description = _("May be also variable, e.g. grass_r.");
  58. zmod_opt = G_define_option();
  59. zmod_opt->key = "zmod";
  60. zmod_opt->type = TYPE_STRING;
  61. zmod_opt->required = NO;
  62. zmod_opt->description = _("Modifier for z coordinates");
  63. zmod_opt->description = _("This string is appended to each z coordinate. "
  64. "Examples: '*10', '+1000', '*10+100', '*exaggeration'");
  65. objmod_opt = G_define_option();
  66. objmod_opt->key = "objmod";
  67. objmod_opt->type = TYPE_STRING;
  68. objmod_opt->required = NO;
  69. objmod_opt->label = _("Object modifier (OBJECT_MODIFIER in POV-Ray documentation)");
  70. objmod_opt->description = _("Example: \"pigment { color red 0 green 1 blue 0 }\"");
  71. if (G_parser(argc, argv))
  72. exit(EXIT_FAILURE);
  73. /* Check output type */
  74. otype = Vect_option_to_types(type_opt);
  75. Points = Vect_new_line_struct();
  76. Cats = Vect_new_cats_struct();
  77. /* open input vector */
  78. Vect_set_open_level(2);
  79. if (Vect_open_old2(&In, in_opt->answer, "", field_opt->answer) < 0)
  80. G_fatal_error(_("Unable to open vector map <%s>"), in_opt->answer);
  81. field = Vect_get_field_number(&In, field_opt->answer);
  82. /* Open output file */
  83. if ((fd = fopen(out_opt->answer, "w")) == NULL) {
  84. Vect_close(&In);
  85. G_fatal_error(_("Unable to create output file <%s>"), out_opt->answer);
  86. }
  87. if (zmod_opt->answer == NULL)
  88. zmod_opt->answer = G_store("");
  89. if (objmod_opt->answer == NULL)
  90. objmod_opt->answer = G_store("");
  91. nlines = Vect_get_num_lines(&In);
  92. nareas = Vect_get_num_areas(&In);
  93. count = 0;
  94. /* Lines */
  95. if ((otype &
  96. (GV_POINTS | GV_LINES | GV_BOUNDARY | GV_CENTROID | GV_FACE |
  97. GV_KERNEL))) {
  98. for (i = 1; i <= nlines; i++) {
  99. G_percent(i, nlines, 2);
  100. type = Vect_read_line(&In, Points, Cats, i);
  101. G_debug(2, "line = %d type = %d", i, type);
  102. if (field != -1 && Vect_cat_get(Cats, field, NULL) == 0)
  103. continue;
  104. if (!(otype & type)) {
  105. continue;
  106. }
  107. switch (type) {
  108. case GV_POINT:
  109. case GV_CENTROID:
  110. case GV_KERNEL:
  111. fprintf(fd, "sphere { <%f, %f %s, %f>, %s\n%s\n}\n",
  112. Points->x[0], Points->z[0], zmod_opt->answer,
  113. Points->y[0], size_opt->answer, objmod_opt->answer);
  114. count++;
  115. break;
  116. case GV_LINE:
  117. case GV_BOUNDARY:
  118. if (Points->n_points < 2)
  119. break; /* At least 2 points */
  120. fprintf(fd, "sphere_sweep { linear_spline %d,\n",
  121. Points->n_points);
  122. for (j = 0; j < Points->n_points; j++) {
  123. fprintf(fd, " <%f, %f %s, %f>, %s\n",
  124. Points->x[j], Points->z[j], zmod_opt->answer,
  125. Points->y[j], size_opt->answer);
  126. }
  127. fprintf(fd, " %s\n}\n", objmod_opt->answer);
  128. count++;
  129. break;
  130. case GV_FACE:
  131. if (Points->n_points < 3)
  132. break; /* At least 3 points */
  133. Vect_append_point(Points, Points->x[0], Points->y[0], Points->z[0]); /* close */
  134. fprintf(fd, "polygon { %d, \n", Points->n_points);
  135. for (j = 0; j < Points->n_points; j++) {
  136. fprintf(fd, " <%f, %f %s, %f>\n",
  137. Points->x[j], Points->z[j], zmod_opt->answer,
  138. Points->y[j]);
  139. }
  140. fprintf(fd, " %s\n}\n", objmod_opt->answer);
  141. count++;
  142. break;
  143. }
  144. }
  145. }
  146. /* Areas (run always to count features of different type) */
  147. if (otype & GV_AREA && nareas > 0) {
  148. G_message(_("Processing areas..."));
  149. for (i = 1; i <= nareas; i++) {
  150. G_percent(i, nareas, 2);
  151. /* TODO : Use this later for attributes from database: */
  152. centroid = Vect_get_area_centroid(&In, i);
  153. if (centroid > 0) {
  154. Vect_read_line(&In, NULL, Cats, centroid);
  155. if (field != -1 && Vect_cat_get(Cats, field, NULL) < 0)
  156. continue;
  157. }
  158. G_debug(2, "area = %d centroid = %d", i, centroid);
  159. /* Geometry */
  160. /* Area */
  161. Vect_get_area_points(&In, i, Points);
  162. if (Points->n_points > 2) {
  163. for (j = 0; j < Points->n_points; j++) {
  164. fprintf(fd, "polygon { %d, \n", Points->n_points);
  165. for (j = 0; j < Points->n_points; j++) {
  166. fprintf(fd, " <%f, %f %s, %f>\n",
  167. Points->x[j], Points->z[j], zmod_opt->answer,
  168. Points->y[j]);
  169. }
  170. fprintf(fd, " %s\n}\n", objmod_opt->answer);
  171. }
  172. /* TODO: Isles */
  173. /*
  174. for ( k = 0; k < Vect_get_area_num_isles (&In, i); k++ ) {
  175. Vect_get_isle_points ( &In, Vect_get_area_isle (&In, i, k), Points );
  176. for ( j = 0; j < Points->n_points; j++ ) {
  177. }
  178. }
  179. */
  180. count++;
  181. }
  182. }
  183. }
  184. fclose(fd);
  185. Vect_close(&In);
  186. /* Summary */
  187. G_done_msg(n_("%d feature written.",
  188. "%d features written.",
  189. count), count);
  190. exit(EXIT_SUCCESS);
  191. }