File: OrthoTree.cpp

package info (click to toggle)
tulip 6.0.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196,224 kB
  • sloc: cpp: 571,851; ansic: 13,983; python: 4,105; sh: 1,555; yacc: 522; xml: 484; makefile: 168; pascal: 148; lex: 55
file content (142 lines) | stat: -rw-r--r-- 4,035 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
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: 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 <vector>
#include <tulip/SizeProperty.h>
#include <tulip/LayoutProperty.h>
#include <tulip/StaticProperty.h>
#include <tulip/TreeTest.h>

using namespace std;
using namespace tlp;

class OrthoTree : public tlp::LayoutAlgorithm {

  unsigned int nodeSpacing;
  unsigned int layerSpacing;
  tlp::SizeProperty *size;
  Graph *tree;

  void computeVerticalSize(const tlp::node n, tlp::NodeStaticProperty<double> &verticalSize);
  void computeLayout(const tlp::node n, tlp::NodeStaticProperty<double> &verticalSize);

public:
  PLUGININFORMATION("OrthoTree", "Romain Bourqui", "20/02/2012", "Orthogonal Tree layout", "1.0",
                    "Tree")

  OrthoTree(const tlp::PluginContext *context);

  bool run() override;
};

PLUGIN(OrthoTree)

//================================================================================
static const char *paramHelp[] = {
    // layer spacing
    "Define the spacing between two successive layers",
    // node spacing
    "Define the spacing between two nodes"};

OrthoTree::OrthoTree(const tlp::PluginContext *context)
    : tlp::LayoutAlgorithm(context), nodeSpacing(4), layerSpacing(10), size(nullptr) {
  addInParameter<unsigned int>("layer spacing", paramHelp[0], "10", true);
  addInParameter<unsigned int>("node spacing", paramHelp[1], "4", true);
}

void OrthoTree::computeVerticalSize(const node n, NodeStaticProperty<double> &verticalSize) {
  auto d = tree->outdeg(n);
  if (d == 0) {
    verticalSize[n] = size->getNodeValue(n)[1];
  } else {
    double s = 0.;
    for (auto u : tree->getOutNodes(n)) {
      computeVerticalSize(u, verticalSize);
      s += verticalSize[u];
    }

    if (d > 1) {
      s += nodeSpacing * (d - 1);
    }

    verticalSize[n] = s;
  }
}

void OrthoTree::computeLayout(const node n, NodeStaticProperty<double> &verticalSize) {
  const Coord &cn = result->getNodeValue(n);
  double prev = 0.;
  for (auto e : tree->getOutEdges(n)) {
    node u = tree->opposite(e, n);
    Coord c(cn[0] + layerSpacing, cn[1] - prev, cn[2]);

    prev += verticalSize[u] + nodeSpacing;
    result->setNodeValue(u, c);

    vector<Coord> bends{Coord(cn[0], c[1], 0)};
    result->setEdgeValue(e, bends);
    computeLayout(u, verticalSize);
  }
}

bool OrthoTree::run() {
  layerSpacing = 10;
  nodeSpacing = 4;

  if (dataSet != nullptr) {
    dataSet->get("layer spacing", layerSpacing);
    dataSet->get("node spacing", nodeSpacing);
  }

  NodeStaticProperty<double> verticalSize(graph, 0);
  size = graph->getProperty<SizeProperty>("viewSize");

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

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

  // preserve result if it not a temporary property
  if (!result->getName().empty())
    propsToPreserve.push_back(result);

  graph->push(false, &propsToPreserve);

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

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

  node root = tree->getSource();
  assert(root.isValid());

  computeVerticalSize(root, verticalSize);

  result->setAllNodeValue(Coord(0, 0, 0));
  result->setAllEdgeValue(vector<Coord>(0));
  computeLayout(root, verticalSize);

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

  return true;
}