gimpdisplayshell-coords.c 12.4 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 */

#include "config.h"

#include <gtk/gtk.h>

22 23
#include "libgimpmath/gimpmath.h"

24 25 26 27 28
#include "display-types.h"

#include "gimpdisplayshell.h"
#include "gimpdisplayshell-coords.h"

29 30
#include "core/gimpcoords-interpolate.h"

31 32
/* Velocity unit is screen pixels per millisecond we pass to tools as 1. */
#define VELOCITY_UNIT 3.0
33

34 35
#define EVENT_FILL_PRECISION 6.0

36 37
#define DIRECTION_RADIUS  (1.5 / MAX (shell->scale_x, shell->scale_y))

38 39 40
#define SMOOTH_FACTOR 0.3


41 42 43
static void gimp_display_shell_interpolate_stroke (GimpDisplayShell *shell,
                                                   GimpCoords       *coords);

44 45 46 47

static const GimpCoords default_coords = GIMP_COORDS_DEFAULT_VALUES;


48 49
/*  public functions  */

50 51 52 53 54 55 56 57 58
/**
 * gimp_display_shell_eval_event:
 * @shell:
 * @coords:
 * @inertia_factor:
 * @time:
 *
 * This function evaluates the event to decide if the change is
 * big enough to need handling and returns FALSE, if change is less
59 60
 * than set filter level taking a whole lot of load off any draw tools
 * that have no use for these events anyway. If the event is
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 * seen fit at first look, it is evaluated for speed and smoothed.
 * Due to lousy time resolution of events pretty strong smoothing is
 * applied to timestamps for sensible speed result. This function is
 * also ideal for other event adjustment like pressure curve or
 * calculating other derived dynamics factors like angular velocity
 * calculation from tilt values, to allow for even more dynamic
 * brushes. Calculated distance to last event is stored in GimpCoords
 * because its a sideproduct of velocity calculation and is currently
 * calculated in each tool. If they were to use this distance, more
 * resouces on recalculating the same value would be saved.
 *
 * Return value:
 **/
gboolean
gimp_display_shell_eval_event (GimpDisplayShell *shell,
                               GimpCoords       *coords,
77 78
                               gdouble           inertia_factor,
                               guint32           time)
79
{
80 81 82 83 84 85 86
  gdouble  delta_time  = 0.001;
  gdouble  delta_x     = 0.0;
  gdouble  delta_y     = 0.0;
  gdouble  dir_delta_x = 0.0;
  gdouble  dir_delta_y = 0.0;
  gdouble  distance    = 1.0;
  gboolean event_fill  = (inertia_factor > 0);
87
  gdouble  delta_pressure = 0.0;
88

89
  /* Smoothing causes problems with cursor tracking
90 91 92 93
   * when zoomed above screen resolution so we need to supress it.
   */
  if (shell->scale_x > 1.0 || shell->scale_y > 1.0)
    {
94
      inertia_factor = 0.0;
95
    }
96

97 98 99 100 101 102 103 104 105 106 107 108

  delta_time = (shell->last_motion_delta_time * (1 - SMOOTH_FACTOR)
                +  (time - shell->last_motion_time) * SMOOTH_FACTOR);

  delta_pressure = coords->pressure - shell->last_coords.pressure;

  /* Try to detect a pen lift */
  if ((delta_time < 50) && (fabs(delta_pressure) > 0.04) && (delta_pressure < 0.0))
    {
      return FALSE;
    }

109
  if (shell->last_motion_time == 0)
110 111
    {
      /* First pair is invalid to do any velocity calculation,
112
       * so we apply a constant value.
113
       */
114
      coords->velocity = 1.0;
115 116 117
    }
  else
    {
118 119
      gdouble filter;
      gdouble dist;
120
      gdouble delta_dir;
121

122 123 124
      delta_x = shell->last_coords.x - coords->x;
      delta_y = shell->last_coords.y - coords->y;

125 126
      /* Events with distances less than the screen resolution are not
       * worth handling.
127
       */
128 129
      filter = MIN (1 / shell->scale_x, 1 / shell->scale_y) / 2.0;

130
      if (fabs (delta_x) < filter && fabs (delta_y) < filter)
131 132
        return FALSE;

133 134
      delta_time = (shell->last_motion_delta_time * (1 - SMOOTH_FACTOR)
                    +  (time - shell->last_motion_time) * SMOOTH_FACTOR);
135

136
      distance = dist = sqrt (SQR (delta_x) + SQR (delta_y));
137

138 139 140
      /* If even smoothed time resolution does not allow to guess for speed,
       * use last velocity.
       */
141
      if (delta_time == 0)
142 143 144 145 146
        {
          coords->velocity = shell->last_coords.velocity;
        }
      else
        {
147 148 149
          /* We need to calculate the velocity in screen coordinates
           * for human interaction
           */
150
          gdouble screen_distance = (distance *
151 152 153
                                     MIN (shell->scale_x, shell->scale_y));

          /* Calculate raw valocity */
154
          coords->velocity = ((screen_distance / delta_time) / VELOCITY_UNIT);
155

156
          /* Adding velocity dependent smoothing, feels better in tools. */
157 158 159 160
          coords->velocity = (shell->last_coords.velocity *
                              (1 - MIN (SMOOTH_FACTOR, coords->velocity)) +
                              coords->velocity *
                              MIN (SMOOTH_FACTOR, coords->velocity));
161

162 163
          /* Speed needs upper limit */
          coords->velocity = MIN (coords->velocity, 1.0);
164
        }
165

166
      if ((fabs (delta_x) > DIRECTION_RADIUS) && (fabs (delta_y) > DIRECTION_RADIUS))
167 168 169 170 171 172 173 174
        {
          dir_delta_x = delta_x;
          dir_delta_y = delta_y;
        }
      else
        {
          gint x = 3;

175 176
          while (((fabs (dir_delta_x) < DIRECTION_RADIUS) ||
                  (fabs (dir_delta_y) < DIRECTION_RADIUS)) && (x >= 0))
177 178 179
            {
              const GimpCoords old_event = g_array_index (shell->event_history,
                                                          GimpCoords, x);
180

181 182 183 184 185 186 187
              dir_delta_x = old_event.x - coords->x;
              dir_delta_y = old_event.y - coords->y;

              x--;
            }
        }

188 189
      if ((fabs (dir_delta_x) < DIRECTION_RADIUS) ||
          (fabs (dir_delta_y) < DIRECTION_RADIUS))
190
        {
191
          coords->direction = shell->last_coords.direction;
192 193 194
        }
      else
        {
195 196 197 198
          coords->direction = atan ((- 1.0 * dir_delta_y) /
                                    dir_delta_x) / (2 * G_PI);

          if (dir_delta_x > 0.0)
199 200 201
            coords->direction = coords->direction + 0.5;
        }

202
      coords->direction = coords->direction - floor (coords->direction);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

      delta_dir = coords->direction - shell->last_coords.direction;

      if ((fabs (delta_dir) > 0.5) && (delta_dir < 0.0))
        {
          coords->direction = (0.5 * coords->direction +
                               0.5 * (shell->last_coords.direction - 1.0));
        }
      else if ((fabs (delta_dir) > 0.5) && (delta_dir > 0.0))
        {
          coords->direction = (0.5 * coords->direction +
                               0.5 * (shell->last_coords.direction + 1.0));
        }
      else
        {
          coords->direction = (0.5 * coords->direction +
                               0.5 * shell->last_coords.direction);
        }

222
      coords->direction = coords->direction - floor (coords->direction);
223

224 225
      /* High speed -> less smooth*/
      inertia_factor *= (1 - coords->velocity);
226

227
      if (inertia_factor > 0 && distance > 0)
228
        {
229 230 231
          /* Apply smoothing to X and Y. */

          /* This tells how far from the pointer can stray from the line */
232
          gdouble max_deviation = SQR (20 * inertia_factor);
233 234 235 236 237 238 239 240 241 242
          gdouble cur_deviation = max_deviation;
          gdouble sin_avg;
          gdouble sin_old;
          gdouble sin_new;
          gdouble cos_avg;
          gdouble cos_old;
          gdouble cos_new;
          gdouble new_x;
          gdouble new_y;

243 244
          sin_new = delta_x / distance;
          sin_old = shell->last_motion_delta_x / shell->last_motion_distance;
245 246
          sin_avg = sin (asin (sin_old) * inertia_factor +
                         asin (sin_new) * (1 - inertia_factor));
247

248 249
          cos_new = delta_y / distance;
          cos_old = shell->last_motion_delta_y / shell->last_motion_distance;
250 251
          cos_avg = cos (acos (cos_old) * inertia_factor +
                         acos (cos_new) * (1 - inertia_factor));
252

253 254
          delta_x = sin_avg * distance;
          delta_y = cos_avg * distance;
255

256 257
          new_x = (shell->last_coords.x - delta_x) * 0.5 + coords->x * 0.5;
          new_y = (shell->last_coords.y - delta_y) * 0.5 + coords->y * 0.5;
258

259
          cur_deviation = SQR (coords->x - new_x) + SQR (coords->y - new_y);
260 261 262 263 264 265 266 267 268 269 270 271 272

          while (cur_deviation >= max_deviation)
            {
              new_x = new_x * 0.8 + coords->x * 0.2;
              new_y = new_y * 0.8 + coords->y * 0.2;

              cur_deviation = (SQR (coords->x - new_x) +
                               SQR (coords->y - new_y));
            }

          coords->x = new_x;
          coords->y = new_y;

273 274
          delta_x = shell->last_coords.x - coords->x;
          delta_y = shell->last_coords.y - coords->y;
275 276

          /* Recalculate distance */
277
          distance = sqrt (SQR (delta_x) + SQR (delta_y));
278 279
        }

280 281 282 283 284 285 286 287 288 289 290 291 292
        /* do event fill for devices that do not provide enough events*/
        if (distance >= EVENT_FILL_PRECISION &&
            event_fill                       &&
            shell->event_history->len >= 2)
          {
            if (shell->event_delay)
              {
                gimp_display_shell_interpolate_stroke (shell,
                                                       coords);
              }
            else
              {
                shell->event_delay = TRUE;
293
                gimp_display_shell_push_event_history (shell, coords);
294 295 296 297 298 299 300 301 302
              }
          }
        else
          {
            if (shell->event_delay)
              {
                shell->event_delay = FALSE;
              }

303
            gimp_display_shell_push_event_history (shell, coords);
304 305
          }

306 307
#ifdef VERBOSE
      g_printerr ("DIST: %f, DT:%f, Vel:%f, Press:%f,smooth_dd:%f, sf %f\n",
308 309
                  distance,
                  delta_time,
310 311
                  shell->last_coords.velocity,
                  coords->pressure,
312
                  distance - dist,
313
                  inertia_factor);
314 315 316
#endif
    }

317
  g_array_append_val (shell->event_queue, *coords);
318

319
  shell->last_coords            = *coords;
320 321 322 323 324
  shell->last_motion_time       = time;
  shell->last_motion_delta_time = delta_time;
  shell->last_motion_delta_x    = delta_x;
  shell->last_motion_delta_y    = delta_y;
  shell->last_motion_distance   = distance;
325

326 327
  return TRUE;
}
328 329 330 331 332 333 334 335 336


/* Helper function fo managing event history */
void
gimp_display_shell_push_event_history (GimpDisplayShell *shell,
                                       GimpCoords       *coords)
{
   if (shell->event_history->len == 4)
     g_array_remove_index (shell->event_history, 0);
337

338 339 340 341 342 343 344
   g_array_append_val (shell->event_history, *coords);
}

static void
gimp_display_shell_interpolate_stroke (GimpDisplayShell *shell,
                                       GimpCoords       *coords)
{
345 346
  GArray *ret_coords;
  gint    i = shell->event_history->len - 1;
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

  /* Note that there must be exactly one event in buffer or bad things
   * can happen. This should never get called under other circumstances.
   */
  ret_coords = g_array_new (FALSE, FALSE, sizeof (GimpCoords));

  gimp_coords_interpolate_catmull (g_array_index (shell->event_history,
                                                  GimpCoords, i - 1),
                                   g_array_index (shell->event_history,
                                                  GimpCoords, i),
                                   g_array_index (shell->event_queue,
                                                  GimpCoords, 0),
                                   *coords,
                                   EVENT_FILL_PRECISION / 2,
                                   &ret_coords,
                                   NULL);

  /* Push the last actual event in history */
  gimp_display_shell_push_event_history (shell,
                                         &g_array_index (shell->event_queue,
                                                         GimpCoords, 0));

369
  g_array_set_size (shell->event_queue, 0);
370 371 372 373 374

  g_array_append_vals (shell->event_queue,
                       &g_array_index (ret_coords, GimpCoords, 0),
                       ret_coords->len);

375
  g_array_free (ret_coords, TRUE);
376
}