def downsample_threshold(self, threshold): """ Downsample by removing consecutive samples for which the euclidean distance is inferior to threshold. @type threshod: int """ if len(self) == 0: return new_s = Stroke() new_s.append_point(self[0]) last = 0 for i in range(1, len(self) - 2): u = [self[last].x, self[last].y] v = [self[i].x, self[i].y] if euclidean_distance(u, v) > threshold: new_s.append_point(self[i]) last = i new_s.append_point(self[-1]) self.copy_from(new_s)
def draw_stroke(self, stroke, index, color, draw_annotation=False, draw_circle=False): l = len(stroke) self.cr.save() self._with_handwriting_line() self.cr.set_source_rgba(*color) point0 = stroke[0] if draw_circle: self._draw_small_circle(point0.x, point0.y) self.cr.move_to(point0.x, point0.y) last_point = point0 n_points = len(stroke) i = 1 for point in stroke[1:]: self.cr.line_to(point.x, point.y) self.cr.stroke() self.cr.move_to(point.x, point.y) dist = euclidean_distance(point.get_coordinates(), last_point.get_coordinates()) if dist > 50 or i == n_points - 1: win = 100 # window size x1 = last_point.x - win; y1 = last_point.y - win x2 = point.x + win; y2 = point.y + win if x1 > x2: x1, x2 = x2, x1 if y1 > y2: y1, y2 = y2, y1 w = x2 - x1; h = y2 - y1 if point.timestamp and last_point.timestamp: delay = point.timestamp - last_point.timestamp else: delay = None if w > 0 and h > 0: self._area_changed(x1, y1, w, h, delay) last_point = point i += 1 self.cr.stroke() self.cr.restore() if self.stroke_added_cb: self.stroke_added_cb() if draw_annotation: self._draw_annotation(stroke, index)
def _upsample(self, func): """ 'Artificially' increase sampling, using func(distance) to determine how many samples to add between consecutive points. """ if len(self) == 0: return new_s = Stroke() for i in range(len(self) - 1): x1, y1 = [self[i].x, self[i].y] x2, y2 = [self[i + 1].x, self[i + 1].y] new_s.append_point(self[i]) dx = x2 - x1 dy = y2 - y1 if dx == 0: alpha = pi / 2 cosalpha = 0.0 sinalpha = 1.0 else: alpha = atan(float(abs(dy)) / abs(x2 - x1)) cosalpha = cos(alpha) sinalpha = sin(alpha) d = euclidean_distance([x1, y1], [x2, y2]) signx = cmp(dx, 0) signy = cmp(dy, 0) n = func(d) for j in range(1, n + 1): dx = cosalpha * 1.0 / (n + 1) * d dy = sinalpha * 1.0 / (n + 1) * d new_s.append_point( Point(x=int(x1 + j * dx * signx), y=int(y1 + j * dy * signy))) new_s.append_point(self[-1]) self.copy_from(new_s)
def _upsample(self, func): """ 'Artificially' increase sampling, using func(distance) to determine how many samples to add between consecutive points. """ if len(self) == 0: return new_s = Stroke() for i in range(len(self)- 1): x1, y1 = [self[i].x, self[i].y] x2, y2 = [self[i+1].x, self[i+1].y] new_s.append_point(self[i]) dx = x2 - x1 dy = y2 - y1 if dx == 0: alpha = pi / 2 cosalpha = 0.0 sinalpha = 1.0 else: alpha = atan(float(abs(dy)) / abs(x2 - x1)) cosalpha = cos(alpha) sinalpha = sin(alpha) d = euclidean_distance([x1, y1], [x2, y2]) signx = cmp(dx, 0) signy = cmp(dy, 0) n = func(d) for j in range(1, n+1): dx = cosalpha * 1.0 / (n + 1) * d dy = sinalpha * 1.0 / (n + 1) * d new_s.append_point(Point(x=int(x1+j*dx*signx), y=int(y1+j*dy*signy))) new_s.append_point(self[-1]) self.copy_from(new_s)
def draw_stroke(self, stroke, index, color, draw_annotation=False, draw_circle=False): l = len(stroke) self.cr.save() self._with_handwriting_line() self.cr.set_source_rgba(*color) point0 = stroke[0] if draw_circle: self._draw_small_circle(point0.x, point0.y) self.cr.move_to(point0.x, point0.y) last_point = point0 n_points = len(stroke) i = 1 for point in stroke[1:]: self.cr.line_to(point.x, point.y) self.cr.stroke() self.cr.move_to(point.x, point.y) dist = euclidean_distance(point.get_coordinates(), last_point.get_coordinates()) if dist > 50 or i == n_points - 1: win = 100 # window size x1 = last_point.x - win y1 = last_point.y - win x2 = point.x + win y2 = point.y + win if x1 > x2: x1, x2 = x2, x1 if y1 > y2: y1, y2 = y2, y1 w = x2 - x1 h = y2 - y1 if point.timestamp and last_point.timestamp: delay = point.timestamp - last_point.timestamp else: delay = None if w > 0 and h > 0: self._area_changed(x1, y1, w, h, delay) last_point = point i += 1 self.cr.stroke() self.cr.restore() if self.stroke_added_cb: self.stroke_added_cb() if draw_annotation: self._draw_annotation(stroke, index)