G4OpenInventorXtExaminerViewer.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // Open Inventor Xt Extended Viewer - 30 Oct 2012
00027 // Rastislav Ondrasek, Pierre-Luc Gagnon, Frederick Jones TRIUMF
00028 
00029 #ifndef HookEventProcState_H
00030 #define HookEventProcState_H 1
00031 #include "G4VStateDependent.hh"
00032 
00033 class G4OpenInventorXtExaminerViewer;
00034 class HookEventProcState : public G4VStateDependent
00035 {
00036 private:
00037         G4OpenInventorXtExaminerViewer *viewer;
00038 public:
00039         HookEventProcState(G4OpenInventorXtExaminerViewer*);
00040         ~HookEventProcState();
00041 
00042         virtual G4bool Notify(G4ApplicationState requiredState);
00043 };
00044 #endif /* HookEventProcState_H */
00045 
00046 
00047 #ifndef G4OPENINVENTORXTEXAMINERVIEWER_HH
00048 #define G4OPENINVENTORXTEXAMINERVIEWER_HH
00049 
00050 #include <map>
00051 #include <vector>
00052 #include <fstream>
00053 #include <Inventor/SbLinear.h>
00054 #include <Inventor/nodes/SoLineSet.h>
00055 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
00056 #include <Inventor/events/SoKeyboardEvent.h>
00057 
00058 class SoCoordinate3;
00059 class SoFont;
00060 class SoText2;
00061 class SoPointSet;
00062 
00063 class G4OpenInventorXtExaminerViewer : public SoXtExaminerViewer {
00064 private:
00065   Widget prevViewPtButton, nextViewPtButton;
00066   Widget menuBar, fileMenu, openFileDialog, newFileDialog,
00067   loadRefCoordsDialog, saveRefCoordsDialog,
00068   loadSceneGraphDialog, saveSceneGraphDialog,
00069   viewPtSelection, listsDialog, myShellDialog, myViewPtList, myElementList;
00070 
00071   static G4OpenInventorXtExaminerViewer *viewer;
00072   void (*escapeCallback)(void *);
00073   void * examinerObject;
00074   SbBool lshiftdown, rshiftdown, lctrldown, rctrldown;
00075   
00076 public:
00077 
00078   // Same constructor as the ExaminerViewer
00079   G4OpenInventorXtExaminerViewer(Widget parent = NULL,
00080              const char *name = NULL,
00081              SbBool embed = TRUE, 
00082              SoXtFullViewer::BuildFlag flag = BUILD_ALL,
00083              SoXtViewer::Type type = BROWSER);
00084 
00085   ~G4OpenInventorXtExaminerViewer();
00086   template <class T> void parseString(T &t, const std::string &s, bool &error);
00087   Widget addMenu(std::string name);
00088   void addButton(Widget menu, std::string name, XtCallbackProc);
00089   Widget getMenuBar() { return menuBar; }
00090   Widget getMenu() { return fileMenu; }
00091   void warningMsgDialog(std::string, String, XtCallbackProc);
00092   bool warningFlag;
00093 
00094   std::string saveScenegraphFileName;
00095   Widget saveScenegraphWidget;
00096   std::string saveRefCoordsFileName;
00097   Widget saveRefCoordsWidget;
00098 
00099   Widget createScale(Widget, char *, int, float);
00100   void addEscapeCallback(void (*cb)(void *), void *);
00101   bool abbrOutputFlag;
00102   bool pickRefPathFlag;
00103   bool viewingBeforePickRef;
00104                      
00105 protected:
00106   // Same constructor as the ExaminerViewer 
00107   G4OpenInventorXtExaminerViewer(Widget parent,
00108              const char *name,
00109              SbBool embed,
00110              SoXtFullViewer::BuildFlag flag,
00111              SoXtViewer::Type type,
00112              SbBool build);
00113 
00114   // Overloaded for adding the MenuBar
00115   Widget buildWidget(Widget parent);
00116   // Overloaded so additional buttons can be added
00117   virtual void createViewerButtons (Widget parent, SbPList * buttonlist);
00118   // Overloaded for catching various keyboard events  
00119   virtual SbBool processSoEvent(const SoEvent * const event);
00120   void moveCamera(float dist = 0, bool lookdown = false);
00121   std::string curEltName;
00122   SbVec3f camUpVec;
00123   SbVec3f camDir;
00124   void rotateCamera();
00125   void updateViewParams(SoKeyboardEvent::Key);
00126   bool loadViewPts();
00127   virtual void afterRealizeHook();
00128 
00129 private:
00130   // Each constructor calls this generic constructor
00131   void constructor(const SbBool build);
00132 
00133   static G4OpenInventorXtExaminerViewer *getObject();
00134 
00135   HookEventProcState *hookBeamOn;
00136   friend class HookEventProcState;
00137   bool newEvents;
00138   static void sceneChangeCB(void *, SoSensor *);
00139     
00140   void setViewPt();
00141   void writeViewPtIdx();
00142   void cleanUpAfterPrevFile();
00143   
00144   void popUpFileSelDialog(Widget&, std::string, std::string, XtCallbackProc);
00145   static void cancelFileSelDialogCB(Widget, XtPointer, XtPointer);
00146   static void openViewPtFileCB(Widget, XtPointer, XtPointer);
00147   static void viewPtFileSelectedCB(Widget, XtPointer, XtPointer);
00148   static void newViewPtFileCB(Widget, XtPointer, XtPointer);
00149   static void createNewVPFileCB(Widget, XtPointer, XtPointer);
00150   static void overwriteFileCB(Widget, XtPointer, XtPointer);
00151   static void loadRefCoordsDialogCB(Widget, XtPointer, XtPointer);      //pop file dialog
00152   static void loadRefCoordsCB(Widget, XtPointer, XtPointer);                    //execute loading
00153   static void saveRefCoordsDialogCB(Widget, XtPointer, XtPointer);      //pop file dialog
00154   static void saveRefCoordsCB(Widget, XtPointer, XtPointer);                    //execute saving
00155   static void saveRefCoordsOverWriteCB(Widget, XtPointer, XtPointer);
00156   static void loadSceneGraphDialogCB(Widget, XtPointer, XtPointer);
00157   static void loadSceneGraphCB(Widget, XtPointer, XtPointer);
00158   static void saveSceneGraphDialogCB(Widget, XtPointer, XtPointer);
00159   static void saveSceneGraphCB(Widget, XtPointer, XtPointer);
00160   static void saveSceneGraphOverWriteCB(Widget, XtPointer, XtPointer);
00161   static void mouseoverCB(void *aThis, SoEventCallback *eventCB);
00162   static void pickingCB(void *aThis, SoEventCallback *eventCB);
00163 
00164                      
00165   // Viewpoint operations
00166   void addViewPoints();
00167   static void closeListsDialogCB(Widget, XtPointer, XtPointer);
00168   static void loadBookmarkCB(Widget, XtPointer, XtPointer);
00169   static void renameBookmarkCB(Widget, XtPointer, XtPointer);
00170   void renameViewPt(char *vpName);
00171   static void sortBookmarksCB(Widget, XtPointer, XtPointer);
00172   void sortViewPts(std::vector<std::string>);
00173   static void deleteBookmarkCB(Widget, XtPointer, XtPointer);
00174   static void deleteViewPtCB(Widget, XtPointer, XtPointer);
00175   void deleteViewPt(char *vpName = NULL);
00176     
00177   // Animation
00178   static void animateRefParticleCB(Widget, XtPointer, XtPointer);
00179   static void animateSensorCB(void *, SoSensor *);
00180   static void animateSensorRotationCB(void *, SoSensor *);
00181   void animateRefParticle();
00182   void saveCurCamera();
00183   void restoreCamera();
00184   double animateBtwPtsPeriod, speedStep;
00185   void incSpeed();
00186   void decSpeed();
00187   
00188   SoTimerSensor *animateSensor;
00189   SoTimerSensor *animateSensorRotation;
00190   SoNodeSensor *sceneChangeSensor;
00191   SbVec3f camStartPos, camEndPos;
00192   SbRotation camStartOrient, camEndOrient;
00193   
00194   static void prevViewPtCB(Widget, XtPointer, XtPointer);
00195   static void nextViewPtCB(Widget, XtPointer, XtPointer);
00196   static void saveViewPtCB(Widget, XtPointer, XtPointer);
00197   static void abbrOutputCB(Widget, XtPointer, XtPointer);
00198   static void pickRefPathCB(Widget, XtPointer, XtPointer);
00199   static void constructListsDialog(Widget, XtPointer, XtPointer);
00200   void saveViewPt(char *name);
00201   
00202 
00203   static void lookAtSceneElementCB(Widget, XtPointer, XtPointer);
00204   static void cancelSceneElementSelectionCB(Widget, XtPointer, XtPointer);
00205 
00206   void setReferencePath(SoLineSet*, SoCoordinate3*, bool append = false);
00207   void setReferencePathZPos();
00208   void findAndSetRefPath();
00209   SoCoordinate3 *getCoordsNode(SoFullPath *path);
00210   void getSceneElements(); // reads elements from the scene graph
00211   float sqrlen(const SbVec3f&);
00212   void distanceToTrajectory(const SbVec3f&, float&, SbVec3f&, int&);
00213   void sortElements();
00214   void createElementsList(Widget);
00215   static void closeMainWindowCB(Widget, XtPointer, XtPointer);
00216   void evenOutRefParticlePts();
00217 
00218   static void gotoRefPathStartCB(Widget, XtPointer, XtPointer);
00219   void gotoRefPathStart();
00220   static void invertRefPathCB(Widget, XtPointer, XtPointer);
00221   void invertRefPath();
00222 
00223   enum CameraType {
00224     PERSPECTIVE,
00225     ORTHOGRAPHIC
00226   };
00227 
00228     
00229   enum State {
00230     GENERAL,
00231     BEAMLINE,
00232     VIEWPOINT,
00233     ANIMATION,
00234     REVERSED_ANIMATION,
00235     PAUSED_ANIMATION,
00236     ROTATING
00237   };
00238 
00239   // For storing the view point
00240   struct viewPtData {
00241           char* viewPtName;
00242           int viewportMapping;
00243           SbVec3f position;
00244           SbRotation orientation;
00245           float aspectRatio;
00246           float nearDistance;
00247           float farDistance;
00248           float focalDistance;
00249           CameraType camType;
00250           float height;
00251   };
00252   
00253   struct sceneElement {
00254           std::string name;
00255           SoFullPath * path;
00256           SbVec3f center;
00257           float closestPointZCoord;
00258 
00259           sceneElement operator=(sceneElement const &rhs) const {
00260                   sceneElement el;
00261                   el.name = rhs.name;
00262                   el.path = rhs.path;
00263                   el.center = rhs.center;
00264                   el.closestPointZCoord = rhs.closestPointZCoord;
00265 
00266                   return el;
00267           }
00268   };
00269 
00270    struct elementForSorting {
00271       float closestPointZCoord;
00272       SbVec3f closestPoint;
00273       float smallestDistance;
00274       float distanceToBeamlineStart;
00275       std::string name;
00276 
00277       bool operator<(elementForSorting const &other) const
00278       {
00279          if (closestPointZCoord < other.closestPointZCoord)
00280             return true;
00281          if (closestPointZCoord > other.closestPointZCoord)
00282             return false;
00283 
00284          // otherwise closestPointZCoord == other.closestPointZCoord.
00285          // Compare the distances from the center of the element to
00286          // the start of the beamline.
00287          if (distanceToBeamlineStart < other.distanceToBeamlineStart)
00288             return true;
00289          if (distanceToBeamlineStart > other.distanceToBeamlineStart)
00290             return false;
00291 
00292          // In case both closestPointZCoord and smallestDistance are
00293          // equal, we have two exactly overlapping elements, if so
00294          // the order doesn't matter
00295          return true;
00296       }
00297    };
00298 
00299   bool zcoordSetFlag;
00300 
00301   std::vector<sceneElement> sceneElements;
00302   std::vector<viewPtData> viewPtList;
00303   std::string fileName;
00304   std::ifstream fileIn;
00305   std::ofstream fileOut; 
00306   int viewPtIdx;
00307   int MAX_VP_IDX;
00308   int MAX_VP_NAME;
00309   
00310   // For storing coordinate points of the reference particle
00311   std::vector<SbVec3f> refParticleTrajectory;
00312   // For displaying distance during anim and beamline modes
00313   std::vector<float> refZPositions;
00314   int refParticleIdx;
00315   int prevRefIdx;
00316   float distance;
00317   State currentState, prevState, beforePausing;
00318   char *curViewPtName;
00319   
00320   int step;
00321   SbVec3f prevPt;
00322   SbVec3f prevParticleDir;
00323   void * prevColorField;
00324   
00325   viewPtData camB4Animation;
00326   bool returnToSaveVP;
00327   bool returnToAnim;
00328   SoCamera *myCam;
00329   void setStartingPtForAnimation(); 
00330   float left_right, up_down;    
00331   SbVec3f rotAxis; // For 90 degree rotations
00332   int rotCnt;  // # of steps rotation is split into
00333 
00334   static void getViewPtNameCB(Widget, XtPointer, XtPointer);
00335   std::string viewPtAutoName();
00336 
00338   SoSearchAction * searcher;
00339 
00340   SoNode * superimposition;
00341   SoCoordinate3 * sgeometry;
00342   SoScale * sscale;
00343   
00344   SoTranslation * stranslation;
00345   SoTranslation * curInfoTrans;
00346   SoTranslation * mouseOverTransSolid;
00347   SoTranslation * mouseOverTransMaterial;
00348   SoTranslation * mouseOverTransLogName;
00349   SoTranslation * mouseOverTransZPos;
00350 
00351   // Used for 2 similar purposes: 1. Displays z position during animation
00352   //                              2. Displays name of the current viewpoint
00353   SoText2 * curInfoText;
00354   /* Need to use many different fields for mouseover
00355    * because newlines are ignored when the scene is rendered */
00356   SoText2 * mouseOverTextSolid;
00357   SoText2 * mouseOverTextMaterial;
00358   SoText2 * mouseOverTextLogName;
00359   SoText2 * mouseOverTextZPos;
00360    
00361   SoFont * curInfoFont;
00362   SoFont * mouseOverFontSolid;
00363   SoFont * mouseOverFontMaterial;
00364   SoFont * mouseOverFontLogName;
00365   SoFont * mouseOverFontZPos;
00366   SoSwitch * axisSwitch;
00367   SoSwitch * animSpeedOutlineSwitch; 
00368   SoSwitch * animSpeedSwitch;
00369   SoSwitch * curInfoSwitch;
00370     
00371   SoNode * getSuperimpositionNode(SoNode *, const char * name);
00372 
00373   void superimpositionEvent(SoAction * action);
00374   static void superimpositionCB(void * closure, SoAction * action);
00375 
00376   virtual void actualRedraw(void);
00377   void updateSpeedIndicator(void);
00378 
00379   float maxSpeed; 
00381 
00382   // FWJ added for Ortho camera
00383   float defaultHeight;
00384   float defaultHeightAngle;
00385   
00386   // Used by G4 app during element rotations, stores previous view
00387   SbVec3f upVector, offsetFromCenter, center;   
00388   bool rotUpVec;
00389 
00390   SoSeparator * newSceneGraph;
00391 
00392 
00393 };
00394 #endif /* G4OPENINVENTORXTEXAMINERVIEWER_HH */

Generated on Mon May 27 17:49:12 2013 for Geant4 by  doxygen 1.4.7