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

vigra/meshgrid.hxx
00001 /************************************************************************/
00002 /*                                                                      */
00003 /*                  Copyright 2009 by Ullrich Koethe                    */
00004 /*                                                                      */
00005 /*    This file is part of the VIGRA computer vision library.           */
00006 /*    The VIGRA Website is                                              */
00007 /*        http://hci.iwr.uni-heidelberg.de/vigra/                       */
00008 /*    Please direct questions, bug reports, and contributions to        */
00009 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00010 /*        vigra@informatik.uni-hamburg.de                               */
00011 /*                                                                      */
00012 /*    Permission is hereby granted, free of charge, to any person       */
00013 /*    obtaining a copy of this software and associated documentation    */
00014 /*    files (the "Software"), to deal in the Software without           */
00015 /*    restriction, including without limitation the rights to use,      */
00016 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00017 /*    sell copies of the Software, and to permit persons to whom the    */
00018 /*    Software is furnished to do so, subject to the following          */
00019 /*    conditions:                                                       */
00020 /*                                                                      */
00021 /*    The above copyright notice and this permission notice shall be    */
00022 /*    included in all copies or substantial portions of the             */
00023 /*    Software.                                                         */
00024 /*                                                                      */
00025 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00026 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00027 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00028 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00029 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00030 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00031 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00032 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */
00033 /*                                                                      */
00034 /************************************************************************/
00035 
00036 #ifndef VIGRA_MESHGRID_HXX
00037 #define VIGRA_MESHGRID_HXX
00038 
00039 #include "tinyvector.hxx"
00040 #include "diff2d.hxx"
00041 
00042 namespace vigra{
00043 /** \addtogroup RangesAndPoints Two-dimensional Ranges and Points
00044 
00045     Specify a 2D position, extent, or rectangle.
00046 */
00047 //@{
00048 
00049 /********************************************************/
00050 /*                                                      */
00051 /*                 MeshGridAccessor                     */
00052 /*                                                      */
00053 /********************************************************/
00054 /** Accessor for turning iteration over Diff2D into a mesh grid.
00055 
00056     The mesh grid concept is adapted from MATLAB. It is a two banded image
00057     (i.e. with 2D vector pixel type) whose first band contains the x-coordinates
00058     of the current pixel, and whose second band contains the y-coordinates.
00059     See \ref meshGrid() for more detailed documentation.
00060 */
00061 struct MeshGridAccessor
00062 {
00063         /** the value_type of a mesh grid is a 2D vector
00064         */
00065     typedef TinyVector<Diff2D::MoveX, 2> value_type;
00066 
00067         /** read the current data item
00068         */
00069     template <class ITERATOR>
00070     value_type operator()(ITERATOR const & i) const
00071     {
00072         return value_type(i->x, i->y);
00073     }
00074 
00075         /** read the data item at an offset (can be 1D or 2D or higher order difference).
00076         */
00077     template <class ITERATOR, class DIFFERENCE>
00078     value_type operator()(ITERATOR const & i, DIFFERENCE const & diff) const
00079     {
00080         return value_type(i->x+diff.x, i->y+diff.y);
00081     }
00082 };
00083 
00084 /** Create a mesh grid for the specified rectangle.
00085 
00086     The mesh grid concept is adapted from MATLAB. It is a two banded image
00087     (i.e. with 2D vector pixel type) whose first band contains the x-coordinates
00088     of the current pixel, and whose second band contains the y-coordinates.
00089     If \a upperLeft is not the point (0,0), the mesh grid is translated relative to
00090     the pixel indices.
00091 
00092     <b> Declarations:</b>
00093 
00094     \code
00095     triple<Diff2D, Diff2D, MeshGridAccessor>
00096     meshGrid(Diff2D upperLeft, Diff2D lowerRight);
00097 
00098     triple<Diff2D, Diff2D, MeshGridAccessor>
00099     meshGrid(Rect2D const & r);
00100 
00101     \endcode
00102 
00103     <b>Usage:</b>
00104 
00105     \code
00106     #include <vigra/meshgrid.hxx>
00107     // create an image whose values are equal to each pixel's distance from the image center
00108     int width = 5, height = 7;
00109     int xc = width/2, yc = height/2; // the image center
00110 
00111     FImage dist(width, height);
00112     Point2D upperLeft(-xc, -yc);
00113 
00114     using namespace vigra::functor;
00115     transformImage(meshGrid(upperLeft, upperLeft+dist.size()),
00116                    destImage(dist),
00117                    norm(Arg1()));
00118     \endcode
00119 */
00120 inline
00121 triple<Diff2D, Diff2D, MeshGridAccessor>
00122 meshGrid(Diff2D upperLeft, Diff2D lowerRight)
00123 {
00124     return triple<Diff2D, Diff2D, MeshGridAccessor>(upperLeft, lowerRight, MeshGridAccessor());
00125 }
00126 
00127 inline
00128 triple<Diff2D, Diff2D, MeshGridAccessor>
00129 meshGrid(Rect2D const & r)
00130 {
00131     return triple<Diff2D, Diff2D, MeshGridAccessor>(r.upperLeft(), r.lowerRight(), MeshGridAccessor());
00132 }
00133 
00134 }//namespace vigra
00135 //@}
00136 #endif //VIGRA_MESHGRID_HXX

© 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.7.0 (Thu Aug 25 2011)