Skip to content

Commit 3baaf7b

Browse files
committedApr 4, 2016
Added trajectory splitting functions.
1 parent eadd1b9 commit 3baaf7b

File tree

1 file changed

+90
-3
lines changed

1 file changed

+90
-3
lines changed
 

‎src/prpy/util.py

+90-3
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,92 @@ def CopyTrajectory(traj, env=None):
295295
return copy_traj
296296

297297

298+
def GetTrajectoryHead(trajectory, t_split):
299+
"""
300+
Split the given trajectory at the given time and return the head.
301+
302+
The head trajectory is a partial copy of the original.
303+
304+
@param trajectory the trajectory that will be split
305+
@param t_split the time at which to split the trajectory
306+
@return a trajectory that goes from t=0 to t=split
307+
"""
308+
if not IsTimedTrajectory(trajectory):
309+
raise ValueError("Cannot split untimed trajectory at a time.")
310+
311+
if not (0. <= t_split <= trajectory.GetDuration()):
312+
raise ValueError("Cannot split trajectory of duration {:f} at t={:f}."
313+
.format(trajectory.GetDuration(), t_split))
314+
315+
# Find the intermediate point and index of the split time.
316+
split_waypoint = trajectory.Sample(t_split)
317+
split_idx = trajectory.GetFirstWaypointIndexAfterTime(t_split)
318+
319+
# Create a trajectory from the beginning to the split.
320+
head = openravepy.RaveCreateTrajectory(trajectory.GetEnv(),
321+
trajectory.GetXMLId())
322+
head.Init(trajectory.GetConfigurationSpecification())
323+
head.Insert(0, trajectory.GetWaypoints(0, split_idx))
324+
head.Insert(head.GetNumWaypoints(), split_waypoint)
325+
return head
326+
327+
328+
def GetTrajectoryTail(trajectory, t_split):
329+
"""
330+
Split the given trajectory at the given time and returns the tail.
331+
332+
The tail trajectory is a partial copy of the original.
333+
334+
@param trajectory the trajectory that will be split
335+
@param t_split the time at which to split the trajectory
336+
@return a trajectory that goes from t=split to t=end
337+
"""
338+
if not IsTimedTrajectory(trajectory):
339+
raise ValueError("Cannot split untimed trajectory at a time.")
340+
341+
if not (0. <= t_split <= trajectory.GetDuration()):
342+
raise ValueError("Cannot split trajectory of duration {:f} at t={:f}."
343+
.format(trajectory.GetDuration(), t_split))
344+
345+
# Find the intermediate point and index of the split time.
346+
split_waypoint = trajectory.Sample(t_split)
347+
split_idx = trajectory.GetFirstWaypointIndexAfterTime(t_split)
348+
349+
# Correct for the delta-time offset of the split waypoint.
350+
cspec = trajectory.GetConfigurationSpecification()
351+
split_delta = cspec.ExtractDeltaTime(split_waypoint)
352+
cspec.InsertDeltaTime(split_waypoint, 0.)
353+
354+
# Create a trajectory from the split to the end.
355+
tail = openravepy.RaveCreateTrajectory(trajectory.GetEnv(),
356+
trajectory.GetXMLId())
357+
tail.Init(trajectory.GetConfigurationSpecification())
358+
tail.Insert(0, split_waypoint)
359+
tail.Insert(1, trajectory.GetWaypoints(split_idx,
360+
trajectory.GetNumWaypoints()))
361+
362+
# Correct the delta-time of the first waypoint in the tail.
363+
waypoint = tail.GetWaypoint(1)
364+
waypoint_delta = cspec.ExtractDeltaTime(waypoint)
365+
cspec.InsertDeltaTime(waypoint, waypoint_delta - split_delta)
366+
tail.Insert(1, waypoint, True)
367+
return tail
368+
369+
370+
def SplitTrajectory(trajectory, t_split):
371+
"""
372+
Split the given trajectory at the given time and returns the head and tail.
373+
374+
The head and tail trajectories are copies of the original.
375+
376+
@param trajectory the trajectory that will be split
377+
@param t_split the time at which to split the trajectory
378+
@return a trajectory tuple of (head, tail)
379+
"""
380+
return (GetTrajectoryHead(trajectory, t_split),
381+
GetTrajectoryTail(trajectory, t_split))
382+
383+
298384
def GetTrajectoryTags(traj):
299385
""" Read key/value pairs from a trajectory.
300386
@@ -1137,6 +1223,7 @@ def ComputeEnabledAABB(kinbody):
11371223
half_extents = (max_corner - min_corner) / 2.
11381224
return AABB(center, half_extents)
11391225

1226+
11401227
def UntimeTrajectory(trajectory, env=None):
11411228
"""
11421229
Returns an untimed copy of the provided trajectory.
@@ -1617,7 +1704,7 @@ def GetLinearCollisionCheckPts(robot, traj, norm_order=2, sampling_func=None):
16171704
inf ==> The L_infinity norm
16181705
@param generator sampling_func A function that returns a sequence of
16191706
sample times.
1620-
e.g. SampleTimeGenerator()
1707+
e.g. SampleTimeGenerator()
16211708
or
16221709
VanDerCorputSampleGenerator()
16231710
@@ -1684,7 +1771,7 @@ def GetLinearCollisionCheckPts(robot, traj, norm_order=2, sampling_func=None):
16841771
q1 = traj_cspec.ExtractJointValues(waypoint, robot, dof_indices)
16851772
dq = numpy.abs(q1 - q0)
16861773
max_diff_float = numpy.max( numpy.abs(q1 - q0) / q_resolutions)
1687-
1774+
16881775
# Get the number of steps (as a float) required for
16891776
# each joint at DOF resolution
16901777
num_steps = dq / q_resolutions
@@ -1717,7 +1804,7 @@ def GetLinearCollisionCheckPts(robot, traj, norm_order=2, sampling_func=None):
17171804

17181805
# Sample the trajectory using the specified sample generator
17191806
seq = None
1720-
if sampling_func == None:
1807+
if sampling_func is None:
17211808
# (default) Linear sequence, from start to end
17221809
seq = SampleTimeGenerator(0, traj_duration, step=2)
17231810
else:

0 commit comments

Comments
 (0)
Please sign in to comment.