g4display
Loading...
Searching...
No Matches
g4SceneProperties.cc
Go to the documentation of this file.
1
7
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 const bool gui = gopts->getSwitch("gui");
40 const 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 const bool open_configured_viewer = gui || g4view.driver == "TOOLSSG_OFFSCREEN";
48 if (!use_dawn && !open_configured_viewer) { return cmds; }
49
50 if (use_dawn) {
51 cmds.emplace_back("/vis/open DAWNFILE");
52 }
53 if (open_configured_viewer) {
54 // Open the configured viewer driver. Offscreen drivers ignore window position, so omit it.
55 std::string openArg = g4view.driver + " " + g4view.dimension;
56 if (g4view.driver != "TOOLSSG_OFFSCREEN") openArg += g4view.position;
57 cmds.emplace_back("/vis/open " + openArg);
58 }
59
60 // Open the viewer without a scene, then create and attach the initialized ROOT model. Opening a
61 // viewer on either an empty or a pre-populated unattached scene causes warnings or crashes in OGLSQt.
62 cmds.emplace_back("/vis/viewer/set/autoRefresh false");
63 cmds.emplace_back("/vis/scene/create gemc");
64 cmds.emplace_back("/vis/sceneHandler/attach");
65 cmds.emplace_back("/vis/scene/add/volume root -1");
66
67 if (use_dawn && !open_configured_viewer) {
68 cmds.emplace_back("/vis/geometry/set/visibility World 0 false");
69 cmds.emplace_back("/vis/viewer/set/style surface");
70 }
71 if (open_configured_viewer) {
72 // Convert configured camera angles to degrees for the Geant4 viewer command.
73 const double toDegrees = 180.0 / M_PI;
74 double thetaValue = gutilities::getG4Number(g4camera.theta) * toDegrees;
75 double phiValue = gutilities::getG4Number(g4camera.phi) * toDegrees;
76 double lightThetaValue = gutilities::getG4Number(g4light.theta) * toDegrees;
77 double lightPhiValue = gutilities::getG4Number(g4light.phi) * toDegrees;
78 if (lightThetaValue == 0.0 && lightPhiValue == 0.0) {
79 lightThetaValue = thetaValue;
80 lightPhiValue = phiValue;
81 }
82
83 cmds.emplace_back("/vis/viewer/set/viewpointThetaPhi " + std::to_string(thetaValue) + " " + std::to_string(phiValue));
84 cmds.emplace_back("/vis/viewer/set/lightsThetaPhi " + std::to_string(lightThetaValue) + " " + std::to_string(lightPhiValue));
85 cmds.emplace_back("/vis/viewer/set/lineSegmentsPerCircle " + std::to_string(g4view.segsPerCircle));
86 cmds.emplace_back("/vis/viewer/set/background " + g4view.background);
87 cmds.emplace_back("/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
88 }
89 cmds.emplace_back("/vis/viewer/set/autoRefresh true");
90
91 return cmds;
92}
93
94std::vector<std::string> G4SceneProperties::addSceneDecorations(const std::shared_ptr<GOptions> &gopts) {
95 std::vector<std::string> commands;
96 const auto decorations = g4display::getG4Decorations(gopts);
97
98 if (decorations.scale) {
99 commands.emplace_back("/vis/scene/add/scale " +
100 std::to_string(decorations.scaleLength) + " " +
101 decorations.scaleUnit + " " +
102 decorations.scaleDirection + " " +
103 colourToRGB(decorations.scaleColor));
104 }
105 if (decorations.axes) { commands.emplace_back("/vis/scene/add/axes"); }
106 if (decorations.date) {
107 commands.emplace_back("/vis/scene/add/date " + std::to_string(decorations.dateSize));
108 }
109 if (decorations.logo2D) { commands.emplace_back("/vis/scene/add/logo2D"); }
110 if (decorations.logo) { commands.emplace_back("/vis/scene/add/logo"); }
111 if (decorations.frame) {
112 commands.emplace_back("/vis/set/colour " + decorations.frameColor);
113 commands.emplace_back("/vis/set/lineWidth " + std::to_string(decorations.frameLineWidth));
114 commands.emplace_back("/vis/scene/add/frame");
115 commands.emplace_back("/vis/set/colour");
116 commands.emplace_back("/vis/set/lineWidth");
117 }
118
119 return commands;
120}
121
122std::vector<std::string> G4SceneProperties::addSceneTexts(const std::shared_ptr<GOptions> &gopts) {
123 std::vector<std::string> commands;
124
125 std::vector<g4display::G4SceneText> text_to_add = g4display::getSceneTexts(gopts);
126
127 // Map each configured text item into Geant4 text commands.
128 for (const auto &text: text_to_add) {
129 commands.emplace_back("/vis/set/textColour " + text.color);
130 if (!text.layout.empty()) {
131 commands.emplace_back("/vis/set/textLayout " + text.layout);
132 }
133
134 if (is3DTextKind(text.kind)) {
135 if (!text.z) {
137 "A 3D g4text entry requires a z coordinate");
138 }
139 commands.emplace_back(
140 "/vis/scene/add/text " +
141 std::to_string(text.x) + " " +
142 std::to_string(text.y) + " " +
143 std::to_string(*text.z) + " " +
144 text.unit + " " +
145 std::to_string(text.size) + " " +
146 std::to_string(text.dx) + " " +
147 std::to_string(text.dy) + " " +
148 text.text);
149 } else {
150 commands.emplace_back(
151 "/vis/scene/add/text2D " +
152 std::to_string(text.x) + " " +
153 std::to_string(text.y) + " " +
154 std::to_string(text.size) + " ! ! " +
155 text.text);
156 }
157
158 if (!text.layout.empty()) {
159 commands.emplace_back("/vis/set/textLayout left");
160 }
161
162 // Restore default text color.
163 commands.emplace_back("/vis/set/textColour");
164 }
165
166 return commands;
167}
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.
std::shared_ptr< GLogger > log
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.
constexpr int EC__MANDATORY_NOT_FILLED
double getG4Number(const string &v, bool warnIfNotUnit=false)