2013-04-15 22:10:18 -04:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
2013-04-15 22:10:18 -04:00
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
#include "canvas/item.h"
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "canvas/lookup_table.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
LookupTable::LookupTable (Item const & item)
|
|
|
|
: _item (item)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
LookupTable::~LookupTable ()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
DumbLookupTable::DumbLookupTable (Item const & item)
|
|
|
|
: LookupTable (item)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Item *>
|
2014-12-05 16:40:26 -05:00
|
|
|
DumbLookupTable::get (Rect const &area)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
vector<Item *> vitems;
|
2014-12-05 16:40:26 -05:00
|
|
|
#if 1
|
2023-01-12 19:05:23 -05:00
|
|
|
for (auto const & item : _item.items()) {
|
|
|
|
Rect item_bbox = item->bounding_box ();
|
2014-12-05 16:40:26 -05:00
|
|
|
if (!item_bbox) continue;
|
2023-01-12 19:05:23 -05:00
|
|
|
Rect item_rect = item->item_to_window (item_bbox);
|
|
|
|
if (item_rect.intersection (area)) {
|
|
|
|
vitems.push_back (item);
|
2014-12-05 16:40:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2013-04-04 00:32:52 -04:00
|
|
|
copy (items.begin(), items.end(), back_inserter (vitems));
|
2014-12-05 16:40:26 -05:00
|
|
|
#endif
|
2013-04-04 00:32:52 -04:00
|
|
|
return vitems;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Item *>
|
2013-12-12 10:03:33 -05:00
|
|
|
DumbLookupTable::items_at_point (Duple const & point) const
|
|
|
|
{
|
2014-05-23 22:05:08 -04:00
|
|
|
/* Point is in window coordinate system */
|
2013-12-12 10:03:33 -05:00
|
|
|
|
|
|
|
vector<Item *> vitems;
|
|
|
|
|
2023-01-12 19:05:23 -05:00
|
|
|
for (auto const & item : _item.items()) {
|
2013-12-12 10:03:33 -05:00
|
|
|
|
2023-01-12 19:05:23 -05:00
|
|
|
if (item->covers (point)) {
|
|
|
|
vitems.push_back (item);
|
2013-12-12 10:03:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vitems;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
DumbLookupTable::has_item_at_point (Duple const & point) const
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2014-05-23 22:05:08 -04:00
|
|
|
/* Point is in window coordinate system */
|
2013-10-28 12:27:03 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
vector<Item *> vitems;
|
|
|
|
|
2023-01-12 19:05:23 -05:00
|
|
|
for (auto const & item : _item.items()) {
|
2013-11-03 10:07:00 -05:00
|
|
|
|
2023-01-12 19:05:23 -05:00
|
|
|
if (!item->visible()) {
|
2013-11-03 10:07:00 -05:00
|
|
|
continue;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-01-12 19:05:23 -05:00
|
|
|
if (item->covers (point)) {
|
2013-12-12 10:03:33 -05:00
|
|
|
// std::cerr << "\t\t" << (*i)->whatami() << '/' << (*i)->name << " covers " << point << std::endl;
|
|
|
|
return true;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 10:03:33 -05:00
|
|
|
return false;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
OptimizingLookupTable::OptimizingLookupTable (Item const & item, int items_per_cell)
|
|
|
|
: LookupTable (item)
|
2013-04-04 00:32:52 -04:00
|
|
|
, _items_per_cell (items_per_cell)
|
|
|
|
, _added (false)
|
|
|
|
{
|
2014-06-21 11:43:42 -04:00
|
|
|
list<Item*> const & items = _item.items ();
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
/* number of cells */
|
|
|
|
int const cells = items.size() / _items_per_cell;
|
|
|
|
/* hence number down each side of the table's square */
|
2013-09-25 10:36:42 -04:00
|
|
|
_dimension = max (1, int (rint (sqrt ((double)cells))));
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
_cells = new Cell*[_dimension];
|
|
|
|
for (int i = 0; i < _dimension; ++i) {
|
|
|
|
_cells[i] = new Cell[_dimension];
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
/* our item's bounding box in its coordinates */
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect bbox = _item.bounding_box ();
|
2013-04-04 00:32:52 -04:00
|
|
|
if (!bbox) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-19 14:54:24 -05:00
|
|
|
_cell_size.x = bbox.width() / _dimension;
|
|
|
|
_cell_size.y = bbox.height() / _dimension;
|
|
|
|
_offset.x = bbox.x0;
|
|
|
|
_offset.y = bbox.y0;
|
2013-04-04 00:32:52 -04:00
|
|
|
|
2017-01-19 14:54:24 -05:00
|
|
|
// cout << "BUILD bbox=" << bbox << ", cellsize=" << _cell_size << ", offset=" << _offset << ", dimension=" << _dimension << "\n";
|
2013-04-04 00:32:52 -04:00
|
|
|
|
2023-01-12 19:05:23 -05:00
|
|
|
for (auto const & our_item : _item.items()) {
|
2013-04-04 00:32:52 -04:00
|
|
|
/* item bbox in its own coordinates */
|
2023-01-12 19:05:23 -05:00
|
|
|
Rect item_bbox = our_item->bounding_box ();
|
2013-04-04 00:32:52 -04:00
|
|
|
if (!item_bbox) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
/* and in the item's coordinates */
|
2023-01-12 19:05:23 -05:00
|
|
|
Rect const item_bbox_in_item = our_item->item_to_parent (item_bbox);
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
int x0, y0, x1, y1;
|
2014-06-21 11:43:42 -04:00
|
|
|
area_to_indices (item_bbox_in_item, x0, y0, x1, y1);
|
2013-04-04 00:32:52 -04:00
|
|
|
|
|
|
|
/* 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) {
|
2017-01-19 14:54:24 -05:00
|
|
|
cout << "WARNING: item outside bbox by " << (item_bbox_in_item.x0 - bbox.x0) << "\n";
|
2013-04-04 00:32:52 -04:00
|
|
|
x0 = _dimension;
|
|
|
|
}
|
|
|
|
if (x1 > _dimension) {
|
2017-01-19 14:54:24 -05:00
|
|
|
cout << "WARNING: item outside bbox by " << (item_bbox_in_item.x1 - bbox.x1) << "\n";
|
2013-04-04 00:32:52 -04:00
|
|
|
x1 = _dimension;
|
|
|
|
}
|
|
|
|
if (y0 > _dimension) {
|
2017-01-19 14:54:24 -05:00
|
|
|
cout << "WARNING: item outside bbox by " << (item_bbox_in_item.y0 - bbox.y0) << "\n";
|
2013-04-04 00:32:52 -04:00
|
|
|
y0 = _dimension;
|
|
|
|
}
|
|
|
|
if (y1 > _dimension) {
|
2017-01-19 14:54:24 -05:00
|
|
|
cout << "WARNING: item outside bbox by " << (item_bbox_in_item.y1 - bbox.y1) << "\n";
|
2013-04-04 00:32:52 -04:00
|
|
|
y1 = _dimension;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int x = x0; x < x1; ++x) {
|
|
|
|
for (int y = y0; y < y1; ++y) {
|
2023-01-12 19:05:23 -05:00
|
|
|
_cells[x][y].push_back (our_item);
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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*>
|
2013-12-12 10:03:33 -05:00
|
|
|
OptimizingLookupTable::items_at_point (Duple const & point) const
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
/* 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) {
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect const item_bbox = (*i)->bounding_box ();
|
2013-04-04 00:32:52 -04:00
|
|
|
if (item_bbox) {
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect parent_bbox = (*i)->item_to_parent (item_bbox);
|
2013-04-04 00:32:52 -04:00
|
|
|
if (parent_bbox.contains (point)) {
|
|
|
|
items.push_back (*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
}
|
2013-12-12 10:03:33 -05:00
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-12-12 10:03:33 -05:00
|
|
|
/* 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) {
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect const item_bbox = (*i)->bounding_box ();
|
2013-12-12 10:03:33 -05:00
|
|
|
if (item_bbox) {
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect parent_bbox = (*i)->item_to_parent (item_bbox);
|
2013-12-12 10:03:33 -05:00
|
|
|
if (parent_bbox.contains (point)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
/** @param area Area in our owning item's coordinates */
|
2013-04-04 00:32:52 -04:00
|
|
|
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));
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
return vitems;
|
|
|
|
}
|
|
|
|
|