FluidPlotter now allows > 1 identifier to highlight

nix
Ted Moore 4 years ago
parent 79385e7f9f
commit 6c2a5b90f8

@ -17,7 +17,7 @@ FluidPlotterPoint {
}
FluidPlotter : FluidViewer {
var <parent, <userView, <xmin, <xmax, <ymin, <ymax, <pointSize = 6, pointSizeScale = 1, dict_internal, <dict, shape = \circle, highlightIdentifier;
var <parent, <userView, <xmin, <xmax, <ymin, <ymax, <pointSize = 6, pointSizeScale = 1, dict_internal, <dict, shape = \circle, highlightIdentifiersArray;
*new {
arg parent, bounds, dict, mouseMoveAction,xmin = 0,xmax = 1,ymin = 0,ymax = 1;
@ -46,7 +46,10 @@ FluidPlotter : FluidViewer {
var counter = 0;
dict_internal.keysValuesDo({
arg id, fp_pt;
var category_string = labelSetDict.at("data").at(id)[0];
// the id has to be converted back into a string because the
// labelSetDict that comes in has the keys as strings by default
var category_string = labelSetDict.at("data").at(id.asString)[0];
var category_int;
var color;
@ -66,12 +69,13 @@ FluidPlotter : FluidViewer {
});
this.refresh;
},{
"FluidPlotter::setCategories_ FluidPlotter cannot receive setCategories. It has no data. First set a dictionary.".warn;
"FluidPlotter::setCategories_ FluidPlotter cannot receive metthod \"categories_\". It has no data. First set a dictionary.".warn;
});
}
pointSize_ {
arg identifier, size;
identifier = identifier.asSymbol;
if(dict_internal.at(identifier).notNil,{
dict_internal.at(identifier).size_(size);
this.refresh;
@ -80,9 +84,9 @@ FluidPlotter : FluidViewer {
});
}
// TODO: addPoint_ that checks if the key already exists and throws an error if it does
addPoint_ {
arg identifier, x, y, color, size = 1;
identifier = identifier.asSymbol;
if(dict_internal.at(identifier).notNil,{
"FluidPlotter::addPoint_ There already exists a point with identifier %. Point not added. Use setPoint_ to overwrite existing points.".format(identifier).warn;
},{
@ -93,6 +97,8 @@ FluidPlotter : FluidViewer {
setPoint_ {
arg identifier, x, y, color, size = 1;
identifier = identifier.asSymbol;
dict_internal.put(identifier,FluidPlotterPoint(identifier,x,y,color ? Color.black,size));
this.refresh;
@ -100,6 +106,7 @@ FluidPlotter : FluidViewer {
pointColor_ {
arg identifier, color;
identifier = identifier.asSymbol;
if(dict_internal.at(identifier).notNil,{
dict_internal.at(identifier).color_(color);
this.refresh;
@ -142,7 +149,7 @@ FluidPlotter : FluidViewer {
dict_internal = Dictionary.new;
dict.at("data").keysValuesDo({
arg k, v;
dict_internal.put(k,FluidPlotterPoint(k,v[0],v[1],Color.black,1));
dict_internal.put(k.asSymbol,FluidPlotterPoint(k,v[0],v[1],Color.black,1));
});
if(userView.notNil,{
this.refresh;
@ -174,8 +181,11 @@ FluidPlotter : FluidViewer {
}
highlight_ {
arg identifier;
highlightIdentifier = identifier;
arg arr;
if(arr.isKindOf(String).or(arr.isKindOf(Symbol)),{arr = [arr]});
highlightIdentifiersArray = arr.collect({arg item; item.asSymbol});
this.refresh;
}
@ -209,11 +219,17 @@ FluidPlotter : FluidViewer {
pt.x.postln;
pt.y.postln;*/
if(key == highlightIdentifier,{
// highlightIdentifiersArray.postln;
if(highlightIdentifiersArray.notNil,{
//"FluidPLotter:createPlotWindow is % in %".format(key,highlightIdentifiersArray).postln;
if(highlightIdentifiersArray.includes(key),{
pointSize_ = pointSize * 2.3 * pt.size
},{
pointSize_ = pointSize * pt.size
});
},{
pointSize_ = pointSize * pt.size;
});
pointSize_ = pointSize_ * pointSizeScale;

@ -56,7 +56,7 @@ d = ~dummy_data.value;
~fp.highlight_("example-95");
// a different one
~fp.highlight_("example-94");
~fp.highlight_(["example-94","example-22"]);
// none
~fp.highlight_(nil);
@ -90,7 +90,7 @@ d = ~dummy_data.value;
s.waitForBoot{
Routine{
var labelset = FluidLabelSet(s);
var kmeans = FluidKMeans(s);
var kmeans = FluidKMeans(s,4);
var ds = FluidDataSet(s);
s.sync;
@ -144,3 +144,66 @@ Window.closeAll;
// now set data
~fp.dict_(~dummy_data.(0.01,1,0.0,1.0).postln);
// ===== using KDTree to lookup the nearest neighbor of the mouse =====
// make some dummy data and plot it
s.boot;
~ds = FluidDataSet(s);
( // define a function to make some dummy data
~dummy_data = {
arg xmin = 20, xmax = 20000, ymin = -130, ymax = 0;
Dictionary.newFrom([
"cols",2,
"data",Dictionary.newFrom(Array.fill(200,{
arg i;
var return;
if((i % 2) == 0,{
return = "example-%".format((i/2).asInteger);
},{
return = [rrand(xmin,xmax),rrand(ymin,ymax)];
});
// return.postln;
return;
}))
]);
};
)
// load some data into the dataset
~ds.load(~dummy_data.(0.0,1.0,0.0,1.0));
// make the kdtree and specify that it should return the
// 4 nearest neighbours
~kdtree = FluidKDTree(s,4);
// fit the kdtree to the dataset
~kdtree.fit(~ds);
// we need to have a buffer to put the xy mouse position into
~xybuf = Buffer.alloc(s,2);
(
// now we can make the FluidPlotter. in the mouseMoveAction function
// we can put the xy position in the buffer and use the buffer to
// call kNearest on the FluidKDTree, which will return for us the
// identifier of the nearest neighbor (what FluidKDTree returns will
// always be a Symbol)
~ds.dump({
arg dict;
// dict.at("data").dopostln;
~fp = FluidPlotter(bounds:Rect(200,200,600,600),dict:dict,mouseMoveAction:{
arg view, x, y, modifiers;
// [x,y].postln;
~xybuf.setn(0,[x,y]);
~kdtree.kNearest(~xybuf,{
arg nearest;
nearest.postln;
view.highlight_(nearest);
});
// [view, x, y, modifiers].dopostln;
// "".postln;
});
});
)
Loading…
Cancel
Save