File: BubbleTree.cpp

package info (click to toggle)
tulip 3.7.0dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 39,428 kB
  • sloc: cpp: 231,403; php: 11,023; python: 1,128; sh: 671; yacc: 522; makefile: 315; xml: 63; lex: 55
file content (396 lines) | stat: -rwxr-xr-x 11,636 bytes parent folder | download
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
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */
#include <algorithm>
#include <tulip/Circle.h>
#include "BubbleTree.h"
#include "DatasetTools.h"

LAYOUTPLUGINOFGROUP(BubbleTree,"Bubble Tree","D.Auber/S.Grivet","16/05/2003","Stable","1.0","Tree");

using namespace std;
using namespace tlp;

struct greaterRadius {
  const std::vector<double> &radius;
  greaterRadius(const std::vector<double> &r):radius(r) {}
  bool operator()(unsigned i1,unsigned i2) const {
    return radius[i1]>radius[i2];
  }
};

double BubbleTree::computeRelativePosition(tlp::node n, TLP_HASH_MAP<tlp::node, tlp::Vector<double, 5 > > *relativePosition) {

  Size tmpSizeFather = nodeSize->getNodeValue(n);
  tmpSizeFather[2] = 0.; //remove z-coordiantes because the drawing is 2D
  double sizeFather = tmpSizeFather.norm() / 2.;

  if (sizeFather < 1E-5) sizeFather = 1.;

  double sizeVirtualNode = 1.;

  if (tree->indeg(n) == 0) sizeVirtualNode = 0.;

  /*
   * Iniatilize node position
   */
  (*relativePosition)[n][0] = 0.;
  (*relativePosition)[n][1] = 0.;

  /*
   * Special case if the node is a leaf.
   */
  if (tree->outdeg(n)==0) {
    (*relativePosition)[n][2] = 0.;
    (*relativePosition)[n][3] = 0.;
    Size tmpSizeNode = nodeSize->getNodeValue(n);
    tmpSizeNode[2] = 0.;
    (*relativePosition)[n][4] = tmpSizeNode.norm() / 2.;
    return (*relativePosition)[n][4];
  }

  /*
   * Recursive call to obtain the set of radius of the children of n
   * A node is dynamically inserted in the neighborhood of n in order to
   * reserve space for the connection of the father of n
   */
  unsigned int Nc = tree->outdeg(n)+1;
  vector<double> angularSector(Nc);
  std::vector<double> realCircleRadius(Nc);
  realCircleRadius[0] = sizeVirtualNode;
  double sumRadius = sizeVirtualNode;

  Iterator<node> *itN=tree->getOutNodes(n);

  for (unsigned int i=1; itN->hasNext(); ++i)  {
    node itn = itN->next();
    realCircleRadius[i] = computeRelativePosition(itn, relativePosition);
    sumRadius += realCircleRadius[i];
  }

  delete itN;


  double resolution = 0.;

  if (nAlgo) {
    std::vector<double> subCircleRadius(Nc);
    subCircleRadius[0] = realCircleRadius[0];
    double maxRadius = sizeVirtualNode;
    unsigned int maxRadiusIndex = 0;

    for (unsigned int i=0; i<Nc; ++i)  {
      subCircleRadius[i] = realCircleRadius[i];

      if (maxRadius < subCircleRadius[i]) {
        maxRadius = subCircleRadius[i];
        maxRadiusIndex = i;
      }
    }

    if ( maxRadius > (sumRadius/2.)) {
      double ratio;

      if (sumRadius - maxRadius > 1E-5)
        ratio = maxRadius / (sumRadius - maxRadius);
      else
        ratio = 1.;

      for (unsigned int i=0; i<Nc; ++i) {
        if (i!=maxRadiusIndex)
          subCircleRadius[i] *= ratio;
      }

      sumRadius = 2. * maxRadius;
    }

    for (unsigned int i = 0; i<Nc; ++i) {
      angularSector[i] = 2. * M_PI * subCircleRadius[i]/sumRadius;
    }
  }
  else {
    resolution = 2. * M_PI;
    std::vector<unsigned> index(Nc);

    for(unsigned i=0; i<Nc; ++i)
      index[i]=i;

    sort(index.begin(), index.end(), greaterRadius(realCircleRadius));
    std::vector<unsigned>::const_iterator  i=index.begin();

    for (; i!=index.end(); ++i) {
      double radius = realCircleRadius[*i];
      double angleMax = 2. * asin(radius/(radius+sizeFather));
      double angle = radius*resolution / sumRadius;

      if (angle>angleMax) {
        angularSector[*i] = angleMax;
        sumRadius -= radius;
        resolution -= angleMax;
      }
      else break;
    }

    if (i!=index.end()) {
      for (; i!=index.end(); ++i) {
        double radius = realCircleRadius[*i];
        double angle = radius*resolution/sumRadius;
        angularSector[*i] = angle;
      }

      resolution = 0.;
    }
    else
      resolution /= Nc;
  }

  double angle = 0.;
  vector<tlp::Circle<double> > circles(Nc);

  for (unsigned int i=0; i<Nc; ++i) {
    double packRadius;

    if (fabs(sin(angularSector[i])) > 1E-05)
      packRadius = realCircleRadius[i] / sin(angularSector[i] /2.);
    else
      packRadius = 0.;

    packRadius = std::max(packRadius, sizeFather + realCircleRadius[i]);

    if (i > 0)
      angle += (angularSector[i-1]+angularSector[i]) / 2. + resolution;

    circles[i][0] = packRadius*cos(angle);
    circles[i][1] = packRadius*sin(angle);
    circles[i].radius = realCircleRadius[i];
  }

  Circle<double> circleH = tlp::enclosingCircle(circles);
  (*relativePosition)[n][2] = -circleH[0];
  (*relativePosition)[n][3] = -circleH[1];
  (*relativePosition)[n][4] = sqrt(circleH.radius*circleH.radius - circleH[1]*circleH[1])-fabs(circleH[0]);
  /*
   * Set relative position of all children
   * according to the center of the enclosing circle
   */
  itN = tree->getOutNodes(n);

  for (unsigned int i=1; i<Nc; ++i) {
    node itn = itN->next();
    (*relativePosition)[itn][0] = circles[i][0]-circleH[0];
    (*relativePosition)[itn][1] = circles[i][1]-circleH[1];
  }

  delete itN;
  return circleH.radius;
}

void BubbleTree::calcLayout2(tlp::node n, TLP_HASH_MAP<tlp::node,tlp::Vector<double, 5 > > *relativePosition,
                             const tlp::Vector<double,3> &enclosingCircleCenter,
                             const tlp::Vector<double,3> &originNodePosition) {
  /*
   * Make rotation around the center of the enclosing circle in order to align :
   * the virtual node, the enclosing circle' center and the grand father of the node.
   */
  Vector<double,3> bend,zeta,zetaOriginal;
  bend.fill(0.);
  bend[0] = (*relativePosition)[n][4];

  zeta[0] = (*relativePosition)[n][2];
  zeta[1] = (*relativePosition)[n][3];
  zeta[2] = 0.;
  zetaOriginal = zeta;

  Vector<double,3> vect,vect3;
  vect = originNodePosition-enclosingCircleCenter;
  vect /= vect.norm();
  vect3 = zeta+bend;
  vect3 /= vect3.norm();

  double cosAlpha,sinAlpha;
  cosAlpha = (vect3.dotProduct(vect));
  sinAlpha = (vect^vect3)[2];

  Vector<double,3> rot1,rot2;
  rot1[0] = cosAlpha;
  rot1[1] = -sinAlpha;
  rot1[2]=0.;
  rot2[0] = sinAlpha;
  rot2[1] =  cosAlpha;
  rot2[2]=0.;
  zeta = rot1*zeta[0] + rot2*zeta[1];

  layoutResult->setNodeValue(n, Coord(static_cast<float>(enclosingCircleCenter[0]+zeta[0]),
                                      static_cast<float>(enclosingCircleCenter[1]+zeta[1]),
                                      0.) );

  /*
   * Place bend on edge to prevent overlaping
   */
  if(tree->outdeg(n)>0) {
    bend += zetaOriginal;
    bend = rot1*bend[0]+rot2*bend[1];
    bend += enclosingCircleCenter;
    Vector<double,3> a = enclosingCircleCenter+zeta-bend;
    Vector<double,3> b = originNodePosition-bend;
    a /= a.norm();
    b /= b.norm();

    if ((1. - fabs(a.dotProduct(b))) > 1E-5) {
      Iterator<edge> *itE=tree->getInEdges(n);
      edge ite=itE->next();
      delete itE;
      vector<Coord>tmp(1);
      tmp[0] = Coord(static_cast<float>(bend[0]), static_cast<float>(bend[1]), 0.);
      layoutResult->setEdgeValue(ite,tmp);
    }
  }

  /*
   * Make the recursive call, to place the children of n.
   */
  Iterator<node> *it=tree->getOutNodes(n);

  while (it->hasNext()) {
    node itn = it->next();
    Vector<double,3> newpos;
    newpos[0] = (*relativePosition)[itn][0];
    newpos[1] = (*relativePosition)[itn][1];
    newpos[2] = 0.;
    newpos = rot1*newpos[0] + rot2*newpos[1];
    newpos += enclosingCircleCenter;
    calcLayout2(itn, relativePosition, newpos, enclosingCircleCenter+zeta);
  }

  delete it;
}

void BubbleTree::calcLayout(tlp::node n, TLP_HASH_MAP< tlp::node, tlp::Vector< double, 5 > >* relativePosition) {
  /*
   * Make the recursive call, to place the children of n.
   */
  layoutResult->setNodeValue(n,Coord(0., 0., 0.));
  Iterator<node> *it = tree->getOutNodes(n);

  while (it->hasNext()) {
    node itn=it->next();
    Coord newpos(static_cast<float>((*relativePosition)[itn][0]-(*relativePosition)[n][2]),
                 static_cast<float>((*relativePosition)[itn][1]-(*relativePosition)[n][3]), 0.f);
    Vector<double,3> origin,tmp;
    origin[0] = (*relativePosition)[itn][0]-(*relativePosition)[n][2];
    origin[1] = (*relativePosition)[itn][1]-(*relativePosition)[n][3];
    origin[2] = 0.;
    tmp.fill(0.);
    calcLayout2(itn, relativePosition, origin, tmp);
  }

  delete it;
}

namespace {
const char * paramHelp[] = {
  //Complexity
  HTML_HELP_OPEN() \
  HTML_HELP_DEF( "type", "bool" ) \
  HTML_HELP_DEF( "values", "[true, false] o(nlog(n)) / o(n)" ) \
  HTML_HELP_DEF( "default", "true" ) \
  HTML_HELP_BODY() \
  "This parameter enables to choose the complexity of the algorithm." \
  HTML_HELP_CLOSE()
};
}

BubbleTree::BubbleTree(const tlp::PropertyContext &context):LayoutAlgorithm(context) {
  addNodeSizePropertyParameter(this);
  addParameter<bool>("complexity",paramHelp[0],"true");
  addDependency<LayoutAlgorithm>("Connected Component Packing", "1.0");
}

BubbleTree::~BubbleTree() {}

bool BubbleTree::run() {

  if (!ConnectedTest::isConnected(graph)) {
    // for each component draw
    std::vector<std::set<node> > components;
    string err;
    // push a temporary graph state (not redoable)
    graph->push(false);
    ConnectedTest::computeConnectedComponents(graph, components);

    for (unsigned int i = 0; i < components.size(); ++i) {
      Graph * tmp = graph->inducedSubGraph(components[i]);
      tmp->computeProperty("Bubble Tree", layoutResult, err, pluginProgress, dataSet);
    }

    // call connected componnent packing
    LayoutProperty tmpLayout(graph);
    DataSet tmpdataSet;
    tmpdataSet.set("coordinates", layoutResult);
    graph->computeProperty("Connected Component Packing", &tmpLayout, err, pluginProgress, &tmpdataSet);
    // forget last temporary graph state
    graph->pop();
    *layoutResult = tmpLayout;
    return true;
  }

  if (!getNodeSizePropertyParameter(dataSet, nodeSize)) {
    if (graph->existProperty("viewSize")) {
      nodeSize = graph->getProperty<SizeProperty>("viewSize");
    }
    else {
      nodeSize = graph->getProperty<SizeProperty>("viewSize");
      nodeSize->setAllNodeValue(Size(1., 1., 1.));
    }
  }

  if (dataSet == 0 || !dataSet->get("complexity",nAlgo))
    nAlgo = true;

  layoutResult->setAllEdgeValue(vector<Coord>(0));

  if (pluginProgress)
    pluginProgress->showPreview(false);

  // push a temporary graph state (not redoable)
  // preserving layout updates
  std::vector<PropertyInterface*> propsToPreserve;

  if (layoutResult->getName() != "")
    propsToPreserve.push_back(layoutResult);

  graph->push(false, &propsToPreserve);

  tree = TreeTest::computeTree(graph, pluginProgress);

  if (pluginProgress && pluginProgress->state() != TLP_CONTINUE) {
    graph->pop();
    return false;
  }

  node startNode = tree->getSource();
  assert(startNode.isValid());
  TLP_HASH_MAP<node,Vector<double,5> > relativePosition;
  computeRelativePosition(startNode, &relativePosition);
  calcLayout(startNode, &relativePosition);

  // forget last temporary graph state
  graph->pop();

  return true;
}