Logo ROOT  
Reference Guide
csgdemo.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_eve7
3/// Combinatorial Solid Geometry example
4///
5/// Stripped down to demonstrate EVE shape-extracts.
6/// 1. `Run root csgdemo.C`
7/// This will produce csg.root containing the extract.
8/// 2. Display the assebly as:
9/// `root show_extract.C("csg.root")`
10///
11/// \image html eve_csgdemo.png
12/// \macro_code
13///
14/// \author Andrei Gheata
15
16#include "TSystem.h"
17
18#include "TGeoManager.h"
19#include "TGeoCompositeShape.h"
20#include "TGeoSphere.h"
21
22#include <ROOT/REveManager.hxx>
24#include <ROOT/REveGeoShape.hxx>
26
27namespace REX = ROOT::Experimental;
28
29REX::REveGeoPolyShape *eve_pshape = nullptr;
30REX::REveGeoShape *eve_shape = nullptr;
31
32//____________________________________________________________________________
33void csgdemo ()
34{
35 //TCanvas *c = new TCanvas("composite shape", "A * B - C");
36 // c->Iconify();
37
38 if (gGeoManager) delete gGeoManager;
39
40 new TGeoManager("xtru", "poza12");
41 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
42 TGeoMedium *med = new TGeoMedium("MED",1,mat);
43 TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
45
46 // define shape components with names
47 TGeoBBox *box = new TGeoBBox("box", 20., 20., 20.);
48 TGeoBBox *box1 = new TGeoBBox("box1", 5., 5., 5.);
49 TGeoSphere *sph = new TGeoSphere("sph", 5., 25.);
50 TGeoSphere *sph1 = new TGeoSphere("sph1", 1., 15.);
51 // create the composite shape based on a Boolean expression
52 TGeoTranslation *tr = new TGeoTranslation(0., 30., 0.);
53 TGeoTranslation *tr1 = new TGeoTranslation(0., 40., 0.);
54 TGeoTranslation *tr2 = new TGeoTranslation(0., 30., 0.);
55 TGeoTranslation *tr3 = new TGeoTranslation(0., 30., 0.);
56 tr->SetName("tr");
57 tr1->SetName("tr1");
58 tr2->SetName("tr2");
59 tr3->SetName("tr3");
60 // register all used transformations
61 tr->RegisterYourself();
62 tr1->RegisterYourself();
63 tr2->RegisterYourself();
64 tr3->RegisterYourself();
65
66 auto cs = new TGeoCompositeShape("mir", "(sph * box) + (sph1:tr - box1:tr1)");
67
68 TGeoVolume *vol = new TGeoVolume("COMP4", cs);
70 top->AddNode(vol,1);
72
73 // To draw original
74 // gGeoManager->SetNsegments(80);
75 // top->Draw("ogl");
76
78
80
81 auto node = gGeoManager->GetTopNode();
82 auto geo_cshape = dynamic_cast<TGeoCompositeShape*>(node->GetDaughter(0)->GetVolume()->GetShape());
83
84 if (!geo_cshape) throw std::runtime_error("The first vshape is not a CSG shape.");
85
86 bool poly_first = false;
87 if (poly_first)
88 {
89 eve_pshape = new REX::REveGeoPolyShape;
90 eve_pshape->BuildFromComposite(geo_cshape, 40);
91
92 eve_shape = new REX::REveGeoShape("CSG_Result");
93 eve_shape->SetShape(eve_pshape);
94 }
95 else
96 {
97 eve_shape = new REX::REveGeoShape("CSG_Result");
98 eve_shape->SetNSegments(40);
99 eve_shape->SetShape(geo_cshape);
100
101 eve_pshape = dynamic_cast<REX::REveGeoPolyShape*>(eve_shape->GetShape());
102 }
103 eve_shape->SetMainColor(kMagenta);
104
105 // If one doesn't enable triangles globally, one can do it on per shape basis:
106 // eve_pshape->EnforceTriangles();
107
108 eve_pshape->Draw("ogl");
109
110 eve_shape->SaveExtract("csg.root", "CSG Demo");
111}
@ kMagenta
Definition: Rtypes.h:64
R__EXTERN TGeoManager * gGeoManager
Definition: TGeoManager.h:601
static void SetAutoEnforceTriangles(Bool_t f)
void BuildFromComposite(TGeoCompositeShape *cshp, Int_t n_seg=60)
Produce all polygons from composite shape.
void SetNSegments(Int_t s)
Set number of segments.
void SaveExtract(const char *file, const char *name)
Save the shape tree as REveGeoShapeExtract.
void SetShape(TGeoShape *s)
Set TGeoShape shown by this object.
static REveManager * Create()
If global REveManager* REX::gEve is not set initialize it.
void SetMainColor(Color_t color) override
Set main color.
Definition: REveShape.cxx:77
Box class.
Definition: TGeoBBox.h:18
Class handling Boolean composition of shapes.
The manager class for any TGeo geometry.
Definition: TGeoManager.h:43
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.
TGeoNode * GetTopNode() const
Definition: TGeoManager.h:532
Base class describing materials.
Definition: TGeoMaterial.h:31
virtual void RegisterYourself()
Register the matrix in the current manager, which will become the owner.
Definition: TGeoMatrix.cxx:526
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition: TGeoMedium.h:24
virtual void Draw(Option_t *option="")
Draw this shape.
Definition: TGeoShape.cxx:721
Spherical shell class.
Definition: TGeoSphere.h:18
Class describing translations.
Definition: TGeoMatrix.h:122
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition: TGeoVolume.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
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:931
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1