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

details vigra/accessor.hxx VIGRA

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 1998-2002 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    ( Version 1.2.0, Aug 07 2003 )                                    */
00008 /*    You may use, modify, and distribute this software according       */
00009 /*    to the terms stated in the LICENSE file included in               */
00010 /*    the VIGRA distribution.                                           */
00011 /*                                                                      */
00012 /*    The VIGRA Website is                                              */
00013 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00014 /*    Please direct questions, bug reports, and contributions to        */
00015 /*        koethe@informatik.uni-hamburg.de                              */
00016 /*                                                                      */
00017 /*  THIS SOFTWARE IS PROVIDED AS IS AND WITHOUT ANY EXPRESS OR          */
00018 /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED      */
00019 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
00020 /*                                                                      */
00021 /************************************************************************/
00022  
00023 #ifndef VIGRA_ACCESSOR_HXX
00024 #define VIGRA_ACCESSOR_HXX
00025 
00026 #include "vigra/numerictraits.hxx"
00027 #include "vigra/tuple.hxx"
00028 
00029 namespace vigra {
00030 
00031 /** \addtogroup DataAccessors Data Accessors
00032 
00033     Basic templates to encapsulate access to the data of an iterator.
00034     
00035     Data accessors are used to allow for flexible access to the data
00036     an interator points to. When we access the data directly, we
00037     are bound to what <TT>operator*()</TT> returns, if this method exists at 
00038     all. Encapsulating access in an accessor enables a better
00039     decoupling of data structures and algorithms. 
00040     <a href="documents/DataAccessors.ps">This paper</a> contains
00041     a detailed description of the concept. Here is a brief list of the basic
00042     accessor requirements:
00043 <p>
00044 <table border=2 cellspacing=0 cellpadding=2 width="100%">
00045 <tr><td>
00046     \htmlonly
00047     <th>
00048     \endhtmlonly
00049     Operation
00050     \htmlonly
00051     </th><th>
00052     \endhtmlonly
00053     Result
00054     \htmlonly
00055     </th><th>
00056     \endhtmlonly
00057     Semantics
00058     \htmlonly
00059     </th>
00060     \endhtmlonly
00061 </td></tr>
00062 <tr>
00063     <td><tt>accessor(iter)</tt></td><td>convertible to <br><tt>Iterator::value_type const &</tt></td>
00064     <td>read data at the current position of the iterator</td>
00065 </tr>
00066 <tr>
00067     <td><tt>accessor(iter, index)</tt></td><td>convertible to <br><tt>Accessor::value_type const &</tt></td>
00068     <td>read data at offset <tt>index</tt> relative to iterator's current position 
00069     (random-access iterator only)</td>
00070 </tr>
00071 <tr>
00072     <td><tt>accessor.set(value, iter)</tt></td><td><tt>void</tt></td>
00073     <td>write data <tt>value</tt> at the current position of the iterator (mutable iterator only)</td>
00074 </tr>
00075 <tr>
00076     <td><tt>accessor.set(value, iter, index)</tt></td><td><tt>void</tt></td>
00077     <td>write data <tt>value</tt> at offset <tt>index</tt> relative to iterator's current position 
00078     (mutable random-access iterator only)</td>
00079 </tr>
00080 <tr>
00081     <td>
00082     \htmlonly
00083     <td colspan=2>
00084     \endhtmlonly
00085     <tt>Accessor::value_type</tt></td>
00086     <td>type of the data field the accessor refers to</td>
00087 </tr>
00088 <tr>
00089     <td>
00090     \htmlonly
00091     <td colspan=3>
00092     \endhtmlonly
00093         <tt>iter</tt> is an iterator<br>
00094         <tt>index</tt> has the iterator's index type (<tt>Iterator::difference_type</tt>)<br>
00095         <tt>value</tt> is convertible to <tt>Accessor::value_type const &</tt>
00096     </td>
00097 </tr>
00098 </table>
00099 </p>
00100 */
00101 //@{
00102 
00103 /********************************************************/
00104 /*                                                      */
00105 /*                     StandardAccessor                 */
00106 /*                                                      */
00107 /********************************************************/
00108 
00109 /** \brief Encapsulate access to the values an iterator points to.
00110 
00111     StandardAccessor is a trivial accessor that simply encapsulates 
00112     the iterator's operator*() and operator[]() in its 
00113     read and write functions. It passes its arguments <em>by reference</em>. 
00114     If you want to return items by value, you 
00115     must use StandardValueAccessor instead of StandardAccessor.
00116     Both accessors have different optimization properties --
00117     StandardAccessor is usually faster for compound pixel types,
00118     while StandardValueAccessor is faster for the built-in types.
00119 
00120     When a floating point number is assigned by means of an accessor
00121     with integral value_type, the value is rounded and clipped as approriate.
00122 
00123     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>
00124     Namespace: vigra
00125 */
00126 template <class VALUETYPE>
00127 class StandardAccessor
00128 {
00129   public:
00130         /** the value_type
00131         */
00132     typedef VALUETYPE value_type;
00133     
00134         /** read the current data item
00135         */
00136     template <class ITERATOR>
00137     VALUETYPE const & operator()(ITERATOR const & i) const { return *i; }
00138     
00139     VALUETYPE const & operator()(VALUETYPE const * i) const { return *i; }
00140     
00141         /** read the data item at an offset (can be 1D or 2D or higher order difference).
00142         */
00143     template <class ITERATOR, class DIFFERENCE>
00144     VALUETYPE const & operator()(ITERATOR const & i, DIFFERENCE const & diff) const
00145     { 
00146         return i[diff]; 
00147     }
00148     
00149         /** Write the current data item. The type <TT>V</TT> of the passed
00150             in <TT>value</TT> is automatically converted to <TT>VALUETYPE</TT>.
00151             In case of a conversion floating point -> intergral this includes rounding and clipping.
00152         */
00153     template <class V, class ITERATOR>
00154     void set(V const & value, ITERATOR const & i) const 
00155     { *i = detail::RequiresExplicitCast<VALUETYPE>::cast(value); }
00156     
00157         /** Write the data item at an offset (can be 1D or 2D or higher order difference)..
00158             The type <TT>V</TT> of the passed
00159             in <TT>value</TT> is automatically converted to <TT>VALUETYPE</TT>.
00160             In case of a conversion floating point -> intergral this includes rounding and clipping.
00161         */
00162     template <class V, class ITERATOR, class DIFFERENCE>
00163     void set(V const & value, ITERATOR const & i, DIFFERENCE const & diff) const 
00164     { 
00165         i[diff]= detail::RequiresExplicitCast<VALUETYPE>::cast(value); 
00166     }
00167 };
00168 
00169 /** \brief Encapsulate access to the values an iterator points to.
00170 
00171     StandardValueAccessor is a trivial accessor that simply encapsulates 
00172     the iterator's operator*() and operator[]() in its 
00173     read and write functions. It passes its arguments <em>by value</em>. 
00174     If the iterator returns its items by reference (such as \ref vigra::ImageIterator), 
00175     you can also use StandardAccessor.
00176     These accessors have different optimization properties --
00177     StandardAccessor is usually faster for compound pixel types,
00178     while StandardValueAccessor is faster for the built-in types.
00179     
00180     When a floating point number is assigned by means of an accessor
00181     with integral value_type, the value is rounded and clipped as approriate.
00182 
00183     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>
00184     Namespace: vigra
00185 */
00186 template <class VALUETYPE>
00187 class StandardValueAccessor
00188 {
00189   public:
00190         /** the value_type
00191         */
00192     typedef VALUETYPE value_type;
00193     
00194         /** Read the current data item. The type <TT>ITERATOR::reference</TT>
00195             is automatically converted to <TT>VALUETYPE</TT>.
00196             In case of a conversion floating point -> intergral this includes rounding and clipping.
00197         */
00198     template <class ITERATOR>
00199     VALUETYPE operator()(ITERATOR const & i) const 
00200         { return detail::RequiresExplicitCast<VALUETYPE>::cast(*i); }
00201     
00202         /** Read the data item at an offset (can be 1D or 2D or higher order difference).
00203             The type <TT>ITERATOR::index_reference</TT>
00204             is automatically converted to <TT>VALUETYPE</TT>.
00205             In case of a conversion floating point -> intergral this includes rounding and clipping.
00206         */
00207     template <class ITERATOR, class DIFFERENCE>
00208     VALUETYPE operator()(ITERATOR const & i, DIFFERENCE const & diff) const
00209     { 
00210         return detail::RequiresExplicitCast<VALUETYPE>::cast(i[diff]); 
00211     }
00212         /** Write the current data item. The type <TT>V</TT> of the passed
00213             in <TT>value</TT> is automatically converted to <TT>VALUETYPE</TT>.
00214             In case of a conversion floating point -> intergral this includes rounding and clipping.
00215         */
00216     template <class V, class ITERATOR>
00217     void set(V value, ITERATOR const & i) const 
00218         { *i = detail::RequiresExplicitCast<VALUETYPE>::cast(value); }
00219     
00220         /** Write the data item at an offset (can be 1D or 2D or higher order difference)..
00221             The type <TT>V</TT> of the passed
00222             in <TT>value</TT> is automatically converted to <TT>VALUETYPE</TT>.
00223             In case of a conversion floating point -> intergral this includes rounding and clipping.
00224         */
00225     template <class V, class ITERATOR, class DIFFERENCE>
00226     void set(V value, ITERATOR const & i, DIFFERENCE const & diff) const 
00227     { 
00228         i[diff]= detail::RequiresExplicitCast<VALUETYPE>::cast(value); 
00229     }
00230 };
00231 
00232 /********************************************************/
00233 /*                                                      */
00234 /*                StandardConstAccessor                 */
00235 /*                                                      */
00236 /********************************************************/
00237 
00238 /** \brief Encapsulate read access to the values an iterator points to.
00239 
00240     StandardConstAccessor is a trivial accessor that simply encapsulates 
00241     the iterator's operator*() and operator[]() in its 
00242     read functions. It passes its arguments <em>by reference</em>. 
00243     If the iterator returns its items by value (such as \ref vigra::CoordinateIterator), you 
00244     must use StandardConstValueAccessor instead of StandardConstAccessor.
00245     Both accessors also have different optimization properties --
00246     StandardConstAccessor is usually faster for compound pixel types,
00247     while StandardConstValueAccessor is faster for the built-in types.
00248 
00249     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>
00250     Namespace: vigra
00251 */
00252 template <class VALUETYPE>
00253 class StandardConstAccessor
00254 {
00255   public:
00256     typedef VALUETYPE value_type;
00257     
00258         /** read the current data item
00259         */
00260     template <class ITERATOR>
00261     VALUETYPE const & operator()(ITERATOR const & i) const 
00262         { return *i; }
00263     
00264         /** read the data item at an offset (can be 1D or 2D or higher order difference).
00265         */
00266     template <class ITERATOR, class DIFFERENCE>
00267     VALUETYPE const & operator()(ITERATOR const & i, DIFFERENCE const & diff) const
00268     { 
00269         return i[diff]; 
00270     }
00271 };
00272 
00273 /** \brief Encapsulate access to the values an iterator points to.
00274 
00275     StandardConstValueAccessor is a trivial accessor that simply encapsulates 
00276     the iterator's operator*() and operator[]() in its 
00277     read functions. It passes its arguments <em>by value</em>. 
00278     If the iterator returns its items by reference (such as \ref vigra::ConstImageIterator), 
00279     you can also use StandardConstAccessor.
00280     These accessors have different optimization properties --
00281     StandardConstAccessor is usually faster for compound pixel types,
00282     while StandardConstValueAccessor is faster for the built-in types.
00283 
00284     When an iterator passes a floating point number to an accessor
00285     with integral value_type, the value is rounded and clipped as approriate.
00286 
00287     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>
00288     Namespace: vigra
00289 */
00290 template <class VALUETYPE>
00291 class StandardConstValueAccessor
00292 {
00293   public:
00294     typedef VALUETYPE value_type;
00295     
00296         /** Read the current data item. The type <TT>ITERATOR::reference</TT>
00297             is automatically converted to <TT>VALUETYPE</TT>.
00298             In case of a conversion floating point -> intergral this includes rounding and clipping.
00299         */
00300     template <class ITERATOR>
00301     VALUETYPE operator()(ITERATOR const & i) const 
00302         { return detail::RequiresExplicitCast<VALUETYPE>::cast(*i); }
00303     
00304         /** Read the data item at an offset (can be 1D or 2D or higher order difference).
00305             The type <TT>ITERATOR::index_reference</TT>
00306             is automatically converted to <TT>VALUETYPE</TT>.
00307             In case of a conversion floating point -> intergral this includes rounding and clipping.
00308         */
00309     template <class ITERATOR, class DIFFERENCE>
00310     VALUETYPE operator()(ITERATOR const & i, DIFFERENCE const & diff) const
00311     { 
00312         return detail::RequiresExplicitCast<VALUETYPE>::cast(i[diff]); 
00313     }
00314 };
00315 
00316 /********************************************************/
00317 /*                                                      */
00318 /*                 VectorComponentAccessor              */
00319 /*                                                      */
00320 /********************************************************/
00321 
00322 /** \brief Accessor for one component of a vector.
00323 
00324     This accessor allows to select a single component (a single 'band')
00325     of a vector valued pixel type. The pixel type must support
00326     <TT>operator[]</TT>. The index of the component to be selected
00327     is passed in the constructor. The accessor returns its items 
00328     <em>by reference</em>. If you want to pass/return items by value,
00329     use VectorComponentValueAccessor. If a floating point number
00330     is assigned by means of an accessor with integral value_type, the
00331     value is rounded and clipped as appropriate. 
00332 
00333     <b>Usage:</b>
00334     
00335     \code
00336     vigra::BRGBImage image(w,h);
00337     
00338     // init red channel with 255
00339     initImage(destImageRange(image, 
00340                              VectorComponentAccessor<vigra::BRGBImage::value_type>(0)),
00341               255);
00342     \endcode
00343     
00344     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>
00345     Namespace: vigra
00346     
00347 */
00348 template <class VECTORTYPE>
00349 class VectorComponentAccessor
00350 {
00351     int index_;
00352   public:
00353         /** the value_type
00354         */
00355     typedef typename VECTORTYPE::value_type value_type;
00356     
00357         /** determine the component to be accessed
00358         */
00359     VectorComponentAccessor(int index)
00360     : index_(index)
00361     {}
00362     
00363         /** read the current data item
00364         */
00365     template <class ITERATOR>
00366     value_type const & operator()(ITERATOR const & i) const 
00367         { return (*i)[index_]; }
00368     
00369         /** read the data item at an offset (can be 1D or 2D or higher order difference).
00370         */
00371     template <class ITERATOR, class DIFFERENCE>
00372     value_type const & operator()(ITERATOR const & i, DIFFERENCE const & diff) const
00373     { 
00374         return i[diff][index_]; 
00375     }
00376     
00377         /** Write the current data item. The type <TT>V</TT> of the passed
00378             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00379             In case of a conversion floating point -> intergral this includes rounding and clipping.
00380         */
00381     template <class V, class ITERATOR>
00382     void set(V const & value, ITERATOR const & i) const 
00383     { 
00384         (*i)[index_] = detail::RequiresExplicitCast<value_type>::cast(value); 
00385     }
00386     
00387         /** Write the data item at an offset (can be 1D or 2D or higher order difference)..
00388             The type <TT>V</TT> of the passed
00389             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00390             In case of a conversion floating point -> intergral this includes rounding and clipping.
00391         */
00392     template <class V, class ITERATOR, class DIFFERENCE>
00393     void set(V const & value, ITERATOR const & i, DIFFERENCE const & diff) const 
00394     { 
00395         i[diff][index_]= detail::RequiresExplicitCast<value_type>::cast(value); 
00396     }
00397 };
00398 
00399 /** \brief Accessor for one component of a vector.
00400 
00401     This accessor allows to select a single component (a single 'band')
00402     of a vector valued pixel type. The pixel type must support
00403     <TT>operator[]</TT>. The index of the component to be selected
00404     is passed in the constructor. The accessor returns its items 
00405     <em>by value</em>. If you want to pass/return items by reference,
00406     use VectorComponentAccessor. If a floating point number
00407     is assigned by means of an accessor with integral value_type, the
00408     value is rounded and clipped as appropriate. 
00409 
00410     <b>Usage:</b>
00411     
00412     \code
00413     vigra::BRGBImage image(w,h);
00414     
00415     // init red channel with 255
00416     initImage(destImageRange(image, 
00417                              VectorComponentValueAccessor<vigra::BRGBImage::value_type>(0)),
00418               255);
00419     \endcode
00420     
00421     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>
00422     Namespace: vigra
00423     
00424 */
00425 template <class VECTORTYPE>
00426 class VectorComponentValueAccessor
00427 {
00428     int index_;
00429   public:
00430         /** the value_type
00431         */
00432     typedef typename VECTORTYPE::value_type value_type;
00433     
00434         /** determine the component to be accessed
00435         */
00436     VectorComponentValueAccessor(int index)
00437     : index_(index)
00438     {}
00439     
00440         /** Read the current data item.
00441             The type <TT>ITERATOR::index_reference::value_type</TT>
00442             is automatically converted to <TT>value_type</TT>.
00443             In case of a conversion floating point -> intergral this includes rounding and clipping.
00444         */
00445     template <class ITERATOR>
00446     value_type operator()(ITERATOR const & i) const 
00447         { return detail::RequiresExplicitCast<value_type>::cast((*i)[index_]); }
00448     
00449         /** Read the data item at an offset (can be 1D or 2D or higher order difference).
00450             The type <TT>ITERATOR::index_reference::value_type</TT>
00451             is automatically converted to <TT>value_type</TT>.
00452             In case of a conversion floating point -> intergral this includes rounding and clipping.
00453         */
00454     template <class ITERATOR, class DIFFERENCE>
00455     value_type operator()(ITERATOR const & i, DIFFERENCE const & diff) const
00456     { 
00457         return detail::RequiresExplicitCast<value_type>::cast(i[diff][index_]); 
00458     }
00459     
00460         /** Write the current data item. The type <TT>V</TT> of the passed
00461             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00462             In case of a conversion floating point -> intergral this includes rounding and clipping.
00463         */
00464     template <class V, class ITERATOR>
00465     void set(V value, ITERATOR const & i) const 
00466     { 
00467         (*i)[index_] = detail::RequiresExplicitCast<value_type>::cast(value); 
00468     }
00469     
00470         /** Write the data item at an offset (can be 1D or 2D or higher order difference)..
00471             The type <TT>V</TT> of the passed
00472             in <TT>value</TT> is automatically converted to <TT>value_type</TT>.
00473             In case of a conversion floating point -> intergral this includes rounding and clipping.
00474         */
00475     template <class V, class ITERATOR, class DIFFERENCE>
00476     void set(V value, ITERATOR const & i, DIFFERENCE const & diff) const 
00477     { 
00478         i[diff][index_]= detail::RequiresExplicitCast<value_type>::cast(value); 
00479     }
00480 };
00481 
00482 /********************************************************/
00483 /*                                                      */
00484 /*                    SequenceAccessor                  */
00485 /*                                                      */
00486 /********************************************************/
00487 
00488 /** \brief Accessor for items that are STL compatible sequences.
00489 
00490     It encapsulates access to the sequences' begin() and end()
00491     functions.
00492     
00493     <b>Usage:</b>
00494 
00495     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>
00496     Namespace: vigra
00497     
00498     \code
00499     typedef std::list<std::list<int> > ListOfLists;
00500     
00501     ListOfLists ll;
00502     ...
00503     
00504     typedef vigra::SequenceAccessor<ListOfLists::value_type> ListOfListsAccessor;
00505     ListOfListsAccessor a;
00506     for(ListOfLists::iterator li = ll.begin(); li != ll.end(); ++li) 
00507     {
00508         for(ListOfListsAccessor::iterator i = a.begin(li); i != a.end(li); ++i) 
00509         {
00510             *i = 10;
00511         }
00512     }
00513     \endcode
00514 */
00515 template <class SEQUENCE>
00516 class SequenceAccessor
00517 : public StandardAccessor<SEQUENCE>
00518 {
00519   public:
00520     /** the sequence's value_type
00521     */
00522     typedef typename SEQUENCE::value_type component_type;
00523 
00524     /** the sequence's iterator type
00525     */
00526     typedef typename SEQUENCE::iterator iterator;
00527     
00528     /** get begin iterator for sequence at given iterator position
00529     */
00530     template <class ITERATOR>
00531     iterator begin(ITERATOR const & i) const
00532     { 
00533         return (*i).begin(); 
00534     }
00535     
00536     /** get end iterator for sequence at given iterator position
00537     */
00538     template <class ITERATOR>
00539     iterator end(ITERATOR const & i)  const
00540     {
00541          return (*i).end(); 
00542     }
00543     
00544     /** get begin iterator for sequence at an offset
00545         of given iterator position
00546     */
00547     template <class ITERATOR, class DIFFERENCE>
00548     iterator begin(ITERATOR const & i, DIFFERENCE const & diff)  const
00549     { 
00550         return i[diff].begin(); 
00551     }
00552     
00553     /** get end iterator for sequence at a 2D difference vector
00554         of given iterator position
00555     */
00556     template <class ITERATOR, class DIFFERENCE>
00557     iterator end(ITERATOR const & i, DIFFERENCE const & diff)  const
00558     { 
00559         return i[diff].end(); 
00560     }
00561 
00562     /** get size of sequence at given iterator position
00563     */
00564     template <class ITERATOR>
00565     int size(ITERATOR const & i) const { return (*i).size(); }
00566 
00567     /** get size of sequence at 2D difference vector of given iterator position
00568     */
00569     template <class ITERATOR, class DIFFERENCE>
00570     int size(ITERATOR const & i, DIFFERENCE const & diff) const
00571     { return i[diff].size(); }
00572 };
00573 
00574 /********************************************************/
00575 /*                                                      */
00576 /*                     VectorAccessor                   */
00577 /*                                                      */
00578 /********************************************************/
00579 
00580 /** \brief Accessor for items that are STL compatible vectors.
00581 
00582     It encapsulates access to a vector's access functionality.
00583     
00584     <b> Usage:</b>
00585     
00586     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>    
00587     Namespace: vigra
00588         
00589     The accessor has two modes of operation:
00590     
00591     <ol>
00592     <li> Access the vector's iterator via the <TT>begin()</TT> and <TT>end()</TT>
00593     functions:
00594     
00595     \code
00596     typedef std::list<std::vector<int> > ListOfVectors;
00597     
00598     ListOfVectors ll;
00599     ...
00600     
00601     typedef vigra::SequenceAccessor<ListOfVectors::value_type> ListOfVectorsAccessor;
00602     ListOfVectorsAccessor a;
00603     for(ListOfVectors::iterator li = ll.begin(); li != ll.end(); ++li) 
00604     {
00605         for(ListOfVectorsAccessor::iterator i = a.begin(li); i != a.end(li); ++i) 
00606         {
00607             *i = 10;
00608         }
00609     }
00610     \endcode
00611     <li> Access the vector's components via an index (internally calls 
00612     the vector's <TT>operator[]</TT> ):
00613     \code
00614     typedef std::list<std::vector<int> > ListOfVectors;
00615     
00616     ListOfVectors ll;
00617     ...
00618     
00619     typedef vigra::SequenceAccessor<ListOfVectors::value_type> ListOfVectorsAccessor;
00620     ListOfVectorsAccessor a;
00621     for(ListOfVectors::iterator li = ll.begin(); li != ll.end(); ++li) 
00622     {
00623         for(int i = 0; i != a.size(li); ++i) 
00624         {
00625             a.setComponent(10, li, i);
00626         }
00627     }
00628     \endcode
00629     </ol>
00630     
00631     <b> Required Interface:</b>
00632     
00633     \code
00634     VECTOR v;
00635     VECTOR::iterator i;
00636     value_type d;
00637     int index;
00638     
00639     d = v[index];
00640     v[index] = d;
00641     i = v.begin();
00642     i = v.end();
00643     v.size();
00644     \endcode
00645 */
00646 template <class VECTOR>
00647 class VectorAccessor
00648 : public SequenceAccessor<VECTOR>
00649 {
00650   public:
00651         /** the vector's value_type
00652         */
00653     typedef typename VECTOR::value_type component_type;
00654 
00655         /** Read the component data at given vector index
00656             at given iterator position 
00657         */
00658     template <class ITERATOR>
00659     component_type const & getComponent(ITERATOR const & i, int idx) const 
00660     { 
00661         return (*i)[idx]; 
00662     }
00663     
00664         /** Set the component data at given vector index
00665             at given iterator position. The type <TT>V</TT> of the passed
00666             in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00667             In case of a conversion floating point -> intergral this includes rounding and clipping.
00668         */
00669     template <class V, class ITERATOR>
00670     void setComponent(V const & value, ITERATOR const & i, int idx) const
00671     { 
00672         (*i)[idx] = detail::RequiresExplicitCast<component_type>::cast(value); 
00673     }
00674     
00675         /** Read the component data at given vector index
00676             at an offset of given iterator position
00677         */
00678     template <class ITERATOR, class DIFFERENCE>
00679     component_type const & getComponent(ITERATOR const & i, DIFFERENCE const & diff, int idx) const
00680     { 
00681         return i[diff][idx]; 
00682     }
00683     
00684     /** Set the component data at given vector index
00685         at an offset of given iterator position. The type <TT>V</TT> of the passed
00686         in <TT>value</TT> is automatically converted to <TT>component_type</TT>.
00687             In case of a conversion floating point -> intergral this includes rounding and clipping.
00688     */
00689     template <class V, class ITERATOR, class DIFFERENCE>
00690     void 
00691     setComponent(V const & value, ITERATOR const & i, DIFFERENCE const & diff, int idx) const 
00692     { 
00693         i[diff][idx] = detail::RequiresExplicitCast<component_type>::cast(value); 
00694     }
00695 };
00696 
00697 
00698 /********************************************************/
00699 /*                                                      */
00700 /*                 MultiImageAccessor2                  */
00701 /*                                                      */
00702 /********************************************************/
00703 
00704 /** \brief Access two images simultaneously.
00705 
00706     This accessor is used when two images need to be treated as one
00707     because an algorithm accepts only one image. For example, 
00708     \ref seededRegionGrowing() uses only one image two calculate
00709     the cost for aggregating each pixel into a region. Somtimes, we
00710     need more information to calcuate this cost, for example gray value
00711     and local gradient magnitude. These values can be stored in two images,
00712     which appear as only one when we pass a <TT>MultiImageAccessor2</TT> to
00713     the lagorithms. Of course, the cost functor must accept a <TT>pair</TT> 
00714     of values for this to work. Instead of an actual image iterator, we
00715     pass a <a href="CoordinateIterator.html">CoordinateIterator</a> which 
00716     selects the right pixels form both images.
00717     
00718     <b> Usage:</b>
00719 
00720     <b>\#include</b> "<a href="accessor_8hxx-source.html">vigra/accessor.hxx</a>"<br>    
00721     Namespace: vigra
00722     
00723     \code
00724     using namespace vigra;
00725     
00726     FImage gray_values(w,h), gradient_magnitude(w,h);
00727     IImage seeds(w,h), labels(w,h);
00728     
00729     seededRegionGrowing(
00730         srcIterRange(CoordinateIterator(), CoordinateIterator(w,h),
00731            MultiImageAccessor2<FImage::iterator, FImage::Accessor,
00732                                FImage::iterator, FImage::Accessor>
00733                               (gray_values.upperLeft(), gray_values.accessor(),
00734                                gradient_magnitude.upperLeft(), gradient_magnitude.accessor())), 
00735         srcImage(seeds), 
00736         destImage(labels), 
00737         SomeCostFunctor());       
00738     \endcode   
00739 */
00740 
00741 template <class Iter1, class Acc1, class Iter2, class Acc2>
00742 class MultiImageAccessor2
00743 {
00744   public:
00745         /** The accessors value_type: construct a pair that contains
00746             the corresponding image values.
00747         */
00748     typedef pair<typename Acc1::value_type, typename Acc2::value_type>
00749             value_type;
00750     
00751         /** Construct from two image iterators and associated accessors.
00752         */
00753     MultiImageAccessor2(Iter1 i1, Acc1 a1, Iter2 i2, Acc2 a2)
00754     : i1_(i1), a1_(a1), i2_(i2), a2_(a2)
00755     {}
00756 
00757         /** read the current data item
00758         */
00759     template <class DIFFERENCE>
00760     value_type operator()(DIFFERENCE const & d) const
00761     { 
00762         return std::make_pair(a1_(i1_, d), a2_(i2_, i.x, i.y)); 
00763     }
00764     
00765         /** read the data item at an offset
00766         */
00767     template <class DIFFERENCE1, class DIFFERENCE2>
00768     value_type operator()(DIFFERENCE1 const & d1, DIFFERENCE2 const & d2) const
00769     { 
00770         d2 += d1;
00771         return std::make_pair(a1_(i1_, d2), a2_(i2_, d2)); 
00772     }
00773     
00774   private:
00775     Iter1 i1_;
00776     Acc1 a1_;
00777     Iter2 i2_;
00778     Acc2 a2_;
00779 };
00780 
00781 //@}
00782 
00783 } // namespace vigra
00784 
00785 #endif // VIGRA_ACCESSOR_HXX

© Ullrich Köthe (koethe@informatik.uni-hamburg.de)
Cognitive Systems Group, University of Hamburg, Germany

html generated using doxygen and Python
VIGRA 1.2.0 (7 Aug 2003)