Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / canvas / lookup_table.h
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __CANVAS_LOOKUP_TABLE_H__
21 #define __CANVAS_LOOKUP_TABLE_H__
22
23 #include <vector>
24 #include <boost/multi_array.hpp>
25
26 #include "canvas/visibility.h"
27 #include "canvas/types.h"
28
29 class OptimizingLookupTableTest;
30
31 namespace ArdourCanvas {
32
33 class Item;
34 class Group;
35
36 class LIBCANVAS_API LookupTable
37 {
38 public:
39     LookupTable (Group const &);
40     virtual ~LookupTable ();
41     
42     virtual std::vector<Item*> get (Rect const &) = 0;
43     virtual std::vector<Item*> items_at_point (Duple const &) const = 0;
44     virtual bool has_item_at_point (Duple const & point) const = 0;
45
46 protected:
47         
48     Group const & _group;
49 };
50
51 class LIBCANVAS_API DumbLookupTable : public LookupTable
52 {
53 public:
54     DumbLookupTable (Group const &);
55     
56     std::vector<Item*> get (Rect const &);
57     std::vector<Item*> items_at_point (Duple const &) const;
58     bool has_item_at_point (Duple const & point) const;
59 };
60
61 class LIBCANVAS_API OptimizingLookupTable : public LookupTable
62 {
63 public:
64     OptimizingLookupTable (Group const &, int);
65     ~OptimizingLookupTable ();
66     std::vector<Item*> get (Rect const &);
67     std::vector<Item*> items_at_point (Duple const &) const;
68     bool has_item_at_point (Duple const & point) const;
69     
70     static int default_items_per_cell;
71     
72   private:
73     
74     void area_to_indices (Rect const &, int &, int &, int &, int &) const;
75     void point_to_indices (Duple, int &, int &) const;
76     
77     friend class ::OptimizingLookupTableTest;
78     
79     typedef std::vector<Item*> Cell;
80     int _items_per_cell;
81     int _dimension;
82     Duple _cell_size;
83     Duple _offset;
84     Cell** _cells;
85     bool _added;
86 };
87
88 }
89
90 #endif