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) delete gGeoManager;
53
54 new TGeoManager("xtru", "poza12");
55 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
56 TGeoMedium *med = new TGeoMedium("MED",1,mat);
57 TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
59
60 // define shape components with names
61 TGeoPgon *pgon = new TGeoPgon("pg",0.,360.,6,2);
62 pgon->DefineSection(0,0,0,20);
63 pgon->DefineSection(1, 30,0,20);
64
65 TGeoSphere *sph = new TGeoSphere("sph", 40., 45., 5., 175., 0., 340.);
66 // define named geometrical transformations with names
67 TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
68 tr->SetName("tr");
69 // register all used transformations
70 tr->RegisterYourself();
71 // create the composite shape based on a Boolean expression
72 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr + pg");
73
74 TGeoVolume *vol = new TGeoVolume("COMP1",cs);
75 top->AddNode(vol,1);
78 top->Draw();
79 MakePicture();
80
81 c->cd(2);
82 TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
83 pt->SetLineColor(1);
84 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
85 text->SetTextColor(2);
86 pt->AddText("----- It's an example of boolean union operation : A + B");
87 pt->AddText("----- A == part of sphere (5-175, 0-340), B == pgon");
88 pt->AddText(" ");
89 pt->SetAllWith("-----","color",4);
90 pt->SetAllWith("-----","font",72);
91 pt->SetAllWith("-----","size",0.04);
92 pt->SetTextAlign(12);
93 pt->SetTextSize(.044);
94 pt->Draw();
95 c->cd(1);
96}
97
98//______________________________________________________________________________
99void s_intersection()
100{
101 auto c = create_canvas("Intersection boolean operation");
102
103 if (gGeoManager) delete gGeoManager;
104
105 new TGeoManager("xtru", "poza12");
106 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
107 TGeoMedium *med = new TGeoMedium("MED",1,mat);
108 TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
110
111 // define shape components with names
112 TGeoBBox *box = new TGeoBBox("bx", 40., 40., 40.);
113 TGeoSphere *sph = new TGeoSphere("sph", 40., 45.);
114 // define named geometrical transformations with names
115 TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
116 tr->SetName("tr");
117 // register all used transformations
118 tr->RegisterYourself();
119 // create the composite shape based on a Boolean expression
120 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr * bx");
121
122 TGeoVolume *vol = new TGeoVolume("COMP2",cs);
123 top->AddNode(vol,1);
126 top->Draw();
127 MakePicture();
128
129 c->cd(2);
130
131 TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
132
133 pt->SetLineColor(1);
134
135 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
136
137 text->SetTextColor(2);
138 pt->AddText("----- Here is an example of boolean intersection operation : A * B");
139 pt->AddText("----- A == sphere (with inner radius non-zero), B == box");
140 pt->AddText(" ");
141 pt->SetAllWith("-----","color",4);
142 pt->SetAllWith("-----","font",72);
143 pt->SetAllWith("-----","size",0.04);
144 pt->SetTextAlign(12);
145 pt->SetTextSize(0.044);
146 pt->Draw();
147 c->cd(1);
148}
149
150//______________________________________________________________________________
151void s_difference()
152{
153 auto c = create_canvas("Difference boolean operation");
154
155 if (gGeoManager) delete gGeoManager;
156
157 new TGeoManager("xtru", "poza12");
158 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
159 TGeoMedium *med = new TGeoMedium("MED",1,mat);
160 TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
162
163 // define shape components with names
164 TGeoTorus *tor = new TGeoTorus("tor", 45., 15., 20., 45., 145.);
165 TGeoSphere *sph = new TGeoSphere("sph", 20., 45., 0., 180., 0., 270.);
166 // create the composite shape based on a Boolean expression
167 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph - tor");
168
169 TGeoVolume *vol = new TGeoVolume("COMP3",cs);
170 top->AddNode(vol,1);
173 top->Draw();
174 MakePicture();
175
176 c->cd(2);
177
178 TPaveText *pt = new TPaveText(.01, .01, .99, .99);
179
180 pt->SetLineColor(1);
181
182 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
183
184 text->SetTextColor(2);
185
186 pt->AddText("----- It's an example of boolean difference: A - B");
187 pt->AddText("----- A == part of sphere (0-180, 0-270), B == partial torus (45-145)");
188 pt->AddText(" ");
189 pt->SetAllWith("-----","color",4);
190 pt->SetAllWith("-----","font",72);
191 pt->SetAllWith("-----","size",0.04);
192 pt->SetTextAlign(12);
193 pt->SetTextSize(0.044);
194 pt->Draw();
195 c->cd(1);
196}
197
198//______________________________________________________________________________
199void s_complex()
200{
201 auto c = create_canvas("A * B - C");
202
203 if (gGeoManager) delete gGeoManager;
204
205 new TGeoManager("xtru", "poza12");
206 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
207 TGeoMedium *med = new TGeoMedium("MED",1,mat);
208 TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
210
211 // define shape components with names
212 TGeoBBox *box = new TGeoBBox("box", 20., 20., 20.);
213 TGeoBBox *box1 = new TGeoBBox("box1", 5., 5., 5.);
214 TGeoSphere *sph = new TGeoSphere("sph", 5., 25.);
215 TGeoSphere *sph1 = new TGeoSphere("sph1", 1., 15.);
216 // create the composite shape based on a Boolean expression
217 TGeoTranslation *tr = new TGeoTranslation(0., 30., 0.);
218 TGeoTranslation *tr1 = new TGeoTranslation(0., 40., 0.);
219 TGeoTranslation *tr2 = new TGeoTranslation(0., 30., 0.);
220 TGeoTranslation *tr3 = new TGeoTranslation(0., 30., 0.);
221 tr->SetName("tr");
222 tr1->SetName("tr1");
223 tr2->SetName("tr2");
224 tr3->SetName("tr3");
225 // register all used transformations
226 tr->RegisterYourself();
227 tr1->RegisterYourself();
228 tr2->RegisterYourself();
229 tr3->RegisterYourself();
230
231 TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "(sph * box) + (sph1:tr - box1:tr1)");
232
233 TGeoVolume *vol = new TGeoVolume("COMP4",cs);
234// vol->SetLineColor(randomColor());
235 top->AddNode(vol,1);
238 top->Draw();
239 MakePicture();
240
241 c->cd(2);
242 TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
243 pt->SetLineColor(1);
244 TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
245 text->SetTextColor(2);
246 pt->AddText("----- (sphere * box) + (sphere - box) ");
247
248 pt->AddText(" ");
249 pt->SetAllWith("-----","color",4);
250 pt->SetAllWith("-----","font",72);
251 pt->SetAllWith("-----","size",0.04);
252 pt->SetTextAlign(12);
253 pt->SetTextSize(0.044);
254 pt->Draw();
255 c->cd(1);
256
257}
258
259//______________________________________________________________________________
260void raytrace()
261{
262 if (gGeoManager && gPad) {
263 auto top = gGeoManager->GetTopVolume();
264 bool drawn = gPad->GetListOfPrimitives()->FindObject(top);
265 if (drawn) top->SetVisRaytrace(raytracing);
266
267 printf("raytrace %d\n", raytracing);
268 gPad->Modified();
269 gPad->Update();
270 }
271}
272
273//______________________________________________________________________________
274void help()
275{
276 auto c = create_canvas("Help to run demos", false);
277
278 TPaveText* welcome = new TPaveText(.1,.8,.9,.97);
279 welcome->AddText("Welcome to the new geometry package");
280 welcome->SetTextFont(32);
281 welcome->SetTextColor(4);
282 welcome->SetFillColor(24);
283 welcome->Draw();
284
285 TPaveText* hdemo = new TPaveText(.05,.05,.95,.7);
286 hdemo->SetTextAlign(12);
287 hdemo->SetTextFont(52);
288 hdemo->AddText("- Demo for building TGeo composite shapes");
289 hdemo->AddText(" ");
290 hdemo->AddText(" .... s_union() : Union boolean operation");
291 hdemo->AddText(" .... s_difference() : Difference boolean operation");
292 hdemo->AddText(" .... s_intersection() : Intersection boolean operation");
293 hdemo->AddText(" .... s_complex() : Combination of (A * B) + (C - D)");
294 hdemo->AddText(" ");
295 hdemo->SetAllWith("....","color",2);
296 hdemo->SetAllWith("....","font",72);
297 hdemo->SetAllWith("....","size",0.03);
298
299 hdemo->Draw();
300}
301
302//______________________________________________________________________________
303void csgdemo ()
304{
305 gSystem->Load("libGeom");
306 TControlBar* bar = new TControlBar("vertical", "TGeo composite shapes",20,20);
307 bar->AddButton("How to run ","help()","Instructions ");
308 bar->AddButton("Union ", "s_union()", "A + B ");
309 bar->AddButton("Intersection ", "s_intersection()", "A * B ");
310 bar->AddButton("Difference ", "s_difference()", "A - B ");
311 bar->AddButton("Complex composite", "s_complex()", "(A * B) + (C - D)");
312 bar->AddButton("RAY-TRACE ON/OFF","raytrace()","Toggle ray-tracing mode");
313 bar->Show();
314}
#define c(i)
Definition RSha256.hxx:101
bool Bool_t
Definition RtypesCore.h:63
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
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:555
#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
Box class.
Definition TGeoBBox.h:17
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)
Defines z position of a section plane, rmin and rmax at this z.
Definition TGeoPcon.cxx:684
Polygons are defined in the same way as polycones, the difference being just that the segments betwee...
Definition TGeoPgon.h:20
TGeoSphere are not just balls having internal and external radii, but sectors of a sphere having defi...
Definition TGeoSphere.h:17
The torus is defined by its axial radius, its inner and outer radius.
Definition TGeoTorus.h:17
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:403
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:2604
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