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 
27 namespace REX = ROOT::Experimental;
28 
29 REX::REveGeoPolyShape *eve_pshape = nullptr;
30 REX::REveGeoShape *eve_shape = nullptr;
31 
32 //____________________________________________________________________________
33 void 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);
69  vol->SetLineColor(kMagenta);
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 }
ROOT::Experimental::REveShape::SetMainColor
void SetMainColor(Color_t color) override
Set main color.
Definition: REveShape.cxx:77
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
TGeoManager::MakeBox
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.
Definition: TGeoManager.cxx:2918
ROOT::Experimental::REveGeoShape
Definition: REveGeoShape.hxx:33
gGeoManager
R__EXTERN TGeoManager * gGeoManager
Definition: TGeoManager.h:602
ROOT::Experimental::REveManager::Create
static REveManager * Create()
If global REveManager* REX::gEve is not set initialize it.
Definition: REveManager.cxx:698
TGeoManager::CloseGeometry
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
Definition: TGeoManager.cxx:1359
box
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
ROOT::Experimental::REveGeoShape::GetShape
TGeoShape * GetShape() const
Definition: REveGeoShape.hxx:60
TGeoVolume::AddNode
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:932
ROOT::Experimental::REveGeoShape::SetNSegments
void SetNSegments(Int_t s)
Set number of segments.
Definition: REveGeoShape.cxx:176
TGeoTranslation
Definition: TGeoMatrix.h:121
TGeoMaterial
Definition: TGeoMaterial.h:34
TGeoCompositeShape
Definition: TGeoCompositeShape.h:27
TGeoShape::Draw
virtual void Draw(Option_t *option="")
Draw this shape.
Definition: TGeoShape.cxx:721
ROOT::Experimental::REveGeoPolyShape::BuildFromComposite
void BuildFromComposite(TGeoCompositeShape *cshp, Int_t n_seg=60)
Produce all polygons from composite shape.
Definition: REveGeoPolyShape.cxx:93
TGeoManager::SetTopVolume
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
Definition: TGeoManager.cxx:3397
kMagenta
@ kMagenta
Definition: Rtypes.h:66
TSystem.h
TGeoSphere
Definition: TGeoSphere.h:17
ROOT::Experimental::REveGeoPolyShape::SetAutoEnforceTriangles
static void SetAutoEnforceTriangles(Bool_t f)
Definition: REveGeoPolyShape.cxx:41
TGeoBBox
Definition: TGeoBBox.h:17
TGeoCompositeShape.h
REveGeoShape.hxx
TGeoManager.h
REveManager.hxx
REveGeoPolyShape.hxx
ROOT::Experimental::REveGeoShape::SetShape
void SetShape(TGeoShape *s)
Set TGeoShape shown by this object.
Definition: REveGeoShape.cxx:195
TGeoMatrix::RegisterYourself
virtual void RegisterYourself()
Register the matrix in the current manager, which will become the owner.
Definition: TGeoMatrix.cxx:526
TGeoManager::GetTopNode
TGeoNode * GetTopNode() const
Definition: TGeoManager.h:533
ROOT::Experimental
Definition: RDirectory.hxx:36
TGeoMedium
Definition: TGeoMedium.h:23
REveGeoShapeExtract.hxx
TGeoVolume::SetLineColor
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TGeoVolume.cxx:2035
TGeoManager
Definition: TGeoManager.h:44
TGeoVolume
Definition: TGeoVolume.h:44
ROOT::Experimental::REveGeoPolyShape
Definition: REveGeoPolyShape.hxx:28
ROOT::Experimental::REveGeoShape::SaveExtract
void SaveExtract(const char *file, const char *name)
Save the shape tree as REveGeoShapeExtract.
Definition: REveGeoShape.cxx:242
TGeoSphere.h