File: GraphRenderer.cpp

package info (click to toggle)
mysql-admin 1.2.5rc-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 80,944 kB
  • ctags: 43,103
  • sloc: sql: 295,916; pascal: 256,535; cpp: 74,487; ansic: 68,881; objc: 26,417; sh: 16,867; yacc: 10,755; java: 9,917; xml: 8,453; php: 2,806; python: 2,068; makefile: 1,252; perl: 3
file content (791 lines) | stat: -rw-r--r-- 17,799 bytes parent folder | download | duplicates (2)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
//#include <windows.h>
//#endif
#include "GraphRenderer.h"

#ifdef __GNUC__
const int GraphRenderer::K1F;
const int GraphRenderer::K1N;
const int GraphRenderer::K2;
const int GraphRenderer::K3;
#endif

/*
// equals for doubles
// algorithm by D.E.Knuth (TAOCP Vol.3 Sec. 4.2.2)
static bool eq(double d1, double d2)
{
  static int delta = 1;

  long long i1 = * (long long*) &d1;
  long long i2 = * (long long*) &d2;

  if(i1 < 0) 
  {
    i1 = -i1;//0x8000000000000000 - i1;
  }
  if(i2 < 0) 
  {
    i2 = -i2;//0x8000000000000000 - i2;
  }

  long long v = i1 - i2;
  if(v < 0)
  {
    v = -v;
  }

  return (v < delta);
}
 */

static inline bool edge_is_special(GraphEdge& edge)
{
  return edge.is_special();
}

static inline void reset_visited(GraphNode *node)
{
  node->set_visited(false);
}

double GraphNode::distance(const GraphNode& n1, const GraphNode& n2)
{
  double xdist, left, right, leftw;
  double ydist, top, bottom, toph;

  if(n1._left < n2._left) 
  {
    left = n1._left;
    leftw = n1._width;
    right = n2._left;
  } 
  else
  {
    left = n2._left;
    leftw = n2._width;
    right = n1._left;
  }

  xdist = right - left - leftw;
  if(xdist <= 0) 
  {
    xdist = 1;
  }

  if(n1._top < n2._top) 
  {
    top = n1._top;
    toph = n1._height;
    bottom = n2._top;
  } 
  else
  {
    top = n2._top;
    toph = n2._height;
    bottom = n1._top;
  }

  ydist = bottom - top - toph;
  if(ydist <= 0) 
  {
    ydist = 1;
  }

  return sqrt(xdist*xdist + ydist*ydist);
}

bool operator == (const GraphNode& n1, const GraphNode& n2) 
{
  return (n1._left == n2._left) && (n1._top == n2._top) && (n1._width == n2._width) && (n1._height == n2._height); 
}

GraphRenderer::GraphRenderer(double margin, double maxwidth, double maxheight) 
  : focus_recalced(false), _length(50), _left(0), _top(0), _right(200), _bottom(200), _avg_force(-1)
{
  _margin= margin;
  _maxw= maxwidth;
  _maxh= maxheight;
}

GraphRenderer::~GraphRenderer()
{
  GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++)
  {
    delete *it;
  }
}

void GraphRenderer::mark_neighbours(const GraphNode& node)
{
  std::for_each(_allnodes.begin(), _allnodes.end(), reset_visited);
  
  for(GraphEdgeList::const_iterator it= _alledges.begin(); it != _alledges.end(); it++)
  {
    GraphEdge e= *it;
    if(e.contains(node))
    {
      e.get_other(node).set_visited(true);    
    }
  }
}

bool GraphRenderer::is_focus_node(const GraphNode& node) const
{
  unsigned counter= 0;

  for(GraphEdgeList::const_iterator edgeit= _alledges.begin(); edgeit != _alledges.end(); edgeit++)
  {
    GraphEdge e= *edgeit;
    if(e.contains(node))
    {
      counter++;
      if(counter > 1)
        return true;
    }
  }

  return false;
}

// horizontal with vertical
static inline bool hv_lines_intersect(double x11, double x12, double y1, double x2, double y21, double y22)
{
  return (x2 >= x11) && (x2 <= x12) && (y1 >= y21) && (y1 <= y22);
}

static inline bool nodes_intersect(double l, double t, double w, double h,
                                   double ol, double ot, double ow, double oh)
{
    // to simplify things
    // this doesn't test full inclusion (which is very improbable)
    return hv_lines_intersect(l, l+w, t, ol, ot, ot+oh)
      || hv_lines_intersect(l, l+w, t+h, ol, ot, ot+oh)
      || hv_lines_intersect(l, l+w, t, ol+ow, ot, ot+oh)
      || hv_lines_intersect(l, l+w, t+h, ol+ow, ot, ot+oh)
      || hv_lines_intersect(ol, ol+ow, ot, l, t, t+h)
      || hv_lines_intersect(ol, ol+ow, ot+oh, l, t, t+h)
      || hv_lines_intersect(ol, ol+ow, ot, l+w, t, t+h)
      || hv_lines_intersect(ol, ol+ow, ot+oh, l+w, t, t+h);
}

bool GraphRenderer::has_intersections() const
{
  GraphNodeRefList::const_iterator e= _allnodes.end();
  for(GraphNodeRefList::const_iterator it= _allnodes.begin(); it != e; it++)
  {
    GraphNode& node= **it;
    double l= node.left();
    double t= node.top();
    double w= node.width();
    double h= node.height();

    GraphNodeRefList::const_iterator it2= it;
    it2++;
    for(; it2 != e; it2++)
    {
      GraphNode& other= **it2;
      double ol= other.left();
      double ot= other.top();
      double ow= other.width();
      double oh= other.height();

      bool ix= nodes_intersect(l, t, w, h, ol, ot, ow, oh);
      if(ix)
        return true;
    }
  }
  return false;
}

void GraphRenderer::get_delta(const GraphNode& node, double *deltax, double *deltay)
{
  static const int NNODES= 2;
  
  mark_neighbours(node);

  double ccx= node.left();
  double ccy= node.top();
  bool node_is_focus= node.is_focus();

  double all_sum_x= 0.;
  double all_sum_y= 0.;

  GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphNodeRefList::const_iterator it= _allnodes.begin(); it != e; it++) 
  {
    const GraphNode* nei[NNODES];
    nei[0]= *it;
    if(nei[0] == &node)
    {
      it++;
      nei[0]= *it;
    }

    if(it == e)
      break;
    
    it++;
    
    nei[1]= *it;
    if(nei[1] == &node)
    {
      it++;
      nei[1]= *it;
    }

    if(it == e)
    {
      nei[1]= nei[0]; // we waste calculations but save if() jumps
      it--;
    }

    double d0, d1;
    
    d0= GraphNode::distance(node, *nei[0]);
    d1= GraphNode::distance(node, *nei[1]);

    if(d0 == 0.)
      d0= 1.;
    if(d1 == 0.)
      d1= 1.;

    double dcx[NNODES];
    double dcy[NNODES];
    
    dcx[0]= ccx - nei[0]->left();
    dcy[0]= ccy - nei[0]->top();
    dcx[1]= ccx - nei[1]->left();
    dcy[1]= ccy - nei[1]->top();

    double vx0, vx1;
    double vy0, vy1;
    vx0= dcx[0];
    vy0= dcy[0];
    vx1= dcx[1];
    vy1= dcy[1];

    double dq0, dq1;
    dq0= d0*d0;
    dq1= d1*d1;
    
    bool neif[NNODES];
    neif[0]= nei[0]->is_focus();
    neif[1]= nei[1]->is_focus();

    vx0 *= GraphRenderer::K2;
    vy0 *= GraphRenderer::K2;
    vx1 *= GraphRenderer::K2;
    vy1 *= GraphRenderer::K2;

    vx0 /= dq0;
    vy0 /= dq0;
    vx1 /= dq1;
    vy1 /= dq1;

    all_sum_x += vx0;
    all_sum_y += vy0;
    all_sum_x += vx1;
    all_sum_y += vy1;

    if(neif[0])
    {
      all_sum_x += vx0;
      all_sum_y += vy0;
    }
    if(neif[1])
    {
      all_sum_x += vx1;
      all_sum_y += vy1;
    }

    if(nei[0]->is_visisted())
    {
      double k= _length - d0;
      
      vx0= k*dcx[0]/d0;
      vy0= k*dcy[0]/d0;

      k= (node_is_focus || neif[0]) ? GraphRenderer::K1F : GraphRenderer::K1N;
      vx0 /= k;
      vy0 /= k;
      
      all_sum_x += vx0;
      all_sum_y += vy0;
   }

    if(nei[1]->is_visisted())
    {
      double k= _length - d1;

      vx1= k*dcx[1]/d1;
      vy1= k*dcy[1]/d1;

      k= (node_is_focus || neif[1]) ? GraphRenderer::K1F : GraphRenderer::K1N;
      vx1 /= k;
      vy1 /= k;
      
      all_sum_x += vx1;
      all_sum_y += vy1;
    }
  }

  static const double t= 10.; // threshold
  static const double s= 4.;  // step

  if(all_sum_x >= t)
    *deltax= s;
  else if(all_sum_x <= -t)
    *deltax= -s;
  else
    *deltax= 0;

  if(all_sum_y >= t)
    *deltay= s;
  else if(all_sum_y <= -t)
    *deltay= -s;
  else
    *deltay= 0;

}

GraphNode *GraphRenderer::add_node(double left, double top, double width, double height) 
{ 
  GraphNode *node= new GraphNode(left, top, width, height);
  _allnodes.push_back(node); 
  focus_recalced= false;
  return node;
}

void GraphRenderer::add_edge(GraphNode *n1, GraphNode *n2) 
{ 
  _alledges.push_back(GraphEdge(*n1, *n2)); 
  focus_recalced= false;
}

void GraphRenderer::add_special_edge(GraphNode *n1, GraphNode *n2) 
{ 
  _alledges.push_back(GraphEdge(*n1, *n2, true)); 
  focus_recalced= false;
}

void GraphRenderer::mark_reachable(GraphNode& node)
{
  if(node.is_visisted())
    return;
  
  node.set_visited(true);

  for(GraphRenderer::GraphEdgeList::iterator it= _alledges.begin(); it != _alledges.end(); it++) 
  {
    GraphEdge& edge= *it;
    if(!edge.contains(node))
      continue;
    GraphNode& other= edge.get_other(node);
    mark_reachable(other);
  }
}

void GraphRenderer::concat_graph(GraphNode& node)
{
  mark_reachable(node);
  
  GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++)
  {
    GraphNode& next= **it;
    if(!next.is_visisted())
    {
      add_special_edge(&node, &next);
      mark_reachable(next);
    }
  }
}

void GraphRenderer::recalc_focus_nodes()
{
  if(focus_recalced)
    return;
  
  GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node = **it;
    node.set_focus(is_focus_node(node));
  }

  // add edges to avoid unconnected nodes/pieces
  std::remove_if(_alledges.begin(), _alledges.end(), edge_is_special);
  std::for_each(_allnodes.begin(), _allnodes.end(), reset_visited);
  if(_allnodes.size() > 0)
  {
    GraphNode* node = _allnodes.front();
    concat_graph(*node);
  }

  focus_recalced= true;
}

void GraphRenderer::recalc_outer_rect()
{
  _left = INT_MAX;
  _top = INT_MAX;
  _right = INT_MIN;
  _bottom = INT_MIN;

  GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node = **it;
    double left = node.left();
    double top = node.top();
    double right = left + node.width();
    double bottom = top + node.height();
    
    if(_left > left) {
      _left = left;
    } 
    if(_right < right) {
      _right = right;
    }
    if(_top > top) {
      _top = top;
    } 
    if(_bottom < bottom) {
      _bottom = bottom;
    } 
  }
}

void GraphRenderer::recalc_positions()
{
  double xf, yf;
  CoordSet cs;
  _avg_force= 0;

  GraphRenderer::GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node= **it;

    if(!node.is_movable())
      continue;

    get_delta(node, &xf, &yf);
    node.setnewleft(node.left() + xf);
    node.setnewtop(node.top() + yf);
    _avg_force += sqrt(xf*xf + yf*yf);
    std::pair<CoordSet::iterator, bool> pr = cs.insert(CoordPair(node.newleft(), node.newtop()));
    while(!pr.second) 
    {
      node.setnewleft(node.newleft() + 1);
      node.setnewtop(node.newtop() + 1);
      pr = cs.insert(CoordPair(node.newleft(), node.newtop()));
    }
  }

  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node = **it;
    if(!node.is_movable())
      continue;
    node.apply();
  }
}

void GraphRenderer::rotate_point(double *x, double *y, double angle)
{
  double x0 = *x;
  double y0 = *y;
  double sina= sin(angle);
  double cosa= cos(angle);
  *x = x0*cosa + y0*sina;
  *y = -x0*sina + y0*cosa;
}

void GraphRenderer::move(double dx, double dy)
{
  GraphRenderer::GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node = **it;
    node.setnewleft(node.left() + dx);
    node.setnewtop(node.top() + dy);
    node.apply();
  }
}

void GraphRenderer::rotate()
{
  static double pi = 3.1415926535;
  static double step = pi/300.;

  double curmsd = 0, posmsd = 0, negmsd = 0;
  double yzero = (_top + _bottom)/2.;
  double xzero = (_left + _right)/2.;

  GraphRenderer::GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node = **it;
    
    if(!node.is_movable())
      continue;

    double x = node.centerx() - xzero;
    double y = node.centery() - yzero;
    double x1 = x;
    double y1 = y;
    double x2 = x;
    double y2 = y;

    rotate_point(&x1, &y1, step);
    rotate_point(&x2, &y2, -step);

    curmsd += y*y;
    posmsd += y1*y1;
    negmsd += y2*y2;
  }

  if(posmsd > negmsd)
  {
    step = -step;
  }

  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node = **it;
    
    if(!node.is_movable())
      continue;

    double x = node.left() - xzero;
    double y = node.top() - yzero;
    rotate_point(&x, &y, step);
    node.setnewleft(x + xzero);
    node.setnewtop( y + yzero);
    node.apply();
  }
}

void GraphRenderer::shift_to_origin()
{
  GraphRenderer::GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node= **it;
    node.setnewleft(node.left() - _left + _margin);
    node.setnewtop(node.top() - _top + _margin);
    node.apply();
  }

  _right -= _left;
  _bottom -= _top;
  _left= _top= 0;
}

void GraphRenderer::scale(double xfactor, double yfactor)
{
  GraphRenderer::GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node= **it;
    node.setnewleft(node.left()*xfactor);
    node.setnewtop(node.top()*yfactor);
    node.apply();
  }
}

bool GraphRenderer::has_nonmovable_nodes() const
{
  GraphRenderer::GraphNodeRefList::const_iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::const_iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node= **it;
    if(!node.is_movable())
      return true;
  }
  return false;
}

void GraphRenderer::scale_up()
{
  double scale_factor_x= 1;
  double scale_factor_y= 1;
  
  GraphNodeRefList::const_iterator e= _allnodes.end();
  for(GraphNodeRefList::const_iterator it= _allnodes.begin(); it != e; it++)
  {
    GraphNode& node= **it;
    double l= node.left();
    double t= node.top();
    double w= node.width();
    double h= node.height();

    GraphNodeRefList::const_iterator it2= it;
    it2++;
    for(; it2 != e; it2++)
    {
      GraphNode& other= **it2;
      double ol= other.left();
      double ot= other.top();
      double ow= other.width();
      double oh= other.height();

      if(nodes_intersect(l, t, w, h, ol, ot, ow, oh))
      {
        double minl= 0;
        double maxl= 0;
        double minw= 0;
        
        if(l < ol)
        {
          minl= l;
          maxl= ol;
          minw= w;
        }
        else
        {
          minl= ol;
          maxl= l;
          minw= ow;
        }

        if((minl + minw + _margin) > maxl)
        {
          double f= (minw + _margin)/(maxl - minl);
          if(f > scale_factor_x)
            scale_factor_x= f;
        }

        if(t < ot)
        {
          minl= t;
          maxl= ot;
          minw= h;
        }
        else
        {
          minl= ot;
          maxl= t;
          minw= oh;
        }

        if((minl + minw + _margin) > maxl)
        {
          double f= (minw + _margin)/(maxl - minl);
          if(f > scale_factor_y)
            scale_factor_y= f;
        }
      }
    }
  }

  scale(scale_factor_x, scale_factor_y);
}

void GraphRenderer::scale_down()
{
  double maxw= _maxw - 2*_margin;
  double maxh= _maxh - 2*_margin;
  
  if((maxw >= (_right - _left)) && (maxh >= (_bottom - _top))) 
    return;
  
  double xfactor= maxw < (_right - _left) ? maxw/(_right - _left) : 1;
  double yfactor= maxh < (_bottom - _top) ? maxh/(_bottom - _top) : 1;

  scale(xfactor, yfactor);
}

void GraphRenderer::recalc()
{
  unsigned count= 0;
  unsigned iter_count= 200;
  bool has_nonmovable= has_nonmovable_nodes();

  double temp_maxw= _maxw;
  double temp_maxh= _maxh;

  _maxw= 200;
  _maxh= 200;
  
  if(!has_nonmovable)
  {
    recalc_outer_rect();
    scale_down();
  }

  _maxw= temp_maxw;
  _maxh= temp_maxh;

  recalc_focus_nodes();

  while(!is_steady() && (count < iter_count))
  {
    recalc_length();
    recalc_positions();
    rotate();
    recalc_outer_rect();
    count++;
  }

  if(!is_steady()) 
  {
    count= 0;
    recalc_focus_nodes();
    while(has_intersections() && (count < iter_count))
    {
      recalc_length();
      recalc_positions();
      rotate();
      recalc_outer_rect();
      count++;
    }
  }

  recalc_outer_rect();
  shift_to_origin();

  if(!has_nonmovable)
  {
    recalc_outer_rect();
    scale_up();

    recalc_outer_rect();
    scale_down();

    recalc_outer_rect();
    shift_to_origin();
  }
}

void GraphRenderer::get_outer_rect(double *left, double *top, double *right, double *bottom)
{
  *left= _left;
  *top= _top;
  *right= _right;
  *bottom= _bottom;
}

void GraphRenderer::recalc_length()
{
  int l[4] = {0};
  _density_ratio = 0.;
  double cx= (_left+_right)/2.;
  double cy= (_top+_bottom)/2.;

  GraphRenderer::GraphNodeRefList::iterator e= _allnodes.end();
  for(GraphRenderer::GraphNodeRefList::iterator it= _allnodes.begin(); it != e; it++) 
  {
    GraphNode& node= **it;
    _density_ratio += node.width()*node.height();
    int idx= 0;
    idx += node.centerx() < cx ? 0 : 1;
    idx += node.centery() < cy ? 0 : 2;
    l[idx]++;
  }
  _density_ratio /= (_right - _left)*(_bottom - _top);
  _length= 1000.*_density_ratio*_density_ratio;
}