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());
486 if (fGL2PSAction->enableFileWriting()) {
488 fGL2PSAction->disableFileWriting();
492 void G4OpenInventorViewer::WritePixmapPostScript(
const G4String& aFile) {
493 fSoImageWriter->fileName.setValue(aFile.c_str());
495 fSoImageWriter->enable();
497 fSoImageWriter->disable();
498 if(fSoImageWriter->getStatus()) {
499 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
503 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
509 void G4OpenInventorViewer::WriteInventor(
const G4String& aFile) {
512 SbBool genAlternateRep =
TRUE;
514 SbBool binary =
TRUE;
515 SoAlternateRepAction alternateRepAction;
516 if(genAlternateRep==
TRUE) {
517 alternateRepAction.setGenerate(
TRUE);
518 alternateRepAction.apply(fSoSelection);
521 SoWriteAction writeAction;
522 writeAction.getOutput()->openFile(aFile.c_str());
523 writeAction.getOutput()->setBinary(binary);
524 writeAction.apply(fSoSelection);
525 writeAction.getOutput()->closeFile();
527 if(genAlternateRep==
TRUE) {
528 alternateRepAction.setGenerate(
FALSE);
529 alternateRepAction.apply(fSoSelection);
542 static void CountTrianglesCB(
545 ,
const SoPrimitiveVertex*
546 ,
const SoPrimitiveVertex*,
547 const SoPrimitiveVertex*)
549 Counter* counter = (Counter*)userData;
550 counter->fTriangles++;
553 static void CountLineSegmentsCB(
556 ,
const SoPrimitiveVertex*
557 ,
const SoPrimitiveVertex*)
559 Counter* counter = (Counter*)userData;
560 counter->fLineSegments++;
563 static void CountPointsCB(
566 ,
const SoPrimitiveVertex*)
568 Counter* counter = (Counter*)userData;
572 void G4OpenInventorViewer::SceneGraphStatistics() {
574 counter.fTriangles = 0;
575 counter.fLineSegments = 0;
578 SoCallbackAction callbackAction;
579 callbackAction.addTriangleCallback
580 (SoShape::getClassTypeId(),CountTrianglesCB,(
void*)&counter);
581 callbackAction.addLineSegmentCallback
582 (SoShape::getClassTypeId(),CountLineSegmentsCB,(
void*)&counter);
583 callbackAction.addPointCallback
584 (SoShape::getClassTypeId(),CountPointsCB,(
void*)&counter);
585 callbackAction.apply(fSoSelection);
587 SoCounterAction counterAction;
588 counterAction.apply(fSoSelection);
589 int nodes = counterAction.getCount();
591 counterAction.setLookFor(SoCounterAction::TYPE);
592 counterAction.setType(SoShape::getClassTypeId());
593 counterAction.apply(fSoSelection);
594 int shapes = counterAction.getCount();
596 G4cout <<
"Number of triangles : " << counter.fTriangles <<
G4endl;
597 G4cout <<
"Number of line segments : " << counter.fLineSegments <<
G4endl;
598 G4cout <<
"Number of points : " << counter.fPoints <<
G4endl;
603 void G4OpenInventorViewer::EraseDetector() {
604 fG4OpenInventorSceneHandler.fDetectorRoot->removeAllChildren();
606 void G4OpenInventorViewer::EraseEvent() {
607 fG4OpenInventorSceneHandler.fTransientRoot->removeAllChildren();
610 void G4OpenInventorViewer::SetPreviewAndFull() {
611 fG4OpenInventorSceneHandler.fPreviewAndFull =
true;
617 void G4OpenInventorViewer::SetPreview() {
618 fG4OpenInventorSceneHandler.fPreviewAndFull =
false;
628 void G4OpenInventorViewer::SetSolid() {
632 switch (existingStyle) {
644 SetViewParameters(vp);
647 void G4OpenInventorViewer::SetWireFrame() {
650 switch (existingStyle) {
662 SetViewParameters(vp);
667 void G4OpenInventorViewer::SetReducedWireFrame(
bool aValue) {
675 switch (existingStyle) {
687 SetViewParameters(vp);
692 void G4OpenInventorViewer::UpdateScene() {
705 if(aTopic==
"controls") {
707 Controls on an Inventor examiner viewer are :\n\
708 - in picking mode (cursor is the upper left arrow)\n\
709 Ctrl + pick a volume : see daughters.\n\
710 Shift + pick a volume : see mother.\n\
711 - in viewing mode (cursor is the hand)\n\
712 Left-button + pointer move : rotate.\n\
713 Ctrl+Left-button + pointer move : pan.\n\
714 Ctrl+Shift+Left-button + pointer move : scale.\n\
715 Middle-button + pointer move : pan.\n\
716 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 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