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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkCompositeCellGridReader.h"
#include "vtkCellAttribute.h"
#include "vtkCellGrid.h"
#include "vtkCellGridBoundsQuery.h"
#include "vtkCellGridElevationQuery.h"
#include "vtkCellGridReader.h"
#include "vtkCellGridSidesQuery.h"
#include "vtkCellMetadata.h"
#include "vtkDGBoundsResponder.h"
#include "vtkDGElevationResponder.h"
#include "vtkDGHex.h"
#include "vtkDGSidesResponder.h"
#include "vtkDGTet.h"
#include "vtkDataArray.h"
#include "vtkDataArraySelection.h"
#include "vtkDataSetAttributes.h"
#include "vtkFiltersCellGrid.h"
#include "vtkIOCellGrid.h"
#include "vtkInformation.h"
#include "vtkLogger.h"
#include "vtkObjectFactory.h"
#include "vtkPartitionedDataSet.h"
#include "vtkPartitionedDataSetCollection.h"
#include "vtkStringToken.h"
#include <vtk_nlohmannjson.h>
#include VTK_NLOHMANN_JSON(json.hpp)
#include <array>
#include <sstream>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkCompositeCellGridReader);
vtkCompositeCellGridReader::vtkCompositeCellGridReader()
{
this->SetNumberOfInputPorts(0);
vtkFiltersCellGrid::RegisterCellsAndResponders();
vtkIOCellGrid::RegisterCellsAndResponders();
}
vtkCompositeCellGridReader::~vtkCompositeCellGridReader()
{
this->SetFileName(nullptr);
}
void vtkCompositeCellGridReader::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "FileName: \"" << (this->FileName ? this->FileName : "(none)") << "\"\n";
}
vtkDataArraySelection* vtkCompositeCellGridReader::GetCellTypeSelection()
{
return this->CellTypeSelection;
}
vtkDataArraySelection* vtkCompositeCellGridReader::GetCellAttributeSelection()
{
return this->CellAttributeSelection;
}
vtkMTimeType vtkCompositeCellGridReader::GetMTime()
{
vtkMTimeType result = this->Superclass::GetMTime();
vtkMTimeType ctt = this->CellTypeSelection->GetMTime();
vtkMTimeType cat = this->CellAttributeSelection->GetMTime();
if (result < ctt)
{
result = ctt;
}
if (result < cat)
{
result = cat;
}
return result;
}
int vtkCompositeCellGridReader::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkPartitionedDataSetCollection");
return 1;
}
int vtkCompositeCellGridReader::ReadMetaData(vtkInformation* metadata)
{
vtkLogScopeF(TRACE, "ReadMetaData");
// Read block and array info if needed.
this->UpdateMetadata();
metadata->Set(vtkAlgorithm::CAN_HANDLE_PIECE_REQUEST(), 1);
return 1;
}
int vtkCompositeCellGridReader::ReadMesh(
int piece, int npieces, int nghosts, int timestep, vtkDataObject* output)
{
(void)nghosts;
(void)timestep;
vtkLogScopeF(TRACE, "ReadMesh");
vtkIndent indent;
vtkLog(TRACE,
"ReadMesh " << output << " p " << (piece + 1) << "/" << npieces << " g " << nghosts << " t "
<< timestep);
auto* pdc = vtkPartitionedDataSetCollection::SafeDownCast(output);
std::size_t nfiles = this->Groups.Files.size();
pdc->SetNumberOfPartitionedDataSets(static_cast<unsigned int>(nfiles));
for (std::size_t ii = 0; ii < nfiles / npieces; ++ii)
{
std::size_t ff = ii * npieces + piece;
if (ff > nfiles)
{
break;
}
vtkNew<vtkCellGridReader> partReader;
vtkNew<vtkPartitionedDataSet> part;
partReader->SetFileName(this->Groups.Files[ff].c_str());
partReader->Update();
auto* cellgrid = vtkCellGrid::SafeDownCast(partReader->GetOutputDataObject(0));
if (!cellgrid)
{
continue;
}
// Downselect cell types and attributes to the enabled set.
for (const auto& typeToken : cellgrid->CellTypeArray())
{
if (!this->CellTypeSelection->ArrayIsEnabled(typeToken.Data().c_str()))
{
cellgrid->RemoveCellMetadata(cellgrid->GetCellType(typeToken));
vtkLog(TRACE, " Disabling " << typeToken.Data());
}
}
for (const auto& attributeId : cellgrid->GetCellAttributeIds())
{
auto* attribute = cellgrid->GetCellAttributeById(attributeId);
if (!attribute)
{
continue;
}
if (!this->CellAttributeSelection->ArrayIsEnabled(attribute->GetName().Data().c_str()))
{
vtkLog(TRACE, " Disabling " << attribute->GetName().Data());
cellgrid->RemoveCellAttribute(attribute);
}
}
part->SetNumberOfPartitions(1);
part->SetPartition(0, cellgrid);
pdc->SetPartitionedDataSet(static_cast<unsigned int>(ff), part);
}
return 1;
}
int vtkCompositeCellGridReader::ReadPoints(
int piece, int npieces, int nghosts, int timestep, vtkDataObject* output)
{
(void)piece;
(void)npieces;
(void)nghosts;
(void)timestep;
(void)output;
vtkLogScopeF(TRACE, "ReadPoints");
return 1;
}
int vtkCompositeCellGridReader::ReadArrays(
int piece, int npieces, int nghosts, int timestep, vtkDataObject* output)
{
(void)piece;
(void)npieces;
(void)nghosts;
(void)timestep;
(void)output;
vtkLogScopeF(TRACE, "ReadArrays");
return 1;
}
struct vtkCompositeCellGridReader::MetadataGuard
{
MetadataGuard(vtkCompositeCellGridReader* self)
: Self(self)
{
}
/// When destroyed, copy the buffered changes to the reader.
~MetadataGuard()
{
if (this->Self)
{
bool didUpdate = false;
auto cellTypeMTime = this->Self->GetCellTypeSelection()->GetMTime();
auto cellAttributeMTime = this->Self->GetCellAttributeSelection()->GetMTime();
this->Self->GetCellTypeSelection()->CopySelections(this->CellTypeSelection);
this->Self->GetCellAttributeSelection()->CopySelections(this->CellAttributeSelection);
didUpdate |= (cellTypeMTime < this->Self->GetCellTypeSelection()->GetMTime());
didUpdate |= (cellAttributeMTime < this->Self->GetCellAttributeSelection()->GetMTime());
if (this->Self->Groups.Files != this->Group.Files)
{
this->Self->Groups = this->Group;
didUpdate = true;
}
if (didUpdate)
{
this->Self->MetadataTime.Modified();
}
}
}
vtkCompositeCellGridReader* Self{ nullptr };
vtkCompositeCellGridReader::FileGroup Group;
vtkNew<vtkDataArraySelection> CellTypeSelection;
vtkNew<vtkDataArraySelection> CellAttributeSelection;
};
bool vtkCompositeCellGridReader::UpdateMetadata()
{
if (this->MetadataTime.GetMTime() >= this->GetMTime())
{
return false;
}
MetadataGuard meta(this);
if (!this->FileName)
{ // Make sure we have a file to read.
return false;
}
// All files listed in this->FileName must be absolute or are assumed
// relative to the location of this->FileName. Get the absolute path
// for this->FileName so we can turn relative paths into absolute ones.
std::string err;
auto realFileName = vtksys::SystemTools::GetRealPath(this->FileName, &err);
if (!err.empty())
{
vtkErrorMacro("Could not determine location of \"" << this->FileName << "\".");
return false;
}
std::vector<std::string> pathParts;
vtksys::SystemTools::SplitPath(realFileName, pathParts);
if (pathParts.size() < 2)
{
vtkErrorMacro("Could not determine parent directory of \"" << this->FileName << "\".");
return false;
}
pathParts.pop_back(); // Drop the filename so we are left with the parent directory.
// Check the file's validity.
std::ifstream file(this->FileName);
if (!file.good())
{
vtkErrorMacro("Cannot read file \"" << this->FileName << "\".");
return false;
}
// Read the file into nlohmann json.
nlohmann::json jj;
try
{
jj = nlohmann::json::parse(file);
}
catch (...)
{
vtkErrorMacro("Cannot parse file \"" << this->FileName << "\".");
return false;
}
vtkLogScopeF(TRACE, "UpdateMetadata");
bool isComposite = true;
auto jtype = jj.find("data-type");
if (jtype == jj.end())
{
vtkErrorMacro("Data type is missing.");
return false;
}
auto dtype = jtype->get<std::string>();
isComposite = (dtype == "composite");
if (!isComposite && dtype != "cell-grid")
{
vtkErrorMacro("Data type \"" << dtype << "\" is unsupported.");
return false;
}
// If we are given a "leaf" type file as input, we should read it
// and present a single block with its data as our output.
if (!isComposite)
{
// This is horrible, but we need to find the cell-types and
// cell-attributes inside each file, which means parsing it.
vtkNew<vtkCellGridReader> reader;
reader->SetFileName(this->FileName);
reader->Update();
auto* grid = vtkCellGrid::SafeDownCast(reader->GetOutputDataObject(0));
if (!grid || grid->GetNumberOfCells() == 0)
{
vtkErrorMacro("Unsupported file \"" << this->FileName << "\". Skipping.");
return false;
}
meta.Group.Files.emplace_back(this->FileName);
for (const auto& typeToken : grid->CellTypeArray())
{
const char* cellTypeName = typeToken.Data().c_str();
meta.CellTypeSelection->AddArray(cellTypeName);
if (this->GetCellTypeSelection()->ArrayExists(cellTypeName) &&
!this->GetCellTypeSelection()->ArrayIsEnabled(cellTypeName))
{
meta.CellTypeSelection->DisableArray(cellTypeName);
}
}
for (const auto& attributeId : grid->GetCellAttributeIds())
{
auto* cellAtt = grid->GetCellAttributeById(attributeId);
const char* attName = cellAtt->GetName().Data().c_str();
meta.CellAttributeSelection->AddArray(attName);
vtkLog(TRACE, " Adding " << attName);
if (this->GetCellAttributeSelection()->ArrayExists(attName) &&
!this->GetCellAttributeSelection()->ArrayIsEnabled(attName))
{
vtkLog(TRACE, " Disabling " << attName);
meta.CellAttributeSelection->DisableArray(attName);
}
}
return true;
}
auto jArrayGroup = jj.find("group");
if (jArrayGroup == jj.end() || !jArrayGroup->is_object())
{
vtkErrorMacro("Missing group section.");
return false;
}
auto jGroupType = jArrayGroup->find("group-type");
if (jGroupType == jArrayGroup->end() || jGroupType->get<std::string>() != "collection")
{
vtkErrorMacro("Missing or unsupported group-type inside group specifier.");
return false;
}
auto jFileList = jArrayGroup->find("files");
if (jFileList == jArrayGroup->end() || !jFileList->is_array())
{
vtkErrorMacro("Missing files section in group specification.");
return false;
}
// Add each file's cell types and attributes to "meta" metadata-buffer.
for (const auto& jPath : *jFileList)
{
// meta.Group.Files = jFileList.get<std::vector<std::string>>();
auto path = jPath.get<std::string>();
if (!vtksys::SystemTools::FileIsFullPath(path))
{
std::vector<std::string> location = pathParts;
location.push_back(path);
auto newPath = vtksys::SystemTools::JoinPath(location);
vtkLog(TRACE, " Expanding \"" << path << "\" to \"" << newPath << "\"");
path = newPath;
}
std::ifstream groupData(path.c_str());
if (!groupData.good())
{
vtkErrorMacro("Cannot read file \"" << path << "\".");
return false;
}
// This is horrible, but we need to find the cell-types and
// cell-attributes inside each file, which means parsing it.
vtkNew<vtkCellGridReader> reader;
reader->SetFileName(path.c_str());
reader->Update();
auto* grid = vtkCellGrid::SafeDownCast(reader->GetOutputDataObject(0));
if (!grid || grid->GetNumberOfCells() == 0)
{
vtkErrorMacro("Empty or missing file \"" << path << "\". Skipping.");
continue;
}
meta.Group.Files.push_back(path);
for (const auto& typeToken : grid->CellTypeArray())
{
const char* cellTypeName = typeToken.Data().c_str();
meta.CellTypeSelection->AddArray(cellTypeName);
if (this->GetCellTypeSelection()->ArrayExists(cellTypeName) &&
!this->GetCellTypeSelection()->ArrayIsEnabled(cellTypeName))
{
meta.CellTypeSelection->DisableArray(cellTypeName);
}
}
if (meta.Group.Files.size() == 1)
{
for (const auto& attributeId : grid->GetCellAttributeIds())
{
auto* cellAtt = grid->GetCellAttributeById(attributeId);
const char* attName = cellAtt->GetName().Data().c_str();
meta.CellAttributeSelection->AddArray(attName);
vtkLog(TRACE, " Adding " << attName);
if (this->GetCellAttributeSelection()->ArrayExists(attName) &&
!this->GetCellAttributeSelection()->ArrayIsEnabled(attName))
{
vtkLog(TRACE, " Disabling " << attName);
meta.CellAttributeSelection->DisableArray(attName);
}
}
}
else
{
// Subtract any attributes not present in the current grid.
int nn = meta.CellAttributeSelection->GetNumberOfArrays();
for (int ii = 0; ii < nn; ++ii)
{
const char* arrayName = meta.CellAttributeSelection->GetArrayName(ii);
if (!arrayName)
{
continue;
}
auto* cellAtt = grid->GetCellAttributeByName(arrayName);
// TODO: Add more checks that attributes with the same name
// have the same hash (i.e., same number of components,
// same space, etc.).
if (!cellAtt)
{
vtkLog(TRACE, " Disabling " << arrayName << "; it is not in " << path);
meta.CellAttributeSelection->RemoveArrayByName(arrayName);
// We just removed an array, so change loop variable and count.
--nn;
--ii;
}
else
{
vtkLog(TRACE, " Validated " << arrayName << " is present.");
}
}
}
}
return true;
}
VTK_ABI_NAMESPACE_END
|