Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))
# Try a test with no offset specified
self.assertTrue(np.array_equal(
construct_rotation_tf(50, 0, 0),
svgpathtools.parser.parse_transform('rotate(50)')
))
expected_tf_skewx = np.identity(3)
expected_tf_skewx[0, 1] = np.tan(40.0 * np.pi/180.0)
tf_skewx = svgpathtools.parser.parse_transform('skewX(40)')
self.assertTrue(np.array_equal(expected_tf_skewx, tf_skewx))
expected_tf_skewy = np.identity(3)
expected_tf_skewy[1, 0] = np.tan(30.0 * np.pi / 180.0)
tf_skewy = svgpathtools.parser.parse_transform('skewY(30)')
self.assertTrue(np.array_equal(expected_tf_skewy, tf_skewy))
self.assertTrue(np.array_equal(
tf_rotation.dot(tf_translate).dot(tf_skewx).dot(tf_scale),
svgpathtools.parser.parse_transform(
"""rotate(-10 50 100)
translate(-36 45.5)
tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))
tf_rotation = svgpathtools.parser.parse_transform('rotate(-10 50 100)')
expected_tf_rotation = construct_rotation_tf(-10, 50, 100)
self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))
# Try a test with no offset specified
self.assertTrue(np.array_equal(
construct_rotation_tf(50, 0, 0),
svgpathtools.parser.parse_transform('rotate(50)')
))
expected_tf_skewx = np.identity(3)
expected_tf_skewx[0, 1] = np.tan(40.0 * np.pi/180.0)
tf_skewx = svgpathtools.parser.parse_transform('skewX(40)')
self.assertTrue(np.array_equal(expected_tf_skewx, tf_skewx))
expected_tf_skewy = np.identity(3)
expected_tf_skewy[1, 0] = np.tan(30.0 * np.pi / 180.0)
tf_skewy = svgpathtools.parser.parse_transform('skewY(30)')
self.assertTrue(np.array_equal(expected_tf_skewy, tf_skewy))
self.assertTrue(np.array_equal(
tf_rotation.dot(tf_translate).dot(tf_skewx).dot(tf_scale),
svgpathtools.parser.parse_transform(
"""rotate(-10 50 100)
translate(-36 45.5)
svgpathtools.parser.parse_transform('translate(-36)')
))
# Now specify y
expected_tf_translate[1, 2] = 45.5
tf_translate = svgpathtools.parser.parse_transform(
'translate(-36 45.5)')
self.assertTrue(np.array_equal(expected_tf_translate, tf_translate))
# Try a test with no y specified
expected_tf_scale = np.identity(3)
expected_tf_scale[0, 0] = 10
expected_tf_scale[1, 1] = 10
self.assertTrue(np.array_equal(
expected_tf_scale,
svgpathtools.parser.parse_transform('scale(10)')
))
# Now specify y
expected_tf_scale[1, 1] = 0.5
tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))
tf_rotation = svgpathtools.parser.parse_transform('rotate(-10 50 100)')
expected_tf_rotation = construct_rotation_tf(-10, 50, 100)
self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))
# Try a test with no offset specified
self.assertTrue(np.array_equal(
construct_rotation_tf(50, 0, 0),
svgpathtools.parser.parse_transform('rotate(50)')
))
tf_translate = svgpathtools.parser.parse_transform(
'translate(-36 45.5)')
self.assertTrue(np.array_equal(expected_tf_translate, tf_translate))
# Try a test with no y specified
expected_tf_scale = np.identity(3)
expected_tf_scale[0, 0] = 10
expected_tf_scale[1, 1] = 10
self.assertTrue(np.array_equal(
expected_tf_scale,
svgpathtools.parser.parse_transform('scale(10)')
))
# Now specify y
expected_tf_scale[1, 1] = 0.5
tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))
tf_rotation = svgpathtools.parser.parse_transform('rotate(-10 50 100)')
expected_tf_rotation = construct_rotation_tf(-10, 50, 100)
self.assertTrue(np.array_equal(expected_tf_rotation, tf_rotation))
# Try a test with no offset specified
self.assertTrue(np.array_equal(
construct_rotation_tf(50, 0, 0),
svgpathtools.parser.parse_transform('rotate(50)')
))
expected_tf_skewx = np.identity(3)
expected_tf_skewx[0, 1] = np.tan(40.0 * np.pi/180.0)
tf_skewx = svgpathtools.parser.parse_transform('skewX(40)')
self.assertTrue(np.array_equal(expected_tf_skewx, tf_skewx))
def test_transform(self):
tf_matrix = svgpathtools.parser.parse_transform(
'matrix(1.0 2.0 3.0 4.0 5.0 6.0)')
expected_tf_matrix = np.identity(3)
expected_tf_matrix[0:2, 0:3] = np.array([[1.0, 3.0, 5.0],
[2.0, 4.0, 6.0]])
self.assertTrue(np.array_equal(expected_tf_matrix, tf_matrix))
# Try a test with no y specified
expected_tf_translate = np.identity(3)
expected_tf_translate[0, 2] = -36
self.assertTrue(np.array_equal(
expected_tf_translate,
svgpathtools.parser.parse_transform('translate(-36)')
))
# Now specify y
expected_tf_translate[1, 2] = 45.5
expected_tf_matrix = np.identity(3)
expected_tf_matrix[0:2, 0:3] = np.array([[1.0, 3.0, 5.0],
[2.0, 4.0, 6.0]])
self.assertTrue(np.array_equal(expected_tf_matrix, tf_matrix))
# Try a test with no y specified
expected_tf_translate = np.identity(3)
expected_tf_translate[0, 2] = -36
self.assertTrue(np.array_equal(
expected_tf_translate,
svgpathtools.parser.parse_transform('translate(-36)')
))
# Now specify y
expected_tf_translate[1, 2] = 45.5
tf_translate = svgpathtools.parser.parse_transform(
'translate(-36 45.5)')
self.assertTrue(np.array_equal(expected_tf_translate, tf_translate))
# Try a test with no y specified
expected_tf_scale = np.identity(3)
expected_tf_scale[0, 0] = 10
expected_tf_scale[1, 1] = 10
self.assertTrue(np.array_equal(
expected_tf_scale,
svgpathtools.parser.parse_transform('scale(10)')
))
# Now specify y
expected_tf_scale[1, 1] = 0.5
tf_scale = svgpathtools.parser.parse_transform('scale(10 0.5)')
self.assertTrue(np.array_equal(expected_tf_scale, tf_scale))
def split(self, t):
"""returns two segments, whose union is this segment and which join at
self.point(t)."""
pt = self.point(t)
return Line(self.start, pt), Line(pt, self.end)
This can be undone with:
>>> numpy.seterr(**old_numpy_error_settings)
"""
assert 0 <= t <= 1
dseg = seg.derivative(t)
# Note: dseg might be numpy value, use np.seterr(invalid='raise')
try:
unit_tangent = dseg/abs(dseg)
except (ZeroDivisionError, FloatingPointError):
# This may be a removable singularity, if so we just need to compute
# the limit.
# Note: limit{{dseg / abs(dseg)} = sqrt(limit{dseg**2 / abs(dseg)**2})
dseg_poly = seg.poly().deriv()
dseg_abs_squared_poly = (real(dseg_poly) ** 2 +
imag(dseg_poly) ** 2)
try:
unit_tangent = csqrt(rational_limit(dseg_poly**2,
dseg_abs_squared_poly, t))
except ValueError:
bef = seg.poly().deriv()(t - 1e-4)
aft = seg.poly().deriv()(t + 1e-4)
mes = ("Unit tangent appears to not be well-defined at "
"t = {}, \n".format(t) +
"seg.poly().deriv()(t - 1e-4) = {}\n".format(bef) +
"seg.poly().deriv()(t + 1e-4) = {}".format(aft))
raise ValueError(mes)
return unit_tangent
>>> np.seterr(**old)
"""
dz = self.derivative(t)
ddz = self.derivative(t, n=2)
dx, dy = dz.real, dz.imag
ddx, ddy = ddz.real, ddz.imag
old_np_seterr = np.seterr(invalid='raise')
try:
kappa = abs(dx*ddy - dy*ddx)/sqrt(dx*dx + dy*dy)**3
except (ZeroDivisionError, FloatingPointError):
# tangent vector is zero at t, use polytools to find limit
p = self.poly()
dp = p.deriv()
ddp = dp.deriv()
dx, dy = real(dp), imag(dp)
ddx, ddy = real(ddp), imag(ddp)
f2 = (dx*ddy - dy*ddx)**2
g2 = (dx*dx + dy*dy)**3
lim2 = rational_limit(f2, g2, t)
if lim2 < 0: # impossible, must be numerical error
return 0
kappa = sqrt(lim2)
finally:
np.seterr(**old_np_seterr)
return kappa
"""
dz = self.derivative(t)
ddz = self.derivative(t, n=2)
dx, dy = dz.real, dz.imag
ddx, ddy = ddz.real, ddz.imag
old_np_seterr = np.seterr(invalid='raise')
try:
kappa = abs(dx*ddy - dy*ddx)/sqrt(dx*dx + dy*dy)**3
except (ZeroDivisionError, FloatingPointError):
# tangent vector is zero at t, use polytools to find limit
p = self.poly()
dp = p.deriv()
ddp = dp.deriv()
dx, dy = real(dp), imag(dp)
ddx, ddy = real(ddp), imag(ddp)
f2 = (dx*ddy - dy*ddx)**2
g2 = (dx*dx + dy*dy)**3
lim2 = rational_limit(f2, g2, t)
if lim2 < 0: # impossible, must be numerical error
return 0
kappa = sqrt(lim2)
finally:
np.seterr(**old_np_seterr)
return kappa