Logo ROOT  
Reference Guide
visualizeWavefrontObj.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>
16 
17 //______________________________________________________________________________
18 int randomColor()
19 {
20  gRandom = new TRandom3();
21  TDatime dt;
22  gRandom->SetSeed(dt.GetTime());
23  int ci = TColor::GetFreeColorIndex();
24  TColor *color = new TColor(ci, gRandom->Rndm(), gRandom->Rndm(), gRandom->Rndm());
25  return ci;
26 }
27 
28 //______________________________________________________________________________
29 void visualizeWavefrontObj(const char *dot_obj_file="", bool check = false)
30 {
31  // Input a file in .obj format (https://en.wikipedia.org/wiki/Wavefront_.obj_file)
32  // The file should have a single object inside, only vertex and faces information is used
33 
34  TString name = dot_obj_file;
35  TString sfile = dot_obj_file;
36  if (sfile.IsNull()) {
37  sfile = gROOT->GetTutorialsDir();
38  sfile += "/geom/teddy.obj";
39  }
40  name.ReplaceAll(".obj", "");
41  gROOT->GetListOfCanvases()->Delete();
42  if (gGeoManager)
43  delete gGeoManager;
44  new TGeoManager(name, "Imported from .obj file");
45  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98, 13, 2.7);
46  TGeoMedium *med = new TGeoMedium("MED", 1, mat);
47  TGeoVolume *top = gGeoManager->MakeBox("TOP", med, 10, 10, 10);
49 
50  auto tsl = TGeoTessellated::ImportFromObjFormat(sfile.Data(), check);
51  if (!tsl) return;
52  tsl->ResizeCenter(5.);
53 
54  TGeoVolume *vol = new TGeoVolume(name, tsl, med);
55  vol->SetLineColor(randomColor());
56  vol->SetLineWidth(2);
57  top->AddNode(vol, 1);
59  if (!gROOT->IsBatch()) top->Draw("ogl");
60 }
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
gGeoManager
R__EXTERN TGeoManager * gGeoManager
Definition: TGeoManager.h:602
TString::Data
const char * Data() const
Definition: TString.h:369
TDatime.h
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
TColor::GetFreeColorIndex
static Int_t GetFreeColorIndex()
Static function: Returns a free color index which can be used to define a user custom color.
Definition: TColor.cxx:2002
TGeoVolume::SetLineWidth
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TGeoVolume.cxx:2052
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
TColor.h
TDatime::GetTime
Int_t GetTime() const
Return time in form of 123623 (i.e. 12:36:23)
Definition: TDatime.cxx:259
TString
Basic string class.
Definition: TString.h:136
TGeoMaterial
Base class describing materials.
Definition: TGeoMaterial.h:36
TROOT.h
TGeoManager::SetTopVolume
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
Definition: TGeoManager.cxx:3397
TColor
The color creation and management class.
Definition: TColor.h:19
TRandom3
Random number generator class based on M.
Definition: TRandom3.h:27
TGeoVolume::Draw
virtual void Draw(Option_t *option="")
draw top volume according to option
Definition: TGeoVolume.cxx:1146
gRandom
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
TRandom::SetSeed
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:608
TRandom3.h
TRandom::Rndm
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:552
TGeoManager.h
TString::IsNull
Bool_t IsNull() const
Definition: TString.h:407
name
char name[80]
Definition: TGX11.cxx:110
TGeoTessellated::ImportFromObjFormat
static TGeoTessellated * ImportFromObjFormat(const char *objfile, bool check=false, bool verbose=false)
Reader from .obj format.
Definition: TGeoTessellated.cxx:661
TDatime
This class stores the date and time with a precision of one second in an unsigned 32 bit word (950130...
Definition: TDatime.h:37
TGeoMedium
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition: TGeoMedium.h:24
TGeoVolume::SetLineColor
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TGeoVolume.cxx:2036
TGeoManager
The manager class for any TGeo geometry.
Definition: TGeoManager.h:45
TGeoTessellated.h
TGeoVolume
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition: TGeoVolume.h:49
gROOT
#define gROOT
Definition: TROOT.h:406