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

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

New NeuronDrawer? without U-shape in connections

  • Property svn:eol-style set to native
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
153function canMerge(block, block2, dx, dy){
154    var x1, y1, x2, y2;
155
156    x1 = Math.max(0, block2.minx - block.minx + dx);
157    x2 = Math.min(block.maxx - block.minx, -block.minx + dx + block2.maxx);
158    if (x1 > x2)
159        return 1;
160
161
162    y1 = Math.max(0, block2.miny - block.miny + dy);
163    y2 = Math.min(block.maxy - block.miny, -block.miny + dy + block2.maxy);
164    if (y1 > y2)
165        return 1;
166
167
168    var x, y;
169    dx += block2.minx - block.minx; //dx,dy relative to minx,miny
170    dy += block2.miny - block.miny;
171
172    block.createMap();
173    block2.createMap();
174
175    for (y = y1; y <= y2; y++)
176    {
177        for (x = x1; x <= x2; x++)
178            if (block.map[block.w * y + x] && block2.map[block2.w * (y - dy) + (x - dx)])
179                return 0;
180    }
181    return 1;
182
183}
184
185function merge(block, block2, dx, dy){
186
187    if(!canMerge(block, block2, dx, dy))
188        return 0;
189
190    block.freeMap();
191
192
193    for (var i = 0; i < block2.elements.length; i++)
194    {
195        var e = block2.elements[i];
196        block.addElement(e, einfos[e].x + dx, einfos[e].y + dy);
197    }
198
199    block.minx = Math.min(block.minx, dx + block2.minx);
200    block.miny = Math.min(block.miny, dy + block2.miny);
201    block.maxx = Math.max(block.maxx, dx + block2.maxx);
202    block.maxy = Math.max(block.maxy, dy + block2.maxy);
203
204
205    block2.destroy();
206    delete block2;
207    return 1;
208
209}
210
211function connectAsInput(e, e2){
212    var b, b2;
213
214    b = einfos[e].block;
215
216
217    if (!einfos[e2].block)
218        new Block(e2);
219
220    b2 = einfos[e2].block;
221
222    if (b == b2)
223    {
224        return;
225    }
226    var dx, dy;
227    dx = einfos[e].x - einfos[e2].x;
228    dy = einfos[e].y - einfos[e2].y;
229
230    if (merge(b, b2, dx - 1, dy))
231        return;
232
233    for (var proba = 1;; proba++)
234    {
235        if (merge(b, b2, dx - 1, dy - proba))
236            return;
237        if (merge(b, b2, dx - 1, dy + proba))
238            return;
239    }
240}
241
242function setElement(e){
243    if (einfos[e].block != null)
244    {
245        return;
246    }
247
248    new Block(e);
249    //trzeba pobrać elementy
250    var n = getNumOfInCon(e);
251    for (var we = 0; we < n; we++)
252    {
253        var e2 = getLink(e, we);
254        if (e2 < 0)
255            continue;
256        if (e == e2)
257            continue;
258        setElement(e2);
259        connectAsInput(e, e2);
260    }
261}
262
263function getNumOfInCon(nr){
264    var number = 0;
265    for(var i = 0;  i < neuroConnSL.length; i++){
266        if(neuroConnSL[i].getDestination() == nr)
267            number++;
268    }
269    return number;
270}
271
272function getLink(destination, number){
273    var temp = 0;
274    for(var i = 0; i < neuroConnSL.length; i++){
275        if(neuroConnSL[i].getDestination() == destination){
276
277            if(temp == number)
278                return neuroConnSL[i].getSource();
279            else
280                temp++;
281        }
282    }
283}
Note: See TracBrowser for help on using the repository browser.