Logo ROOT  
Reference Guide
glvox2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_gl
3 /// This macro demonstrates how to use "glcol" option for TH3
4 /// and how to create user defined TRANSFER FUNCTION:
5 /// transfer function maps bin value to voxel's opacity.
6 /// codomain is [0, 1] (1. - non-transparent, 0.5 is semitransparent, etc.)
7 /// To pass transparency function into painting algorithm, you have to:
8 /// 1. Create TF1 object (with symbolic expression like "0.5 * (sin(x) + 1)":
9 /// ~~~{.cpp}
10 /// ...
11 /// TF1 * tf = new TF1("TransferFunction", "0.5 * (sin(x) + 1)", -10., 10.);
12 /// ...
13 /// ~~~
14 /// IMPORTANT, the name of TF1 object MUST be "TransferFunction".
15 /// 2. Add this function into a hist's list of functions:
16 /// ~~~{.cpp}
17 /// ...
18 /// TList * lof = hist->GetListOfFunctions();
19 /// if (lof) lof->Add(tf);
20 /// ...
21 /// ~~~
22 /// It's also possible to use your own function and pass it into TF1, please read
23 /// TF1 documentation to learn how.
24 ///
25 /// This macro is to be compiled: TF1 is extremely slow with CINT's function
26 /// as an argument.
27 ///
28 /// \macro_image(nobatch)
29 /// \macro_code
30 ///
31 /// \author Timur Pocheptsov
32 
33 
34 #include "TStyle.h"
35 #include "TList.h"
36 #include "TH3.h"
37 #include "TF1.h"
38 
39 namespace {
40 
41 Double_t my_transfer_function(const Double_t *x, const Double_t * /*param*/)
42 {
43  // Bin values in our example range from -2 to 1.
44  // Let's make values from -2. to -1.5 more transparent:
45  if (*x < -1.5)
46  return 0.08;
47 
48  if (*x > -1.5 && *x < -0.5)
49  return 0.015;
50 
51  if (*x < -0.5 && *x < 0.)
52  return 0.02;
53 
54  if (*x > 0 && *x < 0.5)
55  return 0.03;
56 
57  if (*x > 0.8)
58  return 0.01;
59 
60  return 0.2;
61 }
62 
63 }
64 
65 void glvox2()
66 {
67  //Create and fill TH3.
68  const UInt_t nX = 30;
69  const Double_t xMin = -1., xMax = 1., xStep = (xMax - xMin) / (nX - 1);
70 
71  const UInt_t nY = 30;
72  const Double_t yMin = -1., yMax = 1., yStep = (yMax - yMin) / (nY - 1);
73 
74  const UInt_t nZ = 30;
75  const Double_t zMin = -1., zMax = 1., zStep = (zMax - zMin) / (nZ - 1);
76 
77  TH3F *hist = new TH3F("glvoxel", "glvoxel", nX, -1., 1., nY, -1., 1., nZ, -1., 1.);
78 
79  //Fill the histogram to create a "sphere".
80  for (UInt_t i = 0; i < nZ; ++i) {
81  const Double_t z = zMin + i * zStep;
82 
83  for (UInt_t j = 0; j < nY; ++j) {
84  const Double_t y = yMin + j * yStep;
85 
86  for (UInt_t k = 0; k < nX; ++k) {
87  const Double_t x = xMin + k * xStep;
88 
89  const Double_t val = 1. - (x * x + y * y + z * z);
90  hist->SetBinContent(k + 1, j + 1, i + 1, val);
91  }
92  }
93  }
94 
95  //Now, specify the transfer function.
96  TList * lf = hist->GetListOfFunctions();
97  if (lf) {
98  TF1 * tf = new TF1("TransferFunction", my_transfer_function);
99  lf->Add(tf);
100  }
101 
103 
104  hist->Draw("glcol");
105 }
106 
TStyle::SetCanvasPreferGL
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:325
TH3::SetBinContent
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH3.cxx:3340
TStyle.h
x
Double_t x[n]
Definition: legend1.C:17
TList.h
TH1::GetListOfFunctions
TList * GetListOfFunctions() const
Definition: TH1.h:243
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
UInt_t
unsigned int UInt_t
Definition: RtypesCore.h:46
y
Double_t y[n]
Definition: legend1.C:17
TH3.h
TH3F
3-D histogram with a float per channel (see TH1 documentation)}
Definition: TH3.h:268
Double_t
double Double_t
Definition: RtypesCore.h:59
TF1.h
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TF1
1-Dim function class
Definition: TF1.h:213
TList
A doubly linked list.
Definition: TList.h:44
TH1::Draw
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:3050