36#include <vtkPolyDataReader.h>
38#if VTK_MAJOR_VERSION >= 9
43#include <vtkCellArray.h>
44#include <vtkCellDataToPointData.h>
45#include <vtkContourFilter.h>
46#include <vtkPointData.h>
48#include <vtkPolyData.h>
50#include <boost/program_options.hpp>
51namespace po = boost::program_options;
76 Vertex(
double pX,
double pY,
double pZ,
double pScalar,
double factor)
77 :
x((int)floor(pX * factor)),
y((int)floor(pY * factor)),
78 z((int)floor(pZ * factor)),
scalar(pScalar)
89 return (
x == v.
x &&
y == v.
y &&
z == v.
z);
97 return v1->operator==(*v2);
112typedef std::unordered_set<VertexSharedPtr, VertexHash>
VertexSet;
119int main(
int argc,
char *argv[])
122 po::options_description desc(
"Available options");
126 (
"help,h",
"Produce this help message.")
127 (
"name,n", po::value<string>()->default_value(
"Intensity"),
128 "Name of field in VTK file to use for intensity.")
129 (
"outname,m", po::value<string>()->default_value(
"intensity"),
130 "Name of field in output FLD file.")
131 (
"precision,p", po::value<double>()->default_value(1),
132 "Precision of vertex matching.");
135 po::options_description hidden(
"Hidden options");
139 (
"file", po::value<vector<string> >(),
"Input filename");
142 po::options_description cmdline_options;
143 cmdline_options.add(desc).add(hidden);
145 po::positional_options_description
p;
148 po::variables_map vm;
153 po::store(po::command_line_parser(argc, argv)
154 .options(cmdline_options)
160 catch (
const std::exception &e)
162 cerr << e.what() << endl;
167 if (vm.count(
"help") || vm.count(
"file") == 0 ||
168 vm[
"file"].as<vector<string>>().size() != 3)
170 cerr <<
"Usage: VtkToFld session.xml intensity.vtk output.fld [options]"
177 std::vector<std::string> vFiles = vm[
"file"].as<vector<string>>();
178 const string infile = vFiles[1];
179 const string outfile = vFiles[2];
180 const double factor = vm[
"precision"].as<
double>();
181 const string name = vm[
"name"].as<
string>();
182 const string outname = vm[
"outname"].as<
string>();
184 std::vector<std::string> vFilenames;
189 vFilenames.push_back(vFiles[0]);
190 vSession = LibUtilities::SessionReader::CreateInstance(2, argv, vFilenames);
196 graph2D = SpatialDomains::MeshGraphIO::Read(vSession);
207 int coordim = Exp->GetCoordim(0);
208 int nq = Exp->GetNpoints();
217 Exp->GetCoords(xc0, xc1);
220 Exp->GetCoords(xc0, xc1, xc2);
223 ASSERTL0(
false,
"Coordim not valid");
228 vtkPolyDataReader *vtkMeshReader = vtkPolyDataReader::New();
229 vtkMeshReader->SetFileName(infile.c_str());
230 vtkMeshReader->Update();
232 vtkPolyData *vtkMesh = vtkMeshReader->GetOutput();
233 vtkCellDataToPointData *c2p = vtkCellDataToPointData::New();
234#if VTK_MAJOR_VERSION <= 5
235 c2p->SetInput(vtkMesh);
237 c2p->SetInputData(vtkMesh);
239 c2p->PassCellDataOn();
241 vtkPolyData *vtkDataAtPoints = c2p->GetPolyDataOutput();
243 vtkPoints *vtkPoints = vtkMesh->GetPoints();
244 ASSERTL0(vtkPoints,
"ERROR: cannot get points from mesh.");
246 vtkCellArray *vtkPolys = vtkMesh->GetPolys();
247 ASSERTL0(vtkPolys,
"ERROR: cannot get polygons from mesh.");
249 vtkPointData *vtkPData = vtkDataAtPoints->GetPointData();
250 ASSERTL0(vtkPolys,
"ERROR: cannot get point data from file.");
253 VertexSet::iterator vIter;
260 if (!vtkDataAtPoints->GetPointData()->HasArray(
name.c_str()))
262 n = vtkDataAtPoints->GetPointData()->GetNumberOfArrays();
263 cerr <<
"Input file '" << infile
264 <<
"' does not have a field named '" <<
name <<
"'" << endl;
265 cerr <<
"There are " << n <<
" arrays in this file." << endl;
266 for (
int i = 0; i < n; ++i)
269 << vtkDataAtPoints->GetPointData()->GetArray(i)->GetName()
278 n = vtkPoints->GetNumberOfPoints();
279 for (i = 0; i < n; ++i)
281 vtkPoints->GetPoint(i,
p);
282 val = vtkPData->GetScalars(
name.c_str())->GetTuple1(i);
283 std::shared_ptr<Vertex> v(
284 new Vertex(
p[0],
p[1],
p[2], val, factor));
290 for (i = 0; i < Exp->GetNumElmts(); ++i)
293 for (j = 0; j < e->GetNverts(); ++j)
296 coeff_idx = Exp->GetCoeff_Offset(i) + e->GetVertexMap(j);
305 std::shared_ptr<Vertex> v(
new Vertex(x, y,
z, 0.0, factor));
306 vIter = points.find(v);
311 if (vIter == points.end())
313 cerr <<
"Vertex " << i <<
" not found. Looking for (" << x
314 <<
", " << y <<
", " <<
z <<
")" << endl;
318 Exp->UpdateCoeffs()[coeff_idx] = (*vIter)->scalar;
322 Exp->SetPhysState(
false);
326 std::vector<LibUtilities::FieldDefinitionsSharedPtr> FieldDef =
327 Exp->GetFieldDefinitions();
328 std::vector<std::vector<NekDouble>> FieldData(FieldDef.size());
330 for (i = 0; i < FieldDef.size(); ++i)
332 FieldDef[i]->m_fields.push_back(outname);
333 Exp->AppendFieldData(FieldDef[i], FieldData[i]);
337 LibUtilities::FieldIO::CreateDefault(vSession);
338 vFld->Write(outfile, FieldDef, FieldData);
343 cout <<
"An error occurred." << endl;
#define ASSERTL0(condition, msg)
int main(int argc, char *argv[])
bool operator==(const VertexSharedPtr &v1, const VertexSharedPtr &v2)
Define comparison operator for the vertex struct.
std::unordered_set< VertexSharedPtr, VertexHash > VertexSet
Define a set of Vertices with associated hash function.
std::shared_ptr< Vertex > VertexSharedPtr
General purpose memory allocation routines with the ability to allocate from thread specific memory p...
void GetCoords(Array< OneD, NekDouble > &coords_1, Array< OneD, NekDouble > &coords_2=NullNekDouble1DArray, Array< OneD, NekDouble > &coords_3=NullNekDouble1DArray)
this function returns the physical coordinates of the quadrature points of the expansion
std::shared_ptr< FieldIO > FieldIOSharedPtr
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
std::shared_ptr< MeshGraph > MeshGraphSharedPtr
std::shared_ptr< PointGeom > PointGeomSharedPtr
std::shared_ptr< StdExpansion > StdExpansionSharedPtr
std::vector< double > z(NPUPPER)
void hash_combine(std::size_t &seed)
Hash function for the Vertex struct used for defining sets.
std::size_t operator()(VertexSharedPtr const &p) const
Represents a vertex in the mesh.
Vertex(double pX, double pY, double pZ, double pScalar, double factor)
bool operator==(const Vertex &v)