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
18// c++
19#include <numbers>
20
21namespace {
22bool is3DTextKind(const std::string& kind) {
23 return kind == "3D" || kind == "3d" || kind == "text" || kind == "Text";
24}
25
26std::string colourToRGB(const std::string& colour) {
27 if (colour.find(' ') != std::string::npos) { return colour; }
28 if (colour == "black") { return "0 0 0"; }
29 if (colour == "blue") { return "0 0 1"; }
30 if (colour == "cyan") { return "0 1 1"; }
31 if (colour == "green") { return "0 1 0"; }
32 if (colour == "grey" || colour == "gray") { return "0.9 0.9 0.9"; }
33 if (colour == "red") { return "1 0 0"; }
34 if (colour == "yellow") { return "1 1 0"; }
35 return "1 1 1";
36}
37}
38
39std::vector<std::string> G4SceneProperties::scene_commands(const std::shared_ptr<GOptions> &gopts) {
40 std::vector<std::string> cmds;
41
42 const bool gui = gopts->getSwitch("gui");
43 const bool use_dawn = gopts->getSwitch("useDawn");
44
45 // Project options onto simple structs for downstream use.
46 auto g4view = g4display::getG4View(gopts);
47 auto g4camera = g4display::getG4Camera(gopts);
48 auto g4light = g4display::getG4Light(gopts);
49
50 const bool open_configured_viewer = gui || g4view.driver == "TOOLSSG_OFFSCREEN";
51 if (!use_dawn && !open_configured_viewer) { return cmds; }
52
53 if (use_dawn) {
54 cmds.emplace_back("/vis/open DAWNFILE");
55 }
56 if (open_configured_viewer) {
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
63 // Open the viewer without a scene, then create and attach the initialized ROOT model. Opening a
64 // viewer on either an empty or a pre-populated unattached scene causes warnings or crashes in OGLSQt.
65 cmds.emplace_back("/vis/viewer/set/autoRefresh false");
66 cmds.emplace_back("/vis/scene/create gemc");
67 cmds.emplace_back("/vis/sceneHandler/attach");
68 cmds.emplace_back("/vis/scene/add/volume root -1");
69
70 if (use_dawn && !open_configured_viewer) {
71 cmds.emplace_back("/vis/geometry/set/visibility World 0 false");
72 cmds.emplace_back("/vis/viewer/set/style surface");
73 }
74 if (open_configured_viewer) {
75 // Convert configured camera angles to degrees for the Geant4 viewer command.
76 const double toDegrees = 180.0 / std::numbers::pi;
77 double thetaValue = gutilities::getG4Number(g4camera.theta) * toDegrees;
78 double phiValue = gutilities::getG4Number(g4camera.phi) * toDegrees;
79 double lightThetaValue = gutilities::getG4Number(g4light.theta) * toDegrees;
80 double lightPhiValue = gutilities::getG4Number(g4light.phi) * toDegrees;
81 if (lightThetaValue == 0.0 && lightPhiValue == 0.0) {
82 lightThetaValue = thetaValue;
83 lightPhiValue = phiValue;
84 }
85
86 cmds.emplace_back("/vis/viewer/set/viewpointThetaPhi " + std::to_string(thetaValue) + " " + std::to_string(phiValue));
87 cmds.emplace_back("/vis/viewer/set/lightsThetaPhi " + std::to_string(lightThetaValue) + " " + std::to_string(lightPhiValue));
88 cmds.emplace_back("/vis/viewer/set/lineSegmentsPerCircle " + std::to_string(g4view.segsPerCircle));
89 cmds.emplace_back("/vis/viewer/set/background " + g4view.background);
90 cmds.emplace_back("/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
91 }
92 cmds.emplace_back("/vis/viewer/set/autoRefresh true");
93
94 return cmds;
95}
96
97std::vector<std::string> G4SceneProperties::addSceneDecorations(const std::shared_ptr<GOptions> &gopts) {
98 std::vector<std::string> commands;
99 const auto decorations = g4display::getG4Decorations(gopts);
100
101 if (decorations.scale) {
102 commands.emplace_back("/vis/scene/add/scale " +
103 std::to_string(decorations.scaleLength) + " " +
104 decorations.scaleUnit + " " +
105 decorations.scaleDirection + " " +
106 colourToRGB(decorations.scaleColor));
107 }
108 if (decorations.axes) { commands.emplace_back("/vis/scene/add/axes"); }
109 if (decorations.date) {
110 commands.emplace_back("/vis/scene/add/date " + std::to_string(decorations.dateSize));
111 }
112 if (decorations.logo2D) { commands.emplace_back("/vis/scene/add/logo2D"); }
113 if (decorations.logo) { commands.emplace_back("/vis/scene/add/logo"); }
114 if (decorations.frame) {
115 commands.emplace_back("/vis/set/colour " + decorations.frameColor);
116 commands.emplace_back("/vis/set/lineWidth " + std::to_string(decorations.frameLineWidth));
117 commands.emplace_back("/vis/scene/add/frame");
118 commands.emplace_back("/vis/set/colour");
119 commands.emplace_back("/vis/set/lineWidth");
120 }
121
122 return commands;
123}
124
125std::vector<std::string> G4SceneProperties::addSceneTexts(const std::shared_ptr<GOptions> &gopts) {
126 std::vector<std::string> commands;
127
128 std::vector<g4display::G4SceneText> text_to_add = g4display::getSceneTexts(gopts);
129
130 // Map each configured text item into Geant4 text commands.
131 for (const auto &text: text_to_add) {
132 commands.emplace_back("/vis/set/textColour " + text.color);
133 if (!text.layout.empty()) {
134 commands.emplace_back("/vis/set/textLayout " + text.layout);
135 }
136
137 if (is3DTextKind(text.kind)) {
138 if (!text.z) {
139 log->error(goptions::EC__MANDATORY_NOT_FILLED, "A 3D g4text entry requires a z coordinate");
140 }
141 commands.emplace_back(
142 "/vis/scene/add/text " +
143 std::to_string(text.x) + " " +
144 std::to_string(text.y) + " " +
145 std::to_string(*text.z) + " " +
146 text.unit + " " +
147 std::to_string(text.size) + " " +
148 std::to_string(text.dx) + " " +
149 std::to_string(text.dy) + " " +
150 text.text);
151 } else {
152 commands.emplace_back(
153 "/vis/scene/add/text2D " +
154 std::to_string(text.x) + " " +
155 std::to_string(text.y) + " " +
156 std::to_string(text.size) + " ! ! " +
157 text.text);
158 }
159
160 if (!text.layout.empty()) {
161 commands.emplace_back("/vis/set/textLayout left");
162 }
163
164 // Restore default text color.
165 commands.emplace_back("/vis/set/textColour");
166 }
167
168 return commands;
169}
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)