source: js/viewer-f0/js/NeuronDrawer.js @ 143

Last change on this file since 143 was 143, checked in by mmichalski, 10 years ago

Load NeuroClass? from f0def.xml and load correct xml from server

File size: 8.9 KB
Line 
1function NeuronDrawer() {
2    this._scene = undefined;
3    this._camera = undefined;
4    this._renderer = undefined;
5    this._canvasWidth = 500;
6    this._canvasHeight = 500;
7    this._containerContext = undefined;
8    this._controls = undefined;
9    this._showAxis = false;
10}
11
12NeuronDrawer.prototype.showPartAxis = function () {
13    this._showAxis = true;
14}
15
16NeuronDrawer.prototype._createRenderer = function () {
17    this._renderer = new THREE.CanvasRenderer();//WebGLRenderer({antialias: true});
18    this._renderer.setClearColor(0xffffff, 1);
19    this._renderer.setSize(this._canvasWidth, this._canvasHeight);
20    this._containerContext = $("#containerNeuron");
21    this._containerContext.append(this._renderer.domElement);
22}
23
24NeuronDrawer.prototype._prepareCamera = function () {
25    this._camera = new THREE.PerspectiveCamera(45, this._canvasWidth / this._canvasHeight, 1, 4000);
26    this._camera.position.set(0, 0, 10);
27    this._camera.lookAt(this._scene.position);
28    this._scene.add(this._camera);
29}
30
31NeuronDrawer.prototype._addLight = function () {
32    var directionalLight = new THREE.DirectionalLight(0xffffff);
33
34    directionalLight.position = this._camera.position;
35    directionalLight.intensity = 1;
36
37    this._scene.add(directionalLight);
38}
39
40NeuronDrawer.prototype.initializeScene = function () {
41
42    this._createRenderer();
43    this._scene = new THREE.Scene();
44    this._prepareCamera();
45    this._addLight();
46    this._controls = new THREE.TrackballControls(this._camera, this._renderer.domElement)
47    this._debug();
48}
49
50NeuronDrawer.prototype.renderScene = function () {
51
52    var self = this;
53    requestAnimationFrame(
54        function () {
55            self.renderScene();
56        });
57    this._renderer.render(this._scene, this._camera);
58    this._controls.update();
59
60}
61
62NeuronDrawer.prototype._debug = function()
63{
64    this._scene.add( new THREE.AxisHelper( 20 ) );
65}
66
67NeuronDrawer.prototype.drawNeuron = function(x, y){
68    var scheme = [4,4,26,27,26,73,73,73,73,27,26,27,1,73,50,100,50,1,56,68,46,68,2,41,47,51,32,51,68];
69    //pobieranie schematu
70    //inne określenie położeń
71
72
73    var obj = new THREE.Object3D();
74
75    var material = new THREE.LineBasicMaterial({
76        color: 0x0000ff
77    });
78    var position = 0;
79    var noOfBlocks = 0;//number of "blocks" to draw
80    var noOfLines = 0;//number of line to draw
81    noOfBlocks = scheme[position++];
82    for (var i = 0; i < noOfBlocks; i++)
83    {
84        noOfLines = scheme[position++];
85
86        for (var j = 0; j < noOfLines; j++)
87        {
88            var geometry = new THREE.Geometry();
89            geometry.vertices.push(new THREE.Vector3(scheme[position++] + x, scheme[position++] + y, 0));
90            geometry.vertices.push(new THREE.Vector3(scheme[position++] + x, scheme[position++] + y, 0));
91            position = position - 2; //get to last point in list
92            var line = new THREE.Line(geometry, material);
93            obj.add(line);
94        }
95        position = position + 2;//move to value which define number of lines to draw
96    }
97
98    var SCALE = 0.05;
99
100    obj.scale.set(SCALE, SCALE,SCALE);
101    obj.rotateX(Math.PI);//rotate 180 degree
102    obj.translateY(-5.5);
103    this._scene.add(obj)
104
105}
106
107
108
109NeuronDrawer.prototype.drawConnection = function(id, connections, einfos){
110
111    var destination = connections[id].getDestination();
112    var numberOfConnToDest = 0;
113    var numberOfDrawenCon = 0;
114
115    //check how many conenctions have the same destination
116    for(var i = 0; i < connections.length; i++){
117        if(connections[i].getDestination() == destination)
118            numberOfConnToDest++;
119    }
120
121    //check how many connections had been drawen
122    for(var i = 0; i < id; i++){
123        if(connections[i].getDestination() == destination)
124            numberOfDrawenCon++;
125    }
126
127    var positionY = (numberOfDrawenCon + 1) * 100 / (numberOfConnToDest + 1);
128    var positionX = (numberOfDrawenCon + 1) * 10 / (numberOfConnToDest + 1);
129
130    var x1 = einfos[connections[id].getDestination()].x;
131    var y1 = einfos[connections[id].getDestination()].y;
132
133    var x2 = einfos[connections[id].getSource()].x;
134    var y2 = einfos[connections[id].getSource()].y;
135
136    var material = new THREE.LineBasicMaterial({
137        color: 0x0000ff
138    });
139
140    var SCALE = 0.05;
141
142    var geometry = new THREE.Geometry();
143    geometry.vertices.push(new THREE.Vector3(100 + x2 * 100, 10 + 50 + y2 * 100, 0));
144    geometry.vertices.push(new THREE.Vector3(10 + 100 + x2 * 100 , 10 + 50 + y2 * 100, 0));
145    var line = new THREE.Line(geometry, material);
146    line.scale.set(SCALE, SCALE, SCALE);
147    this._scene.add(line);
148
149    var geometry1 = new THREE.Geometry();
150    geometry1.vertices.push(new THREE.Vector3(10 + positionX + x1 * 100, 10 + positionY + y1 * 100, 0));
151    geometry1.vertices.push(new THREE.Vector3(10 + 25 + x1 * 100, 10 + positionY + y1 * 100, 0));
152    var line1 = new THREE.Line(geometry1, material);
153
154    line1.scale.set(SCALE, SCALE, SCALE);
155    this._scene.add(line1);
156
157    //bezposredni poprzednik - to koniec :)
158    if (x1 == x2 + 1)//kazdy polozony w kolumnie po lewej ma jedna ukosna linie
159    {
160        var geometry = new THREE.Geometry();
161        geometry.vertices.push(new THREE.Vector3(10 + positionX + (x1 * 100), 10 + positionY + y1 * 100, 0));
162        geometry.vertices.push(new THREE.Vector3(110 + x2 * 100, 10 + 50 + y2 * 100, 0));
163        var line = new THREE.Line(geometry, material);
164        line.scale.set(SCALE, SCALE, SCALE);
165        this._scene.add(line);
166        return;
167    }
168
169    //pierwszy pionowy
170    if (y1 >= y2)
171    {
172        var geometry = new THREE.Geometry();
173        geometry.vertices.push(new THREE.Vector3(110 + (x2 * 100), 60 + y2 * 100, 0));
174        geometry.vertices.push(new THREE.Vector3(110 + (x2 * 100), 105 + y2 * 100, 0));
175        var line = new THREE.Line(geometry, material);
176        line.scale.set(SCALE, SCALE, SCALE);
177        this._scene.add(line);
178        return;
179    }
180
181    else
182    {
183        var geometry = new THREE.Geometry();
184        geometry.vertices.push(new THREE.Vector3(110 + (x2 * 100), 60 + y2 * 100, 0));
185        geometry.vertices.push(new THREE.Vector3(110 + (x2 * 100), 15 + y2 * 100, 0));
186        var line = new THREE.Line(geometry, material);
187        line.scale.set(SCALE, SCALE, SCALE);
188        this._scene.add(line);
189        return;
190    }
191
192
193    //poziomy
194    if (y1 >= y2)
195    {
196        var geometry = new THREE.Geometry();
197        geometry.vertices.push(new THREE.Vector3(110 + (x2 * 100), 105 + y2 * 100, 0));
198        geometry.vertices.push(new THREE.Vector3(10 + positionX + (x1 * 100), 105 + y2 * 100, 0));
199        var line = new THREE.Line(geometry, material);
200        line.scale.set(SCALE, SCALE, SCALE);
201        this._scene.add(line);
202        return;
203        //g.drawLine((int) ((110 + (x2 * 100)) * zoom), (int) ((105 + y2 * 100) * zoom), (int) ((10 + polozenieX + (x1 * 100)) * zoom), (int) ((105 + y2 * 100) * zoom));
204    }
205
206    else
207    {
208        var geometry = new THREE.Geometry();
209        geometry.vertices.push(new THREE.Vector3(110 + (x2 * 100), 15 + y2 * 100, 0));
210        geometry.vertices.push(new THREE.Vector3(10 + positionX + (x1 * 100), 15 + y2 * 100, 0));
211        var line = new THREE.Line(geometry, material);
212        line.scale.set(SCALE, SCALE, SCALE);
213        this._scene.add(line);
214        return;
215        //g.drawLine((int) ((110 + (x2 * 100)) * zoom), (int) ((15 + y2 * 100) * zoom), (int) ((10 + polozenieX + (x1 * 100)) * zoom), (int) ((15 + y2 * 100) * zoom));
216    }
217
218
219    //drugi pionowy
220    if (y1 >= y2)
221    {
222        var geometry = new THREE.Geometry();
223        geometry.vertices.push(new THREE.Vector3(10 + positionX + (x1 * 100), 10 + positionY + y1 * 100, 0));
224        geometry.vertices.push(new THREE.Vector3(10 + positionX + (x1 * 100), 105 + y2 * 100, 0));
225        var line = new THREE.Line(geometry, material);
226        line.scale.set(SCALE, SCALE, SCALE);
227        this._scene.add(line);
228        return;
229        //g.drawLine((int) ((10 + polozenieX + (x1 * 100)) * zoom), (int) ((10 + polozenieY + y1 * 100) * zoom), (int) ((10 + polozenieX + (x1 * 100)) * zoom), (int) ((105 + y2 * 100) * zoom));
230    }
231    else
232    {
233        var geometry = new THREE.Geometry();
234        geometry.vertices.push(new THREE.Vector3(10 + positionX + (x1 * 100), 10 + positionY + y1 * 100, 0));
235        geometry.vertices.push(new THREE.Vector3(10 + positionX + (x1 * 100), 15 + y2 * 100, 0));
236        var line = new THREE.Line(geometry, material);
237        line.scale.set(SCALE, SCALE, SCALE);
238        this._scene.add(line);
239        return;
240        //g.drawLine((int) ((10 + polozenieX + (x1 * 100)) * zoom), (int) ((10 + polozenieY + y1 * 100) * zoom), (int) ((10 + polozenieX + (x1 * 100)) * zoom), (int) ((15 + y2 * 100) * zoom));
241    }
242
243}
244
245NeuronDrawer.prototype.drawNetwork = function(neurons, connections, layouts){
246
247    for(var i = 0; i < layouts.length; i++){
248        this.drawNeuron(layouts[i].x * 100, -layouts[i].y * 100);
249    }
250
251    for(var i = 0; i < connections.length; i++)
252        this.drawConnection(i, connections, einfos);
253}
254
255NeuronDrawer.prototype.debugTest = function()
256{
257
258}
259
Note: See TracBrowser for help on using the repository browser.