File: quaternion.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 3,615 bytes parent folder | download | duplicates (4)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2017 The Chromium Authors
// 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_H_
#define UI_GFX_GEOMETRY_QUATERNION_H_

#include <iosfwd>
#include <string>

#include "base/component_export.h"

namespace gfx {

class Vector3dF;

class COMPONENT_EXPORT(GEOMETRY) 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);

  static Quaternion FromAxisAngle(double x, double y, double z, double angle);

  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_}; }

  Quaternion flip() 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;

  double Length() const;

  Quaternion Normalized() const;

  std::string ToString() const;

  // Returns true if the x, y, z, w values of |lhs| and |rhs| are equal. Note
  // that two quaternions can represent the same orientation with different
  // values. This operator will return false in that scenario.
  friend bool operator==(const Quaternion&, const Quaternion&) = default;

 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;
}

// This is declared here for use in gtest-based unit tests but is defined in
// the //ui/gfx:test_support target. Depend on that to use this in your unit
// test. This should not be used in production code - call ToString() instead.
void PrintTo(const Quaternion& transform, ::std::ostream* os);

}  // namespace gfx

#endif  // UI_GFX_GEOMETRY_QUATERNION_H_