1 //============================================================================
3 // KRotation screen saver for KDE
4 // Copyright (C) 2004 Georg Drenkhahn
7 //============================================================================
10 #define __ROTATION_H__
19 #include <QResizeEvent>
24 #include <kscreensaver.h>
27 #include "rkodesolver.h"
30 #include "rotationcfg.h"
32 //--------------------------------------------------------------------
34 /** @brief ODE solver for the Euler equations.
36 * Class implements RkOdeSolver<double> to solve the Euler equations of motion
37 * tor the rotating object. */
38 class EulerOdeSolver
: public RkOdeSolver
<double>
41 /** @brief Constructor for the ODE solver for the Euler equations.
42 * @param t Time in seconds, integration variable
43 * @param dt Initial time increment in seconds for integration, auto adjusted
44 * later to guarantee precision
45 * @param _A Moment of inertia along 1. figure axis
46 * @param _B Moment of inertia along 2. figure axis
47 * @param _C Moment of inertia along 3. figure axis
48 * @param _y Vector of 12 elements containing the initial rotation vector
49 * omega (elements 0 to 2), and the initial rotating systems coordinate
50 * vectors e1, e2, e3 (elements 3 to 5, 6 to 8, and 9 to 11).
51 * @param eps Relative precision per integration step, see
52 * RkOdeSolver::RkOdeSolver(). */
59 std::valarray
<double> &_y
,
63 /** @brief ODE function for the Euler equation system
64 * @param x time in seconds
65 * @param y Vector of 12 elements containing the rotation vector omega
66 * (elements 0 to 2), and the rotating systems coordinate vectors e1, e2, e3
67 * (elements 3 to 5, 6 to 8, and 9 to 11).
68 * @return derivation dy/dx */
70 f(const double &x
, const std::valarray
<double> &y
) const;
73 /** Moments of inertia along the three figure axes */
78 //--------------------------------------------------------------------
80 /** @brief GL widget class for the KRotation screen saver
82 * Class implements QGLWidget to display the KRotation screen saver. */
83 class RotationGLWidget
: public QGLWidget
88 /** @brief Constructor of KRotation's GL widget
89 * @param parent parent widget, passed to QGLWidget's constructor
90 * @param name name of widget, passed to QGLWidget's constructor
91 * @param omega current rotation vector
92 * @param e1 x trace data
93 * @param e2 y trace data
94 * @param e3 z trace data
95 * @param J 3 vector with momenta of inertia with respect to the 3 figure
97 RotationGLWidget(QWidget
* parent
, const char* name
,
98 const vec3
<double>& omega
,
99 const std::deque
<vec3
<double> >& e1
,
100 const std::deque
<vec3
<double> >& e2
,
101 const std::deque
<vec3
<double> >& e3
,
102 const vec3
<double>& J
);
105 /** Called if scenery (GL view) must be updated */
106 virtual void paintGL();
107 /** Called if gl widget was resized. Method makes adjustments for new
109 virtual void resizeGL(int w
, int h
);
110 /** Setup the GL enviroment */
111 virtual void initializeGL();
114 /** @brief Draw 3D arrow
115 * @param total_length total length of arrow
116 * @param head_length length of arrow head (cone)
117 * @param base_width width of arrow base
118 * @param head_width width of arrow head (cone)
120 * The arrow is drawn from the coordinates zero point along th z direction.
121 * The cone's tip is located at (0,0,@a total_length). */
122 void myGlArrow(GLfloat total_length
, GLfloat head_length
,
123 GLfloat base_width
, GLfloat head_width
);
124 /** Draw the traces in the GL area */
125 void draw_traces (void);
127 private: // Private attributes
128 /** Eye position distance from coordinate zero point */
130 /** Eye position theta angle from z axis */
132 /** Eye position phi angle (longitude) */
135 vec3
<double> boxSize
;
136 /** GL object list of fixed coordinate systems axses */
138 /** GL object list of rotating coordinate systems axses */
140 /** Light position distance from coordinate zero point */
142 /** Light position theta angle from z axis */
144 /** Light position phi angle (longitude) */
147 /** stores position where the mouse button was pressed down */
148 QPoint mouse_press_pos
;
150 /** Length of the rotating coordinate system axses */
151 GLfloat bodyAxsesLength
;
152 /** Length of the fixed coordinate system axses */
153 GLfloat fixedAxsesLength
;
155 /** The openGL rotation matrix for the box. */
158 /** reference to current rotation vector */
159 const vec3
<double>& omega
;
160 /** reference to x trace values */
161 const std::deque
<vec3
<double> >& e1
;
162 /** reference to y trace values */
163 const std::deque
<vec3
<double> >& e2
;
164 /** reference to z trace values */
165 const std::deque
<vec3
<double> >& e3
;
168 //--------------------------------------------------------------------
170 /** @brief Main class of the KRotation screen saver
172 * This class implements KScreenSaver for the KRotation screen saver. */
173 class KRotationSaver
: public KScreenSaver
178 /** @brief Constructor of the KRotation screen saver object
179 * @param drawable Id of the window in which the screen saver is drawed
181 * Initial settings are read from disk, the GL widget is set up and displayed
182 * and the eq. of motion solver is started. */
183 KRotationSaver(WId drawable
);
184 /** @brief Destructor of the KPendulum screen saver object
186 * Only KPendulumSaver::solver is destoyed. */
188 /** read the saved settings from disk */
190 /** init physical quantities and set up the ode solver */
193 /** Returns length of traces in seconds of visibility, parameter from setup
195 inline double traceLengthSeconds(void) const {return m_traceLengthSeconds
;}
196 /** Sets the length of traces in seconds of visibility. */
197 void setTraceLengthSeconds(const double& t
);
198 /** Lower argument limit for setTraceLengthSeconds() */
199 static const double traceLengthSecondsLimitLower
;
200 /** Upper argument limit for setTraceLengthSeconds() */
201 static const double traceLengthSecondsLimitUpper
;
202 /** Default value of KRotationSaver::m_traceLengthSeconds */
203 static const double traceLengthSecondsDefault
;
205 /** Flags indicating if the traces for x,y,z are shown. Only relevant if
206 * ::randomTraces is not set to true. Parameter from setup dialog */
207 inline bool traceFlag(unsigned int n
) const {return m_traceFlag
[n
];}
208 /** (Un)Sets the x,y,z traces flags. */
209 inline void setTraceFlag(unsigned int n
, const bool& flag
)
210 {m_traceFlag
[n
] = flag
;}
211 /** Default values for KRotationSaver::m_traceFlag */
212 static const bool traceFlagDefault
[3];
214 /** If flag is set to true the traces will be (de)activated randomly all 10
215 * seconds. Parameter from setup dialog */
216 inline bool randomTraces(void) const {return m_randomTraces
;}
217 /** (Un)Sets the random trace flag. */
218 inline void setRandomTraces(const bool& flag
) {m_randomTraces
= flag
;}
219 /** Default value for KRotationSaver::m_randomTraces */
220 static const bool randomTracesDefault
= true;
222 /** Returns the angular momentum. */
223 inline double Lz(void) const {return m_Lz
;}
224 /** Sets the angular momentum. */
225 void setLz(const double& Lz
);
226 /** Lower argument limit for setLz() */
227 static const double LzLimitLower
;
228 /** Upper argument limit for setLz() */
229 static const double LzLimitUpper
;
230 /** Default value for KRotationSaver::m_Lz */
231 static const double LzDefault
;
233 /** Returns initial eulerian angle theta of the top body at t=0 sec. */
234 inline double initEulerTheta(void) const {return m_initEulerTheta
;}
235 /** Set the initial eulerian angle theta of the top body at t=0 sec. */
236 void setInitEulerTheta(const double& theta
);
237 /** Lower argument limit for setInitEulerTheta() */
238 static const double initEulerThetaLimitLower
;
239 /** Upper argument limit for setInitEulerTheta() */
240 static const double initEulerThetaLimitUpper
;
241 /** Default value for KRotationSaver::m_initEulerTheta */
242 static const double initEulerThetaDefault
;
245 /** slot is called if integration should proceed by ::deltaT */
247 /** slot is called if setup dialog changes in size and the GL area should be
249 void resizeGlArea(QResizeEvent
* e
);
252 /** Momentum of inertia along figure axes */
254 /** Initial eulerian angles phi of the top body at t=0s */
256 /** Initial eulerian angles psi of the top body at t=0s */
259 /** The ode solver which is used to integrate the equations of motion */
260 EulerOdeSolver
* solver
;
261 /** Gl widget of simulation */
262 RotationGLWidget
* glArea
;
263 /** Timer for the real time integration of the Euler equations */
266 /** current rotation vector */
268 /** deque of unit vectors of e1 figure axes in fixed frame coordinates */
269 std::deque
<vec3
<double> > e1
;
270 /** deque of unit vectors of e2 figure axes in fixed frame coordinates */
271 std::deque
<vec3
<double> > e2
;
272 /** deque of unit vectors of e3 figure axes in fixed frame coordinates */
273 std::deque
<vec3
<double> > e3
;
275 /** Time step size for the integration in milliseconds. Used in
276 * ::KRotationSaver and ::RotationGLWidget. */
277 static const unsigned int deltaT
= 20;
279 /** Length of traces in seconds of visibility, parameter from setup dialog */
280 double m_traceLengthSeconds
;
281 /** Flags indicating if the traces for x,y,z are shown. Only relevant if
282 * ::randomTraces is not set to true. Parameter from setup dialog */
284 /** If flag is set to true the traces will be (de)activated randomly all 10
285 * seconds. Parameter from setup dialog */
287 /** Angular momentum. This is a constant of motion and points always into
288 * positive z direction. Parameter from setup dialog */
290 /** Initial eulerian angles theta of the top body at t=0 sec. Parameter from
292 double m_initEulerTheta
;
295 //--------------------------------------------------------------------
297 /** @brief KRotation screen saver setup dialog.
299 * This class handles the KRotation screen saver setup dialog. */
300 class KRotationSetup
: public KRotationSetupUi
305 KRotationSetup(QWidget
* parent
= NULL
, const char* name
= NULL
);
309 /// slot for the OK Button: save settings and exit
310 void okButtonClickedSlot(void);
311 /// slot for the About Button: show the About dialog
312 void aboutButtonClickedSlot(void);
313 void randomTracesToggled(bool state
);
314 void xTraceToggled(bool state
);
315 void yTraceToggled(bool state
);
316 void zTraceToggled(bool state
);
317 void lengthEnteredSlot(const QString
& s
);
318 void LzEnteredSlot(const QString
& s
);
319 void thetaEnteredSlot(const QString
& s
);
322 /// the screen saver widget which is displayed in the preview area
323 KRotationSaver
* saver
;