put_grid.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <grass/raster.h>
  2. #include <grass/glocale.h>
  3. #include "pv.h"
  4. /*
  5. * Use centers of GRASS CELLS as vertexes for grid.
  6. * Currently, grid space is "unitized" so that the
  7. * largest dimension of the current region in GRASS == 1.0
  8. */
  9. void vrml_put_grid(FILE * vout,
  10. struct Cell_head *w,
  11. int elevfd, int colorfd,
  12. struct Colors *colr,
  13. int color_ok, int rows, int cols, int shh)
  14. {
  15. char str[512];
  16. FCELL *tf;
  17. FCELL *dbuf;
  18. dbuf = (FCELL *) G_malloc(cols * sizeof(FCELL));
  19. #ifdef VRML2
  20. fprintf(vout, "grid\n");
  21. vrml_putline(0, vout, "grid");
  22. #else
  23. vrml_putline(0, vout, "Separator");
  24. vrml_putline(1, vout, OCB);
  25. /* write grid vertices */
  26. {
  27. double coordx, coordy, coordz;
  28. int row, col;
  29. if (!shh)
  30. G_message(_("Writing vertices..."));
  31. vrml_putline(0, vout, "Coordinate3");
  32. vrml_putline(1, vout, OCB);
  33. vrml_putline(0, vout, "point");
  34. vrml_putline(1, vout, OSB);
  35. for (row = 0; row < rows; row++) {
  36. tf = dbuf;
  37. if (!shh)
  38. G_percent(row, rows - 1, 10);
  39. Rast_get_f_row(elevfd, tf, row);
  40. coordz = Rast_row_to_northing((double)row, w);
  41. do_coordcnv(&coordz, 'z');
  42. /* write a row */
  43. for (col = 0; col < cols; col++) {
  44. coordx = Rast_col_to_easting((double)col, w);
  45. do_coordcnv(&coordx, 'x');
  46. /* HACK: no nulls in vrml grid */
  47. if (Rast_is_f_null_value(tf))
  48. *tf = 0.0;
  49. coordy = *tf;
  50. do_coordcnv(&coordy, 'y');
  51. sprintf(str, "%f %f %f,", coordx, coordy, coordz);
  52. vrml_putline(0, vout, str);
  53. tf++;
  54. }
  55. /* end a row */
  56. }
  57. vrml_putline(-1, vout, CSB); /* end point */
  58. vrml_putline(-1, vout, CCB); /* end Coordinate3 */
  59. }
  60. if (color_ok)
  61. /* write material color */
  62. {
  63. int row, col;
  64. unsigned char *red, *green, *blue, *set;
  65. if (!shh)
  66. G_message(_("Writing color file..."));
  67. vrml_putline(0, vout, "Material");
  68. vrml_putline(1, vout, OCB);
  69. vrml_putline(0, vout, "diffuseColor");
  70. vrml_putline(1, vout, OSB);
  71. /* allocate buffers */
  72. red = G_malloc(cols);
  73. green = G_malloc(cols);
  74. blue = G_malloc(cols);
  75. set = G_malloc(cols);
  76. tf = dbuf;
  77. for (row = 0; row < rows; row++) {
  78. if (!shh)
  79. G_percent(row, rows - 1, 5);
  80. Rast_get_f_row(colorfd, tf, row);
  81. Rast_lookup_f_colors(tf, red, green, blue, set, cols, colr);
  82. for (col = 0; col < cols; col++) {
  83. sprintf(str, "%.3f %.3f %.3f,",
  84. red[col] / 255., green[col] / 255., blue[col] / 255.);
  85. vrml_putline(0, vout, str);
  86. }
  87. }
  88. vrml_putline(-1, vout, CSB); /* end diffuseColor */
  89. vrml_putline(-1, vout, CCB); /* end Material */
  90. vrml_putline(0, vout, "MaterialBinding");
  91. vrml_putline(1, vout, OCB);
  92. vrml_putline(0, vout, "value PER_VERTEX_INDEXED");
  93. vrml_putline(-1, vout, CCB); /* end MaterialBinding */
  94. G_free(red);
  95. G_free(green);
  96. G_free(blue);
  97. G_free(set);
  98. }
  99. /* write face set indices */
  100. {
  101. int row, col, c1, c2;
  102. vrml_putline(0, vout, "IndexedFaceSet");
  103. vrml_putline(1, vout, OCB);
  104. vrml_putline(0, vout, "coordIndex");
  105. vrml_putline(1, vout, OSB);
  106. /* write indexes */
  107. for (row = 0; row < rows - 1; row++) {
  108. for (col = 0; col < cols - 1; col++) {
  109. c1 = row * cols + col;
  110. c2 = c1 + cols + 1;
  111. sprintf(str, "%d, %d, %d, -1, %d, %d, %d, -1,",
  112. c1, c1 + cols, c1 + 1, c2, c2 - cols, c2 - 1);
  113. vrml_putline(0, vout, str);
  114. }
  115. }
  116. vrml_putline(-1, vout, CSB); /* end coordIndex */
  117. vrml_putline(-1, vout, CCB); /* end IndexedFaceSet */
  118. }
  119. vrml_putline(-1, vout, CCB); /* end Separator */
  120. #endif
  121. /*
  122. G_free(ibuf);
  123. */
  124. G_free(dbuf);
  125. }