123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
323b3eb3cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
423b3eb3cSopenharmony_ci * you may not use this file except in compliance with the License.
523b3eb3cSopenharmony_ci * You may obtain a copy of the License at
623b3eb3cSopenharmony_ci *
723b3eb3cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
823b3eb3cSopenharmony_ci *
923b3eb3cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1023b3eb3cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1123b3eb3cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1223b3eb3cSopenharmony_ci * See the License for the specific language governing permissions and
1323b3eb3cSopenharmony_ci * limitations under the License.
1423b3eb3cSopenharmony_ci */
1523b3eb3cSopenharmony_ci
1623b3eb3cSopenharmony_ci#ifndef FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_QUATERNION_H
1723b3eb3cSopenharmony_ci#define FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_QUATERNION_H
1823b3eb3cSopenharmony_ci
1923b3eb3cSopenharmony_ci#include "base/utils/utils.h"
2023b3eb3cSopenharmony_ci
2123b3eb3cSopenharmony_cinamespace OHOS::Ace {
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ciclass Quaternion {
2423b3eb3cSopenharmony_cipublic:
2523b3eb3cSopenharmony_ci    Quaternion() = default;
2623b3eb3cSopenharmony_ci    Quaternion(double x, double y, double z, double w) : x_(x), y_(y), z_(z), w_(w) {}
2723b3eb3cSopenharmony_ci    ~Quaternion() = default;
2823b3eb3cSopenharmony_ci
2923b3eb3cSopenharmony_ci    double GetX() const
3023b3eb3cSopenharmony_ci    {
3123b3eb3cSopenharmony_ci        return x_;
3223b3eb3cSopenharmony_ci    }
3323b3eb3cSopenharmony_ci    double GetY() const
3423b3eb3cSopenharmony_ci    {
3523b3eb3cSopenharmony_ci        return y_;
3623b3eb3cSopenharmony_ci    }
3723b3eb3cSopenharmony_ci    double GetZ() const
3823b3eb3cSopenharmony_ci    {
3923b3eb3cSopenharmony_ci        return z_;
4023b3eb3cSopenharmony_ci    }
4123b3eb3cSopenharmony_ci    double GetW() const
4223b3eb3cSopenharmony_ci    {
4323b3eb3cSopenharmony_ci        return w_;
4423b3eb3cSopenharmony_ci    }
4523b3eb3cSopenharmony_ci    void SetX(double x)
4623b3eb3cSopenharmony_ci    {
4723b3eb3cSopenharmony_ci        x_ = x;
4823b3eb3cSopenharmony_ci    }
4923b3eb3cSopenharmony_ci    void SetY(double y)
5023b3eb3cSopenharmony_ci    {
5123b3eb3cSopenharmony_ci        y_ = y;
5223b3eb3cSopenharmony_ci    }
5323b3eb3cSopenharmony_ci    void SetZ(double z)
5423b3eb3cSopenharmony_ci    {
5523b3eb3cSopenharmony_ci        z_ = z;
5623b3eb3cSopenharmony_ci    }
5723b3eb3cSopenharmony_ci    void SetW(double w)
5823b3eb3cSopenharmony_ci    {
5923b3eb3cSopenharmony_ci        w_ = w;
6023b3eb3cSopenharmony_ci    }
6123b3eb3cSopenharmony_ci
6223b3eb3cSopenharmony_ci    Quaternion operator+(const Quaternion& q) const
6323b3eb3cSopenharmony_ci    {
6423b3eb3cSopenharmony_ci        auto x = this->x_ + q.x_;
6523b3eb3cSopenharmony_ci        auto y = this->y_ + q.y_;
6623b3eb3cSopenharmony_ci        auto z = this->z_ + q.z_;
6723b3eb3cSopenharmony_ci        auto w = this->w_ + q.w_;
6823b3eb3cSopenharmony_ci        return Quaternion(x, y, z, w);
6923b3eb3cSopenharmony_ci    }
7023b3eb3cSopenharmony_ci
7123b3eb3cSopenharmony_ci    Quaternion operator*(const Quaternion& q) const
7223b3eb3cSopenharmony_ci    {
7323b3eb3cSopenharmony_ci        auto x = w_ * q.x_ + x_ * q.w_ + y_ * q.z_ - z_ * q.y_;
7423b3eb3cSopenharmony_ci        auto y = w_ * q.y_ - x_ * q.z_ + y_ * q.w_ + z_ * q.x_;
7523b3eb3cSopenharmony_ci        auto z = w_ * q.z_ + x_ * q.y_ - y_ * q.x_ + z_ * q.w_;
7623b3eb3cSopenharmony_ci        auto w = w_ * q.w_ - x_ * q.x_ - y_ * q.y_ - z_ * q.z_;
7723b3eb3cSopenharmony_ci        return Quaternion(x, y, z, w);
7823b3eb3cSopenharmony_ci    }
7923b3eb3cSopenharmony_ci
8023b3eb3cSopenharmony_ci    bool operator==(const Quaternion& q) const
8123b3eb3cSopenharmony_ci    {
8223b3eb3cSopenharmony_ci        return NearEqual(x_, q.x_) && NearEqual(y_, q.y_)
8323b3eb3cSopenharmony_ci            && NearEqual(z_, q.z_) && NearEqual(w_, q.w_);
8423b3eb3cSopenharmony_ci    }
8523b3eb3cSopenharmony_ci
8623b3eb3cSopenharmony_ci    bool operator!=(const Quaternion& q) const
8723b3eb3cSopenharmony_ci    {
8823b3eb3cSopenharmony_ci        return !operator==(q);
8923b3eb3cSopenharmony_ci    }
9023b3eb3cSopenharmony_ci
9123b3eb3cSopenharmony_ci    Quaternion inverse() const
9223b3eb3cSopenharmony_ci    {
9323b3eb3cSopenharmony_ci        return { -x_, -y_, -z_, w_ };
9423b3eb3cSopenharmony_ci    }
9523b3eb3cSopenharmony_ci
9623b3eb3cSopenharmony_ci    Quaternion flip() const
9723b3eb3cSopenharmony_ci    {
9823b3eb3cSopenharmony_ci        return { -x_, -y_, -z_, -w_ };
9923b3eb3cSopenharmony_ci    }
10023b3eb3cSopenharmony_ci
10123b3eb3cSopenharmony_ci    // Blends with the given quaternion, |q|, via spherical linear interpolation.
10223b3eb3cSopenharmony_ci    // Values of |t| in the range [0, 1] will interpolate between |this| and |q|,
10323b3eb3cSopenharmony_ci    // and values outside that range will extrapolate beyond in either direction.
10423b3eb3cSopenharmony_ci    Quaternion Slerp(const Quaternion& q, double t) const;
10523b3eb3cSopenharmony_ci
10623b3eb3cSopenharmony_ciprivate:
10723b3eb3cSopenharmony_ci    double x_ = 0.0;
10823b3eb3cSopenharmony_ci    double y_ = 0.0;
10923b3eb3cSopenharmony_ci    double z_ = 0.0;
11023b3eb3cSopenharmony_ci    double w_ = 0.0;
11123b3eb3cSopenharmony_ci};
11223b3eb3cSopenharmony_ci
11323b3eb3cSopenharmony_ci// |s| is an arbitrary, real constant.
11423b3eb3cSopenharmony_ciinline Quaternion operator*(const Quaternion& q, double s)
11523b3eb3cSopenharmony_ci{
11623b3eb3cSopenharmony_ci    return Quaternion(q.GetX() * s, q.GetY() * s, q.GetZ() * s, q.GetW() * s);
11723b3eb3cSopenharmony_ci}
11823b3eb3cSopenharmony_ci
11923b3eb3cSopenharmony_ci// |s| is an arbitrary, real constant.
12023b3eb3cSopenharmony_ciinline Quaternion operator*(double s, const Quaternion& q)
12123b3eb3cSopenharmony_ci{
12223b3eb3cSopenharmony_ci    return Quaternion(q.GetX() * s, q.GetY() * s, q.GetZ() * s, q.GetW() * s);
12323b3eb3cSopenharmony_ci}
12423b3eb3cSopenharmony_ci
12523b3eb3cSopenharmony_ci} // namespace OHOS::Ace
12623b3eb3cSopenharmony_ci
12723b3eb3cSopenharmony_ci#endif // FOUNDATION_ACE_FRAMEWORKS_BASE_GEOMETRY_QUATERNION_H
128