SLIP  1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Iterations through 2d containers

Introduction

Here, we present various ways to computes the mean of a 2d container denoted S2d. It illustrates the different uses to iterate through bidimensional containers 2d Containers like slip::block2d, slip::Array2d, slip::Matrix, slip::GrayscaleImage and multicomponent bidimensional containers Multi-component 2d Containers like: slip::ColorImage, slip::MultispectralImage... Here are the axis conventions used in the library:

iterator2d_conventions.png

The classical loop

iterator2d.png
double mean = 0.0;
const std::size_t rows = S2d.rows();
const std::size_t cols = S2d.cols();
for(std::size_t i = 0; i < rows; ++i)
{
for(std::size_t j = 0; j < cols; ++j)
{
mean += S2d[i][j]; //or equivalently: mean += S2d(i,j);
}
}
mean = mean / double(S2d.size());

1d iterators

These iterators follow the RandomAccessIterator concept of the standard library.

Direct global 1d iterator

iterator2d.png
double m = slip::mean<double>(S2d.begin(),S2d.end());

Reverse global 1d iterator

reverse_iterator2d.png
double m = slip::mean<double>(S2d.rbegin(),S2d.rend());

Row iterators

Direct row iterator

row_iterator.png
double m = slip::mean<double>(S2d.row_begin(i),S2d.row_end(i));

Reverse row iterator

reverse_row_iterator.png
double m = slip::mean<double>(S2d.row_rbegin(i),S2d.row_rend(i));

Direct row range iterator

row_range_iterator.png
slip::Range<int> range_rows(1,3,2);
double m = slip::mean<double>(S2d.row_begin(i,range_rows),S2d.row_end(i,range_rows));

Reverse row range iterator

row_reverse_range_iterator.png
slip::Range<int> range_rows(1,3,2);
double m = slip::mean<double>(S2d.row_rbegin(i,range_rows),S2d.row_rend(i,range_rows));

Column iterators

Direct col iterator

col_iterator.png
double m = slip::mean<double>(S2d.col_begin(i),S2d.col_end(i));

Reverse column iterator

reverse_col_iterator.png
double m = slip::mean<double>(S2d.col_rbegin(i),S2d.col_rend(i));

Direct column range iterator

col_range_iterator.png
slip::Range<int> range_cols(1,3,2);
double m = slip::mean<double>(S2d.col_begin(i,range_cols),S2d.col_end(i,range_cols));

Reverse column range iterator

col_reverse_range_iterator.png
slip::Range<int> range_cols(1,3,2);
double m = slip::mean<double>(S2d.col_rbegin(i,range_cols),S2d.col_rend(i,range_cols));

2d iterators

Box iterators

These iterators follow the BidirectionalIterator concept of the standard library. Specific bidimensional iterator that store the i (x1) and j (x2) positions of the iterator within the container. These coordinates are accessible by the method i() (x1())and j() (x2()). These iterators are less fast than RandomAccessIterator because they verify that the iteration stay within the defined box. See the internal classes slip::iterator2d_box and slip::const_iterator2d_box for more details.

iterator2d.png
double m = slip::mean<double>(S2d.upper_left(),S2d.bottom_right());
box_iterator.png
slip::Box2d<int> box(1,1,2,3);
double m = slip::mean<double>(S2d.upper_left(box),S2d.bottom_right(box));
box_reverse_iterator.png
slip::Box2d<int> box(1,1,2,3);
double m = slip::mean<double>(S2d.rupper_left(box),S2d.rbottom_right(box));

Range iterators

These iterators follow the BidirectionalIterator concept of the standard library. Specific bidimensional iterator that store the i (x1) and j (x2) positions of the iterator within the container. These coordinates are accessible by the method i() (x1())and j() (x2()). These iterators are less fast than RandomAccessIterator because they verify that the iteration stay within the defined ranges. See the internal classes slip::iterator2d_range and slip::const_iterator2d_range for more details.

range_iterator2d.png
slip::Range<int> range_rows(0,2,2);
slip::Range<int> range_cols(0,2,2);
double m = slip::mean<double>(S2d.upper_left(range_rows,range_cols),
S2d.bottom_right(range_rows,range_cols));
range_reverse_iterator2d.png
slip::Range<int> range_rows(0,2,2);
slip::Range<int> range_cols(0,2,2);
double m = slip::mean<double>(S2d.rupper_left(range_rows,range_cols),
S2d.rbottom_right(range_rows,range_cols));

Multi-component iterators

To iterate through a specific component of a multi-component container. It suffies to add first the number of the component to all the previous iterator methods. See slip::GenericMultiComponent2d interface for a more detailed description. As an example, we give here the iteration through a row of a specific component:

row_iterator_multi.png
const std::size_t component = 1;
const std::size_t row = 1;
double m = slip::mean<double>(S2d.row_begin(component,row),S2d.row_end(component,row));
row_iterator_multi.png
const std::size_t component = 1;
const std::size_t row = 1;
double m = slip::mean<double>(S2d.row_rbegin(component,row),S2d.row_rend(component,row));