Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 26 #include "G4VtkMessenger.hh" 27 28 #include "G4Tokenizer.hh" 29 #include "G4UIcmdWithABool.hh" 30 #include "G4UIcmdWithAString.hh" 31 #include "G4UIcmdWithoutParameter.hh" 32 #include "G4UIcommand.hh" 33 #include "G4UIdirectory.hh" 34 #include "G4VisManager.hh" 35 #include "G4VtkSceneHandler.hh" 36 #include "G4VtkViewer.hh" 37 38 #include <vtkObject.h> 39 40 G4VtkMessenger* G4VtkMessenger::fpInstance = n 41 42 G4VtkMessenger* G4VtkMessenger::GetInstance() 43 { 44 if (fpInstance == nullptr) fpInstance = new 45 return fpInstance; 46 } 47 48 G4VtkMessenger::G4VtkMessenger() 49 { 50 G4bool omitable; 51 52 /***************************** Vtk directory 53 fpDirectory = new G4UIdirectory("/vis/vtk/", 54 fpDirectory->SetGuidance("G4VtkViewer comman 55 56 // Clear non-G4 57 fpCommandClearNonG4 = new G4UIcommand("/vis/ 58 fpCommandClearNonG4->SetGuidance("Clear non 59 60 // Export command 61 fpCommandExport = new G4UIcommand("/vis/vtk/ 62 fpCommandExport->SetGuidance("Export a scree 63 64 // File type for export 65 auto parameterExport = new G4UIparameter("fo 66 fpCommandExport->SetGuidance("File type (jpg 67 fpCommandExport->SetParameter(parameterExpor 68 69 // File name for export 70 parameterExport = new G4UIparameter("file-na 71 fpCommandExport->SetGuidance("File name"); 72 fpCommandExport->SetParameter(parameterExpor 73 74 // Export cutter command 75 fpCommandExportCutter = new G4UIcommand("/vi 76 fpCommandExportCutter->SetGuidance("Export a 77 78 // File name for export 79 auto parameterExportCutter = new G4UIparamet 80 parameterExportCutter->SetGuidance("File nam 81 parameterExportCutter->SetDefaultValue("cutt 82 fpCommandExportCutter->SetParameter(paramete 83 84 // Vtk debug print 85 fpCommandDebugPrint = new G4UIcommand("/vis/ 86 fpCommandDebugPrint->SetGuidance("Debug prin 87 88 // start interactor in native mode 89 fpCommandInteractorStart = new G4UIcommand(" 90 fpCommandInteractorStart->SetGuidance("Start 91 92 /***************************** Set directory 93 fpDirectorySet = new G4UIdirectory("/vis/vtk 94 fpDirectorySet->SetGuidance("G4VtkViewer set 95 96 // Vtk warnings output 97 fpCommandWarnings = new G4UIcmdWithABool("/v 98 fpCommandWarnings->SetParameterName("enable- 99 fpCommandWarnings->SetGuidance("Enable (True 100 101 // HUD command 102 fpCommandHUD = new G4UIcmdWithABool("/vis/vt 103 fpCommandHUD->SetGuidance("Enable or disable 104 105 // Camera orientation widget 106 fpCameraOrientation = new G4UIcmdWithABool(" 107 fpCameraOrientation->SetGuidance("Enable or 108 109 // Clipper command 110 fpCommandClipper = new G4UIcommand("/vis/vtk 111 fpCommandClipper->SetGuidance("Enable a cuta 112 auto fpCommandClipperParam = new G4UIparamet 113 fpCommandClipperParam->SetDefaultValue(1); 114 fpCommandClipper->SetParameter(fpCommandClip 115 116 // Clutter command 117 fpCommandCutter = new G4UIcommand("/vis/vtk/ 118 fpCommandCutter->SetGuidance("Enable a secti 119 auto fpCommandCutterParam = new G4UIparamete 120 fpCommandCutterParam->SetDefaultValue(1); 121 fpCommandCutter->SetParameter(fpCommandCutte 122 123 // Vtk polyhedron pipeline selection 124 auto fpCommandPolyhedronPipelineParam = new 125 fpCommandPolyhedronPipeline = new G4UIcomman 126 fpCommandPolyhedronPipeline->SetGuidance("Se 127 fpCommandPolyhedronPipeline->SetGuidance("Ty 128 fpCommandPolyhedronPipeline->SetParameter(fp 129 130 // Shadows command 131 fpCommandShadow = new G4UIcommand("/vis/vtk/ 132 fpCommandShadow->SetGuidance("Enable/disable 133 auto fpCommandShadowParam = new G4UIparamete 134 fpCommandShadowParam->SetDefaultValue(1); 135 fpCommandShadow->SetParameter(fpCommandShado 136 137 /***************************** Add directory 138 fpDirectoryAdd = new G4UIdirectory("/vis/vtk 139 fpDirectoryAdd->SetGuidance("G4VtkViewer add 140 141 fpCommandImageOverlay = new G4UIcommand("/vi 142 auto parameterImageOverlay = new G4UIparamet 143 parameterImageOverlay->SetGuidance("Image fi 144 fpCommandImageOverlay->SetParameter(paramete 145 146 parameterImageOverlay = new G4UIparameter("i 147 parameterImageOverlay->SetGuidance("image bo 148 fpCommandImageOverlay->SetParameter(paramete 149 150 parameterImageOverlay = new G4UIparameter("i 151 parameterImageOverlay->SetGuidance("image bo 152 fpCommandImageOverlay->SetParameter(paramete 153 154 parameterImageOverlay = new G4UIparameter("i 155 parameterImageOverlay->SetGuidance("image to 156 fpCommandImageOverlay->SetParameter(paramete 157 158 parameterImageOverlay = new G4UIparameter("i 159 parameterImageOverlay->SetGuidance("image to 160 fpCommandImageOverlay->SetParameter(paramete 161 162 parameterImageOverlay = new G4UIparameter("w 163 parameterImageOverlay->SetGuidance("world bo 164 fpCommandImageOverlay->SetParameter(paramete 165 166 parameterImageOverlay = new G4UIparameter("w 167 parameterImageOverlay->SetGuidance("world bo 168 fpCommandImageOverlay->SetParameter(paramete 169 170 parameterImageOverlay = new G4UIparameter("w 171 parameterImageOverlay->SetGuidance("world to 172 fpCommandImageOverlay->SetParameter(paramete 173 174 parameterImageOverlay = new G4UIparameter("w 175 parameterImageOverlay->SetGuidance("world to 176 fpCommandImageOverlay->SetParameter(paramete 177 178 parameterImageOverlay = new G4UIparameter("x 179 parameterImageOverlay->SetGuidance("rotation 180 parameterImageOverlay->SetDefaultValue(0.0); 181 fpCommandImageOverlay->SetParameter(paramete 182 183 parameterImageOverlay = new G4UIparameter("y 184 parameterImageOverlay->SetGuidance("rotation 185 parameterImageOverlay->SetDefaultValue(0.0); 186 fpCommandImageOverlay->SetParameter(paramete 187 188 parameterImageOverlay = new G4UIparameter("z 189 parameterImageOverlay->SetGuidance("rotation 190 parameterImageOverlay->SetDefaultValue(0.0); 191 fpCommandImageOverlay->SetParameter(paramete 192 193 parameterImageOverlay = new G4UIparameter("x 194 parameterImageOverlay->SetGuidance("translat 195 parameterImageOverlay->SetDefaultValue(0.0); 196 fpCommandImageOverlay->SetParameter(paramete 197 198 parameterImageOverlay = new G4UIparameter("y 199 parameterImageOverlay->SetGuidance("translat 200 parameterImageOverlay->SetDefaultValue(0.0); 201 fpCommandImageOverlay->SetParameter(paramete 202 203 parameterImageOverlay = new G4UIparameter("z 204 parameterImageOverlay->SetGuidance("translat 205 parameterImageOverlay->SetDefaultValue(0.0); 206 fpCommandImageOverlay->SetParameter(paramete 207 208 parameterImageOverlay = new G4UIparameter("a 209 parameterImageOverlay->SetGuidance("alpha"); 210 parameterImageOverlay->SetDefaultValue(0.5); 211 fpCommandImageOverlay->SetParameter(paramete 212 213 fpCommandGeometryOverlay = new G4UIcommand(" 214 auto parameterGeometryOverlay = new G4UIpara 215 parameterGeometryOverlay->SetGuidance("Geome 216 fpCommandGeometryOverlay->SetParameter(param 217 218 parameterGeometryOverlay = new G4UIparameter 219 parameterGeometryOverlay->SetGuidance("scale 220 parameterGeometryOverlay->SetDefaultValue(0. 221 fpCommandGeometryOverlay->SetParameter(param 222 223 parameterGeometryOverlay = new G4UIparameter 224 parameterGeometryOverlay->SetGuidance("scale 225 parameterGeometryOverlay->SetDefaultValue(0. 226 fpCommandGeometryOverlay->SetParameter(param 227 228 parameterGeometryOverlay = new G4UIparameter 229 parameterGeometryOverlay->SetGuidance("scale 230 parameterGeometryOverlay->SetDefaultValue(0. 231 fpCommandGeometryOverlay->SetParameter(param 232 233 parameterGeometryOverlay = new G4UIparameter 234 parameterGeometryOverlay->SetGuidance("rotat 235 parameterGeometryOverlay->SetDefaultValue(0. 236 fpCommandGeometryOverlay->SetParameter(param 237 238 parameterGeometryOverlay = new G4UIparameter 239 parameterGeometryOverlay->SetGuidance("rotat 240 parameterGeometryOverlay->SetDefaultValue(0. 241 fpCommandGeometryOverlay->SetParameter(param 242 243 parameterGeometryOverlay = new G4UIparameter 244 parameterGeometryOverlay->SetGuidance("rotat 245 parameterGeometryOverlay->SetDefaultValue(0. 246 fpCommandGeometryOverlay->SetParameter(param 247 248 parameterGeometryOverlay = new G4UIparameter 249 parameterGeometryOverlay->SetGuidance("trans 250 parameterGeometryOverlay->SetDefaultValue(0. 251 fpCommandGeometryOverlay->SetParameter(param 252 253 parameterGeometryOverlay = new G4UIparameter 254 parameterGeometryOverlay->SetGuidance("trans 255 parameterGeometryOverlay->SetDefaultValue(0. 256 fpCommandGeometryOverlay->SetParameter(param 257 258 parameterGeometryOverlay = new G4UIparameter 259 parameterGeometryOverlay->SetGuidance("trans 260 parameterGeometryOverlay->SetDefaultValue(0. 261 fpCommandGeometryOverlay->SetParameter(param 262 263 parameterGeometryOverlay = new G4UIparameter 264 parameterGeometryOverlay->SetGuidance("alpha 265 parameterGeometryOverlay->SetDefaultValue(0. 266 fpCommandGeometryOverlay->SetParameter(param 267 268 parameterGeometryOverlay = new G4UIparameter 269 parameterGeometryOverlay->SetGuidance("repre 270 parameterGeometryOverlay->SetDefaultValue("s 271 fpCommandGeometryOverlay->SetParameter(param 272 } 273 274 G4VtkMessenger::~G4VtkMessenger() 275 { 276 delete fpDirectory; 277 delete fpDirectorySet; 278 delete fpDirectoryAdd; 279 delete fpCommandExport; 280 delete fpCommandExportCutter; 281 delete fpCommandWarnings; 282 delete fpCommandDebugPrint; 283 delete fpCommandPolyhedronPipeline; 284 delete fpCommandImageOverlay; 285 delete fpCommandGeometryOverlay; 286 } 287 288 G4String G4VtkMessenger::GetCurrentValue(G4UIc 289 { 290 return G4String(); 291 } 292 293 void G4VtkMessenger::SetNewValue(G4UIcommand* 294 { 295 G4VisManager* pVisManager = G4VisManager::Ge 296 297 G4VViewer* pViewer = pVisManager->GetCurrent 298 if (pViewer == nullptr) { 299 G4cout << "G4VtkMessenger::SetNewValue: No 300 << "\"/vis/open\", or similar, to g 301 return; 302 } 303 304 auto* pVtkViewer = dynamic_cast<G4VtkViewer* 305 if (pVtkViewer == nullptr) { 306 G4cout << "G4VtkMessenger::SetNewValue: Cu 307 << "(It is \"" << pViewer->GetName( 308 << "Use \"/vis/viewer/select\" or \ 309 return; 310 } 311 312 if (command == fpCommandClearNonG4) { 313 auto sceneHandler = dynamic_cast<G4VtkScen 314 auto transientStore = sceneHandler->GetTra 315 316 transientStore.ClearNonG4(); 317 } 318 else if (command == fpCommandExport) { 319 G4String format, name; 320 321 std::istringstream iss(newValue); 322 iss >> format >> name; 323 324 if (format == "jpg" || format == "tiff" || 325 || format == "ps") 326 pVtkViewer->ExportScreenShot(name, forma 327 else if (format == "obj") 328 pVtkViewer->ExportOBJScene(name); 329 else if (format == "vrml") 330 pVtkViewer->ExportVRMLScene(name); 331 else if (format == "vtp") 332 pVtkViewer->ExportVTPScene(name); 333 else if (format == "gltf") 334 pVtkViewer->ExportGLTFScene(name); 335 else if (format == "x3d") 336 pVtkViewer->ExportX3DScene(name); 337 else 338 G4cout << "Unknown /vis/vtk/export file 339 } 340 else if (command == fpCommandExportCutter) { 341 std::istringstream iss(newValue); 342 343 G4String fileName; 344 iss >> fileName; 345 pVtkViewer->ExportVTPCutter(fileName); 346 } 347 else if (command == fpCommandWarnings) { 348 if (G4UIcommand::ConvertToBool(newValue)) 349 vtkObject::GlobalWarningDisplayOn(); 350 } 351 else { 352 vtkObject::GlobalWarningDisplayOff(); 353 } 354 } 355 else if (command == fpCommandHUD) { 356 if (G4UIcommand::ConvertToBool(newValue)) 357 pVtkViewer->EnableHUD(); 358 } 359 else { 360 pVtkViewer->DisableHUD(); 361 } 362 } 363 else if (command == fpCameraOrientation) { 364 G4cout << newValue << G4endl; 365 if (G4UIcommand::ConvertToBool(newValue)) 366 pVtkViewer->EnableCameraOrientationWidge 367 } 368 else { 369 pVtkViewer->DisableCameraOrientationWidg 370 } 371 } 372 else if (command == fpCommandDebugPrint) { 373 pVtkViewer->Print(); 374 } 375 else if (command == fpCommandPolyhedronPipel 376 G4String temp; 377 378 std::istringstream iss(newValue); 379 380 G4String pipelineType; 381 iss >> pipelineType; 382 383 pVtkViewer->SetPolyhedronPipeline(pipeline 384 } 385 else if (command == fpCommandImageOverlay) { 386 G4String temp; 387 388 G4String fileName; 389 G4double imageBottomLeft[2] = {0, 0}; 390 G4double imageTopRight[2] = {1, 1}; 391 G4double worldBottomLeft[2] = {0, 0}; 392 G4double worldTopRight[2] = {1, 1}; 393 G4double rotation[3] = {0, 0, 0}; 394 G4double translation[3] = {0, 0, 0}; 395 G4double alpha = 0.5; 396 397 std::istringstream iss(newValue); 398 399 iss >> fileName >> imageBottomLeft[0] >> i 400 >> imageTopRight[1] >> worldBottomLeft[0 401 >> worldTopRight[1] >> rotation[0] >> ro 402 >> translation[1] >> translation[2] >> a 403 404 pVtkViewer->AddImageOverlay(fileName, alph 405 worldTopRight, 406 } 407 else if (command == fpCommandGeometryOverlay 408 G4String temp; 409 410 G4String fileName; 411 G4double scale[3] = {1, 1, 1}; 412 G4double rotation[3] = {0,0,0}; 413 G4double translation[3] = {0,0,0}; 414 G4double colour[3] = {0.5, 0.5, 0.5}; 415 G4double alpha = 1.0; 416 G4String representation = "w"; 417 418 std::istringstream iss(newValue); 419 420 iss >> fileName 421 >> scale[0] >> scale[1] >> scale[2] 422 >> rotation[0] >> rotation[1] >> rotat 423 >> translation[0] >> translation[1] >> 424 >> alpha >> representation; 425 426 G4cout << fileName << G4endl; 427 pVtkViewer->AddGeometryOverlay(fileName, c 428 scale, rota 429 } 430 431 432 else if (command == fpCommandClipper) { 433 pVtkViewer->EnableClipper(G4Plane3D(), tru 434 } 435 else if (command == fpCommandCutter) { 436 pVtkViewer->EnableCutter(G4Plane3D(), true 437 } 438 else if (command == fpCommandShadow) { 439 pVtkViewer->EnableShadows(); 440 } 441 else if (command == fpCommandInteractorStart 442 pVtkViewer->StartInteractor(); 443 } 444 } 445