vcglib/vcg/math/similarity.h

208 lines
7.5 KiB
C
Raw Normal View History

2004-03-08 02:48:13 +01:00
/****************************************************************************
* VCGLib o o *
* Visual and Computer Graphics Library o o *
* _ O _ *
* Copyright(C) 2004 \/)\/ *
* Visual Computing Lab /\/| *
* ISTI - Italian National Research Council | *
* \ *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
* *
****************************************************************************/
/****************************************************************************
History
2004-03-25 15:57:50 +01:00
$Log: not supported by cvs2svn $
2004-12-15 19:45:50 +01:00
Revision 1.10 2004/10/07 13:55:47 ganovelli
templated on the kind of class used to implement rotation
(default is QUternion but it can be Matrix44 as well)
Revision 1.9 2004/06/04 13:35:07 cignoni
added InverseMatrix,
2004-06-04 15:35:07 +02:00
Revision 1.8 2004/05/07 10:09:13 cignoni
missing final newline
2004-05-07 12:09:13 +02:00
Revision 1.7 2004/05/04 23:23:45 cignoni
unified to the gl stlyle matix*vector. removed vector*matrix operator
Revision 1.6 2004/03/25 14:57:49 ponchio
Microerror. ($LOG$ -> $Log: not supported by cvs2svn $
2004-12-15 19:45:50 +01:00
Microerror. ($LOG$ -> Revision 1.10 2004/10/07 13:55:47 ganovelli
Microerror. ($LOG$ -> templated on the kind of class used to implement rotation
Microerror. ($LOG$ -> (default is QUternion but it can be Matrix44 as well)
Microerror. ($LOG$ ->
Microerror. ($LOG$ -> Revision 1.9 2004/06/04 13:35:07 cignoni
Microerror. ($LOG$ -> added InverseMatrix,
Microerror. ($LOG$ ->
2004-06-04 15:35:07 +02:00
Microerror. ($LOG$ -> Revision 1.8 2004/05/07 10:09:13 cignoni
Microerror. ($LOG$ -> missing final newline
Microerror. ($LOG$ ->
2004-05-07 12:09:13 +02:00
Microerror. ($LOG$ -> Revision 1.7 2004/05/04 23:23:45 cignoni
Microerror. ($LOG$ -> unified to the gl stlyle matix*vector. removed vector*matrix operator
Microerror. ($LOG$ ->
2004-03-08 02:48:13 +01:00
****************************************************************************/
2004-03-06 16:45:35 +01:00
#ifndef SIMILARITY_H
#define SIMILARITY_H
#include <vcg/math/quaternion.h>
#include <vcg/math/matrix44.h>
namespace vcg {
template <class S,class RotationType = Quaternion<S> > class Similarity {
2004-03-06 16:45:35 +01:00
public:
Similarity() {}
Similarity(const RotationType &q) { SetRotate(q); }
2004-03-25 14:54:06 +01:00
Similarity(const Point3<S> &p) { SetTranslate(p); }
Similarity(S s) { SetScale(s); }
2004-03-06 16:45:35 +01:00
Similarity operator*(const Similarity &affine) const;
Similarity &operator*=(const Similarity &affine);
2004-03-25 14:54:06 +01:00
//Point3<S> operator*(const Point3<S> &p) const;
2004-03-06 16:45:35 +01:00
Similarity &SetIdentity();
Similarity &SetScale(const S s);
Similarity &SetTranslate(const Point3<S> &t);
///use radiants for angle.
2004-03-09 14:57:29 +01:00
Similarity &SetRotate(S angle, const Point3<S> & axis);
Similarity &SetRotate(const RotationType &q);
2004-03-06 16:45:35 +01:00
Matrix44<S> Matrix() const;
2004-06-04 15:35:07 +02:00
Matrix44<S> InverseMatrix() const;
2004-03-09 14:57:29 +01:00
void FromMatrix(const Matrix44<S> &m);
2004-03-06 16:45:35 +01:00
RotationType rot;
2004-03-06 16:45:35 +01:00
Point3<S> tra;
S sca;
};
template <class S,class RotationType> Similarity<S,RotationType> &Invert(Similarity<S,RotationType> &m);
template <class S,class RotationType> Similarity<S,RotationType> Inverse(const Similarity<S,RotationType> &m);
template <class S,class RotationType> Point3<S> operator*(const Similarity<S,RotationType> &m, const Point3<S> &p);
2004-03-09 14:57:29 +01:00
template <class S,class RotationType> Similarity<S,RotationType> Similarity<S,RotationType>::operator*(const Similarity &a) const {
Similarity<S,RotationType> r;
2004-03-06 16:45:35 +01:00
r.rot = rot * a.rot;
r.sca = sca * a.sca;
r.tra = (rot.Rotate(a.tra)) * sca + tra;
return r;
}
template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::operator*=(const Similarity &a) {
2004-03-06 16:45:35 +01:00
rot = rot * a.rot;
sca = sca * a.sca;
tra = (rot.Rotate(a.tra)) * sca + tra;
return *this;
}
template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetIdentity() {
rot.SetIdentity();
2004-03-06 16:45:35 +01:00
tra = Point3<S>(0, 0, 0);
sca = 1;
return *this;
}
template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetScale(const S s) {
2004-03-06 16:45:35 +01:00
SetIdentity();
sca = s;
return *this;
}
template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetTranslate(const Point3<S> &t) {
2004-03-06 16:45:35 +01:00
SetIdentity();
tra = t;
return *this;
}
template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetRotate(S angle, const Point3<S> &axis) {
2004-03-06 16:45:35 +01:00
SetIdentity();
rot.FromAxis(angle, axis);
2004-03-09 14:57:29 +01:00
return *this;
}
template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetRotate(const RotationType &q) {
2004-03-09 14:57:29 +01:00
SetIdentity();
rot = q;
return *this;
2004-03-06 16:45:35 +01:00
}
2004-03-09 14:57:29 +01:00
template <class S,class RotationType> Matrix44<S> Similarity<S,RotationType>::Matrix() const {
2004-03-06 16:45:35 +01:00
Matrix44<S> r;
rot.ToMatrix(r);
Matrix44<S> s = Matrix44<S>().SetScale(sca, sca, sca);
2004-03-06 16:45:35 +01:00
Matrix44<S> t = Matrix44<S>().SetTranslate(tra[0], tra[1], tra[2]);
return s*r*t; // scale * rot * trans
2004-03-06 16:45:35 +01:00
}
template <class S,class RotationType> Matrix44<S> Similarity<S,RotationType>::InverseMatrix() const {
2004-06-04 15:35:07 +02:00
return Inverse(Matrix());
}
template <class S,class RotationType> void Similarity<S,RotationType>::FromMatrix(const Matrix44<S> &m) {
2004-03-25 14:54:06 +01:00
sca = (S)pow(m.Determinant(), 1/3);
2004-03-09 14:57:29 +01:00
assert(sca != 0);
Matrix44<S> t = m * Matrix44<S>().SetScale(1/sca, 1/sca, 1/sca);
rot.FromMatrix(t);
2004-12-15 19:45:50 +01:00
tra[0] = t.ElementAt(3, 0);
tra[1] = t.ElementAt(3, 1);
tra[2] = t.ElementAt(3, 2);
2004-03-09 14:57:29 +01:00
}
template <class S,class RotationType> Similarity<S,RotationType> &Invert(Similarity<S,RotationType> &a) {
2004-03-06 16:45:35 +01:00
a.rot.Invert();
a.sca = 1/a.sca;
a.tra = a.rot.Rotate(-a.tra)*a.sca;
return a;
}
template <class S,class RotationType> Similarity<S,RotationType> Inverse(const Similarity<S,RotationType> &m) {
Similarity<S,RotationType> a = m;
2004-03-09 14:57:29 +01:00
return Invert(a);
}
template <class S,class RotationType> Similarity<S,RotationType> Interpolate(const Similarity<S,RotationType> &a, const Similarity<S,RotationType> &b, const S t) {
Similarity<S,RotationType> r;
2004-03-06 16:45:35 +01:00
r.rot = interpolate(a.rot, b.rot, t);
r.tra = t * a.tra + (1-t) * b.tra;
r.sca = t * a.sca + (1-t) * b.sca;
return r;
}
template <class S,class RotationType> Point3<S> operator*(const Similarity<S,RotationType> &m, const Point3<S> &p) {
2004-03-25 14:54:06 +01:00
Point3<S> r = m.rot.Rotate(p);
r *= m.sca;
r += m.tra;
return r;
}
//typedef Similarity<float> Similarityf;
//typedef Similarity<double>Similarityd;
class Similarityf:public Similarity<float>{};
class Similarityd:public Similarity<double>{};
2004-03-06 16:45:35 +01:00
} //namespace
2004-05-07 12:09:13 +02:00
#endif