2 * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
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.
28 #include "weston-test-runner.h"
30 #include "../src/vertex-clipping.h"
32 #define BOUNDING_BOX_TOP_Y 100.0f
33 #define BOUNDING_BOX_LEFT_X 50.0f
34 #define BOUNDING_BOX_RIGHT_X 100.0f
35 #define BOUNDING_BOX_BOTTOM_Y 50.0f
37 #define INSIDE_X1 (BOUNDING_BOX_LEFT_X + 1.0f)
38 #define INSIDE_X2 (BOUNDING_BOX_RIGHT_X - 1.0f)
39 #define INSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y + 1.0f)
40 #define INSIDE_Y2 (BOUNDING_BOX_TOP_Y - 1.0f)
42 #define OUTSIDE_X1 (BOUNDING_BOX_LEFT_X - 1.0f)
43 #define OUTSIDE_X2 (BOUNDING_BOX_RIGHT_X + 1.0f)
44 #define OUTSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y - 1.0f)
45 #define OUTSIDE_Y2 (BOUNDING_BOX_TOP_Y + 1.0f)
48 populate_clip_context (struct clip_context *ctx)
50 ctx->clip.x1 = BOUNDING_BOX_LEFT_X;
51 ctx->clip.y1 = BOUNDING_BOX_BOTTOM_Y;
52 ctx->clip.x2 = BOUNDING_BOX_RIGHT_X;
53 ctx->clip.y2 = BOUNDING_BOX_TOP_Y;
57 clip_polygon (struct clip_context *ctx,
58 struct polygon8 *polygon,
62 populate_clip_context(ctx);
63 return clip_transformed(ctx, polygon, vertices_x, vertices_y);
66 struct vertex_clip_test_data
68 struct polygon8 surface;
69 struct polygon8 expected;
72 const struct vertex_clip_test_data test_data[] =
77 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
78 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
82 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
83 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
90 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
91 { INSIDE_Y1, INSIDE_Y1, OUTSIDE_Y2, OUTSIDE_Y2 },
95 { INSIDE_X1, INSIDE_X1, INSIDE_X2, INSIDE_X2 },
96 { BOUNDING_BOX_TOP_Y, INSIDE_Y1, INSIDE_Y1, BOUNDING_BOX_TOP_Y },
103 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
104 { OUTSIDE_Y1, OUTSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
108 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
109 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y, INSIDE_Y2, INSIDE_Y2 },
116 { OUTSIDE_X1, INSIDE_X2, INSIDE_X2, OUTSIDE_X1 },
117 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
121 { BOUNDING_BOX_LEFT_X, INSIDE_X2, INSIDE_X2, BOUNDING_BOX_LEFT_X },
122 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
129 { INSIDE_X1, OUTSIDE_X2, OUTSIDE_X2, INSIDE_X1 },
130 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
134 { INSIDE_X1, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, INSIDE_X1 },
135 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
139 /* Diamond extending from bounding box edges, clip to bounding box */
142 { BOUNDING_BOX_LEFT_X - 25, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 25, BOUNDING_BOX_RIGHT_X - 25 },
143 { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 25, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 25 },
147 { BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X },
148 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_BOTTOM_Y },
152 /* Diamond inside of bounding box edges, clip t bounding box, 8 resulting vertices */
155 { BOUNDING_BOX_LEFT_X - 12.5, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 12.5, BOUNDING_BOX_RIGHT_X - 25 },
156 { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 12.5, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 12.5 },
160 { BOUNDING_BOX_LEFT_X + 12.5, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X + 12.5,
161 BOUNDING_BOX_RIGHT_X - 12.5, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X - 12.5 },
162 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_TOP_Y,
163 BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_BOTTOM_Y },
169 /* clip_polygon modifies the source operand and the test data must
170 * be const, so we need to deep copy it */
172 deep_copy_polygon8(const struct polygon8 *src, struct polygon8 *dst)
175 memcpy((void *) dst->x, src->x, sizeof (src->x));
176 memcpy((void *) dst->y, src->y, sizeof (src->y));
179 TEST_P(clip_polygon_n_vertices_emitted, test_data)
181 struct vertex_clip_test_data *tdata = data;
182 struct clip_context ctx;
183 struct polygon8 polygon;
184 GLfloat vertices_x[8];
185 GLfloat vertices_y[8];
186 deep_copy_polygon8(&tdata->surface, &polygon);
187 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
189 assert(emitted == tdata->expected.n);
192 TEST_P(clip_polygon_expected_vertices, test_data)
194 struct vertex_clip_test_data *tdata = data;
195 struct clip_context ctx;
196 struct polygon8 polygon;
197 GLfloat vertices_x[8];
198 GLfloat vertices_y[8];
199 deep_copy_polygon8(&tdata->surface, &polygon);
200 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
203 for (; i < emitted; ++i)
205 assert(vertices_x[i] == tdata->expected.x[i]);
206 assert(vertices_y[i] == tdata->expected.y[i]);
210 TEST(float_difference_different)
212 assert(float_difference(1.0f, 0.0f) == 1.0f);
215 TEST(float_difference_same)
217 assert(float_difference(1.0f, 1.0f) == 0.0f);