-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParameterSpace.sc
137 lines (117 loc) · 2.96 KB
/
ParameterSpace.sc
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
ParameterSpace{
var <>rect,<curloc;
var <points;
var <parameterDict;
var <weightDict;
var <defaultParDict;
var <parKeys;
var <currentPars;
var <weightfactor=2;
var <>updateFunc;
var <>moveFunc,<>moveScale=1;
var <task;
var <>dt=0.1;
var <>mindist = 0.01;
// GUI
var <w,<slider,<watcher;
var <keysGui;
var <guiUpdate;
*new{ |rect|
^super.new.rect_(rect).init;
}
init{
points = List.new;
parKeys = List.new;
parameterDict = MultiLevelIdentityDictionary.new;
defaultParDict = IdentityDictionary.new;
weightDict = IdentityDictionary.new;
curloc = Point.new;
currentPars = IdentityDictionary.new;
updateFunc = {};
moveFunc = { [0,0] };
guiUpdate = {};
task = Task.new{
loop{
this.move( (moveFunc.value * moveScale).asPoint );
dt.wait;
};
};
}
start{
task.start;
}
stop{
task.stop;
}
addParameter{ |key,default,weight|
weight = weight ? weightfactor;
parKeys.add( key );
defaultParDict.put( key, default );
weightDict.put( key, weight );
currentPars.put( key, default );
parameterDict.do{ |dict| dict.put( key, default ) };
}
addPoint{ |point|
if ( rect.contains( point ),
{
points.add( point );
parameterDict.putTree( point, defaultParDict.deepCopy );
},
{ "point not inside rectangle".warn });
}
set{ |loc|
if ( curloc.dist( loc ) > mindist,{
curloc = loc.constrain( rect );
this.calculate;
guiUpdate.value;
updateFunc.value;
});
}
weightfactor_{ |newweight|
weightDict.keysValuesChange{ |key,value|
if ( value == weightfactor,
{ newweight },
{ value });
};
}
calculate{
var distances,invdistsum,thispoint;
thispoint = points.detect( { |it| it.dist( curloc) < mindist } );
if ( thispoint.isNil, {
distances = points.collect{ |it,i| it.dist( curloc ) };
currentPars.keysValuesChange( { |key,value|
invdistsum = distances.sum( { |it| 1/pow(it,weightDict.at( key )) } );
defaultParDict.at( key ) + (points.sum( { |point,i| (parameterDict.at( point, key )-defaultParDict.at( key ))/pow(distances[i],weightDict.at( key ) ) } ) / invdistsum );
} );
},{
currentPars = parameterDict.at( thispoint );
});
}
move{ |vector|
this.set( curloc + vector );
}
makeGui{
var ysize,vpos,vbounds;
ysize = parKeys.size*20 + 130;
// create a window to view the buffer contents:
w = GUI.window.new( "Parameter Space", Rect( 0, 0, 130, ysize ) );
vpos = 2;
keysGui = parKeys.collect({ arg key,i;
GUI.staticText.new(w, Rect(2, vpos, 60, 18)).string_( key );
vbounds = Rect(65, vpos, 60, 18);
vpos = vpos + 20;
GUI.staticText.new(w, vbounds);
});
slider = GUI.slider2D.new( w, Rect( 2, vpos, 125, 125 ) );
slider.action = { |sl| this.set( [sl.x, sl.y].asPoint*rect.extent + rect.origin ) };
w.front;
guiUpdate = {
defer{
parKeys.do{ |key,i|
keysGui[i].string_( currentPars.at( key ).round(0.001) );
};
slider.setXY( (curloc.x - rect.left) / rect.width, (curloc.y - rect.top)/ rect.height );
}
};
}
}