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);
118 glPolygonOffset(2,2);
119 glEnable(GL_POLYGON_OFFSET_FILL);
121 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
122 glBindTexture(GL_TEXTURE_2D, fTexture);
123 glEnable(GL_TEXTURE_2D);
126 DisplayTexture(iSeg.GetPadWidth(), iSeg.GetPadHeight(), iSeg.GetRLow(),
127 iSeg.GetNMaxPads(), iSeg.GetNRows(),
129 if(fSector->fRnrOut1)
130 DisplayTexture(o1Seg.GetPadWidth(), o1Seg.GetPadHeight(), o1Seg.GetRLow(),
131 o1Seg.GetNMaxPads(), o1Seg.GetNRows(),
132 iSeg.GetNMaxPads(), 0);
133 if(fSector->fRnrOut2)
134 DisplayTexture(o2Seg.GetPadWidth(), o2Seg.GetPadHeight(), o2Seg.GetRLow(),
135 o2Seg.GetNMaxPads(), o2Seg.GetNRows(),
136 0, o1Seg.GetNRows());
138 glDisable(GL_TEXTURE_2D);
141 DisplayQuads(iSeg.GetPadWidth(), iSeg.GetPadHeight(), iSeg.GetRLow(),
142 iSeg.GetNMaxPads(), iSeg.GetNRows(),
144 if(fSector->fRnrOut1)
145 DisplayQuads(o1Seg.GetPadWidth(), o1Seg.GetPadHeight(), o1Seg.GetRLow(),
146 o1Seg.GetNMaxPads(), o1Seg.GetNRows(),
147 iSeg.GetNMaxPads(), 0);
148 if(fSector->fRnrOut2)
149 DisplayQuads(o2Seg.GetPadWidth(), o2Seg.GetPadHeight(), o2Seg.GetRLow(),
150 o2Seg.GetNMaxPads(), o2Seg.GetNRows(),
151 0, o1Seg.GetNRows());
156 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
162 /**************************************************************************/
164 /**************************************************************************/
166 void TPCSector2DGL::LoadPadrow(TPCSectorData::RowIterator& iter,
167 Int_t row, Int_t col_off) const
172 Int_t minTime = fSector->fMinTime;
173 Int_t maxTime = fSector->fMaxTime;
174 Bool_t halfBorderTime = ((maxTime - minTime) % 2 == 0);
176 UChar_t* img_pos = GetRowCol(row, col_off);
177 while (iter.NextPad()) {
180 while (iter.Next()) {
184 if(time < minTime || time > maxTime)
187 if(fSector->fShowMax) {
192 if(halfBorderTime && (time == minTime || time == maxTime))
199 if(fSector->fShowMax == kFALSE && fSector->fAverage) {
200 padVal = (Int_t)((Float_t)padVal / (maxTime - minTime));
202 padVal = TMath::Min(padVal, fSector->fMaxVal);
203 if(padVal > fSector->fThreshold)
204 SetCol(padVal, img_pos);
209 /**************************************************************************/
211 void TPCSector2DGL::CreateTexture() const
214 fImage = new UChar_t[fgkTextureByteSize];
215 glGenTextures(1, &fTexture);
217 memset(fImage, 0, fgkTextureByteSize);
219 Int_t rowOff[3], colOff[3];
222 rowOff[1] = rowOff[2] = -TPCSectorData::GetSeg(1).GetFirstRow();
223 colOff[0] = colOff[2] = 0;
224 colOff[1] = TPCSectorData::GetSeg(0).GetNMaxPads();
225 isOn[0] = fSector->fRnrInn;
226 isOn[1] = fSector->fRnrOut1;
227 isOn[2] = fSector->fRnrOut2;
229 // Loop over 3 main segments
230 for (Int_t sId = 0; sId <= 2; ++sId) {
231 if(isOn[sId] == kFALSE)
233 const TPCSectorData::SegmentInfo& sInfo = TPCSectorData::GetSeg(sId);
234 for (Int_t row=sInfo.GetFirstRow(); row<=sInfo.GetLastRow(); ++row) {
235 TPCSectorData::RowIterator i = fSectorData->MakeRowIterator(row);
236 Int_t offset = (sInfo.GetNMaxPads() - TPCSectorData::GetNPadsInRow(row))/2;
237 LoadPadrow(i, row + rowOff[sId], offset + colOff[sId]);
241 glBindTexture (GL_TEXTURE_2D, fTexture);
242 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
243 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
246 // glTexEnvf (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE, GL_MODULATE); // Lightning is off anyway.
247 glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, fgkTextureWidth, fgkTextureHeight,
248 0, GL_RGBA, GL_UNSIGNED_BYTE, fImage);
252 /**************************************************************************/
254 /**************************************************************************/
256 void TPCSector2DGL::DisplayTexture(Float_t padW, Float_t padH, Float_t startR,
257 Int_t numMaxPads, Int_t numRows,
258 Int_t startCol, Int_t startRow) const
260 Float_t w = numMaxPads*padW/2;
261 Float_t u1 = (Float_t) startCol / fgkTextureWidth;
262 Float_t v1 = (Float_t) startRow / fgkTextureHeight;
263 Float_t u2 = u1 + (Float_t) numMaxPads / fgkTextureWidth;
264 Float_t v2 = v1 + (Float_t) numRows / fgkTextureHeight;
267 glTexCoord2f(u1, v1); glVertex2f(-w, startR);
268 glTexCoord2f(u1, v2); glVertex2f(-w, startR + numRows*padH);
269 glTexCoord2f(u2, v2); glVertex2f( w, startR + numRows*padH);
270 glTexCoord2f(u2, v1); glVertex2f( w, startR);
274 /**************************************************************************/
276 void TPCSector2DGL::DisplayQuads(Float_t padW, Float_t padH, Float_t startR,
277 Int_t numMaxPads, Int_t numRows,
278 Int_t startCol, Int_t startRow) const
285 for (Int_t row=0; row<numRows; row++) {
286 y_d = startR + row*padH;
288 x_off = -numMaxPads*padW/2;
289 pix = GetRowCol(row + startRow, startCol);
290 for (Int_t pad=0; pad<numMaxPads; pad++, pix+=4) {
291 x = x_off + pad*padW;
294 glVertex2f(x+padW, y_d);
297 glVertex2f(x+padW, y_u);
304 /**************************************************************************/
306 /**************************************************************************/
308 void TPCSector2DGL::TraceStepsUp(const TPCSectorData::SegmentInfo& s)
310 Float_t x = -(s.GetNMaxPads()*1.0/2 - s.GetNYSteps())*s.GetPadWidth();
311 Float_t y = s.GetRLow();
313 for (Int_t i=0; i<s.GetNYSteps(); ++i) {
316 x -= s.GetPadWidth();
319 y = s.GetRLow() + s.GetNRows()*s.GetPadHeight();
320 glVertex2f(-s.GetNMaxPads()*s.GetPadWidth()/2, y);
323 void TPCSector2DGL::TraceStepsDown(const TPCSectorData::SegmentInfo& s)
325 Float_t x = s.GetNMaxPads()*s.GetPadWidth()/2;
326 Float_t y = s.GetRLow() + s.GetNRows()*s.GetPadHeight();
328 for (Int_t i=s.GetNYSteps() - 1; i>=0; --i) {
331 x -= s.GetPadWidth();
335 glVertex2f((0.5*s.GetNMaxPads() - s.GetNYSteps())*s.GetPadWidth(), y);
338 void TPCSector2DGL::DisplayFrame() const
340 TColor* c = gROOT->GetColor(fSector->fFrameCol);
343 // x[0] = (UChar_t)(255*c->GetRed()); x[1] = (UChar_t)(255*c->GetGreen());
344 // x[2] = (UChar_t)(255*c->GetBlue()); x[3] = 255;
346 glColor3ub((UChar_t)(255*c->GetRed()),
347 (UChar_t)(255*c->GetGreen()),
348 (UChar_t)(255*c->GetBlue()));
350 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
351 if(fSector->fRnrInn) {
353 TraceStepsUp (TPCSectorData::GetInnSeg());
354 TraceStepsDown(TPCSectorData::GetInnSeg());
357 if(fSector->fRnrOut1) {
359 TraceStepsUp (TPCSectorData::GetOut1Seg());
360 TraceStepsDown(TPCSectorData::GetOut1Seg());
363 if(fSector->fRnrOut2) {
365 TraceStepsUp (TPCSectorData::GetOut2Seg());
366 TraceStepsDown(TPCSectorData::GetOut2Seg());