3 #include "TPCSector2DGL.h"
5 #include <Alieve/TPCData.h>
9 #include <TStopwatch.h>
14 using namespace Alieve;
17 // This can be optimized to non-pow-2 values once everybody has GL 1.4.
19 const Int_t TPCSector2DGL::fgkTextureWidth = 256;
20 const Int_t TPCSector2DGL::fgkTextureHeight = 128;
21 const Int_t TPCSector2DGL::fgkTextureByteSize = 4*256*128;
23 /**************************************************************************/
25 TPCSector2DGL::TPCSector2DGL() : TGLObject()
35 TPCSector2DGL::~TPCSector2DGL()
37 if(fImage) delete fImage;
38 if(fTexture) glDeleteTextures(1, &fTexture);
41 /**************************************************************************/
43 Bool_t TPCSector2DGL::SetModel(TObject* obj)
45 if (set_model(obj, "Alieve::TPCSector2D")) {
46 fSector = (TPCSector2D*) fExternalObj;
52 void TPCSector2DGL::SetBBox()
54 set_axis_aligned_bbox(((TPCSector2D*)fExternalObj)->AssertBBox());
57 /**************************************************************************/
59 void TPCSector2DGL::SetCol(Float_t z, UChar_t* pixel) const
62 Int_t n_col = gStyle->GetNumberOfColors();
64 Int_t ci = gStyle->GetColorPalette
65 (TMath::Min(n_col - 1,
66 Int_t((n_col*(z - fSector->fthreshold))/(fSector->fMaxVal - fSector->fthreshold))));
68 TColor* c = gROOT->GetColor(ci);
71 // UChar_t *x = (UChar_t*) &c;
72 pixel[0] = (UChar_t)(255*c->GetRed());
73 pixel[1] = (UChar_t)(255*c->GetGreen());
74 pixel[2] = (UChar_t)(255*c->GetBlue());
79 /**************************************************************************/
81 void TPCSector2DGL::DirectDraw(const TGLDrawFlags& /*flags*/) const
85 // printf("TPCSector2DGL::DirectDraw \n");
87 if(fSector->fTPCData == 0)
90 fSectorData = fSector->fTPCData->GetSectorData(fSector->fSectorID);
92 if(fRTS < fSector->fRTS && fSectorData != 0) {
97 glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT |
98 GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_POLYGON_BIT);
100 glDisable(GL_LIGHTING);
101 glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
102 glEnable(GL_COLOR_MATERIAL);
103 glDisable(GL_CULL_FACE);
106 if(fSectorData != 0) {
108 const TPCSectorData::SegmentInfo& iSeg = TPCSectorData::GetInnSeg();
109 const TPCSectorData::SegmentInfo& o1Seg = TPCSectorData::GetOut1Seg();
110 const TPCSectorData::SegmentInfo& o2Seg = TPCSectorData::GetOut2Seg();
112 if(fSector->fUseTexture) {
115 glDepthMask(GL_FALSE);
116 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
117 glShadeModel(GL_FLAT);
119 glBindTexture (GL_TEXTURE_2D, fTexture);
121 glPolygonOffset(2,2);
122 glEnable(GL_POLYGON_OFFSET_FILL);
124 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
125 glBindTexture(GL_TEXTURE_2D, fTexture);
126 glEnable(GL_TEXTURE_2D);
128 DisplayTexture(iSeg.GetPadWidth(), iSeg.GetPadHeight(), iSeg.GetRLow(),
129 iSeg.GetNMaxPads(), iSeg.GetNRows(),
131 DisplayTexture(o1Seg.GetPadWidth(), o1Seg.GetPadHeight(), o1Seg.GetRLow(),
132 o1Seg.GetNMaxPads(), o1Seg.GetNRows(),
133 iSeg.GetNMaxPads(), 0);
134 DisplayTexture(o2Seg.GetPadWidth(), o2Seg.GetPadHeight(), o2Seg.GetRLow(),
135 o2Seg.GetNMaxPads(), o2Seg.GetNRows(),
136 0, o1Seg.GetNRows());
138 glDisable(GL_TEXTURE_2D);
140 DisplayQuads(iSeg.GetPadWidth(), iSeg.GetPadHeight(), iSeg.GetRLow(),
141 iSeg.GetNMaxPads(), iSeg.GetNRows(),
143 DisplayQuads(o1Seg.GetPadWidth(), o1Seg.GetPadHeight(), o1Seg.GetRLow(),
144 o1Seg.GetNMaxPads(), o1Seg.GetNRows(),
145 iSeg.GetNMaxPads(), 0);
146 DisplayQuads(o2Seg.GetPadWidth(), o2Seg.GetPadHeight(), o2Seg.GetRLow(),
147 o2Seg.GetNMaxPads(), o2Seg.GetNRows(),
148 0, o1Seg.GetNRows());
153 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
159 /**************************************************************************/
161 /**************************************************************************/
163 void TPCSector2DGL::LoadPadrow(TPCSectorData::RowIterator& iter,
164 Int_t row, Int_t col_off) const
169 Int_t min_time = fSector->fMinTime;
170 Int_t max_time = fSector->fMaxTime;
171 Bool_t half_border_time = ((fSector->fMaxTime - fSector->fMinTime) % 2 == 0);
173 UChar_t* img_pos = GetRowCol(row, col_off);
174 while (iter.NextPad()) {
177 while (iter.Next()) {
181 if(fSector->fShowMax) {
186 // Integrate int max_val.
187 if(time >= min_time && time <= max_time) {
188 if(half_border_time && (time == min_time || time == max_time))
196 pad_var = TMath::Min(pad_var, fSector->fMaxVal);
197 if(pad_var > fSector->fthreshold)
198 SetCol(pad_var, img_pos);
203 /**************************************************************************/
205 void TPCSector2DGL::CreateTexture() const
208 fImage = new UChar_t[fgkTextureByteSize];
209 glGenTextures(1, &fTexture);
211 memset(fImage, 0, fgkTextureByteSize);
213 Int_t rowOff[3], colOff[3];
214 rowOff[0] = 0; rowOff[1] = rowOff[2] = -TPCSectorData::GetSeg(1).GetFirstRow();
215 colOff[0] = colOff[2] = 0; colOff[1] = TPCSectorData::GetSeg(0).GetNMaxPads();
217 // Loop over 3 main segments
218 for (Int_t sId = 0; sId <= 2; ++sId) {
219 const TPCSectorData::SegmentInfo& sInfo = TPCSectorData::GetSeg(sId);
220 for (Int_t row=sInfo.GetFirstRow(); row<=sInfo.GetLastRow(); ++row) {
221 TPCSectorData::RowIterator i = fSectorData->MakeRowIterator(row);
222 Int_t offset = (sInfo.GetNMaxPads() - TPCSectorData::GetNPadsInRow(row))/2;
223 LoadPadrow(i, row + rowOff[sId], offset + colOff[sId]);
227 glBindTexture (GL_TEXTURE_2D, fTexture);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232 // glTexEnvf (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_MODULATE); // Lightning is off anyway.
233 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, fgkTextureWidth, fgkTextureHeight,
234 0, GL_RGBA, GL_UNSIGNED_BYTE, fImage);
238 /**************************************************************************/
240 /**************************************************************************/
242 void TPCSector2DGL::DisplayTexture(Float_t padW, Float_t padH, Float_t startR,
243 Int_t numMaxPads, Int_t numRows,
244 Int_t startCol, Int_t startRow) const
246 Float_t w = numMaxPads*padW/2;
247 Float_t u1 = (Float_t) startCol / fgkTextureWidth;
248 Float_t v1 = (Float_t) startRow / fgkTextureHeight;
249 Float_t u2 = u1 + (Float_t) numMaxPads / fgkTextureWidth;
250 Float_t v2 = v1 + (Float_t) numRows / fgkTextureHeight;
253 glTexCoord2f(u1, v1); glVertex2f(-w, startR);
254 glTexCoord2f(u1, v2); glVertex2f(-w, startR + numRows*padH);
255 glTexCoord2f(u2, v2); glVertex2f( w, startR + numRows*padH);
256 glTexCoord2f(u2, v1); glVertex2f( w, startR);
260 /**************************************************************************/
262 void TPCSector2DGL::DisplayQuads(Float_t padW, Float_t padH, Float_t startR,
263 Int_t numMaxPads, Int_t numRows,
264 Int_t startCol, Int_t startRow) const
271 for (Int_t row=0; row<numRows; row++) {
272 y_d = startR + row*padH;
274 x_off = -numMaxPads*padW/2;
275 pix = GetRowCol(row + startRow, startCol);
276 for (Int_t pad=0; pad<numMaxPads; pad++, pix+=4) {
277 x = x_off + pad*padW;
280 glVertex2f(x+padW, y_d);
283 glVertex2f(x+padW, y_u);
290 /**************************************************************************/
292 /**************************************************************************/
294 void TPCSector2DGL::TraceStepsUp(const TPCSectorData::SegmentInfo& s)
296 Float_t x = -(s.GetNMaxPads()*1.0/2 - s.GetNYSteps())*s.GetPadWidth();
297 Float_t y = s.GetRLow();
299 for (Int_t i=0; i<s.GetNYSteps(); ++i) {
302 x -= s.GetPadWidth();
305 y = s.GetRLow() + s.GetNRows()*s.GetPadHeight();
306 glVertex2f(-s.GetNMaxPads()*s.GetPadWidth()/2, y);
309 void TPCSector2DGL::TraceStepsDown(const TPCSectorData::SegmentInfo& s)
311 Float_t x = s.GetNMaxPads()*s.GetPadWidth()/2;
312 Float_t y = s.GetRLow() + s.GetNRows()*s.GetPadHeight();
314 for (Int_t i=s.GetNYSteps() - 1; i>=0; --i) {
317 x -= s.GetPadWidth();
321 glVertex2f((0.5*s.GetNMaxPads() - s.GetNYSteps())*s.GetPadWidth(), y);
324 void TPCSector2DGL::DisplayFrame() const
326 TColor* c = gROOT->GetColor(fSector->fFrameCol);
329 // x[0] = (UChar_t)(255*c->GetRed()); x[1] = (UChar_t)(255*c->GetGreen());
330 // x[2] = (UChar_t)(255*c->GetBlue()); x[3] = 255;
332 glColor3ub((UChar_t)(255*c->GetRed()),
333 (UChar_t)(255*c->GetGreen()),
334 (UChar_t)(255*c->GetBlue()));
336 glBegin(GL_LINE_LOOP);
337 TraceStepsUp (TPCSectorData::GetInnSeg());
338 TraceStepsDown(TPCSectorData::GetInnSeg());
341 glBegin(GL_LINE_LOOP);
342 TraceStepsUp (TPCSectorData::GetOut1Seg());
343 TraceStepsDown(TPCSectorData::GetOut1Seg());
346 glBegin(GL_LINE_STRIP);
347 TraceStepsUp (TPCSectorData::GetOut2Seg());
348 TraceStepsDown(TPCSectorData::GetOut2Seg());