Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
tessellatedNav.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_geom
3/// Macro allowing to vizualize tessellations from Wavefront's .obj format.
4///
5/// \image html geom_visualizeWavefrontObj.png width=500px
6/// \macro_code
7///
8/// \author Andrei Gheata
9
10#include <TROOT.h>
11#include <TColor.h>
12#include <TDatime.h>
13#include <TRandom3.h>
14#include <TGeoManager.h>
15#include <TGeoTessellated.h>
17#include <TView.h>
18
19//______________________________________________________________________________
20int randomColor()
21{
22 gRandom = new TRandom3();
23 TDatime dt;
24 gRandom->SetSeed(dt.GetTime());
26 TColor *color = new TColor(ci, gRandom->Rndm(), gRandom->Rndm(), gRandom->Rndm());
27 return ci;
28}
29
30//______________________________________________________________________________
31void tessellatedNav(const char *dot_obj_file = "", bool check = false)
32{
33 // Input a file in .obj format (https://en.wikipedia.org/wiki/Wavefront_.obj_file)
34 // The file should have a single object inside, only vertex and faces information is used
35 TString name = dot_obj_file;
36 TString sfile = dot_obj_file;
37 if (sfile.IsNull()) {
38 sfile = gROOT->GetTutorialsDir();
39 sfile += "/geom/teddy.obj";
40 }
41 name.ReplaceAll(".obj", "");
42 gROOT->GetListOfCanvases()->Delete();
43 if (gGeoManager)
44 delete gGeoManager;
45 auto geom = new TGeoManager(name, "Imported from .obj file");
46 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
47 TGeoMedium *med = new TGeoMedium("MED", 1, mat);
48 TGeoVolume *top = geom->MakeBox("TOP", med, 10, 10, 10);
49 geom->SetTopVolume(top);
50
51 auto tsl = TGeoTessellated::ImportFromObjFormat(sfile.Data(), check);
52 if (!tsl)
53 return;
54 tsl->ResizeCenter(5.);
55
56 TGeoVolume *vol = new TGeoVolume(name, tsl, med);
57 vol->SetLineColor(randomColor());
58 vol->SetLineWidth(2);
59 top->AddNode(vol, 1);
60 geom->CloseGeometry();
61
62 // Convert to VecGeom tessellated solid
63 auto converter = TVirtualGeoConverter::Instance(geom);
64 if (!converter) {
65 printf("Raytracing a tessellated shape without VecGeom support will just draw a box\n");
66 } else {
67 converter->ConvertGeometry();
68 }
69
70 if (gROOT->IsBatch())
71 return;
72 // Set the view
73 top->Draw();
74 TView *view = gPad->GetView();
75 if (!view)
76 return;
77 view->Top();
78
79 // Raytracing will call VecGeom navigation
80 top->Raytrace();
81}
char name[80]
Definition TGX11.cxx:110
R__EXTERN TGeoManager * gGeoManager
#define gROOT
Definition TROOT.h:406
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
#define gPad
The color creation and management class.
Definition TColor.h:21
static Int_t GetFreeColorIndex()
Static function: Returns a free color index which can be used to define a user custom color.
Definition TColor.cxx:2245
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition TDatime.h:37
Int_t GetTime() const
Return time in form of 123623 (i.e. 12:36:23)
Definition TDatime.cxx:259
The manager class for any TGeo geometry.
Definition TGeoManager.h:44
Base class describing materials.
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition TGeoMedium.h:23
static TGeoTessellated * ImportFromObjFormat(const char *objfile, bool check=false, bool verbose=false)
Reader from .obj format.
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition TGeoVolume.h:43
void SetLineWidth(Width_t lwidth) override
Set the line width.
void Raytrace(Bool_t flag=kTRUE)
Draw this volume with current settings and perform raytracing in the pad.
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
void SetLineColor(Color_t lcolor) override
Set the line color.
Random number generator class based on M.
Definition TRandom3.h:27
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:615
Double_t Rndm() override
Machine independent random number generator.
Definition TRandom.cxx:559
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
Bool_t IsNull() const
Definition TString.h:414
See TView3D.
Definition TView.h:25
virtual void Top()=0
static TVirtualGeoConverter * Instance(TGeoManager *geom=nullptr)
Static function returning a pointer to the current geometry converter.