slightly optimize bounding box computation for ArdourCanvas::PolyItem by avoiding...
[ardour.git] / libs / canvas / poly_item.cc
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 #include <algorithm>
21
22 #include "pbd/compose.h"
23
24 #include "canvas/poly_item.h"
25 #include "canvas/canvas.h"
26
27 using namespace std;
28 using namespace ArdourCanvas;
29
30 PolyItem::PolyItem (Group* parent)
31         : Item (parent)
32         , Outline (parent)
33 {
34
35 }
36
37 void
38 PolyItem::compute_bounding_box () const
39 {
40         
41         if (!_points.empty()) {
42
43                 Rect bbox;
44                 Points::const_iterator i = _points.begin();
45                 
46                 bbox.x0 = bbox.x1 = i->x;
47                 bbox.y0 = bbox.y1 = i->y;
48                 
49                 while (i != _points.end()) {
50                         bbox.x0 = min (bbox.x0, i->x);
51                         bbox.y0 = min (bbox.y0, i->y);
52                         bbox.x1 = max (bbox.x1, i->x);
53                         bbox.y1 = max (bbox.y1, i->y);
54                         ++i;
55                 }
56
57                 _bounding_box = bbox.expand (_outline_width / 2);
58
59                 
60         } else {
61                 _bounding_box = boost::optional<Rect> ();
62         }
63         
64         _bounding_box_dirty = false;
65 }
66
67 void
68 PolyItem::render_path (Rect const & /* area */, Cairo::RefPtr<Cairo::Context> context) const
69 {
70         if (_points.size() < 2) {
71                 return;
72         }
73
74         Points::const_iterator i = _points.begin();
75         Duple c (item_to_window (Duple (i->x, i->y)));
76         const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
77
78         context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
79         ++i;
80
81         while (i != _points.end()) {
82                 c = item_to_window (Duple (i->x, i->y));
83                 context->line_to (c.x + pixel_adjust, c.y + pixel_adjust);
84                 ++i;
85         }
86 }
87
88 void
89 PolyItem::render_curve (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Points const & first_control_points, Points const & second_control_points) const
90 {
91         if (_points.size() <= 2) {
92                 render_path (area, context);
93                 return;
94         }
95
96         Points::const_iterator cp1 = first_control_points.begin();
97         Points::const_iterator cp2 = second_control_points.begin();
98         Points::const_iterator p = _points.begin();
99         const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
100
101         Duple c = item_to_window (Duple (p->x, p->y));
102         context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
103         ++p;
104
105         while (p != _points.end()) {
106
107                 Duple c1 = item_to_window (Duple (cp1->x, cp1->y));
108                 Duple c2 = item_to_window (Duple (cp2->x, cp2->y));
109
110                 c = item_to_window (Duple (p->x, p->y));
111                 
112                 context->curve_to (c1.x + pixel_adjust, 
113                                    c1.y + pixel_adjust, 
114                                    c2.x + pixel_adjust, 
115                                    c2.y + pixel_adjust, 
116                                    c.x + pixel_adjust, 
117                                    c.y + pixel_adjust);
118                 
119                 ++cp1;
120                 ++cp2;
121                 ++p;
122         }
123 }
124
125 void
126 PolyItem::set (Points const & points)
127 {
128         begin_change ();
129         
130         _points = points;
131         
132         _bounding_box_dirty = true;
133         end_change ();
134 }
135
136 Points const &
137 PolyItem::get () const
138 {
139         return _points;
140 }
141
142 void
143 PolyItem::dump (ostream& o) const
144 {
145         Item::dump (o);
146
147         o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
148         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
149                 o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
150         }
151 }