From 40052e62006fafaa426c24c9a718dcf5433fda6d Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Sun, 10 Dec 2000 00:06:50 +0000
Subject: [PATCH] Corrige les rotations

---
 Common/Context.cpp | 107 +++++++++++++++++++++++++++++++++++++++------
 Common/Context.h   |   9 ++--
 Graphics/Draw.cpp  |  16 ++-----
 Graphics/Scale.cpp |  13 ++----
 Unix/CbGeneral.cpp |  17 +++++--
 5 files changed, 118 insertions(+), 44 deletions(-)

diff --git a/Common/Context.cpp b/Common/Context.cpp
index f177d5d2f3..b3ef62b887 100644
--- a/Common/Context.cpp
+++ b/Common/Context.cpp
@@ -1,4 +1,4 @@
-/* $Id: Context.cpp,v 1.20 2000-12-09 22:26:12 geuzaine Exp $ */
+/* $Id: Context.cpp,v 1.21 2000-12-10 00:06:50 geuzaine Exp $ */
 
 #include "Gmsh.h"
 #include "Const.h"
@@ -392,26 +392,105 @@ void Print_Context(char *filename){
   }
 }
 
-void Context_T::buildRotmatrix(float m[4][4])
+/*
+  3 rotations successives autour de x, y et z:
+
+           c(y)c(z)    s(x)s(y)c(z)+c(x)s(z)   -c(x)s(y)c(z)+s(x)s(z) 
+  t[][] = -c(y)s(z)   -s(x)s(y)s(z)+c(x)c(z)    c(x)s(y)s(z)+s(x)c(z)
+           s(y)       -s(x)c(y)                 c(x)c(y)
+
+  get the position angles:
+
+  y = asin(t31)
+      Pi - asin(t31)
+ 
+  si y != +- Pi/2 :
+
+  x = atan(-t32/t33)    si t33 cos y > 0
+      atan(-t32/t33)+Pi si t33 cos y < 0
+
+  z = atan(-t21/t11)    si t11 cos y > 0
+      atan(-t21/t11)+Pi si t11 cos y < 0
+
+*/
+
+void Context_T::buildRotmatrix(void)
 {
-  double r0, r1, r2;
+  double x, y, z;
   extern void set_r(int i, double val);
 
-  build_rotmatrix(m, quaternion);
-
-  r1 = atan2(-m[0][2],sqrt(m[1][2]*m[1][2] + m[2][2]*m[2][2]));
+  if(CTX.useTrackball){
+    build_rotmatrix(rot, quaternion);
+    // get the position angles
+    /*
+      double x=0., y=0., z=0.
 
-  double c = cos(r1);  
-  if(c != 0.0)
-    {
-      r0 = atan2(m[1][2]/c,m[2][2]/c) ;
-      r2 = atan2(-m[1][0]/c,m[0][0]/c) ;
+      y = asin(rot[2][0]) ; y = Pi - asin(rot[2][0]) ; // choix ???
+  
+      if(fabs(y) != Pi/2.){
+        if(rot[2][2]*cos(y) > 0.) x = atan2(-rot[2][1],rot[2][2]);
+	else                      x = atan2(-rot[2][1],rot[2][2]) + Pi;
+	if(rot[0][0]*cos(y) > 0.) z = atan2(-rot[1][0],rot[0][0]);
+	else                      z = atan2(-rot[1][0],rot[0][0]) + Pi;
+      }
+      set_r(0, x * 180./Pi);
+      set_r(1, y * 180./Pi);
+      set_r(2, z * 180./Pi);
+    */
+    /*
+    double r0, r1, r2;
+
+    r1 = atan2(-rot[0][2],sqrt(rot[1][2]*rot[1][2] + rot[2][2]*rot[2][2]));
+    
+    double c = cos(r1);  
+    if(c != 0.0){
+      r0 = atan2(rot[1][2]/c,rot[2][2]/c) ;
+      r2 = atan2(-rot[1][0]/c,rot[0][0]/c) ;
       r0 *= 180./(Pi);
       r2 *= 180./(Pi);
     }
-  set_r(0, r0);
-  set_r(1, r1 * 180./(Pi));  // lazyyyyyy
-  set_r(2, r2);
+    set_r(0, r0);
+    set_r(1, r1 * 180./(Pi));  // lazyyyyyy
+    set_r(2, r2);
+    */
+
+    // until we can compute this correctly
+    set_r(0, 0.);
+    set_r(1, 0.);
+    set_r(2, 0.);
+
+  }
+  else{
+    x = r[0] * Pi / 180.;
+    y = r[1] * Pi / 180.;
+    z = r[2] * Pi / 180.;
+
+    rot[0][0] = cos(y)*cos(z) ; 
+    rot[0][1] = sin(x)*sin(y)*cos(z)+cos(x)*sin(z);
+    rot[0][2] = -cos(x)*sin(y)*cos(z)+sin(x)*sin(z);
+    rot[0][3] = 0.0;
+
+    rot[1][0] = -cos(y)*sin(z);
+    rot[1][1] = -sin(x)*sin(y)*sin(z)+cos(x)*cos(z);
+    rot[1][2] = cos(x)*sin(y)*sin(z)+sin(x)*cos(z);
+    rot[1][3] = 0.0;
+    
+    rot[2][0] = sin(y);
+    rot[2][1] = -sin(x)*cos(y);
+    rot[2][2] = cos(x)*cos(y);
+    rot[2][3] = 0.0;
+
+    rot[3][0] = 0.0 ;
+    rot[3][1] = 0.0 ;
+    rot[3][2] = 0.0 ;
+    rot[3][3] = 1.0 ;
+    /*
+    glRotated (CTX.r[0], 1., 0., 0.);
+    glRotated (CTX.r[1], 0., 1., 0.);
+    glRotated (CTX.r[2], 0., 0., 1.);
+    */
+  }
+
 }
 
 void Context_T::addQuaternion (float p1x, float p1y, float p2x, float p2y)
diff --git a/Common/Context.h b/Common/Context.h
index eeef7da088..8b9f5de27d 100644
--- a/Common/Context.h
+++ b/Common/Context.h
@@ -1,4 +1,4 @@
-/* $Id: Context.h,v 1.18 2000-12-09 17:33:39 geuzaine Exp $ */
+/* $Id: Context.h,v 1.19 2000-12-10 00:06:50 geuzaine Exp $ */
 #ifndef _CONTEXT_H_
 #define _CONTEXT_H_
 
@@ -59,10 +59,11 @@ class Context_T {
   int verbosity;              /* 0=silent -> 3=debug */
   int expose;                 /* 1 if everything is ready to expose and draw */
 
-  double r[3], t[3], s[3];    /* current rotation, translation and scale */
+  float rot[4][4];            /* current rotation matrix */
+  double r[3];                /* position angles (if succ. rot. along x, y and z) */
+  double t[3], s[3];          /* current translation and scale */
   int rlock[3], tlock[3], slock[3];
                               /* locks for r, t and s */
-  
   float quaternion[4];        /* the actual quaternion used for "trackball" rotating */
   int useTrackball;           /* do or do not use the trackball for rotations */
 
@@ -145,7 +146,7 @@ class Context_T {
   } print;
 
   // trackball functions 
-  void buildRotmatrix(float m[4][4]);
+  void buildRotmatrix(void);
   void setQuaternion (float p1x, float p1y, float p2x, float p2y);
   void addQuaternion (float p1x, float p1y, float p2x, float p2y);
 };
diff --git a/Graphics/Draw.cpp b/Graphics/Draw.cpp
index 7dd263ace5..e65c1db060 100644
--- a/Graphics/Draw.cpp
+++ b/Graphics/Draw.cpp
@@ -1,4 +1,4 @@
-/* $Id: Draw.cpp,v 1.9 2000-12-05 23:01:06 geuzaine Exp $ */
+/* $Id: Draw.cpp,v 1.10 2000-12-10 00:06:50 geuzaine Exp $ */
 
 #include "Gmsh.h"
 #include "GmshUI.h"
@@ -230,18 +230,8 @@ void InitPosition(void){
   glScaled    (CTX.s[0], CTX.s[1], CTX.s[2]);
   glTranslated(CTX.t[0], CTX.t[1], CTX.t[2]);
 
-  if(CTX.useTrackball)
-    {
-      float m[4][4];
-      CTX.buildRotmatrix(m);
-      glMultMatrixf(&(m[0][0]));
-    }
-  else
-    {
-      glRotated   (CTX.r[0], 1., 0., 0.);
-      glRotated   (CTX.r[1], 0., 1., 0.);
-      glRotated   (CTX.r[2], 0., 0., 1.);
-    }
+  CTX.buildRotmatrix();
+  glMultMatrixf(&(CTX.rot[0][0]));
 }
 
 /* ------------------------------------------------------------------------ */
diff --git a/Graphics/Scale.cpp b/Graphics/Scale.cpp
index d0bb9d5d61..e4c13e361c 100644
--- a/Graphics/Scale.cpp
+++ b/Graphics/Scale.cpp
@@ -1,4 +1,4 @@
-/* $Id: Scale.cpp,v 1.7 2000-12-05 15:23:56 geuzaine Exp $ */
+/* $Id: Scale.cpp,v 1.8 2000-12-10 00:06:50 geuzaine Exp $ */
 
 #include "Gmsh.h"
 #include "GmshUI.h"
@@ -287,21 +287,16 @@ void Draw_Scales(void){
 /* ------------------------------------------------------------------------ */
 
 void Draw_SmallAxes(void){
-  double a,b,c;
   double l,o,xx,xy,yx,yy,zx,zy,cx,cy;
 
-  a = CTX.r[0] * Pi/180. ;
-  b = CTX.r[1] * Pi/180. ;
-  c = CTX.r[2] * Pi/180. ;
-
   l  = 30  ;
   o  = 2  ;
   cx = CTX.viewport[2] - 45;
   cy = CTX.viewport[1] + 35;
 
-  xx =  l*cos(b)*cos(c) ; xy =  l*(sin(a)*sin(b)*cos(c)+cos(a)*sin(c)) ;
-  yx = -l*cos(b)*sin(c) ; yy = -l*(sin(a)*sin(b)*sin(c)-cos(a)*cos(c)) ;
-  zx =  l*sin(b)        ; zy = -l*sin(a)*cos(b) ;
+  xx = l*CTX.rot[0][0] ; xy = l*CTX.rot[0][1] ;
+  yx = l*CTX.rot[1][0] ; yy = l*CTX.rot[1][1] ;
+  zx = l*CTX.rot[2][0] ; zy = l*CTX.rot[2][1] ;
 
   glColor4ubv((GLubyte*)&CTX.color.small_axes);
 
diff --git a/Unix/CbGeneral.cpp b/Unix/CbGeneral.cpp
index 4968e1228f..43beda97d0 100644
--- a/Unix/CbGeneral.cpp
+++ b/Unix/CbGeneral.cpp
@@ -1,4 +1,4 @@
-/* $Id: CbGeneral.cpp,v 1.5 2000-11-27 10:58:59 geuzaine Exp $ */
+/* $Id: CbGeneral.cpp,v 1.6 2000-12-10 00:06:50 geuzaine Exp $ */
 
 #include "Gmsh.h"
 #include "GmshUI.h"
@@ -133,14 +133,23 @@ void CurrentInfoCb (Widget w, XtPointer client_data, XtPointer call_data){
    ------------------------------------------------------------------------ */
 
 void set_r(int i, double val){
-  if(!CTX.rlock[i]){
-    CTX.r[i] = val;
+  if(CTX.useTrackball){
     if(XtIsManaged(WID.OD.viewportDialog)){
-      sprintf(label, "%.5g", CTX.r[i]);
+      sprintf(label, "---");
       XtVaSetValues(WID.OD.viewportText[0][i], XmNvalue, label, NULL);
       XmUpdateDisplay(WID.OD.viewportText[0][i]);  
     }
   }
+  else{
+    if(!CTX.rlock[i]){
+      CTX.r[i] = val;
+      if(XtIsManaged(WID.OD.viewportDialog)){
+	sprintf(label, "%.5g", CTX.r[i]);
+	XtVaSetValues(WID.OD.viewportText[0][i], XmNvalue, label, NULL);
+	XmUpdateDisplay(WID.OD.viewportText[0][i]);  
+      }
+    }
+  }
 }
 
 void set_t(int i, double val){
-- 
GitLab