1 /*
2 * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
3 *
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:
11 *
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.
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.
24 */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <string.h>
30
31 #include "weston-test-runner.h"
32
33 #include "shared/helpers.h"
34 #include "vertex-clipping.h"
35
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
40
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)
45
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)
50
51 static void
populate_clip_context(struct clip_context * ctx)52 populate_clip_context (struct clip_context *ctx)
53 {
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;
58 }
59
60 static int
clip_polygon(struct clip_context * ctx,struct polygon8 * polygon,float * vertices_x,float * vertices_y)61 clip_polygon (struct clip_context *ctx,
62 struct polygon8 *polygon,
63 float *vertices_x,
64 float *vertices_y)
65 {
66 populate_clip_context(ctx);
67 return clip_transformed(ctx, polygon, vertices_x, vertices_y);
68 }
69
70 struct vertex_clip_test_data
71 {
72 struct polygon8 surface;
73 struct polygon8 expected;
74 };
75
76 const struct vertex_clip_test_data test_data[] =
77 {
78 /* All inside */
79 {
80 {
81 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
82 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
83 4
84 },
85 {
86 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
87 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
88 4
89 }
90 },
91 /* Top outside */
92 {
93 {
94 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
95 { INSIDE_Y1, INSIDE_Y1, OUTSIDE_Y2, OUTSIDE_Y2 },
96 4
97 },
98 {
99 { INSIDE_X1, INSIDE_X1, INSIDE_X2, INSIDE_X2 },
100 { BOUNDING_BOX_TOP_Y, INSIDE_Y1, INSIDE_Y1, BOUNDING_BOX_TOP_Y },
101 4
102 }
103 },
104 /* Bottom outside */
105 {
106 {
107 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
108 { OUTSIDE_Y1, OUTSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
109 4
110 },
111 {
112 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
113 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y, INSIDE_Y2, INSIDE_Y2 },
114 4
115 }
116 },
117 /* Left outside */
118 {
119 {
120 { OUTSIDE_X1, INSIDE_X2, INSIDE_X2, OUTSIDE_X1 },
121 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
122 4
123 },
124 {
125 { BOUNDING_BOX_LEFT_X, INSIDE_X2, INSIDE_X2, BOUNDING_BOX_LEFT_X },
126 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
127 4
128 }
129 },
130 /* Right outside */
131 {
132 {
133 { INSIDE_X1, OUTSIDE_X2, OUTSIDE_X2, INSIDE_X1 },
134 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
135 4
136 },
137 {
138 { INSIDE_X1, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, INSIDE_X1 },
139 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
140 4
141 }
142 },
143 /* Diamond extending from bounding box edges, clip to bounding box */
144 {
145 {
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 },
148 4
149 },
150 {
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 },
153 4
154 }
155 },
156 /* Diamond inside of bounding box edges, clip t bounding box, 8 resulting vertices */
157 {
158 {
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 },
161 4
162 },
163 {
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 },
168 8
169 }
170 }
171 };
172
173 /* clip_polygon modifies the source operand and the test data must
174 * be const, so we need to deep copy it */
175 static void
deep_copy_polygon8(const struct polygon8 * src,struct polygon8 * dst)176 deep_copy_polygon8(const struct polygon8 *src, struct polygon8 *dst)
177 {
178 dst->n = src->n;
179 memcpy((void *) dst->x, src->x, sizeof (src->x));
180 memcpy((void *) dst->y, src->y, sizeof (src->y));
181 }
182
TEST_P(clip_polygon_n_vertices_emitted,test_data)183 TEST_P(clip_polygon_n_vertices_emitted, test_data)
184 {
185 struct vertex_clip_test_data *tdata = data;
186 struct clip_context ctx;
187 struct polygon8 polygon;
188 float vertices_x[8];
189 float vertices_y[8];
190 deep_copy_polygon8(&tdata->surface, &polygon);
191 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
192
193 assert(emitted == tdata->expected.n);
194 }
195
TEST_P(clip_polygon_expected_vertices,test_data)196 TEST_P(clip_polygon_expected_vertices, test_data)
197 {
198 struct vertex_clip_test_data *tdata = data;
199 struct clip_context ctx;
200 struct polygon8 polygon;
201 float vertices_x[8];
202 float vertices_y[8];
203 deep_copy_polygon8(&tdata->surface, &polygon);
204 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
205 int i = 0;
206
207 for (; i < emitted; ++i)
208 {
209 assert(vertices_x[i] == tdata->expected.x[i]);
210 assert(vertices_y[i] == tdata->expected.y[i]);
211 }
212 }
213
TEST(float_difference_different)214 TEST(float_difference_different)
215 {
216 assert(float_difference(1.0f, 0.0f) == 1.0f);
217 }
218
TEST(float_difference_same)219 TEST(float_difference_same)
220 {
221 assert(float_difference(1.0f, 1.0f) == 0.0f);
222 }
223
224