ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
csgdemo.C
Go to the documentation of this file.
1 //Conbinatorial Solid Geometry example
2 //Author: Andrei Gheata
3 
5 
6 #include "TGeoManager.h"
7 //______________________________________________________________________________
8 void csgdemo ()
9 {
10  gSystem->Load("libGeom");
11  bar = new TControlBar("vertical", "TGeo composite shapes",20,20);
12  bar->AddButton("How to run ","help()","Instructions ");
13  bar->AddButton("Union ", "s_union()", "A + B ");
14  bar->AddButton("Intersection ", "s_intersection()","A * B ");
15  bar->AddButton("Difference ", "s_difference()","A - B ");
16  //These two buttons are disabled cause of bugs
17  bar->AddButton("Complex composite", "complex_1()","(A * B) + (C - D)");
18  bar->Show();
19 
20  gROOT->SaveContext();
21 }
22 
23 //______________________________________________________________________________
25 {
27  gPad->Modified();
28  gPad->Update();
29 }
30 
31 
32 void s_union()
33 {
34  gROOT->GetListOfCanvases()->Delete();
35  TCanvas *c = new TCanvas("composite shape", "Union boolean operation", 700, 1000);
36 
37  c->Divide(1,2,0,0);
38  c->cd(2);
39  gPad->SetPad(0,0,1,0.4);
40  c->cd(1);
41  gPad->SetPad(0,0.4,1,1);
42 
43  if (gGeoManager) delete gGeoManager;
44 
45  new TGeoManager("xtru", "poza12");
46  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
47  TGeoMedium *med = new TGeoMedium("MED",1,mat);
48  TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
50 
51  // define shape components with names
52  TGeoPgon *pgon = new TGeoPgon("pg",0.,360.,6,2);
53  pgon->DefineSection(0,0,0,20);
54  pgon->DefineSection(1, 30,0,20);
55 
56  TGeoSphere *sph = new TGeoSphere("sph", 40., 45., 5., 175., 0., 340.);
57  // define named geometrical transformations with names
58  TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
59  tr->SetName("tr");
60  // register all used transformations
61  tr->RegisterYourself();
62  // create the composite shape based on a Boolean expression
63  TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr + pg");
64 
65  TGeoVolume *vol = new TGeoVolume("COMP1",cs);
66  top->AddNode(vol,1);
69  top->Draw();
70  MakePicture();
71 
72  c->cd(2);
73  TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
74  pt->SetLineColor(1);
75  TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
76  text->SetTextColor(2);
77  pt->AddText("----- It's an example of boolean union operation : A + B");
78  pt->AddText("----- A == part of sphere (5-175, 0-340), B == pgon");
79  pt->AddText(" ");
80  pt->SetAllWith("-----","color",4);
81  pt->SetAllWith("-----","font",72);
82  pt->SetAllWith("-----","size",0.04);
83  pt->SetTextAlign(12);
84  pt->SetTextSize(.044);
85  pt->Draw();
86  c->cd(1);
87 }
88 
90 {
91  gROOT->GetListOfCanvases()->Delete();
92  TCanvas *c = new TCanvas("composite shape", "Intersection boolean operation", 700, 1000);
93 
94  c->Divide(1,2,0,0);
95  c->cd(2);
96  gPad->SetPad(0,0,1,0.4);
97  c->cd(1);
98  gPad->SetPad(0,0.4,1,1);
99 
100  if (gGeoManager) delete gGeoManager;
101 
102  new TGeoManager("xtru", "poza12");
103  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
104  TGeoMedium *med = new TGeoMedium("MED",1,mat);
105  TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
107 
108  // define shape components with names
109  TGeoBBox *box = new TGeoBBox("bx", 40., 40., 40.);
110  TGeoSphere *sph = new TGeoSphere("sph", 40., 45.);
111  // define named geometrical transformations with names
112  TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
113  tr->SetName("tr");
114  // register all used transformations
115  tr->RegisterYourself();
116  // create the composite shape based on a Boolean expression
117  TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr * bx");
118 
119  TGeoVolume *vol = new TGeoVolume("COMP2",cs);
120  top->AddNode(vol,1);
123  top->Draw();
124  MakePicture();
125 
126  c->cd(2);
127 
128  TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
129 
130  pt->SetLineColor(1);
131 
132  TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
133 
134  text->SetTextColor(2);
135  pt->AddText("----- Here is an example of boolean intersection operation : A * B");
136  pt->AddText("----- A == sphere (with inner radius non-zero), B == box");
137  pt->AddText(" ");
138  pt->SetAllWith("-----","color",4);
139  pt->SetAllWith("-----","font",72);
140  pt->SetAllWith("-----","size",0.04);
141  pt->SetTextAlign(12);
142  pt->SetTextSize(0.044);
143  pt->Draw();
144  c->cd(1);
145 }
146 
148 {
149  gROOT->GetListOfCanvases()->Delete();
150  TCanvas *c = new TCanvas("composite shape", "Difference boolean operation", 700, 1000);
151 
152  c->Divide(1,2,0,0);
153  c->cd(2);
154  gPad->SetPad(0,0,1,0.4);
155  c->cd(1);
156  gPad->SetPad(0,0.4,1,1);
157 
158  if (gGeoManager) 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 void complex_1()
202 {
203  gROOT->GetListOfCanvases()->Delete();
204  TCanvas *c = new TCanvas("composite shape", "A * B - C", 700, 1000);
205 
206  c->Divide(1,2,0,0);
207  c->cd(2);
208  gPad->SetPad(0,0,1,0.4);
209  c->cd(1);
210  gPad->SetPad(0,0.4,1,1);
211 
212  if (gGeoManager) delete gGeoManager;
213 
214  new TGeoManager("xtru", "poza12");
215  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
216  TGeoMedium *med = new TGeoMedium("MED",1,mat);
217  TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
219 
220  // define shape components with names
221  TGeoBBox *box = new TGeoBBox("box", 20., 20., 20.);
222  TGeoBBox *box1 = new TGeoBBox("box1", 5., 5., 5.);
223  TGeoSphere *sph = new TGeoSphere("sph", 5., 25.);
224  TGeoSphere *sph1 = new TGeoSphere("sph1", 1., 15.);
225  // create the composite shape based on a Boolean expression
226  TGeoTranslation *tr = new TGeoTranslation(0., 30., 0.);
227  TGeoTranslation *tr1 = new TGeoTranslation(0., 40., 0.);
228  TGeoTranslation *tr2 = new TGeoTranslation(0., 30., 0.);
229  TGeoTranslation *tr3 = new TGeoTranslation(0., 30., 0.);
230  tr->SetName("tr");
231  tr1->SetName("tr1");
232  tr2->SetName("tr2");
233  tr3->SetName("tr3");
234  // register all used transformations
235  tr->RegisterYourself();
236  tr1->RegisterYourself();
237  tr2->RegisterYourself();
238  tr3->RegisterYourself();
239 
240  TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "(sph * box) + (sph1:tr - box1:tr1)");
241 
242  TGeoVolume *vol = new TGeoVolume("COMP4",cs);
243 // vol->SetLineColor(randomColor());
244  top->AddNode(vol,1);
247  top->Draw();
248  MakePicture();
249 
250  c->cd(2);
251  TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
252  pt->SetLineColor(1);
253  TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
254  text->SetTextColor(2);
255  pt->AddText("----- (sphere * box) + (sphere - box) ");
256 
257  pt->AddText(" ");
258  pt->SetAllWith("-----","color",4);
259  pt->SetAllWith("-----","font",72);
260  pt->SetAllWith("-----","size",0.04);
261  pt->SetTextAlign(12);
262  pt->SetTextSize(0.044);
263  pt->Draw();
264  c->cd(1);
265 
266 }
267 
268 //______________________________________________________________________________
269 void AddText(TPaveText *pave, const char *datamember, Double_t value, const char *comment)
270 {
271  char line[128];
272  for (Int_t i=0; i<128; i++) line[i] = ' ';
273  memcpy(&line[0], datamember, strlen(datamember));
274  line[10] = '=';
275  char number[20];
276  sprintf(number, "%5.2f", value);
277  memcpy(&line[12], number, strlen(number));
278  line[26] = '=';
279  line[27] = '>';
280  sprintf(&line[30], "%s",comment);
281  TText *text = pave->AddText(line);
282  text->SetTextAlign(12);
283 }
284 
285 //______________________________________________________________________________
286 void AddText(TPaveText *pave, const char *datamember, Int_t value, const char *comment)
287 {
288  char line[128];
289  for (Int_t i=0; i<128; i++) line[i] = ' ';
290  memcpy(&line[0], datamember, strlen(datamember));
291  line[10] = '=';
292  char number[20];
293  sprintf(number, "%5i", value);
294  memcpy(&line[12], number, strlen(number));
295  line[26] = '=';
296  line[27] = '>';
297  sprintf(&line[30], "%s",comment);
298  TText *text = pave->AddText(line);
299  text->SetTextAlign(12);
300 }
301 
302 //______________________________________________________________________________
303 void AddText(TPaveText *pave, TObject *pf, Int_t iaxis)
304 {
305  char line[128];
306  TGeoPatternFinder *finder = (TGeoPatternFinder*)pf;
307  if (!pave || !pf) return;
308  for (Int_t i=0; i<128; i++) line[i] = ' ';
309  TGeoVolume *volume = finder->GetVolume();
310  TGeoShape *sh = volume->GetShape();
311  sprintf(line, "Division of %s on axis %d (%s)", volume->GetName(), iaxis,sh->GetAxisName(iaxis));
312  TText *text = pave->AddText(line);
313  text->SetTextColor(3);
314  text->SetTextAlign(12);
315  AddText(pave, "fNdiv",finder->GetNdiv(),"number of divisions");
316  AddText(pave, "fStart",finder->GetStart(),"start divisioning position");
317  AddText(pave, "fStep",finder->GetStep(),"division step");
318 }
319 
320 //______________________________________________________________________________
321 void raytrace() {
322  if (!gGeoManager) return;
323 
325 
326  if (!painter) return;
327 
328  painter->SetRaytracing(raytracing);
329 
330  if (!gPad) return;
331 
332  gPad->Modified();
333  gPad->Update();
334 }
335 
336 //______________________________________________________________________________
337 void help() {
338  //
339 
340  new TCanvas("chelp","Help to run demos",200,10,700,600);
341 
342  welcome = new TPaveText(.1,.8,.9,.97);
343  welcome->AddText("Welcome to the new geometry package");
344  welcome->SetTextFont(32);
345  welcome->SetTextColor(4);
346  welcome->SetFillColor(24);
347  welcome->Draw();
348 
349  hdemo = new TPaveText(.05,.05,.95,.7);
350  hdemo->SetTextAlign(12);
351  hdemo->SetTextFont(52);
352  hdemo->AddText("- Demo for building TGeo composite shapes");
353  hdemo->AddText(" ");
354  hdemo->SetAllWith("....","color",2);
355  hdemo->SetAllWith("....","font",72);
356  hdemo->SetAllWith("....","size",0.03);
357 
358  hdemo->Draw();
359 }
360 
TControlBar * bar
Definition: demos.C:15
void Show()
Show control bar.
void AddButton(TControlBarButton *button)
Add button.
Double_t GetStart() const
void s_union()
Definition: csgdemo.C:32
A Control Bar is a fully user configurable tool which provides fast access to frequently used operati...
Definition: TControlBar.h:37
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:211
void MakePicture()
Definition: csgdemo.C:24
TLine * line
return c
static const std::string comment("comment")
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:659
void complex_1()
Definition: csgdemo.C:201
virtual void Draw(Option_t *option="")
draw top volume according to option
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:160
#define gROOT
Definition: TROOT.h:344
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1766
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
void help()
Definition: csgdemo.C:337
Bool_t raytracing
Definition: csgdemo.C:4
void s_intersection()
Definition: csgdemo.C:89
void s_difference()
Definition: csgdemo.C:147
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.
Base class for several text objects.
Definition: TText.h:42
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
virtual void AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=0, Option_t *option="")
Add a TGeoNode to the list of nodes.
Definition: TGeoVolume.cxx:948
TGeoVolume * GetVolume() const
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
Double_t GetStep() const
TPaveText * pt
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual void SetRaytracing(Bool_t flag=kTRUE)=0
virtual void RegisterYourself()
Register the matrix in the current manager, which will become the owner.
Definition: TGeoMatrix.cxx:532
void pgon(Int_t iaxis=0, Int_t ndiv=8, Double_t start=0, Double_t step=0)
Definition: geodemo.C:1189
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
void raytrace()
Definition: csgdemo.C:321
virtual const char * GetAxisName(Int_t iaxis) const =0
void AddText(TPaveText *pave, const char *datamember, Double_t value, const char *comment)
Definition: csgdemo.C:269
The Canvas class.
Definition: TCanvas.h:48
TGeoShape * GetShape() const
Definition: TGeoVolume.h:204
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:35
R__EXTERN TGeoManager * gGeoManager
Definition: TGeoManager.h:556
double Double_t
Definition: RtypesCore.h:55
TText * text
Mother of all ROOT objects.
Definition: TObject.h:58
tuple pave
Definition: h1draw.py:67
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1073
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:616
#define gPad
Definition: TVirtualPad.h:288
virtual void SetTextColor(Color_t tcolor=1)
Definition: TAttText.h:57
Int_t GetNdiv() const
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
void csgdemo()
Definition: csgdemo.C:8
const Bool_t kTRUE
Definition: Rtypes.h:91
float value
Definition: math.cpp:443
TVirtualGeoPainter * GetGeomPainter()
Make a default painter if none present. Returns pointer to it.
virtual void SetAllWith(const char *text, Option_t *option, Double_t value)
Set attribute option for all lines containing string text.
Definition: TPaveText.cxx:851