]> git.uio.no Git - u/mrichter/AliRoot.git/blame - EVE/Reve/QuadSet.cxx
Main color now set from frame-box (instead of default palette color).
[u/mrichter/AliRoot.git] / EVE / Reve / QuadSet.cxx
CommitLineData
a8b53f69 1// $Header$
2
5a5a1232 3#include "QuadSet.h"
8be1b0cc 4#include "RGBAPalette.h"
5a5a1232 5
6#include <TColor.h>
7
8#include <TBuffer3D.h>
9#include <TBuffer3DTypes.h>
10#include <TGeometry.h>
11#include <TVirtualPad.h>
12#include <TVirtualViewer3D.h>
13
14#include <TROOT.h>
15#include <TRandom.h>
16
17
18using namespace Reve;
19
20/**************************************************************************/
21// Quad
22/**************************************************************************/
23ClassImp(Reve::Quad)
24
25void Quad::ColorFromIdx(Color_t ci)
26{
27 TColor* c = gROOT->GetColor(ci);
28 if(c) {
29 UChar_t *x = (UChar_t*) &color;
30 x[0] = (UChar_t)(255*c->GetRed()); x[1] = (UChar_t)(255*c->GetGreen());
31 x[2] = (UChar_t)(255*c->GetBlue()); x[3] = 255;
32 }
33}
34
265ecb21 35Quad::Quad(TRandom& rnd, Float_t origin, Float_t size) : color(0)
5a5a1232 36{
37 ColorFromIdx(Int_t(30*rnd.Rndm()));
38 Float_t x = 2*origin*(rnd.Rndm() - 0.5);
39 Float_t y = 2*origin*(rnd.Rndm() - 0.5);
40 Float_t z = 2*origin*(rnd.Rndm() - 0.5);
41 Float_t* p = vertices;
42 for(int i=0; i<4; ++i) {
43 p[0] = x + 2*size*(rnd.Rndm() - 0.5);
44 p[1] = y + 2*size*(rnd.Rndm() - 0.5);
45 p[2] = z + 2*size*(rnd.Rndm() - 0.5);
46 p += 3;
47 }
48}
49
50/**************************************************************************/
3aa97c5d 51// OldQuadSet
5a5a1232 52/**************************************************************************/
3aa97c5d 53ClassImp(Reve::OldQuadSet)
5a5a1232 54
55
3aa97c5d 56OldQuadSet::OldQuadSet(const Text_t* n, const Text_t* t) :
606c4ed7 57 TNamed(n, t),
265ecb21 58 fQuads(),
606c4ed7 59 fTrans(false)
60{}
5a5a1232 61
3aa97c5d 62void OldQuadSet::Test(Int_t nquads)
5a5a1232 63{
64 TRandom rnd(0);
65 fQuads.resize(nquads);
66 for(Int_t i=0; i<nquads; ++i) {
67 new (&fQuads[i]) Quad(rnd, 10, 2);
68 }
69}
70
3aa97c5d 71void OldQuadSet::Paint(Option_t* )
5a5a1232 72{
73 TBuffer3D buffer(TBuffer3DTypes::kGeneric);
74
75 // Section kCore
76 buffer.fID = this;
77 buffer.fColor = 1;
78 buffer.fTransparency = 0;
79 buffer.fLocalFrame = fTrans;
80 if (fTrans)
81 memcpy(buffer.fLocalMaster, fMatrix, 16*sizeof(Double_t));
82 buffer.SetSectionsValid(TBuffer3D::kCore);
83
84 // We fill kCore on first pass and try with viewer
85 Int_t reqSections = gPad->GetViewer3D()->AddObject(buffer);
86 if (reqSections == TBuffer3D::kNone) {
3aa97c5d 87 // printf("OldQuadSet::Paint viewer was happy with Core buff3d.\n");
5a5a1232 88 return;
89 }
90
91 if (reqSections & TBuffer3D::kRawSizes) {
92 Int_t nbPnts = fQuads.size()*4;
93 Int_t nbSegs = nbPnts;
94 if (!buffer.SetRawSizes(nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, fQuads.size(), fQuads.size()*6)) {
95 return;
96 }
97 buffer.SetSectionsValid(TBuffer3D::kRawSizes);
98 }
99
100 if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
101 // Points
102 Int_t pidx = 0;
103 for (std::vector<Quad>::iterator i=fQuads.begin(); i!=fQuads.end(); ++i) {
104 for (Int_t k = 0; k < 12; k++ ){
105 buffer.fPnts[pidx] = (*i).vertices[k];
106 pidx++;
107 }
108 }
109
110 // Segments
111 Int_t sidx = 0;
112 for (Int_t q = 0; q < (Int_t)fQuads.size(); ++q) {
113 for (Int_t s = 0; s < 4; ++s ) {
114 buffer.fSegs[3*sidx ] = 4;
115 buffer.fSegs[3*sidx+1] = sidx;
116 if (s == 3)
117 buffer.fSegs[3*sidx+2] = q*4;
118 else
119 buffer.fSegs[3*sidx+2] = sidx + 1;
120 sidx ++;
121 }
122 }
123
124 // Polygons
125 for (Int_t q = 0; q < (Int_t)fQuads.size(); ++q) {
126 buffer.fPols[6*q] = fQuads[q].color;
127 buffer.fPols[6*q +1] = 4;
c4f03896 128 buffer.fPols[6*q +2] = 4*q + 0;
129 buffer.fPols[6*q +3] = 4*q + 1;
130 buffer.fPols[6*q +4] = 4*q + 2;
131 buffer.fPols[6*q +5] = 4*q + 3;
5a5a1232 132 }
133
134 buffer.SetSectionsValid(TBuffer3D::kRaw);
135 buffer.fColor = 5;
136 }
137
138 gPad->GetViewer3D()->AddObject(buffer);
139}
140
141/**************************************************************************/
142
3aa97c5d 143void OldQuadSet::ComputeBBox()
5a5a1232 144{
145 if(fQuads.empty()) {
606c4ed7 146 BBoxZero();
5a5a1232 147 return;
148 }
606c4ed7 149 BBoxInit();
5a5a1232 150 for(std::vector<Quad>::iterator q=fQuads.begin(); q!=fQuads.end(); ++q) {
151 Float_t* p = q->vertices;
152 for(int i=0; i<4; ++i, p+=3)
606c4ed7 153 BBoxCheckPoint(p);
5a5a1232 154 }
155
156 // printf("%s BBox is x(%f,%f), y(%f,%f), z(%f,%f)\n", GetName(),
606c4ed7 157 // fBBox[0], fBBox[1], fBBox[2], fBBox[3], fBBox[4], fBBox[5]);
5a5a1232 158}
8be1b0cc 159
160/**************************************************************************/
161/**************************************************************************/
162/**************************************************************************/
163/**************************************************************************/
164
165//__________________________________________________________________________
166// QuadSet
167//
168// Supports various internal formats that result in rendering of a
169// set of rectangular objects.
170//
171// Names of internal structures and their variables use fixed
172// assignment to x, z, y coordinates; the render types can override
173// this convention and impose Y as a fixed coordinate.
174// For quad modes the deltas are expected to be positive.
175// For line modes negative deltas are ok.
176
177ClassImp(Reve::QuadSet)
178
179QuadSet::QuadSet(const Text_t* n, const Text_t* t) :
180 RenderElement(),
181 TNamed(n, t),
182
183 fQuadType(QT_Undef),
184 fValueIsColor(kFALSE),
185 fDefaultValue(kMinInt),
186 fPlex(),
187 fLastQuad(0),
188
189 fDefWidth(1), fDefHeight(1), fDefCoord(0),
190
191 fFrame (0),
3c67f72c 192 fPalette(0),
193 fRenderMode(RM_AsIs),
194 fHMTrans()
8be1b0cc 195{}
196
197QuadSet::QuadSet(QuadType_e quadType, Bool_t valIsCol, Int_t chunkSize,
198 const Text_t* n, const Text_t* t) :
199 RenderElement(),
200 TNamed(n, t),
201
202 fQuadType(quadType),
203 fValueIsColor(valIsCol),
204 fDefaultValue(valIsCol ? 0 : kMinInt),
205 fPlex(SizeofAtom(quadType), chunkSize),
206 fLastQuad(0),
207
208 fDefWidth(1), fDefHeight(1), fDefCoord(0),
209
210 fFrame (0),
3c67f72c 211 fPalette(0),
212 fRenderMode(RM_AsIs),
213 fHMTrans()
8be1b0cc 214{}
215
216QuadSet::~QuadSet()
217{
218 SetFrame(0);
219 SetPalette(0);
220}
221
222/**************************************************************************/
223
224Int_t QuadSet::SizeofAtom(QuadSet::QuadType_e qt)
225{
226 switch (qt) {
227 case QT_Undef: return 0;
228 case QT_FreeQuad: return sizeof(FreeQuad);
229 case QT_AxisAligned: return sizeof(AAQuad);
230 case QT_AxisAlignedFixedDim: return sizeof(AAFixDimQuad);
231 case QT_AxisAlignedFixedY:
232 case QT_AxisAlignedFixedZ: return sizeof(AAFixZQuad);
233 case QT_AxisAlignedFixedDimY:
234 case QT_AxisAlignedFixedDimZ: return sizeof(AAFixDimZQuad);
235 case QT_LineFixedY:
236 case QT_LineFixedZ: return sizeof(LineFixedZ);
237 }
238 return 0;
239}
240
241/**************************************************************************/
242
243void QuadSet::Reset(QuadSet::QuadType_e quadType, Bool_t valIsCol, Int_t chunkSize)
244{
245 fQuadType = quadType;
246 fValueIsColor = valIsCol;
247 fDefaultValue = valIsCol ? 0 : kMinInt;
248 fPlex.Reset(SizeofAtom(fQuadType), chunkSize);
249}
250
251void QuadSet::RefitPlex()
252{
253 // Instruct underlying memory allocator to regroup itself into a
254 // contiguous memory chunk.
255
256 fPlex.Refit();
257}
258
259/**************************************************************************/
260
261void QuadSet::ScanMinMaxValues(Int_t& min, Int_t& max)
262{
263 if (fValueIsColor || fPlex.Size() == 0) return;
264 min = kMaxInt;
265 max = kMinInt;
266 for (Int_t c=0; c<fPlex.VecSize(); ++c)
267 {
268 Char_t* a = fPlex.Chunk(c);
269 Int_t n = fPlex.NAtoms(c);
270 while (n--)
271 {
272 Int_t v = ((QuadBase*)a)->fValue;
273 if (v < min) min = v;
274 if (v > max) max = v;
275 a += fPlex.S();
276 }
277 }
278 if (min == max)
279 --min;
280}
281
282/**************************************************************************/
283
284void QuadSet::SetFrame(FrameBox* b)
285{
286 if (fFrame == b) return;
287 if (fFrame) fFrame->DecRefCount();
288 fFrame = b;
289 if (fFrame) {
290 fFrame->IncRefCount();
291 SetMainColorPtr(fFrame->PtrFrameColor());
292 } else {
293 SetMainColorPtr(0);
294 }
295}
296
297void QuadSet::SetPalette(RGBAPalette* p)
298{
299 if (fPalette == p) return;
300 if (fPalette) fPalette->DecRefCount();
301 fPalette = p;
c4f03896 302 if (fPalette) fPalette->IncRefCount();
8be1b0cc 303}
304
305/**************************************************************************/
306
307QuadSet::QuadBase* QuadSet::NewQuad()
308{
309 fLastQuad = new (fPlex.NewAtom()) QuadBase(fDefaultValue);
310 return fLastQuad;
311}
312
313void QuadSet::AddQuad(Float_t* verts)
314{
315 static const Exc_t eH("QuadSet::AddQuad ");
316 if (fQuadType != QT_FreeQuad)
317 throw(eH + "expect free quad-type.");
318
319 FreeQuad* fq = (FreeQuad*) NewQuad();
320 memcpy(fq->fVertices, verts, sizeof(fq->fVertices));
321}
322
323void QuadSet::AddQuad(Float_t x, Float_t y)
324{
325 AddQuad(x, y, fDefCoord, fDefWidth, fDefHeight);
326}
327
328void QuadSet::AddQuad(Float_t x, Float_t y, Float_t z)
329{
330 AddQuad(x, y, z, fDefWidth, fDefHeight);
331}
332
333void QuadSet::AddQuad(Float_t x, Float_t y, Float_t w, Float_t h)
334{
335 AddQuad(x, y, fDefCoord, w, h);
336}
337
338void QuadSet::AddQuad(Float_t x, Float_t y, Float_t z, Float_t w, Float_t h)
339{
340 static const Exc_t eH("QuadSet::AddAAQuad ");
341
342 AAFixDimZQuad& fq = * (AAFixDimZQuad*) NewQuad();
343 fq.fX = x; fq.fY = y;
344 switch (fQuadType)
345 {
346 case QT_AxisAligned: {
347 AAQuad& q = (AAQuad&) fq;
348 q.fZ = z; q.fW = w; q.fH = h;
349 break;
350 }
351 case QT_AxisAlignedFixedDim: {
352 AAFixDimQuad& q = (AAFixDimQuad&) fq;
353 q.fZ = z;
354 break;
355 }
356 case QT_AxisAlignedFixedY:
357 case QT_AxisAlignedFixedZ: {
358 AAFixZQuad& q = (AAFixZQuad&) fq;
359 q.fW = w; q.fH = h;
360 break;
361 }
362 case QT_AxisAlignedFixedDimY:
363 case QT_AxisAlignedFixedDimZ: {
364 break;
365 }
366 case QT_LineFixedY:
367 case QT_LineFixedZ: {
368 LineFixedZ& q = (LineFixedZ&) fq;
369 q.fDx = w; q.fDy = h;
370 break;
371 }
372 default:
373 throw(eH + "expect axis-aligned quad-type.");
374 }
375}
376
377/**************************************************************************/
378
379void QuadSet::QuadValue(Int_t value)
380{
381 fLastQuad->fValue = value;
382}
383
384void QuadSet::QuadColor(Color_t ci)
385{
386 ColorFromIdx(ci, (UChar_t*) & fLastQuad->fValue, kTRUE);
387}
388
389void QuadSet::QuadColor(UChar_t r, UChar_t g, UChar_t b, UChar_t a)
390{
391 UChar_t* x = (UChar_t*) & fLastQuad->fValue;
392 x[0] = r; x[1] = g; x[2] = b; x[3] = a;
393}
394
395/**************************************************************************/
396/**************************************************************************/
397
398void QuadSet::ComputeBBox()
399{
400 static const Exc_t eH("QuadSet::ComputeBBox ");
401
402 // !!!! Missing handling of FrameBox !!!!
403 // It shoud even simpify things ...
404
405 if(fPlex.Size() == 0) {
406 BBoxZero();
407 return;
408 }
409
410 BBoxInit();
411 if (fQuadType == QT_AxisAlignedFixedZ ||
412 fQuadType == QT_AxisAlignedFixedDimZ)
413 {
414 fBBox[4] = fDefCoord;
415 fBBox[5] = fDefCoord;
416 }
417 else if (fQuadType == QT_AxisAlignedFixedY ||
418 fQuadType == QT_AxisAlignedFixedDimY)
419 {
420 fBBox[2] = fDefCoord;
421 fBBox[3] = fDefCoord;
422 }
423
424 for (Int_t c=0; c<fPlex.VecSize(); ++c)
425 {
426 QuadBase* qbp = (QuadBase*) fPlex.Chunk(c);
427 Int_t n = fPlex.NAtoms(c);
428
429 switch (fQuadType)
430 {
431
432 case QT_FreeQuad:
433 {
434 FreeQuad* qp = (FreeQuad*) qbp;
435 while (n--) {
436 Float_t* p = qp->fVertices;
437 BBoxCheckPoint(p); p += 3;
438 BBoxCheckPoint(p); p += 3;
439 BBoxCheckPoint(p); p += 3;
440 BBoxCheckPoint(p);
441 ++qp;
442 }
443 break;
444 }
445
446 case QT_AxisAligned:
447 {
448 AAQuad* qp = (AAQuad*) qbp;
449 while (n--) {
450 AAQuad& q = * qp;
451 if(q.fX < fBBox[0]) fBBox[0] = q.fX;
452 if(q.fX + q.fW > fBBox[1]) fBBox[1] = q.fX + q.fW;
453 if(q.fY < fBBox[2]) fBBox[2] = q.fY;
454 if(q.fY + q.fH > fBBox[3]) fBBox[3] = q.fY + q.fH;
455 if(q.fZ < fBBox[4]) fBBox[4] = q.fZ;
456 if(q.fZ > fBBox[5]) fBBox[5] = q.fZ;
457 ++qp;
458 }
459 break;
460 }
461
462 case QT_AxisAlignedFixedDim:
463 {
464 AAFixDimQuad* qp = (AAFixDimQuad*) qbp;
465 const Float_t& w = fDefWidth;
466 const Float_t& h = fDefHeight;
467 while (n--) {
468 AAFixDimQuad& q = * qp;
469 if(q.fX < fBBox[0]) fBBox[0] = q.fX;
470 if(q.fX + w > fBBox[1]) fBBox[1] = q.fX + w;
471 if(q.fY < fBBox[2]) fBBox[2] = q.fY;
472 if(q.fY + h > fBBox[3]) fBBox[3] = q.fY + h;
473 if(q.fZ < fBBox[4]) fBBox[4] = q.fZ;
474 if(q.fZ > fBBox[5]) fBBox[5] = q.fZ;
475 ++qp;
476 }
477 break;
478 }
479
480 case QT_AxisAlignedFixedZ:
481 {
482 AAFixZQuad* qp = (AAFixZQuad*) qbp;
483 while (n--) {
484 AAFixZQuad& q = * qp;
485 if(q.fX < fBBox[0]) fBBox[0] = q.fX;
486 if(q.fX + q.fW > fBBox[1]) fBBox[1] = q.fX + q.fW;
487 if(q.fY < fBBox[2]) fBBox[2] = q.fY;
488 if(q.fY + q.fH > fBBox[3]) fBBox[3] = q.fY + q.fH;
489 ++qp;
490 }
491 break;
492 }
493
494 case QT_AxisAlignedFixedY:
495 {
496 AAFixZQuad* qp = (AAFixZQuad*) qbp;
497 while (n--) {
498 AAFixZQuad& q = * qp;
499 if(q.fX < fBBox[0]) fBBox[0] = q.fX;
500 if(q.fX + q.fW > fBBox[1]) fBBox[1] = q.fX + q.fW;
501 if(q.fY < fBBox[4]) fBBox[4] = q.fY;
502 if(q.fY + q.fH > fBBox[5]) fBBox[5] = q.fY + q.fH;
503 ++qp;
504 }
505 break;
506 }
507
508 case QT_AxisAlignedFixedDimZ:
509 {
510 AAFixDimZQuad* qp = (AAFixDimZQuad*) qbp;
511 const Float_t& w = fDefWidth;
512 const Float_t& h = fDefHeight;
513 while (n--) {
514 AAFixDimZQuad& q = * qp;
515 if(q.fX < fBBox[0]) fBBox[0] = q.fX;
516 if(q.fX + w > fBBox[1]) fBBox[1] = q.fX + w;
517 if(q.fY < fBBox[2]) fBBox[2] = q.fY;
518 if(q.fY + h > fBBox[3]) fBBox[3] = q.fY + h;
519 ++qp;
520 }
521 break;
522 }
523
524 case QT_AxisAlignedFixedDimY:
525 {
526 AAFixDimZQuad* qp = (AAFixDimZQuad*) qbp;
527 const Float_t& w = fDefWidth;
528 const Float_t& h = fDefHeight;
529 while (n--) {
530 AAFixDimZQuad& q = * qp;
531 if(q.fX < fBBox[0]) fBBox[0] = q.fX;
532 if(q.fX + w > fBBox[1]) fBBox[1] = q.fX + w;
533 if(q.fY < fBBox[4]) fBBox[4] = q.fY;
534 if(q.fY + h > fBBox[5]) fBBox[5] = q.fY + h;
535 ++qp;
536 }
537 break;
538 }
539
540 case QT_LineFixedZ:
541 {
542 LineFixedZ* qp = (LineFixedZ*) qbp;
543 while (n--) {
544 LineFixedZ& q = * qp;
545 BBoxCheckPoint(q.fX, q.fY, fDefCoord);
546 BBoxCheckPoint(q.fX + q.fDx, q.fY + q.fDy, fDefCoord);
547 ++qp;
548 }
549 break;
550 }
551
552 case QT_LineFixedY:
553 {
554 LineFixedZ* qp = (LineFixedZ*) qbp;
555 while (n--) {
556 LineFixedZ& q = * qp;
557 BBoxCheckPoint(q.fX, fDefCoord, q.fY);
558 BBoxCheckPoint(q.fX + q.fDx, fDefCoord, q.fY + q.fDy);
559 ++qp;
560 }
561 break;
562 }
563
564 default: {
565 throw(eH + "unsupported quad-type.");
566 }
567
568 } // end switch quad-type
569
570 } // end for chunk
571
572}
573
574/**************************************************************************/
575
576void QuadSet::Paint(Option_t* /*option*/)
577{
578 static const Exc_t eH("QuadSet::Paint ");
579
580 TBuffer3D buff(TBuffer3DTypes::kGeneric);
581
582 // Section kCore
583 buff.fID = this;
584 buff.fColor = 1;
585 buff.fTransparency = 0;
586 fHMTrans.SetBuffer3D(buff);
587 buff.SetSectionsValid(TBuffer3D::kCore);
588
589 Int_t reqSections = gPad->GetViewer3D()->AddObject(buff);
590 if (reqSections != TBuffer3D::kNone)
591 Error(eH, "only direct GL rendering supported.");
592}
593
594/**************************************************************************/