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())->
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)) {
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);
328 SbMatrix rot = SbMatrix::identity();
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);
368 if (std::abs(up.dot(z)) > 1.
e-6) {
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) {
672 SetViewParameters(vp);
675 void G4OpenInventorViewer::SetWireFrame() {
678 switch (existingStyle) {
690 SetViewParameters(vp);
695 void G4OpenInventorViewer::SetReducedWireFrame(
bool aValue) {
703 switch (existingStyle) {
715 SetViewParameters(vp);
720 void G4OpenInventorViewer::UpdateScene() {
733 if(aTopic==
"controls") {
735 Controls on an Inventor examiner viewer are :\n\
736 - in picking mode (cursor is the upper left arrow)\n\
737 Ctrl + pick a volume : see daughters.\n\
738 Shift + pick a volume : see mother.\n\
739 - in viewing mode (cursor is the hand)\n\
740 Left-button + pointer move : rotate.\n\
741 Ctrl+Left-button + pointer move : pan.\n\
742 Ctrl+Shift+Left-button + pointer move : scale.\n\
743 Middle-button + pointer move : pan.\n\
744 Right-button : popup menu.\n");
const std::vector< G4ModelingParameters::VisAttributesModifier > & GetVisAttributesModifiers() const
G4bool IsMarkerNotHidden() const
G4double GetExplodeFactor() const
static const G4double pos
void SetDrawingStyle(G4ViewParameters::DrawingStyle style)
G4bool IsDensityCulling() const
const G4Plane3D & GetSectionPlane() const
BasicVector3D< T > unit() const
G4bool IsAuxEdgeVisible() const
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
const std::vector< G4double > & GetCBDParameters() const
const G4Vector3D & GetScaleFactor() const
void SetAuxEdgeVisible(G4bool)
G4int GetCBDAlgorithmNumber() const
const G4VisAttributes * GetDefaultVisAttributes() const
G4int GetNoOfSides() const
G4bool IsCullingInvisible() const
HepGeom::Vector3D< G4double > G4Vector3D
G4VInteractorManager * GetInteractorManager()
void setType(const SoType, SbBool=TRUE)
DrawingStyle GetDrawingStyle() const
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
const G4Planes & GetCutawayPlanes() const
const G4Colour & GetBackgroundColour() const
G4GLOB_DLL std::ostream G4cout
const G4VisAttributes * GetDefaultTextVisAttributes() const
G4bool IsCullingCovered() const
G4double GetVisibleDensity() const
HepGeom::Point3D< G4double > G4Point3D
const G4Colour & GetColour() const
static G4bool GetColour(const G4String &key, G4Colour &result)