From 37cb55303798727ac612906947bdde4aaa26960e Mon Sep 17 00:00:00 2001 From: UmairWaseef <117443651+UmairWaseef@users.noreply.github.com> Date: Wed, 30 Oct 2024 19:59:35 +0530 Subject: [PATCH] created new geometry tool --- geometry/example_tool_kit.py | 25 +++++++++++++++++++ geometry/geometry_tool_kit.py | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 geometry/example_tool_kit.py create mode 100644 geometry/geometry_tool_kit.py diff --git a/geometry/example_tool_kit.py b/geometry/example_tool_kit.py new file mode 100644 index 000000000000..041a75d150e6 --- /dev/null +++ b/geometry/example_tool_kit.py @@ -0,0 +1,25 @@ +# examples/example_usage.py +from geometry_tool_kit import GeometryToolkit + +# Define points for geometric analysis +points = [(0, 0), (10, 0), (1, 1), (0, 1)] + +# Initialize the toolkit with points +toolkit = GeometryToolkit(points) + + +# Example: Display the convex hull +toolkit.convex_hull() + +# Example: Display the Voronoi diagram +toolkit.voronoi_diagram() + +# Example: Calculate the area of a polygon +print("Area of polygon:", toolkit.calculate_area()) + +# Example: Visualize points in 3D space +toolkit.visualize_3d() + +# Example: Create a symbolic circle +circle = toolkit.create_circle((0, 0), 1) +print("Symbolic circle:", circle) diff --git a/geometry/geometry_tool_kit.py b/geometry/geometry_tool_kit.py new file mode 100644 index 000000000000..ac51896f0d3a --- /dev/null +++ b/geometry/geometry_tool_kit.py @@ -0,0 +1,47 @@ +# geometry_toolkit.py +from shapely.geometry import Point, Polygon +from sympy import Circle, Point as SymPoint, Polygon as SymPolygon +import matplotlib.pyplot as plt +from scipy.spatial import Voronoi, ConvexHull +import pyvista as pv +import numpy as np + + +class GeometryToolkit: + def __init__(self, points): + self.points = points + self.shapely_points = [Point(p) for p in points] + self.sympy_points = [SymPoint(*p) for p in points] + self.np_points = np.array(points) + + def convex_hull(self): + """Visualize the convex hull of a set of points.""" + hull = ConvexHull(self.np_points) + plt.plot(self.np_points[:, 0], self.np_points[:, 1], 'o') + for simplex in hull.simplices: + plt.plot(self.np_points[simplex, 0], self.np_points[simplex, 1], 'k-') + plt.title("Convex Hull") + plt.show() + + def voronoi_diagram(self): + """Visualize the Voronoi diagram for a set of points.""" + vor = Voronoi(self.np_points) + plt.plot(self.np_points[:, 0], self.np_points[:, 1], 'o') + plt.voronoi_plot_2d(vor) + plt.title("Voronoi Diagram") + plt.show() + + def calculate_area(self): + """Calculate the area of a polygon formed by points (if applicable).""" + sym_polygon = SymPolygon(*self.sympy_points) + return sym_polygon.area + + def visualize_3d(self): + """Visualize points in a 3D space.""" + plotter = pv.Plotter() + plotter.add_mesh(pv.PolyData(self.np_points), color="red", point_size=10) + plotter.show() + + def create_circle(self, center, radius): + """Create a symbolic circle with a given center and radius.""" + return Circle(SymPoint(*center), radius)