summaryrefslogtreecommitdiff
path: root/libs/canvas/lookup_table.cc
blob: 92d96da30759e7667db28fa9b31eaedc7e9eee07 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "canvas/item.h"
#include "canvas/lookup_table.h"

using namespace std;
using namespace ArdourCanvas;

LookupTable::LookupTable (Item const & item)
	: _item (item)
{

}

LookupTable::~LookupTable ()
{

}

DumbLookupTable::DumbLookupTable (Item const & item)
	: LookupTable (item)
{

}

vector<Item *>
DumbLookupTable::get (Rect const &area)
{
	list<Item *> const & items = _item.items ();
	vector<Item *> vitems;
#if 1
	for (list<Item *>::const_iterator i = items.begin(); i != items.end(); ++i) {
		Rect item_bbox = (*i)->bounding_box ();
		if (!item_bbox) continue;
		Rect item = (*i)->item_to_window (item_bbox);
		if (item.intersection (area)) {
			vitems.push_back (*i);
		}
	}
#else
	copy (items.begin(), items.end(), back_inserter (vitems));
#endif
	return vitems;
}

vector<Item *>
DumbLookupTable::items_at_point (Duple const & point) const
{
	/* Point is in window coordinate system */

	list<Item *> const & items (_item.items ());
	vector<Item *> vitems;

	for (list<Item *>::const_iterator i = items.begin(); i != items.end(); ++i) {

		if ((*i)->covers (point)) {
			// std::cerr << "\t\t" << (*i)->whatami() << '/' << (*i)->name << " covers " << point << std::endl;
			vitems.push_back (*i);
		}
	}

	return vitems;
}

bool
DumbLookupTable::has_item_at_point (Duple const & point) const
{
	/* Point is in window coordinate system */

	list<Item *> const & items (_item.items ());
	vector<Item *> vitems;

	for (list<Item *>::const_iterator i = items.begin(); i != items.end(); ++i) {

		if (!(*i)->visible()) {
			continue;
		}

		if ((*i)->covers (point)) {
			// std::cerr << "\t\t" << (*i)->whatami() << '/' << (*i)->name << " covers " << point << std::endl;
			return true;

		}
	}

	return false;
}

OptimizingLookupTable::OptimizingLookupTable (Item const & item, int items_per_cell)
	: LookupTable (item)
	, _items_per_cell (items_per_cell)
	, _added (false)
{
	list<Item*> const & items = _item.items ();

	/* number of cells */
	int const cells = items.size() / _items_per_cell;
	/* hence number down each side of the table's square */
	_dimension = max (1, int (rint (sqrt ((double)cells))));

	_cells = new Cell*[_dimension];
	for (int i = 0; i < _dimension; ++i) {
		_cells[i] = new Cell[_dimension];
	}

	/* our item's bounding box in its coordinates */
	Rect bbox = _item.bounding_box ();
	if (!bbox) {
		return;
	}

	_cell_size.x = bbox.width() / _dimension;
	_cell_size.y = bbox.height() / _dimension;
	_offset.x = bbox.x0;
	_offset.y = bbox.y0;

//	cout << "BUILD bbox=" << bbox << ", cellsize=" << _cell_size << ", offset=" << _offset << ", dimension=" << _dimension << "\n";

	for (list<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {

		/* item bbox in its own coordinates */
		Rect item_bbox = (*i)->bounding_box ();
		if (!item_bbox) {
			continue;
		}

		/* and in the item's coordinates */
		Rect const item_bbox_in_item = (*i)->item_to_parent (item_bbox);

		int x0, y0, x1, y1;
		area_to_indices (item_bbox_in_item, x0, y0, x1, y1);

		/* XXX */
		assert (x0 >= 0);
		assert (y0 >= 0);
		assert (x1 >= 0);
		assert (y1 >= 0);
		//assert (x0 <= _dimension);
		//assert (y0 <= _dimension);
		//assert (x1 <= _dimension);
		//assert (y1 <= _dimension);

		if (x0 > _dimension) {
			cout << "WARNING: item outside bbox by " << (item_bbox_in_item.x0 - bbox.x0) << "\n";
			x0 = _dimension;
		}
		if (x1 > _dimension) {
			cout << "WARNING: item outside bbox by " << (item_bbox_in_item.x1 - bbox.x1) << "\n";
			x1 = _dimension;
		}
		if (y0 > _dimension) {
			cout << "WARNING: item outside bbox by " << (item_bbox_in_item.y0 - bbox.y0) << "\n";
			y0 = _dimension;
		}
		if (y1 > _dimension) {
			cout << "WARNING: item outside bbox by " << (item_bbox_in_item.y1 - bbox.y1) << "\n";
			y1 = _dimension;
		}

		for (int x = x0; x < x1; ++x) {
			for (int y = y0; y < y1; ++y) {
				_cells[x][y].push_back (*i);
			}
		}
	}
}

void
OptimizingLookupTable::area_to_indices (Rect const & area, int& x0, int& y0, int& x1, int& y1) const
{
	if (_cell_size.x == 0 || _cell_size.y == 0) {
		x0 = y0 = x1 = y1 = 0;
		return;
	}

	Rect const offset_area = area.translate (-_offset);

	x0 = floor (offset_area.x0 / _cell_size.x);
	y0 = floor (offset_area.y0 / _cell_size.y);
	x1 = ceil  (offset_area.x1 / _cell_size.x);
	y1 = ceil  (offset_area.y1 / _cell_size.y);
}

OptimizingLookupTable::~OptimizingLookupTable ()
{
	for (int i = 0; i < _dimension; ++i) {
		delete[] _cells[i];
	}

	delete[] _cells;
}

void
OptimizingLookupTable::point_to_indices (Duple point, int& x, int& y) const
{
	if (_cell_size.x == 0 || _cell_size.y == 0) {
		x = y = 0;
		return;
	}

	Duple const offset_point = point - _offset;

	x = floor (offset_point.x / _cell_size.x);
	y = floor (offset_point.y / _cell_size.y);
}

vector<Item*>
OptimizingLookupTable::items_at_point (Duple const & point) const
{
	int x;
	int y;
	point_to_indices (point, x, y);

	if (x >= _dimension) {
		cout << "WARNING: x=" << x << ", dim=" << _dimension << ", px=" << point.x << " cellsize=" << _cell_size << "\n";
	}

	if (y >= _dimension) {
		cout << "WARNING: y=" << y << ", dim=" << _dimension << ", py=" << point.y << " cellsize=" << _cell_size << "\n";
	}

	/* XXX: hmm */
	x = min (_dimension - 1, x);
	y = min (_dimension - 1, y);

	assert (x >= 0);
	assert (y >= 0);

	Cell const & cell = _cells[x][y];
	vector<Item*> items;
	for (Cell::const_iterator i = cell.begin(); i != cell.end(); ++i) {
		Rect const item_bbox = (*i)->bounding_box ();
		if (item_bbox) {
			Rect parent_bbox = (*i)->item_to_parent (item_bbox);
			if (parent_bbox.contains (point)) {
				items.push_back (*i);
			}
		}
	}

	return items;
}

bool
OptimizingLookupTable::has_item_at_point (Duple const & point) const
{
	int x;
	int y;
	point_to_indices (point, x, y);

	if (x >= _dimension) {
		cout << "WARNING: x=" << x << ", dim=" << _dimension << ", px=" << point.x << " cellsize=" << _cell_size << "\n";
	}

	if (y >= _dimension) {
		cout << "WARNING: y=" << y << ", dim=" << _dimension << ", py=" << point.y << " cellsize=" << _cell_size << "\n";
	}

	/* XXX: hmm */
	x = min (_dimension - 1, x);
	y = min (_dimension - 1, y);

	assert (x >= 0);
	assert (y >= 0);

	Cell const & cell = _cells[x][y];
	vector<Item*> items;
	for (Cell::const_iterator i = cell.begin(); i != cell.end(); ++i) {
		Rect const item_bbox = (*i)->bounding_box ();
		if (item_bbox) {
			Rect parent_bbox = (*i)->item_to_parent (item_bbox);
			if (parent_bbox.contains (point)) {
				return true;
			}
		}
	}

	return false;
}

/** @param area Area in our owning item's coordinates */
vector<Item*>
OptimizingLookupTable::get (Rect const & area)
{
	list<Item*> items;
	int x0, y0, x1, y1;
	area_to_indices (area, x0, y0, x1, y1);

	/* XXX: hmm... */
	x0 = min (_dimension - 1, x0);
	y0 = min (_dimension - 1, y0);
	x1 = min (_dimension, x1);
	y1 = min (_dimension, y1);

	for (int x = x0; x < x1; ++x) {
		for (int y = y0; y < y1; ++y) {
			for (Cell::const_iterator i = _cells[x][y].begin(); i != _cells[x][y].end(); ++i) {
				if (find (items.begin(), items.end(), *i) == items.end ()) {
					items.push_back (*i);
				}
			}
		}
	}

	vector<Item*> vitems;
	copy (items.begin (), items.end (), back_inserter (vitems));

	return vitems;
}