aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/vasp/source/env.cpp
blob: 010f888e3d4f2bf71b47cf8f8a927fbefc692145 (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
/* 

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.  

*/

#include "main.h"
#include "env.h"
#include "classes.h"
#include "util.h"

Env::Env(I argc,const t_atom *argv)
{
	I ix = 0;
	t_symbol *v = ix < argc?flext::GetASymbol(argv[ix]):NULL;
	if(v && v == vasp_base::sym_env) ix++; // if it is "env" ignore it

	cnt = (argc-ix)/2;
	pos = new R[cnt];
	val = new R[cnt];

	R prev = -BIG;
	BL ok = true;
	for(I i = 0; i < cnt; ++i) {
		val[i] = flext::GetAFloat(argv[ix++]);
		pos[i] = flext::GetAFloat(argv[ix++]);
		if(pos[i] < prev) ok = false;
		prev = pos[i];
	}

	if(ix < argc) {
		post("vasp - env pos/value pairs incomplete, omitted dangling value");
	}

	if(!ok) Clear();
}

/*
Env::Env(const Env &s):
	cnt(s.cnt),pos(new R[s.cnt]),val(new R[s.cnt])
{
	for(I i = 0; i < cnt; ++i) pos[i] = s.pos[i],val[i] = s.val[i];
}
*/

Env::~Env() { Clear(); }


BL Env::ChkArgs(I argc,const t_atom *argv)
{
	I ix = 0;

	// vasp keyword
	t_symbol *v = ix < argc?flext::GetASymbol(argv[ix]):NULL;
	if(v && v == vasp_base::sym_env) ix++; // if it is "env" ignore it

	while(argc > ix) {
		// check for value 
		if(flext::CanbeFloat(argv[ix])) ix++;
		else 
			return false;

		// check for position
		if(argc > ix)
			if(flext::CanbeFloat(argv[ix])) ix++;
			else 
				return false;
	}

	return true;
}

V Env::MakeList(flext::AtomList &ret) const
{
	ret(cnt*2+1);
	flext::SetSymbol(ret[0],vasp_base::sym_env);
	for(I i = 0; i < cnt; ++i) {
		flext::SetFloat(ret[i*2+1],val[i]);
		flext::SetFloat(ret[i*2+2],pos[i]);
	}
}

V Env::Clear()
{
	cnt = 0;
	if(pos) delete[] pos; pos = NULL;
	if(val) delete[] val; val = NULL;
}


Env::Iter::Iter(const Env &bpl): bp(bpl),ppt(-BIG),npt(BIG),pvl(0),k(0) {}

V Env::Iter::Init(R p) 
{
	I cnt = bp.Count();
	ASSERT(cnt > 0);

	if(p < bp.Pos(0)) {
		// position is before the head
		ix = -1;
		ppt = -BIG; pvl = bp.Val(0);
	}
	else if(p > bp.Pos(cnt-1)) { 
		// position is after the tail
		ix = cnt-1;
		ppt = bp.Pos(ix); pvl = bp.Val(ix);
	}
	else { 
		// somewhere in the list
		for(ix = 0; ix < cnt; ++ix)
			if(p >= bp.Pos(ix)) break;
		ppt = bp.Pos(ix); pvl = bp.Val(ix);

		ASSERT(ix < cnt);
	}

	if(ix >= cnt) {
		npt = BIG; nvl = pvl;
		k = 0;
	}
	else {
		npt = bp.Pos(ix+1); nvl = bp.Val(ix+1);
		k = (nvl-pvl)/(npt-ppt); 
	}
}

// \todo iteration first, then calculation of k
V Env::Iter::UpdateFwd(R p) 
{
	do {
		ppt = npt,pvl = nvl;
		if(++ix >= bp.Count()-1) npt = BIG,k = 0;
		else {
			k = ((nvl = bp.Val(ix+1))-pvl)/((npt = bp.Pos(ix+1))-ppt); 
		}
	} while(p > npt);
}

// \todo iteration first, then calculation of k
V Env::Iter::UpdateBwd(R p) 
{
	do {
		npt = ppt,nvl = pvl;
		if(--ix < 0) ppt = -BIG,k = 0;
		else {
			k = (nvl-(pvl = bp.Val(ix)))/(npt-(ppt = bp.Pos(ix))); 
		}
	} while(p < ppt);
}