summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOndřej Majerech <oxyd.oxyd@gmail.com>2014-08-19 15:59:44 +0200
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>2014-08-20 11:43:26 +0300
commit06e089275d30a7803f6873cd55d1041356eb07ab (patch)
treec4d3363efe1e3633c9128da3865f54037008d0a8
parentc9c74156cde72ba5fe772eb17b8db0edd8b3e2f2 (diff)
downloadweston-06e089275d30a7803f6873cd55d1041356eb07ab.tar.gz
Remove duplicated code from cliptest.c
Signed-off-by: Ondřej Majerech <oxyd.oxyd@gmail.com> Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
-rw-r--r--Makefile.am2
-rw-r--r--clients/cliptest.c293
2 files changed, 4 insertions, 291 deletions
diff --git a/Makefile.am b/Makefile.am
index 191dcc9e..8a27d44b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -476,7 +476,7 @@ weston_image_SOURCES = clients/image.c
weston_image_LDADD = libtoytoolkit.la
weston_image_CFLAGS = $(AM_CFLAGS) $(CLIENT_CFLAGS)
-weston_cliptest_SOURCES = clients/cliptest.c
+weston_cliptest_SOURCES = clients/cliptest.c src/vertex-clipping.c
weston_cliptest_CFLAGS = $(AM_CFLAGS) $(CLIENT_CFLAGS)
weston_cliptest_LDADD = libtoytoolkit.la
diff --git a/clients/cliptest.c b/clients/cliptest.c
index 907c5d43..af7bff17 100644
--- a/clients/cliptest.c
+++ b/clients/cliptest.c
@@ -49,6 +49,7 @@
#include <linux/input.h>
#include <wayland-client.h>
+#include "src/vertex-clipping.h"
#include "window.h"
typedef float GLfloat;
@@ -81,269 +82,6 @@ weston_surface_to_global_float(struct weston_surface *surface,
*y = -g->s * sx + g->c * sy;
}
-/* ---------------------- copied begins -----------------------*/
-
-struct polygon8 {
- GLfloat x[8];
- GLfloat y[8];
- int n;
-};
-
-struct clip_context {
- struct {
- GLfloat x;
- GLfloat y;
- } prev;
-
- struct {
- GLfloat x1, y1;
- GLfloat x2, y2;
- } clip;
-
- struct {
- GLfloat *x;
- GLfloat *y;
- } vertices;
-};
-
-static GLfloat
-float_difference(GLfloat a, GLfloat b)
-{
- /* http://www.altdevblogaday.com/2012/02/22/comparing-floating-point-numbers-2012-edition/ */
- static const GLfloat max_diff = 4.0f * FLT_MIN;
- static const GLfloat max_rel_diff = 4.0e-5;
- GLfloat diff = a - b;
- GLfloat adiff = fabsf(diff);
-
- if (adiff <= max_diff)
- return 0.0f;
-
- a = fabsf(a);
- b = fabsf(b);
- if (adiff <= (a > b ? a : b) * max_rel_diff)
- return 0.0f;
-
- return diff;
-}
-
-/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line x = x_arg.
- * Compute the y coordinate of the intersection.
- */
-static GLfloat
-clip_intersect_y(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
- GLfloat x_arg)
-{
- GLfloat a;
- GLfloat diff = float_difference(p1x, p2x);
-
- /* Practically vertical line segment, yet the end points have already
- * been determined to be on different sides of the line. Therefore
- * the line segment is part of the line and intersects everywhere.
- * Return the end point, so we use the whole line segment.
- */
- if (diff == 0.0f)
- return p2y;
-
- a = (x_arg - p2x) / diff;
- return p2y + (p1y - p2y) * a;
-}
-
-/* A line segment (p1x, p1y)-(p2x, p2y) intersects the line y = y_arg.
- * Compute the x coordinate of the intersection.
- */
-static GLfloat
-clip_intersect_x(GLfloat p1x, GLfloat p1y, GLfloat p2x, GLfloat p2y,
- GLfloat y_arg)
-{
- GLfloat a;
- GLfloat diff = float_difference(p1y, p2y);
-
- /* Practically horizontal line segment, yet the end points have already
- * been determined to be on different sides of the line. Therefore
- * the line segment is part of the line and intersects everywhere.
- * Return the end point, so we use the whole line segment.
- */
- if (diff == 0.0f)
- return p2x;
-
- a = (y_arg - p2y) / diff;
- return p2x + (p1x - p2x) * a;
-}
-
-enum path_transition {
- PATH_TRANSITION_OUT_TO_OUT = 0,
- PATH_TRANSITION_OUT_TO_IN = 1,
- PATH_TRANSITION_IN_TO_OUT = 2,
- PATH_TRANSITION_IN_TO_IN = 3,
-};
-
-static void
-clip_append_vertex(struct clip_context *ctx, GLfloat x, GLfloat y)
-{
- *ctx->vertices.x++ = x;
- *ctx->vertices.y++ = y;
-}
-
-static enum path_transition
-path_transition_left_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
-{
- return ((ctx->prev.x >= ctx->clip.x1) << 1) | (x >= ctx->clip.x1);
-}
-
-static enum path_transition
-path_transition_right_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
-{
- return ((ctx->prev.x < ctx->clip.x2) << 1) | (x < ctx->clip.x2);
-}
-
-static enum path_transition
-path_transition_top_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
-{
- return ((ctx->prev.y >= ctx->clip.y1) << 1) | (y >= ctx->clip.y1);
-}
-
-static enum path_transition
-path_transition_bottom_edge(struct clip_context *ctx, GLfloat x, GLfloat y)
-{
- return ((ctx->prev.y < ctx->clip.y2) << 1) | (y < ctx->clip.y2);
-}
-
-static void
-clip_polygon_leftright(struct clip_context *ctx,
- enum path_transition transition,
- GLfloat x, GLfloat y, GLfloat clip_x)
-{
- GLfloat yi;
-
- switch (transition) {
- case PATH_TRANSITION_IN_TO_IN:
- clip_append_vertex(ctx, x, y);
- break;
- case PATH_TRANSITION_IN_TO_OUT:
- yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
- clip_append_vertex(ctx, clip_x, yi);
- break;
- case PATH_TRANSITION_OUT_TO_IN:
- yi = clip_intersect_y(ctx->prev.x, ctx->prev.y, x, y, clip_x);
- clip_append_vertex(ctx, clip_x, yi);
- clip_append_vertex(ctx, x, y);
- break;
- case PATH_TRANSITION_OUT_TO_OUT:
- /* nothing */
- break;
- default:
- assert(0 && "bad enum path_transition");
- }
-
- ctx->prev.x = x;
- ctx->prev.y = y;
-}
-
-static void
-clip_polygon_topbottom(struct clip_context *ctx,
- enum path_transition transition,
- GLfloat x, GLfloat y, GLfloat clip_y)
-{
- GLfloat xi;
-
- switch (transition) {
- case PATH_TRANSITION_IN_TO_IN:
- clip_append_vertex(ctx, x, y);
- break;
- case PATH_TRANSITION_IN_TO_OUT:
- xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
- clip_append_vertex(ctx, xi, clip_y);
- break;
- case PATH_TRANSITION_OUT_TO_IN:
- xi = clip_intersect_x(ctx->prev.x, ctx->prev.y, x, y, clip_y);
- clip_append_vertex(ctx, xi, clip_y);
- clip_append_vertex(ctx, x, y);
- break;
- case PATH_TRANSITION_OUT_TO_OUT:
- /* nothing */
- break;
- default:
- assert(0 && "bad enum path_transition");
- }
-
- ctx->prev.x = x;
- ctx->prev.y = y;
-}
-
-static void
-clip_context_prepare(struct clip_context *ctx, const struct polygon8 *src,
- GLfloat *dst_x, GLfloat *dst_y)
-{
- ctx->prev.x = src->x[src->n - 1];
- ctx->prev.y = src->y[src->n - 1];
- ctx->vertices.x = dst_x;
- ctx->vertices.y = dst_y;
-}
-
-static int
-clip_polygon_left(struct clip_context *ctx, const struct polygon8 *src,
- GLfloat *dst_x, GLfloat *dst_y)
-{
- enum path_transition trans;
- int i;
-
- clip_context_prepare(ctx, src, dst_x, dst_y);
- for (i = 0; i < src->n; i++) {
- trans = path_transition_left_edge(ctx, src->x[i], src->y[i]);
- clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
- ctx->clip.x1);
- }
- return ctx->vertices.x - dst_x;
-}
-
-static int
-clip_polygon_right(struct clip_context *ctx, const struct polygon8 *src,
- GLfloat *dst_x, GLfloat *dst_y)
-{
- enum path_transition trans;
- int i;
-
- clip_context_prepare(ctx, src, dst_x, dst_y);
- for (i = 0; i < src->n; i++) {
- trans = path_transition_right_edge(ctx, src->x[i], src->y[i]);
- clip_polygon_leftright(ctx, trans, src->x[i], src->y[i],
- ctx->clip.x2);
- }
- return ctx->vertices.x - dst_x;
-}
-
-static int
-clip_polygon_top(struct clip_context *ctx, const struct polygon8 *src,
- GLfloat *dst_x, GLfloat *dst_y)
-{
- enum path_transition trans;
- int i;
-
- clip_context_prepare(ctx, src, dst_x, dst_y);
- for (i = 0; i < src->n; i++) {
- trans = path_transition_top_edge(ctx, src->x[i], src->y[i]);
- clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
- ctx->clip.y1);
- }
- return ctx->vertices.x - dst_x;
-}
-
-static int
-clip_polygon_bottom(struct clip_context *ctx, const struct polygon8 *src,
- GLfloat *dst_x, GLfloat *dst_y)
-{
- enum path_transition trans;
- int i;
-
- clip_context_prepare(ctx, src, dst_x, dst_y);
- for (i = 0; i < src->n; i++) {
- trans = path_transition_bottom_edge(ctx, src->x[i], src->y[i]);
- clip_polygon_topbottom(ctx, trans, src->x[i], src->y[i],
- ctx->clip.y2);
- }
- return ctx->vertices.x - dst_x;
-}
-
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) > (b)) ? (b) : (a))
#define clip(x, a, b) min(max(x, a), b)
@@ -361,9 +99,8 @@ static int
calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
{
- struct polygon8 polygon;
struct clip_context ctx;
- int i, n;
+ int i;
GLfloat min_x, max_x, min_y, max_y;
struct polygon8 surf = {
{ surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 },
@@ -417,31 +154,7 @@ calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
* http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm
* but without looking at any of that code.
*/
- polygon.n = clip_polygon_left(&ctx, &surf, polygon.x, polygon.y);
- surf.n = clip_polygon_right(&ctx, &polygon, surf.x, surf.y);
- polygon.n = clip_polygon_top(&ctx, &surf, polygon.x, polygon.y);
- surf.n = clip_polygon_bottom(&ctx, &polygon, surf.x, surf.y);
-
- /* Get rid of duplicate vertices */
- ex[0] = surf.x[0];
- ey[0] = surf.y[0];
- n = 1;
- for (i = 1; i < surf.n; i++) {
- if (float_difference(ex[n - 1], surf.x[i]) == 0.0f &&
- float_difference(ey[n - 1], surf.y[i]) == 0.0f)
- continue;
- ex[n] = surf.x[i];
- ey[n] = surf.y[i];
- n++;
- }
- if (float_difference(ex[n - 1], surf.x[0]) == 0.0f &&
- float_difference(ey[n - 1], surf.y[0]) == 0.0f)
- n--;
-
- if (n < 3)
- return 0;
-
- return n;
+ return clip_transformed(&ctx, &surf, ex, ey);
}