World of Rigid Bodies (WoRB)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
WoRB_TestBed.h
Go to the documentation of this file.
00001 #ifndef _WORB_TESTBED_H_INCLUDED
00002 #define _WORB_TESTBED_H_INCLUDED
00003 
00004 /**
00005  *  @file      WoRB_TestBed.h
00006  *  @brief     Definitions for the WoRB_TestBed, which encapsulates
00007  *             a GLUT based test-bed application based on the WoRB framework.
00008  *  @author    Mikica Kocic
00009  *  @version   0.4
00010  *  @date      2012-05-10
00011  *  @copyright GNU Public License.
00012  */
00013 
00014 #include "WoRB.h"
00015 #include "Utilities.h"
00016 
00017 #include <vector> // for GLUT_Renderer collection
00018 
00019 /////////////////////////////////////////////////////////////////////////////////////////
00020 
00021 /** Encapsulates a rigid body test-bed application.
00022  */
00023 class WoRB_TestBed
00024 {
00025 protected:
00026 
00027     /////////////////////////////////////////////////////////////////////////////////////
00028 
00029     /** Holds the WoRB physics simulation framework.
00030      */
00031     WoRB::WorldOfRigidBodies<256,1024> worb;
00032 
00033     /** Final simulation time, s.
00034      */
00035     double FinalTime;
00036 
00037     /////////////////////////////////////////////////////////////////////////////////////
00038 
00039     typedef std::vector<WoRB::GLUT_Renderer*> RBObjects;
00040 
00041     /** Holds the collection of all rendered rigid bodies in the system.
00042      */
00043     RBObjects Objects;
00044 
00045     /** Holds the ground plane, with geometry of a half-space.
00046      */
00047     WoRB::HalfSpace GroundPlane;
00048 
00049     /** Holds the box walls, with geometry of half-spaces.
00050      */
00051     WoRB::HalfSpace BoxWall[4];
00052 
00053     /** Holds the length of a single tick in the system grid.
00054      */
00055     double GridTickLength;
00056 
00057     /** Holds the number of the ticks in the system grid (bounded by the walls).
00058      */
00059     int GridTicks;
00060 
00061     /////////////////////////////////////////////////////////////////////////////////////
00062 
00063     /** Represents trajectory snapshot item (position and orientation of the rigid body).
00064      * Contains GLUT_Renderer object (with WoRB geometry and rigid body info) and 
00065      * current GL transform matrix at snapshot time.
00066      */
00067     struct TrajectoryItem {
00068         WoRB::GLUT_Renderer* object;
00069         double matrix[ 16 ];
00070     };
00071 
00072     /** Represents a collection of trajectory snapshots.
00073      */
00074     typedef std::vector<TrajectoryItem> Trajectory;
00075 
00076     /** Holds the collection of trajectory snapshots of the rigid bodies in the system.
00077      */
00078     Trajectory Trajectories;
00079 
00080     /////////////////////////////////////////////////////////////////////////////////////
00081 
00082     /** Indicates whether the instance is properly constructed.
00083      */
00084     volatile bool IsInitialized;
00085     
00086     /** Holds window title.
00087      */
00088     const char* WindowTitle;
00089 
00090     /** Holds GLUT window ID.
00091      */
00092     int WindowId;
00093 
00094     /** Indicates whether the application is running.
00095      */
00096     volatile bool IsRunning;
00097 
00098     /** Indicates whether the simulation is running (alternative to being paused).
00099      */
00100     bool IsPaused;
00101 
00102     /** Indicates whether the simulation halts after every time-step.
00103      */
00104     bool AutoPause; 
00105 
00106     /** Indicates whether to display objects in wireframe.
00107      */
00108     bool Wireframe;
00109 
00110     /** Indicates whether to display axes of the rigid bodies.
00111      */
00112     bool ShowBodyAxes;
00113 
00114     /** Indicates whether to show objects mirrored in the floor.
00115      */
00116     bool ShowFloorMirror;
00117 
00118     /** Indicates whether to display contact normals.
00119      */
00120     bool ShowContacts;
00121 
00122     /** Indicates whether to display trajectories.
00123      */
00124     bool ShowTrajectories;
00125 
00126     /** Indicates whether to display the system state variables.
00127      */
00128     bool ShowStateVariables;
00129 
00130     /** Indicates whether to display help info.
00131      */
00132     bool ShowHelp;
00133 
00134     /** Holds the index of an object that is followed by the camera view.
00135      */
00136     unsigned FollowObject;
00137 
00138     /** Holds integrator time-step length, in seconds.
00139      */
00140     double TimeStep;
00141 
00142     /** Holds number of integrator time-steps solved per one video frame.
00143      */
00144     unsigned TimeStepsPerFrame;
00145 
00146     /** Holds number of integrator time-steps solved per one trajectory snapshot.
00147      */
00148     unsigned TimeStepsPerSnapshot;
00149 
00150     /** Holds the camera zoom (distance from the look-at point).
00151      */
00152     double CameraZoom;
00153 
00154     /** Holds the point camera looks at.
00155      */
00156     WoRB::Quaternion CameraLookAt;
00157 
00158     /** Holds the camera angle (phi).
00159      */
00160     double CameraAngle;
00161 
00162     /** Holds the camera elevation (theta).
00163      */
00164     double CameraElevation;
00165 
00166     /** Holds the position of the mouse at the last frame of a drag.
00167      */
00168     struct { double x, y; int button, state; } LastMouse;
00169 
00170     /** Holds time-stamp of the last screen update
00171      */
00172     double LastDisplayTime;
00173 
00174     /** Holds next requested configuration profile.
00175      */
00176     volatile int TestSuite;
00177 
00178     /////////////////////////////////////////////////////////////////////////////////////
00179 
00180 public:
00181 
00182     /** Dummy constructor. Use Initialize() to construct an instance.
00183      */
00184     WoRB_TestBed () 
00185     {
00186         IsInitialized = false;
00187     }
00188 
00189     /** Deallocates all objects.
00190      */
00191     ~WoRB_TestBed ();
00192 
00193     /** Delayed constructor; after all static members are initialized.
00194      */
00195     void Initialize ();
00196 
00197     /** Dumps all parameter values using Printf().
00198      */
00199     void Dump () const;
00200 
00201     /** Runs the simulation and rendering.
00202      */
00203     void Run ();
00204 
00205     /** Clears the current simulation data and prepares a new simulation.
00206       */
00207     void ClearTestBed ();
00208 
00209     /** Initializes the test-bed with the default objects according to selected profile.
00210       */
00211     void ReconfigureTestBed ();
00212 
00213     /** Updates the current state of the scene.
00214      */
00215     void Simulate ();
00216 
00217     /** Processes data calculated, solved and derived during the simulation time-step.
00218      * This virtual method can be used to save simulation data in files
00219      * or to return simulated data to MATLAB for example.
00220      */
00221     virtual void OnProcessData () {}
00222 
00223     /** Renders the current scene
00224      */
00225     void DisplayEventHandler ();
00226 
00227     /** Displays debugging information (like state variables) and short help.
00228      */
00229     void RenderDebugInfo ();
00230 
00231     /** Returns whether application is properly initialized
00232      */
00233     bool IsValid () const
00234     {
00235         return IsInitialized;
00236     }
00237 
00238     /** Creates the GLUT window and initializes the view.
00239      */
00240     void SetupAnimation ();
00241 
00242     /** Sets the projection characteristics of the camera.
00243      */
00244     void SetupProjection ();
00245 
00246     /** Handles on-window-close event.
00247      */
00248     void CloseEventHandler ()
00249     {
00250         IsRunning = false;
00251     }
00252 
00253     /** Handles event when the window has changed size.
00254      */
00255     void ReshapeEventHandler( int width, int height )
00256     {
00257         glViewport( 0, 0, width, height );
00258         SetupProjection ();
00259     }
00260 
00261     /** Called when GLUT detects a key press.
00262      */
00263     void KeyboardEventHandler( unsigned char key );
00264 
00265     /** Called when GLUT detects a function key press.
00266      */
00267     void SpecialKeyEventHandler( int key );
00268 
00269     /** Called when GLUT detects a mouse drag.
00270      */
00271     void MotionEventHandler( int x, int y );
00272 
00273     /** Called when GLUT detects a mouse button press.
00274      */
00275     void MouseEventHandler( int button, int state, int x, int y )
00276     {
00277         // Remember the current mouse state and position
00278         //
00279         LastMouse.button = button;
00280         LastMouse.state = state;
00281         LastMouse.x = x;
00282         LastMouse.y = y;
00283     }
00284 
00285     /** Called when GLUT detects a mouse wheel is spun
00286      */
00287     void MouseWheelEventHandler( int /*wheel*/, int direction, int x, int y )
00288     {
00289         CameraZoom -= direction;
00290         CameraZoom = std::max( 0.01, std::min( 200.0, CameraZoom ) );
00291 
00292         // Remember the current mouse position
00293         //
00294         LastMouse.x = x;
00295         LastMouse.y = y;
00296     }
00297 };
00298 
00299 #endif // _WORB_TESTBED_H_INCLUDED