2 * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 #include "weston-test-runner.h"
33 #include "shared/helpers.h"
34 #include "vertex-clipping.h"
36 #define BOUNDING_BOX_TOP_Y 100.0f
37 #define BOUNDING_BOX_LEFT_X 50.0f
38 #define BOUNDING_BOX_RIGHT_X 100.0f
39 #define BOUNDING_BOX_BOTTOM_Y 50.0f
41 #define INSIDE_X1 (BOUNDING_BOX_LEFT_X + 1.0f)
42 #define INSIDE_X2 (BOUNDING_BOX_RIGHT_X - 1.0f)
43 #define INSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y + 1.0f)
44 #define INSIDE_Y2 (BOUNDING_BOX_TOP_Y - 1.0f)
46 #define OUTSIDE_X1 (BOUNDING_BOX_LEFT_X - 1.0f)
47 #define OUTSIDE_X2 (BOUNDING_BOX_RIGHT_X + 1.0f)
48 #define OUTSIDE_Y1 (BOUNDING_BOX_BOTTOM_Y - 1.0f)
49 #define OUTSIDE_Y2 (BOUNDING_BOX_TOP_Y + 1.0f)
52 populate_clip_context (struct clip_context *ctx)
54 ctx->clip.x1 = BOUNDING_BOX_LEFT_X;
55 ctx->clip.y1 = BOUNDING_BOX_BOTTOM_Y;
56 ctx->clip.x2 = BOUNDING_BOX_RIGHT_X;
57 ctx->clip.y2 = BOUNDING_BOX_TOP_Y;
61 clip_polygon (struct clip_context *ctx,
62 struct polygon8 *polygon,
66 populate_clip_context(ctx);
67 return clip_transformed(ctx, polygon, vertices_x, vertices_y);
70 struct vertex_clip_test_data
72 struct polygon8 surface;
73 struct polygon8 expected;
76 const struct vertex_clip_test_data test_data[] =
81 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
82 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
86 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
87 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
94 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
95 { INSIDE_Y1, INSIDE_Y1, OUTSIDE_Y2, OUTSIDE_Y2 },
99 { INSIDE_X1, INSIDE_X1, INSIDE_X2, INSIDE_X2 },
100 { BOUNDING_BOX_TOP_Y, INSIDE_Y1, INSIDE_Y1, BOUNDING_BOX_TOP_Y },
107 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
108 { OUTSIDE_Y1, OUTSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
112 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
113 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y, INSIDE_Y2, INSIDE_Y2 },
120 { OUTSIDE_X1, INSIDE_X2, INSIDE_X2, OUTSIDE_X1 },
121 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
125 { BOUNDING_BOX_LEFT_X, INSIDE_X2, INSIDE_X2, BOUNDING_BOX_LEFT_X },
126 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
133 { INSIDE_X1, OUTSIDE_X2, OUTSIDE_X2, INSIDE_X1 },
134 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
138 { INSIDE_X1, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, INSIDE_X1 },
139 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
143 /* Diamond extending from bounding box edges, clip to bounding box */
146 { BOUNDING_BOX_LEFT_X - 25, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 25, BOUNDING_BOX_RIGHT_X - 25 },
147 { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 25, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 25 },
151 { BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X },
152 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y, BOUNDING_BOX_BOTTOM_Y },
156 /* Diamond inside of bounding box edges, clip t bounding box, 8 resulting vertices */
159 { BOUNDING_BOX_LEFT_X - 12.5, BOUNDING_BOX_LEFT_X + 25, BOUNDING_BOX_RIGHT_X + 12.5, BOUNDING_BOX_RIGHT_X - 25 },
160 { BOUNDING_BOX_BOTTOM_Y + 25, BOUNDING_BOX_TOP_Y + 12.5, BOUNDING_BOX_TOP_Y - 25, BOUNDING_BOX_BOTTOM_Y - 12.5 },
164 { BOUNDING_BOX_LEFT_X + 12.5, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X, BOUNDING_BOX_LEFT_X + 12.5,
165 BOUNDING_BOX_RIGHT_X - 12.5, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X - 12.5 },
166 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_TOP_Y,
167 BOUNDING_BOX_TOP_Y, BOUNDING_BOX_TOP_Y - 12.5, BOUNDING_BOX_BOTTOM_Y + 12.5, BOUNDING_BOX_BOTTOM_Y },
173 /* clip_polygon modifies the source operand and the test data must
174 * be const, so we need to deep copy it */
176 deep_copy_polygon8(const struct polygon8 *src, struct polygon8 *dst)
179 memcpy((void *) dst->x, src->x, sizeof (src->x));
180 memcpy((void *) dst->y, src->y, sizeof (src->y));
183 TEST_P(clip_polygon_n_vertices_emitted, test_data)
185 struct vertex_clip_test_data *tdata = data;
186 struct clip_context ctx;
187 struct polygon8 polygon;
190 deep_copy_polygon8(&tdata->surface, &polygon);
191 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
193 assert(emitted == tdata->expected.n);
196 TEST_P(clip_polygon_expected_vertices, test_data)
198 struct vertex_clip_test_data *tdata = data;
199 struct clip_context ctx;
200 struct polygon8 polygon;
203 deep_copy_polygon8(&tdata->surface, &polygon);
204 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
207 for (; i < emitted; ++i)
209 assert(vertices_x[i] == tdata->expected.x[i]);
210 assert(vertices_y[i] == tdata->expected.y[i]);
214 TEST(float_difference_different)
216 assert(float_difference(1.0f, 0.0f) == 1.0f);
219 TEST(float_difference_same)
221 assert(float_difference(1.0f, 1.0f) == 0.0f);