/*============================================================================= NOTE: THIS FILE IS A HANDMADE WRAPPER FOR THE ManagedITK PROJECT. Project: ManagedITK Program: Insight Segmentation & Registration Toolkit Module: itkManagedArray.cxx Language: C++/CLI Author: Dan Mueller $Date: 2007-04-11 16:26:16 +1000 (Wed, 11 Apr 2007) $ $Revision: 128 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. Portions of this code are covered under the ITK and VTK copyright. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.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 notices for more information. =============================================================================*/ #pragma once #pragma warning( disable : 4635 ) // Disable warnings about XML doc comments #ifndef __itkManagedArray_cxx #define __itkManagedArray_cxx // Use some managed namespaces #using #using using namespace System; using namespace System::IO; using namespace System::Reflection; using namespace System::ComponentModel; using namespace System::Diagnostics; namespace itk { /// ///The base array class from with all ManagedITK arrays are derived. /// /// ///This class is templated (generic) over the array data value type (TValueType). /// generic where TValueType : System::IComparable, System::IConvertible public ref class itkArray { private: array^ m_Data; public: ///Constructor taking the data values as a params array. ///The variable length data array. The dimension will be initialised as data.Length. The data is cloned. itkArray ( ... array^ data ) { this->m_Data = (array^)data->Clone(); } ///Constructor taking the number of dimensions for the array. ///The data values are initialised as zero. ///The dimensionality of the array to initialise. itkArray( unsigned int dimension ) { this->m_Data = gcnew array(dimension); } ///Constructor taking another array. ///Another array to clone the data from. itkArray ( itkArray^ other) { this->m_Data = (array^)other->Data->Clone(); } ///Get/set the data values via the given index. property typename TValueType default[int] { typename TValueType get(int index) { return this->m_Data[index]; } void set(int index, typename TValueType value) { this->m_Data[index] = value; } } ///Get the underlying data array. property array^ Data { array^ get() { return this->m_Data; } } ///Get the number of dimensions or length of the data array. property unsigned int Dimension { unsigned int get() { return this->m_Data->Length; } } ///Get the number of dimensions or length of the data array. ///This is another way to access Dimension. property unsigned int Length { unsigned int get() { return this->Dimension; } } ///Assign the given value to each dimension in the array. ///The data value to assign to each element of the array. void Fill(typename TValueType value) { for (unsigned int i = 0; i < this->Dimension; i++) this[i] = value; } /// ///Converts the array to a string representation in the following format: /// Real types: "[0.00 1.00 2.00]" /// Integer types: "[1 12 123]" /// ///A string representation of the array. virtual String^ ToString() override { Type^ type = TValueType::typeid; if ( type == Single::typeid || type == Double::typeid || type == Decimal::typeid ) { // Force 2 decimal places for real types return this->ToString("000.00"); } else { // Use the default conversion for all other types return this->ToString(""); } } ///Converts the array to a string represenation using the given format. ///The number formatting string. Eg. "00.00", "#00.0", etc. ///A string representation of the array using the given format. virtual String^ ToString(String^ format) { // Add starting bracket String^ result = "["; // Construct the array string for (unsigned int i = 0; i < this->Dimension; i++) { Type^ type = TValueType::typeid; if ( type == Byte::typeid ) result += Convert::ToByte(this->Data[i]).ToString(format); else if ( type == SByte::typeid ) result += Convert::ToSByte(this->Data[i]).ToString(format); else if ( type == Int16::typeid ) result += Convert::ToInt16(this->Data[i]).ToString(format); else if ( type == UInt16::typeid ) result += Convert::ToUInt16(this->Data[i]).ToString(format); else if ( type == Int32::typeid ) result += Convert::ToInt32(this->Data[i]).ToString(format); else if ( type == UInt32::typeid ) result += Convert::ToUInt32(this->Data[i]).ToString(format); else if ( type == Int64::typeid ) result += Convert::ToInt64(this->Data[i]).ToString(format); else if ( type == UInt64::typeid ) result += Convert::ToUInt64(this->Data[i]).ToString(format); else if ( type == Single::typeid ) result += Convert::ToSingle(this->Data[i]).ToString(format); else if ( type == Double::typeid ) result += Convert::ToDouble(this->Data[i]).ToString(format); else if ( type == Decimal::typeid ) result += Convert::ToDecimal(this->Data[i]).ToString(format); else // Use default ToString and ignore format result += this->Data[i]->System::Object::ToString(); // Add separator result += ", "; } // Trim the trailing separator chars result = result->TrimEnd(',', ' '); // Add trailing bracket result += "]"; return result; } ///Serves as a hash function for a particular type. ///A hashcode computed from the Data array. virtual int GetHashCode() override { return this->m_Data->GetHashCode(); } }; // end ref class } // end namespace itk #endif