Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TSPHE.cxx
Go to the documentation of this file.
1// @(#)root/g3d:$Id$
2// Author: Rene Brun 13/06/97
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 "TSPHE.h"
13#include "TNode.h"
14#include "TBuffer.h"
15#include "TBuffer3D.h"
16#include "TBuffer3DTypes.h"
17#include "TGeometry.h"
18#include "TMath.h"
19
21
22/** \class TSPHE
23\ingroup g3d
24A Sphere.
25
26It has 9 parameters:
27
28 - name: name of the shape
29 - title: shape's title
30 - material: (see TMaterial)
31 - rmin: minimum radius
32 - rmax: maximum radius
33 - themin: theta min
34 - themax: theta max
35 - phimin: phi min
36 - phimax: phi max
37
38ROOT color indx = max(i-i0,j-j0);
39*/
40
41////////////////////////////////////////////////////////////////////////////////
42/// SPHE shape default constructor
43
45{
46 fRmin = 0;
47 fRmax = 0;
48 fThemin = 0;
49 fThemax = 0;
50 fPhimin = 0;
51 fPhimax = 0;
52 fSiTab = nullptr;
53 fCoTab = nullptr;
54 fCoThetaTab = nullptr;
55 fNdiv = 0;
56 fAspectRatio=1.0;
57 faX = faY = faZ = 1.0; // Coeff along Ox
58 fNz = 0;
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// SPHE shape normal constructor
63
64TSPHE::TSPHE(const char *name, const char *title, const char *material, Float_t rmin, Float_t rmax, Float_t themin,
65 Float_t themax, Float_t phimin, Float_t phimax)
66 : TShape(name, title,material)
67{
68 fRmin = rmin;
69 fRmax = rmax;
70 fThemin = themin;
71 fThemax = themax;
72 fPhimin = phimin;
73 fPhimax = phimax;
74
75 fSiTab = nullptr;
76 fCoTab = nullptr;
77 fCoThetaTab = nullptr;
78 fNdiv = 0;
79
80 fAspectRatio=1.0;
81 faX = faY = faZ = 1.0; // Coeff along Ox
82
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// SPHE shape "simplified" constructor
88
89TSPHE::TSPHE(const char *name, const char *title, const char *material, Float_t rmax)
90 : TShape(name, title,material)
91{
92 fRmin = 0;
93 fRmax = rmax;
94 fThemin = 0;
95 fThemax = 180;
96 fPhimin = 0;
97 fPhimax = 360;
98
99 fSiTab = nullptr;
100 fCoTab = nullptr;
101 fCoThetaTab = nullptr;
102 fNdiv = 0;
103
104 fAspectRatio=1.0;
105 faX = faY = faZ = 1.0; // Coeff along Ox
106
108}
109
110////////////////////////////////////////////////////////////////////////////////
111/// SPHE shape default destructor
112
114{
115 if (fCoThetaTab) delete [] fCoThetaTab;
116 if (fSiTab) delete [] fSiTab;
117 if (fCoTab) delete [] fCoTab;
118
119 fCoTab = nullptr;
120 fSiTab = nullptr;
121 fCoThetaTab=nullptr;
122}
123
124////////////////////////////////////////////////////////////////////////////////
125/// Compute distance from point px,py to a PSPHE
126///
127/// Compute the closest distance of approach from point px,py to each
128/// computed outline point of the PSPHE (stolen from PCON).
129
131{
133 Int_t numPoints = 2*n*(fNz+1);
134 return ShapeDistancetoPrimitive(numPoints,px,py);
135}
136
137////////////////////////////////////////////////////////////////////////////////
138/// Set ellipse.
139
140void TSPHE::SetEllipse(const Float_t *factors)
141{
142 if (factors[0] > 0) faX = factors[0];
143 if (factors[1] > 0) faY = factors[1];
144 if (factors[2] > 0) faZ = factors[2];
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Set number of divisions.
149
151{
152 if (GetNumberOfDivisions () == p) return;
153 fNdiv=p;
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// Create SPHE points
160
162{
163 Int_t i, j, n;
164 Int_t indx = 0;
165
167
168 if (points) {
169 if (!fCoTab) MakeTableOfCoSin();
170 Float_t z;
171 for (i = 0; i < fNz+1; i++) {
172 z = fRmin * fCoThetaTab[i]; // fSinPhiTab[i];
174 Float_t zi = fRmin*sithet;
175 for (j = 0; j < n; j++) {
176 points[indx++] = faX*zi * fCoTab[j];
177 points[indx++] = faY*zi * fSiTab[j];
178 points[indx++] = faZ*z;
179 }
180 z = fRmax * fCoThetaTab[i];
181 zi = fRmax*sithet;
182 for (j = 0; j < n; j++) {
183 points[indx++] = faX*zi * fCoTab[j];
184 points[indx++] = faY*zi * fSiTab[j];
185 points[indx++] = faZ*z;
186 }
187 }
188 }
189}
190
191////////////////////////////////////////////////////////////////////////////////
192/// Return total X3D needed by TNode::ls (when called with option "x")
193
194void TSPHE::Sizeof3D() const
195{
196 Int_t n;
197
199 Int_t nz = fNz+1;
200 Bool_t specialCase = kFALSE;
201
202 if (TMath::Abs(TMath::Sin(2*(fPhimax - fPhimin))) <= 0.01) //mark this as a very special case, when
203 specialCase = kTRUE; //we have to draw this PCON like a TUBE
204
205 gSize3D.numPoints += 2*n*nz;
206 gSize3D.numSegs += 4*(nz*n-1+(specialCase == kTRUE));
207 gSize3D.numPolys += 2*(nz*n-1+(specialCase == kTRUE));
208}
209
210////////////////////////////////////////////////////////////////////////////////
211/// Make table of sine and cosine.
212
214{
215 const Double_t pi = TMath::ATan(1) * 4.0;
216 const Double_t ragrad = pi/180.0;
217
218 Float_t dphi = fPhimax - fPhimin;
219 while (dphi > 360) dphi -= 360;
220
221 Float_t dtet = fThemax - fThemin;
222 while (dtet > 180) dtet -= 180;
223
224 Int_t j;
226 if (fCoTab)
227 delete [] fCoTab; // Delete the old tab if any
228 fCoTab = new Double_t [n];
229 if (!fCoTab ) return;
230
231 if (fSiTab)
232 delete [] fSiTab; // Delete the old tab if any
233 fSiTab = new Double_t [n];
234 if (!fSiTab ) return;
235
236 Double_t range = Double_t(dphi * ragrad);
237 Double_t phi1 = Double_t(fPhimin * ragrad);
238 Double_t angstep = range/(n-1);
239
240 Double_t ph = phi1;
241 for (j = 0; j < n; j++)
242 {
243 ph = phi1 + j*angstep;
244 fCoTab[j] = TMath::Cos(ph);
245 fSiTab[j] = TMath::Sin(ph);
246 }
247
248 n = fNz + 1;
249
250 if (fCoThetaTab)
251 delete [] fCoThetaTab; // Delete the old tab if any
252 fCoThetaTab = new Double_t [n];
253 if (!fCoThetaTab ) return;
254
255 range = Double_t(dtet * ragrad);
256 phi1 = Double_t(fThemin * ragrad);
257 angstep = range/(n-1);
258
259 ph = phi1;
260 for (j = 0; j < n; j++)
261 {
262 fCoThetaTab[n-j-1] = TMath::Cos(ph);
263 ph += angstep;
264 }
265
266}
267
268////////////////////////////////////////////////////////////////////////////////
269/// Stream a class object
270
272{
273 if (b.IsReading()) {
274 UInt_t R__s, R__c;
275 Version_t R__v = b.ReadVersion(&R__s, &R__c);
276 if (R__v > 2) {
277 b.ReadClassBuffer(TSPHE::Class(), this, R__v, R__s, R__c);
278 Int_t ndiv = fNdiv;
279 fNdiv = 0;
281 return;
282 }
283 //====process old versions before automatic schema evolution
285 b >> fRmin; // minimum radius
286 b >> fRmax; // maximum radius
287 b >> fThemin; // minimum theta
288 b >> fThemax; // maximum theta
289 b >> fPhimin; // minimum phi
290 b >> fPhimax; // maximum phi
291 Int_t tNdiv; // XXX added by RvdE XXX (fNdiv is set by SetNumberOfDivisions)
292 b >> tNdiv;
293 if (R__v > 1) {
294 b >> faX;
295 b >> faY;
296 b >> faZ;
297 }
298 SetNumberOfDivisions (tNdiv); // XXX added by RvdE
299 b.CheckByteCount(R__s, R__c, TSPHE::IsA());
300 //====end of old versions
301
302 } else {
303 b.WriteClassBuffer(TSPHE::Class(),this);
304 }
305}
306
307////////////////////////////////////////////////////////////////////////////////
308/// Get buffer 3d.
309
310const TBuffer3D & TSPHE::GetBuffer3D(Int_t reqSections) const
311{
312 static TBuffer3D buffer(TBuffer3DTypes::kGeneric);
313
314 TShape::FillBuffer3D(buffer, reqSections);
315
316 // Needed by kRawSizes / kRaw
317 const Int_t n = GetNumberOfDivisions()+1;
318 const Int_t nz = fNz+1;
319 Bool_t specialCase = (TMath::Abs(TMath::Sin(2*(fPhimax - fPhimin))) <= 0.01);
320
321 if (reqSections & TBuffer3D::kRawSizes) {
322 Int_t nbPnts = 2*n*nz;
323 Int_t nbSegs = 4*(nz*n-1+(specialCase == kTRUE));
324 Int_t nbPols = 2*(nz*n-1+(specialCase == kTRUE));
325 if (buffer.SetRawSizes(nbPnts, 3*nbPnts, nbSegs, 3*nbSegs, nbPols, 6*nbPols)) {
327 }
328 }
329 if ((reqSections & TBuffer3D::kRaw) && buffer.SectionsValid(TBuffer3D::kRawSizes)) {
330 // Points
331 SetPoints(buffer.fPnts);
332 if (!buffer.fLocalFrame) {
333 TransformPoints(buffer.fPnts, buffer.NbPnts());
334 }
335
337
338 // Segments
339 Int_t indx = 0;
340 Int_t indx2 = 0;
341 Int_t i, j, k;
342 //inside & outside spheres, number of segments: 2*nz*(n-1)
343 // special case number of segments: 2*nz*n
344 for (i = 0; i < nz*2; i++) {
345 indx2 = i*n;
346 for (j = 1; j < n; j++) {
347 buffer.fSegs[indx++] = c;
348 buffer.fSegs[indx++] = indx2+j-1;
349 buffer.fSegs[indx++] = indx2+j;
350 }
351 if (specialCase) {
352 buffer.fSegs[indx++] = c;
353 buffer.fSegs[indx++] = indx2+j-1;
354 buffer.fSegs[indx++] = indx2;
355 }
356 }
357
358 //bottom & top lines, number of segments: 2*n
359 for (i = 0; i < 2; i++) {
360 indx2 = i*(nz-1)*2*n;
361 for (j = 0; j < n; j++) {
362 buffer.fSegs[indx++] = c;
363 buffer.fSegs[indx++] = indx2+j;
364 buffer.fSegs[indx++] = indx2+n+j;
365 }
366 }
367
368 //inside & outside spheres, number of segments: 2*(nz-1)*n
369 for (i = 0; i < (nz-1); i++) {
370
371 //inside sphere
372 indx2 = i*n*2;
373 for (j = 0; j < n; j++) {
374 buffer.fSegs[indx++] = c+2;
375 buffer.fSegs[indx++] = indx2+j;
376 buffer.fSegs[indx++] = indx2+n*2+j;
377 }
378 //outside sphere
379 indx2 = i*n*2+n;
380 for (j = 0; j < n; j++) {
381 buffer.fSegs[indx++] = c+3;
382 buffer.fSegs[indx++] = indx2+j;
383 buffer.fSegs[indx++] = indx2+n*2+j;
384 }
385 }
386
387 //left & right sections, number of segments: 2*(nz-2)
388 // special case number of segments: 0
389 if (!specialCase) {
390 for (i = 1; i < (nz-1); i++) {
391 for (j = 0; j < 2; j++) {
392 buffer.fSegs[indx++] = c;
393 buffer.fSegs[indx++] = 2*i * n + j*(n-1);
394 buffer.fSegs[indx++] = (2*i+1) * n + j*(n-1);
395 }
396 }
397 }
398
399 // Polygons
400 Int_t m = n - 1 + (specialCase == kTRUE);
401 indx = 0;
402
403 //bottom & top, number of polygons: 2*(n-1)
404 // special case number of polygons: 2*n
405 for (j = 0; j < n-1; j++) {
406 buffer.fPols[indx++] = c+3;
407 buffer.fPols[indx++] = 4;
408 buffer.fPols[indx++] = 2*nz*m+j;
409 buffer.fPols[indx++] = m+j;
410 buffer.fPols[indx++] = 2*nz*m+j+1;
411 buffer.fPols[indx++] = j;
412 }
413 for (j = 0; j < n-1; j++) {
414 buffer.fPols[indx++] = c+3;
415 buffer.fPols[indx++] = 4;
416 buffer.fPols[indx++] = 2*nz*m+n+j;
417 buffer.fPols[indx++] = (nz*2-2)*m+j;
418 buffer.fPols[indx++] = 2*nz*m+n+j+1;
419 buffer.fPols[indx++] = (nz*2-2)*m+m+j;
420 }
421 if (specialCase) {
422 buffer.fPols[indx++] = c+3;
423 buffer.fPols[indx++] = 4;
424 buffer.fPols[indx++] = 2*nz*m+j;
425 buffer.fPols[indx++] = m+j;
426 buffer.fPols[indx++] = 2*nz*m;
427 buffer.fPols[indx++] = j;
428
429 buffer.fPols[indx++] = c+3;
430 buffer.fPols[indx++] = 4;
431 buffer.fPols[indx++] = 2*nz*m+n+j;
432 buffer.fPols[indx++] = (nz*2-2)*m+j;
433 buffer.fPols[indx++] = 2*nz*m+n;
434 buffer.fPols[indx++] = (nz*2-2)*m+m+j;
435 }
436
437 //inside & outside, number of polygons: (nz-1)*2*(n-1)
438 for (k = 0; k < (nz-1); k++) {
439 for (j = 0; j < n-1; j++) {
440 buffer.fPols[indx++] = c;
441 buffer.fPols[indx++] = 4;
442 buffer.fPols[indx++] = 2*k*m+j;
443 buffer.fPols[indx++] = nz*2*m+(2*k+2)*n+j+1;
444 buffer.fPols[indx++] = (2*k+2)*m+j;
445 buffer.fPols[indx++] = nz*2*m+(2*k+2)*n+j;
446 }
447 for (j = 0; j < n-1; j++) {
448 buffer.fPols[indx++] = c+1;
449 buffer.fPols[indx++] = 4;
450 buffer.fPols[indx++] = (2*k+1)*m+j;
451 buffer.fPols[indx++] = nz*2*m+(2*k + 3)*n+j;
452 buffer.fPols[indx++] = (2*k+ 3)*m+j;
453 buffer.fPols[indx++] = nz*2*m+(2*k+3)*n+j+1;
454 }
455
456 if (specialCase) {
457 buffer.fPols[indx++] = c;
458 buffer.fPols[indx++] = 4;
459 buffer.fPols[indx++] = 2*k*m+j;
460 buffer.fPols[indx++] = nz*2*m+(2*k+2)*n+j;
461 buffer.fPols[indx++] = (2*k+2)*m+j;
462 buffer.fPols[indx++] = nz*2*m+(2*k+2)*n;
463
464 buffer.fPols[indx++] = c+1;
465 buffer.fPols[indx++] = 4;
466 buffer.fPols[indx++] = (2*k+1)*m+j;
467 buffer.fPols[indx++] = nz*2*m+(2*k+3)*n+j;
468 buffer.fPols[indx++] = (2*k+3)*m+j;
469 buffer.fPols[indx++] = nz*2*m+(2*k+3)*n;
470 }
471 }
472
473 //left & right sections, number of polygons: 2*(nz-1)
474 // special case number of polygons: 0
475 if (!specialCase) {
476 indx2 = nz*2*(n-1);
477 for (k = 0; k < (nz-1); k++) {
478 buffer.fPols[indx++] = c+2;
479 buffer.fPols[indx++] = 4;
480 buffer.fPols[indx++] = k==0 ? indx2 : indx2+2*nz*n+2*(k-1);
481 buffer.fPols[indx++] = indx2+2*(k+1)*n;
482 buffer.fPols[indx++] = indx2+2*nz*n+2*k;
483 buffer.fPols[indx++] = indx2+(2*k+3)*n;
484
485 buffer.fPols[indx++] = c+2;
486 buffer.fPols[indx++] = 4;
487 buffer.fPols[indx++] = k==0 ? indx2+n-1 : indx2+2*nz*n+2*(k-1)+1;
488 buffer.fPols[indx++] = indx2+(2*k+3)*n+n-1;
489 buffer.fPols[indx++] = indx2+2*nz*n+2*k+1;
490 buffer.fPols[indx++] = indx2+2*(k+1)*n+n-1;
491 }
492
493 buffer.fPols[indx-8] = indx2+n;
494 buffer.fPols[indx-2] = indx2+2*n-1;
495 }
496
498 }
499
500 return buffer;
501}
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
int Int_t
Definition RtypesCore.h:45
short Version_t
Definition RtypesCore.h:65
float Float_t
Definition RtypesCore.h:57
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
double Double_t
Definition RtypesCore.h:59
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:377
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t points
char name[80]
Definition TGX11.cxx:110
#define gSize3D
Definition X3DBuffer.h:40
Generic 3D primitive description class.
Definition TBuffer3D.h:18
Int_t * fPols
Definition TBuffer3D.h:115
UInt_t NbPnts() const
Definition TBuffer3D.h:80
Bool_t SectionsValid(UInt_t mask) const
Definition TBuffer3D.h:67
void SetSectionsValid(UInt_t mask)
Definition TBuffer3D.h:65
Int_t * fSegs
Definition TBuffer3D.h:114
Bool_t fLocalFrame
Definition TBuffer3D.h:90
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.
Double_t * fPnts
Definition TBuffer3D.h:113
Buffer base class used for serializing objects.
Definition TBuffer.h:43
A Sphere.
Definition TSPHE.h:28
Float_t faY
Definition TSPHE.h:46
virtual void SetNumberOfDivisions(Int_t p)
Set number of divisions.
Definition TSPHE.cxx:150
Float_t fPhimax
Definition TSPHE.h:44
~TSPHE() override
SPHE shape default destructor.
Definition TSPHE.cxx:113
Int_t DistancetoPrimitive(Int_t px, Int_t py) override
Compute distance from point px,py to a PSPHE.
Definition TSPHE.cxx:130
Float_t fThemax
Definition TSPHE.h:42
virtual Int_t GetNumberOfDivisions() const
Definition TSPHE.h:67
virtual void MakeTableOfCoSin() const
Make table of sine and cosine.
Definition TSPHE.cxx:213
TSPHE()
SPHE shape default constructor.
Definition TSPHE.cxx:44
TClass * IsA() const override
Definition TSPHE.h:73
Float_t fRmax
Definition TSPHE.h:40
void Streamer(TBuffer &) override
Stream a class object.
Definition TSPHE.cxx:271
void SetPoints(Double_t *points) const override
Create SPHE points.
Definition TSPHE.cxx:161
const TBuffer3D & GetBuffer3D(Int_t reqSections) const override
Get buffer 3d.
Definition TSPHE.cxx:310
Float_t faX
Definition TSPHE.h:45
Int_t fNz
Definition TSPHE.h:35
Float_t fRmin
Definition TSPHE.h:39
Float_t fPhimin
Definition TSPHE.h:43
Float_t faZ
Definition TSPHE.h:47
static TClass * Class()
Double_t * fCoThetaTab
Table of cos(fPhimin) .... cos(Phi)
Definition TSPHE.h:33
Float_t fThemin
Definition TSPHE.h:41
Double_t * fCoTab
Table of sin(fPhimin) .... sin(Phi)
Definition TSPHE.h:32
virtual void SetEllipse(const Float_t *factors)
Set ellipse.
Definition TSPHE.cxx:140
Float_t fAspectRatio
number of sections
Definition TSPHE.h:36
Double_t * fSiTab
Definition TSPHE.h:31
void Sizeof3D() const override
Return total X3D needed by TNode::ls (when called with option "x")
Definition TSPHE.cxx:194
Int_t fNdiv
Table of sin(gThemin) .... cos(Theta)
Definition TSPHE.h:34
This is the base class for all geometry shapes.
Definition TShape.h:35
void Streamer(TBuffer &) override
Stream an object of class TShape.
Definition TShape.cxx:162
Int_t GetBasicColor() const
Get basic color.
Definition TShape.cxx:241
Int_t ShapeDistancetoPrimitive(Int_t numPoints, Int_t px, Int_t py)
Distance to primitive.
Definition TShape.cxx:117
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
void TransformPoints(Double_t *points, UInt_t NbPnts) const
Transform points (LocalToMaster)
Definition TShape.cxx:190
const Int_t n
Definition legend1.C:16
Double_t ATan(Double_t)
Returns the principal value of the arc tangent of x, expressed in radians.
Definition TMath.h:640
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:662
Double_t Cos(Double_t)
Returns the cosine of an angle of x radians.
Definition TMath.h:594
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:588
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123
TMarker m
Definition textangle.C:8