-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathtf.js
175 lines (168 loc) · 6.53 KB
/
tf.js
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
// var exports = module.exports = {};
var widgets = require('@jupyter-widgets/base');
var _ = require('underscore')
var serialize = require('./serialize.js')
var semver_range = require('./utils.js').semver_range;
var ndarray_pack = require("ndarray-pack");
var TransferFunctionView = widgets.DOMWidgetView.extend( {
render: function() {
this.img = document.createElement('img');
this.img.setAttribute('src', this.model.get('rgba'));
this.img.setAttribute('style', this.model.get('style'));
this.model.on('change:rgba', function() {
this.img.setAttribute('src', this.model.get('rgba'));
}, this);
this.model.on('change:style', function() {
this.img.setAttribute('style', this.model.get('style'));
}, this);
this.el.appendChild(this.img);
},
});
var TransferFunctionModel = widgets.DOMWidgetModel.extend({
defaults: function() {
return _.extend(widgets.DOMWidgetModel.prototype.defaults(), {
_model_name : 'TransferFunctionModel',
_view_name : 'TransferFunctionView',
_model_module : 'ipyvolume',
_view_module : 'ipyvolume',
_model_module_version: semver_range,
_view_module_version: semver_range,
rgba: null,
})
},
get_data_array: function() {
var flat_array = [];
var rgba = this.get("rgba")
for(var i = 0; i < rgba.shape[0]; i++) {
for(var j = 0; j < 4; j++) {
flat_array.push(rgba.get(i,j)*255)
}
}
var transfer_function_uint8_array = new Uint8Array(flat_array);
// REMOVE: for debugging
//window.transfer_function_uint8_array = transfer_function_uint8_array
//window.flat_array = flat_array
return transfer_function_uint8_array
},
},{
serializers: _.extend({
rgba: serialize.ndarray,
}, widgets.WidgetModel.serializers)
});
var TransferFunctionJsBumpsModel = TransferFunctionModel.extend({
defaults: function() {
return _.extend(TransferFunctionModel.prototype.defaults(), {
_model_name : 'TransferFunctionJsBumpsModel',
levels: [0.1, 0.5, 0.8],
opacities: [0.01, 0.05, 0.1],
widths: [0.1, 0.1, 0.1]
})
},
constructor : function () {
TransferFunctionModel.prototype.constructor.apply(this, arguments);
this.on("change:levels", this.recalculate_rgba, this);
this.on("change:opacities", this.recalculate_rgba, this);
this.on("change:widths", this.recalculate_rgba, this);
this.recalculate_rgba()
},
recalculate_rgba: function() {
var rgba = []
var colors = [[1,0,0], [0,1,0], [0,0,1]]
var levels = this.get("levels")
var widths = this.get("widths")
var opacities = this.get("opacities")
window.rgba = rgba
window.tfjs = this
var N = 256
for(var i = 0; i < N; i++) {
var x = i/(N-1);
var color = [0, 0, 0, 0]; // red, green, blue and alpha
for(var j = 0; j < levels.length; j++) {
var basecolor = colors[j]
var intensity = Math.exp(-(Math.pow(x-levels[j],2) / Math.pow(widths[j], 2)))
for(var k = 0; k < 3; k++) {
color[k] += (basecolor[k] * intensity * opacities[j])
}
color[3] += intensity * opacities[j]
}
var max_value = color[0];
for(var k = 1; k < 3; k++) {
max_value = Math.max(max_value, color[k])
}
for(var k = 0; k < 3; k++) {
color[k] = Math.min(1, color[k]/max_value); // normalize and clip to 1
}
color[3] = Math.min(1, color[3]); // clip alpha
rgba.push(color)
}
this.set("rgba", rgba)
this.save_changes()
}
});
var TransferFunctionWidgetJs3Model = TransferFunctionModel.extend({
defaults: function() {
return _.extend(TransferFunctionModel.prototype.defaults(), {
_model_name : 'TransferFunctionWidgetJs3Model',
level1: 0.1,
level2: 0.5,
level3: 0.8,
opacity1: 0.01,
opacity2: 0.05,
opacity3: 0.1,
width1: 0.1,
width2: 0.1,
width3: 0.1
})
},
constructor : function () {
TransferFunctionModel.prototype.constructor.apply(this, arguments);
this.on("change:level1", this.recalculate_rgba, this);
this.on("change:level2", this.recalculate_rgba, this);
this.on("change:level3", this.recalculate_rgba, this);
this.on("change:opacity1", this.recalculate_rgba, this);
this.on("change:opacity2", this.recalculate_rgba, this);
this.on("change:opacity3", this.recalculate_rgba, this);
this.on("change:width1", this.recalculate_rgba, this);
this.on("change:width2", this.recalculate_rgba, this);
this.on("change:width3", this.recalculate_rgba, this);
this.recalculate_rgba()
},
recalculate_rgba: function() {
var rgba = []
var colors = [[1,0,0], [0,1,0], [0,0,1]]
var levels = [this.get("level1"), this.get("level2"), this.get("level3")]
var widths = [this.get("width1"), this.get("width2"), this.get("width3")]
var opacities = [this.get("opacity1"), this.get("opacity2"), this.get("opacity3")]
var N = 256
for(var i = 0; i < N; i++) {
var x = i/(N-1);
var color = [0, 0, 0, 0]; // red, green, blue and alpha
for(var j = 0; j < 3; j++) {
var basecolor = colors[j]
var intensity = Math.exp(-(Math.pow(x-levels[j],2) / Math.pow(widths[j], 2)))
for(var k = 0; k < 3; k++) {
color[k] += (basecolor[k] * intensity * opacities[j])
}
color[3] += intensity * opacities[j]
}
var max_value = color[0];
for(var k = 1; k < 3; k++) {
max_value = Math.max(max_value, color[k])
}
for(var k = 0; k < 3; k++) {
color[k] = Math.min(1, color[k]/max_value); // normalize and clip to 1
}
color[3] = Math.min(1, color[3]); // clip alpha
rgba.push(color)
}
rgba = ndarray_pack(rgba)
this.set("rgba", rgba)
this.save_changes()
},
});
module.exports = {
TransferFunctionModel: TransferFunctionModel,
TransferFunctionView: TransferFunctionView,
TransferFunctionWidgetJs3Model: TransferFunctionWidgetJs3Model,
TransferFunctionJsBumpsModel: TransferFunctionJsBumpsModel
}