blob: 2a5ec10e1546652d5390006c2c1551790e8d83a6 [file] [log] [blame]
Sam Spilsburyb42fb522013-09-13 10:01:22 +08001/*
2 * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
3 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07004 * 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:
Sam Spilsburyb42fb522013-09-13 10:01:22 +080011 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070012 * 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.
15 *
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
23 * SOFTWARE.
Sam Spilsburyb42fb522013-09-13 10:01:22 +080024 */
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010025
26#include "config.h"
27
Sam Spilsburyb42fb522013-09-13 10:01:22 +080028#include <assert.h>
Sam Spilsburyb42fb522013-09-13 10:01:22 +080029#include <string.h>
Sam Spilsburyb42fb522013-09-13 10:01:22 +080030
31#include "weston-test-runner.h"
32
Jon Cruz4678bab2015-06-15 15:37:07 -070033#include "src/vertex-clipping.h"
Sam Spilsburyb42fb522013-09-13 10:01:22 +080034
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
39
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)
44
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)
49
50static void
51populate_clip_context (struct clip_context *ctx)
52{
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;
57}
58
59static int
60clip_polygon (struct clip_context *ctx,
61 struct polygon8 *polygon,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010062 float *vertices_x,
63 float *vertices_y)
Sam Spilsburyb42fb522013-09-13 10:01:22 +080064{
65 populate_clip_context(ctx);
66 return clip_transformed(ctx, polygon, vertices_x, vertices_y);
67}
68
69struct vertex_clip_test_data
70{
71 struct polygon8 surface;
72 struct polygon8 expected;
73};
74
75const struct vertex_clip_test_data test_data[] =
76{
77 /* All inside */
78 {
79 {
80 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
81 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
82 4
83 },
84 {
85 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
86 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
87 4
88 }
89 },
90 /* Top outside */
91 {
92 {
93 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
94 { INSIDE_Y1, INSIDE_Y1, OUTSIDE_Y2, OUTSIDE_Y2 },
95 4
96 },
97 {
98 { INSIDE_X1, INSIDE_X1, INSIDE_X2, INSIDE_X2 },
99 { BOUNDING_BOX_TOP_Y, INSIDE_Y1, INSIDE_Y1, BOUNDING_BOX_TOP_Y },
100 4
101 }
102 },
103 /* Bottom outside */
104 {
105 {
106 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
107 { OUTSIDE_Y1, OUTSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
108 4
109 },
110 {
111 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
112 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y, INSIDE_Y2, INSIDE_Y2 },
113 4
114 }
115 },
116 /* Left outside */
117 {
118 {
119 { OUTSIDE_X1, INSIDE_X2, INSIDE_X2, OUTSIDE_X1 },
120 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
121 4
122 },
123 {
124 { BOUNDING_BOX_LEFT_X, INSIDE_X2, INSIDE_X2, BOUNDING_BOX_LEFT_X },
125 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
126 4
127 }
128 },
129 /* Right outside */
130 {
131 {
132 { INSIDE_X1, OUTSIDE_X2, OUTSIDE_X2, INSIDE_X1 },
133 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
134 4
135 },
136 {
137 { INSIDE_X1, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, INSIDE_X1 },
138 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
139 4
140 }
141 },
142 /* Diamond extending from bounding box edges, clip to bounding box */
143 {
144 {
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 },
147 4
148 },
149 {
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 },
152 4
153 }
154 },
155 /* Diamond inside of bounding box edges, clip t bounding box, 8 resulting vertices */
156 {
157 {
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 },
160 4
161 },
162 {
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 },
167 8
168 }
169 }
170};
171
172/* clip_polygon modifies the source operand and the test data must
173 * be const, so we need to deep copy it */
174static void
175deep_copy_polygon8(const struct polygon8 *src, struct polygon8 *dst)
176{
177 dst->n = src->n;
178 memcpy((void *) dst->x, src->x, sizeof (src->x));
179 memcpy((void *) dst->y, src->y, sizeof (src->y));
180}
181
182TEST_P(clip_polygon_n_vertices_emitted, test_data)
183{
184 struct vertex_clip_test_data *tdata = data;
185 struct clip_context ctx;
186 struct polygon8 polygon;
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100187 float vertices_x[8];
188 float vertices_y[8];
Sam Spilsburyb42fb522013-09-13 10:01:22 +0800189 deep_copy_polygon8(&tdata->surface, &polygon);
190 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
191
192 assert(emitted == tdata->expected.n);
193}
194
195TEST_P(clip_polygon_expected_vertices, test_data)
196{
197 struct vertex_clip_test_data *tdata = data;
198 struct clip_context ctx;
199 struct polygon8 polygon;
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100200 float vertices_x[8];
201 float vertices_y[8];
Sam Spilsburyb42fb522013-09-13 10:01:22 +0800202 deep_copy_polygon8(&tdata->surface, &polygon);
203 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
204 int i = 0;
205
206 for (; i < emitted; ++i)
207 {
208 assert(vertices_x[i] == tdata->expected.x[i]);
209 assert(vertices_y[i] == tdata->expected.y[i]);
210 }
211}
212
213TEST(float_difference_different)
214{
215 assert(float_difference(1.0f, 0.0f) == 1.0f);
216}
217
218TEST(float_difference_same)
219{
220 assert(float_difference(1.0f, 1.0f) == 0.0f);
221}
222