27 #ifdef G4VIS_BUILD_OI_DRIVER
32 #include <Inventor/nodes/SoSelection.h>
33 #include <Inventor/nodes/SoShape.h>
34 #include <Inventor/nodes/SoOrthographicCamera.h>
35 #include <Inventor/nodes/SoPerspectiveCamera.h>
36 #include <Inventor/actions/SoCallbackAction.h>
37 #include <Inventor/actions/SoWriteAction.h>
38 #include <Inventor/sensors/SoNodeSensor.h>
55 G4OpenInventorViewer::G4OpenInventorViewer(
56 G4OpenInventorSceneHandler& sceneHandler
58 :
G4VViewer(sceneHandler, sceneHandler.IncrementViewCount(), name)
59 ,fG4OpenInventorSceneHandler(sceneHandler)
60 ,fInteractorManager(0)
64 ,fGroupCameraSensor(0)
67 fNeedKernelVisit =
true;
69 fVP.SetAutoRefresh(
true);
70 fDefaultVP.SetAutoRefresh(
true);
72 fDefaultVP.SetPicking(
true);
80 ((
G4OpenInventor*)fG4OpenInventorSceneHandler.GetGraphicsSystem())->
81 GetInteractorManager();
84 fSoSelection =
new SoSelection;
86 fSoSelection->addSelectionCallback(SelectionCB,
this);
88 fSoSelection->policy = SoSelection::SINGLE;
90 SoGroup* group =
new SoGroup;
91 fSoSelection->addChild(group);
99 SoOrthographicCamera* camera =
new SoOrthographicCamera;
100 camera->viewportMapping.setValue(SoCamera::ADJUST_CAMERA);
102 camera->position.setValue(0,0,10);
103 camera->orientation.setValue(SbRotation(SbVec3f(0,1,0),0));
104 camera->height.setValue(10);
105 camera->nearDistance.setValue(1);
106 camera->farDistance.setValue(100);
107 camera->focalDistance.setValue(10);
108 group->addChild(camera);
111 if(soInput.openFile(
"g4view.iv",
TRUE)) {
112 SoSeparator*
separator = SoDB::readAll(&soInput);
113 if(separator) fSoSelection->addChild(separator);
116 fSoSelection->addChild(fG4OpenInventorSceneHandler.fRoot);
120 fSoImageWriter->fileName.setValue(
"g4out.ps");
121 fSoSelection->addChild(fSoImageWriter);
125 fGroupCameraSensor =
new SoNodeSensor(GroupCameraSensorCB,
this);
126 fGroupCameraSensor->setPriority(0);
127 fGroupCameraSensor->attach(group);
129 fCameraSensor =
new SoNodeSensor(CameraSensorCB,
this);
130 fCameraSensor->setPriority(0);
133 G4OpenInventorViewer::~G4OpenInventorViewer () {
134 fCameraSensor->detach();
135 delete fCameraSensor;
136 fGroupCameraSensor->detach();
137 delete fGroupCameraSensor;
138 fSoSelection->unref();
141 void G4OpenInventorViewer::KernelVisitDecision () {
151 CompareForKernelVisit(fLastVP)) {
168 fVP.GetCBDAlgorithmNumber()) ||
180 fVP.GetDefaultVisAttributes()->
GetColour()) ||
182 fVP.GetDefaultTextVisAttributes()->
GetColour()) ||
193 fVP.GetVisAttributesModifiers().size())
202 if (vp.
GetCBDParameters().size() != fVP.GetCBDParameters().size())
return true;
212 fVP.GetCutawayPlanes ().size ())
return true;
225 void G4OpenInventorViewer::ClearView () {
228 void G4OpenInventorViewer::SetView () {
232 = fSceneHandler.GetScene()->GetStandardTargetPoint()
233 + fVP.GetCurrentTargetPoint ();
234 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
235 if(radius<=0.) radius = 1.;
236 const G4double cameraDistance = fVP.GetCameraDistance (radius);
238 const G4Point3D cameraPosition = target + cameraDistance * direction;
256 SoCamera* camera = GetCamera();
260 camera->position.setValue((
float)cameraPosition.
x(),
261 (float)cameraPosition.
y(),
262 (float)cameraPosition.
z());
264 SbVec3f sbTarget((
float)target.
x(),
267 SbVec3f sbUp((
float)up.
x(),
273 pointAt(camera,sbTarget,sbUp);
280 if(camera->isOfType(SoOrthographicCamera::getClassTypeId())) {
281 if (fVP.GetFieldHalfAngle() == 0.) {
290 }
else if(camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
291 if (fVP.GetFieldHalfAngle() == 0.) {
303 G4OpenInventorViewer::pointAt(SoCamera* camera,
const SbVec3f & targetpoint,
const SbVec3f & upvector)
305 SbVec3f
dir = targetpoint - camera->position.getValue();
306 if (dir.normalize() == 0.0f)
return;
307 lookAt(camera,dir, upvector);
314 G4OpenInventorViewer::lookAt(SoCamera* camera,
const SbVec3f & dir,
const SbVec3f & up)
318 SbVec3f
x = y.cross(z);
341 camera->orientation.setValue(SbRotation(rot));
345 G4OpenInventorViewer::lookedAt(SoCamera* camera,SbVec3f & dir, SbVec3f & up)
347 SbRotation rot = camera->orientation.getValue();
348 SbMatrix mrot; rot.getValue(mrot);
367 up = SbVec3f(0.
f,1.
f,0.
f);
374 void G4OpenInventorViewer::DrawView () {
376 if (!fNeedKernelVisit) KernelVisitDecision();
381 void G4OpenInventorViewer::ShowView () {
382 fInteractorManager -> SecondaryLoop ();
385 void G4OpenInventorViewer::GroupCameraSensorCB(
void* aThis,SoSensor* aSensor){
386 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
388 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
392 if(node->isOfType(SoCamera::getClassTypeId())) {
395 SoCamera* camera = (SoCamera*)node;
396 This->fCameraSensor->detach();
397 This->fCameraSensor->attach(camera);
402 void G4OpenInventorViewer::CameraSensorCB(
void* aThis,SoSensor* aSensor) {
403 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
407 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
409 if(node->isOfType(SoCamera::getClassTypeId())) {
410 SoCamera* camera = (SoCamera*)node;
412 SbVec3f direction, up;
413 lookedAt(camera,direction, up);
414 This->fVP.SetViewpointDirection
415 (
G4Vector3D(-direction[0],-direction[1],-direction[2]));
416 This->fVP.SetUpVector(
G4Vector3D(up[0],up[1],up[2]));
418 SbVec3f
pos = camera->position.getValue();
419 SbVec3f target = pos + direction * camera->focalDistance.getValue();
421 This->fVP.SetCurrentTargetPoint(
G4Point3D(target[0],target[1],target[2]));
425 void G4OpenInventorViewer::SelectionCB(
430 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
431 SoNode* node = ((SoFullPath*)aPath)->getTail();
433 if(attHolder && attHolder->
GetAttDefs().size()) {
434 for (
size_t i = 0; i < attHolder->
GetAttDefs().size(); ++i) {
440 G4String cls((
char*)node->getTypeId().getName().getString());
441 G4cout <<
"SoNode : " << node
442 <<
" SoType : " << cls
443 <<
" name : " << name
455 This->fSoSelection->deselectAll();
469 void G4OpenInventorViewer::DrawDetector() {
487 void G4OpenInventorViewer::Escape(){
489 fInteractorManager->RequireExitSecondaryLoop (
OIV_EXIT_CODE);
492 void G4OpenInventorViewer::WritePostScript(
const G4String& aFile) {
493 if(!fGL2PSAction)
return;
494 fGL2PSAction->setFileName(aFile.c_str());
495 fGL2PSAction->setExportImageFormat(
GL2PS_EPS);
497 fGL2PSAction->setBufferSize(0);
499 if (fGL2PSAction->enableFileWriting()) {
501 fGL2PSAction->disableFileWriting();
503 fGL2PSAction->resetBufferSizeParameters();
506 void G4OpenInventorViewer::WritePDF(
const G4String& aFile) {
507 if(!fGL2PSAction)
return;
508 fGL2PSAction->setFileName(aFile.c_str());
509 fGL2PSAction->setExportImageFormat(
GL2PS_PDF);
511 fGL2PSAction->setBufferSize(0);
513 if (fGL2PSAction->enableFileWriting()) {
515 fGL2PSAction->disableFileWriting();
517 fGL2PSAction->resetBufferSizeParameters();
520 void G4OpenInventorViewer::WritePixmapPostScript(
const G4String& aFile) {
521 fSoImageWriter->fileName.setValue(aFile.c_str());
523 fSoImageWriter->enable();
525 fSoImageWriter->disable();
526 if(fSoImageWriter->getStatus()) {
527 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
531 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
537 void G4OpenInventorViewer::WriteInventor(
const G4String& aFile) {
540 SbBool genAlternateRep =
TRUE;
542 SbBool binary =
TRUE;
544 if(genAlternateRep==
TRUE) {
546 alternateRepAction.apply(fSoSelection);
549 SoWriteAction writeAction;
550 writeAction.getOutput()->openFile(aFile.c_str());
551 writeAction.getOutput()->setBinary(binary);
552 writeAction.apply(fSoSelection);
553 writeAction.getOutput()->closeFile();
555 if(genAlternateRep==
TRUE) {
557 alternateRepAction.apply(fSoSelection);
570 static void CountTrianglesCB(
573 ,
const SoPrimitiveVertex*
574 ,
const SoPrimitiveVertex*,
575 const SoPrimitiveVertex*)
577 Counter* counter = (Counter*)userData;
578 counter->fTriangles++;
581 static void CountLineSegmentsCB(
584 ,
const SoPrimitiveVertex*
585 ,
const SoPrimitiveVertex*)
587 Counter* counter = (Counter*)userData;
588 counter->fLineSegments++;
591 static void CountPointsCB(
594 ,
const SoPrimitiveVertex*)
596 Counter* counter = (Counter*)userData;
600 void G4OpenInventorViewer::SceneGraphStatistics() {
602 counter.fTriangles = 0;
603 counter.fLineSegments = 0;
606 SoCallbackAction callbackAction;
607 callbackAction.addTriangleCallback
608 (SoShape::getClassTypeId(),CountTrianglesCB,(
void*)&counter);
609 callbackAction.addLineSegmentCallback
610 (SoShape::getClassTypeId(),CountLineSegmentsCB,(
void*)&counter);
611 callbackAction.addPointCallback
612 (SoShape::getClassTypeId(),CountPointsCB,(
void*)&counter);
613 callbackAction.apply(fSoSelection);
616 counterAction.apply(fSoSelection);
617 int nodes = counterAction.
getCount();
620 counterAction.
setType(SoShape::getClassTypeId());
621 counterAction.apply(fSoSelection);
622 int shapes = counterAction.
getCount();
624 G4cout <<
"Number of triangles : " << counter.fTriangles <<
G4endl;
625 G4cout <<
"Number of line segments : " << counter.fLineSegments <<
G4endl;
626 G4cout <<
"Number of points : " << counter.fPoints <<
G4endl;
631 void G4OpenInventorViewer::EraseDetector() {
632 fG4OpenInventorSceneHandler.fDetectorRoot->removeAllChildren();
634 void G4OpenInventorViewer::EraseEvent() {
635 fG4OpenInventorSceneHandler.fTransientRoot->removeAllChildren();
638 void G4OpenInventorViewer::SetPreviewAndFull() {
639 fG4OpenInventorSceneHandler.fPreviewAndFull =
true;
645 void G4OpenInventorViewer::SetPreview() {
646 fG4OpenInventorSceneHandler.fPreviewAndFull =
false;
656 void G4OpenInventorViewer::SetSolid() {
660 switch (existingStyle) {
675 SetViewParameters(vp);
678 void G4OpenInventorViewer::SetWireFrame() {
681 switch (existingStyle) {
696 SetViewParameters(vp);
701 void G4OpenInventorViewer::SetReducedWireFrame(
bool aValue) {
709 switch (existingStyle) {
724 SetViewParameters(vp);
729 void G4OpenInventorViewer::UpdateScene() {
742 if(aTopic==
"controls") {
744 Controls on an Inventor examiner viewer are :\n\
745 - in picking mode (cursor is the upper left arrow)\n\
746 Ctrl + pick a volume : see daughters.\n\
747 Shift + pick a volume : see mother.\n\
748 - in viewing mode (cursor is the hand)\n\
749 Left-button + pointer move : rotate.\n\
750 Ctrl+Left-button + pointer move : pan.\n\
751 Ctrl+Shift+Left-button + pointer move : scale.\n\
752 Middle-button + pointer move : pan.\n\
753 Right-button : popup menu.\n");