Geant4 User's Guide For Application Developers Visualization |
A set of visualization attributes is held by an instance of class G4VisAttributes defined in the graphics_reps category. In the following, we explain the main fields of the G4VisAttributes one by one.
void G4VisAttributes::SetVisibility (G4bool visibility);If you give false to the argument, and if culling is activated (see below), visualization is skipped for objects for which this set of visualization attributes is assigned. The default value of visibility is true.
Note that whether an object is visible or not is also affected by the current culling policy, which can be tuned with visualization commands.
By default the following public static function is defined:
static const G4VisAttributes& GetInvisible();which returns a reference to a const object in which visibility is set to false. It can be used as follows:
experimentalHall_logical -> SetVisAttributes (G4VisAttributes::GetInvisible());Direct access to the public static const data member G4VisAttributes::Invisible is also possible but deprecated on account of initialisation issues with dynamic libraries.
A G4Colour object is instantiated by giving red, green, and blue components to its constructor, i.e.,
G4Colour::G4Colour ( G4double r = 1.0, G4double g = 1.0, G4double b = 1.0, G4double a = 1.0); // 0<=red, green, blue <= 1.0The default value of each component is 1.0. That is to say, the default colour is "white" (opaque).
For example, colours which are often used can be instantiated as follows:
G4Colour white () ; // white G4Colour white (1.0, 1.0, 1.0) ; // white G4Colour gray (0.5, 0.5, 0.5) ; // gray G4Colour black (0.0, 0.0, 0.0) ; // black G4Colour red (1.0, 0.0, 0.0) ; // red G4Colour green (0.0, 1.0, 0.0) ; // green G4Colour blue (0.0, 0.0, 1.0) ; // blue G4Colour cyan (0.0, 1.0, 1.0) ; // cyan G4Colour magenta (1.0, 0.0, 1.0) ; // magenta G4Colour yellow (1.0, 1.0, 0.0) ; // yellow
It is also possible to instantiate common colours through static public data member functions:
static const G4Colour& White (); static const G4Colour& Gray (); static const G4Colour& Grey (); static const G4Colour& Black (); static const G4Colour& Red (); static const G4Colour& Green (); static const G4Colour& Blue (); static const G4Colour& Cyan (); static const G4Colour& Magenta (); static const G4Colour& Yellow ();
For example, a local G4Colour could be constructed as:
G4Colour myRed(G4Colour::Red());
After instantiation of a G4Colour object, you can access to its components with the following access functions:
G4double G4Colour::GetRed () const ; // Get the red component. G4double G4Colour::GetGreen () const ; // Get the green component. G4double G4Colour::GetBlue () const ; // Get the blue component.
G4String G4Colour --------------------------------------- white G4Colour::White () gray G4Colour::Gray () grey G4Colour::Grey () black G4Colour::Black () red G4Colour::Red () green G4Colour::Green () blue G4Colour::Blue () cyan G4Colour::Cyan () magenta G4Colour::Magenta () yellow G4Colour::Yellow ()
Colours can be retrieved through the GetColour method:
bool G4Colour::GetColour(const G4String& key, G4Colour& result)For example:
G4Colour myColour(G4Colour::Black()); if (G4Colour::GetColour("red", myColour)) { // Successfully retrieved colour "red". myColour is now red } else { // Colour did not exist in map. myColour is still black }
If the key is not registered in the colour map, a warning message is printed and the input colour is not changed. The colour map is case insensitive.
It is also possible to load user defined G4Colour's into the map through the public AddToMap method. For example:
G4Colour myColour(0.2, 0.2, 0.2, 1); G4Colour::AddToMap("custom", myColour);
This loads a user defined G4Colour with key "custom" into the colour map.
//----- Set functions of G4VisAttributes. void G4VisAttributes::SetColour (const G4Colour& colour); void G4VisAttributes::SetColor (const G4Color& color );We can also set RGBA components directly:
//----- Set functions of G4VisAttributes void G4VisAttributes::SetColour ( G4double red , G4double green , G4double blue , G4double alpha = 1.0); void G4VisAttributes::SetColor ( G4double red , G4double green , G4double blue , G4double alpha = 1.);The following constructor with G4Colour as its argument is also supported:
//----- Constructor of G4VisAttributes G4VisAttributes::G4VisAttributes (const G4Colour& colour);
Note that colour assigned to a G4VisAttributes object is not always the colour that ultimately appears in the visualization. The ultimate appearance may be affected by shading and lighting models applied in the selected visualization driver or stand-alone graphics system.
The forced wireframe and forced solid styles make it possible to mix the wireframe and surface visualization (if your selected graphics system supports such visualization). For example, you can make only the outer wall of your detector "wired" (transparent) and can see inside in detail.
Forced wireframe style is set with the following access function:
void G4VisAttributes::SetForceWireframe (G4bool force);If you give true as the argument, objects for which this set of visualization attributes is assigned are always visualized in wireframe even if in general, the surface drawing style has been requested. The default value of the forced wireframe style is false.
Similarly, forced solid style, i.e., to force that objects are always visualized with surfaces, is set with:
void G4VisAttributes::SetForceSolid (G4bool force);The default value of the forced solid style is false, too.
You can also force auxiliary edges to be visible. Normally they are not visible unless you set the appropriate view parameter. Forcing the auxiliary edges to be visible means that auxiliary edges will be seen whatever the view parameters.
Auxiliary edges are not genuine edges of the volume. They may be in a curved surface made out of polygons, for example, or in plane surface of complicated shape that has to be broken down into simpler polygons. HepPolyhedron breaks all surfaces into triangles or quadrilaterals. There will be auxiliary edges for any volumes with a curved surface, such as a tube or a sphere, or a volume resulting from a Boolean operation. Normally, they are not shown, but sometimes it is useful to see them. In particular, a sphere, because it has no egdes, will not be seen in wireframe mode in some graphics systems unless requested by the view parameters or forced, as described here.
To force auxiliary edges to be visible, use:
void G4VisAttributes::SetForceAuxEdgeVisible (G4bool force);The default value of the force auxiliary edges visible flag is false.
For volumes with edges that are parts of a circle, such as a tube (G4Tubs), etc., it is possible to force the precision of polyhedral representation for visualisation. This is recommended for volumes containing only a small angle of circle, for example, a thin tube segment.
For visualisation, a circle is represented by an N-sided polygon. The default is 24 sides or segments. The user may change this for all volumes in a particular viewer at run time with /vis/viewer/set/lineSegmentsPerCircle; alternatively it can be forced for a particular volume with:
void G4VisAttributes::SetForceLineSegmentsPerCircle (G4int nSegments);
//----- Constructors of class G4VisAttributes G4VisAttributes (void); G4VisAttributes (G4bool visibility); G4VisAttributes (const G4Colour& colour); G4VisAttributes (G4bool visibility, const G4Colour& colour);
Class G4LogicalVolume holds a pointer of G4VisAttributes. This field is set and referenced with the following access functions:
//----- Set functions of G4VisAttributes void G4VisAttributes::SetVisAttributes (const G4VisAttributes* pVA); void G4VisAttributes::SetVisAttributes (const G4VisAttributes& VA); //----- Get functions of G4VisAttributes const G4VisAttributes* G4VisAttributes::GetVisAttributes () const;
The following is sample C++ source codes for assigning a set of visualization attributes with cyan colour and forced wireframe style to a logical volume:
//----- C++ source codes: Assigning G4VisAttributes to a logical volume ... // Instantiation of a logical volume myTargetLog = new G4LogicalVolume( myTargetTube,BGO, "TLog", 0, 0, 0); ... // Instantiation of a set of visualization attributes with cyan colour G4VisAttributes * calTubeVisAtt = new G4VisAttributes(G4Colour(0.,1.,1.)); // Set the forced wireframe style calTubeVisAtt->SetForceWireframe(true); // Assignment of the visualization attributes to the logical volume myTargetLog->SetVisAttributes(calTubeVisAtt); //----- end of C++ source codes
Note that the life of the visualization attributes must be at least as long as the objects to which they are assigned; it is the users' responsibility to ensure this, and to delete the visualization attributes when they are no longer needed (or just leave them to die at the end of the job).
Define the attributes with lines such as:
std::mapThen fill the attributes with lines such as:* store = G4AttDefStore::GetInstance("G4Trajectory",isNew); G4String PN("PN"); (*store)[PN] = G4AttDef(PN,"Particle Name","Physics","","G4String"); G4String IMom("IMom"); (*store)[IMom] = G4AttDef(IMom, "Momentum of track at start of trajectory", "Physics","","G4ThreeVector");
std::vectorSee geant4/source/tracking/src/G4Trajectory.cc for a good example.* values = new std::vector ; values->push_back(G4AttValue("PN",ParticleName,"")); s.seekp(std::ios::beg); s << G4BestUnit(initialMomentum,"Energy") << std::ends; values->push_back(G4AttValue("IMom",c,""));
G4AttValue objects are light, containing just the value; for the long description and other sharable information the G4AttValue object refers to a G4AttDef object. They are based on the HepRep standard described at http://www.slac.stanford.edu/~perl/heprep/. Geant4 also provides an G4AttDefStore.
Geant4 provides some default examples of the use of this facility in the trajectory classes in /source/tracking such as G4Trajectory, G4SmoothTrajectory. G4Trajectory::CreateAttValues shows how G4AttValue objects can be made and G4Trajectory::GetAttDefs shows how to make the corresponding G4AttDef objects and use the G4AttDefStore. Note that the "user" of CreateAttValues guarantees to destroy them; this is a way of allowing creation on demand and leaving the G4Trajectory object, for example, free of such objects in memory. The comments in G4VTrajectory.hh explain further and additional insights might be obtained by looking at two methods which use them, namely G4VTrajectory::DrawTrajectory and G4VTrajectory::ShowTrajectory.
Hits classes in examples /extended/analysis/A01 and /extended/runAndEvent/RE01 show how to do the same for your hits. The base class no-action methods CreateAttValues and GetAttDefs should be overridden in your concrete class. The comments in G4VHit.hh explain further.
In addition, the user is free to add a G4std::vector
At the time of writing, only the HepRep graphics systems are capable of displaying the G4AttValue information, but this information will become useful for all Geant4 visualization systems through improvements in release 8.1 or later.