1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * gimpcoords.c
5 * Copyright (C) 2002 Simon Budig <simon@gimp.org>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <glib-object.h>
24
25 #include "libgimpmath/gimpmath.h"
26
27 #include "core-types.h"
28
29 #include "gimpcoords.h"
30
31
32 #define INPUT_RESOLUTION 256
33
34
35 /* amul * a + bmul * b = ret_val */
36
37 void
gimp_coords_mix(const gdouble amul,const GimpCoords * a,const gdouble bmul,const GimpCoords * b,GimpCoords * ret_val)38 gimp_coords_mix (const gdouble amul,
39 const GimpCoords *a,
40 const gdouble bmul,
41 const GimpCoords *b,
42 GimpCoords *ret_val)
43 {
44 if (b)
45 {
46 ret_val->x = amul * a->x + bmul * b->x;
47 ret_val->y = amul * a->y + bmul * b->y;
48 ret_val->pressure = amul * a->pressure + bmul * b->pressure;
49 ret_val->xtilt = amul * a->xtilt + bmul * b->xtilt;
50 ret_val->ytilt = amul * a->ytilt + bmul * b->ytilt;
51 ret_val->wheel = amul * a->wheel + bmul * b->wheel;
52 ret_val->velocity = amul * a->velocity + bmul * b->velocity;
53 ret_val->direction = amul * a->direction + bmul * b->direction;
54 ret_val->extended = b->extended || a->extended;
55 }
56 else
57 {
58 ret_val->x = amul * a->x;
59 ret_val->y = amul * a->y;
60 ret_val->pressure = amul * a->pressure;
61 ret_val->xtilt = amul * a->xtilt;
62 ret_val->ytilt = amul * a->ytilt;
63 ret_val->wheel = amul * a->wheel;
64 ret_val->velocity = amul * a->velocity;
65 ret_val->direction = amul * a->direction;
66 ret_val->extended = a->extended;
67 }
68 }
69
70
71 /* (a+b)/2 = ret_average */
72
73 void
gimp_coords_average(const GimpCoords * a,const GimpCoords * b,GimpCoords * ret_average)74 gimp_coords_average (const GimpCoords *a,
75 const GimpCoords *b,
76 GimpCoords *ret_average)
77 {
78 gimp_coords_mix (0.5, a, 0.5, b, ret_average);
79 }
80
81
82 /* a + b = ret_add */
83
84 void
gimp_coords_add(const GimpCoords * a,const GimpCoords * b,GimpCoords * ret_add)85 gimp_coords_add (const GimpCoords *a,
86 const GimpCoords *b,
87 GimpCoords *ret_add)
88 {
89 gimp_coords_mix (1.0, a, 1.0, b, ret_add);
90 }
91
92
93 /* a - b = ret_difference */
94
95 void
gimp_coords_difference(const GimpCoords * a,const GimpCoords * b,GimpCoords * ret_difference)96 gimp_coords_difference (const GimpCoords *a,
97 const GimpCoords *b,
98 GimpCoords *ret_difference)
99 {
100 gimp_coords_mix (1.0, a, -1.0, b, ret_difference);
101 }
102
103
104 /* a * f = ret_product */
105
106 void
gimp_coords_scale(const gdouble f,const GimpCoords * a,GimpCoords * ret_product)107 gimp_coords_scale (const gdouble f,
108 const GimpCoords *a,
109 GimpCoords *ret_product)
110 {
111 gimp_coords_mix (f, a, 0.0, NULL, ret_product);
112 }
113
114
115 /* local helper for measuring the scalarproduct of two gimpcoords. */
116
117 gdouble
gimp_coords_scalarprod(const GimpCoords * a,const GimpCoords * b)118 gimp_coords_scalarprod (const GimpCoords *a,
119 const GimpCoords *b)
120 {
121 return (a->x * b->x +
122 a->y * b->y +
123 a->pressure * b->pressure +
124 a->xtilt * b->xtilt +
125 a->ytilt * b->ytilt +
126 a->wheel * b->wheel +
127 a->velocity * b->velocity +
128 a->direction * b->direction);
129 }
130
131
132 /*
133 * The "length" of the gimpcoord.
134 * Applies a metric that increases the weight on the
135 * pressure/xtilt/ytilt/wheel to ensure proper interpolation
136 */
137
138 gdouble
gimp_coords_length_squared(const GimpCoords * a)139 gimp_coords_length_squared (const GimpCoords *a)
140 {
141 GimpCoords upscaled_a;
142
143 upscaled_a.x = a->x;
144 upscaled_a.y = a->y;
145 upscaled_a.pressure = a->pressure * INPUT_RESOLUTION;
146 upscaled_a.xtilt = a->xtilt * INPUT_RESOLUTION;
147 upscaled_a.ytilt = a->ytilt * INPUT_RESOLUTION;
148 upscaled_a.wheel = a->wheel * INPUT_RESOLUTION;
149 upscaled_a.velocity = a->velocity * INPUT_RESOLUTION;
150 upscaled_a.direction = a->direction * INPUT_RESOLUTION;
151
152 return gimp_coords_scalarprod (&upscaled_a, &upscaled_a);
153 }
154
155
156 gdouble
gimp_coords_length(const GimpCoords * a)157 gimp_coords_length (const GimpCoords *a)
158 {
159 return sqrt (gimp_coords_length_squared (a));
160 }
161
162 /*
163 * Distance via manhattan metric, an upper bound for the eucledian metric.
164 * used for e.g. bezier approximation
165 */
166
167 gdouble
gimp_coords_manhattan_dist(const GimpCoords * a,const GimpCoords * b)168 gimp_coords_manhattan_dist (const GimpCoords *a,
169 const GimpCoords *b)
170 {
171 gdouble dist = 0;
172
173 dist += ABS (a->pressure - b->pressure);
174 dist += ABS (a->xtilt - b->xtilt);
175 dist += ABS (a->ytilt - b->ytilt);
176 dist += ABS (a->wheel - b->wheel);
177 dist += ABS (a->velocity - b->velocity);
178 dist += ABS (a->direction - b->direction);
179
180 dist *= INPUT_RESOLUTION;
181
182 dist += ABS (a->x - b->x);
183 dist += ABS (a->y - b->y);
184
185 return dist;
186 }
187
188 gboolean
gimp_coords_equal(const GimpCoords * a,const GimpCoords * b)189 gimp_coords_equal (const GimpCoords *a,
190 const GimpCoords *b)
191 {
192 return (a->x == b->x &&
193 a->y == b->y &&
194 a->pressure == b->pressure &&
195 a->xtilt == b->xtilt &&
196 a->ytilt == b->ytilt &&
197 a->wheel == b->wheel &&
198 a->velocity == b->velocity &&
199 a->direction == b->direction);
200
201 /* Extended attribute was omitted from this comparison deliberately,
202 * it describes the events origin, not its value
203 */
204 }
205
206 /* helper for calculating direction of two gimpcoords. */
207
208 gdouble
gimp_coords_direction(const GimpCoords * a,const GimpCoords * b)209 gimp_coords_direction (const GimpCoords *a,
210 const GimpCoords *b)
211 {
212 gdouble direction;
213 gdouble delta_x, delta_y;
214
215 delta_x = a->x - b->x;
216 delta_y = a->y - b->y;
217
218 if ((delta_x == 0) && (delta_y == 0))
219 {
220 direction = a->direction;
221 }
222 else if (delta_x == 0)
223 {
224 if (delta_y > 0)
225 direction = 0.25;
226 else
227 direction = 0.75;
228 }
229 else if (delta_y == 0)
230 {
231 if (delta_x < 0)
232 direction = 0.0;
233 else
234 direction = 0.5;
235 }
236 else
237 {
238 direction = atan ((- 1.0 * delta_y) / delta_x) / (2 * G_PI);
239
240 if (delta_x > 0.0)
241 direction = direction + 0.5;
242
243 if (direction < 0.0)
244 direction = direction + 1.0;
245 }
246
247 return direction;
248 }
249