ROOT logo
// @(#)root/g3d:$Id: TTUBS.cxx 31624 2009-12-08 09:58:40Z couet $
// Author: Nenad Buncic   18/09/95

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TTUBS.h"
#include "TNode.h"
#include "TVirtualPad.h"
#include "TBuffer3D.h"
#include "TBuffer3DTypes.h"
#include "TGeometry.h"
#include "TMath.h"

ClassImp(TTUBS)


//______________________________________________________________________________
// Begin_Html <P ALIGN=CENTER> <IMG SRC="gif/tubs.gif"> </P> End_Html
// TUBS is a segment of a tube. It has 8 parameters:
//
//     - name       name of the shape
//     - title      shape's title
//     - material  (see TMaterial)
//     - rmin       inside radius
//     - rmax       outside radius
//     - dz         half length in z
//     - phi1       starting angle of the segment
//     - phi2       ending angle of the segment
//
//
// NOTE: phi1 should be smaller than phi2. If this is not the case,
//       the system adds 360 degrees to phi2.


//______________________________________________________________________________
TTUBS::TTUBS()
{
   // TUBS shape default constructor

   fPhi1 = 0.;
   fPhi2 = 0.;
}


//______________________________________________________________________________
TTUBS::TTUBS(const char *name, const char *title, const char *material, Float_t rmin,
             Float_t rmax, Float_t dz, Float_t phi1, Float_t phi2)
      : TTUBE(name,title,material,rmin,rmax,dz)
{
   // TUBS shape normal constructor

   fPhi1 = phi1;
   fPhi2 = phi2;
   MakeTableOfCoSin();
}


//______________________________________________________________________________
TTUBS::TTUBS(const char *name, const char *title, const char *material, Float_t rmax, Float_t dz,
               Float_t phi1, Float_t phi2)
      : TTUBE(name,title,material,rmax,dz)
{
   // TUBS shape "simplified" constructor

   fPhi1 = phi1;
   fPhi2 = phi2;
   MakeTableOfCoSin();
}


//______________________________________________________________________________
void TTUBS::MakeTableOfCoSin() const
{
   // Make table of sine and cosine.

   const Double_t pi  = TMath::ATan(1) * 4.0;
   const Double_t ragrad  = pi/180.0;

   Int_t j;
   Int_t n = GetNumberOfDivisions () + 1;

   if (fCoTab)
      delete [] fCoTab; // Delete the old tab if any
      fCoTab = new Double_t [n];
   if (!fCoTab ) return;

   if (fSiTab)
      delete [] fSiTab; // Delete the old tab if any
   fSiTab = new Double_t [n];
   if (!fSiTab ) return;

   Double_t phi1    = Double_t(fPhi1  * ragrad);
   Double_t phi2    = Double_t(fPhi2  * ragrad);

   if (phi1 > phi2 ) phi2 += 2*pi;

   Double_t range = phi2- phi1;

   Double_t angstep = range/(n-1);

   Double_t ph = phi1;
   for (j = 0; j < n; j++) {
      ph = phi1 + j*angstep;
      fCoTab[j] = TMath::Cos(ph);
      fSiTab[j] = TMath::Sin(ph);
   }
}


//______________________________________________________________________________
TTUBS::~TTUBS()
{
   // TUBS shape default destructor
}


//______________________________________________________________________________
Int_t TTUBS::DistancetoPrimitive(Int_t px, Int_t py)
{
   // Compute distance from point px,py to a TUBE
   //
   // Compute the closest distance of approach from point px,py to each
   // computed outline point of the TUBE.

   Int_t n = GetNumberOfDivisions()+1;
   Int_t numPoints = n*4;
   return ShapeDistancetoPrimitive(numPoints,px,py);
}


//______________________________________________________________________________
void TTUBS::SetPoints(Double_t *points) const
{
   // Create TUBS points

   Int_t j, n;
   Int_t indx = 0;
   Float_t dz = TTUBE::fDz;

   n = GetNumberOfDivisions()+1;

   if (points) {
      if (!fCoTab)   MakeTableOfCoSin();
      for (j = 0; j < n; j++) {
         points[indx+6*n] = points[indx] = fRmin * fCoTab[j];
         indx++;
         points[indx+6*n] = points[indx] = fAspectRatio*fRmin * fSiTab[j];
         indx++;
         points[indx+6*n] = dz;
         points[indx]     =-dz;
         indx++;
      }
      for (j = 0; j < n; j++) {
         points[indx+6*n] = points[indx] = fRmax * fCoTab[j];
         indx++;
         points[indx+6*n] = points[indx] = fAspectRatio*fRmax * fSiTab[j];
         indx++;
         points[indx+6*n]= dz;
         points[indx]    =-dz;
         indx++;
      }
   }
}


//______________________________________________________________________________
void TTUBS::Sizeof3D() const
{
   // Return total X3D needed by TNode::ls (when called with option "x")

   Int_t n = GetNumberOfDivisions()+1;

   gSize3D.numPoints += n*4;
   gSize3D.numSegs   += n*8;
   gSize3D.numPolys  += n*4-2;  
}


//_______________________________________________________________________
const TBuffer3D & TTUBS::GetBuffer3D(Int_t reqSections) const
{
   // Get buffer 3d.

   static TBuffer3D buffer(TBuffer3DTypes::kGeneric);

   TShape::FillBuffer3D(buffer, reqSections);

   // TODO: Although we now have a TBuffer3DTubeSeg class for
   // tube segment shapes, we do not use it for old geometry shapes, as 
   // OGL viewer needs various rotation matrix info we can't easily
   // pass yet. To be revisited.

   // We also don't provide a bounding box - as fiddly to calculate
   // leave to viewer to work it out from points

   if (reqSections & TBuffer3D::kRawSizes) {
      const Int_t n = GetNumberOfDivisions()+1;
      Int_t nbPnts = 4*n;
      Int_t nbSegs = 2*nbPnts;
      Int_t nbPols = nbPnts-2;

      if (buffer.SetRawSizes(nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols)) {
         buffer.SetSectionsValid(TBuffer3D::kRawSizes);
      }
   }
   if (reqSections & TBuffer3D::kRaw) {
      // Points
      SetPoints(buffer.fPnts);
      if (!buffer.fLocalFrame) {
         TransformPoints(buffer.fPnts, buffer.NbPnts());
      }

      const Int_t n = GetNumberOfDivisions()+1;
      Int_t i,j;
      Int_t c = GetBasicColor();

      // Segments
      memset(buffer.fSegs, 0, buffer.NbSegs()*3*sizeof(Int_t));
      for (i = 0; i < 4; i++) {
         for (j = 1; j < n; j++) {
            buffer.fSegs[(i*n+j-1)*3  ] = c;
            buffer.fSegs[(i*n+j-1)*3+1] = i*n+j-1;
            buffer.fSegs[(i*n+j-1)*3+2] = i*n+j;
         }
      }
      for (i = 4; i < 6; i++) {
         for (j = 0; j < n; j++) {
            buffer.fSegs[(i*n+j)*3  ] = c+1;
            buffer.fSegs[(i*n+j)*3+1] = (i-4)*n+j;
            buffer.fSegs[(i*n+j)*3+2] = (i-2)*n+j;
         }
      }
      for (i = 6; i < 8; i++) {
         for (j = 0; j < n; j++) {
            buffer.fSegs[(i*n+j)*3  ] = c;
            buffer.fSegs[(i*n+j)*3+1] = 2*(i-6)*n+j;
            buffer.fSegs[(i*n+j)*3+2] = (2*(i-6)+1)*n+j;
         }
      }

      // Polygons
      Int_t indx = 0;
      memset(buffer.fPols, 0, buffer.NbPols()*6*sizeof(Int_t));
      i = 0;
      for (j = 0; j < n-1; j++) {
         buffer.fPols[indx++] = c;
         buffer.fPols[indx++] = 4;
         buffer.fPols[indx++] = (4+i)*n+j+1;
         buffer.fPols[indx++] = (2+i)*n+j;
         buffer.fPols[indx++] = (4+i)*n+j;
         buffer.fPols[indx++] = i*n+j;
      }
      i = 1;
      for (j = 0; j < n-1; j++) {
         buffer.fPols[indx++] = c;
         buffer.fPols[indx++] = 4;
         buffer.fPols[indx++] = i*n+j;
         buffer.fPols[indx++] = (4+i)*n+j;
         buffer.fPols[indx++] = (2+i)*n+j;
         buffer.fPols[indx++] = (4+i)*n+j+1;
      }
      i = 2;
      for (j = 0; j < n-1; j++) {
         buffer.fPols[indx++] = c+i;
         buffer.fPols[indx++] = 4;
         buffer.fPols[indx++] = (i-2)*2*n+j;
         buffer.fPols[indx++] = (4+i)*n+j;
         buffer.fPols[indx++] = ((i-2)*2+1)*n+j;
         buffer.fPols[indx++] = (4+i)*n+j+1;
      }
      i = 3;
      for (j = 0; j < n-1; j++) {
         buffer.fPols[indx++] = c+i;
         buffer.fPols[indx++] = 4;
         buffer.fPols[indx++] = (4+i)*n+j+1;
         buffer.fPols[indx++] = ((i-2)*2+1)*n+j;
         buffer.fPols[indx++] = (4+i)*n+j;
         buffer.fPols[indx++] = (i-2)*2*n+j;
      }
      buffer.fPols[indx++] = c+2;
      buffer.fPols[indx++] = 4;
      buffer.fPols[indx++] = 6*n;
      buffer.fPols[indx++] = 4*n;
      buffer.fPols[indx++] = 7*n;
      buffer.fPols[indx++] = 5*n;
      buffer.fPols[indx++] = c+2;
      buffer.fPols[indx++] = 4;
      buffer.fPols[indx++] = 6*n-1;
      buffer.fPols[indx++] = 8*n-1;
      buffer.fPols[indx++] = 5*n-1;
      buffer.fPols[indx++] = 7*n-1;

      buffer.SetSectionsValid(TBuffer3D::kRaw);
   }
   return buffer;
}
 TTUBS.cxx:1
 TTUBS.cxx:2
 TTUBS.cxx:3
 TTUBS.cxx:4
 TTUBS.cxx:5
 TTUBS.cxx:6
 TTUBS.cxx:7
 TTUBS.cxx:8
 TTUBS.cxx:9
 TTUBS.cxx:10
 TTUBS.cxx:11
 TTUBS.cxx:12
 TTUBS.cxx:13
 TTUBS.cxx:14
 TTUBS.cxx:15
 TTUBS.cxx:16
 TTUBS.cxx:17
 TTUBS.cxx:18
 TTUBS.cxx:19
 TTUBS.cxx:20
 TTUBS.cxx:21
 TTUBS.cxx:22
 TTUBS.cxx:23
 TTUBS.cxx:24
 TTUBS.cxx:25
 TTUBS.cxx:26
 TTUBS.cxx:27
 TTUBS.cxx:28
 TTUBS.cxx:29
 TTUBS.cxx:30
 TTUBS.cxx:31
 TTUBS.cxx:32
 TTUBS.cxx:33
 TTUBS.cxx:34
 TTUBS.cxx:35
 TTUBS.cxx:36
 TTUBS.cxx:37
 TTUBS.cxx:38
 TTUBS.cxx:39
 TTUBS.cxx:40
 TTUBS.cxx:41
 TTUBS.cxx:42
 TTUBS.cxx:43
 TTUBS.cxx:44
 TTUBS.cxx:45
 TTUBS.cxx:46
 TTUBS.cxx:47
 TTUBS.cxx:48
 TTUBS.cxx:49
 TTUBS.cxx:50
 TTUBS.cxx:51
 TTUBS.cxx:52
 TTUBS.cxx:53
 TTUBS.cxx:54
 TTUBS.cxx:55
 TTUBS.cxx:56
 TTUBS.cxx:57
 TTUBS.cxx:58
 TTUBS.cxx:59
 TTUBS.cxx:60
 TTUBS.cxx:61
 TTUBS.cxx:62
 TTUBS.cxx:63
 TTUBS.cxx:64
 TTUBS.cxx:65
 TTUBS.cxx:66
 TTUBS.cxx:67
 TTUBS.cxx:68
 TTUBS.cxx:69
 TTUBS.cxx:70
 TTUBS.cxx:71
 TTUBS.cxx:72
 TTUBS.cxx:73
 TTUBS.cxx:74
 TTUBS.cxx:75
 TTUBS.cxx:76
 TTUBS.cxx:77
 TTUBS.cxx:78
 TTUBS.cxx:79
 TTUBS.cxx:80
 TTUBS.cxx:81
 TTUBS.cxx:82
 TTUBS.cxx:83
 TTUBS.cxx:84
 TTUBS.cxx:85
 TTUBS.cxx:86
 TTUBS.cxx:87
 TTUBS.cxx:88
 TTUBS.cxx:89
 TTUBS.cxx:90
 TTUBS.cxx:91
 TTUBS.cxx:92
 TTUBS.cxx:93
 TTUBS.cxx:94
 TTUBS.cxx:95
 TTUBS.cxx:96
 TTUBS.cxx:97
 TTUBS.cxx:98
 TTUBS.cxx:99
 TTUBS.cxx:100
 TTUBS.cxx:101
 TTUBS.cxx:102
 TTUBS.cxx:103
 TTUBS.cxx:104
 TTUBS.cxx:105
 TTUBS.cxx:106
 TTUBS.cxx:107
 TTUBS.cxx:108
 TTUBS.cxx:109
 TTUBS.cxx:110
 TTUBS.cxx:111
 TTUBS.cxx:112
 TTUBS.cxx:113
 TTUBS.cxx:114
 TTUBS.cxx:115
 TTUBS.cxx:116
 TTUBS.cxx:117
 TTUBS.cxx:118
 TTUBS.cxx:119
 TTUBS.cxx:120
 TTUBS.cxx:121
 TTUBS.cxx:122
 TTUBS.cxx:123
 TTUBS.cxx:124
 TTUBS.cxx:125
 TTUBS.cxx:126
 TTUBS.cxx:127
 TTUBS.cxx:128
 TTUBS.cxx:129
 TTUBS.cxx:130
 TTUBS.cxx:131
 TTUBS.cxx:132
 TTUBS.cxx:133
 TTUBS.cxx:134
 TTUBS.cxx:135
 TTUBS.cxx:136
 TTUBS.cxx:137
 TTUBS.cxx:138
 TTUBS.cxx:139
 TTUBS.cxx:140
 TTUBS.cxx:141
 TTUBS.cxx:142
 TTUBS.cxx:143
 TTUBS.cxx:144
 TTUBS.cxx:145
 TTUBS.cxx:146
 TTUBS.cxx:147
 TTUBS.cxx:148
 TTUBS.cxx:149
 TTUBS.cxx:150
 TTUBS.cxx:151
 TTUBS.cxx:152
 TTUBS.cxx:153
 TTUBS.cxx:154
 TTUBS.cxx:155
 TTUBS.cxx:156
 TTUBS.cxx:157
 TTUBS.cxx:158
 TTUBS.cxx:159
 TTUBS.cxx:160
 TTUBS.cxx:161
 TTUBS.cxx:162
 TTUBS.cxx:163
 TTUBS.cxx:164
 TTUBS.cxx:165
 TTUBS.cxx:166
 TTUBS.cxx:167
 TTUBS.cxx:168
 TTUBS.cxx:169
 TTUBS.cxx:170
 TTUBS.cxx:171
 TTUBS.cxx:172
 TTUBS.cxx:173
 TTUBS.cxx:174
 TTUBS.cxx:175
 TTUBS.cxx:176
 TTUBS.cxx:177
 TTUBS.cxx:178
 TTUBS.cxx:179
 TTUBS.cxx:180
 TTUBS.cxx:181
 TTUBS.cxx:182
 TTUBS.cxx:183
 TTUBS.cxx:184
 TTUBS.cxx:185
 TTUBS.cxx:186
 TTUBS.cxx:187
 TTUBS.cxx:188
 TTUBS.cxx:189
 TTUBS.cxx:190
 TTUBS.cxx:191
 TTUBS.cxx:192
 TTUBS.cxx:193
 TTUBS.cxx:194
 TTUBS.cxx:195
 TTUBS.cxx:196
 TTUBS.cxx:197
 TTUBS.cxx:198
 TTUBS.cxx:199
 TTUBS.cxx:200
 TTUBS.cxx:201
 TTUBS.cxx:202
 TTUBS.cxx:203
 TTUBS.cxx:204
 TTUBS.cxx:205
 TTUBS.cxx:206
 TTUBS.cxx:207
 TTUBS.cxx:208
 TTUBS.cxx:209
 TTUBS.cxx:210
 TTUBS.cxx:211
 TTUBS.cxx:212
 TTUBS.cxx:213
 TTUBS.cxx:214
 TTUBS.cxx:215
 TTUBS.cxx:216
 TTUBS.cxx:217
 TTUBS.cxx:218
 TTUBS.cxx:219
 TTUBS.cxx:220
 TTUBS.cxx:221
 TTUBS.cxx:222
 TTUBS.cxx:223
 TTUBS.cxx:224
 TTUBS.cxx:225
 TTUBS.cxx:226
 TTUBS.cxx:227
 TTUBS.cxx:228
 TTUBS.cxx:229
 TTUBS.cxx:230
 TTUBS.cxx:231
 TTUBS.cxx:232
 TTUBS.cxx:233
 TTUBS.cxx:234
 TTUBS.cxx:235
 TTUBS.cxx:236
 TTUBS.cxx:237
 TTUBS.cxx:238
 TTUBS.cxx:239
 TTUBS.cxx:240
 TTUBS.cxx:241
 TTUBS.cxx:242
 TTUBS.cxx:243
 TTUBS.cxx:244
 TTUBS.cxx:245
 TTUBS.cxx:246
 TTUBS.cxx:247
 TTUBS.cxx:248
 TTUBS.cxx:249
 TTUBS.cxx:250
 TTUBS.cxx:251
 TTUBS.cxx:252
 TTUBS.cxx:253
 TTUBS.cxx:254
 TTUBS.cxx:255
 TTUBS.cxx:256
 TTUBS.cxx:257
 TTUBS.cxx:258
 TTUBS.cxx:259
 TTUBS.cxx:260
 TTUBS.cxx:261
 TTUBS.cxx:262
 TTUBS.cxx:263
 TTUBS.cxx:264
 TTUBS.cxx:265
 TTUBS.cxx:266
 TTUBS.cxx:267
 TTUBS.cxx:268
 TTUBS.cxx:269
 TTUBS.cxx:270
 TTUBS.cxx:271
 TTUBS.cxx:272
 TTUBS.cxx:273
 TTUBS.cxx:274
 TTUBS.cxx:275
 TTUBS.cxx:276
 TTUBS.cxx:277
 TTUBS.cxx:278
 TTUBS.cxx:279
 TTUBS.cxx:280
 TTUBS.cxx:281
 TTUBS.cxx:282
 TTUBS.cxx:283
 TTUBS.cxx:284
 TTUBS.cxx:285
 TTUBS.cxx:286
 TTUBS.cxx:287
 TTUBS.cxx:288
 TTUBS.cxx:289
 TTUBS.cxx:290
 TTUBS.cxx:291
 TTUBS.cxx:292
 TTUBS.cxx:293
 TTUBS.cxx:294
 TTUBS.cxx:295
 TTUBS.cxx:296
 TTUBS.cxx:297
 TTUBS.cxx:298
 TTUBS.cxx:299
 TTUBS.cxx:300
 TTUBS.cxx:301
 TTUBS.cxx:302