2 * Copyright © 2012 Intel Corporation
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.
26 #include "vertex-clipping.h"
29 float_difference(float a, float b)
31 /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
32 static const float max_diff = 4.0f * FLT_MIN;
33 static const float max_rel_diff = 4.0e-5;
35 float adiff = fabsf(diff);
37 if (adiff <= max_diff)
42 if (adiff <= (a > b ? a : b) * max_rel_diff)
48 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
49 * Compute the y coordinate of the intersection.
52 clip_intersect_y(float p1x, float p1y, float p2x, float p2y,
56 float diff = float_difference(p1x, p2x);
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.
66 a = (x_arg - p2x) / diff;
67 return p2y + (p1y - p2y) * a;
70 /* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
71 * Compute the x coordinate of the intersection.
74 clip_intersect_x(float p1x, float p1y, float p2x, float p2y,
78 float diff = float_difference(p1y, p2y);
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.
88 a = (y_arg - p2y) / diff;
89 return p2x + (p1x - p2x) * a;
92 enum 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,
100 clip_append_vertex(struct clip_context *ctx, float x, float y)
102 *ctx->vertices.x++ = x;
103 *ctx->vertices.y++ = y;
106 static enum path_transition
107 path_transition_left_edge(struct clip_context *ctx, float x, float y)
109 return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
112 static enum path_transition
113 path_transition_right_edge(struct clip_context *ctx, float x, float y)
115 return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
118 static enum path_transition
119 path_transition_top_edge(struct clip_context *ctx, float x, float y)
121 return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
124 static enum path_transition
125 path_transition_bottom_edge(struct clip_context *ctx, float x, float y)
127 return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
131 clip_polygon_leftright(struct clip_context *ctx,
132 enum path_transition transition,
133 float x, float y, float clip_x)
137 switch (transition) {
138 case PATH_TRANSITION_IN_TO_IN:
139 clip_append_vertex(ctx, x, y);
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);
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);
150 case PATH_TRANSITION_OUT_TO_OUT:
154 assert(0 && "bad enum path_transition");
162 clip_polygon_topbottom(struct clip_context *ctx,
163 enum path_transition transition,
164 float x, float y, float clip_y)
168 switch (transition) {
169 case PATH_TRANSITION_IN_TO_IN:
170 clip_append_vertex(ctx, x, y);
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);
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);
181 case PATH_TRANSITION_OUT_TO_OUT:
185 assert(0 && "bad enum path_transition");
193 clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
194 float *dst_x, float *dst_y)
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;
203 clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
204 float *dst_x, float *dst_y)
206 enum path_transition trans;
212 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],
218 return ctx->vertices.x - dst_x;
222 clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
223 float *dst_x, float *dst_y)
225 enum path_transition trans;
231 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],
237 return ctx->vertices.x - dst_x;
241 clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
242 float *dst_x, float *dst_y)
244 enum path_transition trans;
250 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],
256 return ctx->vertices.x - dst_x;
260 clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
261 float *dst_x, float *dst_y)
263 enum path_transition trans;
269 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],
275 return ctx->vertices.x - dst_x;
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)
283 clip_simple(struct clip_context *ctx,
284 struct polygon8 *surf,
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);
297 clip_transformed(struct clip_context *ctx,
298 struct polygon8 *surf,
302 struct polygon8 polygon;
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);
310 /* Get rid of duplicate vertices */
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)
322 if (float_difference(ex[n - 1], surf->x[0]) == 0.0f &&
323 float_difference(ey[n - 1], surf->y[0]) == 0.0f)