Ardour  8.7-15-gadf511264b
interpolated_curve.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef __CANVAS_INTERPOLATED_CURVE_H__
20 #define __CANVAS_INTERPOLATED_CURVE_H__
21 
22 #include "canvas/types.h"
23 #include "canvas/visibility.h"
24 
25 namespace ArdourCanvas {
26 
28 {
29 public:
30  enum SplineType {
33  };
34 
35 protected:
36 
55  static void
56  interpolate (const Points& coordinates, uint32_t points_per_segment, SplineType curve_type, bool closed, Points& results)
57  {
58  if (points_per_segment < 2) {
59  return;
60  }
61 
62  // Cannot interpolate curves given only two points. Two points
63  // is best represented as a simple line segment.
64  if (coordinates.size() < 3) {
65  results = coordinates;
66  return;
67  }
68 
69  // Copy the incoming coordinates. We need to modify it during interpolation
70  Points vertices = coordinates;
71 
72  // Test whether the shape is open or closed by checking to see if
73  // the first point intersects with the last point. M and Z are ignored.
74  if (closed) {
75  // Use the second and second from last points as control points.
76  // get the second point.
77  Duple p2 = vertices[1];
78  // get the point before the last point
79  Duple pn1 = vertices[vertices.size() - 2];
80 
81  // insert the second from the last point as the first point in the list
82  // because when the shape is closed it keeps wrapping around to
83  // the second point.
84  vertices.insert(vertices.begin(), pn1);
85  // add the second point to the end.
86  vertices.push_back(p2);
87  } else {
88  // The shape is open, so use control points that simply extend
89  // the first and last segments
90 
91  // Get the change in x and y between the first and second coordinates.
92  double dx = vertices[1].x - vertices[0].x;
93  double dy = vertices[1].y - vertices[0].y;
94 
95  // Then using the change, extrapolate backwards to find a control point.
96  double x1 = vertices[0].x - dx;
97  double y1 = vertices[0].y - dy;
98 
99  // Actually create the start point from the extrapolated values.
100  Duple start (x1, y1);
101 
102  // Repeat for the end control point.
103  int n = vertices.size() - 1;
104  dx = vertices[n].x - vertices[n - 1].x;
105  dy = vertices[n].y - vertices[n - 1].y;
106  double xn = vertices[n].x + dx;
107  double yn = vertices[n].y + dy;
108  Duple end (xn, yn);
109 
110  // insert the start control point at the start of the vertices list.
111  vertices.insert (vertices.begin(), start);
112 
113  // append the end control ponit to the end of the vertices list.
114  vertices.push_back (end);
115  }
116 
117  // When looping, remember that each cycle requires 4 points, starting
118  // with i and ending with i+3. So we don't loop through all the points.
119 
120  for (Points::size_type i = 0; i < vertices.size() - 3; i++) {
121 
122  // Actually calculate the Catmull-Rom curve for one segment.
123  Points r;
124 
125  _interpolate (vertices, i, points_per_segment, curve_type, r);
126 
127  // Since the middle points are added twice, once for each bordering
128  // segment, we only add the 0 index result point for the first
129  // segment. Otherwise we will have duplicate points.
130 
131  if (results.size() > 0) {
132  r.erase (r.begin());
133  }
134 
135  // Add the coordinates for the segment to the result list.
136 
137  results.insert (results.end(), r.begin(), r.end());
138  }
139  }
140 
141 private:
154  static double
155  __interpolate (double p[4], double time[4], double t)
156  {
157  const double L01 = p[0] * (time[1] - t) / (time[1] - time[0]) + p[1] * (t - time[0]) / (time[1] - time[0]);
158  const double L12 = p[1] * (time[2] - t) / (time[2] - time[1]) + p[2] * (t - time[1]) / (time[2] - time[1]);
159  const double L23 = p[2] * (time[3] - t) / (time[3] - time[2]) + p[3] * (t - time[2]) / (time[3] - time[2]);
160  const double L012 = L01 * (time[2] - t) / (time[2] - time[0]) + L12 * (t - time[0]) / (time[2] - time[0]);
161  const double L123 = L12 * (time[3] - t) / (time[3] - time[1]) + L23 * (t - time[1]) / (time[3] - time[1]);
162  const double C12 = L012 * (time[2] - t) / (time[2] - time[1]) + L123 * (t - time[1]) / (time[2] - time[1]);
163  return C12;
164  }
165 
184  static void
185  _interpolate (const Points& points, Points::size_type index, int points_per_segment, SplineType curve_type, Points& results)
186  {
187  double x[4];
188  double y[4];
189  double time[4];
190 
191  for (int i = 0; i < 4; i++) {
192  x[i] = points[index + i].x;
193  y[i] = points[index + i].y;
194  time[i] = i;
195  }
196 
197  double tstart = 1;
198  double tend = 2;
199 
200  if (curve_type != CatmullRomUniform) {
201  double total = 0;
202  for (int i = 1; i < 4; i++) {
203  double dx = x[i] - x[i - 1];
204  double dy = y[i] - y[i - 1];
205  if (curve_type == CatmullRomCentripetal) {
206  total += pow (dx * dx + dy * dy, .25);
207  } else {
208  total += pow (dx * dx + dy * dy, .5);
209  }
210  time[i] = total;
211  }
212  tstart = time[1];
213  tend = time[2];
214  }
215 
216  int segments = points_per_segment - 1;
217  results.push_back (points[index + 1]);
218 
219  for (int i = 1; i < segments; i++) {
220  double xi = __interpolate (x, time, tstart + (i * (tend - tstart)) / segments);
221  double yi = __interpolate (y, time, tstart + (i * (tend - tstart)) / segments);
222  results.push_back (Duple (xi, yi));
223  }
224 
225  results.push_back (points[index + 2]);
226  }
227 };
228 
229 }
230 
231 #endif
#define LIBCANVAS_API
static void _interpolate(const Points &points, Points::size_type index, int points_per_segment, SplineType curve_type, Points &results)
static void interpolate(const Points &coordinates, uint32_t points_per_segment, SplineType curve_type, bool closed, Points &results)
static double __interpolate(double p[4], double time[4], double t)
PBD::PropertyDescriptor< timepos_t > start
std::vector< Duple > Points