diff --git a/Modules/Core/Common/include/itkMatrix.h b/Modules/Core/Common/include/itkMatrix.h index 801289bb29e..017fef23072 100644 --- a/Modules/Core/Common/include/itkMatrix.h +++ b/Modules/Core/Common/include/itkMatrix.h @@ -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; @@ -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) { diff --git a/Modules/Core/Common/test/itkMatrixGTest.cxx b/Modules/Core/Common/test/itkMatrixGTest.cxx index 1d5a2811f5a..50e693a23b0 100644 --- a/Modules/Core/Common/test/itkMatrixGTest.cxx +++ b/Modules/Core/Common/test/itkMatrixGTest.cxx @@ -91,6 +91,23 @@ Expect_Matrix_is_constructible_from_raw_array_of_arrays() } } } + + +template +void +Expect_MakeFilled_corresponds_with_Fill_member_function() +{ + using ValueType = typename TMatrix::ValueType; + + for (const auto fillValue : + { ValueType(), std::numeric_limits::min(), std::numeric_limits::max() }) + { + TMatrix matrixToBeFilled{}; + matrixToBeFilled.Fill(fillValue); + + EXPECT_EQ(itk::MakeFilled(fillValue), matrixToBeFilled); + } +} } // namespace @@ -129,3 +146,34 @@ TEST(Matrix, IsConstructibleFromRawArrayOfArrays) Expect_Matrix_is_constructible_from_raw_array_of_arrays>(); Expect_Matrix_is_constructible_from_raw_array_of_arrays>(); } + + +// 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>(); + Expect_MakeFilled_corresponds_with_Fill_member_function>(); +} + + +// 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()); + check(itk::Matrix()); +} + + +// 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()); + check(itk::Matrix()); +}