1 /************************************************************************/
2 /* */
3 /* Copyright 2009 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35
36 #ifndef VIGRA_MESHGRID_HXX
37 #define VIGRA_MESHGRID_HXX
38
39 #include "tinyvector.hxx"
40 #include "diff2d.hxx"
41
42 namespace vigra{
43 /** \addtogroup RangesAndPoints */
44 //@{
45
46 /********************************************************/
47 /* */
48 /* MeshGridAccessor */
49 /* */
50 /********************************************************/
51 /** Accessor for turning iteration over Diff2D into a mesh grid.
52
53 The mesh grid concept is adapted from MATLAB. It is a two banded image
54 (i.e. with 2D vector pixel type) whose first band contains the x-coordinates
55 of the current pixel, and whose second band contains the y-coordinates.
56 See \ref meshGrid() for more detailed documentation.
57 */
58 struct MeshGridAccessor
59 {
60 /** the value_type of a mesh grid is a 2D vector
61 */
62 typedef TinyVector<Diff2D::MoveX, 2> value_type;
63
64 /** read the current data item
65 */
66 template <class ITERATOR>
operator ()vigra::MeshGridAccessor67 value_type operator()(ITERATOR const & i) const
68 {
69 return value_type(i->x, i->y);
70 }
71
72 /** read the data item at an offset (can be 1D or 2D or higher order difference).
73 */
74 template <class ITERATOR, class DIFFERENCE>
operator ()vigra::MeshGridAccessor75 value_type operator()(ITERATOR const & i, DIFFERENCE const & diff) const
76 {
77 return value_type(i->x+diff.x, i->y+diff.y);
78 }
79 };
80
81 /** Create a mesh grid for the specified rectangle.
82
83 The mesh grid concept is adapted from MATLAB. It is a two banded image
84 (i.e. with 2D vector pixel type) whose first band contains the x-coordinates
85 of the current pixel, and whose second band contains the y-coordinates.
86 If \a upperLeft is not the point (0,0), the mesh grid is translated relative to
87 the pixel indices.
88
89 <b> Declarations:</b>
90
91 \code
92 triple<Diff2D, Diff2D, MeshGridAccessor>
93 meshGrid(Diff2D upperLeft, Diff2D lowerRight);
94
95 triple<Diff2D, Diff2D, MeshGridAccessor>
96 meshGrid(Rect2D const & r);
97
98 \endcode
99
100 <b>Usage:</b>
101
102 \code
103 #include <vigra/meshgrid.hxx>
104 // create an image whose values are equal to each pixel's distance from the image center
105 int width = 5, height = 7;
106 int xc = width/2, yc = height/2; // the image center
107
108 FImage dist(width, height);
109 Point2D upperLeft(-xc, -yc);
110
111 using namespace vigra::functor;
112 transformImage(meshGrid(upperLeft, upperLeft+dist.size()),
113 destImage(dist),
114 norm(Arg1()));
115 \endcode
116 */
117 inline
118 triple<Diff2D, Diff2D, MeshGridAccessor>
meshGrid(Diff2D upperLeft,Diff2D lowerRight)119 meshGrid(Diff2D upperLeft, Diff2D lowerRight)
120 {
121 return triple<Diff2D, Diff2D, MeshGridAccessor>(upperLeft, lowerRight, MeshGridAccessor());
122 }
123
124 inline
125 triple<Diff2D, Diff2D, MeshGridAccessor>
meshGrid(Rect2D const & r)126 meshGrid(Rect2D const & r)
127 {
128 return triple<Diff2D, Diff2D, MeshGridAccessor>(r.upperLeft(), r.lowerRight(), MeshGridAccessor());
129 }
130
131 }//namespace vigra
132 //@}
133 #endif //VIGRA_MESHGRID_HXX
134