-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHelper_Functions.py
More file actions
132 lines (104 loc) · 4.22 KB
/
Helper_Functions.py
File metadata and controls
132 lines (104 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import moderngl
import glm
import numpy as np
import stl
import math
def print_help():
print("File, Depth of Cut, Tool Diameter (all units in mm).")
def deg_to_rad(degrees):
return (degrees * np.pi) / 180.0
def load_shader(filepath):
shader_file = open(filepath)
with shader_file as file:
shader = file.read()
shader_file.close()
return shader
def calculate_aspect_ratio(res_tuple):
gcd = np.gcd(res_tuple[0], res_tuple[1])
return ((res_tuple[0] / gcd), (res_tuple[1] / gcd))
def get_model_min_max(model):
x_min = np.min(model[:,:,0])
x_max = np.max(model[:,:,0])
y_min = np.min(model[:,:,1])
y_max = np.max(model[:,:,1])
z_min = np.min(model[:,:,2])
z_max = np.max(model[:,:,2])
return (x_min, x_max, y_min, y_max, z_min, z_max)
def determine_square_ortho(min_max_tuple):
highest_coords = np.ceil(min_max_tuple[1::2])
lowest_coords = np.floor(min_max_tuple[0::2])
xy_midpoint = (highest_coords[0:4] + lowest_coords[0:4]) / 2
largest_dim = 0
if xy_midpoint[0] > xy_midpoint[1]:
largest_dim = xy_midpoint[0]
else:
largest_dim = xy_midpoint[1]
return largest_dim
def bounding_box_circle(center, radius: float, target_res=0.1, margin=0):
top = (math.floor(center[0]), math.ceil(center[1] + radius + margin))
bottom = (math.floor(center[0]), math.floor(center[1] - radius - margin))
left = (math.floor(center[0] - radius - margin), math.floor(center[1]))
right = (math.ceil(center[0] + radius + margin), math.floor(center[1]))
return (top, bottom, left, right)
def double_circle_bbox(center1, radius1: float, center2, radius2: float):
'''
Takes two circles, represented by their center coordinates and
radii, and calculates a bounding box containing both circles.
'''
top_circ1 = math.ceil(center1[1] + radius1)
bottom_circ1 = math.floor(center1[1] - radius1)
left_circ1 = math.floor(center1[0] - radius1)
right_circ1 = math.ceil(center1[0] + radius1)
top_circ2 = math.ceil(center2[1] + radius2)
bottom_circ2 = math.floor(center2[1] - radius2)
left_circ2 = math.floor(center2[0] - radius2)
right_circ2 = math.ceil(center2[0] + radius2)
top = top_circ1 if top_circ1 > top_circ2 else top_circ2
bottom = bottom_circ1 if bottom_circ1 < bottom_circ2 else bottom_circ2
left = left_circ1 if left_circ1 < left_circ2 else left_circ2
right = right_circ1 if right_circ1 > right_circ2 else right_circ2
return (top, bottom, left, right)
def check_point_in_circle(circ_center, radius, pixel_coord):
pythag = (pixel_coord[0] - circ_center[0])**2 + (pixel_coord[1] - circ_center[1])**2
if pythag <= radius**2:
return True
else:
return False
def gen_test_gcode(array, retract_height):
if len(array) < 1:
raise Exception("Location list is empty, no Gcode to output.")
gcode_file = open("testGcode.ngc", "w")
gcode_file.write(f"G21\nG0 Z{retract_height}\n")
for layer, height in array:
first_move = layer[0]
first_coords = []
print(first_move)
match first_move[0]:
case 0:
first_coords = first_move[1][0]
case 1:
first_coords = first_move[1]
case 2:
first_coords = first_move[1]
case _:
first_coords = []
if len(first_coords) < 2 or len(first_coords) > 2:
raise Exception("Malformed Paths Found in test_gcode")
gcode_file.write(f"G0 Z{retract_height}\n")
gcode_file.write(f"G0 X{first_coords[0]} Y{first_coords[1]}\n")
for link in layer:
gcode = ""
if link[0] == 0:
for coord in link[1]:
gcode = f"G1 F600 X{coord[0]} Y{coord[1]} Z{height}\n"
gcode_file.write(gcode)
elif link[0] == 1:
coord = link[1]
gcode = f"G0 X{coord[0]} Y{coord[1]} Z{height}\n"
gcode_file.write(gcode)
elif link[0] == 2:
coord = link[1]
gcode = f"G0 Z{retract_height}\nG0 X{coord[0]} Y{coord[1]}\nG0 Z{height}\n"
gcode_file.write(gcode)
gcode_file.write("M2\n")
gcode_file.close()