-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement EllipseSketch and CircleSketch
- Loading branch information
1 parent
ee381c2
commit eaefd73
Showing
3 changed files
with
108 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
"""``CircleSketch`` class module.""" | ||
|
||
from ansys.geometry.core.primitives.direction import Direction2D | ||
from ansys.geometry.core.primitives.point import Point3D | ||
import numpy as np | ||
|
||
from ansys.geometry.core.primitives.point import Point2D | ||
from ansys.geometry.core.sketch.curve import SketchCurve | ||
|
||
|
||
class CircleSketch(SketchCurve): | ||
""" | ||
Provides circle representation within a sketch environment. | ||
Parameters | ||
---------- | ||
origin : Point3D | ||
Centered origin of the circle. | ||
dir_x: Direction | ||
X-plane direction. | ||
dir_y: Direction | ||
Y-plane direction. | ||
radius: float | ||
Circle radius. | ||
""" | ||
|
||
def __init__(self, origin: Point3D, dir_x: Direction2D, dir_y: Direction2D, radius: float): | ||
"""Provides circle representation within a sketch environment.""" | ||
|
||
def __init__(self, points, origin): | ||
"""Constructor method for ``CircleSketch``.""" | ||
self._origin = origin | ||
self._dir_x = dir_x | ||
self._dir_y = dir_y | ||
self._radius = radius | ||
super().__init__(points, origin) | ||
self._radius = np.linalg.norm(origin - points[0]) | ||
|
||
@classmethod | ||
def from_radius(cls, radius, origin=None, resolution=150): | ||
"""Create a circle from its radius and center.""" | ||
|
||
# Unpack the x and y coordinates for the center point | ||
if origin is None: | ||
origin = Point2D([0, 0]) | ||
|
||
# Collect the coordinates of the points for the point | ||
theta = np.linspace(0, 2 * np.pi, resolution) | ||
x_coords = origin.x + radius * np.cos(theta) | ||
y_coords = origin.y + radius * np.sin(theta) | ||
|
||
# Generate all the point instances | ||
points = [Point2D([x, y]) for x, y in zip(x_coords, y_coords)] | ||
return cls(points, origin) | ||
|
||
@property | ||
def radius(self): | ||
"""Return the radius of the circle.""" | ||
return self._radius |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,43 @@ | ||
"""``SketchCurve`` class module.""" | ||
|
||
from ansys.geometry.core.primitives.point import Point2D | ||
|
||
|
||
class SketchCurve: | ||
""" | ||
Provides base sketch object class all sketch objects should be | ||
derived from for easy classification. | ||
""" | ||
"""Provides base sketch object class all sketch objects.""" | ||
|
||
def __init__(self, points: list[Point2D], origin): | ||
"""Initializes the sketch curve from its points. | ||
Parameters | ||
---------- | ||
points : list[Point2D] | ||
A list of points defining the sketch curve. | ||
""" | ||
self._points = points | ||
self._origin = origin | ||
|
||
@property | ||
def points(self): | ||
"""Return a list of ``Point2D`` instances defining the sketch | ||
Returns | ||
------- | ||
points : list[Point2D] | ||
A list of points defining the sketch curve. | ||
""" | ||
return self._points | ||
|
||
@property | ||
def origin(self): | ||
return self._origin | ||
|
||
@property | ||
def x_coords(self): | ||
return [point.x for point in self.points] | ||
|
||
@property | ||
def y_coords(self): | ||
return [point.y for point in self.points] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
"""A module containing a class for modeling ellipses.""" | ||
|
||
import numpy as np | ||
|
||
from ansys.geometry.core.primitives.point import Point2D | ||
from ansys.geometry.core.sketch.curve import SketchCurve | ||
|
||
|
||
class EllipseSketch(SketchCurve): | ||
"""A class for modelling ellipses.""" | ||
|
||
def __init__(self, points, origin): | ||
super().__init__(points, origin) | ||
|
||
@classmethod | ||
def from_axes(cls, a, b, origin=None, resolution=150): | ||
"""Create an ellipse from its semi-major and semi-minor axes.""" | ||
# Assert that the curve is an ellipse and not a parabola or hyperbola | ||
ecc = (a ** 2 - b ** 2) ** 0.5 / a | ||
if ecc >= 1: | ||
raise ValueError("The curve defined is not an ellipse.") | ||
|
||
# Unpack the x and y coordinates for the origin point | ||
if origin is None: | ||
origin = Point2D([0, 0]) | ||
|
||
# Generate the points on the ellipse | ||
theta = np.linspace(0, 2 * np.pi, resolution) | ||
x_coords = origin.x + a * np.cos(theta) | ||
y_coords = origin.y + b * np.sin(theta) | ||
|
||
# Generate all the point instances | ||
points = [Point2D([x, y]) for x, y in zip(x_coords, y_coords)] | ||
return cls(points, origin) | ||
|
||
@classmethod | ||
def from_focii_and_point(f1, f2, point, origin=None, resolution=100): | ||
"""Create an ellipse from its focci and a point.""" | ||
raise NotImplementedError |