blob: 6d44aa2a16d00e89c11348673fc3a5bb41046ae1 [file] [log] [blame]
Sam Spilsburyb42fb522013-09-13 10:01:22 +08001/*
2 * Copyright © 2013 Sam Spilsbury <smspillaz@gmail.com>
3 *
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.
13 *
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.
21 */
22#include <assert.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <math.h>
27
28#include "weston-test-runner.h"
29
30#include "../src/vertex-clipping.h"
31
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
36
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)
41
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)
46
47static void
48populate_clip_context (struct clip_context *ctx)
49{
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;
54}
55
56static int
57clip_polygon (struct clip_context *ctx,
58 struct polygon8 *polygon,
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +010059 float *vertices_x,
60 float *vertices_y)
Sam Spilsburyb42fb522013-09-13 10:01:22 +080061{
62 populate_clip_context(ctx);
63 return clip_transformed(ctx, polygon, vertices_x, vertices_y);
64}
65
66struct vertex_clip_test_data
67{
68 struct polygon8 surface;
69 struct polygon8 expected;
70};
71
72const struct vertex_clip_test_data test_data[] =
73{
74 /* All inside */
75 {
76 {
77 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
78 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
79 4
80 },
81 {
82 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
83 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
84 4
85 }
86 },
87 /* Top outside */
88 {
89 {
90 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
91 { INSIDE_Y1, INSIDE_Y1, OUTSIDE_Y2, OUTSIDE_Y2 },
92 4
93 },
94 {
95 { INSIDE_X1, INSIDE_X1, INSIDE_X2, INSIDE_X2 },
96 { BOUNDING_BOX_TOP_Y, INSIDE_Y1, INSIDE_Y1, BOUNDING_BOX_TOP_Y },
97 4
98 }
99 },
100 /* Bottom outside */
101 {
102 {
103 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
104 { OUTSIDE_Y1, OUTSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
105 4
106 },
107 {
108 { INSIDE_X1, INSIDE_X2, INSIDE_X2, INSIDE_X1 },
109 { BOUNDING_BOX_BOTTOM_Y, BOUNDING_BOX_BOTTOM_Y, INSIDE_Y2, INSIDE_Y2 },
110 4
111 }
112 },
113 /* Left outside */
114 {
115 {
116 { OUTSIDE_X1, INSIDE_X2, INSIDE_X2, OUTSIDE_X1 },
117 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
118 4
119 },
120 {
121 { BOUNDING_BOX_LEFT_X, INSIDE_X2, INSIDE_X2, BOUNDING_BOX_LEFT_X },
122 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
123 4
124 }
125 },
126 /* Right outside */
127 {
128 {
129 { INSIDE_X1, OUTSIDE_X2, OUTSIDE_X2, INSIDE_X1 },
130 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
131 4
132 },
133 {
134 { INSIDE_X1, BOUNDING_BOX_RIGHT_X, BOUNDING_BOX_RIGHT_X, INSIDE_X1 },
135 { INSIDE_Y1, INSIDE_Y1, INSIDE_Y2, INSIDE_Y2 },
136 4
137 }
138 },
139 /* Diamond extending from bounding box edges, clip to bounding box */
140 {
141 {
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 },
144 4
145 },
146 {
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 },
149 4
150 }
151 },
152 /* Diamond inside of bounding box edges, clip t bounding box, 8 resulting vertices */
153 {
154 {
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 },
157 4
158 },
159 {
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 },
164 8
165 }
166 }
167};
168
169/* clip_polygon modifies the source operand and the test data must
170 * be const, so we need to deep copy it */
171static void
172deep_copy_polygon8(const struct polygon8 *src, struct polygon8 *dst)
173{
174 dst->n = src->n;
175 memcpy((void *) dst->x, src->x, sizeof (src->x));
176 memcpy((void *) dst->y, src->y, sizeof (src->y));
177}
178
179TEST_P(clip_polygon_n_vertices_emitted, test_data)
180{
181 struct vertex_clip_test_data *tdata = data;
182 struct clip_context ctx;
183 struct polygon8 polygon;
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100184 float vertices_x[8];
185 float vertices_y[8];
Sam Spilsburyb42fb522013-09-13 10:01:22 +0800186 deep_copy_polygon8(&tdata->surface, &polygon);
187 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
188
189 assert(emitted == tdata->expected.n);
190}
191
192TEST_P(clip_polygon_expected_vertices, test_data)
193{
194 struct vertex_clip_test_data *tdata = data;
195 struct clip_context ctx;
196 struct polygon8 polygon;
Tomeu Vizoso0f0a6ff2013-11-27 13:22:42 +0100197 float vertices_x[8];
198 float vertices_y[8];
Sam Spilsburyb42fb522013-09-13 10:01:22 +0800199 deep_copy_polygon8(&tdata->surface, &polygon);
200 int emitted = clip_polygon(&ctx, &polygon, vertices_x, vertices_y);
201 int i = 0;
202
203 for (; i < emitted; ++i)
204 {
205 assert(vertices_x[i] == tdata->expected.x[i]);
206 assert(vertices_y[i] == tdata->expected.y[i]);
207 }
208}
209
210TEST(float_difference_different)
211{
212 assert(float_difference(1.0f, 0.0f) == 1.0f);
213}
214
215TEST(float_difference_same)
216{
217 assert(float_difference(1.0f, 1.0f) == 0.0f);
218}
219