ROOT  6.06/09
Reference Guide
TTUBE.cxx
Go to the documentation of this file.
1 // @(#)root/g3d:$Id$
2 // Author: Nenad Buncic 18/09/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "TTUBE.h"
13 #include "TNode.h"
14 #include "TVirtualPad.h"
15 #include "TBuffer3D.h"
16 #include "TBuffer3DTypes.h"
17 #include "TGeometry.h"
18 #include "TClass.h"
19 #include "TMath.h"
20 
22 
23 /** \class TTUBE
24 \ingroup g3d
25 A tube.
26 
27 \image html g3d_tube.png
28 
29 It has 6 parameters:
30 
31  - name: name of the shape
32  - title: shape's title
33  - material: (see TMaterial)
34  - rmin: inside radius
35  - rmax: outside radius
36  - dz: half length in z
37 */
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// TUBE shape default constructor
41 
42 TTUBE::TTUBE()
43 {
44  fCoTab = 0;
45  fSiTab = 0;
46  fAspectRatio = 1;
47  fDz = 0.;
48  fNdiv = 0;
49  fRmin = 0.;
50  fRmax = 0.;
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// TUBE shape normal constructor
55 
56 TTUBE::TTUBE(const char *name, const char *title, const char *material, Float_t rmin, Float_t rmax, Float_t dz,Float_t aspect)
57  : TShape(name, title,material)
58 {
59  fRmin = rmin;
60  fRmax = rmax;
61 
62  fDz = dz;
63  fNdiv = 0;
64 
65  fCoTab = 0;
66  fSiTab = 0;
67 
68  fAspectRatio = aspect;
69 
71 }
72 
73 ////////////////////////////////////////////////////////////////////////////////
74 /// TUBE shape "simplified" constructor
75 
76 TTUBE::TTUBE(const char *name, const char *title, const char *material, Float_t rmax, Float_t dz)
77  : TShape(name, title,material)
78 {
79  fRmin = 0;
80  fRmax = rmax;
81 
82  fDz = dz;
83  fNdiv = 0;
84 
85  fCoTab = 0;
86  fSiTab = 0;
87 
88  fAspectRatio = 1;
89 
91 }
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 ///copy constructor
95 
96 TTUBE::TTUBE(const TTUBE& tu) :
97  TShape(tu),
98  fRmin(tu.fRmin),
99  fRmax(tu.fRmax),
100  fDz(tu.fDz),
101  fNdiv(tu.fNdiv),
102  fAspectRatio(tu.fAspectRatio),
103  fSiTab(tu.fSiTab),
104  fCoTab(tu.fCoTab)
105 {
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// assignment operator
110 
112 {
113  if(this!=&tu) {
114  TShape::operator=(tu);
115  fRmin=tu.fRmin;
116  fRmax=tu.fRmax;
117  fDz=tu.fDz;
118  fNdiv=tu.fNdiv;
119  fAspectRatio=tu.fAspectRatio;
120  fSiTab=tu.fSiTab;
121  fCoTab=tu.fCoTab;
122  }
123  return *this;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Make table of sine and cosine.
128 
129 void TTUBE::MakeTableOfCoSin() const // Internal cache - const so other const fn can use
130 {
131  const Double_t pi = TMath::ATan(1) * 4.0;
132 
133  Int_t j;
135  if (fCoTab) delete [] fCoTab; // Delete the old tab if any
136  fCoTab = new Double_t [n];
137  if (!fCoTab ) {
138  Error("MakeTableOfCoSin()","No cos table done");
139  return;
140  }
141 
142  if (fSiTab) delete [] fSiTab; // Delete the old tab if any
143  fSiTab = new Double_t [n];
144  if (!fSiTab ) {
145  Error("MakeTableOfCoSin()","No sin table done");
146  return;
147  }
148 
149  Double_t range = 2*pi;
150 
151  Double_t angstep = range/n;
152 
153  Double_t ph = 0;
154  for (j = 0; j < n; j++) {
155  ph = j*angstep;
156  fCoTab[j] = TMath::Cos(ph);
157  fSiTab[j] = TMath::Sin(ph);
158  }
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// TUBE shape default destructor
163 
165 {
166  delete [] fCoTab;
167  delete [] fSiTab;
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Compute distance from point px,py to a TUBE
172 ///
173 /// Compute the closest distance of approach from point px,py to each
174 /// computed outline point of the TUBE.
175 
177 {
179  Int_t numPoints = n*4;
180  return ShapeDistancetoPrimitive(numPoints,px,py);
181 }
182 
183 ////////////////////////////////////////////////////////////////////////////////
184 /// Set number of divisions used to draw this tube
185 
187 {
188  fNdiv = ndiv;
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// Create TUBE points
194 
196 {
197  Int_t j, n;
198  Int_t indx = 0;
199 
200  n = GetNumberOfDivisions();
201 
202  if (points) {
203  if (!fCoTab) MakeTableOfCoSin();
204  for (j = 0; j < n; j++) {
205  points[indx+6*n] = points[indx] = fRmin * fCoTab[j];
206  indx++;
207  points[indx+6*n] = points[indx] = fAspectRatio*fRmin * fSiTab[j];
208  indx++;
209  points[indx+6*n] = fDz;
210  points[indx] =-fDz;
211  indx++;
212  }
213  for (j = 0; j < n; j++) {
214  points[indx+6*n] = points[indx] = fRmax * fCoTab[j];
215  indx++;
216  points[indx+6*n] = points[indx] = fAspectRatio*fRmax * fSiTab[j];
217  indx++;
218  points[indx+6*n]= fDz;
219  points[indx] =-fDz;
220  indx++;
221  }
222  }
223 }
224 
225 ////////////////////////////////////////////////////////////////////////////////
226 /// Set segments and polygons.
227 
228 void TTUBE::SetSegsAndPols(TBuffer3D & buffer) const
229 {
230  Int_t i, j;
232  Int_t c = GetBasicColor();
233 
234  for (i = 0; i < 4; i++) {
235  for (j = 0; j < n; j++) {
236  buffer.fSegs[(i*n+j)*3 ] = c;
237  buffer.fSegs[(i*n+j)*3+1] = i*n+j;
238  buffer.fSegs[(i*n+j)*3+2] = i*n+j+1;
239  }
240  buffer.fSegs[(i*n+j-1)*3+2] = i*n;
241  }
242  for (i = 4; i < 6; i++) {
243  for (j = 0; j < n; j++) {
244  buffer.fSegs[(i*n+j)*3 ] = c+1;
245  buffer.fSegs[(i*n+j)*3+1] = (i-4)*n+j;
246  buffer.fSegs[(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  buffer.fSegs[(i*n+j)*3 ] = c;
252  buffer.fSegs[(i*n+j)*3+1] = 2*(i-6)*n+j;
253  buffer.fSegs[(i*n+j)*3+2] = (2*(i-6)+1)*n+j;
254  }
255  }
256 
257  Int_t indx = 0;
258  i=0;
259  for (j = 0; j < n; j++) {
260  indx = 6*(i*n+j);
261  buffer.fPols[indx ] = c;
262  buffer.fPols[indx+1] = 4;
263  buffer.fPols[indx+5] = i*n+j;
264  buffer.fPols[indx+4] = (4+i)*n+j;
265  buffer.fPols[indx+3] = (2+i)*n+j;
266  buffer.fPols[indx+2] = (4+i)*n+j+1;
267  }
268  buffer.fPols[indx+2] = (4+i)*n;
269  i=1;
270  for (j = 0; j < n; j++) {
271  indx = 6*(i*n+j);
272  buffer.fPols[indx ] = c;
273  buffer.fPols[indx+1] = 4;
274  buffer.fPols[indx+2] = i*n+j;
275  buffer.fPols[indx+3] = (4+i)*n+j;
276  buffer.fPols[indx+4] = (2+i)*n+j;
277  buffer.fPols[indx+5] = (4+i)*n+j+1;
278  }
279  buffer.fPols[indx+5] = (4+i)*n;
280  i=2;
281  for (j = 0; j < n; j++) {
282  indx = 6*(i*n+j);
283  buffer.fPols[indx ] = c+i;
284  buffer.fPols[indx+1] = 4;
285  buffer.fPols[indx+2] = (i-2)*2*n+j;
286  buffer.fPols[indx+3] = (4+i)*n+j;
287  buffer.fPols[indx+4] = ((i-2)*2+1)*n+j;
288  buffer.fPols[indx+5] = (4+i)*n+j+1;
289  }
290  buffer.fPols[indx+5] = (4+i)*n;
291  i=3;
292  for (j = 0; j < n; j++) {
293  indx = 6*(i*n+j);
294  buffer.fPols[indx ] = c+i;
295  buffer.fPols[indx+1] = 4;
296  buffer.fPols[indx+5] = (i-2)*2*n+j;
297  buffer.fPols[indx+4] = (4+i)*n+j;
298  buffer.fPols[indx+3] = ((i-2)*2+1)*n+j;
299  buffer.fPols[indx+2] = (4+i)*n+j+1;
300  }
301  buffer.fPols[indx+2] = (4+i)*n;
302 }
303 
304 ////////////////////////////////////////////////////////////////////////////////
305 /// Return total X3D needed by TNode::ls (when called with option "x")
306 
307 void TTUBE::Sizeof3D() const
308 {
310 
311  gSize3D.numPoints += n*4;
312  gSize3D.numSegs += n*8;
313  gSize3D.numPolys += n*4;
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////
317 /// Stream an object of class TTUBE.
318 
319 void TTUBE::Streamer(TBuffer &R__b)
320 {
321  if (R__b.IsReading()) {
322  UInt_t R__s, R__c;
323  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
324  if (R__v > 2) {
325  R__b.ReadClassBuffer(TTUBE::Class(), this, R__v, R__s, R__c);
326  return;
327  }
328  //====process old versions before automatic schema evolution
329  TShape::Streamer(R__b);
330  R__b >> fRmin;
331  R__b >> fRmax;
332  R__b >> fDz;
333  R__b >> fNdiv;
334  if (R__v > 1) R__b >> fAspectRatio;
335  R__b.CheckByteCount(R__s, R__c, TTUBE::IsA());
336  //====end of old versions
337  } else {
338  R__b.WriteClassBuffer(TTUBE::Class(),this);
339  }
340 }
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// Get buffer 3d.
344 
345 const TBuffer3D & TTUBE::GetBuffer3D(Int_t reqSections) const
346 {
347  static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
348 
349  TShape::FillBuffer3D(buffer, reqSections);
350 
351  // TODO: Although we now have a TBuffer3DTube class for
352  // tube shapes, we do not use it for old geometry tube, as
353  // OGL viewer needs various rotation matrix info we can't easily
354  // pass yet. To be revisited.
355 
356  // We also do not fill the bounding box as derived classes can adjust shape
357  // leave up to viewer to work out
358  if (reqSections & TBuffer3D::kRawSizes) {
360  Int_t nbPnts = 4*n;
361  Int_t nbSegs = 8*n;
362  Int_t nbPols = 4*n;
363  if (buffer.SetRawSizes(nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols)) {
364  buffer.SetSectionsValid(TBuffer3D::kRawSizes);
365  }
366  }
367  if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
368  SetPoints(buffer.fPnts);
369  if (!buffer.fLocalFrame) {
370  TransformPoints(buffer.fPnts, buffer.NbPnts());
371  }
372  SetSegsAndPols(buffer);
373  buffer.SetSectionsValid(TBuffer3D::kRaw);
374  }
375  return buffer;
376 }
virtual Int_t WriteClassBuffer(const TClass *cl, void *pointer)=0
Bool_t IsReading() const
Definition: TBuffer.h:81
short Version_t
Definition: RtypesCore.h:61
const double pi
float Float_t
Definition: RtypesCore.h:53
virtual const TBuffer3D & GetBuffer3D(Int_t reqSections) const
Get buffer 3d.
Definition: TTUBE.cxx:345
Buffer base class used for serializing objects.
Definition: TBuffer.h:40
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
int Int_t
Definition: RtypesCore.h:41
virtual void FillBuffer3D(TBuffer3D &buffer, Int_t reqSections) const
We have to set kRawSize (unless already done) to allocate buffer space before kRaw can be filled...
Definition: TShape.cxx:211
Float_t fDz
Definition: TTUBE.h:40
void Class()
Definition: Class.C:29
virtual void SetNumberOfDivisions(Int_t ndiv)
Set number of divisions used to draw this tube.
Definition: TTUBE.cxx:186
virtual void MakeTableOfCoSin() const
Make table of sine and cosine.
Definition: TTUBE.cxx:129
Double_t * fPnts
Definition: TBuffer3D.h:114
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
void SetSectionsValid(UInt_t mask)
Definition: TBuffer3D.h:67
Int_t * fPols
Definition: TBuffer3D.h:116
Int_t ShapeDistancetoPrimitive(Int_t numPoints, Int_t px, Int_t py)
Distance to primitive.
Definition: TShape.cxx:117
Bool_t fLocalFrame
Definition: TBuffer3D.h:92
point * points
Definition: X3DBuffer.c:20
This is the base class for all geometry shapes.
Definition: TShape.h:47
#define gSize3D
Definition: X3DBuffer.h:42
void TransformPoints(Double_t *points, UInt_t NbPnts) const
Transform points (LocalToMaster)
Definition: TShape.cxx:190
virtual ~TTUBE()
TUBE shape default destructor.
Definition: TTUBE.cxx:164
virtual void SetPoints(Double_t *points) const
Create TUBE points.
Definition: TTUBE.cxx:195
TClass * IsA() const
Float_t fRmax
Definition: TTUBE.h:38
Int_t GetBasicColor() const
Get basic color.
Definition: TShape.cxx:241
unsigned int UInt_t
Definition: RtypesCore.h:42
Bool_t SetRawSizes(UInt_t reqPnts, UInt_t reqPntsCapacity, UInt_t reqSegs, UInt_t reqSegsCapacity, UInt_t reqPols, UInt_t reqPolsCapacity)
Set kRaw tessellation section of buffer with supplied sizes.
Definition: TBuffer3D.cxx:357
Generic 3D primitive description class.
Definition: TBuffer3D.h:19
virtual void SetSegsAndPols(TBuffer3D &buffer) const
Set segments and polygons.
Definition: TTUBE.cxx:228
Float_t fRmin
Definition: TTUBE.h:37
Double_t Cos(Double_t)
Definition: TMath.h:424
virtual Int_t ReadClassBuffer(const TClass *cl, void *pointer, const TClass *onfile_class=0)=0
#define ClassImp(name)
Definition: Rtypes.h:279
double Double_t
Definition: RtypesCore.h:55
Float_t fAspectRatio
Definition: TTUBE.h:43
#define name(a, b)
Definition: linkTestLib0.cpp:5
Int_t * fSegs
Definition: TBuffer3D.h:115
UInt_t NbPnts() const
Definition: TBuffer3D.h:82
Double_t Sin(Double_t)
Definition: TMath.h:421
virtual void Sizeof3D() const
Return total X3D needed by TNode::ls (when called with option "x")
Definition: TTUBE.cxx:307
A tube.
Definition: TTUBE.h:34
TTUBE & operator=(const TTUBE &)
assignment operator
Definition: TTUBE.cxx:111
TShape & operator=(const TShape &)
assignment operator
Definition: TShape.cxx:92
Int_t fNdiv
Definition: TTUBE.h:41
const Int_t n
Definition: legend1.C:16
Double_t * fSiTab
Definition: TTUBE.h:46
virtual Int_t DistancetoPrimitive(Int_t px, Int_t py)
Compute distance from point px,py to a TUBE.
Definition: TTUBE.cxx:176
virtual Int_t GetNumberOfDivisions() const
Definition: TTUBE.h:69
Double_t * fCoTab
Table of sin(fPhi1) .... sin(fPhil+fDphi1)
Definition: TTUBE.h:47
virtual Version_t ReadVersion(UInt_t *start=0, UInt_t *bcnt=0, const TClass *cl=0)=0
Bool_t SectionsValid(UInt_t mask) const
Definition: TBuffer3D.h:69
Double_t ATan(Double_t)
Definition: TMath.h:451