2018-08-26 21:23:42 +08:00
|
|
|
// (C) Copyright 2017, Google Inc.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2018-08-26 21:23:42 +08:00
|
|
|
#include "linlsq.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2018-08-26 21:23:42 +08:00
|
|
|
#include "include_gunit.h"
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2020-12-27 17:41:48 +08:00
|
|
|
namespace tesseract {
|
2018-08-24 21:07:48 +08:00
|
|
|
|
|
|
|
class LLSQTest : public testing::Test {
|
2021-03-13 05:06:34 +08:00
|
|
|
protected:
|
2021-03-22 15:26:05 +08:00
|
|
|
void SetUp() override {
|
2019-05-17 00:12:06 +08:00
|
|
|
std::locale::global(std::locale(""));
|
|
|
|
}
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
public:
|
2021-03-22 15:26:05 +08:00
|
|
|
void TearDown() override {}
|
2018-08-24 21:07:48 +08:00
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
void ExpectCorrectLine(const LLSQ &llsq, double m, double c, double rms, double pearson,
|
|
|
|
double tolerance) {
|
2018-08-24 21:07:48 +08:00
|
|
|
EXPECT_NEAR(m, llsq.m(), tolerance);
|
|
|
|
EXPECT_NEAR(c, llsq.c(llsq.m()), tolerance);
|
|
|
|
EXPECT_NEAR(rms, llsq.rms(llsq.m(), llsq.c(llsq.m())), tolerance);
|
|
|
|
EXPECT_NEAR(pearson, llsq.pearson(), tolerance);
|
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
FCOORD PtsMean(const std::vector<FCOORD> &pts) {
|
2018-08-24 21:07:48 +08:00
|
|
|
FCOORD total(0, 0);
|
2021-03-13 05:06:34 +08:00
|
|
|
for (const auto &p : pts) {
|
2019-03-25 16:33:56 +08:00
|
|
|
total += p;
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
return (pts.size() > 0) ? total / pts.size() : total;
|
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
void VerifyRmsOrth(const std::vector<FCOORD> &pts, const FCOORD &orth) {
|
2018-08-24 21:07:48 +08:00
|
|
|
LLSQ llsq;
|
|
|
|
FCOORD xavg = PtsMean(pts);
|
|
|
|
FCOORD nvec = !orth;
|
|
|
|
nvec.normalise();
|
|
|
|
double expected_answer = 0;
|
2021-03-13 05:06:34 +08:00
|
|
|
for (const auto &p : pts) {
|
2019-03-25 16:33:56 +08:00
|
|
|
llsq.add(p.x(), p.y());
|
|
|
|
double dot = nvec % (p - xavg);
|
2018-08-24 21:07:48 +08:00
|
|
|
expected_answer += dot * dot;
|
|
|
|
}
|
|
|
|
expected_answer /= pts.size();
|
|
|
|
expected_answer = sqrt(expected_answer);
|
|
|
|
EXPECT_NEAR(expected_answer, llsq.rms_orth(orth), 0.0001);
|
|
|
|
}
|
2021-03-13 05:06:34 +08:00
|
|
|
void ExpectCorrectVector(const LLSQ &llsq, FCOORD correct_mean_pt, FCOORD correct_vector,
|
|
|
|
float tolerance) {
|
2018-08-24 21:07:48 +08:00
|
|
|
FCOORD mean_pt = llsq.mean_point();
|
|
|
|
FCOORD vector = llsq.vector_fit();
|
|
|
|
EXPECT_NEAR(correct_mean_pt.x(), mean_pt.x(), tolerance);
|
|
|
|
EXPECT_NEAR(correct_mean_pt.y(), mean_pt.y(), tolerance);
|
|
|
|
EXPECT_NEAR(correct_vector.x(), vector.x(), tolerance);
|
|
|
|
EXPECT_NEAR(correct_vector.y(), vector.y(), tolerance);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests a simple baseline-style normalization.
|
|
|
|
TEST_F(LLSQTest, BasicLines) {
|
|
|
|
LLSQ llsq;
|
|
|
|
llsq.add(1.0, 1.0);
|
|
|
|
llsq.add(2.0, 2.0);
|
|
|
|
ExpectCorrectLine(llsq, 1.0, 0.0, 0.0, 1.0, 1e-6);
|
|
|
|
float half_root_2 = sqrt(2.0) / 2.0f;
|
2021-03-13 05:06:34 +08:00
|
|
|
ExpectCorrectVector(llsq, FCOORD(1.5f, 1.5f), FCOORD(half_root_2, half_root_2), 1e-6);
|
2018-08-24 21:07:48 +08:00
|
|
|
llsq.remove(2.0, 2.0);
|
|
|
|
llsq.add(1.0, 2.0);
|
|
|
|
llsq.add(10.0, 1.0);
|
|
|
|
llsq.add(-8.0, 1.0);
|
|
|
|
// The point at 1,2 pulls the result away from what would otherwise be a
|
|
|
|
// perfect fit to a horizontal line by 0.25 unit, with rms error of 0.433.
|
|
|
|
ExpectCorrectLine(llsq, 0.0, 1.25, 0.433, 0.0, 1e-2);
|
2018-09-29 15:19:13 +08:00
|
|
|
ExpectCorrectVector(llsq, FCOORD(1.0f, 1.25f), FCOORD(1.0f, 0.0f), 1e-3);
|
2018-08-24 21:07:48 +08:00
|
|
|
llsq.add(1.0, 2.0, 10.0);
|
|
|
|
// With a heavy weight, the point at 1,2 pulls the line nearer.
|
|
|
|
ExpectCorrectLine(llsq, 0.0, 1.786, 0.41, 0.0, 1e-2);
|
2018-09-29 15:19:13 +08:00
|
|
|
ExpectCorrectVector(llsq, FCOORD(1.0f, 1.786f), FCOORD(1.0f, 0.0f), 1e-3);
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests a simple baseline-style normalization with a rotation.
|
|
|
|
TEST_F(LLSQTest, Vectors) {
|
|
|
|
LLSQ llsq;
|
|
|
|
llsq.add(1.0, 1.0);
|
|
|
|
llsq.add(1.0, -1.0);
|
2018-09-29 15:19:13 +08:00
|
|
|
ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-6);
|
2018-08-24 21:07:48 +08:00
|
|
|
llsq.add(0.9, -2.0);
|
|
|
|
llsq.add(1.1, -3.0);
|
|
|
|
llsq.add(0.9, 2.0);
|
|
|
|
llsq.add(1.10001, 3.0);
|
2018-09-29 15:19:13 +08:00
|
|
|
ExpectCorrectVector(llsq, FCOORD(1.0f, 0.0f), FCOORD(0.0f, 1.0f), 1e-3);
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that rms_orth() actually calculates:
|
|
|
|
// sqrt( sum (!nvec * (x_i - x_avg))^2 / n)
|
|
|
|
TEST_F(LLSQTest, RmsOrthWorksAsIntended) {
|
|
|
|
std::vector<FCOORD> pts;
|
2024-11-22 22:52:03 +08:00
|
|
|
pts.emplace_back(0.56f, 0.95f);
|
|
|
|
pts.emplace_back(0.09f, 0.09f);
|
|
|
|
pts.emplace_back(0.13f, 0.77f);
|
|
|
|
pts.emplace_back(0.16f, 0.83f);
|
|
|
|
pts.emplace_back(0.45f, 0.79f);
|
|
|
|
VerifyRmsOrth(pts, FCOORD(1.f, 0.f));
|
|
|
|
VerifyRmsOrth(pts, FCOORD(1.f, 1.f));
|
|
|
|
VerifyRmsOrth(pts, FCOORD(1.f, 2.f));
|
|
|
|
VerifyRmsOrth(pts, FCOORD(2.f, 1.f));
|
2018-08-24 21:07:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-13 05:06:34 +08:00
|
|
|
} // namespace tesseract
|