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)) {
178 fVP.GetDefaultVisAttributes()->
GetColour()) ||
180 fVP.GetDefaultTextVisAttributes()->
GetColour()) ||
191 fVP.GetVisAttributesModifiers().size())
205 fVP.GetCutawayPlanes ().size ())
return true;
218 void G4OpenInventorViewer::ClearView () {
221 void G4OpenInventorViewer::SetView () {
225 = fSceneHandler.GetScene()->GetStandardTargetPoint()
226 + fVP.GetCurrentTargetPoint ();
227 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
228 if(radius<=0.) radius = 1.;
229 const G4double cameraDistance = fVP.GetCameraDistance (radius);
231 const G4Point3D cameraPosition = target + cameraDistance * direction;
249 SoCamera* camera = GetCamera();
253 camera->position.setValue((
float)cameraPosition.
x(),
254 (float)cameraPosition.
y(),
255 (float)cameraPosition.
z());
257 SbVec3f sbTarget((
float)target.
x(),
260 SbVec3f sbUp((
float)up.
x(),
266 pointAt(camera,sbTarget,sbUp);
273 if(camera->isOfType(SoOrthographicCamera::getClassTypeId())) {
274 if (fVP.GetFieldHalfAngle() == 0.) {
283 }
else if(camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
284 if (fVP.GetFieldHalfAngle() == 0.) {
296 G4OpenInventorViewer::pointAt(SoCamera* camera,
const SbVec3f & targetpoint,
const SbVec3f & upvector)
298 SbVec3f dir = targetpoint - camera->position.getValue();
299 if (dir.normalize() == 0.0f)
return;
300 lookAt(camera,dir, upvector);
307 G4OpenInventorViewer::lookAt(SoCamera* camera,
const SbVec3f & dir,
const SbVec3f & up)
311 SbVec3f
x = y.cross(z);
321 SbMatrix rot = SbMatrix::identity();
334 camera->orientation.setValue(SbRotation(rot));
338 G4OpenInventorViewer::lookedAt(SoCamera* camera,SbVec3f & dir, SbVec3f & up)
340 SbRotation rot = camera->orientation.getValue();
341 SbMatrix mrot; rot.getValue(mrot);
360 up = SbVec3f(0.f,1.f,0.f);
361 if (std::abs(up.dot(z)) > 1.e-6) {
367 void G4OpenInventorViewer::DrawView () {
369 if (!fNeedKernelVisit) KernelVisitDecision();
374 void G4OpenInventorViewer::ShowView () {
375 fInteractorManager -> SecondaryLoop ();
378 void G4OpenInventorViewer::GroupCameraSensorCB(
void* aThis,SoSensor* aSensor){
379 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
381 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
385 if(node->isOfType(SoCamera::getClassTypeId())) {
388 SoCamera* camera = (SoCamera*)node;
389 This->fCameraSensor->detach();
390 This->fCameraSensor->attach(camera);
395 void G4OpenInventorViewer::CameraSensorCB(
void* aThis,SoSensor* aSensor) {
396 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
400 SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode();
402 if(node->isOfType(SoCamera::getClassTypeId())) {
403 SoCamera* camera = (SoCamera*)node;
405 SbVec3f direction, up;
406 lookedAt(camera,direction, up);
407 This->fVP.SetViewpointDirection
408 (
G4Vector3D(-direction[0],-direction[1],-direction[2]));
409 This->fVP.SetUpVector(
G4Vector3D(up[0],up[1],up[2]));
411 SbVec3f
pos = camera->position.getValue();
412 SbVec3f target = pos + direction * camera->focalDistance.getValue();
414 This->fVP.SetCurrentTargetPoint(
G4Point3D(target[0],target[1],target[2]));
418 void G4OpenInventorViewer::SelectionCB(
423 G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis;
424 SoNode* node = ((SoFullPath*)aPath)->getTail();
426 if(attHolder && attHolder->
GetAttDefs().size()) {
427 for (
size_t i = 0; i < attHolder->
GetAttDefs().size(); ++i) {
433 G4String cls((
char*)node->getTypeId().getName().getString());
434 G4cout <<
"SoNode : " << node
435 <<
" SoType : " << cls
436 <<
" name : " << name
448 This->fSoSelection->deselectAll();
462 void G4OpenInventorViewer::DrawDetector() {
480 void G4OpenInventorViewer::Escape(){
482 fInteractorManager->RequireExitSecondaryLoop (
OIV_EXIT_CODE);
485 void G4OpenInventorViewer::WritePostScript(
const G4String& aFile) {
486 if(!fGL2PSAction)
return;
487 fGL2PSAction->setFileName(aFile.c_str());
488 fGL2PSAction->setExportImageFormat(
GL2PS_EPS);
490 fGL2PSAction->setBufferSize(0);
492 if (fGL2PSAction->enableFileWriting()) {
494 fGL2PSAction->disableFileWriting();
496 fGL2PSAction->resetBufferSizeParameters();
499 void G4OpenInventorViewer::WritePDF(
const G4String& aFile) {
500 if(!fGL2PSAction)
return;
501 fGL2PSAction->setFileName(aFile.c_str());
502 fGL2PSAction->setExportImageFormat(
GL2PS_PDF);
504 fGL2PSAction->setBufferSize(0);
506 if (fGL2PSAction->enableFileWriting()) {
508 fGL2PSAction->disableFileWriting();
510 fGL2PSAction->resetBufferSizeParameters();
513 void G4OpenInventorViewer::WritePixmapPostScript(
const G4String& aFile) {
514 fSoImageWriter->fileName.setValue(aFile.c_str());
516 fSoImageWriter->enable();
518 fSoImageWriter->disable();
519 if(fSoImageWriter->getStatus()) {
520 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
524 G4cout <<
G4String(fSoImageWriter->fileName.getValue().getString())
530 void G4OpenInventorViewer::WriteInventor(
const G4String& aFile) {
533 SbBool genAlternateRep =
TRUE;
535 SbBool binary =
TRUE;
537 if(genAlternateRep==
TRUE) {
539 alternateRepAction.apply(fSoSelection);
542 SoWriteAction writeAction;
543 writeAction.getOutput()->openFile(aFile.c_str());
544 writeAction.getOutput()->setBinary(binary);
545 writeAction.apply(fSoSelection);
546 writeAction.getOutput()->closeFile();
548 if(genAlternateRep==
TRUE) {
550 alternateRepAction.apply(fSoSelection);
563 static void CountTrianglesCB(
566 ,
const SoPrimitiveVertex*
567 ,
const SoPrimitiveVertex*,
568 const SoPrimitiveVertex*)
570 Counter* counter = (Counter*)userData;
571 counter->fTriangles++;
574 static void CountLineSegmentsCB(
577 ,
const SoPrimitiveVertex*
578 ,
const SoPrimitiveVertex*)
580 Counter* counter = (Counter*)userData;
581 counter->fLineSegments++;
584 static void CountPointsCB(
587 ,
const SoPrimitiveVertex*)
589 Counter* counter = (Counter*)userData;
593 void G4OpenInventorViewer::SceneGraphStatistics() {
595 counter.fTriangles = 0;
596 counter.fLineSegments = 0;
599 SoCallbackAction callbackAction;
600 callbackAction.addTriangleCallback
601 (SoShape::getClassTypeId(),CountTrianglesCB,(
void*)&counter);
602 callbackAction.addLineSegmentCallback
603 (SoShape::getClassTypeId(),CountLineSegmentsCB,(
void*)&counter);
604 callbackAction.addPointCallback
605 (SoShape::getClassTypeId(),CountPointsCB,(
void*)&counter);
606 callbackAction.apply(fSoSelection);
609 counterAction.apply(fSoSelection);
610 int nodes = counterAction.
getCount();
613 counterAction.
setType(SoShape::getClassTypeId());
614 counterAction.apply(fSoSelection);
615 int shapes = counterAction.
getCount();
617 G4cout <<
"Number of triangles : " << counter.fTriangles <<
G4endl;
618 G4cout <<
"Number of line segments : " << counter.fLineSegments <<
G4endl;
619 G4cout <<
"Number of points : " << counter.fPoints <<
G4endl;
624 void G4OpenInventorViewer::EraseDetector() {
625 fG4OpenInventorSceneHandler.fDetectorRoot->removeAllChildren();
627 void G4OpenInventorViewer::EraseEvent() {
628 fG4OpenInventorSceneHandler.fTransientRoot->removeAllChildren();
631 void G4OpenInventorViewer::SetPreviewAndFull() {
632 fG4OpenInventorSceneHandler.fPreviewAndFull =
true;
638 void G4OpenInventorViewer::SetPreview() {
639 fG4OpenInventorSceneHandler.fPreviewAndFull =
false;
649 void G4OpenInventorViewer::SetSolid() {
653 switch (existingStyle) {
665 SetViewParameters(vp);
668 void G4OpenInventorViewer::SetWireFrame() {
671 switch (existingStyle) {
683 SetViewParameters(vp);
688 void G4OpenInventorViewer::SetReducedWireFrame(
bool aValue) {
696 switch (existingStyle) {
708 SetViewParameters(vp);
713 void G4OpenInventorViewer::UpdateScene() {
726 if(aTopic==
"controls") {
728 Controls on an Inventor examiner viewer are :\n\
729 - in picking mode (cursor is the upper left arrow)\n\
730 Ctrl + pick a volume : see daughters.\n\
731 Shift + pick a volume : see mother.\n\
732 - in viewing mode (cursor is the hand)\n\
733 Left-button + pointer move : rotate.\n\
734 Ctrl+Left-button + pointer move : pan.\n\
735 Ctrl+Shift+Left-button + pointer move : scale.\n\
736 Middle-button + pointer move : pan.\n\
737 Right-button : popup menu.\n");
void setType(const SoType, SbBool=TRUE)
const G4Colour & GetBackgroundColour() const
G4double GetVisibleDensity() const
void SetAuxEdgeVisible(G4bool)
G4bool IsCullingInvisible() const
G4double GetExplodeFactor() const
BasicVector3D< T > unit() 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