Lemma is an Electromagnetics API
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RectilinearGridVTKExporter.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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& key ) : LemmaObject( key ), 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, key) {
  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: GetName
  60. // Description: Class identifier
  61. //--------------------------------------------------------------------------------------
  62. inline std::string RectilinearGridVTKExporter::GetName ( ) const {
  63. return CName;
  64. } // ----- end of method RectilinearGridVTKExporter::GetName -----
  65. //--------------------------------------------------------------------------------------
  66. // Class: RectilinearGridVTKExporter
  67. // Method: SetGrid
  68. //--------------------------------------------------------------------------------------
  69. void RectilinearGridVTKExporter::SetGrid ( std::shared_ptr<RectilinearGrid> GridIn ) {
  70. Grid = GridIn;
  71. // I see no harm in just doing this now.
  72. BuildVTKRectilinearGrid();
  73. return ;
  74. } // ----- end of method RectilinearGridVTKExporter::SetGrid -----
  75. //--------------------------------------------------------------------------------------
  76. // Class: RectilinearGridVTKExporter
  77. // Method: Serialize
  78. //--------------------------------------------------------------------------------------
  79. YAML::Node RectilinearGridVTKExporter::Serialize ( ) const {
  80. YAML::Node node = LemmaObject::Serialize();;
  81. node.SetTag( GetName() );
  82. if (Grid != nullptr) {
  83. node["Grid"] = Grid->Serialize();
  84. }
  85. return node;
  86. } // ----- end of method RectilinearGridVTKExporter::Serialize -----
  87. //--------------------------------------------------------------------------------------
  88. // Class: RectilinearGridVTKExporter
  89. // Method: DeSerialize
  90. //--------------------------------------------------------------------------------------
  91. std::shared_ptr<RectilinearGridVTKExporter> RectilinearGridVTKExporter::DeSerialize ( const YAML::Node& node ) {
  92. if (node.Tag() != "RectilinearGridVTKExporter") {
  93. throw DeSerializeTypeMismatch( "RectilinearGridVTKExporter", node.Tag());
  94. }
  95. return std::make_shared< RectilinearGridVTKExporter >( node, ctor_key() ); //, ctor_key() );
  96. } // ----- end of method RectilinearGridVTKExporter::DeSerialize -----
  97. //--------------------------------------------------------------------------------------
  98. // Class: RectilinearGridVTKExporter
  99. // Method: GetVTKRectilinearGrid
  100. //--------------------------------------------------------------------------------------
  101. vtkSmartPointer<vtkRectilinearGrid> RectilinearGridVTKExporter::GetVTKGrid ( ) {
  102. return VTKGrid;
  103. } // ----- end of method RectilinearGridVTKExporter::GetVTKRectilinearGrid -----
  104. //--------------------------------------------------------------------------------------
  105. // Class: RectilinearGridVTKExporter
  106. // Method: WriteVTKGrid
  107. //--------------------------------------------------------------------------------------
  108. void RectilinearGridVTKExporter::WriteVTKGrid ( const std::string& fname ) {
  109. vtkXMLRectilinearGridWriter *gridWrite = vtkXMLRectilinearGridWriter::New();
  110. gridWrite->SetInputData(VTKGrid);
  111. gridWrite->SetFileName( (fname+std::string(".vtr")).c_str() );
  112. gridWrite->Update();
  113. gridWrite->Write();
  114. gridWrite->Delete();
  115. return ;
  116. } // ----- end of method RectilinearGridVTKExporter::WriteVTKGrid -----
  117. //--------------------------------------------------------------------------------------
  118. // Class: RectilinearGridVTKExporter
  119. // Method: BuildVTKRectilinearGrid
  120. //--------------------------------------------------------------------------------------
  121. void RectilinearGridVTKExporter::BuildVTKRectilinearGrid ( ) {
  122. // Set Coordinate>s
  123. vtkDoubleArray *xCoords = vtkDoubleArray::New();
  124. xCoords->InsertNextValue(Grid->GetOx()-Grid->GetDx1(0)/2.);
  125. double xm1 = Grid->GetOx() - Grid->GetDx1(0)/2.;
  126. for (int ix=0; ix<Grid->GetNx(); ix++) {
  127. xCoords->InsertNextValue(xm1 + Grid->GetDx1(ix));
  128. xm1 += Grid->GetDx1(ix);
  129. }
  130. vtkDoubleArray *yCoords = vtkDoubleArray::New();
  131. yCoords->InsertNextValue(Grid->GetOy()-Grid->GetDy1(0)/2.);
  132. double ym1 = Grid->GetOy()-Grid->GetDy1(0)/2.;
  133. for (int iy=0; iy<Grid->GetNy(); iy++) {
  134. yCoords->InsertNextValue(ym1 + Grid->GetDy1(iy));
  135. ym1 += Grid->GetDy1(iy);
  136. }
  137. vtkDoubleArray *zCoords = vtkDoubleArray::New();
  138. zCoords->InsertNextValue(Grid->GetOz()-Grid->GetDz1(0)/2.);
  139. double zm1 = Grid->GetOz()-Grid->GetDz1(0)/2.;
  140. for (int iz=0; iz<Grid->GetNz(); iz++) {
  141. zCoords->InsertNextValue(zm1 + Grid->GetDz1(iz));
  142. zm1 += Grid->GetDz1(iz);
  143. }
  144. VTKGrid = vtkSmartPointer<vtkRectilinearGrid>::New();
  145. VTKGrid->SetDimensions(Grid->GetNx()+1, Grid->GetNy()+1, Grid->GetNz()+1);
  146. VTKGrid->SetXCoordinates(xCoords);
  147. VTKGrid->SetYCoordinates(yCoords);
  148. VTKGrid->SetZCoordinates(zCoords);
  149. // Clean up
  150. xCoords->Delete();
  151. yCoords->Delete();
  152. zCoords->Delete();
  153. return ;
  154. } // ----- end of method RectilinearGridVTKExporter::BuildVTKRectilinearGrid -----
  155. } // ----- end of Lemma name -----
  156. #endif // ----- not LEMMAUSEVTK -----
  157. /* vim: set tabstop=4 expandtab: */
  158. /* vim: set filetype=cpp syntax=cpp.doxygen: */