Back to index

kdeartwork  4.3.2
Public Slots | Public Member Functions | Static Public Attributes | Private Attributes | Static Private Attributes
KPendulumSaver Class Reference

Main class of the KPendulum screen saver. More...

#include <pendulum.h>

Collaboration diagram for KPendulumSaver:
Collaboration graph
[legend]

List of all members.

Public Slots

void doTimeStep ()
 slot is called if integration should proceed by ::deltaT
void resizeGlArea (QResizeEvent *e)
 slot is called if setup dialog changes in size and the GL are should be adjusted

Public Member Functions

 KPendulumSaver (WId drawable)
 Constructor of the KPendulum screen saver object.
 ~KPendulumSaver ()
 Destructor of the KPendulum screen saver object.
void readSettings ()
 read the saved settings from disk
void initData ()
 init physical quantities, set up the GL area and (re)start the ode solver.
void setBarColor (const QColor &c)
 Set the displayed bar color of the pendulum.
QColor barColor (void) const
 Get the displayed bar color of the pendulum.
void setM1Color (const QColor &c)
 Set the displayed color of the 1.
QColor m1Color (void) const
 Get the displayed color of the 1.
void setM2Color (const QColor &c)
 Set the displayed color of the 2.
QColor m2Color (void) const
 Get the displayed color of the 2.
void setMassRatio (const double &massRatio)
 Set the mass ratio of the pendulum system.
double massRatio (void) const
 Get the mass ratio of the pendulum system.
void setLengthRatio (const double &lengthRatio)
 Set the length ratio of the pendulum system.
double lengthRatio (void) const
 Get the length ratio of the pendulum system.
void setG (const double &g)
 Set the gravitational constant.
double g (void) const
 Get the gravitational constant.
void setE (const double &E)
 Set the total energy.
double E (void) const
 Get the total energy.
void setPersChangeInterval (const unsigned int &persChangeInterval)
 Set the time interval for the periodic perspective change.
unsigned int persChangeInterval (void) const
 Get the time interval for the periodic perspective change.

Static Public Attributes

static const QColor sm_barColorDefault
static const QColor sm_m1ColorDefault
static const QColor sm_m2ColorDefault
static const double sm_massRatioLimitUpper = 0.99
 lower, upper limits (inclusive) and default values for the setup parameter massRatio
static const double sm_massRatioLimitLower = 0.01
static const double sm_massRatioDefault = 0.5
static const double sm_lengthRatioLimitLower = 0.01
 lower, upper limits (inclusive) and default values for the setup parameter lengthRatio
static const double sm_lengthRatioLimitUpper = 0.99
static const double sm_lengthRatioDefault = 0.5
static const double sm_gLimitLower = 0.1
 lower, upper limits (inclusive) and default values for the setup parameter g
static const double sm_gLimitUpper = 300.0
static const double sm_gDefault = 40.0
static const double sm_ELimitLower = 0.0
 lower, upper limits (inclusive) and default values for the setup parameter E
static const double sm_ELimitUpper = 5.0
static const double sm_EDefault = 1.2
static const unsigned int sm_persChangeIntervalLimitLower = 5
 lower, upper limits (inclusive) and default values for the setup parameter persChangeInterval
static const unsigned int sm_persChangeIntervalLimitUpper = 600
static const unsigned int sm_persChangeIntervalDefault = 15

Private Attributes

PendulumOdeSolverm_solver
 The ode solver which is used to integrate the equations of motion.
PendulumGLWidgetm_glArea
 Gl widget of simulation.
QTimer * m_timer
 Timer for the real time integration of the eqs.
double m_massRatio
 Mass ratio m2/(m1+m2) of the pendulum masses.
double m_lengthRatio
 Length ratio l2/(l1+l2) of the pendulums.
double m_g
 Gravitational constant (in arbitrary units).
double m_E
 Total energy of the system in units of the maximum possible potential energy.
unsigned int m_persChangeInterval
 Time interval after which a new perspective changed happens.

Static Private Attributes

static const unsigned int sm_deltaT = 20
 Time step size for the integration in milliseconds.
static const double sm_eyePhiDefault = 0.25 * M_PI
 Default angle phi for the eye to look onto the pendulum.

Detailed Description

Main class of the KPendulum screen saver.

This class implements KScreenSaver for the KPendulum screen saver.

Definition at line 194 of file pendulum.h.


Constructor & Destructor Documentation

KPendulumSaver::KPendulumSaver ( WId  drawable)

Constructor of the KPendulum screen saver object.

Parameters:
drawableId of the window in which the screen saver is drawed

Initial settings are read from disk, the GL widget is set up and displayed and the eq. of motion solver is started.

Definition at line 393 of file pendulum.cpp.

                                     :
   KScreenSaver(id),
   m_solver(0),
   m_massRatio(sm_massRatioDefault),
   m_lengthRatio(sm_lengthRatioDefault),
   m_g(sm_gDefault),
   m_E(sm_EDefault),
   m_persChangeInterval(sm_persChangeIntervalDefault)
{
   // no need to set our parent widget's background here, the GL widget sets its
   // own background color


   m_glArea = new PendulumGLWidget(); // create gl widget w/o parent
   m_glArea->setEyePhi(sm_eyePhiDefault);
   readSettings();              // read global settings into pars


   // init m_glArea with read settings, construct and init m_solver
   initData();

   embed(m_glArea);               // embed gl widget and resize it
   m_glArea->show();              // show embedded gl widget

   // set up and start cyclic timer
   m_timer = new QTimer(this);
   m_timer->start(sm_deltaT);
   connect(m_timer, SIGNAL(timeout()), this, SLOT(doTimeStep()));
}

Here is the call graph for this function:

Destructor of the KPendulum screen saver object.

Only KPendulumSaver::solver is destoyed.

Definition at line 423 of file pendulum.cpp.

{
   m_timer->stop();

   // m_timer is automatically deleted with parent KPendulumSaver
   delete m_solver;
   delete m_glArea;
}

Member Function Documentation

QColor KPendulumSaver::barColor ( void  ) const

Get the displayed bar color of the pendulum.

Definition at line 515 of file pendulum.cpp.

{
   return m_glArea->barColor();
}

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::doTimeStep ( ) [slot]

slot is called if integration should proceed by ::deltaT

Definition at line 610 of file pendulum.cpp.

{
   /* time (in seconds) of perspective change.
    * - t<0: no change yet
    * - t=0: change starts
    * - 0<t<moving time: change takes place
    * - t=moving time: end of the change */
   static double persChangeTime = -5;

   // integrate a step ahead
   m_solver->integrate(0.001 * sm_deltaT);

   // read new y from solver
   const Vector4d& y = m_solver->Y();

   // tell glArea the new coordinates/angles of the pendulum
   m_glArea->setAngles(y[0], y[1]);

   // handle perspective change
   persChangeTime += 0.001 * sm_deltaT;
   if (persChangeTime > 0)
   {
      // phi value at the start of a perspective change
      static double eyePhi0     = sm_eyePhiDefault;
      // phi value at the end of a perspective change
      static double eyePhi1     = 0.75*M_PI;
      static double deltaEyePhi = eyePhi1-eyePhi0;

      // movement acceleration/deceleration
      const double a = 3;
      // duration of the change period
      const double movingTime = 2.*std::sqrt(std::abs(deltaEyePhi)/a);

      // new current phi of eye
      double eyePhi = persChangeTime < 0.5*movingTime ?
         // accelerating phase
         eyePhi0 + (deltaEyePhi>0?1:-1)
         * 0.5*a*persChangeTime*persChangeTime:
         // decellerating phase
         eyePhi1 - (deltaEyePhi>0?1:-1)
         * 0.5*a*(movingTime-persChangeTime)*(movingTime-persChangeTime);

      if (persChangeTime > movingTime)
      {  // perspective change has finished
         // set new time till next change
         persChangeTime = -double(m_persChangeInterval);
         eyePhi0 = eyePhi = eyePhi1;
         // find new phi value with angleLimit < phi < Pi-angleLimit or
         // Pi+angleLimit < phi < 2*Pi-angleLimit
         const double angleLimit = M_PI*0.2;
         for (eyePhi1 = 0;
              (eyePhi1<angleLimit)
                 || ((eyePhi1<M_PI+angleLimit) && (eyePhi1>M_PI-angleLimit))
                 || (eyePhi1>2*M_PI-angleLimit);
              eyePhi1 = double(rand())/RAND_MAX * 2*M_PI)
         {
         }
         // new delta phi for next change
         deltaEyePhi = eyePhi1 - eyePhi0;
         // find shortest perspective change
         if (deltaEyePhi < -M_PI)
         {
            deltaEyePhi += 2*M_PI;
         }
      }

      m_glArea->setEyePhi(eyePhi); // set new perspective
   }

   m_glArea->updateGL();          // repaint scenery

   // restarting timer not necessary here, it is a cyclic timer
}

Here is the call graph for this function:

Here is the caller graph for this function:

double KPendulumSaver::E ( void  ) const [inline]

Get the total energy.

See also:
KPendulumSaver::m_E

Definition at line 267 of file pendulum.h.

      {
         return m_E;
      }

Here is the caller graph for this function:

double KPendulumSaver::g ( void  ) const [inline]

Get the gravitational constant.

See also:
KPendulumSaver::m_g

Definition at line 259 of file pendulum.h.

      {
         return m_g;
      }

Here is the caller graph for this function:

init physical quantities, set up the GL area and (re)start the ode solver.

Called if new parameters are specified in the setup dialog and at startup.

Definition at line 467 of file pendulum.cpp.

{
   const double m1plusm2 = 2;   // m1+m2
   const double m2       = m_massRatio * m1plusm2;
   const double m1       = m1plusm2 - m2;
   m_glArea->setMasses(m1, m2);
   m_glArea->setAngles(0, 0);

   const double l1plusl2 = 9;   // l1+l2
   const double l2       = m_lengthRatio * l1plusl2;
   const double l1       = l1plusl2 - l2;
   m_glArea->setLengths(l1, l2);

   // kinetic energy of m2 and m1
   const double kin_energy = m_E * m_g * (l1*m1 + (m1+m2)*(l1+l2));
   // angular velocity for 1. and 2. pendulum
   const double qp = std::sqrt(2.*kin_energy/((m1+m2)*l1*l1 + m2*l2*l2 + m2*l1*l2));

   // assemble initial y for solver
   Vector4d y;
   y[0] = 0;                                  // q1
   y[1] = 0;                                  // q2
   y[2] = (m1+m2)*l1*l1*qp + 0.5*m2*l1*l2*qp; // p1
   y[3] = m2*l2*l2*qp + 0.5*m2*l1*l2*qp;      // p2

   // delete old solver
   if (m_solver!=0)
   {
      delete m_solver;
   }
   // init new solver
   m_solver = new PendulumOdeSolver(
      0.0,                      // t
      0.01,                     // first dt step size estimation
      y,
      1e-5,                     // eps
      m1,
      m2,
      l1,
      l2,
      m_g);
}

Here is the call graph for this function:

Here is the caller graph for this function:

double KPendulumSaver::lengthRatio ( void  ) const [inline]

Get the length ratio of the pendulum system.

See also:
KPendulumSaver::m_lengthRatio

Definition at line 251 of file pendulum.h.

      {
         return m_lengthRatio;
      }

Here is the caller graph for this function:

QColor KPendulumSaver::m1Color ( void  ) const

Get the displayed color of the 1.

pendulum mass

Definition at line 524 of file pendulum.cpp.

{
   return m_glArea->m1Color();
}

Here is the call graph for this function:

Here is the caller graph for this function:

QColor KPendulumSaver::m2Color ( void  ) const

Get the displayed color of the 2.

pendulum mass

Definition at line 533 of file pendulum.cpp.

{
   return m_glArea->m2Color();
}

Here is the call graph for this function:

Here is the caller graph for this function:

double KPendulumSaver::massRatio ( void  ) const [inline]

Get the mass ratio of the pendulum system.

See also:
KPendulumSaver::m_massRatio

Definition at line 241 of file pendulum.h.

      {
         return m_massRatio;
      }

Here is the caller graph for this function:

unsigned int KPendulumSaver::persChangeInterval ( void  ) const [inline]

Get the time interval for the periodic perspective change.

See also:
KPendulumSaver::m_persChangeInterval

Definition at line 277 of file pendulum.h.

      {
         return m_persChangeInterval;
      }

Here is the caller graph for this function:

read the saved settings from disk

Definition at line 433 of file pendulum.cpp.

{
   // read configuration settings from config file
   KConfigGroup config(KGlobal::config(), "Settings");

   // internal saver parameters are set to stored values or left at their
   // default values if stored values are out of range
   setMassRatio(
      config.readEntry(
         "mass ratio",
         KPendulumSaver::sm_massRatioDefault));
   setLengthRatio(
      config.readEntry(
         "length ratio",
         KPendulumSaver::sm_lengthRatioDefault));
   setG(
      config.readEntry(
         "g",
         KPendulumSaver::sm_gDefault));
   setE(
      config.readEntry(
         "E",
         KPendulumSaver::sm_EDefault));
   setPersChangeInterval(
      config.readEntry(
         "perspective change interval",
         (uint)KPendulumSaver::sm_persChangeIntervalDefault));

   // set the colours
   setBarColor(config.readEntry("bar color", sm_barColorDefault));
   setM1Color( config.readEntry("m1 color",  sm_m1ColorDefault));
   setM2Color( config.readEntry("m2 color",  sm_m2ColorDefault));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::resizeGlArea ( QResizeEvent *  e) [slot]

slot is called if setup dialog changes in size and the GL are should be adjusted

Definition at line 686 of file pendulum.cpp.

{
   m_glArea->resize(e->size());
}
void KPendulumSaver::setBarColor ( const QColor &  c)

Set the displayed bar color of the pendulum.

Definition at line 511 of file pendulum.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::setE ( const double &  E)

Set the total energy.

See also:
KPendulumSaver::m_E

Definition at line 584 of file pendulum.cpp.

{
   if ((E >= sm_ELimitLower)
       && (E <= sm_ELimitUpper)
       && (m_E != E))
   {
      m_E = E;
      if (m_timer!=0)
      {
         initData();
      }
   }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::setG ( const double &  g)

Set the gravitational constant.

See also:
KPendulumSaver::m_g

Definition at line 570 of file pendulum.cpp.

{
   if ((g >= sm_gLimitLower)
       && (g <= sm_gLimitUpper)
       && (m_g != g))
   {
      m_g = g;
      if (m_timer!=0)
      {
         initData();
      }
   }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::setLengthRatio ( const double &  lengthRatio)

Set the length ratio of the pendulum system.

See also:
KPendulumSaver::m_lengthRatio

Definition at line 556 of file pendulum.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::setM1Color ( const QColor &  c)

Set the displayed color of the 1.

pendulum mass

Definition at line 520 of file pendulum.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::setM2Color ( const QColor &  c)

Set the displayed color of the 2.

pendulum mass

Definition at line 529 of file pendulum.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::setMassRatio ( const double &  massRatio)

Set the mass ratio of the pendulum system.

See also:
KPendulumSaver::m_massRatio

Definition at line 539 of file pendulum.cpp.

{
   // range check is not necessary in normal operation because validators check
   // the values at input.  But the validators do not check for corrupted
   // settings read from disk.
   if ((massRatio >= sm_massRatioLimitLower)
       && (massRatio <= sm_massRatioLimitUpper)
       && (m_massRatio != massRatio))
   {
      m_massRatio = massRatio;
      if (m_timer!=0)
      {
         initData();
      }
   }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void KPendulumSaver::setPersChangeInterval ( const unsigned int &  persChangeInterval)

Set the time interval for the periodic perspective change.

See also:
KPendulumSaver::m_persChangeInterval

Definition at line 598 of file pendulum.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

double KPendulumSaver::m_E [private]

Total energy of the system in units of the maximum possible potential energy.

Value is determined by the setup dialog. Variable is accessed by setE() and E().

Definition at line 354 of file pendulum.h.

double KPendulumSaver::m_g [private]

Gravitational constant (in arbitrary units).

Value is determined by the setup dialog. Variable is accessed by setG() and g().

Definition at line 350 of file pendulum.h.

Gl widget of simulation.

Definition at line 335 of file pendulum.h.

Length ratio l2/(l1+l2) of the pendulums.

Value is determined by the setup dialog. Variable is accessed by setLengthRatio() and lengthRatio().

Definition at line 347 of file pendulum.h.

double KPendulumSaver::m_massRatio [private]

Mass ratio m2/(m1+m2) of the pendulum masses.

Value is determined by the setup dialog. Variable is accessed by setMassRatio() and massRatio().

Definition at line 343 of file pendulum.h.

unsigned int KPendulumSaver::m_persChangeInterval [private]

Time interval after which a new perspective changed happens.

Value is determined by the setup dialog. Variable is accessed by setPersChangeInterval() and persChangeInterval().

Definition at line 358 of file pendulum.h.

The ode solver which is used to integrate the equations of motion.

Definition at line 333 of file pendulum.h.

QTimer* KPendulumSaver::m_timer [private]

Timer for the real time integration of the eqs.

of motion

Definition at line 337 of file pendulum.h.

const QColor KPendulumSaver::sm_barColorDefault [static]

Definition at line 284 of file pendulum.h.

const unsigned int KPendulumSaver::sm_deltaT = 20 [static, private]

Time step size for the integration in milliseconds.

20 ms corresponds to a frame rate of 50 fps.

Definition at line 328 of file pendulum.h.

const double KPendulumSaver::sm_EDefault = 1.2 [static]

Definition at line 310 of file pendulum.h.

const double KPendulumSaver::sm_ELimitLower = 0.0 [static]

lower, upper limits (inclusive) and default values for the setup parameter E

Definition at line 308 of file pendulum.h.

const double KPendulumSaver::sm_ELimitUpper = 5.0 [static]

Definition at line 309 of file pendulum.h.

const double KPendulumSaver::sm_eyePhiDefault = 0.25 * M_PI [static, private]

Default angle phi for the eye to look onto the pendulum.

Definition at line 330 of file pendulum.h.

const double KPendulumSaver::sm_gDefault = 40.0 [static]

Definition at line 304 of file pendulum.h.

const double KPendulumSaver::sm_gLimitLower = 0.1 [static]

lower, upper limits (inclusive) and default values for the setup parameter g

Definition at line 302 of file pendulum.h.

const double KPendulumSaver::sm_gLimitUpper = 300.0 [static]

Definition at line 303 of file pendulum.h.

const double KPendulumSaver::sm_lengthRatioDefault = 0.5 [static]

Definition at line 298 of file pendulum.h.

const double KPendulumSaver::sm_lengthRatioLimitLower = 0.01 [static]

lower, upper limits (inclusive) and default values for the setup parameter lengthRatio

Definition at line 296 of file pendulum.h.

const double KPendulumSaver::sm_lengthRatioLimitUpper = 0.99 [static]

Definition at line 297 of file pendulum.h.

const QColor KPendulumSaver::sm_m1ColorDefault [static]

Definition at line 285 of file pendulum.h.

const QColor KPendulumSaver::sm_m2ColorDefault [static]

Definition at line 286 of file pendulum.h.

const double KPendulumSaver::sm_massRatioDefault = 0.5 [static]

Definition at line 292 of file pendulum.h.

const double KPendulumSaver::sm_massRatioLimitLower = 0.01 [static]

Definition at line 291 of file pendulum.h.

const double KPendulumSaver::sm_massRatioLimitUpper = 0.99 [static]

lower, upper limits (inclusive) and default values for the setup parameter massRatio

Definition at line 290 of file pendulum.h.

const unsigned int KPendulumSaver::sm_persChangeIntervalDefault = 15 [static]

Definition at line 316 of file pendulum.h.

const unsigned int KPendulumSaver::sm_persChangeIntervalLimitLower = 5 [static]

lower, upper limits (inclusive) and default values for the setup parameter persChangeInterval

Definition at line 314 of file pendulum.h.

const unsigned int KPendulumSaver::sm_persChangeIntervalLimitUpper = 600 [static]

Definition at line 315 of file pendulum.h.


The documentation for this class was generated from the following files: