Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

cam.H

Go to the documentation of this file.
00001 #ifndef CAM_H_IS_INCLUDED
00002 #define CAM_H_IS_INCLUDED
00003 
00004 
00005 #include "disp/base_collide.H"
00006 #include "std/support.H"
00007 #include "mlib/points.H"
00008 #include "disp/gel.H"
00009 #include "disp/bbox.H"
00010 #include "std/stop_watch.H"
00011 
00012 #ifdef WIN32
00013 #define uint UINT
00014 #endif
00015 
00016 
00017 #define CSCREENptr  const SCREENptr
00018 #define CCAMptr     const CAMptr
00019 #define CCAMdata    const CAMdata
00020 #define CCAMdataptr const CAMdataptr
00021 
00022 // would like to use typedef, but this same #define
00023 // occurs in many .H files and so is a pain to fix:
00024 class VIEWptr;
00025 #define CVIEWptr const VIEWptr
00026 
00027 //----------------------------------------------
00028 //
00029 //  CAMdata -
00030 //     contains high-level camera parameters
00031 //  has the ability to generate a matrix that
00032 //  transforms points in world-space to normalized
00033 //  device coordinates (suitable for passing to OGl)
00034 //
00035 //----------------------------------------------
00036 class SCREEN; typedef REFptr<SCREEN> SCREENptr;
00037 class CAM;
00038 class CAMobs;
00039 class REF_CLASS(CAMdata) : public REFcounter {
00040   public:
00041    enum eye { LEFT = 0, RIGHT, MIDDLE, NUM_EYE_POSITIONS };
00042 
00043   protected : 
00044 
00045    CAM*         _cam;
00046    mlib::Wpt    _from;            // camera location
00047    mlib::Wpt    _at;              // point we're looking at
00048    mlib::Wpt    _up;              // point in the "up" direction
00049    mlib::Wpt    _center;          // "center" of interest... different from "at"?
00050    double       _focal;           // distance from _from to the film plane (WC)
00051    double       _width;           // width of film plane rectangle (WC)
00052    double       _height;          // height o film plane rectangle (WC)
00053    bool         _perspective;     // true = perspective, false = orthographic
00054    double       _tilt_up;         // used in film_normal()
00055    double       _tilt_left;
00056    double       _iod;             // interocular distance in ROOM units (inches)
00057    double       _feature_scale;   // size of the data scale in world units
00058 
00059    mlib::Wvec   _film_normal;     // film plane normal used for head tracking 
00060    bool         _film_normal_flag;// indicates film plane normal was explicitly set
00061 
00062    // Indicates camera parameters were loaded from file:
00063    bool         _loaded_from_file; 
00064 
00065    // Auxiliary members
00066    ARRAY<CAMobs *> _cblist;
00067 
00068    bool            _changed, _cached;
00069    bool            _proj_mat_dirty;
00070    bool            _pos_mat_dirty   [NUM_EYE_POSITIONS];
00071    mlib::Wtransf   _proj_matrix;
00072    mlib::Wtransf   _pos_matrix      [NUM_EYE_POSITIONS];
00073 
00074    uint            _stamp;      // incremented whenever changed() is called   
00075    double          _dist;       // cached data...
00076    mlib::Wvec      _at_v, _up_v, _right_v, _pup_v;
00077 
00078    void callback() const;
00079    void cache()    const;
00080    bool film_vectors (mlib::CWvec&, mlib::CWvec&, mlib::Wvec&, mlib::Wvec&) const;
00081    bool film_normal  (mlib::CWvec&, mlib::CWvec&, mlib::Wvec&) const;
00082    bool setup_vectors(mlib::Wvec &,  mlib::Wvec&, mlib::Wpt&,
00083                         mlib::Wvec&, mlib::Wvec&, mlib::Wvec&) const;
00084 
00085   public : 
00086    CAMdata(CAM* = 0);
00087    void      add_cb(CAMobs *cb)         { _cblist.add(cb);}
00088    void      rem_cb(CAMobs *cb)         { _cblist.rem(cb);}
00089  
00090    mlib::CWpt&from()            const   { return _from; }
00091    mlib::CWpt&at()              const   { return _at  ; }
00092    mlib::CWpt&up()              const   { return _up  ; }
00093    mlib::CWpt&center()          const   { return _center; }
00094    double     focal()           const   { return _focal; }
00095    bool       persp()           const   { return _perspective; }
00096    double     width()           const   { return _width; }
00097    double     height()          const   { return _height; }
00098    double     iod()             const   { return _iod; }
00099    mlib::Wvec film_normal()     const   { return _film_normal; }
00100    uint       stamp()           const   { return _stamp; }
00101    double     feature_scale()   const   { return _feature_scale; }
00102    double     distance()        const   { cache(); return _dist; }
00103    mlib::Wvec at_v()            const   { cache(); return _at_v; }
00104    mlib::Wvec up_v()            const   { cache(); return _up_v; }
00105    mlib::Wvec pup_v()           const   { cache(); return _pup_v;}
00106    mlib::Wvec right_v()         const   { cache(); return _right_v;}
00107            
00108    // Setting methods
00109    void      changed();
00110    void      start_manip(); //Notifies all the CAMobs of start of cam manip
00111    void      end_manip();   //Notifies all the CAMobs of end of cam manip
00112            
00113    bool      get_changed()const        { return _changed; }
00114    void      clear_changed_flag()      { _changed = false;}
00115 
00116    void      set_from    (mlib::CWpt &p)     { _from   = p; changed();}
00117    void      set_at      (mlib::CWpt &p)     { _at     = p; changed();}
00118    void      set_up      (mlib::CWpt &p)     { _up     = p; changed();}
00119    void      set_center  (mlib::CWpt &p)     { _center = p; changed();}
00120    void      set_focal   (double f)    { _focal  = f; changed();}
00121    void      set_persp   (bool   p)    { _perspective = p;changed();}
00122    void      set_width   (double w)    { _width  = w; changed();}
00123    void      set_height  (double h)    { _height = h; changed();}
00124    void      set_iod     (double i)    { _iod    = i; changed();}
00125    void      set_film_normal(mlib::CWvec &v) { _film_normal_flag = 1;
00126       _film_normal = v;   changed();}
00127    void      unset_film_normal()       { _film_normal_flag=0;changed();}
00128 
00129    bool     loaded_from_file() const { return _loaded_from_file; }
00130 
00131    void      rotate   (mlib::CWline &, double);
00132    void      swivel   (double);
00133    void      translate(mlib::CWvec &);
00134    bool      operator ==(CCAMdata &d) const;
00135    CAMdata  &operator = (CCAMdata &d);
00136 
00137    // CAMdata::xform() returns the matrix that transforms world to eye
00138    // coordinates, transformed by the given screen and eye specs if
00139    // needed for stereo viewing.  The matrix returned should be loaded
00140    // into the MODELVIEW stack whenever drawing is about to start. This
00141    // is already done by gl_view.C.
00142    virtual mlib::CWtransf& xform(SCREENptr, eye e=MIDDLE) const;
00143 
00144    // CAMdata::projection_xform() returns the matrix that
00145    // transforms eye coordinates to the window coordinates.
00146    virtual mlib::CWtransf&
00147       projection_xform(SCREENptr &, mlib::CNDCpt &,double,double, eye e=MIDDLE) const;
00148 
00149    friend  STDdstream &operator<<(STDdstream &ds, CCAMdataptr &p);
00150    friend  STDdstream &operator>>(STDdstream &ds,  CAMdataptr &p);
00151 
00152    void      notify_callbacks();
00153 };
00154 inline ostream &operator<<(ostream &os, CAMdata *p) 
00155 { return os << "CAM(" << p->from()  << " " << p->at()    << " " <<
00156       p->up()    << " " << p->width() << " " <<
00157       p->height()<<")"; }
00158 
00159 class CAMobs {
00160  public:
00161    virtual ~CAMobs() {}
00162    virtual void notify(CCAMdataptr &data) = 0;
00163    virtual void notify_manip_start(CCAMdataptr &data) { };
00164    virtual void notify_manip_end(CCAMdataptr &data) { };
00165 };
00166 
00167 //----------------------------------------------
00168 //
00169 //  CAM-
00170 //     abstract camera class that contains
00171 //  a high-level camera data object.   This class
00172 //  also encapsulates the passing of camera data
00173 //  to OGl.
00174 //
00175 //----------------------------------------------
00176 class REF_CLASS(CAM): public SCHEDULER {
00177   protected: 
00178    str_ptr      _name;
00179 
00180    CAMdataptr   _data;
00181 
00182    mlib::NDCpt  _min;        // lower left corner of view in NDC coords
00183    double       _width;      // width of display window in NDC coords
00184    double       _height;     // height of display window in NDC coords
00185    double       _zoom;       // zoom factor on XY coordinate system
00186 
00187    bool          _ndc_proj_dirty;
00188    mlib::Wtransf _ndc_proj;
00189    mlib::Wtransf _ndc_proj_inv;
00190 
00191   protected:
00192 
00193   public:
00194    CAM(Cstr_ptr &s):_name(s),//,_data(new CAMdata(this)),
00195       _min(-1,-1),_width(2),_height(2),
00196       _zoom(1), _ndc_proj_dirty(1) { _data = new CAMdata(this);}
00197       virtual           ~CAM()               { }
00198       virtual str_ptr    name()        const { return str_ptr(_name); }
00199 
00200       void     set_aspect(double a);
00201 
00202       CAMdataptr   data()              { return _data; }
00203       CCAMdataptr &data()        const { return _data; }
00204       CAMdata     *data_ptr()          { return _data; }
00205       const CAMdata     *data_ptr()    const { return _data; }
00206       double       aspect()      const { return _height/_width; }
00207       mlib::CNDCpt      &min   ()      const { return _min;  }
00208       double       width ()      const { return _zoom * _width;  }
00209       double       height()      const { return _zoom * _height; }
00210       double       zoom  ()      const { return _zoom; }
00211       mlib::CWtransf    &xform(SCREENptr s=SCREENptr(),
00212                                CAMdata::eye e=CAMdata::MIDDLE) const{
00213          return _data->xform(s,e);}
00214       mlib::CWtransf    &projection_xform(SCREENptr s=SCREENptr(),
00215                                           CAMdata::eye e=CAMdata::MIDDLE) const {
00216          return _data->projection_xform(s,min(),width(),height(),e);}
00217 
00218       void       set_min  (mlib::CNDCpt n) { _min  = n; }
00219       void       set_zoom (double z) { _zoom = z; }
00220       mlib::CWtransf&  ndc_projection() const;
00221       mlib::CWtransf&  ndc_projection_inv() const;
00222       void       data_changed()      { _ndc_proj_dirty = 1; }
00223 
00224       friend  ostream &operator<<(ostream &os, CAM *p) 
00225          { os << p->xform(); return os; }
00226       CAM   &operator = (const CAM &cam);
00227       int    operator == (const CAM &cam);
00228 
00229       mlib::Wvec  film_dir(mlib::CXYpt &p)           const;
00230       mlib::Wpt   xy_to_w (mlib::CXYpt &p, mlib::CWpt &w)  const;
00231       mlib::Wpt   xy_to_w (mlib::CXYpt &p, double d) const;
00232       mlib::Wpt   xy_to_w (mlib::CXYpt &p)           const;
00233       mlib::XYpt  w_to_xy (mlib::CWpt  &p)           const;
00234 };
00235 
00236 // ---------------------------------------------
00237 // CAMhist -
00238 //   history list for the camera.
00239 // ---------------------------------------------
00240 #define CCAMhist const CAMhist
00241 class CAMhist : public LIST<CAMptr> {
00242  public :
00243    CAMhist(int num=100):LIST<CAMptr>(num) {}
00244 };
00245 
00246 // ---------------------------------------------
00247 // 
00248 // SCREEN -
00249 //    virtual base class for a screen description.  examples
00250 // of a screen are desktop monitors, responsive workbenches,
00251 // or cave walls.
00252 // 
00253 // the SCREEN object is responsible for setting up a CAMera's
00254 // parameters to render for a particular SCREEN.
00255 // 
00256 // it also attempts to synchronize mouse interaction
00257 // with BOOTH parameters.
00258 // 
00259 // ---------------------------------------------
00260 #define CSCREENptr const SCREENptr
00261 class REF_CLASS(SCREEN) : public REFcounter
00262 {
00263   protected:
00264    CAMptr  _saved_cam;
00265 
00266   public:
00267    virtual ~SCREEN() {}
00268    SCREEN() { _saved_cam = new CAM("saved cam"); }
00269 
00270    virtual void push_eye_offset (CCAMptr &,CAMdata::eye);
00271    virtual void pop_eye_offset  (const CAM *c);
00272    virtual void config_cam      (CCAMptr &) {}
00273 
00274    virtual mlib::Wvec iod_v() = 0;
00275 };
00276 
00277 class REF_CLASS(SCREENbasic) : public SCREEN
00278 {
00279   protected:
00280    CAMptr   _cam;
00281 
00282   public:
00283    SCREENbasic( CCAMptr &cam ) : _cam(cam) {}
00284 
00285       virtual mlib::Wvec iod_v() {
00286          CCAMdataptr &data = ((CCAMptr &)_cam)->data();
00287          return data->right_v(); 
00288       }
00289 };
00290 
00291 #endif // CAM_H_IS_INCLUDED
00292 
00293 /* end of file cam.H */

Generated on Mon Sep 18 11:39:29 2006 for jot by  doxygen 1.4.4