43#include <vtk3DSImporter.h>
44#include <vtkBMPWriter.h>
45#include <vtkIVExporter.h>
46#include <vtkImageWriter.h>
47#include <vtkImplicitPlaneRepresentation.h>
48#include <vtkImplicitPlaneWidget2.h>
49#include <vtkJPEGWriter.h>
50#include <vtkLightCollection.h>
51#include <vtkOBJExporter.h>
52#include <vtkOBJImporter.h>
53#include <vtkGLTFExporter.h>
54#include <vtkOOGLExporter.h>
55#include <vtkX3DExporter.h>
56#include <vtkJSONRenderWindowExporter.h>
57#include <vtkVtkJSSceneGraphSerializer.h>
59#include <vtkPNGWriter.h>
60#include <vtkPNMWriter.h>
61#include <vtkPOVExporter.h>
62#include <vtkPostScriptWriter.h>
63#include <vtkRIBExporter.h>
64#include <vtkRendererCollection.h>
65#include <vtkSingleVTPExporter.h>
66#include <vtkTIFFWriter.h>
67#include <vtkVRMLExporter.h>
68#include <vtkVRMLImporter.h>
69#include <vtkWindowToImageFilter.h>
70#include <vtkX3DExporter.h>
74#include <vtkCameraPass.h>
75#include <vtkOpenGLRenderer.h>
76#include <vtkRenderPass.h>
77#include <vtkRenderPassCollection.h>
78#include <vtkSequencePass.h>
79#include <vtkShadowMapBakerPass.h>
80#include <vtkShadowMapPass.h>
83 :
G4VViewer(sceneHandler, sceneHandler.IncrementViewCount(), name)
85 vtkObject::GlobalWarningDisplayOff();
88 fVP.SetAutoRefresh(
true);
99 G4cout <<
"G4VtkViewer::G4VtkViewer> " <<
fVP.GetWindowSizeHintX() <<
" "
101 G4cout <<
"G4VtkViewer::G4VtkViewer> " <<
fVP.GetWindowLocationHintX() <<
" "
102 <<
fVP.GetWindowLocationHintY() <<
G4endl;
108 G4int positionX =
fVP.GetWindowLocationHintX();
109 if (
fVP.IsWindowLocationHintXNegative()) {
110 positionX = screenSizeX + positionX -
fVP.GetWindowSizeHintX();
112 G4int positionY =
fVP.GetWindowLocationHintY();
113 if (!
fVP.IsWindowLocationHintYNegative()) {
114 positionY = screenSizeY + positionY -
fVP.GetWindowSizeHintY();
125 pixelFactor *
fVP.GetWindowSizeHintY());
132 camera->SetPosition(0, 0, 1000);
133 camera->SetFocalPoint(0, 0, 0);
137 renderer->SetUseHiddenLineRemoval(0);
163 const G4Colour backgroundColour =
fVP.GetBackgroundColour();
174 G4Point3D viewpointDirection =
fVP.GetViewpointDirection();
181 vtkCamera* activeCamera =
renderer->GetActiveCamera();
182 activeCamera->SetFocalPoint(targetPosition.
x(), targetPosition.
y(), targetPosition.
z());
183 activeCamera->SetViewAngle(2*fieldHalfAngle / CLHEP::pi * 180);
184 activeCamera->SetPosition(cameraPosition.
x(), cameraPosition.
y(), cameraPosition.
z());
187 if (fieldHalfAngle == 0) {
188 activeCamera->SetParallelProjection(1);
191 activeCamera->SetParallelProjection(0);
203 renderer->GetActiveCamera()->SetViewUp(upVector.
x(), upVector.
y(), upVector.
z());
206 const G4Vector3D lightDirection =
fVP.GetLightpointDirection();
207 G4bool lightsMoveWithCamera =
fVP.GetLightsMoveWithCamera();
210 vtkLightCollection* currentLights =
renderer->GetLights();
211 if (currentLights->GetNumberOfItems() != 0) {
212 auto currentLight =
dynamic_cast<vtkLight*
>(currentLights->GetItemAsObject(0));
213 if (currentLight !=
nullptr) {
214 currentLight->SetPosition(lightPosition.
x(), lightPosition.
y(), lightPosition.
z());
215 if (lightsMoveWithCamera) {
216 currentLight->SetLightTypeToCameraLight();
219 currentLight->SetLightTypeToSceneLight();
225 if (
fVP.IsCutaway()) {
230 if (
fVP.IsSection()) {
242 G4VtkStore& ts = fVtkSceneHandler.GetTransientStore();
287 vtkNew<vtkShadowMapPass> shadows;
288 vtkNew<vtkSequencePass> seq;
290 vtkNew<vtkRenderPassCollection> passes;
291 passes->AddItem(shadows->GetShadowMapBakerPass());
292 passes->AddItem(shadows);
293 seq->SetPasses(passes);
295 vtkNew<vtkCameraPass> cameraP;
296 cameraP->SetDelegatePass(seq);
299 auto glrenderer =
dynamic_cast<vtkOpenGLRenderer*
>(
renderer.GetPointer());
300 glrenderer->SetPass(cameraP);
314 std::fmod(colour.
GetGreen() + 0.5, 1.0),
315 std::fmod(colour.
GetBlue() + 0.5, 1.0));
345 vtkImageWriter* imWriter =
nullptr;
347 if (format ==
"bmp") {
348 imWriter = vtkBMPWriter::New();
350 else if (format ==
"jpg") {
351 imWriter = vtkJPEGWriter::New();
353 else if (format ==
"pnm") {
354 imWriter = vtkPNMWriter::New();
356 else if (format ==
"png") {
357 imWriter = vtkPNGWriter::New();
359 else if (format ==
"tiff") {
360 imWriter = vtkTIFFWriter::New();
362 else if (format ==
"ps") {
363 imWriter = vtkPostScriptWriter::New();
374 winToImage->SetScale(1);
375 if (format ==
"ps") {
376 winToImage->SetInputBufferTypeToRGB();
377 winToImage->ReadFrontBufferOff();
378 winToImage->Update();
381 winToImage->SetInputBufferTypeToRGBA();
384 imWriter->SetFileName((path +
"." + format).c_str());
385 imWriter->SetInputConnection(winToImage->GetOutputPort());
393 exporter->SetFilePrefix(path.c_str());
401 exporter->SetFileName((path +
".vrml").c_str());
409 exporter->SetFileName((path +
".vtp").c_str());
416 exporter->SetFileName((fileName+
".gltf").c_str());
417 exporter->InlineDataOn();
424 exporter->SetFileName((fileName+
".x3d").c_str());
433 exporter->SetSerializer(serializer);
443 vtkNew<vtkRenderer> tempRenderer;
447 for (
const auto& i : separate) {
448 i.second->GetActor();
449 auto children = i.second->GetChildPipelines();
450 for (
auto child : children) {
451 if (child->GetTypeName() ==
"G4VtkCutterPipeline") {
453 tempRenderer->AddActor(childCutter->GetActor());
458 auto tensor = s.GetTensorPipeMap();
459 for (
const auto& i : tensor) {
460 i.second->GetActor();
461 auto children = i.second->GetChildPipelines();
462 for (
auto child : children) {
463 if (child->GetTypeName() ==
"G4VtkCutterPipeline") {
465 tempRenderer->AddActor(childCutter->GetActor());
470 auto append = s.GetAppendPipeMap();
471 for (
const auto& i : append) {
472 i.second->GetActor();
473 auto children = i.second->GetChildPipelines();
474 for (
auto child : children) {
475 if (child->GetTypeName() ==
"G4VtkCutterPipeline") {
477 tempRenderer->AddActor(childCutter->GetActor());
482 auto baked = s.GetBakePipeMap();
483 for (
const auto& i : baked) {
484 i.second->GetActor();
485 auto children = i.second->GetChildPipelines();
486 for (
auto child : children) {
487 if (child->GetTypeName() ==
"G4VtkCutterPipeline") {
489 tempRenderer->AddActor(childCutter->GetActor());
494 vtkNew<vtkRenderWindow> tempRenderWindow;
495 tempRenderWindow->AddRenderer(tempRenderer);
496 vtkNew<vtkSingleVTPExporter> exporter;
497 exporter->SetRenderWindow(tempRenderWindow);
498 exporter->SetFileName(fileName.c_str());
505 vtkNew<vtkRenderer> tempRenderer;
507 tempRenderWindow->AddRenderer(tempRenderer);
511 if (storeName ==
"transient") {
512 G4VtkStore& store = fVtkSceneHandler.GetTransientStore();
516 G4VtkStore& store = fVtkSceneHandler.GetStore();
520 if (fileName.find(
"obj") != std::string::npos) {
521 vtkNew<vtkOBJExporter> exporter;
522 exporter->SetRenderWindow(tempRenderWindow);
523 exporter->SetFilePrefix(fileName.c_str());
526 else if (fileName.find(
"vrml") != std::string::npos) {
527 vtkNew<vtkVRMLExporter> exporter;
528 exporter->SetRenderWindow(tempRenderWindow);
529 exporter->SetFileName(fileName.c_str());
532 else if (fileName.find(
"vtp") != std::string::npos) {
533 vtkNew<vtkSingleVTPExporter> exporter;
534 exporter->SetRenderWindow(tempRenderWindow);
535 exporter->SetFileName(fileName.c_str());
538 else if (fileName.find(
"gltf") != std::string::npos) {
539 vtkNew<vtkGLTFExporter> exporter;
540 exporter->SetRenderWindow(tempRenderWindow);
541 exporter->SetFileName(fileName.c_str());
542 exporter->InlineDataOn();
552 std::fmod(colour.
GetGreen() + 0.5, 1.0),
553 std::fmod(colour.
GetBlue() + 0.5, 1.0));
563 vtkNew<vtkIPWCallback> clipperCallback;
565 G4VtkStore& store = fVtkSceneHandler.GetStore();
566 clipperCallback->SetStore(&store);
567 clipperCallback->SetUpdatePipelineName(
"clipper",
"clipper");
577 vtkNew<vtkPropCollection> planeRepActors;
579 planeRepActors->InitTraversal();
590 vtkNew<vtkIPWCallback> cutterCallback;
592 G4VtkStore& store = fVtkSceneHandler.GetStore();
593 cutterCallback->SetStore(&store);
594 cutterCallback->SetUpdatePipelineName(
"cutter",
"cutter");
635 s.AddClipper(name, plane);
663 s.AddCutter(name, plane);
710 auto xScale = (worldTopRight[0] - worldBottomLeft[0]) / (imageTopRight[0] - imageBottomLeft[0]);
711 auto yScale = -(worldTopRight[1] - worldBottomLeft[1]) / (imageTopRight[1] - imageBottomLeft[1]);
715 auto scal =
G4Scale3D(xScale, yScale, 1);
716 auto rotx =
G4RotateX3D(rotation[0]/180*CLHEP::pi);
717 auto roty =
G4RotateY3D(rotation[1]/180*CLHEP::pi);
718 auto rotz =
G4RotateZ3D(rotation[2]/180*CLHEP::pi);
719 auto tranImg =
G4Translate3D( -std::fabs(imageBottomLeft[0] + imageTopRight[0]) / 2.0,
720 -std::fabs(imageBottomLeft[1] + imageTopRight[1]) / 2.0,
726 G4cout << translation[0] <<
" " << translation[1] <<
" " << translation[2] <<
G4endl;
727 transformation = tran * rotz * roty * rotx * scal * tranImg * transformation;
729 G4cout << transformation.dx() <<
" " << transformation.dy() <<
" " << transformation.dz() <<
G4endl;
731 G4VtkStore& st = fVtkSceneHandler.GetTransientStore();
744 auto scal =
G4Scale3D(scale[0], scale[1], scale[2]);
748 auto tran =
G4Translate3D(translation[0], translation[1], translation[2]);
750 transformation = tran * rotz * roty * rotx * scal * transformation;
753 G4VtkStore& st = fVtkSceneHandler.GetTransientStore();
757 if (representation ==
"w")
759 else if (representation ==
"s")
763 vc.
green = colour[1];
776 G4VtkStore& st = fVtkSceneHandler.GetTransientStore();
HepGeom::Plane3D< G4double > G4Plane3D
HepGeom::Point3D< G4double > G4Point3D
HepGeom::Vector3D< G4double > G4Vector3D
vtkSmartPointer< vtkPlane > G4Plane3DToVtkPlane(const G4Plane3D &g4plane)
G4GLOB_DLL std::ostream G4cout
G4double GetGreen() const
G4VSceneHandler & fSceneHandler
G4ViewParameters fDefaultVP
G4VViewer(G4VSceneHandler &, G4int id, const G4String &name="")
void SetPolyhedronPipeline(const G4String &str)
std::map< std::size_t, std::shared_ptr< G4VtkPolydataPipeline > > & GetSeparatePipeMap()
void AddNonG4ObjectImage(const G4String &fileName, const G4VtkVisContext &vc)
void AddToRenderer(vtkRenderer *renderer)
void RemoveCutter(G4String name)
void GetBounds(G4double maxBound[6])
void RemoveClipper(G4String name)
void AddNonG4ObjectPolydata(const G4String fileName, const G4VtkVisContext &vc)
vtkNew< vtkCamera > camera
vtkNew< vtkCameraOrientationWidget > camOrientWidget
void DisableCutter(G4String name)
void ExportVRMLScene(G4String)
void AddImageOverlay(const G4String &fileName, const G4double alpha, const G4double imageBottomLeft[2], const G4double worldBottomLeft[2], const G4double imageTopRight[2], const G4double worldTopRight[2], const G4double rot[3], const G4double trans[3])
vtkNew< vtkImplicitPlaneWidget2 > cutterPlaneWidget
vtkNew< vtkGeant4Callback > geant4Callback
void Initialise() override
void SetPolyhedronPipeline(const G4String &t)
void ExportScreenShot(G4String, G4String)
void FinishView() override
void ExportGLTFScene(G4String)
vtkNew< vtkTextActor > infoTextActor
virtual void SetWidgetInteractor(vtkAbstractWidget *widget)
virtual void AddClipperPlaneWidget(const G4Plane3D &plane)
vtkRenderWindowInteractor * renderWindowInteractor
vtkNew< vtkImplicitPlaneRepresentation > cutterPlaneRepresentation
void ExportJSONRenderWindowScene(G4String)
virtual void EnableClipperWidget()
G4VtkViewer(G4VSceneHandler &, const G4String &name)
virtual void AddCameraOrientationWidget()
void ExportFormatStore(G4String fileName, G4String store)
void EnableClipper(const G4Plane3D &plane, G4bool widget)
virtual void AddCutterPlaneWidget(const G4Plane3D &plane)
virtual void EnableCameraOrientationWidget()
vtkNew< vtkInfoCallback > infoCallback
virtual void DisableCutterWidget()
virtual void DisableCameraOrientationWidget()
vtkNew< vtkImplicitPlaneWidget2 > clipperPlaneWidget
vtkNew< vtkRenderer > renderer
void EnableCutter(const G4Plane3D &plane, G4bool bWidget)
void ExportVTPScene(G4String)
vtkNew< vtkImplicitPlaneRepresentation > clipperPlaneRepresentation
virtual void DisableClipperWidget()
void ExportOBJScene(G4String)
void ExportX3DScene(G4String)
void ClearView() override
virtual void EnableCutterWidget()
void AddGeometryOverlay(const G4String &fileName, const G4double colour[3], const G4double alpha, const G4String &representation, const G4double scale[3], const G4double rotation[3], const G4double translation[3])
vtkRenderWindow * _renderWindow
void ExportVTPCutter(G4String fileName)
G4ViewParameters::DrawingStyle fDrawingStyle
BasicVector3D< T > unit() const