[529] | 1 | // |
---|
| 2 | // ******************************************************************** |
---|
| 3 | // * License and Disclaimer * |
---|
| 4 | // * * |
---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of * |
---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and * |
---|
| 7 | // * conditions of the Geant4 Software License, included in the file * |
---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These * |
---|
| 9 | // * include a list of copyright holders. * |
---|
| 10 | // * * |
---|
| 11 | // * Neither the authors of this software system, nor their employing * |
---|
| 12 | // * institutes,nor the agencies providing financial support for this * |
---|
| 13 | // * work make any representation or warranty, express or implied, * |
---|
| 14 | // * regarding this software system or assume any liability for its * |
---|
| 15 | // * use. Please see the license in the file LICENSE and URL above * |
---|
| 16 | // * for the full disclaimer and the limitation of liability. * |
---|
| 17 | // * * |
---|
| 18 | // * This code implementation is the result of the scientific and * |
---|
| 19 | // * technical work of the GEANT4 collaboration. * |
---|
| 20 | // * By using, copying, modifying or distributing the software (or * |
---|
| 21 | // * any work based on the software) you agree to acknowledge its * |
---|
| 22 | // * use in resulting scientific publications, and indicate your * |
---|
| 23 | // * acceptance of all terms of the Geant4 Software license. * |
---|
| 24 | // ******************************************************************** |
---|
| 25 | // |
---|
[924] | 26 | // $Id: G4OpenInventorViewer.cc,v 1.60 2009/02/16 15:31:05 lgarnier Exp $ |
---|
| 27 | // GEANT4 tag $Name: $ |
---|
[529] | 28 | |
---|
| 29 | #ifdef G4VIS_BUILD_OI_DRIVER |
---|
| 30 | |
---|
| 31 | // this : |
---|
| 32 | #include "G4OpenInventorViewer.hh" |
---|
| 33 | |
---|
| 34 | #include <Inventor/nodes/SoSelection.h> |
---|
| 35 | #include <Inventor/nodes/SoShape.h> |
---|
| 36 | #include <Inventor/nodes/SoOrthographicCamera.h> |
---|
| 37 | #include <Inventor/nodes/SoPerspectiveCamera.h> |
---|
| 38 | #include <Inventor/actions/SoCallbackAction.h> |
---|
| 39 | #include <Inventor/actions/SoWriteAction.h> |
---|
| 40 | #include <Inventor/sensors/SoNodeSensor.h> |
---|
| 41 | |
---|
| 42 | #include "HEPVis/nodes/SoImageWriter.h" |
---|
| 43 | #include "HEPVis/actions/SoGL2PSAction.h" |
---|
| 44 | #include "HEPVis/actions/SoCounterAction.h" |
---|
| 45 | #include "HEPVis/actions/SoAlternateRepAction.h" |
---|
| 46 | |
---|
| 47 | #include "G4OpenInventor.hh" |
---|
| 48 | #include "G4OpenInventorSceneHandler.hh" |
---|
| 49 | #include "G4VInteractorManager.hh" |
---|
| 50 | #include "G4Scene.hh" |
---|
| 51 | #include "Geant4_SoPolyhedron.h" |
---|
[924] | 52 | #include "G4AttValue.hh" |
---|
| 53 | #include "G4AttDef.hh" |
---|
[529] | 54 | #include "G4AttCheck.hh" |
---|
| 55 | #include "G4AttHolder.hh" |
---|
| 56 | |
---|
| 57 | G4OpenInventorViewer::G4OpenInventorViewer( |
---|
| 58 | G4OpenInventorSceneHandler& sceneHandler |
---|
| 59 | ,const G4String& name) |
---|
| 60 | :G4VViewer(sceneHandler, sceneHandler.IncrementViewCount(), name) |
---|
| 61 | ,fG4OpenInventorSceneHandler(sceneHandler) |
---|
| 62 | ,fInteractorManager(0) |
---|
| 63 | ,fSoSelection(0) |
---|
| 64 | ,fSoImageWriter(0) |
---|
| 65 | ,fGL2PSAction(0) //To be set be suclass. |
---|
| 66 | ,fGroupCameraSensor(0) |
---|
| 67 | ,fCameraSensor(0) |
---|
| 68 | { |
---|
| 69 | fNeedKernelVisit = true; //?? Temporary, until KernelVisitDecision fixed. |
---|
| 70 | |
---|
[593] | 71 | fVP.SetAutoRefresh(true); |
---|
[529] | 72 | fDefaultVP.SetAutoRefresh(true); |
---|
[593] | 73 | fVP.SetPicking(true); |
---|
| 74 | fDefaultVP.SetPicking(true); |
---|
[529] | 75 | |
---|
| 76 | //FIXME : G.Barrand : not convinced that we have to rm culling. |
---|
| 77 | // For viewing of all objects by default : |
---|
| 78 | //fDefaultVP.SetCulling(false); |
---|
| 79 | //fVP.SetCulling(false); |
---|
| 80 | |
---|
| 81 | fInteractorManager = |
---|
| 82 | ((G4OpenInventor*)fG4OpenInventorSceneHandler.GetGraphicsSystem())-> |
---|
| 83 | GetInteractorManager(); |
---|
| 84 | |
---|
| 85 | // Main user scene graph root sent to the viewers. |
---|
| 86 | fSoSelection = new SoSelection; |
---|
| 87 | fSoSelection->ref(); |
---|
| 88 | fSoSelection->addSelectionCallback(SelectionCB,this); |
---|
| 89 | //fSoSelection->addDeselectionCallback(DeselectionCB,this); |
---|
| 90 | fSoSelection->policy = SoSelection::SINGLE; |
---|
| 91 | |
---|
| 92 | SoGroup* group = new SoGroup; |
---|
| 93 | fSoSelection->addChild(group); |
---|
| 94 | |
---|
| 95 | // Have a camera under fSoSelection in order |
---|
| 96 | // that the below SceneGraphSensor be notifed |
---|
| 97 | // when the viewer changes the camera type. |
---|
| 98 | // But we put the camera under a SoGroup so that |
---|
| 99 | // the SceneGraphSensor be not triggered at each change |
---|
| 100 | // under the fG4OpenInventorSceneHandler.fRoot. |
---|
| 101 | SoOrthographicCamera* camera = new SoOrthographicCamera; |
---|
| 102 | camera->viewportMapping.setValue(SoCamera::ADJUST_CAMERA); |
---|
| 103 | //camera->aspectRatio.setValue(10); |
---|
| 104 | camera->position.setValue(0,0,10); |
---|
| 105 | camera->orientation.setValue(SbRotation(SbVec3f(0,1,0),0)); |
---|
| 106 | camera->height.setValue(10); |
---|
| 107 | camera->nearDistance.setValue(1); |
---|
| 108 | camera->farDistance.setValue(100); |
---|
| 109 | camera->focalDistance.setValue(10); |
---|
| 110 | group->addChild(camera); |
---|
| 111 | |
---|
| 112 | {SoInput soInput; |
---|
| 113 | if(soInput.openFile("g4view.iv",TRUE)) { |
---|
| 114 | SoSeparator* separator = SoDB::readAll(&soInput); |
---|
| 115 | if(separator) fSoSelection->addChild(separator); |
---|
| 116 | }} |
---|
| 117 | |
---|
| 118 | fSoSelection->addChild(fG4OpenInventorSceneHandler.fRoot); |
---|
| 119 | |
---|
| 120 | // SoImageWriter should be the last. |
---|
| 121 | fSoImageWriter = new SoImageWriter(); |
---|
| 122 | fSoImageWriter->fileName.setValue("g4out.ps"); |
---|
| 123 | fSoSelection->addChild(fSoImageWriter); |
---|
| 124 | |
---|
| 125 | // Sensors : |
---|
| 126 | // To detect that the viewer had changed the camera type : |
---|
| 127 | fGroupCameraSensor = new SoNodeSensor(GroupCameraSensorCB,this); |
---|
| 128 | fGroupCameraSensor->setPriority(0);//Needed in order to do getTriggerNode() |
---|
| 129 | fGroupCameraSensor->attach(group); |
---|
| 130 | |
---|
| 131 | fCameraSensor = new SoNodeSensor(CameraSensorCB,this); |
---|
| 132 | fCameraSensor->setPriority(0);//Needed in order to do getTriggerNode() |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | G4OpenInventorViewer::~G4OpenInventorViewer () { |
---|
| 136 | fCameraSensor->detach(); |
---|
| 137 | delete fCameraSensor; |
---|
| 138 | fGroupCameraSensor->detach(); |
---|
| 139 | delete fGroupCameraSensor; |
---|
| 140 | fSoSelection->unref(); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | void G4OpenInventorViewer::KernelVisitDecision () { |
---|
| 144 | |
---|
| 145 | // If there's a significant difference with the last view parameters |
---|
| 146 | // of either the scene handler or this viewer, trigger a rebuild. |
---|
| 147 | |
---|
| 148 | if ( |
---|
| 149 | //??fG4OpenInventorSceneHandler.fPODLList.size() == 0 || |
---|
| 150 | // We need a test for empty scene graph, such as |
---|
| 151 | // staticRoot.size() or something?????????? See temporary fix |
---|
| 152 | // in contructor. (John Allison Aug 2001) |
---|
| 153 | CompareForKernelVisit(fLastVP)) { |
---|
| 154 | NeedKernelVisit (); |
---|
| 155 | } |
---|
| 156 | fLastVP = fVP; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | G4bool G4OpenInventorViewer::CompareForKernelVisit(G4ViewParameters& vp) { |
---|
| 160 | |
---|
| 161 | if ( |
---|
| 162 | (vp.GetDrawingStyle () != fVP.GetDrawingStyle ()) || |
---|
| 163 | (vp.IsAuxEdgeVisible () != fVP.IsAuxEdgeVisible ()) || |
---|
| 164 | (vp.GetRepStyle () != fVP.GetRepStyle ()) || |
---|
| 165 | (vp.IsCulling () != fVP.IsCulling ()) || |
---|
| 166 | (vp.IsCullingInvisible () != fVP.IsCullingInvisible ()) || |
---|
| 167 | (vp.IsDensityCulling () != fVP.IsDensityCulling ()) || |
---|
| 168 | (vp.IsCullingCovered () != fVP.IsCullingCovered ()) || |
---|
| 169 | (vp.IsSection () != fVP.IsSection ()) || |
---|
| 170 | (vp.IsCutaway () != fVP.IsCutaway ()) || |
---|
| 171 | // This assumes use of generic clipping (sectioning, slicing, |
---|
| 172 | // DCUT, cutaway). If a decision is made to implement locally, |
---|
| 173 | // this will need changing. See G4OpenGLViewer::SetView, |
---|
| 174 | // G4OpenGLStoredViewer.cc::CompareForKernelVisit and |
---|
| 175 | // G4OpenGLStoredSceneHander::CreateSection/CutawayPolyhedron. |
---|
| 176 | (vp.IsExplode () != fVP.IsExplode ()) || |
---|
| 177 | (vp.GetNoOfSides () != fVP.GetNoOfSides ()) || |
---|
| 178 | (vp.IsMarkerNotHidden () != fVP.IsMarkerNotHidden ()) || |
---|
[593] | 179 | (vp.GetBackgroundColour ()!= fVP.GetBackgroundColour ())|| |
---|
| 180 | (vp.IsPicking () != fVP.IsPicking ()) || |
---|
| 181 | // Scaling for Open Inventor is done by the scene handler so it |
---|
| 182 | // needs a kernel visit. (In this respect, it differs from the |
---|
| 183 | // OpenGL drivers, where it's done in SetView.) |
---|
| 184 | (vp.GetScaleFactor () != fVP.GetScaleFactor ()) |
---|
| 185 | ) |
---|
| 186 | return true; |
---|
| 187 | |
---|
[529] | 188 | if (vp.IsDensityCulling () && |
---|
| 189 | (vp.GetVisibleDensity () != fVP.GetVisibleDensity ())) |
---|
| 190 | return true; |
---|
| 191 | |
---|
| 192 | if (vp.IsSection () && |
---|
| 193 | (vp.GetSectionPlane () != fVP.GetSectionPlane ())) |
---|
| 194 | return true; |
---|
| 195 | |
---|
| 196 | if (vp.IsCutaway ()) { |
---|
| 197 | if (vp.GetCutawayPlanes ().size () != |
---|
| 198 | fVP.GetCutawayPlanes ().size ()) return true; |
---|
| 199 | for (size_t i = 0; i < vp.GetCutawayPlanes().size(); ++i) |
---|
| 200 | if (vp.GetCutawayPlanes()[i] != fVP.GetCutawayPlanes()[i]) |
---|
| 201 | return true; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if (vp.IsExplode () && |
---|
| 205 | (vp.GetExplodeFactor () != fVP.GetExplodeFactor ())) |
---|
| 206 | return true; |
---|
| 207 | |
---|
| 208 | return false; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | void G4OpenInventorViewer::ClearView () { |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | void G4OpenInventorViewer::SetView () { |
---|
| 215 | |
---|
| 216 | // Get G4 camera infos : |
---|
| 217 | const G4Point3D target |
---|
| 218 | = fSceneHandler.GetScene()->GetStandardTargetPoint() |
---|
| 219 | + fVP.GetCurrentTargetPoint (); |
---|
| 220 | G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius(); |
---|
| 221 | if(radius<=0.) radius = 1.; |
---|
| 222 | const G4double cameraDistance = fVP.GetCameraDistance (radius); |
---|
| 223 | const G4Vector3D& direction = fVP.GetViewpointDirection().unit(); |
---|
| 224 | const G4Point3D cameraPosition = target + cameraDistance * direction; |
---|
| 225 | //const G4double pnear = fVP.GetNearDistance (cameraDistance, radius); |
---|
| 226 | //const G4double pfar = fVP.GetFarDistance (cameraDistance, pnear, radius); |
---|
| 227 | const G4Normal3D& up = fVP.GetUpVector (); |
---|
| 228 | |
---|
| 229 | /* |
---|
| 230 | printf("debug : target : %g %g %g\n",target.x(), |
---|
| 231 | target.y(), |
---|
| 232 | target.z()); |
---|
| 233 | printf("debug : dir : %g %g %g\n",direction.x(), |
---|
| 234 | direction.y(), |
---|
| 235 | direction.z()); |
---|
| 236 | printf("debug : pos : %g %g %g\n",cameraPosition.x(), |
---|
| 237 | cameraPosition.y(), |
---|
| 238 | cameraPosition.z()); |
---|
| 239 | //printf("debug : near %g far %g\n",pnear,pfar); |
---|
| 240 | */ |
---|
| 241 | |
---|
| 242 | SoCamera* camera = GetCamera(); |
---|
| 243 | if(!camera) return; |
---|
| 244 | |
---|
| 245 | // viewer camera setup : |
---|
| 246 | camera->position.setValue((float)cameraPosition.x(), |
---|
| 247 | (float)cameraPosition.y(), |
---|
| 248 | (float)cameraPosition.z()); |
---|
| 249 | |
---|
| 250 | SbVec3f sbTarget((float)target.x(), |
---|
| 251 | (float)target.y(), |
---|
| 252 | (float)target.z()); |
---|
| 253 | SbVec3f sbUp((float)up.x(), |
---|
| 254 | (float)up.y(), |
---|
| 255 | (float)up.z()); |
---|
| 256 | sbUp.normalize(); |
---|
| 257 | // Need Coin's camera->pointAt(sbTarget,sbUp); not in the SGI API |
---|
| 258 | // Stole Coin's code... |
---|
| 259 | pointAt(camera,sbTarget,sbUp); |
---|
| 260 | |
---|
| 261 | //camera->height.setValue(10); |
---|
| 262 | //camera->nearDistance.setValue((float)pnear); |
---|
| 263 | //camera->farDistance.setValue((float)pfar); |
---|
| 264 | //camera->focalDistance.setValue((float)cameraDistance); |
---|
| 265 | |
---|
| 266 | if(camera->isOfType(SoOrthographicCamera::getClassTypeId())) { |
---|
| 267 | if (fVP.GetFieldHalfAngle() == 0.) { |
---|
| 268 | //FIXME : ((SoOrthographicCamera*)camera)->height.setValue(); |
---|
| 269 | //FIXME : (Don't think we have to do that.) |
---|
| 270 | } else { |
---|
| 271 | //FIXME : Have to set a perspective camera ! |
---|
| 272 | //FIXME : viewer->setCameraType(SoPerspectiveCamera::getClassTypeId()) |
---|
| 273 | //FIXME : ((SoPerspectiveCamera*)camera)->heightAngle.setValue |
---|
| 274 | //FIXME : (2.*fVP.GetFieldHalfAngle()); |
---|
| 275 | } |
---|
| 276 | } else if(camera->isOfType(SoPerspectiveCamera::getClassTypeId())) { |
---|
| 277 | if (fVP.GetFieldHalfAngle() == 0.) { |
---|
| 278 | //FIXME : Have to set an orthographic camera ! |
---|
| 279 | //FIXME : viewer->setCameraType(SoOrthographicCamera::getClassTypeId()) |
---|
| 280 | } else { |
---|
| 281 | //FIXME : ((SoPerspectiveCamera*)camera)->heightAngle.setValue |
---|
| 282 | //FIXME : (2.*fVP.GetFieldHalfAngle()); |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | //COIN_FUNCTION_EXTENSION |
---|
| 288 | void |
---|
| 289 | G4OpenInventorViewer::pointAt(SoCamera* camera,const SbVec3f & targetpoint, const SbVec3f & upvector) |
---|
| 290 | { |
---|
| 291 | SbVec3f dir = targetpoint - camera->position.getValue(); |
---|
| 292 | if (dir.normalize() == 0.0f) return; |
---|
| 293 | lookAt(camera,dir, upvector); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | //COIN_FUNCTION |
---|
| 297 | // Private method that calculates a new orientation based on camera |
---|
| 298 | // direction and camera up vector. Vectors must be unit length. |
---|
| 299 | void |
---|
| 300 | G4OpenInventorViewer::lookAt(SoCamera* camera,const SbVec3f & dir, const SbVec3f & up) |
---|
| 301 | { |
---|
| 302 | SbVec3f z = -dir; |
---|
| 303 | SbVec3f y = up; |
---|
| 304 | SbVec3f x = y.cross(z); |
---|
| 305 | |
---|
| 306 | // recompute y to create a valid coordinate system |
---|
| 307 | y = z.cross(x); |
---|
| 308 | |
---|
| 309 | // normalize x and y to create an orthonormal coord system |
---|
| 310 | y.normalize(); |
---|
| 311 | x.normalize(); |
---|
| 312 | |
---|
| 313 | // create a rotation matrix |
---|
| 314 | SbMatrix rot = SbMatrix::identity(); |
---|
| 315 | rot[0][0] = x[0]; |
---|
| 316 | rot[0][1] = x[1]; |
---|
| 317 | rot[0][2] = x[2]; |
---|
| 318 | |
---|
| 319 | rot[1][0] = y[0]; |
---|
| 320 | rot[1][1] = y[1]; |
---|
| 321 | rot[1][2] = y[2]; |
---|
| 322 | |
---|
| 323 | rot[2][0] = z[0]; |
---|
| 324 | rot[2][1] = z[1]; |
---|
| 325 | rot[2][2] = z[2]; |
---|
| 326 | |
---|
| 327 | camera->orientation.setValue(SbRotation(rot)); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | void |
---|
| 331 | G4OpenInventorViewer::lookedAt(SoCamera* camera,SbVec3f & dir, SbVec3f & up) |
---|
| 332 | { |
---|
| 333 | SbRotation rot = camera->orientation.getValue(); |
---|
| 334 | SbMatrix mrot; rot.getValue(mrot); |
---|
| 335 | |
---|
| 336 | SbVec3f x, y, z; |
---|
| 337 | |
---|
| 338 | // create a rotation matrix |
---|
| 339 | x[0] = mrot[0][0]; |
---|
| 340 | x[1] = mrot[0][1]; |
---|
| 341 | x[2] = mrot[0][2]; |
---|
| 342 | |
---|
| 343 | y[0] = mrot[1][0]; |
---|
| 344 | y[1] = mrot[1][1]; |
---|
| 345 | y[2] = mrot[1][2]; |
---|
| 346 | |
---|
| 347 | z[0] = mrot[2][0]; |
---|
| 348 | z[1] = mrot[2][1]; |
---|
| 349 | z[2] = mrot[2][2]; |
---|
| 350 | |
---|
| 351 | dir = -z; |
---|
| 352 | dir.normalize(); |
---|
| 353 | up = SbVec3f(0.f,1.f,0.f); // Choose y-axis if possible. |
---|
| 354 | if (std::abs(up.dot(z)) > 1.e-6) { |
---|
| 355 | up = y; |
---|
| 356 | up.normalize(); |
---|
| 357 | } |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | void G4OpenInventorViewer::DrawView () { |
---|
| 361 | //G4cout << "debug Iv::DrawViewer " <<G4endl; |
---|
| 362 | if (!fNeedKernelVisit) KernelVisitDecision(); |
---|
| 363 | ProcessView(); |
---|
| 364 | FinishView(); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | void G4OpenInventorViewer::ShowView () { |
---|
| 368 | fInteractorManager -> SecondaryLoop (); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | void G4OpenInventorViewer::GroupCameraSensorCB(void* aThis,SoSensor* aSensor){ |
---|
| 372 | G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis; |
---|
| 373 | |
---|
| 374 | SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode(); |
---|
| 375 | //printf("debug : GroupCameraSensorCB %s\n", |
---|
| 376 | //node->getTypeId().getName().getString()); |
---|
| 377 | |
---|
| 378 | if(node->isOfType(SoCamera::getClassTypeId())) { |
---|
| 379 | // Viewer had changed the camera type, |
---|
| 380 | // attach the fCameraSensor to the new camera. |
---|
| 381 | SoCamera* camera = (SoCamera*)node; |
---|
| 382 | This->fCameraSensor->detach(); |
---|
| 383 | This->fCameraSensor->attach(camera); |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | void G4OpenInventorViewer::CameraSensorCB(void* aThis,SoSensor* aSensor) { |
---|
| 389 | G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis; |
---|
| 390 | |
---|
| 391 | //printf("debug : CameraSensorCB\n"); |
---|
| 392 | |
---|
| 393 | SoNode* node = ((SoNodeSensor*)aSensor)->getTriggerNode(); |
---|
| 394 | |
---|
| 395 | if(node->isOfType(SoCamera::getClassTypeId())) { |
---|
| 396 | SoCamera* camera = (SoCamera*)node; |
---|
| 397 | |
---|
| 398 | SbVec3f direction, up; |
---|
| 399 | lookedAt(camera,direction, up); |
---|
| 400 | This->fVP.SetViewpointDirection |
---|
| 401 | (G4Vector3D(-direction[0],-direction[1],-direction[2])); |
---|
| 402 | This->fVP.SetUpVector(G4Vector3D(up[0],up[1],up[2])); |
---|
| 403 | |
---|
| 404 | SbVec3f pos = camera->position.getValue(); |
---|
| 405 | SbVec3f target = pos + direction * camera->focalDistance.getValue(); |
---|
| 406 | |
---|
| 407 | This->fVP.SetCurrentTargetPoint(G4Point3D(target[0],target[1],target[2])); |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | void G4OpenInventorViewer::SelectionCB( |
---|
| 412 | void* aThis |
---|
| 413 | ,SoPath* aPath |
---|
| 414 | ) |
---|
| 415 | { |
---|
| 416 | G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis; |
---|
| 417 | SoNode* node = ((SoFullPath*)aPath)->getTail(); |
---|
| 418 | G4AttHolder* attHolder = dynamic_cast<G4AttHolder*>(node); |
---|
| 419 | if(attHolder && attHolder->GetAttDefs().size()) { |
---|
| 420 | for (size_t i = 0; i < attHolder->GetAttDefs().size(); ++i) { |
---|
| 421 | G4cout << G4AttCheck(attHolder->GetAttValues()[i], |
---|
| 422 | attHolder->GetAttDefs()[i]); |
---|
| 423 | } |
---|
| 424 | } else { |
---|
| 425 | G4String name((char*)node->getName().getString()); |
---|
| 426 | G4String cls((char*)node->getTypeId().getName().getString()); |
---|
| 427 | G4cout << "SoNode : " << node |
---|
| 428 | << " SoType : " << cls |
---|
| 429 | << " name : " << name |
---|
| 430 | << G4endl; |
---|
| 431 | G4cout << "No attributes attached." << G4endl; |
---|
| 432 | } |
---|
| 433 | /*FIXME : to explore (need different button - this is used for picking. |
---|
| 434 | if(node->isOfType(Geant4_SoPolyhedron::getClassTypeId())) { |
---|
| 435 | Geant4_SoPolyhedron* polyhedron = (Geant4_SoPolyhedron*)node; |
---|
| 436 | if(polyhedron->solid.getValue()==FALSE) |
---|
| 437 | polyhedron->solid.setValue(TRUE); |
---|
| 438 | else |
---|
| 439 | polyhedron->solid.setValue(FALSE); |
---|
| 440 | }*/ |
---|
| 441 | This->fSoSelection->deselectAll(); |
---|
| 442 | } |
---|
| 443 | /* |
---|
| 444 | void G4OpenInventorViewer::DeselectionCB( |
---|
| 445 | void* aThis |
---|
| 446 | ,SoPath* aPath |
---|
| 447 | ) |
---|
| 448 | { |
---|
| 449 | //G4OpenInventorViewer* This = (G4OpenInventorViewer*)aThis; |
---|
| 450 | G4String name((char*)aPath->getTail()->getTypeId().getName().getString()); |
---|
| 451 | G4cout << "Deselect : " << name << G4endl; |
---|
| 452 | } |
---|
| 453 | */ |
---|
| 454 | |
---|
| 455 | void G4OpenInventorViewer::DrawDetector() { |
---|
| 456 | /* Replace this... - JA |
---|
| 457 | // DrawView does a ClearStore. Do not clear the transient store : |
---|
| 458 | SoSeparator* tmp = fG4OpenInventorSceneHandler.fTransientRoot; |
---|
| 459 | fG4OpenInventorSceneHandler.fTransientRoot = new SoSeparator; |
---|
| 460 | if (!fNeedKernelVisit) KernelVisitDecision(); |
---|
| 461 | ProcessView(); |
---|
| 462 | fG4OpenInventorSceneHandler.fTransientRoot->unref(); |
---|
| 463 | fG4OpenInventorSceneHandler.fTransientRoot = tmp; |
---|
| 464 | */ |
---|
| 465 | // ...by this... - JA |
---|
| 466 | DrawView(); |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 470 | /// Menu items callbacks ///////////////////////////////////////////////////// |
---|
| 471 | ////////////////////////////////////////////////////////////////////////////// |
---|
| 472 | |
---|
| 473 | void G4OpenInventorViewer::Escape(){ |
---|
| 474 | G4cout << "Escape..." <<G4endl; |
---|
| 475 | fInteractorManager->RequireExitSecondaryLoop (OIV_EXIT_CODE); |
---|
| 476 | } |
---|
| 477 | |
---|
| 478 | void G4OpenInventorViewer::WritePostScript(const G4String& aFile) { |
---|
| 479 | if(!fGL2PSAction) return; |
---|
| 480 | fGL2PSAction->setFileName(aFile.c_str()); |
---|
| 481 | G4cout << "Produce " << aFile << "..." << G4endl; |
---|
[921] | 482 | if (fGL2PSAction->enableFileWriting()) { |
---|
| 483 | ViewerRender(); |
---|
| 484 | fGL2PSAction->disableFileWriting(); |
---|
| 485 | } |
---|
[529] | 486 | } |
---|
| 487 | |
---|
| 488 | void G4OpenInventorViewer::WritePixmapPostScript(const G4String& aFile) { |
---|
| 489 | fSoImageWriter->fileName.setValue(aFile.c_str()); |
---|
| 490 | //imageWriter->format.setValue(SoImageWriter::POST_SCRIPT); |
---|
| 491 | fSoImageWriter->enable(); |
---|
| 492 | ViewerRender(); |
---|
| 493 | fSoImageWriter->disable(); |
---|
| 494 | if(fSoImageWriter->getStatus()) { |
---|
| 495 | G4cout << G4String(fSoImageWriter->fileName.getValue().getString()) |
---|
| 496 | << " produced." |
---|
| 497 | << G4endl; |
---|
| 498 | } else { |
---|
| 499 | G4cout << G4String(fSoImageWriter->fileName.getValue().getString()) |
---|
| 500 | << " not produced." |
---|
| 501 | << G4endl; |
---|
| 502 | } |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | void G4OpenInventorViewer::WriteInventor(const G4String& aFile) { |
---|
| 506 | G4cout << "Produce " << aFile << "..." << G4endl; |
---|
| 507 | |
---|
| 508 | SbBool genAlternateRep = TRUE; |
---|
| 509 | //SbBool binary = FALSE; |
---|
| 510 | SbBool binary = TRUE; |
---|
| 511 | SoAlternateRepAction alternateRepAction; |
---|
| 512 | if(genAlternateRep==TRUE) { |
---|
| 513 | alternateRepAction.setGenerate(TRUE); //Clear alternate reps. |
---|
| 514 | alternateRepAction.apply(fSoSelection); |
---|
| 515 | } |
---|
| 516 | |
---|
| 517 | SoWriteAction writeAction; |
---|
| 518 | writeAction.getOutput()->openFile(aFile.c_str()); |
---|
| 519 | writeAction.getOutput()->setBinary(binary); |
---|
| 520 | writeAction.apply(fSoSelection); |
---|
| 521 | writeAction.getOutput()->closeFile(); |
---|
| 522 | |
---|
| 523 | if(genAlternateRep==TRUE) { |
---|
| 524 | alternateRepAction.setGenerate(FALSE); //Clear alternate reps. |
---|
| 525 | alternateRepAction.apply(fSoSelection); |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | |
---|
| 529 | |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | struct Counter { |
---|
| 533 | int fTriangles; |
---|
| 534 | int fLineSegments; |
---|
| 535 | int fPoints; |
---|
| 536 | }; |
---|
| 537 | |
---|
| 538 | static void CountTrianglesCB( |
---|
| 539 | void* userData |
---|
| 540 | ,SoCallbackAction* |
---|
| 541 | ,const SoPrimitiveVertex* |
---|
| 542 | ,const SoPrimitiveVertex*, |
---|
| 543 | const SoPrimitiveVertex*) |
---|
| 544 | { |
---|
| 545 | Counter* counter = (Counter*)userData; |
---|
| 546 | counter->fTriangles++; |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | static void CountLineSegmentsCB( |
---|
| 550 | void* userData |
---|
| 551 | ,SoCallbackAction* |
---|
| 552 | ,const SoPrimitiveVertex* |
---|
| 553 | ,const SoPrimitiveVertex*) |
---|
| 554 | { |
---|
| 555 | Counter* counter = (Counter*)userData; |
---|
| 556 | counter->fLineSegments++; |
---|
| 557 | } |
---|
| 558 | |
---|
| 559 | static void CountPointsCB( |
---|
| 560 | void* userData |
---|
| 561 | ,SoCallbackAction* |
---|
| 562 | ,const SoPrimitiveVertex*) |
---|
| 563 | { |
---|
| 564 | Counter* counter = (Counter*)userData; |
---|
| 565 | counter->fPoints++; |
---|
| 566 | } |
---|
| 567 | |
---|
| 568 | void G4OpenInventorViewer::SceneGraphStatistics() { |
---|
| 569 | Counter counter; |
---|
| 570 | counter.fTriangles = 0; |
---|
| 571 | counter.fLineSegments = 0; |
---|
| 572 | counter.fPoints = 0; |
---|
| 573 | |
---|
| 574 | SoCallbackAction callbackAction; |
---|
| 575 | callbackAction.addTriangleCallback |
---|
| 576 | (SoShape::getClassTypeId(),CountTrianglesCB,(void*)&counter); |
---|
| 577 | callbackAction.addLineSegmentCallback |
---|
| 578 | (SoShape::getClassTypeId(),CountLineSegmentsCB,(void*)&counter); |
---|
| 579 | callbackAction.addPointCallback |
---|
| 580 | (SoShape::getClassTypeId(),CountPointsCB,(void*)&counter); |
---|
| 581 | callbackAction.apply(fSoSelection); |
---|
| 582 | |
---|
| 583 | SoCounterAction counterAction; |
---|
| 584 | counterAction.apply(fSoSelection); |
---|
| 585 | int nodes = counterAction.getCount(); |
---|
| 586 | |
---|
| 587 | counterAction.setLookFor(SoCounterAction::TYPE); |
---|
| 588 | counterAction.setType(SoShape::getClassTypeId()); |
---|
| 589 | counterAction.apply(fSoSelection); |
---|
| 590 | int shapes = counterAction.getCount(); |
---|
| 591 | |
---|
| 592 | G4cout << "Number of triangles : " << counter.fTriangles << G4endl; |
---|
| 593 | G4cout << "Number of line segments : " << counter.fLineSegments << G4endl; |
---|
| 594 | G4cout << "Number of points : " << counter.fPoints << G4endl; |
---|
| 595 | G4cout << "Number of nodes : " << nodes << G4endl; |
---|
| 596 | G4cout << "Number of shapes : " << shapes << G4endl; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | void G4OpenInventorViewer::EraseDetector() { |
---|
| 600 | fG4OpenInventorSceneHandler.fDetectorRoot->removeAllChildren(); |
---|
| 601 | } |
---|
| 602 | void G4OpenInventorViewer::EraseEvent() { |
---|
| 603 | fG4OpenInventorSceneHandler.fTransientRoot->removeAllChildren(); |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | void G4OpenInventorViewer::SetPreviewAndFull() { |
---|
| 607 | fG4OpenInventorSceneHandler.fPreviewAndFull = true; |
---|
| 608 | |
---|
| 609 | NeedKernelVisit(); |
---|
| 610 | DrawDetector(); |
---|
| 611 | } |
---|
| 612 | |
---|
| 613 | void G4OpenInventorViewer::SetPreview() { |
---|
| 614 | fG4OpenInventorSceneHandler.fPreviewAndFull = false; |
---|
| 615 | |
---|
| 616 | NeedKernelVisit(); |
---|
| 617 | DrawDetector(); |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | // When ViewParameter <-> SoCamera mapping ready |
---|
| 621 | // uncomment the below |
---|
| 622 | //#define USE_SET_VIEW |
---|
| 623 | |
---|
| 624 | void G4OpenInventorViewer::SetSolid() { |
---|
| 625 | G4ViewParameters vp = GetViewParameters(); |
---|
| 626 | G4ViewParameters::DrawingStyle existingStyle = vp.GetDrawingStyle(); |
---|
| 627 | //From G4VisCommandsViewerSet : /vis/viewer/set/style solid. |
---|
| 628 | switch (existingStyle) { |
---|
| 629 | case G4ViewParameters::wireframe: |
---|
| 630 | vp.SetDrawingStyle(G4ViewParameters::hsr); |
---|
| 631 | break; |
---|
| 632 | case G4ViewParameters::hlr: |
---|
| 633 | vp.SetDrawingStyle(G4ViewParameters::hlhsr); |
---|
| 634 | break; |
---|
| 635 | case G4ViewParameters::hsr: |
---|
| 636 | break; |
---|
| 637 | case G4ViewParameters::hlhsr: |
---|
| 638 | break; |
---|
| 639 | } |
---|
| 640 | SetViewParameters(vp); |
---|
| 641 | DrawDetector(); |
---|
| 642 | } |
---|
| 643 | void G4OpenInventorViewer::SetWireFrame() { |
---|
| 644 | G4ViewParameters vp = GetViewParameters(); |
---|
| 645 | G4ViewParameters::DrawingStyle existingStyle = vp.GetDrawingStyle(); |
---|
| 646 | switch (existingStyle) { |
---|
| 647 | case G4ViewParameters::wireframe: |
---|
| 648 | break; |
---|
| 649 | case G4ViewParameters::hlr: |
---|
| 650 | break; |
---|
| 651 | case G4ViewParameters::hsr: |
---|
| 652 | vp.SetDrawingStyle(G4ViewParameters::wireframe); |
---|
| 653 | break; |
---|
| 654 | case G4ViewParameters::hlhsr: |
---|
| 655 | vp.SetDrawingStyle(G4ViewParameters::hlr); |
---|
| 656 | break; |
---|
| 657 | } |
---|
| 658 | SetViewParameters(vp); |
---|
| 659 | DrawDetector(); |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | |
---|
| 663 | void G4OpenInventorViewer::SetReducedWireFrame(bool aValue) { |
---|
| 664 | G4ViewParameters vp = GetViewParameters(); |
---|
| 665 | |
---|
| 666 | // Set the wire frame kind : |
---|
| 667 | vp.SetAuxEdgeVisible(!aValue); |
---|
| 668 | |
---|
| 669 | // Set wire frame : |
---|
| 670 | G4ViewParameters::DrawingStyle existingStyle = vp.GetDrawingStyle(); |
---|
| 671 | switch (existingStyle) { |
---|
| 672 | case G4ViewParameters::wireframe: |
---|
| 673 | break; |
---|
| 674 | case G4ViewParameters::hlr: |
---|
| 675 | break; |
---|
| 676 | case G4ViewParameters::hsr: |
---|
| 677 | vp.SetDrawingStyle(G4ViewParameters::wireframe); |
---|
| 678 | break; |
---|
| 679 | case G4ViewParameters::hlhsr: |
---|
| 680 | vp.SetDrawingStyle(G4ViewParameters::hlr); |
---|
| 681 | break; |
---|
| 682 | } |
---|
| 683 | SetViewParameters(vp); |
---|
| 684 | NeedKernelVisit(); // Just in case it was alread in wire framw. |
---|
| 685 | DrawDetector(); |
---|
| 686 | } |
---|
| 687 | |
---|
| 688 | void G4OpenInventorViewer::UpdateScene() { |
---|
| 689 | /* Replace this... - JA |
---|
| 690 | fG4OpenInventorSceneHandler.ClearStore(); |
---|
| 691 | ClearView(); |
---|
| 692 | if (!fNeedKernelVisit) KernelVisitDecision(); |
---|
| 693 | ProcessView(); |
---|
| 694 | ShowView(); |
---|
| 695 | */ |
---|
| 696 | // ...by this - JA |
---|
| 697 | NeedKernelVisit(); |
---|
| 698 | DrawView(); |
---|
| 699 | } |
---|
| 700 | G4String G4OpenInventorViewer::Help(const G4String& aTopic) { |
---|
| 701 | if(aTopic=="controls") { |
---|
| 702 | return G4String("\ |
---|
| 703 | Controls on an Inventor examiner viewer are :\n\ |
---|
| 704 | - in picking mode (cursor is the upper left arrow)\n\ |
---|
| 705 | Ctrl + pick a volume : see daughters.\n\ |
---|
| 706 | Shift + pick a volume : see mother.\n\ |
---|
| 707 | - in viewing mode (cursor is the hand)\n\ |
---|
| 708 | Left-button + pointer move : rotate.\n\ |
---|
| 709 | Ctrl+Left-button + pointer move : pan.\n\ |
---|
| 710 | Ctrl+Shift+Left-button + pointer move : scale.\n\ |
---|
| 711 | Middle-button + pointer move : pan.\n\ |
---|
| 712 | Right-button : popup menu.\n"); |
---|
| 713 | } else { |
---|
| 714 | return ""; |
---|
| 715 | } |
---|
| 716 | } |
---|
| 717 | |
---|
| 718 | #endif |
---|