123b3eb3cSopenharmony_ci/*
223b3eb3cSopenharmony_ci * Copyright (c) 2021-2022 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#include "base/geometry/matrix4.h"
1723b3eb3cSopenharmony_ci
1823b3eb3cSopenharmony_cinamespace OHOS::Ace {
1923b3eb3cSopenharmony_cinamespace {
2023b3eb3cSopenharmony_ciconstexpr int32_t MATRIX_LENGTH = Matrix4::DIMENSION * Matrix4::DIMENSION;
2123b3eb3cSopenharmony_ciconstexpr double ANGLE_UNIT = 0.017453f; // PI / 180
2223b3eb3cSopenharmony_ci
2323b3eb3cSopenharmony_ciinline bool IsEqual(const double& left, const double& right)
2423b3eb3cSopenharmony_ci{
2523b3eb3cSopenharmony_ci    constexpr double epsilon = 0.0001;
2623b3eb3cSopenharmony_ci    return NearEqual(left, right, epsilon);
2723b3eb3cSopenharmony_ci}
2823b3eb3cSopenharmony_ci
2923b3eb3cSopenharmony_ci} // namespace
3023b3eb3cSopenharmony_ci
3123b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateIdentity()
3223b3eb3cSopenharmony_ci{
3323b3eb3cSopenharmony_ci    return Matrix4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
3423b3eb3cSopenharmony_ci}
3523b3eb3cSopenharmony_ci
3623b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateTranslate(double x, double y, double z)
3723b3eb3cSopenharmony_ci{
3823b3eb3cSopenharmony_ci    return Matrix4(1.0f, 0.0f, 0.0f, x, 0.0f, 1.0f, 0.0f, y, 0.0f, 0.0f, 1.0f, z, 0.0f, 0.0f, 0.0f, 1.0f);
3923b3eb3cSopenharmony_ci}
4023b3eb3cSopenharmony_ci
4123b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateScale(double x, double y, double z)
4223b3eb3cSopenharmony_ci{
4323b3eb3cSopenharmony_ci    return Matrix4(x, 0.0f, 0.0f, 0.0f, 0.0f, y, 0.0f, 0.0f, 0.0f, 0.0f, z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
4423b3eb3cSopenharmony_ci}
4523b3eb3cSopenharmony_ci
4623b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateRotate(double angle, double dx, double dy, double dz)
4723b3eb3cSopenharmony_ci{
4823b3eb3cSopenharmony_ci    // (x,y,z) need normalize
4923b3eb3cSopenharmony_ci    double sum = dx * dx + dy * dy + dz * dz;
5023b3eb3cSopenharmony_ci    if (NearZero(sum)) {
5123b3eb3cSopenharmony_ci        return Matrix4::CreateIdentity();
5223b3eb3cSopenharmony_ci    }
5323b3eb3cSopenharmony_ci
5423b3eb3cSopenharmony_ci    double x = dx / sqrt(sum);
5523b3eb3cSopenharmony_ci    double y = dy / sqrt(sum);
5623b3eb3cSopenharmony_ci    double z = dz / sqrt(sum);
5723b3eb3cSopenharmony_ci    double redian = static_cast<double>(angle * (M_PI / 180.0f));
5823b3eb3cSopenharmony_ci    double cosValue = cosf(redian);
5923b3eb3cSopenharmony_ci    double sinValue = sinf(redian);
6023b3eb3cSopenharmony_ci
6123b3eb3cSopenharmony_ci    return Matrix4(cosValue + (x * x * (1.0f - cosValue)), (x * y * (1.0f - cosValue)) - (z * sinValue),
6223b3eb3cSopenharmony_ci        (x * z * (1.0f - cosValue)) + (y * sinValue), 0.0f, (y * x * (1.0f - cosValue)) + (z * sinValue),
6323b3eb3cSopenharmony_ci        cosValue + (y * y * (1.0f - cosValue)), (y * z * (1.0f - cosValue)) - (x * sinValue), 0.0f,
6423b3eb3cSopenharmony_ci        (z * x * (1.0f - cosValue)) - (y * sinValue), (z * y * (1.0f - cosValue)) + (x * sinValue),
6523b3eb3cSopenharmony_ci        cosValue + (z * z * (1.0f - cosValue)), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
6623b3eb3cSopenharmony_ci}
6723b3eb3cSopenharmony_ci
6823b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateMatrix2D(double m00, double m10, double m01, double m11, double m03, double m13)
6923b3eb3cSopenharmony_ci{
7023b3eb3cSopenharmony_ci    return Matrix4(m00, m01, 0.0f, m03, m10, m11, 0.0f, m13, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
7123b3eb3cSopenharmony_ci}
7223b3eb3cSopenharmony_ci
7323b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateSkew(double x, double y)
7423b3eb3cSopenharmony_ci{
7523b3eb3cSopenharmony_ci    return Matrix4(1.0f, std::tan(x * ANGLE_UNIT), 0.0f, 0.0f, std::tan(y * ANGLE_UNIT), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
7623b3eb3cSopenharmony_ci        1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
7723b3eb3cSopenharmony_ci}
7823b3eb3cSopenharmony_ci
7923b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateFactorSkew(double x, double y)
8023b3eb3cSopenharmony_ci{
8123b3eb3cSopenharmony_ci    return Matrix4(1.0f, x, 0.0f, 0.0f, y, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
8223b3eb3cSopenharmony_ci        1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
8323b3eb3cSopenharmony_ci}
8423b3eb3cSopenharmony_ci
8523b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateFactorPerspective(double x, double y)
8623b3eb3cSopenharmony_ci{
8723b3eb3cSopenharmony_ci    return Matrix4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
8823b3eb3cSopenharmony_ci        1.0f, 0.0f, x, y, 0.0f, 1.0f);
8923b3eb3cSopenharmony_ci}
9023b3eb3cSopenharmony_ci
9123b3eb3cSopenharmony_ciMatrix4 Matrix4::CreatePerspective(double distance)
9223b3eb3cSopenharmony_ci{
9323b3eb3cSopenharmony_ci    auto result = CreateIdentity();
9423b3eb3cSopenharmony_ci    if (GreatNotEqual(distance, 0.0f)) {
9523b3eb3cSopenharmony_ci        result.matrix4x4_[2][3] = -1.0f / distance;
9623b3eb3cSopenharmony_ci    }
9723b3eb3cSopenharmony_ci    return result;
9823b3eb3cSopenharmony_ci}
9923b3eb3cSopenharmony_ci
10023b3eb3cSopenharmony_ciMatrix4 Matrix4::Invert(const Matrix4& matrix)
10123b3eb3cSopenharmony_ci{
10223b3eb3cSopenharmony_ci    Matrix4 inverted = CreateInvert(matrix);
10323b3eb3cSopenharmony_ci    double determinant = matrix(0, 0) * inverted(0, 0) + matrix(0, 1) * inverted(1, 0) + matrix(0, 2) * inverted(2, 0) +
10423b3eb3cSopenharmony_ci                         matrix(0, 3) * inverted(3, 0);
10523b3eb3cSopenharmony_ci    if (!NearZero(determinant)) {
10623b3eb3cSopenharmony_ci        inverted = inverted * (1.0f / determinant);
10723b3eb3cSopenharmony_ci    } else {
10823b3eb3cSopenharmony_ci        inverted = CreateIdentity();
10923b3eb3cSopenharmony_ci    }
11023b3eb3cSopenharmony_ci
11123b3eb3cSopenharmony_ci    return inverted;
11223b3eb3cSopenharmony_ci}
11323b3eb3cSopenharmony_ci
11423b3eb3cSopenharmony_ciMatrix4 Matrix4::QuaternionToMatrix(double x, double y, double z, double w)
11523b3eb3cSopenharmony_ci{
11623b3eb3cSopenharmony_ci    double norm = std::sqrt(w * w + x * x + y * y + z * z);
11723b3eb3cSopenharmony_ci    if (LessOrEqual(norm, 0.0f)) {
11823b3eb3cSopenharmony_ci        return Matrix4();
11923b3eb3cSopenharmony_ci    }
12023b3eb3cSopenharmony_ci    w /= norm;
12123b3eb3cSopenharmony_ci    x /= norm;
12223b3eb3cSopenharmony_ci    y /= norm;
12323b3eb3cSopenharmony_ci    z /= norm;
12423b3eb3cSopenharmony_ci
12523b3eb3cSopenharmony_ci    // Quaternion to matrix operation wiki:reference/apis-arkui/js-apis-matrix4.md.
12623b3eb3cSopenharmony_ci    return Matrix4(1.0 - 2.0 * (y * y + z * z), 2.0 * (x * y - w * z), 2.0 * (x * z + w * y), 0.0,
12723b3eb3cSopenharmony_ci        2.0 * (x * y + w * z), 1.0 - 2.0 * (x * x + z * z), 2.0 * (y * z - w * x), 0.0,
12823b3eb3cSopenharmony_ci        2.0 * (x * z - w * y), 2.0 * (y * z + w * x), 1.0 - 2.0 * (x * x + y * y), 0.0,
12923b3eb3cSopenharmony_ci        0.0, 0.0, 0.0, 1.0);
13023b3eb3cSopenharmony_ci}
13123b3eb3cSopenharmony_ci
13223b3eb3cSopenharmony_ciMatrix4::Matrix4()
13323b3eb3cSopenharmony_ci    : Matrix4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f)
13423b3eb3cSopenharmony_ci{}
13523b3eb3cSopenharmony_ci
13623b3eb3cSopenharmony_ciMatrix4::Matrix4(const Matrix4& matrix)
13723b3eb3cSopenharmony_ci{
13823b3eb3cSopenharmony_ci    std::copy_n(&matrix.matrix4x4_[0][0], MATRIX_LENGTH, &matrix4x4_[0][0]);
13923b3eb3cSopenharmony_ci}
14023b3eb3cSopenharmony_ci
14123b3eb3cSopenharmony_ciMatrix4::Matrix4(double m00, double m01, double m02, double m03, double m10, double m11, double m12, double m13,
14223b3eb3cSopenharmony_ci    double m20, double m21, double m22, double m23, double m30, double m31, double m32, double m33)
14323b3eb3cSopenharmony_ci{
14423b3eb3cSopenharmony_ci    matrix4x4_[0][0] = m00;
14523b3eb3cSopenharmony_ci    matrix4x4_[1][0] = m01;
14623b3eb3cSopenharmony_ci    matrix4x4_[2][0] = m02;
14723b3eb3cSopenharmony_ci    matrix4x4_[3][0] = m03;
14823b3eb3cSopenharmony_ci    matrix4x4_[0][1] = m10;
14923b3eb3cSopenharmony_ci    matrix4x4_[1][1] = m11;
15023b3eb3cSopenharmony_ci    matrix4x4_[2][1] = m12;
15123b3eb3cSopenharmony_ci    matrix4x4_[3][1] = m13;
15223b3eb3cSopenharmony_ci    matrix4x4_[0][2] = m20;
15323b3eb3cSopenharmony_ci    matrix4x4_[1][2] = m21;
15423b3eb3cSopenharmony_ci    matrix4x4_[2][2] = m22;
15523b3eb3cSopenharmony_ci    matrix4x4_[3][2] = m23;
15623b3eb3cSopenharmony_ci    matrix4x4_[0][3] = m30;
15723b3eb3cSopenharmony_ci    matrix4x4_[1][3] = m31;
15823b3eb3cSopenharmony_ci    matrix4x4_[2][3] = m32;
15923b3eb3cSopenharmony_ci    matrix4x4_[3][3] = m33;
16023b3eb3cSopenharmony_ci}
16123b3eb3cSopenharmony_ci
16223b3eb3cSopenharmony_civoid Matrix4::SetScale(double x, double y, double z)
16323b3eb3cSopenharmony_ci{
16423b3eb3cSopenharmony_ci    // The 4X4 matrix scale index is [0][0], [1][1], [2][2], [3][3].
16523b3eb3cSopenharmony_ci    matrix4x4_[0][0] = x;
16623b3eb3cSopenharmony_ci    matrix4x4_[1][1] = y;
16723b3eb3cSopenharmony_ci    matrix4x4_[2][2] = z;
16823b3eb3cSopenharmony_ci    matrix4x4_[3][3] = 1.0f;
16923b3eb3cSopenharmony_ci}
17023b3eb3cSopenharmony_ci
17123b3eb3cSopenharmony_cidouble Matrix4::GetScaleX() const
17223b3eb3cSopenharmony_ci{
17323b3eb3cSopenharmony_ci    return matrix4x4_[0][0];
17423b3eb3cSopenharmony_ci}
17523b3eb3cSopenharmony_ci
17623b3eb3cSopenharmony_cidouble Matrix4::GetScaleY() const
17723b3eb3cSopenharmony_ci{
17823b3eb3cSopenharmony_ci    return matrix4x4_[1][1];
17923b3eb3cSopenharmony_ci}
18023b3eb3cSopenharmony_ci
18123b3eb3cSopenharmony_civoid Matrix4::SetEntry(int32_t row, int32_t col, double value)
18223b3eb3cSopenharmony_ci{
18323b3eb3cSopenharmony_ci    if ((row < 0 || row >= DIMENSION) || (col < 0 || col >= DIMENSION)) {
18423b3eb3cSopenharmony_ci        return;
18523b3eb3cSopenharmony_ci    }
18623b3eb3cSopenharmony_ci    matrix4x4_[row][col] = value;
18723b3eb3cSopenharmony_ci}
18823b3eb3cSopenharmony_ci
18923b3eb3cSopenharmony_cibool Matrix4::IsIdentityMatrix() const
19023b3eb3cSopenharmony_ci{
19123b3eb3cSopenharmony_ci    return *this == CreateIdentity();
19223b3eb3cSopenharmony_ci}
19323b3eb3cSopenharmony_ci
19423b3eb3cSopenharmony_civoid Matrix4::Rotate(double angle, double dx, double dy, double dz)
19523b3eb3cSopenharmony_ci{
19623b3eb3cSopenharmony_ci    Matrix4 transform = *this;
19723b3eb3cSopenharmony_ci    *this = transform * CreateRotate(angle, dx, dy, dz);
19823b3eb3cSopenharmony_ci}
19923b3eb3cSopenharmony_ci
20023b3eb3cSopenharmony_ciint32_t Matrix4::Count() const
20123b3eb3cSopenharmony_ci{
20223b3eb3cSopenharmony_ci    return MATRIX_LENGTH;
20323b3eb3cSopenharmony_ci}
20423b3eb3cSopenharmony_ci
20523b3eb3cSopenharmony_ciMatrix4 Matrix4::CreateInvert(const Matrix4& matrix)
20623b3eb3cSopenharmony_ci{
20723b3eb3cSopenharmony_ci    return Matrix4(
20823b3eb3cSopenharmony_ci        matrix(1, 1) * matrix(2, 2) * matrix(3, 3) - matrix(1, 1) * matrix(2, 3) * matrix(3, 2) -
20923b3eb3cSopenharmony_ci            matrix(2, 1) * matrix(1, 2) * matrix(3, 3) + matrix(2, 1) * matrix(1, 3) * matrix(3, 2) +
21023b3eb3cSopenharmony_ci            matrix(3, 1) * matrix(1, 2) * matrix(2, 3) - matrix(3, 1) * matrix(1, 3) * matrix(2, 2),
21123b3eb3cSopenharmony_ci        -matrix(1, 0) * matrix(2, 2) * matrix(3, 3) + matrix(1, 0) * matrix(2, 3) * matrix(3, 2) +
21223b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(1, 2) * matrix(3, 3) - matrix(2, 0) * matrix(1, 3) * matrix(3, 2) -
21323b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(1, 2) * matrix(2, 3) + matrix(3, 0) * matrix(1, 3) * matrix(2, 2),
21423b3eb3cSopenharmony_ci        matrix(1, 0) * matrix(2, 1) * matrix(3, 3) - matrix(1, 0) * matrix(2, 3) * matrix(3, 1) -
21523b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(1, 1) * matrix(3, 3) + matrix(2, 0) * matrix(1, 3) * matrix(3, 1) +
21623b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(1, 1) * matrix(2, 3) - matrix(3, 0) * matrix(1, 3) * matrix(2, 1),
21723b3eb3cSopenharmony_ci        -matrix(1, 0) * matrix(2, 1) * matrix(3, 2) + matrix(1, 0) * matrix(2, 2) * matrix(3, 1) +
21823b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(1, 1) * matrix(3, 2) - matrix(2, 0) * matrix(1, 2) * matrix(3, 1) -
21923b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(1, 1) * matrix(2, 2) + matrix(3, 0) * matrix(1, 2) * matrix(2, 1),
22023b3eb3cSopenharmony_ci        -matrix(0, 1) * matrix(2, 2) * matrix(3, 3) + matrix(0, 1) * matrix(2, 3) * matrix(3, 2) +
22123b3eb3cSopenharmony_ci            matrix(2, 1) * matrix(0, 2) * matrix(3, 3) - matrix(2, 1) * matrix(0, 3) * matrix(3, 2) -
22223b3eb3cSopenharmony_ci            matrix(3, 1) * matrix(0, 2) * matrix(2, 3) + matrix(3, 1) * matrix(0, 3) * matrix(2, 2),
22323b3eb3cSopenharmony_ci        matrix(0, 0) * matrix(2, 2) * matrix(3, 3) - matrix(0, 0) * matrix(2, 3) * matrix(3, 2) -
22423b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(0, 2) * matrix(3, 3) + matrix(2, 0) * matrix(0, 3) * matrix(3, 2) +
22523b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(0, 2) * matrix(2, 3) - matrix(3, 0) * matrix(0, 3) * matrix(2, 2),
22623b3eb3cSopenharmony_ci        -matrix(0, 0) * matrix(2, 1) * matrix(3, 3) + matrix(0, 0) * matrix(2, 3) * matrix(3, 1) +
22723b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(0, 1) * matrix(3, 3) - matrix(2, 0) * matrix(0, 3) * matrix(3, 1) -
22823b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(0, 1) * matrix(2, 3) + matrix(3, 0) * matrix(0, 3) * matrix(2, 1),
22923b3eb3cSopenharmony_ci        matrix(0, 0) * matrix(2, 1) * matrix(3, 2) - matrix(0, 0) * matrix(2, 2) * matrix(3, 1) -
23023b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(0, 1) * matrix(3, 2) + matrix(2, 0) * matrix(0, 2) * matrix(3, 1) +
23123b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(0, 1) * matrix(2, 2) - matrix(3, 0) * matrix(0, 2) * matrix(2, 1),
23223b3eb3cSopenharmony_ci        matrix(0, 1) * matrix(1, 2) * matrix(3, 3) - matrix(0, 1) * matrix(1, 3) * matrix(3, 2) -
23323b3eb3cSopenharmony_ci            matrix(1, 1) * matrix(0, 2) * matrix(3, 3) + matrix(1, 1) * matrix(0, 3) * matrix(3, 2) +
23423b3eb3cSopenharmony_ci            matrix(3, 1) * matrix(0, 2) * matrix(1, 3) - matrix(3, 1) * matrix(0, 3) * matrix(1, 2),
23523b3eb3cSopenharmony_ci        -matrix(0, 0) * matrix(1, 2) * matrix(3, 3) + matrix(0, 0) * matrix(1, 3) * matrix(3, 2) +
23623b3eb3cSopenharmony_ci            matrix(1, 0) * matrix(0, 2) * matrix(3, 3) - matrix(1, 0) * matrix(0, 3) * matrix(3, 2) -
23723b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(0, 2) * matrix(1, 3) + matrix(3, 0) * matrix(0, 3) * matrix(1, 2),
23823b3eb3cSopenharmony_ci        matrix(0, 0) * matrix(1, 1) * matrix(3, 3) - matrix(0, 0) * matrix(1, 3) * matrix(3, 1) -
23923b3eb3cSopenharmony_ci            matrix(1, 0) * matrix(0, 1) * matrix(3, 3) + matrix(1, 0) * matrix(0, 3) * matrix(3, 1) +
24023b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(0, 1) * matrix(1, 3) - matrix(3, 0) * matrix(0, 3) * matrix(1, 1),
24123b3eb3cSopenharmony_ci        -matrix(0, 0) * matrix(1, 1) * matrix(3, 2) + matrix(0, 0) * matrix(1, 2) * matrix(3, 1) +
24223b3eb3cSopenharmony_ci            matrix(1, 0) * matrix(0, 1) * matrix(3, 2) - matrix(1, 0) * matrix(0, 2) * matrix(3, 1) -
24323b3eb3cSopenharmony_ci            matrix(3, 0) * matrix(0, 1) * matrix(1, 2) + matrix(3, 0) * matrix(0, 2) * matrix(1, 1),
24423b3eb3cSopenharmony_ci        -matrix(0, 1) * matrix(1, 2) * matrix(2, 3) + matrix(0, 1) * matrix(1, 3) * matrix(2, 2) +
24523b3eb3cSopenharmony_ci            matrix(1, 1) * matrix(0, 2) * matrix(2, 3) - matrix(1, 1) * matrix(0, 3) * matrix(2, 2) -
24623b3eb3cSopenharmony_ci            matrix(2, 1) * matrix(0, 2) * matrix(1, 3) + matrix(2, 1) * matrix(0, 3) * matrix(1, 2),
24723b3eb3cSopenharmony_ci        matrix(0, 0) * matrix(1, 2) * matrix(2, 3) - matrix(0, 0) * matrix(1, 3) * matrix(2, 2) -
24823b3eb3cSopenharmony_ci            matrix(1, 0) * matrix(0, 2) * matrix(2, 3) + matrix(1, 0) * matrix(0, 3) * matrix(2, 2) +
24923b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(0, 2) * matrix(1, 3) - matrix(2, 0) * matrix(0, 3) * matrix(1, 2),
25023b3eb3cSopenharmony_ci        -matrix(0, 0) * matrix(1, 1) * matrix(2, 3) + matrix(0, 0) * matrix(1, 3) * matrix(2, 1) +
25123b3eb3cSopenharmony_ci            matrix(1, 0) * matrix(0, 1) * matrix(2, 3) - matrix(1, 0) * matrix(0, 3) * matrix(2, 1) -
25223b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(0, 1) * matrix(1, 3) + matrix(2, 0) * matrix(0, 3) * matrix(1, 1),
25323b3eb3cSopenharmony_ci        matrix(0, 0) * matrix(1, 1) * matrix(2, 2) - matrix(0, 0) * matrix(1, 2) * matrix(2, 1) -
25423b3eb3cSopenharmony_ci            matrix(1, 0) * matrix(0, 1) * matrix(2, 2) + matrix(1, 0) * matrix(0, 2) * matrix(2, 1) +
25523b3eb3cSopenharmony_ci            matrix(2, 0) * matrix(0, 1) * matrix(1, 2) - matrix(2, 0) * matrix(0, 2) * matrix(1, 1));
25623b3eb3cSopenharmony_ci}
25723b3eb3cSopenharmony_ci
25823b3eb3cSopenharmony_cibool Matrix4::operator==(const Matrix4& matrix) const
25923b3eb3cSopenharmony_ci{
26023b3eb3cSopenharmony_ci    return std::equal(&matrix4x4_[0][0], &matrix4x4_[0][0] + MATRIX_LENGTH, &matrix.matrix4x4_[0][0], IsEqual);
26123b3eb3cSopenharmony_ci}
26223b3eb3cSopenharmony_ci
26323b3eb3cSopenharmony_ciMatrix4 Matrix4::operator*(double num)
26423b3eb3cSopenharmony_ci{
26523b3eb3cSopenharmony_ci    Matrix4 ret(*this);
26623b3eb3cSopenharmony_ci    auto function = [num](double& v) { v *= num; };
26723b3eb3cSopenharmony_ci    auto it = &ret.matrix4x4_[0][0];
26823b3eb3cSopenharmony_ci    for (int32_t i = 0; i < MATRIX_LENGTH; ++it, ++i) {
26923b3eb3cSopenharmony_ci        function(*it);
27023b3eb3cSopenharmony_ci    }
27123b3eb3cSopenharmony_ci    return ret;
27223b3eb3cSopenharmony_ci}
27323b3eb3cSopenharmony_ci
27423b3eb3cSopenharmony_ciMatrix4 Matrix4::operator*(const Matrix4& matrix)
27523b3eb3cSopenharmony_ci{
27623b3eb3cSopenharmony_ci    return Matrix4(
27723b3eb3cSopenharmony_ci        matrix4x4_[0][0] * matrix(0, 0) + matrix4x4_[1][0] * matrix(0, 1) + matrix4x4_[2][0] * matrix(0, 2) +
27823b3eb3cSopenharmony_ci            matrix4x4_[3][0] * matrix(0, 3),
27923b3eb3cSopenharmony_ci        matrix4x4_[0][0] * matrix(1, 0) + matrix4x4_[1][0] * matrix(1, 1) + matrix4x4_[2][0] * matrix(1, 2) +
28023b3eb3cSopenharmony_ci            matrix4x4_[3][0] * matrix(1, 3),
28123b3eb3cSopenharmony_ci        matrix4x4_[0][0] * matrix(2, 0) + matrix4x4_[1][0] * matrix(2, 1) + matrix4x4_[2][0] * matrix(2, 2) +
28223b3eb3cSopenharmony_ci            matrix4x4_[3][0] * matrix(2, 3),
28323b3eb3cSopenharmony_ci        matrix4x4_[0][0] * matrix(3, 0) + matrix4x4_[1][0] * matrix(3, 1) + matrix4x4_[2][0] * matrix(3, 2) +
28423b3eb3cSopenharmony_ci            matrix4x4_[3][0] * matrix(3, 3),
28523b3eb3cSopenharmony_ci        matrix4x4_[0][1] * matrix(0, 0) + matrix4x4_[1][1] * matrix(0, 1) + matrix4x4_[2][1] * matrix(0, 2) +
28623b3eb3cSopenharmony_ci            matrix4x4_[3][1] * matrix(0, 3),
28723b3eb3cSopenharmony_ci        matrix4x4_[0][1] * matrix(1, 0) + matrix4x4_[1][1] * matrix(1, 1) + matrix4x4_[2][1] * matrix(1, 2) +
28823b3eb3cSopenharmony_ci            matrix4x4_[3][1] * matrix(1, 3),
28923b3eb3cSopenharmony_ci        matrix4x4_[0][1] * matrix(2, 0) + matrix4x4_[1][1] * matrix(2, 1) + matrix4x4_[2][1] * matrix(2, 2) +
29023b3eb3cSopenharmony_ci            matrix4x4_[3][1] * matrix(2, 3),
29123b3eb3cSopenharmony_ci        matrix4x4_[0][1] * matrix(3, 0) + matrix4x4_[1][1] * matrix(3, 1) + matrix4x4_[2][1] * matrix(3, 2) +
29223b3eb3cSopenharmony_ci            matrix4x4_[3][1] * matrix(3, 3),
29323b3eb3cSopenharmony_ci        matrix4x4_[0][2] * matrix(0, 0) + matrix4x4_[1][2] * matrix(0, 1) + matrix4x4_[2][2] * matrix(0, 2) +
29423b3eb3cSopenharmony_ci            matrix4x4_[3][2] * matrix(0, 3),
29523b3eb3cSopenharmony_ci        matrix4x4_[0][2] * matrix(1, 0) + matrix4x4_[1][2] * matrix(1, 1) + matrix4x4_[2][2] * matrix(1, 2) +
29623b3eb3cSopenharmony_ci            matrix4x4_[3][2] * matrix(1, 3),
29723b3eb3cSopenharmony_ci        matrix4x4_[0][2] * matrix(2, 0) + matrix4x4_[1][2] * matrix(2, 1) + matrix4x4_[2][2] * matrix(2, 2) +
29823b3eb3cSopenharmony_ci            matrix4x4_[3][2] * matrix(2, 3),
29923b3eb3cSopenharmony_ci        matrix4x4_[0][2] * matrix(3, 0) + matrix4x4_[1][2] * matrix(3, 1) + matrix4x4_[2][2] * matrix(3, 2) +
30023b3eb3cSopenharmony_ci            matrix4x4_[3][2] * matrix(3, 3),
30123b3eb3cSopenharmony_ci        matrix4x4_[0][3] * matrix(0, 0) + matrix4x4_[1][3] * matrix(0, 1) + matrix4x4_[2][3] * matrix(0, 2) +
30223b3eb3cSopenharmony_ci            matrix4x4_[3][3] * matrix(0, 3),
30323b3eb3cSopenharmony_ci        matrix4x4_[0][3] * matrix(1, 0) + matrix4x4_[1][3] * matrix(1, 1) + matrix4x4_[2][3] * matrix(1, 2) +
30423b3eb3cSopenharmony_ci            matrix4x4_[3][3] * matrix(1, 3),
30523b3eb3cSopenharmony_ci        matrix4x4_[0][3] * matrix(2, 0) + matrix4x4_[1][3] * matrix(2, 1) + matrix4x4_[2][3] * matrix(2, 2) +
30623b3eb3cSopenharmony_ci            matrix4x4_[3][3] * matrix(2, 3),
30723b3eb3cSopenharmony_ci        matrix4x4_[0][3] * matrix(3, 0) + matrix4x4_[1][3] * matrix(3, 1) + matrix4x4_[2][3] * matrix(3, 2) +
30823b3eb3cSopenharmony_ci            matrix4x4_[3][3] * matrix(3, 3));
30923b3eb3cSopenharmony_ci}
31023b3eb3cSopenharmony_ci
31123b3eb3cSopenharmony_ciMatrix4N Matrix4::operator*(const Matrix4N& matrix) const
31223b3eb3cSopenharmony_ci{
31323b3eb3cSopenharmony_ci    int32_t columns = matrix.GetColNum();
31423b3eb3cSopenharmony_ci    Matrix4N matrix4n(columns);
31523b3eb3cSopenharmony_ci    for (auto i = 0; i < DIMENSION; i++) {
31623b3eb3cSopenharmony_ci        for (auto j = 0; j < columns; j++) {
31723b3eb3cSopenharmony_ci            double value = 0.0;
31823b3eb3cSopenharmony_ci            for (auto k = 0; k < DIMENSION; k++) {
31923b3eb3cSopenharmony_ci                value += matrix4x4_[i][k] * matrix[k][j];
32023b3eb3cSopenharmony_ci            }
32123b3eb3cSopenharmony_ci            matrix4n[i][j] = value;
32223b3eb3cSopenharmony_ci        }
32323b3eb3cSopenharmony_ci    }
32423b3eb3cSopenharmony_ci    return matrix4n;
32523b3eb3cSopenharmony_ci}
32623b3eb3cSopenharmony_ci
32723b3eb3cSopenharmony_ciPoint Matrix4::operator*(const Point& point)
32823b3eb3cSopenharmony_ci{
32923b3eb3cSopenharmony_ci    double x = point.GetX();
33023b3eb3cSopenharmony_ci    double y = point.GetY();
33123b3eb3cSopenharmony_ci    return Point(matrix4x4_[0][0] * x + matrix4x4_[1][0] * y + matrix4x4_[3][0],
33223b3eb3cSopenharmony_ci        matrix4x4_[0][1] * x + matrix4x4_[1][1] * y + matrix4x4_[3][1]);
33323b3eb3cSopenharmony_ci}
33423b3eb3cSopenharmony_ci
33523b3eb3cSopenharmony_ciMatrix4& Matrix4::operator=(const Matrix4& matrix)
33623b3eb3cSopenharmony_ci{
33723b3eb3cSopenharmony_ci    if (this == &matrix) {
33823b3eb3cSopenharmony_ci        return *this;
33923b3eb3cSopenharmony_ci    }
34023b3eb3cSopenharmony_ci    std::copy_n(&matrix.matrix4x4_[0][0], MATRIX_LENGTH, &matrix4x4_[0][0]);
34123b3eb3cSopenharmony_ci    return *this;
34223b3eb3cSopenharmony_ci}
34323b3eb3cSopenharmony_ci
34423b3eb3cSopenharmony_cidouble Matrix4::operator[](int32_t index) const
34523b3eb3cSopenharmony_ci{
34623b3eb3cSopenharmony_ci    if (index < 0 || index >= MATRIX_LENGTH) {
34723b3eb3cSopenharmony_ci        return 0.0f;
34823b3eb3cSopenharmony_ci    }
34923b3eb3cSopenharmony_ci    int32_t row = index / DIMENSION;
35023b3eb3cSopenharmony_ci    int32_t col = index % DIMENSION;
35123b3eb3cSopenharmony_ci    return matrix4x4_[row][col];
35223b3eb3cSopenharmony_ci}
35323b3eb3cSopenharmony_ci
35423b3eb3cSopenharmony_cidouble Matrix4::operator()(int32_t row, int32_t col) const
35523b3eb3cSopenharmony_ci{
35623b3eb3cSopenharmony_ci    // Caller guarantee row and col in range of [0, 3].
35723b3eb3cSopenharmony_ci    return matrix4x4_[row][col];
35823b3eb3cSopenharmony_ci}
35923b3eb3cSopenharmony_ci
36023b3eb3cSopenharmony_cidouble Matrix4::Determinant() const
36123b3eb3cSopenharmony_ci{
36223b3eb3cSopenharmony_ci    if (this->IsIdentityMatrix()) {
36323b3eb3cSopenharmony_ci        return 1.0;
36423b3eb3cSopenharmony_ci    }
36523b3eb3cSopenharmony_ci
36623b3eb3cSopenharmony_ci    double m00 = matrix4x4_[0][0];
36723b3eb3cSopenharmony_ci    double m01 = matrix4x4_[0][1];
36823b3eb3cSopenharmony_ci    double m02 = matrix4x4_[0][2];
36923b3eb3cSopenharmony_ci    double m03 = matrix4x4_[0][3];
37023b3eb3cSopenharmony_ci    double m10 = matrix4x4_[1][0];
37123b3eb3cSopenharmony_ci    double m11 = matrix4x4_[1][1];
37223b3eb3cSopenharmony_ci    double m12 = matrix4x4_[1][2];
37323b3eb3cSopenharmony_ci    double m13 = matrix4x4_[1][3];
37423b3eb3cSopenharmony_ci    double m20 = matrix4x4_[2][0];
37523b3eb3cSopenharmony_ci    double m21 = matrix4x4_[2][1];
37623b3eb3cSopenharmony_ci    double m22 = matrix4x4_[2][2];
37723b3eb3cSopenharmony_ci    double m23 = matrix4x4_[2][3];
37823b3eb3cSopenharmony_ci    double m30 = matrix4x4_[3][0];
37923b3eb3cSopenharmony_ci    double m31 = matrix4x4_[3][1];
38023b3eb3cSopenharmony_ci    double m32 = matrix4x4_[3][2];
38123b3eb3cSopenharmony_ci    double m33 = matrix4x4_[3][3];
38223b3eb3cSopenharmony_ci
38323b3eb3cSopenharmony_ci    double b00 = m00 * m11 - m01 * m10;
38423b3eb3cSopenharmony_ci    double b01 = m00 * m12 - m02 * m10;
38523b3eb3cSopenharmony_ci    double b02 = m00 * m13 - m03 * m10;
38623b3eb3cSopenharmony_ci    double b03 = m01 * m12 - m02 * m11;
38723b3eb3cSopenharmony_ci    double b04 = m01 * m13 - m03 * m11;
38823b3eb3cSopenharmony_ci    double b05 = m02 * m13 - m03 * m12;
38923b3eb3cSopenharmony_ci    double b06 = m20 * m31 - m21 * m30;
39023b3eb3cSopenharmony_ci    double b07 = m20 * m32 - m22 * m30;
39123b3eb3cSopenharmony_ci    double b08 = m20 * m33 - m23 * m30;
39223b3eb3cSopenharmony_ci    double b09 = m21 * m32 - m22 * m31;
39323b3eb3cSopenharmony_ci    double b10 = m21 * m33 - m23 * m31;
39423b3eb3cSopenharmony_ci    double b11 = m22 * m33 - m23 * m32;
39523b3eb3cSopenharmony_ci
39623b3eb3cSopenharmony_ci    return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
39723b3eb3cSopenharmony_ci}
39823b3eb3cSopenharmony_ci
39923b3eb3cSopenharmony_civoid Matrix4::Transpose()
40023b3eb3cSopenharmony_ci{
40123b3eb3cSopenharmony_ci    std::swap(matrix4x4_[0][1], matrix4x4_[1][0]);
40223b3eb3cSopenharmony_ci    std::swap(matrix4x4_[0][2], matrix4x4_[2][0]);
40323b3eb3cSopenharmony_ci    std::swap(matrix4x4_[0][3], matrix4x4_[3][0]);
40423b3eb3cSopenharmony_ci    std::swap(matrix4x4_[1][2], matrix4x4_[2][1]);
40523b3eb3cSopenharmony_ci    std::swap(matrix4x4_[1][3], matrix4x4_[3][1]);
40623b3eb3cSopenharmony_ci    std::swap(matrix4x4_[2][3], matrix4x4_[3][2]);
40723b3eb3cSopenharmony_ci}
40823b3eb3cSopenharmony_ci
40923b3eb3cSopenharmony_civoid Matrix4::MapScalars(const double src[DIMENSION], double dst[DIMENSION]) const
41023b3eb3cSopenharmony_ci{
41123b3eb3cSopenharmony_ci    double storage[DIMENSION];
41223b3eb3cSopenharmony_ci
41323b3eb3cSopenharmony_ci    double* result = (src == dst) ? storage : dst;
41423b3eb3cSopenharmony_ci
41523b3eb3cSopenharmony_ci    for (int i = 0; i < DIMENSION; i++) {
41623b3eb3cSopenharmony_ci        double value = 0;
41723b3eb3cSopenharmony_ci        for (int j = 0; j < DIMENSION; j++) {
41823b3eb3cSopenharmony_ci            value += matrix4x4_[j][i] * src[j];
41923b3eb3cSopenharmony_ci        }
42023b3eb3cSopenharmony_ci        result[i] = value;
42123b3eb3cSopenharmony_ci    }
42223b3eb3cSopenharmony_ci
42323b3eb3cSopenharmony_ci    if (storage == result) {
42423b3eb3cSopenharmony_ci        std::copy_n(result, DIMENSION, dst);
42523b3eb3cSopenharmony_ci    }
42623b3eb3cSopenharmony_ci}
42723b3eb3cSopenharmony_ci
42823b3eb3cSopenharmony_cistd::string Matrix4::ToString() const
42923b3eb3cSopenharmony_ci{
43023b3eb3cSopenharmony_ci    std::string out;
43123b3eb3cSopenharmony_ci    for (auto& i : matrix4x4_) {
43223b3eb3cSopenharmony_ci        for (double j : i) {
43323b3eb3cSopenharmony_ci            out += std::to_string(j);
43423b3eb3cSopenharmony_ci            out += ",";
43523b3eb3cSopenharmony_ci        }
43623b3eb3cSopenharmony_ci        out += "\n";
43723b3eb3cSopenharmony_ci    }
43823b3eb3cSopenharmony_ci    return out;
43923b3eb3cSopenharmony_ci}
44023b3eb3cSopenharmony_ci
44123b3eb3cSopenharmony_ciMatrix4N::Matrix4N(int32_t columns) : columns_(columns)
44223b3eb3cSopenharmony_ci{
44323b3eb3cSopenharmony_ci    matrix4n_.resize(DIMENSION, std::vector<double>(columns_, 0));
44423b3eb3cSopenharmony_ci}
44523b3eb3cSopenharmony_ci
44623b3eb3cSopenharmony_cibool Matrix4N::SetEntry(int32_t row, int32_t col, double value)
44723b3eb3cSopenharmony_ci{
44823b3eb3cSopenharmony_ci    if (row < 0 || row >= DIMENSION || col < 0 || col >= columns_) {
44923b3eb3cSopenharmony_ci        return false;
45023b3eb3cSopenharmony_ci    }
45123b3eb3cSopenharmony_ci    matrix4n_[row][col] = value;
45223b3eb3cSopenharmony_ci    return true;
45323b3eb3cSopenharmony_ci}
45423b3eb3cSopenharmony_ci
45523b3eb3cSopenharmony_ciMatrix4 Matrix4N::operator*(const MatrixN4& matrix) const
45623b3eb3cSopenharmony_ci{
45723b3eb3cSopenharmony_ci    auto matrix4 = Matrix4::CreateIdentity();
45823b3eb3cSopenharmony_ci    if (columns_ != matrix.GetRowNum()) {
45923b3eb3cSopenharmony_ci        return matrix4;
46023b3eb3cSopenharmony_ci    }
46123b3eb3cSopenharmony_ci    for (auto i = 0; i < DIMENSION; i++) {
46223b3eb3cSopenharmony_ci        for (auto j = 0; j < DIMENSION; j++) {
46323b3eb3cSopenharmony_ci            double value = 0.0;
46423b3eb3cSopenharmony_ci            for (auto k = 0; k < columns_; k++) {
46523b3eb3cSopenharmony_ci                value += matrix4n_[i][k] * matrix[k][j];
46623b3eb3cSopenharmony_ci            }
46723b3eb3cSopenharmony_ci            matrix4.SetEntry(i, j, value);
46823b3eb3cSopenharmony_ci        }
46923b3eb3cSopenharmony_ci    }
47023b3eb3cSopenharmony_ci    return matrix4;
47123b3eb3cSopenharmony_ci}
47223b3eb3cSopenharmony_ci
47323b3eb3cSopenharmony_ciMatrixN4 Matrix4N::Transpose() const
47423b3eb3cSopenharmony_ci{
47523b3eb3cSopenharmony_ci    MatrixN4 matrix { columns_ };
47623b3eb3cSopenharmony_ci    for (auto i = 0; i < DIMENSION; i++) {
47723b3eb3cSopenharmony_ci        for (auto j = 0; j < columns_; j++) {
47823b3eb3cSopenharmony_ci            matrix[j][i] = matrix4n_[i][j];
47923b3eb3cSopenharmony_ci        }
48023b3eb3cSopenharmony_ci    }
48123b3eb3cSopenharmony_ci    return matrix;
48223b3eb3cSopenharmony_ci}
48323b3eb3cSopenharmony_ci
48423b3eb3cSopenharmony_cistd::vector<double> Matrix4N::MapScalars(const std::vector<double>& src) const
48523b3eb3cSopenharmony_ci{
48623b3eb3cSopenharmony_ci    std::vector<double> value(DIMENSION, 0);
48723b3eb3cSopenharmony_ci    if (static_cast<int32_t>(src.size()) != columns_) {
48823b3eb3cSopenharmony_ci        return value;
48923b3eb3cSopenharmony_ci    }
49023b3eb3cSopenharmony_ci    for (int32_t i = 0; i < DIMENSION; i++) {
49123b3eb3cSopenharmony_ci        double item = 0.0;
49223b3eb3cSopenharmony_ci        for (int32_t j = 0; j < columns_; j++) {
49323b3eb3cSopenharmony_ci            item = item + matrix4n_[i][j] * src[j];
49423b3eb3cSopenharmony_ci        }
49523b3eb3cSopenharmony_ci        value[i] = item;
49623b3eb3cSopenharmony_ci    }
49723b3eb3cSopenharmony_ci    return value;
49823b3eb3cSopenharmony_ci}
49923b3eb3cSopenharmony_ci
50023b3eb3cSopenharmony_cibool Matrix4N::MapScalars(const std::vector<double>& src, std::vector<double>& result) const
50123b3eb3cSopenharmony_ci{
50223b3eb3cSopenharmony_ci    if (static_cast<int32_t>(src.size()) != columns_) {
50323b3eb3cSopenharmony_ci        return false;
50423b3eb3cSopenharmony_ci    }
50523b3eb3cSopenharmony_ci    result.resize(DIMENSION, 0);
50623b3eb3cSopenharmony_ci    for (int32_t i = 0; i < DIMENSION; i++) {
50723b3eb3cSopenharmony_ci        double item = 0.0;
50823b3eb3cSopenharmony_ci        for (int32_t j = 0; j < columns_; j++) {
50923b3eb3cSopenharmony_ci            item = item + matrix4n_[i][j] * src[j];
51023b3eb3cSopenharmony_ci        }
51123b3eb3cSopenharmony_ci        result[i] = item;
51223b3eb3cSopenharmony_ci    }
51323b3eb3cSopenharmony_ci    return true;
51423b3eb3cSopenharmony_ci}
51523b3eb3cSopenharmony_ci
51623b3eb3cSopenharmony_ciMatrixN4::MatrixN4(int32_t rows) : rows_(rows)
51723b3eb3cSopenharmony_ci{
51823b3eb3cSopenharmony_ci    matrixn4_.resize(rows, std::vector<double>(DIMENSION, 0));
51923b3eb3cSopenharmony_ci}
52023b3eb3cSopenharmony_ci
52123b3eb3cSopenharmony_cibool MatrixN4::SetEntry(int32_t row, int32_t col, double value)
52223b3eb3cSopenharmony_ci{
52323b3eb3cSopenharmony_ci    if (row < 0 || row >= rows_ || col < 0 || col >= DIMENSION) {
52423b3eb3cSopenharmony_ci        return false;
52523b3eb3cSopenharmony_ci    }
52623b3eb3cSopenharmony_ci    matrixn4_[row][col] = value;
52723b3eb3cSopenharmony_ci    return true;
52823b3eb3cSopenharmony_ci}
52923b3eb3cSopenharmony_ci
53023b3eb3cSopenharmony_ciMatrix4N MatrixN4::Transpose() const
53123b3eb3cSopenharmony_ci{
53223b3eb3cSopenharmony_ci    Matrix4N matrix { rows_ };
53323b3eb3cSopenharmony_ci    for (auto i = 0; i < DIMENSION; i++) {
53423b3eb3cSopenharmony_ci        for (auto j = 0; j < rows_; j++) {
53523b3eb3cSopenharmony_ci            matrix[i][j] = matrixn4_[j][i];
53623b3eb3cSopenharmony_ci        }
53723b3eb3cSopenharmony_ci    }
53823b3eb3cSopenharmony_ci    return matrix;
53923b3eb3cSopenharmony_ci}
54023b3eb3cSopenharmony_ci
54123b3eb3cSopenharmony_cistd::vector<double> MatrixN4::MapScalars(const std::vector<double>& src) const
54223b3eb3cSopenharmony_ci{
54323b3eb3cSopenharmony_ci    std::vector<double> value(rows_, 0);
54423b3eb3cSopenharmony_ci    if (static_cast<int32_t>(src.size()) != DIMENSION) {
54523b3eb3cSopenharmony_ci        return value;
54623b3eb3cSopenharmony_ci    }
54723b3eb3cSopenharmony_ci    for (int32_t i = 0; i < rows_; i++) {
54823b3eb3cSopenharmony_ci        double item = 0.0;
54923b3eb3cSopenharmony_ci        for (int32_t j = 0; j < DIMENSION; j++) {
55023b3eb3cSopenharmony_ci            item = item + matrixn4_[i][j] * src[j];
55123b3eb3cSopenharmony_ci        }
55223b3eb3cSopenharmony_ci        value[i] = item;
55323b3eb3cSopenharmony_ci    }
55423b3eb3cSopenharmony_ci    return value;
55523b3eb3cSopenharmony_ci}
55623b3eb3cSopenharmony_ci} // namespace OHOS::Ace
557