1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/*
* Copyright (C) 2011,2012 Thorsten Liebig (Thorsten.Liebig@gmx.de)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using namespace std;
#include "vtk_file_writer.h"
#include <vtkRectilinearGrid.h>
#include <vtkRectilinearGridWriter.h>
#include <vtkXMLRectilinearGridWriter.h>
#include <vtkStructuredGrid.h>
#include <vtkStructuredGridWriter.h>
#include <vtkXMLStructuredGridWriter.h>
#include <vtkZLibDataCompressor.h>
#include <vtkFloatArray.h>
#include <vtkDoubleArray.h>
#include <vtkFieldData.h>
#include <vtkPointData.h>
#include <sstream>
#include <iomanip>
VTK_File_Writer::VTK_File_Writer(string filename, int meshType)
{
SetFilename(filename);
m_MeshType = meshType;
m_NativeDump = false;
m_Binary = true;
m_Compress = true;
m_AppendMode = false;
m_ActiveTS = false;
if (m_MeshType==0) //cartesian mesh
m_GridData = vtkRectilinearGrid::New();
else if (m_MeshType==1) //cylindrical mesh
m_GridData = vtkStructuredGrid::New();
else
{
cerr << "VTK_File_Writer::VTK_File_Writer: Error, unknown mesh type: " << m_MeshType << endl;
m_GridData=NULL;
}
}
VTK_File_Writer::~VTK_File_Writer()
{
if (m_GridData)
m_GridData->Delete();
m_GridData = NULL;
}
void VTK_File_Writer::SetMeshLines(double const* const* lines, unsigned int const* count, double scaling)
{
if (m_MeshType==0) //cartesian mesh
{
vtkRectilinearGrid* RectGrid = dynamic_cast<vtkRectilinearGrid*>(m_GridData);
if (RectGrid==NULL)
{
cerr << "VTK_File_Writer::SetMeshLines: Error, grid invalid, this should not have happened! " << endl;
exit(1);
}
RectGrid->SetDimensions(count[0],count[1],count[2]);
vtkDoubleArray *Coords[3];
for (int n=0;n<3;++n)
{
m_MeshLines[n].clear();
m_MeshLines[n].reserve(count[n]);
Coords[n] = vtkDoubleArray::New();
for (unsigned int i=0; i<count[n]; i++)
{
Coords[n]->InsertNextValue(lines[n][i]*scaling);
m_MeshLines[n].push_back(lines[n][i]*scaling);
}
}
RectGrid->SetXCoordinates(Coords[0]);
RectGrid->SetYCoordinates(Coords[1]);
RectGrid->SetZCoordinates(Coords[2]);
for (int n=0;n<3;++n)
Coords[n]->Delete();
}
else if (m_MeshType==1) //cylindrical mesh
{
vtkStructuredGrid* StructGrid = dynamic_cast<vtkStructuredGrid*>(m_GridData);
if (StructGrid==NULL)
{
cerr << "VTK_File_Writer::SetMeshLines: Error, grid invalid, this should not have happened! " << endl;
exit(1);
}
for (int n=0;n<3;++n)
{
m_MeshLines[n].clear();
m_MeshLines[n].reserve(count[n]);
double scale=1;
if (n!=1)
scale*=scaling;
for (unsigned int i=0; i<count[n]; i++)
m_MeshLines[n].push_back(lines[n][i]*scale);
}
StructGrid->SetDimensions(count[0],count[1],count[2]);
vtkPoints *points = vtkPoints::New();
points->SetNumberOfPoints(count[0]*count[1]*count[2]);
double r[3];
int id=0;
for (unsigned int k=0; k<count[2]; ++k)
for (unsigned int j=0; j<count[1]; ++j)
for (unsigned int i=0; i<count[0]; ++i)
{
r[0] = lines[0][i] * cos(lines[1][j]) * scaling;
r[1] = lines[0][i] * sin(lines[1][j]) * scaling;
r[2] = lines[2][k] * scaling;
points->SetPoint(id++,r);
}
StructGrid->SetPoints(points);
points->Delete();
}
else
{
cerr << "VTK_File_Writer::SetMeshLines: Error, unknown mesh type: " << m_MeshType << endl;
}
}
void VTK_File_Writer::AddScalarField(string fieldname, double const* const* const* field)
{
vtkDoubleArray* array = vtkDoubleArray::New();
array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size());
array->SetName(fieldname.c_str());
int id=0;
for (unsigned int k=0;k<m_MeshLines[2].size();++k)
{
for (unsigned int j=0;j<m_MeshLines[1].size();++j)
{
for (unsigned int i=0;i<m_MeshLines[0].size();++i)
{
array->SetTuple1(id++,field[i][j][k]);
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
void VTK_File_Writer::AddScalarField(string fieldname, float const* const* const* field)
{
vtkFloatArray* array = vtkFloatArray::New();
array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size());
array->SetName(fieldname.c_str());
int id=0;
for (unsigned int k=0;k<m_MeshLines[2].size();++k)
{
for (unsigned int j=0;j<m_MeshLines[1].size();++j)
{
for (unsigned int i=0;i<m_MeshLines[0].size();++i)
{
array->SetTuple1(id++,field[i][j][k]);
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
void VTK_File_Writer::AddVectorField(string fieldname, double const* const* const* const* field)
{
vtkDoubleArray* array = vtkDoubleArray::New();
array->SetNumberOfComponents(3);
array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size());
array->SetName(fieldname.c_str());
int id=0;
double out[3];
for (unsigned int k=0;k<m_MeshLines[2].size();++k)
{
for (unsigned int j=0;j<m_MeshLines[1].size();++j)
{
double cos_a = cos(m_MeshLines[1].at(j)); //needed only for m_MeshType==1 (cylindrical mesh)
double sin_a = sin(m_MeshLines[1].at(j)); //needed only for m_MeshType==1 (cylindrical mesh)
for (unsigned int i=0;i<m_MeshLines[0].size();++i)
{
if ((m_MeshType==0) || (m_NativeDump))
array->SetTuple3(id++,field[0][i][j][k],field[1][i][j][k],field[2][i][j][k]);
else
{
out[0] = field[0][i][j][k] * cos_a - field[1][i][j][k] * sin_a;
out[1] = field[0][i][j][k] * sin_a + field[1][i][j][k] * cos_a;
out[2] = field[2][i][j][k];
array->SetTuple3(id++,out[0],out[1],out[2]);
}
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
void VTK_File_Writer::AddVectorField(string fieldname, float const* const* const* const* field)
{
vtkFloatArray* array = vtkFloatArray::New();
array->SetNumberOfComponents(3);
array->SetNumberOfTuples(m_MeshLines[0].size()*m_MeshLines[1].size()*m_MeshLines[2].size());
array->SetName(fieldname.c_str());
int id=0;
float out[3];
for (unsigned int k=0;k<m_MeshLines[2].size();++k)
{
for (unsigned int j=0;j<m_MeshLines[1].size();++j)
{
float cos_a = cos(m_MeshLines[1].at(j)); //needed only for m_MeshType==1 (cylindrical mesh)
float sin_a = sin(m_MeshLines[1].at(j)); //needed only for m_MeshType==1 (cylindrical mesh)
for (unsigned int i=0;i<m_MeshLines[0].size();++i)
{
if ((m_MeshType==0) || (m_NativeDump))
array->SetTuple3(id++,field[0][i][j][k],field[1][i][j][k],field[2][i][j][k]);
else
{
out[0] = field[0][i][j][k] * cos_a - field[1][i][j][k] * sin_a;
out[1] = field[0][i][j][k] * sin_a + field[1][i][j][k] * cos_a;
out[2] = field[2][i][j][k];
array->SetTuple3(id++,out[0],out[1],out[2]);
}
}
}
}
m_GridData->GetPointData()->AddArray(array);
array->Delete();
}
int VTK_File_Writer::GetNumberOfFields() const
{
return m_GridData->GetPointData()->GetNumberOfArrays();
}
void VTK_File_Writer::ClearAllFields()
{
while (m_GridData->GetPointData()->GetNumberOfArrays()>0)
{
const char* name = m_GridData->GetPointData()->GetArrayName(0);
m_GridData->GetPointData()->RemoveArray(name);
}
}
bool VTK_File_Writer::Write()
{
return WriteXML();
}
string VTK_File_Writer::GetTimestepFilename(int pad_length) const
{
if (m_ActiveTS==false)
return m_filename;
stringstream ss;
ss << m_filename << "_" << std::setw( pad_length ) << std::setfill( '0' ) << m_timestep;
return ss.str();
}
bool VTK_File_Writer::WriteASCII()
{
vtkDataWriter* writer = NULL;
if (m_MeshType==0) //cartesian mesh
writer = vtkRectilinearGridWriter::New();
else if (m_MeshType==1) //cylindrical mesh
writer = vtkStructuredGridWriter::New();
else
{
cerr << "VTK_File_Writer::WriteASCII: Error, unknown mesh type: " << m_MeshType << endl;
return false;
}
writer->SetHeader(m_header.c_str());
#if VTK_MAJOR_VERSION>=6
writer->SetInputData(m_GridData);
#else
writer->SetInput(m_GridData);
#endif
string filename = GetTimestepFilename() + ".vtk";
writer->SetFileName(filename.c_str());
if (m_Binary)
writer->SetFileTypeToBinary();
else
writer->SetFileTypeToASCII();
writer->Write();
writer->Delete();
return true;
}
bool VTK_File_Writer::WriteXML()
{
vtkXMLStructuredDataWriter* writer = NULL;
if (m_MeshType==0) //cartesian mesh
writer = vtkXMLRectilinearGridWriter::New();
else if (m_MeshType==1) //cylindrical mesh
writer = vtkXMLStructuredGridWriter::New();
else
{
cerr << "VTK_File_Writer::WriteXML: Error, unknown mesh type: " << m_MeshType << endl;
return false;
}
#if VTK_MAJOR_VERSION>=6
writer->SetInputData(m_GridData);
#else
writer->SetInput(m_GridData);
#endif
string filename = GetTimestepFilename() + "." + writer->GetDefaultFileExtension();
writer->SetFileName(filename.c_str());
if (m_Compress)
writer->SetCompressor(vtkZLibDataCompressor::New());
else
writer->SetCompressor(NULL);
if (m_Binary)
writer->SetDataModeToBinary();
else
writer->SetDataModeToAscii();
writer->Write();
writer->Delete();
return true;
}
|