/*========================================================================= Program: Visualization Toolkit Module: vtkCPExodusIIResultsArrayTemplate.txx Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen All rights reserved. See Copyright.txt or http://www.kitware.com/Copyright.htm for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notice for more information. =========================================================================*/ #include "vtkCPExodusIIResultsArrayTemplate.h" #include "vtkIdList.h" #include "vtkObjectFactory.h" #include "vtkVariant.h" #include "vtkVariantCast.h" //------------------------------------------------------------------------------ // Can't use vtkStandardNewMacro on a templated class. template vtkCPExodusIIResultsArrayTemplate* vtkCPExodusIIResultsArrayTemplate::New() { VTK_STANDARD_NEW_BODY(vtkCPExodusIIResultsArrayTemplate); } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::PrintSelf(ostream& os, vtkIndent indent) { this->vtkCPExodusIIResultsArrayTemplate::Superclass::PrintSelf(os, indent); os << indent << "Number of arrays: " << this->Arrays.size() << "\n"; vtkIndent deeper = indent.GetNextIndent(); for (size_t i = 0; i < this->Arrays.size(); ++i) { os << deeper << "Array " << i << ": " << this->Arrays.at(i) << "\n"; } os << indent << "TempDoubleArray: " << this->TempDoubleArray << "\n"; os << indent << "Save: " << this->Save << "\n"; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetExodusScalarArrays( std::vector arrays, vtkIdType numTuples) { this->Initialize(); this->NumberOfComponents = static_cast(arrays.size()); this->Arrays = arrays; this->Size = this->NumberOfComponents * numTuples; this->MaxId = this->Size - 1; this->TempDoubleArray = new double[this->NumberOfComponents]; this->Modified(); } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetExodusScalarArrays( std::vector arrays, vtkIdType numTuples, bool save) { this->SetExodusScalarArrays(arrays, numTuples); this->Save = save; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::Initialize() { if (!this->Save) { for (size_t i = 0; i < this->Arrays.size(); ++i) { delete this->Arrays[i]; } } this->Arrays.clear(); this->Arrays.push_back(nullptr); delete[] this->TempDoubleArray; this->TempDoubleArray = nullptr; this->MaxId = -1; this->Size = 0; this->NumberOfComponents = 1; // the default is to have this class delete the arrays when done with them. this->Save = false; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::GetTuples( vtkIdList* ptIds, vtkAbstractArray* output) { vtkDataArray* da = vtkDataArray::FastDownCast(output); if (!da) { vtkWarningMacro(<< "Input is not a vtkDataArray"); return; } if (da->GetNumberOfComponents() != this->GetNumberOfComponents()) { vtkWarningMacro(<< "Incorrect number of components in input array."); return; } const vtkIdType numPoints = ptIds->GetNumberOfIds(); for (vtkIdType i = 0; i < numPoints; ++i) { da->SetTuple(i, this->GetTuple(ptIds->GetId(i))); } } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::GetTuples( vtkIdType p1, vtkIdType p2, vtkAbstractArray* output) { vtkDataArray* da = vtkDataArray::FastDownCast(output); if (!da) { vtkErrorMacro(<< "Input is not a vtkDataArray"); return; } if (da->GetNumberOfComponents() != this->GetNumberOfComponents()) { vtkErrorMacro(<< "Incorrect number of components in input array."); return; } for (vtkIdType daTupleId = 0; p1 <= p2; ++p1) { da->SetTuple(daTupleId++, this->GetTuple(p1)); } } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::Squeeze() { // noop } //------------------------------------------------------------------------------ template vtkArrayIterator* vtkCPExodusIIResultsArrayTemplate::NewIterator() { vtkErrorMacro(<< "Not implemented."); return nullptr; } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::LookupValue(vtkVariant value) { bool valid = true; Scalar val = vtkVariantCast(value, &valid); if (valid) { return this->Lookup(val, 0); } return -1; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::LookupValue(vtkVariant value, vtkIdList* ids) { bool valid = true; Scalar val = vtkVariantCast(value, &valid); ids->Reset(); if (valid) { vtkIdType index = 0; while ((index = this->Lookup(val, index)) >= 0) { ids->InsertNextId(index); ++index; } } } //------------------------------------------------------------------------------ template vtkVariant vtkCPExodusIIResultsArrayTemplate::GetVariantValue(vtkIdType idx) { return vtkVariant(this->GetValueReference(idx)); } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::ClearLookup() { // no-op, no fast lookup implemented. } //------------------------------------------------------------------------------ template double* vtkCPExodusIIResultsArrayTemplate::GetTuple(vtkIdType i) { this->GetTuple(i, this->TempDoubleArray); return this->TempDoubleArray; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::GetTuple(vtkIdType i, double* tuple) { for (size_t comp = 0; comp < this->Arrays.size(); ++comp) { tuple[comp] = static_cast(this->Arrays[comp][i]); } } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::LookupTypedValue(Scalar value) { return this->Lookup(value, 0); } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::LookupTypedValue(Scalar value, vtkIdList* ids) { ids->Reset(); vtkIdType index = 0; while ((index = this->Lookup(value, index)) >= 0) { ids->InsertNextId(index); ++index; } } //------------------------------------------------------------------------------ template typename vtkCPExodusIIResultsArrayTemplate::ValueType vtkCPExodusIIResultsArrayTemplate::GetValue(vtkIdType idx) const { return const_cast*>(this)->GetValueReference(idx); } //------------------------------------------------------------------------------ template typename vtkCPExodusIIResultsArrayTemplate::ValueType& vtkCPExodusIIResultsArrayTemplate::GetValueReference(vtkIdType idx) { const vtkIdType tuple = idx / this->NumberOfComponents; const vtkIdType comp = idx % this->NumberOfComponents; return this->Arrays[comp][tuple]; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::GetTypedTuple( vtkIdType tupleId, Scalar* tuple) const { for (size_t comp = 0; comp < this->Arrays.size(); ++comp) { tuple[comp] = this->Arrays[comp][tupleId]; } } //------------------------------------------------------------------------------ template vtkTypeBool vtkCPExodusIIResultsArrayTemplate::Allocate(vtkIdType, vtkIdType) { vtkErrorMacro("Read only container."); return 0; } //------------------------------------------------------------------------------ template vtkTypeBool vtkCPExodusIIResultsArrayTemplate::Resize(vtkIdType) { vtkErrorMacro("Read only container."); return 0; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetNumberOfTuples(vtkIdType) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetTuple(vtkIdType, vtkIdType, vtkAbstractArray*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetTuple(vtkIdType, const float*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetTuple(vtkIdType, const double*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertTuple(vtkIdType, vtkIdType, vtkAbstractArray*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertTuple(vtkIdType, const float*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertTuple(vtkIdType, const double*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertTuples( vtkIdList*, vtkIdList*, vtkAbstractArray*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertTuples( vtkIdType, vtkIdType, vtkIdType, vtkAbstractArray*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::InsertNextTuple(vtkIdType, vtkAbstractArray*) { vtkErrorMacro("Read only container."); return -1; } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::InsertNextTuple(const float*) { vtkErrorMacro("Read only container."); return -1; } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::InsertNextTuple(const double*) { vtkErrorMacro("Read only container."); return -1; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::DeepCopy(vtkAbstractArray*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::DeepCopy(vtkDataArray*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InterpolateTuple( vtkIdType, vtkIdList*, vtkAbstractArray*, double*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InterpolateTuple( vtkIdType, vtkIdType, vtkAbstractArray*, vtkIdType, vtkAbstractArray*, double) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetVariantValue(vtkIdType, vtkVariant) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertVariantValue(vtkIdType, vtkVariant) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::RemoveTuple(vtkIdType) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::RemoveFirstTuple() { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::RemoveLastTuple() { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetTypedTuple(vtkIdType, const Scalar*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertTypedTuple(vtkIdType, const Scalar*) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::InsertNextTypedTuple(const Scalar*) { vtkErrorMacro("Read only container."); return -1; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::SetValue(vtkIdType, Scalar) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::InsertNextValue(Scalar) { vtkErrorMacro("Read only container."); return -1; } //------------------------------------------------------------------------------ template void vtkCPExodusIIResultsArrayTemplate::InsertValue(vtkIdType, Scalar) { vtkErrorMacro("Read only container."); return; } //------------------------------------------------------------------------------ template vtkCPExodusIIResultsArrayTemplate::vtkCPExodusIIResultsArrayTemplate() : TempDoubleArray(nullptr) , Save(false) { } //------------------------------------------------------------------------------ template vtkCPExodusIIResultsArrayTemplate::~vtkCPExodusIIResultsArrayTemplate() { typedef typename std::vector::const_iterator ArrayIterator; if (!this->Save) { for (ArrayIterator it = this->Arrays.begin(), itEnd = this->Arrays.end(); it != itEnd; ++it) { delete[] * it; } } delete[] this->TempDoubleArray; } //------------------------------------------------------------------------------ template vtkIdType vtkCPExodusIIResultsArrayTemplate::Lookup(const Scalar& val, vtkIdType index) { while (index <= this->MaxId) { if (this->GetValueReference(index++) == val) { return index; } } return -1; }