ofxMSASpline – openFrameworks addon to interpolate any-dimensional data (2008)

A set of C++ template classes for doing various types of interpolations on data with any number of dimensions. You can feed the system an arbitrary number of data (data can be simple types like float, int or complex types like structs, classes), then resample at any resolution, or ask for the value at any percentage along the data – or just draw it – including splines in 3D.
This is useful for creating and drawing splines (in any dimensions), or creating smooth animation paths from keyframes (again, in any dimensions).

Made with openFrameworks.

Source code on SVN here


Example usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// e.g. declare a dataset of ofxVec3f (3d vector)
ofxMSASpline<ofxVec3f> data;
ofxMSASpline3D data; // ofxMSASpline3D is the same as ofxMSASpline<ofxVec3f>

// add some data (similar to std::vector>
data.push_back(myVector1);
data.push_back(myVector2);
data.push_back(myVector3);

// sample at any resolution and draw
glBegin(GL_LINE_STRIP);
for(float f=0; f<1; f+= spacing) {
ofxVec3f v = sampleAt(f);
glVertex3fv((const float*)&v);
}
glEnd();

More information can be found in the header file:

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
ofxMSASpline<float> mySpline1; // create spline of floats
ofxMSASpline<myDataType> mySpline2; // create spline of custom data types (more info below)

// OR use preset classes:

ofxMSASpline1D mySpline1D; // create spline of floats (1D)
ofxMSASpline2D mySpline2D; // create spline of ofxVec2f (2D)
ofxMSASpline3D mySpline3D; // create spline of ofxVec3f (3D)

// splines wrap basic functionality of stl::vector:
mySpline.size(); // return number of data elements
mySpline.reserve(int count); // if you know how many elements up front it will improved performance when adding (you can still add more than this number of elements)
mySpline.at(int i); // return data at i'th index
mySpline.clear(); // remove all elements
mySpline.push_back(data1); // add some data to the spline
mySpline.push_back(data2);

mySpline.sampleAt(float t); // (e.g. t:0.34 =>) samples along 34% of the whole spline using the current interpolation method and options

setInterpolation(i); // set interpolation type, see ofxMSASplineInterpolationTypes.h (currently cubic catmull rom and linear)
int getInterpolation(); // get interpolation type

setUseDistance(bool b); // whether to use distance or not. using distance is slightly slower than not using (depending on number of data points)
bool getUseDistance(); // if useDistance is true, sampleAt(0.57) means sample at 57% along the physical length of the spline (using the non interpolation spline for distance calculation)
// if useDistance is false, the %t refers to % along the data points. If data points are evenly spaced its no problem, but if they are randomly spaced, the interpolation will not be uniform

mySpline.drawRaw(int dotSize, int lineWidth); // draws raw data with dotSize and lineWidth (make either zero to not draw dots or lines)
mySpline.drawSmooth(int numSteps, int dotSize, int lineWidth); // draws smoothed data in (make either zero to not draw dots or lines)

Using custom data type:
ofxMSASpline<myDataType> mySpline2; // create spline of custom data types (more info below)
myDataType has to be a scalar or class with the overloaded operators:
+ (myDataType&)
- (myDataType&)
== (myDataType&)
= (myDataType&)
* (float)

and also overload the global function fabs(myDataType&) to return a scalar float value depicting the 'magnitude' of the data type (used in calculating distance)

A set of C++ template classes for doing various types of interpolations on data with any number of dimensions. You […]

Related keywords

open source, openframeworks