ardev.h

00001 /* -- 2007-05-07 -- 
00002  *  ardev - an augmented reality library for robot developers
00003  *  Copyright 2005-2007 - Toby Collett (ardev _at_ plan9.net.nz)
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
00018  *
00019  */
00020 
00021 // Global Documentation
00022 
00028 #ifndef ARDEV_H
00029 #define ARDEV_H
00030 
00031 
00032 /* This file decleares the 5 basic classes that are used for the 
00033 ardev Augmented Reality Development system interface */
00034 
00035 /* Ardev is a class of static functions that can be used to control the 
00036 debugging process */
00037 
00038 #include <ardev/ardev_types.h>
00039 
00040 #include <pthread.h>
00041 #include <math.h>
00042 #include <list>
00043 #include <GL/gl.h>
00044 
00045 #include <libthjc/geometry.h>
00046 #include <libthjc/matrix.h>
00047 #include <libthjc/misc.h>
00048 
00049 #ifndef PI_OVER_ONEEIGHTY
00050 #define PI_OVER_ONEEIGHTY (M_PI/180.0)
00051 #endif
00052 
00053 #ifndef RTOD
00054 #define RTOD(x) (x*(180.0/M_PI))
00055 #endif
00056 
00057 
00058 using namespace std;
00059 
00060 
00061 
00062 class RenderObject;
00063 class PositionObject;
00064         
00065 
00066 #define RENDERPAIR_POSITION_REVERSE 1
00067 
00069 void ARDevInit(int argc, char **argv);
00070 
00073 class RenderPair
00074 {
00075         public: 
00078                 RenderPair(RenderObject * _R, PositionObject * _P,int _Flags = 0) {Rend = _R; Pos = _P; Flags=_Flags;ThreadInitilised=false;};
00079                 
00081                 bool operator == (const RenderPair & rhs) {return Rend == rhs.Rend && Pos == rhs.Pos;};
00082                 
00083                 RenderObject * Rend; 
00084                 PositionObject * Pos; 
00085                 unsigned int Flags; 
00086                 bool ThreadInitilised; 
00087 };
00088 
00089 
00090 
00091 class OutputObject;
00092 
00097 class ARDev
00098 {
00099         public:
00101                 ARDev();
00103                 ~ARDev() {};
00105                 static int DebugLevel;
00106 
00108                 static int Start(OutputObject * output, const char * Name="default");
00110                 static int Pause(const char * Name="default");
00112                 static int Resume(const char * Name="default");
00114                 static int Stop(const char * Name="default");
00116                 static int Add(RenderPair render, const char * Name="all");
00118                 static int Remove(RenderPair render, const char * Name="all");
00120                 static void Lock();
00122                 static void Unlock();
00123         
00124         private:
00126                 static list<OutputObject*> ActiveARList;        
00128                 static pthread_mutex_t lock;
00129         
00130 };
00131 
00132 // ARDev Objects...all inherit from ARObject
00133 
00139 class ARObject 
00140 {
00141         public:
00142                 ARObject(); 
00143                 virtual ~ARObject(); 
00144                         
00146                 virtual int Initialise(bool Active = true) {if(!(IsActive = Active)) Pause(); initialised = true; return 0;};
00148                 virtual void Terminate() {initialised = false; if (thread) StopThread();};
00149 
00151                 void StartThread();
00153                 void StopThread();
00155                 virtual void * Main() {return NULL;};
00156 
00158                 virtual void Pause();
00160                 virtual void Resume(); 
00161                 
00162                 virtual void Lock(); 
00163                 virtual void Unlock(); 
00164 
00166                 virtual bool Initialised() {return initialised;};
00167 
00168         protected:
00170                 static void * DummyMain(void * ThisPtr);
00171 
00172                 pthread_mutex_t lock; 
00173                 pthread_t thread; 
00174                 bool IsActive; 
00175         
00176                 bool initialised; 
00177         
00178                 friend class ARDev;
00179 };
00180 
00181 
00182 
00183 /* \brief Base class for all Misc Objects 
00184  *
00185  */
00186 typedef ARObject MiscObject;
00187 
00192 class CaptureObject : public ARObject
00193 {
00194         public:
00196         CaptureObject() {};
00198         virtual const ARImage &GetFrame() = 0;
00200         virtual bool Fresh() {return true;};
00202         virtual int GetMaxWidth()=0;
00204         virtual int GetMaxHeight()=0;
00205 };
00206 
00209 class CaptureNull : public CaptureObject
00210 {
00211         public:
00212                 CaptureNull() {Frame.x_size = 640;Frame.y_size = 480;Frame.Allocate();Frame.Erase();};
00214                 const ARImage & GetFrame() {return Frame;};
00216                 virtual int GetMaxWidth() {return 640;};
00218                 virtual int GetMaxHeight() {return 480;};
00219                 
00220                 ARImage Frame;
00221 };
00222 
00226 class CameraObject : public ARObject
00227 {
00228         public:
00230                 CameraObject() {};
00232                 virtual const ARCamera GetCamera() {return GetCameraRef();};
00234                 virtual ARCamera & GetCameraRef() = 0;
00235 };
00236 
00238 class CameraConstant : public CameraObject
00239 {
00240         public:
00241                 CameraConstant(const ARCamera & CamIn = ARCamera()) {Cam = CamIn;};
00243                 ARCamera & GetCameraRef() {return Cam;};
00244                 
00245         private:
00246                 ARCamera Cam;
00247 };
00248 
00250 class PositionObject : public ARObject
00251 {
00252         public:
00254                 PositionObject() {present = true; Next = NULL;};
00256                 virtual ~PositionObject() {};
00258                 virtual ARPosition GetPosition() = 0;
00260                 PositionObject * Next;
00262                 virtual bool Present() {return present;};
00264                 ARPosition GetTransformedPosition();
00266                 bool FullChainPresent();
00267         protected:
00269                 bool present;
00270                 
00271 };
00272 
00274 class PositionNull : public PositionObject
00275 {
00276         public:
00278                 ARPosition GetPosition() {return ARPosition();};
00279 };
00280 
00282 class PositionConstant : public PositionObject
00283 {
00284         public:
00286                 PositionConstant(ARPosition in) {Pos = in;}; 
00288                 virtual ARPosition GetPosition() {return Pos;};
00289                 
00290                 ARPosition Pos;
00291 };
00292 
00294 class PositionRotate : public PositionConstant
00295 {
00296         public:
00298                 PositionRotate(ARPosition in, ARPoint _Rate) : PositionConstant(in) {Rate=_Rate;Timer.GetElapsedDouble();}; 
00300                 ARPosition GetPosition() 
00301                 {
00302                         double Elapsed=Timer.GetElapsedDouble()/1000000;
00303                         Pos.Direction.x += Rate.x * Elapsed;
00304                         Pos.Direction.y += Rate.y * Elapsed;
00305                         Pos.Direction.z += Rate.z * Elapsed;
00306                         return Pos;
00307                 };
00308                 
00309                 ARPoint Rate; //< Rate in radians per second, for each axis
00310                 
00311         protected:
00312                 StopWatch Timer;
00313         
00314 };
00315 
00325 class FrameProcessObject : public ARObject
00326 {
00327         public:
00328                 virtual void ProcessFrame(const ARImage & frame) = 0; 
00329 };
00330 
00331 typedef list<RenderPair> RenderList;
00332 typedef list<FrameProcessObject *> PostProcessList;
00333 typedef list<FrameProcessObject *> PreProcessList;
00334 
00337 class OutputObject : public ARObject
00338 {
00339         public:
00340                 OutputObject(); 
00341                 virtual void RenderFrame(RenderList & List, const ARImage & Frame, const ARCamera & cam, const ARPosition & pos, unsigned int Tick, double * Distance=NULL); 
00342                 virtual const ARImage & GetFrame(); 
00343 
00344                 virtual void ShowFrame() {};
00345         
00346                 int width; 
00347                 int height; 
00348                 unsigned int TexWidth; 
00349                 unsigned int TexHeight; 
00350                 unsigned int Backdrop; 
00351 
00353                 void Add(const RenderPair & render);
00355                 void Remove(const RenderPair & render);
00356 
00358                 void AddPost(FrameProcessObject * out);
00360                 void RemovePost(FrameProcessObject * out);
00361                         
00363                 void AddPre(FrameProcessObject * out);
00365                 void RemovePre(FrameProcessObject * out);
00366 
00367                 const char * Name; 
00368                 bool Alive; 
00369                 bool Paused; 
00370                         
00371         protected:
00372         
00374                 RenderList RenderObjects;
00376                 PostProcessList PostProcessObjects;
00378                 PreProcessList PreProcessObjects;
00379                 
00380         
00381                 CaptureObject * Capture; 
00382                 CameraObject * Camera; 
00383                 PositionObject * CameraPosition; 
00384                 //char * Name; ///< The name of the ARDev Object, used for calls to stop, add and remove
00385 
00386                 void *Main(); 
00387         
00389                 void DrawText(GLint x, GLint y, char * s, GLfloat r, GLfloat g, GLfloat b);
00390                 
00392                 StopWatch TimerFPS;
00393                 StopWatch Timer;
00394                 double FrameRate;
00395                 double OtherTime;
00396                 double CaptureTime;
00397                 double TextureTime;
00398                 double PreprocessTime;
00399                 double RenderTime;
00400                 double PostprocessTime;
00401                 
00402 };
00403 
00404 
00405 
00406 
00409 class RenderObject : public ARObject
00410 {
00411         public:
00413                 RenderObject() {enabled = true; Size=0.1;}; 
00415                 RenderObject(const ARColour & aColour) {enabled = true; Size=0.1; Colour = aColour;}; 
00418                 virtual void ThreadInit() {};
00422                 virtual void RenderBase() {};
00425                 virtual void Render() {};
00428                 virtual void RenderTransparent() {};
00430                 virtual double TraceDistance(const Ray & Offset, const Ray & R, const ARPoint & Rotation);
00433                 virtual bool Enabled() {return enabled;}
00436                 virtual void SetEnabled(const bool EnableState) 
00437                 {
00438                         enabled = EnableState;
00439                         if (!enabled && initialised)
00440                                 Terminate();
00441                         if (enabled && !initialised)
00442                                 Initialise();
00443                 }
00444                 
00445         protected:
00446                 double Size;
00447                 bool enabled;
00448                 ARColour Colour;
00449 };
00450 
00452 class RenderAxes : public RenderObject
00453 {
00454         public:
00456                 void Render() 
00457                 {
00458                         glBegin(GL_LINES);
00459                         glColor3f(0,1,1); // Cyan -> X
00460                         glVertex3f(0,0,0);
00461                         glVertex3f(1.000,0,0);
00462                         glColor3f(1,0,1); // Magenta -> Y
00463                         glVertex3f(0,0,0);
00464                         glVertex3f(0,1.000,0);
00465                         glColor3f(1,1,0); // Yellow -> Z
00466                         glVertex3f(0,0,0);
00467                         glVertex3f(0,0,1.000);
00468                         glEnd();
00469                 }
00470 };
00471         
00472 
00473                         
00474 #endif

SourceForge.net Logo Generated on Sat May 12 15:25:43 2007 for ardev by doxygen 1.5.1