From 181017b90d8153724b7ed75f76509f979d51fe7e Mon Sep 17 00:00:00 2001 From: ExMix Date: Tue, 6 Jan 2015 11:58:47 +0300 Subject: [drape] path text adaptation --- geometry/geometry_tests/spline_test.cpp | 52 +++++++------- geometry/spline.cpp | 121 ++++++++++++++++++++++---------- geometry/spline.hpp | 10 ++- 3 files changed, 118 insertions(+), 65 deletions(-) (limited to 'geometry') diff --git a/geometry/geometry_tests/spline_test.cpp b/geometry/geometry_tests/spline_test.cpp index 6f70967732..d901340583 100644 --- a/geometry/geometry_tests/spline_test.cpp +++ b/geometry/geometry_tests/spline_test.cpp @@ -28,11 +28,11 @@ UNIT_TEST(SmoothedDirections) PointD dir2(sqrt2 / 2.0, -sqrt2 / 2.0); itr.Attach(spl); TestPointDDir(itr.m_avrDir, dir1); - itr.Step(sqrt2 * 30.0); + itr.Advance(sqrt2 * 30.0); TestPointDDir(itr.m_avrDir, dir1); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_avrDir, dir1 * 0.25 + dir2 * 0.75); - itr.Step(sqrt2 * 10.0); + itr.Advance(sqrt2 * 10.0); TestPointDDir(itr.m_avrDir, dir2); path.clear(); @@ -46,12 +46,12 @@ UNIT_TEST(SmoothedDirections) Spline spl2(path); itr.Attach(spl2); TestPointDDir(itr.m_avrDir, dir1); - itr.Step(sqrt2 * 80.0 + 40.0); + itr.Advance(sqrt2 * 80.0 + 40.0); TestPointDDir(itr.m_avrDir, dir12); itr.Attach(spl2); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_avrDir, dir1); - itr.Step(80.0); + itr.Advance(80.0); TestPointDDir(itr.m_avrDir, dir12 * 0.5 + dir2 * 0.5); } @@ -69,9 +69,9 @@ UNIT_TEST(UsualDirections) PointD dir2(sqrt2 / 2.0, -sqrt2 / 2.0); itr.Attach(spl); TestPointDDir(itr.m_dir, dir1); - itr.Step(sqrt2 * 30.0); + itr.Advance(sqrt2 * 30.0); TestPointDDir(itr.m_dir, dir1); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_dir, dir2); path.clear(); @@ -85,12 +85,12 @@ UNIT_TEST(UsualDirections) Spline spl2(path); itr.Attach(spl2); TestPointDDir(itr.m_dir, dir1); - itr.Step(sqrt2 * 80.0 + 35.0); + itr.Advance(sqrt2 * 80.0 + 35.0); TestPointDDir(itr.m_dir, dir2); itr.Attach(spl2); - itr.Step(sqrt2 * 45.0); + itr.Advance(sqrt2 * 45.0); TestPointDDir(itr.m_dir, dir12); - itr.Step(80.0); + itr.Advance(80.0); TestPointDDir(itr.m_dir, dir2); } @@ -108,15 +108,15 @@ UNIT_TEST(Positions) Spline::iterator itr; itr.Attach(spl0); TestPointDDir(itr.m_pos, PointD(0, 0)); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_pos, PointD(40, 40)); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_pos, PointD(80, 0)); itr.Attach(spl4); TestPointDDir(itr.m_pos, PointD(0, 0)); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_pos, PointD(40, 40)); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_pos, PointD(80, 0)); path.clear(); @@ -130,16 +130,16 @@ UNIT_TEST(Positions) Spline spl3 = spl2; itr.Attach(spl3); TestPointDDir(itr.m_pos, PointD(0, 0)); - itr.Step(sqrt2 * 80.0 + 40.0); + itr.Advance(sqrt2 * 80.0 + 40.0); TestPointDDir(itr.m_pos, PointD(120, 0)); itr.Attach(spl2); - itr.Step(sqrt2 * 40.0); + itr.Advance(sqrt2 * 40.0); TestPointDDir(itr.m_pos, PointD(40, 40)); - itr.Step(2.0); + itr.Advance(2.0); TestPointDDir(itr.m_pos, PointD(42, 40)); - itr.Step(20.0); + itr.Advance(20.0); TestPointDDir(itr.m_pos, PointD(62, 40)); - itr.Step(18.0); + itr.Advance(18.0); TestPointDDir(itr.m_pos, PointD(80, 40)); } @@ -157,11 +157,11 @@ UNIT_TEST(BeginAgain) PointD dir2(sqrt2 / 2.0, -sqrt2 / 2.0); itr.Attach(spl); TEST_EQUAL(itr.BeginAgain(), false, ()); - itr.Step(90.0); + itr.Advance(90.0); TEST_EQUAL(itr.BeginAgain(), false, ()); - itr.Step(90.0); + itr.Advance(90.0); TEST_EQUAL(itr.BeginAgain(), true, ()); - itr.Step(190.0); + itr.Advance(190.0); TEST_EQUAL(itr.BeginAgain(), true, ()); path.clear(); @@ -174,11 +174,11 @@ UNIT_TEST(BeginAgain) Spline spl2(path); itr.Attach(spl2); TEST_EQUAL(itr.BeginAgain(), false, ()); - itr.Step(90.0); + itr.Advance(90.0); TEST_EQUAL(itr.BeginAgain(), false, ()); - itr.Step(90.0); + itr.Advance(90.0); TEST_EQUAL(itr.BeginAgain(), true, ()); - itr.Step(190.0); + itr.Advance(190.0); TEST_EQUAL(itr.BeginAgain(), true, ()); } diff --git a/geometry/spline.cpp b/geometry/spline.cpp index 1a2df1b5d3..1f2048f1a4 100644 --- a/geometry/spline.cpp +++ b/geometry/spline.cpp @@ -76,6 +76,34 @@ Spline::iterator::iterator() , m_index(0) , m_dist(0) {} +Spline::iterator::iterator(Spline::iterator const & other) +{ + m_checker = other.m_checker; + m_spl = other.m_spl; + m_index = other.m_index; + m_dist = other.m_dist; + + m_pos = other.m_pos; + m_dir = other.m_dir; + m_avrDir = other.m_avrDir; +} + +Spline::iterator & Spline::iterator::operator=(Spline::iterator const & other) +{ + if (this == &other) + return *this; + + m_checker = other.m_checker; + m_spl = other.m_spl; + m_index = other.m_index; + m_dist = other.m_dist; + + m_pos = other.m_pos; + m_dir = other.m_dir; + m_avrDir = other.m_avrDir; + return *this; +} + void Spline::iterator::Attach(Spline const & spl) { m_spl = &spl; @@ -87,44 +115,12 @@ void Spline::iterator::Attach(Spline const & spl) m_pos = m_spl->m_position[m_index] + m_dir * m_dist; } -void Spline::iterator::Step(double speed) -{ - m_dist += speed; - if (m_checker) - { - m_pos = m_spl->m_position[m_index] + m_dir * m_dist; - return; - } - while (m_dist > m_spl->m_length[m_index]) - { - m_dist -= m_spl->m_length[m_index]; - m_index++; - if (m_index >= m_spl->m_direction.size()) - { - m_index--; - m_dist += m_spl->m_length[m_index]; - m_checker = true; - break; - } - } - m_dir = m_spl->m_direction[m_index]; - m_avrDir = -m_pos; - m_pos = m_spl->m_position[m_index] + m_dir * m_dist; - m_avrDir += m_pos; -} - -void Spline::iterator::StepBack(double speed) +void Spline::iterator::Advance(double step) { - m_dist -= speed; - while(m_dist < 0.0f) - { - m_index--; - m_dist += m_spl->m_length[m_index]; - } - m_dir = m_spl->m_direction[m_index]; - m_avrDir = -m_pos; - m_pos = m_spl->m_position[m_index] + m_dir * m_dist; - m_avrDir += m_pos; + if (step < 0.0) + AdvanceBackward(step); + else + AdvanceForward(step); } double Spline::iterator::GetLength() const @@ -152,6 +148,57 @@ int Spline::iterator::GetIndex() const return m_index; } +void Spline::iterator::AdvanceBackward(double step) +{ + m_dist += step; + while(m_dist < 0.0f) + { + m_index--; + if (m_index < 0) + { + m_index = 0; + m_checker = true; + m_pos = m_spl->m_position[m_index]; + m_dir = m_spl->m_direction[m_index]; + m_avrDir = m2::PointD::Zero(); + m_dist = 0.0; + return; + } + + m_dist += m_spl->m_length[m_index]; + } + m_dir = m_spl->m_direction[m_index]; + m_avrDir = -m_pos; + m_pos = m_spl->m_position[m_index] + m_dir * m_dist; + m_avrDir += m_pos; +} + +void Spline::iterator::AdvanceForward(double step) +{ + m_dist += step; + if (m_checker) + { + m_pos = m_spl->m_position[m_index] + m_dir * m_dist; + return; + } + while (m_dist > m_spl->m_length[m_index]) + { + m_dist -= m_spl->m_length[m_index]; + m_index++; + if (m_index >= m_spl->m_direction.size()) + { + m_index--; + m_dist += m_spl->m_length[m_index]; + m_checker = true; + break; + } + } + m_dir = m_spl->m_direction[m_index]; + m_avrDir = -m_pos; + m_pos = m_spl->m_position[m_index] + m_dir * m_dist; + m_avrDir += m_pos; +} + SharedSpline::SharedSpline(vector const & path) { m_spline.reset(new Spline(path)); diff --git a/geometry/spline.hpp b/geometry/spline.hpp index d313968ae3..12c5c77e79 100644 --- a/geometry/spline.hpp +++ b/geometry/spline.hpp @@ -17,10 +17,13 @@ public: PointD m_pos; PointD m_dir; PointD m_avrDir; + iterator(); + iterator(iterator const & other); + iterator & operator=(iterator const & other); + void Attach(Spline const & spl); - void Step(double speed); - void StepBack(double speed); + void Advance(double step); bool BeginAgain() const; double GetLength() const; double GetFullLength() const; @@ -30,6 +33,9 @@ public: double GetDistance() const; int GetIndex() const; + void AdvanceForward(double step); + void AdvanceBackward(double step); + private: bool m_checker; Spline const * m_spl; -- cgit v1.2.3