G4OpenGLXmRotationCallbacks.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // Andrew Walkden  16th April 1997
00031 // G4OpenGLXmRotationCallbacks : 
00032 //                       Several callback functions used by
00033 //                       elements of the control panel to
00034 //                       rotate the view.
00035 
00036 #ifdef G4VIS_BUILD_OPENGLXM_DRIVER
00037 
00038 #include "G4OpenGLXmViewer.hh"
00039 
00040 #include "G4PhysicalConstants.hh"
00041 #include "G4Scene.hh"
00042 #include "G4UImanager.hh"
00043 #include "G4ios.hh"
00044 
00045 void G4OpenGLXmViewer::theta_rotation_callback (Widget w, 
00046                                               XtPointer clientData, 
00047                                               XtPointer callData) 
00048 {
00049   XmArrowButtonCallbackStruct *cbs = (XmArrowButtonCallbackStruct*) callData;
00050   G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
00051 
00052   pView->rotate_right = get_boolean_userData (w);
00053 
00054   if (cbs->reason == XmCR_ARM) {
00055     rotate_in_theta (pView, NULL);
00056   } else if (cbs->reason == XmCR_DISARM) {
00057     XtRemoveTimeOut (pView->rotation_timer);
00058   }
00059 }
00060 
00061 void G4OpenGLXmViewer::rotate_in_theta (XtPointer clientData,
00062                                       XtIntervalId* timer_id) 
00063 {
00064   //theta spin stuff here
00065   G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
00066 
00067   if (pView->rotate_right) {
00068     pView->rotateScene(1,0);
00069   } else {
00070     pView->rotateScene(-1,0);
00071   }
00072   /*
00073   G4double delta_theta;
00074 
00075   if (pView->fVP.GetLightsMoveWithCamera()) {
00076     if (pView->rotate_right) {
00077       delta_theta = -(pView->fRot_sens);
00078     } else {
00079       delta_theta = pView->fRot_sens;
00080     }
00081   } else {
00082     if (pView->rotate_right) {
00083       delta_theta = pView->fRot_sens;
00084     } else {
00085       delta_theta = -(pView->fRot_sens);
00086     }
00087   }    
00088   delta_theta *= deg;
00089   // Rotates by fixed azimuthal angle delta_theta.
00090 
00091   const G4Vector3D& vp = pView->fVP.GetViewpointDirection ().unit ();
00092   const G4Vector3D& up = pView->fVP.GetUpVector ().unit ();
00093   const G4Vector3D& zprime = up;
00094   G4double cosalpha = up.dot (vp);
00095   G4double sinalpha = std::sqrt (1. - std::pow (cosalpha, 2));
00096   G4Vector3D yprime = (zprime.cross (vp)).unit ();
00097   G4Vector3D xprime = yprime.cross (zprime);
00098   // Projection of vp on plane perpendicular to up...
00099   G4Vector3D a1 = sinalpha * xprime;
00100   // Required new projection...
00101   G4Vector3D a2 =
00102     sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime);
00103   // Required Increment vector...
00104   G4Vector3D delta = a2 - a1;
00105   // So new viewpoint is...
00106   G4Vector3D viewPoint = vp + delta;
00107 
00108   pView->fVP.SetViewAndLights (viewPoint);  
00109   */
00110 
00111   pView->SetView ();
00112   pView->ClearView ();
00113   pView->DrawView ();
00114 
00115   pView->rotation_timer = XtAppAddTimeOut 
00116     (pView->app,
00117      timer_id == NULL ? 500 : 1,
00118      rotate_in_theta,
00119      pView);
00120 }  
00121 
00122 void G4OpenGLXmViewer::phi_rotation_callback (Widget w, 
00123                                             XtPointer clientData, 
00124                                             XtPointer callData) 
00125 {
00126   XmArrowButtonCallbackStruct *cbs = (XmArrowButtonCallbackStruct*) callData;
00127   G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
00128 
00129   pView->rotate_up = get_boolean_userData (w);
00130 
00131   if (cbs->reason == XmCR_ARM) {
00132     rotate_in_phi (pView, NULL);
00133   } else if (cbs->reason == XmCR_DISARM) {
00134     XtRemoveTimeOut (pView->rotation_timer);
00135   }
00136 }
00137 
00138 void G4OpenGLXmViewer::rotate_in_phi (XtPointer clientData,
00139                                     XtIntervalId* timer_id)
00140 {
00141   //phi spin stuff here
00142   //  G4double delta_alpha;
00143   G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
00144 
00145   if (pView -> rotate_up) {
00146     pView->rotateScene(0,-1);
00147   } else {
00148     pView->rotateScene(0,1);
00149   }
00150   /*
00151   if (pView->fVP.GetLightsMoveWithCamera()) {
00152     if (pView -> rotate_up) {
00153       delta_alpha = -(pView->fRot_sens);
00154     } else {
00155       delta_alpha = pView->fRot_sens;
00156     }
00157   } else {
00158     if (pView -> rotate_up) {
00159       delta_alpha = pView->fRot_sens;
00160     } else {
00161       delta_alpha = -(pView->fRot_sens);
00162     }
00163   }    
00164 
00165   delta_alpha *= deg;
00166 
00167   const G4Vector3D& vp = pView->fVP.GetViewpointDirection ().unit ();
00168   const G4Vector3D& up = pView->fVP.GetUpVector ().unit ();
00169 
00170   const G4Vector3D& xprime = vp;
00171   G4Vector3D yprime = (up.cross(xprime)).unit();
00172   G4Vector3D zprime = (xprime.cross(yprime)).unit();
00173 
00174   G4Vector3D new_vp = std::cos(delta_alpha) * xprime + std::sin(delta_alpha) * zprime;
00175 
00176   pView->fVP.SetViewAndLights (new_vp.unit());
00177 
00178   if (pView->fVP.GetLightsMoveWithCamera()) {
00179     G4Vector3D new_up = (new_vp.cross(yprime)).unit();
00180     pView->fVP.SetUpVector(new_up);
00181   }
00182 
00183   */
00184   pView->SetView ();
00185   pView->ClearView ();
00186   pView->DrawView ();
00187   
00188   pView->rotation_timer = XtAppAddTimeOut 
00189     (pView->app,
00190      timer_id == NULL ? 500 : 1,
00191      rotate_in_phi,
00192      pView);
00193 }
00194 
00195 void G4OpenGLXmViewer::set_rot_sens_callback (Widget w, 
00196                                             XtPointer clientData, 
00197                                             XtPointer callData) 
00198 {
00199   XmScaleCallbackStruct *cbs = (XmScaleCallbackStruct*) callData;
00200   G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*) clientData;
00201   short dp = -1;
00202   G4float ten_to_the_dp = 10.;
00203 
00204   XtVaGetValues (w, 
00205                  XmNdecimalPoints, &dp,
00206                  NULL);
00207 
00208   if (dp == 0) {
00209     ten_to_the_dp = 1.;
00210   } else if ( dp > 0) {
00211     for (G4int i = 1; i < (G4int)dp; i++) {
00212       ten_to_the_dp *= 10.;
00213     }
00214   } else {
00215     G4Exception
00216       ("G4OpenGLXmViewer::set_rot_sens_callback",
00217        "opengl2004", FatalException,
00218        "Bad value returned for dp in set_rot_sens_callback");
00219   }
00220 
00221   pView->fRot_sens = (G4float)(cbs->value) / ten_to_the_dp;
00222 }  
00223 
00224 void G4OpenGLXmViewer::set_rot_subject_callback (Widget w, 
00225                                                XtPointer clientData, 
00226                                                XtPointer) 
00227 {
00228   G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*)clientData;
00229   
00230   G4int choice = get_int_userData (w);
00231   
00232   switch (choice) {
00233   case 0: 
00234     {
00235       pView->fVP.SetLightsMoveWithCamera (true);
00236       break;
00237     }
00238   case 1:
00239     {
00240       pView->fVP.SetLightsMoveWithCamera (false);
00241       break;
00242     }
00243   default:
00244     {
00245       G4Exception
00246         ("G4OpenGLXmViewer::set_rot_subject_callback",
00247          "opengl2005", FatalException,
00248          "Unrecognised choice made in set_rot_subject_callback"); 
00249     }
00250   }
00251 }  
00252 
00253 void G4OpenGLXmViewer::wobble_callback (Widget w, 
00254                                       XtPointer, 
00255                                       XtPointer) 
00256 {
00257   G4OpenGLXmViewer* pView;
00258   
00259   XtVaGetValues (w,
00260                  XmNuserData, &pView,
00261                  NULL);
00262   
00263   pView->original_vp = pView->fVP.GetViewpointDirection();
00264   pView->wobble_timer = XtAppAddTimeOut
00265     (pView->app,
00266      (long unsigned int) (1000. * (1. / pView->wob_sens)),
00267      wobble_timer_callback,
00268      pView);
00269 }  
00270 
00271 void G4OpenGLXmViewer::wobble_timer_callback (XtPointer clientData,
00272                                             XtIntervalId*) 
00273 {
00274   G4OpenGLXmViewer* pView = (G4OpenGLXmViewer*)clientData;
00275   const G4Vector3D& up = pView->fVP.GetUpVector();
00276   G4Vector3D third_axis = up.cross(pView->original_vp);
00277   G4double pi_div_by_ten = pi / 10.0;
00278   G4Vector3D d_up = 0.1 * (std::sin ((G4double)pView->frameNo * pi_div_by_ten * 2.)) * up;
00279   G4Vector3D d_third_axis = 0.1 * (std::sin ((G4double)pView->frameNo * (pi_div_by_ten))) * third_axis;
00280 
00281   pView->fVP.SetViewAndLights (pView->original_vp + d_up + d_third_axis);
00282 
00283   pView->SetView ();
00284   pView->ClearView ();
00285   pView->DrawView ();
00286   
00287   if (pView->frameNo++ == 20) {
00288     if (pView->wobble_timer) {
00289       XtRemoveTimeOut (pView->wobble_timer);
00290       pView->frameNo = 0;
00291       pView->fVP.SetViewAndLights (pView->original_vp);
00292       pView->SetView ();
00293       pView->ClearView ();
00294       pView->DrawView ();
00295    }
00296   } else {
00297     pView->wobble_timer = XtAppAddTimeOut
00298       (pView->app,
00299        (long unsigned int) (1000. * (1. / pView->wob_sens)),
00300        wobble_timer_callback,
00301        pView);
00302   }
00303 }
00304 
00305 void G4OpenGLXmViewer::reset_callback (Widget w, 
00306                                      XtPointer, 
00307                                      XtPointer) 
00308 {
00309   
00310   G4OpenGLXmViewer* pView;
00311   
00312   XtVaGetValues (w,
00313                  XmNuserData, &pView,
00314                  NULL);
00315   
00316   pView->fVP.SetCurrentTargetPoint(G4Point3D());
00317   pView->fVP.SetZoomFactor(1.0);
00318   pView->fVP.SetDolly(0.0);
00319   pView->SetView ();
00320   pView->ClearView ();
00321   pView->DrawView ();
00322   pView->zoom_low = 0.1;
00323   pView->zoom_high = 10.0;
00324   
00325 }
00326 #endif

Generated on Mon May 27 17:49:11 2013 for Geant4 by  doxygen 1.4.7