Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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);
44  gGeoManager->SetTopVolume(top);
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);
71  gGeoManager->CloseGeometry();
72 
73  // To draw original
74  // gGeoManager->SetNsegments(80);
75  // top->Draw("ogl");
76 
77  REX::REveManager::Create();
78 
79  REX::REveGeoPolyShape::SetAutoEnforceTriangles(true);
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 }