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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 tirons@egi.utah.edu
  14. * @copyright Copyright (c) 2013, Trevor Irons
  15. */
  16. #if LEMMAUSEVTK
  17. #include "RectilinearGridVTKExporter.h"
  18. namespace Lemma {
  19. // ==================== FRIEND METHODS =====================
  20. std::ostream &operator << (std::ostream &stream, const RectilinearGridVTKExporter &ob) {
  21. stream << ob.Serialize() << "\n---\n"; // End of doc
  22. return stream;
  23. }
  24. // ==================== LIFECYCLE =======================
  25. //--------------------------------------------------------------------------------------
  26. // Class: RectilinearGridVTKExporter
  27. // Method: RectilinearGridVTKExporter
  28. // Description: constructor (protected)
  29. //--------------------------------------------------------------------------------------
  30. RectilinearGridVTKExporter::RectilinearGridVTKExporter ( const ctor_key& ) : LemmaObject( ), Grid(nullptr), VTKGrid(nullptr) {
  31. } // ----- end of method RectilinearGridVTKExporter::RectilinearGridVTKExporter (constructor) -----
  32. //--------------------------------------------------------------------------------------
  33. // Class: RectilinearGridVTKExporter
  34. // Method: RectilinearGridVTKExporter
  35. // Description: DeSerializing constructor (protected)
  36. //--------------------------------------------------------------------------------------
  37. RectilinearGridVTKExporter::RectilinearGridVTKExporter (const YAML::Node& node, const ctor_key& key) : LemmaObject(node) {
  38. if (node["Grid"]) {
  39. this->Grid = RectilinearGrid::DeSerialize( node["Grid"] );
  40. }
  41. } // ----- end of method RectilinearGridVTKExporter::RectilinearGridVTKExporter (constructor) -----
  42. //--------------------------------------------------------------------------------------
  43. // Class: RectilinearGridVTKExporter
  44. // Method: NewSP()
  45. // Description: public constructor
  46. //--------------------------------------------------------------------------------------
  47. std::shared_ptr< RectilinearGridVTKExporter > RectilinearGridVTKExporter::NewSP() {
  48. return std::make_shared< RectilinearGridVTKExporter > ( ctor_key() );
  49. }
  50. //--------------------------------------------------------------------------------------
  51. // Class: RectilinearGridVTKExporter
  52. // Method: ~RectilinearGridVTKExporter
  53. // Description: destructor (protected)
  54. //--------------------------------------------------------------------------------------
  55. RectilinearGridVTKExporter::~RectilinearGridVTKExporter () {
  56. } // ----- end of method RectilinearGridVTKExporter::~RectilinearGridVTKExporter (destructor) -----
  57. //--------------------------------------------------------------------------------------
  58. // Class: RectilinearGridVTKExporter
  59. // Method: SetGrid
  60. //--------------------------------------------------------------------------------------
  61. void RectilinearGridVTKExporter::SetGrid ( std::shared_ptr<RectilinearGrid> GridIn ) {
  62. Grid = GridIn;
  63. // I see no harm in just doing this now.
  64. BuildVTKRectilinearGrid();
  65. return ;
  66. } // ----- end of method RectilinearGridVTKExporter::SetGrid -----
  67. //--------------------------------------------------------------------------------------
  68. // Class: RectilinearGridVTKExporter
  69. // Method: Serialize
  70. //--------------------------------------------------------------------------------------
  71. YAML::Node RectilinearGridVTKExporter::Serialize ( ) const {
  72. YAML::Node node = LemmaObject::Serialize();;
  73. node.SetTag( GetName() );
  74. if (Grid != nullptr) {
  75. node["Grid"] = Grid->Serialize();
  76. }
  77. return node;
  78. } // ----- end of method RectilinearGridVTKExporter::Serialize -----
  79. //--------------------------------------------------------------------------------------
  80. // Class: RectilinearGridVTKExporter
  81. // Method: DeSerialize
  82. //--------------------------------------------------------------------------------------
  83. std::shared_ptr<RectilinearGridVTKExporter> RectilinearGridVTKExporter::DeSerialize ( const YAML::Node& node ) {
  84. if (node.Tag() != "RectilinearGridVTKExporter") {
  85. throw DeSerializeTypeMismatch( "RectilinearGridVTKExporter", node.Tag());
  86. }
  87. return std::make_shared< RectilinearGridVTKExporter >( node, ctor_key() ); //, ctor_key() );
  88. } // ----- end of method RectilinearGridVTKExporter::DeSerialize -----
  89. //--------------------------------------------------------------------------------------
  90. // Class: RectilinearGridVTKExporter
  91. // Method: GetVTKRectilinearGrid
  92. //--------------------------------------------------------------------------------------
  93. vtkSmartPointer<vtkRectilinearGrid> RectilinearGridVTKExporter::GetVTKGrid ( ) {
  94. return VTKGrid;
  95. } // ----- end of method RectilinearGridVTKExporter::GetVTKRectilinearGrid -----
  96. //--------------------------------------------------------------------------------------
  97. // Class: RectilinearGridVTKExporter
  98. // Method: WriteVTKGrid
  99. //--------------------------------------------------------------------------------------
  100. void RectilinearGridVTKExporter::WriteVTKGrid ( const std::string& fname ) {
  101. vtkXMLRectilinearGridWriter *gridWrite = vtkXMLRectilinearGridWriter::New();
  102. gridWrite->SetInputData(VTKGrid);
  103. gridWrite->SetFileName( (fname+std::string(".vtr")).c_str() );
  104. gridWrite->Update();
  105. gridWrite->Write();
  106. gridWrite->Delete();
  107. return ;
  108. } // ----- end of method RectilinearGridVTKExporter::WriteVTKGrid -----
  109. //--------------------------------------------------------------------------------------
  110. // Class: RectilinearGridVTKExporter
  111. // Method: BuildVTKRectilinearGrid
  112. //--------------------------------------------------------------------------------------
  113. void RectilinearGridVTKExporter::BuildVTKRectilinearGrid ( ) {
  114. // Set Coordinate>s
  115. vtkDoubleArray *xCoords = vtkDoubleArray::New();
  116. xCoords->InsertNextValue(Grid->GetOx()-Grid->GetDx(0)/2.);
  117. double xm1 = Grid->GetOx() - Grid->GetDx(0)/2.;
  118. for (int ix=0; ix<Grid->GetNx(); ix++) {
  119. xCoords->InsertNextValue(xm1 + Grid->GetDx(ix));
  120. xm1 += Grid->GetDx(ix);
  121. }
  122. vtkDoubleArray *yCoords = vtkDoubleArray::New();
  123. yCoords->InsertNextValue(Grid->GetOy()-Grid->GetDy(0)/2.);
  124. double ym1 = Grid->GetOy()-Grid->GetDy(0)/2.;
  125. for (int iy=0; iy<Grid->GetNy(); iy++) {
  126. yCoords->InsertNextValue(ym1 + Grid->GetDy(iy));
  127. ym1 += Grid->GetDy(iy);
  128. }
  129. vtkDoubleArray *zCoords = vtkDoubleArray::New();
  130. zCoords->InsertNextValue(Grid->GetOz()-Grid->GetDz(0)/2.);
  131. double zm1 = Grid->GetOz()-Grid->GetDz(0)/2.;
  132. for (int iz=0; iz<Grid->GetNz(); iz++) {
  133. zCoords->InsertNextValue(zm1 + Grid->GetDz(iz));
  134. zm1 += Grid->GetDz(iz);
  135. }
  136. VTKGrid = vtkSmartPointer<vtkRectilinearGrid>::New();
  137. VTKGrid->SetDimensions(Grid->GetNx()+1, Grid->GetNy()+1, Grid->GetNz()+1);
  138. VTKGrid->SetXCoordinates(xCoords);
  139. VTKGrid->SetYCoordinates(yCoords);
  140. VTKGrid->SetZCoordinates(zCoords);
  141. // Clean up
  142. xCoords->Delete();
  143. yCoords->Delete();
  144. zCoords->Delete();
  145. return ;
  146. } // ----- end of method RectilinearGridVTKExporter::BuildVTKRectilinearGrid -----
  147. } // ----- end of Lemma name -----
  148. #endif // ----- not LEMMAUSEVTK -----
  149. /* vim: set tabstop=4 expandtab: */
  150. /* vim: set filetype=cpp syntax=cpp.doxygen: */