1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 15:23:05 +04:00

Merge branch 4.x

This commit is contained in:
Alexander Smorkalov
2025-05-27 16:48:22 +03:00
167 changed files with 7028 additions and 4687 deletions
+174 -1
View File
@@ -1734,7 +1734,7 @@ static void checkRoot(Mat& r, T re, T im)
{
for (int i = 0; i < r.cols*r.rows; i++)
{
Vec<T, 2> v = *(Vec<T, 2>*)r.ptr(i);
Vec<T, 2>& v = *(Vec<T, 2>*)r.ptr(i);
if (fabs(re - v[0]) < 1e-6 && fabs(im - v[1]) < 1e-6)
{
v[0] = std::numeric_limits<T>::quiet_NaN();
@@ -1744,6 +1744,179 @@ static void checkRoot(Mat& r, T re, T im)
}
GTEST_NONFATAL_FAILURE_("Can't find root") << "(" << re << ", " << im << ")";
}
TEST(Core_SolveCubicConstant, accuracy)
{
{
const std::vector<double> coeffs{0., 0., 0., 1.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 0);
}
{
const std::vector<double> coeffs{0., 0., 0., 0.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, -1);
}
}
TEST(Core_SolveCubicLinear, accuracy)
{
const std::vector<double> coeffs{0., 0., 2., -2.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_EQ(roots[0], 1.);
}
TEST(Core_SolveCubicQuadratic, accuracy)
{
{
const std::vector<double> coeffs{0., 2., -4., 4.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 0);
}
{
const std::vector<double> coeffs{0., 2., -4., 2.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_EQ(roots[0], 1.);
}
{
const std::vector<double> coeffs{0., 2., -6., 4.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 2);
EXPECT_EQ(roots[0], 2.);
EXPECT_EQ(roots[1], 1.);
}
}
TEST(Core_SolveCubicCubic, accuracy)
{
{
const std::vector<double> coeffs{2., -6., 6., -2.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_EQ(roots[0], 1.);
}
{
const std::vector<double> coeffs{2., -10., 24., -16.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_NEAR(roots[0], 1., 1e-8);
}
{
const std::vector<double> coeffs{2., -10., 16., -8.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_TRUE(num_roots == 2 || num_roots == 3);
EXPECT_NEAR(roots[0], 1., 1e-8);
EXPECT_NEAR(roots[1], 2., 1e-8);
if (num_roots == 3)
{
EXPECT_NEAR(roots[2], 2., 1e-8);
}
}
{
const std::vector<double> coeffs{2., -12., 22., -12.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 3);
EXPECT_NEAR(roots[0], 1., 1e-8);
EXPECT_NEAR(roots[1], 3., 1e-8);
EXPECT_NEAR(roots[2], 2., 1e-8);
}
}
TEST(Core_SolveCubicNormalizedCubic, accuracy)
{
{
const std::vector<double> coeffs{-3., 3., -1.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_EQ(roots[0], 1.);
}
{
const std::vector<double> coeffs{-5., 12., -8.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_NEAR(roots[0], 1., 1e-8);
}
{
const std::vector<double> coeffs{-5., 8., -4.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_TRUE(num_roots == 2 || num_roots == 3);
EXPECT_NEAR(roots[0], 1., 1e-8);
EXPECT_NEAR(roots[1], 2., 1e-8);
if (num_roots == 3)
{
EXPECT_NEAR(roots[2], 2., 1e-8);
}
}
{
const std::vector<double> coeffs{-6., 11., -6.};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 3);
EXPECT_NEAR(roots[0], 1., 1e-8);
EXPECT_NEAR(roots[1], 3., 1e-8);
EXPECT_NEAR(roots[2], 2., 1e-8);
}
}
TEST(Core_SolveCubic, regression_27323)
{
{
const std::vector<double> coeffs{2e-13, 1, -2, 1};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_EQ(roots[0], -5e12 - 2.);
}
{
const std::vector<double> coeffs{5e12, -1e13, 5e12};
std::vector<double> roots;
const auto num_roots = solveCubic(coeffs, roots);
EXPECT_EQ(num_roots, 1);
EXPECT_EQ(roots[0], -5e12 - 2.);
}
}
TEST(Core_SolvePoly, regression_5599)
{
// x^4 - x^2 = 0, roots: 1, -1, 0, 0