diff --git a/Modules/Core/Common/include/itkArray.h b/Modules/Core/Common/include/itkArray.h index ed315b30361..682970dc072 100644 --- a/Modules/Core/Common/include/itkArray.h +++ b/Modules/Core/Common/include/itkArray.h @@ -65,9 +65,14 @@ class ITK_TEMPLATE_EXPORT Array : public vnl_vector /** Construct from a VnlVectorType */ explicit Array(const VnlVectorType &); - /** Constructor with size. Size can only be changed by assignment */ + /** Constructor with size. Size can only be changed by assignment. + * \note This constructor may not initialize its elements. + */ explicit Array(SizeValueType dimension); + /** Constructor with size and initial value for each element. */ + explicit Array(SizeValueType dimension, const ValueType & value); + /** Constructor that initializes array with contents from a user supplied * buffer. The pointer to the buffer and the length is specified. By default, * the array does not manage the memory of the buffer. It merely points to diff --git a/Modules/Core/Common/include/itkArray.hxx b/Modules/Core/Common/include/itkArray.hxx index f296facf36f..1e4db3762c8 100644 --- a/Modules/Core/Common/include/itkArray.hxx +++ b/Modules/Core/Common/include/itkArray.hxx @@ -60,6 +60,13 @@ Array::Array(SizeValueType dimension) m_LetArrayManageMemory(true) {} +/** Constructor with size and initial value for each element. */ +template +Array::Array(const SizeValueType dimension, const TValue & value) + : vnl_vector(dimension, value) + , m_LetArrayManageMemory{ true } +{} + /** Constructor with user specified data */ template Array::Array(ValueType * datain, SizeValueType sz, bool LetArrayManageMemory) diff --git a/Modules/Core/Common/include/itkOptimizerParameters.h b/Modules/Core/Common/include/itkOptimizerParameters.h index 77a5588720c..23cc217d4d0 100644 --- a/Modules/Core/Common/include/itkOptimizerParameters.h +++ b/Modules/Core/Common/include/itkOptimizerParameters.h @@ -67,7 +67,9 @@ class ITK_TEMPLATE_EXPORT OptimizerParameters : public Array(dimension) {} @@ -77,6 +79,11 @@ class ITK_TEMPLATE_EXPORT OptimizerParameters : public Array(array) {} + /** Constructor with size and initial value for each element. */ + explicit OptimizerParameters(const SizeValueType dimension, const ValueType & value) + : Array(dimension, value) + {} + /** Initialize. Initialization called by constructors. */ void Initialize() diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 864e0cbbc45..799b939f807 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -617,6 +617,7 @@ set(ITKCommonGTests itkIndexRangeGTest.cxx itkMersenneTwisterRandomVariateGeneratorGTest.cxx itkNeighborhoodAllocatorGTest.cxx + itkOptimizerParametersGTest.cxx itkPointGTest.cxx itkShapedImageNeighborhoodRangeGTest.cxx itkSizeGTest.cxx diff --git a/Modules/Core/Common/test/itkOptimizerParametersGTest.cxx b/Modules/Core/Common/test/itkOptimizerParametersGTest.cxx new file mode 100644 index 00000000000..7f50b03b80f --- /dev/null +++ b/Modules/Core/Common/test/itkOptimizerParametersGTest.cxx @@ -0,0 +1,42 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +// First include the header file to be tested: +#include "itkOptimizerParameters.h" +#include +#include // For std::count. + + +// Tests constructing OptimizerParameters of the specified size and initial value. +TEST(OptimizerParameters, ConstructWithSpecifiedSizeAndInitialValue) +{ + using OptimizerParametersType = itk::OptimizerParameters; + + for (double initialValue{ -1.0 }; initialValue <= 1.0; ++initialValue) + { + EXPECT_EQ(OptimizerParametersType(0, initialValue).size(), 0); + + for (std::size_t size{ 1 }; size <= 4; ++size) + { + const OptimizerParametersType optimizerParameters(size, initialValue); + + EXPECT_EQ(optimizerParameters.size(), size); + EXPECT_EQ(std::count(optimizerParameters.begin(), optimizerParameters.end(), initialValue), size); + } + } +}