-
Notifications
You must be signed in to change notification settings - Fork 12
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
92826c9
commit 861ca0a
Showing
3 changed files
with
89 additions
and
23 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,40 @@ | ||
"""``CircleSketch`` class module.""" | ||
|
||
from ansys.geometry.core.primitives.direction import Direction | ||
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: Direction, dir_y: Direction, 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]) | ||
x_origin, y_origin = origin.x, origin.y | ||
|
||
# Collect the coordinates of the points for the point | ||
x_coords = np.linspace(radius, -radius, resolution) | ||
y_coords = np.abs((radius**2 - (x_coords - x_origin) ** 2) ** 0.5 + y_origin) | ||
x_coords, y_coords = np.concatenate((x_coords, x_coords)), np.concatenate( | ||
(y_coords, -y_coords) | ||
) | ||
|
||
# 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
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,42 @@ | ||
"""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 = np.sqrt((b - a) / 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]) | ||
x_origin, y_origin = origin.x, origin.y | ||
|
||
# Collect the coordinates of the points for the ellipse | ||
x_coords = np.linspace(a, -a, resolution) | ||
y_coords = np.abs((b * (1 - ((x_coords - x_origin) / a) ** 2)) ** 0.5 + y_origin) | ||
x_coords, y_coords = np.concatenate((x_coords, x_coords)), np.concatenate( | ||
(y_coords, -y_coords) | ||
) | ||
|
||
# Generate all the point instances | ||
points = [Point2D([x, y]) for x, y in zip(x_coords, y_coords)] | ||
return cls(points) | ||
|
||
@classmethod | ||
def from_focii_and_point(f1, f2, point, origin=None, resolution=100): | ||
"""Create an ellipse from its focci and a point.""" | ||
raise NotImplementedError |