Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix main branch actions failures, other pre-commit tweaks #68

Merged
merged 7 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Applied `ruff` and `black` (gh-64)
1fec42d0baf90e00d510efd76cb6006fa0c70dc4

# Applied `pre-commit` `end-of-file-fixer`
e9104b3616899f54257bb38959d6e1c0acc70f6a
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,3 @@ coverage.xml

# Sphinx documentation
docs/_build/

4 changes: 1 addition & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ repos:
hooks:
- id: "check-added-large-files"
- id: "check-vcs-permalinks"
- id: "no-commit-to-branch"
# TODO: Apply this fixer and add an entry to .git-blame-ignore-revs
# - id: "end-of-file-fixer"
- id: "end-of-file-fixer"

- repo: "https://github.com/charliermarsh/ruff-pre-commit"
rev: "v0.0.269"
Expand Down
108 changes: 27 additions & 81 deletions viscm/bezierbuilder.py → viscm/bezierbuilder/__init__.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
# BézierBuilder
#
# Copyright (c) 2013, Juan Luis Cano Rodríguez <juanlu001@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""BézierBuilder, an interactive Bézier curve explorer.

Just run it with

$ python bezier_builder.py

Copyright (c) 2013, Juan Luis Cano Rodríguez <juanlu001@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

from math import factorial

import numpy as np
from matplotlib.backends.qt_compat import QtCore
from matplotlib.lines import Line2D

from .minimvc import Trigger
from viscm.bezierbuilder.curve import curve_method
from viscm.minimvc import Trigger


class ControlPointModel:
Expand Down Expand Up @@ -193,7 +185,7 @@ def compute_arc_length(xp, yp, method, t=None, grid=256):

class SingleBezierCurveModel:
def __init__(self, control_point_model, method="CatmulClark"):
self.method = eval(method)
self.method = curve_method[method]
self.control_point_model = control_point_model
x, y = self.get_bezier_points()
self.bezier_curve = Line2D(x, y)
Expand All @@ -215,7 +207,7 @@ def _refresh(self):

class TwoBezierCurveModel:
def __init__(self, control_point_model, method="CatmulClark"):
self.method = eval(method)
self.method = curve_method[method]
self.control_point_model = control_point_model
x, y = self.get_bezier_points()
self.bezier_curve = Line2D(x, y)
Expand Down Expand Up @@ -286,49 +278,3 @@ def _refresh(self):
x, y = self.bezier_curve_model.get_bezier_points()
self.bezier_curve.set_data(x, y)
self.canvas.draw()


# We used to use scipy.special.binom here,
# but reimplementing it ourself lets us avoid pulling in a dependency
# scipy just for that one function.
def binom(n, k):
return factorial(n) * 1.0 / (factorial(k) * factorial(n - k))


def Bernstein(n, k):
"""Bernstein polynomial."""
coeff = binom(n, k)

def _bpoly(x):
return coeff * x**k * (1 - x) ** (n - k)

return _bpoly


def Bezier(points, at):
"""Build Bézier curve from points.
Deprecated. CatmulClark builds nicer splines
"""
at = np.asarray(at)
at_flat = at.ravel()
N = len(points)
curve = np.zeros((at_flat.shape[0], 2))
for ii in range(N):
curve += np.outer(Bernstein(N - 1, ii)(at_flat), points[ii])
return curve.reshape((*at.shape, 2))


def CatmulClark(points, at):
points = np.asarray(points)

while len(points) < len(at):
new_p = np.zeros((2 * len(points), 2))
new_p[0] = points[0]
new_p[-1] = points[-1]
new_p[1:-2:2] = 3 / 4.0 * points[:-1] + 1 / 4.0 * points[1:]
new_p[2:-1:2] = 1 / 4.0 * points[:-1] + 3 / 4.0 * points[1:]
points = new_p
xp, yp = zip(*points)
xp = np.interp(at, np.linspace(0, 1, len(xp)), xp)
yp = np.interp(at, np.linspace(0, 1, len(yp)), yp)
return np.asarray(list(zip(xp, yp)))
53 changes: 53 additions & 0 deletions viscm/bezierbuilder/curve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import warnings

import numpy as np
from scipy.special import binom


def Bernstein(n, k):
"""Bernstein polynomial."""
coeff = binom(n, k)

def _bpoly(x):
return coeff * x**k * (1 - x) ** (n - k)

return _bpoly


def Bezier(points, at):
"""Build Bézier curve from points."""
warnings.warn(
message="Deprecated. CatmulClark builds nicer splines.",
category=FutureWarning,
stacklevel=1,
)

at = np.asarray(at)
at_flat = at.ravel()
N = len(points)
curve = np.zeros((at_flat.shape[0], 2))
for ii in range(N):
curve += np.outer(Bernstein(N - 1, ii)(at_flat), points[ii])
return curve.reshape((*at.shape, 2))


def CatmulClark(points, at):
points = np.asarray(points)

while len(points) < len(at):
new_p = np.zeros((2 * len(points), 2))
new_p[0] = points[0]
new_p[-1] = points[-1]
new_p[1:-2:2] = 3 / 4.0 * points[:-1] + 1 / 4.0 * points[1:]
new_p[2:-1:2] = 1 / 4.0 * points[:-1] + 3 / 4.0 * points[1:]
points = new_p
xp, yp = zip(*points)
xp = np.interp(at, np.linspace(0, 1, len(xp)), xp)
yp = np.interp(at, np.linspace(0, 1, len(yp)), yp)
return np.asarray(list(zip(xp, yp)))


curve_method = {
"Bezier": Bezier,
"CatmulClark": CatmulClark,
}
2 changes: 1 addition & 1 deletion viscm/examples/sample_diverging.jscm
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@
"domain": "continuous",
"content-type": "application/vnd.matplotlib.colormap-v1+json",
"license": "http://creativecommons.org/publicdomain/zero/1.0/"
}
}
2 changes: 1 addition & 1 deletion viscm/examples/sample_diverging_continuous.jscm
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
"colorspace": "sRGB",
"name": "sample_diverging_continuous",
"domain": "continuous"
}
}
2 changes: 1 addition & 1 deletion viscm/examples/sample_linear.jscm
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
"cmtype": "linear"
}
}
}
}