Allolib  1.0
C++ Components For Interactive Multimedia
al_ControlNav.hpp
1 #ifndef INCLUDE_AL_CONTROL_NAV_HPP
2 #define INCLUDE_AL_CONTROL_NAV_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology,
8  UCSB. Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  Key/Mouse events to control 3D navigation
42 
43  File author(s):
44  Lance Putnam, 2010, putnam.lance@gmail.com
45  Graham Wakefield, 2010, grrrwaaa@gmail.com
46  Keehong Youn, 2017, younkeehong@gmail.com
47 
48 */
49 
50 #include "al/io/al_Window.hpp"
51 #include "al/spatial/al_Pose.hpp"
52 
53 namespace al {
54 
60 
61 class Nav : public Pose {
62 public:
65  Nav(const Vec3d &pos = Vec3d(0), double smooth = 0);
66 
68  Nav(const Nav &nav);
69 
71  double smooth() const { return mSmooth; }
72 
74  const Vec3d &ur() const { return mUR; }
75 
77  const Vec3d &uu() const { return mUU; }
78 
80  const Vec3d &uf() const { return mUF; }
81 
83  Pose vel() const { return Pose(mMove1, Quatd().fromEuler(mSpin1)); }
84 
85  double velScale() const { return mVelScale; }
86 
88  Nav &smooth(double v) {
89  mSmooth = v;
90  return *this;
91  }
92 
93  Nav &view(double azimuth, double elevation, double bank);
94 
95  Nav &view(const Quatd &v);
96 
98  void faceToward(const Vec3d &p, double amt = 1.);
99 
102  void faceToward(const Vec3d &point, const Vec3d &up, double amt = 1.);
103 
105  void nudgeToward(const Vec3d &p, double amt = 1.);
106 
108  void move(double dr, double du, double df) {
109  moveR(dr);
110  moveU(du);
111  moveF(df);
112  }
113  template <class T> void move(const Vec<3, T> &dp) {
114  move(dp[0], dp[1], dp[2]);
115  }
116 
118  void moveR(double v) { mMove0[0] = v; }
119 
121  void moveU(double v) { mMove0[1] = v; }
122 
124  void moveF(double v) { mMove0[2] = v; }
125 
126  Vec3d &move() { return mMove0; }
127 
129  void nudge(double dr, double du, double df) {
130  nudgeR(dr);
131  nudgeU(du);
132  nudgeF(-df);
133  }
134  template <class T> void nudge(const Vec<3, T> &dp) {
135  nudge(dp[0], dp[1], dp[2]);
136  }
137 
138  void nudgeR(double amount) { mNudge[0] += amount; }
139  void nudgeU(double amount) { mNudge[1] += amount; }
140  void nudgeF(double amount) { mNudge[2] += amount; }
141 
144  void spin(double da, double de, double db) {
145  spinR(de);
146  spinU(da);
147  spinF(db);
148  }
149  template <class T> void spin(const Vec<3, T> &daeb) {
150  spin(daeb[0], daeb[1], daeb[2]);
151  }
152 
154  void spin(const Quatd &v) { v.toEuler(mSpin1); }
155 
157  void spinR(double v) { mSpin0[1] = v; }
158 
160  void spinU(double v) { mSpin0[0] = v; }
161 
163  void spinF(double v) { mSpin0[2] = v; }
164 
166  Vec3d &spin() { return mSpin0; }
167 
169  void turn(double az, double el, double ba) {
170  turnR(el);
171  turnU(az);
172  turnF(ba);
173  }
174  template <class T> void turn(const Vec<3, T> &daeb) {
175  turn(daeb[0], daeb[1], daeb[2]);
176  }
177 
180  void turnR(double v) { mTurn[1] = v; }
181 
183  void turnU(double v) { mTurn[0] = v; }
184 
186  void turnF(double v) { mTurn[2] = v; }
187 
189  Nav &halt();
190 
192  Nav &home();
193 
196 
199  quat().normalize();
200  directionVectors(mUR, mUU, mUF);
201  }
202 
203  Nav &set(const Pose &v);
204 
205  Nav &set(const Nav &v);
206 
208  void step(double dt = 1);
209 
211  double pullBack() const { return mPullBack0; }
212 
214  Nav &pullBack(double v) {
215  mPullBack0 = v > 0. ? v : 0.;
216  return *this;
217  }
218 
220  Pose &transformed() { return mTransformed; }
221 
222 protected:
223  Vec3d mMove0, mMove1; // linear velocities (raw, smoothed)
224  Vec3d mSpin0, mSpin1; // angular velocities (raw, smoothed)
225  Vec3d mTurn; // orientation increment for one step
226  Vec3d mNudge; // position increment for one step
227  Vec3d mUR, mUU, mUF; // basis vectors of local coordinate frame
228  double mSmooth;
229  double mVelScale; // velocity scaling factor
230  double mPullBack0, mPullBack1;
231  Pose mTransformed;
232  Pose mHome;
233 };
234 
239 public:
240  NavInputControl(double vscale = 0.125, double tscale = 2.);
241  NavInputControl(Nav &nav, double vscale = 0.125, double tscale = 2.);
243 
244  virtual ~NavInputControl() {}
245 
246  virtual bool keyDown(const Keyboard &k);
247  virtual bool keyUp(const Keyboard &k);
248  virtual bool mouseDrag(const Mouse &m);
249 
250  void nav(Nav &n) { mNav = &n; }
251  Nav &nav() { return *mNav; }
252  const Nav &nav() const { return *mNav; }
253  // NavInputControl& nav(Nav& v){ mNav=v; return *this; }
254 
255  // NavInputControl& target(Pose& pose) {
256  // mNav.target(pose);
257  // return *this;
258  // }
259 
260  void active(bool b) {
261  mActive = b;
262  if (!mActive)
263  nav().halt();
264  }
265  bool active() { return mActive; }
266  void enable() { active(true); }
267  void disable() { active(false); }
268  void toggleActive() { active(!active()); }
269 
270  double vscale() const { return mVScale; }
271  NavInputControl &vscale(double v) {
272  mVScale = v;
273  return *this;
274  }
275 
276  double tscale() const { return mTScale; }
277  NavInputControl &tscale(double v) {
278  mTScale = v;
279  return *this;
280  }
281 
282  void useMouse(bool use) { mUseMouse = use; }
283 
284  // void step(double dt=1) {
285  // mNav.step(dt);
286  // }
287 protected:
288  Nav *mNav = nullptr;
289  double mVScale, mTScale;
290  bool mUseMouse;
291  bool mActive = true;
292 };
293 
294 } // namespace al
295 
296 #endif
void updateDirectionVectors()
Update coordinate frame basis vectors based on internal quaternion.
void spinR(double v)
Set angular velocity around right vector (elevation), in radians.
Nav & halt()
Stop moving and spinning.
Pose vel() const
Get current linear and angular velocities as a Pose.
const Vec3d & ur() const
Get right unit vector.
void move(double dr, double du, double df)
Set linear velocity.
void turnF(double v)
Turn by a single increment, in radians, around the forward vector (bank)
Pose & transformed()
Get transformed pose.
void step(double dt=1)
Accumulate pose based on velocity.
void moveU(double v)
Set linear velocity long up vector.
void spinU(double v)
Set angular velocity around up vector (azimuth), in radians.
void spinF(double v)
Set angular velocity around forward vector (bank), in radians.
void spin(const Quatd &v)
Set angular velocity from a unit quaternion (versor)
void turnR(double v)
void nudge(double dr, double du, double df)
Move by a single increment.
void turn(double az, double el, double ba)
Turn by a single increment for one step, in radians.
Vec3d & spin()
Set angular velocity directly.
void faceToward(const Vec3d &point, const Vec3d &up, double amt=1.)
const Vec3d & uf() const
Get forward unit vector.
Nav & home()
Go to origin, reset orientation.
Nav & pullBack(double v)
Set pull-back amount.
void nudgeToward(const Vec3d &p, double amt=1.)
Move toward a given world-coordinate point.
double smooth() const
Get smoothing amount.
void moveR(double v)
Set linear velocity along right vector.
void moveF(double v)
Set linear velocity long forward vector.
void turnU(double v)
Turn by a single increment, in radians, around the up vector (azimuth)
Nav(const Vec3d &pos=Vec3d(0), double smooth=0)
double pullBack() const
Get pull-back amount.
Nav & smooth(double v)
Set smoothing amount in [0,1)
void faceToward(const Vec3d &p, double amt=1.)
Turn to face a given world-coordinate point.
Nav & setHome()
set current nav to be home
Nav(const Nav &nav)
Copy constructor.
const Vec3d & uu() const
Get up unit vector.
void spin(double da, double de, double db)
virtual bool keyUp(const Keyboard &k)
Called when a keyboard key is released.
virtual bool mouseDrag(const Mouse &m)
Called when the mouse moves while a button is down.
virtual bool keyDown(const Keyboard &k)
Called when a keyboard key is pressed.
A local coordinate frame.
Definition: al_Pose.hpp:63
Pose(const Vec3d &pos=Vec3d(0), const Quatd &ori=Quatd::identity())
Vec3d & pos()
Get "position" vector.
Definition: al_Pose.hpp:100
Quatd & quat()
Get quaternion component (represents orientation)
Definition: al_Pose.hpp:108
void directionVectors(Vec< 3, T > &ur, Vec< 3, T > &uu, Vec< 3, T > &uf) const
Get local right, up, and forward unit vectors.
Definition: al_Pose.hpp:144
void toEuler(T &az, T &el, T &ba) const
Convert to YXZ Euler angles (azimuth, elevation, bank), in radians.
Definition: al_Quat.hpp:675
Quat & normalize()
Normalize magnitude to one.
Definition: al_Quat.hpp:443
Controller for handling input and window events.
Definition: al_Window.hpp:194
Definition: al_App.hpp:23
Vec< 3, double > Vec3d
double 3-vector
Definition: al_Vec.hpp:60
Quat< double > Quatd
Double-precision quaternion.
Definition: al_Quat.hpp:57