writeVTKHead.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 (RASTER3D) 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/raster3d.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 structured points Header ************************************* */
  29. /* ************************************************************************* */
  30. void write_vtk_structured_point_header(FILE * fp, char *vtkFile,
  31. RASTER3D_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 GIS 7 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. } else {
  61. if (param.origin->answer)
  62. fprintf(fp, "ORIGIN %.*f %.*f %.*f\n", dp, region.west - x_extent,
  63. dp, region.south - y_extent, dp, region.bottom * scale);
  64. else
  65. fprintf(fp, "ORIGIN %.*f %.*f %.*f\n", dp, region.west - x_extent,
  66. dp, region.south - y_extent, dp, region.bottom);
  67. }
  68. if (param.point->answer)
  69. fprintf(fp, "POINT_DATA %i\n", region.cols * region.rows * region.depths); /*We have pointdata */
  70. else
  71. fprintf(fp, "CELL_DATA %i\n", region.cols * region.rows * region.depths); /*We have celldata */
  72. return;
  73. }
  74. /* ************************************************************************* */
  75. /* Writes the strcutured grid header **************************************** */
  76. /* ************************************************************************* */
  77. void write_vtk_structured_grid_header(FILE * fp, char *vtkFile,
  78. RASTER3D_Region region)
  79. {
  80. G_debug(3,
  81. "write_vtk_structured_grid_header: Writing VTKStructuredGrid-Header");
  82. fprintf(fp, "# vtk DataFile Version 3.0\n");
  83. fprintf(fp, "GRASS GIS 7 Export\n");
  84. fprintf(fp, "ASCII\n");
  85. fprintf(fp, "DATASET STRUCTURED_GRID\n"); /*We are using the structured grid dataset. */
  86. fprintf(fp, "DIMENSIONS %i %i %i\n", region.cols, region.rows,
  87. region.depths);
  88. /*Only point data is available */
  89. fprintf(fp, "POINTS %i float\n",
  90. region.cols * region.rows * region.depths);
  91. return;
  92. }
  93. /* ************************************************************************* */
  94. /* Writes the unstrcutured grid header ************************************* */
  95. /* ************************************************************************* */
  96. void write_vtk_unstructured_grid_header(FILE * fp, char *vtkFile,
  97. RASTER3D_Region region)
  98. {
  99. G_debug(3,
  100. "write_vtk_unstructured_grid_header: Writing VTKUnstructuredGrid-Header");
  101. fprintf(fp, "# vtk DataFile Version 3.0\n");
  102. fprintf(fp, "GRASS GIS 7 Export\n");
  103. fprintf(fp, "ASCII\n");
  104. fprintf(fp, "DATASET UNSTRUCTURED_GRID\n"); /*We are using the unstructured grid dataset. */
  105. /*Only cell data is available, because we creating a hexaeder/vtk-voxel for every voxel */
  106. fprintf(fp, "POINTS %i float\n", region.cols * region.rows * region.depths * 8); /*a Voxel has 8 points */
  107. return;
  108. }