Geant4-11
G4VVisCommand.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27
28// Base class for visualization commands - John Allison 9th August 1998
29// It is really a messenger - we have one command per messenger.
30
31#include "G4VVisCommand.hh"
32
33#include "G4UIcommand.hh"
34#include "G4UImanager.hh"
35#include "G4UnitsTable.hh"
36#include <sstream>
37#include <cctype>
38
45// Not yet used: G4VisAttributes::LineStyle G4VVisCommand::fCurrentLineStyle = G4VisAttributes::unbroken;
46// Not yet used: G4VMarker::FillStyle G4VVisCommand::fCurrentFillStyle = G4VMarker::filled;
47// Not yet used: G4VMarker::SizeType G4VVisCommand::fCurrentSizeType = G4VMarker::screen;
50std::vector<G4PhysicalVolumesSearchScene::Findings> G4VVisCommand::fCurrrentPVFindingsForField;
53
55
57
59
61{
62 return fpVisManager;
63}
64
66{
67 fpVisManager = pVisManager;
68}
69
71{
72 return fCurrentTextColour;
73}
74
76(G4double x, G4double y, const char * unitName)
77{
78 G4double uv = G4UIcommand::ValueOf(unitName);
79
80 std::ostringstream oss;
81 oss << x/uv << " " << y/uv << " " << unitName;
82 return oss.str();
83}
84
86 G4double& xval,
87 G4double& yval)
88{
89 G4double x, y;
90 G4String unit;
91
92 std::istringstream is(paramString);
93 is >> x >> y >> unit;
94
96 xval = x*G4UIcommand::ValueOf(unit);
97 yval = y*G4UIcommand::ValueOf(unit);
98 } else {
100 if (verbosity >= G4VisManager::errors) {
101 G4cout << "ERROR: Unrecognised unit" << G4endl;
102 }
103 return false;
104 }
105
106 return true;
107}
108
110{
111 static G4String guidance
112 ("Accepts (a) RGB triplet. e.g., \".3 .4 .5\", or"
113 "\n (b) string such as \"white\", \"black\", \"grey\", \"red\"...or"
114 "\n (c) an additional number for opacity, e.g., \".3 .4 .5 .6\""
115 "\n or \"grey ! ! .6\" (note \"!\"'s for unused parameters).");
116 return guidance;
117}
118
120(G4Colour& colour,
121 const G4String& redOrString, G4double green, G4double blue, G4double opacity)
122{
123 // Note: colour is supplied by the caller and some or all of its components
124 // may act as default.
125 //
126 // Note: redOrString is either a number or string. If a string it must be
127 // one of the recognised colours.
128 //
129 // Thus the arguments can be, for example:
130 // (colour,"red",...,...,0.5): will give the colour red with opacity 0.5 (the
131 // third and fourth arguments are ignored), or
132 // (1.,0.,0.,0.5): this also will be red with opacity 0.5.
133
135
136 const size_t iPos0 = 0;
137 if (std::isalpha(redOrString[iPos0])) {
138
139 // redOrString is probably alphabetic characters defining the colour
140 if (!G4Colour::GetColour(redOrString, colour)) {
141 // Not a recognised string
142 if (verbosity >= G4VisManager::warnings) {
143 G4cout << "WARNING: Colour \"" << redOrString
144 << "\" not found. Defaulting to " << colour
145 << G4endl;
146 }
147 return;
148 } else {
149 // It was a recognised string. Now add opacity.
150 colour.SetAlpha(opacity);
151 return;
152 }
153
154 } else {
155
156 // redOrString is probably numeric defining the red component
157 std::istringstream iss(redOrString);
158 G4double red;
159 iss >> red;
160 if (iss.fail()) {
161 if (verbosity >= G4VisManager::warnings) {
162 G4cout << "WARNING: String \"" << redOrString
163 << "\" cannot be parsed. Defaulting to " << colour
164 << G4endl;
165 }
166 return;
167 } else {
168 colour = G4Colour(red,green,blue,opacity);
169 return;
170 }
171
172 }
173}
174
176(const G4String& where,
177 const G4String& unit,
178 const G4String& category,
179 G4double& value)
180{
181 // Return false if there's a problem
182
184
185 G4bool success = true;
187 if (verbosity >= G4VisManager::warnings) {
188 G4cerr << where
189 << "\n Unit \"" << unit << "\" not defined"
190 << G4endl;
191 }
192 success = false;
193 } else if (G4UnitDefinition::GetCategory(unit) != category) {
194 if (verbosity >= G4VisManager::warnings) {
195 G4cerr << where
196 << "\n Unit \"" << unit << "\" not a unit of " << category;
197 if (category == "Volumic Mass") G4cerr << " (density)";
198 G4cerr << G4endl;
199 }
200 success = false;
201 } else {
202 value = G4UnitDefinition::GetValueOf(unit);
203 }
204 return success;
205}
206
208{
210
211 if (!pScene) {
212 if (verbosity >= G4VisManager::warnings) {
213 G4cout << "WARNING: Scene pointer is null."
214 << G4endl;
215 }
216 return;
217 }
218
219 G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler();
220 if (!pSceneHandler) {
221 if (verbosity >= G4VisManager::warnings) {
222 G4cout << "WARNING: Scene handler not found." << G4endl;
223 }
224 return;
225 }
226
227 // Scene has changed. If it is the scene of the currrent scene handler
228 // refresh viewers of all scene handlers using this scene. If not, it may be
229 // a scene that the user is building up before attaching to a scene handler,
230 // so do nothing.
231 if (pScene == pSceneHandler->GetScene()) {
232 G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/scene/notifyHandlers");
233 }
234
235}
236
238{
240 G4VViewer* viewer = fpVisManager -> GetCurrentViewer ();
241
242 if (!viewer) {
243 if (verbosity >= G4VisManager::errors) {
244 G4cerr <<
245 "ERROR: No current viewer - \"/vis/viewer/list\" to see possibilities."
246 << G4endl;
247 }
248 return false;
249 }
250
251 return true;
252}
253
255(G4VisManager::Verbosity verbosity) {
256 // Some frequently used error printing...
257 if (verbosity >= G4VisManager::warnings) {
258 G4cout <<
259 "WARNING: For some reason, possibly mentioned above, it has not been"
260 "\n possible to add to the scene."
261 << G4endl;
262 }
263}
264
266(G4VViewer* viewer, const G4ViewParameters& viewParams) {
267 viewer->SetViewParameters(viewParams);
268 RefreshIfRequired(viewer);
269}
270
273 G4VSceneHandler* sceneHandler = viewer->GetSceneHandler();
274 const G4ViewParameters& viewParams = viewer->GetViewParameters();
275 if (sceneHandler && sceneHandler->GetScene()) {
276 if (viewParams.IsAutoRefresh()) {
277 G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/refresh");
278 }
279 else {
280 if (verbosity >= G4VisManager::warnings) {
281 G4cout << "Issue /vis/viewer/refresh or flush to see effect."
282 << G4endl;
283 }
284 }
285 }
286}
287
289(G4VViewer* currentViewer,
290 std::vector<G4ViewParameters> viewVector,
291 const G4int nInterpolationPoints,
292 const G4int waitTimePerPointmilliseconds,
293 const G4String exportString)
294{
295 const G4int safety = viewVector.size()*nInterpolationPoints;
296 G4int safetyCount = 0;
297 do {
298 G4ViewParameters* vp =
299 G4ViewParameters::CatmullRomCubicSplineInterpolation(viewVector,nInterpolationPoints);
300 if (!vp) break; // Finished.
301 currentViewer->SetViewParameters(*vp);
302 currentViewer->RefreshView();
303 if (exportString == "export" &&
304 G4StrUtil::contains(currentViewer->GetName(), "OpenGL")) {
305 G4UImanager::GetUIpointer()->ApplyCommand("/vis/ogl/export");
306 }
307 currentViewer->ShowView();
308 if (waitTimePerPointmilliseconds > 0)
309 std::this_thread::sleep_for(std::chrono::milliseconds(waitTimePerPointmilliseconds));
310 } while (safetyCount++ < safety); // Loop checking, 16.02.2016, J.Allison
311}
312
314(G4VViewer* currentViewer,
315 const G4ViewParameters& oldVP,
316 const G4ViewParameters& newVP,
317 const G4int nInterpolationPoints,
318 const G4int waitTimePerPointmilliseconds,
319 const G4String exportString)
320{
321 std::vector<G4ViewParameters> viewVector;
322 viewVector.push_back(oldVP);
323 viewVector.push_back(oldVP);
324 viewVector.push_back(newVP);
325 viewVector.push_back(newVP);
326
328 (currentViewer,
329 viewVector,
330 nInterpolationPoints,
331 waitTimePerPointmilliseconds,
332 exportString);
333}
334
336(const G4UIcommand* fromCmd, G4UIcommand* toCmd, G4int startLine)
337{
338 if (fromCmd && toCmd) {
339 const G4int nGuideEntries = fromCmd->GetGuidanceEntries();
340 for (G4int i = startLine; i < nGuideEntries; ++i) {
341 const G4String& guidance = fromCmd->GetGuidanceLine(i);
342 toCmd->SetGuidance(guidance);
343 }
344 }
345}
346
348(const G4UIcommand* fromCmd, G4UIcommand* toCmd)
349{
350 if (fromCmd && toCmd) {
351 const G4int nParEntries = fromCmd->GetParameterEntries();
352 for (G4int i = 0; i < nParEntries; ++i) {
353 G4UIparameter* parameter = new G4UIparameter(*(fromCmd->GetParameter(i)));
354 toCmd->SetParameter(parameter);
355 }
356 }
357}
358
360{
361 if (fpVisManager) {
362 const G4double halfX = (extent.GetXmax() - extent.GetXmin()) / 2.;
363 const G4double halfY = (extent.GetYmax() - extent.GetYmin()) / 2.;
364 const G4double halfZ = (extent.GetZmax() - extent.GetZmin()) / 2.;
365 if (halfX > 0. && halfY > 0. && halfZ > 0.) {
366 const G4Box box("vis_extent",halfX,halfY,halfZ);
367 const G4VisAttributes visAtts(G4Color::Red());
368 const G4Point3D& centre = extent.GetExtentCenter();
369 fpVisManager->Draw(box,visAtts,G4Translate3D(centre));
370 }
371 }
372}
373
375(G4ViewParameters& target, const G4ViewParameters& from)
376{
377 // Copy view parameters except for autoRefresh and background...
378 auto targetAutoRefresh = target.IsAutoRefresh();
379 auto targetBackground = target.GetBackgroundColour();
380 target = from;
381 target.SetAutoRefresh(targetAutoRefresh);
382 target.SetBackgroundColour(targetBackground);
383}
384
386(G4ViewParameters& target, const G4ViewParameters& from)
387{
388 // Copy view parameters pertaining only to camera
392 target.SetUpVector (from.GetUpVector());
393 target.SetFieldHalfAngle (from.GetFieldHalfAngle());
394 target.SetZoomFactor (from.GetZoomFactor());
395 target.SetScaleFactor (from.GetScaleFactor());
397 target.SetDolly (from.GetDolly());
398}
HepGeom::Translate3D G4Translate3D
double G4double
Definition: G4Types.hh:83
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4GLOB_DLL std::ostream G4cerr
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
Definition: G4Box.hh:56
static G4Colour White()
Definition: G4Colour.hh:156
static G4bool GetColour(const G4String &key, G4Colour &result)
Definition: G4Colour.cc:161
void SetAlpha(G4double)
Definition: G4Colour.cc:70
static G4Colour Red()
Definition: G4Colour.hh:161
static G4Colour Blue()
Definition: G4Colour.hh:163
Layout
Definition: G4Text.hh:76
@ left
Definition: G4Text.hh:76
std::size_t GetParameterEntries() const
Definition: G4UIcommand.hh:138
const G4String & GetGuidanceLine(G4int i) const
Definition: G4UIcommand.hh:132
G4UIparameter * GetParameter(G4int i) const
Definition: G4UIcommand.hh:139
static G4double ValueOf(const char *unitName)
Definition: G4UIcommand.cc:363
void SetParameter(G4UIparameter *const newParameter)
Definition: G4UIcommand.hh:146
void SetGuidance(const char *aGuidance)
Definition: G4UIcommand.hh:156
std::size_t GetGuidanceEntries() const
Definition: G4UIcommand.hh:128
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:485
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
static G4bool IsUnitDefined(const G4String &)
static G4double GetValueOf(const G4String &)
static G4String GetCategory(const G4String &)
G4Scene * GetScene() const
const G4String & GetName() const
const G4ViewParameters & GetViewParameters() const
void SetViewParameters(const G4ViewParameters &vp)
Definition: G4VViewer.cc:120
void RefreshView()
virtual void ShowView()
Definition: G4VViewer.cc:103
G4VSceneHandler * GetSceneHandler() const
static G4double fCurrentTextSize
void G4VisCommandsSceneAddUnsuccessful(G4VisManager::Verbosity verbosity)
static void SetVisManager(G4VisManager *pVisManager)
static G4Colour fCurrentTextColour
void CopyCameraParameters(G4ViewParameters &target, const G4ViewParameters &from)
static G4VisManager * GetVisManager()
void CheckSceneAndNotifyHandlers(G4Scene *=nullptr)
static std::vector< G4PhysicalVolumesSearchScene::Findings > fCurrrentPVFindingsForField
G4bool CheckView()
void ConvertToColour(G4Colour &colour, const G4String &redOrString, G4double green, G4double blue, G4double opacity)
static G4VisManager * fpVisManager
static G4VisExtent fCurrentExtentForField
void DrawExtent(const G4VisExtent &)
void InterpolateToNewView(G4VViewer *currentViewer, const G4ViewParameters &oldVP, const G4ViewParameters &newVP, const G4int nInterpolationPoints=50, const G4int waitTimePerPointmilliseconds=20, const G4String exportString="")
static const G4Colour & GetCurrentTextColour()
static G4bool ConvertToDoublePair(const G4String &paramString, G4double &xval, G4double &yval)
void RefreshIfRequired(G4VViewer *viewer)
void SetViewParameters(G4VViewer *viewer, const G4ViewParameters &viewParams)
const G4String & ConvertToColourGuidance()
void CopyParametersFrom(const G4UIcommand *fromCmd, G4UIcommand *toCmd)
static G4int fCurrentArrow3DLineSegmentsPerCircle
static G4ViewParameters fVPExistingViewer
static G4PhysicalVolumeModel::TouchableProperties fCurrentTouchableProperties
G4bool ProvideValueOfUnit(const G4String &where, const G4String &unit, const G4String &category, G4double &value)
void CopyMostViewParameters(G4ViewParameters &target, const G4ViewParameters &from)
static G4Text::Layout fCurrentTextLayout
static G4bool fThereWasAViewer
virtual ~G4VVisCommand()
static G4double fCurrentLineWidth
static G4String ConvertToString(G4double x, G4double y, const char *unitName)
static G4Colour fCurrentColour
void CopyGuidanceFrom(const G4UIcommand *fromCmd, G4UIcommand *toCmd, G4int startLine=0)
void InterpolateViews(G4VViewer *currentViewer, std::vector< G4ViewParameters > viewVector, const G4int nInterpolationPoints=50, const G4int waitTimePerPointmilliseconds=20, const G4String exportString="")
void SetViewpointDirection(const G4Vector3D &viewpointDirection)
void SetScaleFactor(const G4Vector3D &scaleFactor)
static G4ViewParameters * CatmullRomCubicSplineInterpolation(const std::vector< G4ViewParameters > &views, G4int nInterpolationPoints=50)
const G4Vector3D & GetScaleFactor() const
void SetAutoRefresh(G4bool)
void SetBackgroundColour(const G4Colour &)
void SetCurrentTargetPoint(const G4Point3D &currentTargetPoint)
const G4Colour & GetBackgroundColour() const
const G4Vector3D & GetLightpointDirection() const
void SetFieldHalfAngle(G4double fieldHalfAngle)
const G4Vector3D & GetViewpointDirection() const
const G4Point3D & GetCurrentTargetPoint() const
G4double GetFieldHalfAngle() const
G4double GetZoomFactor() const
void SetDolly(G4double dolly)
const G4Vector3D & GetUpVector() const
void SetZoomFactor(G4double zoomFactor)
void SetUpVector(const G4Vector3D &upVector)
void SetLightpointDirection(const G4Vector3D &lightpointDirection)
void SetLightsMoveWithCamera(G4bool moves)
G4bool IsAutoRefresh() const
G4bool GetLightsMoveWithCamera() const
G4double GetDolly() const
G4double GetYmin() const
Definition: G4VisExtent.hh:101
G4double GetXmax() const
Definition: G4VisExtent.hh:100
const G4Point3D & GetExtentCenter() const
Definition: G4VisExtent.hh:106
G4double GetYmax() const
Definition: G4VisExtent.hh:102
G4double GetZmax() const
Definition: G4VisExtent.hh:104
G4double GetZmin() const
Definition: G4VisExtent.hh:103
G4double GetXmin() const
Definition: G4VisExtent.hh:99
void Draw(const G4Circle &, const G4Transform3D &objectTransformation=G4Transform3D())
static Verbosity GetVerbosity()
G4bool contains(const G4String &str, std::string_view ss)
Check if a string contains a given substring.