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
39namespace {
40
41Double_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
65void 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
unsigned int UInt_t
Definition: RtypesCore.h:46
double Double_t
Definition: RtypesCore.h:59
R__EXTERN TStyle * gStyle
Definition: TStyle.h:414
1-Dim function class
Definition: TF1.h:213
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition: TH1.cxx:3060
TList * GetListOfFunctions() const
Definition: TH1.h:242
3-D histogram with a float per channel (see TH1 documentation)}
Definition: TH3.h:268
void SetBinContent(Int_t bin, Double_t content) override
Set bin content.
Definition: TH3.cxx:3381
A doubly linked list.
Definition: TList.h:38
void Add(TObject *obj) override
Definition: TList.h:81
void SetCanvasPreferGL(Bool_t prefer=kTRUE)
Definition: TStyle.h:326
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17