Skip to content

Commit

Permalink
feat(arc): Add to_polyline methods to the Arc classes
Browse files Browse the repository at this point in the history
This will be useful for both climate graphics and for the planarization ob objects like the cylinder.
  • Loading branch information
chriswmackey authored and Chris Mackey committed Mar 14, 2020
1 parent a517ba0 commit 249518e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
21 changes: 18 additions & 3 deletions ladybug_geometry/geometry2d/arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import division

from .pointvector import Point2D, Vector2D
from .polyline import Polyline2D
from ..intersection2d import closest_point2d_on_arc2d, intersect_line2d_arc2d, \
intersect_line2d_infinite_arc2d

Expand Down Expand Up @@ -368,15 +369,29 @@ def split_line_infinite(self, line_ray):
Arc2D(self.c, self.r, am2, am1),
Arc2D(self.c, self.r, am1, self.a2)]

def duplicate(self):
"""Get a copy of this object."""
return self.__copy__()
def to_polyline(self, divisions, interpolated=True):
"""Get this Arc2D as an approximated Polyline2D.
Args:
divisions: The number of segments into which the arc will be divided.
interpolated: Boolean to note whether the polyline should be interpolated
between the input vertices when it is translated to other interfaces.
This property has no effect on the geometric calculations performed
by this library and is only present in order to assist with
display/translation. (Default: True)
"""
pts = self.subdivide_evenly(divisions)
return Polyline2D(pts, interpolated)

def to_dict(self):
"""Get Arc2D as a dictionary."""
return {'type': 'Arc2D', 'c': self.c.to_array(),
'r': self.r, 'a1': self.a1, 'a2': self.a2}

def duplicate(self):
"""Get a copy of this object."""
return self.__copy__()

def _pt_in(self, point):
if self.is_circle:
return True
Expand Down
21 changes: 18 additions & 3 deletions ladybug_geometry/geometry3d/arc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..geometry2d.pointvector import Point2D, Vector2D
from ..geometry2d.ray import Ray2D
from ..geometry2d.arc import Arc2D
from .polyline import Polyline3D

import math

Expand Down Expand Up @@ -323,15 +324,29 @@ def split_with_plane(self, plane):
for arc in _int_pt2d]
return [self]

def duplicate(self):
"""Get a copy of this object."""
return self.__copy__()
def to_polyline(self, divisions, interpolated=True):
"""Get this Arc3D as an approximated Polyline3D.
Args:
divisions: The number of segments into which the arc will be divided.
interpolated: Boolean to note whether the polyline should be interpolated
between the input vertices when it is translated to other interfaces.
This property has no effect on the geometric calculations performed
by this library and is only present in order to assist with
display/translation. (Default: True)
"""
pts = self.subdivide_evenly(divisions)
return Polyline3D(pts, interpolated)

def to_dict(self):
"""Get Arc3D as a dictionary."""
return {'type': 'Arc3D', 'plane': self.plane.to_dict(),
'radius': self.radius, 'a1': self.a1, 'a2': self.a2}

def duplicate(self):
"""Get a copy of this object."""
return self.__copy__()

@staticmethod
def _plane_from_vertices(pt1, pt2, pt3):
"""Get a plane from three vertices."""
Expand Down
14 changes: 14 additions & 0 deletions tests/arc2d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ladybug_geometry.geometry2d.line import LineSegment2D
from ladybug_geometry.geometry2d.ray import Ray2D
from ladybug_geometry.geometry2d.arc import Arc2D
from ladybug_geometry.geometry2d.polyline import Polyline2D

import math

Expand Down Expand Up @@ -258,6 +259,19 @@ def test_subdivide():
assert len(divisions) == 5


def test_to_polyline():
"""Test the Arc3D to_polyline methods."""
pt = Point2D(2, 0)
arc = Arc2D(pt, 1, 1.5 * math.pi, 0.5 * math.pi)

pline = arc.to_polyline(4, True)
assert isinstance(pline, Polyline2D)
assert pline.vertices[0] == arc.p1
assert pline.vertices[-1] == arc.p2
assert len(pline.vertices) == 5
assert pline.interpolated


def test_point_at_angle():
"""Test the Arc2D point_at_angle method."""
pt = Point2D(2, 0)
Expand Down
14 changes: 14 additions & 0 deletions tests/arc3d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ladybug_geometry.geometry3d.arc import Arc3D
from ladybug_geometry.geometry3d.plane import Plane
from ladybug_geometry.geometry3d.pointvector import Point3D, Vector3D
from ladybug_geometry.geometry3d.polyline import Polyline3D

from ladybug_geometry.geometry2d.arc import Arc2D

Expand Down Expand Up @@ -290,6 +291,19 @@ def test_subdivide():
assert len(divisions) == 5


def test_to_polyline():
"""Test the Arc3D to_polyline methods."""
pt = Point3D(2, 0, 2)
arc = Arc3D(Plane(o=pt), 1, 1.5 * math.pi, 0.5 * math.pi)

pline = arc.to_polyline(4, True)
assert isinstance(pline, Polyline3D)
assert pline.vertices[0] == arc.p1
assert pline.vertices[-1] == arc.p2
assert len(pline.vertices) == 5
assert pline.interpolated


def test_point_at_angle():
"""Test the Arc3D point_at_angle method."""
pt = Point3D(2, 0)
Expand Down

0 comments on commit 249518e

Please sign in to comment.