/* Mathematiques de l'informatique graphique */ /* Rotation 3D en coordonnees homogenes */ /* */ /* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Mars 2019 */ #include #include #include #include "TG3D.h" #include "Rt3D.h" #include "Dir3D.h" #ifndef M_PI #define M_PI 3.14159 #endif /* Constructeurs */ Rt3D::Rt3D(void):TG3D() { } Rt3D::Rt3D(double alpha,double ax,double ay,double az):TG3D() { initialisation(alpha,&Dir3D(ax,ay,az)); } Rt3D::Rt3D(double alpha,Dir3D *d):TG3D() { initialisation(alpha,d); } Rt3D::Rt3D(Rt3D *rt):TG3D(rt) { } /* Destructeur */ Rt3D::~Rt3D(void) { } /* Initialisation d'une rotation */ void Rt3D::initialisation(double alpha,Dir3D *d) { Dir3D axe(d); axe.normalisation(); double x = axe.getx(); double y = axe.gety(); double z = axe.getz(); double aa = alpha/180.0*M_PI; double sn = sin(aa); double cs = cos(aa); c[0][0] = x*x+cs*(1-x*x); c[0][1] = x*y*(1-cs)-sn*z; c[0][2] = x*z*(1-cs)+sn*y; c[1][0] = x*y*(1-cs)+sn*z; c[1][1] = y*y+cs*(1-y*y); c[1][2] = y*z*(1-cs)-sn*x; c[2][0] = x*z*(1-cs)-sn*y; c[2][1] = y*z*(1-cs)+sn*x; c[2][2] = z*z+cs*(1-z*z); } /* Setter */ bool Rt3D::set(double m[4][4]) { if ( estRotation(m) ) { TG3D::set(m); return true; } return false; } /* Test si une matrice 4x4 de double */ /* est representative d'une rotation */ bool Rt3D::estRotation(double m[4][4]) { if ( ( m[0][3] != 0.0 ) || ( m[1][3] != 0.0 ) || ( m[2][3] != 0.0 ) || ( m[3][0] != 0.0 ) || ( m[3][1] != 0.0 ) || ( m[3][2] != 0.0 ) || ( m[3][3] != 1.0 ) ) return false; Dir3D d0(m[0][0],m[1][0],m[2][0]); Dir3D d1(m[0][1],m[1][1],m[2][1]); Dir3D d2(m[0][2],m[1][2],m[2][2]); if ( ( fabs(d0.produitScalaire(&d1)) > 0.0001 ) || ( fabs(d1.produitScalaire(&d2)) > 0.0001 ) || ( fabs(d2.produitScalaire(&d0)) > 0.0001 ) ) return false; if ( ( d0.norme() < 0.9999 ) || ( d1.norme() < 0.9999 ) || ( d2.norme() < 0.9999 ) ) return false; return true; }