]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Alieve/ITSModuleStepper.cxx
c7fd4f96f76243e7ec43aaf4cd8281ccb0e8450d
[u/mrichter/AliRoot.git] / EVE / Alieve / ITSModuleStepper.cxx
1 // $Header$
2
3 #include "ITSModuleStepper.h"
4 #include "ITSDigitsInfo.h"
5 #include "ITSScaledModule.h"
6
7 #include "Reve/ReveManager.h"
8 #include "Reve/RGEditor.h"
9 #include "Reve/GridStepper.h"
10 #include "Reve/GLTextNS.h"
11 #include "Reve/ZTrans.h"
12
13 #include <TObject.h>
14
15 #include <TBuffer3D.h>
16 #include <TBuffer3DTypes.h>
17 #include <TVirtualPad.h>
18 #include <TVirtualViewer3D.h>
19
20 #include <TGLRnrCtx.h>
21 #include <TGLSelectRecord.h>
22 #include <TGLText.h>
23 // #include <FTFont.h>
24 #include <TGLAxis.h>
25 #include <TGLViewer.h>
26
27 using namespace Reve;
28 using namespace Alieve;
29
30 //______________________________________________________________________
31 // ITSModuleStepper
32 //
33
34 ClassImp(ITSModuleStepper)
35
36 ITSModuleStepper::ITSModuleStepper(ITSDigitsInfo* di) :
37   RenderElementList("ITS 2DStore", "ITSModuleStepper", kTRUE),
38
39   fPosition(0), 
40     
41   fDigitsInfo(di),
42   fScaleInfo(0),
43
44   fSubDet(-1),
45
46   fStepper(0),
47   fAxis(0),
48   fText(0),
49   fTextSize(0.05),
50   fPagerGap(0.1),
51   fRnrFrame(kFALSE),
52
53   fExpandCell(0.85),
54   fModuleFrameCol(2),
55
56   fPaletteOffset(0.2),
57   fPaletteLength(0.6),
58
59   fWActive(-1),
60   fWWidth(0.025),
61   fWHeight(0.032),
62   fWOff(0.05),
63   fWCol(30),
64   fWActiveCol(45),
65   fFontCol(8)
66 {
67   // override member from base RenderElementList
68   fChildClass = ITSScaledModule::Class();
69
70   SetMainColorPtr(&fWCol);
71
72   fDigitsInfo->IncRefCount();
73
74   fStepper = new GridStepper();
75   fStepper->SetNs(5, 4);
76
77   fScaleInfo = new DigitScaleInfo();
78   fScaleInfo->IncRefCount();
79
80   fAxis = new TGLAxis();
81   fAxis->SetLineColor(4);
82   fAxis->SetTextColor(fFontCol);
83
84   fText = new TGLText();
85   fText->SetTextColor(fFontCol);
86   fText->SetGLTextFont(40);
87   fText->SetGLTextAngles(0, 0, 0);
88   fText->SetTextSize(fTextSize);
89
90   gReve->GetGLViewer()->AddOverlayElement(this);
91 }
92
93 ITSModuleStepper::~ITSModuleStepper()
94 {
95   gReve->GetGLViewer()->RemoveOverlayElement(this);
96
97    fScaleInfo->DecRefCount();
98   fDigitsInfo->DecRefCount();
99
100   delete fStepper;
101
102   delete fAxis;
103   delete fText;
104 }
105
106 /**************************************************************************/
107
108 void ITSModuleStepper::Capacity()
109 {
110   Int_t N = fStepper->Nx*fStepper->Ny;
111   if(N != GetNChildren())
112   {
113     DestroyElements();
114     for(Int_t m=0; m<N; m++) 
115     {
116       AddElement(new ITSScaledModule(m, fDigitsInfo, fScaleInfo));
117     }
118   }
119 }
120
121 /**************************************************************************/
122
123 void ITSModuleStepper::SetFirst(Int_t first)
124 {
125   Int_t lastpage = fIDs.size()/Nxy();
126   if(fIDs.size() % Nxy() ) lastpage++;  
127         
128   Int_t first_lastpage = (lastpage -1)*Nxy();
129   if(first > first_lastpage) first = first_lastpage;
130   if(first < 0) first = 0;
131   fPosition = first;
132   Apply();
133 }
134
135 void ITSModuleStepper::Start()
136 {
137   fPosition = 0;
138   Apply();
139 }
140
141 void ITSModuleStepper::Next()
142 {
143   SetFirst(fPosition + Nxy());
144 }
145
146 void ITSModuleStepper::Previous()
147 {
148   // move to the top left corner first
149   SetFirst(fPosition - Nxy());
150 }
151
152 void ITSModuleStepper::End()
153
154   Int_t lastpage = fIDs.size()/Nxy();
155   if(fIDs.size() % Nxy() ) lastpage++;  
156   fPosition = (lastpage -1)*Nxy();
157
158   fStepper->Reset(); 
159   Apply();
160 }
161
162 /**************************************************************************/
163
164 void ITSModuleStepper::DisplayDet(Int_t det, Int_t layer)
165 {
166   fSubDet = det;
167   fIDs.clear();
168   ITSModuleSelection sel = ITSModuleSelection();
169   sel.fType = det; sel.fLayer=layer;
170   fDigitsInfo->GetModuleIDs(&sel, fIDs);
171   //in reder menu define a space between left and right pager
172   fPagerGap = 1.2*TextLength(Form("%d/%d",GetPages(), GetPages()));
173   Start();
174 }
175
176 /**************************************************************************/
177
178 void ITSModuleStepper::DisplayTheta(Float_t min, Float_t max)
179 {
180   fIDs.clear();
181   ITSModuleSelection sel = ITSModuleSelection();
182   sel.fMaxTheta = max; sel.fMinTheta=min; 
183   fDigitsInfo->GetModuleIDs(&sel, fIDs);
184   Start();
185 }
186
187 /**************************************************************************/
188
189 Int_t ITSModuleStepper::GetCurrentPage()
190 {
191   Int_t idx = fPosition +1; 
192   Int_t n = idx/Nxy();
193   if(idx % Nxy()) n++;
194   return n;
195 }
196
197 /**************************************************************************/
198
199 Int_t ITSModuleStepper::GetPages()
200 {
201   Int_t n = fIDs.size()/Nxy(); 
202   if(fIDs.size() % Nxy()) n++; 
203   return n;
204 }
205   
206 /**************************************************************************/
207
208 void  ITSModuleStepper::Apply()
209 {
210   // printf("ITSModuleStepper::Apply fPosition %d \n", fPosition);
211   gReve->DisableRedraw();
212   Capacity();
213
214   UInt_t idx = fPosition;
215   for(List_i childit=fChildren.begin(); childit!=fChildren.end(); ++childit)
216   {
217     if(idx < fIDs.size()) 
218     {
219       ITSScaledModule* mod = dynamic_cast<ITSScaledModule*>(*childit);
220       mod->SetID(fIDs[idx], kFALSE); 
221       ZTrans& tr = mod->RefHMTrans();
222       tr.UnitTrans();
223       tr.RotateLF(3,2,TMath::PiOver2());
224       tr.RotateLF(1,3,TMath::PiOver2());   
225
226       // scaling 
227       Float_t mz, mx;
228       Float_t* fp = mod->GetFrame()->GetFramePoints();
229       // switch x,z it will be rotated afterwards
230       mx = -2*fp[0];
231       mz = -2*fp[2];
232
233       // fit width first
234       Double_t sx = fStepper->Dx;
235       Double_t sy = (mx*fStepper->Dx)/mz;
236       if(sy > fStepper->Dy)
237       {
238         //      printf("fit width \n");
239         sy =  fStepper->Dy;
240         sx =  (mz*fStepper->Dx)/mx;
241       }
242       Float_t scale = (fExpandCell*sx)/mz;
243       tr.Scale(scale, scale, scale);
244
245       Float_t  p[3];
246       fStepper->GetPosition(p);
247       tr.SetPos(p[0]+0.5*fStepper->Dx, p[1]+0.5*fStepper->Dy, p[2]+0.5*fStepper->Dz);
248   
249       if(mod->GetSubDetID() == 2)
250         mod->SetName(Form("SSD %d", idx));
251       else if(mod->GetSubDetID() == 1)
252         mod->SetName(Form("SDD %d", idx));
253       else
254         mod->SetName(Form("SPD %d", idx));
255       mod->SetRnrSelf(kTRUE);
256       mod->UpdateItems();
257
258       fStepper->Step();
259       idx++;
260     }
261     else {
262       (*childit)->SetRnrSelf(kFALSE);
263     }
264   }
265
266   fStepper->Reset();
267   ElementChanged();
268   gReve->EnableRedraw();
269 }
270
271 /**************************************************************************/
272
273 void ITSModuleStepper::Render(TGLRnrCtx& rnrCtx)
274 {
275   // render everyting in relative coordinates
276   glMatrixMode(GL_PROJECTION);
277   glPushMatrix();
278   glLoadIdentity();
279   if (rnrCtx.Selection())
280   {
281     // Should be
282     // glLoadMatrix(rnrCtx.GetCamera()->GetProjMBase());
283     TGLRect rect(*rnrCtx.GetPickRectangle());
284     rnrCtx.GetCamera()->WindowToViewport(rect);
285     gluPickMatrix(rect.X(), rect.Y(), rect.Width(), rect.Height(),
286                   (Int_t*) rnrCtx.GetCamera()->RefViewport().CArr());
287   }
288    
289   glMatrixMode(GL_MODELVIEW);
290   glPushMatrix();
291   glLoadIdentity();
292
293   GLboolean lightp;
294   glGetBooleanv(GL_LIGHTING, &lightp);
295   if (lightp) glDisable(GL_LIGHTING);
296
297   glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
298   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
299   glDisable(GL_CULL_FACE);
300   glEnable(GL_BLEND);
301   glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 
302   RenderMenu();
303   RenderPalette(fPaletteLength, 1.6*fWWidth, fWHeight*0.6);
304   glPopMatrix();
305   glPopAttrib();
306   
307   if (lightp) glEnable(GL_LIGHTING);
308
309   glMatrixMode(GL_PROJECTION);
310   glPopMatrix();
311
312   glMatrixMode(GL_MODELVIEW);
313   RenderCellIDs();
314 }
315
316
317 /**************************************************************************/
318 // Protected sub-renderers
319 /**************************************************************************/
320
321 //______________________________________________________________________
322 Float_t ITSModuleStepper::TextLength(const char* txt)
323 {
324   Float_t llx, lly, llz, urx, ury, urz;
325   fText->BBox(txt, llx, lly, llz, urx, ury, urz);
326   return (urx-llx)*fTextSize;
327 }
328
329 //______________________________________________________________________
330 void ITSModuleStepper::RenderString(TString string, Int_t id)
331 {
332   Float_t txtY = fWHeight*0.5;
333   Float_t txtl = TextLength(string.Data());
334
335   if(id > 0) glLoadName(id);
336   if(id>0 && fWActive == id)
337     fText->SetTextColor(fWActiveCol);
338   else  
339     fText->SetTextColor(fFontCol);
340
341   
342   if(id>0)
343   { 
344     if(fWActive == id) 
345       fText->SetTextColor(fWActiveCol);
346     else
347       fText->SetTextColor(fFontCol);
348
349     glLoadName(id);
350     Float_t ss = fWWidth*0.4;
351     fText->PaintGLText(ss, txtY, -0.8, string.Data());
352     // box
353     Float_t bw =2*ss+txtl;
354     RenderFrame(bw,fWHeight*2,id);
355     glTranslatef( bw, 0, 0);
356   }
357   else 
358   {
359     fText->SetTextColor(fFontCol);
360     fText->PaintGLText(0, txtY, -0.8, string.Data());
361     glTranslatef(txtl, 0, 0);
362   }
363 }
364
365 //______________________________________________________________________
366 void ITSModuleStepper::RenderFrame(Float_t dx, Float_t dy, Int_t id)
367 {
368   if(fRnrFrame == kFALSE)return;
369
370   glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
371   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
372   UChar_t color[4];
373   if(fWActive == id)
374     ColorFromIdx(fWActiveCol, color);
375   else  
376     ColorFromIdx(fWCol, color);
377   glColor4ubv(color);
378
379   glBegin(GL_QUADS);
380   glVertex2f(0, 0);   glVertex2f(dx, 0);
381   glVertex2f(dx, dy); glVertex2f(0, dy);
382   glEnd();
383   glPopAttrib();
384 }
385
386 //______________________________________________________________________
387 void ITSModuleStepper::RenderSymbol(Float_t dx, Float_t dy, Int_t id)
388 {
389   glLoadName(id);
390
391   UChar_t color[4];
392   if(fWActive == id)
393     ColorFromIdx(fWActiveCol, color);
394   else  
395     ColorFromIdx(fWCol, color);
396   glColor4ubv(color);
397
398   Float_t xs = dx/4, ys = dy/4;
399   if(id == 0) {
400     glBegin(GL_QUADS);
401     glVertex2f(0,ys); glVertex2f(0, ys*3); 
402     glVertex2f(dx, ys*3); glVertex2f(dx, ys);
403     glEnd();
404     return;
405   }
406
407   glBegin(GL_TRIANGLES);
408   switch (id) {
409     case 1:
410     {
411       // left
412       //      glVertex2f(xs*2.5, ys*3); glVertex2f(xs*1.5, ys*2); glVertex2f(xs*2.5, ys);
413       glVertex2f(xs*3, ys*3); glVertex2f(xs*1, ys*2); glVertex2f(xs*3, ys);
414       break;
415     }
416     case 2:
417     {
418       //double left
419       glVertex2f(xs*2, ys*3); glVertex2f(xs, ys*2);    glVertex2f(xs*2, ys);
420       glVertex2f(xs*3, ys*3); glVertex2f(xs*2, ys*2);  glVertex2f(xs*3, ys);
421       break;
422     }
423     case 3:
424     {
425       // right
426       //glVertex2f(xs*1.5, ys); glVertex2f(xs*2.5, ys*2); glVertex2f(xs*1.5, ys*3);
427       glVertex2f(xs*1, ys); glVertex2f(xs*3, ys*2); glVertex2f(xs*1, ys*3);
428       break;
429     }
430     case 4:
431     {
432       // double right
433       glVertex2f(xs, ys);     glVertex2f(xs*2, ys*2);   glVertex2f(xs, ys*3);
434       glVertex2f(xs*2, ys);   glVertex2f(xs*3, ys*2);   glVertex2f(xs*2, ys*3);
435       break;
436     }
437     case 5:
438     {
439       // up
440       glVertex2f(xs, ys*2.5);  glVertex2f(xs*2, ys*3.5); glVertex2f(xs*3, ys*2.5);
441       break;
442     }
443     case 6:
444     {
445       // down
446       glVertex2f(xs, ys*1.5);  glVertex2f(xs*2, ys*0.5); glVertex2f(xs*3, ys*1.5);
447       break;
448     }
449    
450     default:
451       break;
452   }
453   glEnd();
454   glLoadName(0);
455 }
456
457 //______________________________________________________________________
458 void ITSModuleStepper::RenderPalette(Float_t dx, Float_t x, Float_t y)
459 {
460   glPushMatrix();
461   glLoadIdentity();
462   glTranslatef(1 -x- dx, -1+y*4, 0);
463   ITSModule* qs = dynamic_cast<ITSModule*>(*BeginChildren());
464   RGBAPalette* p = qs->GetPalette();
465   glBegin(GL_QUAD_STRIP);
466   glColor4ubv(p->ColorFromValue(p->GetMinVal()));
467   glVertex2f(0, 0);
468   glVertex2f(0, y);
469   if (p->GetMaxVal() > p->GetMinVal() + 1)
470   {
471     Float_t xs = dx/(p->GetMaxVal() - p->GetMinVal());
472     Float_t x0 = xs;
473     for(Int_t i=p->GetMinVal() + 1; i<p->GetMaxVal(); i++) 
474     {
475       glColor4ubv(p->ColorFromValue(i));
476       glVertex2f(x0, 0);
477       glVertex2f(x0, y);
478       x0+=xs;
479     }
480   }
481   glColor4ubv(p->ColorFromValue(p->GetMaxVal()));
482   glVertex2f(dx, 0);
483   glVertex2f(dx, y);
484   glEnd();
485
486   if (p->GetMaxVal() > p->GetMinVal())
487   {
488     glRotatef(-90,1, 0, 0 );
489     Double_t v1[3] = {0., 0., 0.};
490     Double_t v2[3] = {dx, 0, 0.};
491     fAxis->SetLabelsSize(fTextSize/dx);
492     fAxis->PaintGLAxis(v1, v2, p->GetMinVal(), p->GetMaxVal(), 206);
493   }
494   glPopMatrix();
495 }
496
497 //______________________________________________________________________
498 void ITSModuleStepper::RenderMenu()
499 {
500   Float_t ww = 2*fWWidth;
501   Float_t wh = 2*fWHeight;
502
503   // transparent bar
504   Float_t a=0.3;
505   glColor4f(a, a, a, a);
506   Float_t H = 1.9*wh*(1+ 2*fWOff);
507   if(1) {
508     glBegin(GL_QUADS);
509     glVertex3f(-1, -1,   0.1); glVertex3f(-1, -1+H, 0.1); 
510     glVertex3f(1 , -1+H, 0.1); glVertex3f( 1, -1  , 0.1);
511     glEnd();
512   }
513
514   Float_t y_base = -1 + wh*0.35;
515   glTranslatef(-1, y_base, 0.);
516   glPushName(0);
517   // pager
518   glPushMatrix();
519   glTranslatef(ww, 0, 0.);
520   fText->SetTextSize(fTextSize);
521   Float_t soff = ww*1.3;
522   glTranslatef(0, fWOff*wh, 0);
523   RenderSymbol(ww, wh, 2);
524   RenderFrame(ww,wh,2);
525   glTranslatef(soff, 0, 0);
526   RenderSymbol(ww, wh, 1);
527   RenderFrame(ww,wh,1);
528   glTranslatef(soff, 0, 0);
529   // text info
530   { 
531     const char* txt =  Form("%d/%d ", GetCurrentPage(), GetPages());
532     Float_t dx = (fPagerGap - TextLength(txt))*0.5;
533     fText->SetTextColor(fFontCol);
534     fText->PaintGLText(dx, wh*0.25, -0.8, txt);
535   }
536   glTranslatef(fPagerGap, 0, 0);
537
538   RenderSymbol(ww, wh, 3);
539   RenderFrame(ww,wh,3);
540   glTranslatef(soff, 0, 0);
541   RenderSymbol(ww, wh, 4);
542   RenderFrame(ww,wh,4);
543   glTranslatef(2*ww, 0, 0);
544   glPopMatrix();  
545
546   // scale info
547   glPushMatrix();
548   ITSDigitsInfo* di = fDigitsInfo;
549   Int_t scale = fScaleInfo->GetScale() - 1;
550   ITSScaledModule* sm = dynamic_cast<ITSScaledModule*>(*BeginChildren());
551   Int_t cnx = 0, cnz = 0;
552   switch(sm->GetSubDetID())
553   {
554     case 0: 
555       cnx = di->fSPDScaleX[scale], cnz = di->fSPDScaleZ[scale];
556       break;
557     case 1: 
558       cnx = di->fSDDScaleX[scale], cnz = di->fSDDScaleZ[scale];
559       break;
560     case 2:
561       cnx = di->fSSDScale[scale], cnz = 1;
562       break;
563   }
564   glTranslatef(10*ww,0, 0);
565   RenderString(Form("Zoom: "));
566   glPushMatrix();
567   glTranslatef(0, 0.2*wh, 0);
568   RenderSymbol(ww, wh*0.9, 5);
569   glTranslatef(0, 0.4*wh, 0);
570   RenderFrame(ww, wh*0.5, 5);
571   glPopMatrix();
572   RenderSymbol(ww, wh*0.9, 6);
573   RenderFrame(ww, wh*0.5, 6);
574   glTranslatef(ww, 0, 0);
575   RenderString(Form("%dx%d ", cnx, cnz));
576   glPopMatrix();
577
578   //choose detector
579   glPushMatrix();
580   glTranslatef(18*ww, 0, 0);
581   Float_t bs = ww*0.2;
582   RenderString("SPD", 8);  
583   glTranslatef(bs, 0, 0);
584   RenderString("SDD", 9); 
585   glTranslatef(bs, 0, 0);
586   RenderString("SSD", 10);
587   glPopMatrix();
588
589   glPopName();
590 }
591
592 //______________________________________________________________________
593 void ITSModuleStepper::RenderCellIDs()
594 {
595   fText->SetTextSize(fStepper->Dy*0.1);
596   fText->SetTextColor(fFontCol);
597   Double_t x, y, z;
598   Double_t sx, sy, sz;
599   UInt_t idx = fPosition;
600   for (List_i childit=fChildren.begin(); childit!=fChildren.end(); ++childit)
601   {
602     if(idx < fIDs.size()) 
603     { 
604       ITSScaledModule* mod = dynamic_cast<ITSScaledModule*>(*childit);
605       ZTrans& tr = mod->RefHMTrans();
606       TString name = Form("%d",mod->GetID());
607       tr.GetPos(x,y,z);
608       x += fStepper->Dx*0.5;
609       y -= fStepper->Dy*0.5;
610       z += 0.4; // !!! MT hack - cross check with overlay rendering.
611       Float_t llx, lly, llz, urx, ury, urz;
612       fText->BBox(name, llx, lly, llz, urx, ury, urz);
613       tr.GetScale(sx, sy, sz);
614       fText->PaintGLText(x-(urx-llx)*sx, y, z, name);
615       idx++;
616     }
617   }
618 }
619
620
621 /**************************************************************************/
622 // Virtual event handlers from TGLOverlayElement
623 /**************************************************************************/
624
625 //______________________________________________________________________
626 Bool_t ITSModuleStepper::Handle(TGLRnrCtx          & /*rnrCtx*/,
627                                 TGLOvlSelectRecord & rec,
628                                 Event_t            * event)
629 {
630   // Handle overlay event.
631   // Return TRUE if event was handled.
632
633   switch (event->fType)
634   { 
635     case kMotionNotify:
636     {
637       Int_t item = rec.GetN() < 2 ? -1 : (Int_t)rec.GetItem(1);
638       if (fWActive != item) {
639         fWActive = item;
640         return kTRUE;
641       } else {
642         return kFALSE;
643       }
644       break;
645     }
646     case kButtonPress:
647     {
648       if (event->fCode != kButton1) {
649         return kFALSE;
650       }
651       switch (rec.GetItem(1))
652       {
653         case 1:
654           Previous();
655           break;
656         case 2:
657           Start();
658           break;
659         case 3:
660           Next();
661           break;
662         case 4:
663           End();
664           break;
665         case 5:
666         {
667           DigitScaleInfo* si = fScaleInfo;
668           if(si->GetScale() < 5) 
669           {
670             si->ScaleChanged(si->GetScale() + 1);       
671             ElementChanged(kTRUE, kTRUE);
672           }
673           break;
674         }
675         case 6:
676         {
677           DigitScaleInfo* si = fScaleInfo;
678           if(si->GetScale() > 1) 
679           {
680             si->ScaleChanged(si->GetScale() - 1);       
681             ElementChanged(kTRUE, kTRUE);
682           }
683           break;
684         }
685         case 7:
686           gReve->GetEditor()->DisplayRenderElement(*BeginChildren());
687           break;
688
689         case 8:
690             DisplayDet(0, -1);
691           break;
692         case 9: 
693             DisplayDet(1, -1);
694           break;
695         case 10: 
696             DisplayDet(2, -1);
697           break;
698         default:
699           break;
700       }
701       return kTRUE;
702       break;
703     }
704     default:
705       break;
706   } // end switch
707   return kFALSE;
708 }
709
710 //______________________________________________________________________
711 Bool_t ITSModuleStepper::MouseEnter(TGLOvlSelectRecord& /*rec*/)
712 {
713   return kTRUE;
714 }
715
716 //______________________________________________________________________
717 void ITSModuleStepper::MouseLeave()
718 {
719   // Mouse has left the element.
720
721   fWActive = -1;
722