Skip to content

Commit

Permalink
Added equal/not equal comparison to voronoi python objects with unit …
Browse files Browse the repository at this point in the history
…tests.
  • Loading branch information
mlampert committed Oct 25, 2020
1 parent 1f46b72 commit 8f45dee
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Mod/Path/App/VoronoiCellPyImp.cpp
Expand Up @@ -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<VoronoiCellPy*>(lhs)->getVoronoiCellPtr();
const VoronoiCell *vr = static_cast<VoronoiCellPy*>(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);
Expand Down
10 changes: 4 additions & 6 deletions src/Mod/Path/App/VoronoiEdgePyImp.cpp
Expand Up @@ -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<VoronoiEdgePy*>(lhs)->getVoronoiEdgePtr();
const VoronoiEdge *vr = static_cast<VoronoiEdgePy*>(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);
Expand Down
6 changes: 3 additions & 3 deletions src/Mod/Path/App/VoronoiVertexPyImp.cpp
Expand Up @@ -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<VoronoiVertexPy*>(lhs)->getVoronoiVertexPtr();
const VoronoiVertex *vr = static_cast<VoronoiVertexPy*>(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);
Expand Down
89 changes: 89 additions & 0 deletions src/Mod/Path/PathTests/TestPathVoronoi.py
@@ -0,0 +1,89 @@
# -*- coding: utf-8 -*-

# ***************************************************************************
# * *
# * Copyright (c) 2020 sliptonic <shopinthewoods@gmail.com> *
# * *
# * 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])

2 changes: 2 additions & 0 deletions src/Mod/Path/TestPathApp.py
Expand Up @@ -42,6 +42,7 @@
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
Expand All @@ -62,4 +63,5 @@
False if TestPathHelix.__name__ else True
False if TestPathPreferences.__name__ else True
False if TestPathToolBit.__name__ else True
False if TestPathVoronoi.__name__ else True

0 comments on commit 8f45dee

Please sign in to comment.