[go: up one dir, main page]

Menu

[e79954]: / geometry / Spline.h  Maximize  Restore  History

Download this file

187 lines (138 with data), 5.2 kB

  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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2001--2004 Sam Varner
//
// This file is part of Vamos Automotive Simulator.
//
// Vamos is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Vamos is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Vamos. If not, see <http://www.gnu.org/licenses/>.
#ifndef _SPLINE_H_
#define _SPLINE_H_
#include "Interpolator.h"
#include "Three_Vector.h"
#include "Two_Vector.h"
#include <vector>
namespace Vamos_Geometry
{
class Spline : public Interpolator
{
public:
// Construct an empty curve.
Spline ();
Spline (double first_slope, double last_slope);
// Construct a cuvre from an array of points.
Spline (const std::vector <Two_Vector>& points);
Spline (const std::vector <Two_Vector>& points,
double first_slope,
double last_slope);
// Add a point to the curve.
virtual void load (const Two_Vector& point);
virtual void load (double x, double y) { load (Two_Vector (x, y)); }
// Add multiple points to the curve.
virtual void load (const std::vector <Two_Vector>& points);
// Replace all points on the curve.
virtual void replace (const std::vector <Two_Vector>& points)
{
clear ();
load (points);
}
// Remove all points from the curve.
virtual void clear ();
void set_periodic (double period_end);
// Remove points with x > LIMIT.
virtual void remove_greater (double limit);
// Scale all of the x values by FACTOR.
virtual void scale (double factor);
// Return the y value at the x value DIST
virtual double interpolate (double dist) const;
double slope (double distance) const;
double second_derivative (double distance) const;
// Return the normal to the tanget at DIST.
virtual Two_Vector normal (double dist) const;
// Add 'delta' to all points.
virtual void shift (double delta);
// Return the number of control points.
virtual size_t size () const { return m_points.size (); }
virtual Two_Vector& operator [] (size_t i) { return m_points [i]; }
virtual const Two_Vector& operator [] (size_t i) const { return m_points [i]; }
private:
// The array of calculated second derivatives.
mutable std::vector <double> m_second_deriv;
// The first derivative of the spline at the first point.
double m_first_slope;
bool m_first_slope_is_set;
// The first derivative of the spline at the last point.
double m_last_slope;
bool m_last_slope_is_set;
// True if the second derivatives have been calculated.
mutable bool m_calculated;
// The 1st and 2nd derivatives at the interpolated point
// calculated during the last call to interpolate().
mutable double m_slope;
mutable double m_second_derivative;
// Calculate the coefficients for interpolation.
void calculate () const;
// The segment index from the previous interpolation.
mutable size_t m_last_index;
double m_period_end;
bool m_periodic;
void check (double* a, double* b, double* r) const;
};
class Parametric_Spline
{
public:
Parametric_Spline ();
Parametric_Spline (double first_x_slope, double last_x_slope,
double first_y_slope, double last_y_slope);
// Add a point to the curve.
void load (double parameter, const Two_Vector& point);
void load (double parameter, double x, double y)
{ load (parameter, Two_Vector (x, y)); }
// Remove all points from the curve.
void clear ();
void set_periodic (double period_end);
// Return the point at PARAMETER.
Two_Vector interpolate (double parameter) const;
// Return the number of control points.
size_t size () const;
Two_Vector operator [] (size_t i) const;
double parameter (size_t i) const;
private:
Spline m_x;
Spline m_y;
};
class Vector_Spline
{
public:
Vector_Spline ();
Vector_Spline (double first_x_slope, double last_x_slope,
double first_y_slope, double last_y_slope,
double first_z_slope, double last_z_slope);
// Add a point to the curve.
void load (double parameter, const Vamos_Geometry::Three_Vector& point);
void load (double parameter, double x, double y, double z)
{ load (parameter, Three_Vector (x, y, z)); }
// Remove all points from the curve.
void clear ();
void set_periodic (double period_end);
// Return the point at PARAMETER.
Vamos_Geometry::Three_Vector interpolate (double parameter) const;
// Return the number of control points.
size_t size () const;
Vamos_Geometry::Three_Vector operator [] (size_t i) const;
double parameter (size_t i) const;
private:
Spline m_x;
Spline m_y;
Spline m_z;
};
}
#endif