28 #ifdef G4VIS_BUILD_OI_DRIVER
33 #include <Inventor/nodes/SoSelection.h>
34 #include <Inventor/nodes/SoShape.h>
35 #include <Inventor/nodes/SoOrthographicCamera.h>
36 #include <Inventor/nodes/SoPerspectiveCamera.h>
37 #include <Inventor/actions/SoCallbackAction.h>
38 #include <Inventor/actions/SoWriteAction.h>
39 #include <Inventor/sensors/SoNodeSensor.h>
41 #include "HEPVis/nodes/SoImageWriter.h"
42 #include "HEPVis/actions/SoGL2PSAction.h"
43 #include "HEPVis/actions/SoCounterAction.h"
44 #include "HEPVis/actions/SoAlternateRepAction.h"
50 #include "Geant4_SoPolyhedron.h"
56 G4OpenInventorViewer::G4OpenInventorViewer(
57 G4OpenInventorSceneHandler& sceneHandler
59 :
G4VViewer(sceneHandler, sceneHandler.IncrementViewCount(), name)
60 ,fG4OpenInventorSceneHandler(sceneHandler)
61 ,fInteractorManager(0)
65 ,fGroupCameraSensor(0)
68 fNeedKernelVisit =
true;
70 fVP.SetAutoRefresh(
true);
71 fDefaultVP.SetAutoRefresh(
true);
73 fDefaultVP.SetPicking(
true);
81 ((
G4OpenInventor*)fG4OpenInventorSceneHandler.GetGraphicsSystem())->
85 fSoSelection =
new SoSelection;
87 fSoSelection->addSelectionCallback(SelectionCB,
this);
89 fSoSelection->policy = SoSelection::SINGLE;
91 SoGroup* group =
new SoGroup;
92 fSoSelection->addChild(group);
100 SoOrthographicCamera* camera =
new SoOrthographicCamera;
101 camera->viewportMapping.setValue(SoCamera::ADJUST_CAMERA);
103 camera->position.setValue(0,0,10);
104 camera->orientation.setValue(SbRotation(SbVec3f(0,1,0),0));
105 camera->height.setValue(10);
106 camera->nearDistance.setValue(1);
107 camera->farDistance.setValue(100);
108 camera->focalDistance.setValue(10);
109 group->addChild(camera);
112 if(soInput.openFile(
"g4view.iv",
TRUE)) {
113 SoSeparator* separator = SoDB::readAll(&soInput);
114 if(separator) fSoSelection->addChild(separator);
117 fSoSelection->addChild(fG4OpenInventorSceneHandler.fRoot);
120 fSoImageWriter =
new SoImageWriter();
121 fSoImageWriter->fileName.setValue(
"g4out.ps");
122 fSoSelection->addChild(fSoImageWriter);
126 fGroupCameraSensor =
new SoNodeSensor(GroupCameraSensorCB,
this);
127 fGroupCameraSensor->setPriority(0);
128 fGroupCameraSensor->attach(group);
130 fCameraSensor =
new SoNodeSensor(CameraSensorCB,
this);
131 fCameraSensor->setPriority(0);
134 G4OpenInventorViewer::~G4OpenInventorViewer () {
135 fCameraSensor->detach();
136 delete fCameraSensor;
137 fGroupCameraSensor->detach();
138 delete fGroupCameraSensor;
139 fSoSelection->unref();
142 void G4OpenInventorViewer::KernelVisitDecision () {
152 CompareForKernelVisit(fLastVP)) {
178 fVP.GetDefaultVisAttributes()->
GetColour()) ||
180 fVP.GetDefaultTextVisAttributes()->
GetColour()) ||
188 fVP.GetVisAttributesModifiers().size())
202 fVP.GetCutawayPlanes ().size ())
return true;
215 void G4OpenInventorViewer::ClearView () {
218 void G4OpenInventorViewer::SetView () {
222 = fSceneHandler.GetScene()->GetStandardTargetPoint()
223 + fVP.GetCurrentTargetPoint ();
224 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
225 if(radius<=0.) radius = 1.;
226 const G4double cameraDistance = fVP.GetCameraDistance (radius);
227 const G4Vector3D& direction = fVP.GetViewpointDirection().unit();
228 const G4Point3D cameraPosition = target + cameraDistance * direction;
246 SoCamera* camera = GetCamera();
250 camera->position.setValue((
float)cameraPosition.x(),
251 (float)cameraPosition.y(),
252 (float)cameraPosition.z());
254 SbVec3f sbTarget((
float)target.x(),
257 SbVec3f sbUp((
float)up.x(),
263 pointAt(camera,sbTarget,sbUp);
270 if(camera->isOfType(SoOrthographicCamera::getClassTypeId())) {
271 if (fVP.GetFieldHalfAngle() == 0.) {
280 }
else if(camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
281 if (fVP.GetFieldHalfAngle() == 0.) {
293 G4OpenInventorViewer::pointAt(SoCamera* camera,
const SbVec3f & targetpoint,
const SbVec3f & upvector)
295 SbVec3f dir = targetpoint - camera->position.getValue();
296 if (dir.normalize() == 0.0f)
return;
297 lookAt(camera,dir, upvector);
304 G4OpenInventorViewer::lookAt(SoCamera* camera,
const SbVec3f & dir,
const SbVec3f & up)
308 SbVec3f x = y.cross(z);
318 SbMatrix rot = SbMatrix::identity();
331 camera->orientation.setValue(SbRotation(rot));
335 G4OpenInventorViewer::lookedAt(SoCamera* camera,SbVec3f & dir, SbVec3f & up)
337 SbRotation rot = camera->orientation.getValue();
338 SbMatrix mrot; rot.getValue(mrot);
357 up = SbVec3f(0.f,1.f,0.f);
358 if (std::abs(up.dot(z)) > 1.e-6) {
364 void G4OpenInventorViewer::DrawView () {
366 if (!fNeedKernelVisit) KernelVisitDecision();
371 void G4OpenInventorViewer::ShowView () {
372 fInteractorManager -> SecondaryLoop ();
375 void G4OpenInventorViewer::GroupCameraSensorCB(
void* aThis,SoSensor* aSensor){
376 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
378 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
382 if(node->isOfType(SoCamera::getClassTypeId())) {
385 SoCamera* camera = (SoCamera*)node;
386 This->fCameraSensor->detach();
387 This->fCameraSensor->attach(camera);
392 void G4OpenInventorViewer::CameraSensorCB(
void* aThis,SoSensor* aSensor) {
393 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
397 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
399 if(node->isOfType(SoCamera::getClassTypeId())) {
400 SoCamera* camera = (SoCamera*)node;
402 SbVec3f direction, up;
403 lookedAt(camera,direction, up);
404 This->fVP.SetViewpointDirection
405 (
G4Vector3D(-direction[0],-direction[1],-direction[2]));
406 This->fVP.SetUpVector(
G4Vector3D(up[0],up[1],up[2]));
408 SbVec3f
pos = camera->position.getValue();
409 SbVec3f target = pos + direction * camera->focalDistance.getValue();
411 This->fVP.SetCurrentTargetPoint(
G4Point3D(target[0],target[1],target[2]));
415 void G4OpenInventorViewer::SelectionCB(
420 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
421 SoNode* node = ((SoFullPath*)aPath)->getTail();
423 if(attHolder && attHolder->
GetAttDefs().size()) {
424 for (
size_t i = 0; i < attHolder->
GetAttDefs().size(); ++i) {
430 G4String cls((
char*)node->getTypeId().getName().getString());
431 G4cout <<
"SoNode : " << node
432 <<
" SoType : " << cls
433 <<
" name : " << name
445 This->fSoSelection->deselectAll();
459 void G4OpenInventorViewer::DrawDetector() {
477 void G4OpenInventorViewer::Escape(){
479 fInteractorManager->RequireExitSecondaryLoop (
OIV_EXIT_CODE);
482 void G4OpenInventorViewer::WritePostScript(
const G4String& aFile) {
483 if(!fGL2PSAction)
return;
484 fGL2PSAction->setFileName(aFile.c_str());
485 fGL2PSAction->setExportImageFormat(GL2PS_EPS);
487 fGL2PSAction->setBufferSize(0);
489 if (fGL2PSAction->enableFileWriting()) {
491 fGL2PSAction->disableFileWriting();
493 fGL2PSAction->resetBufferSizeParameters();
496 void G4OpenInventorViewer::WritePDF(
const G4String& aFile) {
497 if(!fGL2PSAction)
return;
498 fGL2PSAction->setFileName(aFile.c_str());
499 fGL2PSAction->setExportImageFormat(GL2PS_PDF);
501 fGL2PSAction->setBufferSize(0);
503 if (fGL2PSAction->enableFileWriting()) {
505 fGL2PSAction->disableFileWriting();
507 fGL2PSAction->resetBufferSizeParameters();
510 void G4OpenInventorViewer::WritePixmapPostScript(
const G4String& aFile) {
511 fSoImageWriter->fileName.setValue(aFile.c_str());
513 fSoImageWriter->enable();
515 fSoImageWriter->disable();
516 if(fSoImageWriter->getStatus()) {
517 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
521 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
527 void G4OpenInventorViewer::WriteInventor(
const G4String& aFile) {
530 SbBool genAlternateRep =
TRUE;
532 SbBool binary =
TRUE;
533 SoAlternateRepAction alternateRepAction;
534 if(genAlternateRep==
TRUE) {
535 alternateRepAction.setGenerate(
TRUE);
536 alternateRepAction.apply(fSoSelection);
539 SoWriteAction writeAction;
540 writeAction.getOutput()->openFile(aFile.c_str());
541 writeAction.getOutput()->setBinary(binary);
542 writeAction.apply(fSoSelection);
543 writeAction.getOutput()->closeFile();
545 if(genAlternateRep==
TRUE) {
546 alternateRepAction.setGenerate(
FALSE);
547 alternateRepAction.apply(fSoSelection);
560 static void CountTrianglesCB(
563 ,
const SoPrimitiveVertex*
564 ,
const SoPrimitiveVertex*,
565 const SoPrimitiveVertex*)
567 Counter* counter = (Counter*)userData;
568 counter->fTriangles++;
571 static void CountLineSegmentsCB(
574 ,
const SoPrimitiveVertex*
575 ,
const SoPrimitiveVertex*)
577 Counter* counter = (Counter*)userData;
578 counter->fLineSegments++;
581 static void CountPointsCB(
584 ,
const SoPrimitiveVertex*)
586 Counter* counter = (Counter*)userData;
590 void G4OpenInventorViewer::SceneGraphStatistics() {
592 counter.fTriangles = 0;
593 counter.fLineSegments = 0;
596 SoCallbackAction callbackAction;
597 callbackAction.addTriangleCallback
598 (SoShape::getClassTypeId(),CountTrianglesCB,(
void*)&counter);
599 callbackAction.addLineSegmentCallback
600 (SoShape::getClassTypeId(),CountLineSegmentsCB,(
void*)&counter);
601 callbackAction.addPointCallback
602 (SoShape::getClassTypeId(),CountPointsCB,(
void*)&counter);
603 callbackAction.apply(fSoSelection);
605 SoCounterAction counterAction;
606 counterAction.apply(fSoSelection);
607 int nodes = counterAction.getCount();
609 counterAction.setLookFor(SoCounterAction::TYPE);
610 counterAction.setType(SoShape::getClassTypeId());
611 counterAction.apply(fSoSelection);
612 int shapes = counterAction.getCount();
614 G4cout <<
"Number of triangles : " << counter.fTriangles <<
G4endl;
615 G4cout <<
"Number of line segments : " << counter.fLineSegments <<
G4endl;
616 G4cout <<
"Number of points : " << counter.fPoints <<
G4endl;
621 void G4OpenInventorViewer::EraseDetector() {
622 fG4OpenInventorSceneHandler.fDetectorRoot->removeAllChildren();
624 void G4OpenInventorViewer::EraseEvent() {
625 fG4OpenInventorSceneHandler.fTransientRoot->removeAllChildren();
628 void G4OpenInventorViewer::SetPreviewAndFull() {
629 fG4OpenInventorSceneHandler.fPreviewAndFull =
true;
635 void G4OpenInventorViewer::SetPreview() {
636 fG4OpenInventorSceneHandler.fPreviewAndFull =
false;
646 void G4OpenInventorViewer::SetSolid() {
650 switch (existingStyle) {
662 SetViewParameters(vp);
665 void G4OpenInventorViewer::SetWireFrame() {
668 switch (existingStyle) {
680 SetViewParameters(vp);
685 void G4OpenInventorViewer::SetReducedWireFrame(
bool aValue) {
693 switch (existingStyle) {
705 SetViewParameters(vp);
710 void G4OpenInventorViewer::UpdateScene() {
723 if(aTopic==
"controls") {
725 Controls on an Inventor examiner viewer are :\n\
726 - in picking mode (cursor is the upper left arrow)\n\
727 Ctrl + pick a volume : see daughters.\n\
728 Shift + pick a volume : see mother.\n\
729 - in viewing mode (cursor is the hand)\n\
730 Left-button + pointer move : rotate.\n\
731 Ctrl+Left-button + pointer move : pan.\n\
732 Ctrl+Shift+Left-button + pointer move : scale.\n\
733 Middle-button + pointer move : pan.\n\
734 Right-button : popup menu.\n");
const G4Colour & GetBackgroundColour() const
G4double GetVisibleDensity() const
void SetAuxEdgeVisible(G4bool)
G4VInteractorManager * GetInteractorManager()
G4bool IsCullingInvisible() const
G4double GetExplodeFactor() const
static G4bool GetColour(const G4String &key, G4Colour &result)
const G4Colour & GetColour() const
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
G4bool IsDensityCulling() const
HepGeom::Point3D< G4double > G4Point3D
HepGeom::Vector3D< G4double > G4Vector3D
const G4Planes & GetCutawayPlanes() const
const char * name(G4int ptype)
const std::vector< G4ModelingParameters::VisAttributesModifier > & GetVisAttributesModifiers() const
G4GLOB_DLL std::ostream G4cout
G4bool IsAuxEdgeVisible() const
G4bool IsCullingCovered() const
const G4VisAttributes * GetDefaultTextVisAttributes() const
G4bool IsMarkerNotHidden() const
void SetDrawingStyle(G4ViewParameters::DrawingStyle style)
const G4Vector3D & GetScaleFactor() const
G4int GetNoOfSides() const
DrawingStyle GetDrawingStyle() const
const G4Plane3D & GetSectionPlane() const
const G4VisAttributes * GetDefaultVisAttributes() const
static const G4double pos
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
HepGeom::Normal3D< G4double > G4Normal3D