/*========================================================================= Program: Visualization Toolkit Module: vtkDenseArray.txx ------------------------------------------------------------------------- Copyright 2008 Sandia Corporation. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain rights in this software. ------------------------------------------------------------------------- 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. =========================================================================*/ #ifndef __vtkDenseArray_txx #define __vtkDenseArray_txx /////////////////////////////////////////////////////////////////////////////// // vtkDenseArray::MemoryBlock template vtkDenseArray::MemoryBlock::~MemoryBlock() { } /////////////////////////////////////////////////////////////////////////////// // vtkDenseArray::HeapMemoryBlock template vtkDenseArray::HeapMemoryBlock::HeapMemoryBlock(const vtkArrayExtents& extents) : Storage(new T[extents.GetSize()]) { } template vtkDenseArray::HeapMemoryBlock::~HeapMemoryBlock() { delete[] this->Storage; } template T* vtkDenseArray::HeapMemoryBlock::GetAddress() { return this->Storage; } /////////////////////////////////////////////////////////////////////////////// // vtkDenseArray::StaticMemoryBlock template vtkDenseArray::StaticMemoryBlock::StaticMemoryBlock(T* const storage) : Storage(storage) { } template T* vtkDenseArray::StaticMemoryBlock::GetAddress() { return this->Storage; } /////////////////////////////////////////////////////////////////////////////// // vtkDenseArray template vtkDenseArray* vtkDenseArray::New() { vtkObject* ret = vtkObjectFactory::CreateInstance(typeid(ThisT).name()); if(ret) { return static_cast(ret); } return new ThisT(); } template void vtkDenseArray::PrintSelf(ostream& os, vtkIndent indent) { vtkDenseArray::Superclass::PrintSelf(os, indent); } template bool vtkDenseArray::IsDense() { return true; } template const vtkArrayExtents& vtkDenseArray::GetExtents() { return this->Extents; } template typename vtkDenseArray::SizeT vtkDenseArray::GetNonNullSize() { return this->Extents.GetSize(); } template void vtkDenseArray::GetCoordinatesN(const SizeT n, vtkArrayCoordinates& coordinates) { coordinates.SetDimensions(this->GetDimensions()); vtkIdType divisor = 1; for(DimensionT i = 0; i < this->GetDimensions(); ++i) { coordinates[i] = ((n / divisor) % this->Extents[i].GetSize()) + this->Extents[i].GetBegin(); divisor *= this->Extents[i].GetSize(); } } template vtkArray* vtkDenseArray::DeepCopy() { vtkDenseArray* const copy = vtkDenseArray::New(); copy->SetName(this->GetName()); copy->Resize(this->Extents); copy->DimensionLabels = this->DimensionLabels; std::copy(this->Begin, this->End, copy->Begin); return copy; } template const T& vtkDenseArray::GetValue(CoordinateT i) { if(1 != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); static T temp; return temp; } return this->Begin[this->MapCoordinates(i)]; } template const T& vtkDenseArray::GetValue(CoordinateT i, CoordinateT j) { if(2 != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); static T temp; return temp; } return this->Begin[this->MapCoordinates(i, j)]; } template const T& vtkDenseArray::GetValue(CoordinateT i, CoordinateT j, CoordinateT k) { if(3 != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); static T temp; return temp; } return this->Begin[this->MapCoordinates(i, j, k)]; } template const T& vtkDenseArray::GetValue(const vtkArrayCoordinates& coordinates) { if(coordinates.GetDimensions() != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); static T temp; return temp; } return this->Begin[this->MapCoordinates(coordinates)]; } template const T& vtkDenseArray::GetValueN(const SizeT n) { return this->Begin[n]; } template void vtkDenseArray::SetValue(CoordinateT i, const T& value) { if(1 != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); return; } this->Begin[this->MapCoordinates(i)] = value; } template void vtkDenseArray::SetValue(CoordinateT i, CoordinateT j, const T& value) { if(2 != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); return; } this->Begin[this->MapCoordinates(i, j)] = value; } template void vtkDenseArray::SetValue(CoordinateT i, CoordinateT j, CoordinateT k, const T& value) { if(3 != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); return; } this->Begin[this->MapCoordinates(i, j, k)] = value; } template void vtkDenseArray::SetValue(const vtkArrayCoordinates& coordinates, const T& value) { if(coordinates.GetDimensions() != this->GetDimensions()) { vtkErrorMacro(<< "Index-array dimension mismatch."); return; } this->Begin[this->MapCoordinates(coordinates)] = value; } template void vtkDenseArray::SetValueN(const SizeT n, const T& value) { this->Begin[n] = value; } template void vtkDenseArray::ExternalStorage(const vtkArrayExtents& extents, MemoryBlock* storage) { this->Reconfigure(extents, storage); } template void vtkDenseArray::Fill(const T& value) { std::fill(this->Begin, this->End, value); } template T& vtkDenseArray::operator[](const vtkArrayCoordinates& coordinates) { if(coordinates.GetDimensions() != this->GetDimensions()) { static T temp; vtkErrorMacro(<< "Index-array dimension mismatch."); return temp; } return this->Begin[this->MapCoordinates(coordinates)]; } template const T* vtkDenseArray::GetStorage() const { return this->Begin; } template T* vtkDenseArray::GetStorage() { return this->Begin; } template vtkDenseArray::vtkDenseArray() : Storage(0), Begin(0), End(0) { } template vtkDenseArray::~vtkDenseArray() { delete this->Storage; this->Storage = 0; this->Begin = 0; this->End = 0; } template void vtkDenseArray::InternalResize(const vtkArrayExtents& extents) { this->Reconfigure(extents, new HeapMemoryBlock(extents)); } template void vtkDenseArray::InternalSetDimensionLabel(DimensionT i, const vtkStdString& label) { this->DimensionLabels[i] = label; } template vtkStdString vtkDenseArray::InternalGetDimensionLabel(DimensionT i) { return this->DimensionLabels[i]; } template vtkIdType vtkDenseArray::MapCoordinates(CoordinateT i) { return ((i + this->Offsets[0]) * this->Strides[0]); } template vtkIdType vtkDenseArray::MapCoordinates(CoordinateT i, CoordinateT j) { return ((i + this->Offsets[0]) * this->Strides[0]) + ((j + this->Offsets[1]) * this->Strides[1]); } template vtkIdType vtkDenseArray::MapCoordinates(CoordinateT i, CoordinateT j, CoordinateT k) { return ((i + this->Offsets[0]) * this->Strides[0]) + ((j + this->Offsets[1]) * this->Strides[1]) + ((k + this->Offsets[2]) * this->Strides[2]); } template vtkIdType vtkDenseArray::MapCoordinates(const vtkArrayCoordinates& coordinates) { vtkIdType index = 0; for(vtkIdType i = 0; i != static_cast(this->Strides.size()); ++i) index += ((coordinates[i] + this->Offsets[i]) * this->Strides[i]); return index; } template void vtkDenseArray::Reconfigure(const vtkArrayExtents& extents, MemoryBlock* storage) { this->Extents = extents; this->DimensionLabels.resize(extents.GetDimensions(), vtkStdString()); delete this->Storage; this->Storage = storage; this->Begin = storage->GetAddress(); this->End = this->Begin + extents.GetSize(); this->Offsets.resize(extents.GetDimensions()); for(DimensionT i = 0; i != extents.GetDimensions(); ++i) { this->Offsets[i] = -extents[i].GetBegin(); } this->Strides.resize(extents.GetDimensions()); for(DimensionT i = 0; i != extents.GetDimensions(); ++i) { if(i == 0) this->Strides[i] = 1; else this->Strides[i] = this->Strides[i-1] * extents[i-1].GetSize(); } } #endif