Fix 3d bresenham

This commit is contained in:
2024-06-12 20:49:20 +02:00
parent 7ad9b45f6c
commit bbdd59ca9b
4 changed files with 77 additions and 49 deletions

40
main.py
View File

@@ -266,12 +266,46 @@ block_list = ["blue_concrete", "red_concrete", "green_concrete",
# # polyline._alpha_assign(1, polyline.length_polyline-1)
# print(polyline.alpha_radii)
p = Polyline((Point2D(0, 0), Point2D(8, 0), Point2D(
8, 8), Point2D(16, 16)))
# p = Polyline((Point2D(0, 0), Point2D(8, 0), Point2D(
# 16, 8), Point2D(24, 0)))
# p = Polyline((Point2D(-1420, 867), Point2D(-1362, 738),
# Point2D(-1157, 717), Point2D(-1099, 843)))
# p = Polyline((Point2D(-1183, 528), Point2D(-1138, 481),
# Point2D(-1188, 451), Point2D(-1152, 416)))
p = Polyline((Point2D(-1225, 468), Point2D(-1138, 481),
Point2D(-1188, 451), Point2D(-1152, 416)))
# Point2D(-1156, 378), Point2D(-1220, 359), Point2D(-1265, 290)
# print(p.alpha_radii)
print(p.get_radius())
radius = p.get_radii()
center = p.get_centers()
print(radius)
print(center)
print(p.lengths)
y = 280
for i in range(len(p.coordinates)-1):
if p.coordinates[i] != None:
s = Segment3D(Point3D(p.coordinates[i].x, y, p.coordinates[i].y), Point3D(
p.coordinates[i+1].x, y, p.coordinates[i+1].y))
for j in range(len(s.coordinates)-1):
editor.placeBlock(
s.coordinates[j].coordinate, Block("cyan_concrete"))
for i in range(len(center)):
if center[i]:
circle = Circle(center[i], radius[i], radius[i])
for j in range(len(circle.coordinates)-1):
editor.placeBlock(
(circle.coordinates[j].x, y, circle.coordinates[j].y), Block("white_concrete"))
# s = Segment2D(Point2D(0, 0), Point2D(10, 10)).perpendicular(10)
# print(s)

View File

@@ -8,6 +8,7 @@ class Point3D:
self.x = x
self.y = y
self.z = z
self.coordinate = (x, y, z)
def copy(self):
return Point3D(self.x, self.y, self.z)

View File

@@ -18,21 +18,22 @@ class Polyline:
>>> Polyline((Point2D(0, 0), Point2D(0, 10), Point2D(50, 10), Point2D(20, 20)))
"""
self.coordinates = points
self.points = Point2D.to_vectors(points)
self.length_polyline = len(points)
if self.length_polyline < 4:
raise ValueError("The list must contain at least 4 elements.")
self.vectors = [None] * self.length_polyline
self.lengths = [None] * self.length_polyline
self.unit_vectors = [None] * self.length_polyline
self.tangente = [0] * self.length_polyline
self.vectors = [None] * self.length_polyline # v
self.lengths = [None] * self.length_polyline # l
self.unit_vectors = [None] * self.length_polyline # n
self.tangente = [0] * self.length_polyline # f
self.alpha_radii = [None] * self.length_polyline
self.alpha_radii = [None] * self.length_polyline # alpha
self.radii = [None] * self.length_polyline
self.centers = [None] * self.length_polyline
self.radii = [None] * self.length_polyline # r
self.centers = [None] * self.length_polyline # c
self._compute_requirements()
self._compute_alpha_radii()
@@ -42,19 +43,19 @@ class Polyline:
def __repr__(self):
return str(self.alpha_radii)
def get_radius(self):
def get_radii(self):
for i in range(1, self.length_polyline-1):
self.radii[i] = self.alpha_radii[i] * self.tangente[i]
self.radii[i] = round(self.alpha_radii[i] * self.tangente[i])
return self.radii
def get_centers(self):
print(self.radii)
for i in range(1, self.length_polyline-2):
print(i)
bi = (self.unit_vectors[i] + self.unit_vectors[i-1]) / \
np.linalg.norm(self.unit_vectors[i] - self.unit_vectors[i-1])
self.centers[i] = self.points[i] + \
sqrt(self.radii[i] ** 2 + self.alpha_radii[i] ** 2) * bi
for i in range(1, self.length_polyline-1):
bisector = (self.unit_vectors[i] - self.unit_vectors[i-1]) / (
np.linalg.norm(self.unit_vectors[i] + self.unit_vectors[i-1]))
array = self.points[i] + sqrt(self.radii[i]
** 2 + self.alpha_radii[i] ** 2) * bisector
self.centers[i] = Point2D(array[0], array[1]).round()
return self.centers
def _alpha_assign(self, start_index: int, end_index: int):
@@ -69,22 +70,21 @@ class Polyline:
alpha_b = min(
self.lengths[start_index] - self.alpha_radii[start_index], self.lengths[start_index + 1])
current_radius = max(self.tangente[start_index] * self.alpha_radii[start_index],
self.tangente[start_index + 1] * alpha_b) # Radis at initial segment
self.tangente[start_index + 1] * alpha_b) # Radius at initial segment
if current_radius < minimum_radius:
minimum_radius, minimum_index = current_radius, start_index # 8, 0
minimum_radius, minimum_index = current_radius, start_index
# 0, 8
alpha_low, alpha_high = self.alpha_radii[start_index], alpha_b
for i in range(start_index + 1, end_index - 1): # Radii for internal segments
alpha_a, alpha_b, current_radius = self._radius_balance(
i) # i = 1 # 4, 4, 4,
if current_radius < minimum_radius: # 4 < 8
minimum_radius, minimum_index = current_radius, i # 4, 1
alpha_low, alpha_high = alpha_a, alpha_b # 4, 4
alpha_a, alpha_b, current_radius = self._radius_balance(i)
if current_radius < minimum_radius:
minimum_radius, minimum_index = current_radius, i
alpha_low, alpha_high = alpha_a, alpha_b
alpha_a = min(self.lengths[end_index-2],
self.lengths[end_index-1]-self.alpha_radii[end_index]) # 8
self.lengths[end_index-1]-self.alpha_radii[end_index])
current_radius = max(self.tangente[end_index-1]*alpha_a, self.tangente[end_index]
* self.alpha_radii[end_index]) # Radius at final segment
@@ -106,13 +106,10 @@ class Polyline:
"""
Returns the radius that balances the radii on either end segement i.
"""
alpha_a = min(self.lengths[i-1], (self.lengths[i]*self.tangente[i+1]) /
(self.tangente[i] + self.tangente[i+1]))
alpha_b = min(self.lengths[i+1], self.lengths[i]-alpha_a)
print(alpha_a, alpha_b, max(
self.tangente[i]*alpha_a, self.tangente[i+1]*alpha_b))
return alpha_a, alpha_b, max(self.tangente[i]*alpha_a, self.tangente[i+1]*alpha_b)
return alpha_a, alpha_b, min(self.tangente[i]*alpha_a, self.tangente[i+1]*alpha_b)
def _compute_requirements(self):
# Between two points, there is only one segment
@@ -123,13 +120,9 @@ class Polyline:
# Between two segments, there is only one angle
for k in range(1, self.length_polyline-1):
dot = np.dot(self.unit_vectors[k], self.unit_vectors[k-1])
dot = np.dot(self.unit_vectors[k], -self.unit_vectors[k-1])
self.tangente[k] = sqrt((1+dot)/(1-dot))
def _compute_alpha_radii(self):
self.alpha_radii[0] = 0
self.alpha_radii[self.length_polyline-1] = 0
# for i in range(1, self.length_polyline-2):
# self.alpha_radii[i] = min(self.lengths[i-1] - self.alpha_radii[i-1], (self.lengths[i]
# * self.tangente[i+1])/(self.tangente[i]+self.tangente[i+1]))

View File

@@ -10,12 +10,12 @@ class Segment3D:
self.coordinates = []
self.overlap = overlap
self.compute_segment(self.start, self.end, self.overlap)
self._compute_segment(self.start, self.end, self.overlap)
def __repr__(self):
return str(self.coordinates)
def compute_segment(self, start: Point3D, end: Point3D, overlap: bool = True):
def _compute_segment(self, start: Point3D, end: Point3D, 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/
@@ -23,11 +23,11 @@ class Segment3D:
Args:
start (Point3D): First coordinates.
end (Point3D): Second coordinates.
overlap (bool, optional): If true, remove unnecessary coordinates connecting to other coordinates side by side, leaving only a diagonal connection. Defaults to True.
overlap (bool, optional): If False, remove unnecessary coordinates connecting to other coordinates side by side, leaving only a diagonal connection. Defaults to False.
>>> Segment3D(Point3D(0, 0, 0), Point3D(10, 10, 15))
"""
self.coordinates.append(start)
self.coordinates.append(start.copy())
dx = abs(end.x - start.x)
dy = abs(end.y - start.y)
dz = abs(end.z - start.z)
@@ -50,18 +50,18 @@ class Segment3D:
p2 = 2 * dz - dx
while start.x != end.x:
start.x += xs
self.coordinates.append(start)
self.coordinates.append(start.copy())
if p1 >= 0:
start.y += ys
if not overlap:
if self.coordinates[-1].y != start.y:
self.coordinates.append(start)
self.coordinates.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)
self.coordinates.append(start.copy())
p2 -= 2 * dx
p1 += 2 * dy
p2 += 2 * dz
@@ -72,18 +72,18 @@ class Segment3D:
p2 = 2 * dz - dy
while start.y != end.y:
start.y += ys
self.coordinates.append(start)
self.coordinates.append(start.copy())
if p1 >= 0:
start.x += xs
if not overlap:
if self.coordinates[-1].x != start.x:
self.coordinates.append(start)
self.coordinates.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)
self.coordinates.append(start.copy())
p2 -= 2 * dy
p1 += 2 * dx
p2 += 2 * dz
@@ -94,18 +94,18 @@ class Segment3D:
p2 = 2 * dx - dz
while start.z != end.z:
start.z += zs
self.coordinates.append(start)
self.coordinates.append(start.copy())
if p1 >= 0:
start.y += ys
if not overlap:
if self.coordinates[-1].y != start.y:
self.coordinates.append(start)
self.coordinates.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)
self.coordinates.append(start.copy())
p2 -= 2 * dz
p1 += 2 * dy
p2 += 2 * dx