/*============================================================================= NOTE: THIS FILE IS A HANDMADE WRAPPER FOR THE ManagedITK PROJECT. Project: ManagedITK Program: Insight Segmentation & Registration Toolkit Module: itkManagedMatrix.cxx Language: C++/CLI Author: Dan Mueller Date: $Date: 2007-09-01 06:17:25 +1000 (Sat, 01 Sep 2007) $ Revision: $Revision: 2 $ 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 __itkManagedMatrix_cxx #define __itkManagedMatrix_cxx // Include some useful ManagedITK files #include "itkManagedPoint.cxx" #include "itkManagedVector.cxx" // Use some managed namespaces #using #using using namespace System; using namespace System::IO; using namespace System::Reflection; using namespace System::Diagnostics; using namespace System::Collections::Generic; namespace itk { #define itkMatrixValueType System::Double /// ///This class is a managed replacement for itk::Matrix. /// public ref class itkMatrix { private: unsigned int m_NumberOfRows; unsigned int m_NumberOfCols; array^ m_Data; public: /// ///Constructor taking the number of rows and columns. ///The matrix is initialised to all zeros. /// ///The number of rows in the matrix. ///The number of columns in the matrix. itkMatrix ( unsigned int numrows, unsigned int numcols ) { this->m_NumberOfRows = numrows; this->m_NumberOfCols = numcols; this->m_Data = gcnew array( numrows*numcols ); this->Fill( 0.0 ); } /// ///Static constructor creating a new matrix set to the identity. /// ///The number of rows in the matrix. ///The number of columns in the matrix. static itkMatrix^ NewIdentity ( unsigned int numrows, unsigned int numcols ) { itkMatrix^ result = gcnew itkMatrix( numrows, numcols ); result->SetIdentity( ); return result; } /// ///Static constructor creating a new matrix set to all zeros. /// ///The number of rows in the matrix. ///The number of columns in the matrix. static itkMatrix^ NewZeros ( unsigned int numrows, unsigned int numcols ) { itkMatrix^ result = gcnew itkMatrix( numrows, numcols ); return result; } ///Get the number of rows in the Matrix. property unsigned int NumberOfRows { unsigned int get() { return this->m_NumberOfRows; } } ///Get the number of columns in the Matrix. property unsigned int NumberOfCols { unsigned int get() { return this->m_NumberOfCols; } } ///Get the underlying data array. property array^ Data { array^ get() { return this->m_Data; } } ///Get/set the value at the given [row, col]. property itkMatrixValueType default[unsigned int, unsigned int] { itkMatrixValueType get(unsigned int row, unsigned int col) { return this->m_Data[row*m_NumberOfRows + col]; } void set(unsigned int row, unsigned int col, itkMatrixValueType value) { this->m_Data[row*m_NumberOfRows + col] = value; } } ///Get the specified matrix component. ///The matrix row. ///The matrix column. ///A reference to the matrix component at [row, col]. virtual itkMatrixValueType Get( unsigned int row, unsigned int col ) { return this[row, col]; } ///Set the specified matrix component. ///The matrix row. ///The matrix column. ///The value for [row, col]. virtual void Set( unsigned int row, unsigned int col, itkMatrixValueType value ) { this[row, col] = value; } ///Set the matrix to identity. virtual void SetIdentity( ) { this->Fill(0.0); for (unsigned int i = 0; iFill the matrix with a value. virtual void Fill( itkMatrixValueType value ) { for (unsigned int i = 0; im_Data[i] = value; } ///Performs matrix multiplication. /// /// ///The resultant itkMatrix from multiplying lhs with rhs. static itkMatrix^ operator*(itkMatrix^ lhs, itkMatrix^ rhs) { if ( lhs == nullptr || rhs == nullptr ) throw gcnew ArgumentNullException("Can not operate on nullptr objects."); else if ( lhs->NumberOfRows != rhs->NumberOfRows ) throw gcnew ArgumentException("Number of rows for left and right hand sides do not match."); else if ( lhs->NumberOfCols != rhs->NumberOfCols ) throw gcnew ArgumentException("Number of columns for left and right hand sides do not match."); else { itkMatrix^ result = gcnew itkMatrix( lhs->NumberOfRows, lhs->NumberOfCols ); for (unsigned i = 0; i < lhs->NumberOfRows; i++) { // For each row for (unsigned j = 0; j < rhs->NumberOfCols; j++) { // For each column itkMatrixValueType sum = 0; for (unsigned k = 0; k < lhs->NumberOfCols; k++) // For each column sum += (lhs[i,k] * rhs[k,j]); result[i,j] = sum; } } return result; } } ///Product by a vector. /// /// ///The itkVector after multiplying vector by matrix. static itkVector^ operator*(itkVector^ vector, itkMatrix^ matrix) { if ( vector == nullptr || matrix == nullptr ) throw gcnew ArgumentNullException("Can not operate on nullptr objects."); else if ( vector->Dimension != matrix->NumberOfRows ) throw gcnew ArgumentException("Incompatiable lengths."); else { itkVector^ result = gcnew itkVector( vector->Dimension ); for( unsigned int r=0; rNumberOfRows; r++) { itkMatrixValueType sum = 0.0; for( unsigned int c=0; cNumberOfCols; c++ ) { sum += matrix[r,c] * vector[c]; } result[r] = sum; } return result; } } ///Product by a point. /// /// ///The itkPoint after multiplying point by matrix. static itkPoint^ operator*(itkPoint^ point, itkMatrix^ matrix) { if ( point == nullptr || matrix == nullptr ) throw gcnew ArgumentNullException("Can not operate on nullptr objects."); else if ( point->Dimension != matrix->NumberOfRows ) throw gcnew ArgumentException("Incompatiable lengths."); else { itkPoint^ result = gcnew itkPoint( point->Dimension ); for( unsigned int r=0; rNumberOfRows; r++) { itkMatrixValueType sum = 0.0; for( unsigned int c=0; cNumberOfCols; c++ ) { sum += matrix[r,c] * point[c]; } result[r] = sum; } return result; } } /// ///Converts the Matrix to a string representation. /// ///A string representation of the matrix. virtual String^ ToString() override { String^ result = String::Empty; for (unsigned int i = 0; im_Data[i].ToString() + " "; return result->Trim(' '); } }; // end ref class } // end namespace itk #endif