Commit d8a124a2 authored by andibraimllari's avatar andibraimllari Committed by AndiBraimllari
Browse files

clip a container

parent 4229e118
Pipeline #873216 failed with stages
in 122 minutes and 43 seconds
...@@ -609,6 +609,20 @@ namespace elsa ...@@ -609,6 +609,20 @@ namespace elsa
} }
} }
template <typename data_t>
DataContainer<data_t> clip(DataContainer<data_t> dc, data_t min, data_t max)
{
for (int i = 0; i < dc.getSize(); ++i) {
if (dc[i] < min) {
dc[i] = min;
} else if (dc[i] > max) {
dc[i] = max;
}
}
return dc;
}
template <typename data_t> template <typename data_t>
DataContainer<data_t> concatenate(const DataContainer<data_t>& dc1, DataContainer<data_t> concatenate(const DataContainer<data_t>& dc1,
const DataContainer<data_t>& dc2) const DataContainer<data_t>& dc2)
...@@ -693,6 +707,9 @@ namespace elsa ...@@ -693,6 +707,9 @@ namespace elsa
template class DataContainer<complex<double>>; template class DataContainer<complex<double>>;
template class DataContainer<index_t>; template class DataContainer<index_t>;
template DataContainer<float> clip<float>(DataContainer<float> dc, float min, float max);
template DataContainer<double> clip<double>(DataContainer<double> dc, double min, double max);
template DataContainer<float> concatenate<float>(const DataContainer<float>&, template DataContainer<float> concatenate<float>(const DataContainer<float>&,
const DataContainer<float>&); const DataContainer<float>&);
template DataContainer<double> concatenate<double>(const DataContainer<double>&, template DataContainer<double> concatenate<double>(const DataContainer<double>&,
......
...@@ -517,6 +517,10 @@ namespace elsa ...@@ -517,6 +517,10 @@ namespace elsa
return os; return os;
} }
/// clip the container values outside of the interval, to the interval edges
template <typename data_t>
DataContainer<data_t> clip(DataContainer<data_t> dc, data_t min, data_t max);
/// Concatenate two DataContainers to one (requires copying of both) /// Concatenate two DataContainers to one (requires copying of both)
template <typename data_t> template <typename data_t>
DataContainer<data_t> concatenate(const DataContainer<data_t>& dc1, DataContainer<data_t> concatenate(const DataContainer<data_t>& dc1,
......
...@@ -927,6 +927,150 @@ TEST_CASE("DataContainer: Testing iterators for DataContainer") ...@@ -927,6 +927,150 @@ TEST_CASE("DataContainer: Testing iterators for DataContainer")
} }
} }
TEST_CASE_TEMPLATE("DataContainer: Clip a DataContainer", data_t, float, double)
{
GIVEN("some 1D vectors")
{
index_t size = 7;
IndexVector_t numCoeff(1);
numCoeff << size;
VolumeDescriptor desc(numCoeff);
data_t min = 6;
data_t max = 19;
Vector_t<data_t> dataVec1(desc.getNumberOfCoefficients());
dataVec1 << 6, 10, 7, 18, 10, 11, 9;
Vector_t<data_t> expectedDataVec1(desc.getNumberOfCoefficients());
expectedDataVec1 << 6, 10, 7, 18, 10, 11, 9;
Vector_t<data_t> dataVec2(desc.getNumberOfCoefficients());
dataVec2 << 4, -23, 7, 18, 18, 10, 10;
Vector_t<data_t> expectedDataVec2(desc.getNumberOfCoefficients());
expectedDataVec2 << min, min, 7, 18, 18, 10, 10;
Vector_t<data_t> dataVec3(desc.getNumberOfCoefficients());
dataVec3 << 14, 23, 7, 18, 20, 10, 10;
Vector_t<data_t> expectedDataVec3(desc.getNumberOfCoefficients());
expectedDataVec3 << 14, max, 7, 18, max, 10, 10;
Vector_t<data_t> dataVec4(desc.getNumberOfCoefficients());
dataVec4 << 1, 23, 5, 28, 20, 30, 0;
Vector_t<data_t> expectedDataVec4(desc.getNumberOfCoefficients());
expectedDataVec4 << min, max, min, max, max, max, min;
WHEN("creating a data container out of a vector within bounds")
{
DataContainer dc(desc, dataVec1);
auto clipped = clip(dc, min, max);
THEN("the size of the clipped DataContainer is equal to that of the original container")
{
REQUIRE_EQ(clipped.getSize(), size);
}
THEN("the values correspond to the original DataContainers")
{
for (int i = 0; i < size; ++i) {
INFO("Error at position: ", i);
REQUIRE_EQ(clipped[i], expectedDataVec1[i]);
}
}
}
WHEN("creating a data container out of a vector within or lower than the bounds")
{
DataContainer dc(desc, dataVec2);
auto clipped = clip(dc, min, max);
THEN("the size of the clipped DataContainer is equal to that of the original container")
{
REQUIRE_EQ(clipped.getSize(), size);
}
THEN("the values correspond to the original DataContainers")
{
for (int i = 0; i < size; ++i) {
INFO("Error at position: ", i);
REQUIRE_EQ(clipped[i], expectedDataVec2[i]);
}
}
}
WHEN("creating a data container out of a vector within or higher than the bounds")
{
DataContainer dc(desc, dataVec3);
auto clipped = clip(dc, min, max);
THEN("the size of the clipped DataContainer is equal to that of the original container")
{
REQUIRE_EQ(clipped.getSize(), size);
}
THEN("the values correspond to the original DataContainers")
{
for (int i = 0; i < size; ++i) {
INFO("Error at position: ", i);
REQUIRE_EQ(clipped[i], expectedDataVec3[i]);
}
}
}
WHEN("creating a data container out of a vector outside the bounds")
{
DataContainer dc(desc, dataVec4);
auto clipped = clip(dc, min, max);
THEN("the size of the clipped DataContainer is equal to that of the original container")
{
REQUIRE_EQ(clipped.getSize(), size);
}
THEN("the values correspond to the original DataContainers")
{
for (int i = 0; i < size; ++i) {
INFO("Error at position: ", i);
REQUIRE_EQ(clipped[i], expectedDataVec4[i]);
}
}
}
}
GIVEN("a 2D data container")
{
IndexVector_t numCoeff(2);
numCoeff << 3, 2;
VolumeDescriptor desc(numCoeff);
data_t min = 0;
data_t max = 8;
Vector_t<data_t> dataVec(desc.getNumberOfCoefficients());
dataVec << -19, -23, 7, 8, 20, 1;
Vector_t<data_t> expectedDataVec(desc.getNumberOfCoefficients());
expectedDataVec << min, min, 7, 8, max, 1;
WHEN("creating a data container out of a vector within and outside of both bounds")
{
DataContainer dc(desc, dataVec);
auto clipped = clip(dc, min, max);
THEN("the size of the clipped DataContainer is equal to that of the original container")
{
REQUIRE_EQ(clipped.getSize(), desc.getNumberOfCoefficients());
}
THEN("the values correspond to the original DataContainers")
{
for (int i = 0; i < desc.getNumberOfCoefficients(); ++i) {
INFO("Error at position: ", i);
REQUIRE_EQ(clipped[i], expectedDataVec[i]);
}
}
}
}
}
TEST_CASE_TEMPLATE("DataContainer: Concatenate two DataContainers", data_t, float, double, TEST_CASE_TEMPLATE("DataContainer: Concatenate two DataContainers", data_t, float, double,
complex<float>, complex<double>) complex<float>, complex<double>)
{ {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment