source: js/viewer-f0/js/SmartLayout.js @ 161

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

Implementation of SmartLayout?

File size: 6.1 KB
Line 
1var neuroSL = null;
2var neuroConnSL = null;
3
4var einfos = [];
5var blocks = [];
6
7var N = null;
8
9function SmartLayout(neu, con)
10{
11    neuroSL = neu;
12    neuroConnSL = con;
13    N = neuroSL.length;
14
15    /////////////////////////
16    //////Faza pierwsza//////
17    /////////////////////////
18    blocks = new Array(N);
19    for(var i = 0; i < N; i++){
20        einfos.push(new einfo())
21    }
22
23    for(var i = 0; i < N; i++)
24        setElement(i);
25
26    /////////////////////////
27    //////Faza druga/////////
28    /////////////////////////
29
30    var first = null;
31    var el;
32    for(el = 0; el < N; el++){
33        if(blocks[el]){
34            first = blocks[el];
35            el++;
36            break;
37        }
38    }
39
40    while(el < N){
41        if((first.maxx - first.minx) > (first.maxy - first.miny)){
42            var y = first.maxy + 2;
43            var x = first.minx;
44            var ex = first.maxx;
45            while (el<N)
46            {
47                if (blocks[el])
48                {
49                    var dx = blocks[el].maxx - blocks[el].minx + 2;
50                    merge(first, blocks[el], x - blocks[el].minx, y - blocks[el].miny);
51                    x += dx;
52                    if (x > ex)
53                        break;
54                }
55                el++;
56            }
57        }
58        else
59        {
60            var x = first.maxx + 2;
61            var y = first.miny;
62            var ey = first.maxy;
63            while (el<N)
64            {
65                if (blocks[el])
66                {
67                    var dy = blocks[el].maxy - blocks[el].miny + 2;
68                    merge(first, blocks[el], x - blocks[el].minx, y - blocks[el].miny);
69                    y += dy;
70                    if (y > ey)
71                        break;
72                }
73                el++;
74            }
75        }
76        }
77
78    //Pokazanie wartości
79    if (first) // at this stage we have a single block containing all neurons
80    {
81        console.log(" - - setting coordinates - -\n");
82        var i;
83        first.createMap();
84        for (i = 0; i < first.elements.length; i++)
85        {
86            el = first.elements[i];
87            console.log(el, einfos[el].x * 70, -1 * einfos[el].y * 70);
88        }
89        delete first;
90    }
91
92}
93
94function einfo(){
95    this.block = null;
96    this.x = null;
97    this.y = null;
98}
99
100function Block(number){
101    this.id = number;
102    this.elements = [];
103    this.w = 1;
104    this.h = 1;
105    this.minx = 0;
106    this.miny = 0;
107    this.maxx = 0;
108    this.maxy = 0;
109    this.addElement(number,0, 0);
110    blocks[this.id] = this;
111    this.map = [];
112}
113
114Block.prototype.destroy = function()
115{
116    blocks[this.id] = 0;
117    this.freeMap();
118}
119
120Block.prototype.freeMap = function(){
121    if(this.map)
122        this.map = [];
123}
124
125Block.prototype.needMap = function(){
126    if(this.map)
127        return;
128    this.createMap();
129
130}
131
132Block.prototype.createMap = function(){
133    this.freeMap();
134    this.w = this.maxx - this.minx + 1;
135    this.h = this.maxy - this.miny + 1;
136    for(var i = 0; i < this.w * this.h; i++)
137        this.map.push(0);
138
139    for (var i = 0; i < this.elements.length; i++)
140    {
141        var e = this.elements[i];
142        this.map[this.w*(einfos[e].y - this.miny) + (einfos[e].x - this.minx)] = 1;
143    }
144}
145
146Block.prototype.addElement = function(number, x, y){
147    this.elements.push(number);
148    einfos[number].x = x;
149    einfos[number].y = y;
150    einfos[number].block = this;
151}
152
153
154
155
156function canMerge(block, block2, dx, dy){
157    var x1, y1, x2, y2;
158
159    x1 = Math.max(0, block2.minx - block.minx + dx);
160    x2 = Math.min(block.maxx - block.minx, -block.minx + dx + block2.maxx);
161    if (x1 > x2)
162        return 1;
163
164
165    y1 = Math.max(0, block2.miny - block.miny + dy);
166    y2 = Math.min(block.maxy - block.miny, -block.miny + dy + block2.maxy);
167    if (y1 > y2)
168        return 1;
169
170
171    var x, y;
172    dx += block2.minx - block.minx; //dx,dy relative to minx,miny
173    dy += block2.miny - block.miny;
174
175    block.createMap();
176    block2.createMap();
177
178    for (y = y1; y <= y2; y++)
179    {
180        for (x = x1; x <= x2; x++)
181            if (block.map[block.w * y + x] && block2.map[block2.w * (y - dy) + (x - dx)])
182                return 0;
183    }
184    return 1;
185
186}
187
188function merge(block, block2, dx, dy){
189
190    if(!canMerge(block, block2, dx, dy))
191        return 0;
192
193    block.freeMap();
194
195
196    for (var i = 0; i < block2.elements.length; i++)
197    {
198        var e = block2.elements[i];
199        block.addElement(e, einfos[e].x + dx, einfos[e].y + dy);
200    }
201
202    block.minx = Math.min(block.minx, dx + block2.minx);
203    block.miny = Math.min(block.miny, dy + block2.miny);
204    block.maxx = Math.max(block.maxx, dx + block2.maxx);
205    block.maxy = Math.max(block.maxy, dy + block2.maxy);
206
207
208    block2.destroy();
209    delete block2;
210    return 1;
211
212}
213
214function connectAsInput(e, e2){
215    var b, b2;
216
217    b = einfos[e].block;
218
219
220    if (!einfos[e2].block)
221        new Block(e2);
222
223    b2 = einfos[e2].block;
224
225    if (b == b2)
226    {
227        return;
228    }
229    var dx, dy;
230    dx = einfos[e].x - einfos[e2].x;
231    dy = einfos[e].y - einfos[e2].y;
232
233    if (merge(b, b2, dx - 1, dy))
234        return;
235
236    for (var proba = 1;; proba++)
237    {
238        if (merge(b, b2, dx - 1, dy - proba))
239            return;
240        if (merge(b, b2, dx - 1, dy + proba))
241            return;
242    }
243}
244
245function setElement(e){
246    if (einfos[e].block != null)
247    {
248        return;
249    }
250
251    new Block(e);
252    //trzeba pobrać elementy
253    var n = getNumOfInCon(e);
254    for (var we = 0; we < n; we++)
255    {
256        var e2 = getLink(e, we);
257        if (e2 < 0)
258            continue;
259        if (e == e2)
260            continue;
261        setElement(e2);
262        connectAsInput(e, e2);
263    }
264}
265
266function getNumOfInCon(nr){
267    var number = 0;
268    for(var i = 0;  i < neuroConnSL.length; i++){
269        if(neuroConnSL[i].getDestination() == nr)
270            number++;
271    }
272    return number;
273}
274
275function getLink(destination, number){
276    var temp = 0;
277    for(var i = 0; i < neuroConnSL.length; i++){
278        if(neuroConnSL[i].getDestination() == destination){
279
280            if(temp == number)
281                return neuroConnSL[i].getSource();
282            else
283                temp++;
284        }
285    }
286}
Note: See TracBrowser for help on using the repository browser.