merge exportvis branch into cairocanvas, to reduce the number of "floating" branches.
[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 #include "canvas/types.h"
26
27 class OptimizingLookupTableTest;
28
29 namespace ArdourCanvas {
30
31 class Item;
32 class Group;
33
34 class LookupTable
35 {
36 public:
37     LookupTable (Group const &);
38     virtual ~LookupTable ();
39     
40     virtual std::vector<Item*> get (Rect const &) = 0;
41     virtual std::vector<Item*> items_at_point (Duple const &) const = 0;
42     virtual bool has_item_at_point (Duple const & point) const = 0;
43
44 protected:
45         
46     Group const & _group;
47 };
48
49 class DumbLookupTable : public LookupTable
50 {
51 public:
52     DumbLookupTable (Group const &);
53     
54     std::vector<Item*> get (Rect const &);
55     std::vector<Item*> items_at_point (Duple const &) const;
56     bool has_item_at_point (Duple const & point) const;
57 };
58
59 class OptimizingLookupTable : public LookupTable
60 {
61 public:
62     OptimizingLookupTable (Group const &, int);
63     ~OptimizingLookupTable ();
64     std::vector<Item*> get (Rect const &);
65     std::vector<Item*> items_at_point (Duple const &) const;
66     bool has_item_at_point (Duple const & point) const;
67     
68     static int default_items_per_cell;
69     
70   private:
71     
72     void area_to_indices (Rect const &, int &, int &, int &, int &) const;
73     void point_to_indices (Duple, int &, int &) const;
74     
75     friend class ::OptimizingLookupTableTest;
76     
77     typedef std::vector<Item*> Cell;
78     int _items_per_cell;
79     int _dimension;
80     Duple _cell_size;
81     Duple _offset;
82     Cell** _cells;
83     bool _added;
84 };
85
86 }
87
88 #endif