ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4VVisCommand.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4VVisCommand.cc
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 
40 
47 // Not yet used: G4VisAttributes::LineStyle G4VVisCommand::fCurrentLineStyle = G4VisAttributes::unbroken;
48 // Not yet used: G4VMarker::FillStyle G4VVisCommand::fCurrentFillStyle = G4VMarker::filled;
49 // Not yet used: G4VMarker::SizeType G4VVisCommand::fCurrentSizeType = G4VMarker::screen;
52 std::vector<G4PhysicalVolumesSearchScene::Findings> G4VVisCommand::fCurrrentPVFindingsForField;
53 
55 
57 
59 
61 (G4double x, G4double y, const char * unitName)
62 {
63  G4double uv = G4UIcommand::ValueOf(unitName);
64 
65  std::ostringstream oss;
66  oss << x/uv << " " << y/uv << " " << unitName;
67  return oss.str();
68 }
69 
71  G4double& xval,
72  G4double& yval)
73 {
74  G4double x, y;
75  G4String unit;
76 
77  std::istringstream is(paramString);
78  is >> x >> y >> unit;
79 
81  xval = x*G4UIcommand::ValueOf(unit);
82  yval = y*G4UIcommand::ValueOf(unit);
83  } else {
85  if (verbosity >= G4VisManager::errors) {
86  G4cout << "ERROR: Unrecognised unit" << G4endl;
87  }
88  return false;
89  }
90 
91  return true;
92 }
93 
95 {
96  static G4String guidance
97  ("Accepts (a) RGB triplet. e.g., \".3 .4 .5\", or"
98  "\n (b) string such as \"white\", \"black\", \"grey\", \"red\"...or"
99  "\n (c) an additional number for opacity, e.g., \".3 .4 .5 .6\""
100  "\n or \"grey ! ! .6\" (note \"!\"'s for unused parameters).");
101  return guidance;
102 }
103 
105 (G4Colour& colour,
106  const G4String& redOrString, G4double green, G4double blue, G4double opacity)
107 {
108  // Note: colour is supplied by the caller and some or all of its components
109  // may act as default.
110  //
111  // Note: redOrString is either a number or string. If a string it must be
112  // one of the recognised colours.
113  //
114  // Thus the arguments can be, for example:
115  // (colour,"red",...,...,0.5): will give the colour red with opacity 0.5 (the
116  // third and fourth arguments are ignored), or
117  // (1.,0.,0.,0.5): this also will be red with opacity 0.5.
118 
119  G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
120 
121  const size_t iPos0 = 0;
122  if (std::isalpha(redOrString[iPos0])) {
123 
124  // redOrString is probably alphabetic characters defining the colour
125  if (!G4Colour::GetColour(redOrString, colour)) {
126  // Not a recognised string
127  if (verbosity >= G4VisManager::warnings) {
128  G4cout << "WARNING: Colour \"" << redOrString
129  << "\" not found. Defaulting to " << colour
130  << G4endl;
131  }
132  return;
133  } else {
134  // It was a recognised string. Now add opacity.
135  colour.SetAlpha(opacity);
136  return;
137  }
138 
139  } else {
140 
141  // redOrString is probably numeric defining the red component
142  std::istringstream iss(redOrString);
143  G4double red;
144  iss >> red;
145  if (iss.fail()) {
146  if (verbosity >= G4VisManager::warnings) {
147  G4cout << "WARNING: String \"" << redOrString
148  << "\" cannot be parsed. Defaulting to " << colour
149  << G4endl;
150  }
151  return;
152  } else {
153  colour = G4Colour(red,green,blue,opacity);
154  return;
155  }
156 
157  }
158 }
159 
161 (const G4String& where,
162  const G4String& unit,
163  const G4String& category,
164  G4double& value)
165 {
166  // Return false if there's a problem
167 
168  G4VisManager::Verbosity verbosity = fpVisManager->GetVerbosity();
169 
170  G4bool success = true;
171  if (!G4UnitDefinition::IsUnitDefined(unit)) {
172  if (verbosity >= G4VisManager::warnings) {
173  G4cerr << where
174  << "\n Unit \"" << unit << "\" not defined"
175  << G4endl;
176  }
177  success = false;
178  } else if (G4UnitDefinition::GetCategory(unit) != category) {
179  if (verbosity >= G4VisManager::warnings) {
180  G4cerr << where
181  << "\n Unit \"" << unit << "\" not a unit of " << category;
182  if (category == "Volumic Mass") G4cerr << " (density)";
183  G4cerr << G4endl;
184  }
185  success = false;
186  } else {
187  value = G4UnitDefinition::GetValueOf(unit);
188  }
189  return success;
190 }
191 
193 {
195 
196  if (!pScene) {
197  if (verbosity >= G4VisManager::warnings) {
198  G4cout << "WARNING: Scene pointer is null."
199  << G4endl;
200  }
201  return;
202  }
203 
204  G4VSceneHandler* pSceneHandler = fpVisManager -> GetCurrentSceneHandler();
205  if (!pSceneHandler) {
206  if (verbosity >= G4VisManager::warnings) {
207  G4cout << "WARNING: Scene handler not found." << G4endl;
208  }
209  return;
210  }
211 
212  // Scene has changed. If it is the scene of the currrent scene handler
213  // refresh viewers of all scene handlers using this scene. If not, it may be
214  // a scene that the user is building up before attaching to a scene handler,
215  // so do nothing.
216  if (pScene == pSceneHandler->GetScene()) {
217  G4UImanager::GetUIpointer () -> ApplyCommand ("/vis/scene/notifyHandlers");
218  }
219 
220 }
221 
224  // Some frequently used error printing...
225  if (verbosity >= G4VisManager::warnings) {
226  G4cout <<
227  "WARNING: For some reason, possibly mentioned above, it has not been"
228  "\n possible to add to the scene."
229  << G4endl;
230  }
231 }
232 
234 (G4VViewer* viewer, const G4ViewParameters& viewParams) {
235  viewer->SetViewParameters(viewParams);
236  RefreshIfRequired(viewer);
237 }
238 
241  G4VSceneHandler* sceneHandler = viewer->GetSceneHandler();
242  const G4ViewParameters& viewParams = viewer->GetViewParameters();
243  if (sceneHandler && sceneHandler->GetScene()) {
244  if (viewParams.IsAutoRefresh()) {
245  G4UImanager::GetUIpointer()->ApplyCommand("/vis/viewer/refresh");
246  }
247  else {
248  if (verbosity >= G4VisManager::warnings) {
249  G4cout << "Issue /vis/viewer/refresh or flush to see effect."
250  << G4endl;
251  }
252  }
253  }
254 }
255 
257 (G4VViewer* currentViewer,
258  std::vector<G4ViewParameters> viewVector,
259  const G4int nInterpolationPoints,
260  const G4int waitTimePerPointmilliseconds,
261  const G4String exportString)
262 {
263  const G4int safety = viewVector.size()*nInterpolationPoints;
264  G4int safetyCount = 0;
265  do {
266  G4ViewParameters* vp =
267  G4ViewParameters::CatmullRomCubicSplineInterpolation(viewVector,nInterpolationPoints);
268  if (!vp) break; // Finished.
269  currentViewer->SetViewParameters(*vp);
270  currentViewer->RefreshView();
271  if (exportString == "export" &&
272  currentViewer->GetName().contains("OpenGL")) {
273  G4UImanager::GetUIpointer()->ApplyCommand("/vis/ogl/export");
274  }
275  // File-writing viewers need to close the file
276  currentViewer->ShowView();
277 #ifdef G4VIS_USE_STD11
278  if (waitTimePerPointmilliseconds > 0)
279  std::this_thread::sleep_for(std::chrono::milliseconds(waitTimePerPointmilliseconds));
280 #endif
281  } while (safetyCount++ < safety); // Loop checking, 16.02.2016, J.Allison
282 }
283 
285 (G4VViewer* currentViewer,
286  const G4ViewParameters& oldVP,
287  const G4ViewParameters& newVP,
288  const G4int nInterpolationPoints,
289  const G4int waitTimePerPointmilliseconds,
290  const G4String exportString)
291 {
292  std::vector<G4ViewParameters> viewVector;
293  viewVector.push_back(oldVP);
294  viewVector.push_back(oldVP);
295  viewVector.push_back(newVP);
296  viewVector.push_back(newVP);
297 
298  InterpolateViews
299  (currentViewer,
300  viewVector,
301  nInterpolationPoints,
302  waitTimePerPointmilliseconds,
303  exportString);
304 }
305 
307 (const G4UIcommand* fromCmd, G4UIcommand* toCmd, G4int startLine)
308 {
309  if (fromCmd && toCmd) {
310  const G4int nGuideEntries = fromCmd->GetGuidanceEntries();
311  for (G4int i = startLine; i < nGuideEntries; ++i) {
312  const G4String& guidance = fromCmd->GetGuidanceLine(i);
313  toCmd->SetGuidance(guidance);
314  }
315  }
316 }
317 
319 (const G4UIcommand* fromCmd, G4UIcommand* toCmd)
320 {
321  if (fromCmd && toCmd) {
322  const G4int nParEntries = fromCmd->GetParameterEntries();
323  for (G4int i = 0; i < nParEntries; ++i) {
324  G4UIparameter* parameter = new G4UIparameter(*(fromCmd->GetParameter(i)));
325  toCmd->SetParameter(parameter);
326  }
327  }
328 }
329 
331 {
332  if (fpVisManager) {
333  const G4double halfX = (extent.GetXmax() - extent.GetXmin()) / 2.;
334  const G4double halfY = (extent.GetYmax() - extent.GetYmin()) / 2.;
335  const G4double halfZ = (extent.GetZmax() - extent.GetZmin()) / 2.;
336  if (halfX > 0. && halfY > 0. && halfZ > 0.) {
337  const G4Box box("vis_extent",halfX,halfY,halfZ);
338  const G4VisAttributes visAtts(G4Color::Red());
339  const G4Point3D& centre = extent.GetExtentCenter();
340  fpVisManager->Draw(box,visAtts,G4Translate3D(centre));
341  }
342  }
343 }