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>
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())->
82 GetInteractorManager();
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);
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)) {
179 fVP.GetDefaultVisAttributes()->
GetColour()) ||
181 fVP.GetDefaultTextVisAttributes()->
GetColour()) ||
189 fVP.GetVisAttributesModifiers().size())
203 fVP.GetCutawayPlanes ().size ())
return true;
216 void G4OpenInventorViewer::ClearView () {
219 void G4OpenInventorViewer::SetView () {
223 = fSceneHandler.GetScene()->GetStandardTargetPoint()
224 + fVP.GetCurrentTargetPoint ();
225 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
226 if(radius<=0.) radius = 1.;
227 const G4double cameraDistance = fVP.GetCameraDistance (radius);
229 const G4Point3D cameraPosition = target + cameraDistance * direction;
247 SoCamera* camera = GetCamera();
251 camera->position.setValue((
float)cameraPosition.
x(),
252 (float)cameraPosition.
y(),
253 (float)cameraPosition.
z());
255 SbVec3f sbTarget((
float)target.
x(),
258 SbVec3f sbUp((
float)up.
x(),
264 pointAt(camera,sbTarget,sbUp);
271 if(camera->isOfType(SoOrthographicCamera::getClassTypeId())) {
272 if (fVP.GetFieldHalfAngle() == 0.) {
281 }
else if(camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
282 if (fVP.GetFieldHalfAngle() == 0.) {
294 G4OpenInventorViewer::pointAt(SoCamera* camera,
const SbVec3f & targetpoint,
const SbVec3f & upvector)
296 SbVec3f
dir = targetpoint - camera->position.getValue();
297 if (dir.normalize() == 0.0f)
return;
298 lookAt(camera,dir, upvector);
305 G4OpenInventorViewer::lookAt(SoCamera* camera,
const SbVec3f & dir,
const SbVec3f & up)
309 SbVec3f
x = y.cross(z);
319 SbMatrix rot = SbMatrix::identity();
332 camera->orientation.setValue(SbRotation(rot));
336 G4OpenInventorViewer::lookedAt(SoCamera* camera,SbVec3f & dir, SbVec3f & up)
338 SbRotation rot = camera->orientation.getValue();
339 SbMatrix mrot; rot.getValue(mrot);
358 up = SbVec3f(0.
f,1.
f,0.
f);
359 if (std::abs(up.dot(z)) > 1.
e-6) {
365 void G4OpenInventorViewer::DrawView () {
367 if (!fNeedKernelVisit) KernelVisitDecision();
372 void G4OpenInventorViewer::ShowView () {
373 fInteractorManager -> SecondaryLoop ();
376 void G4OpenInventorViewer::GroupCameraSensorCB(
void* aThis,SoSensor* aSensor){
377 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
379 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
383 if(node->isOfType(SoCamera::getClassTypeId())) {
386 SoCamera* camera = (SoCamera*)node;
387 This->fCameraSensor->detach();
388 This->fCameraSensor->attach(camera);
393 void G4OpenInventorViewer::CameraSensorCB(
void* aThis,SoSensor* aSensor) {
394 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
398 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
400 if(node->isOfType(SoCamera::getClassTypeId())) {
401 SoCamera* camera = (SoCamera*)node;
403 SbVec3f direction, up;
404 lookedAt(camera,direction, up);
405 This->fVP.SetViewpointDirection
406 (
G4Vector3D(-direction[0],-direction[1],-direction[2]));
407 This->fVP.SetUpVector(
G4Vector3D(up[0],up[1],up[2]));
409 SbVec3f pos = camera->position.getValue();
410 SbVec3f target = pos + direction * camera->focalDistance.getValue();
412 This->fVP.SetCurrentTargetPoint(
G4Point3D(target[0],target[1],target[2]));
416 void G4OpenInventorViewer::SelectionCB(
421 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
422 SoNode* node = ((SoFullPath*)aPath)->getTail();
424 if(attHolder && attHolder->
GetAttDefs().size()) {
425 for (
size_t i = 0; i < attHolder->
GetAttDefs().size(); ++i) {
431 G4String cls((
char*)node->getTypeId().getName().getString());
432 G4cout <<
"SoNode : " << node
433 <<
" SoType : " << cls
434 <<
" name : " << name
446 This->fSoSelection->deselectAll();
460 void G4OpenInventorViewer::DrawDetector() {
478 void G4OpenInventorViewer::Escape(){
480 fInteractorManager->RequireExitSecondaryLoop (
OIV_EXIT_CODE);
483 void G4OpenInventorViewer::WritePostScript(
const G4String& aFile) {
484 if(!fGL2PSAction)
return;
485 fGL2PSAction->setFileName(aFile.c_str());
487 if (fGL2PSAction->enableFileWriting()) {
489 fGL2PSAction->disableFileWriting();
493 void G4OpenInventorViewer::WritePixmapPostScript(
const G4String& aFile) {
494 fSoImageWriter->fileName.setValue(aFile.c_str());
496 fSoImageWriter->enable();
498 fSoImageWriter->disable();
499 if(fSoImageWriter->getStatus()) {
500 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
504 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
510 void G4OpenInventorViewer::WriteInventor(
const G4String& aFile) {
513 SbBool genAlternateRep =
TRUE;
515 SbBool binary =
TRUE;
517 if(genAlternateRep==
TRUE) {
519 alternateRepAction.apply(fSoSelection);
522 SoWriteAction writeAction;
523 writeAction.getOutput()->openFile(aFile.c_str());
524 writeAction.getOutput()->setBinary(binary);
525 writeAction.apply(fSoSelection);
526 writeAction.getOutput()->closeFile();
528 if(genAlternateRep==
TRUE) {
530 alternateRepAction.apply(fSoSelection);
543 static void CountTrianglesCB(
546 ,
const SoPrimitiveVertex*
547 ,
const SoPrimitiveVertex*,
548 const SoPrimitiveVertex*)
550 Counter* counter = (Counter*)userData;
551 counter->fTriangles++;
554 static void CountLineSegmentsCB(
557 ,
const SoPrimitiveVertex*
558 ,
const SoPrimitiveVertex*)
560 Counter* counter = (Counter*)userData;
561 counter->fLineSegments++;
564 static void CountPointsCB(
567 ,
const SoPrimitiveVertex*)
569 Counter* counter = (Counter*)userData;
573 void G4OpenInventorViewer::SceneGraphStatistics() {
575 counter.fTriangles = 0;
576 counter.fLineSegments = 0;
579 SoCallbackAction callbackAction;
580 callbackAction.addTriangleCallback
581 (SoShape::getClassTypeId(),CountTrianglesCB,(
void*)&counter);
582 callbackAction.addLineSegmentCallback
583 (SoShape::getClassTypeId(),CountLineSegmentsCB,(
void*)&counter);
584 callbackAction.addPointCallback
585 (SoShape::getClassTypeId(),CountPointsCB,(
void*)&counter);
586 callbackAction.apply(fSoSelection);
589 counterAction.apply(fSoSelection);
590 int nodes = counterAction.
getCount();
593 counterAction.
setType(SoShape::getClassTypeId());
594 counterAction.apply(fSoSelection);
595 int shapes = counterAction.
getCount();
597 G4cout <<
"Number of triangles : " << counter.fTriangles <<
G4endl;
598 G4cout <<
"Number of line segments : " << counter.fLineSegments <<
G4endl;
599 G4cout <<
"Number of points : " << counter.fPoints <<
G4endl;
604 void G4OpenInventorViewer::EraseDetector() {
605 fG4OpenInventorSceneHandler.fDetectorRoot->removeAllChildren();
607 void G4OpenInventorViewer::EraseEvent() {
608 fG4OpenInventorSceneHandler.fTransientRoot->removeAllChildren();
611 void G4OpenInventorViewer::SetPreviewAndFull() {
612 fG4OpenInventorSceneHandler.fPreviewAndFull =
true;
618 void G4OpenInventorViewer::SetPreview() {
619 fG4OpenInventorSceneHandler.fPreviewAndFull =
false;
629 void G4OpenInventorViewer::SetSolid() {
633 switch (existingStyle) {
645 SetViewParameters(vp);
648 void G4OpenInventorViewer::SetWireFrame() {
651 switch (existingStyle) {
663 SetViewParameters(vp);
668 void G4OpenInventorViewer::SetReducedWireFrame(
bool aValue) {
676 switch (existingStyle) {
688 SetViewParameters(vp);
693 void G4OpenInventorViewer::UpdateScene() {
706 if(aTopic==
"controls") {
708 Controls on an Inventor examiner viewer are :\n\
709 - in picking mode (cursor is the upper left arrow)\n\
710 Ctrl + pick a volume : see daughters.\n\
711 Shift + pick a volume : see mother.\n\
712 - in viewing mode (cursor is the hand)\n\
713 Left-button + pointer move : rotate.\n\
714 Ctrl+Left-button + pointer move : pan.\n\
715 Ctrl+Shift+Left-button + pointer move : scale.\n\
716 Middle-button + pointer move : pan.\n\
717 Right-button : popup menu.\n");