summaryrefslogtreecommitdiff
path: root/libs/canvas/test/group.cc
blob: 31b7d13e4951169549c1229f0ee502f8f6bbb082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "canvas/group.h"
#include "canvas/types.h"
#include "canvas/rectangle.h"
#include "canvas/canvas.h"
#include "group.h"

using namespace std;
using namespace ArdourCanvas;

CPPUNIT_TEST_SUITE_REGISTRATION (GroupTest);

/* Do some basic checks on the group's computation of its bounding box */
void
GroupTest::bounding_box ()
{
	/* a group with 4 rectangles in it */
	ImageCanvas canvas;
	Rectangle a (canvas.root(), Rect (0, 0, 32, 32));
	a.set_outline_width (0);
	Rectangle b (canvas.root(), Rect (0, 33, 32, 64));
	b.set_outline_width (0);
	Rectangle c (canvas.root(), Rect (33, 0, 64, 32));
	c.set_outline_width (0);
	Rectangle d (canvas.root(), Rect (33, 33, 64, 64));
	d.set_outline_width (0);
	boost::optional<Rect> bbox = canvas.root()->bounding_box ();

	/* check the bounding box */
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 64);
	CPPUNIT_ASSERT (bbox.get().y1 == 64);

	/* check that adding an item resets the bbox */
	Rectangle e (canvas.root(), Rect (64, 64, 128, 128));
	bbox = canvas.root()->bounding_box ();

	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 128.25);
	CPPUNIT_ASSERT (bbox.get().y1 == 128.25);
}

/* Check that a group containing only items with no bounding box itself has no bounding box */
void
GroupTest::null_bounding_box ()
{
	ImageCanvas canvas;

	Group empty (canvas.root());

	boost::optional<Rect> bbox = empty.bounding_box ();
	CPPUNIT_ASSERT (!bbox.is_initialized ());
}

/* Do some basic tests on layering */
void
GroupTest::layers ()
{
	/* Set up 4 rectangles; order from the bottom is
	   a - b - c - d
	*/
	ImageCanvas canvas;
	Rectangle a (canvas.root(), Rect (0, 0, 32, 32));
	Rectangle b (canvas.root(), Rect (0, 0, 32, 32));
	Rectangle c (canvas.root(), Rect (0, 0, 32, 32));
	Rectangle d (canvas.root(), Rect (0, 0, 32, 32));

	/* Put a on top and check */
	a.raise_to_top ();

	list<Item*>::const_iterator i = canvas.root()->items().begin();
	CPPUNIT_ASSERT (*i++ == &b);
	CPPUNIT_ASSERT (*i++ == &c);
	CPPUNIT_ASSERT (*i++ == &d);
	CPPUNIT_ASSERT (*i++ == &a);

	/* Put a on the bottom and check */
	a.lower_to_bottom ();

	i = canvas.root()->items().begin();
	CPPUNIT_ASSERT (*i++ == &a);
	CPPUNIT_ASSERT (*i++ == &b);
	CPPUNIT_ASSERT (*i++ == &c);
	CPPUNIT_ASSERT (*i++ == &d);

	/* Check raise by a number of levels */

	a.raise (2);

	i = canvas.root()->items().begin();
	CPPUNIT_ASSERT (*i++ == &b);
	CPPUNIT_ASSERT (*i++ == &c);
	CPPUNIT_ASSERT (*i++ == &a);
	CPPUNIT_ASSERT (*i++ == &d);

	a.raise (4);

	i = canvas.root()->items().begin();
	CPPUNIT_ASSERT (*i++ == &b);
	CPPUNIT_ASSERT (*i++ == &c);
	CPPUNIT_ASSERT (*i++ == &d);
	CPPUNIT_ASSERT (*i++ == &a);
}

/* Check that groups notice when their children change */
void
GroupTest::children_changing ()
{
	ImageCanvas canvas;

	/* Put a rectangle in the root group */
	Rectangle a (canvas.root(), Rect (0, 0, 32, 32));
	a.set_outline_width (0);

	/* Check that initial bbox */
	boost::optional<Rect> bbox = canvas.root()->bounding_box ();
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 32);
	CPPUNIT_ASSERT (bbox.get().y1 == 32);

	/* Change the rectangle's size and check the parent */
	a.set (Rect (0, 0, 48, 48));
	bbox = canvas.root()->bounding_box ();
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 48);
	CPPUNIT_ASSERT (bbox.get().y1 == 48);

	/* Change the rectangle's line width and check the parent */
	a.set_outline_width (1);
	bbox = canvas.root()->bounding_box ();
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == -0.5);
	CPPUNIT_ASSERT (bbox.get().y0 == -0.5);
	CPPUNIT_ASSERT (bbox.get().x1 == 48.5);
	CPPUNIT_ASSERT (bbox.get().y1 == 48.5);
}

/* Check that a group notices when its grandchildren change */
void
GroupTest::grandchildren_changing ()
{
	ImageCanvas canvas;

	/* Put a child group B in the root group */
	Group B (canvas.root());

	/* Grandchild rectangle */
	Rectangle a (&B, Rect (0, 0, 32, 32));
	a.set_outline_width (0);

	/* Check the initial bboxes */
	boost::optional<Rect> bbox = canvas.root()->bounding_box ();
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 32);
	CPPUNIT_ASSERT (bbox.get().y1 == 32);

	bbox = B.bounding_box ();
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 32);
	CPPUNIT_ASSERT (bbox.get().y1 == 32);

	/* Change the grandchild and check its parent and grandparent */
	a.set (Rect (0, 0, 48, 48));

	bbox = canvas.root()->bounding_box ();
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 48);
	CPPUNIT_ASSERT (bbox.get().y1 == 48);

	bbox = B.bounding_box ();
	CPPUNIT_ASSERT (bbox.is_initialized ());
	CPPUNIT_ASSERT (bbox.get().x0 == 0);
	CPPUNIT_ASSERT (bbox.get().y0 == 0);
	CPPUNIT_ASSERT (bbox.get().x1 == 48);
	CPPUNIT_ASSERT (bbox.get().y1 == 48);
}

/* Basic tests on the code to find items at a particular point */
void
GroupTest::add_items_at_point ()
{
	ImageCanvas canvas;

	Group gA (canvas.root());
	gA.set_position (Duple (128, 64));

	Group gB (&gA);
	gB.set_position (Duple (64, 32));

	/* two rectangles in the same place, rB on top of rA */
	Rectangle rA (&gB);
	rA.set_position (Duple (4, 2));
	rA.set (Rect (0, 0, 8, 4));
	Rectangle rB (&gB);
	rB.set_position (Duple (4, 2));
	rB.set (Rect (0, 0, 8, 4));

	/* rC below those two */
	Rectangle rC (&gB);
	rC.set_position (Duple (12, 6));
	rC.set (Rect (0, 0, 8, 4));

	vector<Item const *> items;
	canvas.root()->add_items_at_point (Duple (128 + 64 + 4 + 4, 64 + 32 + 2 + 2), items);
	CPPUNIT_ASSERT (items.size() == 5);
	vector<Item const *>::iterator i = items.begin ();
	CPPUNIT_ASSERT (*i++ == canvas.root ());
	CPPUNIT_ASSERT (*i++ == &gA);
	CPPUNIT_ASSERT (*i++ == &gB);
	CPPUNIT_ASSERT (*i++ == &rA);
	CPPUNIT_ASSERT (*i++ == &rB);

	items.clear ();
	canvas.root()->add_items_at_point (Duple (128 + 64 + 12 + 4, 64 + 32 + 6 + 2), items);
	CPPUNIT_ASSERT (items.size() == 4);
	i = items.begin ();
	CPPUNIT_ASSERT (*i++ == canvas.root ());
	CPPUNIT_ASSERT (*i++ == &gA);
	CPPUNIT_ASSERT (*i++ == &gB);
	CPPUNIT_ASSERT (*i++ == &rC);
}

static double
double_random ()
{
	return ((double) rand() / RAND_MAX);
}

/* Check the find items at point code more thoroughly */
void
GroupTest::torture_add_items_at_point ()
{
	int const n_rectangles = 10000;
	int const n_tests = 1000;
	double const rough_size = 1000;
	srand (1);

	ImageCanvas canvas;

	list<Item*> rectangles;

	for (int i = 0; i < n_rectangles; ++i) {
		Rectangle* r = new Rectangle (canvas.root());
		double const x = double_random () * rough_size / 2;
		double const y = double_random () * rough_size / 2;
		double const w = double_random () * rough_size / 2;
		double const h = double_random () * rough_size / 2;
		r->set (Rect (x, y, x + w, y + h));
		rectangles.push_back (r);
	}

	for (int i = 0; i < n_tests; ++i) {
		Duple test (double_random() * rough_size, double_random() * rough_size);

		/* ask the group what's at this point */
		vector<Item const *> items_A;
		canvas.root()->add_items_at_point (test, items_A);

		/* work it out ourselves */
		vector<Item*> items_B;
		if (canvas.root()->bounding_box() && canvas.root()->bounding_box().get().contains (test)) {
			items_B.push_back (canvas.root());
		}

		for (list<Item*>::iterator j = rectangles.begin(); j != rectangles.end(); ++j) {
			boost::optional<Rect> bbox = (*j)->bounding_box ();
			assert (bbox);
			if (bbox.get().contains (test)) {
				items_B.push_back (*j);
			}
		}

		CPPUNIT_ASSERT (items_A.size() == items_B.size());
		vector<Item const *>::iterator j = items_A.begin ();
		vector<Item*>::iterator k = items_B.begin ();
		while (j != items_A.end ()) {
			CPPUNIT_ASSERT (*j == *k);
			++j;
			++k;
		}
	}
}