g4display
Loading...
Searching...
No Matches
g4SceneProperties.cc
Go to the documentation of this file.
1
8// g4display
9#include "g4SceneProperties.h"
10#include "g4Text.h"
11
12// gemc
13#include "gutilities.h"
14
15// geant4
16#include "G4UImanager.hh"
17
18namespace {
19bool is3DTextKind(const std::string& kind) {
20 return kind == "3D" || kind == "3d" || kind == "text" || kind == "Text";
21}
22
23std::string colourToRGB(const std::string& colour) {
24 if (colour.find(' ') != std::string::npos) { return colour; }
25 if (colour == "black") { return "0 0 0"; }
26 if (colour == "blue") { return "0 0 1"; }
27 if (colour == "cyan") { return "0 1 1"; }
28 if (colour == "green") { return "0 1 0"; }
29 if (colour == "grey" || colour == "gray") { return "0.9 0.9 0.9"; }
30 if (colour == "red") { return "1 0 0"; }
31 if (colour == "yellow") { return "1 1 0"; }
32 return "1 1 1";
33}
34}
35
36std::vector<std::string> G4SceneProperties::scene_commands(const std::shared_ptr<GOptions> &gopts) {
37 std::vector<std::string> cmds;
38
39 bool gui = gopts->getSwitch("gui");
40 bool use_dawn = gopts->getSwitch("useDawn");
41
42 // Project options onto simple structs for downstream use.
43 auto g4view = g4display::getG4View(gopts);
44 auto g4camera = g4display::getG4Camera(gopts);
45 auto g4light = g4display::getG4Light(gopts);
46
47 // Create a named scene. Caller is expected to apply these commands to the Geant4 UI manager.
48 cmds.emplace_back("/vis/scene/create gemc");
49
50 if (use_dawn) {
51 // DAWNFILE workflow: open the DAWN viewer and adjust a minimal set of scene properties.
52 cmds.emplace_back("/vis/open DAWNFILE");
53 cmds.emplace_back("/vis/geometry/set/visibility World 0 false");
54 cmds.emplace_back("/vis/viewer/set/style surface");
55 }
56 if (gui || g4view.driver == "TOOLSSG_OFFSCREEN") {
57 // Open the configured viewer driver. Offscreen drivers ignore window position, so omit it.
58 std::string openArg = g4view.driver + " " + g4view.dimension;
59 if (g4view.driver != "TOOLSSG_OFFSCREEN") openArg += g4view.position;
60 cmds.emplace_back("/vis/open " + openArg);
61
62 // Convert configured camera angles to degrees for the Geant4 viewer command.
63 const double toDegrees = 180.0 / M_PI;
64 double thetaValue = gutilities::getG4Number(g4camera.theta) * toDegrees;
65 double phiValue = gutilities::getG4Number(g4camera.phi) * toDegrees;
66 double lightThetaValue = gutilities::getG4Number(g4light.theta) * toDegrees;
67 double lightPhiValue = gutilities::getG4Number(g4light.phi) * toDegrees;
68 if (lightThetaValue == 0.0 && lightPhiValue == 0.0) {
69 lightThetaValue = thetaValue;
70 lightPhiValue = phiValue;
71 }
72
73 // Disable auto refresh and quieten vis messages whilst scene and trajectories are established.
74 cmds.emplace_back("/vis/viewer/set/autoRefresh false");
75
76 cmds.emplace_back("/vis/viewer/set/viewpointThetaPhi " + std::to_string(thetaValue) + " " + std::to_string(phiValue));
77 cmds.emplace_back("/vis/viewer/set/lightsThetaPhi " + std::to_string(lightThetaValue) + " " + std::to_string(lightPhiValue));
78 cmds.emplace_back("/vis/viewer/set/lineSegmentsPerCircle " + std::to_string(g4view.segsPerCircle));
79 cmds.emplace_back("/vis/viewer/set/background " + g4view.background);
80 cmds.emplace_back("/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
81
82 cmds.emplace_back("/vis/viewer/set/autoRefresh true");
83 }
84
85 return cmds;
86}
87
88std::vector<std::string> G4SceneProperties::addSceneDecorations(const std::shared_ptr<GOptions> &gopts) {
89 std::vector<std::string> commands;
90 const auto decorations = g4display::getG4Decorations(gopts);
91
92 if (decorations.scale) {
93 commands.emplace_back("/vis/scene/add/scale " +
94 std::to_string(decorations.scaleLength) + " " +
95 decorations.scaleUnit + " " +
96 decorations.scaleDirection + " " +
97 colourToRGB(decorations.scaleColor));
98 }
99 if (decorations.axes) { commands.emplace_back("/vis/scene/add/axes"); }
100 if (decorations.date) {
101 commands.emplace_back("/vis/scene/add/date " + std::to_string(decorations.dateSize));
102 }
103 if (decorations.logo2D) { commands.emplace_back("/vis/scene/add/logo2D"); }
104 if (decorations.logo) { commands.emplace_back("/vis/scene/add/logo"); }
105 if (decorations.frame) {
106 commands.emplace_back("/vis/set/colour " + decorations.frameColor);
107 commands.emplace_back("/vis/set/lineWidth " + std::to_string(decorations.frameLineWidth));
108 commands.emplace_back("/vis/scene/add/frame");
109 commands.emplace_back("/vis/set/colour");
110 commands.emplace_back("/vis/set/lineWidth");
111 }
112
113 return commands;
114}
115
116std::vector<std::string> G4SceneProperties::addSceneTexts(const std::shared_ptr<GOptions> &gopts) {
117 std::vector<std::string> commands;
118
119 std::vector<g4display::G4SceneText> text_to_add = g4display::getSceneTexts(gopts);
120
121 // Map each configured text item into Geant4 text commands.
122 for (const auto &text: text_to_add) {
123 commands.emplace_back("/vis/set/textColour " + text.color);
124 if (!text.layout.empty()) {
125 commands.emplace_back("/vis/set/textLayout " + text.layout);
126 }
127
128 if (is3DTextKind(text.kind)) {
129 commands.emplace_back(
130 "/vis/scene/add/text " +
131 std::to_string(text.x) + " " +
132 std::to_string(text.y) + " " +
133 std::to_string(text.z) + " " +
134 text.unit + " " +
135 std::to_string(text.size) + " " +
136 std::to_string(text.dx) + " " +
137 std::to_string(text.dy) + " " +
138 text.text);
139 } else {
140 commands.emplace_back(
141 "/vis/scene/add/text2D " +
142 std::to_string(text.x) + " " +
143 std::to_string(text.y) + " " +
144 std::to_string(text.size) + " ! ! " +
145 text.text);
146 }
147
148 if (!text.layout.empty()) {
149 commands.emplace_back("/vis/set/textLayout left");
150 }
151
152 // Restore default text color.
153 commands.emplace_back("/vis/set/textColour");
154 }
155
156 return commands;
157}
std::vector< std::string > addSceneTexts(const std::shared_ptr< GOptions > &gopts)
Build commands that insert configured text annotations into the scene.
std::vector< std::string > addSceneDecorations(const std::shared_ptr< GOptions > &gopts)
Build commands for optional scene decorations such as scale, axes, logos, and frame.
std::vector< std::string > scene_commands(const std::shared_ptr< GOptions > &gopts)
Build the full command sequence for scene initialization.
Declaration of the G4SceneProperties helper used to initialize Geant4 scene visualization.
Scene text option structures and helpers for the g4display module.
G4Light getG4Light(const std::shared_ptr< GOptions > &gopts)
Read the g4light option node and return a projected G4Light struct.
G4View getG4View(const std::shared_ptr< GOptions > &gopts)
Read the g4view option node and return a projected G4View struct.
vector< G4SceneText > getSceneTexts(const std::shared_ptr< GOptions > &gopts)
Extract scene text entries from the g4text option node.
Definition g4Text.cc:13
G4Camera getG4Camera(const std::shared_ptr< GOptions > &gopts)
Read the g4camera option node and return a projected G4Camera struct.
G4Decorations getG4Decorations(const std::shared_ptr< GOptions > &gopts)
Read the g4decoration option node and return scene decoration settings.
double getG4Number(const string &v, bool warnIfNotUnit=false)