From d3615f7e541997bc5465c4a3728a119229108dd0 Mon Sep 17 00:00:00 2001 From: Markus Lampert Date: Tue, 13 Oct 2020 20:34:03 -0700 Subject: [PATCH] Added equal/not equal comparison to voronoi python objects with unit tests. --- src/Mod/Path/App/VoronoiCellPyImp.cpp | 6 +- src/Mod/Path/App/VoronoiEdgePyImp.cpp | 10 +-- src/Mod/Path/App/VoronoiVertexPyImp.cpp | 6 +- src/Mod/Path/PathTests/TestPathVoronoi.py | 89 +++++++++++++++++++++++ src/Mod/Path/TestPathApp.py | 2 + 5 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 src/Mod/Path/PathTests/TestPathVoronoi.py diff --git a/src/Mod/Path/App/VoronoiCellPyImp.cpp b/src/Mod/Path/App/VoronoiCellPyImp.cpp index 113f248c0b..9bae03a385 100644 --- a/src/Mod/Path/App/VoronoiCellPyImp.cpp +++ b/src/Mod/Path/App/VoronoiCellPyImp.cpp @@ -75,14 +75,14 @@ int VoronoiCellPy::PyInit(PyObject* args, PyObject* /*kwd*/) PyObject* VoronoiCellPy::richCompare(PyObject *lhs, PyObject *rhs, int op) { - PyObject *cmp = Py_False; + PyObject *cmp = (op == Py_EQ) ? Py_False : Py_True; if ( PyObject_TypeCheck(lhs, &VoronoiCellPy::Type) && PyObject_TypeCheck(rhs, &VoronoiCellPy::Type) - && op == Py_EQ) { + && (op == Py_EQ || op == Py_NE)) { const VoronoiCell *vl = static_cast(lhs)->getVoronoiCellPtr(); const VoronoiCell *vr = static_cast(rhs)->getVoronoiCellPtr(); if (vl->index == vr->index && vl->dia == vr->dia) { - cmp = Py_True; + cmp = (op == Py_EQ) ? Py_True : Py_False; } } Py_INCREF(cmp); diff --git a/src/Mod/Path/App/VoronoiEdgePyImp.cpp b/src/Mod/Path/App/VoronoiEdgePyImp.cpp index 451eca0220..8da24deab6 100644 --- a/src/Mod/Path/App/VoronoiEdgePyImp.cpp +++ b/src/Mod/Path/App/VoronoiEdgePyImp.cpp @@ -92,16 +92,14 @@ int VoronoiEdgePy::PyInit(PyObject* args, PyObject* /*kwd*/) PyObject* VoronoiEdgePy::richCompare(PyObject *lhs, PyObject *rhs, int op) { - PyObject *cmp = Py_False; + PyObject *cmp = (op == Py_EQ) ? Py_False : Py_True; if ( PyObject_TypeCheck(lhs, &VoronoiEdgePy::Type) && PyObject_TypeCheck(rhs, &VoronoiEdgePy::Type) - && op == Py_EQ) { + && (op == Py_EQ || op == Py_NE)) { const VoronoiEdge *vl = static_cast(lhs)->getVoronoiEdgePtr(); const VoronoiEdge *vr = static_cast(rhs)->getVoronoiEdgePtr(); - if (vl->index == vr->index && vl->dia == vr->dia) { - cmp = Py_True; - } else { - std::cerr << "VoronoiEdge==(" << vl->index << " != " << vr->index << " || " << (vl->dia == vr->dia) << ")" << std::endl; + if (vl->dia == vr->dia && vl->index == vr->index) { + cmp = (op == Py_EQ) ? Py_True : Py_False; } } Py_INCREF(cmp); diff --git a/src/Mod/Path/App/VoronoiVertexPyImp.cpp b/src/Mod/Path/App/VoronoiVertexPyImp.cpp index 45e74a04b2..02b1c393d1 100644 --- a/src/Mod/Path/App/VoronoiVertexPyImp.cpp +++ b/src/Mod/Path/App/VoronoiVertexPyImp.cpp @@ -76,14 +76,14 @@ int VoronoiVertexPy::PyInit(PyObject* args, PyObject* /*kwd*/) PyObject* VoronoiVertexPy::richCompare(PyObject *lhs, PyObject *rhs, int op) { - PyObject *cmp = Py_False; + PyObject *cmp = (op == Py_EQ) ? Py_False : Py_True; if ( PyObject_TypeCheck(lhs, &VoronoiVertexPy::Type) && PyObject_TypeCheck(rhs, &VoronoiVertexPy::Type) - && op == Py_EQ) { + && (op == Py_EQ || op == Py_NE)) { const VoronoiVertex *vl = static_cast(lhs)->getVoronoiVertexPtr(); const VoronoiVertex *vr = static_cast(rhs)->getVoronoiVertexPtr(); if (vl->index == vr->index && vl->dia == vr->dia) { - cmp = Py_True; + cmp = (op == Py_EQ) ? Py_True : Py_False; } } Py_INCREF(cmp); diff --git a/src/Mod/Path/PathTests/TestPathVoronoi.py b/src/Mod/Path/PathTests/TestPathVoronoi.py new file mode 100644 index 0000000000..a06c8437c1 --- /dev/null +++ b/src/Mod/Path/PathTests/TestPathVoronoi.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- + +# *************************************************************************** +# * * +# * Copyright (c) 2020 sliptonic * +# * * +# * This program is free software; you can redistribute it and/or modify * +# * it under the terms of the GNU Lesser General Public License (LGPL) * +# * as published by the Free Software Foundation; either version 2 of * +# * the License, or (at your option) any later version. * +# * for detail see the LICENCE text file. * +# * * +# * This program is distributed in the hope that it will be useful, * +# * but WITHOUT ANY WARRANTY; without even the implied warranty of * +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +# * GNU Library General Public License for more details. * +# * * +# * You should have received a copy of the GNU Library General Public * +# * License along with this program; if not, write to the Free Software * +# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +# * USA * +# * * +# *************************************************************************** + +import FreeCAD +import Path +import PathTests.PathTestUtils as PathTestUtils + +vd = None + +def initVD(): + global vd + if vd is None: + pts = [(0,0), (3.5,0), (3.5,1), (1,1), (1,2), (2.5,2), (2.5,3), (1,3), (1,4), (3.5, 4), (3.5,5), (0,5)] + ptv = [FreeCAD.Vector(p[0], p[1]) for p in pts] + ptv.append(ptv[0]) + + vd = Path.Voronoi() + for i in range(len(pts)): + vd.addSegment(ptv[i], ptv[i+1]) + + vd.construct() + + for e in vd.Edges: + e.Color = 0 if e.isPrimary() else 1; + vd.colorExterior(2) + vd.colorColinear(3) + vd.colorTwins(4) + + +class TestPathVoronoi(PathTestUtils.PathTestBase): + + def setUp(self): + initVD() + + def test00(self): + '''Check vertex comparison''' + + self.assertTrue( vd.Vertices[0] == vd.Vertices[0]) + self.assertTrue( vd.Vertices[1] == vd.Vertices[1]) + self.assertTrue( vd.Vertices[0] != vd.Vertices[1]) + self.assertEqual( vd.Vertices[0], vd.Vertices[0]) + self.assertEqual( vd.Vertices[1], vd.Vertices[1]) + self.assertNotEqual(vd.Vertices[0], vd.Vertices[1]) + self.assertNotEqual(vd.Vertices[1], vd.Vertices[0]) + + def test10(self): + '''Check edge comparison''' + + self.assertTrue( vd.Edges[0] == vd.Edges[0]) + self.assertTrue( vd.Edges[1] == vd.Edges[1]) + self.assertTrue( vd.Edges[0] != vd.Edges[1]) + self.assertEqual( vd.Edges[0], vd.Edges[0]) + self.assertEqual( vd.Edges[1], vd.Edges[1]) + self.assertNotEqual(vd.Edges[0], vd.Edges[1]) + self.assertNotEqual(vd.Edges[1], vd.Edges[0]) + + + def test20(self): + '''Check cell comparison''' + + self.assertTrue( vd.Cells[0] == vd.Cells[0]) + self.assertTrue( vd.Cells[1] == vd.Cells[1]) + self.assertTrue( vd.Cells[0] != vd.Cells[1]) + self.assertEqual( vd.Cells[0], vd.Cells[0]) + self.assertEqual( vd.Cells[1], vd.Cells[1]) + self.assertNotEqual(vd.Cells[0], vd.Cells[1]) + self.assertNotEqual(vd.Cells[1], vd.Cells[0]) + diff --git a/src/Mod/Path/TestPathApp.py b/src/Mod/Path/TestPathApp.py index b5ac4e215c..147cf9a169 100644 --- a/src/Mod/Path/TestPathApp.py +++ b/src/Mod/Path/TestPathApp.py @@ -42,6 +42,7 @@ from PathTests.TestPathToolController import TestPathToolController from PathTests.TestPathSetupSheet import TestPathSetupSheet from PathTests.TestPathDeburr import TestPathDeburr from PathTests.TestPathHelix import TestPathHelix +from PathTests.TestPathVoronoi import TestPathVoronoi # dummy usage to get flake8 and lgtm quiet False if TestApp.__name__ else True @@ -62,4 +63,5 @@ False if TestPathDeburr.__name__ else True False if TestPathHelix.__name__ else True False if TestPathPreferences.__name__ else True False if TestPathToolBit.__name__ else True +False if TestPathVoronoi.__name__ else True