From cc77817e795e688df1bf271239ef5492c7196150 Mon Sep 17 00:00:00 2001 From: gaganjyot Date: Tue, 24 May 2016 01:34:50 +0530 Subject: [PATCH 1/2] Old Code Cleanup, Moved -> Move, Updated UnitTests --- lckernel/CMakeLists.txt | 2 - lckernel/cad/geometry/geoarc.h | 2 +- lckernel/cad/geometry/geocircle.h | 2 +- lckernel/cad/geometry/geoellipse.h | 2 +- lckernel/cad/math/equation.cpp | 12 +- lckernel/cad/math/equation.h | 6 +- lckernel/cad/math/lcmath.cpp | 151 +--------------- lckernel/cad/math/lcmath.h | 10 -- lckernel/cad/math/quadratic_math.cpp | 253 --------------------------- lckernel/cad/math/quadratic_math.h | 107 ----------- unittest/testmatrices.h | 108 ++++-------- 11 files changed, 50 insertions(+), 605 deletions(-) delete mode 100644 lckernel/cad/math/quadratic_math.cpp delete mode 100644 lckernel/cad/math/quadratic_math.h diff --git a/lckernel/CMakeLists.txt b/lckernel/CMakeLists.txt index fc79b6a48..7597a695a 100644 --- a/lckernel/CMakeLists.txt +++ b/lckernel/CMakeLists.txt @@ -26,7 +26,6 @@ cad/geometry/geoellipse.cpp cad/geometry/geospline.cpp cad/geometry/geobezier.cpp cad/math/lcmath.cpp -cad/math/quadratic_math.cpp cad/math/equation.cpp cad/math/intersectionhandler.cpp cad/meta/layer.cpp @@ -95,7 +94,6 @@ cad/interface/metatype.h cad/interface/snapable.h cad/interface/snapconstrain.h cad/math/lcmath.h -cad/math/quadratic_math.h cad/math/equation.h cad/math/intersectionhandler.h cad/meta/color.h diff --git a/lckernel/cad/geometry/geoarc.h b/lckernel/cad/geometry/geoarc.h index 73cdf967f..e5b3b41cf 100644 --- a/lckernel/cad/geometry/geoarc.h +++ b/lckernel/cad/geometry/geoarc.h @@ -110,7 +110,7 @@ namespace lc { Coordinate nearestPointOnEntity(const Coordinate& coord) const; const maths::Equation equation() const { - return maths::Equation(1., 0.,1., 0., 0., -_radius* _radius).moved(_center); + return maths::Equation(1., 0.,1., 0., 0., -_radius* _radius).move(_center); } diff --git a/lckernel/cad/geometry/geocircle.h b/lckernel/cad/geometry/geocircle.h index 67bc10533..e548354ab 100644 --- a/lckernel/cad/geometry/geocircle.h +++ b/lckernel/cad/geometry/geocircle.h @@ -33,7 +33,7 @@ namespace lc { Coordinate nearestPointOnEntity(const Coordinate& coord) const; const maths::Equation equation() const { - return maths::Equation(1., 0.,1., 0., 0., -_radius* _radius).moved(_center); + return maths::Equation(1., 0.,1., 0., 0., -_radius* _radius).move(_center); } virtual void accept(GeoEntityVisitor &v) const override { v.visit(*this); } diff --git a/lckernel/cad/geometry/geoellipse.h b/lckernel/cad/geometry/geoellipse.h index fd327982c..805825e6e 100644 --- a/lckernel/cad/geometry/geoellipse.h +++ b/lckernel/cad/geometry/geoellipse.h @@ -109,7 +109,7 @@ namespace lc { return maths::Equation(); } - return maths::Equation(1. / ce0, 0., 1. / ce2, 0., 0., -1.).rotated(getAngle()).moved(_center); + return maths::Equation(1. / ce0, 0., 1. / ce2, 0., 0., -1.).rotate(getAngle()).move(_center); } virtual void accept(GeoEntityVisitor &v) const override { v.visit(*this); } diff --git a/lckernel/cad/math/equation.cpp b/lckernel/cad/math/equation.cpp index f47eca1ea..5b9e9e4a4 100644 --- a/lckernel/cad/math/equation.cpp +++ b/lckernel/cad/math/equation.cpp @@ -45,24 +45,24 @@ const std::vector Equation::Coefficients() const { return vec; } -const Equation Equation::moved ( +const Equation Equation::move ( const geo::Coordinate &v) const { Eigen::Matrix3d mat = translateMatrix(v).transpose() * matrix_ * translateMatrix(v); return Equation(mat); } -const Equation Equation::rotated(double angle) const { +const Equation Equation::rotate(double angle) const { const auto & m = rotationMatrix(angle); const auto & t = m.transpose(); Eigen::Matrix3d ret = t * matrix_ * m; return Equation(ret); } -const Equation Equation::rotated( +const Equation Equation::rotate( const geo::Coordinate ¢er, double angle) const { - return moved(geo::Coordinate(-center.x(), -center.y())) - .rotated(angle) - .moved(center); + return move(geo::Coordinate(-center.x(), -center.y())) + .rotate(angle) + .move(center); } Eigen::Matrix3d Equation::rotationMatrix(double angle) { diff --git a/lckernel/cad/math/equation.h b/lckernel/cad/math/equation.h index 7932d95a6..62b2e93b5 100644 --- a/lckernel/cad/math/equation.h +++ b/lckernel/cad/math/equation.h @@ -26,11 +26,11 @@ namespace lc { const std::vector Coefficients() const; - const Equation moved(const geo::Coordinate &v) const ; + const Equation move(const geo::Coordinate &v) const ; - const Equation rotated(double angle) const; + const Equation rotate(double angle) const; - const Equation rotated(const geo::Coordinate ¢er, + const Equation rotate(const geo::Coordinate ¢er, double angle) const; const Eigen::Matrix3d Matrix() const; diff --git a/lckernel/cad/math/lcmath.cpp b/lckernel/cad/math/lcmath.cpp index 71be11121..2823d6bfc 100644 --- a/lckernel/cad/math/lcmath.cpp +++ b/lckernel/cad/math/lcmath.cpp @@ -2,7 +2,7 @@ #include "lcmath.h" #include "cad/const.h" - +#include using namespace lc; using namespace geo; @@ -390,137 +390,6 @@ std::vector Math::quarticSolverFull(const std::vector& ce) { return roots; } -//linear Equation solver by Gauss-Jordan -/** - * Solve linear equation set - *@ mt holds the augmented matrix - *@ sn holds the solution - *@ return true, if the equation set has a unique solution, return false otherwise - * - *@Author: Dongxu Li - */ - -bool Math::linearSolver(const std::vector >& mt, std::vector& sn) { - //verify the matrix size - int mSize(mt.size()); //rows - uint aSize(mSize + 1); //columns of augmented matrix - - for (int i = 0; i < mSize; i++) { - if (mt[i].size() != aSize) { - return false; - } - } - - sn.resize(mSize);//to hold the solution - //#ifdef HAS_BOOST -#if false - boost::numeric::ublas::matrix bm(mSize, mSize); - boost::numeric::ublas::vector bs(mSize); - - for (int i = 0; i < mSize; i++) { - for (int j = 0; j < mSize; j++) { - bm(i, j) = mt[i][j]; - } - - bs(i) = mt[i][mSize]; - } - - //solve the linear equation set by LU decomposition in boost ublas - - if (boost::numeric::ublas::lu_factorize >(bm)) { - std::cout << __FILE__ << " : " << __FUNCTION__ << " : line " << __LINE__ << std::endl; - std::cout << " linear solver failed" << std::endl; - // RS_DEBUG->print(RS_Debug::D_WARNING, "linear solver failed"); - return false; - } - - boost::numeric::ublas:: triangular_matrix - lm = boost::numeric::ublas::triangular_adaptor< boost::numeric::ublas::matrix, boost::numeric::ublas::unit_lower>(bm); - boost::numeric::ublas:: triangular_matrix - um = boost::numeric::ublas::triangular_adaptor< boost::numeric::ublas::matrix, boost::numeric::ublas::upper>(bm); - ; - boost::numeric::ublas::inplace_solve(lm, bs, boost::numeric::ublas::lower_tag()); - boost::numeric::ublas::inplace_solve(um, bs, boost::numeric::ublas::upper_tag()); - - for (int i = 0; i < mSize; i++) { - sn[i] = bs(i); - } - - // std::cout<<"dn="< > mt0(mt); //copy the matrix; - - for (int i = 0; i < mSize; i++) { - int imax(i); - double cmax(std::abs(mt0[i][i])); - - for (int j = i + 1; j < mSize; j++) { - if (std::abs(mt0[j][i]) > cmax) { - imax = j; - cmax = std::abs(mt0[j][i]); - } - } - - if (cmax < TOLERANCE2) { - return false; //singular matrix - } - - if (imax != i) { //move the line with largest absolute value at column i to row i, to avoid division by zero - std::swap(mt0[i], mt0[imax]); - // for(int j=i;j<=mSize;j++) { - // std::swap(m[i][j],m[imax][j]); - // } - } - - // for(int k=i+1;k<5;k++) { //normalize the i-th row - for (int k = mSize; k >= i; k--) { //normalize the i-th row - mt0[i][k] /= mt0[i][i]; - } - - for (int j = 0; j < mSize; j++) { //Gauss-Jordan - if (j != i) { - // for(int k=i+1;k<5;k++) { - for (int k = mSize; k >= i; k--) { - mt0[j][k] -= mt0[i][k] * mt0[j][i]; - } - } - } - - //output gauss-jordan results for debugging - // std::cout<<"========"< Math::simultaneousQuadraticSolverMixed(const st if (p1->size() == 3) { //linear - std::vector sn(2, 0.); - std::vector > ce; - ce.push_back(std::vector(m[0])); - ce.push_back(std::vector(m[1])); - ce[0][2] = -ce[0][2]; - ce[1][2] = -ce[1][2]; - - if (Math::linearSolver(ce, sn)) { - ret.push_back(geo::Coordinate(sn[0], sn[1])); - } - + Eigen::Matrix2d M; + Eigen::Vector2d V; + M << m[0][0], m[0][1], + m[1][0], m[1][1]; + V << -m[0][2], -m[1][2]; + Eigen::Vector2d sn = M.colPivHouseholderQr().solve(V); + ret.push_back(geo::Coordinate(sn[0], sn[1])); return ret; } diff --git a/lckernel/cad/math/lcmath.h b/lckernel/cad/math/lcmath.h index aa68b654a..ead96270b 100644 --- a/lckernel/cad/math/lcmath.h +++ b/lckernel/cad/math/lcmath.h @@ -67,16 +67,6 @@ namespace lc { **/ static std::vector quarticSolverFull(const std::vector& ce); - //solver for linear equation set - /** - * Solve linear equation set - *@ mt holds the augmented matrix - *@ sn holds the solution - *@ return true, if the equation set has a unique solution, return false otherwise - * - *@Author: Dongxu Li - */ - static bool linearSolver(const std::vector>& m, std::vector& sn); /** solver quadratic simultaneous equations of a set of two **/ /* solve the following quadratic simultaneous equations, diff --git a/lckernel/cad/math/quadratic_math.cpp b/lckernel/cad/math/quadratic_math.cpp deleted file mode 100644 index 1a59cecf5..000000000 --- a/lckernel/cad/math/quadratic_math.cpp +++ /dev/null @@ -1,253 +0,0 @@ -#include "quadratic_math.h" - -using namespace lc; - -Quadratic::Quadratic(): - m_mQuad(2, 2), - m_vLinear(2), - m_bValid(false) {} - -Quadratic::Quadratic(const Quadratic& lc0): - m_bIsQuadratic(lc0.isQuadratic()) - , m_bValid(lc0.isValid()) { - if (m_bValid == false) { - return; - } - - if (m_bIsQuadratic) { - m_mQuad = lc0.getQuad(); - } - - m_vLinear = lc0.getLinear(); - m_dConst = lc0.m_dConst; -} - -Quadratic& Quadratic::operator = (const Quadratic& lc0) { - if (lc0.isQuadratic()) { - m_mQuad.resize(2, 2, false); - m_mQuad = lc0.getQuad(); - } - - m_vLinear.resize(2); - m_vLinear = lc0.getLinear(); - m_dConst = lc0.m_dConst; - m_bIsQuadratic = lc0.isQuadratic(); - m_bValid = lc0.isValid(); - return *this; -} - - -Quadratic::Quadratic(std::vector ce): - m_mQuad(2, 2), - m_vLinear(2) { - if (ce.size() == 6) { - //quadratic - m_mQuad(0, 0) = ce[0]; - m_mQuad(0, 1) = 0.5 * ce[1]; - m_mQuad(1, 0) = m_mQuad(0, 1); - m_mQuad(1, 1) = ce[2]; - m_vLinear(0) = ce[3]; - m_vLinear(1) = ce[4]; - m_dConst = ce[5]; - m_bIsQuadratic = true; - m_bValid = true; - return; - } - - if (ce.size() == 3) { - m_vLinear(0) = ce[0]; - m_vLinear(1) = ce[1]; - m_dConst = ce[2]; - m_bIsQuadratic = false; - m_bValid = true; - return; - } - - m_bValid = false; -} - -Quadratic::Quadratic(double c0, double c1, double c2,double c3, double c4, double c5): - m_mQuad(2, 2), - m_vLinear(2) { - m_mQuad(0, 0) = c0; - m_mQuad(0, 1) = 0.5 * c1; - m_mQuad(1, 0) = m_mQuad(0, 1); - m_mQuad(1, 1) = c2; - m_vLinear(0) = c3; - m_vLinear(1) = c4; - m_dConst = c5; - m_bIsQuadratic = true; - m_bValid = true; -} - -Quadratic::Quadratic(double c0, double c1, double c2): - m_mQuad(2, 2), - m_vLinear(2) { - m_vLinear(0) =c0; - m_vLinear(1) =c1; - m_dConst = c2; - m_bIsQuadratic = false; - m_bValid = true; -} - -std::vector Quadratic::getCoefficients() const { - std::vector ret(0, 0.); - - if (isValid() == false) { - return ret; - } - - if (m_bIsQuadratic) { - ret.emplace_back(m_mQuad(0, 0)); - ret.emplace_back(m_mQuad(0, 1) + m_mQuad(1, 0)); - ret.emplace_back(m_mQuad(1, 1)); - } - - ret.emplace_back(m_vLinear(0)); - ret.emplace_back(m_vLinear(1)); - ret.emplace_back(m_dConst); - return ret; -} - -Quadratic Quadratic::move(const geo::Coordinate& v) { - if (m_bValid == false) { - return *this; - } - - m_dConst -= m_vLinear(0) * v.x() + m_vLinear(1) * v.y(); - - if (m_bIsQuadratic) { - m_vLinear(0) -= 2.*m_mQuad(0, 0) * v.x() + (m_mQuad(0, 1) + m_mQuad(1, 0)) * v.y(); - m_vLinear(1) -= 2.*m_mQuad(1, 1) * v.y() + (m_mQuad(0, 1) + m_mQuad(1, 0)) * v.x(); - m_dConst += m_mQuad(0, 0) * v.x() * v.x() + (m_mQuad(0, 1) + m_mQuad(1, 0)) * v.x() * v.y() + m_mQuad(1, 1) * v.y() * v.y(); - } - - return *this; -} - - -Quadratic Quadratic::rotate(const double& angle) { - using namespace boost::numeric::ublas; - auto&& m = rotationMatrix(angle); - auto&& t = trans(m); - m_vLinear = prod(t, m_vLinear); - - if (m_bIsQuadratic) { - m_mQuad = prod(m_mQuad, m); - m_mQuad = prod(t, m_mQuad); - } - - return *this; -} - -Quadratic Quadratic::rotate(const geo::Coordinate& center, const double& angle) { - move(geo::Coordinate(-center.x(), -center.y())); - rotate(angle); - move(center); - return *this; -} - -/** switch x,y coordinates */ -Quadratic Quadratic::flipXY(void) const { - Quadratic qf(*this); - - if (isQuadratic()) { - std::swap(qf.m_mQuad(0, 0), qf.m_mQuad(1, 1)); - std::swap(qf.m_mQuad(0, 1), qf.m_mQuad(1, 0)); - } - - std::swap(qf.m_vLinear(0), qf.m_vLinear(1)); - return qf; -} - -/*** - * (~_|_ _ _|_. _ |~ _ __|_. _ _ _ - * _) | (_| | |(_ |~|_|| |(_ | |(_)| |_\ - * - */ -boost::numeric::ublas::matrix Quadratic::rotationMatrix(const double& angle) { - boost::numeric::ublas::matrix ret(2, 2); - ret(0, 0) = std::cos(angle); - ret(0, 1) = std::sin(angle); - ret(1, 0) = -ret(0, 1); - ret(1, 1) = ret(0, 0); - return ret; -} - -std::vector Quadratic::getIntersection(const Quadratic& l1, const Quadratic& l2) { - std::vector ret; - - if (l1.isValid() == false || l2.isValid() == false) { - // DEBUG_HEADER(); - // std::cout<isQuadratic() == false) { - std::swap(p1, p2); - } - - // DEBUG_HEADER(); - // std::cout<<*p1<isQuadratic() == false) { - //two lines - std::vector > ce(2, std::vector(3, 0.)); - ce[0][0] = p1->m_vLinear(0); - ce[0][1] = p1->m_vLinear(1); - ce[0][2] = -p1->m_dConst; - ce[1][0] = p2->m_vLinear(0); - ce[1][1] = p2->m_vLinear(1); - ce[1][2] = -p2->m_dConst; - std::vector sn(2, 0.); - - if (Math::linearSolver(ce, sn)) { - ret.emplace_back(sn[0], sn[1]); - } - - return ret; - } - - if (p2->isQuadratic() == false) { - //one line, one quadratic - //avoid division by zero - if (std::abs(p2->m_vLinear(0)) < std::abs(p2->m_vLinear(1))) { - auto &&tcoords = getIntersection(p1->flipXY(), p2->flipXY()); - std::transform(tcoords.begin(), tcoords.end(), tcoords.begin(), [](const lc::geo::Coordinate &c) { return std::move(c.flipXY()); }); - return tcoords; - } - - } - - if (std::abs(p1->m_mQuad(0, 0)) < LCTOLERANCE && std::abs(p1->m_mQuad(0, 1)) < LCTOLERANCE - && - std::abs(p2->m_mQuad(0, 0)) < LCTOLERANCE && std::abs(p2->m_mQuad(0, 1)) < LCTOLERANCE - ) { - if (std::abs(p1->m_mQuad(1, 1)) < LCTOLERANCE && std::abs(p2->m_mQuad(1, 1)) < LCTOLERANCE) { - //linear - Quadratic lc10(p1->m_vLinear(0), p1->m_vLinear(1), p1->m_dConst); - Quadratic lc11(p2->m_vLinear(0), p2->m_vLinear(1), p2->m_dConst); - return getIntersection(lc10, lc11); - } - - auto &&tcoords = getIntersection(p1->flipXY(), p2->flipXY()); - std::transform(tcoords.begin(), tcoords.end(), tcoords.begin(), [](const lc::geo::Coordinate &c) { return std::move(c.flipXY()); }); - return tcoords; - } - - std::vector > ce(0); - ce.push_back(p1->getCoefficients()); - ce.push_back(p2->getCoefficients()); - // if(RS_DEBUG->getLevel()>=RS_Debug::D_INFORMATIONAL){ - // DEBUG_HEADER(); - // std::cout<<*p1< -#include -#include "lcmath.h" -#include "cad/const.h" -#include -namespace lc { - class Quadratic { - public: - explicit Quadratic(); - - /** - * @brief Quadratic, quadratic equation constructor - * @param lc0, equation - */ - Quadratic(const Quadratic& lc0); - Quadratic& operator = (const Quadratic& lc0); - Quadratic(std::vector ce); - Quadratic(double c0, double c1, double c2); - Quadratic(double c0, double c1, double c2, double c3, double c4, double c5); - std::vector getCoefficients() const; - - /** - * @brief move, moves at some offset - * @param geo::Coordinate v, offset - * @return Quadratic moved quadratic - */ - Quadratic move(const geo::Coordinate& v); - - /** - * @brief rotate, rotates the quadratic - * @param double a, angle - * @return rotated quadratic - */ - Quadratic rotate(const double& a); - Quadratic rotate(const geo::Coordinate& center, const double& a); - - /** - * @brief isQuadratic, quadratic check - * @return bool - */ - bool isQuadratic() const { - return m_bIsQuadratic; - } - - /** - * @brief isValid, validity check - * @return bool - */ - bool isValid() const { - return m_bValid; - } - void setValid(bool value) { - m_bValid = value; - } - - /** - * @brief getLinear, Returns linear equation - * @return boost::numeric::ublas::vector linear - */ - boost::numeric::ublas::vector& getLinear() { - return m_vLinear; - } - const boost::numeric::ublas::vector& getLinear() const { - return m_vLinear; - } - - /** - * @brief getQuad, Returns Quadratic equation - * @return boost::numeric::ublas::matrix - */ - boost::numeric::ublas::matrix& getQuad() { - return m_mQuad; - } - const boost::numeric::ublas::matrix& getQuad() const { - return m_mQuad; - } - - /** switch x,y coordinates */ - Quadratic flipXY(void) const; - /** the matrix of rotation by angle **/ - static boost::numeric::ublas::matrix rotationMatrix(const double& angle); - - /** - * @brief getIntersection, returns intersection in two entites - * @param l1, quadratic 1 - * @param l2, quadratic 2 - * @return std::vector coordinates - */ - static std::vector getIntersection(const Quadratic& l1, const Quadratic& l2); - private: - // Not 100% sure we might want to consider http://sourceforge.net/projects/tvmet/ because ublas from boost - // is relative slow on small matrixes/vector. see faq on - // http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl?Frequently_Asked_Questions_Using_UBLAS - boost::numeric::ublas::matrix m_mQuad; - boost::numeric::ublas::vector m_vLinear; - double m_dConst; - bool m_bIsQuadratic; - /** whether this quadratic form is valid */ - bool m_bValid; - - - }; -} -# diff --git a/unittest/testmatrices.h b/unittest/testmatrices.h index bc8038add..d528ddd93 100644 --- a/unittest/testmatrices.h +++ b/unittest/testmatrices.h @@ -8,32 +8,24 @@ using namespace std; TEST(Matrix, Move) { - lc::Quadratic qs(1,2,3,4,5,6); - lc::maths::Equation qm(1,2,3,4,5,6); + auto x = lc::maths::Equation(1,2,3,4,5,6).move(lc::geo::Coordinate(5,5)).Coefficients(); - qs.move(lc::geo::Coordinate(5,5)); - auto ret = qm.moved(lc::geo::Coordinate(5,5)); - auto x = qs.getCoefficients(); - auto y = ret.Coefficients(); + std::vector y{ 1, 2, 3, -16, -35, 111 }; ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length"; for (int i = 0; i < x.size(); ++i) { - EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i; + EXPECT_EQ(y[i], x[i]) << "Vectors x and y differ at index " << i; } } TEST(Matrix, Rotate) { - lc::Quadratic qs(1,2,3,4,5,6); - lc::maths::Equation qm(1,2,3,4,5,6); + auto x = lc::maths::Equation(1,2,3,4,5,6).rotate(20).Coefficients(); + std::vector y{1.92182, -2.8241, 2.07818, -2.9324, 5.69219, 6}; - qs.rotate(20); - auto ret = qm.rotated(20); - auto x = qs.getCoefficients(); - auto y = ret.Coefficients(); ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length"; for (int i = 0; i < x.size(); ++i) { - EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i; + ASSERT_NEAR(x[i], y[i], 1e-5) << "Vectors x and y differ at index " << i; } } @@ -41,94 +33,54 @@ TEST(QM, QUADQUAD) { auto _r1 = 5; auto _r2 = 5; auto _c1 = lc::geo::Coordinate(0.,0.); - auto _c2 = lc::geo::Coordinate(2.,2.); + auto _c2 = lc::geo::Coordinate(6.,0.); lc::maths::Equation ret(1.,0.,1.,0,.0,-_r1* _r1); - auto qm1 = ret.moved(_c1); + auto qm1 = ret.move(_c1); lc::maths::Equation ret2(1.,0.,1.,0,.0,-_r2 * _r2); - auto qm2 = ret2.moved(_c2); + auto qm2 = ret2.move(_c2); - lc::Quadratic qr(1.,0.,1.,0,.0,-_r1* _r1); - qr.move(_c1); - lc::Quadratic qr2(1.,0.,1.,0,.0,-_r2 * _r2); - qr2.move(_c2); + std::vector x = lc::maths::Intersection::QuadQuad(qm1, qm2); - std::vector x = lc::Quadratic::getIntersection(qr, qr2); - std::vector y = lc::maths::Intersection::QuadQuad(qm1, qm2); - - ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length"; - - for (int i = 0; i < x.size(); ++i) { - EXPECT_DOUBLE_EQ(x[i].x(), y[i].x()) << "X differs at index " << i; - EXPECT_DOUBLE_EQ(x[i].y(), y[i].y()) << "Y differs at index " << i; - } + EXPECT_DOUBLE_EQ(x[0].x(), 3) << "X differs"; + EXPECT_DOUBLE_EQ(x[0].y(), 4) << "Y differs"; + EXPECT_DOUBLE_EQ(x[1].x(), 3) << "X differs"; + EXPECT_DOUBLE_EQ(x[1].y(),-4) << "Y differs"; } TEST(QM, LineLine) { - lc::geo::Vector _l1 = lc::geo::Vector(lc::geo::Coordinate(-5,-5), lc::geo::Coordinate(5,5)); + lc::geo::Vector _l1 = lc::geo::Vector(lc::geo::Coordinate(0,0), lc::geo::Coordinate(5,5)); lc::geo::Vector _l2 = lc::geo::Vector(lc::geo::Coordinate(5,0), lc::geo::Coordinate(0,5)); + auto eq1 = _l1.equation(); + auto eq2 = _l2.equation(); - auto&& dvp1 = _l1.end() - _l1.start(); - lc::geo::Coordinate normal1(-dvp1.y(), dvp1.x()); - lc::maths::Equation ret1(0,0,0,normal1.x(),normal1.y(),- normal1.dot(_l1.end())); - - auto&& dvp2 = _l2.end() - _l2.start(); - lc::geo::Coordinate normal2(-dvp2.y(), dvp2.x()); - lc::maths::Equation ret2(0,0,0,normal2.x(),normal2.y(),- normal2.dot(_l2.end())); - - auto ql1 = lc::Quadratic(normal1.x(), normal1.y(), -normal1.dot(_l1.end())); - auto ql2 = lc::Quadratic(normal2.x(), normal2.y(), -normal2.dot(_l2.end())); - - std::vector x = lc::Quadratic::getIntersection(ql1, ql2); - std::vector y = lc::maths::Intersection::LineLine(ret1, ret2); - - ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length"; - - for (int i = 0; i < x.size(); ++i) { - EXPECT_DOUBLE_EQ(x[i].x(), y[i].x()) << "X differs at index " << i; - EXPECT_DOUBLE_EQ(x[i].y(), y[i].y()) << "Y differs at index " << i; - } + std::vector x = lc::maths::Intersection::LineLine(eq1, eq2); + EXPECT_DOUBLE_EQ(x[0].x(), 2.5) << "X differs"; + EXPECT_DOUBLE_EQ(x[0].y(), 2.5) << "Y differs"; } TEST(QM, LineQuad) { - lc::geo::Vector _l1 = lc::geo::Vector(lc::geo::Coordinate(0,0), lc::geo::Coordinate(15,15)); - - auto&& dvp1 = _l1.end() - _l1.start(); - lc::geo::Coordinate normal1(-dvp1.y(), dvp1.x()); + lc::geo::Vector _l1 = lc::geo::Vector(lc::geo::Coordinate(0,0), lc::geo::Coordinate(0,10)); + auto circle = lc::geo::Circle(lc::geo::Coordinate(0,0), 10); auto lqm = _l1.equation(); - auto lq = lc::Quadratic(normal1.x(), normal1.y(), -normal1.dot(_l1.end())); - - auto _r1 = 200; - auto _c1 = lc::geo::Coordinate(0.,0.); - - auto circle = lc::geo::Circle(lc::geo::Coordinate(0,0), 200); auto cqm = circle.equation(); - lc::Quadratic cq(1.,0.,1.,0,.0,-_r1* _r1); - cq.move(_c1); - std::vector x = lc::Quadratic::getIntersection(lq, cq); - std::vector y = lc::maths::Intersection::LineQuad(cqm, lqm); - - ASSERT_EQ(x.size(), y.size()) << "Vectors x and y are of unequal length"; - - for (int i = 0; i < x.size(); ++i) { - EXPECT_DOUBLE_EQ(x[i].x(), y[i].x()) << "X differs at index " << i; - EXPECT_DOUBLE_EQ(x[i].y(), y[i].y()) << "Y differs at index " << i; - } + std::vector x = lc::maths::Intersection::LineQuad(cqm, lqm); + EXPECT_DOUBLE_EQ(x[0].x(), 0) << "X differs"; + EXPECT_DOUBLE_EQ(x[0].y(), 10) << "Y differs"; + EXPECT_DOUBLE_EQ(x[1].x(), 0) << "X differs"; + EXPECT_DOUBLE_EQ(x[1].y(),-10) << "Y differs"; } TEST(Maths, LinearSolver) { - std::vector> a {{1,-1, -1}, {3,1, 9}}; - std::vector res1; Eigen::Matrix2d M; Eigen::Vector2d V; M << 1, -1, 3, 1; V << -1, 9; - lc::Math::linearSolver(a, res1); - Eigen::Vector2d res2 = M.colPivHouseholderQr().solve(V); + Eigen::Vector2d res = M.colPivHouseholderQr().solve(V); - EXPECT_DOUBLE_EQ(res1[0], res2[0]) << "X differs"; - EXPECT_DOUBLE_EQ(res1[1], res2[1]) << "Y differs"; + EXPECT_DOUBLE_EQ(res[0], 2) << "X differs"; + EXPECT_DOUBLE_EQ(res[1], 3) << "Y differs"; } From 65736ca337137570016ece0195565adb8f48d2d6 Mon Sep 17 00:00:00 2001 From: gaganjyot Date: Wed, 25 May 2016 11:32:55 +0530 Subject: [PATCH 2/2] Minor fix --- unittest/code.h | 1 - unittest/testmatrices.h | 1 - 2 files changed, 2 deletions(-) diff --git a/unittest/code.h b/unittest/code.h index d0c165d12..266f83212 100644 --- a/unittest/code.h +++ b/unittest/code.h @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include #include diff --git a/unittest/testmatrices.h b/unittest/testmatrices.h index d528ddd93..27dc01f77 100644 --- a/unittest/testmatrices.h +++ b/unittest/testmatrices.h @@ -1,5 +1,4 @@ #include "cad/math/equation.h" -#include "cad/math/quadratic_math.h" #include "cad/math/intersectionhandler.h" #include "cad/geometry/geovector.h" #include "cad/math/lcmath.h"