Add orthogonal and parallel

This commit is contained in:
2024-04-20 20:34:53 +02:00
parent 0e5e6a9313
commit d218a67f42
4 changed files with 64 additions and 16 deletions

18
main.py
View File

@@ -1,5 +1,6 @@
from gdpc import Editor, Block, geometry from gdpc import Editor, Block, geometry
import networks.curve as curve import networks.Curve as curve
import networks.Segment as segment
import numpy as np import numpy as np
editor = Editor(buffering=True) editor = Editor(buffering=True)
@@ -13,10 +14,13 @@ editor = Editor(buffering=True)
# # Build a cube # # Build a cube
# geometry.placeCuboid(editor, (458, 92, 488), (468, 99, 471), Block("oak_planks")) # geometry.placeCuboid(editor, (458, 92, 488), (468, 99, 471), Block("oak_planks"))
curve = curve.Curve([(396, 132, 740), (435, 138, 730), # curve = curve.Curve([(396, 132, 740), (435, 138, 730),
(443, 161, 758), (417, 73, 729)]) # (443, 161, 758), (417, 73, 729)])
curve.compute_curve() # curve.compute_curve()
for point in curve.computed_points: # for point in curve.computed_points:
print(point) # print(point)
editor.placeBlock(point, Block("stone")) # editor.placeBlock(point, Block("stone"))
print(segment.parrallel(((0, 0, 0), (0, 0, 10)), 10))
print(segment.orthogonal((0, 0, 0), (1, 0, 0), 10))

View File

@@ -5,21 +5,17 @@ from scipy import interpolate
class Curve: class Curve:
def __init__(self, target_points): def __init__(self, target_points):
# list of points to [(x1, y1, z1), (...), ...] # list of points to [(x1, y1, z1), (...), ...]
self.target_points = target_points self.computed_points = compute_curve(target_points)
self.computed_points = []
def compute_curve(self, resolution=40): @staticmethod
def compute_curve(self, target_points, resolution=40):
""" """
Fill self.computed_points with a list of points that approximate a smooth curve following self.target_points. Fill self.computed_points with a list of points that approximate a smooth curve following self.target_points.
https://stackoverflow.com/questions/18962175/spline-interpolation-coefficients-of-a-line-curve-in-3d-space https://stackoverflow.com/questions/18962175/spline-interpolation-coefficients-of-a-line-curve-in-3d-space
Args:
points (np.array): Points where the curve should pass in order.
resolution (int, optional): Total number of points to compute. Defaults to 40.
""" """
# Remove duplicates. Curve can't intersect itself # Remove duplicates. Curve can't intersect itself
points = tuple(map(tuple, np.array(self.target_points))) points = tuple(map(tuple, np.array(target_points)))
points = sorted(set(points), key=points.index) points = sorted(set(points), key=points.index)
# Change coordinates structure to (x1, x2, x3, ...), (y1, y2, y3, ...) (z1, z2, z3, ...) # Change coordinates structure to (x1, x2, x3, ...), (y1, y2, y3, ...) (z1, z2, z3, ...)
@@ -38,5 +34,9 @@ class Curve:
y_rounded = np.round(y_fine).astype(int) y_rounded = np.round(y_fine).astype(int)
z_rounded = np.round(z_fine).astype(int) z_rounded = np.round(z_fine).astype(int)
self.computed_points = [(x, y, z) for x, y, z in zip( return [(x, y, z) for x, y, z in zip(
x_rounded, y_rounded, z_rounded)] x_rounded, y_rounded, z_rounded)]
@staticmethod
def offset(self):
pass

44
networks/Segment.py Normal file
View File

@@ -0,0 +1,44 @@
import numpy as np
def parallel(segment, distance, normal=np.array([0, 1, 0])):
"""Get parallel segment in 3D space at a distance.
Args:
segment (np.array, np.array): start and end points of the segement.
distance (int): distance between both segment. Thickness in the context of a line. Positive direction means left.
Returns:
(np.array(), np.array()): parallel segment.
"""
return (orthogonal(segment[0], segment[1], distance, normal), orthogonal(segment[1], segment[0], -distance, normal))
def orthogonal(origin, point, distance, normal=np.array([0, 1, 0])):
"""Get orthogonal point from a given one at the specified distance in 3D space with normal direction.
Args:
origin (tuple or np.array): origin
point (tuple or np.array): (point-origin) makes the first vector. Only the direction is used.
distance (int): distance from the origin. Thickness in the context of a line. Positive direction means left.
normal (list or np.array, optional): second vector. Defaults to the vertical [0, 1, 0].
Raises:
ValueError: if vectors are not linearly independent.
Returns:
np.array: (x y z)
>>>orthogonal((5, 5, 5), (150, 5, 5), 10)
[ 5. 5. 15.]
"""
vector = np.subtract(point, origin)
magnitude = np.linalg.norm(vector)
normalized_vector = vector / magnitude
orthogonal = np.cross(normalized_vector, normal)
if np.array_equal(orthogonal, np.zeros((3,))):
raise ValueError("The input vectors are not linearly independent.")
orthogonal = np.add(np.multiply(orthogonal, distance), origin)
return orthogonal