Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TridHistoGLFrame.cxx

Go to the documentation of this file.
00001  
00002 #include "TridHistoGLFrame.h"
00003 #include "TridPage.h"
00004 #include "TridSketches.h"
00005 #include "TridOpenGLGlobal.h"
00006 #include "TridControl.h"
00007 
00008 #include "TROOT.h"
00009 #include "TGX11.h"
00010 #include "TVirtualX.h"
00011 #include "KeySymbols.h"
00012 
00013 #include <GL/gl.h>
00014 #include <GL/glx.h>
00015 #include <GL/glu.h>
00016 #include "glf.h"
00017 #include <cassert>
00018 #include <iostream>
00019 #include <cmath>
00020 using namespace std;
00021 
00022 TridHistoGLFrame::TridHistoGLFrame( TridPage* tp, 
00023                                     TGWindow& parent, 
00024                                     TH1* hist,
00025                                     TridControl* tc,
00026                                     HistoType_t type)
00027   : TridFlatGLFrame( tp, parent, tc ),
00028     fHist(hist),
00029     fHistoType(type)
00030 {
00031   cout << "TridHistoGLFrame constructor." << endl;
00032 }
00033 
00034 TridHistoGLFrame::~TridHistoGLFrame()
00035 {
00036   cout << "TridHistoGLFrame constructor." << endl;
00037 }
00038 
00039 void TridHistoGLFrame::SetupProjection( void )
00040 {
00041   // Set up the projection for the 3d window.
00042   fAspectRatio = 1;
00043   if(fWidth>0) fAspectRatio = (double)fHeight/(double)fWidth;
00044 
00045   glOrtho( -0.2, 1.1,
00046            -0.3, 1.1,
00047           -1.,1.);
00048 }
00049 
00050 
00051 
00052 void TridHistoGLFrame::TransformToPOV( void )
00053 {
00054   NormalizeAndClipPOV();
00055   
00056   // Need to manipulate the ModelView matrix to move our model around.
00057   glMatrixMode(GL_MODELVIEW);
00058   
00059   // Reset to 0,0,0; no rotation, no scaling.
00060   glLoadIdentity();   
00061 }
00062 
00063 
00064 
00065 Bool_t TridHistoGLFrame::HandleMotion(Event_t* ev)
00066 {
00067   if(fButton != 0) {
00068    
00069     int delx, dely;
00070     delx = ev->fX - fMouseStart_x;
00071     dely = ev->fY - fMouseStart_y;
00072     fButtonTraveled+= abs(delx) + abs(dely);
00073     
00074     if(abs(delx) + abs(dely)>0){
00075       if( (fButton ==kButton1) || (fButton ==kButton3) ) {
00076         // Slide.
00077         
00078         GLfloat z;
00079         GLdouble x0, y0, z0;
00080         GLdouble x1, y1, z1;
00081         GLdouble x2, y2, z2;
00082         GLdouble  model[16];
00083         GLdouble  proj[16];
00084         GLint     view[4];
00085         glGetDoublev(GL_MODELVIEW_MATRIX, model);
00086         glGetDoublev(GL_PROJECTION_MATRIX, proj);
00087         glGetIntegerv(GL_VIEWPORT, view); 
00088         
00089         //glReadPixels(fMouseStart_x, fHeight-fMouseStart_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
00090         z = 0;
00091         gluUnProject(fMouseStart_x, fHeight-fMouseStart_y,  z, model, proj, view, &x0, &y0, &z0);
00092         gluUnProject(fMouseLast_x, fHeight-fMouseLast_y,  z, model, proj, view, &x1, &y1, &z1);
00093         gluUnProject(ev->fX,        fHeight-ev->fY,       z, model, proj, view, &x2, &y2, &z2);
00094         
00095         // Click at x1,y1
00096         // Drag to x2,y2
00097 
00098         if(fTridControl==0) return true;
00099 
00100         // Determine where.
00101         if( (x0>0) && ( y0>0) ) {
00102           // Click was in histogram area. Use this to shift histogram without scaling.
00103           // Insist that minimum drag is ~ 1/2 bin width
00104           if( fabs(x1-x2)>0.01 ) {
00105             double xmin, xmax;
00106             if(fHistoType == kColor) {
00107               fTridControl->GetColorRange(xmin,xmax);
00108               double xshift = (x1-x2)*(xmax-xmin);
00109               fTridControl->ChangeColorRange(xmin+xshift,xmax+xshift);
00110             } else {
00111               fTridControl->GetTransRange(xmin,xmax);
00112               double xshift = (x2-x1)*(xmax-xmin);
00113               fTridControl->ChangeTransRange(xmin-xshift,xmax-xshift);
00114             }
00115             fMouseLast_x = ev->fX;
00116             fMouseLast_y = ev->fY;
00117           }
00118         }
00119 
00120         if( (x0<0) && ( y0>0) ) {
00121           // Click was in y-scale area. Scale drag.
00122           if( fabs(y1-y2)>0.01 ) {
00123             double yscale = (y1/y2);
00124             if(yscale<0.1) yscale = 0.1;
00125             if(yscale>10.0) yscale = 10.0;
00126             
00127             double newmax = fHist->GetMaximum()*yscale;
00128             if(newmax<0.1) newmax = 0.1;
00129             if(newmax>1e6) newmax = 1.e6;
00130             fHist->SetMaximum(newmax);      
00131 
00132             fMouseLast_x = ev->fX;
00133             fMouseLast_y = ev->fY;
00134           }
00135         }
00136         
00137         if( (x0>0) && ( y0<0) && (x1>0) && (y1<0) && (x2>0) && (y2<0) ) {
00138           // Click was in x-scale area. Scale drag.
00139           if( fabs(x1-x2)>0.01 ) {
00140             double xmin, xmax;
00141             if(fHistoType == kColor) {
00142               fTridControl->GetColorRange(xmin,xmax);
00143             } else {
00144               fTridControl->GetTransRange(xmin,xmax);
00145             }
00146             
00147             // Obsolete: allows dragging scale on left OR right.
00148             //if(x1<0.5) {
00149             //  xmin = (x1*(xmax-xmin)+xmin-xmax*x2)/(1-x2);
00150             //} else {
00151             //  xmax = xmin + x1*(xmax-xmin)/x2;
00152             //}
00153             
00154             xmax = xmin + x1*(xmax-xmin)/x2;
00155 
00156             if(fHistoType == kColor) {
00157               fTridControl->ChangeColorRange(xmin,xmax);
00158             } else {
00159               fTridControl->ChangeTransRange(xmin,xmax);
00160             }
00161 
00162             
00163             fMouseLast_x = ev->fX;
00164             fMouseLast_y = ev->fY;
00165           }
00166         }
00167                 
00168         Update();
00169       }
00170     }
00171   }
00172   return true;
00173 }
00174 
00175 
00176 double GetGoodTickWidth( double min, double max, double maxticks, char* format )
00177 {
00178   double dumbTickWidth = (max-min)/maxticks;
00179   //cout << "Dumb width " << dumbTickWidth << endl;
00180   double thelog = log10(dumbTickWidth);
00181   double multiplier = pow(10,floor(thelog));
00182   double abcissa = pow(10,thelog)/multiplier; // Gives a number between 1 and 9.999
00183   double goodTickWidth;
00184   //if(abcissa<3.0) {
00185   //  goodTickWidth = 1.0*multiplier;
00186   //} else if(abcissa>3.0 && abcissa<7.0) {
00187   int sigfigs = 1;
00188   if(abcissa<3.0) {
00189     goodTickWidth = 2.5*multiplier;
00190     sigfigs = 2;
00191   }  else if(abcissa<7.0) {
00192     goodTickWidth = 5.0*multiplier;
00193   } else {
00194     goodTickWidth = 10.0 * multiplier;
00195   }
00196 
00197   double extreme = fabs(max);
00198   if(fabs(min)>fabs(max)) extreme = fabs(min);
00199   if(format) {
00200     if(extreme>10000) strcpy(format,"%1.1e");
00201     else if(extreme>10) strcpy(format,"%1.1f");
00202     else if(extreme>1) strcpy(format,"%1.1f");
00203     else if(extreme>0.001) strcpy(format,"%g");
00204     else strcpy(format,"%1.1e");
00205   }
00206   //cout << "Good width " << goodTickWidth << endl;
00207   return goodTickWidth;
00208 }
00209 
00210 void TridHistoGLFrame::DrawObjects()
00211 {
00212   if(fHist==0) return;
00213   assert(fTridControl.get());
00214 
00215   // Draw everything.
00216   glMatrixMode(GL_MODELVIEW);
00217   
00218   if(!fHist) return;
00219 
00220   // Backing box.
00221   TridSketchPlane backplane ( TVector3(0.5, 0.5, -0.2),
00222                               kv_x, 10.0,
00223                               kv_y, 10.0
00224                               );
00225   backplane.SetColor(fTridControl->GetBackgroundColor());
00226   backplane.Draw();
00227 
00228 
00229   TridSketch::SetDefaultColor(fTridControl->GetForegroundColor());
00230   TridSketch::SetDefaultTrans(1);
00231 
00232   
00233 
00234   //int iminbin = fHist->GetXaxis()->GetFirst()-1;
00235   //int imaxbin = fHist->GetXaxis()->GetLast()+1;
00236   //int nbins   = imaxbin - iminbin;
00237   //double xmin = fHist->GetXaxis()->GetBinLowEdge(iminbin);
00238   //double xmax = fHist->GetXaxis()->GetBinLowEdge(imaxbin);
00239   //double ymin = fHist->GetYaxis()->GetBinLowEdge(0);
00240   //double ymax = fHist->GetMaximum();
00241 
00242   int nbins = fHist->GetNbinsX() + 2;
00243   double xmin,xmax;
00244   if(fHistoType == kColor) {
00245     fTridControl->GetColorRange(xmin,xmax);
00246   } else {
00247     fTridControl->GetTransRange(xmin,xmax);
00248   }
00249 
00250   //double ymin = fHist->GetYaxis()->GetBinLowEdge(0);
00251   double ymin = 0;
00252   double ymax = fHist->GetMaximum();
00253   if(ymax<=0) ymax = 1.0;
00254 
00255   double textScaleX = 1.0;
00256   double textScaleY = 1.0;
00257   if(fAspectRatio>0.4) {
00258     textScaleY = 1.0/fAspectRatio;
00259   } else {
00260     textScaleY = 1.0/0.4;
00261     textScaleX = fAspectRatio/0.4;
00262   }
00263 
00265   // Draw axes.
00266   
00267   // Draw lines.
00268   TridSketchLine xline(TVector3(0,0,0), TVector3(1,0,0));
00269   TridSketchLine yline(TVector3(0,0,0), TVector3(0,1,0));
00270   
00271   xline.Draw();
00272   yline.Draw();
00273 
00274   
00275   // Draw ticks and labels
00276 
00277   // XAxis: Max 20 ticks, each has a label.
00278   char format[10];
00279 
00280   double xTickWidth = GetGoodTickWidth(xmin,xmax,6.0,format);
00281   double xtick = floor(xmin/xTickWidth)*xTickWidth;
00282   while(xtick<xmin) xtick+= xTickWidth;
00283 
00284   while(xtick <= xmax) {
00285     double screenx = (xtick-xmin)/(xmax-xmin);
00286     TridSketchLine tick(TVector3(screenx,0,0),TVector3(screenx,-0.05,0));
00287     tick.Draw();
00288     char buff[10];
00289     sprintf(buff,format,xtick);
00290     TridSketchText ticklabel(TVector3(screenx,-0.05,0),kv_y,kv_z,buff);
00291     ticklabel.SetScale(0.02*textScaleX,0.02*textScaleY);
00292     ticklabel.SetJustify(TridSketchText::kJustifyTop);
00293     ticklabel.Draw();
00294     xtick+= xTickWidth;    
00295   }
00296 
00297   double yTickWidth = GetGoodTickWidth(ymin,ymax,6.0,format);
00298   double ytick = (yTickWidth - fmod(ymin,yTickWidth)) + ymin;
00299     
00300   while(ytick <= ymax) {
00301     double screeny = (ytick-ymin)/(ymax-ymin);
00302     TridSketchLine tick(TVector3(-0.02,screeny,-0.01),TVector3(1,screeny,-0.01));
00303     tick.Draw();
00304     char buff[10];
00305     sprintf(buff,format,ytick);
00306     TridSketchText ticklabel(TVector3(-0.02,screeny,0),kv_y,kv_z,buff);
00307     ticklabel.SetScale(0.02*textScaleX,0.02*textScaleY);
00308     ticklabel.SetJustify(TridSketchText::kJustifyRight);
00309     ticklabel.Draw();
00310     ytick+= yTickWidth;    
00311   }
00312   
00313  
00314   // Draw labels.
00315 
00316   // Draw titles
00317   const char* xtitle_string;
00318   if(fHistoType==kColor) {
00319     xtitle_string =  fTridControl->GetColorModeName();
00320   } else {
00321     xtitle_string =  fTridControl->GetTransModeName();
00322   };
00323   TridSketchText xtitle(TVector3(1.0,-0.15,0),kv_y,kv_z,
00324                           xtitle_string);
00325   xtitle.SetJustify(TridSketchText::kJustifyRight | TridSketchText::kJustifyTop );
00326   xtitle.SetScale(0.03*textScaleX,0.03*textScaleY);
00327   xtitle.Draw();
00328 
00329   TridSketchText ytitle(TVector3(-0.15,1.0,0),kv_x,kv_z,"Counts");
00330   ytitle.SetJustify(TridSketchText::kJustifyRight | TridSketchText::kJustifyBottom );
00331   ytitle.SetScale(0.03*textScaleY,0.03*textScaleX);
00332   ytitle.Draw();
00333 
00334 
00335   TridSketchText histtitle(TVector3(-0.2,-0.3,0),kv_y,kv_z,fHist->GetName());
00336   histtitle.SetColor(1,0.2,0);
00337   histtitle.SetJustify(TridSketchText::kJustifyLeft | TridSketchText::kJustifyBottom );
00338   histtitle.SetScale(0.02*textScaleX,0.02*textScaleY);
00339   histtitle.Draw();
00340  
00342   // Draw bins.
00343 
00344   double pitch = 1.0/(double)(nbins-1);
00345 
00346   glEnable(GL_BLEND);
00347 
00348   for(int i = -1; i<nbins; i++) {
00349     double lowedge = pitch*(double)(i);
00350     double highedge = lowedge + pitch *0.8;
00351  
00352     double top =  (fHist->GetBinContent(i) - ymin)/(ymax-ymin);
00353     if(top>1.05) top = 1.05;
00354 
00355     TVector3 x1(lowedge,  0, 0);
00356     TVector3 x2(highedge, 0, 0);
00357     TVector3 x3(highedge, top, 0);
00358     TVector3 x4(lowedge,  top, 0);
00359     
00360     TridSketchPlane graphbin(x1,x2,x3,x4);
00361     if(fHistoType==kColor) {
00362       graphbin.SetColor(fTridControl->GetColor((lowedge+highedge)*0.5));
00363     } else {
00364       graphbin.SetTransparency(lowedge);
00365     }
00366     graphbin.Draw();
00367   }
00368 
00369   glDisable(GL_BLEND);
00370 }
00371 
00372 void TridHistoGLFrame::DrawHUD(const char*)
00373 {
00374 }

Generated on Thu Nov 1 11:53:30 2007 for loon by  doxygen 1.3.9.1