writeVTKHead.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/G3d.h>
  22. #include <grass/glocale.h>
  23. #include "globalDefs.h"
  24. #include "writeVTKHead.h"
  25. #include "parameters.h"
  26. #include "errorHandling.h"
  27. /* ************************************************************************* */
  28. /* Writes the strcutured points Header ************************************* */
  29. /* ************************************************************************* */
  30. void write_vtk_structured_point_header(FILE * fp, char *vtkFile,
  31. G3D_Region region, int dp,
  32. double scale)
  33. {
  34. G_debug(3,
  35. _("write_vtk_structured_point_header: Writing VTKStructuredPoint-Header"));
  36. /*Simple vtk ASCII header */
  37. fprintf(fp, "# vtk DataFile Version 3.0\n");
  38. fprintf(fp, "GRASS 6 Export\n");
  39. fprintf(fp, "ASCII\n");
  40. fprintf(fp, "DATASET STRUCTURED_POINTS\n"); /*We are using the structured point dataset. */
  41. if (param.point->answer)
  42. fprintf(fp, "DIMENSIONS %i %i %i\n", region.cols, region.rows,
  43. region.depths);
  44. else
  45. fprintf(fp, "DIMENSIONS %i %i %i\n", region.cols + 1, region.rows + 1,
  46. region.depths + 1);
  47. fprintf(fp, "SPACING %.*f %.*f %.*f\n", dp, region.ew_res, dp,
  48. region.ns_res, dp, (region.tb_res * scale));
  49. if (param.point->answer) {
  50. if (param.origin->answer)
  51. fprintf(fp, "ORIGIN %.*f %.*f %.*f\n", dp,
  52. (region.west + region.ew_res / 2) - x_extent, dp,
  53. (region.south + region.ns_res / 2) - y_extent, dp,
  54. region.bottom * scale + (region.tb_res * scale) / 2);
  55. else
  56. fprintf(fp, "ORIGIN %.*f %.*f %.*f\n", dp,
  57. (region.west + region.ew_res / 2) - x_extent, dp,
  58. (region.south + region.ns_res / 2) - y_extent, dp,
  59. region.bottom + (region.tb_res * scale) / 2);
  60. }
  61. else {
  62. if (param.origin->answer)
  63. fprintf(fp, "ORIGIN %.*f %.*f %.*f\n", dp, region.west - x_extent,
  64. dp, region.south - y_extent, dp, region.bottom * scale);
  65. else
  66. fprintf(fp, "ORIGIN %.*f %.*f %.*f\n", dp, region.west - x_extent,
  67. dp, region.south - y_extent, dp, region.bottom);
  68. }
  69. if (param.point->answer)
  70. fprintf(fp, "POINT_DATA %i\n", region.cols * region.rows * region.depths); /*We have pointdata */
  71. else
  72. fprintf(fp, "CELL_DATA %i\n", region.cols * region.rows * region.depths); /*We have celldata */
  73. return;
  74. }
  75. /* ************************************************************************* */
  76. /* Writes the strcutured grid header **************************************** */
  77. /* ************************************************************************* */
  78. void write_vtk_structured_grid_header(FILE * fp, char *vtkFile,
  79. G3D_Region region)
  80. {
  81. G_debug(3,
  82. _("write_vtk_structured_grid_header: Writing VTKStructuredGrid-Header"));
  83. fprintf(fp, "# vtk DataFile Version 3.0\n");
  84. fprintf(fp, "GRASS 6 Export\n");
  85. fprintf(fp, "ASCII\n");
  86. fprintf(fp, "DATASET STRUCTURED_GRID\n"); /*We are using the structured grid dataset. */
  87. fprintf(fp, "DIMENSIONS %i %i %i\n", region.cols, region.rows,
  88. region.depths);
  89. /*Only point data is available */
  90. fprintf(fp, "POINTS %i float\n",
  91. region.cols * region.rows * region.depths);
  92. return;
  93. }
  94. /* ************************************************************************* */
  95. /* Writes the unstrcutured grid header ************************************* */
  96. /* ************************************************************************* */
  97. void write_vtk_unstructured_grid_header(FILE * fp, char *vtkFile,
  98. G3D_Region region)
  99. {
  100. G_debug(3,
  101. _("write_vtk_unstructured_grid_header: Writing VTKUnstructuredGrid-Header"));
  102. fprintf(fp, "# vtk DataFile Version 3.0\n");
  103. fprintf(fp, "GRASS 6 Export\n");
  104. fprintf(fp, "ASCII\n");
  105. fprintf(fp, "DATASET UNSTRUCTURED_GRID\n"); /*We are using the unstructured grid dataset. */
  106. /*Only cell data is available, because we creating a hexaeder/vtk-voxel for every voxel */
  107. fprintf(fp, "POINTS %i float\n", region.cols * region.rows * region.depths * 8); /*a Voxel has 8 points */
  108. return;
  109. }