Blender V4.3
poly_test.cc
Go to the documentation of this file.
1// Copyright (c) 2007, 2008 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21#include "libmv/numeric/poly.h"
23#include "testing/testing.h"
24
25using namespace libmv;
26
27namespace {
28
29// Find the polynomial coefficients of x in the equation
30//
31// (x - a)(x - b)(x - c) == 0
32//
33// by expanding to
34//
35// x^3 - (c+b+a) * x^2 + (a*b+(b+a)*c) * x - a*b*c = 0.
36// = p = q = r
37void CoeffsForCubicZeros(
38 double a, double b, double c, double* p, double* q, double* r) {
39 *p = -(c + b + a);
40 *q = (a * b + (b + a) * c);
41 *r = -a * b * c;
42}
43
45 double a, b, c, aa, bb, cc;
46 double p, q, r;
47
48 a = 1;
49 b = 2;
50 c = 3;
51 CoeffsForCubicZeros(a, b, c, &p, &q, &r);
52 ASSERT_EQ(3, SolveCubicPolynomial(p, q, r, &aa, &bb, &cc));
53 EXPECT_NEAR(a, aa, 1e-10);
54 EXPECT_NEAR(b, bb, 1e-10);
55 EXPECT_NEAR(c, cc, 1e-10);
56
57 a = 0;
58 b = 1;
59 c = 3;
60 CoeffsForCubicZeros(a, b, c, &p, &q, &r);
61 ASSERT_EQ(3, SolveCubicPolynomial(p, q, r, &aa, &bb, &cc));
62 EXPECT_NEAR(a, aa, 1e-10);
63 EXPECT_NEAR(b, bb, 1e-10);
64 EXPECT_NEAR(c, cc, 1e-10);
65
66 a = -10;
67 b = 0;
68 c = 1;
69 CoeffsForCubicZeros(a, b, c, &p, &q, &r);
70 ASSERT_EQ(3, SolveCubicPolynomial(p, q, r, &aa, &bb, &cc));
71 EXPECT_NEAR(a, aa, 1e-10);
72 EXPECT_NEAR(b, bb, 1e-10);
73 EXPECT_NEAR(c, cc, 1e-10);
74
75 a = -8;
76 b = 1;
77 c = 3;
78 CoeffsForCubicZeros(a, b, c, &p, &q, &r);
79 ASSERT_EQ(3, SolveCubicPolynomial(p, q, r, &aa, &bb, &cc));
80 EXPECT_NEAR(a, aa, 1e-10);
81 EXPECT_NEAR(b, bb, 1e-10);
82 EXPECT_NEAR(c, cc, 1e-10);
83
84 a = 28;
85 b = 28;
86 c = 105;
87 CoeffsForCubicZeros(a, b, c, &p, &q, &r);
88 ASSERT_EQ(3, SolveCubicPolynomial(p, q, r, &aa, &bb, &cc));
89 EXPECT_NEAR(a, aa, 1e-10);
90 EXPECT_NEAR(b, bb, 1e-10);
91 EXPECT_NEAR(c, cc, 1e-10);
92}
93} // namespace
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
local_group_size(16, 16) .push_constant(Type b
int SolveCubicPolynomial(Real a, Real b, Real c, Real *x0, Real *x1, Real *x2)
Definition poly.h:40
TEST(PolynomialCameraIntrinsics2, ApplyOnFocalCenter)