-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspline.cpp
executable file
·118 lines (108 loc) · 4.51 KB
/
spline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Copyright ©2013 The Regents of the University of California
(Regents). All Rights Reserved. Permission to use, copy, modify, and
distribute this software and its documentation for educational,
research, and not-for-profit purposes, without fee and without a
signed licensing agreement, is hereby granted, provided that the
above copyright notice, this paragraph and the following two
paragraphs appear in all copies, modifications, and
distributions. Contact The Office of Technology Licensing, UC
Berkeley, 2150 Shattuck Avenue, Suite 510, Berkeley, CA 94720-1620,
(510) 643-7201, for commercial licensing opportunities.
IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
DOCUMENTATION, EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING
DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS
IS". REGENTS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "spline.hpp"
#include "util.hpp"
using namespace std;
// binary search, returns keyframe immediately *after* given time
// range of output: 0 to a.keyfs.size() inclusive
template<typename T>
static int find (const Spline<T> &s, double t) {
int l = 0, u = s.points.size();
while (l != u) {
int m = (l + u)/2;
if (t < s.points[m].t) u = m;
else l = m + 1;
}
return l; // which is equal to u
}
template<typename T>
T Spline<T>::pos (double t) const {
int i = find(*this, t);
std::cout << "inside pos func, i:" << i << " " << points.size() << std::endl;
if (i == 0) {
const Point &p1 = points[i];
return p1.x;
} else if (i == points.size()) {
const Point &p0 = points[i-1];
return p0.x;
} else {
// interpolation
const Point &p0 = points[i-1], &p1 = points[i];
// return p0.x;
double s = (t - p0.t)/(p1.t - p0.t), s2 = s*s, s3 = s2*s;
// std::cout << "p0, p1t t " << p0.t << " " <<p1.t << " " << t << std::endl;
// std::cout << "s, s2, s3 " << s << " " << s2 << " " << s3 << std::endl;
//
// T tmp21 = p0.x*(2*s3 - 3*s2 + 1) + p1.x*(-2*s3 + 3*s2);
// std::cout << "tmp21 obtained" << std::endl;
// T tmp22 = p0.v*(s3 - 2*s2 + s);
// std::cout << "tmp22 obtained" << std::endl;
// T tmp23 = p1.v*(s3 - s2);
// std::cout << "tmp23 obtained" << std::endl;
// T tmp24 = (p0.v*(s3 - 2*s2 + s) + p1.v*(s3 - s2))*(p1.t - p0.t);
// T tmp25 = p0.x*(2*s3 - 3*s2 + 1) + p1.x*(-2*s3 + 3*s2);
// std::cout << "tmp24 obtained" << std::endl;
// T tmp26 = tmp24 + tmp25;
// std::cout << "tmp24 obtained" << std::endl;
T tmp2 = p0.x*(2*s3 - 3*s2 + 1) + p1.x*(-2*s3 + 3*s2)
+ (p0.v*(s3 - 2*s2 + s) + p1.v*(s3 - s2))*(p1.t - p0.t);
// std::cout << "tmp2 obtained" << std::endl;
return tmp2;
// return p0.x*(2*s3 - 3*s2 + 1) + p1.x*(-2*s3 + 3*s2);
// don't consider the velocity term
// return p0.x * (1 - s) + p1.x * s;
}
}
template <typename T>
T Spline<T>::vel (double t) const {
int i = find(*this, t);
if (i == 0 || i == points.size()) {
return T(0);
} else {
const Point &p0 = points[i-1], &p1 = points[i];
double s = (t - p0.t)/(p1.t - p0.t), s2 = s*s;
return (p0.x*(6*s2 - 6*s) + p1.x*(-6*s2 + 6*s))/(p1.t - p0.t)
+ p0.v*(3*s2 - 4*s + 1) + p1.v*(3*s2 - 2*s);
}
}
vector<double> operator+ (const vector<double> &x, const vector<double> &y) {
vector<double> z(min(x.size(), y.size()));
for (int i = 0; i < z.size(); i++) z[i] = x[i] + y[i];
return z;
}
vector<double> operator- (const vector<double> &x, const vector<double> &y) {
vector<double> z(min(x.size(), y.size()));
for (int i = 0; i < z.size(); i++) z[i] = x[i] - y[i];
return z;
}
vector<double> operator* (const vector<double> &x, double a) {
vector<double> y(x.size());
for (int i = 0; i < y.size(); i++) y[i] = x[i]*a;
return y;
}
vector<double> operator/ (const vector<double> &x, double a) {return x*(1/a);}
template class Spline<Vec3>;
template class Spline<Transformation>;
template class Spline<double>;
template class Spline< vector<double> >;