37 template <
class T1,
class T2,
class T3>
38 inline T3
indexOf(
const T1& arr, T3 sz,
const T2& val, T3 notfoundval )
40 for ( T3 idx=0; idx<sz; idx++ )
42 if ( arr[idx] == val )
52 template <
class T1,
class T2,
class T3>
53 inline T3
derefIndexOf(
const T1& arr, T3 sz,
const T2& val, T3 notfoundval )
55 for ( T3 idx=0; idx<sz; idx++ )
57 if ( *arr[idx] == val )
74 template <
class T1,
class T2,
class T3>
75 bool findPos(
const T1& posarr, T3 sz, T2 pos, T3 beforefirst, T3& idx )
78 if ( sz < 1 || pos < posarr[0] )
81 if ( pos == posarr[0] )
82 { idx = 0;
return true; }
83 else if ( pos > posarr[sz-1] || pos == posarr[sz-1] )
84 { idx = sz-1;
return pos == posarr[sz-1]; }
88 while ( idx2 - idx1 > 1 )
90 idx = (idx2 + idx1) / 2;
91 T2 arrval = posarr[idx];
92 if ( arrval == pos )
return true;
93 else if ( arrval > pos ) idx2 = idx;
98 return posarr[idx] == pos;
112 template <
class T1,
class T2,
class T3>
113 bool findFPPos(
const T1& posarr, T3 sz, T2 pos, T3 beforefirst, T3& idx,
117 if ( !sz )
return false;
118 if ( sz < 2 || pos <= posarr[0] )
121 { idx = 0;
return true; }
125 else if ( pos >= posarr[sz-1] )
126 { idx = sz-1;
return mIsEqual(pos,posarr[sz-1],eps); }
131 while ( idx2 - idx1 > 1 )
133 idx = (idx2 + idx1) / 2;
134 T2 diff = posarr[idx] - pos;
135 if (
mIsZero(diff,eps) )
return true;
136 else if ( diff > 0 ) idx2 = idx;
141 return mIsEqual(pos,posarr[idx],eps);
154 int idx;
findFPPos( x, sz, pos, -1, idx );
return idx;
171 const bool exactmatch =
findFPPos( x, sz, pos, -1, idx );
172 return idx < 1 ? 1 : ( exactmatch ? idx : idx+1 );
181 template <
class X,
class Y,
class RT>
184 RT& ret,
bool extrapolate=
false )
189 ret = extrapolate ? y[0] :
mUdf(
RT);
193 else if ( desiredx < x[0] || desiredx > x[sz-1] )
198 ret = desiredx < x[0]
205 int prevpos =
getLowIdx( x, sz, desiredx );
206 int nextpos = prevpos + 1;
210 x[nextpos], y[nextpos], desiredx );
214 { prevpos++; nextpos++; }
215 else if ( nextpos == sz-1 )
216 { prevpos--; nextpos--; }
219 x[prevpos], y[prevpos],
220 x[nextpos], y[nextpos],
221 x[nextpos+1], y[nextpos+1],
227 template <
class X,
class Y>
229 bool extrapolate=
false )
231 float ret =
mUdf(
float);
245 || (!extrap && (pos<-snapdist || (pos+offset)>sz-1+snapdist)) )
249 const float dist = pos - intpos;
251 if ( dist>-snapdist && dist<snapdist && intpos>-1 && intpos<sz )
252 { p[0] = intpos;
return 0; }
254 p[1] = dist > 0 ? intpos : intpos - 1;
255 if ( p[1] < 0 ) p[1] = 0;
256 if ( p[1] >= sz ) p[1] = sz - 1;
257 p[0] = p[1] < 1 ? p[1] : p[1] - 1;
258 p[2] = p[1] < sz-1 ? p[1] + 1 : p[1];
259 p[3] = p[2] < sz-1 ? p[2] + 1 : p[2];
268 return getInterpolateIdxsWithOff<T>(idxabl,sz,0,pos,extrap,snapdist,p);
272 template <
class T,
class RT>
274 bool extrapolate=
false,
float snapdist=
mDefEps )
279 { ret =
mUdf(
RT);
return false; }
281 { ret = idxabl[p[0]];
return true; }
283 const float relpos = pos - p[1];
285 idxabl[p[3]], relpos );
293 template <
class T,
class RT>
295 od_int64 offset,
float pos,
RT& ret,
bool extrapolate=
false,
299 int res = getInterpolateIdxsWithOff<T>( idxabl, sz, offset, pos,
300 extrapolate, snapdist, p );
302 { ret =
mUdf(
RT);
return false; }
304 { ret = idxabl[p[0]];
return true; }
306 const float relpos = pos - p[1];
308 idxabl[p[2]], idxabl[p[3]], relpos );
313 template <
class T,
class RT>
315 bool extrapolate=
false,
float snapdist=
mDefEps )
317 return interpolateRegWithUdfWithOff<T,RT>( idxabl, sz, 0, pos, ret,
318 extrapolate, snapdist );
324 bool extrapolate=
false,
float snapdist=
mDefEps )
326 float ret =
mUdf(
float);
334 bool extrapolate=
false,
337 float ret =
mUdf(
float);
350 template <
class T>
inline 352 const T* controlpts,
const int* controlsamples,
int nrcontrols,
353 bool usefactor, T* output,
354 float* calibrationcurve = 0 )
356 int firstsample =
mUdf(
int), lastsample = -
mUdf(
int);
358 for (
int idx=0; idx<nrcontrols; idx++ )
360 const int sample = controlsamples[idx];
364 if ( !idx || sample<firstsample )
365 firstsample = sample;
366 if ( !idx || sample>=lastsample )
369 const float value = usefactor
370 ? controlpts[idx]/input[sample]
371 : controlpts[idx]-input[sample];
373 func.
add(
mCast(
float,sample), value );
377 { OD::memCopy( output, input,
sizeof(T)*sz );
return; }
379 for (
int idx=0; idx<sz; idx++ )
382 if ( idx<=firstsample )
383 calibration = func.
yVals()[0];
384 else if ( idx>=lastsample )
385 calibration = func.
yVals()[func.
size()-1];
389 output[idx] = usefactor
390 ? input[idx]*calibration
391 : input[idx]+calibration;
393 if ( calibrationcurve )
394 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:241
int getUpperIdx(const X &x, int sz, double pos)
Definition: idxable.h:168
T poly1D(float x0, T v0, float x1, T v1, float x2, T v2, float x3, T v3, float x)
Definition: interpol1d.h:218
int getInterpolateIdxs(const T &idxabl, int sz, float pos, bool extrap, float snapdist, od_int64 p[4])
Definition: idxable.h:265
#define mIsZero(x, eps)
Definition: commondefs.h:53
void interpolatePositioned(const X &x, const Y &y, int sz, float desiredx, RT &ret, bool extrapolate=false)
Definition: idxable.h:182
#define mCast(tp, v)
Definition: commondefs.h:124
bool interpolateReg(const T &idxabl, int sz, float pos, RT &ret, bool extrapolate=false, float snapdist=mDefEps)
Definition: idxable.h:273
#define od_int64
Definition: plftypes.h:36
bool isEmpty() const
Definition: mathfunc.h:165
T polyReg1D(T vm1, T v0, T v1, T v2, float x)
Definition: interpol1d.h:130
bool findFPPos(const T1 &posarr, T3 sz, T2 pos, T3 beforefirst, T3 &idx, T2 eps=mDefEps)
Definition: idxable.h:113
#define mIsEqual(x, y, eps)
Definition: commondefs.h:54
const TypeSet< yT > & yVals() const
Definition: mathfunc.h:172
int getLowIdx(const X &x, int sz, double pos)
Definition: idxable.h:152
T3 indexOf(const T1 &arr, T3 sz, const T2 &val, T3 notfoundval)
Definition: idxable.h:38
T polyReg1DWithUdf(T vm1, T v0, T v1, T v2, float x)
Definition: interpol1d.h:189
#define mUdf(type)
Use this macro to get the undefined for simple types.
Definition: undefval.h:272
#define mNINT64(x)
Definition: commondefs.h:46
bool interpolateRegWithUdf(const T &idxabl, int sz, float pos, RT &ret, bool extrapolate=false, float snapdist=mDefEps)
Definition: idxable.h:314
T3 derefIndexOf(const T1 &arr, T3 sz, const T2 &val, T3 notfoundval)
Definition: idxable.h:53
#define mDefEps
Definition: commondefs.h:58
void add(xT x, yT y)
Definition: mathfunc.h:406
Position-sorted indexable objects.
Definition: idxable.h:30
bool interpolateRegWithUdfWithOff(const T &idxabl, od_int64 sz, od_int64 offset, float pos, RT &ret, bool extrapolate=false, float snapdist=mDefEps)
Definition: idxable.h:294
T linear1D(float x0, T v0, float x1, T v1, float x)
Definition: interpol1d.h:74
int size() const
Definition: mathfunc.h:164
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:351
MathFunction based on bend points.
Definition: mathfunc.h:151
yT getValue(xT x) const
Definition: mathfunc.h:168
bool findPos(const T1 &posarr, T3 sz, T2 pos, T3 beforefirst, T3 &idx)
Definition: idxable.h:75