Geant4_10
G4OpenInventorXtExaminerViewer.hh
Go to the documentation of this file.
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 //
26 // Open Inventor Xt Extended Viewer - 30 Oct 2012
27 // Rastislav Ondrasek, Pierre-Luc Gagnon, Frederick Jones TRIUMF
28 
29 #ifndef HookEventProcState_H
30 #define HookEventProcState_H 1
31 #include "G4VStateDependent.hh"
32 
35 {
36 private:
38 public:
41 
42  virtual G4bool Notify(G4ApplicationState requiredState);
43 };
44 #endif /* HookEventProcState_H */
45 
46 
47 #ifndef G4OPENINVENTORXTEXAMINERVIEWER_HH
48 #define G4OPENINVENTORXTEXAMINERVIEWER_HH
49 
50 #include <map>
51 #include <vector>
52 #include <fstream>
53 #include <Inventor/SbLinear.h>
54 #include <Inventor/nodes/SoLineSet.h>
55 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
56 #include <Inventor/events/SoKeyboardEvent.h>
57 
58 class SoCoordinate3;
59 class SoFont;
60 class SoText2;
61 class SoPointSet;
62 
63 class G4OpenInventorXtExaminerViewer : public SoXtExaminerViewer {
64 private:
65  Widget prevViewPtButton, nextViewPtButton;
66  Widget menuBar, fileMenu, openFileDialog, newFileDialog,
67  loadRefCoordsDialog, saveRefCoordsDialog,
68  loadSceneGraphDialog, saveSceneGraphDialog,
69  viewPtSelection, listsDialog, myShellDialog, myViewPtList, myElementList;
70 
71  static G4OpenInventorXtExaminerViewer *viewer;
72  void (*escapeCallback)(void *);
73  void * examinerObject;
74  SbBool lshiftdown, rshiftdown, lctrldown, rctrldown;
75 
76 public:
77 
78  // Same constructor as the ExaminerViewer
79  G4OpenInventorXtExaminerViewer(Widget parent = NULL,
80  const char *name = NULL,
81  SbBool embed = TRUE,
82  SoXtFullViewer::BuildFlag flag = BUILD_ALL,
83  SoXtViewer::Type type = BROWSER);
84 
86  template <class T> void parseString(T &t, const std::string &s, bool &error);
87  Widget addMenu(std::string name);
88  void addButton(Widget menu, std::string name, XtCallbackProc);
89  Widget getMenuBar() { return menuBar; }
90  Widget getMenu() { return fileMenu; }
91  void warningMsgDialog(std::string, String, XtCallbackProc);
93 
96  std::string saveRefCoordsFileName;
98 
99  Widget createScale(Widget, char *, int, float);
100  void addEscapeCallback(void (*cb)(void *), void *);
104 
105 protected:
106  // Same constructor as the ExaminerViewer
107  G4OpenInventorXtExaminerViewer(Widget parent,
108  const char *name,
109  SbBool embed,
110  SoXtFullViewer::BuildFlag flag,
111  SoXtViewer::Type type,
112  SbBool build);
113 
114  // Overloaded for adding the MenuBar
115  Widget buildWidget(Widget parent);
116  // Overloaded so additional buttons can be added
117  virtual void createViewerButtons (Widget parent, SbPList * buttonlist);
118  // Overloaded for catching various keyboard events
119  virtual SbBool processSoEvent(const SoEvent * const event);
120  void moveCamera(float dist = 0, bool lookdown = false);
121  std::string curEltName;
122  SbVec3f camUpVec;
123  SbVec3f camDir;
124  void rotateCamera();
125  void updateViewParams(SoKeyboardEvent::Key);
126  bool loadViewPts();
127  virtual void afterRealizeHook();
128 
129 private:
130  // Each constructor calls this generic constructor
131  void constructor(const SbBool build);
132 
133  static G4OpenInventorXtExaminerViewer *getObject();
134 
135  HookEventProcState *hookBeamOn;
136  friend class HookEventProcState;
137  bool newEvents;
138  static void sceneChangeCB(void *, SoSensor *);
139 
140  void setViewPt();
141  void writeViewPtIdx();
142  void cleanUpAfterPrevFile();
143 
144  void popUpFileSelDialog(Widget&, std::string, std::string, XtCallbackProc);
145  static void cancelFileSelDialogCB(Widget, XtPointer, XtPointer);
146  static void openViewPtFileCB(Widget, XtPointer, XtPointer);
147  static void viewPtFileSelectedCB(Widget, XtPointer, XtPointer);
148  static void newViewPtFileCB(Widget, XtPointer, XtPointer);
149  static void createNewVPFileCB(Widget, XtPointer, XtPointer);
150  static void overwriteFileCB(Widget, XtPointer, XtPointer);
151  static void loadRefCoordsDialogCB(Widget, XtPointer, XtPointer); //pop file dialog
152  static void loadRefCoordsCB(Widget, XtPointer, XtPointer); //execute loading
153  static void saveRefCoordsDialogCB(Widget, XtPointer, XtPointer); //pop file dialog
154  static void saveRefCoordsCB(Widget, XtPointer, XtPointer); //execute saving
155  static void saveRefCoordsOverWriteCB(Widget, XtPointer, XtPointer);
156  static void loadSceneGraphDialogCB(Widget, XtPointer, XtPointer);
157  static void loadSceneGraphCB(Widget, XtPointer, XtPointer);
158  static void saveSceneGraphDialogCB(Widget, XtPointer, XtPointer);
159  static void saveSceneGraphCB(Widget, XtPointer, XtPointer);
160  static void saveSceneGraphOverWriteCB(Widget, XtPointer, XtPointer);
161  static void mouseoverCB(void *aThis, SoEventCallback *eventCB);
162  static void pickingCB(void *aThis, SoEventCallback *eventCB);
163 
164 
165  // Viewpoint operations
166  void addViewPoints();
167  static void closeListsDialogCB(Widget, XtPointer, XtPointer);
168  static void loadBookmarkCB(Widget, XtPointer, XtPointer);
169  static void renameBookmarkCB(Widget, XtPointer, XtPointer);
170  void renameViewPt(char *vpName);
171  static void sortBookmarksCB(Widget, XtPointer, XtPointer);
172  void sortViewPts(std::vector<std::string>);
173  static void deleteBookmarkCB(Widget, XtPointer, XtPointer);
174  static void deleteViewPtCB(Widget, XtPointer, XtPointer);
175  void deleteViewPt(char *vpName = NULL);
176 
177  // Animation
178  static void animateRefParticleCB(Widget, XtPointer, XtPointer);
179  static void animateSensorCB(void *, SoSensor *);
180  static void animateSensorRotationCB(void *, SoSensor *);
181  void animateRefParticle();
182  void saveCurCamera();
183  void restoreCamera();
184  double animateBtwPtsPeriod, speedStep;
185  void incSpeed();
186  void decSpeed();
187 
188  SoTimerSensor *animateSensor;
189  SoTimerSensor *animateSensorRotation;
190  SoNodeSensor *sceneChangeSensor;
191  SbVec3f camStartPos, camEndPos;
192  SbRotation camStartOrient, camEndOrient;
193 
194  static void prevViewPtCB(Widget, XtPointer, XtPointer);
195  static void nextViewPtCB(Widget, XtPointer, XtPointer);
196  static void saveViewPtCB(Widget, XtPointer, XtPointer);
197  static void abbrOutputCB(Widget, XtPointer, XtPointer);
198  static void pickRefPathCB(Widget, XtPointer, XtPointer);
199  static void constructListsDialog(Widget, XtPointer, XtPointer);
200  void saveViewPt(char *name);
201 
202 
203  static void lookAtSceneElementCB(Widget, XtPointer, XtPointer);
204  static void cancelSceneElementSelectionCB(Widget, XtPointer, XtPointer);
205 
206  void setReferencePath(SoLineSet*, SoCoordinate3*, bool append = false);
207  void setReferencePathZPos();
208  void findAndSetRefPath();
209  SoCoordinate3 *getCoordsNode(SoFullPath *path);
210  void getSceneElements(); // reads elements from the scene graph
211  float sqrlen(const SbVec3f&);
212  void distanceToTrajectory(const SbVec3f&, float&, SbVec3f&, int&);
213  void sortElements();
214  void createElementsList(Widget);
215  static void closeMainWindowCB(Widget, XtPointer, XtPointer);
216  void evenOutRefParticlePts();
217 
218  static void gotoRefPathStartCB(Widget, XtPointer, XtPointer);
219  void gotoRefPathStart();
220  static void invertRefPathCB(Widget, XtPointer, XtPointer);
221  void invertRefPath();
222 
223  enum CameraType {
224  PERSPECTIVE,
225  ORTHOGRAPHIC
226  };
227 
228 
229  enum State {
230  GENERAL,
231  BEAMLINE,
232  VIEWPOINT,
233  ANIMATION,
234  REVERSED_ANIMATION,
235  PAUSED_ANIMATION,
236  ROTATING
237  };
238 
239  // For storing the view point
240  struct viewPtData {
241  char* viewPtName;
242  int viewportMapping;
243  SbVec3f position;
244  SbRotation orientation;
245  float aspectRatio;
246  float nearDistance;
247  float farDistance;
248  float focalDistance;
249  CameraType camType;
250  float height;
251  };
252 
253  // FWJ removed unneeded assignment operator
254  struct sceneElement {
255  std::string name;
256  SoFullPath* path;
257  SbVec3f center;
258  float closestPointZCoord;
259  };
260 
261  struct elementForSorting {
262  float closestPointZCoord;
263  SbVec3f closestPoint;
264  float smallestDistance;
265  float distanceToBeamlineStart;
266  std::string name;
267 
268  bool operator<(elementForSorting const &other) const
269  {
270  if (closestPointZCoord < other.closestPointZCoord)
271  return true;
272  if (closestPointZCoord > other.closestPointZCoord)
273  return false;
274 
275  // otherwise closestPointZCoord == other.closestPointZCoord.
276  // Compare the distances from the center of the element to
277  // the start of the beamline.
278  if (distanceToBeamlineStart < other.distanceToBeamlineStart)
279  return true;
280  if (distanceToBeamlineStart > other.distanceToBeamlineStart)
281  return false;
282 
283  // In case both closestPointZCoord and smallestDistance are
284  // equal, we have two exactly overlapping elements, if so
285  // the order doesn't matter
286  return true;
287  }
288  };
289 
290  bool zcoordSetFlag;
291 
292  std::vector<sceneElement> sceneElements;
293  std::vector<viewPtData> viewPtList;
294  std::string fileName;
295  std::ifstream fileIn;
296  std::ofstream fileOut;
297  int viewPtIdx;
298  int MAX_VP_IDX;
299  int MAX_VP_NAME;
300 
301  // For storing coordinate points of the reference particle
302  std::vector<SbVec3f> refParticleTrajectory;
303  // For displaying distance during anim and beamline modes
304  std::vector<float> refZPositions;
305  int refParticleIdx;
306  int prevRefIdx;
307  float distance;
308  State currentState, prevState, beforePausing;
309  char *curViewPtName;
310 
311  int step;
312  SbVec3f prevPt;
313  SbVec3f prevParticleDir;
314  void * prevColorField;
315 
316  viewPtData camB4Animation;
317  bool returnToSaveVP;
318  bool returnToAnim;
319  SoCamera *myCam;
320  void setStartingPtForAnimation();
321  float left_right, up_down;
322  SbVec3f rotAxis; // For 90 degree rotations
323  int rotCnt; // # of steps rotation is split into
324 
325  static void getViewPtNameCB(Widget, XtPointer, XtPointer);
326  std::string viewPtAutoName();
327 
329  SoSearchAction * searcher;
330 
331  SoNode * superimposition;
332  SoCoordinate3 * sgeometry;
333  SoScale * sscale;
334 
335  SoTranslation * stranslation;
336  SoTranslation * curInfoTrans;
337  SoTranslation * mouseOverTransSolid;
338  SoTranslation * mouseOverTransMaterial;
339  SoTranslation * mouseOverTransLogName;
340  SoTranslation * mouseOverTransZPos;
341 
342  // Used for 2 similar purposes: 1. Displays z position during animation
343  // 2. Displays name of the current viewpoint
344  SoText2 * curInfoText;
345  /* Need to use many different fields for mouseover
346  * because newlines are ignored when the scene is rendered */
347  SoText2 * mouseOverTextSolid;
348  SoText2 * mouseOverTextMaterial;
349  SoText2 * mouseOverTextLogName;
350  SoText2 * mouseOverTextZPos;
351 
352  SoFont * curInfoFont;
353  SoFont * mouseOverFontSolid;
354  SoFont * mouseOverFontMaterial;
355  SoFont * mouseOverFontLogName;
356  SoFont * mouseOverFontZPos;
357  SoSwitch * axisSwitch;
358  SoSwitch * animSpeedOutlineSwitch;
359  SoSwitch * animSpeedSwitch;
360  SoSwitch * curInfoSwitch;
361 
362  SoNode * getSuperimpositionNode(SoNode *, const char * name);
363 
364  void superimpositionEvent(SoAction * action);
365  static void superimpositionCB(void * closure, SoAction * action);
366 
367  virtual void actualRedraw(void);
368  void updateSpeedIndicator(void);
369 
370  float maxSpeed;
372 
373  // FWJ added for Ortho camera
374  float defaultHeight;
375  float defaultHeightAngle;
376 
377  // Used by G4 app during element rotations, stores previous view
378  SbVec3f upVector, offsetFromCenter, center;
379  bool rotUpVec;
380 
381  SoSeparator * newSceneGraph;
382 
383 
384 };
385 #endif /* G4OPENINVENTORXTEXAMINERVIEWER_HH */
const XML_Char * s
Definition: expat.h:262
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
const XML_Char * name
Definition: expat.h:151
void updateViewParams(SoKeyboardEvent::Key)
void parseString(T &t, const std::string &s, bool &error)
#define State(theXInfo)
void moveCamera(float dist=0, bool lookdown=false)
Widget createScale(Widget, char *, int, float)
virtual G4bool Notify(G4ApplicationState requiredState)
G4OpenInventorXtExaminerViewer(Widget parent=NULL, const char *name=NULL, SbBool embed=TRUE, SoXtFullViewer::BuildFlag flag=BUILD_ALL, SoXtViewer::Type type=BROWSER)
bool G4bool
Definition: G4Types.hh:79
void warningMsgDialog(std::string, String, XtCallbackProc)
#define TRUE
Definition: globals.hh:55
HookEventProcState(G4OpenInventorXtExaminerViewer *)
void addButton(Widget menu, std::string name, XtCallbackProc)
int position
Definition: filter.cc:7
bool operator<(const CexmcAngularRange &left, const CexmcAngularRange &right)
virtual SbBool processSoEvent(const SoEvent *const event)
void addEscapeCallback(void(*cb)(void *), void *)
G4ApplicationState
virtual void createViewerButtons(Widget parent, SbPList *buttonlist)