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>
55G4OpenInventorViewer::G4OpenInventorViewer(
56 G4OpenInventorSceneHandler& sceneHandler
58:
G4VViewer(sceneHandler, sceneHandler.IncrementViewCount(),
name)
59,fG4OpenInventorSceneHandler(sceneHandler)
67 fVP.SetAutoRefresh(
true);
68 fDefaultVP.SetAutoRefresh(
true);
70 fDefaultVP.SetPicking(
true);
78 ((
G4OpenInventor*)fG4OpenInventorSceneHandler.GetGraphicsSystem())->
79 GetInteractorManager();
82 fSoSelection =
new SoSelection;
84 fSoSelection->addSelectionCallback(SelectionCB,
this);
86 fSoSelection->policy = SoSelection::SINGLE;
88 SoGroup* group =
new SoGroup;
89 fSoSelection->addChild(group);
97 SoOrthographicCamera* camera =
new SoOrthographicCamera;
98 camera->viewportMapping.setValue(SoCamera::ADJUST_CAMERA);
100 camera->position.setValue(0,0,10);
101 camera->orientation.setValue(SbRotation(SbVec3f(0,1,0),0));
102 camera->height.setValue(10);
103 camera->nearDistance.setValue(1);
104 camera->farDistance.setValue(100);
105 camera->focalDistance.setValue(10);
106 group->addChild(camera);
109 if(soInput.openFile(
"g4view.iv",
TRUE)) {
110 SoSeparator* separator = SoDB::readAll(&soInput);
111 if(separator) fSoSelection->addChild(separator);
114 fSoSelection->addChild(fG4OpenInventorSceneHandler.fRoot);
118 fSoImageWriter->fileName.setValue(
"g4out.ps");
119 fSoSelection->addChild(fSoImageWriter);
123 fGroupCameraSensor =
new SoNodeSensor(GroupCameraSensorCB,
this);
124 fGroupCameraSensor->setPriority(0);
125 fGroupCameraSensor->attach(group);
127 fCameraSensor =
new SoNodeSensor(CameraSensorCB,
this);
128 fCameraSensor->setPriority(0);
131G4OpenInventorViewer::~G4OpenInventorViewer () {
132 fCameraSensor->detach();
133 delete fCameraSensor;
134 fGroupCameraSensor->detach();
135 delete fGroupCameraSensor;
136 fSoSelection->unref();
139void G4OpenInventorViewer::KernelVisitDecision () {
149 CompareForKernelVisit(fLastVP)) {
165 fVP.GetCBDAlgorithmNumber()) ||
179 fVP.GetDefaultVisAttributes()->
GetColour()) ||
181 fVP.GetDefaultTextVisAttributes()->
GetColour()) ||
192 fVP.GetVisAttributesModifiers().size())
201 if (vp.
GetCBDParameters().size() != fVP.GetCBDParameters().size())
return true;
211 fVP.GetCutawayPlanes ().size ())
return true;
224void G4OpenInventorViewer::ClearView () {
227void G4OpenInventorViewer::SetView () {
231 = fSceneHandler.GetScene()->GetStandardTargetPoint()
232 + fVP.GetCurrentTargetPoint ();
233 G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
234 if(radius<=0.) radius = 1.;
235 const G4double cameraDistance = fVP.GetCameraDistance (radius);
237 const G4Point3D cameraPosition = target + cameraDistance * direction;
255 SoCamera* camera = GetCamera();
259 camera->position.setValue((
float)cameraPosition.
x(),
260 (
float)cameraPosition.
y(),
261 (
float)cameraPosition.
z());
263 SbVec3f sbTarget((
float)target.
x(),
266 SbVec3f sbUp((
float)up.
x(),
272 pointAt(camera,sbTarget,sbUp);
279 if(camera->isOfType(SoOrthographicCamera::getClassTypeId())) {
280 if (fVP.GetFieldHalfAngle() == 0.) {
289 }
else if(camera->isOfType(SoPerspectiveCamera::getClassTypeId())) {
290 if (fVP.GetFieldHalfAngle() == 0.) {
302G4OpenInventorViewer::pointAt(SoCamera* camera,
const SbVec3f & targetpoint,
const SbVec3f & upvector)
304 SbVec3f dir = targetpoint - camera->position.getValue();
305 if (dir.normalize() == 0.0f)
return;
306 lookAt(camera,dir, upvector);
313G4OpenInventorViewer::lookAt(SoCamera* camera,
const SbVec3f & dir,
const SbVec3f & up)
317 SbVec3f x = y.cross(z);
327 SbMatrix rot = SbMatrix::identity();
340 camera->orientation.setValue(SbRotation(rot));
344G4OpenInventorViewer::lookedAt(SoCamera* camera,SbVec3f & dir, SbVec3f & up)
346 SbRotation rot = camera->orientation.getValue();
347 SbMatrix mrot; rot.getValue(mrot);
366 up = SbVec3f(0.f,1.f,0.f);
367 if (std::abs(up.dot(z)) > 1.e-6) {
373void G4OpenInventorViewer::DrawView () {
375 if (!fNeedKernelVisit) KernelVisitDecision();
381void G4OpenInventorViewer::ShowView () {
382 fInteractorManager -> SecondaryLoop ();
385void 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);
402void 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]));
425void 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();
469void G4OpenInventorViewer::DrawDetector() {
487void G4OpenInventorViewer::Escape(){
489 fInteractorManager->RequireExitSecondaryLoop (
OIV_EXIT_CODE);
492void 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();
506void 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();
520void 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())
537void 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);
570static void CountTrianglesCB(
573,
const SoPrimitiveVertex*
574,
const SoPrimitiveVertex*,
575const SoPrimitiveVertex*)
577 Counter* counter = (Counter*)
userData;
578 counter->fTriangles++;
581static void CountLineSegmentsCB(
584,
const SoPrimitiveVertex*
585,
const SoPrimitiveVertex*)
587 Counter* counter = (Counter*)
userData;
588 counter->fLineSegments++;
591static void CountPointsCB(
594,
const SoPrimitiveVertex*)
596 Counter* counter = (Counter*)
userData;
600void 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;
631void G4OpenInventorViewer::EraseDetector() {
632 fG4OpenInventorSceneHandler.fDetectorRoot->removeAllChildren();
634void G4OpenInventorViewer::EraseEvent() {
635 fG4OpenInventorSceneHandler.fTransientRoot->removeAllChildren();
638void G4OpenInventorViewer::SetPreviewAndFull() {
639 fG4OpenInventorSceneHandler.fPreviewAndFull =
true;
645void G4OpenInventorViewer::SetPreview() {
646 fG4OpenInventorSceneHandler.fPreviewAndFull =
false;
656void G4OpenInventorViewer::SetSolid() {
660 switch (existingStyle) {
675 SetViewParameters(vp);
678void G4OpenInventorViewer::SetWireFrame() {
681 switch (existingStyle) {
696 SetViewParameters(vp);
701void G4OpenInventorViewer::SetReducedWireFrame(
bool aValue) {
709 switch (existingStyle) {
724 SetViewParameters(vp);
729void G4OpenInventorViewer::UpdateScene() {
742 if(aTopic==
"controls") {
744Controls 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");
HepGeom::Point3D< G4double > G4Point3D
HepGeom::Vector3D< G4double > G4Vector3D
G4GLOB_DLL std::ostream G4cout
const std::vector< const std::vector< G4AttValue > * > & GetAttValues() const
const std::vector< const std::map< G4String, G4AttDef > * > & GetAttDefs() const
static G4bool GetColour(const G4String &key, G4Colour &result)
const std::vector< G4ModelingParameters::VisAttributesModifier > & GetVisAttributesModifiers() const
const G4Vector3D & GetScaleFactor() const
G4int GetNoOfSides() const
G4double GetExplodeFactor() const
G4int GetNumberOfCloudPoints() const
G4bool IsMarkerNotHidden() const
G4double GetGlobalLineWidthScale() const
void SetDrawingStyle(G4ViewParameters::DrawingStyle style)
const G4Colour & GetBackgroundColour() const
const G4VisAttributes * GetDefaultTextVisAttributes() const
const std::vector< G4double > & GetCBDParameters() const
G4int GetCBDAlgorithmNumber() const
G4double GetGlobalMarkerScale() const
G4bool IsCullingInvisible() const
const G4VisAttributes * GetDefaultVisAttributes() const
const G4Planes & GetCutawayPlanes() const
G4bool IsDensityCulling() const
G4double GetVisibleDensity() const
void SetAuxEdgeVisible(G4bool)
G4bool IsCullingCovered() const
const G4Plane3D & GetSectionPlane() const
DrawingStyle GetDrawingStyle() const
G4bool IsAuxEdgeVisible() const
const G4Colour & GetColour() const
BasicVector3D< T > unit() const
void setType(const SoType, SbBool=TRUE)
const char * name(G4int ptype)