]> git.uio.no Git - u/mrichter/AliRoot.git/blob - GEODB/AliGTube.cxx
This commit was generated by cvs2svn to compensate for changes in r209,
[u/mrichter/AliRoot.git] / GEODB / AliGTube.cxx
1 // -*- C++ -*-
2 // 
3 // 1998/10/19
4 // ---------------------------------------------------------------------------
5 //
6 // AliTube Class
7 //
8 // This file is part of the ALICE Geometry Database .
9 //
10 // By:  Joana E. Santo & David Collados
11 //
12 // ---------------------------------------------------------------------------
13
14 #include <TVirtualPad.h>
15 #include <TView.h>
16 #include <TCanvas.h>
17 #include <iostream.h>
18 #include <TGLKernelABC.h>
19 #include "TROOT.h"
20 #include "AliGTube.h"
21 #include "AliGShape.h"
22
23 ClassImp(AliGTube)
24
25 AliGTube::AliGTube()
26 {
27     /* Default Constructor */
28     fCoTab = NULL;    // Table of cos(fPhi1) .... cos(fPhil+fDphi1)
29     fAspectRatio = 1; // defines  (the ellipse semi-axis in Y)/(the ellipse semi-axis in X)
30     fDz    = 0.;      // half length in z
31     fName  = "";
32     fNdiv  = 0;       // number of segments (precision)
33     fRmax  = 0.;      // ellipse  semi-axis   in  X outside
34     fRmin  = 0.;      // ellipse  semi-axis   in  X inside
35     fSiTab = NULL;    // Table of sin(fPhi1) .... sin(fPhil+fDphi1)
36     fTitle = "";
37 }
38
39 //-------------------------------------------------------------------------
40
41 AliGTube::AliGTube( Text_t *name, Text_t *title, Float_t rmin, Float_t rmax, Float_t dz, Float_t aspect ) : AliGShape(name, title)
42 {
43     /* Constructor */
44     fCoTab       = NULL; // Table of cos(fPhi1) .... cos(fPhil+fDphi1)
45     fAspectRatio = aspect; // defines  (the ellipse semi-axis in Y)/(the ellipse semi-axis in X)
46     fDz          = dz;   // half length in z
47     fNdiv        = 0;    // number of segments (precision)
48     fRmax        = rmax; // ellipse  semi-axis   in  X outside
49     fRmin        = rmin; // ellipse  semi-axis   in  X inside
50     fSiTab       = NULL; // Table of sin(fPhi1) .... sin(fPhil+fDphi1)
51
52     MakeTableOfCoSin();
53 }
54
55
56 //-------------------------------------------------------------------------
57
58 AliGTube::AliGTube( AliGTube *tube ) 
59 {
60     /* Copy Constructor */
61     fCoTab       = NULL; // Table of cos(fPhi1) .... cos(fPhil+fDphi1)
62     fAspectRatio = tube->fAspectRatio; // defines  (the ellipse semi-axis in Y)/(the ellipse semi-axis in X)
63     fDz          = tube->fDz;   // half length in z
64     fColor       = tube->fColor;
65     fName        = tube->fName;
66     fNdiv        = 0;    // number of segments (precision)
67     fRmax        = tube->fRmax; // ellipse  semi-axis   in  X outside
68     fRmin        = tube->fRmin; // ellipse  semi-axis   in  X inside
69     fSiTab       = NULL; // Table of sin(fPhi1) .... sin(fPhil+fDphi1)
70     fTitle       = tube->fTitle;
71
72     MakeTableOfCoSin();
73 }
74
75 //-------------------------------------------------------------------------
76
77 AliGTube::AliGTube(Text_t *name, Text_t *title, Float_t rmax, Float_t dz) : AliGShape(name, title)
78 {
79     /* Tube simplified constructor */
80     fCoTab       = NULL; // Table of cos(fPhi1) .... cos(fPhil+fDphi1)
81     fAspectRatio = 1;    // defines  (the ellipse semi-axis in Y)/(the ellipse semi-axis in X)
82     fDz          = dz;   // half length in z
83     fNdiv        = 0;    // number of segments (precision)
84     fRmax        = rmax; // ellipse  semi-axis   in  X outside
85     fRmin        = 0.;   // ellipse  semi-axis   in  X inside
86     fSiTab       = NULL; // Table of sin(fPhi1) .... sin(fPhil+fDphi1)
87
88     MakeTableOfCoSin();
89 }
90
91 //-------------------------------------------------------------------------
92
93 AliGTube::~AliGTube()
94 {
95     /* Destructor */
96     delete [] fCoTab;
97     delete [] fSiTab;
98 }
99
100 //-------------------------------------------------------------------------
101
102 void AliGTube::MakeTableOfCoSin()
103 {
104     const Double_t PI    = TMath::ATan(1) * 4.0;
105     const Double_t TWOPI = 2*PI;
106
107     Int_t j;
108     Int_t n = GetNumberOfDivisions ();
109
110     if (fCoTab)
111         delete [] fCoTab; // Delete the old tab if any
112     fCoTab = new Double_t [n];
113
114     if (!fCoTab ) {
115         Error("MakeTableOfCoSin()","No cos table done");
116         return;
117     }
118
119     if (fSiTab)
120         delete [] fSiTab; // Delete the old tab if any
121     fSiTab = new Double_t [n];
122
123     if (!fSiTab )
124     {
125         Error("MakeTableOfCoSin()","No sin table done");
126         return;
127     }
128
129     Double_t range   = TWOPI;
130     Double_t angstep = range/n;
131
132     Double_t ph = 0;
133     for (j = 0; j < n; j++)
134     {
135         ph = j*angstep;
136         fCoTab[j] = TMath::Cos(ph);
137         fSiTab[j] = TMath::Sin(ph);
138     }
139
140 }
141
142 //-------------------------------------------------------------------------
143
144 void AliGTube::DrawShape(Option_t *option)
145 {
146     Draw(option);
147     gPad->Update();
148 }
149
150 //-------------------------------------------------------------------------
151
152 void AliGTube::Draw(Option_t *option)
153 {
154     //cout << " Entra en " << this->GetName() << "::Draw " << endl;
155     TString opt = option;
156     opt.ToLower();
157
158     if( !gPad ) {
159         gPad = new TCanvas("AliGTube","AliGTube",0,0,400,300);
160         gPad->Range(0,0,1,1);
161         gPad->SetFillColor(32); // Light Green
162         gPad->SetBorderSize(3);
163         gPad->SetBorderMode(0); // -1 (down) 0 (no) 1 (up)
164     }
165     else {
166         if( !opt.Contains("same") ) {
167             gPad->Clear();
168             gPad->SetName("AliGTube");
169             gPad->SetTitle("AliGTube");
170         }
171         else {
172             gPad->SetName("AliShapes");
173             gPad->SetTitle("AliShapes"); 
174         }
175     }
176
177     AppendPad(option);
178     TView *view = gPad->GetView();
179
180     if (!view)
181         view = new TView(1);
182
183     view->SetAutoRange(kTRUE);
184     Paint(option);
185     view->SetAutoRange(kFALSE);
186
187     cout << " Sale de " << this->GetName() << "::Draw " << endl;
188 }
189
190 //-------------------------------------------------------------------------
191
192 void AliGTube::Paint(Option_t *option)
193 {
194 //*-*-*-*-*-*-*-*Paint this 3-D shape with its current attributes*-*-*-*-*-*-*-*
195 //*-*            ================================================
196
197     SetLineColor( GetCol() );
198         
199     Int_t i, j;
200     Int_t n = GetNumberOfDivisions();
201     const Int_t numpoints = 4*n;
202
203 //*-* Allocate memory for points *-*
204
205     Float_t *points = new Float_t[3*numpoints];
206     if (!points) return;
207   
208     SetPoints(points);
209
210     if (gPad->GetView3D()) PaintGLPoints(points);
211
212 //==   for (i = 0; i < numpoints; i++)
213 //==            gNode->Local2Master(&points[3*i],&points[3*i]);
214
215     X3DBuffer *buff = new X3DBuffer;
216     if (buff) {
217         buff->numPoints = numpoints;
218         buff->numSegs   = n*8;
219         buff->numPolys  = n*4;
220     }
221
222
223 //*-* Allocate memory for points *-*
224
225     buff->points = points;
226
227     Int_t c = ((GetLineColor() % 8) - 1) * 4;     // Basic colors: 0, 1, ... 7
228     if (c < 0) c = 0;
229
230 //*-* Allocate memory for segments *-*
231
232     buff->segs = new Int_t[buff->numSegs*3];
233     if (buff->segs) {
234         for (i = 0; i < 4; i++) {
235             for (j = 0; j < n; j++) {
236                 buff->segs[(i*n+j)*3  ] = c;
237                 buff->segs[(i*n+j)*3+1] = i*n+j;
238                 buff->segs[(i*n+j)*3+2] = i*n+j+1;
239             }
240             buff->segs[(i*n+j-1)*3+2] = i*n;
241         }
242         for (i = 4; i < 6; i++) {
243             for (j = 0; j < n; j++) {
244                 buff->segs[(i*n+j)*3  ] = c+1;
245                 buff->segs[(i*n+j)*3+1] = (i-4)*n+j;
246                 buff->segs[(i*n+j)*3+2] = (i-2)*n+j;
247             }
248         }
249         for (i = 6; i < 8; i++) {
250             for (j = 0; j < n; j++) {
251                 buff->segs[(i*n+j)*3  ] = c;
252                 buff->segs[(i*n+j)*3+1] = 2*(i-6)*n+j;
253                 buff->segs[(i*n+j)*3+2] = (2*(i-6)+1)*n+j;
254             }
255         }
256     }
257
258 //*-* Allocate memory for polygons *-*
259
260     Int_t indx = 0;
261
262     buff->polys = new Int_t[buff->numPolys*6];
263     if (buff->polys) {
264         for (i = 0; i < 2; i++) {
265             for (j = 0; j < n; j++) {
266                 indx = 6*(i*n+j);
267                 buff->polys[indx  ] = c;
268                 buff->polys[indx+1] = 4;
269                 buff->polys[indx+2] = i*n+j;
270                 buff->polys[indx+3] = (4+i)*n+j;
271                 buff->polys[indx+4] = (2+i)*n+j;
272                 buff->polys[indx+5] = (4+i)*n+j+1;
273             }
274             buff->polys[indx+5] = (4+i)*n;
275         }
276         for (i = 2; i < 4; i++) {
277             for (j = 0; j < n; j++) {
278                 indx = 6*(i*n+j);
279                 buff->polys[indx  ] = c+(i-2)*2+1;
280                 buff->polys[indx+1] = 4;
281                 buff->polys[indx+2] = (i-2)*2*n+j;
282                 buff->polys[indx+3] = (4+i)*n+j;
283                 buff->polys[indx+4] = ((i-2)*2+1)*n+j;
284                 buff->polys[indx+5] = (4+i)*n+j+1;
285             }
286             buff->polys[indx+5] = (4+i)*n;
287         }
288     }
289
290     //*-* Paint in the pad
291     //*-* Paint in the pad
292     Bool_t rangeView = strcmp(option,"range")==0 ? kTRUE : kFALSE;
293    
294     PaintShape(buff,rangeView);
295     //PaintShape(buff);
296
297     if (strstr(option, "x3d")) {
298         if(buff && buff->points && buff->segs)
299             FillX3DBuffer(buff);
300         else {
301             gSize3D.numPoints -= buff->numPoints;
302             gSize3D.numSegs   -= buff->numSegs;
303             gSize3D.numPolys  -= buff->numPolys;
304         }
305     }
306     
307    if( points ) delete [] points;
308   
309     //if (buff->points)   delete [] buff->points;
310     
311     if (buff->segs)     delete [] buff->segs;
312     
313     if (buff->polys)    delete [] buff->polys;
314     
315     if (buff)           delete    buff;
316       
317 }
318
319 //-------------------------------------------------------------------------
320
321 void AliGTube::PaintGLPoints(Float_t *vertex)
322 {
323 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*Paint BRIK via OpenGL *-*-*-*-*-*-*-*-*-*-*-*-*
324 //*-*                            =====================
325     gGLKernel->PaintCone(vertex,GetNumberOfDivisions(),2);
326 }
327
328 //-------------------------------------------------------------------------
329
330 void AliGTube::SetPoints(Float_t *buff)
331 {
332 //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*Create TUBE points*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
333 //*-*                          ==================
334
335     Float_t dz;
336     Int_t j, n;
337
338     n = GetNumberOfDivisions();
339
340     dz = fDz;
341
342     Int_t indx = 0;
343
344     if (buff) {
345 //*-* We've to checxk whether the table does exist and create it
346 //*-* since fCoTab/fSiTab are not saved with any TShape::Streamer function
347         if (!fCoTab)   MakeTableOfCoSin();
348
349         for (j = 0; j < n; j++) {
350             buff[indx+6*n] = buff[indx] = fRmin * fCoTab[j];
351             indx++;
352             buff[indx+6*n] = buff[indx] = fAspectRatio*fRmin * fSiTab[j];
353             indx++;
354             buff[indx+6*n] = dz;
355             buff[indx]     =-dz;
356             indx++;
357         }
358         for (j = 0; j < n; j++) {
359             buff[indx+6*n] = buff[indx] = fRmax * fCoTab[j];
360             indx++;
361             buff[indx+6*n] = buff[indx] = fAspectRatio*fRmax * fSiTab[j];
362             indx++;
363             buff[indx+6*n]= dz;
364             buff[indx]    =-dz;
365             indx++;
366         }
367     }
368 }
369
370 //-------------------------------------------------------------------------
371
372 void AliGTube::Sizeof3D() const
373 {
374 //*-*-*-*-*-*Return total X3D size of this shape with its attributes*-*-*-*-*-*-*
375 //*-*        =======================================================
376
377   cout << " Entra en AliGTube::Sizeof3D() " << endl;
378
379     Int_t n = GetNumberOfDivisions();
380
381     gSize3D.numPoints += n*4;
382     gSize3D.numSegs   += n*8;
383     gSize3D.numPolys  += n*4;
384 }
385
386 //-------------------------------------------------------------------------
387
388 void AliGTube::Streamer(TBuffer &R__b)
389 {
390    // Stream an object of class AliGTube.
391
392    if (R__b.IsReading()) {
393       Version_t R__v = R__b.ReadVersion(); if (R__v) { }
394       AliGShape::Streamer(R__b);
395       R__b.ReadArray(fCoTab); //
396       R__b >> fAspectRatio;
397       R__b >> fDz;
398       R__b >> fNdiv;
399       R__b >> fRmax;
400       R__b >> fRmin;
401       R__b.ReadArray(fSiTab); //
402    } else {
403       R__b.WriteVersion(AliGTube::IsA());
404       AliGShape::Streamer(R__b);
405       R__b.WriteArray(fCoTab, GetNumberOfDivisions()); //
406       R__b << fAspectRatio;
407       R__b << fDz;
408       R__b << fNdiv;
409       R__b << fRmax;
410       R__b << fRmin;
411       R__b.WriteArray(fSiTab, GetNumberOfDivisions()); //
412    }
413 }
414
415 //-------------------------------------------------------------------------
416