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