geometor.model
The Model module provides a set of tools for constructing geometric models. It relies heavily on sympy for providing the algebraic infrastructure the functions here are for creating the abstract model, not the rendering see the Render module for plotting with matplotlib
This module provides the Model class, which is used to represent a geometric model in 2D space. The Model class is based on the list data structure, and can contain points, lines, circles, polygons, and segments.
- class geometor.model.Model(name: str = '')[source]
Bases:
dict
A collection of geometric elements, including points, lines, circles, and polygons, represented using the sympy.geometry library.
When lines and circles are added to the model, intersection points of the new element with the preceding elements are identify and added.
When new elements or points are added to the model, we check for existing duplicates.
parameters
name
str
establish name for the model instance
attributes
methods
set_point()
->Point
set_wedge()
->Wedge
save()
->load()
->point_label_generator()
-> Iterator[str]
Todo
add get_bounds_polygon method to Model
- set_point(x_val: Expr, y_val: Expr, parents: list | None = None, classes: list | None = None, label: str = '') Point
Adds a point to the model, finds duplicates, cleans values, and sets parents and classes.
parameters
x_val
:sympy.core.expr.Expr
: The x-value of the point.y_val
:sympy.core.expr.Expr
: The y-value of the point.parents
: list, optional: A list of parent elements or references. Defaults to None.classes
list, optional: A list of string names for classes defining a set of styles. Defaults to None.label
str, optional: A text label for use in plotting and reporting. Defaults to an empty string.
returns
sympy.geometry.point.Point
: The set point.
example
>>> from geometor.model import * >>> model = Model("demo") >>> model.set_point(0, 0, classes=["given"]) <spg.Point object ...>
notes
The function simplifies the x and y values before adding, and it updates the attributes if the point is already in the model.
- construct_line(pt_1: Point, pt_2: Point, classes: list | None = None, label: str = '') Line
Constructs a
Line
from two points and adds it to theModel
parameters
pt_1
:sympy.geometry.point.Point
A SymPy Point marking the first point of the linept_2
:sympy.geometry.point.Point
: A SymPy Point marking the second point of the lineclasses
: list: Additional classes (optional)label
: str: Label for the line (optional)
returns
sympy.geometry.line.Line
: The constructed line
example
>>> from geometor.elements import * >>> model = Model("demo") >>> A = model.set_point(0, 0, classes=["given"], label="A") >>> B = model.set_point(1, 0, classes=["given"], label="B") >>> model.construct_line(A, B) <spg.Line object ...>
operations
create an instance of
spg.Line
create a
details
object fromElement
add parents to details
check for duplicates in elements.
find intersection points for new element with all precedng elements
Add
line
to the model.
- construct_line_by_labels(pt_1_label: str, pt_2_label: str, classes: list | None = None, label: str = '') Line
find points by label and use them with
Model.construct_line()
- construct_circle(pt_center: Point, pt_radius: Point, classes: list | None = None, label: str = '') Circle
Constructs a Circle from two points and adds it to the model.
operations
create an instance of
sympy.geometry.ellipse.Circle`as ``circle`
create a
details
object fromElement
- add parents to details
initial parents are the two starting points
check for duplicates in in the
model
find intersection points for new element with all precedng elements
Add
circle
to the model.
parameters
pt_center
:sympy.geometry.point.Point
A SymPy Point representing the circle center.pt_radius
:sympy.geometry.point.Point
A SymPy Point marking the length of the radius.classes
:list
optional A list of string names for classes defining a set of styles. Defaults to None.label
:str
optional A text label for use in plotting and reporting. Defaults to an empty string.
returns
Circle
:The constructed circle.
example
>>> from geometor.elements import * >>> model = Model("demo") >>> A = model.set_point(0, 0, classes=["given"], label="A") >>> B = model.set_point(1, 0, classes=["given"], label="B") >>> model.construct_circle(A, B) <spg.Circle object ...>
notes
SymPy defines a circle as a center point and a radius length, so the radius length is calculated for the spg.Circle.
- construct_circle_by_labels(pt_1_label: str, pt_2_label: str, classes: list | None = None, label: str = '') Line
find points by label and use them with
Model.construct_line()
- set_segment(pt_1: Point, pt_2: Point, classes=[], label='') Segment
set segment (list of points) for demonstration in the model
- set_segment_by_labels(pt_1_label: str, pt_2_label: str, classes: list | None = None, label: str = '') Line
find points by label and use them with
Model.construct_line()
- set_polygon(poly_pts: list[Point], classes=[], label='') Polygon
set polygon (list of 3 or more points)
- set_polygon_by_labels(poly_pts_labels: list[str], classes: list | None = None, label: str = '') Line
find points by label and use them with
Model.construct_line()
- set_wedge(pt_center: Point, pt_radius: Point, pt_sweep_start: Point, pt_sweep_end: Point, direction='clockwise', classes: list | None = None, label: str = '') Wedge
sets a Wedge from 3 points and adds it to the model.
operations
create an instance of
geometor.model.Wedge
create a
details
object fromElement
- add parents to details
initial parents are the two starting points
check for duplicates in in the
model
find intersection points for new element with all precedng elements
Add
circle
to the model.
parameters
pt_center
:sympy.geometry.point.Point
: point for circle centerpt_radius
:sympy.geometry.point.Point
: point to mark radiuspt_end
:sympy.geometry.point.Point
: A SymPy Point marking the sweep of the wedgeclasses
:list
optional : A list of string names for classes defining a set of styles. Defaults to None.label
:str
optional : A text label for use in plotting and reporting. Defaults to an empty string.
returns
Wedge
The portion of a circle
example
>>> from geometor.elements import * >>> model = Model("demo") >>> A = model.set_point(0, 0, classes=["given"], label="A") >>> B = model.set_point(1, 0, classes=["given"], label="B") >>> model.construct_circle(A, B) >>> model.construct_circle(B, A) >>> model._set_wedge_by_labels('A', 'B', 'C') <Wedge object ...>
notes
SymPy defines a circle as a center point and a radius length, so the radius length is calculated for the spg.Circle.
- limits() tuple[tuple[float, float], tuple[float, float]] [source]
Find x, y limits from points and circles of the model
- Returns a list of x, y limits:
((x_min, x_max), (y_min, y_max))
- get_ancestors(element)
Retrieves the ancestors for the given element.
The method recursively traverses the parent elements of the given element and constructs a nested dictionary representing the ancestor tree.
parameters
- elementsympy.geometry object
The element for which the ancestors are to be retrieved.
returns
dict : A nested dictionary representing the ancestors.
example
If element A has parents B and C, and B has parent D, the method returns: {A: {B: {D: {}}, C: {}}}
- get_ancestors_labels(element) dict[str, dict]
Retrieves the labels of the ancestors for the given element.
The method recursively traverses the parent elements of the given element and constructs a nested dictionary with labels representing the ancestor tree.
parameters
- elementsympy.geometry object
The element for which the ancestors’ labels are to be retrieved.
returns - dict : A nested dictionary representing the labels of the ancestors.
example
If element A has parents B and C, and B has parent D, the method returns: {‘A’: {‘B’: {‘D’: {}}, ‘C’: {}}}
- get_element_by_label(label: str)
Finds and returns the element with the given label.
parameters
label
:str
: The label of the desired element.
returns
Element or None: The element with the matching label, or None if no match is found.
- save(file_path)
- classmethod load(file_path)