Main Lemma Repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RectilinearGridVTKExporter.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* This file is part of Lemma, a geophysical modelling and inversion API.
  2. * More information is available at http://lemmasoftware.org
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. /**
  9. * @file
  10. * @date 09/25/2013 08:20:14 AM
  11. * @version $Id$
  12. * @author Trevor Irons (ti)
  13. * @email Trevor.Irons@xri-geo.com
  14. * @copyright Copyright (c) 2013, XRI Geophysics, LLC
  15. * @copyright Copyright (c) 2013, Trevor Irons
  16. */
  17. #if LEMMAUSEVTK
  18. #include "RectilinearGridVTKExporter.h"
  19. namespace Lemma {
  20. // ==================== FRIEND METHODS =====================
  21. std::ostream &operator << (std::ostream &stream, const RectilinearGridVTKExporter &ob) {
  22. stream << ob.Serialize() << "\n---\n"; // End of doc --- as a direct stream should encapulste thingy
  23. return stream;
  24. }
  25. // ==================== LIFECYCLE =======================
  26. //--------------------------------------------------------------------------------------
  27. // Class: RectilinearGridVTKExporter
  28. // Method: RectilinearGridVTKExporter
  29. // Description: constructor (protected)
  30. //--------------------------------------------------------------------------------------
  31. RectilinearGridVTKExporter::RectilinearGridVTKExporter (const std::string& name) :
  32. LemmaObject(name), Grid(nullptr), VTKGrid(nullptr) {
  33. } // ----- end of method RectilinearGridVTKExporter::RectilinearGridVTKExporter (constructor) -----
  34. //--------------------------------------------------------------------------------------
  35. // Class: RectilinearGridVTKExporter
  36. // Method: NewSP()
  37. // Description: public constructor
  38. //--------------------------------------------------------------------------------------
  39. std::shared_ptr< RectilinearGridVTKExporter > RectilinearGridVTKExporter::NewSP() {
  40. std::shared_ptr<RectilinearGridVTKExporter> sp(new RectilinearGridVTKExporter("RectilinearGridVTKExporter"), LemmaObjectDeleter() );
  41. return sp;
  42. }
  43. //--------------------------------------------------------------------------------------
  44. // Class: RectilinearGridVTKExporter
  45. // Method: ~RectilinearGridVTKExporter
  46. // Description: destructor (protected)
  47. //--------------------------------------------------------------------------------------
  48. RectilinearGridVTKExporter::~RectilinearGridVTKExporter () {
  49. } // ----- end of method RectilinearGridVTKExporter::~RectilinearGridVTKExporter (destructor) -----
  50. //--------------------------------------------------------------------------------------
  51. // Class: RectilinearGridVTKExporter
  52. // Method: SetGrid
  53. //--------------------------------------------------------------------------------------
  54. void RectilinearGridVTKExporter::SetGrid ( std::shared_ptr<RectilinearGrid> GridIn ) {
  55. Grid = GridIn;
  56. // I see no harm in just doing this now.
  57. BuildVTKRectilinearGrid();
  58. return ;
  59. } // ----- end of method RectilinearGridVTKExporter::SetGrid -----
  60. //--------------------------------------------------------------------------------------
  61. // Class: RectilinearGridVTKExporter
  62. // Method: GetVTKRectilinearGrid
  63. //--------------------------------------------------------------------------------------
  64. vtkSmartPointer<vtkRectilinearGrid> RectilinearGridVTKExporter::GetVTKGrid ( ) {
  65. return VTKGrid;
  66. } // ----- end of method RectilinearGridVTKExporter::GetVTKRectilinearGrid -----
  67. //--------------------------------------------------------------------------------------
  68. // Class: RectilinearGridVTKExporter
  69. // Method: WriteVTKGrid
  70. //--------------------------------------------------------------------------------------
  71. void RectilinearGridVTKExporter::WriteVTKGrid ( const std::string& fname ) {
  72. vtkXMLRectilinearGridWriter *gridWrite = vtkXMLRectilinearGridWriter::New();
  73. gridWrite->SetInputData(VTKGrid);
  74. gridWrite->SetFileName( (fname+std::string(".vtr")).c_str() );
  75. gridWrite->Update();
  76. gridWrite->Write();
  77. gridWrite->Delete();
  78. return ;
  79. } // ----- end of method RectilinearGridVTKExporter::WriteVTKGrid -----
  80. //--------------------------------------------------------------------------------------
  81. // Class: RectilinearGridVTKExporter
  82. // Method: BuildVTKRectilinearGrid
  83. //--------------------------------------------------------------------------------------
  84. void RectilinearGridVTKExporter::BuildVTKRectilinearGrid ( ) {
  85. // Set Coordinate>s
  86. vtkDoubleArray *xCoords = vtkDoubleArray::New();
  87. xCoords->InsertNextValue(Grid->GetOx()-Grid->GetDx(0)/2.);
  88. double xm1 = Grid->GetOx() - Grid->GetDx(0)/2.;
  89. for (int ix=0; ix<Grid->GetNx(); ix++) {
  90. xCoords->InsertNextValue(xm1 + Grid->GetDx(ix));
  91. xm1 += Grid->GetDx(ix);
  92. }
  93. vtkDoubleArray *yCoords = vtkDoubleArray::New();
  94. yCoords->InsertNextValue(Grid->GetOy()-Grid->GetDy(0)/2.);
  95. double ym1 = Grid->GetOy()-Grid->GetDy(0)/2.;
  96. for (int iy=0; iy<Grid->GetNy(); iy++) {
  97. yCoords->InsertNextValue(ym1 + Grid->GetDy(iy));
  98. ym1 += Grid->GetDy(iy);
  99. }
  100. vtkDoubleArray *zCoords = vtkDoubleArray::New();
  101. zCoords->InsertNextValue(Grid->GetOz()-Grid->GetDz(0)/2.);
  102. double zm1 = Grid->GetOz()-Grid->GetDz(0)/2.;
  103. for (int iz=0; iz<Grid->GetNz(); iz++) {
  104. zCoords->InsertNextValue(zm1 + Grid->GetDz(iz));
  105. zm1 += Grid->GetDz(iz);
  106. }
  107. VTKGrid = vtkSmartPointer<vtkRectilinearGrid>::New();
  108. VTKGrid->SetDimensions(Grid->GetNx()+1, Grid->GetNy()+1, Grid->GetNz()+1);
  109. VTKGrid->SetXCoordinates(xCoords);
  110. VTKGrid->SetYCoordinates(yCoords);
  111. VTKGrid->SetZCoordinates(zCoords);
  112. // Clean up
  113. xCoords->Delete();
  114. yCoords->Delete();
  115. zCoords->Delete();
  116. return ;
  117. } // ----- end of method RectilinearGridVTKExporter::BuildVTKRectilinearGrid -----
  118. } // ----- end of Lemma name -----
  119. #endif // ----- not LEMMAUSEVTK -----