Merge branch 'canvasredesign' into cairocanvas
[ardour.git] / libs / canvas / curve.cc
1 /*
2     Copyright (C) 2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21 #include <exception>
22 #include <algorithm>
23
24 #include "canvas/curve.h"
25
26 using namespace ArdourCanvas;
27 using std::min;
28 using std::max;
29
30 Curve::Curve (Canvas* c)
31         : PolyItem (c)
32         , n_samples (0)
33         , points_per_segment (16)
34         , curve_type (CatmullRomCentripetal)
35         , curve_fill (None)
36 {
37 }
38
39 Curve::Curve (Item* parent)
40         : PolyItem (parent)
41         , n_samples (0)
42         , points_per_segment (16)
43         , curve_type (CatmullRomCentripetal)
44         , curve_fill (None)
45 {
46 }
47
48 /** When rendering the curve, we will always draw a fixed number of straight
49  * line segments to span the x-axis extent of the curve. More segments:
50  * smoother visual rendering. Less rendering: closer to a visibily poly-line
51  * render.
52  */
53 void
54 Curve::set_points_per_segment (uint32_t n)
55 {
56         /* this only changes our appearance rather than the bounding box, so we
57            just need to schedule a redraw rather than notify the parent of any
58            changes
59         */
60         points_per_segment = n;
61         interpolate ();
62         redraw ();
63 }
64
65 void
66 Curve::compute_bounding_box () const
67 {
68         PolyItem::compute_bounding_box ();
69
70         /* possibly add extents of any point indicators here if we ever do that */
71 }
72
73 void
74 Curve::set (Points const& p)
75 {
76         PolyItem::set (p);
77         interpolate ();
78 }
79
80 void
81 Curve::interpolate ()
82 {
83         samples.clear ();
84         InterpolatedCurve::interpolate (_points, points_per_segment, CatmullRomCentripetal, false, samples);
85         n_samples = samples.size();
86 }
87
88 void
89 Curve::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
90 {
91         if (!_outline || _points.size() < 2 || !_bounding_box) {
92                 return;
93         }
94
95         Rect self = item_to_window (_bounding_box.get());
96         boost::optional<Rect> d = self.intersection (area);
97         assert (d);
98         Rect draw = d.get ();
99
100         /* Our approach is to always draw n_segments across our total size.
101          *
102          * This is very inefficient if we are asked to only draw a small
103          * section of the curve. For now we rely on cairo clipping to help
104          * with this.
105          */
106         
107
108         setup_outline_context (context);
109
110         if (_points.size() == 2) {
111
112                 /* straight line */
113
114                 Duple window_space;
115
116                 window_space = item_to_window (_points.front());
117                 context->move_to (window_space.x, window_space.y);
118                 window_space = item_to_window (_points.back());
119                 context->line_to (window_space.x, window_space.y);
120
121
122                 switch (curve_fill) {
123                         case None:
124                                 context->stroke();
125                                 break;
126                         case Inside:
127                                 context->stroke_preserve ();
128                                 window_space = item_to_window (Duple(_points.back().x, draw.height()));
129                                 context->line_to (window_space.x, window_space.y);
130                                 window_space = item_to_window (Duple(_points.front().x, draw.height()));
131                                 context->line_to (window_space.x, window_space.y);
132                                 context->close_path();
133                                 setup_fill_context(context);
134                                 context->fill ();
135                                 break;
136                         case Outside:
137                                 context->stroke_preserve ();
138                                 window_space = item_to_window (Duple(_points.back().x, 0.0));
139                                 context->line_to (window_space.x, window_space.y);
140                                 window_space = item_to_window (Duple(_points.front().x, 0.0));
141                                 context->line_to (window_space.x, window_space.y);
142                                 context->close_path();
143                                 setup_fill_context(context);
144                                 context->fill ();
145                                 break;
146                 }
147
148         } else {
149
150                 /* curve of at least 3 points */
151
152                 /* x-axis limits of the curve, in window space coordinates */
153
154                 Duple w1 = item_to_window (Duple (_points.front().x, 0.0));
155                 Duple w2 = item_to_window (Duple (_points.back().x, 0.0));
156
157                 /* clamp actual draw to area bound by points, rather than our bounding box which is slightly different */
158
159                 context->save ();
160                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
161                 context->clip ();
162
163                 /* expand drawing area by several pixels on each side to avoid cairo stroking effects at the boundary.
164                    they will still occur, but cairo's clipping will hide them.
165                  */
166
167                 draw = draw.expand (4.0);
168
169                 /* now clip it to the actual points in the curve */
170                 
171                 if (draw.x0 < w1.x) {
172                         draw.x0 = w1.x;
173                 }
174
175                 if (draw.x1 >= w2.x) {
176                         draw.x1 = w2.x;
177                 }
178
179                 /* find left and right-most sample */
180                 Duple window_space;
181                 Points::size_type left = 0;
182                 Points::size_type right = n_samples;
183
184                 for (Points::size_type idx = 0; idx < n_samples - 1; ++idx) {
185                         left = idx;
186                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
187                         if (window_space.x >= draw.x0) break;
188                 }
189                 for (Points::size_type idx = n_samples; idx > left + 1; --idx) {
190                         window_space = item_to_window (Duple (samples[idx].x, 0.0));
191                         if (window_space.x <= draw.x1) break;
192                         right = idx;
193                 }
194
195                 /* draw line between samples */
196                 window_space = item_to_window (Duple (samples[left].x, samples[left].y));
197                 context->move_to (window_space.x, window_space.y);
198                 for (uint32_t idx = left + 1; idx < right; ++idx) {
199                         window_space = item_to_window (Duple (samples[idx].x, samples[idx].y));
200                         context->line_to (window_space.x, window_space.y);
201                 }
202
203                 switch (curve_fill) {
204                         case None:
205                                 context->stroke();
206                                 break;
207                         case Inside:
208                                 context->stroke_preserve ();
209                                 window_space = item_to_window (Duple (samples[right-1].x, draw.height()));
210                                 context->line_to (window_space.x, window_space.y);
211                                 window_space = item_to_window (Duple (samples[left].x, draw.height()));
212                                 context->line_to (window_space.x, window_space.y);
213                                 context->close_path();
214                                 setup_fill_context(context);
215                                 context->fill ();
216                                 break;
217                         case Outside:
218                                 context->stroke_preserve ();
219                                 window_space = item_to_window (Duple (samples[right-1].x, 0.0));
220                                 context->line_to (window_space.x, window_space.y);
221                                 window_space = item_to_window (Duple (samples[left].x, 0.0));
222                                 context->line_to (window_space.x, window_space.y);
223                                 context->close_path();
224                                 setup_fill_context(context);
225                                 context->fill ();
226                                 break;
227                 }
228                 context->restore ();
229         }
230
231 #if 0
232         /* add points */
233         setup_outline_context (context);
234         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
235                 Duple window_space (item_to_window (*p));
236                 context->arc (window_space.x, window_space.y, 5.0, 0.0, 2 * M_PI);
237                 context->stroke ();
238         }
239 #endif
240 }
241
242 bool
243 Curve::covers (Duple const & pc) const
244 {
245         Duple point = window_to_item (pc);
246
247         /* O(N) N = number of points, and not accurate */
248
249         for (Points::const_iterator p = _points.begin(); p != _points.end(); ++p) {
250
251                 const Coord dx = point.x - (*p).x;
252                 const Coord dy = point.y - (*p).y;
253                 const Coord dx2 = dx * dx;
254                 const Coord dy2 = dy * dy;
255
256                 if ((dx2 < 2.0 && dy2 < 2.0) || (dx2 + dy2 < 4.0)) {
257                         return true;
258                 }
259         }
260
261         return false;
262 }