Skip to content

Commit

Permalink
Implement EllipseSketch and CircleSketch
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgepiloto committed Aug 31, 2022
1 parent 92826c9 commit aa2e61b
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 23 deletions.
52 changes: 30 additions & 22 deletions src/ansys/geometry/core/sketch/circle.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
"""``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
theta = np.linspace(0, 2 * np.pi, resolution)
x_coords, y_coords = radius * np.cos(theta), 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
15 changes: 14 additions & 1 deletion src/ansys/geometry/core/sketch/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class SketchCurve:
"""Provides base sketch object class all sketch objects."""

def __init__(self, points: list[Point2D]):
def __init__(self, points: list[Point2D], origin):
"""Initializes the sketch curve from its points.
Parameters
Expand All @@ -16,6 +16,7 @@ def __init__(self, points: list[Point2D]):
"""
self._points = points
self._origin = origin

@property
def points(self):
Expand All @@ -28,3 +29,15 @@ def points(self):
"""
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]
43 changes: 43 additions & 0 deletions src/ansys/geometry/core/sketch/ellipse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""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, f1, f2):
super().__init__(points, origin)
self._f1, self._f2 = f1, f2

@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

0 comments on commit aa2e61b

Please sign in to comment.