Skip to content

Commit

Permalink
ENH: Add explicit Matrix(const T (&)[VRows][VColumns]) constructor
Browse files Browse the repository at this point in the history
Allows constructing an `itk::Matrix` by passing a C-style array of
arrays, of the corresponding value type and dimensions.

With help from Lee Newberg.
  • Loading branch information
N-Dekker authored and dzenanz committed Mar 23, 2022
1 parent cb436b8 commit 7dd9e49
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Modules/Core/Common/include/itkMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "vnl/vnl_matrix.h"
#include "vnl/algo/vnl_determinant.h"
#include "itkMath.h"
#include <type_traits> // For is_same.

namespace itk
{
Expand Down Expand Up @@ -224,6 +225,18 @@ class ITK_TEMPLATE_EXPORT Matrix
: m_Matrix(matrix)
{}

/** Explicit constructor template. Copies the elements from the specified C-style array of rows.
* \note It might have been clearer to just declare a `Matrix(const T (&)[VRows][VColumns])` constructor, but SWIG did
* not like that, saying: "Wrapping/Typedefs/itkMatrix.i:76: Error: Syntax error in input(3)."
*/
template <typename TElement>
explicit Matrix(const TElement (&elements)[VRows][VColumns])
: m_Matrix(&elements[0][0])
{
static_assert(std::is_same<TElement, T>::value,
"The type of an element should correspond with this itk::Matrix instantiation.");
}

/** Comparison operators. */
inline bool
operator==(const Self & matrix) const
Expand Down
35 changes: 35 additions & 0 deletions Modules/Core/Common/test/itkMatrixGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// First include the header file to be tested:
#include "itkMatrix.h"
#include <gtest/gtest.h>
#include <numeric> // For iota.
#include <type_traits> // For is_convertible and is_trivially_copyable.


Expand Down Expand Up @@ -71,6 +72,33 @@ vnl_matrix_fixed_is_convertible_to_itk_Matrix()
TMatrix>();
}


template <typename TMatrix>
void
Expect_Matrix_is_constructible_from_raw_array_of_arrays()
{
using ValueType = typename TMatrix::ValueType;

constexpr auto numberOfRows = TMatrix::RowDimensions;
constexpr auto numberOfColumns = TMatrix::ColumnDimensions;
constexpr auto numberOfElements = numberOfRows * numberOfColumns;
ValueType rawArray[numberOfRows][numberOfColumns];
ValueType * const beginOfRawArray = rawArray[0];

// Just ensure that each element of the raw array has a different value.
std::iota(beginOfRawArray, beginOfRawArray + numberOfElements, ValueType{ 1 });

// Construct an itk::Matrix from a raw C-style array-of-arrays.
const TMatrix matrix(rawArray);

for (unsigned int row = 0; row < numberOfRows; ++row)
{
for (unsigned int column = 0; column < numberOfColumns; ++column)
{
EXPECT_EQ(matrix(row, column), rawArray[row][column]);
}
}
}
} // namespace


Expand Down Expand Up @@ -108,3 +136,10 @@ TEST(Matrix, GetIdentity)
Expect_GetIdentity_returns_identity_matrix<itk::Matrix<double>>();
Expect_GetIdentity_returns_identity_matrix<itk::Matrix<double, 2, 2>>();
}


TEST(Matrix, IsConstructibleFromRawArrayOfArrays)
{
Expect_Matrix_is_constructible_from_raw_array_of_arrays<itk::Matrix<float>>();
Expect_Matrix_is_constructible_from_raw_array_of_arrays<itk::Matrix<double, 2, 3>>();
}

0 comments on commit 7dd9e49

Please sign in to comment.