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
19 changes: 7 additions & 12 deletions src/shapepy/bool2d/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import math
from copy import copy
from numbers import Real
from typing import Tuple

import numpy as np
Expand All @@ -18,7 +17,7 @@
from shapepy.geometry.jordancurve import JordanCurve
from shapepy.geometry.point import Point2D

from ..scalar.reals import To
from ..tools import Is, To


class Primitive:
Expand Down Expand Up @@ -68,13 +67,9 @@ def regular_polygon(
.. image:: ../img/primitive/regular5.svg

"""
if not isinstance(nsides, int):
if not Is.integer(nsides) or nsides < 3:
raise ValueError
if nsides < 3:
raise ValueError
if not isinstance(radius, Real):
raise ValueError
if radius <= 0:
if not Is.finite(radius) or radius <= 0:
raise ValueError
center = To.point(center)
if nsides == 4:
Expand Down Expand Up @@ -170,11 +165,11 @@ def square(side: float = 1, center: Point2D = (0, 0)) -> SimpleShape:
.. image:: ../img/primitive/square.svg

"""
if not isinstance(side, Real) or side <= 0:
if not Is.finite(side) or side <= 0:
raise ValueError
center = To.point(center)

if isinstance(side, int):
if Is.integer(side):
side = To.rational(side)
side /= 2
vertices = [(side, side), (-side, side), (-side, -side), (side, -side)]
Expand Down Expand Up @@ -218,9 +213,9 @@ def circle(
terms by changing ``ndivangle``.

"""
if not isinstance(radius, Real) or radius <= 0:
if not Is.finite(radius) or radius <= 0:
raise ValueError
if not isinstance(ndivangle, int) or ndivangle < 4:
if not Is.integer(ndivangle) or ndivangle < 4:
raise ValueError
center = To.point(center)

Expand Down
92 changes: 46 additions & 46 deletions src/shapepy/bool2d/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from shapepy.geometry.jordancurve import IntegrateJordan, JordanCurve
from shapepy.geometry.point import Point2D

from ..scalar.reals import To
from ..tools import Is, To


class SuperclassMeta(type):
Expand Down Expand Up @@ -88,10 +88,10 @@ def polynomial(


"""
assert isinstance(shape, BaseShape)
assert isinstance(expx, int)
assert isinstance(expy, int)
assert nnodes is None or isinstance(nnodes, int)
assert Is.instance(shape, BaseShape)
assert Is.integer(expx)
assert Is.integer(expy)
assert nnodes is None or Is.integer(nnodes)
total = 0
for jordan in shape.jordans:
total += IntegrateJordan.vertical(jordan, expx + 1, expy, nnodes)
Expand Down Expand Up @@ -148,8 +148,8 @@ def split_two_jordans(jordana: JordanCurve, jordanb: JordanCurve):
Find the intersections between two jordan curves and call split on the
nodes which intersects
"""
assert isinstance(jordana, JordanCurve)
assert isinstance(jordanb, JordanCurve)
assert Is.instance(jordana, JordanCurve)
assert Is.instance(jordanb, JordanCurve)
if jordana.box() & jordanb.box() is None:
return
all_positions = (set(), set())
Expand Down Expand Up @@ -211,8 +211,8 @@ def is_rotation(oneobj: Tuple[Any], other: Tuple[Any]) -> bool:
"""
Tells if a list is equal to another
"""
assert isinstance(oneobj, (tuple, list))
assert isinstance(other, (tuple, list))
assert Is.iterable(oneobj)
assert Is.iterable(other)
oneobj = tuple(oneobj)
other = tuple(other)
if len(oneobj) != len(other):
Expand Down Expand Up @@ -277,7 +277,7 @@ def follow_path(
of the intersection between 'jordansa' and 'jordansb'
"""
for jordan in jordans:
assert isinstance(jordan, JordanCurve)
assert Is.instance(jordan, JordanCurve)
bez_indexs = []
for ind_jord, ind_seg in start_indexs:
indices_matrix = FollowPath.pursue_path(ind_jord, ind_seg, jordans)
Expand Down Expand Up @@ -342,8 +342,8 @@ def or_shapes(shapea: BaseShape, shapeb: BaseShape) -> Tuple[JordanCurve]:
Computes the set of jordan curves that defines the boundary of
the union between the two base shapes
"""
assert isinstance(shapea, BaseShape)
assert isinstance(shapeb, BaseShape)
assert Is.instance(shapea, BaseShape)
assert Is.instance(shapeb, BaseShape)
for jordana in shapea.jordans:
for jordanb in shapeb.jordans:
FollowPath.split_two_jordans(jordana, jordanb)
Expand All @@ -360,8 +360,8 @@ def and_shapes(shapea: BaseShape, shapeb: BaseShape) -> Tuple[JordanCurve]:
Computes the set of jordan curves that defines the boundary of
the intersection between the two base shapes
"""
assert isinstance(shapea, BaseShape)
assert isinstance(shapeb, BaseShape)
assert Is.instance(shapea, BaseShape)
assert Is.instance(shapeb, BaseShape)
for jordana in shapea.jordans:
for jordanb in shapeb.jordans:
FollowPath.split_two_jordans(jordana, jordanb)
Expand Down Expand Up @@ -571,10 +571,10 @@ def __invert__(self) -> BaseShape:
return shape_from_jordans(tuple(~jordan for jordan in self.jordans))

def __or__(self, other: BaseShape) -> BaseShape:
assert isinstance(other, BaseShape)
if isinstance(other, WholeShape):
assert Is.instance(other, BaseShape)
if Is.instance(other, WholeShape):
return WholeShape()
if isinstance(other, EmptyShape):
if Is.instance(other, EmptyShape):
return copy(self)
if other in self:
return copy(self)
Expand All @@ -586,10 +586,10 @@ def __or__(self, other: BaseShape) -> BaseShape:
return shape_from_jordans(new_jordans)

def __and__(self, other: BaseShape) -> BaseShape:
assert isinstance(other, BaseShape)
if isinstance(other, WholeShape):
assert Is.instance(other, BaseShape)
if Is.instance(other, WholeShape):
return copy(self)
if isinstance(other, EmptyShape):
if Is.instance(other, EmptyShape):
return EmptyShape()
if other in self:
return copy(other)
Expand All @@ -603,9 +603,9 @@ def __and__(self, other: BaseShape) -> BaseShape:
def __contains__(
self, other: Union[Point2D, JordanCurve, BaseShape]
) -> bool:
if isinstance(other, BaseShape):
if Is.instance(other, BaseShape):
return self.contains_shape(other)
if isinstance(other, JordanCurve):
if Is.instance(other, JordanCurve):
return self.contains_jordan(other)
point = To.point(other)
return self.contains_point(point)
Expand Down Expand Up @@ -723,7 +723,7 @@ def contains_point(

"""
point = To.point(point)
assert isinstance(boundary, bool)
assert Is.bool(boundary)
return self._contains_point(point, boundary)

def contains_jordan(
Expand Down Expand Up @@ -754,8 +754,8 @@ def contains_jordan(
True

"""
assert isinstance(jordan, JordanCurve)
assert isinstance(boundary, bool)
assert Is.instance(jordan, JordanCurve)
assert Is.bool(boundary)
return self._contains_jordan(jordan, boundary)

def contains_shape(self, other: BaseShape) -> bool:
Expand All @@ -782,10 +782,10 @@ def contains_shape(self, other: BaseShape) -> bool:
True

"""
assert isinstance(other, BaseShape)
if isinstance(other, EmptyShape):
assert Is.instance(other, BaseShape)
if Is.instance(other, EmptyShape):
return True
if isinstance(other, WholeShape):
if Is.instance(other, WholeShape):
return False
return self._contains_shape(other)

Expand Down Expand Up @@ -815,7 +815,7 @@ class SimpleShape(DefinedShape):
"""

def __init__(self, jordancurve: JordanCurve):
assert isinstance(jordancurve, JordanCurve)
assert Is.instance(jordancurve, JordanCurve)
super().__init__()
self.__set_jordancurve(jordancurve)

Expand All @@ -842,9 +842,9 @@ def __eq__(self, other: BaseShape) -> bool:

:raises ValueError: If ``other`` is not a BaseShape instance
"""
if not isinstance(other, BaseShape):
if not Is.instance(other, BaseShape):
raise ValueError
if not isinstance(other, SimpleShape):
if not Is.instance(other, SimpleShape):
return False
if float(self) != float(other):
return False
Expand All @@ -863,7 +863,7 @@ def jordans(self) -> Tuple[JordanCurve]:
return (self.__jordancurve,)

def __set_jordancurve(self, other: JordanCurve):
assert isinstance(other, JordanCurve)
assert Is.instance(other, JordanCurve)
self.__jordancurve = copy(other)

def invert(self) -> SimpleShape:
Expand Down Expand Up @@ -928,10 +928,10 @@ def _contains_jordan(
return True

def _contains_shape(self, other: DefinedShape) -> bool:
assert isinstance(other, DefinedShape)
if isinstance(other, SimpleShape):
assert Is.instance(other, DefinedShape)
if Is.instance(other, SimpleShape):
return self.__contains_simple(other)
if isinstance(other, ConnectedShape):
if Is.instance(other, ConnectedShape):
# cap S_i in S_j = any_i (bar S_j in bar S_i)
contains = False
self.invert()
Expand All @@ -952,7 +952,7 @@ def _contains_shape(self, other: DefinedShape) -> bool:

# pylint: disable=chained-comparison
def __contains_simple(self, other: SimpleShape) -> bool:
assert isinstance(other, SimpleShape)
assert Is.instance(other, SimpleShape)
areaa = float(other)
areab = float(self)
jordana = other.jordans[0]
Expand Down Expand Up @@ -995,8 +995,8 @@ def __repr__(self) -> str:
return str(self)

def __eq__(self, other: BaseShape) -> bool:
assert isinstance(other, BaseShape)
if not isinstance(other, ConnectedShape):
assert Is.instance(other, BaseShape)
if not Is.instance(other, ConnectedShape):
return False
if abs(float(self) - float(other)) > 1e-6:
return False
Expand Down Expand Up @@ -1049,7 +1049,7 @@ def subshapes(self) -> Tuple[SimpleShape]:
@subshapes.setter
def subshapes(self, values: Tuple[SimpleShape]):
for value in values:
assert isinstance(value, SimpleShape)
assert Is.instance(value, SimpleShape)
areas = map(float, values)

def algori(pair):
Expand Down Expand Up @@ -1097,7 +1097,7 @@ def __new__(cls, subshapes: Tuple[ConnectedShape]):
if len(subshapes) == 0:
return EmptyShape()
for subshape in subshapes:
assert isinstance(subshape, (SimpleShape, ConnectedShape))
assert Is.instance(subshape, (SimpleShape, ConnectedShape))
if len(subshapes) == 1:
return copy(subshapes[0])
instance = super(DisjointShape, cls).__new__(cls)
Expand All @@ -1114,8 +1114,8 @@ def __float__(self) -> float:
return float(total)

def __eq__(self, other: BaseShape):
assert isinstance(other, BaseShape)
if not isinstance(other, DisjointShape):
assert Is.instance(other, BaseShape)
if not Is.instance(other, DisjointShape):
return False
if float(self) != float(other):
return False
Expand Down Expand Up @@ -1157,13 +1157,13 @@ def _contains_jordan(
return False

def _contains_shape(self, other: DefinedShape) -> bool:
assert isinstance(other, DefinedShape)
if isinstance(other, (SimpleShape, ConnectedShape)):
assert Is.instance(other, DefinedShape)
if Is.instance(other, (SimpleShape, ConnectedShape)):
for subshape in self.subshapes:
if other in subshape:
return True
return False
if isinstance(other, DisjointShape):
if Is.instance(other, DisjointShape):
for subshape in other.subshapes:
if subshape not in self:
return False
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def subshapes(self) -> Tuple[Union[SimpleShape, ConnectedShape]]:
@subshapes.setter
def subshapes(self, values: Tuple[BaseShape]):
for value in values:
assert isinstance(value, (SimpleShape, ConnectedShape))
assert Is.instance(value, (SimpleShape, ConnectedShape))
areas = map(float, values)
lenghts = map(float, [val.jordans[0] for val in values])

Expand Down
6 changes: 4 additions & 2 deletions src/shapepy/geometry/box.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from typing import Union

from ..tools import To
from .point import Point2D


Expand All @@ -26,8 +27,8 @@ class Box:
dy = 1e-6

def __init__(self, lowpt: Point2D, toppt: Point2D):
self.lowpt = lowpt
self.toppt = toppt
self.lowpt = To.point(lowpt)
self.toppt = To.point(toppt)

def __str__(self) -> str:
return f"Box with vertices {self.lowpt} and {self.toppt}"
Expand All @@ -42,6 +43,7 @@ def __float__(self) -> float:
)

def __contains__(self, point: Point2D) -> bool:
point = To.point(point)
if point[0] < self.lowpt[0] - self.dx:
return False
if point[1] < self.lowpt[1] - self.dy:
Expand Down
Loading