File: ImprovedWalker.cpp

package info (click to toggle)
tulip 4.8.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 179,264 kB
  • ctags: 64,517
  • sloc: cpp: 600,444; ansic: 36,311; makefile: 22,136; python: 1,304; sh: 946; yacc: 522; xml: 337; pascal: 157; php: 66; lex: 55
file content (352 lines) | stat: -rw-r--r-- 11,228 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
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux
 *
 * 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 <tulip/GraphTools.h>
#include "DatasetTools.h"
#include "Orientation.h"
#include "ImprovedWalker.h"

using namespace std;
using namespace tlp;

PLUGIN(ImprovedWalker)

//====================================================================
const node  ImprovedWalker::BADNODE;

//====================================================================
class ImprovedWalkerIterator : public Iterator<node> {
private:
  Graph* graph;
  node        n;
  int         currentChild;
  int         endChild;
  bool        isReversed;

public:
  ImprovedWalkerIterator(Graph* graphParam, node nParam,
                         int currentChildParam, int endChildParam) :
    graph(graphParam), n(nParam), currentChild(currentChildParam),
    endChild(endChildParam) {
    isReversed = currentChild > endChild;
  }

  bool hasNext() {
    return currentChild != endChild;
  }

  node next() {
    node tmp = graph->getOutNode(n, currentChild);

    if (isReversed)
      currentChild--;
    else
      currentChild++;

    return tmp;
  }
};
//====================================================================
ImprovedWalker::ImprovedWalker(const tlp::PluginContext* context) :
  LayoutAlgorithm(context) {
  addNodeSizePropertyParameter(this);
  addOrientationParameters(this);
  addOrthogonalParameters(this);
  addSpacingParameters(this);
}
//====================================================================
ImprovedWalker::~ImprovedWalker() {
}
//====================================================================
bool ImprovedWalker::run() {
  if (pluginProgress)
    pluginProgress->showPreview(false);

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

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

  graph->push(false, &propsToPreserve);

  result->setAllEdgeValue(vector<Coord>());

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

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



  node root = tree->getSource();
  orientationType mask = getMask(dataSet);
  oriLayout = new OrientableLayout(result, mask);
  SizeProperty* size;

  if (!getNodeSizePropertyParameter(dataSet, size))
    size = graph->getProperty<SizeProperty>("viewSize");

  getSpacingParameters(dataSet, nodeSpacing, spacing);

  oriSize                   = new OrientableSizeProxy(size, mask);
  depthMax                  = initializeAllNodes(root);
  order[root]               = 1;

  firstWalk(root);

  // check if the specified layer spacing is greater
  // than the max of the minimum layer spacing of the tree
  for (unsigned int i = 0; i < maxYbyLevel.size() - 1;  ++i) {
    float minLayerSpacing = (maxYbyLevel[i] + maxYbyLevel[i + 1]) / 2.f;

    if (minLayerSpacing + nodeSpacing > spacing)
      spacing = minLayerSpacing + nodeSpacing;
  }

  secondWalk(root,0,0);

  if (hasOrthogonalEdge(dataSet))
    oriLayout->setOrthogonalEdge(tree, spacing);

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

  delete oriLayout;
  delete oriSize;
  return true;
}
//====================================================================
int ImprovedWalker::initializeAllNodes(tlp::node root) {
  return initializeNode(root, 0);
}
//====================================================================
int ImprovedWalker::initializeNode(tlp::node n, unsigned int depth) {
  if (maxYbyLevel.size() == depth) {
    maxYbyLevel.push_back(0);
  }

  float nodeHeight       = oriSize->getNodeValue(n).getH();
  maxYbyLevel[depth]     = max(maxYbyLevel[depth], nodeHeight);

  prelimX[n]             = 0;
  modChildX[n]           = 0;

  shiftNode[n]           = 0;
  shiftDelta[n]          = 0;

  ancestor[n]            = n;
  thread[n]              = BADNODE;

  int maxDepth           = 0;
  int count              = 0;
  Iterator<node>* itNode = tree->getOutNodes(n);

  while (itNode->hasNext()) {
    node currentNode   = itNode->next();
    order[currentNode] = ++count;
    int treeDepth      = initializeNode(currentNode, depth + 1);
    maxDepth           = max(treeDepth, maxDepth);
  }

  delete itNode;

  return maxDepth + 1;
}
//====================================================================
int ImprovedWalker::countSibling(tlp::node from, tlp::node to) {
  return abs(order[from] - order[to]);
}
//====================================================================
ImprovedWalkerIterator* ImprovedWalker::iterateSibling(tlp::node from, tlp::node to) {
  int modifier = (order[from] > order[to] ? 1 : -1 );
  node father  = tree->getInNode(from,1);

  return new ImprovedWalkerIterator(tree,father, order[from],
                                    order[to]+modifier);
}
//====================================================================
Iterator<node>* ImprovedWalker::getChildren(tlp::node n) {
  return tree->getOutNodes(n);
}
//====================================================================
ImprovedWalkerIterator* ImprovedWalker::getReversedChildren(tlp::node n) {
  int nbChildren = tree->outdeg(n);
  return new ImprovedWalkerIterator(tree, n, nbChildren, 0);
}

//====================================================================
void ImprovedWalker::firstWalk(tlp::node v) {
  if (isLeaf(tree, v)) {
    prelimX[v]        = 0;
    node vleftSibling = leftSibling(v);

    if (vleftSibling  != BADNODE)
      prelimX[v]     += prelimX[vleftSibling] + nodeSpacing
                        + oriSize->getNodeValue(v).getW()/2.f
                        + oriSize->getNodeValue(vleftSibling).getW()/2.f;
  }
  else {
    node defaultAncestor    = leftmostChild(v);
    Iterator<node>* itNode  = getChildren(v);

    while (itNode->hasNext()) {
      node currentNode    = itNode->next();
      firstWalk(currentNode);
      combineSubtree(currentNode,&defaultAncestor);
    }

    delete itNode;
    executeShifts(v);

    float midPoint   = (  prelimX[leftmostChild(v)]
                          + prelimX[rightmostChild(v)])/2.f;

    node leftBrother = leftSibling(v);

    if (leftBrother != BADNODE) {
      prelimX[v]   =  prelimX[leftBrother] + nodeSpacing
                      + oriSize->getNodeValue(v).getW()/2.f
                      + oriSize->getNodeValue(leftBrother).getW()/2.f;
      modChildX[v] = prelimX[v] - midPoint;
    }
    else
      prelimX[v] = midPoint;
  }
}
//====================================================================
void ImprovedWalker::secondWalk(tlp::node v, float modifierX, int depth) {
  OrientableCoord coord  = oriLayout->createCoord(prelimX[v]+modifierX,
                           depth * spacing, 0);
  oriLayout->setNodeValue(v,coord);
  Iterator<node>* itNode = getChildren(v);

  while (itNode->hasNext())
    secondWalk(itNode->next(),modifierX+modChildX[v],depth+1);

  delete itNode;
}
//====================================================================
void ImprovedWalker::combineSubtree(tlp::node v, tlp::node* defaultAncestor) {
  node leftBrother = leftSibling(v);

  if (leftBrother != BADNODE) {
    node nodeInsideRight;
    node nodeOutsideRight;
    node nodeInsideLeft;
    node nodeOutsideLeft;

    float shiftInsideRight;
    float shiftOutsideRight;
    float shiftInsideLeft;
    float shiftOutsideLeft;

    nodeInsideRight   = v;
    nodeOutsideRight  = v;
    nodeInsideLeft    = leftBrother;
    nodeOutsideLeft   = leftMostSibling(nodeInsideRight);

    shiftInsideRight  = modChildX[nodeInsideRight];
    shiftOutsideRight = modChildX[nodeOutsideRight];
    shiftInsideLeft   = modChildX[nodeInsideLeft];
    shiftOutsideLeft  = modChildX[nodeOutsideLeft];

    while (nextRightContour(nodeInsideLeft)  != BADNODE &&
           nextLeftContour (nodeInsideRight) != BADNODE) {

      nodeInsideLeft    = nextRightContour(nodeInsideLeft);
      nodeInsideRight   = nextLeftContour(nodeInsideRight);

      if (nodeOutsideLeft.isValid())
        nodeOutsideLeft   = nextLeftContour(nodeOutsideLeft);

      if (nodeOutsideRight.isValid())
        nodeOutsideRight  = nextRightContour(nodeOutsideRight);

      ancestor[nodeOutsideRight] = v;

      float shift =   (prelimX[nodeInsideLeft]  + shiftInsideLeft)
                      - (prelimX[nodeInsideRight] + shiftInsideRight)
                      + nodeSpacing
                      + oriSize->getNodeValue(nodeInsideLeft).getW()/2.f
                      + oriSize->getNodeValue(nodeInsideRight).getW()/2.f;

      if (shift > 0) {
        node ancest = findCommonAncestor(nodeInsideLeft, v,
                                         *defaultAncestor);
        moveSubtree(ancest, v, shift);
        shiftInsideRight   += shift;
        shiftOutsideRight  += shift;
      }

      shiftInsideRight  += modChildX[nodeInsideRight];
      shiftOutsideRight += modChildX[nodeOutsideRight];
      shiftInsideLeft   += modChildX[nodeInsideLeft];
      shiftOutsideLeft  += modChildX[nodeOutsideLeft];
    }

    if (nextRightContour(nodeInsideLeft)   != BADNODE &&
        nextRightContour(nodeOutsideRight) == BADNODE) {
      thread[nodeOutsideRight]    = nextRightContour(nodeInsideLeft);
      modChildX[nodeOutsideRight] += shiftInsideLeft - shiftOutsideRight;
    }

    if (nextLeftContour (nodeInsideRight) != BADNODE &&
        nextLeftContour (nodeOutsideLeft) == BADNODE) {
      thread[nodeOutsideLeft]      =  nextLeftContour(nodeInsideRight);
      modChildX[nodeOutsideLeft]   += shiftInsideRight - shiftOutsideLeft;

      *defaultAncestor              =  v;
    }
  }
}
//====================================================================
void ImprovedWalker::moveSubtree(tlp::node fromNode, tlp::node toNode, float rightShift) {
  int nbElementSubtree  = countSibling(toNode,fromNode);
  float shiftByElem     = rightShift/nbElementSubtree;

  shiftDelta[toNode]    -= shiftByElem;
  shiftNode[toNode]     += rightShift;
  shiftDelta[fromNode]  += shiftByElem;

  prelimX[toNode]       += rightShift;
  modChildX[toNode]     += rightShift;
}
//====================================================================
void ImprovedWalker::executeShifts(tlp::node v) {

  float shift = 0;
  float delta = 0;
  Iterator<node>* itNode = getReversedChildren(v);

  while(itNode->hasNext()) {
    node currentNode       = itNode-> next();

    prelimX[currentNode]   += shift;
    modChildX[currentNode] += shift;

    delta                  += shiftDelta[currentNode];
    shift                  += shiftNode[currentNode] + delta;
  }

  delete itNode;
}
//====================================================================