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.
31 #include "weston-test-runner.h"
33 #include "../src/vertex-clipping.h"
35 #define BOUNDING_BOX_TOP_Y 100.0f
36 #define BOUNDING_BOX_LEFT_X 50.0f
37 #define BOUNDING_BOX_RIGHT_X 100.0f
38 #define BOUNDING_BOX_BOTTOM_Y 50.0f
40 #define INSIDE_X1 (BOUNDING_BOX_LEFT_X + 1.0f)
41 #define INSIDE_X2 (BOUNDING_BOX_RIGHT_X - 1.0f)
42 #define INSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y + 1.0f)
43 #define INSIDE_Y2 (BOUNDING_BOX_TOP_Y - 1.0f)
45 #define OUTSIDE_X1 (BOUNDING_BOX_LEFT_X - 1.0f)
46 #define OUTSIDE_X2 (BOUNDING_BOX_RIGHT_X + 1.0f)
47 #define OUTSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y - 1.0f)
48 #define OUTSIDE_Y2 (BOUNDING_BOX_TOP_Y + 1.0f)
51 populate_clip_context (struct clip_context *ctx)
53 ctx->clip.x1 = BOUNDING_BOX_LEFT_X;
54 ctx->clip.y1 = BOUNDING_BOX_BOTTOM_Y;
55 ctx->clip.x2 = BOUNDING_BOX_RIGHT_X;
56 ctx->clip.y2 = BOUNDING_BOX_TOP_Y;
60 clip_polygon (struct clip_context *ctx,
61 struct polygon8 *polygon,
65 populate_clip_context(ctx);
66 return clip_transformed(ctx, polygon, vertices_x, vertices_y);
69 struct vertex_clip_test_data
71 struct polygon8 surface;
72 struct polygon8 expected;
75 const struct vertex_clip_test_data test_data[] =
80 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
81 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
85 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
86 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
93 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
94 { INSIDE_Y1, INSIDE_Y1, OUTSIDE_Y2, OUTSIDE_Y2 },
98 { INSIDE_X1, INSIDE_X1, INSIDE_X2, INSIDE_X2 },
99 { BOUNDING_BOX_TOP_Y, INSIDE_Y1, INSIDE_Y1, BOUNDING_BOX_TOP_Y },
106 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
107 { OUTSIDE_Y1, OUTSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
111 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
112 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y, INSIDE_Y2, INSIDE_Y2 },
119 { OUTSIDE_X1, INSIDE_X2, INSIDE_X2, OUTSIDE_X1 },
120 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
124 { BOUNDING_BOX_LEFT_X, INSIDE_X2, INSIDE_X2, BOUNDING_BOX_LEFT_X },
125 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
132 { INSIDE_X1, OUTSIDE_X2, OUTSIDE_X2, INSIDE_X1 },
133 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
137 { INSIDE_X1, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, INSIDE_X1 },
138 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
142 /* Diamond extending from bounding box edges, clip to bounding box */
145 { BOUNDING_BOX_LEFT_X - 25, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 25, BOUNDING_BOX_RIGHT_X - 25 },
146 { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 25, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 25 },
150 { BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X },
151 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_BOTTOM_Y },
155 /* Diamond inside of bounding box edges, clip t bounding box, 8 resulting vertices */
158 { BOUNDING_BOX_LEFT_X - 12.5, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 12.5, BOUNDING_BOX_RIGHT_X - 25 },
159 { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 12.5, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 12.5 },
163 { BOUNDING_BOX_LEFT_X + 12.5, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X + 12.5,
164 BOUNDING_BOX_RIGHT_X - 12.5, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X - 12.5 },
165 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_TOP_Y,
166 BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_BOTTOM_Y },
172 /* clip_polygon modifies the source operand and the test data must
173 * be const, so we need to deep copy it */
175 deep_copy_polygon8(const struct polygon8 *src, struct polygon8 *dst)
178 memcpy((void *) dst->x, src->x, sizeof (src->x));
179 memcpy((void *) dst->y, src->y, sizeof (src->y));
182 TEST_P(clip_polygon_n_vertices_emitted, test_data)
184 struct vertex_clip_test_data *tdata = data;
185 struct clip_context ctx;
186 struct polygon8 polygon;
189 deep_copy_polygon8(&tdata->surface, &polygon);
190 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
192 assert(emitted == tdata->expected.n);
195 TEST_P(clip_polygon_expected_vertices, test_data)
197 struct vertex_clip_test_data *tdata = data;
198 struct clip_context ctx;
199 struct polygon8 polygon;
202 deep_copy_polygon8(&tdata->surface, &polygon);
203 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
206 for (; i < emitted; ++i)
208 assert(vertices_x[i] == tdata->expected.x[i]);
209 assert(vertices_y[i] == tdata->expected.y[i]);
213 TEST(float_difference_different)
215 assert(float_difference(1.0f, 0.0f) == 1.0f);
218 TEST(float_difference_same)
220 assert(float_difference(1.0f, 1.0f) == 0.0f);