diff --git a/main.py b/main.py index 1f43c45..f21ea28 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ 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 editor = Editor(buffering=True) @@ -13,10 +14,13 @@ editor = Editor(buffering=True) # # Build a cube # geometry.placeCuboid(editor, (458, 92, 488), (468, 99, 471), Block("oak_planks")) -curve = curve.Curve([(396, 132, 740), (435, 138, 730), - (443, 161, 758), (417, 73, 729)]) -curve.compute_curve() +# curve = curve.Curve([(396, 132, 740), (435, 138, 730), +# (443, 161, 758), (417, 73, 729)]) +# curve.compute_curve() -for point in curve.computed_points: - print(point) - editor.placeBlock(point, Block("stone")) +# for point in curve.computed_points: +# print(point) +# 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)) diff --git a/networks/curve.py b/networks/Curve.py similarity index 73% rename from networks/curve.py rename to networks/Curve.py index 01f0c89..74bdbcc 100644 --- a/networks/curve.py +++ b/networks/Curve.py @@ -5,21 +5,17 @@ from scipy import interpolate class Curve: def __init__(self, target_points): # list of points to [(x1, y1, z1), (...), ...] - self.target_points = target_points - self.computed_points = [] + self.computed_points = compute_curve(target_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. 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 - points = tuple(map(tuple, np.array(self.target_points))) + points = tuple(map(tuple, np.array(target_points))) points = sorted(set(points), key=points.index) # 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) 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)] + + @staticmethod + def offset(self): + pass diff --git a/networks/Segment.py b/networks/Segment.py new file mode 100644 index 0000000..a0cd8e6 --- /dev/null +++ b/networks/Segment.py @@ -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 diff --git a/networks/roads/road.py b/networks/roads/Road.py similarity index 100% rename from networks/roads/road.py rename to networks/roads/Road.py