CVR-Lib last update 20 Sep 2009

cvrEuclideanTransformation2D.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008
00003  * Pablo Alvarado
00004  *
00005  * This file is part of the Computer Vision and Robotics Library (CVR-Lib )
00006  *
00007  * The CVR-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the BSD License.
00009  *
00010  * All rights reserved.
00011  *
00012  * Redistribution and use in source and binary forms, with or without
00013  * modification, are permitted provided that the following conditions are met:
00014  *
00015  * 1. Redistributions of source code must retain the above copyright notice,
00016  *    this list of conditions and the following disclaimer.
00017  *
00018  * 2. Redistributions in binary form must reproduce the above copyright notice,
00019  *    this list of conditions and the following disclaimer in the documentation
00020  *    and/or other materials provided with the distribution.
00021  *
00022  * 3. Neither the name of the authors nor the names of its contributors may be
00023  *    used to endorse or promote products derived from this software without
00024  *    specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 /**
00040  * \file   cvrEuclideanTransformation2D.h
00041  *         Contains the class cvr::euclideanTransformation2D,
00042  *         which is used to transform coordinate points in a planar
00043  *         transformation with only rotation and translation.
00044  *
00045  * \author Pablo Alvarado
00046  * \date   15.01.2008
00047  *
00048  * revisions ..: $Id: cvrEuclideanTransformation2D.h,v $
00049  */
00050 
00051 #ifndef _CVR_EUCLIDEAN_TRANSFORMATION2_D_H_
00052 #define _CVR_EUCLIDEAN_TRANSFORMATION2_D_H_
00053 
00054 
00055 #include "cvrLocation.h"
00056 #include "cvrVector.h"
00057 #include "cvrMatrix.h"
00058 #include "cvrFunctor.h"
00059 #include "cvrSinCos.h"
00060 #include "cvrLinearLeastSquares.h"
00061 #include <vector>
00062 #include <limits>
00063 
00064 namespace cvr {
00065 
00066   /**
00067    * Class euclideanTransformation2D
00068    *
00069    * The euclidean transformation (also called isometric transformation) for 2D
00070    * spaces performs a planar modification of points in which only rotation and
00071    * translation are applied.
00072    *
00073    * This class transforms points only, and therefore is not intended for
00074    * images, in which case you should use cvr::matrixTransform.  You can
00075    * however obtain from this class the matrix used by
00076    * cvr::matrixTransform::parameters to generate the same results.
00077    *
00078    * Most apply methods are templates of a point type P, i.e. the type P is
00079    * usually a container that can be accessed with the operator[], like
00080    * the types cvr::point<T>, cvr::vector<T>.
00081    *
00082    * @see euclideanTransformation2D::parameters.
00083    * @see euclideanTransformation3D
00084    * @see similarityTransformation2D
00085    * @see affineTransformation2D
00086    * @see projectiveTransformation2D
00087    *
00088    * @ingroup gGeometricTrans
00089    */
00090   class euclideanTransformation2D : public functor {
00091   public:
00092     /**
00093      * The parameters for the class euclideanTransformation2D
00094      */
00095     class parameters : public functor::parameters {
00096     public:
00097       /**
00098        * Default constructor
00099        */
00100       parameters();
00101 
00102       /**
00103        * Copy constructor
00104        * @param other the parameters object to be copied
00105        */
00106       parameters(const parameters& other);
00107 
00108       /**
00109        * Destructor
00110        */
00111       ~parameters();
00112 
00113       /**
00114        * Copy the contents of a parameters object
00115        * @param other the parameters object to be copied
00116        * @return a reference to this parameters object
00117        */
00118       parameters& copy(const parameters& other);
00119 
00120       /**
00121        * Copy the contents of a parameters object
00122        * @param other the parameters object to be copied
00123        * @return a reference to this parameters object
00124        */
00125       parameters& operator=(const parameters& other);
00126 
00127       /**
00128        * Returns the complete name of the parameters class.
00129        */
00130       virtual const std::string& name() const;
00131 
00132       /**
00133        * Returns a pointer to a clone of the parameters.
00134        */
00135       virtual parameters* clone() const;
00136 
00137       /**
00138        * Returns a pointer to a new instance of the parameters.
00139        */
00140       virtual parameters* newInstance() const;
00141 
00142       /**
00143        * Write the parameters in the given ioHandler
00144        * @param handler the ioHandler to be used
00145        * @param complete if true (the default) the enclosing begin/end will
00146        *        be also written, otherwise only the data block will be written.
00147        * @return true if write was successful
00148        */
00149       virtual bool write(ioHandler& handler,const bool complete=true) const;
00150 
00151       /**
00152        * Read the parameters from the given ioHandler
00153        * @param handler the ioHandler to be used
00154        * @param complete if true (the default) the enclosing begin/end will
00155        *        be also written, otherwise only the data block will be written.
00156        * @return true if write was successful
00157        */
00158       virtual bool read(ioHandler& handler,const bool complete=true);
00159 
00160       // ------------------------------------------------
00161       // the parameters
00162       // ------------------------------------------------
00163 
00164       /**
00165        * Magnitude of the translation in x and y directions
00166        *
00167        * Default value: 0
00168        */
00169       fpoint translation;
00170 
00171       /**
00172        * Magnitude of the rotation, provided in radians.
00173        *
00174        * If the given value is greater than 2Pi then it is assumed to be given
00175        * in degrees.
00176        *
00177        * Default value: 0
00178        */
00179       float angle;
00180 
00181       /**
00182        * Parameters used in the linearLeastSquares functor for
00183        * the estimation tasks.
00184        *
00185        * Default value: default
00186        */
00187       linearLeastSquares::parameters llsParameters;
00188     };
00189 
00190     /**
00191      * Default constructor
00192      */
00193     euclideanTransformation2D();
00194 
00195     /**
00196      * Construct a functor using the given parameters
00197      */
00198     euclideanTransformation2D(const parameters& par);
00199 
00200     /**
00201      * Copy constructor
00202      * @param other the object to be copied
00203      */
00204     euclideanTransformation2D(const euclideanTransformation2D& other);
00205 
00206     /**
00207      * Destructor
00208      */
00209     virtual ~euclideanTransformation2D();
00210 
00211     /**
00212      * Transform the given point.
00213      *
00214      * @param srcdest point<T> with the source data. The result
00215      *                will be left here too.
00216      * @return true if apply successful or false otherwise.
00217      */
00218     template<class P>
00219     bool apply(P& srcdest) const;
00220 
00221     /**
00222      * Transform the point \a src and write the transformation on the \a dest
00223      * point.
00224      *
00225      * @param src point<T> with the source data.
00226      * @param dest point<T> where the result will be left.
00227      * @return true if apply successful or false otherwise.
00228      */
00229     template<class P>
00230     bool apply(const P& src, P& dest) const;
00231 
00232     /**
00233      * Estimate a transformation that maps the first set of points into
00234      * the second set of points.
00235      *
00236      * The resulting estimation is left in the parameters.
00237      *
00238      * This method assumes that the corresponding point to setA[idx] is
00239      * stored in setB[idx].
00240      *
00241      * Only the first \c dof()/2 correspondences will be used for the
00242      * estimation, and you need at least \c dof()/2 elements for the
00243      * transformation to be computed.
00244      */
00245     template<class P>
00246     bool estimate(const std::vector<P>& setA,
00247                   const std::vector<P>& setB);
00248 
00249 
00250     /**
00251      * Do a linear least squares error minimization for a transformation that
00252      * maps the first set of points into the second set of points.
00253      *
00254      * The resulting estimation is left in the parameters.
00255      *
00256      * This method assumes that the corresponding point to setA[idx] is
00257      * stored in setB[idx].
00258      *
00259      * For the LLS to be computed, more than \c dof()/2 correspondences are
00260      * necessary for the estimation.
00261      */
00262     template<class P>
00263     bool estimateLLS(const std::vector<P>& setA,
00264                      const std::vector<P>& setB);
00265 
00266 
00267     /**
00268      * Do a linear least squares error minimization for a transformation that
00269      * maps the first set of points into the second set of points.
00270      *
00271      * The resulting estimation is left in the parameters.
00272      *
00273      * This method assumes that the corresponding point to setA[idx] is
00274      * stored in setB[idx].
00275      *
00276      * For the LLS to be computed, more than \c dof()/2 correspondences are
00277      * necessary for the estimation.
00278      *
00279      * For the estimation, just the points with the indices specified in 
00280      * \c selection are given.
00281      *
00282      * @return true if successful, false otherwise
00283      */
00284     template<class P>
00285     bool estimateLLS(const ivector& selection,
00286                      const std::vector<P>& setA,
00287                      const std::vector<P>& setB);
00288 
00289     /**
00290      * Degrees of freedom of the transformation.
00291      *
00292      * Usually this means the minimal number of parameters with which the
00293      * transforamtion matrix can be generated.
00294      */
00295     int dof() const;
00296 
00297     /**
00298      * Generate the transformation matrix that would be required by
00299      * cvr::matrixTransform to generate the same coordinate transformation.
00300      * The result is, so to speak, the matrix representation of the parameters.
00301      *
00302      * @param mat matrix container of the result.
00303      */
00304     void generateMatrix(fmatrix& mat) const;
00305 
00306     /**
00307      * Generate the transformation matrix that would be required by
00308      * cvr::matrixTransform to generate the same coordinate transformation.
00309      * The result is, so to speak, the matrix representation of the parameters.
00310      */
00311     fmatrix generateMatrix() const;
00312 
00313     /**
00314      * Copy data of "other" functor.
00315      * @param other the functor to be copied
00316      * @return a reference to this functor object
00317      */
00318     euclideanTransformation2D& copy(const euclideanTransformation2D& other);
00319 
00320     /**
00321      * Alias for copy member
00322      * @param other the functor to be copied
00323      * @return a reference to this functor object
00324      */
00325     euclideanTransformation2D&
00326     operator=(const euclideanTransformation2D& other);
00327 
00328     /**
00329      * Returns the complete name of the functor class
00330      */
00331     virtual const std::string& name() const;
00332 
00333     /**
00334      * Returns a pointer to a clone of this functor.
00335      */
00336     virtual euclideanTransformation2D* clone() const;
00337 
00338     /**
00339      * Returns a pointer to a new instance of this functor.
00340      */
00341     virtual euclideanTransformation2D* newInstance() const;
00342 
00343     /**
00344      * Returns used parameters
00345      */
00346     const parameters& getParameters() const;
00347 
00348     /**
00349      * Update parameters
00350      */
00351     bool updateParameters();
00352 
00353   protected:
00354 
00355     /**
00356      * Returns used parameters
00357      */
00358     parameters& getRWParameters();
00359 
00360     /**
00361      * Cosine of the rotation angle
00362      */
00363     float cosa_;
00364 
00365     /**
00366      * Sine of the rotation angle
00367      */
00368     float sina_;
00369 
00370     /**
00371      * Shadow of the translation point
00372      */
00373     fpoint trans_;
00374 
00375     /**
00376      * Singular value decomposition
00377      *
00378      * Convenience method to avoid the SVD to be present in the template
00379      * implementation
00380      */
00381     linearLeastSquares lls_;
00382   };
00383 }
00384 
00385 #include "cvrEuclideanTransformation2D_template.h"
00386 
00387 #endif
00388 

Generated on Sun Sep 20 22:07:59 2009 for CVR-Lib by Doxygen 1.5.8