Jamoma API  0.6.0.a19
Catmullrom3D.cpp
1 /*
2  * Bspline Function Unit for TTBlue
3  * based on the icst.spline code by Jan Schacher, ICST: http://www.icst.net/
4  * ported by Nils Peters
5  *
6  */
7 
8 #include "Catmullrom3D.h"
9 
10 
11 #define thisTTClass Catmullrom3D
12 #define thisTTClassName "spline.catmullrom.3D"
13 #define thisTTClassTags "audio, trajectory, 3D, spline"
14 
15 
16 TT_AUDIO_CONSTRUCTOR
17 { addAttribute(Closed, kTypeBoolean); // whether the trajectory is open or closed
18  //addAttribute(A, kTypeFloat64);
19  //addAttribute(Degree, kTypeUInt8);
20  addAttribute(Steps, kTypeUInt16);
21  //addAttribute(Dimen, kTypeUInt8);
22  addAttribute(Resolution, kTypeUInt16);//was size
23  setProcessMethod(processAudio);
24 // setCalculateMethod(calculateValue);
25 }
26 
27 
28 
29 
30 Catmullrom3D::~Catmullrom3D()
31 {
32  ;
33 }
34 
35 //TTErr Catmullrom3D::calculateValue(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt data)
36 //{
37 // y = x;
38 // return kTTErrNone;
39 //}
40 
41 void Catmullrom3D::interpolate(t_spline *x, t_point pt0, t_point pt1, t_point pt2, t_point pt3)
42 {
43 
44  double s, s2, s3;
45  double m0, m1, m2, m3;
46  double a0, a1, a2, a3;
47 
48 
49 s2 = s * s;
50 s3 = s2 * s;
51 m0 = 2.0 * s3 - 3.0 * s2 + 1.0;
52 m1 = -2.0 * s3 + 3.0 * s2;
53 m2 = s3 - 2 * s2 + s;
54 m3 = s3 - s2;
55 
56 a0 = 0.5 * ( pt2.x - pt0.x );
57 a1 = 0.5 * ( pt3.x - pt1.x );
58 x->b_result[0].a_w.w_float = (m0 * pt1.x + m1 * pt2.x + m2 * a0 + m3 * a1);
59 
60 a0 = 0.5 * ( pt2.y - pt0.y );
61 a1 = 0.5 * ( pt3.y - pt1.y );
62 x->b_result[1].a_w.w_float = (m0 * pt1.y + m1 * pt2.y + m2 * a0 + m3 * a1);
63 
64 a0 = 0.5 * ( pt2.z - pt0.z);
65 a1 = 0.5 * ( pt3.z - pt1.z);
66 x->b_result[2].a_w.w_float = (m0 * pt1.z + m1 * pt2.z + m2 * a0 + m3 * a1);
67 }
68 
69 
70 void Catmullrom3D::spline_exec(t_spline *x)
71 {
72  long i;
73  double inc;
74 
75  t_point delta;
76 
77 spline_make_endpoints(x); // prep for hermite etc: calculate helper points
78 spline_stepthrough(x);
79 x->b_result[0].a_w.w_float = x->b_control[x->b_size - 1].x; // output the last point
80 x->b_result[1].a_w.w_float = x->b_control[x->b_size - 1].y;
81 x->b_result[2].a_w.w_float = x->b_control[x->b_size - 1].z;
82 outlet_list(x->b_outlet1, 0L, x->b_dim, x->b_result);
83 outlet_bang(x->b_outlet2);
84 }
85 
86 
87 
88 
89 
90 /*void Bspline2D::calculatePoint()
91 {
92  TTUInt16 i;
93  TTFloat64 temp;
94 
95  point3D[0] = 0;//x
96  point3D[1] = 0;//y
97  point3D[2] = 0;//z
98 
99  for ( i = 0; i <= mResolution; i++) {
100  temp = calculateBlend(i, mDegree); // same blend is used for each dimension coordinate
101  b_op[0] += b_control[i*3] * temp;
102  b_op[1] += b_control[i*3+1] * temp;
103  b_op[2] += b_control[i*3+2] * temp;
104  }
105 }
106 
107 float Bspline2D::calculateBlend(TTUInt16 k, TTUInt16 t)
108 {
109  TTFloat64 value;
110  TTFloat64 v = interval;
111 
112  if (t == 1) { // base case for the recursion
113  if ((b_knots[k] <= v) && (v < b_knots[k+1])) {
114  value=1;
115  } else {
116  value=0;
117  }
118  } else {
119  if ((b_knots[k + t - 1] == b_knots[k]) && (b_knots[k + t] == b_knots[k + 1])) { // check for divide by zero
120  value = 0;
121  } else if (b_knots[k + t - 1] == b_knots[k]) { // if a term's denominator is zero, use just the other
122  value = (b_knots[k + t] - v) / (b_knots[k + t] - b_knots[k + 1]) * calculateBlend(k + 1, t - 1);
123  } else if (b_knots[k + t] == b_knots[k + 1]) {
124  value = (v - b_knots[k]) / (b_knots[k + t - 1] - b_knots[k]) * calculateBlend(k, t - 1);
125  } else {
126  value = (v - b_knots[k]) / (b_knots[k + t - 1] - b_knots[k]) * calculateBlend(k, t - 1) +
127  (b_knots[k + t] - v) / (b_knots[k + t] - b_knots[k + 1]) * calculateBlend(k + 1, t - 1);
128  }
129  }
130  return value;
131 }
132 
133 void Bspline2D::calculateKnots() // generate knot-vector
134 {
135  TTUInt8 i, t, n;
136  n = mResolution;
137  t = mDegree;
138  for (i = 0; i <= (n + t); i++){
139  if (i < t) {
140  b_knots[i] = 0;
141  } else if ((t <= i) && (i <= n)) {
142  b_knots[i] = i - t + 1;
143  } else if (i > n) {
144  b_knots[i] = n - t + 2; // if n - t = -2 then we're screwed, everything goes to 0
145  }
146  }
147 } */
148 
149 TTErr Catmullrom3D::processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
150 {
151  TTAudioSignal& out = outputs->getSignal(0);
152  TTChannelCount numOutputChannels = out.getNumChannelsAsInt();
153 
154  if (numOutputChannels != 3) {
155  TTValue v = 3;
156  out.setMaxNumChannels(v);
157  out.setNumChannels(v);
158  }
159 
160  TTAudioSignal& in0 = inputs->getSignal(0);
161  TTUInt16 vs = in0.getVectorSizeAsInt();
162 
163  TTSampleValuePtr inSampleX = in0.mSampleVectors[0];
164  TTSampleValuePtr outSampleX = out.mSampleVectors[0];
165  TTSampleValuePtr outSampleY = out.mSampleVectors[1];
166  TTSampleValuePtr outSampleZ = out.mSampleVectors[2];
167  TTFloat64 f;
168 
169  for (int i=0; i<vs; i++) {
170 
171  f = inSampleX[i] * 0.5; //0... 1.
172 
173  if ((f > 0) && (f < 1)){
174  spline_eval(f);
175 
176  //calculatePoint(); // output intermediate point
177  outSampleX[i] = b_op[0];
178  outSampleY[i] = b_op[1];
179  outSampleZ[i] = b_op[2];
180  } else if (f <= 0.0){
181  // output the first point
182  } else if (f >= 1.0){
183  // output the last point
184  }
185  }
186 return kTTErrNone;
187 }
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
#define addAttribute(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter...
Definition: TTAttribute.h:29
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
16-bit unsigned integer, range is 0 through 65,535.
Definition: TTBase.h:276
TTErr setMaxNumChannels(const TTValue &newMaxNumChannels)
Attribute accessor.
Boolean (1/0) or (true/false) flag.
Definition: TTBase.h:281
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTUInt16 TTChannelCount
Data type used when counting the number of channels in multi-channel audio signals and processes...
Definition: TTAudioSignal.h:31
A simple container for an array of TTAudioSignal pointers.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34