• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_QUATERNION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_QUATERNION_H
18 
19 namespace OHOS::Ace {
20 
21 class Quaternion {
22 public:
23     Quaternion() = default;
Quaternion(double x,double y,double z,double w)24     Quaternion(double x, double y, double z, double w) : x_(x), y_(y), z_(z), w_(w) {}
25     ~Quaternion() = default;
26 
GetX()27     double GetX() const
28     {
29         return x_;
30     }
GetY()31     double GetY() const
32     {
33         return y_;
34     }
GetZ()35     double GetZ() const
36     {
37         return z_;
38     }
GetW()39     double GetW() const
40     {
41         return w_;
42     }
SetX(double x)43     void SetX(double x)
44     {
45         x_ = x;
46     }
SetY(double y)47     void SetY(double y)
48     {
49         y_ = y;
50     }
SetZ(double z)51     void SetZ(double z)
52     {
53         z_ = z;
54     }
SetW(double w)55     void SetW(double w)
56     {
57         w_ = w;
58     }
59 
60     Quaternion operator+(const Quaternion& q) const
61     {
62         auto x = this->x_ + q.x_;
63         auto y = this->y_ + q.y_;
64         auto z = this->z_ + q.z_;
65         auto w = this->w_ + q.w_;
66         return Quaternion(x, y, z, w);
67     }
68 
69     Quaternion operator*(const Quaternion& q) const
70     {
71         auto x = w_ * q.x_ + x_ * q.w_ + y_ * q.z_ - z_ * q.y_;
72         auto y = w_ * q.y_ - x_ * q.z_ + y_ * q.w_ + z_ * q.x_;
73         auto z = w_ * q.z_ + x_ * q.y_ - y_ * q.x_ + z_ * q.w_;
74         auto w = w_ * q.w_ - x_ * q.x_ - y_ * q.y_ - z_ * q.z_;
75         return Quaternion(x, y, z, w);
76     }
77 
inverse()78     Quaternion inverse() const
79     {
80         return { -x_, -y_, -z_, w_ };
81     }
82 
flip()83     Quaternion flip() const
84     {
85         return { -x_, -y_, -z_, -w_ };
86     }
87 
88     // Blends with the given quaternion, |q|, via spherical linear interpolation.
89     // Values of |t| in the range [0, 1] will interpolate between |this| and |q|,
90     // and values outside that range will extrapolate beyond in either direction.
91     Quaternion Slerp(const Quaternion& q, double t) const;
92 
93 private:
94     double x_ = 0.0;
95     double y_ = 0.0;
96     double z_ = 0.0;
97     double w_ = 0.0;
98 };
99 
100 // |s| is an arbitrary, real constant.
101 inline Quaternion operator*(const Quaternion& q, double s)
102 {
103     return Quaternion(q.GetX() * s, q.GetY() * s, q.GetZ() * s, q.GetW() * s);
104 }
105 
106 // |s| is an arbitrary, real constant.
107 inline Quaternion operator*(double s, const Quaternion& q)
108 {
109     return Quaternion(q.GetX() * s, q.GetY() * s, q.GetZ() * s, q.GetW() * s);
110 }
111 
112 } // namespace OHOS::Ace
113 
114 #endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_QUATERNION_H
115