[208] | 1 | function NeuronDrawer(contextName, width, height) { |
---|
| 2 | this._canvasWidth = width; |
---|
| 3 | this._canvasHeight = height; |
---|
| 4 | this._containerContextName = contextName; |
---|
[161] | 5 | this._unknown_symbol=[1,4, 25,25, 75,25, 75,75, 25,75, 25,25]; |
---|
| 6 | this._neuron_symbol=[1,4, 75,50, 25,0, 25,99, 75,50, 100,50]; |
---|
| 7 | this._inputonly_symbol=[1,5, 25,40, 35,40, 45,50, 35,60, 25,60, 25,40]; |
---|
| 8 | this._outputonly_symbol=[1,7, 75,50, 75,60, 55,60, 65,50, 55,40, 75,40, 75,50, 100,50]; |
---|
[188] | 9 | this._neurons = undefined; |
---|
[208] | 10 | this._SCALE = 150; |
---|
| 11 | this._scale = 1; |
---|
| 12 | this._min_scale = 0.1; |
---|
[214] | 13 | this._lineDebug = false; |
---|
[208] | 14 | //Kinetic.js |
---|
| 15 | this._stage = undefined; |
---|
| 16 | this._layer = undefined; |
---|
[188] | 17 | } |
---|
[161] | 18 | |
---|
[208] | 19 | NeuronDrawer.prototype.initializeNewCanvas = function () { |
---|
[188] | 20 | |
---|
[214] | 21 | if(typeof (this._canvasWidth) == "undefined" && typeof (this._canvasHeight) == "undefined") |
---|
| 22 | { |
---|
| 23 | var div = $("#" + this._containerContextName); |
---|
| 24 | this._canvasWidth = div.width(); |
---|
| 25 | this._canvasHeight = div.height(); |
---|
| 26 | } |
---|
| 27 | |
---|
[208] | 28 | this._stage = new Kinetic.Stage({ |
---|
| 29 | container: this._containerContextName, |
---|
| 30 | width: this._canvasWidth, |
---|
| 31 | height: this._canvasHeight, |
---|
| 32 | draggable: true |
---|
| 33 | }); |
---|
[188] | 34 | |
---|
[208] | 35 | this._layer = new Kinetic.Layer(); |
---|
[188] | 36 | |
---|
[208] | 37 | this._addZoom(); |
---|
| 38 | } |
---|
[188] | 39 | |
---|
[208] | 40 | NeuronDrawer.prototype._addZoom = function(){ |
---|
| 41 | var self = this; |
---|
[188] | 42 | |
---|
[208] | 43 | var isFirefox = (/Firefox/i.test(navigator.userAgent)) ? true : false; |
---|
[188] | 44 | |
---|
[208] | 45 | var mousewheelevt= isFirefox ? "DOMMouseScroll" : "mousewheel" //FF doesn't recognize mousewheel as of FF3.x |
---|
[188] | 46 | |
---|
[208] | 47 | document.getElementById(this._containerContextName).addEventListener(mousewheelevt,function(e){ |
---|
| 48 | e.preventDefault(); |
---|
[188] | 49 | |
---|
[208] | 50 | var zoomAmount = 0; |
---|
| 51 | if(!isFirefox) |
---|
| 52 | zoomAmount = e.wheelDelta * 0.001; |
---|
| 53 | else |
---|
| 54 | zoomAmount = e.detail * -0.12; |
---|
[143] | 55 | |
---|
[208] | 56 | var newScale = self._stage.scale(); |
---|
| 57 | newScale.x += zoomAmount; |
---|
| 58 | newScale.y += zoomAmount; |
---|
[188] | 59 | |
---|
[208] | 60 | self._stage.scale(newScale) |
---|
| 61 | self._stage.draw(); |
---|
| 62 | }); |
---|
[143] | 63 | } |
---|
| 64 | |
---|
[208] | 65 | NeuronDrawer.prototype.drawNeuralNetwork = function (neurons, connections, layouts, classes) { |
---|
[143] | 66 | |
---|
[208] | 67 | this._neurons = []; |
---|
| 68 | var offsetFactor = 0; |
---|
[143] | 69 | |
---|
[208] | 70 | for (var i = 0; i < layouts.length; i++) { |
---|
| 71 | var scheme = undefined; |
---|
| 72 | scheme = this._chooseSchema(i,neurons, connections, classes); |
---|
[143] | 73 | |
---|
[208] | 74 | var neuronData = this._getSize(scheme); |
---|
| 75 | neuronData.scheme = scheme; |
---|
| 76 | neuronData.x += layouts[i].x * this._SCALE; |
---|
| 77 | neuronData.y += -layouts[i].y * this._SCALE; |
---|
[143] | 78 | |
---|
[208] | 79 | this._neurons.push(neuronData); |
---|
| 80 | this.drawNeuron(layouts[i].x * this._SCALE, -layouts[i].y * this._SCALE, scheme); |
---|
[143] | 81 | |
---|
[208] | 82 | offsetFactor = Math.min(offsetFactor, layouts[i].x); |
---|
| 83 | } |
---|
[143] | 84 | |
---|
[208] | 85 | this._layer.offsetX(offsetFactor * this._SCALE); |
---|
[143] | 86 | |
---|
[208] | 87 | for (var i = 0; i < connections.length; i++) |
---|
| 88 | this.drawConnection(i, neurons, connections, einfos); |
---|
[143] | 89 | } |
---|
| 90 | |
---|
[161] | 91 | NeuronDrawer.prototype.drawNeuron = function (x, y, scheme) { |
---|
[208] | 92 | var points = []; |
---|
[143] | 93 | |
---|
| 94 | var position = 0; |
---|
[208] | 95 | var noOfBlocks = scheme[position++];//number of "blocks" to draw |
---|
[143] | 96 | var noOfLines = 0;//number of line to draw |
---|
[161] | 97 | for (var i = 0; i < noOfBlocks; i++) { |
---|
[143] | 98 | noOfLines = scheme[position++]; |
---|
| 99 | |
---|
[161] | 100 | for (var j = 0; j < noOfLines; j++) { |
---|
[208] | 101 | points.push(scheme[position++]+x); |
---|
| 102 | points.push(scheme[position++]+y); |
---|
[143] | 103 | } |
---|
| 104 | |
---|
[208] | 105 | points.push(scheme[position++]+x); |
---|
| 106 | points.push(scheme[position++]+y); |
---|
[143] | 107 | |
---|
[208] | 108 | var symbol = new Kinetic.Line({ |
---|
| 109 | points: points, |
---|
| 110 | stroke: 'black', |
---|
| 111 | strokeWidth: 1 |
---|
| 112 | }); |
---|
[143] | 113 | |
---|
[208] | 114 | symbol.move({x:0, y: 0}); |
---|
| 115 | var self = this; |
---|
| 116 | symbol.on('mousemove', function(){ |
---|
| 117 | var mousePos = self._stage.getPointerPosition(); |
---|
| 118 | var x = mousePos.x; |
---|
| 119 | var y = mousePos.y; |
---|
| 120 | console.log(x,y); |
---|
| 121 | }); |
---|
[161] | 122 | |
---|
[208] | 123 | this._layer.add(symbol); |
---|
| 124 | points = []; |
---|
[161] | 125 | } |
---|
[143] | 126 | } |
---|
| 127 | |
---|
[208] | 128 | NeuronDrawer.prototype._getConnection = function(connections, id, number){ |
---|
| 129 | |
---|
[161] | 130 | var counter = 0; |
---|
[208] | 131 | for(var i = 0; i < connections.length; i++) |
---|
| 132 | { |
---|
| 133 | if(connections[i].getDestination() == id) |
---|
| 134 | { |
---|
| 135 | if(counter == number) |
---|
| 136 | return connections[i].getSource(); |
---|
| 137 | else |
---|
| 138 | counter++; |
---|
| 139 | } |
---|
[161] | 140 | } |
---|
| 141 | } |
---|
[143] | 142 | |
---|
[188] | 143 | NeuronDrawer.prototype.drawConnection = function (id, neurons, connections, einfos) { |
---|
[161] | 144 | |
---|
[188] | 145 | var n2; |
---|
[208] | 146 | neuroId = connections[id].getDestination(); |
---|
| 147 | //this is hack trick to change order of inputs |
---|
| 148 | var maxVal = this._inputY(this._getNumberOfInputs(neuroId,connections), connections, neuroId); |
---|
[188] | 149 | |
---|
[208] | 150 | for(var input = 0; input < this._getNumberOfInputs(neuroId,connections); input++) |
---|
[188] | 151 | { |
---|
[208] | 152 | n2 = this._neurons[this._getConnection(connections, neuroId, input)]; |
---|
| 153 | var points = []; |
---|
[188] | 154 | |
---|
[208] | 155 | var yw = this._inputY(input, connections, neuroId); |
---|
| 156 | var xw = (maxVal-yw) / 4; |
---|
[143] | 157 | |
---|
[208] | 158 | yw += this._neurons[neuroId].y;//add position of object to y |
---|
[143] | 159 | |
---|
[214] | 160 | //Inputs |
---|
[208] | 161 | points.push(this._neurons[neuroId].x);//x |
---|
| 162 | points.push(yw);//y |
---|
[143] | 163 | |
---|
[208] | 164 | points.push(this._neurons[neuroId].x - xw);//x |
---|
| 165 | points.push(yw);//y |
---|
[143] | 166 | |
---|
[214] | 167 | //straight line to second neuron |
---|
| 168 | if(this._lineDebug || (this._neurons[neuroId].x > n2.x)) |
---|
[208] | 169 | { |
---|
[214] | 170 | points.push(n2.x+n2.sizeX); |
---|
| 171 | points.push(n2.y + n2.sizeY/2); |
---|
[208] | 172 | } |
---|
| 173 | else |
---|
| 174 | { |
---|
[214] | 175 | //U - shape |
---|
| 176 | //Vertical |
---|
| 177 | points.push(this._neurons[neuroId].x - xw);//x |
---|
| 178 | points.push(n2.y + n2.sizeY + (input+1)*5);//y |
---|
[143] | 179 | |
---|
[214] | 180 | //Horizontal |
---|
| 181 | points.push(n2.x + n2.sizeX);//x |
---|
| 182 | points.push(n2.y + n2.sizeY + (input+1)*5);//y |
---|
| 183 | |
---|
| 184 | //Vertical |
---|
| 185 | points.push(n2.x + n2.sizeX);//x |
---|
| 186 | points.push(n2.y + n2.sizeY/2);//y |
---|
| 187 | } |
---|
| 188 | |
---|
[208] | 189 | var line = new Kinetic.Line({ |
---|
| 190 | points: points, |
---|
| 191 | stroke: 'black', |
---|
| 192 | strokeWidth: 1 |
---|
| 193 | }); |
---|
| 194 | this._layer.add(line); |
---|
| 195 | } |
---|
[143] | 196 | |
---|
[208] | 197 | } |
---|
[143] | 198 | |
---|
[208] | 199 | NeuronDrawer.prototype._inputY = function(number, connections, id){ |
---|
| 200 | return ((1 + number)* this._neurons[id].sizeY) / ((this._getNumberOfInputs(id, connections)) + 1); |
---|
| 201 | } |
---|
[143] | 202 | |
---|
[208] | 203 | NeuronDrawer.prototype.finalize = function() |
---|
| 204 | { |
---|
| 205 | this._stage.add(this._layer); |
---|
| 206 | } |
---|
[161] | 207 | |
---|
[208] | 208 | NeuronDrawer.prototype._getNumberOfInputs = function(number, connections){ |
---|
[143] | 209 | |
---|
[208] | 210 | var counter = 0; |
---|
[143] | 211 | |
---|
[208] | 212 | for(var i = 0; i < connections.length; i++){ |
---|
| 213 | if(connections[i].getDestination() == number) |
---|
| 214 | counter++; |
---|
[143] | 215 | } |
---|
| 216 | |
---|
[208] | 217 | return counter; |
---|
| 218 | } |
---|
[143] | 219 | |
---|
[208] | 220 | NeuronDrawer.prototype._getNumberOfOutputs = function(number, connections){ |
---|
| 221 | var counter = 0; |
---|
[143] | 222 | |
---|
[208] | 223 | for(var i = 0; i < connections.length; i++){ |
---|
| 224 | if(connections[i].getSource() == number) |
---|
| 225 | counter++; |
---|
[143] | 226 | } |
---|
| 227 | |
---|
[208] | 228 | return counter; |
---|
[188] | 229 | } |
---|
[143] | 230 | |
---|
| 231 | |
---|
| 232 | |
---|
[161] | 233 | NeuronDrawer.prototype._chooseSchema = function(number, neurons, connections, classes) |
---|
| 234 | { |
---|
| 235 | var schema = this._unknown_symbol; |
---|
| 236 | var type = neurons[number].getSchemeID(); |
---|
| 237 | |
---|
| 238 | if(type != undefined) |
---|
| 239 | { |
---|
| 240 | if(classes[type].getScheme().length != 0) |
---|
| 241 | { |
---|
| 242 | if(this._getNumberOfInputs(number, connections) == 0) |
---|
| 243 | schema = this._outputonly_symbol; |
---|
| 244 | else if (this._getNumberOfOutputs(number, connections) == 0) |
---|
| 245 | schema = this._inputonly_symbol; |
---|
| 246 | else |
---|
| 247 | schema = this._neuron_symbol; |
---|
| 248 | } |
---|
[143] | 249 | } |
---|
| 250 | |
---|
[161] | 251 | return schema; |
---|
[143] | 252 | } |
---|
| 253 | |
---|
[188] | 254 | NeuronDrawer.prototype._getSize = function(scheme) |
---|
| 255 | { |
---|
[208] | 256 | var neuron = {sizeX: 0, sizeY: 0, x: 0, y: 0}; |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | var minX = Number.MAX_VALUE; |
---|
| 260 | var maxX = -1; |
---|
| 261 | |
---|
| 262 | var minY = Number.MAX_VALUE; |
---|
| 263 | var maxY = -1; |
---|
| 264 | |
---|
[188] | 265 | var position = 0; |
---|
[208] | 266 | var noOfBlocks = scheme[position++];//number of "blocks" to draw |
---|
[188] | 267 | var noOfLines = 0;//number of line to draw |
---|
[208] | 268 | |
---|
[188] | 269 | for (var i = 0; i < noOfBlocks; i++) { |
---|
| 270 | noOfLines = scheme[position++]; |
---|
| 271 | |
---|
| 272 | for (var j = 0; j < noOfLines; j++) { |
---|
| 273 | |
---|
[208] | 274 | |
---|
| 275 | if(scheme[position] > maxX) |
---|
| 276 | maxX = scheme[position]; |
---|
| 277 | if(scheme[position] < minX) |
---|
| 278 | minX = scheme[position]; |
---|
[188] | 279 | position++; |
---|
[208] | 280 | |
---|
| 281 | if(scheme[position] > maxY) |
---|
| 282 | maxX = scheme[position]; |
---|
| 283 | if(scheme[position] < minY) |
---|
| 284 | minY = scheme[position]; |
---|
[188] | 285 | position++; |
---|
| 286 | |
---|
[208] | 287 | |
---|
| 288 | if(scheme[position] > maxX) |
---|
| 289 | maxX = scheme[position]; |
---|
| 290 | if(scheme[position] < minX) |
---|
| 291 | minX = scheme[position]; |
---|
[188] | 292 | position++; |
---|
[208] | 293 | |
---|
| 294 | if(scheme[position] > maxY) |
---|
| 295 | maxY = scheme[position]; |
---|
| 296 | if(scheme[position] < minY) |
---|
| 297 | minY = scheme[position]; |
---|
[188] | 298 | position++; |
---|
| 299 | |
---|
| 300 | position = position - 2; |
---|
| 301 | } |
---|
| 302 | position = position + 2;//move to value which define number of lines to draw |
---|
[208] | 303 | neuron.sizeX = maxX - minX; |
---|
| 304 | neuron.sizeY = maxY - minY; |
---|
| 305 | neuron.x = minX; |
---|
| 306 | neuron.y = minY; |
---|
[188] | 307 | } |
---|
| 308 | return neuron; |
---|
| 309 | } |
---|