35 template <
class T1,
class T2,
class T3>
36 inline T3
indexOf(
const T1& arr, T3 sz,
const T2& val, T3 notfoundval )
38 for ( T3 idx=0; idx<sz; idx++ )
40 if ( arr[idx] == val )
50 template <
class T1,
class T2,
class T3>
51 inline T3
derefIndexOf(
const T1& arr, T3 sz,
const T2& val, T3 notfoundval )
53 for ( T3 idx=0; idx<sz; idx++ )
55 if ( *arr[idx] == val )
72 template <
class T1,
class T2,
class T3>
73 bool findPos(
const T1& posarr, T3 sz, T2 pos, T3 beforefirst, T3& idx )
76 if ( sz < 1 || pos < posarr[0] )
79 if ( pos == posarr[0] )
80 { idx = 0;
return true; }
81 else if ( pos > posarr[sz-1] || pos == posarr[sz-1] )
82 { idx = sz-1;
return pos == posarr[sz-1]; }
86 while ( idx2 - idx1 > 1 )
88 idx = (idx2 + idx1) / 2;
89 T2 arrval = posarr[idx];
90 if ( arrval == pos )
return true;
91 else if ( arrval > pos ) idx2 = idx;
96 return posarr[idx] == pos;
110 template <
class T1,
class T2,
class T3>
111 bool findFPPos(
const T1& posarr, T3 sz, T2 pos, T3 beforefirst, T3& idx,
115 if ( !sz )
return false;
116 if ( sz < 2 || pos <= posarr[0] )
119 { idx = 0;
return true; }
123 else if ( pos >= posarr[sz-1] )
124 { idx = sz-1;
return mIsEqual(pos,posarr[sz-1],eps); }
129 while ( idx2 - idx1 > 1 )
131 idx = (idx2 + idx1) / 2;
132 T2 diff = posarr[idx] - pos;
133 if (
mIsZero(diff,eps) )
return true;
134 else if ( diff > 0 ) idx2 = idx;
139 return mIsEqual(pos,posarr[idx],eps);
152 int idx;
findFPPos( x, sz, pos, -1, idx );
return idx;
169 const bool exactmatch =
findFPPos( x, sz, pos, -1, idx );
170 return idx < 1 ? 1 : ( exactmatch ? idx : idx+1 );
179 template <
class X,
class Y,
class RT>
182 RT& ret,
bool extrapolate=
false )
187 ret = extrapolate ? y[0] :
mUdf(
RT);
191 else if ( desiredx < x[0] || desiredx > x[sz-1] )
196 ret = desiredx < x[0]
203 int prevpos =
getLowIdx( x, sz, desiredx );
204 int nextpos = prevpos + 1;
208 x[nextpos], y[nextpos], desiredx );
212 { prevpos++; nextpos++; }
213 else if ( nextpos == sz-1 )
214 { prevpos--; nextpos--; }
217 x[prevpos], y[prevpos],
218 x[nextpos], y[nextpos],
219 x[nextpos+1], y[nextpos+1],
225 template <
class X,
class Y>
227 bool extrapolate=
false )
229 float ret =
mUdf(
float);
243 || (!extrap && (pos<-snapdist || (pos+offset)>sz-1+snapdist)) )
247 const float dist = pos - intpos;
249 if ( dist>-snapdist && dist<snapdist && intpos>-1 && intpos<sz )
250 { p[0] = intpos;
return 0; }
252 p[1] = dist > 0 ? intpos : intpos - 1;
253 if ( p[1] < 0 ) p[1] = 0;
254 if ( p[1] >= sz ) p[1] = sz - 1;
255 p[0] = p[1] < 1 ? p[1] : p[1] - 1;
256 p[2] = p[1] < sz-1 ? p[1] + 1 : p[1];
257 p[3] = p[2] < sz-1 ? p[2] + 1 : p[2];
266 return getInterpolateIdxsWithOff<T>(idxabl,sz,0,pos,extrap,snapdist,p);
270 template <
class T,
class RT>
272 bool extrapolate=
false,
float snapdist=
mDefEps )
277 { ret =
mUdf(
RT);
return false; }
279 { ret = idxabl[p[0]];
return true; }
281 const float relpos = pos - p[1];
283 idxabl[p[3]], relpos );
291 template <
class T,
class RT>
293 od_int64 offset,
float pos,
RT& ret,
bool extrapolate=
false,
297 int res = getInterpolateIdxsWithOff<T>( idxabl, sz, offset, pos,
298 extrapolate, snapdist, p );
300 { ret =
mUdf(
RT);
return false; }
302 { ret = idxabl[p[0]];
return true; }
304 const float relpos = pos - p[1];
306 idxabl[p[2]], idxabl[p[3]], relpos );
311 template <
class T,
class RT>
313 bool extrapolate=
false,
float snapdist=
mDefEps )
315 return interpolateRegWithUdfWithOff<T,RT>( idxabl, sz, 0, pos, ret,
316 extrapolate, snapdist );
322 bool extrapolate=
false,
float snapdist=
mDefEps )
324 float ret =
mUdf(
float);
332 bool extrapolate=
false,
335 float ret =
mUdf(
float);
348 template <
class T>
inline 350 const T* controlpts,
const int* controlsamples,
int nrcontrols,
351 bool usefactor, T* output,
352 float* calibrationcurve = 0 )
354 int firstsample =
mUdf(
int), lastsample = -
mUdf(
int);
356 for (
int idx=0; idx<nrcontrols; idx++ )
358 const int sample = controlsamples[idx];
362 if ( !idx || sample<firstsample )
363 firstsample = sample;
364 if ( !idx || sample>=lastsample )
367 const float value = usefactor
368 ? controlpts[idx]/input[sample]
369 : controlpts[idx]-input[sample];
371 func.
add(
mCast(
float,sample), value );
375 { OD::memCopy( output, input,
sizeof(T)*sz );
return; }
377 for (
int idx=0; idx<sz; idx++ )
380 if ( idx<=firstsample )
381 calibration = func.
yVals()[0];
382 else if ( idx>=lastsample )
383 calibration = func.
yVals()[func.
size()-1];
387 output[idx] = usefactor
388 ? input[idx]*calibration
389 : input[idx]+calibration;
391 if ( calibrationcurve )
392 calibrationcurve[idx] = calibration;
int getInterpolateIdxsWithOff(const T &idxabl, od_int64 sz, od_int64 offset, float pos, bool extrap, float snapdist, od_int64 p[4])
Definition: idxable.h:239
int getUpperIdx(const X &x, int sz, double pos)
Definition: idxable.h:166
T poly1D(float x0, T v0, float x1, T v1, float x2, T v2, float x3, T v3, float x)
Definition: interpol1d.h:216
int getInterpolateIdxs(const T &idxabl, int sz, float pos, bool extrap, float snapdist, od_int64 p[4])
Definition: idxable.h:263
#define mIsZero(x, eps)
Definition: commondefs.h:55
void interpolatePositioned(const X &x, const Y &y, int sz, float desiredx, RT &ret, bool extrapolate=false)
Definition: idxable.h:180
#define mCast(tp, v)
Definition: commondefs.h:120
bool interpolateReg(const T &idxabl, int sz, float pos, RT &ret, bool extrapolate=false, float snapdist=mDefEps)
Definition: idxable.h:271
#define od_int64
Definition: plftypes.h:34
bool isEmpty() const
Definition: mathfunc.h:163
T polyReg1D(T vm1, T v0, T v1, T v2, float x)
Definition: interpol1d.h:128
bool findFPPos(const T1 &posarr, T3 sz, T2 pos, T3 beforefirst, T3 &idx, T2 eps=mDefEps)
Definition: idxable.h:111
#define mIsEqual(x, y, eps)
Definition: commondefs.h:56
const TypeSet< yT > & yVals() const
Definition: mathfunc.h:170
int getLowIdx(const X &x, int sz, double pos)
Definition: idxable.h:150
T3 indexOf(const T1 &arr, T3 sz, const T2 &val, T3 notfoundval)
Definition: idxable.h:36
T polyReg1DWithUdf(T vm1, T v0, T v1, T v2, float x)
Definition: interpol1d.h:187
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:270
#define mNINT64(x)
Definition: commondefs.h:49
bool interpolateRegWithUdf(const T &idxabl, int sz, float pos, RT &ret, bool extrapolate=false, float snapdist=mDefEps)
Definition: idxable.h:312
T3 derefIndexOf(const T1 &arr, T3 sz, const T2 &val, T3 notfoundval)
Definition: idxable.h:51
#define mDefEps
Definition: commondefs.h:60
void add(xT x, yT y)
Definition: mathfunc.h:404
Position-sorted indexable objects.
Definition: idxable.h:28
bool interpolateRegWithUdfWithOff(const T &idxabl, od_int64 sz, od_int64 offset, float pos, RT &ret, bool extrapolate=false, float snapdist=mDefEps)
Definition: idxable.h:292
T linear1D(float x0, T v0, float x1, T v1, float x)
Definition: interpol1d.h:72
int size() const
Definition: mathfunc.h:162
void calibrateArray(const T *input, int sz, const T *controlpts, const int *controlsamples, int nrcontrols, bool usefactor, T *output, float *calibrationcurve=0)
Definition: idxable.h:349
MathFunction based on bend points.
Definition: mathfunc.h:149
yT getValue(xT x) const
Definition: mathfunc.h:166
bool findPos(const T1 &posarr, T3 sz, T2 pos, T3 beforefirst, T3 &idx)
Definition: idxable.h:73