Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
csgdemo.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_geom
3/// Combinatorial Solid Geometry example.
4///
5/// \macro_code
6///
7/// \author Andrei Gheata
8
9Bool_t raytracing = kTRUE;
10
11#include "TGeoManager.h"
12
13//______________________________________________________________________________
14TCanvas *create_canvas(const char *title, bool divide = true)
15{
16 auto c = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("csg_canvas");
17 if (c) {
18 c->Clear();
19 c->Update();
20 c->SetTitle(title);
21 } else {
22 c = new TCanvas("csg_canvas", title, 700, 1000);
23 }
24
25 if (divide) {
26 c->Divide(1, 2, 0, 0);
27 c->cd(2);
28 gPad->SetPad(0, 0, 1, 0.4);
29 c->cd(1);
30 gPad->SetPad(0, 0.4, 1, 1);
31 }
32
33 return c;
34}
35
36//______________________________________________________________________________
37void MakePicture()
38{
39 Bool_t is_raytracing = gGeoManager->GetTopVolume()->IsRaytracing();
40 if (is_raytracing != raytracing) {
42 gPad->Modified();
43 gPad->Update();
44 }
45}
46
47//______________________________________________________________________________
48void s_union()
49{
50 auto c = create_canvas("Union boolean operation");
51
52 if (gGeoManager)
53 delete gGeoManager;
54
55 new TGeoManager("xtru", "poza12");
56 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
57 TGeoMedium *med = new TGeoMedium("MED", 1, mat);
58 TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
60
61 // define shape components with names
62 TGeoPgon *pgon = new TGeoPgon("pg", 0., 360., 6, 2);
63 pgon->DefineSection(0, 0, 0, 20);
64 pgon->DefineSection(1, 30, 0, 20);
65
66 TGeoSphere *sph = new TGeoSphere("sph", 40., 45., 5., 175., 0., 340.);
67 // define named geometrical transformations with names
68 TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
69 tr->SetName("tr");
70 // register all used transformations
71 tr->RegisterYourself();
72 // create the composite shape based on a Boolean expression
73 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr + pg");
74
75 TGeoVolume *vol = new TGeoVolume("COMP1", cs);
76 top->AddNode(vol, 1);
79 top->Draw();
80 MakePicture();
81
82 c->cd(2);
83 TPaveText *pt = new TPaveText(0.01, 0.01, 0.99, 0.99);
84 pt->SetLineColor(1);
85 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
86 text->SetTextColor(2);
87 pt->AddText("----- It's an example of boolean union operation : A + B");
88 pt->AddText("----- A == part of sphere (5-175, 0-340), B == pgon");
89 pt->AddText(" ");
90 pt->SetAllWith("-----", "color", 4);
91 pt->SetAllWith("-----", "font", 72);
92 pt->SetAllWith("-----", "size", 0.04);
93 pt->SetTextAlign(12);
94 pt->SetTextSize(.044);
95 pt->Draw();
96 c->cd(1);
97}
98
99//______________________________________________________________________________
100void s_intersection()
101{
102 auto c = create_canvas("Intersection boolean operation");
103
104 if (gGeoManager)
105 delete gGeoManager;
106
107 new TGeoManager("xtru", "poza12");
108 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
109 TGeoMedium *med = new TGeoMedium("MED", 1, mat);
110 TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
112
113 // define shape components with names
114 TGeoBBox *box = new TGeoBBox("bx", 40., 40., 40.);
115 TGeoSphere *sph = new TGeoSphere("sph", 40., 45.);
116 // define named geometrical transformations with names
117 TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
118 tr->SetName("tr");
119 // register all used transformations
120 tr->RegisterYourself();
121 // create the composite shape based on a Boolean expression
122 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr * bx");
123
124 TGeoVolume *vol = new TGeoVolume("COMP2", cs);
125 top->AddNode(vol, 1);
128 top->Draw();
129 MakePicture();
130
131 c->cd(2);
132
133 TPaveText *pt = new TPaveText(0.01, 0.01, 0.99, 0.99);
134
135 pt->SetLineColor(1);
136
137 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
138
139 text->SetTextColor(2);
140 pt->AddText("----- Here is an example of boolean intersection operation : A * B");
141 pt->AddText("----- A == sphere (with inner radius non-zero), B == box");
142 pt->AddText(" ");
143 pt->SetAllWith("-----", "color", 4);
144 pt->SetAllWith("-----", "font", 72);
145 pt->SetAllWith("-----", "size", 0.04);
146 pt->SetTextAlign(12);
147 pt->SetTextSize(0.044);
148 pt->Draw();
149 c->cd(1);
150}
151
152//______________________________________________________________________________
153void s_difference()
154{
155 auto c = create_canvas("Difference boolean operation");
156
157 if (gGeoManager)
158 delete gGeoManager;
159
160 new TGeoManager("xtru", "poza12");
161 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
162 TGeoMedium *med = new TGeoMedium("MED", 1, mat);
163 TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
165
166 // define shape components with names
167 TGeoTorus *tor = new TGeoTorus("tor", 45., 15., 20., 45., 145.);
168 TGeoSphere *sph = new TGeoSphere("sph", 20., 45., 0., 180., 0., 270.);
169 // create the composite shape based on a Boolean expression
170 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph - tor");
171
172 TGeoVolume *vol = new TGeoVolume("COMP3", cs);
173 top->AddNode(vol, 1);
176 top->Draw();
177 MakePicture();
178
179 c->cd(2);
180
181 TPaveText *pt = new TPaveText(.01, .01, .99, .99);
182
183 pt->SetLineColor(1);
184
185 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
186
187 text->SetTextColor(2);
188
189 pt->AddText("----- It's an example of boolean difference: A - B");
190 pt->AddText("----- A == part of sphere (0-180, 0-270), B == partial torus (45-145)");
191 pt->AddText(" ");
192 pt->SetAllWith("-----", "color", 4);
193 pt->SetAllWith("-----", "font", 72);
194 pt->SetAllWith("-----", "size", 0.04);
195 pt->SetTextAlign(12);
196 pt->SetTextSize(0.044);
197 pt->Draw();
198 c->cd(1);
199}
200
201//______________________________________________________________________________
202void s_complex()
203{
204 auto c = create_canvas("A * B - C");
205
206 if (gGeoManager)
207 delete gGeoManager;
208
209 new TGeoManager("xtru", "poza12");
210 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
211 TGeoMedium *med = new TGeoMedium("MED", 1, mat);
212 TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 100, 100, 100);
214
215 // define shape components with names
216 TGeoBBox *box = new TGeoBBox("box", 20., 20., 20.);
217 TGeoBBox *box1 = new TGeoBBox("box1", 5., 5., 5.);
218 TGeoSphere *sph = new TGeoSphere("sph", 5., 25.);
219 TGeoSphere *sph1 = new TGeoSphere("sph1", 1., 15.);
220 // create the composite shape based on a Boolean expression
221 TGeoTranslation *tr = new TGeoTranslation(0., 30., 0.);
222 TGeoTranslation *tr1 = new TGeoTranslation(0., 40., 0.);
223 TGeoTranslation *tr2 = new TGeoTranslation(0., 30., 0.);
224 TGeoTranslation *tr3 = new TGeoTranslation(0., 30., 0.);
225 tr->SetName("tr");
226 tr1->SetName("tr1");
227 tr2->SetName("tr2");
228 tr3->SetName("tr3");
229 // register all used transformations
230 tr->RegisterYourself();
231 tr1->RegisterYourself();
232 tr2->RegisterYourself();
233 tr3->RegisterYourself();
234
235 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "(sph * box) + (sph1:tr - box1:tr1)");
236
237 TGeoVolume *vol = new TGeoVolume("COMP4", cs);
238 // vol->SetLineColor(randomColor());
239 top->AddNode(vol, 1);
242 top->Draw();
243 MakePicture();
244
245 c->cd(2);
246 TPaveText *pt = new TPaveText(0.01, 0.01, 0.99, 0.99);
247 pt->SetLineColor(1);
248 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
249 text->SetTextColor(2);
250 pt->AddText("----- (sphere * box) + (sphere - box) ");
251
252 pt->AddText(" ");
253 pt->SetAllWith("-----", "color", 4);
254 pt->SetAllWith("-----", "font", 72);
255 pt->SetAllWith("-----", "size", 0.04);
256 pt->SetTextAlign(12);
257 pt->SetTextSize(0.044);
258 pt->Draw();
259 c->cd(1);
260}
261
262//______________________________________________________________________________
263void raytrace()
264{
265 if (gGeoManager && gPad) {
266 auto top = gGeoManager->GetTopVolume();
267 bool drawn = gPad->GetListOfPrimitives()->FindObject(top);
268 if (drawn)
269 top->SetVisRaytrace(raytracing);
270
271 printf("raytrace %d\n", raytracing);
272 gPad->Modified();
273 gPad->Update();
274 }
275}
276
277//______________________________________________________________________________
278void help()
279{
280 auto c = create_canvas("Help to run demos", false);
281
282 TPaveText *welcome = new TPaveText(.1, .8, .9, .97);
283 welcome->AddText("Welcome to the new geometry package");
284 welcome->SetTextFont(32);
285 welcome->SetTextColor(4);
286 welcome->SetFillColor(24);
287 welcome->Draw();
288
289 TPaveText *hdemo = new TPaveText(.05, .05, .95, .7);
290 hdemo->SetTextAlign(12);
291 hdemo->SetTextFont(52);
292 hdemo->AddText("- Demo for building TGeo composite shapes");
293 hdemo->AddText(" ");
294 hdemo->AddText(" .... s_union() : Union boolean operation");
295 hdemo->AddText(" .... s_difference() : Difference boolean operation");
296 hdemo->AddText(" .... s_intersection() : Intersection boolean operation");
297 hdemo->AddText(" .... s_complex() : Combination of (A * B) + (C - D)");
298 hdemo->AddText(" ");
299 hdemo->SetAllWith("....", "color", 2);
300 hdemo->SetAllWith("....", "font", 72);
301 hdemo->SetAllWith("....", "size", 0.03);
302
303 hdemo->Draw();
304}
305
306//______________________________________________________________________________
307void csgdemo()
308{
309 gSystem->Load("libGeom");
310 TControlBar *bar = new TControlBar("vertical", "TGeo composite shapes", 20, 20);
311 bar->AddButton("How to run ", "help()", "Instructions ");
312 bar->AddButton("Union ", "s_union()", "A + B ");
313 bar->AddButton("Intersection ", "s_intersection()", "A * B ");
314 bar->AddButton("Difference ", "s_difference()", "A - B ");
315 bar->AddButton("Complex composite", "s_complex()", "(A * B) + (C - D)");
316 bar->AddButton("RAY-TRACE ON/OFF", "raytrace()", "Toggle ray-tracing mode");
317 bar->Show();
318}
#define c(i)
Definition RSha256.hxx:101
bool Bool_t
Definition RtypesCore.h:63
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
Option_t Option_t TPoint TPoint const char text
R__EXTERN TGeoManager * gGeoManager
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
#define gPad
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:42
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
The Canvas class.
Definition TCanvas.h:23
A Control Bar is a fully user configurable tool which provides fast access to frequently used operati...
Definition TControlBar.h:26
void Show()
Show control bar.
void AddButton(TControlBarButton *button)
Add button.
void SetVisRaytrace(Bool_t flag=kTRUE)
Definition TGeoAtt.h:66
Composite shapes are Boolean combinations of two or more shape components.
The manager class for any TGeo geometry.
Definition TGeoManager.h:44
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
TGeoVolume * MakeBox(const char *name, TGeoMedium *medium, Double_t dx, Double_t dy, Double_t dz)
Make in one step a volume pointing to a box shape with given medium.
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
TGeoVolume * GetTopVolume() const
Base class describing materials.
virtual void RegisterYourself()
Register the matrix in the current manager, which will become the owner.
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition TGeoMedium.h:23
virtual void DefineSection(Int_t snum, Double_t z, Double_t rmin, Double_t rmax)
Class describing translations.
Definition TGeoMatrix.h:116
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition TGeoVolume.h:43
virtual TGeoNode * AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=nullptr, Option_t *option="")
Add a TGeoNode to the list of nodes.
void Draw(Option_t *option="") override
draw top volume according to option
Bool_t IsRaytracing() const
Check if the painter is currently ray-tracing the content of this volume.
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
virtual void Clear(Option_t *="")
Definition TObject.h:119
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:420
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition TPad.cxx:2700
A Pave (see TPave) with text, lines or/and boxes inside.
Definition TPaveText.h:21
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
virtual void SetAllWith(const char *text, Option_t *option, Double_t value)
Set attribute option for all lines containing string text.
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1857
Base class for several text objects.
Definition TText.h:22
TPaveText * pt
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1