def test_tangent_bboxes(self):
nodes1 = np.asfortranarray([
[0.0, 0.0],
[0.5, 1.0],
[1.0, 0.0],
])
curve1 = subdivided_curve(nodes1)
nodes2 = np.asfortranarray([
[1.0, 0.0],
[1.5, 0.5],
[2.0, -0.25],
])
curve2 = subdivided_curve(nodes2)
intersections = []
next_candidates = self._call_function_under_test(
[(curve1, curve2)], intersections)
self.assertEqual(next_candidates, [])
self.assertEqual(intersections, [(1.0, 0.0)])
python类asfortranarray()的实例源码
def test_quadratics_intersect_once(self):
# NOTE: ``nodes1`` is a specialization of [0, 0], [1/2, 1], [1, 1]
# onto the interval [1/4, 1] and ``nodes`` is a specialization
# of [0, 1], [1/2, 1], [1, 0] onto the interval [0, 3/4].
# We expect them to intersect at s = 1/3, t = 2/3, which is
# the point [1/2, 3/4].
nodes1 = np.asfortranarray([
[0.25, 0.4375],
[0.625, 1.0],
[1.0, 1.0],
])
nodes2 = np.asfortranarray([
[0.0, 1.0],
[0.375, 1.0],
[0.75, 0.4375],
])
s_val = 1.0 / 3.0
t_val = 2.0 / 3.0
intersections = self._call_function_under_test(nodes1, nodes2)
# Due to round-off, the answer may be wrong by a tiny wiggle.
self.assertEqual(intersections.shape, (1, 2))
self.assertAlmostEqual(
intersections[0, 0], s_val, delta=SPACING(s_val))
self.assertEqual(intersections[0, 1], t_val)
def test_parallel_failure(self):
from bezier import _geometric_intersection
nodes1 = np.asfortranarray([
[0.0, 0.0],
[0.375, 0.75],
[0.75, 0.375],
])
nodes2 = np.asfortranarray([
[0.25, 0.625],
[0.625, 0.25],
[1.0, 1.0],
])
with self.assertRaises(NotImplementedError) as exc_info:
self._call_function_under_test(nodes1, nodes2)
exc_args = exc_info.exception.args
self.assertEqual(
exc_args, (_geometric_intersection._SEGMENTS_PARALLEL,))
def test_non_convergence(self):
from bezier import _geometric_intersection
multiplier = 16384.0
nodes1 = multiplier * np.asfortranarray([
[0.0, 0.0],
[4.5, 9.0],
[9.0, 0.0],
])
nodes2 = multiplier * np.asfortranarray([
[0.0, 8.0],
[6.0, 0.0],
])
with self.assertRaises(ValueError) as exc_info:
self._call_function_under_test(nodes1, nodes2)
exc_args = exc_info.exception.args
expected = _geometric_intersection._NO_CONVERGE_TEMPLATE.format(
_geometric_intersection._MAX_INTERSECT_SUBDIVISIONS)
self.assertEqual(exc_args, (expected,))
def test_duplicates(self):
# After three subdivisions, there are 8 pairs of curve segments
# which have bounding boxes that touch at corners (these corners are
# also intersections). This test makes sure the duplicates are
# de-duplicated.
nodes1 = np.asfortranarray([
[0.0, 0.0],
[0.5, 1.0],
[1.0, 0.0],
])
nodes2 = np.asfortranarray([
[0.0, 0.75],
[0.5, -0.25],
[1.0, 0.75],
])
intersections = self._call_function_under_test(nodes1, nodes2)
expected = np.asfortranarray([
[0.25, 0.25],
[0.75, 0.75],
])
self.assertEqual(intersections, expected)
def test_workspace_resize(self):
nodes1 = np.asfortranarray([
[-3.0, 0.0],
[5.0, 0.0],
])
nodes2 = np.asfortranarray([
[-7.0, -9.0],
[9.0, 13.0],
[-7.0, -13.0],
[9.0, 9.0],
])
# NOTE: These curves intersect 3 times, so a workspace of
# 2 is not large enough.
self.reset_workspace(2)
intersections = self._call_function_under_test(nodes1, nodes2)
expected = np.asfortranarray([
[0.5, 0.5],
[0.375, 0.25],
[0.625, 0.75],
])
self.assertEqual(intersections, expected)
# Make sure the workspace was resized.
self.assertEqual(self.workspace_size(), 3)
def test_workspace_too_small(self):
from bezier import _curve_intersection_speedup
nodes1 = np.asfortranarray([
[-3.0, 0.0],
[5.0, 0.0],
])
nodes2 = np.asfortranarray([
[-7.0, -9.0],
[9.0, 13.0],
[-7.0, -13.0],
[9.0, 9.0],
])
# NOTE: These curves intersect 3 times, so a workspace of
# 2 is not large enough.
self.reset_workspace(2)
with self.assertRaises(ValueError) as exc_info:
self._call_function_under_test(
nodes1, nodes2, allow_resize=False)
exc_args = exc_info.exception.args
expected = _curve_intersection_speedup.TOO_SMALL_TEMPLATE.format(3, 2)
self.assertEqual(exc_args, (expected,))
# Make sure the workspace was **not** resized.
self.assertEqual(self.workspace_size(), 2)
def test___dict___property(self):
nodes = np.asfortranarray([
[0.0, 1.0],
[0.0, 2.0],
])
curve = subdivided_curve(nodes)
error = 0.0
linearization = self._make_one(curve, error)
props_dict = linearization.__dict__
# NOTE: We cannot use dictionary equality check because of
# the comparison of NumPy arrays.
self.assertEqual(len(props_dict), 4)
self.assertIs(props_dict['curve'], curve)
self.assertEqual(props_dict['error'], error)
self.assertEqual(props_dict['start_node'], nodes[[0], :])
self.assertEqual(props_dict['end_node'], nodes[[1], :])
# Check that modifying ``props_dict`` won't modify ``linearization``.
props_dict['error'] = 0.5
self.assertNotEqual(linearization.error, props_dict['error'])
def test_length_property_not_cached(self):
nodes = np.asfortranarray([
[0.0, 0.0],
[1.0, 2.0],
])
curve = self._make_one(nodes, 1)
self.assertIsNone(curve._length)
patch = unittest.mock.patch(
'bezier._curve_helpers.compute_length',
return_value=unittest.mock.sentinel.length)
with patch as mocked:
self.assertEqual(curve.length, unittest.mock.sentinel.length)
self.assertEqual(mocked.call_count, 1)
call = mocked.mock_calls[0]
_, positional, keyword = call
self.assertEqual(keyword, {})
self.assertEqual(len(positional), 1)
self.assertEqual(positional[0], nodes)
def test_evaluate_multi(self):
s_vals = np.asfortranarray([0.0, 0.25, 0.5, 1.0, 1.25])
nodes = np.asfortranarray([
[0.0, 0.0],
[0.375, 0.375],
[1.0, 1.0],
])
curve = self._make_one(nodes, 2)
expected = np.asfortranarray([
[0.0, 0.0],
[0.203125, 0.203125],
[0.4375, 0.4375],
[1.0, 1.0],
[1.328125, 1.328125],
])
result = curve.evaluate_multi(s_vals)
self.assertEqual(expected, result)
def test_plot_defaults(self, new_axis_mock):
ax = unittest.mock.Mock(spec=['plot'])
new_axis_mock.return_value = ax
nodes = np.asfortranarray([
[0.0, 1.0],
[1.0, 3.0],
])
curve = self._make_one(nodes, 1, _copy=False)
num_pts = 2 # This value is crucial for the plot call.
result = curve.plot(num_pts)
self.assertIs(result, ax)
# Verify mocks.
new_axis_mock.assert_called_once_with()
# Check the call to ax.plot(). We can't assert_any_call()
# since == breaks on NumPy arrays.
self.assertEqual(ax.plot.call_count, 1)
call = ax.plot.mock_calls[0]
utils.check_plot_call(self, call, nodes, color=None, alpha=None)
def test_plot_explicit(self, new_axis_mock):
nodes = np.asfortranarray([
[0.0, 0.0],
[1.0, 1.0],
])
curve = self._make_one(nodes, 1, _copy=False)
num_pts = 2 # This value is crucial for the plot call.
ax = unittest.mock.Mock(spec=['plot'])
color = (0.75, 1.0, 1.0)
alpha = 0.625
result = curve.plot(num_pts, color=color, alpha=alpha, ax=ax)
self.assertIs(result, ax)
# Verify mocks.
new_axis_mock.assert_not_called()
# Check the call to ax.plot(). We can't assert_any_call()
# since == breaks on NumPy arrays.
self.assertEqual(ax.plot.call_count, 1)
call = ax.plot.mock_calls[0]
utils.check_plot_call(self, call, nodes, color=color, alpha=alpha)
def test_intersect_algebraic(self):
from bezier import _intersection_helpers
nodes1 = np.asfortranarray([
[0.0, 0.0],
[1.0, 1.0],
])
curve1 = self._make_one(nodes1, 1)
nodes2 = np.asfortranarray([
[0.0, 1.0],
[1.0, 0.0],
])
curve2 = self._make_one(nodes2, 1)
strategy = _intersection_helpers.IntersectionStrategy.ALGEBRAIC
intersections = curve1.intersect(curve2, strategy=strategy)
expected = np.asfortranarray([
[0.5, 0.5],
])
self.assertEqual(intersections, expected)
def _intersect_helper(self, **kwargs):
# NOTE: ``nodes1`` is a specialization of [0, 0], [1/2, 1], [1, 1]
# onto the interval [1/4, 1] and ``nodes`` is a specialization
# of [0, 1], [1/2, 1], [1, 0] onto the interval [0, 3/4].
# We expect them to intersect at s = 1/3, t = 2/3.
nodes_left = np.asfortranarray([
[0.25, 0.4375],
[0.625, 1.0],
[1.0, 1.0],
])
left = self._make_one(nodes_left, 2)
nodes_right = np.asfortranarray([
[0.0, 1.0],
[0.375, 1.0],
[0.75, 0.4375],
])
right = self._make_one(nodes_right, 2)
result = left.intersect(right, **kwargs)
expected = np.asfortranarray([[1.0, 2.0]]) / 3.0
self.assertTrue(
np.allclose(result, expected, atol=0.0, rtol=0.5**52))
def test_elevate(self):
nodes = np.asfortranarray([
[0.0, 0.5],
[1.0, 1.0],
[3.0, 2.0],
[3.5, 4.0],
])
curve = self._make_one(nodes, 3)
self.assertEqual(curve.degree, 3)
elevated = curve.elevate()
self.assertEqual(elevated.degree, 4)
self.assertEqual(elevated.start, curve.start)
self.assertEqual(elevated.end, curve.end)
s_vals = np.linspace(0.0, 1.0, 64 + 1)
orig_vals = curve.evaluate_multi(s_vals)
new_vals = elevated.evaluate_multi(s_vals)
self.assertEqual(orig_vals, new_vals)
def test_reduce_(self):
nodes = np.asfortranarray([
[0.0, 0.0],
[1.0, 3.0],
[2.0, 3.0],
[3.0, 0.0],
])
curve = self._make_one(nodes, 3)
self.assertEqual(curve.degree, 3)
reduced = curve.reduce_()
expected = np.asfortranarray([
[0.0, 0.0],
[1.5, 4.5],
[3.0, 0.0],
])
self.assertEqual(reduced.nodes, expected)
self.assertEqual(reduced.start, curve.start)
self.assertEqual(reduced.end, curve.end)
s_vals = np.linspace(0.0, 1.0, 64 + 1)
orig_vals = curve.evaluate_multi(s_vals)
new_vals = reduced.evaluate_multi(s_vals)
self.assertEqual(orig_vals, new_vals)
def test_quartic(self):
expected_l = np.asfortranarray([
[1.0, 0.0, 0.0, 0.0, 0.0],
[1.0, 1.0, 0.0, 0.0, 0.0],
[1.0, 2.0, 1.0, 0.0, 0.0],
[1.0, 3.0, 3.0, 1.0, 0.0],
[1.0, 4.0, 6.0, 4.0, 1.0],
])
expected_r = np.asfortranarray([
[1.0, 4.0, 6.0, 4.0, 1.0],
[0.0, 1.0, 3.0, 3.0, 1.0],
[0.0, 0.0, 1.0, 2.0, 1.0],
[0.0, 0.0, 0.0, 1.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 1.0],
])
row_scaling = np.asfortranarray([[1.0], [2.0], [4.0], [8.0], [16.0]])
expected_l /= row_scaling
expected_r /= row_scaling[::-1, :]
self._helper(4, expected_l, expected_r)
def test_cubic(self):
nodes = np.asfortranarray([
[0.0, 1.0],
[4.0, 6.0],
[7.0, 3.0],
[6.0, 5.0],
])
expected_l = np.asfortranarray([
[0.0, 1.0],
[2.0, 3.5],
[3.75, 4.0],
[4.875, 4.125],
])
expected_r = np.asfortranarray([
[4.875, 4.125],
[6.0, 4.25],
[6.5, 4.0],
[6.0, 5.0],
])
self._helper(nodes, expected_l, expected_r)
def test_non_unity(self):
nodes = np.asfortranarray([
[0.0, 0.0, 0.0],
[0.5, 3.0, 1.0],
[1.5, 4.0, 1.0],
[2.0, 8.0, 1.0],
])
lambda1 = np.asfortranarray([0.25, 0.5, 0.75])
lambda2 = np.asfortranarray([0.25, 0.125, -0.75])
result = self._call_function_under_test(nodes, lambda1, lambda2)
expected = np.asfortranarray([
[0.125, 0.453125, 0.109375],
[0.0859375, 0.390625, 0.119140625],
[0.421875, -2.109375, -0.421875],
])
self.assertEqual(result, expected)
def test_linear(self):
num_vals = 129
s_vals = np.linspace(0.0, 1.0, num_vals)
# B(s) = [s + 1, 1 - 2 s, 3 s - 7]
nodes = np.asfortranarray([
[1.0, 1.0, -7.0],
[2.0, -1.0, -4.0],
])
result = self._call_function_under_test(nodes, s_vals)
expected = np.empty((num_vals, 3), order='F')
expected[:, 0] = 1.0 + s_vals
expected[:, 1] = 1.0 - 2.0 * s_vals
expected[:, 2] = -7.0 + 3.0 * s_vals
self.assertEqual(result, expected)