| 39 | |
| 40 | |
| 41 | class MotionTrack(object): |
| 42 | def __init__(self, points=None, speed=0.4): |
| 43 | super(MotionTrack, self).__init__() |
| 44 | self.speed = speed |
| 45 | self.timestamp = 0 |
| 46 | self.event_points = [] # [ts, (x, y), contact_id], timestamp as arrival time |
| 47 | |
| 48 | if points: |
| 49 | for p in points: |
| 50 | self.move(p) |
| 51 | |
| 52 | @property |
| 53 | def last_point(self): |
| 54 | if self.event_points: |
| 55 | return self.event_points[-1][1] |
| 56 | return None |
| 57 | |
| 58 | def start(self, p): |
| 59 | return self.move(p) |
| 60 | |
| 61 | def move(self, p): |
| 62 | if self.last_point: |
| 63 | dt = (Vec2(p) - Vec2(self.last_point)).length / self.speed |
| 64 | self.timestamp += dt |
| 65 | self.event_points.append([self.timestamp, p, 0]) |
| 66 | return self |
| 67 | |
| 68 | def hold(self, t): |
| 69 | self.timestamp += t |
| 70 | if self.event_points: |
| 71 | self.move(self.last_point) |
| 72 | return self |
| 73 | |
| 74 | def set_contact_id(self, _id): |
| 75 | for ep in self.event_points: |
| 76 | ep[2] = _id |
| 77 | |
| 78 | def discretize(self, contact_id=0, accuracy=0.004, dt=0.001): |
| 79 | """ |
| 80 | Sample this motion track into discretized motion events. |
| 81 | |
| 82 | Args: |
| 83 | contact_id: contact point id |
| 84 | accuracy: motion minimum difference in space |
| 85 | dt: sample time difference |
| 86 | """ |
| 87 | |
| 88 | if not self.event_points: |
| 89 | return [] |
| 90 | |
| 91 | events = [] |
| 92 | action_dt = accuracy / self.speed |
| 93 | dt = dt or action_dt |
| 94 | |
| 95 | ep0 = self.event_points[0] |
| 96 | for _ in range(int(ep0[0] / dt)): |
| 97 | events.append(['s', dt]) |
| 98 | events.append(['d', ep0[1], contact_id]) |
no outgoing calls
no test coverage detected