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

manip.C

Go to the documentation of this file.
00001 #include "std/support.H"
00002 #include "disp/ray.H"
00003 #include "geom/fsa.H"
00004 #include "geom/geom.H"
00005 #include "geom/world.H"
00006 #include "manip/manip.H"
00007 
00008 using mlib::Wpt;
00009 using mlib::CWpt;
00010 using mlib::Wvec;
00011 using mlib::CWvec;
00012 using mlib::Wline;
00013 using mlib::Wtransf;
00014 using mlib::XYpt;
00015 using mlib::CXYpt;
00016 
00017 /* -------------------------------------------------------------
00018  *
00019  * Simple_int : base 3-state FSA
00020  *
00021  *   This base class for an interactor is an FSA that calls 
00022  * virtual functions for down(), motion(), and up() events.
00023  *
00024  * ------------------------------------------------------------- */
00025 Simple_int::Simple_int(
00026    CEvent &down_ev, // Definition of the Down event
00027    CEvent &move_ev, //                   Move
00028    CEvent &up_ev    //                   Up
00029    ) 
00030 {
00031    static Cstr_ptr default_entry_name("Simple_int Entry");
00032    static Cstr_ptr default_move_name ("Simple_int Move");
00033    _entry     .set_name(default_entry_name);
00034    _manip_move.set_name(default_move_name);
00035 
00036    _manip_move += Arc(move_ev, Cb(&Simple_int::move));
00037    _manip_move += Arc(up_ev,   Cb(&Simple_int::up,   (State *)-1));
00038    _entry      += Arc(down_ev, Cb(&Simple_int::down, &_manip_move));
00039 }
00040 
00041 void
00042 Simple_int::add_events(
00043    CEvent &down_ev, // Definition of the Down event
00044    CEvent &move_ev, //                   Move
00045    CEvent &up_ev    //                   Up
00046    )
00047 {
00048    _manip_move += Arc(move_ev, Cb(&Simple_int::move));
00049    _manip_move += Arc(up_ev,   Cb(&Simple_int::up,    (State *)-1));
00050    _entry      += Arc(down_ev, Cb(&Simple_int::down, &_manip_move));
00051 }
00052 
00053 /* -------------------------------------------------------------
00054  *
00055  * Key_int : base keyboard interactor
00056  *
00057  *   Provides callbacks when any registered keyboard key is
00058  * pressed or released.   This interactor adds an arc to the
00059  * start state for each key.  When the key down event occurs, 
00060  * this interactor transitions to the _key_down state.  When 
00061  * the key up event occurs, the interactor goes back to the 
00062  * start state.
00063  *
00064  * ------------------------------------------------------------- */
00065 void
00066 Key_int::add_event(
00067    char  *k,
00068    State *start     // where to go when this FSA finishes
00069    )
00070 {
00071    for (char *c = k; *c; c++) {
00072       Event  down_ev(NULL, Evd(*c, KEYD));
00073       Event  up_ev  (NULL, Evd(*c, KEYU));
00074       _key_down += Arc(up_ev,   Cb(&Key_int::up,    start));
00075       _entry    += Arc(down_ev, Cb(&Key_int::down, &_key_down));
00076    }
00077 }
00078 
00079 void
00080 Key_int::add_event(
00081    char   k,
00082    State *start    // where to go when this FSA finishes
00083    )
00084 {
00085    Event  down_ev(NULL, Evd(k, KEYD));
00086    Event  up_ev  (NULL, Evd(k, KEYU));
00087    _key_down += Arc(up_ev,   Cb(&Key_int::up,    start));
00088    _entry    += Arc(down_ev, Cb(&Key_int::down, &_key_down));
00089 }
00090 
00091 
00092 
00093 
00094 FilmTrans::FilmTrans(
00095    CEvent    &d,
00096    CEvent    &m,
00097    CEvent    &u
00098    ):Simple_int(d,m,u), _call_xform_obs(true)
00099 {
00100    _entry     .set_name("FilmTrans Entry");
00101    _manip_move.set_name("FilmTrans Move");
00102 }
00103 
00104 int
00105 FilmTrans::down(
00106    CEvent &e,
00107    State *&s
00108    )
00109 {
00110    DEVice_buttons *btns=(DEVice_buttons *)e._d;
00111    DEVice_2d      *ptr=btns->ptr2d();
00112    RAYhit          r(ptr->cur());
00113    GEOMptr         geom;
00114    
00115    if (e.view()->intersect(r,VIEW::H_TEXT).success() && GEOM::isa(r.geom())) {
00116       geom = ray_geom(r, GEOM::null);
00117       if (geom->interactive(e, s, &r)) {
00118          _obj      = 0;
00119          _no_xform = 1;
00120          return 1; // Interactive - return....
00121       }
00122    }
00123    // interactive() could have changed r so that now !success(),
00124    //  so we have to check again
00125    if (!r.success() || (geom && NO_XFORM_MOD.get(geom))) {
00126       _no_xform = 1;
00127       _obj      = geom;
00128    } else {
00129       if (CONSTRAINT.get(geom) == GEOM::SCREEN_WIDGET)
00130            _down_pt = geom->xform();
00131       else _down_pt = r.surf();
00132 
00133       _down_norm = r.norm();
00134       _obj       = geom;
00135 
00136       if (_call_xform_obs)
00137          XFORMobs::notify_xform_obs(_obj, XFORMobs::START);
00138       _no_xform = 0;
00139    }
00140 
00141    return 0;
00142 }
00143 
00144 int
00145 FilmTrans::move(
00146    CEvent &e,
00147    State *&
00148    )
00149 {
00150    DEVice_2d  *ptr=(DEVice_2d *)e._d;
00151    CAMptr cam(e.view()->cam());
00152    if (_obj) {
00153       Wpt cur(Wpt(ptr->cur(),_down_pt));
00154       Wpt old(Wpt(ptr->old(),_down_pt));
00155       _obj->mult_by(Wtransf::translation(cur-old));
00156       if (_call_xform_obs)
00157          XFORMobs::notify_xform_obs(_obj, XFORMobs::MIDDLE);
00158    }
00159 
00160    return 0;
00161 }
00162 
00163 
00164 int
00165 FilmTrans::up(
00166    CEvent &,
00167    State *&
00168    )
00169 {
00170    // tell observers the ordeal is over
00171    if (_obj && _call_xform_obs)
00172       XFORMobs::notify_xform_obs(_obj, XFORMobs::END);
00173 
00174    // forget this obj
00175    _obj = (GEOMptr) 0;
00176 
00177    return 0;
00178 }
00179 
00180 
00181 void
00182 scale_along_normal(
00183    CGEOMptr &obj,
00184    CWpt     &scale_cent,
00185    CWpt     &down_pt,
00186    CWvec    &down_norm,
00187    CXYpt    &cur
00188    )
00189 {
00190    Wtransf xf  (obj->xform());
00191    Wpt     dpt (xf * down_pt);
00192    Wpt     spt (xf * scale_cent);
00193    Wvec    upv ((xf * down_norm).normalized());
00194 
00195    if (upv * (dpt - spt) < 0)
00196       upv = -upv;
00197 
00198    if ((cur - XYpt(dpt)).length() < 0.01)
00199       return;
00200 
00201    Wpt npt(Wline(dpt,upv).intersect(cur));
00202    Wpt ctr(xf * scale_cent);
00203    double  sval(((npt-dpt) * upv) / (dpt-ctr).length());
00204 
00205    if ((npt-ctr) * (dpt-ctr) < 0)
00206       return;
00207 
00208    Wvec   dnorm (fabs(down_norm[0]),fabs(down_norm[1]),
00209                   fabs(down_norm[2]));
00210 
00211    ((GEOMptr)obj)->set_xform(xf *
00212                   Wtransf::scaling(scale_cent, 
00213                                     (dnorm*sval+Wvec(1,1,1))));
00214 }
00215 

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