Allolib  1.0
C++ Components For Interactive Multimedia
al_Pose.hpp
1 #ifndef INCLUDE_AL_POSE_HPP
2 #define INCLUDE_AL_POSE_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California.
9  All 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 met:
13 
14  Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16 
17  Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20 
21  Neither the name of the University of California nor the names of its
22  contributors may be used to endorse or promote products derived from
23  this software without specific prior written permission.
24 
25  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  POSSIBILITY OF SUCH DAMAGE.
36 
37 
38  File description:
39  Representing an oriented point by vector and quaternion
40 
41  File author(s):
42  Graham Wakefield, 2010, grrrwaaa@gmail.com
43  Lance Putnam, 2010, putnam.lance@gmail.com
44  Pablo Colapinto, 2010, wolftype@gmail.com
45  Wesley Smith, 2010, wesley.hoke@gmail.com
46 */
47 
48 #include "al/math/al_Matrix4.hpp"
49 #include "al/math/al_Quat.hpp"
50 #include "al/math/al_Vec.hpp"
51 #include <iostream>
52 #include <stdio.h>
53 
54 namespace al {
55 
57 
63 class Pose {
64 public:
67  Pose(const Vec3d &pos = Vec3d(0), const Quatd &ori = Quatd::identity());
68 
70  Pose(const Pose &p);
71 
73  static Pose identity() { return Pose().setIdentity(); }
74 
75  // Arithmetic operations
76 
78  Pose operator*(const Pose &v) const { return Pose(*this) *= v; }
79 
81  Pose &operator*=(const Pose &v) {
82  mVec += v.vec();
83  mQuat *= v.quat();
84  return *this;
85  }
86 
87  // Returns true if both vector and quat are equal
88  bool operator==(const Pose &v) const {
89  return (mVec == v.pos()) && (mQuat == v.quat());
90  }
91 
93  void faceToward(const Vec3d &p, double amt = 1.);
94 
97  void faceToward(const Vec3d &point, const Vec3d &up, double amt = 1.);
98 
100  Vec3d &pos() { return mVec; }
101  const Vec3d &pos() const { return mVec; }
102 
104  Vec3d &vec() { return mVec; }
105  const Vec3d &vec() const { return mVec; }
106 
108  Quatd &quat() { return mQuat; }
109  const Quatd &quat() const { return mQuat; }
110 
111  double x() const { return mVec[0]; }
112  double y() const { return mVec[1]; }
113  double z() const { return mVec[2]; }
114 
116  Mat4d matrix() const;
117 
120 
122  void toAED(const Vec3d &to, double &azimuth, double &elevation,
123  double &distance) const;
124 
126  Vec3d ux() const { return quat().toVectorX(); }
127 
129  Vec3d uy() const { return quat().toVectorY(); }
130 
132  Vec3d uz() const { return quat().toVectorZ(); }
133 
135  template <class T>
137  quat().toVectorX<T>(ux);
138  quat().toVectorY<T>(uy);
139  quat().toVectorZ<T>(uz);
140  }
141 
143  template <class T>
145  unitVectors(ur, uu, uf);
146  uf = -uf;
147  }
148 
150  Vec3d ur() const { return ux(); }
151 
153  Vec3d uu() const { return uy(); }
154 
156  Vec3d uf() const { return -uz(); }
157 
159  // (useful ingredient for smooth animations, estimations, etc.)
160  Pose lerp(const Pose &target, double amt) const;
161 
162  // Setters
163 
165  Pose &set(Pose &src) {
166  mVec = src.pos();
167  mQuat = src.quat();
168  // mParentTransform = &(src.parentTransform());
169  return *this;
170  }
171 
173  Pose &set(const Pose &src) {
174  mVec = src.vec();
175  mQuat = src.quat();
176  // mParentTransform = &(src.parentTransform());
177  return *this;
178  }
179 
182  quat().setIdentity();
183  vec().set(0);
184  // mParentTransform = nullptr;
185  return *this;
186  }
187 
189  template <class T> Pose &pos(const Vec<3, T> &v) { return vec(v); }
190 
192  Pose &pos(double x, double y, double z) { return vec(Vec3d(x, y, z)); }
193 
195  template <class T> Pose &vec(const Vec<3, T> &v) {
196  mVec.set(v);
197  return *this;
198  }
199 
201  template <class T> Pose &quat(const Quat<T> &v) {
202  quat() = v;
203  return *this;
204  }
205 
206  // Overloaded cast operators
207  operator Vec3d() { return pos(); }
208  operator Quatd() { return quat(); }
209 
210  // Pose const& parentTransform() const { return *mParentTransform; }
211  // Pose& parentTransform(Pose const& v) {
212  // mParentTransform = &v;
213  // return *this;
214  // }
215  // Pose worldTransform() const {
216  // return mParentTransform ? mParentTransform->worldTransform() * (*this)
217  // : (*this);
218  // }
219 
221  void print() const;
222 
223 protected:
224  Vec3d mVec; // position in 3-space
225  Quatd mQuat; // orientation of reference frame as a quaternion (relative to
226  // global axes)
227  // const Pose* mParentTransform; // parent transform, nullptr if none
228 };
229 
231 
236 class SmoothPose : public Pose {
237 public:
238  SmoothPose(const Pose &init = Pose(), double psmooth = 0.9,
239  double qsmooth = 0.9);
240 
241  // step toward the target:
242  SmoothPose &operator()() {
243  pos().lerp(mTarget.pos(), 1. - mPF);
244  quat().slerpTo(mTarget.quat(), 1. - mQF);
245  return *this;
246  }
247 
248  // set and update:
249  SmoothPose &operator()(const Pose &p) {
250  target(p);
251  return (*this)();
252  }
253 
254  // set the target to smoothly interpolate to:
255  Pose &target() { return mTarget; }
256  void target(const Pose &p) { mTarget.set(p); }
257  void target(const Vec3d &p) { mTarget.pos().set(p); }
258  void target(const Quatd &p) { mTarget.quat().set(p); }
259 
260  // set immediately (without smoothing):
261  void jump(Pose &p) {
262  target(p);
263  set(p);
264  }
265  void jump(Vec3d &v) {
266  target(v);
267  pos().set(v);
268  }
269  void jump(Quatd &q) {
270  target(q);
271  quat().set(q);
272  }
273 
274 protected:
275  Pose mTarget;
276  double mPF, mQF;
277 };
278 
279 } // namespace al
280 
281 #endif
A local coordinate frame.
Definition: al_Pose.hpp:63
Pose & setIdentity()
Set to identity transform.
Definition: al_Pose.hpp:181
static Pose identity()
Get identity.
Definition: al_Pose.hpp:73
Pose & pos(double x, double y, double z)
Set position from individual components.
Definition: al_Pose.hpp:192
Mat4d matrix() const
Convert to 4x4 projection space matrix.
Vec3d uu() const
Get up unit vector.
Definition: al_Pose.hpp:153
Pose(const Vec3d &pos=Vec3d(0), const Quatd &ori=Quatd::identity())
Vec3d ux() const
Get world space X unit vector.
Definition: al_Pose.hpp:126
Pose & pos(const Vec< 3, T > &v)
Set position.
Definition: al_Pose.hpp:189
Pose lerp(const Pose &target, double amt) const
Get a linear-interpolated Pose between this and another.
Pose & quat(const Quat< T > &v)
Set quaternion component.
Definition: al_Pose.hpp:201
Vec3d uf() const
Get forward unit vector (negative of Z)
Definition: al_Pose.hpp:156
void print() const
Print to standard output.
Mat4d directionMatrix() const
Convert to 4x4 direction matrix.
Vec3d & pos()
Get "position" vector.
Definition: al_Pose.hpp:100
void faceToward(const Vec3d &p, double amt=1.)
Turn to face a given world-coordinate point.
Vec3d ur() const
Get right unit vector.
Definition: al_Pose.hpp:150
Vec3d & vec()
Get vector component.
Definition: al_Pose.hpp:104
Pose & operator*=(const Pose &v)
Translate and rotate by argument.
Definition: al_Pose.hpp:81
Pose & vec(const Vec< 3, T > &v)
Set vector component.
Definition: al_Pose.hpp:195
Pose & set(const Pose &src)
Set state from another Pose.
Definition: al_Pose.hpp:173
void toAED(const Vec3d &to, double &azimuth, double &elevation, double &distance) const
Get the azimuth, elevation & distance from this to another point.
void faceToward(const Vec3d &point, const Vec3d &up, double amt=1.)
Pose operator*(const Pose &v) const
Get pose transformed by another pose.
Definition: al_Pose.hpp:78
Vec3d uz() const
Get world space Z unit vector.
Definition: al_Pose.hpp:132
Quatd & quat()
Get quaternion component (represents orientation)
Definition: al_Pose.hpp:108
Pose & set(Pose &src)
Copy all attributes from another Pose.
Definition: al_Pose.hpp:165
void unitVectors(Vec< 3, T > &ux, Vec< 3, T > &uy, Vec< 3, T > &uz) const
Get world space unit vectors.
Definition: al_Pose.hpp:136
Pose(const Pose &p)
Copy constructor.
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
Vec3d uy() const
Get world space Y unit vector.
Definition: al_Pose.hpp:129
Quat & set(const T &w, const T &x, const T &y, const T &z)
Set components.
Definition: al_Quat.hpp:259
Quat & setIdentity()
Set to identity.
Definition: al_Quat.hpp:274
void slerpTo(const Quat &target, T amt)
In-place spherical interpolation.
Definition: al_Quat.hpp:406
void toVectorX(T2 &ax, T2 &ay, T2 &az) const
Get local x unit vector (1,0,0) in absolute coordinates.
Definition: al_Quat.hpp:813
static Quat identity()
Returns identity.
Definition: al_Quat.hpp:133
void toVectorZ(T2 &ax, T2 &ay, T2 &az) const
Get local z unit vector (0,0,1) in absolute coordinates.
Definition: al_Quat.hpp:829
void toVectorY(T2 &ax, T2 &ay, T2 &az) const
Get local y unit vector (0,1,0) in absolute coordinates.
Definition: al_Quat.hpp:821
A Smoothed Pose.
Definition: al_Pose.hpp:236
Vec & lerp(const Vec &target, T amt)
Linearly interpolate towards some target.
Definition: al_Vec.hpp:466
Vec & set(const Vec< N, T2 > &v)
Set elements from another vector.
Definition: al_Vec.hpp:301
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