downstream: ivi-shell: configure the ivi surface when created
[profile/ivi/weston-ivi-shell.git] / tests / vertex-clip-test.c
1 /*
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
23 #include "config.h"
24
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <math.h>
30
31 #include "weston-test-runner.h"
32
33 #include "../src/vertex-clipping.h"
34
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
50 static void
51 populate_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
59 static int
60 clip_polygon (struct clip_context *ctx,
61               struct polygon8 *polygon,
62               float *vertices_x,
63               float *vertices_y)
64 {
65         populate_clip_context(ctx);
66         return clip_transformed(ctx, polygon, vertices_x, vertices_y);
67 }
68
69 struct vertex_clip_test_data
70 {
71         struct polygon8 surface;
72         struct polygon8 expected;
73 };
74
75 const 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 */
174 static void
175 deep_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
182 TEST_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;
187         float vertices_x[8];
188         float vertices_y[8];
189         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
195 TEST_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;
200         float vertices_x[8];
201         float vertices_y[8];
202         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
213 TEST(float_difference_different)
214 {
215         assert(float_difference(1.0f, 0.0f) == 1.0f);
216 }
217
218 TEST(float_difference_same)
219 {
220         assert(float_difference(1.0f, 1.0f) == 0.0f);
221 }
222