Skip to content
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 .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ exclude_lines =
raise NotExpectedError
raise ValueError
raise TypeError
omit =
loggers.py
__init__.py
22 changes: 13 additions & 9 deletions src/shapepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@

import importlib

from shapepy.bool2d.base import EmptyShape, WholeShape
from shapepy.bool2d.primitive import Primitive
from shapepy.bool2d.shape import ConnectedShape, DisjointShape, SimpleShape
from shapepy.common import move, rotate, scale
from shapepy.geometry.integral import IntegrateJordan
from shapepy.geometry.jordancurve import JordanCurve
from shapepy.geometry.point import Point2D
from shapepy.geometry.segment import Segment
from shapepy.plot.plot import ShapePloter
from .bool2d.base import EmptyShape, WholeShape
from .bool2d.primitive import Primitive
from .bool2d.shape import ConnectedShape, DisjointShape, SimpleShape
from .common import move, rotate, scale
from .geometry.integral import IntegrateJordan
from .geometry.jordancurve import JordanCurve
from .geometry.point import Point2D
from .geometry.segment import Segment
from .loggers import set_level
from .plot.plot import ShapePloter

__version__ = importlib.metadata.version("shapepy")

set_level("shapepy", level="INFO")


if __name__ == "__main__":
pass
6 changes: 6 additions & 0 deletions src/shapepy/analytic/bezier.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from rbool import SubSetR1, Whole

from ..loggers import debug
from ..scalar.quadrature import inner
from ..scalar.reals import Math, Rational, Real
from ..tools import Is, NotExpectedError, To
Expand Down Expand Up @@ -123,12 +124,14 @@ def __call__(self, node: Real, derivate: int = 0) -> Real:
def __str__(self):
return str(self.__polynomial)

@debug("shapepy.analytic.bezier")
def clean(self) -> Bezier:
"""
Decreases the degree of the bezier curve if possible
"""
return polynomial2bezier(bezier2polynomial(self).clean())

@debug("shapepy.analytic.bezier")
def scale(self, amount: Real) -> Bezier:
"""
Transforms the polynomial p(t) into p(A*t) by
Expand All @@ -149,13 +152,15 @@ def scale(self, amount: Real) -> Bezier:
"""
return polynomial2bezier(bezier2polynomial(self).scale(amount))

@debug("shapepy.analytic.bezier")
def shift(self, amount: Real) -> Bezier:
"""
Transforms the bezier p(t) into p(t-d) by
translating the bezier by 'd' to the right.
"""
return polynomial2bezier(bezier2polynomial(self).shift(amount))

@debug("shapepy.analytic.bezier")
def integrate(self, times: int = 1) -> Bezier:
"""
Integrates the bezier analytic
Expand All @@ -171,6 +176,7 @@ def integrate(self, times: int = 1) -> Bezier:
"""
return polynomial2bezier(bezier2polynomial(self).integrate(times))

@debug("shapepy.analytic.bezier")
def derivate(self, times: int = 1) -> Bezier:
"""
Derivate the bezier curve, giving a new one
Expand Down
10 changes: 8 additions & 2 deletions src/shapepy/analytic/polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from rbool import move, scale

from ..loggers import debug
from ..scalar.reals import Math
from ..tools import Is, To, vectorize
from .base import BaseAnalytic, IAnalytic
Expand Down Expand Up @@ -103,9 +104,9 @@ def __str__(self):
msgs: List[str] = []
flag = False
for i, coef in enumerate(self):
if coef == 0:
if coef * coef == 0:
continue
msg = "- " if coef < 0 else "+ " if flag else ""
msg = "- " if Is.real(coef) and coef < 0 else "+ " if flag else ""
flag = True
coef = abs(coef)
if coef != 1 or i == 0:
Expand All @@ -119,13 +120,15 @@ def __str__(self):
msgs.append(msg)
return " ".join(msgs)

@debug("shapepy.analytic.polynomial")
def clean(self) -> Polynomial:
"""
Decreases the degree of the bezier curve if possible
"""
degree = max((i for i, v in enumerate(self) if v * v > 0), default=0)
return Polynomial(self[: degree + 1], self.domain)

@debug("shapepy.analytic.polynomial")
def scale(self, amount: Real) -> Polynomial:
"""
Transforms the polynomial p(t) into p(A*t) by
Expand All @@ -148,6 +151,7 @@ def scale(self, amount: Real) -> Polynomial:
coefs = tuple(coef * inv**i for i, coef in enumerate(self))
return Polynomial(coefs, scale(self.domain, amount))

@debug("shapepy.analytic.polynomial")
def shift(self, amount: Real) -> Polynomial:
"""
Transforms the polynomial p(t) into p(t-d) by
Expand Down Expand Up @@ -175,6 +179,7 @@ def shift(self, amount: Real) -> Polynomial:
newcoefs[j] += coef * value
return Polynomial(newcoefs, move(self.domain, amount))

@debug("shapepy.analytic.polynomial")
def integrate(self, times: int = 1) -> Polynomial:
"""
Integrates the polynomial curve
Expand All @@ -197,6 +202,7 @@ def integrate(self, times: int = 1) -> Polynomial:
polynomial = Polynomial(newcoefs, self.domain)
return polynomial

@debug("shapepy.analytic.polynomial")
def derivate(self, times: int = 1) -> Polynomial:
"""
Derivate the polynomial curve, giving a new one
Expand Down
4 changes: 4 additions & 0 deletions src/shapepy/analytic/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
unite,
)

from ..loggers import debug
from ..scalar.reals import Math, Real
from ..tools import Is, NotExpectedError, To
from .base import IAnalytic, derivate_analytic
Expand Down Expand Up @@ -101,6 +102,7 @@ def find_minimum_polynomial(
)


@debug("shapepy.analytic.tools")
def find_roots(analytic: IAnalytic, domain: SubSetR1 = Whole()) -> SubSetR1:
"""
Finds the values of roots of the Analytic function
Expand All @@ -114,6 +116,7 @@ def find_roots(analytic: IAnalytic, domain: SubSetR1 = Whole()) -> SubSetR1:
raise NotExpectedError


@debug("shapepy.analytic.tools")
def where_minimum(analytic: IAnalytic, domain: SubSetR1 = Whole()) -> SubSetR1:
"""
Finds the parameters (t*) such the analytic function is minimum
Expand All @@ -127,6 +130,7 @@ def where_minimum(analytic: IAnalytic, domain: SubSetR1 = Whole()) -> SubSetR1:
raise NotExpectedError


@debug("shapepy.analytic.tools")
def find_minimum(analytic: IAnalytic, domain: SubSetR1 = Whole()) -> SubSetR1:
"""
Finds the minimal value for the given analytic in the given domain
Expand Down
7 changes: 7 additions & 0 deletions src/shapepy/bool2d/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Iterable, Tuple, Union

from ..geometry.point import Point2D
from ..loggers import debug
from ..scalar.angle import Angle
from ..scalar.reals import Real

Expand All @@ -29,10 +30,12 @@ def __init__(self):
def __invert__(self) -> SubSetR2:
"""Invert shape"""

@debug("shapepy.bool2d.base")
def __or__(self, other: SubSetR2) -> SubSetR2:
"""Union shapes"""
return Future.unite((self, other))

@debug("shapepy.bool2d.base")
def __and__(self, other: SubSetR2) -> SubSetR2:
"""Intersection shapes"""
return Future.intersect((self, other))
Expand All @@ -49,18 +52,22 @@ def __neg__(self) -> SubSetR2:
"""Invert the SubSetR2"""
return ~self

@debug("shapepy.bool2d.base")
def __add__(self, other: SubSetR2):
"""Union of SubSetR2"""
return self | other

@debug("shapepy.bool2d.base")
def __mul__(self, value: SubSetR2):
"""Intersection of SubSetR2"""
return self & value

@debug("shapepy.bool2d.base")
def __sub__(self, value: SubSetR2):
"""Subtraction of SubSetR2"""
return self & (~value)

@debug("shapepy.bool2d.base")
def __xor__(self, other: SubSetR2):
"""XOR of SubSetR2"""
return (self - other) | (other - self)
Expand Down
3 changes: 3 additions & 0 deletions src/shapepy/bool2d/boolean.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from ..geometry.intersection import GeometricIntersectionCurves
from ..geometry.unparam import USegment
from ..loggers import debug
from ..tools import CyclicContainer, Is
from .base import EmptyShape, SubSetR2, WholeShape
from .shape import (
Expand All @@ -23,6 +24,7 @@
)


@debug("shapepy.bool2d.boolean")
def unite(subsets: Iterable[SubSetR2]) -> SubSetR2:
"""
Computes the union of given subsets
Expand Down Expand Up @@ -55,6 +57,7 @@ def unite(subsets: Iterable[SubSetR2]) -> SubSetR2:
return shape_from_jordans(new_jordans)


@debug("shapepy.bool2d.boolean")
def intersect(subsets: Iterable[SubSetR2]) -> SubSetR2:
"""
Computes the intersection of given subsets
Expand Down
6 changes: 6 additions & 0 deletions src/shapepy/bool2d/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ..geometry.point import Point2D, cartesian
from ..geometry.segment import Segment
from ..geometry.unparam import USegment
from ..loggers import debug
from ..tools import Is, To
from .base import EmptyShape, WholeShape
from .shape import SimpleShape
Expand All @@ -36,6 +37,7 @@ class Primitive:
whole = WholeShape()

@staticmethod
@debug("shapepy.bool2d.primitive")
def regular_polygon(
nsides: int, radius: float = 1, center: Point2D = (0, 0)
) -> SimpleShape:
Expand Down Expand Up @@ -86,6 +88,7 @@ def regular_polygon(
return Primitive.polygon(vertices)

@staticmethod
@debug("shapepy.bool2d.primitive")
def polygon(vertices: Tuple[Point2D]) -> SimpleShape:
"""
Creates a generic polygon
Expand All @@ -112,6 +115,7 @@ def polygon(vertices: Tuple[Point2D]) -> SimpleShape:
return SimpleShape(jordan_curve)

@staticmethod
@debug("shapepy.bool2d.primitive")
def triangle(side: float = 1, center: Point2D = (0, 0)) -> SimpleShape:
"""
Create a right triangle
Expand Down Expand Up @@ -142,6 +146,7 @@ def triangle(side: float = 1, center: Point2D = (0, 0)) -> SimpleShape:
return Primitive.polygon(vertices)

@staticmethod
@debug("shapepy.bool2d.primitive")
def square(side: float = 1, center: Point2D = (0, 0)) -> SimpleShape:
"""
Creates a square with sides aligned with axis
Expand Down Expand Up @@ -179,6 +184,7 @@ def square(side: float = 1, center: Point2D = (0, 0)) -> SimpleShape:
return Primitive.polygon(vertices)

@staticmethod
@debug("shapepy.bool2d.primitive")
def circle(
radius: float = 1, center: Point2D = (0, 0), ndivangle: int = 16
) -> SimpleShape:
Expand Down
5 changes: 1 addition & 4 deletions src/shapepy/bool2d/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from copy import copy
from typing import Iterable, Set, Tuple, Union

import numpy as np

from ..geometry.box import Box
from ..geometry.integral import winding_number
from ..geometry.jordancurve import JordanCurve
Expand Down Expand Up @@ -48,8 +46,7 @@ def __deepcopy__(self, memo) -> SimpleShape:
def __str__(self) -> str: # pragma: no cover # For debug
area = float(self.area)
vertices = tuple(map(tuple, self.jordan.vertices))
vertices = np.array(vertices, dtype="float64")
return f"Simple Shape of area {area:.2f} with vertices:\n{vertices}"
return f"SimpleShape[{area:.2f}]:[{vertices}]"

def __eq__(self, other: SubSetR2) -> bool:
"""Compare two shapes
Expand Down
3 changes: 3 additions & 0 deletions src/shapepy/geometry/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import Tuple

from ..loggers import debug
from ..tools import To
from .jordancurve import JordanCurve
from .point import Point2D
Expand All @@ -17,6 +18,7 @@ class FactoryJordan:
"""

@staticmethod
@debug("shapepy.geometry.factory")
def polygon(vertices: Tuple[Point2D, ...]) -> JordanCurve:
"""Initialize a polygonal JordanCurve from a list of vertices,

Expand Down Expand Up @@ -46,6 +48,7 @@ def polygon(vertices: Tuple[Point2D, ...]) -> JordanCurve:
return JordanCurve(beziers)

@staticmethod
@debug("shapepy.geometry.factory")
def spline_curve(spline_curve) -> JordanCurve:
"""Initialize a JordanCurve from a spline curve,

Expand Down
2 changes: 2 additions & 0 deletions src/shapepy/geometry/integral.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ..analytic.base import IAnalytic
from ..analytic.tools import find_minimum
from ..common import derivate
from ..loggers import debug
from ..scalar.angle import Angle
from ..scalar.quadrature import AdaptativeIntegrator, IntegratorFactory
from ..scalar.reals import Math
Expand Down Expand Up @@ -64,6 +65,7 @@ def polynomial(jordan: JordanCurve, expx: int, expy: int):


# pylint: disable=too-many-locals
@debug("shapepy.geometry.integral")
def winding_number(
jordan: JordanCurve, center: Optional[Point2D] = (0.0, 0.0)
) -> Union[int, float]:
Expand Down
Loading
Loading