[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

splices.hxx
1#ifndef SPLICES_HXX
2#define SPLICES_HXX
3#include <vigra/multi_array.hxx>
4#include <iterator>
5
6 namespace vigra
7{
8/** Idea for a class to use for easy splicing
9 *
10 * usage (with factory function _spl)
11 *
12 * // copy every even indexed element to a 5 x 5
13 * // sized matrix
14 * Matrix<double> a(10, 10)
15 * MultiArrayView<2,double> b(_spl_shp(_spl(0,2,10),
16 * _spl(0,2,10)));
17 * copy_splice(_spl(0,2,10),_spl(0,2,10), a, b);
18 *
19 * it is also possible to supply iterator ranges
20 * std::vector<int> indices;
21 * indices.push_back(3) (...)
22 *
23 * copy_splice(_spl(indices.begin(), indices.end()),
24 * _spl(a.shape(1)),
25 * a, b)
26 *
27 * if you only have a forward iterator then you must
28 * specify the size of the splice with
29 * _spl(set.begin(), set.end(), set.size());
30 *
31 * ok.. what we actually need is a decent iota iterator
32 * or something like xrange but for now it should suffice.
33 */
34 template<class T>
35 class Splice
36{
37 int size_;
38 T begin_;
39 T end_;
40 public:
41 Splice(T &begin, T &end)
42 : size_(std::distance(begin, end)),
43 begin_(begin),
44 end_(end)
45 {}
46
47 int operator[](int index)
48 {
49 T ii = begin_;
50 std::advance(ii, index);
51 return *ii;
52 }
53
54 int size()
55 {
56 return size_;
57 }
58};
59
60 template<>
61 class Splice<int>
62{
63 int begin_;
64 int interval_;
65 int size_;
66 public:
67 Splice(int begin, int end)
68 : begin_(begin),
69 interval_(1),
70 size_(end - begin)
71 {}
72
73 Splice(int begin, int interval, int end)
74 : begin_(begin),
75 interval_(interval),
76 size_(int(std::floor((double(end) -double(begin))/interval)))
77 {}
78
79 int operator[](int index)
80 {
81 int ii = begin_ + index * interval_;
82 return ii;
83 }
84
85 int size()
86 {
87 return size_;
88 }
89};
90
91 template<class T>
92Splice<T> _spl(T b, T e)
93{
94 return Splice<T>(b, e);
95}
96 template<class T>
97Splice<T> _spl(T b, int size, T e)
98{
99 return Splice<T>(b, size, e);
100}
101
102 inline Splice<int> _spl(int size)
103{
104 return Splice<int>(0, size);
105}
106
107
108
109 template<class T, class G>
110 inline MultiArrayShape<2>::type _spl_shp(Splice<T> f,
111 Splice<G> h)
112{
113 return MultiArrayShape<2>::type(f.size(), h.size());
114}
115
116
117
118
119 template< class R, class F,
120 class T, class C,
121 class T2, class C2 >
122 void copy_splice( Splice<R> _first,
123 Splice<F> _second,
124 MultiArrayView<2, T, C> src,
125 MultiArrayView<2, T2, C2> dest)
126{
127 for(int jj = 0 ; jj < _second.size(); ++jj)
128 {
129 for(int ii = 0 ; ii < _first.size(); ++ii)
130 {
131 dest(ii, jj) = src(_first[ii], _second[jj]);
132 }
133 }
134}
135
136};
137#endif //SPLICES_HXX
vigra::MultiArrayShape::type
TinyVector< MultiArrayIndex, N > type
Definition multi_shape.hxx:272
vigra::RGBValue
Class for a single RGB value.
Definition rgbvalue.hxx:128
vigra::Splice
Definition splices.hxx:36
vigra

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.11.1