versor  3.0
C++11 library for Geometric algebra
vsr_interp.h
1  /*
2  * vsr_interp.h
3  * CONGA_07
4  *
5  * Created by x on 5/29/11.
6  * Copyright 2011 x. All rights reserved.
7  *
8  */
9 
10 #ifndef VSR_INTERP_INCLUDED
11 #define VSR_INTERP_INCLUDED
12 
13 
14 using namespace std;
15 
16 namespace vsr {
17 
18  enum Interpolation {
19  LINEAR,
20  QUADRIC,
21  CUBIC,
22  BSPLINE,
23  NURB,
24  };
25 
26  // template <typename T>
27  class Interp {
28 
29  public:
30 
32  template<typename T>
33  static T cubic(T _a, T _b, T _c, T _d, double t);
35  template<typename T>
36  static T quadric(T _a, T _b, T _c, double t);
38  template<typename T>
39  static T linear(T _a, T _b, double t);
41  template<typename T>
42  static T sqlinear(T _a, T _b, double t);
44  template<typename T>
45  static T linear( T * s, int num, double t);
46 
48  template<typename T>
49  static T quadric(T * s, int num, double t, bool closed =0);
50 
52  template<typename T>
53  static T cubic(T * s, int num, double t);
54 
58  template<typename T>
59  static T surface(T *s, double u, double v);
60 
67  template<typename T>
68  static T surface(T a, T b, T c, T d, double u, double v);
69 
70  template<typename T>
71  static T sqsurface(T a, T b, T c, T d, double u, double v);
72 
73 
75  template<typename T>
76  static T volume( T a, T b, T c, T d, T e, T f, T g, T h, double u, double v, double w);
78  template<typename T>
79  static T volume(T *s, double x, double y, double z);
80 
81 // template<typename T>
82 // static vector<double> svals(T a, T b, T c, T d, double u, double v);
83 //
84 // template<typename T>
85 // static vector<double> svals(T a, T b, T c, T d, double u, double v);
86 
87  };
88 
89  template<typename T>
90  inline T Interp :: cubic(T _a, T _b, T _c, T _d, double t) {
91 
92  //get three states describing HULL
93  T a = _b - _a;
94  T b = _c - _b;
95  T c = _d - _c;
96 
97  //multiply each by p
98  T a2 = _a + a * t;
99  T b2 = _b + b * t;
100  T c2 = _c + c * t;
101 
102  //get second order translation states
103  a = b2 - a2;
104  b = c2 - b2;
105 
106  a2 += a * t;
107  b2 += b * t;
108 
109  //get third order (cubic)
110  a = b2 - a2;
111  a2 += a * t;
112 
113  return a2;
114  }
115 
116  //return state
117  template<typename T>
118  inline T Interp :: quadric(T _a, T _b, T _c, double t) {
119 
120 
121  //remap from 0 - 1 to -1 to 1
122  double nt = -1.0 + 2.0*t;
123  // cout << " nt: " << nt << "\n";
124 
125  //get three states describing HULL
126  double p = nt * nt;
127 
128  T a = ( ( (_a + _c) * .5 ) - _b ) * p;
129  T b = (_c - _a ) * (.5 * nt);
130  T c = a + b + _b;
131 
132  return c;
133  }
134 
135  template<typename T>
136  inline T Interp :: linear(T _a, T _b, double t) {
137  return _a * (1-t) + _b * (t);
138  }
139  template<typename T>
140  inline T Interp :: sqlinear(T _a, T _b, double t) {
141  return _a * ((1-t)*(1-t)) + _b * (t*t);
142  }
143 
144  template<typename T>
145  inline T Interp :: linear(T * s, int num, double t) {
146  double fw = t * (num-1);
147  int iw = floor(fw);
148 
149  double rw = fw - iw;
150  if (iw == num-1) { iw = num-2; rw = 1.0; }
151 
152  return s[iw] * (1.0 - rw) + s[iw+1] * rw;
153  }
154 
155  template<typename T>
156  inline T Interp :: quadric( T * cp, int num, double t, bool closed ) {
157 
158  // Make legal
159  if (t < 0 ) t = 0;
160 
161  //
162  double n = closed ? num / 2.0 : (num-1)/2.0;
163 
164  int fn = floor(n); // number of 3 group sections
165  double rem = n - fn; // remainder ( 0 or .5 )
166 
167  double td = ( t * n ); //fn?
168  int it = floor(td); //current group
169 
170  double ct = td - it; //current position in group (0-1)
171 
172  int gt = it * 2;
173 
174  if ( (rem != 0) && (it==fn) ) {
175  return closed? linear( cp[gt], cp[0], ct *2 ) : linear( cp[gt], cp[gt+1], ct *2 );
176  }
177  else{
178  return closed ? quadric(cp[gt], cp[gt+1], (it==fn-1)? cp[0] : cp[gt+2], ct) : quadric(cp[gt], cp[gt+1], cp[gt+2], ct);
179  }
180 
181  }
182 
183  //d c
184  //a b
185  template<typename T>
186  inline T Interp :: surface(T * cp, double u, double v){
187  T bot = cp[0] * (1-u) + cp[1] * u;
188  T top = cp[2] * (1-u) + cp[3] * u;
189  return bot * (1-v) + top * v;
190  }
191 
192  template<typename T>
193  inline T Interp :: surface(T a, T b, T c, T d, double u, double v){
194  T bot = a * (1-u) + b * u;
195  T top = d * (1-u) + c * u;
196  return bot * (1-v) + top * v;
197  }
198 
199  template<typename T>
200  inline T Interp :: sqsurface(T a, T b, T c, T d, double u, double v){
201  T bot = a * ( (1-u) * (1-u) ) + b * (u*u);
202  T top = d * ( (1-u)* (1-u) ) + c * (u*u);
203  return bot * ( (1-v) * (1-v) ) + top * (v*v);
204  }
205 
206 // template<typename T>
207 // inline T Interp :: quadricSurface(T a, T b, T c, T d, double u, double v){
208 // T bot = a * ( (1-u) * (1-u) ) + b * (u*u);
209 // T top = d * ( (1-u)* (1-u) ) + c * (u*u);
210 // return bot * ( (1-v) * (1-v) ) + top * (v*v);
211 // }
212 
213 // template<>
214 // inline Frame Interp :: surface<Frame>(Frame a, Frame b, Frame c, Frame d, double u, double v){
215 // Dll bot = a.dll() * (1-u) + b.dll() * u;
216 // Dll top = d.dll() * (1-u) + c.dll() * u;
217 // return Frame( Gen::mot ( bot * (1-v) + top * v );
218 // }
219 
220  //d3 c2 h7 g6
221  //a0 b1 <- front e4 f5 <- back
222 
223  //2 6 3 7
224  //0 4 1 5
225  template<typename T>
226  inline T Interp :: volume(T * cp, double u, double v, double w){
227  T fbottom = cp[0] * (1-u) + cp[4] * u;
228  T ftop = cp[2] * (1-u) + cp[6] * u;
229  T fmid = fbottom * (1-v) + ftop * (v);
230 
231  T bbottom = cp[1] * (1-u) + cp[5] * u;
232  T btop = cp[3] * (1-u) + cp[7] * u;
233  T bmid = bbottom * (1-v) + btop * (v);
234 
235  return fmid * (1-w) + bmid * w;
236  }
237 
238  template<typename T>
239  inline T Interp :: volume( T a, T b, T c, T d, T e, T f, T g, T h, double u, double v, double w){
240  T front = surface(a, b, c, d, u, v);
241  T back = surface(e, f, g, h, u, v);
242 
243  return linear(front, back, w);
244  }
245 
246 // template<typename T>
247 // inline T Interp :: svals(T a, T b, T c, T d, double u, double v){
248 // T bot = a * (1-u) + b * u;
249 // T top = d * (1-u) + c * u;
250 // return bot * (1-v) + top * v;
251 // }
252 
253 }
254 #endif
Definition: vsr_interp.h:27
the versor library namespace
Definition: vsr_algebra.h:29