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10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  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 //
 29 // John Allison  27th March 1996
 30 // Abstract interface class for graphics views.
 31 
 32 #include "G4VViewer.hh"
 33 
 34 #include "G4PhysicalVolumeStore.hh"
 35 #include "G4Scene.hh"
 36 #include "G4Timer.hh"
 37 #include "G4Transform3D.hh"
 38 #include "G4UImanager.hh"
 39 #include "G4UIsession.hh"
 40 #include "G4VGraphicsSystem.hh"
 41 #include "G4VInteractiveSession.hh"
 42 #include "G4VPhysicalVolume.hh"
 43 #include "G4VSceneHandler.hh"
 44 #include "G4VisManager.hh"
 45 #include "G4ios.hh"
 46 
 47 #include <sstream>
 48 
 49 G4VViewer::G4VViewer(G4VSceneHandler& sceneHandler, G4int id, const G4String& name)
 50   : fSceneHandler(sceneHandler), fViewId(id), fNeedKernelVisit(true)
 51 {
 52   if (name == "") {
 53     std::ostringstream ost;
 54     ost << fSceneHandler.GetName() << '-' << fViewId;
 55     fName = ost.str();
 56   }
 57   else {
 58     fName = name;
 59   }
 60   fShortName = fName.substr(0, fName.find(' '));
 61   G4StrUtil::strip(fShortName);
 62 
 63   fVP = G4VisManager::GetInstance()->GetDefaultViewParameters();
 64   fDefaultVP = fVP;
 65 
 66   fSceneTree.SetType(G4SceneTreeItem::root);
 67   fSceneTree.SetDescription(fName);
 68 }
 69 
 70 G4VViewer::~G4VViewer()
 71 {
 72   fSceneHandler.RemoveViewerFromList(this);
 73 }
 74 
 75 void G4VViewer::SetName(const G4String& name)
 76 {
 77   fName = name;
 78   fShortName = fName.substr(0, fName.find(' '));
 79   G4StrUtil::strip(fShortName);
 80 }
 81 
 82 void G4VViewer::NeedKernelVisit()
 83 {
 84   fNeedKernelVisit = true;
 85 
 86   // At one time I thought we'd better notify all viewers.  But I guess
 87   // each viewer can take care of itself, so the following code is
 88   // redundant (but keep it commented out for now).   (John Allison)
 89   // Notify all viewers that a kernel visit is required.
 90   // const G4ViewerList& viewerList = fSceneHandler.GetViewerList ();
 91   // G4ViewerListConstIterator i;
 92   // for (i = viewerList.begin(); i != viewerList.end(); i++) {
 93   //   (*i) -> SetNeedKernelVisit ();
 94   // }
 95   // ??...but, there's a problem in OpenGL Stored which seems to
 96   // require *all* viewers to revisit the kernel, so...
 97   //  const G4ViewerList& viewerList = fSceneHandler.GetViewerList ();
 98   //  G4ViewerListConstIterator i;
 99   //  for (i = viewerList.begin(); i != viewerList.end(); i++) {
100   //    (*i) -> SetNeedKernelVisit (true);
101   //  }
102   // Feb 2005 - commented out.  Let's fix OpenGL if necessary.
103 }
104 
105 void G4VViewer::FinishView() {}
106 
107 void G4VViewer::ShowView() {}
108 
109 void G4VViewer::ProcessView()
110 {
111   // If the scene has changed, or if the concrete viewer has decided
112   // that it necessary to visit the kernel, perhaps because the view
113   // parameters have changed significantly (this should be done in the
114   // concrete viewer's DrawView)...
115   if (fNeedKernelVisit) {
116     // Reset flag.  This must be done before ProcessScene to prevent
117     // recursive calls when recomputing transients...
118     G4Timer timer;
119     timer.Start();
120     fNeedKernelVisit = false;
121     fSceneHandler.ClearStore();
122     fSceneHandler.ProcessScene();
123     UpdateGUISceneTree();
124     timer.Stop();
125     fKernelVisitElapsedTimeSeconds = timer.GetRealElapsed();
126   }
127 }
128 
129 void G4VViewer::SetViewParameters(const G4ViewParameters& vp)
130 {
131   fVP = vp;
132 }
133 
134 void G4VViewer::SetTouchable(
135   const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath)
136 {
137   // Set the touchable for /vis/touchable/set/... commands.
138   std::ostringstream oss;
139   const auto& pvStore = G4PhysicalVolumeStore::GetInstance();
140   for (const auto& pvNodeId : fullPath) {
141     const auto& pv = pvNodeId.GetPhysicalVolume();
142     auto iterator = find(pvStore->cbegin(), pvStore->cend(), pv);
143     if (iterator == pvStore->cend()) {
144       G4ExceptionDescription ed;
145       ed << "Volume no longer in physical volume store.";
146       G4Exception("G4VViewer::SetTouchable", "visman0401", JustWarning, ed);
147     }
148     else {
149       oss << ' ' << pvNodeId.GetPhysicalVolume()->GetName() << ' ' << pvNodeId.GetCopyNo();
150     }
151   }
152   G4UImanager::GetUIpointer()->ApplyCommand("/vis/set/touchable" + oss.str());
153 }
154 
155 void G4VViewer::TouchableSetVisibility(
156   const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath, G4bool visibiity)
157 {
158   // Changes the Vis Attribute Modifiers and scene tree WITHOUT triggering a rebuild.
159 
160   // The following is equivalent to
161   //  G4UImanager::GetUIpointer()->ApplyCommand("/vis/touchable/set/visibility ...");
162   // (assuming the touchable has already been set), but avoids view rebuild.
163 
164   // Instantiate a working copy of a G4VisAttributes object...
165   G4VisAttributes workingVisAtts;
166   // and set the visibility.
167   workingVisAtts.SetVisibility(visibiity);
168 
169   fVP.AddVisAttributesModifier(G4ModelingParameters::VisAttributesModifier(
170     workingVisAtts, G4ModelingParameters::VASVisibility,
171     G4PhysicalVolumeModel::GetPVNameCopyNoPath(fullPath)));
172   // G4ModelingParameters::VASVisibility (VAS = Vis Attribute Signifier)
173   // signifies that it is the visibility that should be picked out
174   // and merged with the touchable's normal vis attributes.
175 
176   // Find scene tree item and set visibility
177   // The scene tree works with strings
178   G4String fullPathString = G4PhysicalVolumeModel::GetPVNamePathString(fullPath);
179   std::list<G4SceneTreeItem>::iterator foundIter;
180   if (fSceneTree.FindTouchableFromRoot(fullPathString, foundIter)) {
181     foundIter->AccessVisAttributes().SetVisibility(visibiity);
182     UpdateGUISceneTree();
183   }
184   else {
185     G4ExceptionDescription ed;
186     ed << "Touchable \"" << fullPath << "\" not found";
187     G4Exception("G4VViewer::TouchableSetVisibility", "visman0402", JustWarning, ed);
188   }
189 }
190 
191 void G4VViewer::TouchableSetColour(
192   const std::vector<G4PhysicalVolumeModel::G4PhysicalVolumeNodeID>& fullPath,
193   const G4Colour& colour)
194 {
195   // Changes the Vis Attribute Modifiers and scene tree WITHOUT triggering a rebuild.
196 
197   // The following is equivalent to
198   //  G4UImanager::GetUIpointer()->ApplyCommand("/vis/touchable/set/colour ...");
199   // (assuming the touchable has already been set), but avoids view rebuild.
200 
201   // Instantiate a working copy of a G4VisAttributes object...
202   G4VisAttributes workingVisAtts;
203   // and set the colour.
204   workingVisAtts.SetColour(colour);
205 
206   fVP.AddVisAttributesModifier(G4ModelingParameters::VisAttributesModifier(
207     workingVisAtts, G4ModelingParameters::VASColour,
208     G4PhysicalVolumeModel::GetPVNameCopyNoPath(fullPath)));
209   // G4ModelingParameters::VASColour (VAS = Vis Attribute Signifier)
210   // signifies that it is the colour that should be picked out
211   // and merged with the touchable's normal vis attributes.
212 
213   // Find scene tree item and set colour
214   // The scene tree works with strings
215   G4String fullPathString = G4PhysicalVolumeModel::GetPVNamePathString(fullPath);
216   std::list<G4SceneTreeItem>::iterator foundIter;
217   if (fSceneTree.FindTouchableFromRoot(fullPathString, foundIter)) {
218     foundIter->AccessVisAttributes().SetColour(colour);
219     UpdateGUISceneTree();
220   }
221   else {
222     G4ExceptionDescription ed;
223     ed << "Touchable \"" << fullPath << "\" not found";
224     G4Exception("G4VViewer::TouchableSetColour", "visman0403", JustWarning, ed);
225   }
226 }
227 
228 void G4VViewer::UpdateGUISceneTree()
229 {
230   G4UImanager* UI = G4UImanager::GetUIpointer();
231   auto uiWindow = dynamic_cast<G4VInteractiveSession*>(UI->GetG4UIWindow());
232   if (uiWindow) uiWindow->UpdateSceneTree(fSceneTree);
233 }
234 
235 void G4VViewer::InsertModelInSceneTree(G4VModel* model)
236 {
237   const auto& modelType = model->GetType();
238   const auto& modelDescription = model->GetGlobalDescription();
239 
240   auto type = G4SceneTreeItem::model;
241   auto pvModel = dynamic_cast<G4PhysicalVolumeModel*>(model);
242   if (pvModel) type = G4SceneTreeItem::pvmodel;
243 
244   fCurtailDescent = false;  // This is used later in SceneTreeScene::ProcessVolume
245   G4String furtherInfo;
246   if (pvModel) {
247     struct : public G4PseudoScene {
248       void ProcessVolume(const G4VSolid&) {++fNTotalTouchables;}
249       G4int fNTotalTouchables = 0;
250     } counter;
251     pvModel->DescribeYourselfTo(counter);  // Calls ProcessVolume for every touchable
252     if (counter.fNTotalTouchables > fMaxNTouchables) {
253       std::ostringstream oss;
254       oss << counter.fNTotalTouchables << " touchables - too many for scene tree";
255       furtherInfo = oss.str();
256       if (G4VisManager::GetInstance()->GetVerbosity() >= G4VisManager::warnings) {
257         G4ExceptionDescription ed;
258         ed << pvModel->GetGlobalDescription() <<
259         ":\n  Too many touchables (" << counter.fNTotalTouchables
260         << ") for scene tree. Scene tree for this model will be empty.";
261         G4Exception("G4VViewer::InsertModelInSceneTree", "visman0404", JustWarning, ed);
262       }
263       fCurtailDescent = true;  // This is used later in SceneTreeScene::ProcessVolume
264     }
265   }
266 
267   // Find appropriate model
268   auto& modelItems = fSceneTree.AccessChildren();
269   auto modelIter = modelItems.begin();
270   auto pvModelIter = modelItems.end();
271   for (; modelIter != modelItems.end(); ++modelIter) {
272     if (modelIter->GetType() == G4SceneTreeItem::pvmodel) {
273       pvModelIter = modelIter;  // Last pre-existing PV model (if any)
274     }
275     if (modelIter->GetModelDescription() == modelDescription) break;
276   }
277 
278   if (modelIter == modelItems.end()) {  // Model not seen before
279     G4SceneTreeItem modelItem(type);
280     modelItem.SetDescription("model");
281     modelItem.SetModelType(modelType);
282     modelItem.SetModelDescription(modelDescription);
283     modelItem.SetFurtherInfo(furtherInfo);
284     if (pvModelIter != modelItems.end() &&  // There was pre-existing PV Model...
285         type == G4SceneTreeItem::pvmodel) {   // ...and the new model is also PV...
286       fSceneTree.InsertChild(++pvModelIter, modelItem);  // ...insert after, else...
287     } else {
288       fSceneTree.InsertChild(modelIter, modelItem);  // ...insert at end
289     }
290   } else {  // Existing model - mark visible == active
291     modelIter->AccessVisAttributes().SetVisibility(true);
292   }
293 }
294 
295 G4VViewer::SceneTreeScene::SceneTreeScene(G4VViewer* pViewer, G4PhysicalVolumeModel* pPVModel)
296 : fpViewer (pViewer)
297 , fpPVModel(pPVModel)
298 {
299   if (fpPVModel == nullptr) {
300     G4Exception("G4VViewer::SceneTreeScene::SceneTreeScene", "visman0405", FatalException,
301                 "G4PhysicalVolumeModel pointer is null");
302     return;  // To keep Coverity happy
303   }
304   
305   // Describe the model to an empty scene simply to get the numbers of touchables
306   struct : public G4PseudoScene {
307     void ProcessVolume(const G4VSolid&) {}
308   } counter;
309   fpPVModel->DescribeYourselfTo(counter);  // Calls ProcessVolume for every touchable
310 
311   // Limit the expanded depth to limit the number expanded so as not to swamp the GUI
312   G4int expanded = 0;
313   for (const auto& dn : fpPVModel->GetNumberOfTouchables()) {
314     expanded += dn.second;
315     if (fMaximumExpandedDepth < dn.first) fMaximumExpandedDepth = dn.first;
316     if (expanded > fMaximumExpanded) break;
317   }
318 
319   // Find appropriate model and its iterator
320   const auto& modelID = fpPVModel->GetGlobalDescription();
321   auto& modelItems = fpViewer->fSceneTree.AccessChildren();
322   fModelIter = modelItems.begin();
323   for (; fModelIter != modelItems.end(); ++fModelIter) {
324     if (fModelIter->GetModelDescription() == modelID) break;
325   }
326   if (fModelIter == modelItems.end()) {
327     G4Exception("G4VViewer::SceneTreeScene::SceneTreeScene", "visman0406", JustWarning,
328                 "Model not found");
329   }
330 }
331 
332 void G4VViewer::SceneTreeScene::ProcessVolume(const G4VSolid&)
333 {
334   if (fpViewer->fCurtailDescent) {
335     fpPVModel->CurtailDescent();
336     return;
337   }
338 
339   const auto& modelID = fpPVModel->GetGlobalDescription();
340 
341   std::ostringstream oss;
342   oss << fpPVModel->GetFullPVPath();  // of this volume
343   G4String fullPathString(oss.str());  // Has a leading space
344 
345   // Navigate scene tree and find or insert touchables one by one
346   // Work down the path - "name id", then "name id name id", etc.
347   const auto& nodeIDs = fpPVModel->GetFullPVPath();
348   G4String partialPathString;
349   auto currentIter = fModelIter;
350   G4int depth = 0;
351   for (const auto& nodeID : nodeIDs) {
352     std::ostringstream oss1;
353     oss1 << nodeID;
354     partialPathString += ' ' + oss1.str();
355     currentIter =
356     FindOrInsertTouchable(modelID, *currentIter, ++depth, partialPathString, fullPathString);
357   }
358 }
359 
360 // clang-format off
361 std::list<G4SceneTreeItem>::iterator G4VViewer::SceneTreeScene::FindOrInsertTouchable
362  (const G4String& modelID, G4SceneTreeItem& mother,
363   G4int depth, const G4String& partialPathString, const G4String& fullPathString)
364 {
365   auto& children = mother.AccessChildren();
366   auto childIter = children.begin();
367   for (; childIter != children.end(); ++childIter) {
368     if (childIter->GetPVPath() == partialPathString) break;
369   }
370   if (childIter != children.end()) {
371 
372     // Item already exists
373 
374     if (childIter->GetType() == G4SceneTreeItem::ghost) {
375 
376       // Previously it was a ghost - but maybe this time it's real
377 
378       if (partialPathString == fullPathString) {
379         // Partial path string refers to the actual volume so it's a touchable
380         childIter->SetType(G4SceneTreeItem::touchable);
381         // Populate with information
382         childIter->SetDescription(fpPVModel->GetCurrentTag());
383         childIter->SetModelType(fpPVModel->GetType());
384         childIter->SetModelDescription(modelID);
385         childIter->SetPVPath(partialPathString);
386         if (fpVisAttributes) childIter->SetVisAttributes(*fpVisAttributes);
387         childIter->SetAttDefs(fpPVModel->GetAttDefs());
388         childIter->SetAttValues(fpPVModel->CreateCurrentAttValues());
389       }  // Partial path string refers to an ancester - do nothing
390 
391     } else {
392 
393       // Already a pre-existing full touchable
394 
395       if (partialPathString == fullPathString) {
396         // Partial path string refers to the actual volume
397         // Replace vis attributes (if any) - they might have changed
398         if (fpVisAttributes) childIter->SetVisAttributes(*fpVisAttributes);
399       }  // Partial path string refers to an ancester - do nothing
400 
401     }
402 
403   } else {
404 
405     // Item does not yet exist
406 
407     if (partialPathString == fullPathString) {
408 
409       // Partial path string refers to the actual volume
410       // Insert new touchable item
411       G4SceneTreeItem touchable(G4SceneTreeItem::touchable);
412       touchable.SetExpanded(depth > fMaximumExpandedDepth? false: true);
413       touchable.SetDescription(fpPVModel->GetCurrentTag());
414       touchable.SetModelType(fpPVModel->GetType());
415       touchable.SetModelDescription(modelID);
416       touchable.SetPVPath(partialPathString);
417       if (fpVisAttributes) touchable.SetVisAttributes(*fpVisAttributes);
418       touchable.SetAttDefs(fpPVModel->GetAttDefs());
419       touchable.SetAttValues(fpPVModel->CreateCurrentAttValues());
420       childIter = mother.InsertChild(childIter,touchable);
421 
422     } else {
423 
424       // Partial path string refers to an ancester - it's what we call a "ghost"
425       G4SceneTreeItem ghost(G4SceneTreeItem::ghost);
426       ghost.SetExpanded(depth > fMaximumExpandedDepth? false: true);
427       // Create a tag from the partial path
428       std::istringstream iss(partialPathString);
429       G4String name, copyNo;
430       while (iss >> name >> copyNo);
431       std::ostringstream oss;
432       oss << name << ':' << copyNo;
433       ghost.SetDescription(oss.str());
434       ghost.SetModelType(fpPVModel->GetType());
435       ghost.SetModelDescription(modelID);
436       ghost.SetPVPath(partialPathString);
437       ghost.AccessVisAttributes().SetVisibility(false);
438       childIter = mother.InsertChild(childIter,ghost);
439     }
440   }
441 
442   return childIter;
443 }
444 // clang-format on
445 
446 std::ostream& operator<<(std::ostream& os, const G4VViewer& v)
447 {
448   os << "View " << v.fName << ":\n";
449   os << v.fVP;
450   return os;
451 }
452