Skip to content

Geometry

momapy.geometry

Classes and functions for geometry

Classes:

Name Description
Bbox

Class for bounding boxes

BezierCurve

Class for bezier curves

EllipticalArc

Class for elliptical arcs

GeometryObject

Base class for geometry objects

Line

Class for lines

MatrixTransformation

Class for matrix transformations

Point

Class for points

Rotation

Class for rotations

Scaling

Class for scalings

Segment

Class for segments

Transformation

Base class for transformations

Translation

Class for translations

Bbox dataclass

Bbox(position: Point, width: float, height: float)

Bases: object

Class for bounding boxes

Methods:

Name Description
anchor_point

Return a given anchor point of the bounding box

center

Return the center anchor point of the bounding box

east

Return the east anchor point of the bounding box

east_north_east

Return the east north east anchor point of the bounding box

east_south_east

Return the east south east anchor point of the bounding box

from_bounds

Create and return a bounding box from shaply bounds (min_x, min_y, max_x, max_y)

isnan

Return true if the position of the bounding box has a nan coordinate, false otherwise

north

Return the north anchor point of the bounding box

north_east

Return the north east anchor point of the bounding box

north_north_east

Return the north north east anchor point of the bounding box

north_north_west

Return the north north west anchor point of the bounding box

north_west

Return the north west anchor point of the bounding box

size

The size of the bounding box

south

Return the south anchor point of the bounding box

south_east

Return the south east anchor point of the bounding box

south_south_east

Return the south south east anchor point of the bounding box

south_south_west

Return the south south west anchor point of the bounding box

south_west

Return the south west anchor point of the bounding box

west

Return the west anchor point of the bounding box

west_north_west

Return the west north west anchor point of the bounding box

west_south_west

Return the west south west anchor point of the bounding box

Attributes:

Name Type Description
x float

The x coordinate of the bounding box

y float

The y coordinate of the bounding box

anchor_point

anchor_point(anchor_point: str) -> Point

Return a given anchor point of the bounding box

Source code in src/momapy/geometry.py
def anchor_point(self, anchor_point: str) -> Point:
    """Return a given anchor point of the bounding box"""
    return getattr(self, anchor_point)()

center

center() -> Point

Return the center anchor point of the bounding box

Source code in src/momapy/geometry.py
def center(self) -> Point:
    """Return the center anchor point of the bounding box"""
    return Point(self.x, self.y)

east

east() -> Point

Return the east anchor point of the bounding box

Source code in src/momapy/geometry.py
def east(self) -> Point:
    """Return the east anchor point of the bounding box"""
    return Point(self.x + self.width / 2, self.y)

east_north_east

east_north_east() -> Point

Return the east north east anchor point of the bounding box

Source code in src/momapy/geometry.py
def east_north_east(self) -> Point:
    """Return the east north east anchor point of the bounding box"""
    return Point(self.x + self.width / 2, self.y - self.height / 4)

east_south_east

east_south_east() -> Point

Return the east south east anchor point of the bounding box

Source code in src/momapy/geometry.py
def east_south_east(self) -> Point:
    """Return the east south east anchor point of the bounding box"""
    return Point(self.x + self.width / 2, self.y + self.width / 4)

from_bounds classmethod

from_bounds(bounds: tuple[float, float, float, float])

Create and return a bounding box from shaply bounds (min_x, min_y, max_x, max_y)

Source code in src/momapy/geometry.py
@classmethod
def from_bounds(cls, bounds: tuple[float, float, float, float]):
    """Create and return a bounding box from shaply bounds (min_x, min_y, max_x, max_y)"""
    return cls(
        Point((bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2),
        bounds[2] - bounds[0],
        bounds[3] - bounds[1],
    )

isnan

isnan() -> bool

Return true if the position of the bounding box has a nan coordinate, false otherwise

Source code in src/momapy/geometry.py
def isnan(self) -> bool:
    """Return `true` if the position of the bounding box has a `nan` coordinate, `false` otherwise"""
    return self.position.isnan()

north

north() -> Point

Return the north anchor point of the bounding box

Source code in src/momapy/geometry.py
def north(self) -> Point:
    """Return the north anchor point of the bounding box"""
    return Point(self.x, self.y - self.height / 2)

north_east

north_east() -> Point

Return the north east anchor point of the bounding box

Source code in src/momapy/geometry.py
def north_east(self) -> Point:
    """Return the north east anchor point of the bounding box"""
    return Point(self.x + self.width / 2, self.y - self.height / 2)

north_north_east

north_north_east() -> Point

Return the north north east anchor point of the bounding box

Source code in src/momapy/geometry.py
def north_north_east(self) -> Point:
    """Return the north north east anchor point of the bounding box"""
    return Point(self.x + self.width / 4, self.y - self.height / 2)

north_north_west

north_north_west() -> Point

Return the north north west anchor point of the bounding box

Source code in src/momapy/geometry.py
def north_north_west(self) -> Point:
    """Return the north north west anchor point of the bounding box"""
    return Point(self.x - self.width / 4, self.y - self.height / 2)

north_west

north_west() -> Point

Return the north west anchor point of the bounding box

Source code in src/momapy/geometry.py
def north_west(self) -> Point:
    """Return the north west anchor point of the bounding box"""
    return Point(self.x - self.width / 2, self.y - self.height / 2)

size

size() -> tuple[float, float]

The size of the bounding box

Source code in src/momapy/geometry.py
def size(self) -> tuple[float, float]:
    """The size of the bounding box"""
    return (self.width, self.height)

south

south() -> Point

Return the south anchor point of the bounding box

Source code in src/momapy/geometry.py
def south(self) -> Point:
    """Return the south anchor point of the bounding box"""
    return Point(self.x, self.y + self.height / 2)

south_east

south_east() -> Point

Return the south east anchor point of the bounding box

Source code in src/momapy/geometry.py
def south_east(self) -> Point:
    """Return the south east anchor point of the bounding box"""
    return Point(self.x + self.width / 2, self.y + self.height / 2)

south_south_east

south_south_east() -> Point

Return the south south east anchor point of the bounding box

Source code in src/momapy/geometry.py
def south_south_east(self) -> Point:
    """Return the south south east anchor point of the bounding box"""
    return Point(self.x + self.width / 4, self.y + self.height / 2)

south_south_west

south_south_west() -> Point

Return the south south west anchor point of the bounding box

Source code in src/momapy/geometry.py
def south_south_west(self) -> Point:
    """Return the south south west anchor point of the bounding box"""
    return Point(self.x - self.width / 4, self.y + self.height / 2)

south_west

south_west() -> Point

Return the south west anchor point of the bounding box

Source code in src/momapy/geometry.py
def south_west(self) -> Point:
    """Return the south west anchor point of the bounding box"""
    return Point(self.x - self.width / 2, self.y + self.height / 2)

west

west() -> Point

Return the west anchor point of the bounding box

Source code in src/momapy/geometry.py
def west(self) -> Point:
    """Return the west anchor point of the bounding box"""
    return Point(self.x - self.width / 2, self.y)

west_north_west

west_north_west() -> Point

Return the west north west anchor point of the bounding box

Source code in src/momapy/geometry.py
def west_north_west(self) -> Point:
    """Return the west north west anchor point of the bounding box"""
    return Point(self.x - self.width / 2, self.y - self.height / 4)

west_south_west

west_south_west() -> Point

Return the west south west anchor point of the bounding box

Source code in src/momapy/geometry.py
def west_south_west(self) -> Point:
    """Return the west south west anchor point of the bounding box"""
    return Point(self.x - self.width / 2, self.y + self.height / 4)

x property

x: float

The x coordinate of the bounding box

y property

y: float

The y coordinate of the bounding box

BezierCurve dataclass

BezierCurve(p1: Point, p2: Point, control_points: tuple[Point, ...] = tuple())

Bases: GeometryObject

Class for bezier curves

Methods:

Name Description
bbox

Compute and return the bounding box of the bezier curve

evaluate

Compute and return the point at a given parameter value

evaluate_multi

Compute and return the points at given parameter values

get_angle_at_fraction

Compute and return the angle in radians formed by the tangent of the bezier curve and the horizontal at a given fraction (of the total length)

get_intersection_with_line

Compute and return the intersection of the bezier curve with a given line

get_position_and_angle_at_fraction

Compute and return the position on the bezier curve at a given fraction and the angle in radians formed of the tangent of the bezier curve and the horizontal at that position

get_position_at_fraction

Compute and return the position on the bezier curve at a given fraction (of the total length)

length

Compute and return the length of the bezier curve

reversed

Compute and return a reversed copy of the bezier curve

shortened

Compute and return a copy of the bezier curve shortened by a given length

to_shapely

Compute and return a shapely line string reproducing the bezier curve

transformed

Compute and return a copy of the bezier curve transformed with a given transformation

bbox

bbox()

Compute and return the bounding box of the bezier curve

Source code in src/momapy/geometry.py
def bbox(self):
    """Compute and return the bounding box of the bezier curve"""
    return Bbox.from_bounds(self.to_shapely().bounds)

evaluate

evaluate(s: float) -> Point

Compute and return the point at a given parameter value

Source code in src/momapy/geometry.py
def evaluate(self, s: float) -> Point:
    """Compute and return the point at a given parameter value"""
    evaluation = self._to_bezier().evaluate(s)
    return Point.from_fortranarray(evaluation)

evaluate_multi

evaluate_multi(s: ndarray) -> list[Point]

Compute and return the points at given parameter values

Source code in src/momapy/geometry.py
def evaluate_multi(self, s: numpy.ndarray) -> list[Point]:
    """Compute and return the points at given parameter values"""
    evaluation = self._to_bezier().evaluate_multi(s)
    return [Point(e[0], e[1]) for e in evaluation.T]

get_angle_at_fraction

get_angle_at_fraction(fraction: float)

Compute and return the angle in radians formed by the tangent of the bezier curve and the horizontal at a given fraction (of the total length)

Source code in src/momapy/geometry.py
def get_angle_at_fraction(self, fraction: float):
    """Compute and return the angle in radians formed by the tangent of the bezier curve and the horizontal at a given fraction (of the total length)"""
    return get_angle_at_fraction_of_bezier_curve(self, fraction)

get_intersection_with_line

get_intersection_with_line(line: Line) -> list[Point] | list[Segment]

Compute and return the intersection of the bezier curve with a given line

Source code in src/momapy/geometry.py
def get_intersection_with_line(self, line: Line) -> list[Point] | list[Segment]:
    """Compute and return the intersection of the bezier curve with a given line"""
    return get_intersection_of_line_and_bezier_curve(line, self)

get_position_and_angle_at_fraction

get_position_and_angle_at_fraction(fraction: float)

Compute and return the position on the bezier curve at a given fraction and the angle in radians formed of the tangent of the bezier curve and the horizontal at that position

Source code in src/momapy/geometry.py
def get_position_and_angle_at_fraction(self, fraction: float):
    """Compute and return the position on the bezier curve at a given fraction and the angle in radians formed of the tangent of the bezier curve and the horizontal at that position"""
    return get_position_and_angle_at_fraction_of_bezier_curve(self, fraction)

get_position_at_fraction

get_position_at_fraction(fraction: float)

Compute and return the position on the bezier curve at a given fraction (of the total length)

Source code in src/momapy/geometry.py
def get_position_at_fraction(self, fraction: float):
    """Compute and return the position on the bezier curve at a given fraction (of the total length)"""

    return get_position_at_fraction_of_bezier_curve(self, fraction)

length

length() -> float

Compute and return the length of the bezier curve

Source code in src/momapy/geometry.py
def length(self) -> float:
    """Compute and return the length of the bezier curve"""
    return self._to_bezier().length

reversed

reversed()

Compute and return a reversed copy of the bezier curve

Source code in src/momapy/geometry.py
def reversed(self):
    """Compute and return a reversed copy of the bezier curve"""
    return reverse_bezier_curve(self)

shortened

shortened(length: float, start_or_end: Literal['start', 'end'] = 'end') -> BezierCurve

Compute and return a copy of the bezier curve shortened by a given length

Source code in src/momapy/geometry.py
def shortened(
    self, length: float, start_or_end: typing.Literal["start", "end"] = "end"
) -> "BezierCurve":
    """Compute and return a copy of the bezier curve shortened by a given length"""
    return shorten_bezier_curve(self, length, start_or_end)

to_shapely

to_shapely(n_segs=50)

Compute and return a shapely line string reproducing the bezier curve

Source code in src/momapy/geometry.py
def to_shapely(self, n_segs=50):
    """Compute and return a shapely line string reproducing the bezier curve"""

    points = self.evaluate_multi(
        numpy.arange(0, 1 + 1 / n_segs, 1 / n_segs, dtype="double")
    )
    return shapely.LineString([point.to_tuple() for point in points])

transformed

transformed(transformation)

Compute and return a copy of the bezier curve transformed with a given transformation

Source code in src/momapy/geometry.py
def transformed(self, transformation):
    """Compute and return a copy of the bezier curve transformed with a given transformation"""
    return transform_bezier_curve(self, transformation)

EllipticalArc dataclass

EllipticalArc(p1: Point, p2: Point, rx: float, ry: float, x_axis_rotation: float, arc_flag: int, sweep_flag: int)

Bases: GeometryObject

Class for elliptical arcs

Methods:

Name Description
bbox

Compute and return the bounding box of the elliptical arc

get_angle_at_fraction

Compute and return the angle in radians formed by the tangent of the elliptical arc and the horizontal at a given fraction (of the total length)

get_center

Compute and return the center of the elliptical arc

get_center_parameterization

Compute and return the center paramaterizaion of the elliptical arc (cx, cy, rx, ry, sigma, theta1, theta2, delta_theta)

get_intersection_with_line

Compute and return the intersection of the elliptical arc with a given line

get_position_and_angle_at_fraction

Compute and return the position on the elliptical arc at a given fraction and the angle in radians formed of the tangent of the bezier curve at that position and the horizontal

get_position_at_fraction

Compute and return the position on the elliptical arc at a given fraction (of the total length)

reversed

Compute and return a reversed copy of the elliptical arc

shortened

Compute and return a copy of the elliptical arc shortened by a given length

to_bezier_curves

Compute and return a bezier curve reproducing the elliptical arc

to_shapely

Compute and return a shapely linestring reproducing the elliptical arc

transformed

Compute and return a copy of the elliptical arc transformed by a given transformation

bbox

bbox() -> Bbox

Compute and return the bounding box of the elliptical arc

Source code in src/momapy/geometry.py
def bbox(self) -> "Bbox":
    """Compute and return the bounding box of the elliptical arc"""
    return Bbox.from_bounds(self.to_shapely().bounds)

get_angle_at_fraction

get_angle_at_fraction(fraction: float) -> float

Compute and return the angle in radians formed by the tangent of the elliptical arc and the horizontal at a given fraction (of the total length)

Source code in src/momapy/geometry.py
def get_angle_at_fraction(self, fraction: float) -> float:
    """Compute and return the angle in radians formed by the tangent of the elliptical arc and the horizontal at a given fraction (of the total length)"""
    return get_angle_at_fraction_of_elliptical_arc(self, fraction)

get_center

get_center() -> Point

Compute and return the center of the elliptical arc

Source code in src/momapy/geometry.py
def get_center(self) -> Point:
    """Compute and return the center of the elliptical arc"""
    return get_center_of_elliptical_arc(self)

get_center_parameterization

get_center_parameterization() -> tuple[float, float, float, float, float, float, float, float]

Compute and return the center paramaterizaion of the elliptical arc (cx, cy, rx, ry, sigma, theta1, theta2, delta_theta)

Source code in src/momapy/geometry.py
def get_center_parameterization(
    self,
) -> tuple[float, float, float, float, float, float, float, float]:
    """Compute and return the center paramaterizaion of the elliptical arc (cx, cy, rx, ry, sigma, theta1, theta2, delta_theta)"""
    return get_center_parameterization_of_elliptical_arc(self)

get_intersection_with_line

get_intersection_with_line(line: Line) -> list[Point]

Compute and return the intersection of the elliptical arc with a given line

Source code in src/momapy/geometry.py
def get_intersection_with_line(self, line: Line) -> list[Point]:
    """Compute and return the intersection of the elliptical arc with a given line"""
    return get_intersection_of_line_and_elliptical_arc(line, self)

get_position_and_angle_at_fraction

get_position_and_angle_at_fraction(fraction: float) -> tuple[Point, float]

Compute and return the position on the elliptical arc at a given fraction and the angle in radians formed of the tangent of the bezier curve at that position and the horizontal

Source code in src/momapy/geometry.py
def get_position_and_angle_at_fraction(
    self, fraction: float
) -> tuple[Point, float]:
    """Compute and return the position on the elliptical arc at a given fraction and the angle in radians formed of the tangent of the bezier curve at that position and the horizontal"""
    return get_position_and_angle_at_fraction_of_elliptical_arc(self, fraction)

get_position_at_fraction

get_position_at_fraction(fraction: float) -> Point

Compute and return the position on the elliptical arc at a given fraction (of the total length)

Source code in src/momapy/geometry.py
def get_position_at_fraction(self, fraction: float) -> Point:
    """Compute and return the position on the elliptical arc at a given fraction (of the total length)"""
    return get_position_at_fraction_of_elliptical_arc(self, fraction)

reversed

reversed() -> EllipticalArc

Compute and return a reversed copy of the elliptical arc

Source code in src/momapy/geometry.py
def reversed(self) -> "EllipticalArc":
    """Compute and return a reversed copy of the elliptical arc"""
    return reverse_elliptical_arc(self)

shortened

shortened(length: float, start_or_end: Literal['start', 'end'] = 'end') -> EllipticalArc

Compute and return a copy of the elliptical arc shortened by a given length

Source code in src/momapy/geometry.py
def shortened(
    self,
    length: float,
    start_or_end: typing.Literal["start", "end"] = "end",
) -> "EllipticalArc":
    """Compute and return a copy of the elliptical arc shortened by a given length"""
    return shorten_elliptical_arc(self, length, start_or_end)

to_bezier_curves

to_bezier_curves() -> BezierCurve

Compute and return a bezier curve reproducing the elliptical arc

Source code in src/momapy/geometry.py
def to_bezier_curves(self) -> BezierCurve:
    """Compute and return a bezier curve reproducing the elliptical arc"""
    return transform_elliptical_arc_to_bezier_curves(self)

to_shapely

to_shapely()

Compute and return a shapely linestring reproducing the elliptical arc

Source code in src/momapy/geometry.py
def to_shapely(self):
    """Compute and return a shapely linestring reproducing the elliptical arc"""

    def _split_line_string(
        line_string: shapely.LineString,
        point: Point,
    ):
        segment = Segment(
            Point.from_tuple(line_string.coords[0]),
            Point.from_tuple(line_string.coords[1]),
        )
        min_distance = segment.get_distance_to_point(point)
        min_i = 0
        for i, current_coord in enumerate(line_string.coords[2:]):
            previous_coord = line_string.coords[i + 1]
            segment = Segment(
                Point.from_tuple(previous_coord),
                Point.from_tuple(current_coord),
            )
            distance = segment.get_distance_to_point(point)
            if distance <= min_distance:
                min_distance = distance
                min_i = i
        left_coords = line_string.coords[0 : min_i + 1] + [point.to_shapely()]
        right_coords = [point.to_shapely()] + line_string.coords[min_i + 1 :]
        left_line_string = shapely.LineString(left_coords)
        right_line_string = shapely.LineString(right_coords)
        return [left_line_string, right_line_string]

    origin = shapely.Point(0, 0)
    circle = origin.buffer(1.0).boundary
    ellipse = shapely.affinity.scale(circle, self.rx, self.ry)
    ellipse = shapely.affinity.rotate(ellipse, self.x_axis_rotation)
    center = self.get_center()
    ellipse = shapely.affinity.translate(ellipse, center.x, center.y)
    line1 = Line(center, Point.from_tuple(ellipse.coords[0]))
    angle1 = get_angle_to_horizontal_of_line(line1)
    line2 = Line(center, Point.from_tuple(ellipse.coords[-2]))
    angle2 = get_angle_to_horizontal_of_line(line2)
    angle = angle1 - angle2
    if angle >= 0:
        sweep = 1
    else:
        sweep = 0
    if sweep != self.sweep_flag:
        ellipse = shapely.LineString(ellipse.coords[::-1])
    if ellipse.coords[0] == self.p1.to_tuple():
        ellipse = shapely.LineString(ellipse.coords[1:] + [ellipse.coords[0]])
    first_split = _split_line_string(ellipse, self.p1)
    multi_line = shapely.MultiLineString([first_split[1], first_split[0]])
    line_string = shapely.ops.linemerge(multi_line)
    second_split = _split_line_string(line_string, self.p2)
    shapely_arc = second_split[0]
    return shapely_arc

transformed

transformed(transformation: Transformation) -> EllipticalArc

Compute and return a copy of the elliptical arc transformed by a given transformation

Source code in src/momapy/geometry.py
def transformed(self, transformation: "Transformation") -> "EllipticalArc":
    """Compute and return a copy of the elliptical arc transformed by a given transformation"""
    return transform_elliptical_arc(self, transformation)

GeometryObject dataclass

GeometryObject()

Bases: ABC

Base class for geometry objects

Methods:

Name Description
to_shapely

Compute and return a shapely geometry object that reproduces the geometry object

to_shapely abstractmethod

to_shapely() -> Geometry

Compute and return a shapely geometry object that reproduces the geometry object

Source code in src/momapy/geometry.py
@abc.abstractmethod
def to_shapely(self) -> shapely.Geometry:
    """Compute and return a shapely geometry object that reproduces the geometry object"""
    pass

Line dataclass

Line(p1: Point, p2: Point)

Bases: GeometryObject

Class for lines

Methods:

Name Description
get_angle_to_horizontal

Return the angle in radians formed by the line and the horizontal

get_distance_to_point

Compute and return the distance of a given point to the line

get_intersection_with_line

Compute and return the instersection of the line with another given line

has_point

Return true if a given point is on the line, false otherwise

intercept

Return the intercept of the line

is_coincident_to_line

Return true if the line is coincident to another given line, and false otherwise

is_parallel_to_line

Return true if the line is parallel to another given line, and false otherwise

reversed

Return a reversed copy of the line

slope

Return the slope of the line

to_shapely

Return a shapeply line string reproducing the line

transformed

Return a copy of the line transformed with the given transformation

get_angle_to_horizontal

get_angle_to_horizontal() -> float

Return the angle in radians formed by the line and the horizontal

Source code in src/momapy/geometry.py
def get_angle_to_horizontal(self) -> float:
    """Return the angle in radians formed by the line and the horizontal"""
    return get_angle_to_horizontal_of_line(self)

get_distance_to_point

get_distance_to_point(point: Point) -> float

Compute and return the distance of a given point to the line

Source code in src/momapy/geometry.py
def get_distance_to_point(self, point: Point) -> float:
    """Compute and return the distance of a given point to the line"""
    return get_distance_between_line_and_point(self, point)

get_intersection_with_line

get_intersection_with_line(line: Line) -> list[Line] | list[Point]

Compute and return the instersection of the line with another given line

Source code in src/momapy/geometry.py
def get_intersection_with_line(self, line: "Line") -> list["Line"] | list["Point"]:
    """Compute and return the instersection of the line with another given line"""
    return get_intersection_of_lines(self, line)

has_point

has_point(point: Point, max_distance: float = 0.01) -> bool

Return true if a given point is on the line, false otherwise

Source code in src/momapy/geometry.py
def has_point(self, point: Point, max_distance: float = 0.01) -> bool:
    """Return `true` if a given point is on the line, `false` otherwise"""
    return line_has_point(self, point, max_distance)

intercept

intercept() -> float

Return the intercept of the line

Source code in src/momapy/geometry.py
def intercept(self) -> float:
    """Return the intercept of the line"""
    slope = self.slope()
    if not math.isnan(slope):
        return self.p1.y - slope * self.p1.x
    else:
        return float("NAN")

is_coincident_to_line

is_coincident_to_line(line: Line) -> bool

Return true if the line is coincident to another given line, and false otherwise

Source code in src/momapy/geometry.py
def is_coincident_to_line(self, line: "Line") -> bool:
    """Return `true` if the line is coincident to another given line, and `false` otherwise"""
    return are_lines_coincident(self, line)

is_parallel_to_line

is_parallel_to_line(line: Line) -> bool

Return true if the line is parallel to another given line, and false otherwise

Source code in src/momapy/geometry.py
def is_parallel_to_line(self, line: "Line") -> bool:
    """Return `true` if the line is parallel to another given line, and `false` otherwise"""
    return are_lines_parallel(self, line)

reversed

reversed() -> Line

Return a reversed copy of the line

Source code in src/momapy/geometry.py
def reversed(self) -> "Line":
    """Return a reversed copy of the line"""
    return reverse_line(self)

slope

slope() -> float

Return the slope of the line

Source code in src/momapy/geometry.py
def slope(self) -> float:
    """Return the slope of the line"""
    if self.p1.x != self.p2.x:
        return round((self.p2.y - self.p1.y) / (self.p2.x - self.p1.x), ROUNDING)
    return float("NAN")  # infinite slope

to_shapely

to_shapely() -> LineString

Return a shapeply line string reproducing the line

Source code in src/momapy/geometry.py
def to_shapely(self) -> shapely.LineString:
    """Return a shapeply line string reproducing the line"""
    return shapely.LineString(
        [
            self.p1.to_tuple(),
            self.p2.to_tuple(),
        ]
    )

transformed

transformed(transformation: Transformation) -> Line

Return a copy of the line transformed with the given transformation

Source code in src/momapy/geometry.py
def transformed(self, transformation: "Transformation") -> "Line":
    """Return a copy of the line transformed with the given transformation"""
    return transform_line(self, transformation)

MatrixTransformation dataclass

MatrixTransformation(m: NDArray)

Bases: Transformation

Class for matrix transformations

Methods:

Name Description
inverted

Compute and return the inverse of the matrix transformation

to_matrix

Return a matrix representation of the matrix transformation

inverted

inverted() -> Transformation

Compute and return the inverse of the matrix transformation

Source code in src/momapy/geometry.py
def inverted(self) -> Transformation:
    """Compute and return the inverse of the matrix transformation"""
    return invert_matrix_transformation(self)

to_matrix

to_matrix() -> NDArray

Return a matrix representation of the matrix transformation

Source code in src/momapy/geometry.py
def to_matrix(self) -> numpy.typing.NDArray:
    """Return a matrix representation of the matrix transformation"""
    return self.m

Point dataclass

Point(x: float, y: float)

Bases: GeometryObject

Class for points

Methods:

Name Description
bbox

Return the bounding box of the point

from_fortranarray

Return a point from a numpy fortran array representation

from_shapely

Return a point reproducing a given shapely point

from_tuple

Return a point from a tuple representation

get_angle_to_horizontal

Return the angle in radians formed by the line passing through the origin and the point and the horizontal

get_intersection_with_line

Compute and return a list of the intersections of the point and a given line

isnan

Return true if the point has a nan coordinate, false otherwise

reversed

Return a reversed copy of the point

to_fortranarray

Return a numpy fortran array representation of the point

to_matrix

Return a numpy.array representation of the point

to_shapely

Return a shapely point that reproduces the point

to_tuple

Return a tuple representation of the point

transformed

Return a copy of the the point transformed by the given transformation

bbox

bbox() -> Bbox

Return the bounding box of the point

Source code in src/momapy/geometry.py
def bbox(self) -> "Bbox":
    """Return the bounding box of the point"""
    return Bbox(copy.deepcopy(self), 0, 0)

from_fortranarray classmethod

from_fortranarray(fortranarray: Any) -> Self

Return a point from a numpy fortran array representation

Source code in src/momapy/geometry.py
@classmethod
def from_fortranarray(cls, fortranarray: typing.Any) -> typing.Self:
    """Return a point from a numpy fortran array representation"""
    return cls(fortranarray[0][0], fortranarray[1][1])

from_shapely classmethod

from_shapely(point: Point) -> Self

Return a point reproducing a given shapely point

Source code in src/momapy/geometry.py
@classmethod
def from_shapely(cls, point: shapely.Point) -> typing.Self:
    """Return a point reproducing a given shapely point"""
    return cls(float(point.x), float(point.y))

from_tuple classmethod

from_tuple(t: tuple[float, float]) -> Self

Return a point from a tuple representation

Source code in src/momapy/geometry.py
@classmethod
def from_tuple(cls, t: tuple[float, float]) -> typing.Self:
    """Return a point from a tuple representation"""
    return cls(t[0], t[1])

get_angle_to_horizontal

get_angle_to_horizontal() -> float

Return the angle in radians formed by the line passing through the origin and the point and the horizontal

Source code in src/momapy/geometry.py
def get_angle_to_horizontal(self) -> float:
    """Return the angle in radians formed by the line passing through the origin and the point and the horizontal"""
    return get_angle_to_horizontal_of_point(self)

get_intersection_with_line

get_intersection_with_line(line: Line) -> list[Point]

Compute and return a list of the intersections of the point and a given line

Source code in src/momapy/geometry.py
def get_intersection_with_line(self, line: "Line") -> list["Point"]:
    """Compute and return a list of the intersections of the point and a given line"""
    return get_intersection_of_line_and_point(line, self)

isnan

isnan() -> bool

Return true if the point has a nan coordinate, false otherwise

Source code in src/momapy/geometry.py
def isnan(self) -> bool:
    """Return `true` if the point has a nan coordinate, `false` otherwise"""
    return math.isnan(self.x) or math.isnan(self.y)

reversed

reversed() -> Point

Return a reversed copy of the point

Source code in src/momapy/geometry.py
def reversed(self) -> "Point":
    """Return a reversed copy of the point"""
    return reverse_point(self)

to_fortranarray

to_fortranarray() -> Any

Return a numpy fortran array representation of the point

Source code in src/momapy/geometry.py
def to_fortranarray(self) -> typing.Any:
    """Return a numpy fortran array representation of the point"""
    return numpy.asfortranarray([[self.x], [self.y]])

to_matrix

to_matrix() -> ndarray

Return a numpy.array representation of the point

Source code in src/momapy/geometry.py
def to_matrix(self) -> numpy.ndarray:
    """Return a `numpy.array` representation of the point"""
    m = numpy.array([[self.x], [self.y], [1]], dtype=float)
    return m

to_shapely

to_shapely() -> Point

Return a shapely point that reproduces the point

Source code in src/momapy/geometry.py
def to_shapely(self) -> shapely.Point:
    """Return a shapely point that reproduces the point"""
    return shapely.Point(self.x, self.y)

to_tuple

to_tuple() -> tuple[float, float]

Return a tuple representation of the point

Source code in src/momapy/geometry.py
def to_tuple(self) -> tuple[float, float]:
    """Return a tuple representation of the point"""
    return (
        self.x,
        self.y,
    )

transformed

transformed(transformation: Transformation) -> Point

Return a copy of the the point transformed by the given transformation

Source code in src/momapy/geometry.py
def transformed(self, transformation: "Transformation") -> "Point":
    """Return a copy of the the point transformed by the given transformation"""
    return transform_point(self, transformation)

Rotation dataclass

Rotation(angle: float, point: Point | None = None)

Bases: Transformation

Class for rotations

Methods:

Name Description
inverted

Compute and return the inverse of the rotation

to_matrix

Compute and return a matrix representation of the rotation

inverted

inverted() -> Transformation

Compute and return the inverse of the rotation

Source code in src/momapy/geometry.py
def inverted(self) -> Transformation:
    """Compute and return the inverse of the rotation"""
    return invert_rotation(self)

to_matrix

to_matrix() -> NDArray

Compute and return a matrix representation of the rotation

Source code in src/momapy/geometry.py
def to_matrix(self) -> numpy.typing.NDArray:
    """Compute and return a matrix representation of the rotation"""
    m = numpy.array(
        [
            [math.cos(self.angle), -math.sin(self.angle), 0],
            [math.sin(self.angle), math.cos(self.angle), 0],
            [0, 0, 1],
        ],
        dtype=float,
    )
    if self.point is not None:
        translation = Translation(self.point.x, self.point.y)
        m = numpy.matmul(
            numpy.matmul(translation.to_matrix(), m),
            translation.inverted().to_matrix(),
        )
    return m

Scaling dataclass

Scaling(sx: float, sy: float)

Bases: Transformation

Class for scalings

Methods:

Name Description
inverted

Compute and return the inverse of the scaling

to_matrix

Return a matrix representation of the scaling

inverted

inverted() -> Transformation

Compute and return the inverse of the scaling

Source code in src/momapy/geometry.py
def inverted(self) -> Transformation:
    """Compute and return the inverse of the scaling"""
    return invert_scaling(self)

to_matrix

to_matrix() -> NDArray

Return a matrix representation of the scaling

Source code in src/momapy/geometry.py
def to_matrix(self) -> numpy.typing.NDArray:
    """Return a matrix representation of the scaling"""
    m = numpy.array(
        [
            [self.sx, 0, 0],
            [0, self.sy, 0],
            [0, 0, 1],
        ],
        dtype=float,
    )
    return m

Segment dataclass

Segment(p1: Point, p2: Point)

Bases: GeometryObject

Class for segments

Methods:

Name Description
bbox

Compute and return the bounding box of the segment

from_shapely

Compute and return the segment reproducing a shapely line string

get_angle_at_fraction

Compute and return the angle in radians formed by the segment and the horizontal at a given fraction (of the total length)

get_angle_to_horizontal

Compute and return the angle formed by the segment and the horizontal

get_distance_to_point

Return the distance of a given point to the segment

get_intersection_with_line

Compute and return the intersection of the segment with a given line

get_position_and_angle_at_fraction

Compute and return the position on the segment at a given fraction and the angle in radians formed of the segment and the horizontal at that position

get_position_at_fraction

Compute and return the position on the segment at a given fraction (of the total length)

has_point

Return true if the given point is on the segment, false otherwise

length

Return the length of the segment

reversed

Compute and return a reversed copy of the segment

shortened

Compute and return a copy of the segment shortened by a given length

to_shapely

Return a shapely line string reproducing the segment

transformed

Compute and return a copy of the segment transformed with a given transformation

bbox

bbox() -> Bbox

Compute and return the bounding box of the segment

Source code in src/momapy/geometry.py
def bbox(self) -> "Bbox":
    """Compute and return the bounding box of the segment"""
    return Bbox.from_bounds(self.to_shapely().bounds)

from_shapely classmethod

from_shapely(line_string: LineString) -> Self

Compute and return the segment reproducing a shapely line string

Source code in src/momapy/geometry.py
@classmethod
def from_shapely(cls, line_string: shapely.LineString) -> typing.Self:
    """Compute and return the segment reproducing a shapely line string"""
    shapely_points = line_string.boundary.geoms
    return cls(
        Point.from_shapely(shapely_points[0]),
        Point.from_shapely(shapely_points[1]),
    )

get_angle_at_fraction

get_angle_at_fraction(fraction: float) -> float

Compute and return the angle in radians formed by the segment and the horizontal at a given fraction (of the total length)

Source code in src/momapy/geometry.py
def get_angle_at_fraction(self, fraction: float) -> float:
    """Compute and return the angle in radians formed by the segment and the horizontal at a given fraction (of the total length)"""
    return get_angle_at_fraction_of_segment(self, fraction)

get_angle_to_horizontal

get_angle_to_horizontal() -> float

Compute and return the angle formed by the segment and the horizontal

Source code in src/momapy/geometry.py
def get_angle_to_horizontal(self) -> float:
    """Compute and return the angle formed by the segment and the horizontal"""
    return get_angle_to_horizontal_of_line(self)

get_distance_to_point

get_distance_to_point(point: Point) -> float

Return the distance of a given point to the segment

Source code in src/momapy/geometry.py
def get_distance_to_point(self, point: Point) -> float:
    """Return the distance of a given point to the segment"""
    return get_distance_between_segment_and_point(self, point)

get_intersection_with_line

get_intersection_with_line(line: Line) -> list[Point] | list[Segment]

Compute and return the intersection of the segment with a given line

Source code in src/momapy/geometry.py
def get_intersection_with_line(self, line: Line) -> list[Point] | list["Segment"]:
    """Compute and return the intersection of the segment with a given line"""
    return get_intersection_of_line_and_segment(line, self)

get_position_and_angle_at_fraction

get_position_and_angle_at_fraction(fraction: float) -> tuple[Point, float]

Compute and return the position on the segment at a given fraction and the angle in radians formed of the segment and the horizontal at that position

Source code in src/momapy/geometry.py
def get_position_and_angle_at_fraction(
    self, fraction: float
) -> tuple[Point, float]:
    """Compute and return the position on the segment at a given fraction and the angle in radians formed of the segment and the horizontal at that position"""
    return get_position_and_angle_at_fraction_of_segment(self, fraction)

get_position_at_fraction

get_position_at_fraction(fraction: float) -> Point

Compute and return the position on the segment at a given fraction (of the total length)

Source code in src/momapy/geometry.py
def get_position_at_fraction(self, fraction: float) -> Point:
    """Compute and return the position on the segment at a given fraction (of the total length)"""
    return get_position_at_fraction_of_segment(self, fraction)

has_point

has_point(point: Point, max_distance: float = 0.01) -> bool

Return true if the given point is on the segment, false otherwise

Source code in src/momapy/geometry.py
def has_point(self, point: Point, max_distance: float = 0.01) -> bool:
    """Return `true` if the given point is on the segment, `false` otherwise"""
    return segment_has_point(self, point, max_distance)

length

length() -> float

Return the length of the segment

Source code in src/momapy/geometry.py
def length(self) -> float:
    """Return the length of the segment"""
    return math.sqrt((self.p2.x - self.p1.x) ** 2 + (self.p2.y - self.p1.y) ** 2)

reversed

reversed() -> Segment

Compute and return a reversed copy of the segment

Source code in src/momapy/geometry.py
def reversed(self) -> "Segment":
    """Compute and return a reversed copy of the segment"""
    return reverse_segment(self)

shortened

shortened(length: float, start_or_end: Literal['start', 'end'] = 'end') -> Segment

Compute and return a copy of the segment shortened by a given length

Source code in src/momapy/geometry.py
def shortened(
    self,
    length: float,
    start_or_end: typing.Literal["start", "end"] = "end",
) -> "Segment":
    """Compute and return a copy of the segment shortened by a given length"""
    return shorten_segment(self, length, start_or_end)

to_shapely

to_shapely() -> LineString

Return a shapely line string reproducing the segment

Source code in src/momapy/geometry.py
def to_shapely(self) -> shapely.LineString:
    """Return a shapely line string reproducing the segment"""
    return shapely.LineString(
        [
            self.p1.to_tuple(),
            self.p2.to_tuple(),
        ]
    )

transformed

transformed(transformation: Transformation) -> Segment

Compute and return a copy of the segment transformed with a given transformation

Source code in src/momapy/geometry.py
def transformed(self, transformation: "Transformation") -> "Segment":
    """Compute and return a copy of the segment transformed with a given transformation"""
    return transform_segment(self, transformation)

Transformation dataclass

Transformation()

Bases: ABC

Base class for transformations

Methods:

Name Description
inverted

Compute and return the inverse transformation

to_matrix

Return a matrix representation of the transformation

inverted abstractmethod

inverted() -> Transformation

Compute and return the inverse transformation

Source code in src/momapy/geometry.py
@abc.abstractmethod
def inverted(self) -> "Transformation":
    """Compute and return the inverse transformation"""
    pass

to_matrix abstractmethod

to_matrix() -> NDArray

Return a matrix representation of the transformation

Source code in src/momapy/geometry.py
@abc.abstractmethod
def to_matrix(self) -> numpy.typing.NDArray:
    """Return a matrix representation of the transformation"""
    pass

Translation dataclass

Translation(tx: float, ty: float)

Bases: Transformation

Class for translations

Methods:

Name Description
inverted

Compute and return the inverse of the translation

to_matrix

Return a matrix representation of the translation

inverted

inverted() -> Transformation

Compute and return the inverse of the translation

Source code in src/momapy/geometry.py
def inverted(self) -> Transformation:
    """Compute and return the inverse of the translation"""
    return invert_translation(self)

to_matrix

to_matrix() -> NDArray

Return a matrix representation of the translation

Source code in src/momapy/geometry.py
def to_matrix(self) -> numpy.typing.NDArray:
    """Return a matrix representation of the translation"""
    m = numpy.array([[1, 0, self.tx], [0, 1, self.ty], [0, 0, 1]], dtype=float)
    return m