Skip to content

Commit

Permalink
style: reduce max line lenght to 79
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-adir committed Mar 2, 2024
1 parent 9373d03 commit 11a0f72
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 30 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ build-backend = "poetry.core.masonry.api"

[tool.scriv]
version = "literal: src/compmec/shape/__init__.py: __version__"

[tool.black]
line-length = 79
36 changes: 27 additions & 9 deletions src/compmec/shape/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,15 @@ def closed_linspace(npts: int) -> Tuple[Fraction]:
def open_linspace(npts: int) -> Tuple[Fraction]:
assert isinstance(npts, int)
assert npts >= 1
return tuple(Fraction(num) / (2 * npts) for num in range(1, 2 * npts, 2))
return tuple(
Fraction(num) / (2 * npts) for num in range(1, 2 * npts, 2)
)


class BaseCurve(object):
def __call__(self, nodes: Union[float, Tuple[float]]) -> Union[Any, Tuple[Any]]:
def __call__(
self, nodes: Union[float, Tuple[float]]
) -> Union[Any, Tuple[Any]]:
try:
iter(nodes)
return self.eval(nodes)
Expand Down Expand Up @@ -205,9 +209,13 @@ def __or__(self, other: PlanarCurve) -> PlanarCurve:
knotvectora.scale(node)
knotvectorb = nurbs.GeneratorKnotVector.bezier(other.degree, Fraction)
knotvectorb.scale(1 - node).shift(node)
newknotvector = tuple(knotvectora) + tuple(knotvectorb[self.degree + 1 :])
newknotvector = tuple(knotvectora) + tuple(
knotvectorb[self.degree + 1 :]
)
finalcurve = nurbs.Curve(newknotvector)
finalcurve.ctrlpoints = tuple(self.ctrlpoints) + tuple(other.ctrlpoints)
finalcurve.ctrlpoints = tuple(self.ctrlpoints) + tuple(
other.ctrlpoints
)
finalcurve.knot_clean((node,))
if finalcurve.degree + 1 != finalcurve.npts:
raise ValueError("Union is not a bezier curve!")
Expand Down Expand Up @@ -374,7 +382,9 @@ def degree_decrease(degree: int, times: int) -> Tuple[Tuple[Tuple[float]]]:
assert degree - times >= 0
if (degree, times) not in Operations.__degree_decre:
old_knotvector = nurbs.GeneratorKnotVector.bezier(degree, Fraction)
new_knotvector = nurbs.GeneratorKnotVector.bezier(degree - times, Fraction)
new_knotvector = nurbs.GeneratorKnotVector.bezier(
degree - times, Fraction
)
matrix, error = nurbs.heavy.LeastSquare.spline2spline(
old_knotvector, new_knotvector
)
Expand Down Expand Up @@ -577,7 +587,9 @@ def non_rational_bezier_once(degree: int) -> Tuple[Tuple[float]]:
"""
if degree not in Derivate.__non_rat_bezier_once:
knotvector = nurbs.GeneratorKnotVector.bezier(degree, Fraction)
matrix = nurbs.heavy.Calculus.derivate_nonrational_bezier(knotvector)
matrix = nurbs.heavy.Calculus.derivate_nonrational_bezier(
knotvector
)
Derivate.__non_rat_bezier_once[degree] = tuple(matrix)
return Derivate.__non_rat_bezier_once[degree]

Expand Down Expand Up @@ -696,8 +708,12 @@ def area(curve: PlanarCurve, nnodes: Optional[int] = None):
def winding_number_linear(
pointa: Point2D, pointb: Point2D, center: Point2D
) -> float:
anglea = np.arctan2(float(pointa[1] - center[1]), float(pointa[0] - center[0]))
angleb = np.arctan2(float(pointb[1] - center[1]), float(pointb[0] - center[0]))
anglea = np.arctan2(
float(pointa[1] - center[1]), float(pointa[0] - center[0])
)
angleb = np.arctan2(
float(pointb[1] - center[1]), float(pointb[0] - center[0])
)
wind = (angleb - anglea) / math.tau
if abs(wind) < 0.5:
return wind
Expand All @@ -718,5 +734,7 @@ def winding_number(
total = 0
for pair_node in zip(nodes[:-1], nodes[1:]):
pointa, pointb = curve.eval(pair_node)
total += IntegratePlanar.winding_number_linear(pointa, pointb, center)
total += IntegratePlanar.winding_number_linear(
pointa, pointb, center
)
return total
21 changes: 16 additions & 5 deletions src/compmec/shape/jordancurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ def from_vertices(cls, vertices: Tuple[Point2D]) -> JordanCurve:
return cls.from_segments(beziers)

@classmethod
def from_ctrlpoints(cls, all_ctrlpoints: Tuple[Tuple[Point2D]]) -> JordanCurve:
def from_ctrlpoints(
cls, all_ctrlpoints: Tuple[Tuple[Point2D]]
) -> JordanCurve:
"""Initialize a JordanCurve from a list of control points,
:param all_ctrlpoints: The list of bezier control points
Expand Down Expand Up @@ -285,7 +287,9 @@ def clean(self) -> JordanCurve:
end_point = seg1.ctrlpoints[-1]
segment = seg0 | seg1
segment.ctrlpoints = (
[start_point] + list(segment.ctrlpoints[1:-1]) + [end_point]
[start_point]
+ list(segment.ctrlpoints[1:-1])
+ [end_point]
)
segments[i] = segment
segments.pop(j)
Expand Down Expand Up @@ -517,7 +521,9 @@ def points(self, subnpts: Optional[int] = None) -> Tuple[Tuple[float]]:
assert subnpts is None or (isinstance(subnpts, int) and subnpts >= 0)
all_points = []
for segment in self.segments:
npts = subnpts if subnpts is not None else 10 * (segment.degree - 1)
npts = (
subnpts if subnpts is not None else 10 * (segment.degree - 1)
)
usample = tuple(Fraction(num, npts + 1) for num in range(npts + 1))
points = segment.eval(usample)
all_points += list(tuple(point) for point in points)
Expand Down Expand Up @@ -620,7 +626,9 @@ def segments(self, other: Tuple[PlanarCurve]):
segments.append(new_bezier)
self.__segments = tuple(segments)

def __and__(self, other: JordanCurve) -> Tuple[Tuple[int, int, float, float]]:
def __and__(
self, other: JordanCurve
) -> Tuple[Tuple[int, int, float, float]]:
"""Computes the intersection of two jordan curves"""
return self.intersection(other, equal_beziers=False, end_points=False)

Expand Down Expand Up @@ -729,7 +737,10 @@ def __intersection(
return list(intersections)

def intersection(
self, other: JordanCurve, equal_beziers: bool = True, end_points: bool = True
self,
other: JordanCurve,
equal_beziers: bool = True,
end_points: bool = True,
) -> Tuple[Tuple[int, int, float, float]]:
"""Computes the intersection between two jordan curves
Expand Down
8 changes: 6 additions & 2 deletions src/compmec/shape/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def plot_shape(self, shape: BaseShape, *, kwargs={}):
fill_color = kwargs.pop("fill_color")
alpha = kwargs.pop("alpha")
marker = kwargs.pop("marker")
connecteds = shape.subshapes if isinstance(shape, DisjointShape) else [shape]
connecteds = (
shape.subshapes if isinstance(shape, DisjointShape) else [shape]
)
for connected in connecteds:
path = path_shape(connected)
if float(connected) > 0:
Expand All @@ -135,7 +137,9 @@ def plot_shape(self, shape: BaseShape, *, kwargs={}):
for jordan in connected.jordans:
path = path_jordan(jordan)
color = pos_color if float(jordan) > 0 else neg_color
patch = PathPatch(path, edgecolor=color, facecolor="none", lw=2)
patch = PathPatch(
path, edgecolor=color, facecolor="none", lw=2
)
self.gca().add_patch(patch)
xvals, yvals = np.array(jordan.points(0), dtype="float64").T
self.gca().scatter(xvals, yvals, color=color, marker=marker)
12 changes: 9 additions & 3 deletions src/compmec/shape/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,18 @@ def __str__(self) -> str:
if isinstance(self._x, fractions.Fraction):
xmsg = str(self._x.numerator)
xmsg += (
"" if (self._x.denominator == 1) else ("/" + str(self._x.denominator))
""
if (self._x.denominator == 1)
else ("/" + str(self._x.denominator))
)
else:
xmsg = str(self._x)
if isinstance(self._y, fractions.Fraction):
ymsg = str(self._y.numerator)
ymsg += (
"" if (self._y.denominator == 1) else ("/" + str(self._y.denominator))
""
if (self._y.denominator == 1)
else ("/" + str(self._y.denominator))
)
else:
ymsg = str(self._y)
Expand Down Expand Up @@ -223,7 +227,9 @@ def __bool__(self) -> bool:

def __float__(self) -> float:
"""Returns the area of the box"""
return (self.toppt[0] - self.lowpt[0]) * (self.toppt[1] - self.lowpt[1])
return (self.toppt[0] - self.lowpt[0]) * (
self.toppt[1] - self.lowpt[1]
)

def __contains__(self, point: Point2D) -> bool:
if point[0] < self.lowpt[0] - self.dx:
Expand Down
10 changes: 8 additions & 2 deletions src/compmec/shape/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
from compmec.shape.curve import PlanarCurve
from compmec.shape.jordancurve import JordanCurve
from compmec.shape.polygon import Point2D
from compmec.shape.shape import ConnectedShape, EmptyShape, SimpleShape, WholeShape
from compmec.shape.shape import (
ConnectedShape,
EmptyShape,
SimpleShape,
WholeShape,
)


class Primitive:
"""
Primitive class with functions to create classical shapes such as `circle`, `triangle`, `square`, `regular_polygon` and a generic `polygon`.
Primitive class with functions to create classical shapes such as
`circle`, `triangle`, `square`, `regular_polygon` and a generic `polygon`
.. note:: This class also contains ``empty`` and ``whole`` instances to easy access
Expand Down
36 changes: 27 additions & 9 deletions src/compmec/shape/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,12 @@ def midpoints_one_shape(
def midpoints_shapes(
shapea: BaseShape, shapeb: BaseShape, closed: bool, inside: bool
) -> Tuple[Tuple[int]]:
indexsa = FollowPath.midpoints_one_shape(shapea, shapeb, closed, inside)
indexsb = FollowPath.midpoints_one_shape(shapeb, shapea, closed, inside)
indexsa = FollowPath.midpoints_one_shape(
shapea, shapeb, closed, inside
)
indexsb = FollowPath.midpoints_one_shape(
shapeb, shapea, closed, inside
)
indexsa = list(indexsa)
njordansa = len(shapea.jordans)
for indjorb, indsegb in indexsb:
Expand All @@ -326,7 +330,9 @@ def or_shapes(shapea: BaseShape, shapeb: BaseShape) -> Tuple[JordanCurve]:
for jordana in shapea.jordans:
for jordanb in shapeb.jordans:
FollowPath.split_two_jordans(jordana, jordanb)
indexs = FollowPath.midpoints_shapes(shapea, shapeb, closed=True, inside=False)
indexs = FollowPath.midpoints_shapes(
shapea, shapeb, closed=True, inside=False
)
all_jordans = tuple(shapea.jordans) + tuple(shapeb.jordans)
new_jordans = FollowPath.follow_path(all_jordans, indexs)
return new_jordans
Expand All @@ -338,7 +344,9 @@ def and_shapes(shapea: BaseShape, shapeb: BaseShape) -> Tuple[JordanCurve]:
for jordana in shapea.jordans:
for jordanb in shapeb.jordans:
FollowPath.split_two_jordans(jordana, jordanb)
indexs = FollowPath.midpoints_shapes(shapea, shapeb, closed=False, inside=True)
indexs = FollowPath.midpoints_shapes(
shapea, shapeb, closed=False, inside=True
)
all_jordans = tuple(shapea.jordans) + tuple(shapeb.jordans)
new_jordans = FollowPath.follow_path(all_jordans, indexs)
return new_jordans
Expand Down Expand Up @@ -569,7 +577,9 @@ def __and__(self, other: BaseShape) -> BaseShape:
return EmptyShape()
return ShapeFromJordans(new_jordans)

def __contains__(self, other: Union[Point2D, JordanCurve, BaseShape]) -> bool:
def __contains__(
self, other: Union[Point2D, JordanCurve, BaseShape]
) -> bool:
if isinstance(other, BaseShape):
return self.contains_shape(other)
if isinstance(other, JordanCurve):
Expand Down Expand Up @@ -657,7 +667,9 @@ def rotate(self, angle: float, degrees: bool = False) -> BaseShape:
jordan.rotate(angle, degrees)
return self

def contains_point(self, point: Point2D, boundary: Optional[bool] = True) -> bool:
def contains_point(
self, point: Point2D, boundary: Optional[bool] = True
) -> bool:
"""
Checks if given point is inside the shape
Expand Down Expand Up @@ -853,7 +865,9 @@ def invert(self) -> SimpleShape:
self.__jordancurve.invert()
return self

def _contains_point(self, point: Point2D, boundary: Optional[bool] = True) -> bool:
def _contains_point(
self, point: Point2D, boundary: Optional[bool] = True
) -> bool:
jordan = self.jordans[0]
wind = IntegrateJordan.winding_number(jordan, center=point)
if float(jordan) > 0:
Expand Down Expand Up @@ -1009,7 +1023,9 @@ def subshapes(self, values: Tuple[SimpleShape]):
values = tuple(val[1] for val in values)
self.__subshapes = tuple(values)

def _contains_point(self, point: Point2D, boundary: Optional[bool] = True) -> bool:
def _contains_point(
self, point: Point2D, boundary: Optional[bool] = True
) -> bool:
for subshape in self.subshapes:
if not subshape.contains_point(point, boundary):
return False
Expand Down Expand Up @@ -1088,7 +1104,9 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return self.__str__()

def _contains_point(self, point: Point2D, boundary: Optional[bool] = True) -> bool:
def _contains_point(
self, point: Point2D, boundary: Optional[bool] = True
) -> bool:
for subshape in self.subshapes:
if subshape.contains_point(point, boundary):
return True
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ deps =
pre-commit
commands =
pre-commit run --all-files

[flake8]
per-file-ignores = __init__.py:F401

0 comments on commit 11a0f72

Please sign in to comment.