summaryrefslogtreecommitdiff
path: root/ui/gfx/geometry/quaternion.h
blob: f4e124ce522020d08491a98f5c90786111b012b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_GEOMETRY_QUATERNION_
#define UI_GFX_GEOMETRY_QUATERNION_

#include <string>

#include "ui/gfx/gfx_export.h"

namespace gfx {

class Vector3dF;

class GFX_EXPORT Quaternion {
 public:
  constexpr Quaternion() = default;
  constexpr Quaternion(double x, double y, double z, double w)
      : x_(x), y_(y), z_(z), w_(w) {}
  Quaternion(const Vector3dF& axis, double angle);

  // Constructs a quaternion representing a rotation between |from| and |to|.
  Quaternion(const Vector3dF& from, const Vector3dF& to);

  constexpr double x() const { return x_; }
  void set_x(double x) { x_ = x; }

  constexpr double y() const { return y_; }
  void set_y(double y) { y_ = y; }

  constexpr double z() const { return z_; }
  void set_z(double z) { z_ = z; }

  constexpr double w() const { return w_; }
  void set_w(double w) { w_ = w; }

  Quaternion operator+(const Quaternion& q) const {
    return {q.x_ + x_, q.y_ + y_, q.z_ + z_, q.w_ + w_};
  }

  Quaternion operator*(const Quaternion& q) const {
    return {w_ * q.x_ + x_ * q.w_ + y_ * q.z_ - z_ * q.y_,
            w_ * q.y_ - x_ * q.z_ + y_ * q.w_ + z_ * q.x_,
            w_ * q.z_ + x_ * q.y_ - y_ * q.x_ + z_ * q.w_,
            w_ * q.w_ - x_ * q.x_ - y_ * q.y_ - z_ * q.z_};
  }

  Quaternion inverse() const { return {-x_, -y_, -z_, w_}; }

  // Blends with the given quaternion, |q|, via spherical linear interpolation.
  // Values of |t| in the range [0, 1] will interpolate between |this| and |q|,
  // and values outside that range will extrapolate beyond in either direction.
  Quaternion Slerp(const Quaternion& q, double t) const;

  // Blends with the given quaternion, |q|, via linear interpolation. This is
  // rarely what you want. Use only if you know what you're doing.
  // Values of |t| in the range [0, 1] will interpolate between |this| and |q|,
  // and values outside that range will extrapolate beyond in either direction.
  Quaternion Lerp(const Quaternion& q, double t) const;

  Quaternion Normalized() const;

  std::string ToString() const;

 private:
  double x_ = 0.0;
  double y_ = 0.0;
  double z_ = 0.0;
  double w_ = 1.0;
};

// |s| is an arbitrary, real constant.
inline Quaternion operator*(const Quaternion& q, double s) {
  return Quaternion(q.x() * s, q.y() * s, q.z() * s, q.w() * s);
}

// |s| is an arbitrary, real constant.
inline Quaternion operator*(double s, const Quaternion& q) {
  return Quaternion(q.x() * s, q.y() * s, q.z() * s, q.w() * s);
}

// |s| is an arbitrary, real constant.
inline Quaternion operator/(const Quaternion& q, double s) {
  double inv = 1.0 / s;
  return q * inv;
}

}  // namespace gfx

#endif  // UI_GFX_GEOMETRY_QUATERNION_