1 /* EINA - EFL data type library
2 * Copyright (C) 2010 Cedric Bail
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library;
16 * if not, see <http://www.gnu.org/licenses/>.
26 #include "eina_suite.h"
29 static Eina_Quad_Direction
30 _eina_quadtree_rectangle_vert(const void *object, size_t middle)
32 const Eina_Rectangle *r = object;
34 if (r->y + r->h < (int)middle)
35 return EINA_QUAD_LEFT;
37 if (r->y > (int)middle)
38 return EINA_QUAD_RIGHT;
40 return EINA_QUAD_BOTH;
43 static Eina_Quad_Direction
44 _eina_quadtree_rectangle_hort(const void *object, size_t middle)
46 const Eina_Rectangle *r = object;
48 if (r->x + r->w < (int)middle)
49 return EINA_QUAD_LEFT;
51 if (r->x > (int)middle)
52 return EINA_QUAD_RIGHT;
54 return EINA_QUAD_BOTH;
57 START_TEST(eina_quadtree_collision)
62 Eina_QuadTree_Item *item;
64 { { 10, 10, 30, 30 }, NULL },
65 { { 20, 20, 30, 30 }, NULL },
66 { { 5, 30, 30, 30 }, NULL },
67 { { 70, 130, 100, 100 }, NULL },
68 { { 10, 220, 50, 40 }, NULL },
69 { { 310, 20, 50, 30 }, NULL },
70 { { 300, 220, 40, 40 }, NULL },
71 { { 500, 150, 40, 40 }, NULL },
72 { { 500, 220, 40, 40 }, NULL },
73 { { 330, 250, 40, 40 }, NULL },
74 { { 300, 400, 40, 40 }, NULL },
75 { { 10, 400, 40, 40 }, NULL },
76 { { 0, 0, 0, 0 }, NULL }
84 { { 600, 400, 40, 40 }, 4, { 4, 6, 8, 10 } },
85 { { 20, 30, 10, 10 }, 7, { 0, 1, 2, 4, 5, 6, 8 } },
86 { { 0, 0, 0, 0 }, -1, {} },
88 int hidden[] = { 4, 5, 6, 8, 10 };
89 int show[] = { 0, 1, 2 };
96 fail_if(!eina_init());
98 q = eina_quadtree_new(640, 480,
99 _eina_quadtree_rectangle_vert,
100 _eina_quadtree_rectangle_hort);
104 for (i = 0; objects[i].r.w != 0 && objects[i].r.h != 0; ++i)
106 objects[i].item = eina_quadtree_add(q, &objects[i].r);
107 fail_if(!objects[i].item);
108 fail_if(!eina_quadtree_show(objects[i].item));
111 eina_quadtree_resize(q, 640, 480);
113 for (i = 0; tests[i].count != -1; ++i)
115 head = eina_quadtree_collide(q,
116 tests[i].r.x, tests[i].r.y,
117 tests[i].r.w, tests[i].r.h);
124 r = eina_quadtree_object(head);
126 for (k = 0; k < tests[i].count; ++k)
128 if (&objects[tests[i].result[k]].r == r)
131 fail_if(k == tests[i].count);
136 fail_if(count != tests[i].count);
139 for (i = 0; i < (int)(sizeof (hidden) / sizeof (int)); ++i)
140 eina_quadtree_hide(objects[hidden[i]].item);
141 for (i = 0; i < (int)(sizeof (show) / sizeof (int)); ++i)
142 eina_quadtree_show(objects[show[i]].item);
144 head = eina_quadtree_collide(q,
145 tests[1].r.x, tests[1].r.y,
146 tests[1].r.w, tests[1].r.h);
151 r = eina_quadtree_object(head);
153 fail_if(r != &objects[tests[1].result[show[count]]].r);
160 eina_quadtree_cycle(q);
161 eina_quadtree_show(objects[4].item);
162 eina_quadtree_increase(objects[4].item);
163 eina_quadtree_show(objects[5].item);
164 eina_quadtree_increase(objects[5].item);
165 eina_quadtree_del(objects[5].item);
166 eina_quadtree_change(objects[10].item);
167 eina_quadtree_increase(objects[10].item);
169 eina_quadtree_resize(q, 641, 480);
171 head = eina_quadtree_collide(q,
172 tests[0].r.x, tests[0].r.y,
173 tests[0].r.w, tests[0].r.h);
178 r = eina_quadtree_object(head);
185 eina_quadtree_free(q);
192 eina_test_quadtree(TCase *tc)
194 tcase_add_test(tc, eina_quadtree_collision);