blob: ac926aa2668c0faacc540457ec2b9210448e0bab [file] [log] [blame]
milind-u18a901d2023-02-17 21:51:55 -08001import abc
2import numpy as np
3import sys
4import traceback
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -08005
6# joint_center in x-y space.
milind-u18a901d2023-02-17 21:51:55 -08007IN_TO_M = 0.0254
8joint_center = (-0.203, 0.787)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -08009
10# Joint distances (l1 = "proximal", l2 = "distal")
milind-u18a901d2023-02-17 21:51:55 -080011l1 = 20.0 * IN_TO_M
milind-u68842e12023-02-26 12:45:40 -080012l2 = 38.0 * IN_TO_M
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080013
14max_dist = 0.01
milind-u18a901d2023-02-17 21:51:55 -080015max_dist_theta = np.pi / 64
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080016xy_end_circle_size = 0.01
17theta_end_circle_size = 0.07
18
19
milind-u060e4cf2023-02-22 00:08:52 -080020# Shift the angle between the convention used for input/output and the convention we use for some computations here
21def shift_angle(theta):
22 return np.pi / 2 - theta
23
24
25def shift_angles(thetas):
26 return [shift_angle(theta) for theta in thetas]
27
28
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080029# Convert from x-y coordinates to theta coordinates.
30# orientation is a bool. This orientation is circular_index mod 2.
31# where circular_index is the circular index, or the position in the
32# "hyperextension" zones. "cross_point" allows shifting the place where
33# it rounds the result so that it draws nicer (no other functional differences).
milind-u600738b2023-02-22 14:42:19 -080034def to_theta(pt, circular_index, cross_point=-np.pi, die=True):
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080035 orient = (circular_index % 2) == 0
36 x = pt[0]
37 y = pt[1]
milind-u68842e12023-02-26 12:45:40 -080038 x -= joint_center[0] - 1e-9
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080039 y -= joint_center[1]
milind-u18a901d2023-02-17 21:51:55 -080040 l3 = np.hypot(x, y)
41 t3 = np.arctan2(y, x)
42 theta1 = np.arccos((l1**2 + l3**2 - l2**2) / (2 * l1 * l3))
43 if np.isnan(theta1):
milind-u600738b2023-02-22 14:42:19 -080044 print(("Couldn't fit triangle to %f, %f, %f" % (l1, l2, l3)))
45 if die:
46 traceback.print_stack()
47 sys.exit(1)
48 return None
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080049
50 if orient:
51 theta1 = -theta1
52 theta1 += t3
milind-u18a901d2023-02-17 21:51:55 -080053 theta1 = (theta1 - cross_point) % (2 * np.pi) + cross_point
54 theta2 = np.arctan2(y - l1 * np.sin(theta1), x - l1 * np.cos(theta1))
55 return np.array((theta1, theta2))
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080056
57
58# Simple trig to go back from theta1, theta2 to x-y
59def to_xy(theta1, theta2):
milind-u18a901d2023-02-17 21:51:55 -080060 x = np.cos(theta1) * l1 + np.cos(theta2) * l2 + joint_center[0]
61 y = np.sin(theta1) * l1 + np.sin(theta2) * l2 + joint_center[1]
62 orient = ((theta2 - theta1) % (2.0 * np.pi)) < np.pi
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -080063 return (x, y, orient)
64
65
milind-u18a901d2023-02-17 21:51:55 -080066END_EFFECTOR_X_LEN = (-1.0 * IN_TO_M, 10.425 * IN_TO_M)
milind-u2a28c592023-02-24 23:13:04 -080067END_EFFECTOR_Y_LEN = (-3.5 * IN_TO_M, 7.325 * IN_TO_M)
milind-u18a901d2023-02-17 21:51:55 -080068END_EFFECTOR_Z_LEN = (-11.0 * IN_TO_M, 11.0 * IN_TO_M)
69
70
71def abs_sum(l):
72 result = 0
73 for e in l:
74 result += abs(e)
75 return result
76
77
78def affine_3d(R, T):
79 H = np.eye(4)
80 H[:3, 3] = T
81 H[:3, :3] = R
82 return H
83
84
85# Simple trig to go back from theta1, theta2, and theta3 to
86# the 8 corners on the roll joint x-y-z
87def to_end_effector_points(theta1, theta2, theta3):
88 x, y, _ = to_xy(theta1, theta2)
89 # Homogeneous end effector points relative to the end_effector
90 # ee = end effector
91 endpoints_ee = []
92 for i in range(2):
93 for j in range(2):
94 for k in range(2):
95 endpoints_ee.append(
96 np.array((END_EFFECTOR_X_LEN[i], END_EFFECTOR_Y_LEN[j],
97 END_EFFECTOR_Z_LEN[k], 1.0)))
98
99 # Only roll.
100 # rj = roll joint
101 roll = theta3
102 T_rj_ee = np.zeros(3)
103 R_rj_ee = np.array([[1.0, 0.0, 0.0], [0.0,
104 np.cos(roll), -np.sin(roll)],
105 [0.0, np.sin(roll), np.cos(roll)]])
106 H_rj_ee = affine_3d(R_rj_ee, T_rj_ee)
107
108 # Roll joint pose relative to the origin
109 # o = origin
110 T_o_rj = np.array((x, y, 0))
111 # Only yaw
112 yaw = theta1 + theta2
113 R_o_rj = [[np.cos(yaw), -np.sin(yaw), 0.0],
114 [np.sin(yaw), np.cos(yaw), 0.0], [0.0, 0.0, 1.0]]
115 H_o_rj = affine_3d(R_o_rj, T_o_rj)
116
117 # Now compute the pose of the end effector relative to the origin
118 H_o_ee = H_o_rj @ H_rj_ee
119
120 # Get the translation from these transforms
121 endpoints_o = [(H_o_ee @ endpoint_ee)[:3] for endpoint_ee in endpoints_ee]
122
123 diagonal_distance = np.linalg.norm(
124 np.array(endpoints_o[0]) - np.array(endpoints_o[-1]))
125 actual_diagonal_distance = np.linalg.norm(
126 np.array((abs_sum(END_EFFECTOR_X_LEN), abs_sum(END_EFFECTOR_Y_LEN),
127 abs_sum(END_EFFECTOR_Z_LEN))))
128 assert abs(diagonal_distance - actual_diagonal_distance) < 1e-5
129
130 return np.array(endpoints_o)
131
132
133# Returns all permutations of rectangle points given two opposite corners.
134# x is the two x values, y is the two y values, z is the two z values
135def rect_points(x, y, z):
136 points = []
137 for i in range(2):
138 for j in range(2):
139 for k in range(2):
140 points.append((x[i], y[j], z[k]))
141 return np.array(points)
142
143
144DRIVER_CAM_Z_OFFSET = 3.225 * IN_TO_M
145DRIVER_CAM_POINTS = rect_points(
146 (-5.126 * IN_TO_M + joint_center[0], 0.393 * IN_TO_M + joint_center[0]),
147 (5.125 * IN_TO_M + joint_center[1], 17.375 * IN_TO_M + joint_center[1]),
148 (-8.475 * IN_TO_M - DRIVER_CAM_Z_OFFSET,
149 -4.350 * IN_TO_M - DRIVER_CAM_Z_OFFSET))
150
151
milind-u2a28c592023-02-24 23:13:04 -0800152def rect_collision_1d(min_1, max_1, min_2, max_2):
153 return (min_1 <= min_2 <= max_1) or (min_1 <= max_2 <= max_1) or (
154 min_2 < min_1 and max_2 > max_1)
milind-u18a901d2023-02-17 21:51:55 -0800155
156
157def roll_joint_collision(theta1, theta2, theta3):
milind-u060e4cf2023-02-22 00:08:52 -0800158 theta1 = shift_angle(theta1)
159 theta2 = shift_angle(theta2)
160 theta3 = shift_angle(theta3)
161
milind-u18a901d2023-02-17 21:51:55 -0800162 end_effector_points = to_end_effector_points(theta1, theta2, theta3)
163
164 assert len(end_effector_points) == 8 and len(end_effector_points[0]) == 3
165 assert len(DRIVER_CAM_POINTS) == 8 and len(DRIVER_CAM_POINTS[0]) == 3
milind-u2a28c592023-02-24 23:13:04 -0800166 collided = True
milind-u18a901d2023-02-17 21:51:55 -0800167
milind-u2a28c592023-02-24 23:13:04 -0800168 for i in range(len(end_effector_points[0])):
169 min_ee = min(end_effector_points[:, i])
170 max_ee = max(end_effector_points[:, i])
milind-u18a901d2023-02-17 21:51:55 -0800171
milind-u2a28c592023-02-24 23:13:04 -0800172 min_dc = min(DRIVER_CAM_POINTS[:, i])
173 max_dc = max(DRIVER_CAM_POINTS[:, i])
milind-u18a901d2023-02-17 21:51:55 -0800174
milind-u2a28c592023-02-24 23:13:04 -0800175 collided &= rect_collision_1d(min_ee, max_ee, min_dc, max_dc)
176 return collided
milind-u18a901d2023-02-17 21:51:55 -0800177
178
milind-ueeb08c52023-02-21 22:30:16 -0800179# Delta limit means theta2 - theta1.
180# The limit for the proximal and distal is relative,
181# so define constraints for this delta.
182UPPER_DELTA_LIMIT = 0.0
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800183LOWER_DELTA_LIMIT = -1.98 * np.pi
milind-ueeb08c52023-02-21 22:30:16 -0800184
185# TODO(milind): put actual proximal limits
Austin Schuh9b3e41c2023-02-26 22:29:53 -0800186UPPER_PROXIMAL_LIMIT = np.pi * 2.0
187LOWER_PROXIMAL_LIMIT = -np.pi * 2.0
milind-ueeb08c52023-02-21 22:30:16 -0800188
Austin Schuh8edaf3e2023-02-22 21:20:52 -0800189UPPER_DISTAL_LIMIT = 0.75 * np.pi
190LOWER_DISTAL_LIMIT = -0.75 * np.pi
191
milind-ueeb08c52023-02-21 22:30:16 -0800192UPPER_ROLL_JOINT_LIMIT = 0.75 * np.pi
193LOWER_ROLL_JOINT_LIMIT = -0.75 * np.pi
194
195
196def arm_past_limit(theta1, theta2, theta3):
197 delta = theta2 - theta1
Austin Schuh8edaf3e2023-02-22 21:20:52 -0800198 return delta > UPPER_DELTA_LIMIT or delta < LOWER_DELTA_LIMIT or \
199 theta1 > UPPER_PROXIMAL_LIMIT or theta1 < LOWER_PROXIMAL_LIMIT or \
200 theta2 > UPPER_DISTAL_LIMIT or theta2 < LOWER_DISTAL_LIMIT or \
201 theta3 > UPPER_ROLL_JOINT_LIMIT or theta3 < LOWER_ROLL_JOINT_LIMIT
milind-ueeb08c52023-02-21 22:30:16 -0800202
203
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800204def get_circular_index(theta):
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800205 return int(
206 np.floor((shift_angle(theta[1]) - shift_angle(theta[0])) / np.pi))
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800207
208
209def get_xy(theta):
milind-u060e4cf2023-02-22 00:08:52 -0800210 theta1 = shift_angle(theta[0])
211 theta2 = shift_angle(theta[1])
milind-u18a901d2023-02-17 21:51:55 -0800212 x = np.cos(theta1) * l1 + np.cos(theta2) * l2 + joint_center[0]
213 y = np.sin(theta1) * l1 + np.sin(theta2) * l2 + joint_center[1]
214 return np.array((x, y))
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800215
216
217# Subdivide in theta space.
218def subdivide_theta(lines):
219 out = []
220 last_pt = lines[0]
221 out.append(last_pt)
222 for n_pt in lines[1:]:
223 for pt in subdivide(last_pt, n_pt, max_dist_theta):
224 out.append(pt)
225 last_pt = n_pt
226
227 return out
228
229
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800230def to_theta_with_ci(pt, circular_index):
milind-u18a901d2023-02-17 21:51:55 -0800231 return (to_theta_with_circular_index(pt[0], pt[1], circular_index))
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800232
233
234# to_theta, but distinguishes between
235def to_theta_with_circular_index(x, y, circular_index):
236 theta1, theta2 = to_theta((x, y), circular_index)
milind-u18a901d2023-02-17 21:51:55 -0800237 n_circular_index = int(np.floor((theta2 - theta1) / np.pi))
238 theta2 = theta2 + ((circular_index - n_circular_index)) * np.pi
milind-u060e4cf2023-02-22 00:08:52 -0800239 return np.array((shift_angle(theta1), shift_angle(theta2)))
milind-u18a901d2023-02-17 21:51:55 -0800240
241
242# to_theta, but distinguishes between
243def to_theta_with_circular_index_and_roll(x, y, roll, circular_index):
244 theta12 = to_theta_with_circular_index(x, y, circular_index)
245 theta3 = roll
246 return np.array((theta12[0], theta12[1], theta3))
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800247
248
249# alpha is in [0, 1] and is the weight to merge a and b.
250def alpha_blend(a, b, alpha):
251 """Blends a and b.
252
253 Args:
254 alpha: double, Ratio. Needs to be in [0, 1] and is the weight to blend a
255 and b.
256 """
257 return b * alpha + (1.0 - alpha) * a
258
259
260def normalize(v):
261 """Normalize a vector while handling 0 length vectors."""
milind-u18a901d2023-02-17 21:51:55 -0800262 norm = np.linalg.norm(v)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800263 if norm == 0:
264 return v
265 return v / norm
266
267
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800268# Generic subdivision algorithm.
269def subdivide(p1, p2, max_dist):
270 dx = p2[0] - p1[0]
271 dy = p2[1] - p1[1]
milind-u18a901d2023-02-17 21:51:55 -0800272 dist = np.sqrt(dx**2 + dy**2)
273 n = int(np.ceil(dist / max_dist))
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800274 return [(alpha_blend(p1[0], p2[0],
275 float(i) / n), alpha_blend(p1[1], p2[1],
276 float(i) / n))
277 for i in range(1, n + 1)]
278
279
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800280def spline_eval(start, control1, control2, end, alpha):
281 a = alpha_blend(start, control1, alpha)
282 b = alpha_blend(control1, control2, alpha)
283 c = alpha_blend(control2, end, alpha)
284 return alpha_blend(alpha_blend(a, b, alpha), alpha_blend(b, c, alpha),
285 alpha)
286
287
milind-u18a901d2023-02-17 21:51:55 -0800288SPLINE_SUBDIVISIONS = 100
289
290
291def subdivide_multistep():
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800292 # TODO: pick N based on spline parameters? or otherwise change it to be more evenly spaced?
milind-u18a901d2023-02-17 21:51:55 -0800293 for i in range(0, SPLINE_SUBDIVISIONS + 1):
294 yield i / float(SPLINE_SUBDIVISIONS)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800295
296
milind-u18a901d2023-02-17 21:51:55 -0800297def get_proximal_distal_derivs(t_prev, t, t_next):
298 d_prev = normalize(t - t_prev)
299 d_next = normalize(t_next - t)
300 accel = (d_next - d_prev) / np.linalg.norm(t - t_next)
301 return (ThetaPoint(t[0], d_next[0],
302 accel[0]), ThetaPoint(t[1], d_next[1], accel[1]))
303
304
305def get_roll_joint_theta(theta_i, theta_f, t):
306 # Fit a theta(t) = (1 - cos(pi*t)) / 2,
307 # so that theta(0) = theta_i, and theta(1) = theta_f
308 offset = theta_i
309 scalar = (theta_f - theta_i) / 2.0
310 freq = np.pi
311 theta_curve = lambda t: scalar * (1 - np.cos(freq * t)) + offset
312
313 return theta_curve(t)
314
315
316def get_roll_joint_theta_multistep(alpha_rolls, alpha):
317 # Figure out which segment in the motion we're in
318 theta_i = None
319 theta_f = None
320 t = None
321
322 for i in range(len(alpha_rolls) - 1):
323 # Find the alpha segment we're in
324 if alpha_rolls[i][0] <= alpha <= alpha_rolls[i + 1][0]:
325 theta_i = alpha_rolls[i][1]
326 theta_f = alpha_rolls[i + 1][1]
327
328 total_dalpha = alpha_rolls[-1][0] - alpha_rolls[0][0]
329 assert total_dalpha == 1.0
330 dalpha = alpha_rolls[i + 1][0] - alpha_rolls[i][0]
331 t = (alpha - alpha_rolls[i][0]) * (total_dalpha / dalpha)
332 break
333 assert theta_i is not None
334 assert theta_f is not None
335 assert t is not None
336
337 return get_roll_joint_theta(theta_i, theta_f, t)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800338
339
Maxwell Henderson83cf6d62023-02-10 20:29:26 -0800340# Draw a list of lines to a cairo context.
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800341def draw_lines(cr, lines):
342 cr.move_to(lines[0][0], lines[0][1])
343 for pt in lines[1:]:
344 cr.line_to(pt[0], pt[1])
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800345
346
milind-u18a901d2023-02-17 21:51:55 -0800347class Path(abc.ABC):
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800348
milind-u18a901d2023-02-17 21:51:55 -0800349 def __init__(self, name):
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800350 self.name = name
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800351
milind-u18a901d2023-02-17 21:51:55 -0800352 @abc.abstractmethod
353 def DoToThetaPoints(self):
354 pass
355
356 @abc.abstractmethod
357 def DoDrawTo(self):
358 pass
359
360 @abc.abstractmethod
milind-uadd8fa32023-02-24 23:37:36 -0800361 def joint_thetas(self):
milind-u18a901d2023-02-17 21:51:55 -0800362 pass
363
364 @abc.abstractmethod
365 def intersection(self, event):
366 pass
367
368 def roll_joint_collision(self, points, verbose=False):
369 for point in points:
370 if roll_joint_collision(*point):
371 if verbose:
372 print("Roll joint collision for path %s in point %s" %
373 (self.name, point))
374 return True
375 return False
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800376
milind-ueeb08c52023-02-21 22:30:16 -0800377 def arm_past_limit(self, points, verbose=True):
378 for point in points:
379 if arm_past_limit(*point):
380 if verbose:
381 print("Arm past limit for path %s in point %s" %
382 (self.name, point))
383 return True
384 return False
385
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800386 def DrawTo(self, cr, theta_version):
milind-ueeb08c52023-02-21 22:30:16 -0800387 points = self.DoToThetaPoints()
388 if self.roll_joint_collision(points):
389 # Draw the spline red
milind-u18a901d2023-02-17 21:51:55 -0800390 cr.set_source_rgb(1.0, 0.0, 0.0)
milind-ueeb08c52023-02-21 22:30:16 -0800391 elif self.arm_past_limit(points):
392 # Draw the spline orange
393 cr.set_source_rgb(1.0, 0.5, 0.0)
394
milind-u18a901d2023-02-17 21:51:55 -0800395 self.DoDrawTo(cr, theta_version)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800396
milind-u4037bc72023-02-22 21:39:40 -0800397 def VerifyPoints(self):
milind-u18a901d2023-02-17 21:51:55 -0800398 points = self.DoToThetaPoints()
milind-ueeb08c52023-02-21 22:30:16 -0800399 if self.roll_joint_collision(points, verbose=True) or \
400 self.arm_past_limit(points, verbose=True):
milind-u18a901d2023-02-17 21:51:55 -0800401 sys.exit(1)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800402
403
milind-u18a901d2023-02-17 21:51:55 -0800404class SplineSegmentBase(Path):
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800405
milind-u18a901d2023-02-17 21:51:55 -0800406 def __init__(self, name):
407 super().__init__(name)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800408
milind-u18a901d2023-02-17 21:51:55 -0800409 @abc.abstractmethod
410 # Returns (start, control1, control2, end), each in the form
411 # (theta1, theta2, theta3)
412 def get_controls_theta(self):
413 pass
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800414
milind-u18a901d2023-02-17 21:51:55 -0800415 def intersection(self, event):
416 start, control1, control2, end = self.get_controls_theta()
417 for alpha in subdivide_multistep():
418 x, y = get_xy(spline_eval(start, control1, control2, end, alpha))
419 spline_point = np.array([x, y])
420 hovered_point = np.array([event.x, event.y])
421 if np.linalg.norm(hovered_point - spline_point) < 0.03:
422 return alpha
423 return None
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800424
425
milind-u18a901d2023-02-17 21:51:55 -0800426class ThetaSplineSegment(SplineSegmentBase):
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800427
milind-u18a901d2023-02-17 21:51:55 -0800428 # start and end are [theta1, theta2, theta3].
429 # controls are just [theta1, theta2].
430 # control_alpha_rolls are a list of [alpha, roll]
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800431 def __init__(self,
milind-u18a901d2023-02-17 21:51:55 -0800432 name,
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800433 start,
434 control1,
435 control2,
436 end,
milind-u18a901d2023-02-17 21:51:55 -0800437 control_alpha_rolls=[],
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800438 alpha_unitizer=None,
439 vmax=None):
milind-u18a901d2023-02-17 21:51:55 -0800440 super().__init__(name)
441 self.start = start[:2]
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800442 self.control1 = control1
443 self.control2 = control2
milind-u18a901d2023-02-17 21:51:55 -0800444 self.end = end[:2]
445 # There will always be roll at alpha = 0 and 1
446 self.alpha_rolls = [[0.0, start[2]]
447 ] + control_alpha_rolls + [[1.0, end[2]]]
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800448 self.alpha_unitizer = alpha_unitizer
449 self.vmax = vmax
450
Austin Schuh9a11ebd2023-02-26 14:16:31 -0800451 def Print(self, points):
452 # Find the name of the start end end points.
453 start_name = None
454 end_name = None
455 for name in points:
456 point = points[name]
457 if (self.start == point[:2]).all():
458 start_name = name
459 elif (self.end == point[:2]).all():
460 end_name = name
461
462 alpha_points = '[' + ', '.join([
463 f"({alpha}, np.pi * {theta / np.pi})"
464 for alpha, theta in self.alpha_rolls[1:-1]
465 ]) + ']'
466
467 def FormatToTheta(point):
468 x, y = get_xy(point)
469 circular_index = get_circular_index(point)
470 return "to_theta_with_circular_index(%.3f, %.3f, circular_index=%d)" % (
471 x, y, circular_index)
472
473 def FormatToThetaRoll(point, roll):
474 x, y = get_xy(point)
475 circular_index = get_circular_index(point)
476 return "to_theta_with_circular_index_and_roll(%.3f, %.3f, np.pi * %.2f, circular_index=%d)" % (
477 x, y, roll / np.pi, circular_index)
478
479 print('named_segments.append(')
480 print(' ThetaSplineSegment(')
481 print(f' name="{self.name}",')
482 print(
483 f' start=points["{start_name}"], # {FormatToThetaRoll(self.start, self.alpha_rolls[0][1])}'
484 )
485 print(
486 f' control1=np.array([{self.control1[0]}, {self.control1[1]}]), # {FormatToTheta(self.control1)}'
487 )
488 print(
489 f' control2=np.array([{self.control2[0]}, {self.control2[1]}]), # {FormatToTheta(self.control2)}'
490 )
491 print(
492 f' end=points["{end_name}"], # {FormatToThetaRoll(self.end, self.alpha_rolls[-1][1])}'
493 )
494 print(f' control_alpha_rolls={alpha_points},')
495 print(f'))')
496
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800497 def __repr__(self):
milind-u18a901d2023-02-17 21:51:55 -0800498 return "ThetaSplineSegment(%s, %s, %s, %s)" % (repr(
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800499 self.start), repr(self.control1), repr(
500 self.control2), repr(self.end))
501
milind-u18a901d2023-02-17 21:51:55 -0800502 def DoDrawTo(self, cr, theta_version):
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800503 if (theta_version):
504 draw_lines(cr, [
milind-u060e4cf2023-02-22 00:08:52 -0800505 shift_angles(
506 spline_eval(self.start, self.control1, self.control2,
507 self.end, alpha))
508 for alpha in subdivide_multistep()
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800509 ])
510 else:
511 start = get_xy(self.start)
512 end = get_xy(self.end)
513
514 draw_lines(cr, [
515 get_xy(
516 spline_eval(self.start, self.control1, self.control2,
517 self.end, alpha))
milind-u18a901d2023-02-17 21:51:55 -0800518 for alpha in subdivide_multistep()
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800519 ])
520
521 cr.move_to(start[0] + xy_end_circle_size, start[1])
milind-u18a901d2023-02-17 21:51:55 -0800522 cr.arc(start[0], start[1], xy_end_circle_size, 0, 2.0 * np.pi)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800523 cr.move_to(end[0] + xy_end_circle_size, end[1])
milind-u18a901d2023-02-17 21:51:55 -0800524 cr.arc(end[0], end[1], xy_end_circle_size, 0, 2.0 * np.pi)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800525
milind-u18a901d2023-02-17 21:51:55 -0800526 def DoToThetaPoints(self):
527 points = []
528 for alpha in subdivide_multistep():
529 proximal, distal = spline_eval(self.start, self.control1,
530 self.control2, self.end, alpha)
531 roll_joint = get_roll_joint_theta_multistep(
532 self.alpha_rolls, alpha)
533 points.append((proximal, distal, roll_joint))
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800534
milind-u18a901d2023-02-17 21:51:55 -0800535 return points
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800536
milind-u18a901d2023-02-17 21:51:55 -0800537 def get_controls_theta(self):
538 return (self.start, self.control1, self.control2, self.end)
Maxwell Hendersonf5123fe2023-02-04 13:44:41 -0800539
milind-uadd8fa32023-02-24 23:37:36 -0800540 def joint_thetas(self):
milind-u18a901d2023-02-17 21:51:55 -0800541 ts = []
milind-uadd8fa32023-02-24 23:37:36 -0800542 thetas = [[], [], []]
milind-u18a901d2023-02-17 21:51:55 -0800543 for alpha in subdivide_multistep():
milind-uadd8fa32023-02-24 23:37:36 -0800544 proximal, distal = spline_eval(self.start, self.control1,
545 self.control2, self.end, alpha)
milind-u18a901d2023-02-17 21:51:55 -0800546 roll_joint = get_roll_joint_theta_multistep(
547 self.alpha_rolls, alpha)
milind-uadd8fa32023-02-24 23:37:36 -0800548 thetas[0].append(proximal)
549 thetas[1].append(distal)
550 thetas[2].append(roll_joint)
milind-u18a901d2023-02-17 21:51:55 -0800551 ts.append(alpha)
552 return ts, thetas