blob: 2bb8c0ab475fe50c15173033e638bbd21b6d7b05 [file] [log] [blame]
Sam Spilsbury619859c2013-09-13 10:01:21 +08001/*
2 * Copyright © 2012 Intel Corporation
3 *
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.
13 *
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.
21 */
22#include <assert.h>
23#include <float.h>
24#include <math.h>
25
Sam Spilsbury619859c2013-09-13 10:01:21 +080026#include "vertex-clipping.h"
27
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010028float
29float_difference(float a, float b)
Sam Spilsbury619859c2013-09-13 10:01:21 +080030{
31 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010032 static const float max_diff = 4.0f * FLT_MIN;
33 static const float max_rel_diff = 4.0e-5;
34 float diff = a - b;
35 float adiff = fabsf(diff);
Sam Spilsbury619859c2013-09-13 10:01:21 +080036
37 if (adiff <= max_diff)
38 return 0.0f;
39
40 a = fabsf(a);
41 b = fabsf(b);
42 if (adiff <= (a > b ? a : b) * max_rel_diff)
43 return 0.0f;
44
45 return diff;
46}
47
48/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
49 * Compute the y coordinate of the intersection.
50 */
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010051static float
52clip_intersect_y(float p1x, float p1y, float p2x, float p2y,
53 float x_arg)
Sam Spilsbury619859c2013-09-13 10:01:21 +080054{
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010055 float a;
56 float diff = float_difference(p1x, p2x);
Sam Spilsbury619859c2013-09-13 10:01:21 +080057
58 /* Practically vertical line segment, yet the end points have already
59 * been determined to be on different sides of the line. Therefore
60 * the line segment is part of the line and intersects everywhere.
61 * Return the end point, so we use the whole line segment.
62 */
63 if (diff == 0.0f)
64 return p2y;
65
66 a = (x_arg - p2x) / diff;
67 return p2y + (p1y - p2y) * a;
68}
69
70/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
71 * Compute the x coordinate of the intersection.
72 */
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010073static float
74clip_intersect_x(float p1x, float p1y, float p2x, float p2y,
75 float y_arg)
Sam Spilsbury619859c2013-09-13 10:01:21 +080076{
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010077 float a;
78 float diff = float_difference(p1y, p2y);
Sam Spilsbury619859c2013-09-13 10:01:21 +080079
80 /* Practically horizontal line segment, yet the end points have already
81 * been determined to be on different sides of the line. Therefore
82 * the line segment is part of the line and intersects everywhere.
83 * Return the end point, so we use the whole line segment.
84 */
85 if (diff == 0.0f)
86 return p2x;
87
88 a = (y_arg - p2y) / diff;
89 return p2x + (p1x - p2x) * a;
90}
91
92enum path_transition {
93 PATH_TRANSITION_OUT_TO_OUT = 0,
94 PATH_TRANSITION_OUT_TO_IN = 1,
95 PATH_TRANSITION_IN_TO_OUT = 2,
96 PATH_TRANSITION_IN_TO_IN = 3,
97};
98
99static void
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100100clip_append_vertex(struct clip_context *ctx, float x, float y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800101{
102 *ctx->vertices.x++ = x;
103 *ctx->vertices.y++ = y;
104}
105
106static enum path_transition
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100107path_transition_left_edge(struct clip_context *ctx, float x, float y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800108{
109 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
110}
111
112static enum path_transition
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100113path_transition_right_edge(struct clip_context *ctx, float x, float y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800114{
115 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
116}
117
118static enum path_transition
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100119path_transition_top_edge(struct clip_context *ctx, float x, float y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800120{
121 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
122}
123
124static enum path_transition
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100125path_transition_bottom_edge(struct clip_context *ctx, float x, float y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800126{
127 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
128}
129
130static void
131clip_polygon_leftright(struct clip_context *ctx,
132 enum path_transition transition,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100133 float x, float y, float clip_x)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800134{
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100135 float yi;
Sam Spilsbury619859c2013-09-13 10:01:21 +0800136
137 switch (transition) {
138 case PATH_TRANSITION_IN_TO_IN:
139 clip_append_vertex(ctx, x, y);
140 break;
141 case PATH_TRANSITION_IN_TO_OUT:
142 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
143 clip_append_vertex(ctx, clip_x, yi);
144 break;
145 case PATH_TRANSITION_OUT_TO_IN:
146 yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
147 clip_append_vertex(ctx, clip_x, yi);
148 clip_append_vertex(ctx, x, y);
149 break;
150 case PATH_TRANSITION_OUT_TO_OUT:
151 /* nothing */
152 break;
153 default:
154 assert(0 && "bad enum path_transition");
155 }
156
157 ctx->prev.x = x;
158 ctx->prev.y = y;
159}
160
161static void
162clip_polygon_topbottom(struct clip_context *ctx,
163 enum path_transition transition,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100164 float x, float y, float clip_y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800165{
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100166 float xi;
Sam Spilsbury619859c2013-09-13 10:01:21 +0800167
168 switch (transition) {
169 case PATH_TRANSITION_IN_TO_IN:
170 clip_append_vertex(ctx, x, y);
171 break;
172 case PATH_TRANSITION_IN_TO_OUT:
173 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
174 clip_append_vertex(ctx, xi, clip_y);
175 break;
176 case PATH_TRANSITION_OUT_TO_IN:
177 xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
178 clip_append_vertex(ctx, xi, clip_y);
179 clip_append_vertex(ctx, x, y);
180 break;
181 case PATH_TRANSITION_OUT_TO_OUT:
182 /* nothing */
183 break;
184 default:
185 assert(0 && "bad enum path_transition");
186 }
187
188 ctx->prev.x = x;
189 ctx->prev.y = y;
190}
191
192static void
193clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100194 float *dst_x, float *dst_y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800195{
196 ctx->prev.x = src->x[src->n - 1];
197 ctx->prev.y = src->y[src->n - 1];
198 ctx->vertices.x = dst_x;
199 ctx->vertices.y = dst_y;
200}
201
202static int
203clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100204 float *dst_x, float *dst_y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800205{
206 enum path_transition trans;
207 int i;
208
Ondřej Majerecha64a4752014-08-19 15:59:45 +0200209 if (src->n < 2)
210 return 0;
211
Sam Spilsbury619859c2013-09-13 10:01:21 +0800212 clip_context_prepare(ctx, src, dst_x, dst_y);
213 for (i = 0; i < src->n; i++) {
214 trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
215 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
216 ctx->clip.x1);
217 }
218 return ctx->vertices.x - dst_x;
219}
220
221static int
222clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100223 float *dst_x, float *dst_y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800224{
225 enum path_transition trans;
226 int i;
227
Ondřej Majerecha64a4752014-08-19 15:59:45 +0200228 if (src->n < 2)
229 return 0;
230
Sam Spilsbury619859c2013-09-13 10:01:21 +0800231 clip_context_prepare(ctx, src, dst_x, dst_y);
232 for (i = 0; i < src->n; i++) {
233 trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
234 clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
235 ctx->clip.x2);
236 }
237 return ctx->vertices.x - dst_x;
238}
239
240static int
241clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100242 float *dst_x, float *dst_y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800243{
244 enum path_transition trans;
245 int i;
246
Ondřej Majerecha64a4752014-08-19 15:59:45 +0200247 if (src->n < 2)
248 return 0;
249
Sam Spilsbury619859c2013-09-13 10:01:21 +0800250 clip_context_prepare(ctx, src, dst_x, dst_y);
251 for (i = 0; i < src->n; i++) {
252 trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
253 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
254 ctx->clip.y1);
255 }
256 return ctx->vertices.x - dst_x;
257}
258
259static int
260clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100261 float *dst_x, float *dst_y)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800262{
263 enum path_transition trans;
264 int i;
265
Ondřej Majerecha64a4752014-08-19 15:59:45 +0200266 if (src->n < 2)
267 return 0;
268
Sam Spilsbury619859c2013-09-13 10:01:21 +0800269 clip_context_prepare(ctx, src, dst_x, dst_y);
270 for (i = 0; i < src->n; i++) {
271 trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
272 clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
273 ctx->clip.y2);
274 }
275 return ctx->vertices.x - dst_x;
276}
277
278#define max(a, b) (((a) > (b)) ? (a) : (b))
279#define min(a, b) (((a) > (b)) ? (b) : (a))
280#define clip(x, a, b) min(max(x, a), b)
281
282int
283clip_simple(struct clip_context *ctx,
284 struct polygon8 *surf,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100285 float *ex,
286 float *ey)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800287{
288 int i;
289 for (i = 0; i < surf->n; i++) {
290 ex[i] = clip(surf->x[i], ctx->clip.x1, ctx->clip.x2);
291 ey[i] = clip(surf->y[i], ctx->clip.y1, ctx->clip.y2);
292 }
293 return surf->n;
294}
295
296int
297clip_transformed(struct clip_context *ctx,
298 struct polygon8 *surf,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100299 float *ex,
300 float *ey)
Sam Spilsbury619859c2013-09-13 10:01:21 +0800301{
302 struct polygon8 polygon;
303 int i, n;
304
305 polygon.n = clip_polygon_left(ctx, surf, polygon.x, polygon.y);
306 surf->n = clip_polygon_right(ctx, &polygon, surf->x, surf->y);
307 polygon.n = clip_polygon_top(ctx, surf, polygon.x, polygon.y);
308 surf->n = clip_polygon_bottom(ctx, &polygon, surf->x, surf->y);
309
310 /* Get rid of duplicate vertices */
311 ex[0] = surf->x[0];
312 ey[0] = surf->y[0];
313 n = 1;
314 for (i = 1; i < surf->n; i++) {
315 if (float_difference(ex[n - 1], surf->x[i]) == 0.0f &&
316 float_difference(ey[n - 1], surf->y[i]) == 0.0f)
317 continue;
318 ex[n] = surf->x[i];
319 ey[n] = surf->y[i];
320 n++;
321 }
322 if (float_difference(ex[n - 1], surf->x[0]) == 0.0f &&
323 float_difference(ey[n - 1], surf->y[0]) == 0.0f)
324 n--;
325
326 return n;
327}