repository_name
stringlengths
7
55
func_path_in_repository
stringlengths
4
223
func_name
stringlengths
1
134
whole_func_string
stringlengths
75
104k
language
stringclasses
1 value
func_code_string
stringlengths
75
104k
func_code_tokens
sequencelengths
19
28.4k
func_documentation_string
stringlengths
1
46.9k
func_documentation_tokens
sequencelengths
1
1.97k
split_name
stringclasses
1 value
func_code_url
stringlengths
87
315
dhermes/bezier
src/bezier/_algebraic_intersection.py
_to_power_basis_degree8
def _to_power_basis_degree8(nodes1, nodes2): r"""Compute the coefficients of an **intersection polynomial**. Helper for :func:`to_power_basis` in the case that B |eacute| zout's `theorem`_ tells us the **intersection polynomial** is degree :math:`8`. This happens if the two curves have degrees one and eight or have degrees two and four. .. note:: This uses a least-squares fit to the function evaluated at the Chebyshev nodes (scaled and shifted onto ``[0, 1]``). Hence, the coefficients may be less stable than those produced for smaller degrees. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``9``-array of coefficients. """ evaluated = [ eval_intersection_polynomial(nodes1, nodes2, t_val) for t_val in _CHEB9 ] return polynomial.polyfit(_CHEB9, evaluated, 8)
python
def _to_power_basis_degree8(nodes1, nodes2): r"""Compute the coefficients of an **intersection polynomial**. Helper for :func:`to_power_basis` in the case that B |eacute| zout's `theorem`_ tells us the **intersection polynomial** is degree :math:`8`. This happens if the two curves have degrees one and eight or have degrees two and four. .. note:: This uses a least-squares fit to the function evaluated at the Chebyshev nodes (scaled and shifted onto ``[0, 1]``). Hence, the coefficients may be less stable than those produced for smaller degrees. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``9``-array of coefficients. """ evaluated = [ eval_intersection_polynomial(nodes1, nodes2, t_val) for t_val in _CHEB9 ] return polynomial.polyfit(_CHEB9, evaluated, 8)
[ "def", "_to_power_basis_degree8", "(", "nodes1", ",", "nodes2", ")", ":", "evaluated", "=", "[", "eval_intersection_polynomial", "(", "nodes1", ",", "nodes2", ",", "t_val", ")", "for", "t_val", "in", "_CHEB9", "]", "return", "polynomial", ".", "polyfit", "(", "_CHEB9", ",", "evaluated", ",", "8", ")" ]
r"""Compute the coefficients of an **intersection polynomial**. Helper for :func:`to_power_basis` in the case that B |eacute| zout's `theorem`_ tells us the **intersection polynomial** is degree :math:`8`. This happens if the two curves have degrees one and eight or have degrees two and four. .. note:: This uses a least-squares fit to the function evaluated at the Chebyshev nodes (scaled and shifted onto ``[0, 1]``). Hence, the coefficients may be less stable than those produced for smaller degrees. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``9``-array of coefficients.
[ "r", "Compute", "the", "coefficients", "of", "an", "**", "intersection", "polynomial", "**", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L432-L457
dhermes/bezier
src/bezier/_algebraic_intersection.py
_to_power_basis33
def _to_power_basis33(nodes1, nodes2): r"""Compute the coefficients of an **intersection polynomial**. Helper for :func:`to_power_basis` in the case that each curve is degree three. In this case, B |eacute| zout's `theorem`_ tells us that the **intersection polynomial** is degree :math:`3 \cdot 3` hence we return ten coefficients. .. note:: This uses a least-squares fit to the function evaluated at the Chebyshev nodes (scaled and shifted onto ``[0, 1]``). Hence, the coefficients may be less stable than those produced for smaller degrees. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``10``-array of coefficients. """ evaluated = [ eval_intersection_polynomial(nodes1, nodes2, t_val) for t_val in _CHEB10 ] return polynomial.polyfit(_CHEB10, evaluated, 9)
python
def _to_power_basis33(nodes1, nodes2): r"""Compute the coefficients of an **intersection polynomial**. Helper for :func:`to_power_basis` in the case that each curve is degree three. In this case, B |eacute| zout's `theorem`_ tells us that the **intersection polynomial** is degree :math:`3 \cdot 3` hence we return ten coefficients. .. note:: This uses a least-squares fit to the function evaluated at the Chebyshev nodes (scaled and shifted onto ``[0, 1]``). Hence, the coefficients may be less stable than those produced for smaller degrees. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``10``-array of coefficients. """ evaluated = [ eval_intersection_polynomial(nodes1, nodes2, t_val) for t_val in _CHEB10 ] return polynomial.polyfit(_CHEB10, evaluated, 9)
[ "def", "_to_power_basis33", "(", "nodes1", ",", "nodes2", ")", ":", "evaluated", "=", "[", "eval_intersection_polynomial", "(", "nodes1", ",", "nodes2", ",", "t_val", ")", "for", "t_val", "in", "_CHEB10", "]", "return", "polynomial", ".", "polyfit", "(", "_CHEB10", ",", "evaluated", ",", "9", ")" ]
r"""Compute the coefficients of an **intersection polynomial**. Helper for :func:`to_power_basis` in the case that each curve is degree three. In this case, B |eacute| zout's `theorem`_ tells us that the **intersection polynomial** is degree :math:`3 \cdot 3` hence we return ten coefficients. .. note:: This uses a least-squares fit to the function evaluated at the Chebyshev nodes (scaled and shifted onto ``[0, 1]``). Hence, the coefficients may be less stable than those produced for smaller degrees. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``10``-array of coefficients.
[ "r", "Compute", "the", "coefficients", "of", "an", "**", "intersection", "polynomial", "**", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L460-L486
dhermes/bezier
src/bezier/_algebraic_intersection.py
to_power_basis
def to_power_basis(nodes1, nodes2): """Compute the coefficients of an **intersection polynomial**. .. note:: This assumes that the degree of the curve given by ``nodes1`` is less than or equal to the degree of that given by ``nodes2``. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: Array of coefficients. Raises: NotImplementedError: If the degree pair is not ``1-1``, ``1-2``, ``1-3``, ``1-4``, ``2-2``, ``2-3``, ``2-4`` or ``3-3``. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-return-statements _, num_nodes1 = nodes1.shape _, num_nodes2 = nodes2.shape if num_nodes1 == 2: if num_nodes2 == 2: return _to_power_basis11(nodes1, nodes2) elif num_nodes2 == 3: return _to_power_basis12(nodes1, nodes2) elif num_nodes2 == 4: return _to_power_basis13(nodes1, nodes2) elif num_nodes2 == 5: return _to_power_basis_degree4(nodes1, nodes2) elif num_nodes1 == 3: if num_nodes2 == 3: return _to_power_basis_degree4(nodes1, nodes2) elif num_nodes2 == 4: return _to_power_basis23(nodes1, nodes2) elif num_nodes2 == 5: return _to_power_basis_degree8(nodes1, nodes2) elif num_nodes1 == 4: if num_nodes2 == 4: return _to_power_basis33(nodes1, nodes2) raise NotImplementedError( "Degree 1", num_nodes1 - 1, "Degree 2", num_nodes2 - 1, _POWER_BASIS_ERR, )
python
def to_power_basis(nodes1, nodes2): """Compute the coefficients of an **intersection polynomial**. .. note:: This assumes that the degree of the curve given by ``nodes1`` is less than or equal to the degree of that given by ``nodes2``. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: Array of coefficients. Raises: NotImplementedError: If the degree pair is not ``1-1``, ``1-2``, ``1-3``, ``1-4``, ``2-2``, ``2-3``, ``2-4`` or ``3-3``. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-return-statements _, num_nodes1 = nodes1.shape _, num_nodes2 = nodes2.shape if num_nodes1 == 2: if num_nodes2 == 2: return _to_power_basis11(nodes1, nodes2) elif num_nodes2 == 3: return _to_power_basis12(nodes1, nodes2) elif num_nodes2 == 4: return _to_power_basis13(nodes1, nodes2) elif num_nodes2 == 5: return _to_power_basis_degree4(nodes1, nodes2) elif num_nodes1 == 3: if num_nodes2 == 3: return _to_power_basis_degree4(nodes1, nodes2) elif num_nodes2 == 4: return _to_power_basis23(nodes1, nodes2) elif num_nodes2 == 5: return _to_power_basis_degree8(nodes1, nodes2) elif num_nodes1 == 4: if num_nodes2 == 4: return _to_power_basis33(nodes1, nodes2) raise NotImplementedError( "Degree 1", num_nodes1 - 1, "Degree 2", num_nodes2 - 1, _POWER_BASIS_ERR, )
[ "def", "to_power_basis", "(", "nodes1", ",", "nodes2", ")", ":", "# NOTE: There is no corresponding \"enable\", but the disable only applies", "# in this lexical scope.", "# pylint: disable=too-many-return-statements", "_", ",", "num_nodes1", "=", "nodes1", ".", "shape", "_", ",", "num_nodes2", "=", "nodes2", ".", "shape", "if", "num_nodes1", "==", "2", ":", "if", "num_nodes2", "==", "2", ":", "return", "_to_power_basis11", "(", "nodes1", ",", "nodes2", ")", "elif", "num_nodes2", "==", "3", ":", "return", "_to_power_basis12", "(", "nodes1", ",", "nodes2", ")", "elif", "num_nodes2", "==", "4", ":", "return", "_to_power_basis13", "(", "nodes1", ",", "nodes2", ")", "elif", "num_nodes2", "==", "5", ":", "return", "_to_power_basis_degree4", "(", "nodes1", ",", "nodes2", ")", "elif", "num_nodes1", "==", "3", ":", "if", "num_nodes2", "==", "3", ":", "return", "_to_power_basis_degree4", "(", "nodes1", ",", "nodes2", ")", "elif", "num_nodes2", "==", "4", ":", "return", "_to_power_basis23", "(", "nodes1", ",", "nodes2", ")", "elif", "num_nodes2", "==", "5", ":", "return", "_to_power_basis_degree8", "(", "nodes1", ",", "nodes2", ")", "elif", "num_nodes1", "==", "4", ":", "if", "num_nodes2", "==", "4", ":", "return", "_to_power_basis33", "(", "nodes1", ",", "nodes2", ")", "raise", "NotImplementedError", "(", "\"Degree 1\"", ",", "num_nodes1", "-", "1", ",", "\"Degree 2\"", ",", "num_nodes2", "-", "1", ",", "_POWER_BASIS_ERR", ",", ")" ]
Compute the coefficients of an **intersection polynomial**. .. note:: This assumes that the degree of the curve given by ``nodes1`` is less than or equal to the degree of that given by ``nodes2``. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: Array of coefficients. Raises: NotImplementedError: If the degree pair is not ``1-1``, ``1-2``, ``1-3``, ``1-4``, ``2-2``, ``2-3``, ``2-4`` or ``3-3``.
[ "Compute", "the", "coefficients", "of", "an", "**", "intersection", "polynomial", "**", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L489-L546
dhermes/bezier
src/bezier/_algebraic_intersection.py
polynomial_norm
def polynomial_norm(coeffs): r"""Computes :math:`L_2` norm of polynomial on :math:`\left[0, 1\right]`. We have .. math:: \left\langle f, f \right\rangle = \sum_{i, j} \int_0^1 c_i c_j x^{i + j} \, dx = \sum_{i, j} \frac{c_i c_j}{i + j + 1} = \sum_{i} \frac{c_i^2}{2 i + 1} + 2 \sum_{j > i} \frac{c_i c_j}{i + j + 1}. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. Returns: float: The :math:`L_2` norm of the polynomial. """ num_coeffs, = coeffs.shape result = 0.0 for i in six.moves.xrange(num_coeffs): coeff_i = coeffs[i] result += coeff_i * coeff_i / (2.0 * i + 1.0) for j in six.moves.xrange(i + 1, num_coeffs): coeff_j = coeffs[j] result += 2.0 * coeff_i * coeff_j / (i + j + 1.0) return np.sqrt(result)
python
def polynomial_norm(coeffs): r"""Computes :math:`L_2` norm of polynomial on :math:`\left[0, 1\right]`. We have .. math:: \left\langle f, f \right\rangle = \sum_{i, j} \int_0^1 c_i c_j x^{i + j} \, dx = \sum_{i, j} \frac{c_i c_j}{i + j + 1} = \sum_{i} \frac{c_i^2}{2 i + 1} + 2 \sum_{j > i} \frac{c_i c_j}{i + j + 1}. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. Returns: float: The :math:`L_2` norm of the polynomial. """ num_coeffs, = coeffs.shape result = 0.0 for i in six.moves.xrange(num_coeffs): coeff_i = coeffs[i] result += coeff_i * coeff_i / (2.0 * i + 1.0) for j in six.moves.xrange(i + 1, num_coeffs): coeff_j = coeffs[j] result += 2.0 * coeff_i * coeff_j / (i + j + 1.0) return np.sqrt(result)
[ "def", "polynomial_norm", "(", "coeffs", ")", ":", "num_coeffs", ",", "=", "coeffs", ".", "shape", "result", "=", "0.0", "for", "i", "in", "six", ".", "moves", ".", "xrange", "(", "num_coeffs", ")", ":", "coeff_i", "=", "coeffs", "[", "i", "]", "result", "+=", "coeff_i", "*", "coeff_i", "/", "(", "2.0", "*", "i", "+", "1.0", ")", "for", "j", "in", "six", ".", "moves", ".", "xrange", "(", "i", "+", "1", ",", "num_coeffs", ")", ":", "coeff_j", "=", "coeffs", "[", "j", "]", "result", "+=", "2.0", "*", "coeff_i", "*", "coeff_j", "/", "(", "i", "+", "j", "+", "1.0", ")", "return", "np", ".", "sqrt", "(", "result", ")" ]
r"""Computes :math:`L_2` norm of polynomial on :math:`\left[0, 1\right]`. We have .. math:: \left\langle f, f \right\rangle = \sum_{i, j} \int_0^1 c_i c_j x^{i + j} \, dx = \sum_{i, j} \frac{c_i c_j}{i + j + 1} = \sum_{i} \frac{c_i^2}{2 i + 1} + 2 \sum_{j > i} \frac{c_i c_j}{i + j + 1}. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. Returns: float: The :math:`L_2` norm of the polynomial.
[ "r", "Computes", ":", "math", ":", "L_2", "norm", "of", "polynomial", "on", ":", "math", ":", "\\", "left", "[", "0", "1", "\\", "right", "]", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L549-L576
dhermes/bezier
src/bezier/_algebraic_intersection.py
normalize_polynomial
def normalize_polynomial(coeffs, threshold=_L2_THRESHOLD): r"""Normalizes a polynomial in the :math:`L_2` sense. Does so on the interval :math:\left[0, 1\right]` via :func:`polynomial_norm`. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. threshold (Optional[float]): The point :math:`\tau` below which a polynomial will be considered to be numerically equal to zero, applies to all :math:`f` with :math`\| f \|_{L_2} < \tau`. Returns: numpy.ndarray: The normalized polynomial. """ l2_norm = polynomial_norm(coeffs) if l2_norm < threshold: return np.zeros(coeffs.shape, order="F") else: coeffs /= l2_norm return coeffs
python
def normalize_polynomial(coeffs, threshold=_L2_THRESHOLD): r"""Normalizes a polynomial in the :math:`L_2` sense. Does so on the interval :math:\left[0, 1\right]` via :func:`polynomial_norm`. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. threshold (Optional[float]): The point :math:`\tau` below which a polynomial will be considered to be numerically equal to zero, applies to all :math:`f` with :math`\| f \|_{L_2} < \tau`. Returns: numpy.ndarray: The normalized polynomial. """ l2_norm = polynomial_norm(coeffs) if l2_norm < threshold: return np.zeros(coeffs.shape, order="F") else: coeffs /= l2_norm return coeffs
[ "def", "normalize_polynomial", "(", "coeffs", ",", "threshold", "=", "_L2_THRESHOLD", ")", ":", "l2_norm", "=", "polynomial_norm", "(", "coeffs", ")", "if", "l2_norm", "<", "threshold", ":", "return", "np", ".", "zeros", "(", "coeffs", ".", "shape", ",", "order", "=", "\"F\"", ")", "else", ":", "coeffs", "/=", "l2_norm", "return", "coeffs" ]
r"""Normalizes a polynomial in the :math:`L_2` sense. Does so on the interval :math:\left[0, 1\right]` via :func:`polynomial_norm`. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. threshold (Optional[float]): The point :math:`\tau` below which a polynomial will be considered to be numerically equal to zero, applies to all :math:`f` with :math`\| f \|_{L_2} < \tau`. Returns: numpy.ndarray: The normalized polynomial.
[ "r", "Normalizes", "a", "polynomial", "in", "the", ":", "math", ":", "L_2", "sense", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L579-L601
dhermes/bezier
src/bezier/_algebraic_intersection.py
_get_sigma_coeffs
def _get_sigma_coeffs(coeffs): r"""Compute "transformed" form of polynomial in Bernstein form. Makes sure the "transformed" polynomial is monic (i.e. by dividing by the lead coefficient) and just returns the coefficients of the non-lead terms. For example, the polynomial .. math:: f(s) = 4 (1 - s)^2 + 3 \cdot 2 s(1 - s) + 7 s^2 can be transformed into :math:`f(s) = 7 (1 - s)^2 g\left(\sigma\right)` for :math:`\sigma = \frac{s}{1 - s}` and :math:`g(\sigma)` a polynomial in the power basis: .. math:: g(\sigma) = \frac{4}{7} + \frac{6}{7} \sigma + \sigma^2. In cases where terms with "low exponents" of :math:`(1 - s)` have coefficient zero, the degree of :math:`g(\sigma)` may not be the same as the degree of :math:`f(s)`: .. math:: \begin{align*} f(s) &= 5 (1 - s)^4 - 3 \cdot 4 s(1 - s)^3 + 11 \cdot 6 s^2 (1 - s)^2 \\ \Longrightarrow g(\sigma) &= \frac{5}{66} - \frac{2}{11} \sigma + \sigma^2. \end{align*} Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: Tuple[Optional[numpy.ndarray], int, int]: A triple of * 1D array of the transformed coefficients (will be unset if ``effective_degree == 0``) * the "full degree" based on the size of ``coeffs`` * the "effective degree" determined by the number of leading zeros. """ num_nodes, = coeffs.shape degree = num_nodes - 1 effective_degree = None for index in six.moves.range(degree, -1, -1): if coeffs[index] != 0.0: effective_degree = index break if effective_degree is None: # NOTE: This means ``np.all(coeffs == 0.0)``. return None, 0, 0 if effective_degree == 0: return None, degree, 0 sigma_coeffs = coeffs[:effective_degree] / coeffs[effective_degree] # Now we need to add the binomial coefficients, but we avoid # computing actual binomial coefficients. Starting from the largest # exponent, the first ratio of binomial coefficients is # (d C (e - 1)) / (d C e) # = e / (d - e + 1) binom_numerator = effective_degree binom_denominator = degree - effective_degree + 1 for exponent in six.moves.xrange(effective_degree - 1, -1, -1): sigma_coeffs[exponent] *= binom_numerator sigma_coeffs[exponent] /= binom_denominator # We swap (d C j) with (d C (j - 1)), so `p / q` becomes # (p / q) (d C (j - 1)) / (d C j) # = (p j) / (q (d - j + 1)) binom_numerator *= exponent binom_denominator *= degree - exponent + 1 return sigma_coeffs, degree, effective_degree
python
def _get_sigma_coeffs(coeffs): r"""Compute "transformed" form of polynomial in Bernstein form. Makes sure the "transformed" polynomial is monic (i.e. by dividing by the lead coefficient) and just returns the coefficients of the non-lead terms. For example, the polynomial .. math:: f(s) = 4 (1 - s)^2 + 3 \cdot 2 s(1 - s) + 7 s^2 can be transformed into :math:`f(s) = 7 (1 - s)^2 g\left(\sigma\right)` for :math:`\sigma = \frac{s}{1 - s}` and :math:`g(\sigma)` a polynomial in the power basis: .. math:: g(\sigma) = \frac{4}{7} + \frac{6}{7} \sigma + \sigma^2. In cases where terms with "low exponents" of :math:`(1 - s)` have coefficient zero, the degree of :math:`g(\sigma)` may not be the same as the degree of :math:`f(s)`: .. math:: \begin{align*} f(s) &= 5 (1 - s)^4 - 3 \cdot 4 s(1 - s)^3 + 11 \cdot 6 s^2 (1 - s)^2 \\ \Longrightarrow g(\sigma) &= \frac{5}{66} - \frac{2}{11} \sigma + \sigma^2. \end{align*} Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: Tuple[Optional[numpy.ndarray], int, int]: A triple of * 1D array of the transformed coefficients (will be unset if ``effective_degree == 0``) * the "full degree" based on the size of ``coeffs`` * the "effective degree" determined by the number of leading zeros. """ num_nodes, = coeffs.shape degree = num_nodes - 1 effective_degree = None for index in six.moves.range(degree, -1, -1): if coeffs[index] != 0.0: effective_degree = index break if effective_degree is None: # NOTE: This means ``np.all(coeffs == 0.0)``. return None, 0, 0 if effective_degree == 0: return None, degree, 0 sigma_coeffs = coeffs[:effective_degree] / coeffs[effective_degree] # Now we need to add the binomial coefficients, but we avoid # computing actual binomial coefficients. Starting from the largest # exponent, the first ratio of binomial coefficients is # (d C (e - 1)) / (d C e) # = e / (d - e + 1) binom_numerator = effective_degree binom_denominator = degree - effective_degree + 1 for exponent in six.moves.xrange(effective_degree - 1, -1, -1): sigma_coeffs[exponent] *= binom_numerator sigma_coeffs[exponent] /= binom_denominator # We swap (d C j) with (d C (j - 1)), so `p / q` becomes # (p / q) (d C (j - 1)) / (d C j) # = (p j) / (q (d - j + 1)) binom_numerator *= exponent binom_denominator *= degree - exponent + 1 return sigma_coeffs, degree, effective_degree
[ "def", "_get_sigma_coeffs", "(", "coeffs", ")", ":", "num_nodes", ",", "=", "coeffs", ".", "shape", "degree", "=", "num_nodes", "-", "1", "effective_degree", "=", "None", "for", "index", "in", "six", ".", "moves", ".", "range", "(", "degree", ",", "-", "1", ",", "-", "1", ")", ":", "if", "coeffs", "[", "index", "]", "!=", "0.0", ":", "effective_degree", "=", "index", "break", "if", "effective_degree", "is", "None", ":", "# NOTE: This means ``np.all(coeffs == 0.0)``.", "return", "None", ",", "0", ",", "0", "if", "effective_degree", "==", "0", ":", "return", "None", ",", "degree", ",", "0", "sigma_coeffs", "=", "coeffs", "[", ":", "effective_degree", "]", "/", "coeffs", "[", "effective_degree", "]", "# Now we need to add the binomial coefficients, but we avoid", "# computing actual binomial coefficients. Starting from the largest", "# exponent, the first ratio of binomial coefficients is", "# (d C (e - 1)) / (d C e)", "# = e / (d - e + 1)", "binom_numerator", "=", "effective_degree", "binom_denominator", "=", "degree", "-", "effective_degree", "+", "1", "for", "exponent", "in", "six", ".", "moves", ".", "xrange", "(", "effective_degree", "-", "1", ",", "-", "1", ",", "-", "1", ")", ":", "sigma_coeffs", "[", "exponent", "]", "*=", "binom_numerator", "sigma_coeffs", "[", "exponent", "]", "/=", "binom_denominator", "# We swap (d C j) with (d C (j - 1)), so `p / q` becomes", "# (p / q) (d C (j - 1)) / (d C j)", "# = (p j) / (q (d - j + 1))", "binom_numerator", "*=", "exponent", "binom_denominator", "*=", "degree", "-", "exponent", "+", "1", "return", "sigma_coeffs", ",", "degree", ",", "effective_degree" ]
r"""Compute "transformed" form of polynomial in Bernstein form. Makes sure the "transformed" polynomial is monic (i.e. by dividing by the lead coefficient) and just returns the coefficients of the non-lead terms. For example, the polynomial .. math:: f(s) = 4 (1 - s)^2 + 3 \cdot 2 s(1 - s) + 7 s^2 can be transformed into :math:`f(s) = 7 (1 - s)^2 g\left(\sigma\right)` for :math:`\sigma = \frac{s}{1 - s}` and :math:`g(\sigma)` a polynomial in the power basis: .. math:: g(\sigma) = \frac{4}{7} + \frac{6}{7} \sigma + \sigma^2. In cases where terms with "low exponents" of :math:`(1 - s)` have coefficient zero, the degree of :math:`g(\sigma)` may not be the same as the degree of :math:`f(s)`: .. math:: \begin{align*} f(s) &= 5 (1 - s)^4 - 3 \cdot 4 s(1 - s)^3 + 11 \cdot 6 s^2 (1 - s)^2 \\ \Longrightarrow g(\sigma) &= \frac{5}{66} - \frac{2}{11} \sigma + \sigma^2. \end{align*} Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: Tuple[Optional[numpy.ndarray], int, int]: A triple of * 1D array of the transformed coefficients (will be unset if ``effective_degree == 0``) * the "full degree" based on the size of ``coeffs`` * the "effective degree" determined by the number of leading zeros.
[ "r", "Compute", "transformed", "form", "of", "polynomial", "in", "Bernstein", "form", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L604-L679
dhermes/bezier
src/bezier/_algebraic_intersection.py
bernstein_companion
def bernstein_companion(coeffs): r"""Compute a companion matrix for a polynomial in Bernstein basis. .. note:: This assumes the caller passes in a 1D array but does not check. This takes the polynomial .. math:: f(s) = \sum_{j = 0}^n b_{j, n} \cdot C_j. and uses the variable :math:`\sigma = \frac{s}{1 - s}` to rewrite as .. math:: f(s) = (1 - s)^n \sum_{j = 0}^n \binom{n}{j} C_j \sigma^j. This converts the Bernstein coefficients :math:`C_j` into "generalized Bernstein" coefficients :math:`\widetilde{C_j} = \binom{n}{j} C_j`. Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: Tuple[numpy.ndarray, int, int]: A triple of * 2D NumPy array with the companion matrix. * the "full degree" based on the size of ``coeffs`` * the "effective degree" determined by the number of leading zeros. """ sigma_coeffs, degree, effective_degree = _get_sigma_coeffs(coeffs) if effective_degree == 0: return np.empty((0, 0), order="F"), degree, 0 companion = np.zeros((effective_degree, effective_degree), order="F") companion.flat[ effective_degree :: effective_degree + 1 # noqa: E203 ] = 1.0 companion[0, :] = -sigma_coeffs[::-1] return companion, degree, effective_degree
python
def bernstein_companion(coeffs): r"""Compute a companion matrix for a polynomial in Bernstein basis. .. note:: This assumes the caller passes in a 1D array but does not check. This takes the polynomial .. math:: f(s) = \sum_{j = 0}^n b_{j, n} \cdot C_j. and uses the variable :math:`\sigma = \frac{s}{1 - s}` to rewrite as .. math:: f(s) = (1 - s)^n \sum_{j = 0}^n \binom{n}{j} C_j \sigma^j. This converts the Bernstein coefficients :math:`C_j` into "generalized Bernstein" coefficients :math:`\widetilde{C_j} = \binom{n}{j} C_j`. Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: Tuple[numpy.ndarray, int, int]: A triple of * 2D NumPy array with the companion matrix. * the "full degree" based on the size of ``coeffs`` * the "effective degree" determined by the number of leading zeros. """ sigma_coeffs, degree, effective_degree = _get_sigma_coeffs(coeffs) if effective_degree == 0: return np.empty((0, 0), order="F"), degree, 0 companion = np.zeros((effective_degree, effective_degree), order="F") companion.flat[ effective_degree :: effective_degree + 1 # noqa: E203 ] = 1.0 companion[0, :] = -sigma_coeffs[::-1] return companion, degree, effective_degree
[ "def", "bernstein_companion", "(", "coeffs", ")", ":", "sigma_coeffs", ",", "degree", ",", "effective_degree", "=", "_get_sigma_coeffs", "(", "coeffs", ")", "if", "effective_degree", "==", "0", ":", "return", "np", ".", "empty", "(", "(", "0", ",", "0", ")", ",", "order", "=", "\"F\"", ")", ",", "degree", ",", "0", "companion", "=", "np", ".", "zeros", "(", "(", "effective_degree", ",", "effective_degree", ")", ",", "order", "=", "\"F\"", ")", "companion", ".", "flat", "[", "effective_degree", ":", ":", "effective_degree", "+", "1", "# noqa: E203", "]", "=", "1.0", "companion", "[", "0", ",", ":", "]", "=", "-", "sigma_coeffs", "[", ":", ":", "-", "1", "]", "return", "companion", ",", "degree", ",", "effective_degree" ]
r"""Compute a companion matrix for a polynomial in Bernstein basis. .. note:: This assumes the caller passes in a 1D array but does not check. This takes the polynomial .. math:: f(s) = \sum_{j = 0}^n b_{j, n} \cdot C_j. and uses the variable :math:`\sigma = \frac{s}{1 - s}` to rewrite as .. math:: f(s) = (1 - s)^n \sum_{j = 0}^n \binom{n}{j} C_j \sigma^j. This converts the Bernstein coefficients :math:`C_j` into "generalized Bernstein" coefficients :math:`\widetilde{C_j} = \binom{n}{j} C_j`. Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: Tuple[numpy.ndarray, int, int]: A triple of * 2D NumPy array with the companion matrix. * the "full degree" based on the size of ``coeffs`` * the "effective degree" determined by the number of leading zeros.
[ "r", "Compute", "a", "companion", "matrix", "for", "a", "polynomial", "in", "Bernstein", "basis", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L682-L724
dhermes/bezier
src/bezier/_algebraic_intersection.py
bezier_roots
def bezier_roots(coeffs): r"""Compute polynomial roots from a polynomial in the Bernstein basis. .. note:: This assumes the caller passes in a 1D array but does not check. This takes the polynomial .. math:: f(s) = \sum_{j = 0}^n b_{j, n} \cdot C_j. and uses the variable :math:`\sigma = \frac{s}{1 - s}` to rewrite as .. math:: \begin{align*} f(s) &= (1 - s)^n \sum_{j = 0}^n \binom{n}{j} C_j \sigma^j \\ &= (1 - s)^n \sum_{j = 0}^n \widetilde{C_j} \sigma^j. \end{align*} Then it uses an eigenvalue solver to find the roots of .. math:: g(\sigma) = \sum_{j = 0}^n \widetilde{C_j} \sigma^j and convert them back into roots of :math:`f(s)` via :math:`s = \frac{\sigma}{1 + \sigma}`. For example, consider .. math:: \begin{align*} f_0(s) &= 2 (2 - s)(3 + s) \\ &= 12(1 - s)^2 + 11 \cdot 2s(1 - s) + 8 s^2 \end{align*} First, we compute the companion matrix for .. math:: g_0(\sigma) = 12 + 22 \sigma + 8 \sigma^2 .. testsetup:: bezier-roots0, bezier-roots1, bezier-roots2 import numpy as np import numpy.linalg from bezier._algebraic_intersection import bernstein_companion from bezier._algebraic_intersection import bezier_roots .. doctest:: bezier-roots0 >>> coeffs0 = np.asfortranarray([12.0, 11.0, 8.0]) >>> companion0, _, _ = bernstein_companion(coeffs0) >>> companion0 array([[-2.75, -1.5 ], [ 1. , 0. ]]) then take the eigenvalues of the companion matrix: .. doctest:: bezier-roots0 >>> sigma_values0 = np.linalg.eigvals(companion0) >>> sigma_values0 array([-2. , -0.75]) after transforming them, we have the roots of :math:`f(s)`: .. doctest:: bezier-roots0 >>> sigma_values0 / (1.0 + sigma_values0) array([ 2., -3.]) >>> bezier_roots(coeffs0) array([ 2., -3.]) In cases where :math:`s = 1` is a root, the lead coefficient of :math:`g` would be :math:`0`, so there is a reduction in the companion matrix. .. math:: \begin{align*} f_1(s) &= 6 (s - 1)^2 (s - 3) (s - 5) \\ &= 90 (1 - s)^4 + 33 \cdot 4s(1 - s)^3 + 8 \cdot 6s^2(1 - s)^2 \end{align*} .. doctest:: bezier-roots1 :options: +NORMALIZE_WHITESPACE >>> coeffs1 = np.asfortranarray([90.0, 33.0, 8.0, 0.0, 0.0]) >>> companion1, degree1, effective_degree1 = bernstein_companion( ... coeffs1) >>> companion1 array([[-2.75 , -1.875], [ 1. , 0. ]]) >>> degree1 4 >>> effective_degree1 2 so the roots are a combination of the roots determined from :math:`s = \frac{\sigma}{1 + \sigma}` and the number of factors of :math:`(1 - s)` (i.e. the difference between the degree and the effective degree): .. doctest:: bezier-roots1 >>> bezier_roots(coeffs1) array([3., 5., 1., 1.]) In some cases, a polynomial is represented with an "elevated" degree: .. math:: \begin{align*} f_2(s) &= 3 (s^2 + 1) \\ &= 3 (1 - s)^3 + 3 \cdot 3s(1 - s)^2 + 4 \cdot 3s^2(1 - s) + 6 s^3 \end{align*} This results in a "point at infinity" :math:`\sigma = -1 \Longleftrightarrow s = \infty`: .. doctest:: bezier-roots2 >>> coeffs2 = np.asfortranarray([3.0, 3.0, 4.0, 6.0]) >>> companion2, _, _ = bernstein_companion(coeffs2) >>> companion2 array([[-2. , -1.5, -0.5], [ 1. , 0. , 0. ], [ 0. , 1. , 0. ]]) >>> sigma_values2 = np.linalg.eigvals(companion2) >>> sigma_values2 array([-1. +0.j , -0.5+0.5j, -0.5-0.5j]) so we drop any values :math:`\sigma` that are sufficiently close to :math:`-1`: .. doctest:: bezier-roots2 >>> expected2 = np.asfortranarray([1.0j, -1.0j]) >>> roots2 = bezier_roots(coeffs2) >>> np.allclose(expected2, roots2, rtol=2e-15, atol=0.0) True Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: numpy.ndarray: A 1D array containing the roots. """ companion, degree, effective_degree = bernstein_companion(coeffs) if effective_degree: sigma_roots = np.linalg.eigvals(companion) # Filter out `sigma = -1`, i.e. "points at infinity". # We want the error ||(sigma - (-1))|| ~= 2^{-52} to_keep = np.abs(sigma_roots + 1.0) > _SIGMA_THRESHOLD sigma_roots = sigma_roots[to_keep] s_vals = sigma_roots / (1.0 + sigma_roots) else: s_vals = np.empty((0,), order="F") if effective_degree != degree: delta = degree - effective_degree s_vals = np.hstack([s_vals, [1] * delta]) return s_vals
python
def bezier_roots(coeffs): r"""Compute polynomial roots from a polynomial in the Bernstein basis. .. note:: This assumes the caller passes in a 1D array but does not check. This takes the polynomial .. math:: f(s) = \sum_{j = 0}^n b_{j, n} \cdot C_j. and uses the variable :math:`\sigma = \frac{s}{1 - s}` to rewrite as .. math:: \begin{align*} f(s) &= (1 - s)^n \sum_{j = 0}^n \binom{n}{j} C_j \sigma^j \\ &= (1 - s)^n \sum_{j = 0}^n \widetilde{C_j} \sigma^j. \end{align*} Then it uses an eigenvalue solver to find the roots of .. math:: g(\sigma) = \sum_{j = 0}^n \widetilde{C_j} \sigma^j and convert them back into roots of :math:`f(s)` via :math:`s = \frac{\sigma}{1 + \sigma}`. For example, consider .. math:: \begin{align*} f_0(s) &= 2 (2 - s)(3 + s) \\ &= 12(1 - s)^2 + 11 \cdot 2s(1 - s) + 8 s^2 \end{align*} First, we compute the companion matrix for .. math:: g_0(\sigma) = 12 + 22 \sigma + 8 \sigma^2 .. testsetup:: bezier-roots0, bezier-roots1, bezier-roots2 import numpy as np import numpy.linalg from bezier._algebraic_intersection import bernstein_companion from bezier._algebraic_intersection import bezier_roots .. doctest:: bezier-roots0 >>> coeffs0 = np.asfortranarray([12.0, 11.0, 8.0]) >>> companion0, _, _ = bernstein_companion(coeffs0) >>> companion0 array([[-2.75, -1.5 ], [ 1. , 0. ]]) then take the eigenvalues of the companion matrix: .. doctest:: bezier-roots0 >>> sigma_values0 = np.linalg.eigvals(companion0) >>> sigma_values0 array([-2. , -0.75]) after transforming them, we have the roots of :math:`f(s)`: .. doctest:: bezier-roots0 >>> sigma_values0 / (1.0 + sigma_values0) array([ 2., -3.]) >>> bezier_roots(coeffs0) array([ 2., -3.]) In cases where :math:`s = 1` is a root, the lead coefficient of :math:`g` would be :math:`0`, so there is a reduction in the companion matrix. .. math:: \begin{align*} f_1(s) &= 6 (s - 1)^2 (s - 3) (s - 5) \\ &= 90 (1 - s)^4 + 33 \cdot 4s(1 - s)^3 + 8 \cdot 6s^2(1 - s)^2 \end{align*} .. doctest:: bezier-roots1 :options: +NORMALIZE_WHITESPACE >>> coeffs1 = np.asfortranarray([90.0, 33.0, 8.0, 0.0, 0.0]) >>> companion1, degree1, effective_degree1 = bernstein_companion( ... coeffs1) >>> companion1 array([[-2.75 , -1.875], [ 1. , 0. ]]) >>> degree1 4 >>> effective_degree1 2 so the roots are a combination of the roots determined from :math:`s = \frac{\sigma}{1 + \sigma}` and the number of factors of :math:`(1 - s)` (i.e. the difference between the degree and the effective degree): .. doctest:: bezier-roots1 >>> bezier_roots(coeffs1) array([3., 5., 1., 1.]) In some cases, a polynomial is represented with an "elevated" degree: .. math:: \begin{align*} f_2(s) &= 3 (s^2 + 1) \\ &= 3 (1 - s)^3 + 3 \cdot 3s(1 - s)^2 + 4 \cdot 3s^2(1 - s) + 6 s^3 \end{align*} This results in a "point at infinity" :math:`\sigma = -1 \Longleftrightarrow s = \infty`: .. doctest:: bezier-roots2 >>> coeffs2 = np.asfortranarray([3.0, 3.0, 4.0, 6.0]) >>> companion2, _, _ = bernstein_companion(coeffs2) >>> companion2 array([[-2. , -1.5, -0.5], [ 1. , 0. , 0. ], [ 0. , 1. , 0. ]]) >>> sigma_values2 = np.linalg.eigvals(companion2) >>> sigma_values2 array([-1. +0.j , -0.5+0.5j, -0.5-0.5j]) so we drop any values :math:`\sigma` that are sufficiently close to :math:`-1`: .. doctest:: bezier-roots2 >>> expected2 = np.asfortranarray([1.0j, -1.0j]) >>> roots2 = bezier_roots(coeffs2) >>> np.allclose(expected2, roots2, rtol=2e-15, atol=0.0) True Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: numpy.ndarray: A 1D array containing the roots. """ companion, degree, effective_degree = bernstein_companion(coeffs) if effective_degree: sigma_roots = np.linalg.eigvals(companion) # Filter out `sigma = -1`, i.e. "points at infinity". # We want the error ||(sigma - (-1))|| ~= 2^{-52} to_keep = np.abs(sigma_roots + 1.0) > _SIGMA_THRESHOLD sigma_roots = sigma_roots[to_keep] s_vals = sigma_roots / (1.0 + sigma_roots) else: s_vals = np.empty((0,), order="F") if effective_degree != degree: delta = degree - effective_degree s_vals = np.hstack([s_vals, [1] * delta]) return s_vals
[ "def", "bezier_roots", "(", "coeffs", ")", ":", "companion", ",", "degree", ",", "effective_degree", "=", "bernstein_companion", "(", "coeffs", ")", "if", "effective_degree", ":", "sigma_roots", "=", "np", ".", "linalg", ".", "eigvals", "(", "companion", ")", "# Filter out `sigma = -1`, i.e. \"points at infinity\".", "# We want the error ||(sigma - (-1))|| ~= 2^{-52}", "to_keep", "=", "np", ".", "abs", "(", "sigma_roots", "+", "1.0", ")", ">", "_SIGMA_THRESHOLD", "sigma_roots", "=", "sigma_roots", "[", "to_keep", "]", "s_vals", "=", "sigma_roots", "/", "(", "1.0", "+", "sigma_roots", ")", "else", ":", "s_vals", "=", "np", ".", "empty", "(", "(", "0", ",", ")", ",", "order", "=", "\"F\"", ")", "if", "effective_degree", "!=", "degree", ":", "delta", "=", "degree", "-", "effective_degree", "s_vals", "=", "np", ".", "hstack", "(", "[", "s_vals", ",", "[", "1", "]", "*", "delta", "]", ")", "return", "s_vals" ]
r"""Compute polynomial roots from a polynomial in the Bernstein basis. .. note:: This assumes the caller passes in a 1D array but does not check. This takes the polynomial .. math:: f(s) = \sum_{j = 0}^n b_{j, n} \cdot C_j. and uses the variable :math:`\sigma = \frac{s}{1 - s}` to rewrite as .. math:: \begin{align*} f(s) &= (1 - s)^n \sum_{j = 0}^n \binom{n}{j} C_j \sigma^j \\ &= (1 - s)^n \sum_{j = 0}^n \widetilde{C_j} \sigma^j. \end{align*} Then it uses an eigenvalue solver to find the roots of .. math:: g(\sigma) = \sum_{j = 0}^n \widetilde{C_j} \sigma^j and convert them back into roots of :math:`f(s)` via :math:`s = \frac{\sigma}{1 + \sigma}`. For example, consider .. math:: \begin{align*} f_0(s) &= 2 (2 - s)(3 + s) \\ &= 12(1 - s)^2 + 11 \cdot 2s(1 - s) + 8 s^2 \end{align*} First, we compute the companion matrix for .. math:: g_0(\sigma) = 12 + 22 \sigma + 8 \sigma^2 .. testsetup:: bezier-roots0, bezier-roots1, bezier-roots2 import numpy as np import numpy.linalg from bezier._algebraic_intersection import bernstein_companion from bezier._algebraic_intersection import bezier_roots .. doctest:: bezier-roots0 >>> coeffs0 = np.asfortranarray([12.0, 11.0, 8.0]) >>> companion0, _, _ = bernstein_companion(coeffs0) >>> companion0 array([[-2.75, -1.5 ], [ 1. , 0. ]]) then take the eigenvalues of the companion matrix: .. doctest:: bezier-roots0 >>> sigma_values0 = np.linalg.eigvals(companion0) >>> sigma_values0 array([-2. , -0.75]) after transforming them, we have the roots of :math:`f(s)`: .. doctest:: bezier-roots0 >>> sigma_values0 / (1.0 + sigma_values0) array([ 2., -3.]) >>> bezier_roots(coeffs0) array([ 2., -3.]) In cases where :math:`s = 1` is a root, the lead coefficient of :math:`g` would be :math:`0`, so there is a reduction in the companion matrix. .. math:: \begin{align*} f_1(s) &= 6 (s - 1)^2 (s - 3) (s - 5) \\ &= 90 (1 - s)^4 + 33 \cdot 4s(1 - s)^3 + 8 \cdot 6s^2(1 - s)^2 \end{align*} .. doctest:: bezier-roots1 :options: +NORMALIZE_WHITESPACE >>> coeffs1 = np.asfortranarray([90.0, 33.0, 8.0, 0.0, 0.0]) >>> companion1, degree1, effective_degree1 = bernstein_companion( ... coeffs1) >>> companion1 array([[-2.75 , -1.875], [ 1. , 0. ]]) >>> degree1 4 >>> effective_degree1 2 so the roots are a combination of the roots determined from :math:`s = \frac{\sigma}{1 + \sigma}` and the number of factors of :math:`(1 - s)` (i.e. the difference between the degree and the effective degree): .. doctest:: bezier-roots1 >>> bezier_roots(coeffs1) array([3., 5., 1., 1.]) In some cases, a polynomial is represented with an "elevated" degree: .. math:: \begin{align*} f_2(s) &= 3 (s^2 + 1) \\ &= 3 (1 - s)^3 + 3 \cdot 3s(1 - s)^2 + 4 \cdot 3s^2(1 - s) + 6 s^3 \end{align*} This results in a "point at infinity" :math:`\sigma = -1 \Longleftrightarrow s = \infty`: .. doctest:: bezier-roots2 >>> coeffs2 = np.asfortranarray([3.0, 3.0, 4.0, 6.0]) >>> companion2, _, _ = bernstein_companion(coeffs2) >>> companion2 array([[-2. , -1.5, -0.5], [ 1. , 0. , 0. ], [ 0. , 1. , 0. ]]) >>> sigma_values2 = np.linalg.eigvals(companion2) >>> sigma_values2 array([-1. +0.j , -0.5+0.5j, -0.5-0.5j]) so we drop any values :math:`\sigma` that are sufficiently close to :math:`-1`: .. doctest:: bezier-roots2 >>> expected2 = np.asfortranarray([1.0j, -1.0j]) >>> roots2 = bezier_roots(coeffs2) >>> np.allclose(expected2, roots2, rtol=2e-15, atol=0.0) True Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: numpy.ndarray: A 1D array containing the roots.
[ "r", "Compute", "polynomial", "roots", "from", "a", "polynomial", "in", "the", "Bernstein", "basis", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L727-L896
dhermes/bezier
src/bezier/_algebraic_intersection.py
lu_companion
def lu_companion(top_row, value): r"""Compute an LU-factored :math:`C - t I` and its 1-norm. .. _dgecon: http://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga188b8d30443d14b1a3f7f8331d87ae60.html#ga188b8d30443d14b1a3f7f8331d87ae60 .. _dgetrf: http://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga0019443faea08275ca60a734d0593e60.html#ga0019443faea08275ca60a734d0593e60 .. note:: The output of this function is intended to be used with `dgecon`_ from LAPACK. ``dgecon`` expects both the 1-norm of the matrix and expects the matrix to be passed in an already LU-factored form (via `dgetrf`_). The companion matrix :math:`C` is given by the ``top_row``, for example, the polynomial :math:`t^3 + 3 t^2 - t + 2` has a top row of ``-3, 1, -2`` and the corresponding companion matrix is: .. math:: \left[\begin{array}{c c c} -3 & 1 & -2 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{array}\right] After doing a full cycle of the rows (shifting the first to the last and moving all other rows up), row reduction of :math:`C - t I` yields .. math:: \left[\begin{array}{c c c} 1 & -t & 0 \\ 0 & 1 & -t \\ -3 - t & 1 & -2 \end{array}\right] = \left[\begin{array}{c c c} 1 & 0 & 0 \\ 0 & 1 & 0 \\ -3 - t & 1 + t(-3 - t) & 1 \end{array}\right] \left[\begin{array}{c c c} 1 & -t & 0 \\ 0 & 1 & -t \\ 0 & 0 & -2 + t(1 + t(-3 - t)) \end{array}\right] and in general, the terms in the bottom row correspond to the intermediate values involved in evaluating the polynomial via `Horner's method`_. .. _Horner's method: https://en.wikipedia.org/wiki/Horner%27s_method .. testsetup:: lu-companion import numpy as np import numpy.linalg from bezier._algebraic_intersection import lu_companion .. doctest:: lu-companion >>> top_row = np.asfortranarray([-3.0, 1.0, -2.0]) >>> t_val = 0.5 >>> lu_mat, one_norm = lu_companion(top_row, t_val) >>> lu_mat array([[ 1. , -0.5 , 0. ], [ 0. , 1. , -0.5 ], [-3.5 , -0.75 , -2.375]]) >>> one_norm 4.5 >>> l_mat = np.tril(lu_mat, k=-1) + np.eye(3) >>> u_mat = np.triu(lu_mat) >>> a_mat = l_mat.dot(u_mat) >>> a_mat array([[ 1. , -0.5, 0. ], [ 0. , 1. , -0.5], [-3.5, 1. , -2. ]]) >>> np.linalg.norm(a_mat, ord=1) 4.5 Args: top_row (numpy.ndarray): 1D array, top row of companion matrix. value (float): The :math:`t` value used to form :math:`C - t I`. Returns: Tuple[numpy.ndarray, float]: Pair of * 2D array of LU-factored form of :math:`C - t I`, with the non-diagonal part of :math:`L` stored in the strictly lower triangle and :math:`U` stored in the upper triangle (we skip the permutation matrix, as it won't impact the 1-norm) * the 1-norm the matrix :math:`C - t I` As mentioned above, these two values are meant to be used with `dgecon`_. """ degree, = top_row.shape lu_mat = np.zeros((degree, degree), order="F") if degree == 1: lu_mat[0, 0] = top_row[0] - value return lu_mat, abs(lu_mat[0, 0]) # Column 0: Special case since it doesn't have ``-t`` above the diagonal. horner_curr = top_row[0] - value one_norm = 1.0 + abs(horner_curr) lu_mat[0, 0] = 1.0 lu_mat[degree - 1, 0] = horner_curr # Columns 1-(end - 1): Three values in LU and C - t I. abs_one_plus = 1.0 + abs(value) last_row = degree - 1 for col in six.moves.xrange(1, degree - 1): curr_coeff = top_row[col] horner_curr = value * horner_curr + curr_coeff one_norm = max(one_norm, abs_one_plus + abs(curr_coeff)) lu_mat[col - 1, col] = -value lu_mat[col, col] = 1.0 lu_mat[last_row, col] = horner_curr # Last Column: Special case since it doesn't have ``-1`` on the diagonal. curr_coeff = top_row[last_row] horner_curr = value * horner_curr + curr_coeff one_norm = max(one_norm, abs(value) + abs(curr_coeff)) lu_mat[last_row - 1, last_row] = -value lu_mat[last_row, last_row] = horner_curr return lu_mat, one_norm
python
def lu_companion(top_row, value): r"""Compute an LU-factored :math:`C - t I` and its 1-norm. .. _dgecon: http://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga188b8d30443d14b1a3f7f8331d87ae60.html#ga188b8d30443d14b1a3f7f8331d87ae60 .. _dgetrf: http://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga0019443faea08275ca60a734d0593e60.html#ga0019443faea08275ca60a734d0593e60 .. note:: The output of this function is intended to be used with `dgecon`_ from LAPACK. ``dgecon`` expects both the 1-norm of the matrix and expects the matrix to be passed in an already LU-factored form (via `dgetrf`_). The companion matrix :math:`C` is given by the ``top_row``, for example, the polynomial :math:`t^3 + 3 t^2 - t + 2` has a top row of ``-3, 1, -2`` and the corresponding companion matrix is: .. math:: \left[\begin{array}{c c c} -3 & 1 & -2 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{array}\right] After doing a full cycle of the rows (shifting the first to the last and moving all other rows up), row reduction of :math:`C - t I` yields .. math:: \left[\begin{array}{c c c} 1 & -t & 0 \\ 0 & 1 & -t \\ -3 - t & 1 & -2 \end{array}\right] = \left[\begin{array}{c c c} 1 & 0 & 0 \\ 0 & 1 & 0 \\ -3 - t & 1 + t(-3 - t) & 1 \end{array}\right] \left[\begin{array}{c c c} 1 & -t & 0 \\ 0 & 1 & -t \\ 0 & 0 & -2 + t(1 + t(-3 - t)) \end{array}\right] and in general, the terms in the bottom row correspond to the intermediate values involved in evaluating the polynomial via `Horner's method`_. .. _Horner's method: https://en.wikipedia.org/wiki/Horner%27s_method .. testsetup:: lu-companion import numpy as np import numpy.linalg from bezier._algebraic_intersection import lu_companion .. doctest:: lu-companion >>> top_row = np.asfortranarray([-3.0, 1.0, -2.0]) >>> t_val = 0.5 >>> lu_mat, one_norm = lu_companion(top_row, t_val) >>> lu_mat array([[ 1. , -0.5 , 0. ], [ 0. , 1. , -0.5 ], [-3.5 , -0.75 , -2.375]]) >>> one_norm 4.5 >>> l_mat = np.tril(lu_mat, k=-1) + np.eye(3) >>> u_mat = np.triu(lu_mat) >>> a_mat = l_mat.dot(u_mat) >>> a_mat array([[ 1. , -0.5, 0. ], [ 0. , 1. , -0.5], [-3.5, 1. , -2. ]]) >>> np.linalg.norm(a_mat, ord=1) 4.5 Args: top_row (numpy.ndarray): 1D array, top row of companion matrix. value (float): The :math:`t` value used to form :math:`C - t I`. Returns: Tuple[numpy.ndarray, float]: Pair of * 2D array of LU-factored form of :math:`C - t I`, with the non-diagonal part of :math:`L` stored in the strictly lower triangle and :math:`U` stored in the upper triangle (we skip the permutation matrix, as it won't impact the 1-norm) * the 1-norm the matrix :math:`C - t I` As mentioned above, these two values are meant to be used with `dgecon`_. """ degree, = top_row.shape lu_mat = np.zeros((degree, degree), order="F") if degree == 1: lu_mat[0, 0] = top_row[0] - value return lu_mat, abs(lu_mat[0, 0]) # Column 0: Special case since it doesn't have ``-t`` above the diagonal. horner_curr = top_row[0] - value one_norm = 1.0 + abs(horner_curr) lu_mat[0, 0] = 1.0 lu_mat[degree - 1, 0] = horner_curr # Columns 1-(end - 1): Three values in LU and C - t I. abs_one_plus = 1.0 + abs(value) last_row = degree - 1 for col in six.moves.xrange(1, degree - 1): curr_coeff = top_row[col] horner_curr = value * horner_curr + curr_coeff one_norm = max(one_norm, abs_one_plus + abs(curr_coeff)) lu_mat[col - 1, col] = -value lu_mat[col, col] = 1.0 lu_mat[last_row, col] = horner_curr # Last Column: Special case since it doesn't have ``-1`` on the diagonal. curr_coeff = top_row[last_row] horner_curr = value * horner_curr + curr_coeff one_norm = max(one_norm, abs(value) + abs(curr_coeff)) lu_mat[last_row - 1, last_row] = -value lu_mat[last_row, last_row] = horner_curr return lu_mat, one_norm
[ "def", "lu_companion", "(", "top_row", ",", "value", ")", ":", "degree", ",", "=", "top_row", ".", "shape", "lu_mat", "=", "np", ".", "zeros", "(", "(", "degree", ",", "degree", ")", ",", "order", "=", "\"F\"", ")", "if", "degree", "==", "1", ":", "lu_mat", "[", "0", ",", "0", "]", "=", "top_row", "[", "0", "]", "-", "value", "return", "lu_mat", ",", "abs", "(", "lu_mat", "[", "0", ",", "0", "]", ")", "# Column 0: Special case since it doesn't have ``-t`` above the diagonal.", "horner_curr", "=", "top_row", "[", "0", "]", "-", "value", "one_norm", "=", "1.0", "+", "abs", "(", "horner_curr", ")", "lu_mat", "[", "0", ",", "0", "]", "=", "1.0", "lu_mat", "[", "degree", "-", "1", ",", "0", "]", "=", "horner_curr", "# Columns 1-(end - 1): Three values in LU and C - t I.", "abs_one_plus", "=", "1.0", "+", "abs", "(", "value", ")", "last_row", "=", "degree", "-", "1", "for", "col", "in", "six", ".", "moves", ".", "xrange", "(", "1", ",", "degree", "-", "1", ")", ":", "curr_coeff", "=", "top_row", "[", "col", "]", "horner_curr", "=", "value", "*", "horner_curr", "+", "curr_coeff", "one_norm", "=", "max", "(", "one_norm", ",", "abs_one_plus", "+", "abs", "(", "curr_coeff", ")", ")", "lu_mat", "[", "col", "-", "1", ",", "col", "]", "=", "-", "value", "lu_mat", "[", "col", ",", "col", "]", "=", "1.0", "lu_mat", "[", "last_row", ",", "col", "]", "=", "horner_curr", "# Last Column: Special case since it doesn't have ``-1`` on the diagonal.", "curr_coeff", "=", "top_row", "[", "last_row", "]", "horner_curr", "=", "value", "*", "horner_curr", "+", "curr_coeff", "one_norm", "=", "max", "(", "one_norm", ",", "abs", "(", "value", ")", "+", "abs", "(", "curr_coeff", ")", ")", "lu_mat", "[", "last_row", "-", "1", ",", "last_row", "]", "=", "-", "value", "lu_mat", "[", "last_row", ",", "last_row", "]", "=", "horner_curr", "return", "lu_mat", ",", "one_norm" ]
r"""Compute an LU-factored :math:`C - t I` and its 1-norm. .. _dgecon: http://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga188b8d30443d14b1a3f7f8331d87ae60.html#ga188b8d30443d14b1a3f7f8331d87ae60 .. _dgetrf: http://www.netlib.org/lapack/explore-html/dd/d9a/group__double_g_ecomputational_ga0019443faea08275ca60a734d0593e60.html#ga0019443faea08275ca60a734d0593e60 .. note:: The output of this function is intended to be used with `dgecon`_ from LAPACK. ``dgecon`` expects both the 1-norm of the matrix and expects the matrix to be passed in an already LU-factored form (via `dgetrf`_). The companion matrix :math:`C` is given by the ``top_row``, for example, the polynomial :math:`t^3 + 3 t^2 - t + 2` has a top row of ``-3, 1, -2`` and the corresponding companion matrix is: .. math:: \left[\begin{array}{c c c} -3 & 1 & -2 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{array}\right] After doing a full cycle of the rows (shifting the first to the last and moving all other rows up), row reduction of :math:`C - t I` yields .. math:: \left[\begin{array}{c c c} 1 & -t & 0 \\ 0 & 1 & -t \\ -3 - t & 1 & -2 \end{array}\right] = \left[\begin{array}{c c c} 1 & 0 & 0 \\ 0 & 1 & 0 \\ -3 - t & 1 + t(-3 - t) & 1 \end{array}\right] \left[\begin{array}{c c c} 1 & -t & 0 \\ 0 & 1 & -t \\ 0 & 0 & -2 + t(1 + t(-3 - t)) \end{array}\right] and in general, the terms in the bottom row correspond to the intermediate values involved in evaluating the polynomial via `Horner's method`_. .. _Horner's method: https://en.wikipedia.org/wiki/Horner%27s_method .. testsetup:: lu-companion import numpy as np import numpy.linalg from bezier._algebraic_intersection import lu_companion .. doctest:: lu-companion >>> top_row = np.asfortranarray([-3.0, 1.0, -2.0]) >>> t_val = 0.5 >>> lu_mat, one_norm = lu_companion(top_row, t_val) >>> lu_mat array([[ 1. , -0.5 , 0. ], [ 0. , 1. , -0.5 ], [-3.5 , -0.75 , -2.375]]) >>> one_norm 4.5 >>> l_mat = np.tril(lu_mat, k=-1) + np.eye(3) >>> u_mat = np.triu(lu_mat) >>> a_mat = l_mat.dot(u_mat) >>> a_mat array([[ 1. , -0.5, 0. ], [ 0. , 1. , -0.5], [-3.5, 1. , -2. ]]) >>> np.linalg.norm(a_mat, ord=1) 4.5 Args: top_row (numpy.ndarray): 1D array, top row of companion matrix. value (float): The :math:`t` value used to form :math:`C - t I`. Returns: Tuple[numpy.ndarray, float]: Pair of * 2D array of LU-factored form of :math:`C - t I`, with the non-diagonal part of :math:`L` stored in the strictly lower triangle and :math:`U` stored in the upper triangle (we skip the permutation matrix, as it won't impact the 1-norm) * the 1-norm the matrix :math:`C - t I` As mentioned above, these two values are meant to be used with `dgecon`_.
[ "r", "Compute", "an", "LU", "-", "factored", ":", "math", ":", "C", "-", "t", "I", "and", "its", "1", "-", "norm", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L899-L1023
dhermes/bezier
src/bezier/_algebraic_intersection.py
_reciprocal_condition_number
def _reciprocal_condition_number(lu_mat, one_norm): r"""Compute reciprocal condition number of a matrix. Args: lu_mat (numpy.ndarray): A 2D array of a matrix :math:`A` that has been LU-factored, with the non-diagonal part of :math:`L` stored in the strictly lower triangle and :math:`U` stored in the upper triangle. one_norm (float): The 1-norm of the original matrix :math:`A`. Returns: float: The reciprocal condition number of :math:`A`. Raises: OSError: If SciPy is not installed. RuntimeError: If the reciprocal 1-norm condition number could not be computed. """ if _scipy_lapack is None: raise OSError("This function requires SciPy for calling into LAPACK.") # pylint: disable=no-member rcond, info = _scipy_lapack.dgecon(lu_mat, one_norm) # pylint: enable=no-member if info != 0: raise RuntimeError( "The reciprocal 1-norm condition number could not be computed." ) return rcond
python
def _reciprocal_condition_number(lu_mat, one_norm): r"""Compute reciprocal condition number of a matrix. Args: lu_mat (numpy.ndarray): A 2D array of a matrix :math:`A` that has been LU-factored, with the non-diagonal part of :math:`L` stored in the strictly lower triangle and :math:`U` stored in the upper triangle. one_norm (float): The 1-norm of the original matrix :math:`A`. Returns: float: The reciprocal condition number of :math:`A`. Raises: OSError: If SciPy is not installed. RuntimeError: If the reciprocal 1-norm condition number could not be computed. """ if _scipy_lapack is None: raise OSError("This function requires SciPy for calling into LAPACK.") # pylint: disable=no-member rcond, info = _scipy_lapack.dgecon(lu_mat, one_norm) # pylint: enable=no-member if info != 0: raise RuntimeError( "The reciprocal 1-norm condition number could not be computed." ) return rcond
[ "def", "_reciprocal_condition_number", "(", "lu_mat", ",", "one_norm", ")", ":", "if", "_scipy_lapack", "is", "None", ":", "raise", "OSError", "(", "\"This function requires SciPy for calling into LAPACK.\"", ")", "# pylint: disable=no-member", "rcond", ",", "info", "=", "_scipy_lapack", ".", "dgecon", "(", "lu_mat", ",", "one_norm", ")", "# pylint: enable=no-member", "if", "info", "!=", "0", ":", "raise", "RuntimeError", "(", "\"The reciprocal 1-norm condition number could not be computed.\"", ")", "return", "rcond" ]
r"""Compute reciprocal condition number of a matrix. Args: lu_mat (numpy.ndarray): A 2D array of a matrix :math:`A` that has been LU-factored, with the non-diagonal part of :math:`L` stored in the strictly lower triangle and :math:`U` stored in the upper triangle. one_norm (float): The 1-norm of the original matrix :math:`A`. Returns: float: The reciprocal condition number of :math:`A`. Raises: OSError: If SciPy is not installed. RuntimeError: If the reciprocal 1-norm condition number could not be computed.
[ "r", "Compute", "reciprocal", "condition", "number", "of", "a", "matrix", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1026-L1054
dhermes/bezier
src/bezier/_algebraic_intersection.py
bezier_value_check
def bezier_value_check(coeffs, s_val, rhs_val=0.0): r"""Check if a polynomial in the Bernstein basis evaluates to a value. This is intended to be used for root checking, i.e. for a polynomial :math:`f(s)` and a particular value :math:`s_{\ast}`: Is it true that :math:`f\left(s_{\ast}\right) = 0`? Does so by re-stating as a matrix rank problem. As in :func:`~bezier._algebraic_intersection.bezier_roots`, we can rewrite .. math:: f(s) = (1 - s)^n g\left(\sigma\right) for :math:`\sigma = \frac{s}{1 - s}` and :math:`g\left(\sigma\right)` written in the power / monomial basis. Now, checking if :math:`g\left(\sigma\right) = 0` is a matter of checking that :math:`\det\left(C_g - \sigma I\right) = 0` (where :math:`C_g` is the companion matrix of :math:`g`). Due to issues of numerical stability, we'd rather ask if :math:`C_g - \sigma I` is singular to numerical precision. A typical approach to this using the singular values (assuming :math:`C_g` is :math:`m \times m`) is that the matrix is singular if .. math:: \sigma_m < m \varepsilon \sigma_1 \Longleftrightarrow \frac{1}{\kappa_2} < m \varepsilon (where :math:`\kappa_2` is the 2-norm condition number of the matrix). Since we also know that :math:`\kappa_2 < \kappa_1`, a stronger requirement would be .. math:: \frac{1}{\kappa_1} < \frac{1}{\kappa_2} < m \varepsilon. This is more useful since it is **much** easier to compute the 1-norm condition number / reciprocal condition number (and methods in LAPACK are provided for doing so). Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis representing a polynomial. s_val (float): The value to check on the polynomial: :math:`f(s) = r`. rhs_val (Optional[float]): The value to check that the polynomial evaluates to. Defaults to ``0.0``. Returns: bool: Indicates if :math:`f\left(s_{\ast}\right) = r` (where :math:`s_{\ast}` is ``s_val`` and :math:`r` is ``rhs_val``). """ if s_val == 1.0: return coeffs[-1] == rhs_val shifted_coeffs = coeffs - rhs_val sigma_coeffs, _, effective_degree = _get_sigma_coeffs(shifted_coeffs) if effective_degree == 0: # This means that all coefficients except the ``(1 - s)^n`` # term are zero, so we have ``f(s) = C (1 - s)^n``. Since we know # ``s != 1``, this can only be zero if ``C == 0``. return shifted_coeffs[0] == 0.0 sigma_val = s_val / (1.0 - s_val) lu_mat, one_norm = lu_companion(-sigma_coeffs[::-1], sigma_val) rcond = _reciprocal_condition_number(lu_mat, one_norm) # "Is a root?" IFF Singular IF ``1/kappa_1 < m epsilon`` return rcond < effective_degree * _SINGULAR_EPS
python
def bezier_value_check(coeffs, s_val, rhs_val=0.0): r"""Check if a polynomial in the Bernstein basis evaluates to a value. This is intended to be used for root checking, i.e. for a polynomial :math:`f(s)` and a particular value :math:`s_{\ast}`: Is it true that :math:`f\left(s_{\ast}\right) = 0`? Does so by re-stating as a matrix rank problem. As in :func:`~bezier._algebraic_intersection.bezier_roots`, we can rewrite .. math:: f(s) = (1 - s)^n g\left(\sigma\right) for :math:`\sigma = \frac{s}{1 - s}` and :math:`g\left(\sigma\right)` written in the power / monomial basis. Now, checking if :math:`g\left(\sigma\right) = 0` is a matter of checking that :math:`\det\left(C_g - \sigma I\right) = 0` (where :math:`C_g` is the companion matrix of :math:`g`). Due to issues of numerical stability, we'd rather ask if :math:`C_g - \sigma I` is singular to numerical precision. A typical approach to this using the singular values (assuming :math:`C_g` is :math:`m \times m`) is that the matrix is singular if .. math:: \sigma_m < m \varepsilon \sigma_1 \Longleftrightarrow \frac{1}{\kappa_2} < m \varepsilon (where :math:`\kappa_2` is the 2-norm condition number of the matrix). Since we also know that :math:`\kappa_2 < \kappa_1`, a stronger requirement would be .. math:: \frac{1}{\kappa_1} < \frac{1}{\kappa_2} < m \varepsilon. This is more useful since it is **much** easier to compute the 1-norm condition number / reciprocal condition number (and methods in LAPACK are provided for doing so). Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis representing a polynomial. s_val (float): The value to check on the polynomial: :math:`f(s) = r`. rhs_val (Optional[float]): The value to check that the polynomial evaluates to. Defaults to ``0.0``. Returns: bool: Indicates if :math:`f\left(s_{\ast}\right) = r` (where :math:`s_{\ast}` is ``s_val`` and :math:`r` is ``rhs_val``). """ if s_val == 1.0: return coeffs[-1] == rhs_val shifted_coeffs = coeffs - rhs_val sigma_coeffs, _, effective_degree = _get_sigma_coeffs(shifted_coeffs) if effective_degree == 0: # This means that all coefficients except the ``(1 - s)^n`` # term are zero, so we have ``f(s) = C (1 - s)^n``. Since we know # ``s != 1``, this can only be zero if ``C == 0``. return shifted_coeffs[0] == 0.0 sigma_val = s_val / (1.0 - s_val) lu_mat, one_norm = lu_companion(-sigma_coeffs[::-1], sigma_val) rcond = _reciprocal_condition_number(lu_mat, one_norm) # "Is a root?" IFF Singular IF ``1/kappa_1 < m epsilon`` return rcond < effective_degree * _SINGULAR_EPS
[ "def", "bezier_value_check", "(", "coeffs", ",", "s_val", ",", "rhs_val", "=", "0.0", ")", ":", "if", "s_val", "==", "1.0", ":", "return", "coeffs", "[", "-", "1", "]", "==", "rhs_val", "shifted_coeffs", "=", "coeffs", "-", "rhs_val", "sigma_coeffs", ",", "_", ",", "effective_degree", "=", "_get_sigma_coeffs", "(", "shifted_coeffs", ")", "if", "effective_degree", "==", "0", ":", "# This means that all coefficients except the ``(1 - s)^n``", "# term are zero, so we have ``f(s) = C (1 - s)^n``. Since we know", "# ``s != 1``, this can only be zero if ``C == 0``.", "return", "shifted_coeffs", "[", "0", "]", "==", "0.0", "sigma_val", "=", "s_val", "/", "(", "1.0", "-", "s_val", ")", "lu_mat", ",", "one_norm", "=", "lu_companion", "(", "-", "sigma_coeffs", "[", ":", ":", "-", "1", "]", ",", "sigma_val", ")", "rcond", "=", "_reciprocal_condition_number", "(", "lu_mat", ",", "one_norm", ")", "# \"Is a root?\" IFF Singular IF ``1/kappa_1 < m epsilon``", "return", "rcond", "<", "effective_degree", "*", "_SINGULAR_EPS" ]
r"""Check if a polynomial in the Bernstein basis evaluates to a value. This is intended to be used for root checking, i.e. for a polynomial :math:`f(s)` and a particular value :math:`s_{\ast}`: Is it true that :math:`f\left(s_{\ast}\right) = 0`? Does so by re-stating as a matrix rank problem. As in :func:`~bezier._algebraic_intersection.bezier_roots`, we can rewrite .. math:: f(s) = (1 - s)^n g\left(\sigma\right) for :math:`\sigma = \frac{s}{1 - s}` and :math:`g\left(\sigma\right)` written in the power / monomial basis. Now, checking if :math:`g\left(\sigma\right) = 0` is a matter of checking that :math:`\det\left(C_g - \sigma I\right) = 0` (where :math:`C_g` is the companion matrix of :math:`g`). Due to issues of numerical stability, we'd rather ask if :math:`C_g - \sigma I` is singular to numerical precision. A typical approach to this using the singular values (assuming :math:`C_g` is :math:`m \times m`) is that the matrix is singular if .. math:: \sigma_m < m \varepsilon \sigma_1 \Longleftrightarrow \frac{1}{\kappa_2} < m \varepsilon (where :math:`\kappa_2` is the 2-norm condition number of the matrix). Since we also know that :math:`\kappa_2 < \kappa_1`, a stronger requirement would be .. math:: \frac{1}{\kappa_1} < \frac{1}{\kappa_2} < m \varepsilon. This is more useful since it is **much** easier to compute the 1-norm condition number / reciprocal condition number (and methods in LAPACK are provided for doing so). Args: coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis representing a polynomial. s_val (float): The value to check on the polynomial: :math:`f(s) = r`. rhs_val (Optional[float]): The value to check that the polynomial evaluates to. Defaults to ``0.0``. Returns: bool: Indicates if :math:`f\left(s_{\ast}\right) = r` (where :math:`s_{\ast}` is ``s_val`` and :math:`r` is ``rhs_val``).
[ "r", "Check", "if", "a", "polynomial", "in", "the", "Bernstein", "basis", "evaluates", "to", "a", "value", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1057-L1127
dhermes/bezier
src/bezier/_algebraic_intersection.py
roots_in_unit_interval
def roots_in_unit_interval(coeffs): r"""Compute roots of a polynomial in the unit interval. Args: coeffs (numpy.ndarray): A 1D array (size ``d + 1``) of coefficients in monomial / power basis. Returns: numpy.ndarray: ``N``-array of real values in :math:`\left[0, 1\right]`. """ all_roots = polynomial.polyroots(coeffs) # Only keep roots inside or very near to the unit interval. all_roots = all_roots[ (_UNIT_INTERVAL_WIGGLE_START < all_roots.real) & (all_roots.real < _UNIT_INTERVAL_WIGGLE_END) ] # Only keep roots with very small imaginary part. (Really only # keep the real parts.) real_inds = np.abs(all_roots.imag) < _IMAGINARY_WIGGLE return all_roots[real_inds].real
python
def roots_in_unit_interval(coeffs): r"""Compute roots of a polynomial in the unit interval. Args: coeffs (numpy.ndarray): A 1D array (size ``d + 1``) of coefficients in monomial / power basis. Returns: numpy.ndarray: ``N``-array of real values in :math:`\left[0, 1\right]`. """ all_roots = polynomial.polyroots(coeffs) # Only keep roots inside or very near to the unit interval. all_roots = all_roots[ (_UNIT_INTERVAL_WIGGLE_START < all_roots.real) & (all_roots.real < _UNIT_INTERVAL_WIGGLE_END) ] # Only keep roots with very small imaginary part. (Really only # keep the real parts.) real_inds = np.abs(all_roots.imag) < _IMAGINARY_WIGGLE return all_roots[real_inds].real
[ "def", "roots_in_unit_interval", "(", "coeffs", ")", ":", "all_roots", "=", "polynomial", ".", "polyroots", "(", "coeffs", ")", "# Only keep roots inside or very near to the unit interval.", "all_roots", "=", "all_roots", "[", "(", "_UNIT_INTERVAL_WIGGLE_START", "<", "all_roots", ".", "real", ")", "&", "(", "all_roots", ".", "real", "<", "_UNIT_INTERVAL_WIGGLE_END", ")", "]", "# Only keep roots with very small imaginary part. (Really only", "# keep the real parts.)", "real_inds", "=", "np", ".", "abs", "(", "all_roots", ".", "imag", ")", "<", "_IMAGINARY_WIGGLE", "return", "all_roots", "[", "real_inds", "]", ".", "real" ]
r"""Compute roots of a polynomial in the unit interval. Args: coeffs (numpy.ndarray): A 1D array (size ``d + 1``) of coefficients in monomial / power basis. Returns: numpy.ndarray: ``N``-array of real values in :math:`\left[0, 1\right]`.
[ "r", "Compute", "roots", "of", "a", "polynomial", "in", "the", "unit", "interval", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1130-L1149
dhermes/bezier
src/bezier/_algebraic_intersection.py
_strip_leading_zeros
def _strip_leading_zeros(coeffs, threshold=_COEFFICIENT_THRESHOLD): r"""Strip leading zero coefficients from a polynomial. .. note:: This assumes the polynomial :math:`f` defined by ``coeffs`` has been normalized (via :func:`.normalize_polynomial`). Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. threshold (Optional[float]): The point :math:`\tau` below which a a coefficient will be considered to be numerically zero. Returns: numpy.ndarray: The same coefficients without any unnecessary zero terms. """ while np.abs(coeffs[-1]) < threshold: coeffs = coeffs[:-1] return coeffs
python
def _strip_leading_zeros(coeffs, threshold=_COEFFICIENT_THRESHOLD): r"""Strip leading zero coefficients from a polynomial. .. note:: This assumes the polynomial :math:`f` defined by ``coeffs`` has been normalized (via :func:`.normalize_polynomial`). Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. threshold (Optional[float]): The point :math:`\tau` below which a a coefficient will be considered to be numerically zero. Returns: numpy.ndarray: The same coefficients without any unnecessary zero terms. """ while np.abs(coeffs[-1]) < threshold: coeffs = coeffs[:-1] return coeffs
[ "def", "_strip_leading_zeros", "(", "coeffs", ",", "threshold", "=", "_COEFFICIENT_THRESHOLD", ")", ":", "while", "np", ".", "abs", "(", "coeffs", "[", "-", "1", "]", ")", "<", "threshold", ":", "coeffs", "=", "coeffs", "[", ":", "-", "1", "]", "return", "coeffs" ]
r"""Strip leading zero coefficients from a polynomial. .. note:: This assumes the polynomial :math:`f` defined by ``coeffs`` has been normalized (via :func:`.normalize_polynomial`). Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. threshold (Optional[float]): The point :math:`\tau` below which a a coefficient will be considered to be numerically zero. Returns: numpy.ndarray: The same coefficients without any unnecessary zero terms.
[ "r", "Strip", "leading", "zero", "coefficients", "from", "a", "polynomial", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1152-L1172
dhermes/bezier
src/bezier/_algebraic_intersection.py
_check_non_simple
def _check_non_simple(coeffs): r"""Checks that a polynomial has no non-simple roots. Does so by computing the companion matrix :math:`A` of :math:`f'` and then evaluating the rank of :math:`B = f(A)`. If :math:`B` is not full rank, then :math:`f` and :math:`f'` have a shared factor. See: https://dx.doi.org/10.1016/0024-3795(70)90023-6 .. note:: This assumes that :math:`f \neq 0`. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. Raises: NotImplementedError: If the polynomial has non-simple roots. """ coeffs = _strip_leading_zeros(coeffs) num_coeffs, = coeffs.shape if num_coeffs < 3: return deriv_poly = polynomial.polyder(coeffs) companion = polynomial.polycompanion(deriv_poly) # NOTE: ``polycompanion()`` returns a C-contiguous array. companion = companion.T # Use Horner's method to evaluate f(companion) num_companion, _ = companion.shape id_mat = np.eye(num_companion, order="F") evaluated = coeffs[-1] * id_mat for index in six.moves.xrange(num_coeffs - 2, -1, -1): coeff = coeffs[index] evaluated = ( _helpers.matrix_product(evaluated, companion) + coeff * id_mat ) if num_companion == 1: # NOTE: This relies on the fact that coeffs is normalized. if np.abs(evaluated[0, 0]) > _NON_SIMPLE_THRESHOLD: rank = 1 else: rank = 0 else: rank = np.linalg.matrix_rank(evaluated) if rank < num_companion: raise NotImplementedError(_NON_SIMPLE_ERR, coeffs)
python
def _check_non_simple(coeffs): r"""Checks that a polynomial has no non-simple roots. Does so by computing the companion matrix :math:`A` of :math:`f'` and then evaluating the rank of :math:`B = f(A)`. If :math:`B` is not full rank, then :math:`f` and :math:`f'` have a shared factor. See: https://dx.doi.org/10.1016/0024-3795(70)90023-6 .. note:: This assumes that :math:`f \neq 0`. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. Raises: NotImplementedError: If the polynomial has non-simple roots. """ coeffs = _strip_leading_zeros(coeffs) num_coeffs, = coeffs.shape if num_coeffs < 3: return deriv_poly = polynomial.polyder(coeffs) companion = polynomial.polycompanion(deriv_poly) # NOTE: ``polycompanion()`` returns a C-contiguous array. companion = companion.T # Use Horner's method to evaluate f(companion) num_companion, _ = companion.shape id_mat = np.eye(num_companion, order="F") evaluated = coeffs[-1] * id_mat for index in six.moves.xrange(num_coeffs - 2, -1, -1): coeff = coeffs[index] evaluated = ( _helpers.matrix_product(evaluated, companion) + coeff * id_mat ) if num_companion == 1: # NOTE: This relies on the fact that coeffs is normalized. if np.abs(evaluated[0, 0]) > _NON_SIMPLE_THRESHOLD: rank = 1 else: rank = 0 else: rank = np.linalg.matrix_rank(evaluated) if rank < num_companion: raise NotImplementedError(_NON_SIMPLE_ERR, coeffs)
[ "def", "_check_non_simple", "(", "coeffs", ")", ":", "coeffs", "=", "_strip_leading_zeros", "(", "coeffs", ")", "num_coeffs", ",", "=", "coeffs", ".", "shape", "if", "num_coeffs", "<", "3", ":", "return", "deriv_poly", "=", "polynomial", ".", "polyder", "(", "coeffs", ")", "companion", "=", "polynomial", ".", "polycompanion", "(", "deriv_poly", ")", "# NOTE: ``polycompanion()`` returns a C-contiguous array.", "companion", "=", "companion", ".", "T", "# Use Horner's method to evaluate f(companion)", "num_companion", ",", "_", "=", "companion", ".", "shape", "id_mat", "=", "np", ".", "eye", "(", "num_companion", ",", "order", "=", "\"F\"", ")", "evaluated", "=", "coeffs", "[", "-", "1", "]", "*", "id_mat", "for", "index", "in", "six", ".", "moves", ".", "xrange", "(", "num_coeffs", "-", "2", ",", "-", "1", ",", "-", "1", ")", ":", "coeff", "=", "coeffs", "[", "index", "]", "evaluated", "=", "(", "_helpers", ".", "matrix_product", "(", "evaluated", ",", "companion", ")", "+", "coeff", "*", "id_mat", ")", "if", "num_companion", "==", "1", ":", "# NOTE: This relies on the fact that coeffs is normalized.", "if", "np", ".", "abs", "(", "evaluated", "[", "0", ",", "0", "]", ")", ">", "_NON_SIMPLE_THRESHOLD", ":", "rank", "=", "1", "else", ":", "rank", "=", "0", "else", ":", "rank", "=", "np", ".", "linalg", ".", "matrix_rank", "(", "evaluated", ")", "if", "rank", "<", "num_companion", ":", "raise", "NotImplementedError", "(", "_NON_SIMPLE_ERR", ",", "coeffs", ")" ]
r"""Checks that a polynomial has no non-simple roots. Does so by computing the companion matrix :math:`A` of :math:`f'` and then evaluating the rank of :math:`B = f(A)`. If :math:`B` is not full rank, then :math:`f` and :math:`f'` have a shared factor. See: https://dx.doi.org/10.1016/0024-3795(70)90023-6 .. note:: This assumes that :math:`f \neq 0`. Args: coeffs (numpy.ndarray): ``d + 1``-array of coefficients in monomial / power basis. Raises: NotImplementedError: If the polynomial has non-simple roots.
[ "r", "Checks", "that", "a", "polynomial", "has", "no", "non", "-", "simple", "roots", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1175-L1222
dhermes/bezier
src/bezier/_algebraic_intersection.py
_resolve_and_add
def _resolve_and_add(nodes1, s_val, final_s, nodes2, t_val, final_t): """Resolve a computed intersection and add to lists. We perform one Newton step to deal with any residual issues of high-degree polynomial solves (one of which depends on the already approximate ``x_val, y_val``). Args: nodes1 (numpy.ndarray): The nodes in the first curve. s_val (float): The approximate intersection parameter along ``nodes1``. final_s (List[float]): The list of accepted intersection parameters ``s``. nodes2 (numpy.ndarray): The nodes in the second curve. t_val (float): The approximate intersection parameter along ``nodes2``. final_t (List[float]): The list of accepted intersection parameters ``t``. """ s_val, t_val = _intersection_helpers.newton_refine( s_val, nodes1, t_val, nodes2 ) s_val, success_s = _helpers.wiggle_interval(s_val) t_val, success_t = _helpers.wiggle_interval(t_val) if not (success_s and success_t): return final_s.append(s_val) final_t.append(t_val)
python
def _resolve_and_add(nodes1, s_val, final_s, nodes2, t_val, final_t): """Resolve a computed intersection and add to lists. We perform one Newton step to deal with any residual issues of high-degree polynomial solves (one of which depends on the already approximate ``x_val, y_val``). Args: nodes1 (numpy.ndarray): The nodes in the first curve. s_val (float): The approximate intersection parameter along ``nodes1``. final_s (List[float]): The list of accepted intersection parameters ``s``. nodes2 (numpy.ndarray): The nodes in the second curve. t_val (float): The approximate intersection parameter along ``nodes2``. final_t (List[float]): The list of accepted intersection parameters ``t``. """ s_val, t_val = _intersection_helpers.newton_refine( s_val, nodes1, t_val, nodes2 ) s_val, success_s = _helpers.wiggle_interval(s_val) t_val, success_t = _helpers.wiggle_interval(t_val) if not (success_s and success_t): return final_s.append(s_val) final_t.append(t_val)
[ "def", "_resolve_and_add", "(", "nodes1", ",", "s_val", ",", "final_s", ",", "nodes2", ",", "t_val", ",", "final_t", ")", ":", "s_val", ",", "t_val", "=", "_intersection_helpers", ".", "newton_refine", "(", "s_val", ",", "nodes1", ",", "t_val", ",", "nodes2", ")", "s_val", ",", "success_s", "=", "_helpers", ".", "wiggle_interval", "(", "s_val", ")", "t_val", ",", "success_t", "=", "_helpers", ".", "wiggle_interval", "(", "t_val", ")", "if", "not", "(", "success_s", "and", "success_t", ")", ":", "return", "final_s", ".", "append", "(", "s_val", ")", "final_t", ".", "append", "(", "t_val", ")" ]
Resolve a computed intersection and add to lists. We perform one Newton step to deal with any residual issues of high-degree polynomial solves (one of which depends on the already approximate ``x_val, y_val``). Args: nodes1 (numpy.ndarray): The nodes in the first curve. s_val (float): The approximate intersection parameter along ``nodes1``. final_s (List[float]): The list of accepted intersection parameters ``s``. nodes2 (numpy.ndarray): The nodes in the second curve. t_val (float): The approximate intersection parameter along ``nodes2``. final_t (List[float]): The list of accepted intersection parameters ``t``.
[ "Resolve", "a", "computed", "intersection", "and", "add", "to", "lists", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1225-L1253
dhermes/bezier
src/bezier/_algebraic_intersection.py
intersect_curves
def intersect_curves(nodes1, nodes2): r"""Intersect two parametric B |eacute| zier curves. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. Raises: NotImplementedError: If the "intersection polynomial" is all zeros -- which indicates coincident curves. """ nodes1 = _curve_helpers.full_reduce(nodes1) nodes2 = _curve_helpers.full_reduce(nodes2) _, num_nodes1 = nodes1.shape _, num_nodes2 = nodes2.shape swapped = False if num_nodes1 > num_nodes2: nodes1, nodes2 = nodes2, nodes1 swapped = True coeffs = normalize_polynomial(to_power_basis(nodes1, nodes2)) if np.all(coeffs == 0.0): raise NotImplementedError(_COINCIDENT_ERR) _check_non_simple(coeffs) t_vals = roots_in_unit_interval(coeffs) final_s = [] final_t = [] for t_val in t_vals: (x_val,), (y_val,) = _curve_helpers.evaluate_multi( nodes2, np.asfortranarray([t_val]) ) s_val = locate_point(nodes1, x_val, y_val) if s_val is not None: _resolve_and_add(nodes1, s_val, final_s, nodes2, t_val, final_t) result = np.zeros((2, len(final_s)), order="F") if swapped: final_s, final_t = final_t, final_s result[0, :] = final_s result[1, :] = final_t return result
python
def intersect_curves(nodes1, nodes2): r"""Intersect two parametric B |eacute| zier curves. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. Raises: NotImplementedError: If the "intersection polynomial" is all zeros -- which indicates coincident curves. """ nodes1 = _curve_helpers.full_reduce(nodes1) nodes2 = _curve_helpers.full_reduce(nodes2) _, num_nodes1 = nodes1.shape _, num_nodes2 = nodes2.shape swapped = False if num_nodes1 > num_nodes2: nodes1, nodes2 = nodes2, nodes1 swapped = True coeffs = normalize_polynomial(to_power_basis(nodes1, nodes2)) if np.all(coeffs == 0.0): raise NotImplementedError(_COINCIDENT_ERR) _check_non_simple(coeffs) t_vals = roots_in_unit_interval(coeffs) final_s = [] final_t = [] for t_val in t_vals: (x_val,), (y_val,) = _curve_helpers.evaluate_multi( nodes2, np.asfortranarray([t_val]) ) s_val = locate_point(nodes1, x_val, y_val) if s_val is not None: _resolve_and_add(nodes1, s_val, final_s, nodes2, t_val, final_t) result = np.zeros((2, len(final_s)), order="F") if swapped: final_s, final_t = final_t, final_s result[0, :] = final_s result[1, :] = final_t return result
[ "def", "intersect_curves", "(", "nodes1", ",", "nodes2", ")", ":", "nodes1", "=", "_curve_helpers", ".", "full_reduce", "(", "nodes1", ")", "nodes2", "=", "_curve_helpers", ".", "full_reduce", "(", "nodes2", ")", "_", ",", "num_nodes1", "=", "nodes1", ".", "shape", "_", ",", "num_nodes2", "=", "nodes2", ".", "shape", "swapped", "=", "False", "if", "num_nodes1", ">", "num_nodes2", ":", "nodes1", ",", "nodes2", "=", "nodes2", ",", "nodes1", "swapped", "=", "True", "coeffs", "=", "normalize_polynomial", "(", "to_power_basis", "(", "nodes1", ",", "nodes2", ")", ")", "if", "np", ".", "all", "(", "coeffs", "==", "0.0", ")", ":", "raise", "NotImplementedError", "(", "_COINCIDENT_ERR", ")", "_check_non_simple", "(", "coeffs", ")", "t_vals", "=", "roots_in_unit_interval", "(", "coeffs", ")", "final_s", "=", "[", "]", "final_t", "=", "[", "]", "for", "t_val", "in", "t_vals", ":", "(", "x_val", ",", ")", ",", "(", "y_val", ",", ")", "=", "_curve_helpers", ".", "evaluate_multi", "(", "nodes2", ",", "np", ".", "asfortranarray", "(", "[", "t_val", "]", ")", ")", "s_val", "=", "locate_point", "(", "nodes1", ",", "x_val", ",", "y_val", ")", "if", "s_val", "is", "not", "None", ":", "_resolve_and_add", "(", "nodes1", ",", "s_val", ",", "final_s", ",", "nodes2", ",", "t_val", ",", "final_t", ")", "result", "=", "np", ".", "zeros", "(", "(", "2", ",", "len", "(", "final_s", ")", ")", ",", "order", "=", "\"F\"", ")", "if", "swapped", ":", "final_s", ",", "final_t", "=", "final_t", ",", "final_s", "result", "[", "0", ",", ":", "]", "=", "final_s", "result", "[", "1", ",", ":", "]", "=", "final_t", "return", "result" ]
r"""Intersect two parametric B |eacute| zier curves. Args: nodes1 (numpy.ndarray): The nodes in the first curve. nodes2 (numpy.ndarray): The nodes in the second curve. Returns: numpy.ndarray: ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. Raises: NotImplementedError: If the "intersection polynomial" is all zeros -- which indicates coincident curves.
[ "r", "Intersect", "two", "parametric", "B", "|eacute|", "zier", "curves", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1256-L1301
dhermes/bezier
src/bezier/_algebraic_intersection.py
poly_to_power_basis
def poly_to_power_basis(bezier_coeffs): """Convert a B |eacute| zier curve to polynomial in power basis. .. note:: This assumes, but does not verify, that the "B |eacute| zier degree" matches the true degree of the curve. Callers can guarantee this by calling :func:`.full_reduce`. Args: bezier_coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: numpy.ndarray: 1D array of coefficients in monomial basis. Raises: .UnsupportedDegree: If the degree of the curve is not among 0, 1, 2 or 3. """ num_coeffs, = bezier_coeffs.shape if num_coeffs == 1: return bezier_coeffs elif num_coeffs == 2: # C0 (1 - s) + C1 s = C0 + (C1 - C0) s coeff0, coeff1 = bezier_coeffs return np.asfortranarray([coeff0, coeff1 - coeff0]) elif num_coeffs == 3: # C0 (1 - s)^2 + C1 2 (1 - s) s + C2 s^2 # = C0 + 2(C1 - C0) s + (C2 - 2 C1 + C0) s^2 coeff0, coeff1, coeff2 = bezier_coeffs return np.asfortranarray( [coeff0, 2.0 * (coeff1 - coeff0), coeff2 - 2.0 * coeff1 + coeff0] ) elif num_coeffs == 4: # C0 (1 - s)^3 + C1 3 (1 - s)^2 + C2 3 (1 - s) s^2 + C3 s^3 # = C0 + 3(C1 - C0) s + 3(C2 - 2 C1 + C0) s^2 + # (C3 - 3 C2 + 3 C1 - C0) s^3 coeff0, coeff1, coeff2, coeff3 = bezier_coeffs return np.asfortranarray( [ coeff0, 3.0 * (coeff1 - coeff0), 3.0 * (coeff2 - 2.0 * coeff1 + coeff0), coeff3 - 3.0 * coeff2 + 3.0 * coeff1 - coeff0, ] ) else: raise _helpers.UnsupportedDegree( num_coeffs - 1, supported=(0, 1, 2, 3) )
python
def poly_to_power_basis(bezier_coeffs): """Convert a B |eacute| zier curve to polynomial in power basis. .. note:: This assumes, but does not verify, that the "B |eacute| zier degree" matches the true degree of the curve. Callers can guarantee this by calling :func:`.full_reduce`. Args: bezier_coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: numpy.ndarray: 1D array of coefficients in monomial basis. Raises: .UnsupportedDegree: If the degree of the curve is not among 0, 1, 2 or 3. """ num_coeffs, = bezier_coeffs.shape if num_coeffs == 1: return bezier_coeffs elif num_coeffs == 2: # C0 (1 - s) + C1 s = C0 + (C1 - C0) s coeff0, coeff1 = bezier_coeffs return np.asfortranarray([coeff0, coeff1 - coeff0]) elif num_coeffs == 3: # C0 (1 - s)^2 + C1 2 (1 - s) s + C2 s^2 # = C0 + 2(C1 - C0) s + (C2 - 2 C1 + C0) s^2 coeff0, coeff1, coeff2 = bezier_coeffs return np.asfortranarray( [coeff0, 2.0 * (coeff1 - coeff0), coeff2 - 2.0 * coeff1 + coeff0] ) elif num_coeffs == 4: # C0 (1 - s)^3 + C1 3 (1 - s)^2 + C2 3 (1 - s) s^2 + C3 s^3 # = C0 + 3(C1 - C0) s + 3(C2 - 2 C1 + C0) s^2 + # (C3 - 3 C2 + 3 C1 - C0) s^3 coeff0, coeff1, coeff2, coeff3 = bezier_coeffs return np.asfortranarray( [ coeff0, 3.0 * (coeff1 - coeff0), 3.0 * (coeff2 - 2.0 * coeff1 + coeff0), coeff3 - 3.0 * coeff2 + 3.0 * coeff1 - coeff0, ] ) else: raise _helpers.UnsupportedDegree( num_coeffs - 1, supported=(0, 1, 2, 3) )
[ "def", "poly_to_power_basis", "(", "bezier_coeffs", ")", ":", "num_coeffs", ",", "=", "bezier_coeffs", ".", "shape", "if", "num_coeffs", "==", "1", ":", "return", "bezier_coeffs", "elif", "num_coeffs", "==", "2", ":", "# C0 (1 - s) + C1 s = C0 + (C1 - C0) s", "coeff0", ",", "coeff1", "=", "bezier_coeffs", "return", "np", ".", "asfortranarray", "(", "[", "coeff0", ",", "coeff1", "-", "coeff0", "]", ")", "elif", "num_coeffs", "==", "3", ":", "# C0 (1 - s)^2 + C1 2 (1 - s) s + C2 s^2", "# = C0 + 2(C1 - C0) s + (C2 - 2 C1 + C0) s^2", "coeff0", ",", "coeff1", ",", "coeff2", "=", "bezier_coeffs", "return", "np", ".", "asfortranarray", "(", "[", "coeff0", ",", "2.0", "*", "(", "coeff1", "-", "coeff0", ")", ",", "coeff2", "-", "2.0", "*", "coeff1", "+", "coeff0", "]", ")", "elif", "num_coeffs", "==", "4", ":", "# C0 (1 - s)^3 + C1 3 (1 - s)^2 + C2 3 (1 - s) s^2 + C3 s^3", "# = C0 + 3(C1 - C0) s + 3(C2 - 2 C1 + C0) s^2 +", "# (C3 - 3 C2 + 3 C1 - C0) s^3", "coeff0", ",", "coeff1", ",", "coeff2", ",", "coeff3", "=", "bezier_coeffs", "return", "np", ".", "asfortranarray", "(", "[", "coeff0", ",", "3.0", "*", "(", "coeff1", "-", "coeff0", ")", ",", "3.0", "*", "(", "coeff2", "-", "2.0", "*", "coeff1", "+", "coeff0", ")", ",", "coeff3", "-", "3.0", "*", "coeff2", "+", "3.0", "*", "coeff1", "-", "coeff0", ",", "]", ")", "else", ":", "raise", "_helpers", ".", "UnsupportedDegree", "(", "num_coeffs", "-", "1", ",", "supported", "=", "(", "0", ",", "1", ",", "2", ",", "3", ")", ")" ]
Convert a B |eacute| zier curve to polynomial in power basis. .. note:: This assumes, but does not verify, that the "B |eacute| zier degree" matches the true degree of the curve. Callers can guarantee this by calling :func:`.full_reduce`. Args: bezier_coeffs (numpy.ndarray): A 1D array of coefficients in the Bernstein basis. Returns: numpy.ndarray: 1D array of coefficients in monomial basis. Raises: .UnsupportedDegree: If the degree of the curve is not among 0, 1, 2 or 3.
[ "Convert", "a", "B", "|eacute|", "zier", "curve", "to", "polynomial", "in", "power", "basis", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1304-L1358
dhermes/bezier
src/bezier/_algebraic_intersection.py
locate_point
def locate_point(nodes, x_val, y_val): r"""Find the parameter corresponding to a point on a curve. .. note:: This assumes that the curve :math:`B(s, t)` defined by ``nodes`` lives in :math:`\mathbf{R}^2`. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. x_val (float): The :math:`x`-coordinate of the point. y_val (float): The :math:`y`-coordinate of the point. Returns: Optional[float]: The parameter on the curve (if it exists). """ # First, reduce to the true degree of x(s) and y(s). zero1 = _curve_helpers.full_reduce(nodes[[0], :]) - x_val zero2 = _curve_helpers.full_reduce(nodes[[1], :]) - y_val # Make sure we have the lowest degree in front, to make the polynomial # solve have the fewest number of roots. if zero1.shape[1] > zero2.shape[1]: zero1, zero2 = zero2, zero1 # If the "smallest" is a constant, we can't find any roots from it. if zero1.shape[1] == 1: # NOTE: We assume that callers won't pass ``nodes`` that are # degree 0, so if ``zero1`` is a constant, ``zero2`` won't be. zero1, zero2 = zero2, zero1 power_basis1 = poly_to_power_basis(zero1[0, :]) all_roots = roots_in_unit_interval(power_basis1) if all_roots.size == 0: return None # NOTE: We normalize ``power_basis2`` because we want to check for # "zero" values, i.e. f2(s) == 0. power_basis2 = normalize_polynomial(poly_to_power_basis(zero2[0, :])) near_zero = np.abs(polynomial.polyval(all_roots, power_basis2)) index = np.argmin(near_zero) if near_zero[index] < _ZERO_THRESHOLD: return all_roots[index] return None
python
def locate_point(nodes, x_val, y_val): r"""Find the parameter corresponding to a point on a curve. .. note:: This assumes that the curve :math:`B(s, t)` defined by ``nodes`` lives in :math:`\mathbf{R}^2`. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. x_val (float): The :math:`x`-coordinate of the point. y_val (float): The :math:`y`-coordinate of the point. Returns: Optional[float]: The parameter on the curve (if it exists). """ # First, reduce to the true degree of x(s) and y(s). zero1 = _curve_helpers.full_reduce(nodes[[0], :]) - x_val zero2 = _curve_helpers.full_reduce(nodes[[1], :]) - y_val # Make sure we have the lowest degree in front, to make the polynomial # solve have the fewest number of roots. if zero1.shape[1] > zero2.shape[1]: zero1, zero2 = zero2, zero1 # If the "smallest" is a constant, we can't find any roots from it. if zero1.shape[1] == 1: # NOTE: We assume that callers won't pass ``nodes`` that are # degree 0, so if ``zero1`` is a constant, ``zero2`` won't be. zero1, zero2 = zero2, zero1 power_basis1 = poly_to_power_basis(zero1[0, :]) all_roots = roots_in_unit_interval(power_basis1) if all_roots.size == 0: return None # NOTE: We normalize ``power_basis2`` because we want to check for # "zero" values, i.e. f2(s) == 0. power_basis2 = normalize_polynomial(poly_to_power_basis(zero2[0, :])) near_zero = np.abs(polynomial.polyval(all_roots, power_basis2)) index = np.argmin(near_zero) if near_zero[index] < _ZERO_THRESHOLD: return all_roots[index] return None
[ "def", "locate_point", "(", "nodes", ",", "x_val", ",", "y_val", ")", ":", "# First, reduce to the true degree of x(s) and y(s).", "zero1", "=", "_curve_helpers", ".", "full_reduce", "(", "nodes", "[", "[", "0", "]", ",", ":", "]", ")", "-", "x_val", "zero2", "=", "_curve_helpers", ".", "full_reduce", "(", "nodes", "[", "[", "1", "]", ",", ":", "]", ")", "-", "y_val", "# Make sure we have the lowest degree in front, to make the polynomial", "# solve have the fewest number of roots.", "if", "zero1", ".", "shape", "[", "1", "]", ">", "zero2", ".", "shape", "[", "1", "]", ":", "zero1", ",", "zero2", "=", "zero2", ",", "zero1", "# If the \"smallest\" is a constant, we can't find any roots from it.", "if", "zero1", ".", "shape", "[", "1", "]", "==", "1", ":", "# NOTE: We assume that callers won't pass ``nodes`` that are", "# degree 0, so if ``zero1`` is a constant, ``zero2`` won't be.", "zero1", ",", "zero2", "=", "zero2", ",", "zero1", "power_basis1", "=", "poly_to_power_basis", "(", "zero1", "[", "0", ",", ":", "]", ")", "all_roots", "=", "roots_in_unit_interval", "(", "power_basis1", ")", "if", "all_roots", ".", "size", "==", "0", ":", "return", "None", "# NOTE: We normalize ``power_basis2`` because we want to check for", "# \"zero\" values, i.e. f2(s) == 0.", "power_basis2", "=", "normalize_polynomial", "(", "poly_to_power_basis", "(", "zero2", "[", "0", ",", ":", "]", ")", ")", "near_zero", "=", "np", ".", "abs", "(", "polynomial", ".", "polyval", "(", "all_roots", ",", "power_basis2", ")", ")", "index", "=", "np", ".", "argmin", "(", "near_zero", ")", "if", "near_zero", "[", "index", "]", "<", "_ZERO_THRESHOLD", ":", "return", "all_roots", "[", "index", "]", "return", "None" ]
r"""Find the parameter corresponding to a point on a curve. .. note:: This assumes that the curve :math:`B(s, t)` defined by ``nodes`` lives in :math:`\mathbf{R}^2`. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. x_val (float): The :math:`x`-coordinate of the point. y_val (float): The :math:`y`-coordinate of the point. Returns: Optional[float]: The parameter on the curve (if it exists).
[ "r", "Find", "the", "parameter", "corresponding", "to", "a", "point", "on", "a", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1361-L1402
dhermes/bezier
src/bezier/_algebraic_intersection.py
all_intersections
def all_intersections(nodes_first, nodes_second): r"""Find the points of intersection among a pair of curves. .. note:: This assumes both curves are :math:`\mathbf{R}^2`, but does not **explicitly** check this. However, functions used here will fail if that assumption fails. Args: nodes_first (numpy.ndarray): Control points of a curve to be intersected with ``nodes_second``. nodes_second (numpy.ndarray): Control points of a curve to be intersected with ``nodes_first``. Returns: Tuple[numpy.ndarray, bool]: An array and a flag: * A ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. * Flag indicating if the curves are coincident. (For now, this will always be :data:`False` since :func:`.intersect_curves` fails if coincident curves are detected.) """ # Only attempt this if the bounding boxes intersect. bbox_int = _geometric_intersection.bbox_intersect( nodes_first, nodes_second ) if bbox_int == _DISJOINT: return np.empty((2, 0), order="F"), False return intersect_curves(nodes_first, nodes_second), False
python
def all_intersections(nodes_first, nodes_second): r"""Find the points of intersection among a pair of curves. .. note:: This assumes both curves are :math:`\mathbf{R}^2`, but does not **explicitly** check this. However, functions used here will fail if that assumption fails. Args: nodes_first (numpy.ndarray): Control points of a curve to be intersected with ``nodes_second``. nodes_second (numpy.ndarray): Control points of a curve to be intersected with ``nodes_first``. Returns: Tuple[numpy.ndarray, bool]: An array and a flag: * A ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. * Flag indicating if the curves are coincident. (For now, this will always be :data:`False` since :func:`.intersect_curves` fails if coincident curves are detected.) """ # Only attempt this if the bounding boxes intersect. bbox_int = _geometric_intersection.bbox_intersect( nodes_first, nodes_second ) if bbox_int == _DISJOINT: return np.empty((2, 0), order="F"), False return intersect_curves(nodes_first, nodes_second), False
[ "def", "all_intersections", "(", "nodes_first", ",", "nodes_second", ")", ":", "# Only attempt this if the bounding boxes intersect.", "bbox_int", "=", "_geometric_intersection", ".", "bbox_intersect", "(", "nodes_first", ",", "nodes_second", ")", "if", "bbox_int", "==", "_DISJOINT", ":", "return", "np", ".", "empty", "(", "(", "2", ",", "0", ")", ",", "order", "=", "\"F\"", ")", ",", "False", "return", "intersect_curves", "(", "nodes_first", ",", "nodes_second", ")", ",", "False" ]
r"""Find the points of intersection among a pair of curves. .. note:: This assumes both curves are :math:`\mathbf{R}^2`, but does not **explicitly** check this. However, functions used here will fail if that assumption fails. Args: nodes_first (numpy.ndarray): Control points of a curve to be intersected with ``nodes_second``. nodes_second (numpy.ndarray): Control points of a curve to be intersected with ``nodes_first``. Returns: Tuple[numpy.ndarray, bool]: An array and a flag: * A ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. * Flag indicating if the curves are coincident. (For now, this will always be :data:`False` since :func:`.intersect_curves` fails if coincident curves are detected.)
[ "r", "Find", "the", "points", "of", "intersection", "among", "a", "pair", "of", "curves", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_algebraic_intersection.py#L1405-L1438
dhermes/bezier
src/bezier/_helpers.py
_vector_close
def _vector_close(vec1, vec2, eps=_EPS): r"""Checks that two vectors are equal to some threshold. Does so by computing :math:`s_1 = \|v_1\|_2` and :math:`s_2 = \|v_2\|_2` and then checking if .. math:: \|v_1 - v_2\|_2 \leq \varepsilon \min(s_1, s_2) where :math:`\varepsilon = 2^{-40} \approx 10^{-12}` is a fixed threshold. In the rare case that one of ``vec1`` or ``vec2`` is the zero vector (i.e. when :math:`\min(s_1, s_2) = 0`) instead checks that the other vector is close enough to zero: .. math:: \|v_1\|_2 = 0 \Longrightarrow \|v_2\|_2 \leq \varepsilon .. note:: This function assumes that both vectors have finite values, i.e. that no NaN or infinite numbers occur. NumPy provides :func:`np.allclose` for coverage of **all** cases. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: vec1 (numpy.ndarray): First vector (1D) for comparison. vec2 (numpy.ndarray): Second vector (1D) for comparison. eps (float): Error threshold. Defaults to :math:`2^{-40}`. Returns: bool: Flag indicating if they are close to precision. """ # NOTE: This relies on ``vec1`` and ``vec2`` being one-dimensional # vectors so NumPy doesn't try to use a matrix norm. size1 = np.linalg.norm(vec1, ord=2) size2 = np.linalg.norm(vec2, ord=2) if size1 == 0: return size2 <= eps elif size2 == 0: return size1 <= eps else: upper_bound = eps * min(size1, size2) return np.linalg.norm(vec1 - vec2, ord=2) <= upper_bound
python
def _vector_close(vec1, vec2, eps=_EPS): r"""Checks that two vectors are equal to some threshold. Does so by computing :math:`s_1 = \|v_1\|_2` and :math:`s_2 = \|v_2\|_2` and then checking if .. math:: \|v_1 - v_2\|_2 \leq \varepsilon \min(s_1, s_2) where :math:`\varepsilon = 2^{-40} \approx 10^{-12}` is a fixed threshold. In the rare case that one of ``vec1`` or ``vec2`` is the zero vector (i.e. when :math:`\min(s_1, s_2) = 0`) instead checks that the other vector is close enough to zero: .. math:: \|v_1\|_2 = 0 \Longrightarrow \|v_2\|_2 \leq \varepsilon .. note:: This function assumes that both vectors have finite values, i.e. that no NaN or infinite numbers occur. NumPy provides :func:`np.allclose` for coverage of **all** cases. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: vec1 (numpy.ndarray): First vector (1D) for comparison. vec2 (numpy.ndarray): Second vector (1D) for comparison. eps (float): Error threshold. Defaults to :math:`2^{-40}`. Returns: bool: Flag indicating if they are close to precision. """ # NOTE: This relies on ``vec1`` and ``vec2`` being one-dimensional # vectors so NumPy doesn't try to use a matrix norm. size1 = np.linalg.norm(vec1, ord=2) size2 = np.linalg.norm(vec2, ord=2) if size1 == 0: return size2 <= eps elif size2 == 0: return size1 <= eps else: upper_bound = eps * min(size1, size2) return np.linalg.norm(vec1 - vec2, ord=2) <= upper_bound
[ "def", "_vector_close", "(", "vec1", ",", "vec2", ",", "eps", "=", "_EPS", ")", ":", "# NOTE: This relies on ``vec1`` and ``vec2`` being one-dimensional", "# vectors so NumPy doesn't try to use a matrix norm.", "size1", "=", "np", ".", "linalg", ".", "norm", "(", "vec1", ",", "ord", "=", "2", ")", "size2", "=", "np", ".", "linalg", ".", "norm", "(", "vec2", ",", "ord", "=", "2", ")", "if", "size1", "==", "0", ":", "return", "size2", "<=", "eps", "elif", "size2", "==", "0", ":", "return", "size1", "<=", "eps", "else", ":", "upper_bound", "=", "eps", "*", "min", "(", "size1", ",", "size2", ")", "return", "np", ".", "linalg", ".", "norm", "(", "vec1", "-", "vec2", ",", "ord", "=", "2", ")", "<=", "upper_bound" ]
r"""Checks that two vectors are equal to some threshold. Does so by computing :math:`s_1 = \|v_1\|_2` and :math:`s_2 = \|v_2\|_2` and then checking if .. math:: \|v_1 - v_2\|_2 \leq \varepsilon \min(s_1, s_2) where :math:`\varepsilon = 2^{-40} \approx 10^{-12}` is a fixed threshold. In the rare case that one of ``vec1`` or ``vec2`` is the zero vector (i.e. when :math:`\min(s_1, s_2) = 0`) instead checks that the other vector is close enough to zero: .. math:: \|v_1\|_2 = 0 \Longrightarrow \|v_2\|_2 \leq \varepsilon .. note:: This function assumes that both vectors have finite values, i.e. that no NaN or infinite numbers occur. NumPy provides :func:`np.allclose` for coverage of **all** cases. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: vec1 (numpy.ndarray): First vector (1D) for comparison. vec2 (numpy.ndarray): Second vector (1D) for comparison. eps (float): Error threshold. Defaults to :math:`2^{-40}`. Returns: bool: Flag indicating if they are close to precision.
[ "r", "Checks", "that", "two", "vectors", "are", "equal", "to", "some", "threshold", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L36-L86
dhermes/bezier
src/bezier/_helpers.py
_bbox
def _bbox(nodes): """Get the bounding box for set of points. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): A set of points. Returns: Tuple[float, float, float, float]: The left, right, bottom and top bounds for the box. """ left, bottom = np.min(nodes, axis=1) right, top = np.max(nodes, axis=1) return left, right, bottom, top
python
def _bbox(nodes): """Get the bounding box for set of points. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): A set of points. Returns: Tuple[float, float, float, float]: The left, right, bottom and top bounds for the box. """ left, bottom = np.min(nodes, axis=1) right, top = np.max(nodes, axis=1) return left, right, bottom, top
[ "def", "_bbox", "(", "nodes", ")", ":", "left", ",", "bottom", "=", "np", ".", "min", "(", "nodes", ",", "axis", "=", "1", ")", "right", ",", "top", "=", "np", ".", "max", "(", "nodes", ",", "axis", "=", "1", ")", "return", "left", ",", "right", ",", "bottom", ",", "top" ]
Get the bounding box for set of points. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): A set of points. Returns: Tuple[float, float, float, float]: The left, right, bottom and top bounds for the box.
[ "Get", "the", "bounding", "box", "for", "set", "of", "points", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L115-L132
dhermes/bezier
src/bezier/_helpers.py
_contains_nd
def _contains_nd(nodes, point): r"""Predicate indicating if a point is within a bounding box. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): A set of points. point (numpy.ndarray): A 1D NumPy array representing a point in the same dimension as ``nodes``. Returns: bool: Indicating containment. """ min_vals = np.min(nodes, axis=1) if not np.all(min_vals <= point): return False max_vals = np.max(nodes, axis=1) if not np.all(point <= max_vals): return False return True
python
def _contains_nd(nodes, point): r"""Predicate indicating if a point is within a bounding box. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): A set of points. point (numpy.ndarray): A 1D NumPy array representing a point in the same dimension as ``nodes``. Returns: bool: Indicating containment. """ min_vals = np.min(nodes, axis=1) if not np.all(min_vals <= point): return False max_vals = np.max(nodes, axis=1) if not np.all(point <= max_vals): return False return True
[ "def", "_contains_nd", "(", "nodes", ",", "point", ")", ":", "min_vals", "=", "np", ".", "min", "(", "nodes", ",", "axis", "=", "1", ")", "if", "not", "np", ".", "all", "(", "min_vals", "<=", "point", ")", ":", "return", "False", "max_vals", "=", "np", ".", "max", "(", "nodes", ",", "axis", "=", "1", ")", "if", "not", "np", ".", "all", "(", "point", "<=", "max_vals", ")", ":", "return", "False", "return", "True" ]
r"""Predicate indicating if a point is within a bounding box. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): A set of points. point (numpy.ndarray): A 1D NumPy array representing a point in the same dimension as ``nodes``. Returns: bool: Indicating containment.
[ "r", "Predicate", "indicating", "if", "a", "point", "is", "within", "a", "bounding", "box", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L135-L159
dhermes/bezier
src/bezier/_helpers.py
matrix_product
def matrix_product(mat1, mat2): """Compute the product of two Fortran contiguous matrices. This is to avoid the overhead of NumPy converting to C-contiguous before computing a matrix product. Does so via ``A B = (B^T A^T)^T`` since ``B^T`` and ``A^T`` will be C-contiguous without a copy, then the product ``P = B^T A^T`` will be C-contiguous and we can return the view ``P^T`` without a copy. Args: mat1 (numpy.ndarray): The left-hand side matrix. mat2 (numpy.ndarray): The right-hand side matrix. Returns: numpy.ndarray: The product of the two matrices. """ return np.dot(mat2.T, mat1.T).T
python
def matrix_product(mat1, mat2): """Compute the product of two Fortran contiguous matrices. This is to avoid the overhead of NumPy converting to C-contiguous before computing a matrix product. Does so via ``A B = (B^T A^T)^T`` since ``B^T`` and ``A^T`` will be C-contiguous without a copy, then the product ``P = B^T A^T`` will be C-contiguous and we can return the view ``P^T`` without a copy. Args: mat1 (numpy.ndarray): The left-hand side matrix. mat2 (numpy.ndarray): The right-hand side matrix. Returns: numpy.ndarray: The product of the two matrices. """ return np.dot(mat2.T, mat1.T).T
[ "def", "matrix_product", "(", "mat1", ",", "mat2", ")", ":", "return", "np", ".", "dot", "(", "mat2", ".", "T", ",", "mat1", ".", "T", ")", ".", "T" ]
Compute the product of two Fortran contiguous matrices. This is to avoid the overhead of NumPy converting to C-contiguous before computing a matrix product. Does so via ``A B = (B^T A^T)^T`` since ``B^T`` and ``A^T`` will be C-contiguous without a copy, then the product ``P = B^T A^T`` will be C-contiguous and we can return the view ``P^T`` without a copy. Args: mat1 (numpy.ndarray): The left-hand side matrix. mat2 (numpy.ndarray): The right-hand side matrix. Returns: numpy.ndarray: The product of the two matrices.
[ "Compute", "the", "product", "of", "two", "Fortran", "contiguous", "matrices", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L190-L207
dhermes/bezier
src/bezier/_helpers.py
_wiggle_interval
def _wiggle_interval(value, wiggle=0.5 ** 44): r"""Check if ``value`` is in :math:`\left[0, 1\right]`. Allows a little bit of wiggle room outside the interval. Any value within ``wiggle`` of ``0.0` will be converted to ``0.0` and similar for ``1.0``. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: value (float): Value to check in interval. wiggle (Optional[float]): The amount of wiggle room around the the endpoints ``0.0`` and ``1.0``. Returns: Tuple[float, bool]: Pair of * The ``value`` if it's in the interval, or ``0`` or ``1`` if the value lies slightly outside. If the ``value`` is too far outside the unit interval, will be NaN. * Boolean indicating if the ``value`` is inside the unit interval. """ if -wiggle < value < wiggle: return 0.0, True elif wiggle <= value <= 1.0 - wiggle: return value, True elif 1.0 - wiggle < value < 1.0 + wiggle: return 1.0, True else: return np.nan, False
python
def _wiggle_interval(value, wiggle=0.5 ** 44): r"""Check if ``value`` is in :math:`\left[0, 1\right]`. Allows a little bit of wiggle room outside the interval. Any value within ``wiggle`` of ``0.0` will be converted to ``0.0` and similar for ``1.0``. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: value (float): Value to check in interval. wiggle (Optional[float]): The amount of wiggle room around the the endpoints ``0.0`` and ``1.0``. Returns: Tuple[float, bool]: Pair of * The ``value`` if it's in the interval, or ``0`` or ``1`` if the value lies slightly outside. If the ``value`` is too far outside the unit interval, will be NaN. * Boolean indicating if the ``value`` is inside the unit interval. """ if -wiggle < value < wiggle: return 0.0, True elif wiggle <= value <= 1.0 - wiggle: return value, True elif 1.0 - wiggle < value < 1.0 + wiggle: return 1.0, True else: return np.nan, False
[ "def", "_wiggle_interval", "(", "value", ",", "wiggle", "=", "0.5", "**", "44", ")", ":", "if", "-", "wiggle", "<", "value", "<", "wiggle", ":", "return", "0.0", ",", "True", "elif", "wiggle", "<=", "value", "<=", "1.0", "-", "wiggle", ":", "return", "value", ",", "True", "elif", "1.0", "-", "wiggle", "<", "value", "<", "1.0", "+", "wiggle", ":", "return", "1.0", ",", "True", "else", ":", "return", "np", ".", "nan", ",", "False" ]
r"""Check if ``value`` is in :math:`\left[0, 1\right]`. Allows a little bit of wiggle room outside the interval. Any value within ``wiggle`` of ``0.0` will be converted to ``0.0` and similar for ``1.0``. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: value (float): Value to check in interval. wiggle (Optional[float]): The amount of wiggle room around the the endpoints ``0.0`` and ``1.0``. Returns: Tuple[float, bool]: Pair of * The ``value`` if it's in the interval, or ``0`` or ``1`` if the value lies slightly outside. If the ``value`` is too far outside the unit interval, will be NaN. * Boolean indicating if the ``value`` is inside the unit interval.
[ "r", "Check", "if", "value", "is", "in", ":", "math", ":", "\\", "left", "[", "0", "1", "\\", "right", "]", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L210-L245
dhermes/bezier
src/bezier/_helpers.py
cross_product_compare
def cross_product_compare(start, candidate1, candidate2): """Compare two relative changes by their cross-product. This is meant to be a way to determine which vector is more "inside" relative to ``start``. .. note:: This is a helper for :func:`_simple_convex_hull`. Args: start (numpy.ndarray): The start vector (as 1D NumPy array with 2 elements). candidate1 (numpy.ndarray): The first candidate vector (as 1D NumPy array with 2 elements). candidate2 (numpy.ndarray): The second candidate vector (as 1D NumPy array with 2 elements). Returns: float: The cross product of the two differences. """ delta1 = candidate1 - start delta2 = candidate2 - start return cross_product(delta1, delta2)
python
def cross_product_compare(start, candidate1, candidate2): """Compare two relative changes by their cross-product. This is meant to be a way to determine which vector is more "inside" relative to ``start``. .. note:: This is a helper for :func:`_simple_convex_hull`. Args: start (numpy.ndarray): The start vector (as 1D NumPy array with 2 elements). candidate1 (numpy.ndarray): The first candidate vector (as 1D NumPy array with 2 elements). candidate2 (numpy.ndarray): The second candidate vector (as 1D NumPy array with 2 elements). Returns: float: The cross product of the two differences. """ delta1 = candidate1 - start delta2 = candidate2 - start return cross_product(delta1, delta2)
[ "def", "cross_product_compare", "(", "start", ",", "candidate1", ",", "candidate2", ")", ":", "delta1", "=", "candidate1", "-", "start", "delta2", "=", "candidate2", "-", "start", "return", "cross_product", "(", "delta1", ",", "delta2", ")" ]
Compare two relative changes by their cross-product. This is meant to be a way to determine which vector is more "inside" relative to ``start``. .. note:: This is a helper for :func:`_simple_convex_hull`. Args: start (numpy.ndarray): The start vector (as 1D NumPy array with 2 elements). candidate1 (numpy.ndarray): The first candidate vector (as 1D NumPy array with 2 elements). candidate2 (numpy.ndarray): The second candidate vector (as 1D NumPy array with 2 elements). Returns: float: The cross product of the two differences.
[ "Compare", "two", "relative", "changes", "by", "their", "cross", "-", "product", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L248-L271
dhermes/bezier
src/bezier/_helpers.py
in_sorted
def in_sorted(values, value): """Checks if a value is in a sorted list. Uses the :mod:`bisect` builtin to find the insertion point for ``value``. Args: values (List[int]): Integers sorted in ascending order. value (int): Value to check if contained in ``values``. Returns: bool: Indicating if the value is contained. """ index = bisect.bisect_left(values, value) if index >= len(values): return False return values[index] == value
python
def in_sorted(values, value): """Checks if a value is in a sorted list. Uses the :mod:`bisect` builtin to find the insertion point for ``value``. Args: values (List[int]): Integers sorted in ascending order. value (int): Value to check if contained in ``values``. Returns: bool: Indicating if the value is contained. """ index = bisect.bisect_left(values, value) if index >= len(values): return False return values[index] == value
[ "def", "in_sorted", "(", "values", ",", "value", ")", ":", "index", "=", "bisect", ".", "bisect_left", "(", "values", ",", "value", ")", "if", "index", ">=", "len", "(", "values", ")", ":", "return", "False", "return", "values", "[", "index", "]", "==", "value" ]
Checks if a value is in a sorted list. Uses the :mod:`bisect` builtin to find the insertion point for ``value``. Args: values (List[int]): Integers sorted in ascending order. value (int): Value to check if contained in ``values``. Returns: bool: Indicating if the value is contained.
[ "Checks", "if", "a", "value", "is", "in", "a", "sorted", "list", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L274-L291
dhermes/bezier
src/bezier/_helpers.py
_simple_convex_hull
def _simple_convex_hull(points): r"""Compute the convex hull for a set of points. .. _wikibooks: https://en.wikibooks.org/wiki/Algorithm_Implementation/\ Geometry/Convex_hull/Monotone_chain This uses Andrew's monotone chain convex hull algorithm and this code used a `wikibooks`_ implementation as motivation. tion. The code there is licensed CC BY-SA 3.0. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Note that ``scipy.spatial.ConvexHull`` can do this as well (via Qhull), but that would require a hard dependency on ``scipy`` and that helper computes much more than we need. .. note:: This computes the convex hull in a "naive" way. It's expected that internal callers of this function will have a small number of points so ``n log n`` vs. ``n^2`` vs. ``n`` aren't that relevant. Args: points (numpy.ndarray): A ``2 x N`` array (``float64``) of points. Returns: numpy.ndarray: The ``2 x N`` array (``float64``) of ordered points in the polygonal convex hull. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-branches if points.size == 0: return points # First, drop duplicates. unique_points = np.unique(points, axis=1) _, num_points = unique_points.shape if num_points < 2: return unique_points # Then sort the data in left-to-right order (and break ties by y-value). points = np.empty((2, num_points), order="F") for index, xy_val in enumerate( sorted(tuple(column) for column in unique_points.T) ): points[:, index] = xy_val # After sorting, if there are only 2 points, return. if num_points < 3: return points # Build lower hull lower = [0, 1] for index in six.moves.xrange(2, num_points): point2 = points[:, index] while len(lower) >= 2: point0 = points[:, lower[-2]] point1 = points[:, lower[-1]] if cross_product_compare(point0, point1, point2) > 0: break else: lower.pop() lower.append(index) # Build upper hull upper = [num_points - 1] for index in six.moves.xrange(num_points - 2, -1, -1): # Don't consider indices from the lower hull (other than the ends). if index > 0 and in_sorted(lower, index): continue point2 = points[:, index] while len(upper) >= 2: point0 = points[:, upper[-2]] point1 = points[:, upper[-1]] if cross_product_compare(point0, point1, point2) > 0: break else: upper.pop() upper.append(index) # **Both** corners are double counted. size_polygon = len(lower) + len(upper) - 2 polygon = np.empty((2, size_polygon), order="F") for index, column in enumerate(lower[:-1]): polygon[:, index] = points[:, column] index_start = len(lower) - 1 for index, column in enumerate(upper[:-1]): polygon[:, index + index_start] = points[:, column] return polygon
python
def _simple_convex_hull(points): r"""Compute the convex hull for a set of points. .. _wikibooks: https://en.wikibooks.org/wiki/Algorithm_Implementation/\ Geometry/Convex_hull/Monotone_chain This uses Andrew's monotone chain convex hull algorithm and this code used a `wikibooks`_ implementation as motivation. tion. The code there is licensed CC BY-SA 3.0. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Note that ``scipy.spatial.ConvexHull`` can do this as well (via Qhull), but that would require a hard dependency on ``scipy`` and that helper computes much more than we need. .. note:: This computes the convex hull in a "naive" way. It's expected that internal callers of this function will have a small number of points so ``n log n`` vs. ``n^2`` vs. ``n`` aren't that relevant. Args: points (numpy.ndarray): A ``2 x N`` array (``float64``) of points. Returns: numpy.ndarray: The ``2 x N`` array (``float64``) of ordered points in the polygonal convex hull. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-branches if points.size == 0: return points # First, drop duplicates. unique_points = np.unique(points, axis=1) _, num_points = unique_points.shape if num_points < 2: return unique_points # Then sort the data in left-to-right order (and break ties by y-value). points = np.empty((2, num_points), order="F") for index, xy_val in enumerate( sorted(tuple(column) for column in unique_points.T) ): points[:, index] = xy_val # After sorting, if there are only 2 points, return. if num_points < 3: return points # Build lower hull lower = [0, 1] for index in six.moves.xrange(2, num_points): point2 = points[:, index] while len(lower) >= 2: point0 = points[:, lower[-2]] point1 = points[:, lower[-1]] if cross_product_compare(point0, point1, point2) > 0: break else: lower.pop() lower.append(index) # Build upper hull upper = [num_points - 1] for index in six.moves.xrange(num_points - 2, -1, -1): # Don't consider indices from the lower hull (other than the ends). if index > 0 and in_sorted(lower, index): continue point2 = points[:, index] while len(upper) >= 2: point0 = points[:, upper[-2]] point1 = points[:, upper[-1]] if cross_product_compare(point0, point1, point2) > 0: break else: upper.pop() upper.append(index) # **Both** corners are double counted. size_polygon = len(lower) + len(upper) - 2 polygon = np.empty((2, size_polygon), order="F") for index, column in enumerate(lower[:-1]): polygon[:, index] = points[:, column] index_start = len(lower) - 1 for index, column in enumerate(upper[:-1]): polygon[:, index + index_start] = points[:, column] return polygon
[ "def", "_simple_convex_hull", "(", "points", ")", ":", "# NOTE: There is no corresponding \"enable\", but the disable only applies", "# in this lexical scope.", "# pylint: disable=too-many-branches", "if", "points", ".", "size", "==", "0", ":", "return", "points", "# First, drop duplicates.", "unique_points", "=", "np", ".", "unique", "(", "points", ",", "axis", "=", "1", ")", "_", ",", "num_points", "=", "unique_points", ".", "shape", "if", "num_points", "<", "2", ":", "return", "unique_points", "# Then sort the data in left-to-right order (and break ties by y-value).", "points", "=", "np", ".", "empty", "(", "(", "2", ",", "num_points", ")", ",", "order", "=", "\"F\"", ")", "for", "index", ",", "xy_val", "in", "enumerate", "(", "sorted", "(", "tuple", "(", "column", ")", "for", "column", "in", "unique_points", ".", "T", ")", ")", ":", "points", "[", ":", ",", "index", "]", "=", "xy_val", "# After sorting, if there are only 2 points, return.", "if", "num_points", "<", "3", ":", "return", "points", "# Build lower hull", "lower", "=", "[", "0", ",", "1", "]", "for", "index", "in", "six", ".", "moves", ".", "xrange", "(", "2", ",", "num_points", ")", ":", "point2", "=", "points", "[", ":", ",", "index", "]", "while", "len", "(", "lower", ")", ">=", "2", ":", "point0", "=", "points", "[", ":", ",", "lower", "[", "-", "2", "]", "]", "point1", "=", "points", "[", ":", ",", "lower", "[", "-", "1", "]", "]", "if", "cross_product_compare", "(", "point0", ",", "point1", ",", "point2", ")", ">", "0", ":", "break", "else", ":", "lower", ".", "pop", "(", ")", "lower", ".", "append", "(", "index", ")", "# Build upper hull", "upper", "=", "[", "num_points", "-", "1", "]", "for", "index", "in", "six", ".", "moves", ".", "xrange", "(", "num_points", "-", "2", ",", "-", "1", ",", "-", "1", ")", ":", "# Don't consider indices from the lower hull (other than the ends).", "if", "index", ">", "0", "and", "in_sorted", "(", "lower", ",", "index", ")", ":", "continue", "point2", "=", "points", "[", ":", ",", "index", "]", "while", "len", "(", "upper", ")", ">=", "2", ":", "point0", "=", "points", "[", ":", ",", "upper", "[", "-", "2", "]", "]", "point1", "=", "points", "[", ":", ",", "upper", "[", "-", "1", "]", "]", "if", "cross_product_compare", "(", "point0", ",", "point1", ",", "point2", ")", ">", "0", ":", "break", "else", ":", "upper", ".", "pop", "(", ")", "upper", ".", "append", "(", "index", ")", "# **Both** corners are double counted.", "size_polygon", "=", "len", "(", "lower", ")", "+", "len", "(", "upper", ")", "-", "2", "polygon", "=", "np", ".", "empty", "(", "(", "2", ",", "size_polygon", ")", ",", "order", "=", "\"F\"", ")", "for", "index", ",", "column", "in", "enumerate", "(", "lower", "[", ":", "-", "1", "]", ")", ":", "polygon", "[", ":", ",", "index", "]", "=", "points", "[", ":", ",", "column", "]", "index_start", "=", "len", "(", "lower", ")", "-", "1", "for", "index", ",", "column", "in", "enumerate", "(", "upper", "[", ":", "-", "1", "]", ")", ":", "polygon", "[", ":", ",", "index", "+", "index_start", "]", "=", "points", "[", ":", ",", "column", "]", "return", "polygon" ]
r"""Compute the convex hull for a set of points. .. _wikibooks: https://en.wikibooks.org/wiki/Algorithm_Implementation/\ Geometry/Convex_hull/Monotone_chain This uses Andrew's monotone chain convex hull algorithm and this code used a `wikibooks`_ implementation as motivation. tion. The code there is licensed CC BY-SA 3.0. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Note that ``scipy.spatial.ConvexHull`` can do this as well (via Qhull), but that would require a hard dependency on ``scipy`` and that helper computes much more than we need. .. note:: This computes the convex hull in a "naive" way. It's expected that internal callers of this function will have a small number of points so ``n log n`` vs. ``n^2`` vs. ``n`` aren't that relevant. Args: points (numpy.ndarray): A ``2 x N`` array (``float64``) of points. Returns: numpy.ndarray: The ``2 x N`` array (``float64``) of ordered points in the polygonal convex hull.
[ "r", "Compute", "the", "convex", "hull", "for", "a", "set", "of", "points", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L294-L384
dhermes/bezier
src/bezier/_helpers.py
is_separating
def is_separating(direction, polygon1, polygon2): """Checks if a given ``direction`` is a separating line for two polygons. .. note:: This is a helper for :func:`_polygon_collide`. Args: direction (numpy.ndarray): A 1D ``2``-array (``float64``) of a potential separating line for the two polygons. polygon1 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. polygon2 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. Returns: bool: Flag indicating if ``direction`` is a separating line. """ # NOTE: We assume throughout that ``norm_squared != 0``. If it **were** # zero that would mean the ``direction`` corresponds to an # invalid edge. norm_squared = direction[0] * direction[0] + direction[1] * direction[1] params = [] vertex = np.empty((2,), order="F") for polygon in (polygon1, polygon2): _, polygon_size = polygon.shape min_param = np.inf max_param = -np.inf for index in six.moves.xrange(polygon_size): vertex[:] = polygon[:, index] param = cross_product(direction, vertex) / norm_squared min_param = min(min_param, param) max_param = max(max_param, param) params.append((min_param, max_param)) # NOTE: The indexing is based on: # params[0] = (min_param1, max_param1) # params[1] = (min_param2, max_param2) return params[0][0] > params[1][1] or params[0][1] < params[1][0]
python
def is_separating(direction, polygon1, polygon2): """Checks if a given ``direction`` is a separating line for two polygons. .. note:: This is a helper for :func:`_polygon_collide`. Args: direction (numpy.ndarray): A 1D ``2``-array (``float64``) of a potential separating line for the two polygons. polygon1 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. polygon2 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. Returns: bool: Flag indicating if ``direction`` is a separating line. """ # NOTE: We assume throughout that ``norm_squared != 0``. If it **were** # zero that would mean the ``direction`` corresponds to an # invalid edge. norm_squared = direction[0] * direction[0] + direction[1] * direction[1] params = [] vertex = np.empty((2,), order="F") for polygon in (polygon1, polygon2): _, polygon_size = polygon.shape min_param = np.inf max_param = -np.inf for index in six.moves.xrange(polygon_size): vertex[:] = polygon[:, index] param = cross_product(direction, vertex) / norm_squared min_param = min(min_param, param) max_param = max(max_param, param) params.append((min_param, max_param)) # NOTE: The indexing is based on: # params[0] = (min_param1, max_param1) # params[1] = (min_param2, max_param2) return params[0][0] > params[1][1] or params[0][1] < params[1][0]
[ "def", "is_separating", "(", "direction", ",", "polygon1", ",", "polygon2", ")", ":", "# NOTE: We assume throughout that ``norm_squared != 0``. If it **were**", "# zero that would mean the ``direction`` corresponds to an", "# invalid edge.", "norm_squared", "=", "direction", "[", "0", "]", "*", "direction", "[", "0", "]", "+", "direction", "[", "1", "]", "*", "direction", "[", "1", "]", "params", "=", "[", "]", "vertex", "=", "np", ".", "empty", "(", "(", "2", ",", ")", ",", "order", "=", "\"F\"", ")", "for", "polygon", "in", "(", "polygon1", ",", "polygon2", ")", ":", "_", ",", "polygon_size", "=", "polygon", ".", "shape", "min_param", "=", "np", ".", "inf", "max_param", "=", "-", "np", ".", "inf", "for", "index", "in", "six", ".", "moves", ".", "xrange", "(", "polygon_size", ")", ":", "vertex", "[", ":", "]", "=", "polygon", "[", ":", ",", "index", "]", "param", "=", "cross_product", "(", "direction", ",", "vertex", ")", "/", "norm_squared", "min_param", "=", "min", "(", "min_param", ",", "param", ")", "max_param", "=", "max", "(", "max_param", ",", "param", ")", "params", ".", "append", "(", "(", "min_param", ",", "max_param", ")", ")", "# NOTE: The indexing is based on:", "# params[0] = (min_param1, max_param1)", "# params[1] = (min_param2, max_param2)", "return", "params", "[", "0", "]", "[", "0", "]", ">", "params", "[", "1", "]", "[", "1", "]", "or", "params", "[", "0", "]", "[", "1", "]", "<", "params", "[", "1", "]", "[", "0", "]" ]
Checks if a given ``direction`` is a separating line for two polygons. .. note:: This is a helper for :func:`_polygon_collide`. Args: direction (numpy.ndarray): A 1D ``2``-array (``float64``) of a potential separating line for the two polygons. polygon1 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. polygon2 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. Returns: bool: Flag indicating if ``direction`` is a separating line.
[ "Checks", "if", "a", "given", "direction", "is", "a", "separating", "line", "for", "two", "polygons", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L387-L424
dhermes/bezier
src/bezier/_helpers.py
_polygon_collide
def _polygon_collide(polygon1, polygon2): """Determines if two **convex** polygons collide. .. _SAT: https://en.wikipedia.org/wiki/Hyperplane_separation_theorem .. _see also: https://hackmd.io/s/ryFmIZrsl This code uses the Separating axis theorem (`SAT`_) to quickly determine if the polygons intersect. `See also`_. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: polygon1 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. polygon2 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. Returns: bool: Flag indicating if the two polygons collide. """ direction = np.empty((2,), order="F") for polygon in (polygon1, polygon2): _, polygon_size = polygon.shape for index in six.moves.xrange(polygon_size): # NOTE: When ``index == 0`` this will "wrap around" and refer # to index ``-1``. direction[:] = polygon[:, index] - polygon[:, index - 1] if is_separating(direction, polygon1, polygon2): return False return True
python
def _polygon_collide(polygon1, polygon2): """Determines if two **convex** polygons collide. .. _SAT: https://en.wikipedia.org/wiki/Hyperplane_separation_theorem .. _see also: https://hackmd.io/s/ryFmIZrsl This code uses the Separating axis theorem (`SAT`_) to quickly determine if the polygons intersect. `See also`_. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: polygon1 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. polygon2 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. Returns: bool: Flag indicating if the two polygons collide. """ direction = np.empty((2,), order="F") for polygon in (polygon1, polygon2): _, polygon_size = polygon.shape for index in six.moves.xrange(polygon_size): # NOTE: When ``index == 0`` this will "wrap around" and refer # to index ``-1``. direction[:] = polygon[:, index] - polygon[:, index - 1] if is_separating(direction, polygon1, polygon2): return False return True
[ "def", "_polygon_collide", "(", "polygon1", ",", "polygon2", ")", ":", "direction", "=", "np", ".", "empty", "(", "(", "2", ",", ")", ",", "order", "=", "\"F\"", ")", "for", "polygon", "in", "(", "polygon1", ",", "polygon2", ")", ":", "_", ",", "polygon_size", "=", "polygon", ".", "shape", "for", "index", "in", "six", ".", "moves", ".", "xrange", "(", "polygon_size", ")", ":", "# NOTE: When ``index == 0`` this will \"wrap around\" and refer", "# to index ``-1``.", "direction", "[", ":", "]", "=", "polygon", "[", ":", ",", "index", "]", "-", "polygon", "[", ":", ",", "index", "-", "1", "]", "if", "is_separating", "(", "direction", ",", "polygon1", ",", "polygon2", ")", ":", "return", "False", "return", "True" ]
Determines if two **convex** polygons collide. .. _SAT: https://en.wikipedia.org/wiki/Hyperplane_separation_theorem .. _see also: https://hackmd.io/s/ryFmIZrsl This code uses the Separating axis theorem (`SAT`_) to quickly determine if the polygons intersect. `See also`_. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: polygon1 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. polygon2 (numpy.ndarray): A ``2 x N`` array (``float64``) of ordered points in a polygon. Returns: bool: Flag indicating if the two polygons collide.
[ "Determines", "if", "two", "**", "convex", "**", "polygons", "collide", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L427-L460
dhermes/bezier
src/bezier/_helpers.py
solve2x2
def solve2x2(lhs, rhs): """Solve a square 2 x 2 system via LU factorization. This is meant to be a stand-in for LAPACK's ``dgesv``, which just wraps two calls to ``dgetrf`` and ``dgetrs``. We wrap for two reasons: * We seek to avoid exceptions as part of the control flow (which is what :func`numpy.linalg.solve` does). * We seek to avoid excessive type- and size-checking, since this special case is already known. Args: lhs (numpy.ndarray) A ``2 x 2`` array of real numbers. rhs (numpy.ndarray) A 1D array of 2 real numbers. Returns: Tuple[bool, float, float]: A triple of * A flag indicating if ``lhs`` is a singular matrix. * The first component of the solution. * The second component of the solution. """ # A <--> lhs[0, 0] # B <--> lhs[0, 1] # C <--> lhs[1, 0] # D <--> lhs[1, 1] # E <--> rhs[0] # F <--> rhs[1] if np.abs(lhs[1, 0]) > np.abs(lhs[0, 0]): # NOTE: We know there is no division by zero here since ``C`` # is **strictly** bigger than **some** value (in magnitude). # [A | B][x] = [E] # [C | D][y] [F] ratio = lhs[0, 0] / lhs[1, 0] # r = A / C # [A - rC | B - rD][x] [E - rF] # [C | D ][y] = [F ] # ==> 0x + (B - rD) y = E - rF denominator = lhs[0, 1] - ratio * lhs[1, 1] if denominator == 0.0: return True, None, None y_val = (rhs[0] - ratio * rhs[1]) / denominator # Cx + Dy = F ==> x = (F - Dy) / C x_val = (rhs[1] - lhs[1, 1] * y_val) / lhs[1, 0] return False, x_val, y_val else: if lhs[0, 0] == 0.0: return True, None, None # [A | B][x] = [E] # [C | D][y] [F] ratio = lhs[1, 0] / lhs[0, 0] # r = C / A # [A | B ][x] = [E ] # [C - rA | D - rB][y] [F - rE] # ==> 0x + (D - rB) y = F - rE denominator = lhs[1, 1] - ratio * lhs[0, 1] if denominator == 0.0: return True, None, None y_val = (rhs[1] - ratio * rhs[0]) / denominator # Ax + By = E ==> x = (E - B y) / A x_val = (rhs[0] - lhs[0, 1] * y_val) / lhs[0, 0] return False, x_val, y_val
python
def solve2x2(lhs, rhs): """Solve a square 2 x 2 system via LU factorization. This is meant to be a stand-in for LAPACK's ``dgesv``, which just wraps two calls to ``dgetrf`` and ``dgetrs``. We wrap for two reasons: * We seek to avoid exceptions as part of the control flow (which is what :func`numpy.linalg.solve` does). * We seek to avoid excessive type- and size-checking, since this special case is already known. Args: lhs (numpy.ndarray) A ``2 x 2`` array of real numbers. rhs (numpy.ndarray) A 1D array of 2 real numbers. Returns: Tuple[bool, float, float]: A triple of * A flag indicating if ``lhs`` is a singular matrix. * The first component of the solution. * The second component of the solution. """ # A <--> lhs[0, 0] # B <--> lhs[0, 1] # C <--> lhs[1, 0] # D <--> lhs[1, 1] # E <--> rhs[0] # F <--> rhs[1] if np.abs(lhs[1, 0]) > np.abs(lhs[0, 0]): # NOTE: We know there is no division by zero here since ``C`` # is **strictly** bigger than **some** value (in magnitude). # [A | B][x] = [E] # [C | D][y] [F] ratio = lhs[0, 0] / lhs[1, 0] # r = A / C # [A - rC | B - rD][x] [E - rF] # [C | D ][y] = [F ] # ==> 0x + (B - rD) y = E - rF denominator = lhs[0, 1] - ratio * lhs[1, 1] if denominator == 0.0: return True, None, None y_val = (rhs[0] - ratio * rhs[1]) / denominator # Cx + Dy = F ==> x = (F - Dy) / C x_val = (rhs[1] - lhs[1, 1] * y_val) / lhs[1, 0] return False, x_val, y_val else: if lhs[0, 0] == 0.0: return True, None, None # [A | B][x] = [E] # [C | D][y] [F] ratio = lhs[1, 0] / lhs[0, 0] # r = C / A # [A | B ][x] = [E ] # [C - rA | D - rB][y] [F - rE] # ==> 0x + (D - rB) y = F - rE denominator = lhs[1, 1] - ratio * lhs[0, 1] if denominator == 0.0: return True, None, None y_val = (rhs[1] - ratio * rhs[0]) / denominator # Ax + By = E ==> x = (E - B y) / A x_val = (rhs[0] - lhs[0, 1] * y_val) / lhs[0, 0] return False, x_val, y_val
[ "def", "solve2x2", "(", "lhs", ",", "rhs", ")", ":", "# A <--> lhs[0, 0]", "# B <--> lhs[0, 1]", "# C <--> lhs[1, 0]", "# D <--> lhs[1, 1]", "# E <--> rhs[0]", "# F <--> rhs[1]", "if", "np", ".", "abs", "(", "lhs", "[", "1", ",", "0", "]", ")", ">", "np", ".", "abs", "(", "lhs", "[", "0", ",", "0", "]", ")", ":", "# NOTE: We know there is no division by zero here since ``C``", "# is **strictly** bigger than **some** value (in magnitude).", "# [A | B][x] = [E]", "# [C | D][y] [F]", "ratio", "=", "lhs", "[", "0", ",", "0", "]", "/", "lhs", "[", "1", ",", "0", "]", "# r = A / C", "# [A - rC | B - rD][x] [E - rF]", "# [C | D ][y] = [F ]", "# ==> 0x + (B - rD) y = E - rF", "denominator", "=", "lhs", "[", "0", ",", "1", "]", "-", "ratio", "*", "lhs", "[", "1", ",", "1", "]", "if", "denominator", "==", "0.0", ":", "return", "True", ",", "None", ",", "None", "y_val", "=", "(", "rhs", "[", "0", "]", "-", "ratio", "*", "rhs", "[", "1", "]", ")", "/", "denominator", "# Cx + Dy = F ==> x = (F - Dy) / C", "x_val", "=", "(", "rhs", "[", "1", "]", "-", "lhs", "[", "1", ",", "1", "]", "*", "y_val", ")", "/", "lhs", "[", "1", ",", "0", "]", "return", "False", ",", "x_val", ",", "y_val", "else", ":", "if", "lhs", "[", "0", ",", "0", "]", "==", "0.0", ":", "return", "True", ",", "None", ",", "None", "# [A | B][x] = [E]", "# [C | D][y] [F]", "ratio", "=", "lhs", "[", "1", ",", "0", "]", "/", "lhs", "[", "0", ",", "0", "]", "# r = C / A", "# [A | B ][x] = [E ]", "# [C - rA | D - rB][y] [F - rE]", "# ==> 0x + (D - rB) y = F - rE", "denominator", "=", "lhs", "[", "1", ",", "1", "]", "-", "ratio", "*", "lhs", "[", "0", ",", "1", "]", "if", "denominator", "==", "0.0", ":", "return", "True", ",", "None", ",", "None", "y_val", "=", "(", "rhs", "[", "1", "]", "-", "ratio", "*", "rhs", "[", "0", "]", ")", "/", "denominator", "# Ax + By = E ==> x = (E - B y) / A", "x_val", "=", "(", "rhs", "[", "0", "]", "-", "lhs", "[", "0", ",", "1", "]", "*", "y_val", ")", "/", "lhs", "[", "0", ",", "0", "]", "return", "False", ",", "x_val", ",", "y_val" ]
Solve a square 2 x 2 system via LU factorization. This is meant to be a stand-in for LAPACK's ``dgesv``, which just wraps two calls to ``dgetrf`` and ``dgetrs``. We wrap for two reasons: * We seek to avoid exceptions as part of the control flow (which is what :func`numpy.linalg.solve` does). * We seek to avoid excessive type- and size-checking, since this special case is already known. Args: lhs (numpy.ndarray) A ``2 x 2`` array of real numbers. rhs (numpy.ndarray) A 1D array of 2 real numbers. Returns: Tuple[bool, float, float]: A triple of * A flag indicating if ``lhs`` is a singular matrix. * The first component of the solution. * The second component of the solution.
[ "Solve", "a", "square", "2", "x", "2", "system", "via", "LU", "factorization", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_helpers.py#L463-L528
dhermes/bezier
src/bezier/_plot_helpers.py
add_plot_boundary
def add_plot_boundary(ax, padding=0.125): """Add a buffer of empty space around a plot boundary. .. note:: This only uses ``line`` data from the axis. It **could** use ``patch`` data, but doesn't at this time. Args: ax (matplotlib.artist.Artist): A matplotlib axis. padding (Optional[float]): Amount (as a fraction of width and height) of padding to add around data. Defaults to ``0.125``. """ nodes = np.asfortranarray( np.vstack([line.get_xydata() for line in ax.lines]).T ) left, right, bottom, top = _helpers.bbox(nodes) center_x = 0.5 * (right + left) delta_x = right - left center_y = 0.5 * (top + bottom) delta_y = top - bottom multiplier = (1.0 + padding) * 0.5 ax.set_xlim( center_x - multiplier * delta_x, center_x + multiplier * delta_x ) ax.set_ylim( center_y - multiplier * delta_y, center_y + multiplier * delta_y )
python
def add_plot_boundary(ax, padding=0.125): """Add a buffer of empty space around a plot boundary. .. note:: This only uses ``line`` data from the axis. It **could** use ``patch`` data, but doesn't at this time. Args: ax (matplotlib.artist.Artist): A matplotlib axis. padding (Optional[float]): Amount (as a fraction of width and height) of padding to add around data. Defaults to ``0.125``. """ nodes = np.asfortranarray( np.vstack([line.get_xydata() for line in ax.lines]).T ) left, right, bottom, top = _helpers.bbox(nodes) center_x = 0.5 * (right + left) delta_x = right - left center_y = 0.5 * (top + bottom) delta_y = top - bottom multiplier = (1.0 + padding) * 0.5 ax.set_xlim( center_x - multiplier * delta_x, center_x + multiplier * delta_x ) ax.set_ylim( center_y - multiplier * delta_y, center_y + multiplier * delta_y )
[ "def", "add_plot_boundary", "(", "ax", ",", "padding", "=", "0.125", ")", ":", "nodes", "=", "np", ".", "asfortranarray", "(", "np", ".", "vstack", "(", "[", "line", ".", "get_xydata", "(", ")", "for", "line", "in", "ax", ".", "lines", "]", ")", ".", "T", ")", "left", ",", "right", ",", "bottom", ",", "top", "=", "_helpers", ".", "bbox", "(", "nodes", ")", "center_x", "=", "0.5", "*", "(", "right", "+", "left", ")", "delta_x", "=", "right", "-", "left", "center_y", "=", "0.5", "*", "(", "top", "+", "bottom", ")", "delta_y", "=", "top", "-", "bottom", "multiplier", "=", "(", "1.0", "+", "padding", ")", "*", "0.5", "ax", ".", "set_xlim", "(", "center_x", "-", "multiplier", "*", "delta_x", ",", "center_x", "+", "multiplier", "*", "delta_x", ")", "ax", ".", "set_ylim", "(", "center_y", "-", "multiplier", "*", "delta_y", ",", "center_y", "+", "multiplier", "*", "delta_y", ")" ]
Add a buffer of empty space around a plot boundary. .. note:: This only uses ``line`` data from the axis. It **could** use ``patch`` data, but doesn't at this time. Args: ax (matplotlib.artist.Artist): A matplotlib axis. padding (Optional[float]): Amount (as a fraction of width and height) of padding to add around data. Defaults to ``0.125``.
[ "Add", "a", "buffer", "of", "empty", "space", "around", "a", "plot", "boundary", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_plot_helpers.py#L34-L61
dhermes/bezier
src/bezier/_plot_helpers.py
add_patch
def add_patch(ax, color, pts_per_edge, *edges): """Add a polygonal surface patch to a plot. Args: ax (matplotlib.artist.Artist): A matplotlib axis. color (Tuple[float, float, float]): Color as RGB profile. pts_per_edge (int): Number of points to use in polygonal approximation of edge. edges (Tuple[~bezier.curve.Curve, ...]): Curved edges defining a boundary. """ from matplotlib import patches from matplotlib import path as _path_mod s_vals = np.linspace(0.0, 1.0, pts_per_edge) # Evaluate points on each edge. all_points = [] for edge in edges: points = edge.evaluate_multi(s_vals) # We assume the edges overlap and leave out the first point # in each. all_points.append(points[:, 1:]) # Add first point as last point (polygon is closed). first_edge = all_points[0] all_points.append(first_edge[:, [0]]) # Add boundary first. polygon = np.asfortranarray(np.hstack(all_points)) line, = ax.plot(polygon[0, :], polygon[1, :], color=color) # Reset ``color`` in case it was ``None`` and set from color wheel. color = line.get_color() # ``polygon`` is stored Fortran-contiguous with ``x-y`` points in each # column but ``Path()`` wants ``x-y`` points in each row. path = _path_mod.Path(polygon.T) patch = patches.PathPatch(path, facecolor=color, alpha=0.625) ax.add_patch(patch)
python
def add_patch(ax, color, pts_per_edge, *edges): """Add a polygonal surface patch to a plot. Args: ax (matplotlib.artist.Artist): A matplotlib axis. color (Tuple[float, float, float]): Color as RGB profile. pts_per_edge (int): Number of points to use in polygonal approximation of edge. edges (Tuple[~bezier.curve.Curve, ...]): Curved edges defining a boundary. """ from matplotlib import patches from matplotlib import path as _path_mod s_vals = np.linspace(0.0, 1.0, pts_per_edge) # Evaluate points on each edge. all_points = [] for edge in edges: points = edge.evaluate_multi(s_vals) # We assume the edges overlap and leave out the first point # in each. all_points.append(points[:, 1:]) # Add first point as last point (polygon is closed). first_edge = all_points[0] all_points.append(first_edge[:, [0]]) # Add boundary first. polygon = np.asfortranarray(np.hstack(all_points)) line, = ax.plot(polygon[0, :], polygon[1, :], color=color) # Reset ``color`` in case it was ``None`` and set from color wheel. color = line.get_color() # ``polygon`` is stored Fortran-contiguous with ``x-y`` points in each # column but ``Path()`` wants ``x-y`` points in each row. path = _path_mod.Path(polygon.T) patch = patches.PathPatch(path, facecolor=color, alpha=0.625) ax.add_patch(patch)
[ "def", "add_patch", "(", "ax", ",", "color", ",", "pts_per_edge", ",", "*", "edges", ")", ":", "from", "matplotlib", "import", "patches", "from", "matplotlib", "import", "path", "as", "_path_mod", "s_vals", "=", "np", ".", "linspace", "(", "0.0", ",", "1.0", ",", "pts_per_edge", ")", "# Evaluate points on each edge.", "all_points", "=", "[", "]", "for", "edge", "in", "edges", ":", "points", "=", "edge", ".", "evaluate_multi", "(", "s_vals", ")", "# We assume the edges overlap and leave out the first point", "# in each.", "all_points", ".", "append", "(", "points", "[", ":", ",", "1", ":", "]", ")", "# Add first point as last point (polygon is closed).", "first_edge", "=", "all_points", "[", "0", "]", "all_points", ".", "append", "(", "first_edge", "[", ":", ",", "[", "0", "]", "]", ")", "# Add boundary first.", "polygon", "=", "np", ".", "asfortranarray", "(", "np", ".", "hstack", "(", "all_points", ")", ")", "line", ",", "=", "ax", ".", "plot", "(", "polygon", "[", "0", ",", ":", "]", ",", "polygon", "[", "1", ",", ":", "]", ",", "color", "=", "color", ")", "# Reset ``color`` in case it was ``None`` and set from color wheel.", "color", "=", "line", ".", "get_color", "(", ")", "# ``polygon`` is stored Fortran-contiguous with ``x-y`` points in each", "# column but ``Path()`` wants ``x-y`` points in each row.", "path", "=", "_path_mod", ".", "Path", "(", "polygon", ".", "T", ")", "patch", "=", "patches", ".", "PathPatch", "(", "path", ",", "facecolor", "=", "color", ",", "alpha", "=", "0.625", ")", "ax", ".", "add_patch", "(", "patch", ")" ]
Add a polygonal surface patch to a plot. Args: ax (matplotlib.artist.Artist): A matplotlib axis. color (Tuple[float, float, float]): Color as RGB profile. pts_per_edge (int): Number of points to use in polygonal approximation of edge. edges (Tuple[~bezier.curve.Curve, ...]): Curved edges defining a boundary.
[ "Add", "a", "polygonal", "surface", "patch", "to", "a", "plot", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_plot_helpers.py#L64-L98
dhermes/bezier
docs/custom_html_writer.py
CustomHTMLWriter.visit_literal_block
def visit_literal_block(self, node): """Visit a ``literal_block`` node. This verifies the state of each literal / code block. """ language = node.attributes.get("language", "") test_type = node.attributes.get("testnodetype", "") if test_type != "doctest": if language.lower() in ("", "python"): msg = _LITERAL_ERR_TEMPLATE.format( node.rawsource, language, test_type ) raise errors.ExtensionError(msg) # The base classes are not new-style, so we can't use super(). return html.HTMLTranslator.visit_literal_block(self, node)
python
def visit_literal_block(self, node): """Visit a ``literal_block`` node. This verifies the state of each literal / code block. """ language = node.attributes.get("language", "") test_type = node.attributes.get("testnodetype", "") if test_type != "doctest": if language.lower() in ("", "python"): msg = _LITERAL_ERR_TEMPLATE.format( node.rawsource, language, test_type ) raise errors.ExtensionError(msg) # The base classes are not new-style, so we can't use super(). return html.HTMLTranslator.visit_literal_block(self, node)
[ "def", "visit_literal_block", "(", "self", ",", "node", ")", ":", "language", "=", "node", ".", "attributes", ".", "get", "(", "\"language\"", ",", "\"\"", ")", "test_type", "=", "node", ".", "attributes", ".", "get", "(", "\"testnodetype\"", ",", "\"\"", ")", "if", "test_type", "!=", "\"doctest\"", ":", "if", "language", ".", "lower", "(", ")", "in", "(", "\"\"", ",", "\"python\"", ")", ":", "msg", "=", "_LITERAL_ERR_TEMPLATE", ".", "format", "(", "node", ".", "rawsource", ",", "language", ",", "test_type", ")", "raise", "errors", ".", "ExtensionError", "(", "msg", ")", "# The base classes are not new-style, so we can't use super().", "return", "html", ".", "HTMLTranslator", ".", "visit_literal_block", "(", "self", ",", "node", ")" ]
Visit a ``literal_block`` node. This verifies the state of each literal / code block.
[ "Visit", "a", "literal_block", "node", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/docs/custom_html_writer.py#L42-L57
dhermes/bezier
src/bezier/_geometric_intersection.py
_bbox_intersect
def _bbox_intersect(nodes1, nodes2): r"""Bounding box intersection predicate. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Determines if the bounding box of two sets of control points intersects in :math:`\mathbf{R}^2` with non-trivial intersection (i.e. tangent bounding boxes are insufficient). .. note:: Though we assume (and the code relies on this fact) that the nodes are two-dimensional, we don't check it. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier shape. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier shape. Returns: int: Enum from ``BoxIntersectionType`` indicating the type of bounding box intersection. """ left1, right1, bottom1, top1 = _helpers.bbox(nodes1) left2, right2, bottom2, top2 = _helpers.bbox(nodes2) if right2 < left1 or right1 < left2 or top2 < bottom1 or top1 < bottom2: return BoxIntersectionType.DISJOINT if ( right2 == left1 or right1 == left2 or top2 == bottom1 or top1 == bottom2 ): return BoxIntersectionType.TANGENT else: return BoxIntersectionType.INTERSECTION
python
def _bbox_intersect(nodes1, nodes2): r"""Bounding box intersection predicate. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Determines if the bounding box of two sets of control points intersects in :math:`\mathbf{R}^2` with non-trivial intersection (i.e. tangent bounding boxes are insufficient). .. note:: Though we assume (and the code relies on this fact) that the nodes are two-dimensional, we don't check it. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier shape. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier shape. Returns: int: Enum from ``BoxIntersectionType`` indicating the type of bounding box intersection. """ left1, right1, bottom1, top1 = _helpers.bbox(nodes1) left2, right2, bottom2, top2 = _helpers.bbox(nodes2) if right2 < left1 or right1 < left2 or top2 < bottom1 or top1 < bottom2: return BoxIntersectionType.DISJOINT if ( right2 == left1 or right1 == left2 or top2 == bottom1 or top1 == bottom2 ): return BoxIntersectionType.TANGENT else: return BoxIntersectionType.INTERSECTION
[ "def", "_bbox_intersect", "(", "nodes1", ",", "nodes2", ")", ":", "left1", ",", "right1", ",", "bottom1", ",", "top1", "=", "_helpers", ".", "bbox", "(", "nodes1", ")", "left2", ",", "right2", ",", "bottom2", ",", "top2", "=", "_helpers", ".", "bbox", "(", "nodes2", ")", "if", "right2", "<", "left1", "or", "right1", "<", "left2", "or", "top2", "<", "bottom1", "or", "top1", "<", "bottom2", ":", "return", "BoxIntersectionType", ".", "DISJOINT", "if", "(", "right2", "==", "left1", "or", "right1", "==", "left2", "or", "top2", "==", "bottom1", "or", "top1", "==", "bottom2", ")", ":", "return", "BoxIntersectionType", ".", "TANGENT", "else", ":", "return", "BoxIntersectionType", ".", "INTERSECTION" ]
r"""Bounding box intersection predicate. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Determines if the bounding box of two sets of control points intersects in :math:`\mathbf{R}^2` with non-trivial intersection (i.e. tangent bounding boxes are insufficient). .. note:: Though we assume (and the code relies on this fact) that the nodes are two-dimensional, we don't check it. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier shape. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier shape. Returns: int: Enum from ``BoxIntersectionType`` indicating the type of bounding box intersection.
[ "r", "Bounding", "box", "intersection", "predicate", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L63-L104
dhermes/bezier
src/bezier/_geometric_intersection.py
linearization_error
def linearization_error(nodes): r"""Compute the maximum error of a linear approximation. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This is a helper for :class:`.Linearization`, which is used during the curve-curve intersection process. We use the line .. math:: L(s) = v_0 (1 - s) + v_n s and compute a bound on the maximum error .. math:: \max_{s \in \left[0, 1\right]} \|B(s) - L(s)\|_2. Rather than computing the actual maximum (a tight bound), we use an upper bound via the remainder from Lagrange interpolation in each component. This leaves us with :math:`\frac{s(s - 1)}{2!}` times the second derivative in each component. The second derivative curve is degree :math:`d = n - 2` and is given by .. math:: B''(s) = n(n - 1) \sum_{j = 0}^{d} \binom{d}{j} s^j (1 - s)^{d - j} \cdot \Delta^2 v_j Due to this form (and the convex combination property of B |eacute| zier Curves) we know each component of the second derivative will be bounded by the maximum of that component among the :math:`\Delta^2 v_j`. For example, the curve .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^2 + \left[\begin{array}{c} 3 \\ 1 \end{array}\right] 2s(1 - s) + \left[\begin{array}{c} 9 \\ -2 \end{array}\right] s^2 has :math:`B''(s) \equiv \left[\begin{array}{c} 6 \\ -8 \end{array}\right]` which has norm :math:`10` everywhere, hence the maximum error is .. math:: \left.\frac{s(1 - s)}{2!} \cdot 10\right|_{s = \frac{1}{2}} = \frac{5}{4}. .. image:: ../images/linearization_error.png :align: center .. testsetup:: linearization-error, linearization-error-fail import numpy as np import bezier from bezier._geometric_intersection import linearization_error .. doctest:: linearization-error >>> nodes = np.asfortranarray([ ... [0.0, 3.0, 9.0], ... [0.0, 1.0, -2.0], ... ]) >>> linearization_error(nodes) 1.25 .. testcleanup:: linearization-error import make_images make_images.linearization_error(nodes) As a **non-example**, consider a "pathological" set of control points: .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^3 + \left[\begin{array}{c} 5 \\ 12 \end{array}\right] 3s(1 - s)^2 + \left[\begin{array}{c} 10 \\ 24 \end{array}\right] 3s^2(1 - s) + \left[\begin{array}{c} 30 \\ 72 \end{array}\right] s^3 By construction, this lies on the line :math:`y = \frac{12x}{5}`, but the parametrization is cubic: :math:`12 \cdot x(s) = 5 \cdot y(s) = 180s(s^2 + 1)`. Hence, the fact that the curve is a line is not accounted for and we take the worse case among the nodes in: .. math:: B''(s) = 3 \cdot 2 \cdot \left( \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 15 \\ 36 \end{array}\right] s\right) which gives a nonzero maximum error: .. doctest:: linearization-error-fail >>> nodes = np.asfortranarray([ ... [0.0, 5.0, 10.0, 30.0], ... [0.0, 12.0, 24.0, 72.0], ... ]) >>> linearization_error(nodes) 29.25 Though it may seem that ``0`` is a more appropriate answer, consider the **goal** of this function. We seek to linearize curves and then intersect the linear approximations. Then the :math:`s`-values from the line-line intersection is lifted back to the curves. Thus the error :math:`\|B(s) - L(s)\|_2` is more relevant than the underyling algebraic curve containing :math:`B(s)`. .. note:: It may be more appropriate to use a **relative** linearization error rather than the **absolute** error provided here. It's unclear if the domain :math:`\left[0, 1\right]` means the error is **already** adequately scaled or if the error should be scaled by the arc length of the curve or the (easier-to-compute) length of the line. Args: nodes (numpy.ndarray): Nodes of a curve. Returns: float: The maximum error between the curve and the linear approximation. """ _, num_nodes = nodes.shape degree = num_nodes - 1 if degree == 1: return 0.0 second_deriv = nodes[:, :-2] - 2.0 * nodes[:, 1:-1] + nodes[:, 2:] worst_case = np.max(np.abs(second_deriv), axis=1) # max_{0 <= s <= 1} s(1 - s)/2 = 1/8 = 0.125 multiplier = 0.125 * degree * (degree - 1) # NOTE: worst_case is 1D due to np.max(), so this is the vector norm. return multiplier * np.linalg.norm(worst_case, ord=2)
python
def linearization_error(nodes): r"""Compute the maximum error of a linear approximation. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This is a helper for :class:`.Linearization`, which is used during the curve-curve intersection process. We use the line .. math:: L(s) = v_0 (1 - s) + v_n s and compute a bound on the maximum error .. math:: \max_{s \in \left[0, 1\right]} \|B(s) - L(s)\|_2. Rather than computing the actual maximum (a tight bound), we use an upper bound via the remainder from Lagrange interpolation in each component. This leaves us with :math:`\frac{s(s - 1)}{2!}` times the second derivative in each component. The second derivative curve is degree :math:`d = n - 2` and is given by .. math:: B''(s) = n(n - 1) \sum_{j = 0}^{d} \binom{d}{j} s^j (1 - s)^{d - j} \cdot \Delta^2 v_j Due to this form (and the convex combination property of B |eacute| zier Curves) we know each component of the second derivative will be bounded by the maximum of that component among the :math:`\Delta^2 v_j`. For example, the curve .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^2 + \left[\begin{array}{c} 3 \\ 1 \end{array}\right] 2s(1 - s) + \left[\begin{array}{c} 9 \\ -2 \end{array}\right] s^2 has :math:`B''(s) \equiv \left[\begin{array}{c} 6 \\ -8 \end{array}\right]` which has norm :math:`10` everywhere, hence the maximum error is .. math:: \left.\frac{s(1 - s)}{2!} \cdot 10\right|_{s = \frac{1}{2}} = \frac{5}{4}. .. image:: ../images/linearization_error.png :align: center .. testsetup:: linearization-error, linearization-error-fail import numpy as np import bezier from bezier._geometric_intersection import linearization_error .. doctest:: linearization-error >>> nodes = np.asfortranarray([ ... [0.0, 3.0, 9.0], ... [0.0, 1.0, -2.0], ... ]) >>> linearization_error(nodes) 1.25 .. testcleanup:: linearization-error import make_images make_images.linearization_error(nodes) As a **non-example**, consider a "pathological" set of control points: .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^3 + \left[\begin{array}{c} 5 \\ 12 \end{array}\right] 3s(1 - s)^2 + \left[\begin{array}{c} 10 \\ 24 \end{array}\right] 3s^2(1 - s) + \left[\begin{array}{c} 30 \\ 72 \end{array}\right] s^3 By construction, this lies on the line :math:`y = \frac{12x}{5}`, but the parametrization is cubic: :math:`12 \cdot x(s) = 5 \cdot y(s) = 180s(s^2 + 1)`. Hence, the fact that the curve is a line is not accounted for and we take the worse case among the nodes in: .. math:: B''(s) = 3 \cdot 2 \cdot \left( \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 15 \\ 36 \end{array}\right] s\right) which gives a nonzero maximum error: .. doctest:: linearization-error-fail >>> nodes = np.asfortranarray([ ... [0.0, 5.0, 10.0, 30.0], ... [0.0, 12.0, 24.0, 72.0], ... ]) >>> linearization_error(nodes) 29.25 Though it may seem that ``0`` is a more appropriate answer, consider the **goal** of this function. We seek to linearize curves and then intersect the linear approximations. Then the :math:`s`-values from the line-line intersection is lifted back to the curves. Thus the error :math:`\|B(s) - L(s)\|_2` is more relevant than the underyling algebraic curve containing :math:`B(s)`. .. note:: It may be more appropriate to use a **relative** linearization error rather than the **absolute** error provided here. It's unclear if the domain :math:`\left[0, 1\right]` means the error is **already** adequately scaled or if the error should be scaled by the arc length of the curve or the (easier-to-compute) length of the line. Args: nodes (numpy.ndarray): Nodes of a curve. Returns: float: The maximum error between the curve and the linear approximation. """ _, num_nodes = nodes.shape degree = num_nodes - 1 if degree == 1: return 0.0 second_deriv = nodes[:, :-2] - 2.0 * nodes[:, 1:-1] + nodes[:, 2:] worst_case = np.max(np.abs(second_deriv), axis=1) # max_{0 <= s <= 1} s(1 - s)/2 = 1/8 = 0.125 multiplier = 0.125 * degree * (degree - 1) # NOTE: worst_case is 1D due to np.max(), so this is the vector norm. return multiplier * np.linalg.norm(worst_case, ord=2)
[ "def", "linearization_error", "(", "nodes", ")", ":", "_", ",", "num_nodes", "=", "nodes", ".", "shape", "degree", "=", "num_nodes", "-", "1", "if", "degree", "==", "1", ":", "return", "0.0", "second_deriv", "=", "nodes", "[", ":", ",", ":", "-", "2", "]", "-", "2.0", "*", "nodes", "[", ":", ",", "1", ":", "-", "1", "]", "+", "nodes", "[", ":", ",", "2", ":", "]", "worst_case", "=", "np", ".", "max", "(", "np", ".", "abs", "(", "second_deriv", ")", ",", "axis", "=", "1", ")", "# max_{0 <= s <= 1} s(1 - s)/2 = 1/8 = 0.125", "multiplier", "=", "0.125", "*", "degree", "*", "(", "degree", "-", "1", ")", "# NOTE: worst_case is 1D due to np.max(), so this is the vector norm.", "return", "multiplier", "*", "np", ".", "linalg", ".", "norm", "(", "worst_case", ",", "ord", "=", "2", ")" ]
r"""Compute the maximum error of a linear approximation. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This is a helper for :class:`.Linearization`, which is used during the curve-curve intersection process. We use the line .. math:: L(s) = v_0 (1 - s) + v_n s and compute a bound on the maximum error .. math:: \max_{s \in \left[0, 1\right]} \|B(s) - L(s)\|_2. Rather than computing the actual maximum (a tight bound), we use an upper bound via the remainder from Lagrange interpolation in each component. This leaves us with :math:`\frac{s(s - 1)}{2!}` times the second derivative in each component. The second derivative curve is degree :math:`d = n - 2` and is given by .. math:: B''(s) = n(n - 1) \sum_{j = 0}^{d} \binom{d}{j} s^j (1 - s)^{d - j} \cdot \Delta^2 v_j Due to this form (and the convex combination property of B |eacute| zier Curves) we know each component of the second derivative will be bounded by the maximum of that component among the :math:`\Delta^2 v_j`. For example, the curve .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^2 + \left[\begin{array}{c} 3 \\ 1 \end{array}\right] 2s(1 - s) + \left[\begin{array}{c} 9 \\ -2 \end{array}\right] s^2 has :math:`B''(s) \equiv \left[\begin{array}{c} 6 \\ -8 \end{array}\right]` which has norm :math:`10` everywhere, hence the maximum error is .. math:: \left.\frac{s(1 - s)}{2!} \cdot 10\right|_{s = \frac{1}{2}} = \frac{5}{4}. .. image:: ../images/linearization_error.png :align: center .. testsetup:: linearization-error, linearization-error-fail import numpy as np import bezier from bezier._geometric_intersection import linearization_error .. doctest:: linearization-error >>> nodes = np.asfortranarray([ ... [0.0, 3.0, 9.0], ... [0.0, 1.0, -2.0], ... ]) >>> linearization_error(nodes) 1.25 .. testcleanup:: linearization-error import make_images make_images.linearization_error(nodes) As a **non-example**, consider a "pathological" set of control points: .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^3 + \left[\begin{array}{c} 5 \\ 12 \end{array}\right] 3s(1 - s)^2 + \left[\begin{array}{c} 10 \\ 24 \end{array}\right] 3s^2(1 - s) + \left[\begin{array}{c} 30 \\ 72 \end{array}\right] s^3 By construction, this lies on the line :math:`y = \frac{12x}{5}`, but the parametrization is cubic: :math:`12 \cdot x(s) = 5 \cdot y(s) = 180s(s^2 + 1)`. Hence, the fact that the curve is a line is not accounted for and we take the worse case among the nodes in: .. math:: B''(s) = 3 \cdot 2 \cdot \left( \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 15 \\ 36 \end{array}\right] s\right) which gives a nonzero maximum error: .. doctest:: linearization-error-fail >>> nodes = np.asfortranarray([ ... [0.0, 5.0, 10.0, 30.0], ... [0.0, 12.0, 24.0, 72.0], ... ]) >>> linearization_error(nodes) 29.25 Though it may seem that ``0`` is a more appropriate answer, consider the **goal** of this function. We seek to linearize curves and then intersect the linear approximations. Then the :math:`s`-values from the line-line intersection is lifted back to the curves. Thus the error :math:`\|B(s) - L(s)\|_2` is more relevant than the underyling algebraic curve containing :math:`B(s)`. .. note:: It may be more appropriate to use a **relative** linearization error rather than the **absolute** error provided here. It's unclear if the domain :math:`\left[0, 1\right]` means the error is **already** adequately scaled or if the error should be scaled by the arc length of the curve or the (easier-to-compute) length of the line. Args: nodes (numpy.ndarray): Nodes of a curve. Returns: float: The maximum error between the curve and the linear approximation.
[ "r", "Compute", "the", "maximum", "error", "of", "a", "linear", "approximation", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L107-L254
dhermes/bezier
src/bezier/_geometric_intersection.py
segment_intersection
def segment_intersection(start0, end0, start1, end1): r"""Determine the intersection of two line segments. Assumes each line is parametric .. math:: \begin{alignat*}{2} L_0(s) &= S_0 (1 - s) + E_0 s &&= S_0 + s \Delta_0 \\ L_1(t) &= S_1 (1 - t) + E_1 t &&= S_1 + t \Delta_1. \end{alignat*} To solve :math:`S_0 + s \Delta_0 = S_1 + t \Delta_1`, we use the cross product: .. math:: \left(S_0 + s \Delta_0\right) \times \Delta_1 = \left(S_1 + t \Delta_1\right) \times \Delta_1 \Longrightarrow s \left(\Delta_0 \times \Delta_1\right) = \left(S_1 - S_0\right) \times \Delta_1. Similarly .. math:: \Delta_0 \times \left(S_0 + s \Delta_0\right) = \Delta_0 \times \left(S_1 + t \Delta_1\right) \Longrightarrow \left(S_1 - S_0\right) \times \Delta_0 = \Delta_0 \times \left(S_0 - S_1\right) = t \left(\Delta_0 \times \Delta_1\right). .. note:: Since our points are in :math:`\mathbf{R}^2`, the "traditional" cross product in :math:`\mathbf{R}^3` will always point in the :math:`z` direction, so in the above we mean the :math:`z` component of the cross product, rather than the entire vector. For example, the diagonal lines .. math:: \begin{align*} L_0(s) &= \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 2 \\ 2 \end{array}\right] s \\ L_1(t) &= \left[\begin{array}{c} -1 \\ 2 \end{array}\right] (1 - t) + \left[\begin{array}{c} 1 \\ 0 \end{array}\right] t \end{align*} intersect at :math:`L_0\left(\frac{1}{4}\right) = L_1\left(\frac{3}{4}\right) = \frac{1}{2} \left[\begin{array}{c} 1 \\ 1 \end{array}\right]`. .. image:: ../images/segment_intersection1.png :align: center .. testsetup:: segment-intersection1, segment-intersection2 import numpy as np from bezier._geometric_intersection import segment_intersection .. doctest:: segment-intersection1 :options: +NORMALIZE_WHITESPACE >>> start0 = np.asfortranarray([0.0, 0.0]) >>> end0 = np.asfortranarray([2.0, 2.0]) >>> start1 = np.asfortranarray([-1.0, 2.0]) >>> end1 = np.asfortranarray([1.0, 0.0]) >>> s, t, _ = segment_intersection(start0, end0, start1, end1) >>> s 0.25 >>> t 0.75 .. testcleanup:: segment-intersection1 import make_images make_images.segment_intersection1(start0, end0, start1, end1, s) Taking the parallel (but different) lines .. math:: \begin{align*} L_0(s) &= \left[\begin{array}{c} 1 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 0 \\ 1 \end{array}\right] s \\ L_1(t) &= \left[\begin{array}{c} -1 \\ 3 \end{array}\right] (1 - t) + \left[\begin{array}{c} 3 \\ -1 \end{array}\right] t \end{align*} we should be able to determine that the lines don't intersect, but this function is not meant for that check: .. image:: ../images/segment_intersection2.png :align: center .. doctest:: segment-intersection2 :options: +NORMALIZE_WHITESPACE >>> start0 = np.asfortranarray([1.0, 0.0]) >>> end0 = np.asfortranarray([0.0, 1.0]) >>> start1 = np.asfortranarray([-1.0, 3.0]) >>> end1 = np.asfortranarray([3.0, -1.0]) >>> _, _, success = segment_intersection(start0, end0, start1, end1) >>> success False .. testcleanup:: segment-intersection2 import make_images make_images.segment_intersection2(start0, end0, start1, end1) Instead, we use :func:`parallel_lines_parameters`: .. testsetup:: segment-intersection2-continued import numpy as np from bezier._geometric_intersection import parallel_lines_parameters start0 = np.asfortranarray([1.0, 0.0]) end0 = np.asfortranarray([0.0, 1.0]) start1 = np.asfortranarray([-1.0, 3.0]) end1 = np.asfortranarray([3.0, -1.0]) .. doctest:: segment-intersection2-continued >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: start0 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_0` of the parametric line :math:`L_0(s)`. end0 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_0` of the parametric line :math:`L_0(s)`. start1 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_1` of the parametric line :math:`L_1(s)`. end1 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_1` of the parametric line :math:`L_1(s)`. Returns: Tuple[float, float, bool]: Pair of :math:`s_{\ast}` and :math:`t_{\ast}` such that the lines intersect: :math:`L_0\left(s_{\ast}\right) = L_1\left(t_{\ast}\right)` and then a boolean indicating if an intersection was found (i.e. if the lines aren't parallel). """ delta0 = end0 - start0 delta1 = end1 - start1 cross_d0_d1 = _helpers.cross_product(delta0, delta1) if cross_d0_d1 == 0.0: return None, None, False else: start_delta = start1 - start0 s = _helpers.cross_product(start_delta, delta1) / cross_d0_d1 t = _helpers.cross_product(start_delta, delta0) / cross_d0_d1 return s, t, True
python
def segment_intersection(start0, end0, start1, end1): r"""Determine the intersection of two line segments. Assumes each line is parametric .. math:: \begin{alignat*}{2} L_0(s) &= S_0 (1 - s) + E_0 s &&= S_0 + s \Delta_0 \\ L_1(t) &= S_1 (1 - t) + E_1 t &&= S_1 + t \Delta_1. \end{alignat*} To solve :math:`S_0 + s \Delta_0 = S_1 + t \Delta_1`, we use the cross product: .. math:: \left(S_0 + s \Delta_0\right) \times \Delta_1 = \left(S_1 + t \Delta_1\right) \times \Delta_1 \Longrightarrow s \left(\Delta_0 \times \Delta_1\right) = \left(S_1 - S_0\right) \times \Delta_1. Similarly .. math:: \Delta_0 \times \left(S_0 + s \Delta_0\right) = \Delta_0 \times \left(S_1 + t \Delta_1\right) \Longrightarrow \left(S_1 - S_0\right) \times \Delta_0 = \Delta_0 \times \left(S_0 - S_1\right) = t \left(\Delta_0 \times \Delta_1\right). .. note:: Since our points are in :math:`\mathbf{R}^2`, the "traditional" cross product in :math:`\mathbf{R}^3` will always point in the :math:`z` direction, so in the above we mean the :math:`z` component of the cross product, rather than the entire vector. For example, the diagonal lines .. math:: \begin{align*} L_0(s) &= \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 2 \\ 2 \end{array}\right] s \\ L_1(t) &= \left[\begin{array}{c} -1 \\ 2 \end{array}\right] (1 - t) + \left[\begin{array}{c} 1 \\ 0 \end{array}\right] t \end{align*} intersect at :math:`L_0\left(\frac{1}{4}\right) = L_1\left(\frac{3}{4}\right) = \frac{1}{2} \left[\begin{array}{c} 1 \\ 1 \end{array}\right]`. .. image:: ../images/segment_intersection1.png :align: center .. testsetup:: segment-intersection1, segment-intersection2 import numpy as np from bezier._geometric_intersection import segment_intersection .. doctest:: segment-intersection1 :options: +NORMALIZE_WHITESPACE >>> start0 = np.asfortranarray([0.0, 0.0]) >>> end0 = np.asfortranarray([2.0, 2.0]) >>> start1 = np.asfortranarray([-1.0, 2.0]) >>> end1 = np.asfortranarray([1.0, 0.0]) >>> s, t, _ = segment_intersection(start0, end0, start1, end1) >>> s 0.25 >>> t 0.75 .. testcleanup:: segment-intersection1 import make_images make_images.segment_intersection1(start0, end0, start1, end1, s) Taking the parallel (but different) lines .. math:: \begin{align*} L_0(s) &= \left[\begin{array}{c} 1 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 0 \\ 1 \end{array}\right] s \\ L_1(t) &= \left[\begin{array}{c} -1 \\ 3 \end{array}\right] (1 - t) + \left[\begin{array}{c} 3 \\ -1 \end{array}\right] t \end{align*} we should be able to determine that the lines don't intersect, but this function is not meant for that check: .. image:: ../images/segment_intersection2.png :align: center .. doctest:: segment-intersection2 :options: +NORMALIZE_WHITESPACE >>> start0 = np.asfortranarray([1.0, 0.0]) >>> end0 = np.asfortranarray([0.0, 1.0]) >>> start1 = np.asfortranarray([-1.0, 3.0]) >>> end1 = np.asfortranarray([3.0, -1.0]) >>> _, _, success = segment_intersection(start0, end0, start1, end1) >>> success False .. testcleanup:: segment-intersection2 import make_images make_images.segment_intersection2(start0, end0, start1, end1) Instead, we use :func:`parallel_lines_parameters`: .. testsetup:: segment-intersection2-continued import numpy as np from bezier._geometric_intersection import parallel_lines_parameters start0 = np.asfortranarray([1.0, 0.0]) end0 = np.asfortranarray([0.0, 1.0]) start1 = np.asfortranarray([-1.0, 3.0]) end1 = np.asfortranarray([3.0, -1.0]) .. doctest:: segment-intersection2-continued >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: start0 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_0` of the parametric line :math:`L_0(s)`. end0 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_0` of the parametric line :math:`L_0(s)`. start1 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_1` of the parametric line :math:`L_1(s)`. end1 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_1` of the parametric line :math:`L_1(s)`. Returns: Tuple[float, float, bool]: Pair of :math:`s_{\ast}` and :math:`t_{\ast}` such that the lines intersect: :math:`L_0\left(s_{\ast}\right) = L_1\left(t_{\ast}\right)` and then a boolean indicating if an intersection was found (i.e. if the lines aren't parallel). """ delta0 = end0 - start0 delta1 = end1 - start1 cross_d0_d1 = _helpers.cross_product(delta0, delta1) if cross_d0_d1 == 0.0: return None, None, False else: start_delta = start1 - start0 s = _helpers.cross_product(start_delta, delta1) / cross_d0_d1 t = _helpers.cross_product(start_delta, delta0) / cross_d0_d1 return s, t, True
[ "def", "segment_intersection", "(", "start0", ",", "end0", ",", "start1", ",", "end1", ")", ":", "delta0", "=", "end0", "-", "start0", "delta1", "=", "end1", "-", "start1", "cross_d0_d1", "=", "_helpers", ".", "cross_product", "(", "delta0", ",", "delta1", ")", "if", "cross_d0_d1", "==", "0.0", ":", "return", "None", ",", "None", ",", "False", "else", ":", "start_delta", "=", "start1", "-", "start0", "s", "=", "_helpers", ".", "cross_product", "(", "start_delta", ",", "delta1", ")", "/", "cross_d0_d1", "t", "=", "_helpers", ".", "cross_product", "(", "start_delta", ",", "delta0", ")", "/", "cross_d0_d1", "return", "s", ",", "t", ",", "True" ]
r"""Determine the intersection of two line segments. Assumes each line is parametric .. math:: \begin{alignat*}{2} L_0(s) &= S_0 (1 - s) + E_0 s &&= S_0 + s \Delta_0 \\ L_1(t) &= S_1 (1 - t) + E_1 t &&= S_1 + t \Delta_1. \end{alignat*} To solve :math:`S_0 + s \Delta_0 = S_1 + t \Delta_1`, we use the cross product: .. math:: \left(S_0 + s \Delta_0\right) \times \Delta_1 = \left(S_1 + t \Delta_1\right) \times \Delta_1 \Longrightarrow s \left(\Delta_0 \times \Delta_1\right) = \left(S_1 - S_0\right) \times \Delta_1. Similarly .. math:: \Delta_0 \times \left(S_0 + s \Delta_0\right) = \Delta_0 \times \left(S_1 + t \Delta_1\right) \Longrightarrow \left(S_1 - S_0\right) \times \Delta_0 = \Delta_0 \times \left(S_0 - S_1\right) = t \left(\Delta_0 \times \Delta_1\right). .. note:: Since our points are in :math:`\mathbf{R}^2`, the "traditional" cross product in :math:`\mathbf{R}^3` will always point in the :math:`z` direction, so in the above we mean the :math:`z` component of the cross product, rather than the entire vector. For example, the diagonal lines .. math:: \begin{align*} L_0(s) &= \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 2 \\ 2 \end{array}\right] s \\ L_1(t) &= \left[\begin{array}{c} -1 \\ 2 \end{array}\right] (1 - t) + \left[\begin{array}{c} 1 \\ 0 \end{array}\right] t \end{align*} intersect at :math:`L_0\left(\frac{1}{4}\right) = L_1\left(\frac{3}{4}\right) = \frac{1}{2} \left[\begin{array}{c} 1 \\ 1 \end{array}\right]`. .. image:: ../images/segment_intersection1.png :align: center .. testsetup:: segment-intersection1, segment-intersection2 import numpy as np from bezier._geometric_intersection import segment_intersection .. doctest:: segment-intersection1 :options: +NORMALIZE_WHITESPACE >>> start0 = np.asfortranarray([0.0, 0.0]) >>> end0 = np.asfortranarray([2.0, 2.0]) >>> start1 = np.asfortranarray([-1.0, 2.0]) >>> end1 = np.asfortranarray([1.0, 0.0]) >>> s, t, _ = segment_intersection(start0, end0, start1, end1) >>> s 0.25 >>> t 0.75 .. testcleanup:: segment-intersection1 import make_images make_images.segment_intersection1(start0, end0, start1, end1, s) Taking the parallel (but different) lines .. math:: \begin{align*} L_0(s) &= \left[\begin{array}{c} 1 \\ 0 \end{array}\right] (1 - s) + \left[\begin{array}{c} 0 \\ 1 \end{array}\right] s \\ L_1(t) &= \left[\begin{array}{c} -1 \\ 3 \end{array}\right] (1 - t) + \left[\begin{array}{c} 3 \\ -1 \end{array}\right] t \end{align*} we should be able to determine that the lines don't intersect, but this function is not meant for that check: .. image:: ../images/segment_intersection2.png :align: center .. doctest:: segment-intersection2 :options: +NORMALIZE_WHITESPACE >>> start0 = np.asfortranarray([1.0, 0.0]) >>> end0 = np.asfortranarray([0.0, 1.0]) >>> start1 = np.asfortranarray([-1.0, 3.0]) >>> end1 = np.asfortranarray([3.0, -1.0]) >>> _, _, success = segment_intersection(start0, end0, start1, end1) >>> success False .. testcleanup:: segment-intersection2 import make_images make_images.segment_intersection2(start0, end0, start1, end1) Instead, we use :func:`parallel_lines_parameters`: .. testsetup:: segment-intersection2-continued import numpy as np from bezier._geometric_intersection import parallel_lines_parameters start0 = np.asfortranarray([1.0, 0.0]) end0 = np.asfortranarray([0.0, 1.0]) start1 = np.asfortranarray([-1.0, 3.0]) end1 = np.asfortranarray([3.0, -1.0]) .. doctest:: segment-intersection2-continued >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: start0 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_0` of the parametric line :math:`L_0(s)`. end0 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_0` of the parametric line :math:`L_0(s)`. start1 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_1` of the parametric line :math:`L_1(s)`. end1 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_1` of the parametric line :math:`L_1(s)`. Returns: Tuple[float, float, bool]: Pair of :math:`s_{\ast}` and :math:`t_{\ast}` such that the lines intersect: :math:`L_0\left(s_{\ast}\right) = L_1\left(t_{\ast}\right)` and then a boolean indicating if an intersection was found (i.e. if the lines aren't parallel).
[ "r", "Determine", "the", "intersection", "of", "two", "line", "segments", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L257-L420
dhermes/bezier
src/bezier/_geometric_intersection.py
parallel_lines_parameters
def parallel_lines_parameters(start0, end0, start1, end1): r"""Checks if two parallel lines ever meet. Meant as a back-up when :func:`segment_intersection` fails. .. note:: This function assumes but never verifies that the lines are parallel. In the case that the segments are parallel and lie on **different** lines, then there is a **guarantee** of no intersection. However, if they are on the exact same line, they may define a shared segment coincident to both lines. In :func:`segment_intersection`, we utilized the normal form of the lines (via the cross product): .. math:: \begin{align*} L_0(s) \times \Delta_0 &\equiv S_0 \times \Delta_0 \\ L_1(t) \times \Delta_1 &\equiv S_1 \times \Delta_1 \end{align*} So, we can detect if :math:`S_1` is on the first line by checking if .. math:: S_0 \times \Delta_0 \stackrel{?}{=} S_1 \times \Delta_0. If it is not on the first line, then we are done, the segments don't meet: .. image:: ../images/parallel_lines_parameters1.png :align: center .. testsetup:: parallel-different1, parallel-different2 import numpy as np from bezier._geometric_intersection import parallel_lines_parameters .. doctest:: parallel-different1 >>> # Line: y = 1 >>> start0 = np.asfortranarray([0.0, 1.0]) >>> end0 = np.asfortranarray([1.0, 1.0]) >>> # Vertical shift up: y = 2 >>> start1 = np.asfortranarray([-1.0, 2.0]) >>> end1 = np.asfortranarray([3.0, 2.0]) >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. testcleanup:: parallel-different1 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters1.png") If :math:`S_1` **is** on the first line, we want to check that :math:`S_1` and :math:`E_1` define parameters outside of :math:`\left[0, 1\right]`. To compute these parameters: .. math:: L_1(t) = S_0 + s_{\ast} \Delta_0 \Longrightarrow s_{\ast} = \frac{\Delta_0^T \left( L_1(t) - S_0\right)}{\Delta_0^T \Delta_0}. For example, the intervals :math:`\left[0, 1\right]` and :math:`\left[\frac{3}{2}, 2\right]` (via :math:`S_1 = S_0 + \frac{3}{2} \Delta_0` and :math:`E_1 = S_0 + 2 \Delta_0`) correspond to segments that don't meet: .. image:: ../images/parallel_lines_parameters2.png :align: center .. doctest:: parallel-different2 >>> start0 = np.asfortranarray([1.0, 0.0]) >>> delta0 = np.asfortranarray([2.0, -1.0]) >>> end0 = start0 + 1.0 * delta0 >>> start1 = start0 + 1.5 * delta0 >>> end1 = start0 + 2.0 * delta0 >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. testcleanup:: parallel-different2 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters2.png") but if the intervals overlap, like :math:`\left[0, 1\right]` and :math:`\left[-1, \frac{1}{2}\right]`, the segments meet: .. image:: ../images/parallel_lines_parameters3.png :align: center .. testsetup:: parallel-different3, parallel-different4 import numpy as np from bezier._geometric_intersection import parallel_lines_parameters start0 = np.asfortranarray([1.0, 0.0]) delta0 = np.asfortranarray([2.0, -1.0]) end0 = start0 + 1.0 * delta0 .. doctest:: parallel-different3 >>> start1 = start0 - 1.5 * delta0 >>> end1 = start0 + 0.5 * delta0 >>> disjoint, parameters = parallel_lines_parameters( ... start0, end0, start1, end1) >>> disjoint False >>> parameters array([[0. , 0.5 ], [0.75, 1. ]]) .. testcleanup:: parallel-different3 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters3.png") Similarly, if the second interval completely contains the first, the segments meet: .. image:: ../images/parallel_lines_parameters4.png :align: center .. doctest:: parallel-different4 >>> start1 = start0 + 4.5 * delta0 >>> end1 = start0 - 3.5 * delta0 >>> disjoint, parameters = parallel_lines_parameters( ... start0, end0, start1, end1) >>> disjoint False >>> parameters array([[1. , 0. ], [0.4375, 0.5625]]) .. testcleanup:: parallel-different4 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters4.png") .. note:: This function doesn't currently allow wiggle room around the desired value, i.e. the two values must be bitwise identical. However, the most "correct" version of this function likely should allow for some round off. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: start0 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_0` of the parametric line :math:`L_0(s)`. end0 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_0` of the parametric line :math:`L_0(s)`. start1 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_1` of the parametric line :math:`L_1(s)`. end1 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_1` of the parametric line :math:`L_1(s)`. Returns: Tuple[bool, Optional[numpy.ndarray]]: A pair of * Flag indicating if the lines are disjoint. * An optional ``2 x 2`` matrix of ``s-t`` parameters only present if the lines aren't disjoint. The first column will contain the parameters at the beginning of the shared segment and the second column will correspond to the end of the shared segment. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-branches delta0 = end0 - start0 line0_const = _helpers.cross_product(start0, delta0) start1_against = _helpers.cross_product(start1, delta0) if line0_const != start1_against: return True, None # Each array is a 1D vector, so we can use the vector dot product. norm0_sq = np.vdot(delta0, delta0) # S1 = L1(0) = S0 + sA D0 # <==> sA D0 = S1 - S0 # ==> sA (D0^T D0) = D0^T (S1 - S0) s_val0 = np.vdot(start1 - start0, delta0) / norm0_sq # E1 = L1(1) = S0 + sB D0 # <==> sB D0 = E1 - S0 # ==> sB (D0^T D0) = D0^T (E1 - S0) s_val1 = np.vdot(end1 - start0, delta0) / norm0_sq # s = s_val0 + t (s_val1 - s_val0) # t = 0 <==> s = s_val0 # t = 1 <==> s = s_val1 # t = -s_val0 / (s_val1 - s_val0) <==> s = 0 # t = (1 - s_val0) / (s_val1 - s_val0) <==> s = 1 if s_val0 <= s_val1: # In this branch the segments are moving in the same direction, i.e. # (t=0<-->s=s_val0) are both less than (t=1<-->s_val1). if 1.0 < s_val0: return True, None elif s_val0 < 0.0: start_s = 0.0 start_t = -s_val0 / (s_val1 - s_val0) else: start_s = s_val0 start_t = 0.0 if s_val1 < 0.0: return True, None elif 1.0 < s_val1: end_s = 1.0 end_t = (1.0 - s_val0) / (s_val1 - s_val0) else: end_s = s_val1 end_t = 1.0 else: # In this branch the segments are moving in opposite directions, i.e. # in (t=0<-->s=s_val0) and (t=1<-->s_val1) we have 0 < 1 # but ``s_val0 > s_val1``. if s_val0 < 0.0: return True, None elif 1.0 < s_val0: start_s = 1.0 start_t = (s_val0 - 1.0) / (s_val0 - s_val1) else: start_s = s_val0 start_t = 0.0 if 1.0 < s_val1: return True, None elif s_val1 < 0.0: end_s = 0.0 end_t = s_val0 / (s_val0 - s_val1) else: end_s = s_val1 end_t = 1.0 parameters = np.asfortranarray([[start_s, end_s], [start_t, end_t]]) return False, parameters
python
def parallel_lines_parameters(start0, end0, start1, end1): r"""Checks if two parallel lines ever meet. Meant as a back-up when :func:`segment_intersection` fails. .. note:: This function assumes but never verifies that the lines are parallel. In the case that the segments are parallel and lie on **different** lines, then there is a **guarantee** of no intersection. However, if they are on the exact same line, they may define a shared segment coincident to both lines. In :func:`segment_intersection`, we utilized the normal form of the lines (via the cross product): .. math:: \begin{align*} L_0(s) \times \Delta_0 &\equiv S_0 \times \Delta_0 \\ L_1(t) \times \Delta_1 &\equiv S_1 \times \Delta_1 \end{align*} So, we can detect if :math:`S_1` is on the first line by checking if .. math:: S_0 \times \Delta_0 \stackrel{?}{=} S_1 \times \Delta_0. If it is not on the first line, then we are done, the segments don't meet: .. image:: ../images/parallel_lines_parameters1.png :align: center .. testsetup:: parallel-different1, parallel-different2 import numpy as np from bezier._geometric_intersection import parallel_lines_parameters .. doctest:: parallel-different1 >>> # Line: y = 1 >>> start0 = np.asfortranarray([0.0, 1.0]) >>> end0 = np.asfortranarray([1.0, 1.0]) >>> # Vertical shift up: y = 2 >>> start1 = np.asfortranarray([-1.0, 2.0]) >>> end1 = np.asfortranarray([3.0, 2.0]) >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. testcleanup:: parallel-different1 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters1.png") If :math:`S_1` **is** on the first line, we want to check that :math:`S_1` and :math:`E_1` define parameters outside of :math:`\left[0, 1\right]`. To compute these parameters: .. math:: L_1(t) = S_0 + s_{\ast} \Delta_0 \Longrightarrow s_{\ast} = \frac{\Delta_0^T \left( L_1(t) - S_0\right)}{\Delta_0^T \Delta_0}. For example, the intervals :math:`\left[0, 1\right]` and :math:`\left[\frac{3}{2}, 2\right]` (via :math:`S_1 = S_0 + \frac{3}{2} \Delta_0` and :math:`E_1 = S_0 + 2 \Delta_0`) correspond to segments that don't meet: .. image:: ../images/parallel_lines_parameters2.png :align: center .. doctest:: parallel-different2 >>> start0 = np.asfortranarray([1.0, 0.0]) >>> delta0 = np.asfortranarray([2.0, -1.0]) >>> end0 = start0 + 1.0 * delta0 >>> start1 = start0 + 1.5 * delta0 >>> end1 = start0 + 2.0 * delta0 >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. testcleanup:: parallel-different2 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters2.png") but if the intervals overlap, like :math:`\left[0, 1\right]` and :math:`\left[-1, \frac{1}{2}\right]`, the segments meet: .. image:: ../images/parallel_lines_parameters3.png :align: center .. testsetup:: parallel-different3, parallel-different4 import numpy as np from bezier._geometric_intersection import parallel_lines_parameters start0 = np.asfortranarray([1.0, 0.0]) delta0 = np.asfortranarray([2.0, -1.0]) end0 = start0 + 1.0 * delta0 .. doctest:: parallel-different3 >>> start1 = start0 - 1.5 * delta0 >>> end1 = start0 + 0.5 * delta0 >>> disjoint, parameters = parallel_lines_parameters( ... start0, end0, start1, end1) >>> disjoint False >>> parameters array([[0. , 0.5 ], [0.75, 1. ]]) .. testcleanup:: parallel-different3 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters3.png") Similarly, if the second interval completely contains the first, the segments meet: .. image:: ../images/parallel_lines_parameters4.png :align: center .. doctest:: parallel-different4 >>> start1 = start0 + 4.5 * delta0 >>> end1 = start0 - 3.5 * delta0 >>> disjoint, parameters = parallel_lines_parameters( ... start0, end0, start1, end1) >>> disjoint False >>> parameters array([[1. , 0. ], [0.4375, 0.5625]]) .. testcleanup:: parallel-different4 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters4.png") .. note:: This function doesn't currently allow wiggle room around the desired value, i.e. the two values must be bitwise identical. However, the most "correct" version of this function likely should allow for some round off. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: start0 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_0` of the parametric line :math:`L_0(s)`. end0 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_0` of the parametric line :math:`L_0(s)`. start1 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_1` of the parametric line :math:`L_1(s)`. end1 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_1` of the parametric line :math:`L_1(s)`. Returns: Tuple[bool, Optional[numpy.ndarray]]: A pair of * Flag indicating if the lines are disjoint. * An optional ``2 x 2`` matrix of ``s-t`` parameters only present if the lines aren't disjoint. The first column will contain the parameters at the beginning of the shared segment and the second column will correspond to the end of the shared segment. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-branches delta0 = end0 - start0 line0_const = _helpers.cross_product(start0, delta0) start1_against = _helpers.cross_product(start1, delta0) if line0_const != start1_against: return True, None # Each array is a 1D vector, so we can use the vector dot product. norm0_sq = np.vdot(delta0, delta0) # S1 = L1(0) = S0 + sA D0 # <==> sA D0 = S1 - S0 # ==> sA (D0^T D0) = D0^T (S1 - S0) s_val0 = np.vdot(start1 - start0, delta0) / norm0_sq # E1 = L1(1) = S0 + sB D0 # <==> sB D0 = E1 - S0 # ==> sB (D0^T D0) = D0^T (E1 - S0) s_val1 = np.vdot(end1 - start0, delta0) / norm0_sq # s = s_val0 + t (s_val1 - s_val0) # t = 0 <==> s = s_val0 # t = 1 <==> s = s_val1 # t = -s_val0 / (s_val1 - s_val0) <==> s = 0 # t = (1 - s_val0) / (s_val1 - s_val0) <==> s = 1 if s_val0 <= s_val1: # In this branch the segments are moving in the same direction, i.e. # (t=0<-->s=s_val0) are both less than (t=1<-->s_val1). if 1.0 < s_val0: return True, None elif s_val0 < 0.0: start_s = 0.0 start_t = -s_val0 / (s_val1 - s_val0) else: start_s = s_val0 start_t = 0.0 if s_val1 < 0.0: return True, None elif 1.0 < s_val1: end_s = 1.0 end_t = (1.0 - s_val0) / (s_val1 - s_val0) else: end_s = s_val1 end_t = 1.0 else: # In this branch the segments are moving in opposite directions, i.e. # in (t=0<-->s=s_val0) and (t=1<-->s_val1) we have 0 < 1 # but ``s_val0 > s_val1``. if s_val0 < 0.0: return True, None elif 1.0 < s_val0: start_s = 1.0 start_t = (s_val0 - 1.0) / (s_val0 - s_val1) else: start_s = s_val0 start_t = 0.0 if 1.0 < s_val1: return True, None elif s_val1 < 0.0: end_s = 0.0 end_t = s_val0 / (s_val0 - s_val1) else: end_s = s_val1 end_t = 1.0 parameters = np.asfortranarray([[start_s, end_s], [start_t, end_t]]) return False, parameters
[ "def", "parallel_lines_parameters", "(", "start0", ",", "end0", ",", "start1", ",", "end1", ")", ":", "# NOTE: There is no corresponding \"enable\", but the disable only applies", "# in this lexical scope.", "# pylint: disable=too-many-branches", "delta0", "=", "end0", "-", "start0", "line0_const", "=", "_helpers", ".", "cross_product", "(", "start0", ",", "delta0", ")", "start1_against", "=", "_helpers", ".", "cross_product", "(", "start1", ",", "delta0", ")", "if", "line0_const", "!=", "start1_against", ":", "return", "True", ",", "None", "# Each array is a 1D vector, so we can use the vector dot product.", "norm0_sq", "=", "np", ".", "vdot", "(", "delta0", ",", "delta0", ")", "# S1 = L1(0) = S0 + sA D0", "# <==> sA D0 = S1 - S0", "# ==> sA (D0^T D0) = D0^T (S1 - S0)", "s_val0", "=", "np", ".", "vdot", "(", "start1", "-", "start0", ",", "delta0", ")", "/", "norm0_sq", "# E1 = L1(1) = S0 + sB D0", "# <==> sB D0 = E1 - S0", "# ==> sB (D0^T D0) = D0^T (E1 - S0)", "s_val1", "=", "np", ".", "vdot", "(", "end1", "-", "start0", ",", "delta0", ")", "/", "norm0_sq", "# s = s_val0 + t (s_val1 - s_val0)", "# t = 0 <==> s = s_val0", "# t = 1 <==> s = s_val1", "# t = -s_val0 / (s_val1 - s_val0) <==> s = 0", "# t = (1 - s_val0) / (s_val1 - s_val0) <==> s = 1", "if", "s_val0", "<=", "s_val1", ":", "# In this branch the segments are moving in the same direction, i.e.", "# (t=0<-->s=s_val0) are both less than (t=1<-->s_val1).", "if", "1.0", "<", "s_val0", ":", "return", "True", ",", "None", "elif", "s_val0", "<", "0.0", ":", "start_s", "=", "0.0", "start_t", "=", "-", "s_val0", "/", "(", "s_val1", "-", "s_val0", ")", "else", ":", "start_s", "=", "s_val0", "start_t", "=", "0.0", "if", "s_val1", "<", "0.0", ":", "return", "True", ",", "None", "elif", "1.0", "<", "s_val1", ":", "end_s", "=", "1.0", "end_t", "=", "(", "1.0", "-", "s_val0", ")", "/", "(", "s_val1", "-", "s_val0", ")", "else", ":", "end_s", "=", "s_val1", "end_t", "=", "1.0", "else", ":", "# In this branch the segments are moving in opposite directions, i.e.", "# in (t=0<-->s=s_val0) and (t=1<-->s_val1) we have 0 < 1", "# but ``s_val0 > s_val1``.", "if", "s_val0", "<", "0.0", ":", "return", "True", ",", "None", "elif", "1.0", "<", "s_val0", ":", "start_s", "=", "1.0", "start_t", "=", "(", "s_val0", "-", "1.0", ")", "/", "(", "s_val0", "-", "s_val1", ")", "else", ":", "start_s", "=", "s_val0", "start_t", "=", "0.0", "if", "1.0", "<", "s_val1", ":", "return", "True", ",", "None", "elif", "s_val1", "<", "0.0", ":", "end_s", "=", "0.0", "end_t", "=", "s_val0", "/", "(", "s_val0", "-", "s_val1", ")", "else", ":", "end_s", "=", "s_val1", "end_t", "=", "1.0", "parameters", "=", "np", ".", "asfortranarray", "(", "[", "[", "start_s", ",", "end_s", "]", ",", "[", "start_t", ",", "end_t", "]", "]", ")", "return", "False", ",", "parameters" ]
r"""Checks if two parallel lines ever meet. Meant as a back-up when :func:`segment_intersection` fails. .. note:: This function assumes but never verifies that the lines are parallel. In the case that the segments are parallel and lie on **different** lines, then there is a **guarantee** of no intersection. However, if they are on the exact same line, they may define a shared segment coincident to both lines. In :func:`segment_intersection`, we utilized the normal form of the lines (via the cross product): .. math:: \begin{align*} L_0(s) \times \Delta_0 &\equiv S_0 \times \Delta_0 \\ L_1(t) \times \Delta_1 &\equiv S_1 \times \Delta_1 \end{align*} So, we can detect if :math:`S_1` is on the first line by checking if .. math:: S_0 \times \Delta_0 \stackrel{?}{=} S_1 \times \Delta_0. If it is not on the first line, then we are done, the segments don't meet: .. image:: ../images/parallel_lines_parameters1.png :align: center .. testsetup:: parallel-different1, parallel-different2 import numpy as np from bezier._geometric_intersection import parallel_lines_parameters .. doctest:: parallel-different1 >>> # Line: y = 1 >>> start0 = np.asfortranarray([0.0, 1.0]) >>> end0 = np.asfortranarray([1.0, 1.0]) >>> # Vertical shift up: y = 2 >>> start1 = np.asfortranarray([-1.0, 2.0]) >>> end1 = np.asfortranarray([3.0, 2.0]) >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. testcleanup:: parallel-different1 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters1.png") If :math:`S_1` **is** on the first line, we want to check that :math:`S_1` and :math:`E_1` define parameters outside of :math:`\left[0, 1\right]`. To compute these parameters: .. math:: L_1(t) = S_0 + s_{\ast} \Delta_0 \Longrightarrow s_{\ast} = \frac{\Delta_0^T \left( L_1(t) - S_0\right)}{\Delta_0^T \Delta_0}. For example, the intervals :math:`\left[0, 1\right]` and :math:`\left[\frac{3}{2}, 2\right]` (via :math:`S_1 = S_0 + \frac{3}{2} \Delta_0` and :math:`E_1 = S_0 + 2 \Delta_0`) correspond to segments that don't meet: .. image:: ../images/parallel_lines_parameters2.png :align: center .. doctest:: parallel-different2 >>> start0 = np.asfortranarray([1.0, 0.0]) >>> delta0 = np.asfortranarray([2.0, -1.0]) >>> end0 = start0 + 1.0 * delta0 >>> start1 = start0 + 1.5 * delta0 >>> end1 = start0 + 2.0 * delta0 >>> disjoint, _ = parallel_lines_parameters(start0, end0, start1, end1) >>> disjoint True .. testcleanup:: parallel-different2 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters2.png") but if the intervals overlap, like :math:`\left[0, 1\right]` and :math:`\left[-1, \frac{1}{2}\right]`, the segments meet: .. image:: ../images/parallel_lines_parameters3.png :align: center .. testsetup:: parallel-different3, parallel-different4 import numpy as np from bezier._geometric_intersection import parallel_lines_parameters start0 = np.asfortranarray([1.0, 0.0]) delta0 = np.asfortranarray([2.0, -1.0]) end0 = start0 + 1.0 * delta0 .. doctest:: parallel-different3 >>> start1 = start0 - 1.5 * delta0 >>> end1 = start0 + 0.5 * delta0 >>> disjoint, parameters = parallel_lines_parameters( ... start0, end0, start1, end1) >>> disjoint False >>> parameters array([[0. , 0.5 ], [0.75, 1. ]]) .. testcleanup:: parallel-different3 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters3.png") Similarly, if the second interval completely contains the first, the segments meet: .. image:: ../images/parallel_lines_parameters4.png :align: center .. doctest:: parallel-different4 >>> start1 = start0 + 4.5 * delta0 >>> end1 = start0 - 3.5 * delta0 >>> disjoint, parameters = parallel_lines_parameters( ... start0, end0, start1, end1) >>> disjoint False >>> parameters array([[1. , 0. ], [0.4375, 0.5625]]) .. testcleanup:: parallel-different4 import make_images make_images.helper_parallel_lines( start0, end0, start1, end1, "parallel_lines_parameters4.png") .. note:: This function doesn't currently allow wiggle room around the desired value, i.e. the two values must be bitwise identical. However, the most "correct" version of this function likely should allow for some round off. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: start0 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_0` of the parametric line :math:`L_0(s)`. end0 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_0` of the parametric line :math:`L_0(s)`. start1 (numpy.ndarray): A 1D NumPy ``2``-array that is the start vector :math:`S_1` of the parametric line :math:`L_1(s)`. end1 (numpy.ndarray): A 1D NumPy ``2``-array that is the end vector :math:`E_1` of the parametric line :math:`L_1(s)`. Returns: Tuple[bool, Optional[numpy.ndarray]]: A pair of * Flag indicating if the lines are disjoint. * An optional ``2 x 2`` matrix of ``s-t`` parameters only present if the lines aren't disjoint. The first column will contain the parameters at the beginning of the shared segment and the second column will correspond to the end of the shared segment.
[ "r", "Checks", "if", "two", "parallel", "lines", "ever", "meet", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L423-L676
dhermes/bezier
src/bezier/_geometric_intersection.py
line_line_collide
def line_line_collide(line1, line2): """Determine if two line segments meet. This is a helper for :func:`convex_hull_collide` in the special case that the two convex hulls are actually just line segments. (Even in this case, this is only problematic if both segments are on a single line.) Args: line1 (numpy.ndarray): ``2 x 2`` array of start and end nodes. line2 (numpy.ndarray): ``2 x 2`` array of start and end nodes. Returns: bool: Indicating if the line segments collide. """ s, t, success = segment_intersection( line1[:, 0], line1[:, 1], line2[:, 0], line2[:, 1] ) if success: return _helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval( t, 0.0, 1.0 ) else: disjoint, _ = parallel_lines_parameters( line1[:, 0], line1[:, 1], line2[:, 0], line2[:, 1] ) return not disjoint
python
def line_line_collide(line1, line2): """Determine if two line segments meet. This is a helper for :func:`convex_hull_collide` in the special case that the two convex hulls are actually just line segments. (Even in this case, this is only problematic if both segments are on a single line.) Args: line1 (numpy.ndarray): ``2 x 2`` array of start and end nodes. line2 (numpy.ndarray): ``2 x 2`` array of start and end nodes. Returns: bool: Indicating if the line segments collide. """ s, t, success = segment_intersection( line1[:, 0], line1[:, 1], line2[:, 0], line2[:, 1] ) if success: return _helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval( t, 0.0, 1.0 ) else: disjoint, _ = parallel_lines_parameters( line1[:, 0], line1[:, 1], line2[:, 0], line2[:, 1] ) return not disjoint
[ "def", "line_line_collide", "(", "line1", ",", "line2", ")", ":", "s", ",", "t", ",", "success", "=", "segment_intersection", "(", "line1", "[", ":", ",", "0", "]", ",", "line1", "[", ":", ",", "1", "]", ",", "line2", "[", ":", ",", "0", "]", ",", "line2", "[", ":", ",", "1", "]", ")", "if", "success", ":", "return", "_helpers", ".", "in_interval", "(", "s", ",", "0.0", ",", "1.0", ")", "and", "_helpers", ".", "in_interval", "(", "t", ",", "0.0", ",", "1.0", ")", "else", ":", "disjoint", ",", "_", "=", "parallel_lines_parameters", "(", "line1", "[", ":", ",", "0", "]", ",", "line1", "[", ":", ",", "1", "]", ",", "line2", "[", ":", ",", "0", "]", ",", "line2", "[", ":", ",", "1", "]", ")", "return", "not", "disjoint" ]
Determine if two line segments meet. This is a helper for :func:`convex_hull_collide` in the special case that the two convex hulls are actually just line segments. (Even in this case, this is only problematic if both segments are on a single line.) Args: line1 (numpy.ndarray): ``2 x 2`` array of start and end nodes. line2 (numpy.ndarray): ``2 x 2`` array of start and end nodes. Returns: bool: Indicating if the line segments collide.
[ "Determine", "if", "two", "line", "segments", "meet", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L679-L706
dhermes/bezier
src/bezier/_geometric_intersection.py
convex_hull_collide
def convex_hull_collide(nodes1, nodes2): """Determine if the convex hulls of two curves collide. .. note:: This is a helper for :func:`from_linearized`. Args: nodes1 (numpy.ndarray): Control points of a first curve. nodes2 (numpy.ndarray): Control points of a second curve. Returns: bool: Indicating if the convex hulls collide. """ polygon1 = _helpers.simple_convex_hull(nodes1) _, polygon_size1 = polygon1.shape polygon2 = _helpers.simple_convex_hull(nodes2) _, polygon_size2 = polygon2.shape if polygon_size1 == 2 and polygon_size2 == 2: return line_line_collide(polygon1, polygon2) else: return _helpers.polygon_collide(polygon1, polygon2)
python
def convex_hull_collide(nodes1, nodes2): """Determine if the convex hulls of two curves collide. .. note:: This is a helper for :func:`from_linearized`. Args: nodes1 (numpy.ndarray): Control points of a first curve. nodes2 (numpy.ndarray): Control points of a second curve. Returns: bool: Indicating if the convex hulls collide. """ polygon1 = _helpers.simple_convex_hull(nodes1) _, polygon_size1 = polygon1.shape polygon2 = _helpers.simple_convex_hull(nodes2) _, polygon_size2 = polygon2.shape if polygon_size1 == 2 and polygon_size2 == 2: return line_line_collide(polygon1, polygon2) else: return _helpers.polygon_collide(polygon1, polygon2)
[ "def", "convex_hull_collide", "(", "nodes1", ",", "nodes2", ")", ":", "polygon1", "=", "_helpers", ".", "simple_convex_hull", "(", "nodes1", ")", "_", ",", "polygon_size1", "=", "polygon1", ".", "shape", "polygon2", "=", "_helpers", ".", "simple_convex_hull", "(", "nodes2", ")", "_", ",", "polygon_size2", "=", "polygon2", ".", "shape", "if", "polygon_size1", "==", "2", "and", "polygon_size2", "==", "2", ":", "return", "line_line_collide", "(", "polygon1", ",", "polygon2", ")", "else", ":", "return", "_helpers", ".", "polygon_collide", "(", "polygon1", ",", "polygon2", ")" ]
Determine if the convex hulls of two curves collide. .. note:: This is a helper for :func:`from_linearized`. Args: nodes1 (numpy.ndarray): Control points of a first curve. nodes2 (numpy.ndarray): Control points of a second curve. Returns: bool: Indicating if the convex hulls collide.
[ "Determine", "if", "the", "convex", "hulls", "of", "two", "curves", "collide", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L709-L731
dhermes/bezier
src/bezier/_geometric_intersection.py
from_linearized
def from_linearized(first, second, intersections): """Determine curve-curve intersection from pair of linearizations. .. note:: This assumes that at least one of ``first`` and ``second`` is not a line. The line-line case should be handled "early" by :func:`check_lines`. .. note:: This assumes the caller has verified that the bounding boxes for ``first`` and ``second`` actually intersect. If there is an intersection along the segments, adds that intersection to ``intersections``. Otherwise, returns without doing anything. Args: first (Linearization): First curve being intersected. second (Linearization): Second curve being intersected. intersections (list): A list of existing intersections. Raises: ValueError: If ``first`` and ``second`` both have linearization error of ``0.0`` (i.e. they are both lines). This is because this function expects the caller to have used :func:`check_lines` already. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-return-statements s, t, success = segment_intersection( first.start_node, first.end_node, second.start_node, second.end_node ) bad_parameters = False if success: if not ( _helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval(t, 0.0, 1.0) ): bad_parameters = True else: if first.error == 0.0 and second.error == 0.0: raise ValueError(_UNHANDLED_LINES) # Just fall back to a Newton iteration starting in the middle of # the given intervals. bad_parameters = True s = 0.5 t = 0.5 if bad_parameters: # In the unlikely case that we have parallel segments or segments # that intersect outside of [0, 1] x [0, 1], we can still exit # if the convex hulls don't intersect. if not convex_hull_collide(first.curve.nodes, second.curve.nodes): return # Now, promote ``s`` and ``t`` onto the original curves. orig_s = (1 - s) * first.curve.start + s * first.curve.end orig_t = (1 - t) * second.curve.start + t * second.curve.end refined_s, refined_t = _intersection_helpers.full_newton( orig_s, first.curve.original_nodes, orig_t, second.curve.original_nodes ) refined_s, success = _helpers.wiggle_interval(refined_s) if not success: return refined_t, success = _helpers.wiggle_interval(refined_t) if not success: return add_intersection(refined_s, refined_t, intersections)
python
def from_linearized(first, second, intersections): """Determine curve-curve intersection from pair of linearizations. .. note:: This assumes that at least one of ``first`` and ``second`` is not a line. The line-line case should be handled "early" by :func:`check_lines`. .. note:: This assumes the caller has verified that the bounding boxes for ``first`` and ``second`` actually intersect. If there is an intersection along the segments, adds that intersection to ``intersections``. Otherwise, returns without doing anything. Args: first (Linearization): First curve being intersected. second (Linearization): Second curve being intersected. intersections (list): A list of existing intersections. Raises: ValueError: If ``first`` and ``second`` both have linearization error of ``0.0`` (i.e. they are both lines). This is because this function expects the caller to have used :func:`check_lines` already. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-return-statements s, t, success = segment_intersection( first.start_node, first.end_node, second.start_node, second.end_node ) bad_parameters = False if success: if not ( _helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval(t, 0.0, 1.0) ): bad_parameters = True else: if first.error == 0.0 and second.error == 0.0: raise ValueError(_UNHANDLED_LINES) # Just fall back to a Newton iteration starting in the middle of # the given intervals. bad_parameters = True s = 0.5 t = 0.5 if bad_parameters: # In the unlikely case that we have parallel segments or segments # that intersect outside of [0, 1] x [0, 1], we can still exit # if the convex hulls don't intersect. if not convex_hull_collide(first.curve.nodes, second.curve.nodes): return # Now, promote ``s`` and ``t`` onto the original curves. orig_s = (1 - s) * first.curve.start + s * first.curve.end orig_t = (1 - t) * second.curve.start + t * second.curve.end refined_s, refined_t = _intersection_helpers.full_newton( orig_s, first.curve.original_nodes, orig_t, second.curve.original_nodes ) refined_s, success = _helpers.wiggle_interval(refined_s) if not success: return refined_t, success = _helpers.wiggle_interval(refined_t) if not success: return add_intersection(refined_s, refined_t, intersections)
[ "def", "from_linearized", "(", "first", ",", "second", ",", "intersections", ")", ":", "# NOTE: There is no corresponding \"enable\", but the disable only applies", "# in this lexical scope.", "# pylint: disable=too-many-return-statements", "s", ",", "t", ",", "success", "=", "segment_intersection", "(", "first", ".", "start_node", ",", "first", ".", "end_node", ",", "second", ".", "start_node", ",", "second", ".", "end_node", ")", "bad_parameters", "=", "False", "if", "success", ":", "if", "not", "(", "_helpers", ".", "in_interval", "(", "s", ",", "0.0", ",", "1.0", ")", "and", "_helpers", ".", "in_interval", "(", "t", ",", "0.0", ",", "1.0", ")", ")", ":", "bad_parameters", "=", "True", "else", ":", "if", "first", ".", "error", "==", "0.0", "and", "second", ".", "error", "==", "0.0", ":", "raise", "ValueError", "(", "_UNHANDLED_LINES", ")", "# Just fall back to a Newton iteration starting in the middle of", "# the given intervals.", "bad_parameters", "=", "True", "s", "=", "0.5", "t", "=", "0.5", "if", "bad_parameters", ":", "# In the unlikely case that we have parallel segments or segments", "# that intersect outside of [0, 1] x [0, 1], we can still exit", "# if the convex hulls don't intersect.", "if", "not", "convex_hull_collide", "(", "first", ".", "curve", ".", "nodes", ",", "second", ".", "curve", ".", "nodes", ")", ":", "return", "# Now, promote ``s`` and ``t`` onto the original curves.", "orig_s", "=", "(", "1", "-", "s", ")", "*", "first", ".", "curve", ".", "start", "+", "s", "*", "first", ".", "curve", ".", "end", "orig_t", "=", "(", "1", "-", "t", ")", "*", "second", ".", "curve", ".", "start", "+", "t", "*", "second", ".", "curve", ".", "end", "refined_s", ",", "refined_t", "=", "_intersection_helpers", ".", "full_newton", "(", "orig_s", ",", "first", ".", "curve", ".", "original_nodes", ",", "orig_t", ",", "second", ".", "curve", ".", "original_nodes", ")", "refined_s", ",", "success", "=", "_helpers", ".", "wiggle_interval", "(", "refined_s", ")", "if", "not", "success", ":", "return", "refined_t", ",", "success", "=", "_helpers", ".", "wiggle_interval", "(", "refined_t", ")", "if", "not", "success", ":", "return", "add_intersection", "(", "refined_s", ",", "refined_t", ",", "intersections", ")" ]
Determine curve-curve intersection from pair of linearizations. .. note:: This assumes that at least one of ``first`` and ``second`` is not a line. The line-line case should be handled "early" by :func:`check_lines`. .. note:: This assumes the caller has verified that the bounding boxes for ``first`` and ``second`` actually intersect. If there is an intersection along the segments, adds that intersection to ``intersections``. Otherwise, returns without doing anything. Args: first (Linearization): First curve being intersected. second (Linearization): Second curve being intersected. intersections (list): A list of existing intersections. Raises: ValueError: If ``first`` and ``second`` both have linearization error of ``0.0`` (i.e. they are both lines). This is because this function expects the caller to have used :func:`check_lines` already.
[ "Determine", "curve", "-", "curve", "intersection", "from", "pair", "of", "linearizations", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L734-L805
dhermes/bezier
src/bezier/_geometric_intersection.py
add_intersection
def add_intersection(s, t, intersections): r"""Adds an intersection to list of ``intersections``. .. note:: This is a helper for :func:`from_linearized` and :func:`endpoint_check`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. Accounts for repeated intersection points. If the intersection has already been found, does nothing. If ``s`` is below :math:`2^{-10}`, it will be replaced with ``1 - s`` and compared against ``1 - s'`` for all ``s'`` already in ``intersections``. (Similar if ``t`` is below the :attr:`.ZERO_THRESHOLD`.) This is perfectly "appropriate" since evaluating a B |eacute| zier curve requires using both ``s`` and ``1 - s``, so both values are equally relevant. Compares :math:`\|p - q\|` to :math:`\|p\|` where :math:`p = (s, t)` is current candidate intersection (or the "normalized" version, such as :math:`p = (1 - s, t)`) and :math:`q` is one of the already added intersections. If the difference is below :math:`2^{-42}` (i.e. :attr`.NEWTON_ERROR_RATIO`) then the intersection is considered to be duplicate. Args: s (float): The first parameter in an intersection. t (float): The second parameter in an intersection. intersections (list): List of existing intersections. """ if not intersections: intersections.append((s, t)) return if s < _intersection_helpers.ZERO_THRESHOLD: candidate_s = 1.0 - s else: candidate_s = s if t < _intersection_helpers.ZERO_THRESHOLD: candidate_t = 1.0 - t else: candidate_t = t norm_candidate = np.linalg.norm([candidate_s, candidate_t], ord=2) for existing_s, existing_t in intersections: # NOTE: |(1 - s1) - (1 - s2)| = |s1 - s2| in exact arithmetic, so # we just compute ``s1 - s2`` rather than using # ``candidate_s`` / ``candidate_t``. Due to round-off, these # differences may be slightly different, but only up to machine # precision. delta_s = s - existing_s delta_t = t - existing_t norm_update = np.linalg.norm([delta_s, delta_t], ord=2) if ( norm_update < _intersection_helpers.NEWTON_ERROR_RATIO * norm_candidate ): return intersections.append((s, t))
python
def add_intersection(s, t, intersections): r"""Adds an intersection to list of ``intersections``. .. note:: This is a helper for :func:`from_linearized` and :func:`endpoint_check`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. Accounts for repeated intersection points. If the intersection has already been found, does nothing. If ``s`` is below :math:`2^{-10}`, it will be replaced with ``1 - s`` and compared against ``1 - s'`` for all ``s'`` already in ``intersections``. (Similar if ``t`` is below the :attr:`.ZERO_THRESHOLD`.) This is perfectly "appropriate" since evaluating a B |eacute| zier curve requires using both ``s`` and ``1 - s``, so both values are equally relevant. Compares :math:`\|p - q\|` to :math:`\|p\|` where :math:`p = (s, t)` is current candidate intersection (or the "normalized" version, such as :math:`p = (1 - s, t)`) and :math:`q` is one of the already added intersections. If the difference is below :math:`2^{-42}` (i.e. :attr`.NEWTON_ERROR_RATIO`) then the intersection is considered to be duplicate. Args: s (float): The first parameter in an intersection. t (float): The second parameter in an intersection. intersections (list): List of existing intersections. """ if not intersections: intersections.append((s, t)) return if s < _intersection_helpers.ZERO_THRESHOLD: candidate_s = 1.0 - s else: candidate_s = s if t < _intersection_helpers.ZERO_THRESHOLD: candidate_t = 1.0 - t else: candidate_t = t norm_candidate = np.linalg.norm([candidate_s, candidate_t], ord=2) for existing_s, existing_t in intersections: # NOTE: |(1 - s1) - (1 - s2)| = |s1 - s2| in exact arithmetic, so # we just compute ``s1 - s2`` rather than using # ``candidate_s`` / ``candidate_t``. Due to round-off, these # differences may be slightly different, but only up to machine # precision. delta_s = s - existing_s delta_t = t - existing_t norm_update = np.linalg.norm([delta_s, delta_t], ord=2) if ( norm_update < _intersection_helpers.NEWTON_ERROR_RATIO * norm_candidate ): return intersections.append((s, t))
[ "def", "add_intersection", "(", "s", ",", "t", ",", "intersections", ")", ":", "if", "not", "intersections", ":", "intersections", ".", "append", "(", "(", "s", ",", "t", ")", ")", "return", "if", "s", "<", "_intersection_helpers", ".", "ZERO_THRESHOLD", ":", "candidate_s", "=", "1.0", "-", "s", "else", ":", "candidate_s", "=", "s", "if", "t", "<", "_intersection_helpers", ".", "ZERO_THRESHOLD", ":", "candidate_t", "=", "1.0", "-", "t", "else", ":", "candidate_t", "=", "t", "norm_candidate", "=", "np", ".", "linalg", ".", "norm", "(", "[", "candidate_s", ",", "candidate_t", "]", ",", "ord", "=", "2", ")", "for", "existing_s", ",", "existing_t", "in", "intersections", ":", "# NOTE: |(1 - s1) - (1 - s2)| = |s1 - s2| in exact arithmetic, so", "# we just compute ``s1 - s2`` rather than using", "# ``candidate_s`` / ``candidate_t``. Due to round-off, these", "# differences may be slightly different, but only up to machine", "# precision.", "delta_s", "=", "s", "-", "existing_s", "delta_t", "=", "t", "-", "existing_t", "norm_update", "=", "np", ".", "linalg", ".", "norm", "(", "[", "delta_s", ",", "delta_t", "]", ",", "ord", "=", "2", ")", "if", "(", "norm_update", "<", "_intersection_helpers", ".", "NEWTON_ERROR_RATIO", "*", "norm_candidate", ")", ":", "return", "intersections", ".", "append", "(", "(", "s", ",", "t", ")", ")" ]
r"""Adds an intersection to list of ``intersections``. .. note:: This is a helper for :func:`from_linearized` and :func:`endpoint_check`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. Accounts for repeated intersection points. If the intersection has already been found, does nothing. If ``s`` is below :math:`2^{-10}`, it will be replaced with ``1 - s`` and compared against ``1 - s'`` for all ``s'`` already in ``intersections``. (Similar if ``t`` is below the :attr:`.ZERO_THRESHOLD`.) This is perfectly "appropriate" since evaluating a B |eacute| zier curve requires using both ``s`` and ``1 - s``, so both values are equally relevant. Compares :math:`\|p - q\|` to :math:`\|p\|` where :math:`p = (s, t)` is current candidate intersection (or the "normalized" version, such as :math:`p = (1 - s, t)`) and :math:`q` is one of the already added intersections. If the difference is below :math:`2^{-42}` (i.e. :attr`.NEWTON_ERROR_RATIO`) then the intersection is considered to be duplicate. Args: s (float): The first parameter in an intersection. t (float): The second parameter in an intersection. intersections (list): List of existing intersections.
[ "r", "Adds", "an", "intersection", "to", "list", "of", "intersections", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L808-L868
dhermes/bezier
src/bezier/_geometric_intersection.py
endpoint_check
def endpoint_check( first, node_first, s, second, node_second, t, intersections ): r"""Check if curve endpoints are identical. .. note:: This is a helper for :func:`tangent_bbox_intersection`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. Args: first (SubdividedCurve): First curve being intersected (assumed in :math:\mathbf{R}^2`). node_first (numpy.ndarray): 1D ``2``-array, one of the endpoints of ``first``. s (float): The parameter corresponding to ``node_first``, so expected to be one of ``0.0`` or ``1.0``. second (SubdividedCurve): Second curve being intersected (assumed in :math:\mathbf{R}^2`). node_second (numpy.ndarray): 1D ``2``-array, one of the endpoints of ``second``. t (float): The parameter corresponding to ``node_second``, so expected to be one of ``0.0`` or ``1.0``. intersections (list): A list of already encountered intersections. If these curves intersect at their tangency, then those intersections will be added to this list. """ if _helpers.vector_close(node_first, node_second): orig_s = (1 - s) * first.start + s * first.end orig_t = (1 - t) * second.start + t * second.end add_intersection(orig_s, orig_t, intersections)
python
def endpoint_check( first, node_first, s, second, node_second, t, intersections ): r"""Check if curve endpoints are identical. .. note:: This is a helper for :func:`tangent_bbox_intersection`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. Args: first (SubdividedCurve): First curve being intersected (assumed in :math:\mathbf{R}^2`). node_first (numpy.ndarray): 1D ``2``-array, one of the endpoints of ``first``. s (float): The parameter corresponding to ``node_first``, so expected to be one of ``0.0`` or ``1.0``. second (SubdividedCurve): Second curve being intersected (assumed in :math:\mathbf{R}^2`). node_second (numpy.ndarray): 1D ``2``-array, one of the endpoints of ``second``. t (float): The parameter corresponding to ``node_second``, so expected to be one of ``0.0`` or ``1.0``. intersections (list): A list of already encountered intersections. If these curves intersect at their tangency, then those intersections will be added to this list. """ if _helpers.vector_close(node_first, node_second): orig_s = (1 - s) * first.start + s * first.end orig_t = (1 - t) * second.start + t * second.end add_intersection(orig_s, orig_t, intersections)
[ "def", "endpoint_check", "(", "first", ",", "node_first", ",", "s", ",", "second", ",", "node_second", ",", "t", ",", "intersections", ")", ":", "if", "_helpers", ".", "vector_close", "(", "node_first", ",", "node_second", ")", ":", "orig_s", "=", "(", "1", "-", "s", ")", "*", "first", ".", "start", "+", "s", "*", "first", ".", "end", "orig_t", "=", "(", "1", "-", "t", ")", "*", "second", ".", "start", "+", "t", "*", "second", ".", "end", "add_intersection", "(", "orig_s", ",", "orig_t", ",", "intersections", ")" ]
r"""Check if curve endpoints are identical. .. note:: This is a helper for :func:`tangent_bbox_intersection`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. Args: first (SubdividedCurve): First curve being intersected (assumed in :math:\mathbf{R}^2`). node_first (numpy.ndarray): 1D ``2``-array, one of the endpoints of ``first``. s (float): The parameter corresponding to ``node_first``, so expected to be one of ``0.0`` or ``1.0``. second (SubdividedCurve): Second curve being intersected (assumed in :math:\mathbf{R}^2`). node_second (numpy.ndarray): 1D ``2``-array, one of the endpoints of ``second``. t (float): The parameter corresponding to ``node_second``, so expected to be one of ``0.0`` or ``1.0``. intersections (list): A list of already encountered intersections. If these curves intersect at their tangency, then those intersections will be added to this list.
[ "r", "Check", "if", "curve", "endpoints", "are", "identical", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L871-L903
dhermes/bezier
src/bezier/_geometric_intersection.py
tangent_bbox_intersection
def tangent_bbox_intersection(first, second, intersections): r"""Check if two curves with tangent bounding boxes intersect. .. note:: This is a helper for :func:`intersect_one_round`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. If the bounding boxes are tangent, intersection can only occur along that tangency. If the curve is **not** a line, the **only** way the curve can touch the bounding box is at the endpoints. To see this, consider the component .. math:: x(s) = \sum_j W_j x_j. Since :math:`W_j > 0` for :math:`s \in \left(0, 1\right)`, if there is some :math:`k` with :math:`x_k < M = \max x_j`, then for any interior :math:`s` .. math:: x(s) < \sum_j W_j M = M. If all :math:`x_j = M`, then :math:`B(s)` falls on the line :math:`x = M`. (A similar argument holds for the other three component-extrema types.) .. note:: This function assumes callers will not pass curves that can be linearized / are linear. In :func:`_all_intersections`, curves are pre-processed to do any linearization before the subdivision / intersection process begins. Args: first (SubdividedCurve): First curve being intersected (assumed in :math:\mathbf{R}^2`). second (SubdividedCurve): Second curve being intersected (assumed in :math:\mathbf{R}^2`). intersections (list): A list of already encountered intersections. If these curves intersect at their tangency, then those intersections will be added to this list. """ node_first1 = first.nodes[:, 0] node_first2 = first.nodes[:, -1] node_second1 = second.nodes[:, 0] node_second2 = second.nodes[:, -1] endpoint_check( first, node_first1, 0.0, second, node_second1, 0.0, intersections ) endpoint_check( first, node_first1, 0.0, second, node_second2, 1.0, intersections ) endpoint_check( first, node_first2, 1.0, second, node_second1, 0.0, intersections ) endpoint_check( first, node_first2, 1.0, second, node_second2, 1.0, intersections )
python
def tangent_bbox_intersection(first, second, intersections): r"""Check if two curves with tangent bounding boxes intersect. .. note:: This is a helper for :func:`intersect_one_round`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. If the bounding boxes are tangent, intersection can only occur along that tangency. If the curve is **not** a line, the **only** way the curve can touch the bounding box is at the endpoints. To see this, consider the component .. math:: x(s) = \sum_j W_j x_j. Since :math:`W_j > 0` for :math:`s \in \left(0, 1\right)`, if there is some :math:`k` with :math:`x_k < M = \max x_j`, then for any interior :math:`s` .. math:: x(s) < \sum_j W_j M = M. If all :math:`x_j = M`, then :math:`B(s)` falls on the line :math:`x = M`. (A similar argument holds for the other three component-extrema types.) .. note:: This function assumes callers will not pass curves that can be linearized / are linear. In :func:`_all_intersections`, curves are pre-processed to do any linearization before the subdivision / intersection process begins. Args: first (SubdividedCurve): First curve being intersected (assumed in :math:\mathbf{R}^2`). second (SubdividedCurve): Second curve being intersected (assumed in :math:\mathbf{R}^2`). intersections (list): A list of already encountered intersections. If these curves intersect at their tangency, then those intersections will be added to this list. """ node_first1 = first.nodes[:, 0] node_first2 = first.nodes[:, -1] node_second1 = second.nodes[:, 0] node_second2 = second.nodes[:, -1] endpoint_check( first, node_first1, 0.0, second, node_second1, 0.0, intersections ) endpoint_check( first, node_first1, 0.0, second, node_second2, 1.0, intersections ) endpoint_check( first, node_first2, 1.0, second, node_second1, 0.0, intersections ) endpoint_check( first, node_first2, 1.0, second, node_second2, 1.0, intersections )
[ "def", "tangent_bbox_intersection", "(", "first", ",", "second", ",", "intersections", ")", ":", "node_first1", "=", "first", ".", "nodes", "[", ":", ",", "0", "]", "node_first2", "=", "first", ".", "nodes", "[", ":", ",", "-", "1", "]", "node_second1", "=", "second", ".", "nodes", "[", ":", ",", "0", "]", "node_second2", "=", "second", ".", "nodes", "[", ":", ",", "-", "1", "]", "endpoint_check", "(", "first", ",", "node_first1", ",", "0.0", ",", "second", ",", "node_second1", ",", "0.0", ",", "intersections", ")", "endpoint_check", "(", "first", ",", "node_first1", ",", "0.0", ",", "second", ",", "node_second2", ",", "1.0", ",", "intersections", ")", "endpoint_check", "(", "first", ",", "node_first2", ",", "1.0", ",", "second", ",", "node_second1", ",", "0.0", ",", "intersections", ")", "endpoint_check", "(", "first", ",", "node_first2", ",", "1.0", ",", "second", ",", "node_second2", ",", "1.0", ",", "intersections", ")" ]
r"""Check if two curves with tangent bounding boxes intersect. .. note:: This is a helper for :func:`intersect_one_round`. These functions are used (directly or indirectly) by :func:`_all_intersections` exclusively, and that function has a Fortran equivalent. If the bounding boxes are tangent, intersection can only occur along that tangency. If the curve is **not** a line, the **only** way the curve can touch the bounding box is at the endpoints. To see this, consider the component .. math:: x(s) = \sum_j W_j x_j. Since :math:`W_j > 0` for :math:`s \in \left(0, 1\right)`, if there is some :math:`k` with :math:`x_k < M = \max x_j`, then for any interior :math:`s` .. math:: x(s) < \sum_j W_j M = M. If all :math:`x_j = M`, then :math:`B(s)` falls on the line :math:`x = M`. (A similar argument holds for the other three component-extrema types.) .. note:: This function assumes callers will not pass curves that can be linearized / are linear. In :func:`_all_intersections`, curves are pre-processed to do any linearization before the subdivision / intersection process begins. Args: first (SubdividedCurve): First curve being intersected (assumed in :math:\mathbf{R}^2`). second (SubdividedCurve): Second curve being intersected (assumed in :math:\mathbf{R}^2`). intersections (list): A list of already encountered intersections. If these curves intersect at their tangency, then those intersections will be added to this list.
[ "r", "Check", "if", "two", "curves", "with", "tangent", "bounding", "boxes", "intersect", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L906-L970
dhermes/bezier
src/bezier/_geometric_intersection.py
bbox_line_intersect
def bbox_line_intersect(nodes, line_start, line_end): r"""Determine intersection of a bounding box and a line. We do this by first checking if either the start or end node of the segment are contained in the bounding box. If they aren't, then checks if the line segment intersects any of the four sides of the bounding box. .. note:: This function is "half-finished". It makes no distinction between "tangent" intersections of the box and segment and other types of intersection. However, the distinction is worthwhile, so this function should be "upgraded" at some point. Args: nodes (numpy.ndarray): Points (``2 x N``) that determine a bounding box. line_start (numpy.ndarray): Beginning of a line segment (1D ``2``-array). line_end (numpy.ndarray): End of a line segment (1D ``2``-array). Returns: int: Enum from ``BoxIntersectionType`` indicating the type of bounding box intersection. """ left, right, bottom, top = _helpers.bbox(nodes) if _helpers.in_interval( line_start[0], left, right ) and _helpers.in_interval(line_start[1], bottom, top): return BoxIntersectionType.INTERSECTION if _helpers.in_interval(line_end[0], left, right) and _helpers.in_interval( line_end[1], bottom, top ): return BoxIntersectionType.INTERSECTION # NOTE: We allow ``segment_intersection`` to fail below (i.e. # ``success=False``). At first, this may appear to "ignore" # some potential intersections of parallel lines. However, # no intersections will be missed. If parallel lines don't # overlap, then there is nothing to miss. If they do overlap, # then either the segment will have endpoints on the box (already # covered by the checks above) or the segment will contain an # entire side of the box, which will force it to intersect the 3 # edges that meet at the two ends of those sides. The parallel # edge will be skipped, but the other two will be covered. # Bottom Edge s_bottom, t_bottom, success = segment_intersection( np.asfortranarray([left, bottom]), np.asfortranarray([right, bottom]), line_start, line_end, ) if ( success and _helpers.in_interval(s_bottom, 0.0, 1.0) and _helpers.in_interval(t_bottom, 0.0, 1.0) ): return BoxIntersectionType.INTERSECTION # Right Edge s_right, t_right, success = segment_intersection( np.asfortranarray([right, bottom]), np.asfortranarray([right, top]), line_start, line_end, ) if ( success and _helpers.in_interval(s_right, 0.0, 1.0) and _helpers.in_interval(t_right, 0.0, 1.0) ): return BoxIntersectionType.INTERSECTION # Top Edge s_top, t_top, success = segment_intersection( np.asfortranarray([right, top]), np.asfortranarray([left, top]), line_start, line_end, ) if ( success and _helpers.in_interval(s_top, 0.0, 1.0) and _helpers.in_interval(t_top, 0.0, 1.0) ): return BoxIntersectionType.INTERSECTION # NOTE: We skip the "last" edge. This is because any curve # that doesn't have an endpoint on a curve must cross # at least two, so we will have already covered such curves # in one of the branches above. return BoxIntersectionType.DISJOINT
python
def bbox_line_intersect(nodes, line_start, line_end): r"""Determine intersection of a bounding box and a line. We do this by first checking if either the start or end node of the segment are contained in the bounding box. If they aren't, then checks if the line segment intersects any of the four sides of the bounding box. .. note:: This function is "half-finished". It makes no distinction between "tangent" intersections of the box and segment and other types of intersection. However, the distinction is worthwhile, so this function should be "upgraded" at some point. Args: nodes (numpy.ndarray): Points (``2 x N``) that determine a bounding box. line_start (numpy.ndarray): Beginning of a line segment (1D ``2``-array). line_end (numpy.ndarray): End of a line segment (1D ``2``-array). Returns: int: Enum from ``BoxIntersectionType`` indicating the type of bounding box intersection. """ left, right, bottom, top = _helpers.bbox(nodes) if _helpers.in_interval( line_start[0], left, right ) and _helpers.in_interval(line_start[1], bottom, top): return BoxIntersectionType.INTERSECTION if _helpers.in_interval(line_end[0], left, right) and _helpers.in_interval( line_end[1], bottom, top ): return BoxIntersectionType.INTERSECTION # NOTE: We allow ``segment_intersection`` to fail below (i.e. # ``success=False``). At first, this may appear to "ignore" # some potential intersections of parallel lines. However, # no intersections will be missed. If parallel lines don't # overlap, then there is nothing to miss. If they do overlap, # then either the segment will have endpoints on the box (already # covered by the checks above) or the segment will contain an # entire side of the box, which will force it to intersect the 3 # edges that meet at the two ends of those sides. The parallel # edge will be skipped, but the other two will be covered. # Bottom Edge s_bottom, t_bottom, success = segment_intersection( np.asfortranarray([left, bottom]), np.asfortranarray([right, bottom]), line_start, line_end, ) if ( success and _helpers.in_interval(s_bottom, 0.0, 1.0) and _helpers.in_interval(t_bottom, 0.0, 1.0) ): return BoxIntersectionType.INTERSECTION # Right Edge s_right, t_right, success = segment_intersection( np.asfortranarray([right, bottom]), np.asfortranarray([right, top]), line_start, line_end, ) if ( success and _helpers.in_interval(s_right, 0.0, 1.0) and _helpers.in_interval(t_right, 0.0, 1.0) ): return BoxIntersectionType.INTERSECTION # Top Edge s_top, t_top, success = segment_intersection( np.asfortranarray([right, top]), np.asfortranarray([left, top]), line_start, line_end, ) if ( success and _helpers.in_interval(s_top, 0.0, 1.0) and _helpers.in_interval(t_top, 0.0, 1.0) ): return BoxIntersectionType.INTERSECTION # NOTE: We skip the "last" edge. This is because any curve # that doesn't have an endpoint on a curve must cross # at least two, so we will have already covered such curves # in one of the branches above. return BoxIntersectionType.DISJOINT
[ "def", "bbox_line_intersect", "(", "nodes", ",", "line_start", ",", "line_end", ")", ":", "left", ",", "right", ",", "bottom", ",", "top", "=", "_helpers", ".", "bbox", "(", "nodes", ")", "if", "_helpers", ".", "in_interval", "(", "line_start", "[", "0", "]", ",", "left", ",", "right", ")", "and", "_helpers", ".", "in_interval", "(", "line_start", "[", "1", "]", ",", "bottom", ",", "top", ")", ":", "return", "BoxIntersectionType", ".", "INTERSECTION", "if", "_helpers", ".", "in_interval", "(", "line_end", "[", "0", "]", ",", "left", ",", "right", ")", "and", "_helpers", ".", "in_interval", "(", "line_end", "[", "1", "]", ",", "bottom", ",", "top", ")", ":", "return", "BoxIntersectionType", ".", "INTERSECTION", "# NOTE: We allow ``segment_intersection`` to fail below (i.e.", "# ``success=False``). At first, this may appear to \"ignore\"", "# some potential intersections of parallel lines. However,", "# no intersections will be missed. If parallel lines don't", "# overlap, then there is nothing to miss. If they do overlap,", "# then either the segment will have endpoints on the box (already", "# covered by the checks above) or the segment will contain an", "# entire side of the box, which will force it to intersect the 3", "# edges that meet at the two ends of those sides. The parallel", "# edge will be skipped, but the other two will be covered.", "# Bottom Edge", "s_bottom", ",", "t_bottom", ",", "success", "=", "segment_intersection", "(", "np", ".", "asfortranarray", "(", "[", "left", ",", "bottom", "]", ")", ",", "np", ".", "asfortranarray", "(", "[", "right", ",", "bottom", "]", ")", ",", "line_start", ",", "line_end", ",", ")", "if", "(", "success", "and", "_helpers", ".", "in_interval", "(", "s_bottom", ",", "0.0", ",", "1.0", ")", "and", "_helpers", ".", "in_interval", "(", "t_bottom", ",", "0.0", ",", "1.0", ")", ")", ":", "return", "BoxIntersectionType", ".", "INTERSECTION", "# Right Edge", "s_right", ",", "t_right", ",", "success", "=", "segment_intersection", "(", "np", ".", "asfortranarray", "(", "[", "right", ",", "bottom", "]", ")", ",", "np", ".", "asfortranarray", "(", "[", "right", ",", "top", "]", ")", ",", "line_start", ",", "line_end", ",", ")", "if", "(", "success", "and", "_helpers", ".", "in_interval", "(", "s_right", ",", "0.0", ",", "1.0", ")", "and", "_helpers", ".", "in_interval", "(", "t_right", ",", "0.0", ",", "1.0", ")", ")", ":", "return", "BoxIntersectionType", ".", "INTERSECTION", "# Top Edge", "s_top", ",", "t_top", ",", "success", "=", "segment_intersection", "(", "np", ".", "asfortranarray", "(", "[", "right", ",", "top", "]", ")", ",", "np", ".", "asfortranarray", "(", "[", "left", ",", "top", "]", ")", ",", "line_start", ",", "line_end", ",", ")", "if", "(", "success", "and", "_helpers", ".", "in_interval", "(", "s_top", ",", "0.0", ",", "1.0", ")", "and", "_helpers", ".", "in_interval", "(", "t_top", ",", "0.0", ",", "1.0", ")", ")", ":", "return", "BoxIntersectionType", ".", "INTERSECTION", "# NOTE: We skip the \"last\" edge. This is because any curve", "# that doesn't have an endpoint on a curve must cross", "# at least two, so we will have already covered such curves", "# in one of the branches above.", "return", "BoxIntersectionType", ".", "DISJOINT" ]
r"""Determine intersection of a bounding box and a line. We do this by first checking if either the start or end node of the segment are contained in the bounding box. If they aren't, then checks if the line segment intersects any of the four sides of the bounding box. .. note:: This function is "half-finished". It makes no distinction between "tangent" intersections of the box and segment and other types of intersection. However, the distinction is worthwhile, so this function should be "upgraded" at some point. Args: nodes (numpy.ndarray): Points (``2 x N``) that determine a bounding box. line_start (numpy.ndarray): Beginning of a line segment (1D ``2``-array). line_end (numpy.ndarray): End of a line segment (1D ``2``-array). Returns: int: Enum from ``BoxIntersectionType`` indicating the type of bounding box intersection.
[ "r", "Determine", "intersection", "of", "a", "bounding", "box", "and", "a", "line", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L973-L1066
dhermes/bezier
src/bezier/_geometric_intersection.py
intersect_one_round
def intersect_one_round(candidates, intersections): """Perform one step of the intersection process. .. note:: This is a helper for :func:`_all_intersections` and that function has a Fortran equivalent. Checks if the bounding boxes of each pair in ``candidates`` intersect. If the bounding boxes do not intersect, the pair is discarded. Otherwise, the pair is "accepted". Then we attempt to linearize each curve in an "accepted" pair and track the overall linearization error for every curve encountered. Args: candidates (Union[list, itertools.chain]): An iterable of pairs of curves (or linearized curves). intersections (list): A list of already encountered intersections. If any intersections can be readily determined during this round of subdivision, then they will be added to this list. Returns: list: Returns a list of the next round of ``candidates``. """ next_candidates = [] # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. for first, second in candidates: both_linearized = False if first.__class__ is Linearization: if second.__class__ is Linearization: both_linearized = True bbox_int = bbox_intersect( first.curve.nodes, second.curve.nodes ) else: bbox_int = bbox_line_intersect( second.nodes, first.start_node, first.end_node ) else: if second.__class__ is Linearization: bbox_int = bbox_line_intersect( first.nodes, second.start_node, second.end_node ) else: bbox_int = bbox_intersect(first.nodes, second.nodes) if bbox_int == BoxIntersectionType.DISJOINT: continue elif bbox_int == BoxIntersectionType.TANGENT and not both_linearized: # NOTE: Ignore tangent bounding boxes in the linearized case # because ``tangent_bbox_intersection()`` assumes that both # curves are not linear. tangent_bbox_intersection(first, second, intersections) continue if both_linearized: # If both ``first`` and ``second`` are linearizations, then # we can intersect them immediately. from_linearized(first, second, intersections) continue # If we haven't ``continue``-d, add the accepted pair. # NOTE: This may be a wasted computation, e.g. if ``first`` # or ``second`` occur in multiple accepted pairs (the caller # only passes one pair at a time). However, in practice # the number of such pairs will be small so this cost # will be low. lin1 = six.moves.map(Linearization.from_shape, first.subdivide()) lin2 = six.moves.map(Linearization.from_shape, second.subdivide()) next_candidates.extend(itertools.product(lin1, lin2)) return next_candidates
python
def intersect_one_round(candidates, intersections): """Perform one step of the intersection process. .. note:: This is a helper for :func:`_all_intersections` and that function has a Fortran equivalent. Checks if the bounding boxes of each pair in ``candidates`` intersect. If the bounding boxes do not intersect, the pair is discarded. Otherwise, the pair is "accepted". Then we attempt to linearize each curve in an "accepted" pair and track the overall linearization error for every curve encountered. Args: candidates (Union[list, itertools.chain]): An iterable of pairs of curves (or linearized curves). intersections (list): A list of already encountered intersections. If any intersections can be readily determined during this round of subdivision, then they will be added to this list. Returns: list: Returns a list of the next round of ``candidates``. """ next_candidates = [] # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. for first, second in candidates: both_linearized = False if first.__class__ is Linearization: if second.__class__ is Linearization: both_linearized = True bbox_int = bbox_intersect( first.curve.nodes, second.curve.nodes ) else: bbox_int = bbox_line_intersect( second.nodes, first.start_node, first.end_node ) else: if second.__class__ is Linearization: bbox_int = bbox_line_intersect( first.nodes, second.start_node, second.end_node ) else: bbox_int = bbox_intersect(first.nodes, second.nodes) if bbox_int == BoxIntersectionType.DISJOINT: continue elif bbox_int == BoxIntersectionType.TANGENT and not both_linearized: # NOTE: Ignore tangent bounding boxes in the linearized case # because ``tangent_bbox_intersection()`` assumes that both # curves are not linear. tangent_bbox_intersection(first, second, intersections) continue if both_linearized: # If both ``first`` and ``second`` are linearizations, then # we can intersect them immediately. from_linearized(first, second, intersections) continue # If we haven't ``continue``-d, add the accepted pair. # NOTE: This may be a wasted computation, e.g. if ``first`` # or ``second`` occur in multiple accepted pairs (the caller # only passes one pair at a time). However, in practice # the number of such pairs will be small so this cost # will be low. lin1 = six.moves.map(Linearization.from_shape, first.subdivide()) lin2 = six.moves.map(Linearization.from_shape, second.subdivide()) next_candidates.extend(itertools.product(lin1, lin2)) return next_candidates
[ "def", "intersect_one_round", "(", "candidates", ",", "intersections", ")", ":", "next_candidates", "=", "[", "]", "# NOTE: In the below we replace ``isinstance(a, B)`` with", "# ``a.__class__ is B``, which is a 3-3.5x speedup.", "for", "first", ",", "second", "in", "candidates", ":", "both_linearized", "=", "False", "if", "first", ".", "__class__", "is", "Linearization", ":", "if", "second", ".", "__class__", "is", "Linearization", ":", "both_linearized", "=", "True", "bbox_int", "=", "bbox_intersect", "(", "first", ".", "curve", ".", "nodes", ",", "second", ".", "curve", ".", "nodes", ")", "else", ":", "bbox_int", "=", "bbox_line_intersect", "(", "second", ".", "nodes", ",", "first", ".", "start_node", ",", "first", ".", "end_node", ")", "else", ":", "if", "second", ".", "__class__", "is", "Linearization", ":", "bbox_int", "=", "bbox_line_intersect", "(", "first", ".", "nodes", ",", "second", ".", "start_node", ",", "second", ".", "end_node", ")", "else", ":", "bbox_int", "=", "bbox_intersect", "(", "first", ".", "nodes", ",", "second", ".", "nodes", ")", "if", "bbox_int", "==", "BoxIntersectionType", ".", "DISJOINT", ":", "continue", "elif", "bbox_int", "==", "BoxIntersectionType", ".", "TANGENT", "and", "not", "both_linearized", ":", "# NOTE: Ignore tangent bounding boxes in the linearized case", "# because ``tangent_bbox_intersection()`` assumes that both", "# curves are not linear.", "tangent_bbox_intersection", "(", "first", ",", "second", ",", "intersections", ")", "continue", "if", "both_linearized", ":", "# If both ``first`` and ``second`` are linearizations, then", "# we can intersect them immediately.", "from_linearized", "(", "first", ",", "second", ",", "intersections", ")", "continue", "# If we haven't ``continue``-d, add the accepted pair.", "# NOTE: This may be a wasted computation, e.g. if ``first``", "# or ``second`` occur in multiple accepted pairs (the caller", "# only passes one pair at a time). However, in practice", "# the number of such pairs will be small so this cost", "# will be low.", "lin1", "=", "six", ".", "moves", ".", "map", "(", "Linearization", ".", "from_shape", ",", "first", ".", "subdivide", "(", ")", ")", "lin2", "=", "six", ".", "moves", ".", "map", "(", "Linearization", ".", "from_shape", ",", "second", ".", "subdivide", "(", ")", ")", "next_candidates", ".", "extend", "(", "itertools", ".", "product", "(", "lin1", ",", "lin2", ")", ")", "return", "next_candidates" ]
Perform one step of the intersection process. .. note:: This is a helper for :func:`_all_intersections` and that function has a Fortran equivalent. Checks if the bounding boxes of each pair in ``candidates`` intersect. If the bounding boxes do not intersect, the pair is discarded. Otherwise, the pair is "accepted". Then we attempt to linearize each curve in an "accepted" pair and track the overall linearization error for every curve encountered. Args: candidates (Union[list, itertools.chain]): An iterable of pairs of curves (or linearized curves). intersections (list): A list of already encountered intersections. If any intersections can be readily determined during this round of subdivision, then they will be added to this list. Returns: list: Returns a list of the next round of ``candidates``.
[ "Perform", "one", "step", "of", "the", "intersection", "process", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1069-L1142
dhermes/bezier
src/bezier/_geometric_intersection.py
prune_candidates
def prune_candidates(candidates): """Reduce number of candidate intersection pairs. .. note:: This is a helper for :func:`_all_intersections`. Uses more strict bounding box intersection predicate by forming the actual convex hull of each candidate curve segment and then checking if those convex hulls collide. Args: candidates (List): An iterable of pairs of curves (or linearized curves). Returns: List: A pruned list of curve pairs. """ pruned = [] # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. for first, second in candidates: if first.__class__ is Linearization: nodes1 = first.curve.nodes else: nodes1 = first.nodes if second.__class__ is Linearization: nodes2 = second.curve.nodes else: nodes2 = second.nodes if convex_hull_collide(nodes1, nodes2): pruned.append((first, second)) return pruned
python
def prune_candidates(candidates): """Reduce number of candidate intersection pairs. .. note:: This is a helper for :func:`_all_intersections`. Uses more strict bounding box intersection predicate by forming the actual convex hull of each candidate curve segment and then checking if those convex hulls collide. Args: candidates (List): An iterable of pairs of curves (or linearized curves). Returns: List: A pruned list of curve pairs. """ pruned = [] # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. for first, second in candidates: if first.__class__ is Linearization: nodes1 = first.curve.nodes else: nodes1 = first.nodes if second.__class__ is Linearization: nodes2 = second.curve.nodes else: nodes2 = second.nodes if convex_hull_collide(nodes1, nodes2): pruned.append((first, second)) return pruned
[ "def", "prune_candidates", "(", "candidates", ")", ":", "pruned", "=", "[", "]", "# NOTE: In the below we replace ``isinstance(a, B)`` with", "# ``a.__class__ is B``, which is a 3-3.5x speedup.", "for", "first", ",", "second", "in", "candidates", ":", "if", "first", ".", "__class__", "is", "Linearization", ":", "nodes1", "=", "first", ".", "curve", ".", "nodes", "else", ":", "nodes1", "=", "first", ".", "nodes", "if", "second", ".", "__class__", "is", "Linearization", ":", "nodes2", "=", "second", ".", "curve", ".", "nodes", "else", ":", "nodes2", "=", "second", ".", "nodes", "if", "convex_hull_collide", "(", "nodes1", ",", "nodes2", ")", ":", "pruned", ".", "append", "(", "(", "first", ",", "second", ")", ")", "return", "pruned" ]
Reduce number of candidate intersection pairs. .. note:: This is a helper for :func:`_all_intersections`. Uses more strict bounding box intersection predicate by forming the actual convex hull of each candidate curve segment and then checking if those convex hulls collide. Args: candidates (List): An iterable of pairs of curves (or linearized curves). Returns: List: A pruned list of curve pairs.
[ "Reduce", "number", "of", "candidate", "intersection", "pairs", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1145-L1177
dhermes/bezier
src/bezier/_geometric_intersection.py
make_same_degree
def make_same_degree(nodes1, nodes2): """Degree-elevate a curve so two curves have matching degree. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier curve. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The potentially degree-elevated nodes passed in. """ _, num_nodes1 = nodes1.shape _, num_nodes2 = nodes2.shape for _ in six.moves.xrange(num_nodes2 - num_nodes1): nodes1 = _curve_helpers.elevate_nodes(nodes1) for _ in six.moves.xrange(num_nodes1 - num_nodes2): nodes2 = _curve_helpers.elevate_nodes(nodes2) return nodes1, nodes2
python
def make_same_degree(nodes1, nodes2): """Degree-elevate a curve so two curves have matching degree. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier curve. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The potentially degree-elevated nodes passed in. """ _, num_nodes1 = nodes1.shape _, num_nodes2 = nodes2.shape for _ in six.moves.xrange(num_nodes2 - num_nodes1): nodes1 = _curve_helpers.elevate_nodes(nodes1) for _ in six.moves.xrange(num_nodes1 - num_nodes2): nodes2 = _curve_helpers.elevate_nodes(nodes2) return nodes1, nodes2
[ "def", "make_same_degree", "(", "nodes1", ",", "nodes2", ")", ":", "_", ",", "num_nodes1", "=", "nodes1", ".", "shape", "_", ",", "num_nodes2", "=", "nodes2", ".", "shape", "for", "_", "in", "six", ".", "moves", ".", "xrange", "(", "num_nodes2", "-", "num_nodes1", ")", ":", "nodes1", "=", "_curve_helpers", ".", "elevate_nodes", "(", "nodes1", ")", "for", "_", "in", "six", ".", "moves", ".", "xrange", "(", "num_nodes1", "-", "num_nodes2", ")", ":", "nodes2", "=", "_curve_helpers", ".", "elevate_nodes", "(", "nodes2", ")", "return", "nodes1", ",", "nodes2" ]
Degree-elevate a curve so two curves have matching degree. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier curve. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The potentially degree-elevated nodes passed in.
[ "Degree", "-", "elevate", "a", "curve", "so", "two", "curves", "have", "matching", "degree", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1180-L1199
dhermes/bezier
src/bezier/_geometric_intersection.py
coincident_parameters
def coincident_parameters(nodes1, nodes2): r"""Check if two B |eacute| zier curves are coincident. Does so by projecting each segment endpoint onto the other curve .. math:: B_1(s_0) = B_2(0) B_1(s_m) = B_2(1) B_1(0) = B_2(t_0) B_1(1) = B_2(t_n) and then finding the "shared interval" where both curves are defined. If such an interval can't be found (e.g. if one of the endpoints can't be located on the other curve), returns :data:`None`. If such a "shared interval" does exist, then this will specialize each curve onto that shared interval and check if the new control points agree. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier curve. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier curve. Returns: Optional[Tuple[Tuple[float, float], ...]]: A ``2 x 2`` array of parameters where the two coincident curves meet. If they are not coincident, returns :data:`None`. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-return-statements,too-many-branches nodes1, nodes2 = make_same_degree(nodes1, nodes2) s_initial = _curve_helpers.locate_point( nodes1, nodes2[:, 0].reshape((2, 1), order="F") ) s_final = _curve_helpers.locate_point( nodes1, nodes2[:, -1].reshape((2, 1), order="F") ) if s_initial is not None and s_final is not None: # In this case, if the curves were coincident, then ``curve2`` # would be "fully" contained in ``curve1``, so we specialize # ``curve1`` down to that interval to check. specialized1 = _curve_helpers.specialize_curve( nodes1, s_initial, s_final ) if _helpers.vector_close( specialized1.ravel(order="F"), nodes2.ravel(order="F") ): return ((s_initial, 0.0), (s_final, 1.0)) else: return None t_initial = _curve_helpers.locate_point( nodes2, nodes1[:, 0].reshape((2, 1), order="F") ) t_final = _curve_helpers.locate_point( nodes2, nodes1[:, -1].reshape((2, 1), order="F") ) if t_initial is None and t_final is None: # An overlap must have two endpoints and since at most one of the # endpoints of ``curve2`` lies on ``curve1`` (as indicated by at # least one of the ``s``-parameters being ``None``), we need (at least) # one endpoint of ``curve1`` on ``curve2``. return None if t_initial is not None and t_final is not None: # In this case, if the curves were coincident, then ``curve1`` # would be "fully" contained in ``curve2``, so we specialize # ``curve2`` down to that interval to check. specialized2 = _curve_helpers.specialize_curve( nodes2, t_initial, t_final ) if _helpers.vector_close( nodes1.ravel(order="F"), specialized2.ravel(order="F") ): return ((0.0, t_initial), (1.0, t_final)) else: return None if s_initial is None and s_final is None: # An overlap must have two endpoints and since exactly one of the # endpoints of ``curve1`` lies on ``curve2`` (as indicated by exactly # one of the ``t``-parameters being ``None``), we need (at least) # one endpoint of ``curve1`` on ``curve2``. return None # At this point, we know exactly one of the ``s``-parameters and exactly # one of the ``t``-parameters is not ``None``. if s_initial is None: if t_initial is None: # B1(s_final) = B2(1) AND B1(1) = B2(t_final) start_s = s_final end_s = 1.0 start_t = 1.0 end_t = t_final else: # B1(0) = B2(t_initial) AND B1(s_final) = B2(1) start_s = 0.0 end_s = s_final start_t = t_initial end_t = 1.0 else: if t_initial is None: # B1(s_initial) = B2(0) AND B1(1 ) = B2(t_final) start_s = s_initial end_s = 1.0 start_t = 0.0 end_t = t_final else: # B1(0) = B2(t_initial) AND B1(s_initial) = B2(0) start_s = 0.0 end_s = s_initial start_t = t_initial end_t = 0.0 width_s = abs(start_s - end_s) width_t = abs(start_t - end_t) if width_s < _MIN_INTERVAL_WIDTH and width_t < _MIN_INTERVAL_WIDTH: return None specialized1 = _curve_helpers.specialize_curve(nodes1, start_s, end_s) specialized2 = _curve_helpers.specialize_curve(nodes2, start_t, end_t) if _helpers.vector_close( specialized1.ravel(order="F"), specialized2.ravel(order="F") ): return ((start_s, start_t), (end_s, end_t)) else: return None
python
def coincident_parameters(nodes1, nodes2): r"""Check if two B |eacute| zier curves are coincident. Does so by projecting each segment endpoint onto the other curve .. math:: B_1(s_0) = B_2(0) B_1(s_m) = B_2(1) B_1(0) = B_2(t_0) B_1(1) = B_2(t_n) and then finding the "shared interval" where both curves are defined. If such an interval can't be found (e.g. if one of the endpoints can't be located on the other curve), returns :data:`None`. If such a "shared interval" does exist, then this will specialize each curve onto that shared interval and check if the new control points agree. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier curve. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier curve. Returns: Optional[Tuple[Tuple[float, float], ...]]: A ``2 x 2`` array of parameters where the two coincident curves meet. If they are not coincident, returns :data:`None`. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-return-statements,too-many-branches nodes1, nodes2 = make_same_degree(nodes1, nodes2) s_initial = _curve_helpers.locate_point( nodes1, nodes2[:, 0].reshape((2, 1), order="F") ) s_final = _curve_helpers.locate_point( nodes1, nodes2[:, -1].reshape((2, 1), order="F") ) if s_initial is not None and s_final is not None: # In this case, if the curves were coincident, then ``curve2`` # would be "fully" contained in ``curve1``, so we specialize # ``curve1`` down to that interval to check. specialized1 = _curve_helpers.specialize_curve( nodes1, s_initial, s_final ) if _helpers.vector_close( specialized1.ravel(order="F"), nodes2.ravel(order="F") ): return ((s_initial, 0.0), (s_final, 1.0)) else: return None t_initial = _curve_helpers.locate_point( nodes2, nodes1[:, 0].reshape((2, 1), order="F") ) t_final = _curve_helpers.locate_point( nodes2, nodes1[:, -1].reshape((2, 1), order="F") ) if t_initial is None and t_final is None: # An overlap must have two endpoints and since at most one of the # endpoints of ``curve2`` lies on ``curve1`` (as indicated by at # least one of the ``s``-parameters being ``None``), we need (at least) # one endpoint of ``curve1`` on ``curve2``. return None if t_initial is not None and t_final is not None: # In this case, if the curves were coincident, then ``curve1`` # would be "fully" contained in ``curve2``, so we specialize # ``curve2`` down to that interval to check. specialized2 = _curve_helpers.specialize_curve( nodes2, t_initial, t_final ) if _helpers.vector_close( nodes1.ravel(order="F"), specialized2.ravel(order="F") ): return ((0.0, t_initial), (1.0, t_final)) else: return None if s_initial is None and s_final is None: # An overlap must have two endpoints and since exactly one of the # endpoints of ``curve1`` lies on ``curve2`` (as indicated by exactly # one of the ``t``-parameters being ``None``), we need (at least) # one endpoint of ``curve1`` on ``curve2``. return None # At this point, we know exactly one of the ``s``-parameters and exactly # one of the ``t``-parameters is not ``None``. if s_initial is None: if t_initial is None: # B1(s_final) = B2(1) AND B1(1) = B2(t_final) start_s = s_final end_s = 1.0 start_t = 1.0 end_t = t_final else: # B1(0) = B2(t_initial) AND B1(s_final) = B2(1) start_s = 0.0 end_s = s_final start_t = t_initial end_t = 1.0 else: if t_initial is None: # B1(s_initial) = B2(0) AND B1(1 ) = B2(t_final) start_s = s_initial end_s = 1.0 start_t = 0.0 end_t = t_final else: # B1(0) = B2(t_initial) AND B1(s_initial) = B2(0) start_s = 0.0 end_s = s_initial start_t = t_initial end_t = 0.0 width_s = abs(start_s - end_s) width_t = abs(start_t - end_t) if width_s < _MIN_INTERVAL_WIDTH and width_t < _MIN_INTERVAL_WIDTH: return None specialized1 = _curve_helpers.specialize_curve(nodes1, start_s, end_s) specialized2 = _curve_helpers.specialize_curve(nodes2, start_t, end_t) if _helpers.vector_close( specialized1.ravel(order="F"), specialized2.ravel(order="F") ): return ((start_s, start_t), (end_s, end_t)) else: return None
[ "def", "coincident_parameters", "(", "nodes1", ",", "nodes2", ")", ":", "# NOTE: There is no corresponding \"enable\", but the disable only applies", "# in this lexical scope.", "# pylint: disable=too-many-return-statements,too-many-branches", "nodes1", ",", "nodes2", "=", "make_same_degree", "(", "nodes1", ",", "nodes2", ")", "s_initial", "=", "_curve_helpers", ".", "locate_point", "(", "nodes1", ",", "nodes2", "[", ":", ",", "0", "]", ".", "reshape", "(", "(", "2", ",", "1", ")", ",", "order", "=", "\"F\"", ")", ")", "s_final", "=", "_curve_helpers", ".", "locate_point", "(", "nodes1", ",", "nodes2", "[", ":", ",", "-", "1", "]", ".", "reshape", "(", "(", "2", ",", "1", ")", ",", "order", "=", "\"F\"", ")", ")", "if", "s_initial", "is", "not", "None", "and", "s_final", "is", "not", "None", ":", "# In this case, if the curves were coincident, then ``curve2``", "# would be \"fully\" contained in ``curve1``, so we specialize", "# ``curve1`` down to that interval to check.", "specialized1", "=", "_curve_helpers", ".", "specialize_curve", "(", "nodes1", ",", "s_initial", ",", "s_final", ")", "if", "_helpers", ".", "vector_close", "(", "specialized1", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "nodes2", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", ":", "return", "(", "(", "s_initial", ",", "0.0", ")", ",", "(", "s_final", ",", "1.0", ")", ")", "else", ":", "return", "None", "t_initial", "=", "_curve_helpers", ".", "locate_point", "(", "nodes2", ",", "nodes1", "[", ":", ",", "0", "]", ".", "reshape", "(", "(", "2", ",", "1", ")", ",", "order", "=", "\"F\"", ")", ")", "t_final", "=", "_curve_helpers", ".", "locate_point", "(", "nodes2", ",", "nodes1", "[", ":", ",", "-", "1", "]", ".", "reshape", "(", "(", "2", ",", "1", ")", ",", "order", "=", "\"F\"", ")", ")", "if", "t_initial", "is", "None", "and", "t_final", "is", "None", ":", "# An overlap must have two endpoints and since at most one of the", "# endpoints of ``curve2`` lies on ``curve1`` (as indicated by at", "# least one of the ``s``-parameters being ``None``), we need (at least)", "# one endpoint of ``curve1`` on ``curve2``.", "return", "None", "if", "t_initial", "is", "not", "None", "and", "t_final", "is", "not", "None", ":", "# In this case, if the curves were coincident, then ``curve1``", "# would be \"fully\" contained in ``curve2``, so we specialize", "# ``curve2`` down to that interval to check.", "specialized2", "=", "_curve_helpers", ".", "specialize_curve", "(", "nodes2", ",", "t_initial", ",", "t_final", ")", "if", "_helpers", ".", "vector_close", "(", "nodes1", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "specialized2", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", ":", "return", "(", "(", "0.0", ",", "t_initial", ")", ",", "(", "1.0", ",", "t_final", ")", ")", "else", ":", "return", "None", "if", "s_initial", "is", "None", "and", "s_final", "is", "None", ":", "# An overlap must have two endpoints and since exactly one of the", "# endpoints of ``curve1`` lies on ``curve2`` (as indicated by exactly", "# one of the ``t``-parameters being ``None``), we need (at least)", "# one endpoint of ``curve1`` on ``curve2``.", "return", "None", "# At this point, we know exactly one of the ``s``-parameters and exactly", "# one of the ``t``-parameters is not ``None``.", "if", "s_initial", "is", "None", ":", "if", "t_initial", "is", "None", ":", "# B1(s_final) = B2(1) AND B1(1) = B2(t_final)", "start_s", "=", "s_final", "end_s", "=", "1.0", "start_t", "=", "1.0", "end_t", "=", "t_final", "else", ":", "# B1(0) = B2(t_initial) AND B1(s_final) = B2(1)", "start_s", "=", "0.0", "end_s", "=", "s_final", "start_t", "=", "t_initial", "end_t", "=", "1.0", "else", ":", "if", "t_initial", "is", "None", ":", "# B1(s_initial) = B2(0) AND B1(1 ) = B2(t_final)", "start_s", "=", "s_initial", "end_s", "=", "1.0", "start_t", "=", "0.0", "end_t", "=", "t_final", "else", ":", "# B1(0) = B2(t_initial) AND B1(s_initial) = B2(0)", "start_s", "=", "0.0", "end_s", "=", "s_initial", "start_t", "=", "t_initial", "end_t", "=", "0.0", "width_s", "=", "abs", "(", "start_s", "-", "end_s", ")", "width_t", "=", "abs", "(", "start_t", "-", "end_t", ")", "if", "width_s", "<", "_MIN_INTERVAL_WIDTH", "and", "width_t", "<", "_MIN_INTERVAL_WIDTH", ":", "return", "None", "specialized1", "=", "_curve_helpers", ".", "specialize_curve", "(", "nodes1", ",", "start_s", ",", "end_s", ")", "specialized2", "=", "_curve_helpers", ".", "specialize_curve", "(", "nodes2", ",", "start_t", ",", "end_t", ")", "if", "_helpers", ".", "vector_close", "(", "specialized1", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "specialized2", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", ":", "return", "(", "(", "start_s", ",", "start_t", ")", ",", "(", "end_s", ",", "end_t", ")", ")", "else", ":", "return", "None" ]
r"""Check if two B |eacute| zier curves are coincident. Does so by projecting each segment endpoint onto the other curve .. math:: B_1(s_0) = B_2(0) B_1(s_m) = B_2(1) B_1(0) = B_2(t_0) B_1(1) = B_2(t_n) and then finding the "shared interval" where both curves are defined. If such an interval can't be found (e.g. if one of the endpoints can't be located on the other curve), returns :data:`None`. If such a "shared interval" does exist, then this will specialize each curve onto that shared interval and check if the new control points agree. Args: nodes1 (numpy.ndarray): Set of control points for a B |eacute| zier curve. nodes2 (numpy.ndarray): Set of control points for a B |eacute| zier curve. Returns: Optional[Tuple[Tuple[float, float], ...]]: A ``2 x 2`` array of parameters where the two coincident curves meet. If they are not coincident, returns :data:`None`.
[ "r", "Check", "if", "two", "B", "|eacute|", "zier", "curves", "are", "coincident", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1202-L1334
dhermes/bezier
src/bezier/_geometric_intersection.py
check_lines
def check_lines(first, second): """Checks if two curves are lines and tries to intersect them. .. note:: This is a helper for :func:`._all_intersections`. If they are not lines / not linearized, immediately returns :data:`False` with no "return value". If they are lines, attempts to intersect them (even if they are parallel and share a coincident segment). Args: first (Union[SubdividedCurve, Linearization]): First curve being intersected. second (Union[SubdividedCurve, Linearization]): Second curve being intersected. Returns: Tuple[bool, Optional[Tuple[numpy.ndarray, bool]]]: A pair of * Flag indicating if both candidates in the pair are lines. * Optional "result" populated only if both candidates are lines. When this result is populated, it will be a pair of * array of parameters of intersection * flag indicating if the two candidates share a coincident segment """ # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. if not ( first.__class__ is Linearization and second.__class__ is Linearization and first.error == 0.0 and second.error == 0.0 ): return False, None s, t, success = segment_intersection( first.start_node, first.end_node, second.start_node, second.end_node ) if success: if _helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval( t, 0.0, 1.0 ): intersections = np.asfortranarray([[s], [t]]) result = intersections, False else: result = np.empty((2, 0), order="F"), False else: disjoint, params = parallel_lines_parameters( first.start_node, first.end_node, second.start_node, second.end_node, ) if disjoint: result = np.empty((2, 0), order="F"), False else: result = params, True return True, result
python
def check_lines(first, second): """Checks if two curves are lines and tries to intersect them. .. note:: This is a helper for :func:`._all_intersections`. If they are not lines / not linearized, immediately returns :data:`False` with no "return value". If they are lines, attempts to intersect them (even if they are parallel and share a coincident segment). Args: first (Union[SubdividedCurve, Linearization]): First curve being intersected. second (Union[SubdividedCurve, Linearization]): Second curve being intersected. Returns: Tuple[bool, Optional[Tuple[numpy.ndarray, bool]]]: A pair of * Flag indicating if both candidates in the pair are lines. * Optional "result" populated only if both candidates are lines. When this result is populated, it will be a pair of * array of parameters of intersection * flag indicating if the two candidates share a coincident segment """ # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. if not ( first.__class__ is Linearization and second.__class__ is Linearization and first.error == 0.0 and second.error == 0.0 ): return False, None s, t, success = segment_intersection( first.start_node, first.end_node, second.start_node, second.end_node ) if success: if _helpers.in_interval(s, 0.0, 1.0) and _helpers.in_interval( t, 0.0, 1.0 ): intersections = np.asfortranarray([[s], [t]]) result = intersections, False else: result = np.empty((2, 0), order="F"), False else: disjoint, params = parallel_lines_parameters( first.start_node, first.end_node, second.start_node, second.end_node, ) if disjoint: result = np.empty((2, 0), order="F"), False else: result = params, True return True, result
[ "def", "check_lines", "(", "first", ",", "second", ")", ":", "# NOTE: In the below we replace ``isinstance(a, B)`` with", "# ``a.__class__ is B``, which is a 3-3.5x speedup.", "if", "not", "(", "first", ".", "__class__", "is", "Linearization", "and", "second", ".", "__class__", "is", "Linearization", "and", "first", ".", "error", "==", "0.0", "and", "second", ".", "error", "==", "0.0", ")", ":", "return", "False", ",", "None", "s", ",", "t", ",", "success", "=", "segment_intersection", "(", "first", ".", "start_node", ",", "first", ".", "end_node", ",", "second", ".", "start_node", ",", "second", ".", "end_node", ")", "if", "success", ":", "if", "_helpers", ".", "in_interval", "(", "s", ",", "0.0", ",", "1.0", ")", "and", "_helpers", ".", "in_interval", "(", "t", ",", "0.0", ",", "1.0", ")", ":", "intersections", "=", "np", ".", "asfortranarray", "(", "[", "[", "s", "]", ",", "[", "t", "]", "]", ")", "result", "=", "intersections", ",", "False", "else", ":", "result", "=", "np", ".", "empty", "(", "(", "2", ",", "0", ")", ",", "order", "=", "\"F\"", ")", ",", "False", "else", ":", "disjoint", ",", "params", "=", "parallel_lines_parameters", "(", "first", ".", "start_node", ",", "first", ".", "end_node", ",", "second", ".", "start_node", ",", "second", ".", "end_node", ",", ")", "if", "disjoint", ":", "result", "=", "np", ".", "empty", "(", "(", "2", ",", "0", ")", ",", "order", "=", "\"F\"", ")", ",", "False", "else", ":", "result", "=", "params", ",", "True", "return", "True", ",", "result" ]
Checks if two curves are lines and tries to intersect them. .. note:: This is a helper for :func:`._all_intersections`. If they are not lines / not linearized, immediately returns :data:`False` with no "return value". If they are lines, attempts to intersect them (even if they are parallel and share a coincident segment). Args: first (Union[SubdividedCurve, Linearization]): First curve being intersected. second (Union[SubdividedCurve, Linearization]): Second curve being intersected. Returns: Tuple[bool, Optional[Tuple[numpy.ndarray, bool]]]: A pair of * Flag indicating if both candidates in the pair are lines. * Optional "result" populated only if both candidates are lines. When this result is populated, it will be a pair of * array of parameters of intersection * flag indicating if the two candidates share a coincident segment
[ "Checks", "if", "two", "curves", "are", "lines", "and", "tries", "to", "intersect", "them", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1337-L1398
dhermes/bezier
src/bezier/_geometric_intersection.py
_all_intersections
def _all_intersections(nodes_first, nodes_second): r"""Find the points of intersection among a pair of curves. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This assumes both curves are in :math:`\mathbf{R}^2`, but does not **explicitly** check this. However, functions used here will fail if that assumption fails, e.g. :func:`bbox_intersect` and :func:`newton_refine() <._intersection_helpers._newton_refine>`. Args: nodes_first (numpy.ndarray): Control points of a curve to be intersected with ``nodes_second``. nodes_second (numpy.ndarray): Control points of a curve to be intersected with ``nodes_first``. Returns: Tuple[numpy.ndarray, bool]: An array and a flag: * A ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. * Flag indicating if the curves are coincident. Raises: ValueError: If the subdivision iteration does not terminate before exhausting the maximum number of subdivisions. NotImplementedError: If the subdivision process picks up too many candidate pairs. This typically indicates tangent curves or coincident curves (though there are mitigations for those cases in place). """ curve_first = SubdividedCurve(nodes_first, nodes_first) curve_second = SubdividedCurve(nodes_second, nodes_second) candidate1 = Linearization.from_shape(curve_first) candidate2 = Linearization.from_shape(curve_second) # Handle the line-line intersection case as a one-off. both_linear, result = check_lines(candidate1, candidate2) if both_linear: return result candidates = [(candidate1, candidate2)] intersections = [] coincident = False for _ in six.moves.xrange(_MAX_INTERSECT_SUBDIVISIONS): candidates = intersect_one_round(candidates, intersections) if len(candidates) > _MAX_CANDIDATES: candidates = prune_candidates(candidates) # If pruning didn't fix anything, we check if the curves are # coincident and "fail" if they aren't. if len(candidates) > _MAX_CANDIDATES: params = coincident_parameters(nodes_first, nodes_second) if params is None: raise NotImplementedError( _TOO_MANY_TEMPLATE.format(len(candidates)) ) else: intersections = params coincident = True # Artificially empty out candidates so that this # function exits. candidates = [] # If none of the candidate pairs have been accepted, then there are # no more intersections to find. if not candidates: if intersections: # NOTE: The transpose of a C-ordered array is Fortran-ordered, # i.e. this is on purpose. return np.array(intersections, order="C").T, coincident else: return np.empty((2, 0), order="F"), coincident msg = _NO_CONVERGE_TEMPLATE.format(_MAX_INTERSECT_SUBDIVISIONS) raise ValueError(msg)
python
def _all_intersections(nodes_first, nodes_second): r"""Find the points of intersection among a pair of curves. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This assumes both curves are in :math:`\mathbf{R}^2`, but does not **explicitly** check this. However, functions used here will fail if that assumption fails, e.g. :func:`bbox_intersect` and :func:`newton_refine() <._intersection_helpers._newton_refine>`. Args: nodes_first (numpy.ndarray): Control points of a curve to be intersected with ``nodes_second``. nodes_second (numpy.ndarray): Control points of a curve to be intersected with ``nodes_first``. Returns: Tuple[numpy.ndarray, bool]: An array and a flag: * A ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. * Flag indicating if the curves are coincident. Raises: ValueError: If the subdivision iteration does not terminate before exhausting the maximum number of subdivisions. NotImplementedError: If the subdivision process picks up too many candidate pairs. This typically indicates tangent curves or coincident curves (though there are mitigations for those cases in place). """ curve_first = SubdividedCurve(nodes_first, nodes_first) curve_second = SubdividedCurve(nodes_second, nodes_second) candidate1 = Linearization.from_shape(curve_first) candidate2 = Linearization.from_shape(curve_second) # Handle the line-line intersection case as a one-off. both_linear, result = check_lines(candidate1, candidate2) if both_linear: return result candidates = [(candidate1, candidate2)] intersections = [] coincident = False for _ in six.moves.xrange(_MAX_INTERSECT_SUBDIVISIONS): candidates = intersect_one_round(candidates, intersections) if len(candidates) > _MAX_CANDIDATES: candidates = prune_candidates(candidates) # If pruning didn't fix anything, we check if the curves are # coincident and "fail" if they aren't. if len(candidates) > _MAX_CANDIDATES: params = coincident_parameters(nodes_first, nodes_second) if params is None: raise NotImplementedError( _TOO_MANY_TEMPLATE.format(len(candidates)) ) else: intersections = params coincident = True # Artificially empty out candidates so that this # function exits. candidates = [] # If none of the candidate pairs have been accepted, then there are # no more intersections to find. if not candidates: if intersections: # NOTE: The transpose of a C-ordered array is Fortran-ordered, # i.e. this is on purpose. return np.array(intersections, order="C").T, coincident else: return np.empty((2, 0), order="F"), coincident msg = _NO_CONVERGE_TEMPLATE.format(_MAX_INTERSECT_SUBDIVISIONS) raise ValueError(msg)
[ "def", "_all_intersections", "(", "nodes_first", ",", "nodes_second", ")", ":", "curve_first", "=", "SubdividedCurve", "(", "nodes_first", ",", "nodes_first", ")", "curve_second", "=", "SubdividedCurve", "(", "nodes_second", ",", "nodes_second", ")", "candidate1", "=", "Linearization", ".", "from_shape", "(", "curve_first", ")", "candidate2", "=", "Linearization", ".", "from_shape", "(", "curve_second", ")", "# Handle the line-line intersection case as a one-off.", "both_linear", ",", "result", "=", "check_lines", "(", "candidate1", ",", "candidate2", ")", "if", "both_linear", ":", "return", "result", "candidates", "=", "[", "(", "candidate1", ",", "candidate2", ")", "]", "intersections", "=", "[", "]", "coincident", "=", "False", "for", "_", "in", "six", ".", "moves", ".", "xrange", "(", "_MAX_INTERSECT_SUBDIVISIONS", ")", ":", "candidates", "=", "intersect_one_round", "(", "candidates", ",", "intersections", ")", "if", "len", "(", "candidates", ")", ">", "_MAX_CANDIDATES", ":", "candidates", "=", "prune_candidates", "(", "candidates", ")", "# If pruning didn't fix anything, we check if the curves are", "# coincident and \"fail\" if they aren't.", "if", "len", "(", "candidates", ")", ">", "_MAX_CANDIDATES", ":", "params", "=", "coincident_parameters", "(", "nodes_first", ",", "nodes_second", ")", "if", "params", "is", "None", ":", "raise", "NotImplementedError", "(", "_TOO_MANY_TEMPLATE", ".", "format", "(", "len", "(", "candidates", ")", ")", ")", "else", ":", "intersections", "=", "params", "coincident", "=", "True", "# Artificially empty out candidates so that this", "# function exits.", "candidates", "=", "[", "]", "# If none of the candidate pairs have been accepted, then there are", "# no more intersections to find.", "if", "not", "candidates", ":", "if", "intersections", ":", "# NOTE: The transpose of a C-ordered array is Fortran-ordered,", "# i.e. this is on purpose.", "return", "np", ".", "array", "(", "intersections", ",", "order", "=", "\"C\"", ")", ".", "T", ",", "coincident", "else", ":", "return", "np", ".", "empty", "(", "(", "2", ",", "0", ")", ",", "order", "=", "\"F\"", ")", ",", "coincident", "msg", "=", "_NO_CONVERGE_TEMPLATE", ".", "format", "(", "_MAX_INTERSECT_SUBDIVISIONS", ")", "raise", "ValueError", "(", "msg", ")" ]
r"""Find the points of intersection among a pair of curves. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. .. note:: This assumes both curves are in :math:`\mathbf{R}^2`, but does not **explicitly** check this. However, functions used here will fail if that assumption fails, e.g. :func:`bbox_intersect` and :func:`newton_refine() <._intersection_helpers._newton_refine>`. Args: nodes_first (numpy.ndarray): Control points of a curve to be intersected with ``nodes_second``. nodes_second (numpy.ndarray): Control points of a curve to be intersected with ``nodes_first``. Returns: Tuple[numpy.ndarray, bool]: An array and a flag: * A ``2 x N`` array of intersection parameters. Each row contains a pair of values :math:`s` and :math:`t` (each in :math:`\left[0, 1\right]`) such that the curves intersect: :math:`B_1(s) = B_2(t)`. * Flag indicating if the curves are coincident. Raises: ValueError: If the subdivision iteration does not terminate before exhausting the maximum number of subdivisions. NotImplementedError: If the subdivision process picks up too many candidate pairs. This typically indicates tangent curves or coincident curves (though there are mitigations for those cases in place).
[ "r", "Find", "the", "points", "of", "intersection", "among", "a", "pair", "of", "curves", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1401-L1482
dhermes/bezier
src/bezier/_geometric_intersection.py
SubdividedCurve.subdivide
def subdivide(self): """Split the curve into a left and right half. See :meth:`.Curve.subdivide` for more information. Returns: Tuple[SubdividedCurve, SubdividedCurve]: The left and right sub-curves. """ left_nodes, right_nodes = _curve_helpers.subdivide_nodes(self.nodes) midpoint = 0.5 * (self.start + self.end) left = SubdividedCurve( left_nodes, self.original_nodes, start=self.start, end=midpoint ) right = SubdividedCurve( right_nodes, self.original_nodes, start=midpoint, end=self.end ) return left, right
python
def subdivide(self): """Split the curve into a left and right half. See :meth:`.Curve.subdivide` for more information. Returns: Tuple[SubdividedCurve, SubdividedCurve]: The left and right sub-curves. """ left_nodes, right_nodes = _curve_helpers.subdivide_nodes(self.nodes) midpoint = 0.5 * (self.start + self.end) left = SubdividedCurve( left_nodes, self.original_nodes, start=self.start, end=midpoint ) right = SubdividedCurve( right_nodes, self.original_nodes, start=midpoint, end=self.end ) return left, right
[ "def", "subdivide", "(", "self", ")", ":", "left_nodes", ",", "right_nodes", "=", "_curve_helpers", ".", "subdivide_nodes", "(", "self", ".", "nodes", ")", "midpoint", "=", "0.5", "*", "(", "self", ".", "start", "+", "self", ".", "end", ")", "left", "=", "SubdividedCurve", "(", "left_nodes", ",", "self", ".", "original_nodes", ",", "start", "=", "self", ".", "start", ",", "end", "=", "midpoint", ")", "right", "=", "SubdividedCurve", "(", "right_nodes", ",", "self", ".", "original_nodes", ",", "start", "=", "midpoint", ",", "end", "=", "self", ".", "end", ")", "return", "left", ",", "right" ]
Split the curve into a left and right half. See :meth:`.Curve.subdivide` for more information. Returns: Tuple[SubdividedCurve, SubdividedCurve]: The left and right sub-curves.
[ "Split", "the", "curve", "into", "a", "left", "and", "right", "half", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1543-L1560
dhermes/bezier
src/bezier/_geometric_intersection.py
Linearization.from_shape
def from_shape(cls, shape): """Try to linearize a curve (or an already linearized curve). Args: shape (Union[SubdividedCurve, \ ~bezier._geometric_intersection.Linearization]): A curve or an already linearized curve. Returns: Union[SubdividedCurve, \ ~bezier._geometric_intersection.Linearization]: The (potentially linearized) curve. """ # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. if shape.__class__ is cls: return shape else: error = linearization_error(shape.nodes) if error < _ERROR_VAL: linearized = cls(shape, error) return linearized else: return shape
python
def from_shape(cls, shape): """Try to linearize a curve (or an already linearized curve). Args: shape (Union[SubdividedCurve, \ ~bezier._geometric_intersection.Linearization]): A curve or an already linearized curve. Returns: Union[SubdividedCurve, \ ~bezier._geometric_intersection.Linearization]: The (potentially linearized) curve. """ # NOTE: In the below we replace ``isinstance(a, B)`` with # ``a.__class__ is B``, which is a 3-3.5x speedup. if shape.__class__ is cls: return shape else: error = linearization_error(shape.nodes) if error < _ERROR_VAL: linearized = cls(shape, error) return linearized else: return shape
[ "def", "from_shape", "(", "cls", ",", "shape", ")", ":", "# NOTE: In the below we replace ``isinstance(a, B)`` with", "# ``a.__class__ is B``, which is a 3-3.5x speedup.", "if", "shape", ".", "__class__", "is", "cls", ":", "return", "shape", "else", ":", "error", "=", "linearization_error", "(", "shape", ".", "nodes", ")", "if", "error", "<", "_ERROR_VAL", ":", "linearized", "=", "cls", "(", "shape", ",", "error", ")", "return", "linearized", "else", ":", "return", "shape" ]
Try to linearize a curve (or an already linearized curve). Args: shape (Union[SubdividedCurve, \ ~bezier._geometric_intersection.Linearization]): A curve or an already linearized curve. Returns: Union[SubdividedCurve, \ ~bezier._geometric_intersection.Linearization]: The (potentially linearized) curve.
[ "Try", "to", "linearize", "a", "curve", "(", "or", "an", "already", "linearized", "curve", ")", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_geometric_intersection.py#L1615-L1640
dhermes/bezier
src/bezier/curve.py
Curve.evaluate
def evaluate(self, s): r"""Evaluate :math:`B(s)` along the curve. This method acts as a (partial) inverse to :meth:`locate`. See :meth:`evaluate_multi` for more details. .. image:: ../../images/curve_evaluate.png :align: center .. doctest:: curve-eval :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 0.625, 1.0], ... [0.0, 0.5 , 0.5], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> curve.evaluate(0.75) array([[0.796875], [0.46875 ]]) .. testcleanup:: curve-eval import make_images make_images.curve_evaluate(curve) Args: s (float): Parameter along the curve. Returns: numpy.ndarray: The point on the curve (as a two dimensional NumPy array with a single column). """ return _curve_helpers.evaluate_multi( self._nodes, np.asfortranarray([s]) )
python
def evaluate(self, s): r"""Evaluate :math:`B(s)` along the curve. This method acts as a (partial) inverse to :meth:`locate`. See :meth:`evaluate_multi` for more details. .. image:: ../../images/curve_evaluate.png :align: center .. doctest:: curve-eval :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 0.625, 1.0], ... [0.0, 0.5 , 0.5], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> curve.evaluate(0.75) array([[0.796875], [0.46875 ]]) .. testcleanup:: curve-eval import make_images make_images.curve_evaluate(curve) Args: s (float): Parameter along the curve. Returns: numpy.ndarray: The point on the curve (as a two dimensional NumPy array with a single column). """ return _curve_helpers.evaluate_multi( self._nodes, np.asfortranarray([s]) )
[ "def", "evaluate", "(", "self", ",", "s", ")", ":", "return", "_curve_helpers", ".", "evaluate_multi", "(", "self", ".", "_nodes", ",", "np", ".", "asfortranarray", "(", "[", "s", "]", ")", ")" ]
r"""Evaluate :math:`B(s)` along the curve. This method acts as a (partial) inverse to :meth:`locate`. See :meth:`evaluate_multi` for more details. .. image:: ../../images/curve_evaluate.png :align: center .. doctest:: curve-eval :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 0.625, 1.0], ... [0.0, 0.5 , 0.5], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> curve.evaluate(0.75) array([[0.796875], [0.46875 ]]) .. testcleanup:: curve-eval import make_images make_images.curve_evaluate(curve) Args: s (float): Parameter along the curve. Returns: numpy.ndarray: The point on the curve (as a two dimensional NumPy array with a single column).
[ "r", "Evaluate", ":", "math", ":", "B", "(", "s", ")", "along", "the", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L173-L209
dhermes/bezier
src/bezier/curve.py
Curve.plot
def plot(self, num_pts, color=None, alpha=None, ax=None): """Plot the current curve. Args: num_pts (int): Number of points to plot. color (Optional[Tuple[float, float, float]]): Color as RGB profile. alpha (Optional[float]): The alpha channel for the color. ax (Optional[matplotlib.artist.Artist]): matplotlib axis object to add plot to. Returns: matplotlib.artist.Artist: The axis containing the plot. This may be a newly created axis. Raises: NotImplementedError: If the curve's dimension is not ``2``. """ if self._dimension != 2: raise NotImplementedError( "2D is the only supported dimension", "Current dimension", self._dimension, ) s_vals = np.linspace(0.0, 1.0, num_pts) points = self.evaluate_multi(s_vals) if ax is None: ax = _plot_helpers.new_axis() ax.plot(points[0, :], points[1, :], color=color, alpha=alpha) return ax
python
def plot(self, num_pts, color=None, alpha=None, ax=None): """Plot the current curve. Args: num_pts (int): Number of points to plot. color (Optional[Tuple[float, float, float]]): Color as RGB profile. alpha (Optional[float]): The alpha channel for the color. ax (Optional[matplotlib.artist.Artist]): matplotlib axis object to add plot to. Returns: matplotlib.artist.Artist: The axis containing the plot. This may be a newly created axis. Raises: NotImplementedError: If the curve's dimension is not ``2``. """ if self._dimension != 2: raise NotImplementedError( "2D is the only supported dimension", "Current dimension", self._dimension, ) s_vals = np.linspace(0.0, 1.0, num_pts) points = self.evaluate_multi(s_vals) if ax is None: ax = _plot_helpers.new_axis() ax.plot(points[0, :], points[1, :], color=color, alpha=alpha) return ax
[ "def", "plot", "(", "self", ",", "num_pts", ",", "color", "=", "None", ",", "alpha", "=", "None", ",", "ax", "=", "None", ")", ":", "if", "self", ".", "_dimension", "!=", "2", ":", "raise", "NotImplementedError", "(", "\"2D is the only supported dimension\"", ",", "\"Current dimension\"", ",", "self", ".", "_dimension", ",", ")", "s_vals", "=", "np", ".", "linspace", "(", "0.0", ",", "1.0", ",", "num_pts", ")", "points", "=", "self", ".", "evaluate_multi", "(", "s_vals", ")", "if", "ax", "is", "None", ":", "ax", "=", "_plot_helpers", ".", "new_axis", "(", ")", "ax", ".", "plot", "(", "points", "[", "0", ",", ":", "]", ",", "points", "[", "1", ",", ":", "]", ",", "color", "=", "color", ",", "alpha", "=", "alpha", ")", "return", "ax" ]
Plot the current curve. Args: num_pts (int): Number of points to plot. color (Optional[Tuple[float, float, float]]): Color as RGB profile. alpha (Optional[float]): The alpha channel for the color. ax (Optional[matplotlib.artist.Artist]): matplotlib axis object to add plot to. Returns: matplotlib.artist.Artist: The axis containing the plot. This may be a newly created axis. Raises: NotImplementedError: If the curve's dimension is not ``2``.
[ "Plot", "the", "current", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L245-L274
dhermes/bezier
src/bezier/curve.py
Curve.subdivide
def subdivide(self): r"""Split the curve :math:`B(s)` into a left and right half. Takes the interval :math:`\left[0, 1\right]` and splits the curve into :math:`B_1 = B\left(\left[0, \frac{1}{2}\right]\right)` and :math:`B_2 = B\left(\left[\frac{1}{2}, 1\right]\right)`. In order to do this, also reparameterizes the curve, hence the resulting left and right halves have new nodes. .. image:: ../../images/curve_subdivide.png :align: center .. doctest:: curve-subdivide :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.25, 2.0], ... [0.0, 3.0 , 1.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> left, right = curve.subdivide() >>> left.nodes array([[0. , 0.625, 1.125], [0. , 1.5 , 1.75 ]]) >>> right.nodes array([[1.125, 1.625, 2. ], [1.75 , 2. , 1. ]]) .. testcleanup:: curve-subdivide import make_images make_images.curve_subdivide(curve, left, right) Returns: Tuple[Curve, Curve]: The left and right sub-curves. """ left_nodes, right_nodes = _curve_helpers.subdivide_nodes(self._nodes) left = Curve(left_nodes, self._degree, _copy=False) right = Curve(right_nodes, self._degree, _copy=False) return left, right
python
def subdivide(self): r"""Split the curve :math:`B(s)` into a left and right half. Takes the interval :math:`\left[0, 1\right]` and splits the curve into :math:`B_1 = B\left(\left[0, \frac{1}{2}\right]\right)` and :math:`B_2 = B\left(\left[\frac{1}{2}, 1\right]\right)`. In order to do this, also reparameterizes the curve, hence the resulting left and right halves have new nodes. .. image:: ../../images/curve_subdivide.png :align: center .. doctest:: curve-subdivide :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.25, 2.0], ... [0.0, 3.0 , 1.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> left, right = curve.subdivide() >>> left.nodes array([[0. , 0.625, 1.125], [0. , 1.5 , 1.75 ]]) >>> right.nodes array([[1.125, 1.625, 2. ], [1.75 , 2. , 1. ]]) .. testcleanup:: curve-subdivide import make_images make_images.curve_subdivide(curve, left, right) Returns: Tuple[Curve, Curve]: The left and right sub-curves. """ left_nodes, right_nodes = _curve_helpers.subdivide_nodes(self._nodes) left = Curve(left_nodes, self._degree, _copy=False) right = Curve(right_nodes, self._degree, _copy=False) return left, right
[ "def", "subdivide", "(", "self", ")", ":", "left_nodes", ",", "right_nodes", "=", "_curve_helpers", ".", "subdivide_nodes", "(", "self", ".", "_nodes", ")", "left", "=", "Curve", "(", "left_nodes", ",", "self", ".", "_degree", ",", "_copy", "=", "False", ")", "right", "=", "Curve", "(", "right_nodes", ",", "self", ".", "_degree", ",", "_copy", "=", "False", ")", "return", "left", ",", "right" ]
r"""Split the curve :math:`B(s)` into a left and right half. Takes the interval :math:`\left[0, 1\right]` and splits the curve into :math:`B_1 = B\left(\left[0, \frac{1}{2}\right]\right)` and :math:`B_2 = B\left(\left[\frac{1}{2}, 1\right]\right)`. In order to do this, also reparameterizes the curve, hence the resulting left and right halves have new nodes. .. image:: ../../images/curve_subdivide.png :align: center .. doctest:: curve-subdivide :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.25, 2.0], ... [0.0, 3.0 , 1.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> left, right = curve.subdivide() >>> left.nodes array([[0. , 0.625, 1.125], [0. , 1.5 , 1.75 ]]) >>> right.nodes array([[1.125, 1.625, 2. ], [1.75 , 2. , 1. ]]) .. testcleanup:: curve-subdivide import make_images make_images.curve_subdivide(curve, left, right) Returns: Tuple[Curve, Curve]: The left and right sub-curves.
[ "r", "Split", "the", "curve", ":", "math", ":", "B", "(", "s", ")", "into", "a", "left", "and", "right", "half", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L276-L315
dhermes/bezier
src/bezier/curve.py
Curve.intersect
def intersect( self, other, strategy=IntersectionStrategy.GEOMETRIC, _verify=True ): """Find the points of intersection with another curve. See :doc:`../../algorithms/curve-curve-intersection` for more details. .. image:: ../../images/curve_intersect.png :align: center .. doctest:: curve-intersect :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.0, 0.375, 0.75 ], ... [0.0, 0.75 , 0.375], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.5, 0.5 ], ... [0.0, 0.75], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=1) >>> intersections = curve1.intersect(curve2) >>> 3.0 * intersections array([[2.], [2.]]) >>> s_vals = intersections[0, :] >>> curve1.evaluate_multi(s_vals) array([[0.5], [0.5]]) .. testcleanup:: curve-intersect import make_images make_images.curve_intersect(curve1, curve2, s_vals) Args: other (Curve): Other curve to intersect with. strategy (Optional[~bezier.curve.IntersectionStrategy]): The intersection algorithm to use. Defaults to geometric. _verify (Optional[bool]): Indicates if extra caution should be used to verify assumptions about the input and current curve. Can be disabled to speed up execution time. Defaults to :data:`True`. Returns: numpy.ndarray: ``2 x N`` array of ``s``- and ``t``-parameters where intersections occur (possibly empty). Raises: TypeError: If ``other`` is not a curve (and ``_verify=True``). NotImplementedError: If at least one of the curves isn't two-dimensional (and ``_verify=True``). ValueError: If ``strategy`` is not a valid :class:`.IntersectionStrategy`. """ if _verify: if not isinstance(other, Curve): raise TypeError( "Can only intersect with another curve", "Received", other ) if self._dimension != 2 or other._dimension != 2: raise NotImplementedError( "Intersection only implemented in 2D" ) if strategy == IntersectionStrategy.GEOMETRIC: all_intersections = _geometric_intersection.all_intersections elif strategy == IntersectionStrategy.ALGEBRAIC: all_intersections = _algebraic_intersection.all_intersections else: raise ValueError("Unexpected strategy.", strategy) st_vals, _ = all_intersections(self._nodes, other._nodes) return st_vals
python
def intersect( self, other, strategy=IntersectionStrategy.GEOMETRIC, _verify=True ): """Find the points of intersection with another curve. See :doc:`../../algorithms/curve-curve-intersection` for more details. .. image:: ../../images/curve_intersect.png :align: center .. doctest:: curve-intersect :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.0, 0.375, 0.75 ], ... [0.0, 0.75 , 0.375], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.5, 0.5 ], ... [0.0, 0.75], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=1) >>> intersections = curve1.intersect(curve2) >>> 3.0 * intersections array([[2.], [2.]]) >>> s_vals = intersections[0, :] >>> curve1.evaluate_multi(s_vals) array([[0.5], [0.5]]) .. testcleanup:: curve-intersect import make_images make_images.curve_intersect(curve1, curve2, s_vals) Args: other (Curve): Other curve to intersect with. strategy (Optional[~bezier.curve.IntersectionStrategy]): The intersection algorithm to use. Defaults to geometric. _verify (Optional[bool]): Indicates if extra caution should be used to verify assumptions about the input and current curve. Can be disabled to speed up execution time. Defaults to :data:`True`. Returns: numpy.ndarray: ``2 x N`` array of ``s``- and ``t``-parameters where intersections occur (possibly empty). Raises: TypeError: If ``other`` is not a curve (and ``_verify=True``). NotImplementedError: If at least one of the curves isn't two-dimensional (and ``_verify=True``). ValueError: If ``strategy`` is not a valid :class:`.IntersectionStrategy`. """ if _verify: if not isinstance(other, Curve): raise TypeError( "Can only intersect with another curve", "Received", other ) if self._dimension != 2 or other._dimension != 2: raise NotImplementedError( "Intersection only implemented in 2D" ) if strategy == IntersectionStrategy.GEOMETRIC: all_intersections = _geometric_intersection.all_intersections elif strategy == IntersectionStrategy.ALGEBRAIC: all_intersections = _algebraic_intersection.all_intersections else: raise ValueError("Unexpected strategy.", strategy) st_vals, _ = all_intersections(self._nodes, other._nodes) return st_vals
[ "def", "intersect", "(", "self", ",", "other", ",", "strategy", "=", "IntersectionStrategy", ".", "GEOMETRIC", ",", "_verify", "=", "True", ")", ":", "if", "_verify", ":", "if", "not", "isinstance", "(", "other", ",", "Curve", ")", ":", "raise", "TypeError", "(", "\"Can only intersect with another curve\"", ",", "\"Received\"", ",", "other", ")", "if", "self", ".", "_dimension", "!=", "2", "or", "other", ".", "_dimension", "!=", "2", ":", "raise", "NotImplementedError", "(", "\"Intersection only implemented in 2D\"", ")", "if", "strategy", "==", "IntersectionStrategy", ".", "GEOMETRIC", ":", "all_intersections", "=", "_geometric_intersection", ".", "all_intersections", "elif", "strategy", "==", "IntersectionStrategy", ".", "ALGEBRAIC", ":", "all_intersections", "=", "_algebraic_intersection", ".", "all_intersections", "else", ":", "raise", "ValueError", "(", "\"Unexpected strategy.\"", ",", "strategy", ")", "st_vals", ",", "_", "=", "all_intersections", "(", "self", ".", "_nodes", ",", "other", ".", "_nodes", ")", "return", "st_vals" ]
Find the points of intersection with another curve. See :doc:`../../algorithms/curve-curve-intersection` for more details. .. image:: ../../images/curve_intersect.png :align: center .. doctest:: curve-intersect :options: +NORMALIZE_WHITESPACE >>> nodes1 = np.asfortranarray([ ... [0.0, 0.375, 0.75 ], ... [0.0, 0.75 , 0.375], ... ]) >>> curve1 = bezier.Curve(nodes1, degree=2) >>> nodes2 = np.asfortranarray([ ... [0.5, 0.5 ], ... [0.0, 0.75], ... ]) >>> curve2 = bezier.Curve(nodes2, degree=1) >>> intersections = curve1.intersect(curve2) >>> 3.0 * intersections array([[2.], [2.]]) >>> s_vals = intersections[0, :] >>> curve1.evaluate_multi(s_vals) array([[0.5], [0.5]]) .. testcleanup:: curve-intersect import make_images make_images.curve_intersect(curve1, curve2, s_vals) Args: other (Curve): Other curve to intersect with. strategy (Optional[~bezier.curve.IntersectionStrategy]): The intersection algorithm to use. Defaults to geometric. _verify (Optional[bool]): Indicates if extra caution should be used to verify assumptions about the input and current curve. Can be disabled to speed up execution time. Defaults to :data:`True`. Returns: numpy.ndarray: ``2 x N`` array of ``s``- and ``t``-parameters where intersections occur (possibly empty). Raises: TypeError: If ``other`` is not a curve (and ``_verify=True``). NotImplementedError: If at least one of the curves isn't two-dimensional (and ``_verify=True``). ValueError: If ``strategy`` is not a valid :class:`.IntersectionStrategy`.
[ "Find", "the", "points", "of", "intersection", "with", "another", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L317-L393
dhermes/bezier
src/bezier/curve.py
Curve.elevate
def elevate(self): r"""Return a degree-elevated version of the current curve. Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n + 1}` where .. math:: \begin{align*} w_0 &= v_0 \\ w_j &= \frac{j}{n + 1} v_{j - 1} + \frac{n + 1 - j}{n + 1} v_j \\ w_{n + 1} &= v_n \end{align*} .. image:: ../../images/curve_elevate.png :align: center .. testsetup:: curve-elevate import numpy as np import bezier .. doctest:: curve-elevate :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.5, 0.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> elevated = curve.elevate() >>> elevated <Curve (degree=3, dimension=2)> >>> elevated.nodes array([[0., 1., 2., 3.], [0., 1., 1., 0.]]) .. testcleanup:: curve-elevate import make_images make_images.curve_elevate(curve, elevated) Returns: Curve: The degree-elevated curve. """ new_nodes = _curve_helpers.elevate_nodes(self._nodes) return Curve(new_nodes, self._degree + 1, _copy=False)
python
def elevate(self): r"""Return a degree-elevated version of the current curve. Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n + 1}` where .. math:: \begin{align*} w_0 &= v_0 \\ w_j &= \frac{j}{n + 1} v_{j - 1} + \frac{n + 1 - j}{n + 1} v_j \\ w_{n + 1} &= v_n \end{align*} .. image:: ../../images/curve_elevate.png :align: center .. testsetup:: curve-elevate import numpy as np import bezier .. doctest:: curve-elevate :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.5, 0.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> elevated = curve.elevate() >>> elevated <Curve (degree=3, dimension=2)> >>> elevated.nodes array([[0., 1., 2., 3.], [0., 1., 1., 0.]]) .. testcleanup:: curve-elevate import make_images make_images.curve_elevate(curve, elevated) Returns: Curve: The degree-elevated curve. """ new_nodes = _curve_helpers.elevate_nodes(self._nodes) return Curve(new_nodes, self._degree + 1, _copy=False)
[ "def", "elevate", "(", "self", ")", ":", "new_nodes", "=", "_curve_helpers", ".", "elevate_nodes", "(", "self", ".", "_nodes", ")", "return", "Curve", "(", "new_nodes", ",", "self", ".", "_degree", "+", "1", ",", "_copy", "=", "False", ")" ]
r"""Return a degree-elevated version of the current curve. Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n + 1}` where .. math:: \begin{align*} w_0 &= v_0 \\ w_j &= \frac{j}{n + 1} v_{j - 1} + \frac{n + 1 - j}{n + 1} v_j \\ w_{n + 1} &= v_n \end{align*} .. image:: ../../images/curve_elevate.png :align: center .. testsetup:: curve-elevate import numpy as np import bezier .. doctest:: curve-elevate :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.5, 3.0], ... [0.0, 1.5, 0.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> elevated = curve.elevate() >>> elevated <Curve (degree=3, dimension=2)> >>> elevated.nodes array([[0., 1., 2., 3.], [0., 1., 1., 0.]]) .. testcleanup:: curve-elevate import make_images make_images.curve_elevate(curve, elevated) Returns: Curve: The degree-elevated curve.
[ "r", "Return", "a", "degree", "-", "elevated", "version", "of", "the", "current", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L395-L441
dhermes/bezier
src/bezier/curve.py
Curve.reduce_
def reduce_(self): r"""Return a degree-reduced version of the current curve. .. _pseudo-inverse: https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n - 1}` that correspond to reversing the :meth:`elevate` process. This uses the `pseudo-inverse`_ of the elevation matrix. For example when elevating from degree 2 to 3, the matrix :math:`E_2` is given by .. math:: \mathbf{v} = \left[\begin{array}{c c c} v_0 & v_1 & v_2 \end{array}\right] \longmapsto \left[\begin{array}{c c c c} v_0 & \frac{v_0 + 2 v_1}{3} & \frac{2 v_1 + v_2}{3} & v_2 \end{array}\right] = \frac{1}{3} \mathbf{v} \left[\begin{array}{c c c c} 3 & 1 & 0 & 0 \\ 0 & 2 & 2 & 0 \\ 0 & 0 & 1 & 3 \end{array}\right] and the (right) pseudo-inverse is given by .. math:: R_2 = E_2^T \left(E_2 E_2^T\right)^{-1} = \frac{1}{20} \left[\begin{array}{c c c} 19 & -5 & 1 \\ 3 & 15 & -3 \\ -3 & 15 & 3 \\ 1 & -5 & 19 \end{array}\right]. .. warning:: Though degree-elevation preserves the start and end nodes, degree reduction has no such guarantee. Rather, the nodes produced are "best" in the least squares sense (when solving the normal equations). .. image:: ../../images/curve_reduce.png :align: center .. testsetup:: curve-reduce, curve-reduce-approx import numpy as np import bezier .. doctest:: curve-reduce :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [-3.0, 0.0, 1.0, 0.0], ... [ 3.0, 2.0, 3.0, 6.0], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> reduced = curve.reduce_() >>> reduced <Curve (degree=2, dimension=2)> >>> reduced.nodes array([[-3. , 1.5, 0. ], [ 3. , 1.5, 6. ]]) .. testcleanup:: curve-reduce import make_images make_images.curve_reduce(curve, reduced) In the case that the current curve **is not** degree-elevated. .. image:: ../../images/curve_reduce_approx.png :align: center .. doctest:: curve-reduce-approx :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.25, 3.75, 5.0], ... [2.5, 5.0 , 7.5 , 2.5], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> reduced = curve.reduce_() >>> reduced <Curve (degree=2, dimension=2)> >>> reduced.nodes array([[-0.125, 2.5 , 5.125], [ 2.125, 8.125, 2.875]]) .. testcleanup:: curve-reduce-approx import make_images make_images.curve_reduce_approx(curve, reduced) Returns: Curve: The degree-reduced curve. """ new_nodes = _curve_helpers.reduce_pseudo_inverse(self._nodes) return Curve(new_nodes, self._degree - 1, _copy=False)
python
def reduce_(self): r"""Return a degree-reduced version of the current curve. .. _pseudo-inverse: https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n - 1}` that correspond to reversing the :meth:`elevate` process. This uses the `pseudo-inverse`_ of the elevation matrix. For example when elevating from degree 2 to 3, the matrix :math:`E_2` is given by .. math:: \mathbf{v} = \left[\begin{array}{c c c} v_0 & v_1 & v_2 \end{array}\right] \longmapsto \left[\begin{array}{c c c c} v_0 & \frac{v_0 + 2 v_1}{3} & \frac{2 v_1 + v_2}{3} & v_2 \end{array}\right] = \frac{1}{3} \mathbf{v} \left[\begin{array}{c c c c} 3 & 1 & 0 & 0 \\ 0 & 2 & 2 & 0 \\ 0 & 0 & 1 & 3 \end{array}\right] and the (right) pseudo-inverse is given by .. math:: R_2 = E_2^T \left(E_2 E_2^T\right)^{-1} = \frac{1}{20} \left[\begin{array}{c c c} 19 & -5 & 1 \\ 3 & 15 & -3 \\ -3 & 15 & 3 \\ 1 & -5 & 19 \end{array}\right]. .. warning:: Though degree-elevation preserves the start and end nodes, degree reduction has no such guarantee. Rather, the nodes produced are "best" in the least squares sense (when solving the normal equations). .. image:: ../../images/curve_reduce.png :align: center .. testsetup:: curve-reduce, curve-reduce-approx import numpy as np import bezier .. doctest:: curve-reduce :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [-3.0, 0.0, 1.0, 0.0], ... [ 3.0, 2.0, 3.0, 6.0], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> reduced = curve.reduce_() >>> reduced <Curve (degree=2, dimension=2)> >>> reduced.nodes array([[-3. , 1.5, 0. ], [ 3. , 1.5, 6. ]]) .. testcleanup:: curve-reduce import make_images make_images.curve_reduce(curve, reduced) In the case that the current curve **is not** degree-elevated. .. image:: ../../images/curve_reduce_approx.png :align: center .. doctest:: curve-reduce-approx :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.25, 3.75, 5.0], ... [2.5, 5.0 , 7.5 , 2.5], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> reduced = curve.reduce_() >>> reduced <Curve (degree=2, dimension=2)> >>> reduced.nodes array([[-0.125, 2.5 , 5.125], [ 2.125, 8.125, 2.875]]) .. testcleanup:: curve-reduce-approx import make_images make_images.curve_reduce_approx(curve, reduced) Returns: Curve: The degree-reduced curve. """ new_nodes = _curve_helpers.reduce_pseudo_inverse(self._nodes) return Curve(new_nodes, self._degree - 1, _copy=False)
[ "def", "reduce_", "(", "self", ")", ":", "new_nodes", "=", "_curve_helpers", ".", "reduce_pseudo_inverse", "(", "self", ".", "_nodes", ")", "return", "Curve", "(", "new_nodes", ",", "self", ".", "_degree", "-", "1", ",", "_copy", "=", "False", ")" ]
r"""Return a degree-reduced version of the current curve. .. _pseudo-inverse: https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n - 1}` that correspond to reversing the :meth:`elevate` process. This uses the `pseudo-inverse`_ of the elevation matrix. For example when elevating from degree 2 to 3, the matrix :math:`E_2` is given by .. math:: \mathbf{v} = \left[\begin{array}{c c c} v_0 & v_1 & v_2 \end{array}\right] \longmapsto \left[\begin{array}{c c c c} v_0 & \frac{v_0 + 2 v_1}{3} & \frac{2 v_1 + v_2}{3} & v_2 \end{array}\right] = \frac{1}{3} \mathbf{v} \left[\begin{array}{c c c c} 3 & 1 & 0 & 0 \\ 0 & 2 & 2 & 0 \\ 0 & 0 & 1 & 3 \end{array}\right] and the (right) pseudo-inverse is given by .. math:: R_2 = E_2^T \left(E_2 E_2^T\right)^{-1} = \frac{1}{20} \left[\begin{array}{c c c} 19 & -5 & 1 \\ 3 & 15 & -3 \\ -3 & 15 & 3 \\ 1 & -5 & 19 \end{array}\right]. .. warning:: Though degree-elevation preserves the start and end nodes, degree reduction has no such guarantee. Rather, the nodes produced are "best" in the least squares sense (when solving the normal equations). .. image:: ../../images/curve_reduce.png :align: center .. testsetup:: curve-reduce, curve-reduce-approx import numpy as np import bezier .. doctest:: curve-reduce :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [-3.0, 0.0, 1.0, 0.0], ... [ 3.0, 2.0, 3.0, 6.0], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> reduced = curve.reduce_() >>> reduced <Curve (degree=2, dimension=2)> >>> reduced.nodes array([[-3. , 1.5, 0. ], [ 3. , 1.5, 6. ]]) .. testcleanup:: curve-reduce import make_images make_images.curve_reduce(curve, reduced) In the case that the current curve **is not** degree-elevated. .. image:: ../../images/curve_reduce_approx.png :align: center .. doctest:: curve-reduce-approx :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.25, 3.75, 5.0], ... [2.5, 5.0 , 7.5 , 2.5], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> reduced = curve.reduce_() >>> reduced <Curve (degree=2, dimension=2)> >>> reduced.nodes array([[-0.125, 2.5 , 5.125], [ 2.125, 8.125, 2.875]]) .. testcleanup:: curve-reduce-approx import make_images make_images.curve_reduce_approx(curve, reduced) Returns: Curve: The degree-reduced curve.
[ "r", "Return", "a", "degree", "-", "reduced", "version", "of", "the", "current", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L443-L538
dhermes/bezier
src/bezier/curve.py
Curve.specialize
def specialize(self, start, end): """Specialize the curve to a given sub-interval. .. image:: ../../images/curve_specialize.png :align: center .. doctest:: curve-specialize >>> nodes = np.asfortranarray([ ... [0.0, 0.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> new_curve = curve.specialize(-0.25, 0.75) >>> new_curve.nodes array([[-0.25 , 0.25 , 0.75 ], [-0.625, 0.875, 0.375]]) .. testcleanup:: curve-specialize import make_images make_images.curve_specialize(curve, new_curve) This is generalized version of :meth:`subdivide`, and can even match the output of that method: .. testsetup:: curve-specialize2 import numpy as np import bezier nodes = np.asfortranarray([ [0.0, 0.5, 1.0], [0.0, 1.0, 0.0], ]) curve = bezier.Curve(nodes, degree=2) .. doctest:: curve-specialize2 >>> left, right = curve.subdivide() >>> also_left = curve.specialize(0.0, 0.5) >>> np.all(also_left.nodes == left.nodes) True >>> also_right = curve.specialize(0.5, 1.0) >>> np.all(also_right.nodes == right.nodes) True Args: start (float): The start point of the interval we are specializing to. end (float): The end point of the interval we are specializing to. Returns: Curve: The newly-specialized curve. """ new_nodes = _curve_helpers.specialize_curve(self._nodes, start, end) return Curve(new_nodes, self._degree, _copy=False)
python
def specialize(self, start, end): """Specialize the curve to a given sub-interval. .. image:: ../../images/curve_specialize.png :align: center .. doctest:: curve-specialize >>> nodes = np.asfortranarray([ ... [0.0, 0.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> new_curve = curve.specialize(-0.25, 0.75) >>> new_curve.nodes array([[-0.25 , 0.25 , 0.75 ], [-0.625, 0.875, 0.375]]) .. testcleanup:: curve-specialize import make_images make_images.curve_specialize(curve, new_curve) This is generalized version of :meth:`subdivide`, and can even match the output of that method: .. testsetup:: curve-specialize2 import numpy as np import bezier nodes = np.asfortranarray([ [0.0, 0.5, 1.0], [0.0, 1.0, 0.0], ]) curve = bezier.Curve(nodes, degree=2) .. doctest:: curve-specialize2 >>> left, right = curve.subdivide() >>> also_left = curve.specialize(0.0, 0.5) >>> np.all(also_left.nodes == left.nodes) True >>> also_right = curve.specialize(0.5, 1.0) >>> np.all(also_right.nodes == right.nodes) True Args: start (float): The start point of the interval we are specializing to. end (float): The end point of the interval we are specializing to. Returns: Curve: The newly-specialized curve. """ new_nodes = _curve_helpers.specialize_curve(self._nodes, start, end) return Curve(new_nodes, self._degree, _copy=False)
[ "def", "specialize", "(", "self", ",", "start", ",", "end", ")", ":", "new_nodes", "=", "_curve_helpers", ".", "specialize_curve", "(", "self", ".", "_nodes", ",", "start", ",", "end", ")", "return", "Curve", "(", "new_nodes", ",", "self", ".", "_degree", ",", "_copy", "=", "False", ")" ]
Specialize the curve to a given sub-interval. .. image:: ../../images/curve_specialize.png :align: center .. doctest:: curve-specialize >>> nodes = np.asfortranarray([ ... [0.0, 0.5, 1.0], ... [0.0, 1.0, 0.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> new_curve = curve.specialize(-0.25, 0.75) >>> new_curve.nodes array([[-0.25 , 0.25 , 0.75 ], [-0.625, 0.875, 0.375]]) .. testcleanup:: curve-specialize import make_images make_images.curve_specialize(curve, new_curve) This is generalized version of :meth:`subdivide`, and can even match the output of that method: .. testsetup:: curve-specialize2 import numpy as np import bezier nodes = np.asfortranarray([ [0.0, 0.5, 1.0], [0.0, 1.0, 0.0], ]) curve = bezier.Curve(nodes, degree=2) .. doctest:: curve-specialize2 >>> left, right = curve.subdivide() >>> also_left = curve.specialize(0.0, 0.5) >>> np.all(also_left.nodes == left.nodes) True >>> also_right = curve.specialize(0.5, 1.0) >>> np.all(also_right.nodes == right.nodes) True Args: start (float): The start point of the interval we are specializing to. end (float): The end point of the interval we are specializing to. Returns: Curve: The newly-specialized curve.
[ "Specialize", "the", "curve", "to", "a", "given", "sub", "-", "interval", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L540-L597
dhermes/bezier
src/bezier/curve.py
Curve.locate
def locate(self, point): r"""Find a point on the current curve. Solves for :math:`s` in :math:`B(s) = p`. This method acts as a (partial) inverse to :meth:`evaluate`. .. note:: A unique solution is only guaranteed if the current curve has no self-intersections. This code assumes, but doesn't check, that this is true. .. image:: ../../images/curve_locate.png :align: center .. doctest:: curve-locate >>> nodes = np.asfortranarray([ ... [0.0, -1.0, 1.0, -0.75 ], ... [2.0, 0.0, 1.0, 1.625], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> point1 = np.asfortranarray([ ... [-0.09375 ], ... [ 0.828125], ... ]) >>> curve.locate(point1) 0.5 >>> point2 = np.asfortranarray([ ... [0.0], ... [1.5], ... ]) >>> curve.locate(point2) is None True >>> point3 = np.asfortranarray([ ... [-0.25 ], ... [ 1.375], ... ]) >>> curve.locate(point3) is None Traceback (most recent call last): ... ValueError: Parameters not close enough to one another .. testcleanup:: curve-locate import make_images make_images.curve_locate(curve, point1, point2, point3) Args: point (numpy.ndarray): A (``D x 1``) point on the curve, where :math:`D` is the dimension of the curve. Returns: Optional[float]: The parameter value (:math:`s`) corresponding to ``point`` or :data:`None` if the point is not on the ``curve``. Raises: ValueError: If the dimension of the ``point`` doesn't match the dimension of the current curve. """ if point.shape != (self._dimension, 1): point_dimensions = " x ".join( str(dimension) for dimension in point.shape ) msg = _LOCATE_ERROR_TEMPLATE.format( self._dimension, self._dimension, point, point_dimensions ) raise ValueError(msg) return _curve_helpers.locate_point(self._nodes, point)
python
def locate(self, point): r"""Find a point on the current curve. Solves for :math:`s` in :math:`B(s) = p`. This method acts as a (partial) inverse to :meth:`evaluate`. .. note:: A unique solution is only guaranteed if the current curve has no self-intersections. This code assumes, but doesn't check, that this is true. .. image:: ../../images/curve_locate.png :align: center .. doctest:: curve-locate >>> nodes = np.asfortranarray([ ... [0.0, -1.0, 1.0, -0.75 ], ... [2.0, 0.0, 1.0, 1.625], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> point1 = np.asfortranarray([ ... [-0.09375 ], ... [ 0.828125], ... ]) >>> curve.locate(point1) 0.5 >>> point2 = np.asfortranarray([ ... [0.0], ... [1.5], ... ]) >>> curve.locate(point2) is None True >>> point3 = np.asfortranarray([ ... [-0.25 ], ... [ 1.375], ... ]) >>> curve.locate(point3) is None Traceback (most recent call last): ... ValueError: Parameters not close enough to one another .. testcleanup:: curve-locate import make_images make_images.curve_locate(curve, point1, point2, point3) Args: point (numpy.ndarray): A (``D x 1``) point on the curve, where :math:`D` is the dimension of the curve. Returns: Optional[float]: The parameter value (:math:`s`) corresponding to ``point`` or :data:`None` if the point is not on the ``curve``. Raises: ValueError: If the dimension of the ``point`` doesn't match the dimension of the current curve. """ if point.shape != (self._dimension, 1): point_dimensions = " x ".join( str(dimension) for dimension in point.shape ) msg = _LOCATE_ERROR_TEMPLATE.format( self._dimension, self._dimension, point, point_dimensions ) raise ValueError(msg) return _curve_helpers.locate_point(self._nodes, point)
[ "def", "locate", "(", "self", ",", "point", ")", ":", "if", "point", ".", "shape", "!=", "(", "self", ".", "_dimension", ",", "1", ")", ":", "point_dimensions", "=", "\" x \"", ".", "join", "(", "str", "(", "dimension", ")", "for", "dimension", "in", "point", ".", "shape", ")", "msg", "=", "_LOCATE_ERROR_TEMPLATE", ".", "format", "(", "self", ".", "_dimension", ",", "self", ".", "_dimension", ",", "point", ",", "point_dimensions", ")", "raise", "ValueError", "(", "msg", ")", "return", "_curve_helpers", ".", "locate_point", "(", "self", ".", "_nodes", ",", "point", ")" ]
r"""Find a point on the current curve. Solves for :math:`s` in :math:`B(s) = p`. This method acts as a (partial) inverse to :meth:`evaluate`. .. note:: A unique solution is only guaranteed if the current curve has no self-intersections. This code assumes, but doesn't check, that this is true. .. image:: ../../images/curve_locate.png :align: center .. doctest:: curve-locate >>> nodes = np.asfortranarray([ ... [0.0, -1.0, 1.0, -0.75 ], ... [2.0, 0.0, 1.0, 1.625], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> point1 = np.asfortranarray([ ... [-0.09375 ], ... [ 0.828125], ... ]) >>> curve.locate(point1) 0.5 >>> point2 = np.asfortranarray([ ... [0.0], ... [1.5], ... ]) >>> curve.locate(point2) is None True >>> point3 = np.asfortranarray([ ... [-0.25 ], ... [ 1.375], ... ]) >>> curve.locate(point3) is None Traceback (most recent call last): ... ValueError: Parameters not close enough to one another .. testcleanup:: curve-locate import make_images make_images.curve_locate(curve, point1, point2, point3) Args: point (numpy.ndarray): A (``D x 1``) point on the curve, where :math:`D` is the dimension of the curve. Returns: Optional[float]: The parameter value (:math:`s`) corresponding to ``point`` or :data:`None` if the point is not on the ``curve``. Raises: ValueError: If the dimension of the ``point`` doesn't match the dimension of the current curve.
[ "r", "Find", "a", "point", "on", "the", "current", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/curve.py#L599-L670
dhermes/bezier
scripts/clean_cython.py
clean_file
def clean_file(c_source, virtualenv_dirname): """Strip trailing whitespace and clean up "local" names in C source. These source files are autogenerated from the ``cython`` CLI. Args: c_source (str): Path to a ``.c`` source file. virtualenv_dirname (str): The name of the ``virtualenv`` directory where Cython is installed (this is part of a relative path ``.nox/{NAME}/lib/...``). """ with open(c_source, "r") as file_obj: contents = file_obj.read().rstrip() # Replace the path to the Cython include files. py_version = "python{}.{}".format(*sys.version_info[:2]) lib_path = os.path.join( ".nox", virtualenv_dirname, "lib", py_version, "site-packages", "" ) contents = contents.replace(lib_path, "") # Write the files back, but strip all trailing whitespace. lines = contents.split("\n") with open(c_source, "w") as file_obj: for line in lines: file_obj.write(line.rstrip() + "\n")
python
def clean_file(c_source, virtualenv_dirname): """Strip trailing whitespace and clean up "local" names in C source. These source files are autogenerated from the ``cython`` CLI. Args: c_source (str): Path to a ``.c`` source file. virtualenv_dirname (str): The name of the ``virtualenv`` directory where Cython is installed (this is part of a relative path ``.nox/{NAME}/lib/...``). """ with open(c_source, "r") as file_obj: contents = file_obj.read().rstrip() # Replace the path to the Cython include files. py_version = "python{}.{}".format(*sys.version_info[:2]) lib_path = os.path.join( ".nox", virtualenv_dirname, "lib", py_version, "site-packages", "" ) contents = contents.replace(lib_path, "") # Write the files back, but strip all trailing whitespace. lines = contents.split("\n") with open(c_source, "w") as file_obj: for line in lines: file_obj.write(line.rstrip() + "\n")
[ "def", "clean_file", "(", "c_source", ",", "virtualenv_dirname", ")", ":", "with", "open", "(", "c_source", ",", "\"r\"", ")", "as", "file_obj", ":", "contents", "=", "file_obj", ".", "read", "(", ")", ".", "rstrip", "(", ")", "# Replace the path to the Cython include files.", "py_version", "=", "\"python{}.{}\"", ".", "format", "(", "*", "sys", ".", "version_info", "[", ":", "2", "]", ")", "lib_path", "=", "os", ".", "path", ".", "join", "(", "\".nox\"", ",", "virtualenv_dirname", ",", "\"lib\"", ",", "py_version", ",", "\"site-packages\"", ",", "\"\"", ")", "contents", "=", "contents", ".", "replace", "(", "lib_path", ",", "\"\"", ")", "# Write the files back, but strip all trailing whitespace.", "lines", "=", "contents", ".", "split", "(", "\"\\n\"", ")", "with", "open", "(", "c_source", ",", "\"w\"", ")", "as", "file_obj", ":", "for", "line", "in", "lines", ":", "file_obj", ".", "write", "(", "line", ".", "rstrip", "(", ")", "+", "\"\\n\"", ")" ]
Strip trailing whitespace and clean up "local" names in C source. These source files are autogenerated from the ``cython`` CLI. Args: c_source (str): Path to a ``.c`` source file. virtualenv_dirname (str): The name of the ``virtualenv`` directory where Cython is installed (this is part of a relative path ``.nox/{NAME}/lib/...``).
[ "Strip", "trailing", "whitespace", "and", "clean", "up", "local", "names", "in", "C", "source", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/clean_cython.py#L17-L40
dhermes/bezier
scripts/doc_template_release.py
get_version
def get_version(): """Get the current version from ``setup.py``. Assumes that importing ``setup.py`` will have no side-effects (i.e. assumes the behavior is guarded by ``if __name__ == "__main__"``). Returns: str: The current version in ``setup.py``. """ # "Spoof" the ``setup.py`` helper modules. sys.modules["setup_helpers"] = object() sys.modules["setup_helpers_macos"] = object() sys.modules["setup_helpers_windows"] = object() filename = os.path.join(_ROOT_DIR, "setup.py") loader = importlib.machinery.SourceFileLoader("setup", filename) setup_mod = loader.load_module() return setup_mod.VERSION
python
def get_version(): """Get the current version from ``setup.py``. Assumes that importing ``setup.py`` will have no side-effects (i.e. assumes the behavior is guarded by ``if __name__ == "__main__"``). Returns: str: The current version in ``setup.py``. """ # "Spoof" the ``setup.py`` helper modules. sys.modules["setup_helpers"] = object() sys.modules["setup_helpers_macos"] = object() sys.modules["setup_helpers_windows"] = object() filename = os.path.join(_ROOT_DIR, "setup.py") loader = importlib.machinery.SourceFileLoader("setup", filename) setup_mod = loader.load_module() return setup_mod.VERSION
[ "def", "get_version", "(", ")", ":", "# \"Spoof\" the ``setup.py`` helper modules.", "sys", ".", "modules", "[", "\"setup_helpers\"", "]", "=", "object", "(", ")", "sys", ".", "modules", "[", "\"setup_helpers_macos\"", "]", "=", "object", "(", ")", "sys", ".", "modules", "[", "\"setup_helpers_windows\"", "]", "=", "object", "(", ")", "filename", "=", "os", ".", "path", ".", "join", "(", "_ROOT_DIR", ",", "\"setup.py\"", ")", "loader", "=", "importlib", ".", "machinery", ".", "SourceFileLoader", "(", "\"setup\"", ",", "filename", ")", "setup_mod", "=", "loader", ".", "load_module", "(", ")", "return", "setup_mod", ".", "VERSION" ]
Get the current version from ``setup.py``. Assumes that importing ``setup.py`` will have no side-effects (i.e. assumes the behavior is guarded by ``if __name__ == "__main__"``). Returns: str: The current version in ``setup.py``.
[ "Get", "the", "current", "version", "from", "setup", ".", "py", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/doc_template_release.py#L54-L70
dhermes/bezier
scripts/doc_template_release.py
populate_readme
def populate_readme( version, circleci_build, appveyor_build, coveralls_build, travis_build ): """Populates ``README.rst`` with release-specific data. This is because ``README.rst`` is used on PyPI. Args: version (str): The current version. circleci_build (Union[str, int]): The CircleCI build ID corresponding to the release. appveyor_build (str): The AppVeyor build ID corresponding to the release. coveralls_build (Union[str, int]): The Coveralls.io build ID corresponding to the release. travis_build (int): The Travis CI build ID corresponding to the release. """ with open(RELEASE_README_FILE, "r") as file_obj: template = file_obj.read() contents = template.format( version=version, circleci_build=circleci_build, appveyor_build=appveyor_build, coveralls_build=coveralls_build, travis_build=travis_build, ) with open(README_FILE, "w") as file_obj: file_obj.write(contents)
python
def populate_readme( version, circleci_build, appveyor_build, coveralls_build, travis_build ): """Populates ``README.rst`` with release-specific data. This is because ``README.rst`` is used on PyPI. Args: version (str): The current version. circleci_build (Union[str, int]): The CircleCI build ID corresponding to the release. appveyor_build (str): The AppVeyor build ID corresponding to the release. coveralls_build (Union[str, int]): The Coveralls.io build ID corresponding to the release. travis_build (int): The Travis CI build ID corresponding to the release. """ with open(RELEASE_README_FILE, "r") as file_obj: template = file_obj.read() contents = template.format( version=version, circleci_build=circleci_build, appveyor_build=appveyor_build, coveralls_build=coveralls_build, travis_build=travis_build, ) with open(README_FILE, "w") as file_obj: file_obj.write(contents)
[ "def", "populate_readme", "(", "version", ",", "circleci_build", ",", "appveyor_build", ",", "coveralls_build", ",", "travis_build", ")", ":", "with", "open", "(", "RELEASE_README_FILE", ",", "\"r\"", ")", "as", "file_obj", ":", "template", "=", "file_obj", ".", "read", "(", ")", "contents", "=", "template", ".", "format", "(", "version", "=", "version", ",", "circleci_build", "=", "circleci_build", ",", "appveyor_build", "=", "appveyor_build", ",", "coveralls_build", "=", "coveralls_build", ",", "travis_build", "=", "travis_build", ",", ")", "with", "open", "(", "README_FILE", ",", "\"w\"", ")", "as", "file_obj", ":", "file_obj", ".", "write", "(", "contents", ")" ]
Populates ``README.rst`` with release-specific data. This is because ``README.rst`` is used on PyPI. Args: version (str): The current version. circleci_build (Union[str, int]): The CircleCI build ID corresponding to the release. appveyor_build (str): The AppVeyor build ID corresponding to the release. coveralls_build (Union[str, int]): The Coveralls.io build ID corresponding to the release. travis_build (int): The Travis CI build ID corresponding to the release.
[ "Populates", "README", ".", "rst", "with", "release", "-", "specific", "data", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/doc_template_release.py#L73-L101
dhermes/bezier
scripts/doc_template_release.py
populate_index
def populate_index( version, circleci_build, appveyor_build, coveralls_build, travis_build ): """Populates ``docs/index.rst`` with release-specific data. Args: version (str): The current version. circleci_build (Union[str, int]): The CircleCI build ID corresponding to the release. appveyor_build (str): The AppVeyor build ID corresponding to the release. coveralls_build (Union[str, int]): The Coveralls.io build ID corresponding to the release. travis_build (int): The Travis CI build ID corresponding to the release. """ with open(RELEASE_INDEX_FILE, "r") as file_obj: template = file_obj.read() contents = template.format( version=version, circleci_build=circleci_build, appveyor_build=appveyor_build, coveralls_build=coveralls_build, travis_build=travis_build, ) with open(INDEX_FILE, "w") as file_obj: file_obj.write(contents)
python
def populate_index( version, circleci_build, appveyor_build, coveralls_build, travis_build ): """Populates ``docs/index.rst`` with release-specific data. Args: version (str): The current version. circleci_build (Union[str, int]): The CircleCI build ID corresponding to the release. appveyor_build (str): The AppVeyor build ID corresponding to the release. coveralls_build (Union[str, int]): The Coveralls.io build ID corresponding to the release. travis_build (int): The Travis CI build ID corresponding to the release. """ with open(RELEASE_INDEX_FILE, "r") as file_obj: template = file_obj.read() contents = template.format( version=version, circleci_build=circleci_build, appveyor_build=appveyor_build, coveralls_build=coveralls_build, travis_build=travis_build, ) with open(INDEX_FILE, "w") as file_obj: file_obj.write(contents)
[ "def", "populate_index", "(", "version", ",", "circleci_build", ",", "appveyor_build", ",", "coveralls_build", ",", "travis_build", ")", ":", "with", "open", "(", "RELEASE_INDEX_FILE", ",", "\"r\"", ")", "as", "file_obj", ":", "template", "=", "file_obj", ".", "read", "(", ")", "contents", "=", "template", ".", "format", "(", "version", "=", "version", ",", "circleci_build", "=", "circleci_build", ",", "appveyor_build", "=", "appveyor_build", ",", "coveralls_build", "=", "coveralls_build", ",", "travis_build", "=", "travis_build", ",", ")", "with", "open", "(", "INDEX_FILE", ",", "\"w\"", ")", "as", "file_obj", ":", "file_obj", ".", "write", "(", "contents", ")" ]
Populates ``docs/index.rst`` with release-specific data. Args: version (str): The current version. circleci_build (Union[str, int]): The CircleCI build ID corresponding to the release. appveyor_build (str): The AppVeyor build ID corresponding to the release. coveralls_build (Union[str, int]): The Coveralls.io build ID corresponding to the release. travis_build (int): The Travis CI build ID corresponding to the release.
[ "Populates", "docs", "/", "index", ".", "rst", "with", "release", "-", "specific", "data", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/doc_template_release.py#L104-L130
dhermes/bezier
scripts/doc_template_release.py
populate_native_libraries
def populate_native_libraries(version): """Populates ``binary-extension.rst`` with release-specific data. Args: version (str): The current version. """ with open(BINARY_EXT_TEMPLATE, "r") as file_obj: template = file_obj.read() contents = template.format(revision=version) with open(BINARY_EXT_FILE, "w") as file_obj: file_obj.write(contents)
python
def populate_native_libraries(version): """Populates ``binary-extension.rst`` with release-specific data. Args: version (str): The current version. """ with open(BINARY_EXT_TEMPLATE, "r") as file_obj: template = file_obj.read() contents = template.format(revision=version) with open(BINARY_EXT_FILE, "w") as file_obj: file_obj.write(contents)
[ "def", "populate_native_libraries", "(", "version", ")", ":", "with", "open", "(", "BINARY_EXT_TEMPLATE", ",", "\"r\"", ")", "as", "file_obj", ":", "template", "=", "file_obj", ".", "read", "(", ")", "contents", "=", "template", ".", "format", "(", "revision", "=", "version", ")", "with", "open", "(", "BINARY_EXT_FILE", ",", "\"w\"", ")", "as", "file_obj", ":", "file_obj", ".", "write", "(", "contents", ")" ]
Populates ``binary-extension.rst`` with release-specific data. Args: version (str): The current version.
[ "Populates", "binary", "-", "extension", ".", "rst", "with", "release", "-", "specific", "data", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/doc_template_release.py#L133-L143
dhermes/bezier
scripts/doc_template_release.py
populate_development
def populate_development(version): """Populates ``DEVELOPMENT.rst`` with release-specific data. This is because ``DEVELOPMENT.rst`` is used in the Sphinx documentation. Args: version (str): The current version. """ with open(DEVELOPMENT_TEMPLATE, "r") as file_obj: template = file_obj.read() contents = template.format(revision=version, rtd_version=version) with open(DEVELOPMENT_FILE, "w") as file_obj: file_obj.write(contents)
python
def populate_development(version): """Populates ``DEVELOPMENT.rst`` with release-specific data. This is because ``DEVELOPMENT.rst`` is used in the Sphinx documentation. Args: version (str): The current version. """ with open(DEVELOPMENT_TEMPLATE, "r") as file_obj: template = file_obj.read() contents = template.format(revision=version, rtd_version=version) with open(DEVELOPMENT_FILE, "w") as file_obj: file_obj.write(contents)
[ "def", "populate_development", "(", "version", ")", ":", "with", "open", "(", "DEVELOPMENT_TEMPLATE", ",", "\"r\"", ")", "as", "file_obj", ":", "template", "=", "file_obj", ".", "read", "(", ")", "contents", "=", "template", ".", "format", "(", "revision", "=", "version", ",", "rtd_version", "=", "version", ")", "with", "open", "(", "DEVELOPMENT_FILE", ",", "\"w\"", ")", "as", "file_obj", ":", "file_obj", ".", "write", "(", "contents", ")" ]
Populates ``DEVELOPMENT.rst`` with release-specific data. This is because ``DEVELOPMENT.rst`` is used in the Sphinx documentation. Args: version (str): The current version.
[ "Populates", "DEVELOPMENT", ".", "rst", "with", "release", "-", "specific", "data", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/doc_template_release.py#L146-L158
dhermes/bezier
scripts/doc_template_release.py
main
def main(): """Populate the templates with release-specific fields. Requires user input for the CircleCI, AppVeyor, Coveralls.io and Travis build IDs. """ version = get_version() circleci_build = six.moves.input("CircleCI Build ID: ") appveyor_build = six.moves.input("AppVeyor Build ID: ") coveralls_build = six.moves.input("Coveralls Build ID: ") travis_build = six.moves.input("Travis Build ID: ") populate_readme( version, circleci_build, appveyor_build, coveralls_build, travis_build ) populate_index( version, circleci_build, appveyor_build, coveralls_build, travis_build ) populate_native_libraries(version) populate_development(version)
python
def main(): """Populate the templates with release-specific fields. Requires user input for the CircleCI, AppVeyor, Coveralls.io and Travis build IDs. """ version = get_version() circleci_build = six.moves.input("CircleCI Build ID: ") appveyor_build = six.moves.input("AppVeyor Build ID: ") coveralls_build = six.moves.input("Coveralls Build ID: ") travis_build = six.moves.input("Travis Build ID: ") populate_readme( version, circleci_build, appveyor_build, coveralls_build, travis_build ) populate_index( version, circleci_build, appveyor_build, coveralls_build, travis_build ) populate_native_libraries(version) populate_development(version)
[ "def", "main", "(", ")", ":", "version", "=", "get_version", "(", ")", "circleci_build", "=", "six", ".", "moves", ".", "input", "(", "\"CircleCI Build ID: \"", ")", "appveyor_build", "=", "six", ".", "moves", ".", "input", "(", "\"AppVeyor Build ID: \"", ")", "coveralls_build", "=", "six", ".", "moves", ".", "input", "(", "\"Coveralls Build ID: \"", ")", "travis_build", "=", "six", ".", "moves", ".", "input", "(", "\"Travis Build ID: \"", ")", "populate_readme", "(", "version", ",", "circleci_build", ",", "appveyor_build", ",", "coveralls_build", ",", "travis_build", ")", "populate_index", "(", "version", ",", "circleci_build", ",", "appveyor_build", ",", "coveralls_build", ",", "travis_build", ")", "populate_native_libraries", "(", "version", ")", "populate_development", "(", "version", ")" ]
Populate the templates with release-specific fields. Requires user input for the CircleCI, AppVeyor, Coveralls.io and Travis build IDs.
[ "Populate", "the", "templates", "with", "release", "-", "specific", "fields", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/doc_template_release.py#L161-L179
dhermes/bezier
src/bezier/_curve_helpers.py
make_subdivision_matrices
def make_subdivision_matrices(degree): """Make the matrix used to subdivide a curve. .. note:: This is a helper for :func:`_subdivide_nodes`. It does not have a Fortran speedup because it is **only** used by a function which has a Fortran speedup. Args: degree (int): The degree of the curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The matrices used to convert the nodes into left and right nodes, respectively. """ left = np.zeros((degree + 1, degree + 1), order="F") right = np.zeros((degree + 1, degree + 1), order="F") left[0, 0] = 1.0 right[-1, -1] = 1.0 for col in six.moves.xrange(1, degree + 1): half_prev = 0.5 * left[:col, col - 1] left[:col, col] = half_prev left[1 : col + 1, col] += half_prev # noqa: E203 # Populate the complement col (in right) as well. complement = degree - col # NOTE: We "should" reverse the results when using # the complement, but they are symmetric so # that would be a waste. right[-(col + 1) :, complement] = left[: col + 1, col] # noqa: E203 return left, right
python
def make_subdivision_matrices(degree): """Make the matrix used to subdivide a curve. .. note:: This is a helper for :func:`_subdivide_nodes`. It does not have a Fortran speedup because it is **only** used by a function which has a Fortran speedup. Args: degree (int): The degree of the curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The matrices used to convert the nodes into left and right nodes, respectively. """ left = np.zeros((degree + 1, degree + 1), order="F") right = np.zeros((degree + 1, degree + 1), order="F") left[0, 0] = 1.0 right[-1, -1] = 1.0 for col in six.moves.xrange(1, degree + 1): half_prev = 0.5 * left[:col, col - 1] left[:col, col] = half_prev left[1 : col + 1, col] += half_prev # noqa: E203 # Populate the complement col (in right) as well. complement = degree - col # NOTE: We "should" reverse the results when using # the complement, but they are symmetric so # that would be a waste. right[-(col + 1) :, complement] = left[: col + 1, col] # noqa: E203 return left, right
[ "def", "make_subdivision_matrices", "(", "degree", ")", ":", "left", "=", "np", ".", "zeros", "(", "(", "degree", "+", "1", ",", "degree", "+", "1", ")", ",", "order", "=", "\"F\"", ")", "right", "=", "np", ".", "zeros", "(", "(", "degree", "+", "1", ",", "degree", "+", "1", ")", ",", "order", "=", "\"F\"", ")", "left", "[", "0", ",", "0", "]", "=", "1.0", "right", "[", "-", "1", ",", "-", "1", "]", "=", "1.0", "for", "col", "in", "six", ".", "moves", ".", "xrange", "(", "1", ",", "degree", "+", "1", ")", ":", "half_prev", "=", "0.5", "*", "left", "[", ":", "col", ",", "col", "-", "1", "]", "left", "[", ":", "col", ",", "col", "]", "=", "half_prev", "left", "[", "1", ":", "col", "+", "1", ",", "col", "]", "+=", "half_prev", "# noqa: E203", "# Populate the complement col (in right) as well.", "complement", "=", "degree", "-", "col", "# NOTE: We \"should\" reverse the results when using", "# the complement, but they are symmetric so", "# that would be a waste.", "right", "[", "-", "(", "col", "+", "1", ")", ":", ",", "complement", "]", "=", "left", "[", ":", "col", "+", "1", ",", "col", "]", "# noqa: E203", "return", "left", ",", "right" ]
Make the matrix used to subdivide a curve. .. note:: This is a helper for :func:`_subdivide_nodes`. It does not have a Fortran speedup because it is **only** used by a function which has a Fortran speedup. Args: degree (int): The degree of the curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The matrices used to convert the nodes into left and right nodes, respectively.
[ "Make", "the", "matrix", "used", "to", "subdivide", "a", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L128-L158
dhermes/bezier
src/bezier/_curve_helpers.py
_subdivide_nodes
def _subdivide_nodes(nodes): """Subdivide a curve into two sub-curves. Does so by taking the unit interval (i.e. the domain of the surface) and splitting it into two sub-intervals by splitting down the middle. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The nodes for the two sub-curves. """ _, num_nodes = np.shape(nodes) if num_nodes == 2: left_nodes = _helpers.matrix_product(nodes, _LINEAR_SUBDIVIDE_LEFT) right_nodes = _helpers.matrix_product(nodes, _LINEAR_SUBDIVIDE_RIGHT) elif num_nodes == 3: left_nodes = _helpers.matrix_product(nodes, _QUADRATIC_SUBDIVIDE_LEFT) right_nodes = _helpers.matrix_product( nodes, _QUADRATIC_SUBDIVIDE_RIGHT ) elif num_nodes == 4: left_nodes = _helpers.matrix_product(nodes, _CUBIC_SUBDIVIDE_LEFT) right_nodes = _helpers.matrix_product(nodes, _CUBIC_SUBDIVIDE_RIGHT) else: left_mat, right_mat = make_subdivision_matrices(num_nodes - 1) left_nodes = _helpers.matrix_product(nodes, left_mat) right_nodes = _helpers.matrix_product(nodes, right_mat) return left_nodes, right_nodes
python
def _subdivide_nodes(nodes): """Subdivide a curve into two sub-curves. Does so by taking the unit interval (i.e. the domain of the surface) and splitting it into two sub-intervals by splitting down the middle. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The nodes for the two sub-curves. """ _, num_nodes = np.shape(nodes) if num_nodes == 2: left_nodes = _helpers.matrix_product(nodes, _LINEAR_SUBDIVIDE_LEFT) right_nodes = _helpers.matrix_product(nodes, _LINEAR_SUBDIVIDE_RIGHT) elif num_nodes == 3: left_nodes = _helpers.matrix_product(nodes, _QUADRATIC_SUBDIVIDE_LEFT) right_nodes = _helpers.matrix_product( nodes, _QUADRATIC_SUBDIVIDE_RIGHT ) elif num_nodes == 4: left_nodes = _helpers.matrix_product(nodes, _CUBIC_SUBDIVIDE_LEFT) right_nodes = _helpers.matrix_product(nodes, _CUBIC_SUBDIVIDE_RIGHT) else: left_mat, right_mat = make_subdivision_matrices(num_nodes - 1) left_nodes = _helpers.matrix_product(nodes, left_mat) right_nodes = _helpers.matrix_product(nodes, right_mat) return left_nodes, right_nodes
[ "def", "_subdivide_nodes", "(", "nodes", ")", ":", "_", ",", "num_nodes", "=", "np", ".", "shape", "(", "nodes", ")", "if", "num_nodes", "==", "2", ":", "left_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_LINEAR_SUBDIVIDE_LEFT", ")", "right_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_LINEAR_SUBDIVIDE_RIGHT", ")", "elif", "num_nodes", "==", "3", ":", "left_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_QUADRATIC_SUBDIVIDE_LEFT", ")", "right_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_QUADRATIC_SUBDIVIDE_RIGHT", ")", "elif", "num_nodes", "==", "4", ":", "left_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_CUBIC_SUBDIVIDE_LEFT", ")", "right_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "_CUBIC_SUBDIVIDE_RIGHT", ")", "else", ":", "left_mat", ",", "right_mat", "=", "make_subdivision_matrices", "(", "num_nodes", "-", "1", ")", "left_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "left_mat", ")", "right_nodes", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "right_mat", ")", "return", "left_nodes", ",", "right_nodes" ]
Subdivide a curve into two sub-curves. Does so by taking the unit interval (i.e. the domain of the surface) and splitting it into two sub-intervals by splitting down the middle. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. Returns: Tuple[numpy.ndarray, numpy.ndarray]: The nodes for the two sub-curves.
[ "Subdivide", "a", "curve", "into", "two", "sub", "-", "curves", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L161-L194
dhermes/bezier
src/bezier/_curve_helpers.py
_evaluate_multi_barycentric
def _evaluate_multi_barycentric(nodes, lambda1, lambda2): r"""Evaluates a B |eacute| zier type-function. Of the form .. math:: B(\lambda_1, \lambda_2) = \sum_j \binom{n}{j} \lambda_1^{n - j} \lambda_2^j \cdot v_j for some set of vectors :math:`v_j` given by ``nodes``. Does so via a modified Horner's method for each pair of values in ``lambda1`` and ``lambda2``, rather than using the de Casteljau algorithm. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. lambda1 (numpy.ndarray): Parameters along the curve (as a 1D array). lambda2 (numpy.ndarray): Parameters along the curve (as a 1D array). Typically we have ``lambda1 + lambda2 == 1``. Returns: numpy.ndarray: The evaluated points as a two dimensional NumPy array, with the columns corresponding to each pair of parameter values and the rows to the dimension. """ # NOTE: We assume but don't check that lambda2 has the same shape. num_vals, = lambda1.shape dimension, num_nodes = nodes.shape degree = num_nodes - 1 # Resize as row vectors for broadcast multiplying with # columns of ``nodes``. lambda1 = lambda1[np.newaxis, :] lambda2 = lambda2[np.newaxis, :] result = np.zeros((dimension, num_vals), order="F") result += lambda1 * nodes[:, [0]] binom_val = 1.0 lambda2_pow = np.ones((1, num_vals), order="F") for index in six.moves.xrange(1, degree): lambda2_pow *= lambda2 binom_val = (binom_val * (degree - index + 1)) / index result += binom_val * lambda2_pow * nodes[:, [index]] result *= lambda1 result += lambda2 * lambda2_pow * nodes[:, [degree]] return result
python
def _evaluate_multi_barycentric(nodes, lambda1, lambda2): r"""Evaluates a B |eacute| zier type-function. Of the form .. math:: B(\lambda_1, \lambda_2) = \sum_j \binom{n}{j} \lambda_1^{n - j} \lambda_2^j \cdot v_j for some set of vectors :math:`v_j` given by ``nodes``. Does so via a modified Horner's method for each pair of values in ``lambda1`` and ``lambda2``, rather than using the de Casteljau algorithm. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. lambda1 (numpy.ndarray): Parameters along the curve (as a 1D array). lambda2 (numpy.ndarray): Parameters along the curve (as a 1D array). Typically we have ``lambda1 + lambda2 == 1``. Returns: numpy.ndarray: The evaluated points as a two dimensional NumPy array, with the columns corresponding to each pair of parameter values and the rows to the dimension. """ # NOTE: We assume but don't check that lambda2 has the same shape. num_vals, = lambda1.shape dimension, num_nodes = nodes.shape degree = num_nodes - 1 # Resize as row vectors for broadcast multiplying with # columns of ``nodes``. lambda1 = lambda1[np.newaxis, :] lambda2 = lambda2[np.newaxis, :] result = np.zeros((dimension, num_vals), order="F") result += lambda1 * nodes[:, [0]] binom_val = 1.0 lambda2_pow = np.ones((1, num_vals), order="F") for index in six.moves.xrange(1, degree): lambda2_pow *= lambda2 binom_val = (binom_val * (degree - index + 1)) / index result += binom_val * lambda2_pow * nodes[:, [index]] result *= lambda1 result += lambda2 * lambda2_pow * nodes[:, [degree]] return result
[ "def", "_evaluate_multi_barycentric", "(", "nodes", ",", "lambda1", ",", "lambda2", ")", ":", "# NOTE: We assume but don't check that lambda2 has the same shape.", "num_vals", ",", "=", "lambda1", ".", "shape", "dimension", ",", "num_nodes", "=", "nodes", ".", "shape", "degree", "=", "num_nodes", "-", "1", "# Resize as row vectors for broadcast multiplying with", "# columns of ``nodes``.", "lambda1", "=", "lambda1", "[", "np", ".", "newaxis", ",", ":", "]", "lambda2", "=", "lambda2", "[", "np", ".", "newaxis", ",", ":", "]", "result", "=", "np", ".", "zeros", "(", "(", "dimension", ",", "num_vals", ")", ",", "order", "=", "\"F\"", ")", "result", "+=", "lambda1", "*", "nodes", "[", ":", ",", "[", "0", "]", "]", "binom_val", "=", "1.0", "lambda2_pow", "=", "np", ".", "ones", "(", "(", "1", ",", "num_vals", ")", ",", "order", "=", "\"F\"", ")", "for", "index", "in", "six", ".", "moves", ".", "xrange", "(", "1", ",", "degree", ")", ":", "lambda2_pow", "*=", "lambda2", "binom_val", "=", "(", "binom_val", "*", "(", "degree", "-", "index", "+", "1", ")", ")", "/", "index", "result", "+=", "binom_val", "*", "lambda2_pow", "*", "nodes", "[", ":", ",", "[", "index", "]", "]", "result", "*=", "lambda1", "result", "+=", "lambda2", "*", "lambda2_pow", "*", "nodes", "[", ":", ",", "[", "degree", "]", "]", "return", "result" ]
r"""Evaluates a B |eacute| zier type-function. Of the form .. math:: B(\lambda_1, \lambda_2) = \sum_j \binom{n}{j} \lambda_1^{n - j} \lambda_2^j \cdot v_j for some set of vectors :math:`v_j` given by ``nodes``. Does so via a modified Horner's method for each pair of values in ``lambda1`` and ``lambda2``, rather than using the de Casteljau algorithm. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. lambda1 (numpy.ndarray): Parameters along the curve (as a 1D array). lambda2 (numpy.ndarray): Parameters along the curve (as a 1D array). Typically we have ``lambda1 + lambda2 == 1``. Returns: numpy.ndarray: The evaluated points as a two dimensional NumPy array, with the columns corresponding to each pair of parameter values and the rows to the dimension.
[ "r", "Evaluates", "a", "B", "|eacute|", "zier", "type", "-", "function", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L222-L273
dhermes/bezier
src/bezier/_curve_helpers.py
vec_size
def vec_size(nodes, s_val): r"""Compute :math:`\|B(s)\|_2`. .. note:: This is a helper for :func:`_compute_length` and does not have a Fortran speedup. Intended to be used with ``functools.partial`` to fill in the value of ``nodes`` and create a callable that only accepts ``s_val``. Args: nodes (numpy.ndarray): The nodes defining a curve. s_val (float): Parameter to compute :math:`B(s)`. Returns: float: The norm of :math:`B(s)`. """ result_vec = evaluate_multi(nodes, np.asfortranarray([s_val])) # NOTE: We convert to 1D to make sure NumPy uses vector norm. return np.linalg.norm(result_vec[:, 0], ord=2)
python
def vec_size(nodes, s_val): r"""Compute :math:`\|B(s)\|_2`. .. note:: This is a helper for :func:`_compute_length` and does not have a Fortran speedup. Intended to be used with ``functools.partial`` to fill in the value of ``nodes`` and create a callable that only accepts ``s_val``. Args: nodes (numpy.ndarray): The nodes defining a curve. s_val (float): Parameter to compute :math:`B(s)`. Returns: float: The norm of :math:`B(s)`. """ result_vec = evaluate_multi(nodes, np.asfortranarray([s_val])) # NOTE: We convert to 1D to make sure NumPy uses vector norm. return np.linalg.norm(result_vec[:, 0], ord=2)
[ "def", "vec_size", "(", "nodes", ",", "s_val", ")", ":", "result_vec", "=", "evaluate_multi", "(", "nodes", ",", "np", ".", "asfortranarray", "(", "[", "s_val", "]", ")", ")", "# NOTE: We convert to 1D to make sure NumPy uses vector norm.", "return", "np", ".", "linalg", ".", "norm", "(", "result_vec", "[", ":", ",", "0", "]", ",", "ord", "=", "2", ")" ]
r"""Compute :math:`\|B(s)\|_2`. .. note:: This is a helper for :func:`_compute_length` and does not have a Fortran speedup. Intended to be used with ``functools.partial`` to fill in the value of ``nodes`` and create a callable that only accepts ``s_val``. Args: nodes (numpy.ndarray): The nodes defining a curve. s_val (float): Parameter to compute :math:`B(s)`. Returns: float: The norm of :math:`B(s)`.
[ "r", "Compute", ":", "math", ":", "\\", "|B", "(", "s", ")", "\\", "|_2", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L276-L296
dhermes/bezier
src/bezier/_curve_helpers.py
_compute_length
def _compute_length(nodes): r"""Approximately compute the length of a curve. .. _QUADPACK: https://en.wikipedia.org/wiki/QUADPACK If ``degree`` is :math:`n`, then the Hodograph curve :math:`B'(s)` is degree :math:`d = n - 1`. Using this curve, we approximate the integral: .. math:: \int_{B\left(\left[0, 1\right]\right)} 1 \, d\mathbf{x} = \int_0^1 \left\lVert B'(s) \right\rVert_2 \, ds using `QUADPACK`_ (via SciPy). .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. Returns: float: The length of the curve. Raises: OSError: If SciPy is not installed. """ _, num_nodes = np.shape(nodes) # NOTE: We somewhat replicate code in ``evaluate_hodograph()`` # here. This is so we don't re-compute the nodes for the first # derivative every time it is evaluated. first_deriv = (num_nodes - 1) * (nodes[:, 1:] - nodes[:, :-1]) if num_nodes == 2: # NOTE: We convert to 1D to make sure NumPy uses vector norm. return np.linalg.norm(first_deriv[:, 0], ord=2) if _scipy_int is None: raise OSError("This function requires SciPy for quadrature.") size_func = functools.partial(vec_size, first_deriv) length, _ = _scipy_int.quad(size_func, 0.0, 1.0) return length
python
def _compute_length(nodes): r"""Approximately compute the length of a curve. .. _QUADPACK: https://en.wikipedia.org/wiki/QUADPACK If ``degree`` is :math:`n`, then the Hodograph curve :math:`B'(s)` is degree :math:`d = n - 1`. Using this curve, we approximate the integral: .. math:: \int_{B\left(\left[0, 1\right]\right)} 1 \, d\mathbf{x} = \int_0^1 \left\lVert B'(s) \right\rVert_2 \, ds using `QUADPACK`_ (via SciPy). .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. Returns: float: The length of the curve. Raises: OSError: If SciPy is not installed. """ _, num_nodes = np.shape(nodes) # NOTE: We somewhat replicate code in ``evaluate_hodograph()`` # here. This is so we don't re-compute the nodes for the first # derivative every time it is evaluated. first_deriv = (num_nodes - 1) * (nodes[:, 1:] - nodes[:, :-1]) if num_nodes == 2: # NOTE: We convert to 1D to make sure NumPy uses vector norm. return np.linalg.norm(first_deriv[:, 0], ord=2) if _scipy_int is None: raise OSError("This function requires SciPy for quadrature.") size_func = functools.partial(vec_size, first_deriv) length, _ = _scipy_int.quad(size_func, 0.0, 1.0) return length
[ "def", "_compute_length", "(", "nodes", ")", ":", "_", ",", "num_nodes", "=", "np", ".", "shape", "(", "nodes", ")", "# NOTE: We somewhat replicate code in ``evaluate_hodograph()``", "# here. This is so we don't re-compute the nodes for the first", "# derivative every time it is evaluated.", "first_deriv", "=", "(", "num_nodes", "-", "1", ")", "*", "(", "nodes", "[", ":", ",", "1", ":", "]", "-", "nodes", "[", ":", ",", ":", "-", "1", "]", ")", "if", "num_nodes", "==", "2", ":", "# NOTE: We convert to 1D to make sure NumPy uses vector norm.", "return", "np", ".", "linalg", ".", "norm", "(", "first_deriv", "[", ":", ",", "0", "]", ",", "ord", "=", "2", ")", "if", "_scipy_int", "is", "None", ":", "raise", "OSError", "(", "\"This function requires SciPy for quadrature.\"", ")", "size_func", "=", "functools", ".", "partial", "(", "vec_size", ",", "first_deriv", ")", "length", ",", "_", "=", "_scipy_int", ".", "quad", "(", "size_func", ",", "0.0", ",", "1.0", ")", "return", "length" ]
r"""Approximately compute the length of a curve. .. _QUADPACK: https://en.wikipedia.org/wiki/QUADPACK If ``degree`` is :math:`n`, then the Hodograph curve :math:`B'(s)` is degree :math:`d = n - 1`. Using this curve, we approximate the integral: .. math:: \int_{B\left(\left[0, 1\right]\right)} 1 \, d\mathbf{x} = \int_0^1 \left\lVert B'(s) \right\rVert_2 \, ds using `QUADPACK`_ (via SciPy). .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. Returns: float: The length of the curve. Raises: OSError: If SciPy is not installed.
[ "r", "Approximately", "compute", "the", "length", "of", "a", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L299-L343
dhermes/bezier
src/bezier/_curve_helpers.py
_elevate_nodes
def _elevate_nodes(nodes): r"""Degree-elevate a B |eacute| zier curves. Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n + 1}` where .. math:: \begin{align*} w_0 &= v_0 \\ w_j &= \frac{j}{n + 1} v_{j - 1} + \frac{n + 1 - j}{n + 1} v_j \\ w_{n + 1} &= v_n \end{align*} .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. Returns: numpy.ndarray: The nodes of the degree-elevated curve. """ dimension, num_nodes = np.shape(nodes) new_nodes = np.empty((dimension, num_nodes + 1), order="F") multipliers = np.arange(1, num_nodes, dtype=_FLOAT64)[np.newaxis, :] denominator = float(num_nodes) new_nodes[:, 1:-1] = ( multipliers * nodes[:, :-1] + (denominator - multipliers) * nodes[:, 1:] ) # Hold off on division until the end, to (attempt to) avoid round-off. new_nodes /= denominator # After setting the internal nodes (which require division), set the # boundary nodes. new_nodes[:, 0] = nodes[:, 0] new_nodes[:, -1] = nodes[:, -1] return new_nodes
python
def _elevate_nodes(nodes): r"""Degree-elevate a B |eacute| zier curves. Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n + 1}` where .. math:: \begin{align*} w_0 &= v_0 \\ w_j &= \frac{j}{n + 1} v_{j - 1} + \frac{n + 1 - j}{n + 1} v_j \\ w_{n + 1} &= v_n \end{align*} .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. Returns: numpy.ndarray: The nodes of the degree-elevated curve. """ dimension, num_nodes = np.shape(nodes) new_nodes = np.empty((dimension, num_nodes + 1), order="F") multipliers = np.arange(1, num_nodes, dtype=_FLOAT64)[np.newaxis, :] denominator = float(num_nodes) new_nodes[:, 1:-1] = ( multipliers * nodes[:, :-1] + (denominator - multipliers) * nodes[:, 1:] ) # Hold off on division until the end, to (attempt to) avoid round-off. new_nodes /= denominator # After setting the internal nodes (which require division), set the # boundary nodes. new_nodes[:, 0] = nodes[:, 0] new_nodes[:, -1] = nodes[:, -1] return new_nodes
[ "def", "_elevate_nodes", "(", "nodes", ")", ":", "dimension", ",", "num_nodes", "=", "np", ".", "shape", "(", "nodes", ")", "new_nodes", "=", "np", ".", "empty", "(", "(", "dimension", ",", "num_nodes", "+", "1", ")", ",", "order", "=", "\"F\"", ")", "multipliers", "=", "np", ".", "arange", "(", "1", ",", "num_nodes", ",", "dtype", "=", "_FLOAT64", ")", "[", "np", ".", "newaxis", ",", ":", "]", "denominator", "=", "float", "(", "num_nodes", ")", "new_nodes", "[", ":", ",", "1", ":", "-", "1", "]", "=", "(", "multipliers", "*", "nodes", "[", ":", ",", ":", "-", "1", "]", "+", "(", "denominator", "-", "multipliers", ")", "*", "nodes", "[", ":", ",", "1", ":", "]", ")", "# Hold off on division until the end, to (attempt to) avoid round-off.", "new_nodes", "/=", "denominator", "# After setting the internal nodes (which require division), set the", "# boundary nodes.", "new_nodes", "[", ":", ",", "0", "]", "=", "nodes", "[", ":", ",", "0", "]", "new_nodes", "[", ":", ",", "-", "1", "]", "=", "nodes", "[", ":", ",", "-", "1", "]", "return", "new_nodes" ]
r"""Degree-elevate a B |eacute| zier curves. Does this by converting the current nodes :math:`v_0, \ldots, v_n` to new nodes :math:`w_0, \ldots, w_{n + 1}` where .. math:: \begin{align*} w_0 &= v_0 \\ w_j &= \frac{j}{n + 1} v_{j - 1} + \frac{n + 1 - j}{n + 1} v_j \\ w_{n + 1} &= v_n \end{align*} .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a curve. Returns: numpy.ndarray: The nodes of the degree-elevated curve.
[ "r", "Degree", "-", "elevate", "a", "B", "|eacute|", "zier", "curves", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L346-L385
dhermes/bezier
src/bezier/_curve_helpers.py
de_casteljau_one_round
def de_casteljau_one_round(nodes, lambda1, lambda2): """Perform one round of de Casteljau's algorithm. .. note:: This is a helper for :func:`_specialize_curve`. It does not have a Fortran speedup because it is **only** used by a function which has a Fortran speedup. The weights are assumed to sum to one. Args: nodes (numpy.ndarray): Control points for a curve. lambda1 (float): First barycentric weight on interval. lambda2 (float): Second barycentric weight on interval. Returns: numpy.ndarray: The nodes for a "blended" curve one degree lower. """ return np.asfortranarray(lambda1 * nodes[:, :-1] + lambda2 * nodes[:, 1:])
python
def de_casteljau_one_round(nodes, lambda1, lambda2): """Perform one round of de Casteljau's algorithm. .. note:: This is a helper for :func:`_specialize_curve`. It does not have a Fortran speedup because it is **only** used by a function which has a Fortran speedup. The weights are assumed to sum to one. Args: nodes (numpy.ndarray): Control points for a curve. lambda1 (float): First barycentric weight on interval. lambda2 (float): Second barycentric weight on interval. Returns: numpy.ndarray: The nodes for a "blended" curve one degree lower. """ return np.asfortranarray(lambda1 * nodes[:, :-1] + lambda2 * nodes[:, 1:])
[ "def", "de_casteljau_one_round", "(", "nodes", ",", "lambda1", ",", "lambda2", ")", ":", "return", "np", ".", "asfortranarray", "(", "lambda1", "*", "nodes", "[", ":", ",", ":", "-", "1", "]", "+", "lambda2", "*", "nodes", "[", ":", ",", "1", ":", "]", ")" ]
Perform one round of de Casteljau's algorithm. .. note:: This is a helper for :func:`_specialize_curve`. It does not have a Fortran speedup because it is **only** used by a function which has a Fortran speedup. The weights are assumed to sum to one. Args: nodes (numpy.ndarray): Control points for a curve. lambda1 (float): First barycentric weight on interval. lambda2 (float): Second barycentric weight on interval. Returns: numpy.ndarray: The nodes for a "blended" curve one degree lower.
[ "Perform", "one", "round", "of", "de", "Casteljau", "s", "algorithm", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L388-L408
dhermes/bezier
src/bezier/_curve_helpers.py
_specialize_curve
def _specialize_curve(nodes, start, end): """Specialize a curve to a re-parameterization .. note:: This assumes the curve is degree 1 or greater but doesn't check. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control points for a curve. start (float): The start point of the interval we are specializing to. end (float): The end point of the interval we are specializing to. Returns: numpy.ndarray: The control points for the specialized curve. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-locals _, num_nodes = np.shape(nodes) # Uses start-->0, end-->1 to represent the specialization used. weights = ((1.0 - start, start), (1.0 - end, end)) partial_vals = { (0,): de_casteljau_one_round(nodes, *weights[0]), (1,): de_casteljau_one_round(nodes, *weights[1]), } for _ in six.moves.xrange(num_nodes - 2, 0, -1): new_partial = {} for key, sub_nodes in six.iteritems(partial_vals): # Our keys are ascending so we increment from the last value. for next_id in six.moves.xrange(key[-1], 1 + 1): new_key = key + (next_id,) new_partial[new_key] = de_casteljau_one_round( sub_nodes, *weights[next_id] ) partial_vals = new_partial result = np.empty(nodes.shape, order="F") for index in six.moves.xrange(num_nodes): key = (0,) * (num_nodes - index - 1) + (1,) * index result[:, [index]] = partial_vals[key] return result
python
def _specialize_curve(nodes, start, end): """Specialize a curve to a re-parameterization .. note:: This assumes the curve is degree 1 or greater but doesn't check. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control points for a curve. start (float): The start point of the interval we are specializing to. end (float): The end point of the interval we are specializing to. Returns: numpy.ndarray: The control points for the specialized curve. """ # NOTE: There is no corresponding "enable", but the disable only applies # in this lexical scope. # pylint: disable=too-many-locals _, num_nodes = np.shape(nodes) # Uses start-->0, end-->1 to represent the specialization used. weights = ((1.0 - start, start), (1.0 - end, end)) partial_vals = { (0,): de_casteljau_one_round(nodes, *weights[0]), (1,): de_casteljau_one_round(nodes, *weights[1]), } for _ in six.moves.xrange(num_nodes - 2, 0, -1): new_partial = {} for key, sub_nodes in six.iteritems(partial_vals): # Our keys are ascending so we increment from the last value. for next_id in six.moves.xrange(key[-1], 1 + 1): new_key = key + (next_id,) new_partial[new_key] = de_casteljau_one_round( sub_nodes, *weights[next_id] ) partial_vals = new_partial result = np.empty(nodes.shape, order="F") for index in six.moves.xrange(num_nodes): key = (0,) * (num_nodes - index - 1) + (1,) * index result[:, [index]] = partial_vals[key] return result
[ "def", "_specialize_curve", "(", "nodes", ",", "start", ",", "end", ")", ":", "# NOTE: There is no corresponding \"enable\", but the disable only applies", "# in this lexical scope.", "# pylint: disable=too-many-locals", "_", ",", "num_nodes", "=", "np", ".", "shape", "(", "nodes", ")", "# Uses start-->0, end-->1 to represent the specialization used.", "weights", "=", "(", "(", "1.0", "-", "start", ",", "start", ")", ",", "(", "1.0", "-", "end", ",", "end", ")", ")", "partial_vals", "=", "{", "(", "0", ",", ")", ":", "de_casteljau_one_round", "(", "nodes", ",", "*", "weights", "[", "0", "]", ")", ",", "(", "1", ",", ")", ":", "de_casteljau_one_round", "(", "nodes", ",", "*", "weights", "[", "1", "]", ")", ",", "}", "for", "_", "in", "six", ".", "moves", ".", "xrange", "(", "num_nodes", "-", "2", ",", "0", ",", "-", "1", ")", ":", "new_partial", "=", "{", "}", "for", "key", ",", "sub_nodes", "in", "six", ".", "iteritems", "(", "partial_vals", ")", ":", "# Our keys are ascending so we increment from the last value.", "for", "next_id", "in", "six", ".", "moves", ".", "xrange", "(", "key", "[", "-", "1", "]", ",", "1", "+", "1", ")", ":", "new_key", "=", "key", "+", "(", "next_id", ",", ")", "new_partial", "[", "new_key", "]", "=", "de_casteljau_one_round", "(", "sub_nodes", ",", "*", "weights", "[", "next_id", "]", ")", "partial_vals", "=", "new_partial", "result", "=", "np", ".", "empty", "(", "nodes", ".", "shape", ",", "order", "=", "\"F\"", ")", "for", "index", "in", "six", ".", "moves", ".", "xrange", "(", "num_nodes", ")", ":", "key", "=", "(", "0", ",", ")", "*", "(", "num_nodes", "-", "index", "-", "1", ")", "+", "(", "1", ",", ")", "*", "index", "result", "[", ":", ",", "[", "index", "]", "]", "=", "partial_vals", "[", "key", "]", "return", "result" ]
Specialize a curve to a re-parameterization .. note:: This assumes the curve is degree 1 or greater but doesn't check. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): Control points for a curve. start (float): The start point of the interval we are specializing to. end (float): The end point of the interval we are specializing to. Returns: numpy.ndarray: The control points for the specialized curve.
[ "Specialize", "a", "curve", "to", "a", "re", "-", "parameterization" ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L411-L455
dhermes/bezier
src/bezier/_curve_helpers.py
_evaluate_hodograph
def _evaluate_hodograph(s, nodes): r"""Evaluate the Hodograph curve at a point :math:`s`. The Hodograph (first derivative) of a B |eacute| zier curve degree :math:`d = n - 1` and is given by .. math:: B'(s) = n \sum_{j = 0}^{d} \binom{d}{j} s^j (1 - s)^{d - j} \cdot \Delta v_j where each forward difference is given by :math:`\Delta v_j = v_{j + 1} - v_j`. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes of a curve. s (float): A parameter along the curve at which the Hodograph is to be evaluated. Returns: numpy.ndarray: The point on the Hodograph curve (as a two dimensional NumPy array with a single row). """ _, num_nodes = np.shape(nodes) first_deriv = nodes[:, 1:] - nodes[:, :-1] return (num_nodes - 1) * evaluate_multi( first_deriv, np.asfortranarray([s]) )
python
def _evaluate_hodograph(s, nodes): r"""Evaluate the Hodograph curve at a point :math:`s`. The Hodograph (first derivative) of a B |eacute| zier curve degree :math:`d = n - 1` and is given by .. math:: B'(s) = n \sum_{j = 0}^{d} \binom{d}{j} s^j (1 - s)^{d - j} \cdot \Delta v_j where each forward difference is given by :math:`\Delta v_j = v_{j + 1} - v_j`. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes of a curve. s (float): A parameter along the curve at which the Hodograph is to be evaluated. Returns: numpy.ndarray: The point on the Hodograph curve (as a two dimensional NumPy array with a single row). """ _, num_nodes = np.shape(nodes) first_deriv = nodes[:, 1:] - nodes[:, :-1] return (num_nodes - 1) * evaluate_multi( first_deriv, np.asfortranarray([s]) )
[ "def", "_evaluate_hodograph", "(", "s", ",", "nodes", ")", ":", "_", ",", "num_nodes", "=", "np", ".", "shape", "(", "nodes", ")", "first_deriv", "=", "nodes", "[", ":", ",", "1", ":", "]", "-", "nodes", "[", ":", ",", ":", "-", "1", "]", "return", "(", "num_nodes", "-", "1", ")", "*", "evaluate_multi", "(", "first_deriv", ",", "np", ".", "asfortranarray", "(", "[", "s", "]", ")", ")" ]
r"""Evaluate the Hodograph curve at a point :math:`s`. The Hodograph (first derivative) of a B |eacute| zier curve degree :math:`d = n - 1` and is given by .. math:: B'(s) = n \sum_{j = 0}^{d} \binom{d}{j} s^j (1 - s)^{d - j} \cdot \Delta v_j where each forward difference is given by :math:`\Delta v_j = v_{j + 1} - v_j`. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes of a curve. s (float): A parameter along the curve at which the Hodograph is to be evaluated. Returns: numpy.ndarray: The point on the Hodograph curve (as a two dimensional NumPy array with a single row).
[ "r", "Evaluate", "the", "Hodograph", "curve", "at", "a", "point", ":", "math", ":", "s", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L458-L490
dhermes/bezier
src/bezier/_curve_helpers.py
_get_curvature
def _get_curvature(nodes, tangent_vec, s): r"""Compute the signed curvature of a curve at :math:`s`. Computed via .. math:: \frac{B'(s) \times B''(s)}{\left\lVert B'(s) \right\rVert_2^3} .. image:: ../images/get_curvature.png :align: center .. testsetup:: get-curvature import numpy as np import bezier from bezier._curve_helpers import evaluate_hodograph from bezier._curve_helpers import get_curvature .. doctest:: get-curvature :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [1.0, 0.75, 0.5, 0.25, 0.0], ... [0.0, 2.0 , -2.0, 2.0 , 0.0], ... ]) >>> s = 0.5 >>> tangent_vec = evaluate_hodograph(s, nodes) >>> tangent_vec array([[-1.], [ 0.]]) >>> curvature = get_curvature(nodes, tangent_vec, s) >>> curvature -12.0 .. testcleanup:: get-curvature import make_images make_images.get_curvature(nodes, s, tangent_vec, curvature) .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes of a curve. tangent_vec (numpy.ndarray): The already computed value of :math:`B'(s)` s (float): The parameter value along the curve. Returns: float: The signed curvature. """ _, num_nodes = np.shape(nodes) if num_nodes == 2: # Lines have no curvature. return 0.0 # NOTE: We somewhat replicate code in ``evaluate_hodograph()`` here. first_deriv = nodes[:, 1:] - nodes[:, :-1] second_deriv = first_deriv[:, 1:] - first_deriv[:, :-1] concavity = ( (num_nodes - 1) * (num_nodes - 2) * evaluate_multi(second_deriv, np.asfortranarray([s])) ) curvature = _helpers.cross_product( tangent_vec.ravel(order="F"), concavity.ravel(order="F") ) # NOTE: We convert to 1D to make sure NumPy uses vector norm. curvature /= np.linalg.norm(tangent_vec[:, 0], ord=2) ** 3 return curvature
python
def _get_curvature(nodes, tangent_vec, s): r"""Compute the signed curvature of a curve at :math:`s`. Computed via .. math:: \frac{B'(s) \times B''(s)}{\left\lVert B'(s) \right\rVert_2^3} .. image:: ../images/get_curvature.png :align: center .. testsetup:: get-curvature import numpy as np import bezier from bezier._curve_helpers import evaluate_hodograph from bezier._curve_helpers import get_curvature .. doctest:: get-curvature :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [1.0, 0.75, 0.5, 0.25, 0.0], ... [0.0, 2.0 , -2.0, 2.0 , 0.0], ... ]) >>> s = 0.5 >>> tangent_vec = evaluate_hodograph(s, nodes) >>> tangent_vec array([[-1.], [ 0.]]) >>> curvature = get_curvature(nodes, tangent_vec, s) >>> curvature -12.0 .. testcleanup:: get-curvature import make_images make_images.get_curvature(nodes, s, tangent_vec, curvature) .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes of a curve. tangent_vec (numpy.ndarray): The already computed value of :math:`B'(s)` s (float): The parameter value along the curve. Returns: float: The signed curvature. """ _, num_nodes = np.shape(nodes) if num_nodes == 2: # Lines have no curvature. return 0.0 # NOTE: We somewhat replicate code in ``evaluate_hodograph()`` here. first_deriv = nodes[:, 1:] - nodes[:, :-1] second_deriv = first_deriv[:, 1:] - first_deriv[:, :-1] concavity = ( (num_nodes - 1) * (num_nodes - 2) * evaluate_multi(second_deriv, np.asfortranarray([s])) ) curvature = _helpers.cross_product( tangent_vec.ravel(order="F"), concavity.ravel(order="F") ) # NOTE: We convert to 1D to make sure NumPy uses vector norm. curvature /= np.linalg.norm(tangent_vec[:, 0], ord=2) ** 3 return curvature
[ "def", "_get_curvature", "(", "nodes", ",", "tangent_vec", ",", "s", ")", ":", "_", ",", "num_nodes", "=", "np", ".", "shape", "(", "nodes", ")", "if", "num_nodes", "==", "2", ":", "# Lines have no curvature.", "return", "0.0", "# NOTE: We somewhat replicate code in ``evaluate_hodograph()`` here.", "first_deriv", "=", "nodes", "[", ":", ",", "1", ":", "]", "-", "nodes", "[", ":", ",", ":", "-", "1", "]", "second_deriv", "=", "first_deriv", "[", ":", ",", "1", ":", "]", "-", "first_deriv", "[", ":", ",", ":", "-", "1", "]", "concavity", "=", "(", "(", "num_nodes", "-", "1", ")", "*", "(", "num_nodes", "-", "2", ")", "*", "evaluate_multi", "(", "second_deriv", ",", "np", ".", "asfortranarray", "(", "[", "s", "]", ")", ")", ")", "curvature", "=", "_helpers", ".", "cross_product", "(", "tangent_vec", ".", "ravel", "(", "order", "=", "\"F\"", ")", ",", "concavity", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", "# NOTE: We convert to 1D to make sure NumPy uses vector norm.", "curvature", "/=", "np", ".", "linalg", ".", "norm", "(", "tangent_vec", "[", ":", ",", "0", "]", ",", "ord", "=", "2", ")", "**", "3", "return", "curvature" ]
r"""Compute the signed curvature of a curve at :math:`s`. Computed via .. math:: \frac{B'(s) \times B''(s)}{\left\lVert B'(s) \right\rVert_2^3} .. image:: ../images/get_curvature.png :align: center .. testsetup:: get-curvature import numpy as np import bezier from bezier._curve_helpers import evaluate_hodograph from bezier._curve_helpers import get_curvature .. doctest:: get-curvature :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [1.0, 0.75, 0.5, 0.25, 0.0], ... [0.0, 2.0 , -2.0, 2.0 , 0.0], ... ]) >>> s = 0.5 >>> tangent_vec = evaluate_hodograph(s, nodes) >>> tangent_vec array([[-1.], [ 0.]]) >>> curvature = get_curvature(nodes, tangent_vec, s) >>> curvature -12.0 .. testcleanup:: get-curvature import make_images make_images.get_curvature(nodes, s, tangent_vec, curvature) .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes of a curve. tangent_vec (numpy.ndarray): The already computed value of :math:`B'(s)` s (float): The parameter value along the curve. Returns: float: The signed curvature.
[ "r", "Compute", "the", "signed", "curvature", "of", "a", "curve", "at", ":", "math", ":", "s", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L493-L564
dhermes/bezier
src/bezier/_curve_helpers.py
_newton_refine
def _newton_refine(nodes, point, s): r"""Refine a solution to :math:`B(s) = p` using Newton's method. Computes updates via .. math:: \mathbf{0} \approx \left(B\left(s_{\ast}\right) - p\right) + B'\left(s_{\ast}\right) \Delta s For example, consider the curve .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^2 + \left[\begin{array}{c} 1 \\ 2 \end{array}\right] 2 (1 - s) s + \left[\begin{array}{c} 3 \\ 1 \end{array}\right] s^2 and the point :math:`B\left(\frac{1}{4}\right) = \frac{1}{16} \left[\begin{array}{c} 9 \\ 13 \end{array}\right]`. Starting from the **wrong** point :math:`s = \frac{3}{4}`, we have .. math:: \begin{align*} p - B\left(\frac{1}{2}\right) &= -\frac{1}{2} \left[\begin{array}{c} 3 \\ 1 \end{array}\right] \\ B'\left(\frac{1}{2}\right) &= \frac{1}{2} \left[\begin{array}{c} 7 \\ -1 \end{array}\right] \\ \Longrightarrow \frac{1}{4} \left[\begin{array}{c c} 7 & -1 \end{array}\right] \left[\begin{array}{c} 7 \\ -1 \end{array}\right] \Delta s &= -\frac{1}{4} \left[\begin{array}{c c} 7 & -1 \end{array}\right] \left[\begin{array}{c} 3 \\ 1 \end{array}\right] \\ \Longrightarrow \Delta s &= -\frac{2}{5} \end{align*} .. image:: ../images/newton_refine_curve.png :align: center .. testsetup:: newton-refine-curve, newton-refine-curve-cusp import numpy as np import bezier from bezier._curve_helpers import newton_refine .. doctest:: newton-refine-curve :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.0, 3.0], ... [0.0, 2.0, 1.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> point = curve.evaluate(0.25) >>> point array([[0.5625], [0.8125]]) >>> s = 0.75 >>> new_s = newton_refine(nodes, point, s) >>> 5 * (new_s - s) -2.0 .. testcleanup:: newton-refine-curve import make_images make_images.newton_refine_curve(curve, point, s, new_s) On curves that are not "valid" (i.e. :math:`B(s)` is not injective with non-zero gradient), Newton's method may break down and converge linearly: .. image:: ../images/newton_refine_curve_cusp.png :align: center .. doctest:: newton-refine-curve-cusp :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [ 6.0, -2.0, -2.0, 6.0], ... [-3.0, 3.0, -3.0, 3.0], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> expected = 0.5 >>> point = curve.evaluate(expected) >>> point array([[0.], [0.]]) >>> s_vals = [0.625, None, None, None, None, None] >>> np.log2(abs(expected - s_vals[0])) -3.0 >>> s_vals[1] = newton_refine(nodes, point, s_vals[0]) >>> np.log2(abs(expected - s_vals[1])) -3.983... >>> s_vals[2] = newton_refine(nodes, point, s_vals[1]) >>> np.log2(abs(expected - s_vals[2])) -4.979... >>> s_vals[3] = newton_refine(nodes, point, s_vals[2]) >>> np.log2(abs(expected - s_vals[3])) -5.978... >>> s_vals[4] = newton_refine(nodes, point, s_vals[3]) >>> np.log2(abs(expected - s_vals[4])) -6.978... >>> s_vals[5] = newton_refine(nodes, point, s_vals[4]) >>> np.log2(abs(expected - s_vals[5])) -7.978... .. testcleanup:: newton-refine-curve-cusp import make_images make_images.newton_refine_curve_cusp(curve, s_vals) Due to round-off, the Newton process terminates with an error that is not close to machine precision :math:`\varepsilon` when :math:`\Delta s = 0`. .. testsetup:: newton-refine-curve-cusp-continued import numpy as np import bezier from bezier._curve_helpers import newton_refine nodes = np.asfortranarray([ [ 6.0, -2.0, -2.0, 6.0], [-3.0, 3.0, -3.0, 3.0], ]) curve = bezier.Curve(nodes, degree=3) point = curve.evaluate(0.5) .. doctest:: newton-refine-curve-cusp-continued >>> s_vals = [0.625] >>> new_s = newton_refine(nodes, point, s_vals[-1]) >>> while new_s not in s_vals: ... s_vals.append(new_s) ... new_s = newton_refine(nodes, point, s_vals[-1]) ... >>> terminal_s = s_vals[-1] >>> terminal_s == newton_refine(nodes, point, terminal_s) True >>> 2.0**(-31) <= abs(terminal_s - 0.5) <= 2.0**(-28) True Due to round-off near the cusp, the final error resembles :math:`\sqrt{\varepsilon}` rather than machine precision as expected. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. point (numpy.ndarray): A point on the curve. s (float): An "almost" solution to :math:`B(s) = p`. Returns: float: The updated value :math:`s + \Delta s`. """ pt_delta = point - evaluate_multi(nodes, np.asfortranarray([s])) derivative = evaluate_hodograph(s, nodes) # Each array is 2 x 1 (i.e. a column vector), we want the vector # dot product. delta_s = np.vdot(pt_delta[:, 0], derivative[:, 0]) / np.vdot( derivative[:, 0], derivative[:, 0] ) return s + delta_s
python
def _newton_refine(nodes, point, s): r"""Refine a solution to :math:`B(s) = p` using Newton's method. Computes updates via .. math:: \mathbf{0} \approx \left(B\left(s_{\ast}\right) - p\right) + B'\left(s_{\ast}\right) \Delta s For example, consider the curve .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^2 + \left[\begin{array}{c} 1 \\ 2 \end{array}\right] 2 (1 - s) s + \left[\begin{array}{c} 3 \\ 1 \end{array}\right] s^2 and the point :math:`B\left(\frac{1}{4}\right) = \frac{1}{16} \left[\begin{array}{c} 9 \\ 13 \end{array}\right]`. Starting from the **wrong** point :math:`s = \frac{3}{4}`, we have .. math:: \begin{align*} p - B\left(\frac{1}{2}\right) &= -\frac{1}{2} \left[\begin{array}{c} 3 \\ 1 \end{array}\right] \\ B'\left(\frac{1}{2}\right) &= \frac{1}{2} \left[\begin{array}{c} 7 \\ -1 \end{array}\right] \\ \Longrightarrow \frac{1}{4} \left[\begin{array}{c c} 7 & -1 \end{array}\right] \left[\begin{array}{c} 7 \\ -1 \end{array}\right] \Delta s &= -\frac{1}{4} \left[\begin{array}{c c} 7 & -1 \end{array}\right] \left[\begin{array}{c} 3 \\ 1 \end{array}\right] \\ \Longrightarrow \Delta s &= -\frac{2}{5} \end{align*} .. image:: ../images/newton_refine_curve.png :align: center .. testsetup:: newton-refine-curve, newton-refine-curve-cusp import numpy as np import bezier from bezier._curve_helpers import newton_refine .. doctest:: newton-refine-curve :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.0, 3.0], ... [0.0, 2.0, 1.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> point = curve.evaluate(0.25) >>> point array([[0.5625], [0.8125]]) >>> s = 0.75 >>> new_s = newton_refine(nodes, point, s) >>> 5 * (new_s - s) -2.0 .. testcleanup:: newton-refine-curve import make_images make_images.newton_refine_curve(curve, point, s, new_s) On curves that are not "valid" (i.e. :math:`B(s)` is not injective with non-zero gradient), Newton's method may break down and converge linearly: .. image:: ../images/newton_refine_curve_cusp.png :align: center .. doctest:: newton-refine-curve-cusp :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [ 6.0, -2.0, -2.0, 6.0], ... [-3.0, 3.0, -3.0, 3.0], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> expected = 0.5 >>> point = curve.evaluate(expected) >>> point array([[0.], [0.]]) >>> s_vals = [0.625, None, None, None, None, None] >>> np.log2(abs(expected - s_vals[0])) -3.0 >>> s_vals[1] = newton_refine(nodes, point, s_vals[0]) >>> np.log2(abs(expected - s_vals[1])) -3.983... >>> s_vals[2] = newton_refine(nodes, point, s_vals[1]) >>> np.log2(abs(expected - s_vals[2])) -4.979... >>> s_vals[3] = newton_refine(nodes, point, s_vals[2]) >>> np.log2(abs(expected - s_vals[3])) -5.978... >>> s_vals[4] = newton_refine(nodes, point, s_vals[3]) >>> np.log2(abs(expected - s_vals[4])) -6.978... >>> s_vals[5] = newton_refine(nodes, point, s_vals[4]) >>> np.log2(abs(expected - s_vals[5])) -7.978... .. testcleanup:: newton-refine-curve-cusp import make_images make_images.newton_refine_curve_cusp(curve, s_vals) Due to round-off, the Newton process terminates with an error that is not close to machine precision :math:`\varepsilon` when :math:`\Delta s = 0`. .. testsetup:: newton-refine-curve-cusp-continued import numpy as np import bezier from bezier._curve_helpers import newton_refine nodes = np.asfortranarray([ [ 6.0, -2.0, -2.0, 6.0], [-3.0, 3.0, -3.0, 3.0], ]) curve = bezier.Curve(nodes, degree=3) point = curve.evaluate(0.5) .. doctest:: newton-refine-curve-cusp-continued >>> s_vals = [0.625] >>> new_s = newton_refine(nodes, point, s_vals[-1]) >>> while new_s not in s_vals: ... s_vals.append(new_s) ... new_s = newton_refine(nodes, point, s_vals[-1]) ... >>> terminal_s = s_vals[-1] >>> terminal_s == newton_refine(nodes, point, terminal_s) True >>> 2.0**(-31) <= abs(terminal_s - 0.5) <= 2.0**(-28) True Due to round-off near the cusp, the final error resembles :math:`\sqrt{\varepsilon}` rather than machine precision as expected. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. point (numpy.ndarray): A point on the curve. s (float): An "almost" solution to :math:`B(s) = p`. Returns: float: The updated value :math:`s + \Delta s`. """ pt_delta = point - evaluate_multi(nodes, np.asfortranarray([s])) derivative = evaluate_hodograph(s, nodes) # Each array is 2 x 1 (i.e. a column vector), we want the vector # dot product. delta_s = np.vdot(pt_delta[:, 0], derivative[:, 0]) / np.vdot( derivative[:, 0], derivative[:, 0] ) return s + delta_s
[ "def", "_newton_refine", "(", "nodes", ",", "point", ",", "s", ")", ":", "pt_delta", "=", "point", "-", "evaluate_multi", "(", "nodes", ",", "np", ".", "asfortranarray", "(", "[", "s", "]", ")", ")", "derivative", "=", "evaluate_hodograph", "(", "s", ",", "nodes", ")", "# Each array is 2 x 1 (i.e. a column vector), we want the vector", "# dot product.", "delta_s", "=", "np", ".", "vdot", "(", "pt_delta", "[", ":", ",", "0", "]", ",", "derivative", "[", ":", ",", "0", "]", ")", "/", "np", ".", "vdot", "(", "derivative", "[", ":", ",", "0", "]", ",", "derivative", "[", ":", ",", "0", "]", ")", "return", "s", "+", "delta_s" ]
r"""Refine a solution to :math:`B(s) = p` using Newton's method. Computes updates via .. math:: \mathbf{0} \approx \left(B\left(s_{\ast}\right) - p\right) + B'\left(s_{\ast}\right) \Delta s For example, consider the curve .. math:: B(s) = \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^2 + \left[\begin{array}{c} 1 \\ 2 \end{array}\right] 2 (1 - s) s + \left[\begin{array}{c} 3 \\ 1 \end{array}\right] s^2 and the point :math:`B\left(\frac{1}{4}\right) = \frac{1}{16} \left[\begin{array}{c} 9 \\ 13 \end{array}\right]`. Starting from the **wrong** point :math:`s = \frac{3}{4}`, we have .. math:: \begin{align*} p - B\left(\frac{1}{2}\right) &= -\frac{1}{2} \left[\begin{array}{c} 3 \\ 1 \end{array}\right] \\ B'\left(\frac{1}{2}\right) &= \frac{1}{2} \left[\begin{array}{c} 7 \\ -1 \end{array}\right] \\ \Longrightarrow \frac{1}{4} \left[\begin{array}{c c} 7 & -1 \end{array}\right] \left[\begin{array}{c} 7 \\ -1 \end{array}\right] \Delta s &= -\frac{1}{4} \left[\begin{array}{c c} 7 & -1 \end{array}\right] \left[\begin{array}{c} 3 \\ 1 \end{array}\right] \\ \Longrightarrow \Delta s &= -\frac{2}{5} \end{align*} .. image:: ../images/newton_refine_curve.png :align: center .. testsetup:: newton-refine-curve, newton-refine-curve-cusp import numpy as np import bezier from bezier._curve_helpers import newton_refine .. doctest:: newton-refine-curve :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [0.0, 1.0, 3.0], ... [0.0, 2.0, 1.0], ... ]) >>> curve = bezier.Curve(nodes, degree=2) >>> point = curve.evaluate(0.25) >>> point array([[0.5625], [0.8125]]) >>> s = 0.75 >>> new_s = newton_refine(nodes, point, s) >>> 5 * (new_s - s) -2.0 .. testcleanup:: newton-refine-curve import make_images make_images.newton_refine_curve(curve, point, s, new_s) On curves that are not "valid" (i.e. :math:`B(s)` is not injective with non-zero gradient), Newton's method may break down and converge linearly: .. image:: ../images/newton_refine_curve_cusp.png :align: center .. doctest:: newton-refine-curve-cusp :options: +NORMALIZE_WHITESPACE >>> nodes = np.asfortranarray([ ... [ 6.0, -2.0, -2.0, 6.0], ... [-3.0, 3.0, -3.0, 3.0], ... ]) >>> curve = bezier.Curve(nodes, degree=3) >>> expected = 0.5 >>> point = curve.evaluate(expected) >>> point array([[0.], [0.]]) >>> s_vals = [0.625, None, None, None, None, None] >>> np.log2(abs(expected - s_vals[0])) -3.0 >>> s_vals[1] = newton_refine(nodes, point, s_vals[0]) >>> np.log2(abs(expected - s_vals[1])) -3.983... >>> s_vals[2] = newton_refine(nodes, point, s_vals[1]) >>> np.log2(abs(expected - s_vals[2])) -4.979... >>> s_vals[3] = newton_refine(nodes, point, s_vals[2]) >>> np.log2(abs(expected - s_vals[3])) -5.978... >>> s_vals[4] = newton_refine(nodes, point, s_vals[3]) >>> np.log2(abs(expected - s_vals[4])) -6.978... >>> s_vals[5] = newton_refine(nodes, point, s_vals[4]) >>> np.log2(abs(expected - s_vals[5])) -7.978... .. testcleanup:: newton-refine-curve-cusp import make_images make_images.newton_refine_curve_cusp(curve, s_vals) Due to round-off, the Newton process terminates with an error that is not close to machine precision :math:`\varepsilon` when :math:`\Delta s = 0`. .. testsetup:: newton-refine-curve-cusp-continued import numpy as np import bezier from bezier._curve_helpers import newton_refine nodes = np.asfortranarray([ [ 6.0, -2.0, -2.0, 6.0], [-3.0, 3.0, -3.0, 3.0], ]) curve = bezier.Curve(nodes, degree=3) point = curve.evaluate(0.5) .. doctest:: newton-refine-curve-cusp-continued >>> s_vals = [0.625] >>> new_s = newton_refine(nodes, point, s_vals[-1]) >>> while new_s not in s_vals: ... s_vals.append(new_s) ... new_s = newton_refine(nodes, point, s_vals[-1]) ... >>> terminal_s = s_vals[-1] >>> terminal_s == newton_refine(nodes, point, terminal_s) True >>> 2.0**(-31) <= abs(terminal_s - 0.5) <= 2.0**(-28) True Due to round-off near the cusp, the final error resembles :math:`\sqrt{\varepsilon}` rather than machine precision as expected. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. point (numpy.ndarray): A point on the curve. s (float): An "almost" solution to :math:`B(s) = p`. Returns: float: The updated value :math:`s + \Delta s`.
[ "r", "Refine", "a", "solution", "to", ":", "math", ":", "B", "(", "s", ")", "=", "p", "using", "Newton", "s", "method", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L567-L737
dhermes/bezier
src/bezier/_curve_helpers.py
_locate_point
def _locate_point(nodes, point): r"""Locate a point on a curve. Does so by recursively subdividing the curve and rejecting sub-curves with bounding boxes that don't contain the point. After the sub-curves are sufficiently small, uses Newton's method to zoom in on the parameter value. .. note:: This assumes, but does not check, that ``point`` is ``D x 1``, where ``D`` is the dimension that ``curve`` is in. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. point (numpy.ndarray): The point to locate. Returns: Optional[float]: The parameter value (:math:`s`) corresponding to ``point`` or :data:`None` if the point is not on the ``curve``. Raises: ValueError: If the standard deviation of the remaining start / end parameters among the subdivided intervals exceeds a given threshold (e.g. :math:`2^{-20}`). """ candidates = [(0.0, 1.0, nodes)] for _ in six.moves.xrange(_MAX_LOCATE_SUBDIVISIONS + 1): next_candidates = [] for start, end, candidate in candidates: if _helpers.contains_nd(candidate, point.ravel(order="F")): midpoint = 0.5 * (start + end) left, right = subdivide_nodes(candidate) next_candidates.extend( ((start, midpoint, left), (midpoint, end, right)) ) candidates = next_candidates if not candidates: return None params = [(start, end) for start, end, _ in candidates] if np.std(params) > _LOCATE_STD_CAP: raise ValueError("Parameters not close enough to one another", params) s_approx = np.mean(params) s_approx = newton_refine(nodes, point, s_approx) # NOTE: Since ``np.mean(params)`` must be in ``[0, 1]`` it's # "safe" to push the Newton-refined value back into the unit # interval. if s_approx < 0.0: return 0.0 elif s_approx > 1.0: return 1.0 else: return s_approx
python
def _locate_point(nodes, point): r"""Locate a point on a curve. Does so by recursively subdividing the curve and rejecting sub-curves with bounding boxes that don't contain the point. After the sub-curves are sufficiently small, uses Newton's method to zoom in on the parameter value. .. note:: This assumes, but does not check, that ``point`` is ``D x 1``, where ``D`` is the dimension that ``curve`` is in. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. point (numpy.ndarray): The point to locate. Returns: Optional[float]: The parameter value (:math:`s`) corresponding to ``point`` or :data:`None` if the point is not on the ``curve``. Raises: ValueError: If the standard deviation of the remaining start / end parameters among the subdivided intervals exceeds a given threshold (e.g. :math:`2^{-20}`). """ candidates = [(0.0, 1.0, nodes)] for _ in six.moves.xrange(_MAX_LOCATE_SUBDIVISIONS + 1): next_candidates = [] for start, end, candidate in candidates: if _helpers.contains_nd(candidate, point.ravel(order="F")): midpoint = 0.5 * (start + end) left, right = subdivide_nodes(candidate) next_candidates.extend( ((start, midpoint, left), (midpoint, end, right)) ) candidates = next_candidates if not candidates: return None params = [(start, end) for start, end, _ in candidates] if np.std(params) > _LOCATE_STD_CAP: raise ValueError("Parameters not close enough to one another", params) s_approx = np.mean(params) s_approx = newton_refine(nodes, point, s_approx) # NOTE: Since ``np.mean(params)`` must be in ``[0, 1]`` it's # "safe" to push the Newton-refined value back into the unit # interval. if s_approx < 0.0: return 0.0 elif s_approx > 1.0: return 1.0 else: return s_approx
[ "def", "_locate_point", "(", "nodes", ",", "point", ")", ":", "candidates", "=", "[", "(", "0.0", ",", "1.0", ",", "nodes", ")", "]", "for", "_", "in", "six", ".", "moves", ".", "xrange", "(", "_MAX_LOCATE_SUBDIVISIONS", "+", "1", ")", ":", "next_candidates", "=", "[", "]", "for", "start", ",", "end", ",", "candidate", "in", "candidates", ":", "if", "_helpers", ".", "contains_nd", "(", "candidate", ",", "point", ".", "ravel", "(", "order", "=", "\"F\"", ")", ")", ":", "midpoint", "=", "0.5", "*", "(", "start", "+", "end", ")", "left", ",", "right", "=", "subdivide_nodes", "(", "candidate", ")", "next_candidates", ".", "extend", "(", "(", "(", "start", ",", "midpoint", ",", "left", ")", ",", "(", "midpoint", ",", "end", ",", "right", ")", ")", ")", "candidates", "=", "next_candidates", "if", "not", "candidates", ":", "return", "None", "params", "=", "[", "(", "start", ",", "end", ")", "for", "start", ",", "end", ",", "_", "in", "candidates", "]", "if", "np", ".", "std", "(", "params", ")", ">", "_LOCATE_STD_CAP", ":", "raise", "ValueError", "(", "\"Parameters not close enough to one another\"", ",", "params", ")", "s_approx", "=", "np", ".", "mean", "(", "params", ")", "s_approx", "=", "newton_refine", "(", "nodes", ",", "point", ",", "s_approx", ")", "# NOTE: Since ``np.mean(params)`` must be in ``[0, 1]`` it's", "# \"safe\" to push the Newton-refined value back into the unit", "# interval.", "if", "s_approx", "<", "0.0", ":", "return", "0.0", "elif", "s_approx", ">", "1.0", ":", "return", "1.0", "else", ":", "return", "s_approx" ]
r"""Locate a point on a curve. Does so by recursively subdividing the curve and rejecting sub-curves with bounding boxes that don't contain the point. After the sub-curves are sufficiently small, uses Newton's method to zoom in on the parameter value. .. note:: This assumes, but does not check, that ``point`` is ``D x 1``, where ``D`` is the dimension that ``curve`` is in. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes defining a B |eacute| zier curve. point (numpy.ndarray): The point to locate. Returns: Optional[float]: The parameter value (:math:`s`) corresponding to ``point`` or :data:`None` if the point is not on the ``curve``. Raises: ValueError: If the standard deviation of the remaining start / end parameters among the subdivided intervals exceeds a given threshold (e.g. :math:`2^{-20}`).
[ "r", "Locate", "a", "point", "on", "a", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L740-L801
dhermes/bezier
src/bezier/_curve_helpers.py
_reduce_pseudo_inverse
def _reduce_pseudo_inverse(nodes): """Performs degree-reduction for a B |eacute| zier curve. Does so by using the pseudo-inverse of the degree elevation operator (which is overdetermined). .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: numpy.ndarray: The reduced nodes. Raises: .UnsupportedDegree: If the degree is not 1, 2, 3 or 4. """ _, num_nodes = np.shape(nodes) if num_nodes == 2: reduction = _REDUCTION0 denom = _REDUCTION_DENOM0 elif num_nodes == 3: reduction = _REDUCTION1 denom = _REDUCTION_DENOM1 elif num_nodes == 4: reduction = _REDUCTION2 denom = _REDUCTION_DENOM2 elif num_nodes == 5: reduction = _REDUCTION3 denom = _REDUCTION_DENOM3 else: raise _helpers.UnsupportedDegree(num_nodes - 1, supported=(1, 2, 3, 4)) result = _helpers.matrix_product(nodes, reduction) result /= denom return result
python
def _reduce_pseudo_inverse(nodes): """Performs degree-reduction for a B |eacute| zier curve. Does so by using the pseudo-inverse of the degree elevation operator (which is overdetermined). .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: numpy.ndarray: The reduced nodes. Raises: .UnsupportedDegree: If the degree is not 1, 2, 3 or 4. """ _, num_nodes = np.shape(nodes) if num_nodes == 2: reduction = _REDUCTION0 denom = _REDUCTION_DENOM0 elif num_nodes == 3: reduction = _REDUCTION1 denom = _REDUCTION_DENOM1 elif num_nodes == 4: reduction = _REDUCTION2 denom = _REDUCTION_DENOM2 elif num_nodes == 5: reduction = _REDUCTION3 denom = _REDUCTION_DENOM3 else: raise _helpers.UnsupportedDegree(num_nodes - 1, supported=(1, 2, 3, 4)) result = _helpers.matrix_product(nodes, reduction) result /= denom return result
[ "def", "_reduce_pseudo_inverse", "(", "nodes", ")", ":", "_", ",", "num_nodes", "=", "np", ".", "shape", "(", "nodes", ")", "if", "num_nodes", "==", "2", ":", "reduction", "=", "_REDUCTION0", "denom", "=", "_REDUCTION_DENOM0", "elif", "num_nodes", "==", "3", ":", "reduction", "=", "_REDUCTION1", "denom", "=", "_REDUCTION_DENOM1", "elif", "num_nodes", "==", "4", ":", "reduction", "=", "_REDUCTION2", "denom", "=", "_REDUCTION_DENOM2", "elif", "num_nodes", "==", "5", ":", "reduction", "=", "_REDUCTION3", "denom", "=", "_REDUCTION_DENOM3", "else", ":", "raise", "_helpers", ".", "UnsupportedDegree", "(", "num_nodes", "-", "1", ",", "supported", "=", "(", "1", ",", "2", ",", "3", ",", "4", ")", ")", "result", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "reduction", ")", "result", "/=", "denom", "return", "result" ]
Performs degree-reduction for a B |eacute| zier curve. Does so by using the pseudo-inverse of the degree elevation operator (which is overdetermined). .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: numpy.ndarray: The reduced nodes. Raises: .UnsupportedDegree: If the degree is not 1, 2, 3 or 4.
[ "Performs", "degree", "-", "reduction", "for", "a", "B", "|eacute|", "zier", "curve", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L804-L842
dhermes/bezier
src/bezier/_curve_helpers.py
projection_error
def projection_error(nodes, projected): """Compute the error between ``nodes`` and the projected nodes. .. note:: This is a helper for :func:`maybe_reduce`, which is in turn a helper for :func:`_full_reduce`. Hence there is no corresponding Fortran speedup. For now, just compute the relative error in the Frobenius norm. But, we may wish to consider the error per row / point instead. Args: nodes (numpy.ndarray): Nodes in a curve. projected (numpy.ndarray): The ``nodes`` projected into the space of degree-elevated nodes. Returns: float: The relative error. """ relative_err = np.linalg.norm(nodes - projected, ord="fro") if relative_err != 0.0: relative_err /= np.linalg.norm(nodes, ord="fro") return relative_err
python
def projection_error(nodes, projected): """Compute the error between ``nodes`` and the projected nodes. .. note:: This is a helper for :func:`maybe_reduce`, which is in turn a helper for :func:`_full_reduce`. Hence there is no corresponding Fortran speedup. For now, just compute the relative error in the Frobenius norm. But, we may wish to consider the error per row / point instead. Args: nodes (numpy.ndarray): Nodes in a curve. projected (numpy.ndarray): The ``nodes`` projected into the space of degree-elevated nodes. Returns: float: The relative error. """ relative_err = np.linalg.norm(nodes - projected, ord="fro") if relative_err != 0.0: relative_err /= np.linalg.norm(nodes, ord="fro") return relative_err
[ "def", "projection_error", "(", "nodes", ",", "projected", ")", ":", "relative_err", "=", "np", ".", "linalg", ".", "norm", "(", "nodes", "-", "projected", ",", "ord", "=", "\"fro\"", ")", "if", "relative_err", "!=", "0.0", ":", "relative_err", "/=", "np", ".", "linalg", ".", "norm", "(", "nodes", ",", "ord", "=", "\"fro\"", ")", "return", "relative_err" ]
Compute the error between ``nodes`` and the projected nodes. .. note:: This is a helper for :func:`maybe_reduce`, which is in turn a helper for :func:`_full_reduce`. Hence there is no corresponding Fortran speedup. For now, just compute the relative error in the Frobenius norm. But, we may wish to consider the error per row / point instead. Args: nodes (numpy.ndarray): Nodes in a curve. projected (numpy.ndarray): The ``nodes`` projected into the space of degree-elevated nodes. Returns: float: The relative error.
[ "Compute", "the", "error", "between", "nodes", "and", "the", "projected", "nodes", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L845-L868
dhermes/bezier
src/bezier/_curve_helpers.py
maybe_reduce
def maybe_reduce(nodes): r"""Reduce nodes in a curve if they are degree-elevated. .. note:: This is a helper for :func:`_full_reduce`. Hence there is no corresponding Fortran speedup. We check if the nodes are degree-elevated by projecting onto the space of degree-elevated curves of the same degree, then comparing to the projection. We form the projection by taking the corresponding (right) elevation matrix :math:`E` (from one degree lower) and forming :math:`E^T \left(E E^T\right)^{-1} E`. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: Tuple[bool, numpy.ndarray]: Pair of values. The first indicates if the ``nodes`` were reduced. The second is the resulting nodes, either the reduced ones or the original passed in. Raises: .UnsupportedDegree: If the curve is degree 5 or higher. """ _, num_nodes = nodes.shape if num_nodes < 2: return False, nodes elif num_nodes == 2: projection = _PROJECTION0 denom = _PROJ_DENOM0 elif num_nodes == 3: projection = _PROJECTION1 denom = _PROJ_DENOM1 elif num_nodes == 4: projection = _PROJECTION2 denom = _PROJ_DENOM2 elif num_nodes == 5: projection = _PROJECTION3 denom = _PROJ_DENOM3 else: raise _helpers.UnsupportedDegree( num_nodes - 1, supported=(0, 1, 2, 3, 4) ) projected = _helpers.matrix_product(nodes, projection) / denom relative_err = projection_error(nodes, projected) if relative_err < _REDUCE_THRESHOLD: return True, reduce_pseudo_inverse(nodes) else: return False, nodes
python
def maybe_reduce(nodes): r"""Reduce nodes in a curve if they are degree-elevated. .. note:: This is a helper for :func:`_full_reduce`. Hence there is no corresponding Fortran speedup. We check if the nodes are degree-elevated by projecting onto the space of degree-elevated curves of the same degree, then comparing to the projection. We form the projection by taking the corresponding (right) elevation matrix :math:`E` (from one degree lower) and forming :math:`E^T \left(E E^T\right)^{-1} E`. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: Tuple[bool, numpy.ndarray]: Pair of values. The first indicates if the ``nodes`` were reduced. The second is the resulting nodes, either the reduced ones or the original passed in. Raises: .UnsupportedDegree: If the curve is degree 5 or higher. """ _, num_nodes = nodes.shape if num_nodes < 2: return False, nodes elif num_nodes == 2: projection = _PROJECTION0 denom = _PROJ_DENOM0 elif num_nodes == 3: projection = _PROJECTION1 denom = _PROJ_DENOM1 elif num_nodes == 4: projection = _PROJECTION2 denom = _PROJ_DENOM2 elif num_nodes == 5: projection = _PROJECTION3 denom = _PROJ_DENOM3 else: raise _helpers.UnsupportedDegree( num_nodes - 1, supported=(0, 1, 2, 3, 4) ) projected = _helpers.matrix_product(nodes, projection) / denom relative_err = projection_error(nodes, projected) if relative_err < _REDUCE_THRESHOLD: return True, reduce_pseudo_inverse(nodes) else: return False, nodes
[ "def", "maybe_reduce", "(", "nodes", ")", ":", "_", ",", "num_nodes", "=", "nodes", ".", "shape", "if", "num_nodes", "<", "2", ":", "return", "False", ",", "nodes", "elif", "num_nodes", "==", "2", ":", "projection", "=", "_PROJECTION0", "denom", "=", "_PROJ_DENOM0", "elif", "num_nodes", "==", "3", ":", "projection", "=", "_PROJECTION1", "denom", "=", "_PROJ_DENOM1", "elif", "num_nodes", "==", "4", ":", "projection", "=", "_PROJECTION2", "denom", "=", "_PROJ_DENOM2", "elif", "num_nodes", "==", "5", ":", "projection", "=", "_PROJECTION3", "denom", "=", "_PROJ_DENOM3", "else", ":", "raise", "_helpers", ".", "UnsupportedDegree", "(", "num_nodes", "-", "1", ",", "supported", "=", "(", "0", ",", "1", ",", "2", ",", "3", ",", "4", ")", ")", "projected", "=", "_helpers", ".", "matrix_product", "(", "nodes", ",", "projection", ")", "/", "denom", "relative_err", "=", "projection_error", "(", "nodes", ",", "projected", ")", "if", "relative_err", "<", "_REDUCE_THRESHOLD", ":", "return", "True", ",", "reduce_pseudo_inverse", "(", "nodes", ")", "else", ":", "return", "False", ",", "nodes" ]
r"""Reduce nodes in a curve if they are degree-elevated. .. note:: This is a helper for :func:`_full_reduce`. Hence there is no corresponding Fortran speedup. We check if the nodes are degree-elevated by projecting onto the space of degree-elevated curves of the same degree, then comparing to the projection. We form the projection by taking the corresponding (right) elevation matrix :math:`E` (from one degree lower) and forming :math:`E^T \left(E E^T\right)^{-1} E`. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: Tuple[bool, numpy.ndarray]: Pair of values. The first indicates if the ``nodes`` were reduced. The second is the resulting nodes, either the reduced ones or the original passed in. Raises: .UnsupportedDegree: If the curve is degree 5 or higher.
[ "r", "Reduce", "nodes", "in", "a", "curve", "if", "they", "are", "degree", "-", "elevated", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L871-L923
dhermes/bezier
src/bezier/_curve_helpers.py
_full_reduce
def _full_reduce(nodes): """Apply degree reduction to ``nodes`` until it can no longer be reduced. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: numpy.ndarray: The fully degree-reduced nodes. """ was_reduced, nodes = maybe_reduce(nodes) while was_reduced: was_reduced, nodes = maybe_reduce(nodes) return nodes
python
def _full_reduce(nodes): """Apply degree reduction to ``nodes`` until it can no longer be reduced. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: numpy.ndarray: The fully degree-reduced nodes. """ was_reduced, nodes = maybe_reduce(nodes) while was_reduced: was_reduced, nodes = maybe_reduce(nodes) return nodes
[ "def", "_full_reduce", "(", "nodes", ")", ":", "was_reduced", ",", "nodes", "=", "maybe_reduce", "(", "nodes", ")", "while", "was_reduced", ":", "was_reduced", ",", "nodes", "=", "maybe_reduce", "(", "nodes", ")", "return", "nodes" ]
Apply degree reduction to ``nodes`` until it can no longer be reduced. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. Args: nodes (numpy.ndarray): The nodes in the curve. Returns: numpy.ndarray: The fully degree-reduced nodes.
[ "Apply", "degree", "reduction", "to", "nodes", "until", "it", "can", "no", "longer", "be", "reduced", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/src/bezier/_curve_helpers.py#L926-L943
dhermes/bezier
scripts/rewrite_package_rst.py
get_public_members
def get_public_members(): """Get public members in :mod:`bezier` package. Also validates the contents of ``bezier.__all__``. Returns: list: List of all public members **defined** in the main package (i.e. in ``__init__.py``). Raises: ValueError: If ``__all__`` has repeated elements. ValueError: If the "public" members in ``__init__.py`` don't match the members described in ``__all__``. """ if bezier is None: return [] local_members = [] all_members = set() for name in dir(bezier): # Filter out non-public. if name.startswith("_") and name not in SPECIAL_MEMBERS: continue value = getattr(bezier, name) # Filter out imported modules. if isinstance(value, types.ModuleType): continue all_members.add(name) # Only keep values defined in the base package. home = getattr(value, "__module__", "bezier") if home == "bezier": local_members.append(name) size_all = len(bezier.__all__) all_exports = set(bezier.__all__) if len(all_exports) != size_all: raise ValueError("__all__ has repeated elements") if all_exports != all_members: raise ValueError( "__all__ does not agree with the publicly imported members", all_exports, all_members, ) local_members = [ member for member in local_members if member not in UNDOCUMENTED_SPECIAL_MEMBERS ] return local_members
python
def get_public_members(): """Get public members in :mod:`bezier` package. Also validates the contents of ``bezier.__all__``. Returns: list: List of all public members **defined** in the main package (i.e. in ``__init__.py``). Raises: ValueError: If ``__all__`` has repeated elements. ValueError: If the "public" members in ``__init__.py`` don't match the members described in ``__all__``. """ if bezier is None: return [] local_members = [] all_members = set() for name in dir(bezier): # Filter out non-public. if name.startswith("_") and name not in SPECIAL_MEMBERS: continue value = getattr(bezier, name) # Filter out imported modules. if isinstance(value, types.ModuleType): continue all_members.add(name) # Only keep values defined in the base package. home = getattr(value, "__module__", "bezier") if home == "bezier": local_members.append(name) size_all = len(bezier.__all__) all_exports = set(bezier.__all__) if len(all_exports) != size_all: raise ValueError("__all__ has repeated elements") if all_exports != all_members: raise ValueError( "__all__ does not agree with the publicly imported members", all_exports, all_members, ) local_members = [ member for member in local_members if member not in UNDOCUMENTED_SPECIAL_MEMBERS ] return local_members
[ "def", "get_public_members", "(", ")", ":", "if", "bezier", "is", "None", ":", "return", "[", "]", "local_members", "=", "[", "]", "all_members", "=", "set", "(", ")", "for", "name", "in", "dir", "(", "bezier", ")", ":", "# Filter out non-public.", "if", "name", ".", "startswith", "(", "\"_\"", ")", "and", "name", "not", "in", "SPECIAL_MEMBERS", ":", "continue", "value", "=", "getattr", "(", "bezier", ",", "name", ")", "# Filter out imported modules.", "if", "isinstance", "(", "value", ",", "types", ".", "ModuleType", ")", ":", "continue", "all_members", ".", "add", "(", "name", ")", "# Only keep values defined in the base package.", "home", "=", "getattr", "(", "value", ",", "\"__module__\"", ",", "\"bezier\"", ")", "if", "home", "==", "\"bezier\"", ":", "local_members", ".", "append", "(", "name", ")", "size_all", "=", "len", "(", "bezier", ".", "__all__", ")", "all_exports", "=", "set", "(", "bezier", ".", "__all__", ")", "if", "len", "(", "all_exports", ")", "!=", "size_all", ":", "raise", "ValueError", "(", "\"__all__ has repeated elements\"", ")", "if", "all_exports", "!=", "all_members", ":", "raise", "ValueError", "(", "\"__all__ does not agree with the publicly imported members\"", ",", "all_exports", ",", "all_members", ",", ")", "local_members", "=", "[", "member", "for", "member", "in", "local_members", "if", "member", "not", "in", "UNDOCUMENTED_SPECIAL_MEMBERS", "]", "return", "local_members" ]
Get public members in :mod:`bezier` package. Also validates the contents of ``bezier.__all__``. Returns: list: List of all public members **defined** in the main package (i.e. in ``__init__.py``). Raises: ValueError: If ``__all__`` has repeated elements. ValueError: If the "public" members in ``__init__.py`` don't match the members described in ``__all__``.
[ "Get", "public", "members", "in", ":", "mod", ":", "bezier", "package", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/rewrite_package_rst.py#L69-L120
dhermes/bezier
scripts/rewrite_package_rst.py
get_desired
def get_desired(): """Populate ``DESIRED_TEMPLATE`` with public members. If there are no members, does nothing. Returns: str: The "desired" contents of ``bezier.rst``. """ public_members = get_public_members() if public_members: members = "\n :members: {}".format(", ".join(public_members)) else: members = "" return DESIRED_TEMPLATE.format(members=members)
python
def get_desired(): """Populate ``DESIRED_TEMPLATE`` with public members. If there are no members, does nothing. Returns: str: The "desired" contents of ``bezier.rst``. """ public_members = get_public_members() if public_members: members = "\n :members: {}".format(", ".join(public_members)) else: members = "" return DESIRED_TEMPLATE.format(members=members)
[ "def", "get_desired", "(", ")", ":", "public_members", "=", "get_public_members", "(", ")", "if", "public_members", ":", "members", "=", "\"\\n :members: {}\"", ".", "format", "(", "\", \"", ".", "join", "(", "public_members", ")", ")", "else", ":", "members", "=", "\"\"", "return", "DESIRED_TEMPLATE", ".", "format", "(", "members", "=", "members", ")" ]
Populate ``DESIRED_TEMPLATE`` with public members. If there are no members, does nothing. Returns: str: The "desired" contents of ``bezier.rst``.
[ "Populate", "DESIRED_TEMPLATE", "with", "public", "members", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/rewrite_package_rst.py#L123-L136
dhermes/bezier
scripts/rewrite_package_rst.py
main
def main(): """Main entry point to replace autogenerated contents. Raises: ValueError: If the file doesn't contain the expected or desired contents. """ with open(FILENAME, "r") as file_obj: contents = file_obj.read() desired = get_desired() if contents == EXPECTED: with open(FILENAME, "w") as file_obj: file_obj.write(desired) elif contents != desired: raise ValueError("Unexpected contents", contents, "Expected", EXPECTED)
python
def main(): """Main entry point to replace autogenerated contents. Raises: ValueError: If the file doesn't contain the expected or desired contents. """ with open(FILENAME, "r") as file_obj: contents = file_obj.read() desired = get_desired() if contents == EXPECTED: with open(FILENAME, "w") as file_obj: file_obj.write(desired) elif contents != desired: raise ValueError("Unexpected contents", contents, "Expected", EXPECTED)
[ "def", "main", "(", ")", ":", "with", "open", "(", "FILENAME", ",", "\"r\"", ")", "as", "file_obj", ":", "contents", "=", "file_obj", ".", "read", "(", ")", "desired", "=", "get_desired", "(", ")", "if", "contents", "==", "EXPECTED", ":", "with", "open", "(", "FILENAME", ",", "\"w\"", ")", "as", "file_obj", ":", "file_obj", ".", "write", "(", "desired", ")", "elif", "contents", "!=", "desired", ":", "raise", "ValueError", "(", "\"Unexpected contents\"", ",", "contents", ",", "\"Expected\"", ",", "EXPECTED", ")" ]
Main entry point to replace autogenerated contents. Raises: ValueError: If the file doesn't contain the expected or desired contents.
[ "Main", "entry", "point", "to", "replace", "autogenerated", "contents", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/scripts/rewrite_package_rst.py#L139-L153
dhermes/bezier
setup_helpers_windows.py
run_cleanup
def run_cleanup(build_ext_cmd): """Cleanup after ``BuildFortranThenExt.run``. For in-place builds, moves the built shared library into the source directory. """ if not build_ext_cmd.inplace: return bezier_dir = os.path.join("src", "bezier") shutil.move(os.path.join(build_ext_cmd.build_lib, LIB_DIR), bezier_dir) shutil.move(os.path.join(build_ext_cmd.build_lib, DLL_DIR), bezier_dir)
python
def run_cleanup(build_ext_cmd): """Cleanup after ``BuildFortranThenExt.run``. For in-place builds, moves the built shared library into the source directory. """ if not build_ext_cmd.inplace: return bezier_dir = os.path.join("src", "bezier") shutil.move(os.path.join(build_ext_cmd.build_lib, LIB_DIR), bezier_dir) shutil.move(os.path.join(build_ext_cmd.build_lib, DLL_DIR), bezier_dir)
[ "def", "run_cleanup", "(", "build_ext_cmd", ")", ":", "if", "not", "build_ext_cmd", ".", "inplace", ":", "return", "bezier_dir", "=", "os", ".", "path", ".", "join", "(", "\"src\"", ",", "\"bezier\"", ")", "shutil", ".", "move", "(", "os", ".", "path", ".", "join", "(", "build_ext_cmd", ".", "build_lib", ",", "LIB_DIR", ")", ",", "bezier_dir", ")", "shutil", ".", "move", "(", "os", ".", "path", ".", "join", "(", "build_ext_cmd", ".", "build_lib", ",", "DLL_DIR", ")", ",", "bezier_dir", ")" ]
Cleanup after ``BuildFortranThenExt.run``. For in-place builds, moves the built shared library into the source directory.
[ "Cleanup", "after", "BuildFortranThenExt", ".", "run", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/setup_helpers_windows.py#L77-L88
dhermes/bezier
setup_helpers_windows.py
patch_f90_compiler
def patch_f90_compiler(f90_compiler): """Patch up ``f90_compiler.library_dirs``. Updates flags in ``gfortran`` and ignores other compilers. The only modification is the removal of ``-fPIC`` since it is not used on Windows and the build flags turn warnings into errors. Args: f90_compiler (numpy.distutils.fcompiler.FCompiler): A Fortran compiler instance. """ # NOTE: NumPy may not be installed, but we don't want **this** module to # cause an import failure. from numpy.distutils.fcompiler import gnu # Only Windows. if os.name != "nt": return # Only ``gfortran``. if not isinstance(f90_compiler, gnu.Gnu95FCompiler): return f90_compiler.compiler_f77[:] = _remove_fpic(f90_compiler.compiler_f77) f90_compiler.compiler_f90[:] = _remove_fpic(f90_compiler.compiler_f90) c_compiler = f90_compiler.c_compiler if c_compiler.compiler_type != "msvc": raise NotImplementedError( "MSVC is the only supported C compiler on Windows." )
python
def patch_f90_compiler(f90_compiler): """Patch up ``f90_compiler.library_dirs``. Updates flags in ``gfortran`` and ignores other compilers. The only modification is the removal of ``-fPIC`` since it is not used on Windows and the build flags turn warnings into errors. Args: f90_compiler (numpy.distutils.fcompiler.FCompiler): A Fortran compiler instance. """ # NOTE: NumPy may not be installed, but we don't want **this** module to # cause an import failure. from numpy.distutils.fcompiler import gnu # Only Windows. if os.name != "nt": return # Only ``gfortran``. if not isinstance(f90_compiler, gnu.Gnu95FCompiler): return f90_compiler.compiler_f77[:] = _remove_fpic(f90_compiler.compiler_f77) f90_compiler.compiler_f90[:] = _remove_fpic(f90_compiler.compiler_f90) c_compiler = f90_compiler.c_compiler if c_compiler.compiler_type != "msvc": raise NotImplementedError( "MSVC is the only supported C compiler on Windows." )
[ "def", "patch_f90_compiler", "(", "f90_compiler", ")", ":", "# NOTE: NumPy may not be installed, but we don't want **this** module to", "# cause an import failure.", "from", "numpy", ".", "distutils", ".", "fcompiler", "import", "gnu", "# Only Windows.", "if", "os", ".", "name", "!=", "\"nt\"", ":", "return", "# Only ``gfortran``.", "if", "not", "isinstance", "(", "f90_compiler", ",", "gnu", ".", "Gnu95FCompiler", ")", ":", "return", "f90_compiler", ".", "compiler_f77", "[", ":", "]", "=", "_remove_fpic", "(", "f90_compiler", ".", "compiler_f77", ")", "f90_compiler", ".", "compiler_f90", "[", ":", "]", "=", "_remove_fpic", "(", "f90_compiler", ".", "compiler_f90", ")", "c_compiler", "=", "f90_compiler", ".", "c_compiler", "if", "c_compiler", ".", "compiler_type", "!=", "\"msvc\"", ":", "raise", "NotImplementedError", "(", "\"MSVC is the only supported C compiler on Windows.\"", ")" ]
Patch up ``f90_compiler.library_dirs``. Updates flags in ``gfortran`` and ignores other compilers. The only modification is the removal of ``-fPIC`` since it is not used on Windows and the build flags turn warnings into errors. Args: f90_compiler (numpy.distutils.fcompiler.FCompiler): A Fortran compiler instance.
[ "Patch", "up", "f90_compiler", ".", "library_dirs", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/setup_helpers_windows.py#L103-L132
dhermes/bezier
noxfile.py
clean
def clean(session): """Clean up build files. Cleans up all artifacts that might get created during other ``nox`` sessions. There is no need for the session to create a ``virtualenv`` here (we are just pretending to be ``make``). """ clean_dirs = ( get_path(".cache"), get_path(".coverage"), get_path(".pytest_cache"), get_path("__pycache__"), get_path("build"), get_path("dist"), get_path("docs", "__pycache__"), get_path("docs", "build"), get_path("scripts", "macos", "__pycache__"), get_path("scripts", "macos", "dist_wheels"), get_path("scripts", "macos", "fixed_wheels"), get_path("src", "bezier.egg-info"), get_path("src", "bezier", "__pycache__"), get_path("src", "bezier", "extra-dll"), get_path("src", "bezier", "lib"), get_path("tests", "__pycache__"), get_path("tests", "functional", "__pycache__"), get_path("tests", "unit", "__pycache__"), get_path("wheelhouse"), ) clean_globs = ( get_path(".coverage"), get_path("*.mod"), get_path("*.pyc"), get_path("src", "bezier", "*.pyc"), get_path("src", "bezier", "*.pyd"), get_path("src", "bezier", "*.so"), get_path("src", "bezier", "quadpack", "*.o"), get_path("src", "bezier", "*.o"), get_path("tests", "*.pyc"), get_path("tests", "functional", "*.pyc"), get_path("tests", "unit", "*.pyc"), ) for dir_path in clean_dirs: session.run(shutil.rmtree, dir_path, ignore_errors=True) for glob_path in clean_globs: for filename in glob.glob(glob_path): session.run(os.remove, filename)
python
def clean(session): """Clean up build files. Cleans up all artifacts that might get created during other ``nox`` sessions. There is no need for the session to create a ``virtualenv`` here (we are just pretending to be ``make``). """ clean_dirs = ( get_path(".cache"), get_path(".coverage"), get_path(".pytest_cache"), get_path("__pycache__"), get_path("build"), get_path("dist"), get_path("docs", "__pycache__"), get_path("docs", "build"), get_path("scripts", "macos", "__pycache__"), get_path("scripts", "macos", "dist_wheels"), get_path("scripts", "macos", "fixed_wheels"), get_path("src", "bezier.egg-info"), get_path("src", "bezier", "__pycache__"), get_path("src", "bezier", "extra-dll"), get_path("src", "bezier", "lib"), get_path("tests", "__pycache__"), get_path("tests", "functional", "__pycache__"), get_path("tests", "unit", "__pycache__"), get_path("wheelhouse"), ) clean_globs = ( get_path(".coverage"), get_path("*.mod"), get_path("*.pyc"), get_path("src", "bezier", "*.pyc"), get_path("src", "bezier", "*.pyd"), get_path("src", "bezier", "*.so"), get_path("src", "bezier", "quadpack", "*.o"), get_path("src", "bezier", "*.o"), get_path("tests", "*.pyc"), get_path("tests", "functional", "*.pyc"), get_path("tests", "unit", "*.pyc"), ) for dir_path in clean_dirs: session.run(shutil.rmtree, dir_path, ignore_errors=True) for glob_path in clean_globs: for filename in glob.glob(glob_path): session.run(os.remove, filename)
[ "def", "clean", "(", "session", ")", ":", "clean_dirs", "=", "(", "get_path", "(", "\".cache\"", ")", ",", "get_path", "(", "\".coverage\"", ")", ",", "get_path", "(", "\".pytest_cache\"", ")", ",", "get_path", "(", "\"__pycache__\"", ")", ",", "get_path", "(", "\"build\"", ")", ",", "get_path", "(", "\"dist\"", ")", ",", "get_path", "(", "\"docs\"", ",", "\"__pycache__\"", ")", ",", "get_path", "(", "\"docs\"", ",", "\"build\"", ")", ",", "get_path", "(", "\"scripts\"", ",", "\"macos\"", ",", "\"__pycache__\"", ")", ",", "get_path", "(", "\"scripts\"", ",", "\"macos\"", ",", "\"dist_wheels\"", ")", ",", "get_path", "(", "\"scripts\"", ",", "\"macos\"", ",", "\"fixed_wheels\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier.egg-info\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"__pycache__\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"extra-dll\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"lib\"", ")", ",", "get_path", "(", "\"tests\"", ",", "\"__pycache__\"", ")", ",", "get_path", "(", "\"tests\"", ",", "\"functional\"", ",", "\"__pycache__\"", ")", ",", "get_path", "(", "\"tests\"", ",", "\"unit\"", ",", "\"__pycache__\"", ")", ",", "get_path", "(", "\"wheelhouse\"", ")", ",", ")", "clean_globs", "=", "(", "get_path", "(", "\".coverage\"", ")", ",", "get_path", "(", "\"*.mod\"", ")", ",", "get_path", "(", "\"*.pyc\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"*.pyc\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"*.pyd\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"*.so\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"quadpack\"", ",", "\"*.o\"", ")", ",", "get_path", "(", "\"src\"", ",", "\"bezier\"", ",", "\"*.o\"", ")", ",", "get_path", "(", "\"tests\"", ",", "\"*.pyc\"", ")", ",", "get_path", "(", "\"tests\"", ",", "\"functional\"", ",", "\"*.pyc\"", ")", ",", "get_path", "(", "\"tests\"", ",", "\"unit\"", ",", "\"*.pyc\"", ")", ",", ")", "for", "dir_path", "in", "clean_dirs", ":", "session", ".", "run", "(", "shutil", ".", "rmtree", ",", "dir_path", ",", "ignore_errors", "=", "True", ")", "for", "glob_path", "in", "clean_globs", ":", "for", "filename", "in", "glob", ".", "glob", "(", "glob_path", ")", ":", "session", ".", "run", "(", "os", ".", "remove", ",", "filename", ")" ]
Clean up build files. Cleans up all artifacts that might get created during other ``nox`` sessions. There is no need for the session to create a ``virtualenv`` here (we are just pretending to be ``make``).
[ "Clean", "up", "build", "files", "." ]
train
https://github.com/dhermes/bezier/blob/4f941f82637a8e70a5b159a9203132192e23406b/noxfile.py#L373-L420
allianceauth/allianceauth
allianceauth/hooks.py
register
def register(name, fn=None): """ Decorator to register a function as a hook Register hook for ``hook_name``. Can be used as a decorator:: @register('hook_name') def my_hook(...): pass or as a function call:: def my_hook(...): pass register('hook_name', my_hook) :param name: str Name of the hook/callback to register it as :param fn: function to register in the hook/callback :return: function Decorator if applied as a decorator """ def _hook_add(func): if name not in _hooks: logger.debug("Creating new hook %s" % name) _hooks[name] = [] logger.debug('Registering hook %s for function %s' % (name, fn)) _hooks[name].append(func) if fn is None: # Behave like a decorator def decorator(func): _hook_add(func) return func return decorator else: # Behave like a function, just register hook _hook_add(fn)
python
def register(name, fn=None): """ Decorator to register a function as a hook Register hook for ``hook_name``. Can be used as a decorator:: @register('hook_name') def my_hook(...): pass or as a function call:: def my_hook(...): pass register('hook_name', my_hook) :param name: str Name of the hook/callback to register it as :param fn: function to register in the hook/callback :return: function Decorator if applied as a decorator """ def _hook_add(func): if name not in _hooks: logger.debug("Creating new hook %s" % name) _hooks[name] = [] logger.debug('Registering hook %s for function %s' % (name, fn)) _hooks[name].append(func) if fn is None: # Behave like a decorator def decorator(func): _hook_add(func) return func return decorator else: # Behave like a function, just register hook _hook_add(fn)
[ "def", "register", "(", "name", ",", "fn", "=", "None", ")", ":", "def", "_hook_add", "(", "func", ")", ":", "if", "name", "not", "in", "_hooks", ":", "logger", ".", "debug", "(", "\"Creating new hook %s\"", "%", "name", ")", "_hooks", "[", "name", "]", "=", "[", "]", "logger", ".", "debug", "(", "'Registering hook %s for function %s'", "%", "(", "name", ",", "fn", ")", ")", "_hooks", "[", "name", "]", ".", "append", "(", "func", ")", "if", "fn", "is", "None", ":", "# Behave like a decorator", "def", "decorator", "(", "func", ")", ":", "_hook_add", "(", "func", ")", "return", "func", "return", "decorator", "else", ":", "# Behave like a function, just register hook", "_hook_add", "(", "fn", ")" ]
Decorator to register a function as a hook Register hook for ``hook_name``. Can be used as a decorator:: @register('hook_name') def my_hook(...): pass or as a function call:: def my_hook(...): pass register('hook_name', my_hook) :param name: str Name of the hook/callback to register it as :param fn: function to register in the hook/callback :return: function Decorator if applied as a decorator
[ "Decorator", "to", "register", "a", "function", "as", "a", "hook" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/hooks.py#L47-L81
allianceauth/allianceauth
allianceauth/hooks.py
get_app_submodules
def get_app_submodules(module_name): """ Get a specific sub module of the app :param module_name: module name to get :return: name, module tuple """ for name, module in get_app_modules(): if module_has_submodule(module, module_name): yield name, import_module('{0}.{1}'.format(name, module_name))
python
def get_app_submodules(module_name): """ Get a specific sub module of the app :param module_name: module name to get :return: name, module tuple """ for name, module in get_app_modules(): if module_has_submodule(module, module_name): yield name, import_module('{0}.{1}'.format(name, module_name))
[ "def", "get_app_submodules", "(", "module_name", ")", ":", "for", "name", ",", "module", "in", "get_app_modules", "(", ")", ":", "if", "module_has_submodule", "(", "module", ",", "module_name", ")", ":", "yield", "name", ",", "import_module", "(", "'{0}.{1}'", ".", "format", "(", "name", ",", "module_name", ")", ")" ]
Get a specific sub module of the app :param module_name: module name to get :return: name, module tuple
[ "Get", "a", "specific", "sub", "module", "of", "the", "app", ":", "param", "module_name", ":", "module", "name", "to", "get", ":", "return", ":", "name", "module", "tuple" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/hooks.py#L93-L101
allianceauth/allianceauth
allianceauth/hooks.py
register_all_hooks
def register_all_hooks(): """ Register all hooks found in 'auth_hooks' sub modules :return: """ global _all_hooks_registered if not _all_hooks_registered: logger.debug("Searching for hooks") hooks = list(get_app_submodules('auth_hooks')) logger.debug("Got %s hooks" % len(hooks)) _all_hooks_registered = True
python
def register_all_hooks(): """ Register all hooks found in 'auth_hooks' sub modules :return: """ global _all_hooks_registered if not _all_hooks_registered: logger.debug("Searching for hooks") hooks = list(get_app_submodules('auth_hooks')) logger.debug("Got %s hooks" % len(hooks)) _all_hooks_registered = True
[ "def", "register_all_hooks", "(", ")", ":", "global", "_all_hooks_registered", "if", "not", "_all_hooks_registered", ":", "logger", ".", "debug", "(", "\"Searching for hooks\"", ")", "hooks", "=", "list", "(", "get_app_submodules", "(", "'auth_hooks'", ")", ")", "logger", ".", "debug", "(", "\"Got %s hooks\"", "%", "len", "(", "hooks", ")", ")", "_all_hooks_registered", "=", "True" ]
Register all hooks found in 'auth_hooks' sub modules :return:
[ "Register", "all", "hooks", "found", "in", "auth_hooks", "sub", "modules", ":", "return", ":" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/hooks.py#L104-L114
allianceauth/allianceauth
allianceauth/services/modules/seat/manager.py
SeatManager.exec_request
def exec_request(endpoint, func, raise_for_status=False, **kwargs): """ Send an https api request """ try: endpoint = '{0}/api/v1/{1}'.format(settings.SEAT_URL, endpoint) headers = {'X-Token': settings.SEAT_XTOKEN, 'Accept': 'application/json'} logger.debug(headers) logger.debug(endpoint) ret = getattr(requests, func)(endpoint, headers=headers, data=kwargs) ret.raise_for_status() return ret.json() except requests.HTTPError as e: if raise_for_status: raise e logger.exception("Error encountered while performing API request to SeAT with url {}".format(endpoint)) return {}
python
def exec_request(endpoint, func, raise_for_status=False, **kwargs): """ Send an https api request """ try: endpoint = '{0}/api/v1/{1}'.format(settings.SEAT_URL, endpoint) headers = {'X-Token': settings.SEAT_XTOKEN, 'Accept': 'application/json'} logger.debug(headers) logger.debug(endpoint) ret = getattr(requests, func)(endpoint, headers=headers, data=kwargs) ret.raise_for_status() return ret.json() except requests.HTTPError as e: if raise_for_status: raise e logger.exception("Error encountered while performing API request to SeAT with url {}".format(endpoint)) return {}
[ "def", "exec_request", "(", "endpoint", ",", "func", ",", "raise_for_status", "=", "False", ",", "*", "*", "kwargs", ")", ":", "try", ":", "endpoint", "=", "'{0}/api/v1/{1}'", ".", "format", "(", "settings", ".", "SEAT_URL", ",", "endpoint", ")", "headers", "=", "{", "'X-Token'", ":", "settings", ".", "SEAT_XTOKEN", ",", "'Accept'", ":", "'application/json'", "}", "logger", ".", "debug", "(", "headers", ")", "logger", ".", "debug", "(", "endpoint", ")", "ret", "=", "getattr", "(", "requests", ",", "func", ")", "(", "endpoint", ",", "headers", "=", "headers", ",", "data", "=", "kwargs", ")", "ret", ".", "raise_for_status", "(", ")", "return", "ret", ".", "json", "(", ")", "except", "requests", ".", "HTTPError", "as", "e", ":", "if", "raise_for_status", ":", "raise", "e", "logger", ".", "exception", "(", "\"Error encountered while performing API request to SeAT with url {}\"", ".", "format", "(", "endpoint", ")", ")", "return", "{", "}" ]
Send an https api request
[ "Send", "an", "https", "api", "request" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/modules/seat/manager.py#L32-L46
allianceauth/allianceauth
allianceauth/services/modules/seat/manager.py
SeatManager.add_user
def add_user(cls, username, email): """ Add user to service """ sanitized = str(cls.__sanitize_username(username)) logger.debug("Adding user to SeAT with username %s" % sanitized) password = cls.__generate_random_pass() ret = cls.exec_request('user', 'post', username=sanitized, email=str(email), password=password) logger.debug(ret) if cls._response_ok(ret): logger.info("Added SeAT user with username %s" % sanitized) return sanitized, password logger.info("Failed to add SeAT user with username %s" % sanitized) return None, None
python
def add_user(cls, username, email): """ Add user to service """ sanitized = str(cls.__sanitize_username(username)) logger.debug("Adding user to SeAT with username %s" % sanitized) password = cls.__generate_random_pass() ret = cls.exec_request('user', 'post', username=sanitized, email=str(email), password=password) logger.debug(ret) if cls._response_ok(ret): logger.info("Added SeAT user with username %s" % sanitized) return sanitized, password logger.info("Failed to add SeAT user with username %s" % sanitized) return None, None
[ "def", "add_user", "(", "cls", ",", "username", ",", "email", ")", ":", "sanitized", "=", "str", "(", "cls", ".", "__sanitize_username", "(", "username", ")", ")", "logger", ".", "debug", "(", "\"Adding user to SeAT with username %s\"", "%", "sanitized", ")", "password", "=", "cls", ".", "__generate_random_pass", "(", ")", "ret", "=", "cls", ".", "exec_request", "(", "'user'", ",", "'post'", ",", "username", "=", "sanitized", ",", "email", "=", "str", "(", "email", ")", ",", "password", "=", "password", ")", "logger", ".", "debug", "(", "ret", ")", "if", "cls", ".", "_response_ok", "(", "ret", ")", ":", "logger", ".", "info", "(", "\"Added SeAT user with username %s\"", "%", "sanitized", ")", "return", "sanitized", ",", "password", "logger", ".", "info", "(", "\"Failed to add SeAT user with username %s\"", "%", "sanitized", ")", "return", "None", ",", "None" ]
Add user to service
[ "Add", "user", "to", "service" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/modules/seat/manager.py#L49-L60
allianceauth/allianceauth
allianceauth/services/modules/seat/manager.py
SeatManager.delete_user
def delete_user(cls, username): """ Delete user """ ret = cls.exec_request('user/{}'.format(username), 'delete') logger.debug(ret) if cls._response_ok(ret): logger.info("Deleted SeAT user with username %s" % username) return username return None
python
def delete_user(cls, username): """ Delete user """ ret = cls.exec_request('user/{}'.format(username), 'delete') logger.debug(ret) if cls._response_ok(ret): logger.info("Deleted SeAT user with username %s" % username) return username return None
[ "def", "delete_user", "(", "cls", ",", "username", ")", ":", "ret", "=", "cls", ".", "exec_request", "(", "'user/{}'", ".", "format", "(", "username", ")", ",", "'delete'", ")", "logger", ".", "debug", "(", "ret", ")", "if", "cls", ".", "_response_ok", "(", "ret", ")", ":", "logger", ".", "info", "(", "\"Deleted SeAT user with username %s\"", "%", "username", ")", "return", "username", "return", "None" ]
Delete user
[ "Delete", "user" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/modules/seat/manager.py#L63-L70
allianceauth/allianceauth
allianceauth/services/modules/seat/manager.py
SeatManager.enable_user
def enable_user(cls, username): """ Enable user """ ret = cls.exec_request('user/{}'.format(username), 'put', account_status=1) logger.debug(ret) if cls._response_ok(ret): logger.info("Enabled SeAT user with username %s" % username) return username logger.info("Failed to enabled SeAT user with username %s" % username) return None
python
def enable_user(cls, username): """ Enable user """ ret = cls.exec_request('user/{}'.format(username), 'put', account_status=1) logger.debug(ret) if cls._response_ok(ret): logger.info("Enabled SeAT user with username %s" % username) return username logger.info("Failed to enabled SeAT user with username %s" % username) return None
[ "def", "enable_user", "(", "cls", ",", "username", ")", ":", "ret", "=", "cls", ".", "exec_request", "(", "'user/{}'", ".", "format", "(", "username", ")", ",", "'put'", ",", "account_status", "=", "1", ")", "logger", ".", "debug", "(", "ret", ")", "if", "cls", ".", "_response_ok", "(", "ret", ")", ":", "logger", ".", "info", "(", "\"Enabled SeAT user with username %s\"", "%", "username", ")", "return", "username", "logger", ".", "info", "(", "\"Failed to enabled SeAT user with username %s\"", "%", "username", ")", "return", "None" ]
Enable user
[ "Enable", "user" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/modules/seat/manager.py#L73-L81
allianceauth/allianceauth
allianceauth/services/modules/seat/manager.py
SeatManager.disable_user
def disable_user(cls, username): """ Disable user """ cls.update_roles(username, []) ret = cls.exec_request('user/{}'.format(username), 'put', account_status=0) logger.debug(ret) if cls._response_ok(ret): logger.info("Disabled SeAT user with username %s" % username) return username logger.info("Failed to disable SeAT user with username %s" % username) return None
python
def disable_user(cls, username): """ Disable user """ cls.update_roles(username, []) ret = cls.exec_request('user/{}'.format(username), 'put', account_status=0) logger.debug(ret) if cls._response_ok(ret): logger.info("Disabled SeAT user with username %s" % username) return username logger.info("Failed to disable SeAT user with username %s" % username) return None
[ "def", "disable_user", "(", "cls", ",", "username", ")", ":", "cls", ".", "update_roles", "(", "username", ",", "[", "]", ")", "ret", "=", "cls", ".", "exec_request", "(", "'user/{}'", ".", "format", "(", "username", ")", ",", "'put'", ",", "account_status", "=", "0", ")", "logger", ".", "debug", "(", "ret", ")", "if", "cls", ".", "_response_ok", "(", "ret", ")", ":", "logger", ".", "info", "(", "\"Disabled SeAT user with username %s\"", "%", "username", ")", "return", "username", "logger", ".", "info", "(", "\"Failed to disable SeAT user with username %s\"", "%", "username", ")", "return", "None" ]
Disable user
[ "Disable", "user" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/modules/seat/manager.py#L84-L93
allianceauth/allianceauth
allianceauth/services/modules/seat/manager.py
SeatManager._check_email_changed
def _check_email_changed(cls, username, email): """Compares email to one set on SeAT""" ret = cls.exec_request('user/{}'.format(username), 'get', raise_for_status=True) return ret['email'] != email
python
def _check_email_changed(cls, username, email): """Compares email to one set on SeAT""" ret = cls.exec_request('user/{}'.format(username), 'get', raise_for_status=True) return ret['email'] != email
[ "def", "_check_email_changed", "(", "cls", ",", "username", ",", "email", ")", ":", "ret", "=", "cls", ".", "exec_request", "(", "'user/{}'", ".", "format", "(", "username", ")", ",", "'get'", ",", "raise_for_status", "=", "True", ")", "return", "ret", "[", "'email'", "]", "!=", "email" ]
Compares email to one set on SeAT
[ "Compares", "email", "to", "one", "set", "on", "SeAT" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/modules/seat/manager.py#L96-L99
allianceauth/allianceauth
allianceauth/services/modules/seat/manager.py
SeatManager.update_user
def update_user(cls, username, email, password): """ Edit user info """ if cls._check_email_changed(username, email): # if we try to set the email to whatever it is already on SeAT, we get a HTTP422 error logger.debug("Updating SeAT username %s with email %s and password" % (username, email)) ret = cls.exec_request('user/{}'.format(username), 'put', email=email) logger.debug(ret) if not cls._response_ok(ret): logger.warn("Failed to update email for username {}".format(username)) ret = cls.exec_request('user/{}'.format(username), 'put', password=password) logger.debug(ret) if not cls._response_ok(ret): logger.warn("Failed to update password for username {}".format(username)) return None logger.info("Updated SeAT user with username %s" % username) return username
python
def update_user(cls, username, email, password): """ Edit user info """ if cls._check_email_changed(username, email): # if we try to set the email to whatever it is already on SeAT, we get a HTTP422 error logger.debug("Updating SeAT username %s with email %s and password" % (username, email)) ret = cls.exec_request('user/{}'.format(username), 'put', email=email) logger.debug(ret) if not cls._response_ok(ret): logger.warn("Failed to update email for username {}".format(username)) ret = cls.exec_request('user/{}'.format(username), 'put', password=password) logger.debug(ret) if not cls._response_ok(ret): logger.warn("Failed to update password for username {}".format(username)) return None logger.info("Updated SeAT user with username %s" % username) return username
[ "def", "update_user", "(", "cls", ",", "username", ",", "email", ",", "password", ")", ":", "if", "cls", ".", "_check_email_changed", "(", "username", ",", "email", ")", ":", "# if we try to set the email to whatever it is already on SeAT, we get a HTTP422 error", "logger", ".", "debug", "(", "\"Updating SeAT username %s with email %s and password\"", "%", "(", "username", ",", "email", ")", ")", "ret", "=", "cls", ".", "exec_request", "(", "'user/{}'", ".", "format", "(", "username", ")", ",", "'put'", ",", "email", "=", "email", ")", "logger", ".", "debug", "(", "ret", ")", "if", "not", "cls", ".", "_response_ok", "(", "ret", ")", ":", "logger", ".", "warn", "(", "\"Failed to update email for username {}\"", ".", "format", "(", "username", ")", ")", "ret", "=", "cls", ".", "exec_request", "(", "'user/{}'", ".", "format", "(", "username", ")", ",", "'put'", ",", "password", "=", "password", ")", "logger", ".", "debug", "(", "ret", ")", "if", "not", "cls", ".", "_response_ok", "(", "ret", ")", ":", "logger", ".", "warn", "(", "\"Failed to update password for username {}\"", ".", "format", "(", "username", ")", ")", "return", "None", "logger", ".", "info", "(", "\"Updated SeAT user with username %s\"", "%", "username", ")", "return", "username" ]
Edit user info
[ "Edit", "user", "info" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/modules/seat/manager.py#L102-L117
allianceauth/allianceauth
allianceauth/services/hooks.py
NameFormatter.format_name
def format_name(self): """ :return: str Generated name """ format_data = self.get_format_data() return Formatter().vformat(self.string_formatter, args=[], kwargs=format_data)
python
def format_name(self): """ :return: str Generated name """ format_data = self.get_format_data() return Formatter().vformat(self.string_formatter, args=[], kwargs=format_data)
[ "def", "format_name", "(", "self", ")", ":", "format_data", "=", "self", ".", "get_format_data", "(", ")", "return", "Formatter", "(", ")", ".", "vformat", "(", "self", ".", "string_formatter", ",", "args", "=", "[", "]", ",", "kwargs", "=", "format_data", ")" ]
:return: str Generated name
[ ":", "return", ":", "str", "Generated", "name" ]
train
https://github.com/allianceauth/allianceauth/blob/6585b07e96571a99a4d6dc03cc03f9b8c8f690ca/allianceauth/services/hooks.py#L143-L148