Geant4 Cross Reference |
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 #include "G4SceneTreeItem.hh" 29 30 #include "G4AttCheck.hh" 31 32 #include <iostream> 33 34 // A ghost is a touchable we know to be there but know only its path 35 std::map<G4SceneTreeItem::Type, G4String> G4SceneTreeItem::fTypeMap = { 36 {G4SceneTreeItem::unidentified, "unidentified"}, 37 {G4SceneTreeItem::root, "root"}, 38 {G4SceneTreeItem::ghost, "ghost"}, 39 {G4SceneTreeItem::model, "model"}, 40 {G4SceneTreeItem::pvmodel, "pvmodel"}, 41 {G4SceneTreeItem::touchable, "touchable"}}; 42 43 // Reset visibility of all objects to false - visible objects will then set to true 44 void G4SceneTreeItem::ResetVisibility() 45 { 46 // Reset all but the root item, which is always visible (i.e., active) 47 // Other items will be set visible if and when presented to the scene 48 if (fType != root) fVisAttributes.SetVisibility(false); 49 for (auto& child : fChildren) 50 child.ResetVisibility(); 51 } 52 53 // If found, returns "true" and places iterator in foundIter 54 G4bool G4SceneTreeItem::FindTouchableFromRoot(const G4String& fullPathString, 55 std::list<G4SceneTreeItem>::iterator& foundIter) 56 { 57 if (fType != root) { 58 G4ExceptionDescription ed; 59 ed << "Not a root item:\n"; 60 DumpSingleItem(ed); 61 G4Exception("G4SceneTreeItem::FindTouchableFromRoot", "greps0011", JustWarning, ed); 62 return false; 63 } 64 65 for (auto& aModel : fChildren) { 66 if (aModel.fModelType == "G4PhysicalVolumeModel") { // Top item, i.e., root of touchables 67 // Work down the path - "name id", then "name id name id", etc. 68 G4String partialPathString; 69 auto iter = aModel.fChildren.begin(); 70 auto iterEnd = aModel.fChildren.end(); 71 std::istringstream iss(fullPathString); 72 G4String name, copyNo; 73 while (iss >> name >> copyNo) { 74 partialPathString += ' ' + name + ' ' + copyNo; 75 for (; iter != iterEnd; ++iter) { 76 if (iter->fPVPath == partialPathString) { 77 if (partialPathString != fullPathString) { 78 // Go to next level 79 iter = iter->fChildren.begin(); 80 iterEnd = iter->fChildren.end(); 81 } 82 break; 83 } 84 } 85 if (iter != iterEnd) { // Found 86 foundIter = iter; 87 return true; 88 } 89 } 90 } 91 } 92 return false; 93 } 94 95 // Dump single item, i.e., ignore any children 96 void G4SceneTreeItem::DumpSingleItem(std::ostream& os, G4int verbosity) const 97 { 98 static G4bool first = true; 99 if (first) { 100 first = false; 101 os << " Verbosity actions:" 102 << "\n >=0 one line" 103 << "\n >=1 a few lines" 104 << "\n >=2 check G4Atts" 105 << "\n >=3 print G4Atts" 106 << "\n >=4 print some attValues" 107 << '\n'; 108 } 109 110 os << GetTypeString() << " ("; 111 G4String status; 112 switch (fType) { 113 case unidentified: 114 status = "error"; 115 break; 116 case root: 117 status = "active"; 118 break; 119 case model: 120 [[fallthrough]]; 121 case pvmodel: 122 status = (fVisAttributes.IsVisible() ? "active" : "inactive"); 123 break; 124 case ghost: 125 [[fallthrough]]; 126 case touchable: 127 status = (fVisAttributes.IsVisible() ? "visible" : "invisible"); 128 break; 129 } 130 if (fExpanded) { 131 status += ",expanded"; 132 } else { 133 status += ",collapsed"; 134 } 135 os << status << ')'; 136 137 G4String description; 138 switch (fType) { 139 case unidentified: 140 break; 141 case root: 142 break; 143 case model: 144 [[fallthrough]]; 145 case pvmodel: 146 description = " \"" + fModelDescription + '"'; 147 break; 148 case ghost: 149 [[fallthrough]]; 150 case touchable: 151 description = " (" + fPVPath.substr(1, fPVPath.length() - 1) + ')'; 152 break; 153 } 154 os << description; 155 156 if (fType == touchable) { 157 os << ' ' << fVisAttributes.GetColour(); 158 } 159 160 // clang-format off 161 if (verbosity >= 1) { 162 os << "\n Description: " << GetDescription() 163 << "\n Model type: " << GetModelType() 164 << "\n Model description: " << GetModelDescription() 165 << "\n " << fChildren.size() << (fChildren.size()==1?" child":" children"); 166 } 167 // clang-format on 168 169 if (verbosity >= 2) { 170 const auto& attDefs = GetAttDefs(); 171 const auto& attValues = GetAttValues(); 172 if (attDefs == nullptr || attValues == nullptr) { 173 os << "\n No G4Atts"; // Legitimate 174 } 175 else { 176 G4AttCheck attCheck(attValues, attDefs); 177 if (attCheck.Check("G4SceneTreeItem::Dump")) { 178 G4ExceptionDescription ed; 179 ed << "Item: " << attCheck; 180 G4Exception("G4SceneTreeItem::Dump", "greps0010", JustWarning, ed, 181 "G4Atts don't check out"); 182 return; 183 } 184 185 if (verbosity >= 3) { 186 os << "\n G4Atts:\n" << attCheck; 187 static G4bool first1 = true; 188 if (first1) { 189 first1 = false; 190 os << "\n Available G4Atts for touchable:"; 191 // clang-format off 192 for (const auto& att: *GetAttDefs()) { 193 os << "\n " << att.first 194 << ',' << att.second.GetName() 195 << ',' << att.second.GetDesc() 196 << ',' << att.second.GetCategory() 197 << ',' << att.second.GetExtra() 198 << ',' << att.second.GetValueType() 199 << ',' << att.second.GetTypeKey(); 200 } 201 // clang-format on 202 os << '\n'; 203 } 204 } 205 206 if (verbosity >= 4) { 207 for (G4String name : {"PVPath", "GlobalExtent"}) { 208 G4String result; 209 const auto& iterAttDef = attDefs->find(name); 210 if (iterAttDef != attDefs->end()) { 211 result = result + "\n " + iterAttDef->second.GetName(); 212 result = result + ", " + iterAttDef->second.GetDesc(); 213 } 214 const G4AttValue* pAttValue = nullptr; 215 for (const auto& attValue : *attValues) { 216 // Why are the attValues not in a map like the attDefs??? 217 if (attValue.GetName() == name) { 218 pAttValue = &attValue; // Avoid copy 219 break; 220 } 221 } 222 if (pAttValue) { 223 result = result + ", " + pAttValue->GetValue(); 224 } 225 226 os << result; 227 } 228 } 229 } 230 } 231 232 os << std::endl; 233 } 234 235 // Dump whole tree 236 void G4SceneTreeItem::DumpTree(std::ostream& os, G4int verbosity) const 237 { 238 static G4int depth = 0; 239 for (G4int i = 0; i < depth; i++) os << " "; 240 DumpSingleItem(os, verbosity); 241 for (auto& child : GetChildren()) { 242 depth++; 243 child.DumpTree(os, verbosity); 244 depth--; 245 } 246 } 247