templated on the kind of class used to implement rotation

(default is QUternion but it can be Matrix44 as well)
This commit is contained in:
ganovelli 2004-10-07 13:55:47 +00:00
parent 23a3ce597e
commit 5d07b02cf6
1 changed files with 37 additions and 27 deletions

View File

@ -24,6 +24,9 @@
History History
$Log: not supported by cvs2svn $ $Log: not supported by cvs2svn $
Revision 1.9 2004/06/04 13:35:07 cignoni
added InverseMatrix,
Revision 1.8 2004/05/07 10:09:13 cignoni Revision 1.8 2004/05/07 10:09:13 cignoni
missing final newline missing final newline
@ -32,6 +35,9 @@ unified to the gl stlyle matix*vector. removed vector*matrix operator
Revision 1.6 2004/03/25 14:57:49 ponchio Revision 1.6 2004/03/25 14:57:49 ponchio
Microerror. ($LOG$ -> $Log: not supported by cvs2svn $ Microerror. ($LOG$ -> $Log: not supported by cvs2svn $
Microerror. ($LOG$ -> Revision 1.9 2004/06/04 13:35:07 cignoni
Microerror. ($LOG$ -> added InverseMatrix,
Microerror. ($LOG$ ->
Microerror. ($LOG$ -> Revision 1.8 2004/05/07 10:09:13 cignoni Microerror. ($LOG$ -> Revision 1.8 2004/05/07 10:09:13 cignoni
Microerror. ($LOG$ -> missing final newline Microerror. ($LOG$ -> missing final newline
Microerror. ($LOG$ -> Microerror. ($LOG$ ->
@ -50,10 +56,10 @@ Microerror. ($LOG$ ->
namespace vcg { namespace vcg {
template <class S> class Similarity { template <class S,class RotationType = Quaternion<S> > class Similarity {
public: public:
Similarity() {} Similarity() {}
Similarity(const Quaternion<S> &q) { SetRotate(q); } Similarity(const RotationType &q) { SetRotate(q); }
Similarity(const Point3<S> &p) { SetTranslate(p); } Similarity(const Point3<S> &p) { SetTranslate(p); }
Similarity(S s) { SetScale(s); } Similarity(S s) { SetScale(s); }
@ -67,70 +73,70 @@ public:
Similarity &SetTranslate(const Point3<S> &t); Similarity &SetTranslate(const Point3<S> &t);
///use radiants for angle. ///use radiants for angle.
Similarity &SetRotate(S angle, const Point3<S> & axis); Similarity &SetRotate(S angle, const Point3<S> & axis);
Similarity &SetRotate(const Quaternion<S> &q); Similarity &SetRotate(const RotationType &q);
Matrix44<S> Matrix() const; Matrix44<S> Matrix() const;
Matrix44<S> InverseMatrix() const; Matrix44<S> InverseMatrix() const;
void FromMatrix(const Matrix44<S> &m); void FromMatrix(const Matrix44<S> &m);
Quaternion<S> rot; RotationType rot;
Point3<S> tra; Point3<S> tra;
S sca; S sca;
}; };
template <class S> Similarity<S> &Invert(Similarity<S> &m); template <class S,class RotationType> Similarity<S,RotationType> &Invert(Similarity<S,RotationType> &m);
template <class S> Similarity<S> Inverse(const Similarity<S> &m); template <class S,class RotationType> Similarity<S,RotationType> Inverse(const Similarity<S,RotationType> &m);
template <class S> Point3<S> operator*(const Similarity<S> &m, const Point3<S> &p); template <class S,class RotationType> Point3<S> operator*(const Similarity<S,RotationType> &m, const Point3<S> &p);
template <class S> Similarity<S> Similarity<S>::operator*(const Similarity &a) const { template <class S,class RotationType> Similarity<S,RotationType> Similarity<S,RotationType>::operator*(const Similarity &a) const {
Similarity<S> r; Similarity<S,RotationType> r;
r.rot = rot * a.rot; r.rot = rot * a.rot;
r.sca = sca * a.sca; r.sca = sca * a.sca;
r.tra = (rot.Rotate(a.tra)) * sca + tra; r.tra = (rot.Rotate(a.tra)) * sca + tra;
return r; return r;
} }
template <class S> Similarity<S> &Similarity<S>::operator*=(const Similarity &a) { template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::operator*=(const Similarity &a) {
rot = rot * a.rot; rot = rot * a.rot;
sca = sca * a.sca; sca = sca * a.sca;
tra = (rot.Rotate(a.tra)) * sca + tra; tra = (rot.Rotate(a.tra)) * sca + tra;
return *this; return *this;
} }
template <class S> Similarity<S> &Similarity<S>::SetIdentity() { template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetIdentity() {
rot.FromAxis(0, Point3<S>(1, 0, 0)); rot.SetIdentity();
tra = Point3<S>(0, 0, 0); tra = Point3<S>(0, 0, 0);
sca = 1; sca = 1;
return *this; return *this;
} }
template <class S> Similarity<S> &Similarity<S>::SetScale(const S s) { template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetScale(const S s) {
SetIdentity(); SetIdentity();
sca = s; sca = s;
return *this; return *this;
} }
template <class S> Similarity<S> &Similarity<S>::SetTranslate(const Point3<S> &t) { template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetTranslate(const Point3<S> &t) {
SetIdentity(); SetIdentity();
tra = t; tra = t;
return *this; return *this;
} }
template <class S> Similarity<S> &Similarity<S>::SetRotate(S angle, const Point3<S> &axis) { template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetRotate(S angle, const Point3<S> &axis) {
SetIdentity(); SetIdentity();
rot.FromAxis(angle, axis); rot.FromAxis(angle, axis);
return *this; return *this;
} }
template <class S> Similarity<S> &Similarity<S>::SetRotate(const Quaternion<S> &q) { template <class S,class RotationType> Similarity<S,RotationType> &Similarity<S,RotationType>::SetRotate(const RotationType &q) {
SetIdentity(); SetIdentity();
rot = q; rot = q;
return *this; return *this;
} }
template <class S> Matrix44<S> Similarity<S>::Matrix() const { template <class S,class RotationType> Matrix44<S> Similarity<S,RotationType>::Matrix() const {
Matrix44<S> r; Matrix44<S> r;
rot.ToMatrix(r); rot.ToMatrix(r);
Matrix44<S> s = Matrix44<S>().SetScale(sca, sca, sca); Matrix44<S> s = Matrix44<S>().SetScale(sca, sca, sca);
@ -138,12 +144,12 @@ template <class S> Matrix44<S> Similarity<S>::Matrix() const {
return s*r*t; // scale * rot * trans return s*r*t; // scale * rot * trans
} }
template <class S> Matrix44<S> Similarity<S>::InverseMatrix() const { template <class S,class RotationType> Matrix44<S> Similarity<S,RotationType>::InverseMatrix() const {
return Inverse(Matrix()); return Inverse(Matrix());
} }
template <class S> void Similarity<S>::FromMatrix(const Matrix44<S> &m) { template <class S,class RotationType> void Similarity<S,RotationType>::FromMatrix(const Matrix44<S> &m) {
sca = (S)pow(m.Determinant(), 1/3); sca = (S)pow(m.Determinant(), 1/3);
assert(sca != 0); assert(sca != 0);
Matrix44<S> t = m * Matrix44<S>().SetScale(1/sca, 1/sca, 1/sca); Matrix44<S> t = m * Matrix44<S>().SetScale(1/sca, 1/sca, 1/sca);
@ -153,36 +159,40 @@ template <class S> void Similarity<S>::FromMatrix(const Matrix44<S> &m) {
tra[2] = t.element(3, 2); tra[2] = t.element(3, 2);
} }
template <class S> Similarity<S> &Invert(Similarity<S> &a) { template <class S,class RotationType> Similarity<S,RotationType> &Invert(Similarity<S,RotationType> &a) {
a.rot.Invert(); a.rot.Invert();
a.sca = 1/a.sca; a.sca = 1/a.sca;
a.tra = a.rot.Rotate(-a.tra)*a.sca; a.tra = a.rot.Rotate(-a.tra)*a.sca;
return a; return a;
} }
template <class S> Similarity<S> Inverse(const Similarity<S> &m) { template <class S,class RotationType> Similarity<S,RotationType> Inverse(const Similarity<S,RotationType> &m) {
Similarity<S> a = m; Similarity<S,RotationType> a = m;
return Invert(a); return Invert(a);
} }
template <class S> Similarity<S> Interpolate(const Similarity<S> &a, const Similarity<S> &b, const S t) { template <class S,class RotationType> Similarity<S,RotationType> Interpolate(const Similarity<S,RotationType> &a, const Similarity<S,RotationType> &b, const S t) {
Similarity<S> r; Similarity<S,RotationType> r;
r.rot = interpolate(a.rot, b.rot, t); r.rot = interpolate(a.rot, b.rot, t);
r.tra = t * a.tra + (1-t) * b.tra; r.tra = t * a.tra + (1-t) * b.tra;
r.sca = t * a.sca + (1-t) * b.sca; r.sca = t * a.sca + (1-t) * b.sca;
return r; return r;
} }
template <class S> Point3<S> operator*(const Similarity<S> &m, const Point3<S> &p) { template <class S,class RotationType> Point3<S> operator*(const Similarity<S,RotationType> &m, const Point3<S> &p) {
Point3<S> r = m.rot.Rotate(p); Point3<S> r = m.rot.Rotate(p);
r *= m.sca; r *= m.sca;
r += m.tra; r += m.tra;
return r; return r;
} }
typedef Similarity<float> Similarityf; //typedef Similarity<float> Similarityf;
typedef Similarity<double>Similarityd; //typedef Similarity<double>Similarityd;
class Similarityf:public Similarity<float>{};
class Similarityd:public Similarity<double>{};
} //namespace } //namespace