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 #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 = nullptr; 41 42 G4VtkMessenger* G4VtkMessenger::GetInstance() 43 { 44 if (fpInstance == nullptr) fpInstance = new G4VtkMessenger; 45 return fpInstance; 46 } 47 48 G4VtkMessenger::G4VtkMessenger() 49 { 50 G4bool omitable; 51 52 /***************************** Vtk directory *****************************/ 53 fpDirectory = new G4UIdirectory("/vis/vtk/", true); 54 fpDirectory->SetGuidance("G4VtkViewer commands."); 55 56 // Clear non-G4 57 fpCommandClearNonG4 = new G4UIcommand("/vis/vtk/clearNonG4", this); 58 fpCommandClearNonG4->SetGuidance("Clear non G4 objects from visualisation (image/3d overlays"); 59 60 // Export command 61 fpCommandExport = new G4UIcommand("/vis/vtk/export", this); 62 fpCommandExport->SetGuidance("Export a screenshot or OBJ file of current Vtk viewer"); 63 64 // File type for export 65 auto parameterExport = new G4UIparameter("format", 's', omitable = false); 66 fpCommandExport->SetGuidance("File type (jpg,tiff,eps,ps,obj,vtp,gltf,vrml,x3d)"); 67 fpCommandExport->SetParameter(parameterExport); 68 69 // File name for export 70 parameterExport = new G4UIparameter("file-name", 's', omitable = false); 71 fpCommandExport->SetGuidance("File name"); 72 fpCommandExport->SetParameter(parameterExport); 73 74 // Export cutter command 75 fpCommandExportCutter = new G4UIcommand("/vis/vtk/exportCutter", this); 76 fpCommandExportCutter->SetGuidance("Export a VTP file of the cutters if they exist"); 77 78 // File name for export 79 auto parameterExportCutter = new G4UIparameter("file-name", 's', omitable = true); 80 parameterExportCutter->SetGuidance("File name"); 81 parameterExportCutter->SetDefaultValue("cutter.vtp"); 82 fpCommandExportCutter->SetParameter(parameterExportCutter); 83 84 // Vtk debug print 85 fpCommandDebugPrint = new G4UIcommand("/vis/vtk/printDebug", this); 86 fpCommandDebugPrint->SetGuidance("Debug print of pipelines"); 87 88 // start interactor in native mode 89 fpCommandInteractorStart = new G4UIcommand("/vis/vtk/startInteraction", this); 90 fpCommandInteractorStart->SetGuidance("Start VtkNative window interaction"); 91 92 /***************************** Set directory *****************************/ 93 fpDirectorySet = new G4UIdirectory("/vis/vtk/set/", true); 94 fpDirectorySet->SetGuidance("G4VtkViewer set commands"); 95 96 // Vtk warnings output 97 fpCommandWarnings = new G4UIcmdWithABool("/vis/vtk/set/warnings", this); 98 fpCommandWarnings->SetParameterName("enable-warnings", false); 99 fpCommandWarnings->SetGuidance("Enable (True) or disable (False) VTK warnings"); 100 101 // HUD command 102 fpCommandHUD = new G4UIcmdWithABool("/vis/vtk/set/hud", this); 103 fpCommandHUD->SetGuidance("Enable or disable HUD for VTK"); 104 105 // Camera orientation widget 106 fpCameraOrientation = new G4UIcmdWithABool("/vis/vtk/set/orientation", this); 107 fpCameraOrientation->SetGuidance("Enable or disable camera orientation widget"); 108 109 // Clipper command 110 fpCommandClipper = new G4UIcommand("/vis/vtk/set/clipper", this); 111 fpCommandClipper->SetGuidance("Enable a cutaway plane (clipper) in VTK"); 112 auto fpCommandClipperParam = new G4UIparameter("widget", 'b', omitable = true); 113 fpCommandClipperParam->SetDefaultValue(1); 114 fpCommandClipper->SetParameter(fpCommandClipperParam); 115 116 // Clutter command 117 fpCommandCutter = new G4UIcommand("/vis/vtk/set/cutter", this); 118 fpCommandCutter->SetGuidance("Enable a section plane (cutter) in VTK"); 119 auto fpCommandCutterParam = new G4UIparameter("widget", 'b', omitable = true); 120 fpCommandCutterParam->SetDefaultValue(1); 121 fpCommandCutter->SetParameter(fpCommandCutterParam); 122 123 // Vtk polyhedron pipeline selection 124 auto fpCommandPolyhedronPipelineParam = new G4UIparameter("format", 's', omitable = true); 125 fpCommandPolyhedronPipeline = new G4UIcommand("/vis/vtk/set/polyhedronPipeline", this); 126 fpCommandPolyhedronPipeline->SetGuidance("Select type of polyhedron pipeline"); 127 fpCommandPolyhedronPipeline->SetGuidance("Type (separate, tensor, append, bake)"); 128 fpCommandPolyhedronPipeline->SetParameter(fpCommandPolyhedronPipelineParam); 129 130 // Shadows command 131 fpCommandShadow = new G4UIcommand("/vis/vtk/set/shadows", this); 132 fpCommandShadow->SetGuidance("Enable/disable shadows in VTK"); 133 auto fpCommandShadowParam = new G4UIparameter("enable", 'b', omitable = true); 134 fpCommandShadowParam->SetDefaultValue(1); 135 fpCommandShadow->SetParameter(fpCommandShadowParam); 136 137 /***************************** Add directory *****************************/ 138 fpDirectoryAdd = new G4UIdirectory("/vis/vtk/add/", true); 139 fpDirectoryAdd->SetGuidance("G4VtkViewer add commands"); 140 141 fpCommandImageOverlay = new G4UIcommand("/vis/vtk/add/imageOverlay", this); 142 auto parameterImageOverlay = new G4UIparameter("filename", 's', omitable = false); 143 parameterImageOverlay->SetGuidance("Image file name "); 144 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 145 146 parameterImageOverlay = new G4UIparameter("imageBottomLeftX", 'd', omitable = false); 147 parameterImageOverlay->SetGuidance("image bottom left x"); 148 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 149 150 parameterImageOverlay = new G4UIparameter("imageBottomLeftY", 'd', omitable = false); 151 parameterImageOverlay->SetGuidance("image bottom left y"); 152 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 153 154 parameterImageOverlay = new G4UIparameter("imageTopRightX", 'd', omitable = false); 155 parameterImageOverlay->SetGuidance("image top right x"); 156 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 157 158 parameterImageOverlay = new G4UIparameter("imageTopRightY", 'd', omitable = false); 159 parameterImageOverlay->SetGuidance("image top right y"); 160 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 161 162 parameterImageOverlay = new G4UIparameter("worldBottomLeftX", 'd', omitable = false); 163 parameterImageOverlay->SetGuidance("world bottom left x"); 164 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 165 166 parameterImageOverlay = new G4UIparameter("worldBottomLeftY", 'd', omitable = false); 167 parameterImageOverlay->SetGuidance("world bottom left y"); 168 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 169 170 parameterImageOverlay = new G4UIparameter("worldTopRightX", 'd', omitable = false); 171 parameterImageOverlay->SetGuidance("world top right x"); 172 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 173 174 parameterImageOverlay = new G4UIparameter("worldTopRightY", 'd', omitable = false); 175 parameterImageOverlay->SetGuidance("world top right y"); 176 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 177 178 parameterImageOverlay = new G4UIparameter("xRotation", 'd', omitable = true); 179 parameterImageOverlay->SetGuidance("rotation in x"); 180 parameterImageOverlay->SetDefaultValue(0.0); 181 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 182 183 parameterImageOverlay = new G4UIparameter("yRotation", 'd', omitable = true); 184 parameterImageOverlay->SetGuidance("rotation in y"); 185 parameterImageOverlay->SetDefaultValue(0.0); 186 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 187 188 parameterImageOverlay = new G4UIparameter("zRotation", 'd', omitable = true); 189 parameterImageOverlay->SetGuidance("rotation in z"); 190 parameterImageOverlay->SetDefaultValue(0.0); 191 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 192 193 parameterImageOverlay = new G4UIparameter("xTranslation", 'd', omitable = true); 194 parameterImageOverlay->SetGuidance("translation in x"); 195 parameterImageOverlay->SetDefaultValue(0.0); 196 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 197 198 parameterImageOverlay = new G4UIparameter("yTranslation", 'd', omitable = true); 199 parameterImageOverlay->SetGuidance("translation in y"); 200 parameterImageOverlay->SetDefaultValue(0.0); 201 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 202 203 parameterImageOverlay = new G4UIparameter("zTranslation", 'd', omitable = true); 204 parameterImageOverlay->SetGuidance("translation in z"); 205 parameterImageOverlay->SetDefaultValue(0.0); 206 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 207 208 parameterImageOverlay = new G4UIparameter("alpha", 'd', omitable = true); 209 parameterImageOverlay->SetGuidance("alpha"); 210 parameterImageOverlay->SetDefaultValue(0.5); 211 fpCommandImageOverlay->SetParameter(parameterImageOverlay); 212 213 fpCommandGeometryOverlay = new G4UIcommand("/vis/vtk/add/geometryOverlay", this); 214 auto parameterGeometryOverlay = new G4UIparameter("filename", 's', omitable = false); 215 parameterGeometryOverlay->SetGuidance("Geometry file name "); 216 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 217 218 parameterGeometryOverlay = new G4UIparameter("xScale", 'd', omitable = true); 219 parameterGeometryOverlay->SetGuidance("scale in x"); 220 parameterGeometryOverlay->SetDefaultValue(0.0); 221 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 222 223 parameterGeometryOverlay = new G4UIparameter("yScale", 'd', omitable = true); 224 parameterGeometryOverlay->SetGuidance("scale in y"); 225 parameterGeometryOverlay->SetDefaultValue(0.0); 226 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 227 228 parameterGeometryOverlay = new G4UIparameter("zScale", 'd', omitable = true); 229 parameterGeometryOverlay->SetGuidance("scale in z"); 230 parameterGeometryOverlay->SetDefaultValue(0.0); 231 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 232 233 parameterGeometryOverlay = new G4UIparameter("xRotation", 'd', omitable = true); 234 parameterGeometryOverlay->SetGuidance("rotation in x"); 235 parameterGeometryOverlay->SetDefaultValue(0.0); 236 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 237 238 parameterGeometryOverlay = new G4UIparameter("yRotation", 'd', omitable = true); 239 parameterGeometryOverlay->SetGuidance("rotation in y"); 240 parameterGeometryOverlay->SetDefaultValue(0.0); 241 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 242 243 parameterGeometryOverlay = new G4UIparameter("zRotation", 'd', omitable = true); 244 parameterGeometryOverlay->SetGuidance("rotation in z"); 245 parameterGeometryOverlay->SetDefaultValue(0.0); 246 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 247 248 parameterGeometryOverlay = new G4UIparameter("xTranslation", 'd', omitable = true); 249 parameterGeometryOverlay->SetGuidance("translation in x"); 250 parameterGeometryOverlay->SetDefaultValue(0.0); 251 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 252 253 parameterGeometryOverlay = new G4UIparameter("yTranslation", 'd', omitable = true); 254 parameterGeometryOverlay->SetGuidance("translation in y"); 255 parameterGeometryOverlay->SetDefaultValue(0.0); 256 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 257 258 parameterGeometryOverlay = new G4UIparameter("zTranslation", 'd', omitable = true); 259 parameterGeometryOverlay->SetGuidance("translation in z"); 260 parameterGeometryOverlay->SetDefaultValue(0.0); 261 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 262 263 parameterGeometryOverlay = new G4UIparameter("alpha", 'd', omitable = true); 264 parameterGeometryOverlay->SetGuidance("alpha"); 265 parameterGeometryOverlay->SetDefaultValue(0.5); 266 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 267 268 parameterGeometryOverlay = new G4UIparameter("representation", 's', omitable = true); 269 parameterGeometryOverlay->SetGuidance("representation of mesh"); 270 parameterGeometryOverlay->SetDefaultValue("s"); 271 fpCommandGeometryOverlay->SetParameter(parameterGeometryOverlay); 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(G4UIcommand* /*command*/) 289 { 290 return G4String(); 291 } 292 293 void G4VtkMessenger::SetNewValue(G4UIcommand* command, G4String newValue) 294 { 295 G4VisManager* pVisManager = G4VisManager::GetInstance(); 296 297 G4VViewer* pViewer = pVisManager->GetCurrentViewer(); 298 if (pViewer == nullptr) { 299 G4cout << "G4VtkMessenger::SetNewValue: No current viewer.\n" 300 << "\"/vis/open\", or similar, to get one." << G4endl; 301 return; 302 } 303 304 auto* pVtkViewer = dynamic_cast<G4VtkViewer*>(pViewer); 305 if (pVtkViewer == nullptr) { 306 G4cout << "G4VtkMessenger::SetNewValue: Current viewer is not of type VTK. \n" 307 << "(It is \"" << pViewer->GetName() << "\".)\n" 308 << "Use \"/vis/viewer/select\" or \"/vis/open\"." << G4endl; 309 return; 310 } 311 312 if (command == fpCommandClearNonG4) { 313 auto sceneHandler = dynamic_cast<G4VtkSceneHandler*>(pViewer->GetSceneHandler()); 314 auto transientStore = sceneHandler->GetTransientStore(); 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" || format == "png" || format == "bmp" || format == "pnm" 325 || format == "ps") 326 pVtkViewer->ExportScreenShot(name, format); 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 format" << G4endl; 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->EnableCameraOrientationWidget(); 367 } 368 else { 369 pVtkViewer->DisableCameraOrientationWidget(); 370 } 371 } 372 else if (command == fpCommandDebugPrint) { 373 pVtkViewer->Print(); 374 } 375 else if (command == fpCommandPolyhedronPipeline) { 376 G4String temp; 377 378 std::istringstream iss(newValue); 379 380 G4String pipelineType; 381 iss >> pipelineType; 382 383 pVtkViewer->SetPolyhedronPipeline(pipelineType); 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] >> imageBottomLeft[1] >> imageTopRight[0] 400 >> imageTopRight[1] >> worldBottomLeft[0] >> worldBottomLeft[1] >> worldTopRight[0] 401 >> worldTopRight[1] >> rotation[0] >> rotation[1] >> rotation[2] >> translation[0] 402 >> translation[1] >> translation[2] >> alpha; 403 404 pVtkViewer->AddImageOverlay(fileName, alpha, imageBottomLeft, worldBottomLeft, imageTopRight, 405 worldTopRight, rotation, translation); 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] >> rotation[2] 423 >> translation[0] >> translation[1] >> translation[2] 424 >> alpha >> representation; 425 426 G4cout << fileName << G4endl; 427 pVtkViewer->AddGeometryOverlay(fileName, colour, alpha, representation, 428 scale, rotation, translation); 429 } 430 431 432 else if (command == fpCommandClipper) { 433 pVtkViewer->EnableClipper(G4Plane3D(), true); 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