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