Everything cleaned and tested
This commit is contained in:
@@ -7,22 +7,22 @@ class Segment3D:
|
||||
def __init__(self, start: Point3D, end: Point3D):
|
||||
self.start = start
|
||||
self.end = end
|
||||
self.coordinates = []
|
||||
self.output_points = []
|
||||
|
||||
def __repr__(self):
|
||||
return str(self.coordinates)
|
||||
return str(self.output_points)
|
||||
|
||||
def discrete_coordinates(self, overlap: bool = False):
|
||||
def segment(self, overlap: bool = False):
|
||||
"""Calculate a segment between two points in 3D space. 3d Bresenham algorithm.
|
||||
|
||||
From: https://www.geeksforgeeks.org/bresenhams-algorithm-for-3-d-line-drawing/
|
||||
|
||||
Args:
|
||||
overlap (bool, optional): If False, remove unnecessary coordinates connecting to other coordinates side by side, leaving only a diagonal connection. Defaults to False.
|
||||
overlap (bool, optional): If False, remove unnecessary points connecting to other points side by side, leaving only a diagonal connection. Defaults to False.
|
||||
|
||||
>>> Segment3D(Point3D(0, 0, 0), Point3D(10, 10, 15))
|
||||
"""
|
||||
self.coordinates.append(start.copy())
|
||||
self.output_points.append(start.copy())
|
||||
dx = abs(self.end.x - self.start.x)
|
||||
dy = abs(self.end.y - self.start.y)
|
||||
dz = abs(self.end.z - self.start.z)
|
||||
@@ -45,18 +45,18 @@ class Segment3D:
|
||||
p2 = 2 * dz - dx
|
||||
while start.x != end.x:
|
||||
start.x += xs
|
||||
self.coordinates.append(start.copy())
|
||||
self.output_points.append(start.copy())
|
||||
if p1 >= 0:
|
||||
start.y += ys
|
||||
if not overlap:
|
||||
if self.coordinates[-1].y != start.y:
|
||||
self.coordinates.append(start.copy())
|
||||
if self.output_points[-1].y != start.y:
|
||||
self.output_points.append(start.copy())
|
||||
p1 -= 2 * dx
|
||||
if p2 >= 0:
|
||||
start.z += zs
|
||||
if not overlap:
|
||||
if self.coordinates[-1].z != start.z:
|
||||
self.coordinates.append(start.copy())
|
||||
if self.output_points[-1].z != start.z:
|
||||
self.output_points.append(start.copy())
|
||||
p2 -= 2 * dx
|
||||
p1 += 2 * dy
|
||||
p2 += 2 * dz
|
||||
@@ -67,18 +67,18 @@ class Segment3D:
|
||||
p2 = 2 * dz - dy
|
||||
while start.y != end.y:
|
||||
start.y += ys
|
||||
self.coordinates.append(start.copy())
|
||||
self.output_points.append(start.copy())
|
||||
if p1 >= 0:
|
||||
start.x += xs
|
||||
if not overlap:
|
||||
if self.coordinates[-1].x != start.x:
|
||||
self.coordinates.append(start.copy())
|
||||
if self.output_points[-1].x != start.x:
|
||||
self.output_points.append(start.copy())
|
||||
p1 -= 2 * dy
|
||||
if p2 >= 0:
|
||||
start.z += zs
|
||||
if not overlap:
|
||||
if self.coordinates[-1].z != start.z:
|
||||
self.coordinates.append(start.copy())
|
||||
if self.output_points[-1].z != start.z:
|
||||
self.output_points.append(start.copy())
|
||||
p2 -= 2 * dy
|
||||
p1 += 2 * dx
|
||||
p2 += 2 * dz
|
||||
@@ -89,22 +89,22 @@ class Segment3D:
|
||||
p2 = 2 * dx - dz
|
||||
while start.z != end.z:
|
||||
start.z += zs
|
||||
self.coordinates.append(start.copy())
|
||||
self.output_points.append(start.copy())
|
||||
if p1 >= 0:
|
||||
start.y += ys
|
||||
if not overlap:
|
||||
if self.coordinates[-1].y != start.y:
|
||||
self.coordinates.append(start.copy())
|
||||
if self.output_points[-1].y != start.y:
|
||||
self.output_points.append(start.copy())
|
||||
p1 -= 2 * dz
|
||||
if p2 >= 0:
|
||||
start.x += xs
|
||||
if not overlap:
|
||||
if self.coordinates[-1].x != start.x:
|
||||
self.coordinates.append(start.copy())
|
||||
if self.output_points[-1].x != start.x:
|
||||
self.output_points.append(start.copy())
|
||||
p2 -= 2 * dz
|
||||
p1 += 2 * dy
|
||||
p2 += 2 * dx
|
||||
return self.coordinates
|
||||
return self.output_points
|
||||
|
||||
def middle_point(self):
|
||||
return (np.round((self.start.x + self.end.x) / 2.0).astype(int),
|
||||
|
||||
Reference in New Issue
Block a user