Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Modules/Core/Common/include/itkMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ class ITK_TEMPLATE_EXPORT Matrix
using ValueType = T;
using ComponentType = T;

/** A pointer to the ValueType. */
using pointer = ValueType *;

/** A const pointer to the ValueType. */
using const_pointer = const ValueType *;

/** A reference to the ValueType. */
using reference = ValueType &;

/** A const reference to the ValueType. */
using const_reference = const ValueType &;

/** The return type of the non-const overloads of begin() and end(). */
using iterator = ValueType *;

/** The return type of cbegin() and cend(), and the const overloads of begin() and end(). */
using const_iterator = const ValueType *;

/** Number Of Columns and Rows. */
static constexpr unsigned int RowDimensions = VRows;
static constexpr unsigned int ColumnDimensions = VColumns;
Expand Down Expand Up @@ -298,6 +316,55 @@ class ITK_TEMPLATE_EXPORT Matrix
*/
Matrix() = default;

/** Returns the number of elements. */
constexpr unsigned int
size() const
{
return m_Matrix.size();
}

/** Returns an iterator to the first element. */
iterator
begin()
{
return m_Matrix.begin();
}

/** Returns an iterator just beyond the last element. */
iterator
end()
{
return m_Matrix.end();
}

/** Returns a const iterator to the first element. */
const_iterator
begin() const
{
return m_Matrix.begin();
}

/** Returns a const iterator just beyond the last element. */
const_iterator
end() const
{
return m_Matrix.end();
}

/** Returns a const iterator to the first element. */
const_iterator
cbegin() const
{
return m_Matrix.begin();
}

/** Returns a const iterator just beyond the last element. */
const_iterator
cend() const
{
return m_Matrix.end();
}

void
swap(Self & other)
{
Expand Down
48 changes: 48 additions & 0 deletions Modules/Core/Common/test/itkMatrixGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ Expect_Matrix_is_constructible_from_raw_array_of_arrays()
}
}
}


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

for (const auto fillValue :
{ ValueType(), std::numeric_limits<ValueType>::min(), std::numeric_limits<ValueType>::max() })
{
TMatrix matrixToBeFilled{};
matrixToBeFilled.Fill(fillValue);

EXPECT_EQ(itk::MakeFilled<TMatrix>(fillValue), matrixToBeFilled);
}
}
} // namespace


Expand Down Expand Up @@ -129,3 +146,34 @@ 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>>();
}


// Tests that the result of MakeFilled corresponds with the resulting matrix after calling its Fill member function.
TEST(Matrix, MakeFilled)
{
Expect_MakeFilled_corresponds_with_Fill_member_function<itk::Matrix<float>>();
Expect_MakeFilled_corresponds_with_Fill_member_function<itk::Matrix<double, 2, 3>>();
}


// Tests that cbegin() and cend() return the same iterators as the corresponding begin() and end() member functions.
TEST(Matrix, CBeginAndCEnd)
{
const auto check = [](const auto & matrix) {
EXPECT_EQ(matrix.cbegin(), matrix.begin());
EXPECT_EQ(matrix.cend(), matrix.end());
};

check(itk::Matrix<float>());
check(itk::Matrix<double, 2, 3>());
}


// Tests that `size()` is equal to `end() - begin()`.
TEST(Matrix, SizeIsDifferenceBetweenBeginEnd)
{
const auto check = [](const auto & matrix) { EXPECT_EQ(matrix.size(), matrix.end() - matrix.begin()); };

check(itk::Matrix<float>());
check(itk::Matrix<double, 2, 3>());
}