similarity.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *   
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 /****************************************************************************
00024   History
00025 
00026 $Log: not supported by cvs2svn $
00027 Revision 1.16  2008/02/22 17:40:27  ponchio
00028 Fixed determinantt problem and quaternion problem.
00029 
00030 Revision 1.15  2008/02/21 11:34:08  ponchio
00031 refixed bug in FromMatrix
00032 
00033 Revision 1.14  2008/02/21 10:57:59  ponchio
00034 fixed bug in FromMatrix
00035 
00036 Revision 1.13  2008/02/21 10:30:18  benedetti
00037 corrected bug in FromMatrix
00038 
00039 Revision 1.12  2007/02/05 14:17:48  corsini
00040 add new ctor (build similarity from euler angles)
00041 
00042 Revision 1.11  2004/12/15 18:45:50  tommyfranken
00043 *** empty log message ***
00044 
00045 Revision 1.10  2004/10/07 13:55:47  ganovelli
00046 templated on the kind of class used to implement rotation
00047 (default is QUternion but it can be Matrix44 as well)
00048 
00049 Revision 1.9  2004/06/04 13:35:07  cignoni
00050 added InverseMatrix,
00051 
00052 Revision 1.8  2004/05/07 10:09:13  cignoni
00053 missing final newline
00054 
00055 Revision 1.7  2004/05/04 23:23:45  cignoni
00056 unified to the gl stlyle matix*vector. removed vector*matrix operator
00057 
00058 Revision 1.6  2004/03/25 14:57:49  ponchio
00059 Microerror. ($LOG$ -> $Log: not supported by cvs2svn $
00060 Microerror. ($LOG$ -> Revision 1.16  2008/02/22 17:40:27  ponchio
00061 Microerror. ($LOG$ -> Fixed determinantt problem and quaternion problem.
00062 Microerror. ($LOG$ ->
00063 Microerror. ($LOG$ -> Revision 1.15  2008/02/21 11:34:08  ponchio
00064 Microerror. ($LOG$ -> refixed bug in FromMatrix
00065 Microerror. ($LOG$ ->
00066 Microerror. ($LOG$ -> Revision 1.14  2008/02/21 10:57:59  ponchio
00067 Microerror. ($LOG$ -> fixed bug in FromMatrix
00068 Microerror. ($LOG$ ->
00069 Microerror. ($LOG$ -> Revision 1.13  2008/02/21 10:30:18  benedetti
00070 Microerror. ($LOG$ -> corrected bug in FromMatrix
00071 Microerror. ($LOG$ ->
00072 Microerror. ($LOG$ -> Revision 1.12  2007/02/05 14:17:48  corsini
00073 Microerror. ($LOG$ -> add new ctor (build similarity from euler angles)
00074 Microerror. ($LOG$ ->
00075 Microerror. ($LOG$ -> Revision 1.11  2004/12/15 18:45:50  tommyfranken
00076 Microerror. ($LOG$ -> *** empty log message ***
00077 Microerror. ($LOG$ ->
00078 Microerror. ($LOG$ -> Revision 1.10  2004/10/07 13:55:47  ganovelli
00079 Microerror. ($LOG$ -> templated on the kind of class used to implement rotation
00080 Microerror. ($LOG$ -> (default is QUternion but it can be Matrix44 as well)
00081 Microerror. ($LOG$ ->
00082 Microerror. ($LOG$ -> Revision 1.9  2004/06/04 13:35:07  cignoni
00083 Microerror. ($LOG$ -> added InverseMatrix,
00084 Microerror. ($LOG$ ->
00085 Microerror. ($LOG$ -> Revision 1.8  2004/05/07 10:09:13  cignoni
00086 Microerror. ($LOG$ -> missing final newline
00087 Microerror. ($LOG$ ->
00088 Microerror. ($LOG$ -> Revision 1.7  2004/05/04 23:23:45  cignoni
00089 Microerror. ($LOG$ -> unified to the gl stlyle matix*vector. removed vector*matrix operator
00090 Microerror. ($LOG$ ->
00091 
00092 
00093 ****************************************************************************/
00094 
00095 #ifndef SIMILARITY_H
00096 #define SIMILARITY_H
00097 
00098 #include <vcg/math/quaternion.h>
00099 #include <vcg/math/matrix44.h>
00100 
00101 namespace vcg {
00102 
00103 template <class S,class RotationType = Quaternion<S> > class Similarity {
00104 public:
00105   Similarity() {}
00106   Similarity(const RotationType &q) { SetRotate(q); }
00107   Similarity(const Point3<S> &p) { SetTranslate(p); }
00108   Similarity(S s) { SetScale(s); }
00109         Similarity(S alpha, S beta, S gamma)
00110         {
00111                 rot.FromEulerAngles(alpha, beta, gamma);
00112                 tra = Point3<S>(0, 0, 0);
00113                 sca = 1;
00114         }
00115   
00116   Similarity operator*(const Similarity &affine) const;
00117   Similarity &operator*=(const Similarity &affine);
00118   //Point3<S> operator*(const Point3<S> &p) const;
00119   
00120   
00121   Similarity &SetIdentity();
00122   Similarity &SetScale(const S s);
00123         Similarity &SetTranslate(const Point3<S> &t);   
00125   Similarity &SetRotate(S angle, const Point3<S> & axis); 
00126   Similarity &SetRotate(const RotationType &q);
00127 
00128   Matrix44<S> Matrix() const;
00129   Matrix44<S> InverseMatrix() const;
00130   void FromMatrix(const Matrix44<S> &m);
00131 
00132   RotationType rot;
00133   Point3<S> tra;
00134   S sca;  
00135 };
00136 
00137 
00138 
00139 template <class S,class RotationType> Similarity<S,RotationType> &Invert(Similarity<S,RotationType> &m);
00140 template <class S,class RotationType> Similarity<S,RotationType> Inverse(const Similarity<S,RotationType> &m);
00141 template <class S,class RotationType> Point3<S> operator*(const Similarity<S,RotationType> &m, const Point3<S> &p);
00142 
00143 
00144 template <class S,class RotationType> Similarity<S,RotationType> Similarity<S,RotationType>::operator*(const Similarity &a) const {
00145   Similarity<S,RotationType> r;
00146   r.rot = rot * a.rot;
00147   r.sca = sca * a.sca;
00148   r.tra = (rot.Rotate(a.tra)) * sca + tra;
00149   return r;
00150 }
00151 
00152 template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::operator*=(const Similarity &a) {  
00153   rot = rot * a.rot;
00154   sca = sca * a.sca;
00155   tra = (rot.Rotate(a.tra)) * sca + tra;
00156   return *this;
00157 }
00158   
00159 template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetIdentity() {
00160   rot.SetIdentity();
00161   tra = Point3<S>(0, 0, 0);
00162   sca = 1;
00163   return *this;
00164 }
00165 
00166 template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetScale(const S s) {
00167   SetIdentity();
00168   sca = s;
00169   return *this;
00170 }
00171 
00172 template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetTranslate(const Point3<S> &t) {
00173   SetIdentity();
00174   tra = t;
00175   return *this;
00176 }
00177 
00178 template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetRotate(S angle, const Point3<S> &axis) {
00179   SetIdentity();
00180   rot.FromAxis(angle, axis);
00181   return *this;
00182 }
00183 
00184 template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetRotate(const RotationType &q) {
00185   SetIdentity();
00186   rot = q;  
00187   return *this;
00188 }
00189 
00190 
00191 template <class S,class RotationType> Matrix44<S> Similarity<S,RotationType>::Matrix() const {
00192   Matrix44<S> r;
00193   rot.ToMatrix(r);
00194   Matrix44<S> s = Matrix44<S>().SetScale(sca, sca, sca);
00195   Matrix44<S> t = Matrix44<S>().SetTranslate(tra[0], tra[1], tra[2]);
00196   return Matrix44<S>(s*r*t);  // trans * scale * rot;
00197 }
00198 
00199 template <class S,class RotationType> Matrix44<S> Similarity<S,RotationType>::InverseMatrix() const {
00200   return Inverse(Matrix());
00201 }
00202 
00203 
00204 template <class S,class RotationType> void Similarity<S,RotationType>::FromMatrix(const Matrix44<S> &m) {
00205  //Computes a t*s*r decomposition
00206   S det = m.Determinant();
00207   assert(det > 0);
00208   sca = (S)pow((S)det, (S)(1/3.0));  
00209   Matrix44<S> t = m*Matrix44<S>().SetScale(1/sca, 1/sca, 1/sca);
00210   tra[0] = t.ElementAt(0, 3);t[0][3] = 0.0;
00211   tra[1] = t.ElementAt(1, 3);t[1][3] = 0.0;
00212   tra[2] = t.ElementAt(2, 3);t[2][3] = 0.0;
00213   rot.FromMatrix(t);
00214 
00215         Invert(t);      
00216         tra = t * tra;
00217         tra/= sca;
00218 }
00219 
00220 
00221 template <class S,class RotationType> Similarity<S,RotationType> &Invert(Similarity<S,RotationType> &a) {  
00222   a.rot.Invert();
00223   a.sca = 1/a.sca;
00224   a.tra = a.rot.Rotate(-a.tra)*a.sca;
00225   return a;
00226 }
00227 
00228 template <class S,class RotationType> Similarity<S,RotationType> Inverse(const Similarity<S,RotationType> &m) {
00229   Similarity<S,RotationType> a = m;
00230   return Invert(a);
00231 }
00232 
00233 
00234 template <class S,class RotationType> Similarity<S,RotationType> Interpolate(const Similarity<S,RotationType> &a, const Similarity<S,RotationType> &b, const S t) {
00235   Similarity<S,RotationType> r;
00236   r.rot = Interpolate(a.rot, b.rot, t);
00237   r.tra = a.tra * t + b.tra * (1-t);
00238   r.sca = a.sca * t + b.sca * (1-t);
00239   return r;
00240 }
00241 
00242 template <class S,class RotationType> Point3<S> operator*(const Similarity<S,RotationType> &m, const Point3<S> &p) {
00243   Matrix44<S> t;
00244   m.rot.ToMatrix(t);  
00245   Point3<S> r = t*p;
00246   r *= m.sca;
00247   r += m.tra;
00248   return r;
00249 }
00250 
00251 
00252 //typedef Similarity<float> Similarityf;
00253 //typedef Similarity<double>Similarityd;
00254 
00255 class Similarityf:public Similarity<float>{};
00256 
00257 class Similarityd:public Similarity<double>{};
00258 
00259 } //namespace
00260 
00261 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:35:23