aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/ops_resmp.cpp
blob: 63b02c987d28bbac6a94c13dfae486b6d50cdabe (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* 

VASP modular - vector assembling signal processor / objects for Max/MSP and PD

Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

/*! \file vasp__resmp.cpp
	\brief Routines for resampling
*/

#include "main.h"
#include "ops_resmp.h"


// --- resample ---------------------

/*! \brief Subroutine for resampling.

	\param cnt frame count
	\param src source data
	\param sstr source data stride
	\param dst destination data
	\param dstr destination data stride
	\param factor resampling factor
	\param center resampling center (this will remain untouched by the transformation)
	\param mode interpolation mode
	\return true on success

	\todo implement averaging interpolation methods
	\todo check for operation direction!
	\todo check for src/dst overlap
	\todo support different frame lengths for src and dst
*/

static V do_tilt(OpParam &p)
{
	I mode = p.tilt.mode;
	if(mode == 0&& (p.tilt.factor < 0 || p.tilt.center < 0 || p.tilt.factor >= p.frames)) mode = 1;

	const R center = p.tilt.center;
	const I icenter = (I)center;
	S fll,flr;
	if(p.tilt.fill) 
		fll = p.rsdt[0],flr = p.rsdt[p.frames-1];
	else
		fll = flr = 0;

	if(mode >= 1 && mode <= 3) {
		S *tmp;
		I rss;
		if(p.rsdt == p.rddt) {
			tmp = new S[p.frames],rss = 1;
			for(int i = 0; i < p.frames; ++i) tmp[i] = p.rsdt[i*p.rss];
		}
		else tmp = p.rsdt,rss = p.rss;

		switch(mode) {
		case 1: {
			// no interpolation
			for(int i = 0; i < p.frames; ++i) {
				I pi = (I)(center+(i-center)*p.tilt.factor);
				p.rddt[i*p.rds] = pi >= 0?(pi < p.frames?tmp[pi*rss]:flr):fll;
			}
			break;
		}
		case 2: {
			// linear interpolation
			for(int i = 0; i < p.frames; ++i) {
				R pr = center+(i-center)*p.tilt.factor,v;
				I ip = (I)pr;
				if(ip >= 0)
					if(ip < p.frames-1) {
						R r = pr-ip;
						v = (1-r)*tmp[ip*rss]+r*tmp[(ip+1)*rss];
					}
					else
						v = ip == p.frames-1?tmp[ip*rss]:flr;
				else
					v = fll;
				p.rddt[i*p.rds] = v;
			}
			break;
		}
		case 3: {
			// 4-point interpolation
			R f[4];
			for(int i = 0; i < p.frames; ++i) {
				R pr = center+(i-center)*p.tilt.factor;
				const I ip = (I)pr;
				const S *t = tmp+ip*rss;
				const R r = pr-ip;
				if(ip >= 1) 
					if(ip < p.frames-2) {
						f[0] = t[-rss];
						f[1] = t[0];
						f[2] = t[rss];
						f[3] = t[rss*2];
					}
					else {
						f[0] = ip < p.frames+1?t[-rss]:flr;
						f[1] = ip < p.frames?t[0]:flr;
						f[2] = ip < p.frames-1?t[rss]:flr;
						f[3] = flr;
					}
				else {
					f[0] = fll;
					f[1] = ip >= 0?t[0]:fll;
					f[2] = ip >= -1?t[rss]:fll;
					f[3] = ip >= -2?t[rss*2]:fll;
				}

				const R cmb = f[2]-f[1];
				p.rddt[i*p.rds] = f[1]+r*(cmb-0.5*(r-1.)*((f[0]-f[3]+3.*cmb)*r+(f[1]-f[0]-cmb)));
			}
			break;
		}
		}

		if(p.rsdt == p.rddt) delete[] tmp;
	}
	else {
		const R rem = center-icenter; // 0 <= rem < 1
		// quick and dirty... but in-place!

		if(p.tilt.factor > 1) {
			I i;
			for(i = 0; i <= icenter; ++i) {
				I sp = (I)(center-(i+rem)*p.tilt.factor);
				p.rddt[(icenter-i)*p.rds] = sp >= 0?(sp < p.frames?p.rsdt[sp*p.rss]:flr):fll;
			}
			for(i = 1; i < p.frames-icenter; ++i) {
				I sp = (I)(center+(i-rem)*p.tilt.factor);
				p.rddt[(icenter+i)*p.rds] = sp >= 0?(sp < p.frames?p.rsdt[sp*p.rss]:flr):fll;
			}
		}
		else {
			I i;
			for(i = icenter; i >= 0; --i) {
				I sp = (I)(center-(i+rem)*p.tilt.factor);
				p.rddt[(icenter-i)*p.rds] = p.rsdt[sp*p.rss];
			}
			for(i = p.frames-1-icenter; i > 0; --i) {
				I sp = (I)(center+(i-rem)*p.tilt.factor);
				p.rddt[(icenter+i)*p.rds] = p.rsdt[sp*p.rss];
			}
		}
	}
}


BL VecOp::d_tilt(OpParam &p) 
{ 
	if(p.frames <= 1 || p.tilt.factor == 1) return true;

	// symmetric operation
	if(p.symm == 1) 
		p.tilt.center = p.frames-1-p.tilt.center;

	do_tilt(p); 

	return true;
}


/*! \brief Does vasp resampling.

	\param arg argument list 
	\param arg.factor factor for resampling 
	\param arg.center center of resampling
	\param dst destination vasp (NULL for in-place operation)
	\param symm true for symmetric operation
	\param mode interpolation mode
	\return normalized destination vasp
*/
Vasp *VaspOp::m_tilt(OpParam &p,CVasp &src,const Argument &arg,CVasp *dst,BL symm) 
{ 
	Vasp *ret = NULL;
	if(arg.IsList() && arg.GetList().Count() >= 1) {
		RVecBlock *vecs = GetRVecs(p.opname,src,dst);
		if(vecs) {
			p.tilt.factor = flext::GetAFloat(arg.GetList()[0]);
			p.tilt.center = arg.GetList().Count() >= 2?flext::GetAFloat(arg.GetList()[1]):0;

			ret = DoOp(vecs,VecOp::d_tilt,p,symm);

			delete vecs;
		}
	}
	else
		post("%s - no arguments: no operation",p.opName());

	return ret;
}



class vasp_tilt:
	public vasp_anyop
{																				
	FLEXT_HEADER_S(vasp_tilt,vasp_anyop,Setup)
public:			
	
	vasp_tilt(I argc,t_atom *argv): 
		vasp_anyop(argc,argv,VASP_ARG_R(1),true),
		fill(xtf_zero),inter(xti_4p)
	{}

	static V Setup(t_classid c)
	{
		FLEXT_CADDATTR_VAR1_E(c,"fill",fill);
		FLEXT_CADDATTR_VAR1_E(c,"inter",inter);
	}

	enum xt_fill {
		xtf__ = -1,  // don't change
		xtf_zero = 0,xtf_edge
	};	

	enum xt_inter {
		xti__ = -1,  // don't change
		xti_inpl = 0,xti_none,xti_lin,xti_4p
	};	

	virtual Vasp *do_shift(OpParam &p) 
	{ 
		CVasp cdst(dst),cref(ref);
		return VaspOp::m_tilt(p,cref,arg,&cdst); 
	}
		
	virtual Vasp *tx_work(const Argument &arg) 
	{ 
		OpParam p(thisName(),1);													
		p.tilt.fill  = (I)fill;
		p.tilt.mode  = (I)inter;

		Vasp *ret = do_shift(p);
		return ret;
	}

	virtual V m_help() { post("%s - Resamples buffer data",thisName()); }

protected:
	xt_fill fill;
	xt_inter inter;

private:
	FLEXT_ATTRVAR_E(fill,xt_fill)
	FLEXT_ATTRVAR_E(inter,xt_inter)
};																				
FLEXT_LIB_V("vasp, vasp.tilt",vasp_tilt)


class vasp_xtilt:
	public vasp_tilt
{																				
	FLEXT_HEADER(vasp_xtilt,vasp_tilt)
public:			
	
	vasp_xtilt(I argc,t_atom *argv): vasp_tilt(argc,argv) {}

	virtual Vasp *do_shift(OpParam &p) 
	{ 
		CVasp cdst(dst),cref(ref);
		return VaspOp::m_xtilt(p,cref,arg,&cdst); 
	}
		
	virtual V m_help() { post("%s - Resamples buffer data symmetrically (in two halves)",thisName()); }
};																				
FLEXT_LIB_V("vasp, vasp.xtilt",vasp_xtilt)