last update 20 Sep 2009 |
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 cvrSimilarityTransformation2D.h 00041 * Contains the class cvr::similarityTransformation2D, 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: cvrSimilarityTransformation2D.h,v $ 00049 */ 00050 00051 #ifndef _CVR_SIMILARITY_TRANSFORMATION2_D_H_ 00052 #define _CVR_SIMILARITY_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 similarityTransformation2D 00068 * 00069 * The similarity transformation for 2D spaces performs a planar 00070 * modification of points in which only rotation, translation and 00071 * scaling 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 similarityTransformation2D::parameters 00083 * @see euclideanTransformation2D 00084 * @see euclideanTransformation3D 00085 * @see affineTransformation2D 00086 * @see projectiveTransformation2D 00087 * 00088 * @ingroup gGeometricTrans 00089 */ 00090 class similarityTransformation2D : public functor { 00091 public: 00092 /** 00093 * The parameters for the class similarityTransformation2D 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 * Scaling 00183 * 00184 * Default value: 1.0f (no scaling) 00185 */ 00186 float scaling; 00187 00188 /** 00189 * Parameters used in the linearLeastSquares functor for 00190 * the estimation tasks. 00191 * 00192 * Default value: default 00193 */ 00194 linearLeastSquares::parameters llsParameters; 00195 }; 00196 00197 /** 00198 * Default constructor 00199 */ 00200 similarityTransformation2D(); 00201 00202 /** 00203 * Construct a functor using the given parameters 00204 */ 00205 similarityTransformation2D(const parameters& par); 00206 00207 /** 00208 * Copy constructor 00209 * @param other the object to be copied 00210 */ 00211 similarityTransformation2D(const similarityTransformation2D& other); 00212 00213 /** 00214 * Destructor 00215 */ 00216 virtual ~similarityTransformation2D(); 00217 00218 /** 00219 * Transform the given point. 00220 * 00221 * @param srcdest point<T> with the source data. The result 00222 * will be left here too. 00223 * @return true if apply successful or false otherwise. 00224 */ 00225 template<class P> 00226 bool apply(P& srcdest) const; 00227 00228 /** 00229 * Transform the point \a src and write the transformation on the \a dest 00230 * point. 00231 * 00232 * @param src point<T> with the source data. 00233 * @param dest point<T> where the result will be left. 00234 * @return true if apply successful or false otherwise. 00235 */ 00236 template<class P> 00237 bool apply(const P& src, P& dest) const; 00238 00239 /** 00240 * Estimate a transformation that maps the first set of points into 00241 * the second set of points. 00242 * 00243 * The resulting estimation is left in the parameters. 00244 * 00245 * This method assumes that the corresponding point to setA[idx] is 00246 * stored in setB[idx]. 00247 * 00248 * Only the first \c dof()/2 correspondences will be used for the 00249 * estimation, and you need at least \c dof()/2 elements for the 00250 * transformation to be computed. 00251 */ 00252 template<class P> 00253 bool estimate(const std::vector<P>& setA, 00254 const std::vector<P>& setB); 00255 00256 00257 /** 00258 * Do a linear least squares error minimization for a transformation that 00259 * maps the first set of points into the second set of points. 00260 * 00261 * The resulting estimation is left in the parameters. 00262 * 00263 * This method assumes that the corresponding point to setA[idx] is 00264 * stored in setB[idx]. 00265 * 00266 * For the LLS to be computed, more than \c dof()/2 correspondences are 00267 * necessary for the estimation. 00268 */ 00269 template<class P> 00270 bool estimateLLS(const std::vector<P>& setA, 00271 const std::vector<P>& setB); 00272 00273 /** 00274 * Do a linear least squares error minimization for a transformation that 00275 * maps the first set of points into the second set of points. 00276 * 00277 * The resulting estimation is left in the parameters. 00278 * 00279 * This method assumes that the corresponding point to setA[idx] is 00280 * stored in setB[idx]. 00281 * 00282 * For the LLS to be computed, more than \c dof()/2 correspondences are 00283 * necessary for the estimation. 00284 * 00285 * For the estimation, just the points with the indices specified in 00286 * \c selection are given. 00287 * 00288 * @return true if successful, false otherwise 00289 */ 00290 template<class P> 00291 bool estimateLLS(const ivector& selection, 00292 const std::vector<P>& setA, 00293 const std::vector<P>& setB); 00294 00295 00296 /** 00297 * Degrees of freedom of the transformation. 00298 * 00299 * Usually this means the minimal number of parameters with which the 00300 * transforamtion matrix can be generated. 00301 */ 00302 int dof() const; 00303 00304 /** 00305 * Generate the transformation matrix that would be required by 00306 * cvr::matrixTransform to generate the same coordinate transformation. 00307 * The result is, so to speak, the matrix representation of the parameters. 00308 * 00309 * @param mat matrix container of the result. 00310 */ 00311 void generateMatrix(fmatrix& mat) const; 00312 00313 /** 00314 * Generate the transformation matrix that would be required by 00315 * cvr::matrixTransform to generate the same coordinate transformation. 00316 * The result is, so to speak, the matrix representation of the parameters. 00317 */ 00318 fmatrix generateMatrix() const; 00319 00320 /** 00321 * Copy data of "other" functor. 00322 * @param other the functor to be copied 00323 * @return a reference to this functor object 00324 */ 00325 similarityTransformation2D& copy(const similarityTransformation2D& other); 00326 00327 /** 00328 * Alias for copy member 00329 * @param other the functor to be copied 00330 * @return a reference to this functor object 00331 */ 00332 similarityTransformation2D& 00333 operator=(const similarityTransformation2D& other); 00334 00335 /** 00336 * Returns the complete name of the functor class 00337 */ 00338 virtual const std::string& name() const; 00339 00340 /** 00341 * Returns a pointer to a clone of this functor. 00342 */ 00343 virtual similarityTransformation2D* clone() const; 00344 00345 /** 00346 * Returns a pointer to a new instance of this functor. 00347 */ 00348 virtual similarityTransformation2D* newInstance() const; 00349 00350 /** 00351 * Returns used parameters 00352 */ 00353 const parameters& getParameters() const; 00354 00355 /** 00356 * Update parameters 00357 */ 00358 bool updateParameters(); 00359 00360 protected: 00361 00362 /** 00363 * Returns used parameters 00364 */ 00365 parameters& getRWParameters(); 00366 00367 /** 00368 * Cosine of the rotation angle times the scaling factor 00369 */ 00370 float scosa_; 00371 00372 /** 00373 * Sine of the rotation angle times the scaling factor 00374 */ 00375 float ssina_; 00376 00377 /** 00378 * Shadow of the translation point 00379 */ 00380 fpoint trans_; 00381 00382 /** 00383 * Singular value decomposition 00384 * 00385 * Convenience method to avoid the SVD to be present in the template 00386 * implementation 00387 */ 00388 linearLeastSquares lls_; 00389 }; 00390 } 00391 00392 #include "cvrSimilarityTransformation2D_template.h" 00393 00394 #endif 00395