Split out PathGeom and created test cases for it.

This commit is contained in:
Markus Lampert
2016-11-28 15:11:24 -08:00
parent abe7c4404d
commit a86f05071c
6 changed files with 409 additions and 50 deletions

View File

@@ -16,67 +16,68 @@ INSTALL(
SET(PathScripts_SRCS
PathCommands.py
PathScripts/__init__.py
PathScripts/PostUtils.py
PathScripts/example_pre.py
PathScripts/opensbp_pre.py
PathScripts/opensbp_post.py
PathScripts/example_post.py
PathScripts/linuxcnc_post.py
PathScripts/centroid_post.py
PathScripts/comparams_post.py
PathScripts/dynapath_post.py
PathScripts/generic_post.py
PathScripts/dumper_post.py
PathScripts/rml_post.py
PathScripts/TooltableEditor.py
PathScripts/PathProfile.py
PathScripts/PathProfileEdges.py
PathScripts/PathContour.py
PathScripts/PathMillFace.py
PathScripts/PathPocket.py
PathScripts/PathDrilling.py
PathScripts/PathDressup.py
PathScripts/DogboneDressup.py
PathScripts/DragknifeDressup.py
PathScripts/PathDressupHoldingTags.py
PathScripts/PathHop.py
PathScripts/PathUtils.py
PathScripts/PathSelection.py
PathScripts/PathFixture.py
PathScripts/PathCopy.py
PathScripts/PathAreaUtils.py
PathScripts/PathArray.py
PathScripts/PathComment.py
PathScripts/PathCompoundExtended.py
PathScripts/PathContour.py
PathScripts/PathCopy.py
PathScripts/PathCustom.py
PathScripts/PathDressup.py
PathScripts/PathDressupHoldingTags.py
PathScripts/PathDrilling.py
PathScripts/PathEngrave.py
PathScripts/PathFacePocket.py
PathScripts/PathFaceProfile.py
PathScripts/PathFixture.py
PathScripts/PathFromShape.py
PathScripts/PathGeom.py
PathScripts/PathHop.py
PathScripts/PathInspect.py
PathScripts/PathJob.py
PathScripts/PathStock.py
PathScripts/PathKurveUtils.py
PathScripts/PathLoadTool.py
PathScripts/PathMillFace.py
PathScripts/PathPlane.py
PathScripts/PathPocket.py
PathScripts/PathPost.py
PathScripts/PathPostProcessor.py
PathScripts/PathLoadTool.py
PathScripts/PathToolLenOffset.py
PathScripts/PathComment.py
PathScripts/PathStop.py
PathScripts/PathFromShape.py
PathScripts/PathKurveUtils.py
PathScripts/PathAreaUtils.py
PathScripts/slic3r_pre.py
PathScripts/PathFaceProfile.py
PathScripts/PathFacePocket.py
PathScripts/PathArray.py
PathScripts/PathCustom.py
PathScripts/PathInspect.py
PathScripts/PathSimpleCopy.py
PathScripts/PathEngrave.py
PathScripts/PathSurface.py
PathScripts/PathPreferences.py
PathScripts/PathPreferencesPathJob.py
PathScripts/PathProfile.py
PathScripts/PathProfileEdges.py
PathScripts/PathRemote.py
PathScripts/PathSanity.py
PathScripts/PathSelection.py
PathScripts/PathSimpleCopy.py
PathScripts/PathStock.py
PathScripts/PathStop.py
PathScripts/PathSurface.py
PathScripts/PathToolLenOffset.py
PathScripts/PathToolLibraryManager.py
PathScripts/DogboneDressup.py
PathScripts/PathPreferencesPathJob.py
PathScripts/PathPreferences.py
PathScripts/PathUtils.py
PathScripts/PostUtils.py
PathScripts/TooltableEditor.py
PathScripts/__init__.py
PathScripts/centroid_post.py
PathScripts/comparams_post.py
PathScripts/dumper_post.py
PathScripts/dynapath_post.py
PathScripts/example_post.py
PathScripts/example_pre.py
PathScripts/generic_post.py
PathScripts/linuxcnc_post.py
PathScripts/opensbp_post.py
PathScripts/opensbp_pre.py
PathScripts/rml_post.py
PathScripts/slic3r_pre.py
PathTests/TestPathDressupHoldingTags.py
PathTests/TestPathGeom.py
PathTests/TestPathPost.py
PathTests/__init__.py
PathTests/test_linuxcnc_00.ngc
PathTests/TestPathDressupHoldingTags.py
PathTests/TestPathPost.py
TestPathApp.py
)

View File

@@ -2,7 +2,7 @@
# ***************************************************************************
# * *
# * Copyright (c) 2014 Yorik van Havre <yorik@uncreated.net> *
# * Copyright (c) 2016 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) *

View File

@@ -0,0 +1,159 @@
# -*- coding: utf-8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2016 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 math
import Part
import Path
from FreeCAD import Vector
class Side:
"""Class to determine and define the side a Path is on or Vectors are in relation to each other."""
Left = +1
"""Representing the left side."""
Right = -1
"""Representing the right side."""
Straight = 0
"""Used if two vectors form a line."""
On = 0
"""Synonym for Straight."""
@classmethod
def toString(cls, side):
"""Returns a string representation of the enum value."""
if side == cls.Left:
return 'Left'
if side == cls.Right:
return 'Right'
return 'On'
@classmethod
def of(cls, ptRef, pt):
"""Determine the side of pt in relation to ptRef.
If both Points are viewed as vectors with their origin in (0,0,0)
then the two vectors are either form a straigt line (On) or pt
lies in the left or right hemishpere in regards to ptRef."""
d = -ptRef.x*pt.y + ptRef.y*pt.x
if d < 0:
return cls.Left
if d > 0:
return cls.Right
return cls.Straight
class PathGeom:
"""Class to transform Path Commands into Edges and Wire and back again.
The interface might eventuallly become part of Path itself."""
CmdMoveStraight = ['G1', 'G01']
CmdMoveCW = ['G2', 'G02']
CmdMoveCCW = ['G3', 'G03']
CmdMoveArc = CmdMoveCW + CmdMoveCCW
CmdMove = CmdMoveStraight + CmdMoveArc
@classmethod
def getAngle(cls, vertex):
"""Returns the angle [-pi,pi] of a vertex using the X-axis as the reference.
Positive angles for vertexes in the upper hemishpere (positive y values)
and negative angles for the lower hemishpere."""
a = vertex.getAngle(FreeCAD.Vector(1,0,0))
if vertex.y < 0:
return -a
return a
@classmethod
def diffAngle(cls, a1, a2, direction = 'CW'):
"""Returns the difference between two angles (a1 -> a2) into a given direction."""
if direction == 'CW':
while a1 < a2:
a1 += 2*math.pi
a = a1 - a2
else:
while a2 < a1:
a2 += 2*math.pi
a = a2 - a1
return a
@classmethod
def commandEndPoint(cls, cmd, defaultPoint = Vector(), X='X', Y='Y', Z='Z'):
"""Extracts the end point from a Path Command."""
x = cmd.Parameters.get(X, defaultPoint.x)
y = cmd.Parameters.get(Y, defaultPoint.y)
z = cmd.Parameters.get(Z, defaultPoint.z)
return FreeCAD.Vector(x, y, z)
@classmethod
def xy(cls, pt):
"""Conveninience function to return the projection of the Vector in the XY-plane."""
return Vector(pt.x, pt.y, 0)
@classmethod
def edgeForCmd(cls, cmd, startPoint):
"""Returns a Curve representing the givne command, assuming a given startinPoint."""
endPoint = cls.commandEndPoint(cmd, startPoint)
if cmd.Name in cls.CmdMoveStraight:
return Part.Edge(Part.Line(startPoint, endPoint))
if cmd.Name in cls.CmdMoveArc:
center = startPoint + cls.commandEndPoint(cmd, Vector(0,0,0), 'I', 'J', 'K')
A = cls.xy(startPoint - center)
B = cls.xy(endPoint - center)
d = -B.x * A.y + B.y * A.x
if d == 0:
# we're dealing with half a circle here
angle = cls.getAngle(A) + math.pi/2
if cmd.Name in cls.CmdMoveCW:
angle -= math.pi
else:
C = A + B
angle = cls.getAngle(C)
R = A.Length
#print("arc: p1=(%.2f, %.2f) p2=(%.2f, %.2f) -> center=(%.2f, %.2f)" % (startPoint.x, startPoint.y, endPoint.x, endPoint.y, center.x, center.y))
#print("arc: A=(%.2f, %.2f) B=(%.2f, %.2f) -> d=%.2f" % (A.x, A.y, B.x, B.y, d))
#print("arc: R=%.2f angle=%.2f" % (R, angle/math.pi))
if startPoint.z == endPoint.z:
midPoint = center + FreeCAD.Vector(math.cos(angle), math.sin(angle), 0) * R
return Part.Edge(Part.Arc(startPoint, midPoint, endPoint))
# It's a Helix
#print('angle: A=%.2f B=%.2f' % (cls.getAngle(A)/math.pi, cls.getAngle(B)/math.pi))
if cmd.Name in cls.CmdMoveCW:
cw = True
else:
cw = False
angle = cls.diffAngle(cls.getAngle(A), cls.getAngle(B), 'CW' if cw else 'CCW')
height = endPoint.z - startPoint.z
pitch = height * math.fabs(2 * math.pi / angle)
if angle > 0:
cw = not cw
#print("Helix: R=%.2f h=%.2f angle=%.2f pitch=%.2f" % (R, height, angle/math.pi, pitch))
helix = Part.makeHelix(pitch, height, R, 0, not cw)
helix.rotate(Vector(), Vector(0,0,1), 180 * cls.getAngle(A) / math.pi)
e = helix.Edges[0]
helix.translate(startPoint - e.valueAt(e.FirstParameter))
return helix.Edges[0]

View File

@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2016 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 Part
import math
import unittest
from PathScripts.PathGeom import Side
class PathTestBase(unittest.TestCase):
"""Base test class with some addtional asserts."""
def assertRoughly(self, f1, f2):
"""Verify that two float values are approximately the same."""
self.assertTrue(math.fabs(f1 - f2) < 0.00001, "%f != %f" % (f1, f2))
def assertCoincide(self, pt1, pt2):
"""Verify that two points coincide - roughly speaking."""
self.assertRoughly(pt1.x, pt2.x)
self.assertRoughly(pt1.y, pt2.y)
self.assertRoughly(pt1.z, pt2.z)
def assertLine(self, edge, pt1, pt2):
"""Verify that edge is a line from pt1 to pt2."""
self.assertIs(type(edge.Curve), Part.Line)
self.assertCoincide(edge.Curve.StartPoint, pt1)
self.assertCoincide(edge.Curve.EndPoint, pt2)
def assertArc(self, edge, pt1, pt2, direction = 'CW'):
"""Verify that edge is an arc between pt1 and pt2 with the given direction."""
# If an Arc is wrapped into edge, then it's curve is represented as a circle
# and not as an Arc (GeomTrimmedCurve)
#self.assertIs(type(edge.Curve), Part.Arc)
self.assertIs(type(edge.Curve), Part.Circle)
self.assertCoincide(edge.valueAt(edge.FirstParameter), pt1)
self.assertCoincide(edge.valueAt(edge.LastParameter), pt2)
ptm = edge.valueAt((edge.LastParameter + edge.FirstParameter)/2)
side = Side.of(pt2 - pt1, ptm - pt1)
#print("(%.2f, %.2f) (%.2f, %.2f) (%.2f, %.2f)" % (pt1.x, pt1.y, ptm.x, ptm.y, pt2.x, pt2.y))
#print(" (%.2f, %.2f) (%.2f, %.2f) -> %s" % ((pt2-pt1).x, (pt2-pt1).y, (ptm-pt1).x, (ptm-pt1).y, Side.toString(side)))
#print(" (%.2f, %.2f) (%.2f, %.2f) -> (%.2f, %.2f)" % (pf.x,pf.y, pl.x,pl.y, pm.x, pmy))
if 'CW' == direction:
self.assertEqual(side, Side.Left)
else:
self.assertEqual(side, Side.Right)
def assertCurve(self, edge, p1, p2, p3):
"""Verify that the edge goes through the given 3 points, representing start, mid and end point respectively."""
self.assertCoincide(edge.valueAt(edge.FirstParameter), p1)
self.assertCoincide(edge.valueAt(edge.LastParameter), p3)
self.assertCoincide(edge.valueAt((edge.FirstParameter + edge.LastParameter)/2), p2)

View File

@@ -0,0 +1,123 @@
# -*- coding: utf-8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2016 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 Part
import Path
import PathScripts
import math
import unittest
from FreeCAD import Vector
from PathScripts.PathDressupHoldingTags import *
from PathScripts.PathGeom import PathGeom
from PathTests.PathTestUtils import PathTestBase
class TestPathGeom(PathTestBase):
"""Test Path <-> Wire conversion."""
def test00(self):
"""Verify getAngle functionality."""
self.assertRoughly(PathGeom.getAngle(Vector(1, 0, 0)), 0)
self.assertRoughly(PathGeom.getAngle(Vector(1, 1, 0)), math.pi/4)
self.assertRoughly(PathGeom.getAngle(Vector(0, 1, 0)), math.pi/2)
self.assertRoughly(PathGeom.getAngle(Vector(-1, 1, 0)), 3*math.pi/4)
self.assertRoughly(PathGeom.getAngle(Vector(-1, 0, 0)), math.pi)
self.assertRoughly(PathGeom.getAngle(Vector(-1, -1, 0)), -3*math.pi/4)
self.assertRoughly(PathGeom.getAngle(Vector(0, -1, 0)), -math.pi/2)
self.assertRoughly(PathGeom.getAngle(Vector(1, -1, 0)), -math.pi/4)
def test01(self):
"""Verify diffAngle functionality."""
self.assertRoughly(PathGeom.diffAngle(0, +0*math.pi/4, 'CW') / math.pi, 0/4.)
self.assertRoughly(PathGeom.diffAngle(0, +3*math.pi/4, 'CW') / math.pi, 5/4.)
self.assertRoughly(PathGeom.diffAngle(0, -3*math.pi/4, 'CW') / math.pi, 3/4.)
self.assertRoughly(PathGeom.diffAngle(0, +4*math.pi/4, 'CW') / math.pi, 4/4.)
self.assertRoughly(PathGeom.diffAngle(0, +0*math.pi/4, 'CCW')/ math.pi, 0/4.)
self.assertRoughly(PathGeom.diffAngle(0, +3*math.pi/4, 'CCW')/ math.pi, 3/4.)
self.assertRoughly(PathGeom.diffAngle(0, -3*math.pi/4, 'CCW')/ math.pi, 5/4.)
self.assertRoughly(PathGeom.diffAngle(0, +4*math.pi/4, 'CCW')/ math.pi, 4/4.)
self.assertRoughly(PathGeom.diffAngle(+math.pi/4, +0*math.pi/4, 'CW') / math.pi, 1/4.)
self.assertRoughly(PathGeom.diffAngle(+math.pi/4, +3*math.pi/4, 'CW') / math.pi, 6/4.)
self.assertRoughly(PathGeom.diffAngle(+math.pi/4, -1*math.pi/4, 'CW') / math.pi, 2/4.)
self.assertRoughly(PathGeom.diffAngle(-math.pi/4, +0*math.pi/4, 'CW') / math.pi, 7/4.)
self.assertRoughly(PathGeom.diffAngle(-math.pi/4, +3*math.pi/4, 'CW') / math.pi, 4/4.)
self.assertRoughly(PathGeom.diffAngle(-math.pi/4, -1*math.pi/4, 'CW') / math.pi, 0/4.)
self.assertRoughly(PathGeom.diffAngle(+math.pi/4, +0*math.pi/4, 'CCW') / math.pi, 7/4.)
self.assertRoughly(PathGeom.diffAngle(+math.pi/4, +3*math.pi/4, 'CCW') / math.pi, 2/4.)
self.assertRoughly(PathGeom.diffAngle(+math.pi/4, -1*math.pi/4, 'CCW') / math.pi, 6/4.)
self.assertRoughly(PathGeom.diffAngle(-math.pi/4, +0*math.pi/4, 'CCW') / math.pi, 1/4.)
self.assertRoughly(PathGeom.diffAngle(-math.pi/4, +3*math.pi/4, 'CCW') / math.pi, 4/4.)
self.assertRoughly(PathGeom.diffAngle(-math.pi/4, -1*math.pi/4, 'CCW') / math.pi, 0/4.)
def test10(self):
"""Verify proper geometry objects for G1 and G01 commands are created."""
spt = Vector(1,2,3)
self.assertLine(PathGeom.edgeForCmd(Path.Command('G1', {'X': 7, 'Y': 2, 'Z': 3}), spt), spt, Vector(7, 2, 3))
self.assertLine(PathGeom.edgeForCmd(Path.Command('G01', {'X': 1, 'Y': 3, 'Z': 5}), spt), spt, Vector(1, 3, 5))
def test20(self):
"""Verfiy proper geometry for arcs in the XY-plane are created."""
p1 = Vector(0, -1, 2)
p2 = Vector(-1, 0, 2)
self.assertArc(
PathGeom.edgeForCmd(
Path.Command('G2', {'X': p2.x, 'Y': p2.y, 'Z': p2.z, 'I': 0, 'J': 1, 'K': 0}), p1),
p1, p2, 'CW')
self.assertArc(
PathGeom.edgeForCmd(
Path.Command('G3', {'X': p1.x, 'Y': p1.y, 'z': p1.z, 'I': -1, 'J': 0, 'K': 0}), p2),
p2, p1, 'CCW')
def test30(self):
"""Verify proper geometry for arcs with rising and fall ing Z-axis are created."""
#print("------ rising helix -------")
p1 = Vector(0, 1, 0)
p2 = Vector(1, 0, 2)
self.assertCurve(
PathGeom.edgeForCmd(
Path.Command('G2', {'X': p2.x, 'Y': p2.y, 'Z': p2.z, 'I': 0, 'J': -1, 'K': 1}), p1),
p1, Vector(1/math.sqrt(2), 1/math.sqrt(2), 1), p2)
p1 = Vector(-1, 0, 0)
p2 = Vector(0, -1, 2)
self.assertCurve(
PathGeom.edgeForCmd(
Path.Command('G3', {'X': p2.x, 'Y': p2.y, 'Z': p2.z, 'I': 1, 'J': 0, 'K': 1}), p1),
p1, Vector(-1/math.sqrt(2), -1/math.sqrt(2), 1), p2)
#print("------ falling helix -------")
p1 = Vector(0, -1, 2)
p2 = Vector(-1, 0, 0)
self.assertCurve(
PathGeom.edgeForCmd(
Path.Command('G2', {'X': p2.x, 'Y': p2.y, 'Z': p2.z, 'I': 0, 'J': 1, 'K': -1}), p1),
p1, Vector(-1/math.sqrt(2), -1/math.sqrt(2), 1), p2)
p1 = Vector(-1, 0, 2)
p2 = Vector(0, -1, 0)
self.assertCurve(
PathGeom.edgeForCmd(
Path.Command('G3', {'X': p2.x, 'Y': p2.y, 'Z': p2.z, 'I': 1, 'J': 0, 'K': -1}), p1),
p1, Vector(-1/math.sqrt(2), -1/math.sqrt(2), 1), p2)

View File

@@ -32,3 +32,4 @@ from PathTests.TestPathDressupHoldingTags import TestTag02SquareTag
from PathTests.TestPathDressupHoldingTags import TestTag03TrapezoidTag
from PathTests.TestPathDressupHoldingTags import TestTag04TriangularTag
from PathTests.TestPathGeom import TestPathGeom