[PATCH 2/3] Split vertex clipping code out into vertex-clipping.c in shared/

2013-09-10 Thread Sam Spilsbury
---
 shared/Makefile.am   |   4 +-
 shared/vertex-clipping.c | 317 +++
 shared/vertex-clipping.h |  65 ++
 src/gl-renderer.c| 292 +--
 4 files changed, 389 insertions(+), 289 deletions(-)
 create mode 100644 shared/vertex-clipping.c
 create mode 100644 shared/vertex-clipping.h

diff --git a/shared/Makefile.am b/shared/Makefile.am
index 2fcff7b..f1abc61 100644
--- a/shared/Makefile.am
+++ b/shared/Makefile.am
@@ -7,7 +7,9 @@ libshared_la_SOURCES =  \
option-parser.c \
config-parser.h \
os-compatibility.c  \
-   os-compatibility.h
+   os-compatibility.h  \
+   vertex-clipping.c   \
+   vertex-clipping.h
 
 libshared_cairo_la_CFLAGS =\
$(GCC_CFLAGS)   \
diff --git a/shared/vertex-clipping.c b/shared/vertex-clipping.c
new file mode 100644
index 000..603ce6f
--- /dev/null
+++ b/shared/vertex-clipping.c
@@ -0,0 +1,317 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include 
+#include 
+#include 
+
+#include 
+
+#include "vertex-clipping.h"
+
+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 

[PATCH 2/3] Split vertex clipping code out into vertex-clipping.c in shared/

2013-09-10 Thread Sam Spilsbury
---
 shared/vertex-clipping.c | 317 +++
 shared/vertex-clipping.h |  65 ++
 src/Makefile.am  |   2 +
 src/gl-renderer.c| 292 +--
 4 files changed, 388 insertions(+), 288 deletions(-)
 create mode 100644 shared/vertex-clipping.c
 create mode 100644 shared/vertex-clipping.h

diff --git a/shared/vertex-clipping.c b/shared/vertex-clipping.c
new file mode 100644
index 000..603ce6f
--- /dev/null
+++ b/shared/vertex-clipping.c
@@ -0,0 +1,317 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+#include 
+#include 
+#include 
+
+#include 
+
+#include "vertex-clipping.h"
+
+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);