2 * Copyright © 2012 Jonas Ådahl
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33 filter_dispatch(struct motion_filter *filter,
34 struct motion_params *motion,
35 void *data, uint32_t time)
37 filter->interface->filter(filter, motion, data, time);
41 * Pointer acceleration filter
44 #define MAX_VELOCITY_DIFF 1.0
45 #define MOTION_TIMEOUT 300 /* (ms) */
46 #define NUM_POINTER_TRACKERS 16
48 struct pointer_tracker {
55 struct pointer_accelerator;
56 struct pointer_accelerator {
57 struct motion_filter base;
59 accel_profile_func_t profile;
66 struct pointer_tracker *trackers;
79 UNDEFINED_DIRECTION = 0xff
83 get_direction(int dx, int dy)
85 int dir = UNDEFINED_DIRECTION;
89 if (abs(dx) < 2 && abs(dy) < 2) {
92 else if (dx > 0 && dy < 0)
94 else if (dx < 0 && dy > 0)
96 else if (dx < 0 && dy < 0)
108 /* Calculate r within the interval [0 to 8)
110 * r = [0 .. 2π] where 0 is North
111 * d_f = r / 2π ([0 .. 1))
115 r = fmod(r + 2.5*M_PI, 2*M_PI);
118 /* Mark one or two close enough octants */
119 d1 = (int)(r + 0.9) % 8;
120 d2 = (int)(r + 0.1) % 8;
122 dir = (1 << d1) | (1 << d2);
129 feed_trackers(struct pointer_accelerator *accel,
130 double dx, double dy,
134 struct pointer_tracker *trackers = accel->trackers;
136 for (i = 0; i < NUM_POINTER_TRACKERS; i++) {
137 trackers[i].dx += dx;
138 trackers[i].dy += dy;
141 current = (accel->cur_tracker + 1) % NUM_POINTER_TRACKERS;
142 accel->cur_tracker = current;
144 trackers[current].dx = 0.0;
145 trackers[current].dy = 0.0;
146 trackers[current].time = time;
147 trackers[current].dir = get_direction(dx, dy);
150 static struct pointer_tracker *
151 tracker_by_offset(struct pointer_accelerator *accel, unsigned int offset)
154 (accel->cur_tracker + NUM_POINTER_TRACKERS - offset)
155 % NUM_POINTER_TRACKERS;
156 return &accel->trackers[index];
160 calculate_tracker_velocity(struct pointer_tracker *tracker, uint32_t time)
168 distance = sqrt(dx*dx + dy*dy);
169 return distance / (double)(time - tracker->time);
173 calculate_velocity(struct pointer_accelerator *accel, uint32_t time)
175 struct pointer_tracker *tracker;
178 double initial_velocity;
179 double velocity_diff;
182 unsigned int dir = tracker_by_offset(accel, 0)->dir;
184 /* Find first velocity */
185 for (offset = 1; offset < NUM_POINTER_TRACKERS; offset++) {
186 tracker = tracker_by_offset(accel, offset);
188 if (time <= tracker->time)
191 result = initial_velocity =
192 calculate_tracker_velocity(tracker, time);
193 if (initial_velocity > 0.0)
197 /* Find least recent vector within a timelimit, maximum velocity diff
198 * and direction threshold. */
199 for (; offset < NUM_POINTER_TRACKERS; offset++) {
200 tracker = tracker_by_offset(accel, offset);
202 /* Stop if too far away in time */
203 if (time - tracker->time > MOTION_TIMEOUT ||
204 tracker->time > time)
207 /* Stop if direction changed */
212 velocity = calculate_tracker_velocity(tracker, time);
214 /* Stop if velocity differs too much from initial */
215 velocity_diff = fabs(initial_velocity - velocity);
216 if (velocity_diff > MAX_VELOCITY_DIFF)
226 acceleration_profile(struct pointer_accelerator *accel,
227 void *data, double velocity, uint32_t time)
229 return accel->profile(&accel->base, data, velocity, time);
233 calculate_acceleration(struct pointer_accelerator *accel,
234 void *data, double velocity, uint32_t time)
238 /* Use Simpson's rule to calculate the avarage acceleration between
239 * the previous motion and the most recent. */
240 factor = acceleration_profile(accel, data, velocity, time);
241 factor += acceleration_profile(accel, data, accel->last_velocity, time);
243 acceleration_profile(accel, data,
244 (accel->last_velocity + velocity) / 2,
247 factor = factor / 6.0;
253 soften_delta(double last_delta, double delta)
255 if (delta < -1.0 || delta > 1.0) {
256 if (delta > last_delta)
258 else if (delta < last_delta)
266 apply_softening(struct pointer_accelerator *accel,
267 struct motion_params *motion)
269 motion->dx = soften_delta(accel->last_dx, motion->dx);
270 motion->dy = soften_delta(accel->last_dy, motion->dy);
274 accelerator_filter(struct motion_filter *filter,
275 struct motion_params *motion,
276 void *data, uint32_t time)
278 struct pointer_accelerator *accel =
279 (struct pointer_accelerator *) filter;
283 feed_trackers(accel, motion->dx, motion->dy, time);
284 velocity = calculate_velocity(accel, time);
285 accel_value = calculate_acceleration(accel, data, velocity, time);
287 motion->dx = accel_value * motion->dx;
288 motion->dy = accel_value * motion->dy;
290 apply_softening(accel, motion);
292 accel->last_dx = motion->dx;
293 accel->last_dy = motion->dy;
295 accel->last_velocity = velocity;
299 accelerator_destroy(struct motion_filter *filter)
301 struct pointer_accelerator *accel =
302 (struct pointer_accelerator *) filter;
304 free(accel->trackers);
308 struct motion_filter_interface accelerator_interface = {
313 struct motion_filter *
314 create_pointer_accelator_filter(accel_profile_func_t profile)
316 struct pointer_accelerator *filter;
318 filter = malloc(sizeof *filter);
322 filter->base.interface = &accelerator_interface;
324 filter->profile = profile;
325 filter->last_velocity = 0.0;
330 calloc(NUM_POINTER_TRACKERS, sizeof *filter->trackers);
331 filter->cur_tracker = 0;
333 return &filter->base;