Lemma is an Electromagnetics API
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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 << *(LemmaObject*)(&ob);
  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(NULL), VTKGrid(NULL) {
  33. } // ----- end of method RectilinearGridVTKExporter::RectilinearGridVTKExporter (constructor) -----
  34. //--------------------------------------------------------------------------------------
  35. // Class: RectilinearGridVTKExporter
  36. // Method: New()
  37. // Description: public constructor
  38. //--------------------------------------------------------------------------------------
  39. RectilinearGridVTKExporter* RectilinearGridVTKExporter::New() {
  40. RectilinearGridVTKExporter* Obj = new RectilinearGridVTKExporter("RectilinearGridVTKExporter");
  41. Obj->AttachTo(Obj);
  42. return Obj;
  43. }
  44. //--------------------------------------------------------------------------------------
  45. // Class: RectilinearGridVTKExporter
  46. // Method: ~RectilinearGridVTKExporter
  47. // Description: destructor (protected)
  48. //--------------------------------------------------------------------------------------
  49. RectilinearGridVTKExporter::~RectilinearGridVTKExporter () {
  50. if (Grid) Grid->DetachFrom(this);
  51. if (VTKGrid) VTKGrid->Delete();
  52. } // ----- end of method RectilinearGridVTKExporter::~RectilinearGridVTKExporter (destructor) -----
  53. //--------------------------------------------------------------------------------------
  54. // Class: RectilinearGridVTKExporter
  55. // Method: Delete
  56. // Description: public destructor
  57. //--------------------------------------------------------------------------------------
  58. void RectilinearGridVTKExporter::Delete() {
  59. this->DetachFrom(this);
  60. }
  61. //--------------------------------------------------------------------------------------
  62. // Class: RectilinearGridVTKExporter
  63. // Method: Release
  64. // Description: destructor (protected)
  65. //--------------------------------------------------------------------------------------
  66. void RectilinearGridVTKExporter::Release() {
  67. delete this;
  68. }
  69. //--------------------------------------------------------------------------------------
  70. // Class: RectilinearGridVTKExporter
  71. // Method: SetGrid
  72. //--------------------------------------------------------------------------------------
  73. void RectilinearGridVTKExporter::SetGrid ( RectilinearGrid* GridIn ) {
  74. if (Grid) Grid->DetachFrom(this);
  75. Grid = GridIn;
  76. Grid->AttachTo(this);
  77. // I see no harm in just doing this now.
  78. BuildVTKRectilinearGrid();
  79. return ;
  80. } // ----- end of method RectilinearGridVTKExporter::SetGrid -----
  81. //--------------------------------------------------------------------------------------
  82. // Class: RectilinearGridVTKExporter
  83. // Method: GetVTKRectilinearGrid
  84. //--------------------------------------------------------------------------------------
  85. vtkRectilinearGrid* RectilinearGridVTKExporter::GetVTKGrid ( ) {
  86. return VTKGrid;
  87. } // ----- end of method RectilinearGridVTKExporter::GetVTKRectilinearGrid -----
  88. //--------------------------------------------------------------------------------------
  89. // Class: RectilinearGridVTKExporter
  90. // Method: WriteVTKGrid
  91. //--------------------------------------------------------------------------------------
  92. void RectilinearGridVTKExporter::WriteVTKGrid ( const std::string& fname ) {
  93. vtkXMLRectilinearGridWriter *gridWrite = vtkXMLRectilinearGridWriter::New();
  94. gridWrite->SetInputData(VTKGrid);
  95. gridWrite->SetFileName( (fname+std::string(".vtr")).c_str() );
  96. gridWrite->Update();
  97. gridWrite->Write();
  98. return ;
  99. } // ----- end of method RectilinearGridVTKExporter::WriteVTKGrid -----
  100. //--------------------------------------------------------------------------------------
  101. // Class: RectilinearGridVTKExporter
  102. // Method: BuildVTKRectilinearGrid
  103. //--------------------------------------------------------------------------------------
  104. void RectilinearGridVTKExporter::BuildVTKRectilinearGrid ( ) {
  105. if (VTKGrid) VTKGrid->Delete();
  106. // Set Coordinates
  107. vtkDoubleArray *xCoords = vtkDoubleArray::New();
  108. xCoords->InsertNextValue(Grid->GetOx()-Grid->GetDx(0)/2.);
  109. double xm1 = Grid->GetOx() - Grid->GetDx(0)/2.;
  110. for (int ix=0; ix<Grid->GetNx(); ix++) {
  111. xCoords->InsertNextValue(xm1 + Grid->GetDx(ix));
  112. xm1 += Grid->GetDx(ix);
  113. }
  114. vtkDoubleArray *yCoords = vtkDoubleArray::New();
  115. yCoords->InsertNextValue(Grid->GetOy()-Grid->GetDy(0)/2.);
  116. double ym1 = Grid->GetOy()-Grid->GetDy(0)/2.;
  117. for (int iy=0; iy<Grid->GetNy(); iy++) {
  118. yCoords->InsertNextValue(ym1 + Grid->GetDy(iy));
  119. ym1 += Grid->GetDy(iy);
  120. }
  121. vtkDoubleArray *zCoords = vtkDoubleArray::New();
  122. zCoords->InsertNextValue(Grid->GetOz()-Grid->GetDz(0)/2.);
  123. double zm1 = Grid->GetOz()-Grid->GetDz(0)/2.;
  124. for (int iz=0; iz<Grid->GetNz(); iz++) {
  125. zCoords->InsertNextValue(zm1 + Grid->GetDz(iz));
  126. zm1 += Grid->GetDz(iz);
  127. }
  128. VTKGrid = vtkRectilinearGrid::New();
  129. VTKGrid->SetDimensions(Grid->GetNx()+1, Grid->GetNy()+1, Grid->GetNz()+1);
  130. VTKGrid->SetXCoordinates(xCoords);
  131. VTKGrid->SetYCoordinates(yCoords);
  132. VTKGrid->SetZCoordinates(zCoords);
  133. // Clean up
  134. xCoords->Delete();
  135. yCoords->Delete();
  136. zCoords->Delete();
  137. return ;
  138. } // ----- end of method RectilinearGridVTKExporter::BuildVTKRectilinearGrid -----
  139. } // ----- end of Lemma name -----
  140. #endif // ----- not LEMMAUSEVTK -----