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