File: ConnectedComponentPacking.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 (165 lines) | stat: -rw-r--r-- 5,452 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
/**
 *
 * This file is part of Tulip (https://tulip.labri.fr)
 *
 * 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/ConnectedTest.h>
#include <tulip/DoubleProperty.h>
#include <tulip/DrawingTools.h>
#include <tulip/StringCollection.h>

#include "ConnectedComponentPacking.h"
#include "rectanglePackingFonctions.h"
#include "DatasetTools.h"

using namespace std;
using namespace tlp;

PLUGIN(ConnectedComponentPacking)

const float spacing = 8;
#define COMPLEXITY "auto;n5;n4logn;n4;n3logn;n3;n2logn;n2;nlogn;n;"

static const char *paramHelp[] = {
    // initial layout
    "Input layout of nodes and edges.",

    // rotation
    "Input rotation of nodes around the z-axis.",

    // complexity
    "Complexity of the algorithm.<br> n is the number of connected components in the graph."};

//====================================================================
ConnectedComponentPacking::ConnectedComponentPacking(const tlp::PluginContext *context)
    : LayoutAlgorithm(context) {
  addInParameter<LayoutProperty>("initial layout", paramHelp[0], "viewLayout");
  addNodeSizePropertyParameter(this);
  addInParameter<DoubleProperty>("rotation", paramHelp[1], "viewRotation");
  addInParameter<StringCollection>(
      "complexity", paramHelp[2], COMPLEXITY, true,
      "auto <br> n5 <br> n4logn <br> n4 <br> n3logn <br> n3 <br> n2logn <br> n2 <br> nlogn <br> n");
}
//====================================================================
bool ConnectedComponentPacking::run() {

  LayoutProperty *layout = nullptr;
  SizeProperty *size = nullptr;
  DoubleProperty *rotation = nullptr;
  string complexity("auto");

  if (dataSet != nullptr) {
    dataSet->get("initial layout", layout);
    getNodeSizePropertyParameter(dataSet, size);
    dataSet->get("rotation", rotation);
    StringCollection complexityCol;

    if (dataSet->get("complexity", complexityCol))
      complexity = complexityCol.getCurrentString();
  }

  if (layout == nullptr)
    layout = graph->getProperty<LayoutProperty>("viewLayout");

  if (size == nullptr)
    size = graph->getProperty<SizeProperty>("viewSize");

  if (rotation == nullptr)
    rotation = graph->getProperty<DoubleProperty>("viewRotation");

  if (complexity == "none")
    complexity = "auto";

  // compute the connected components
  std::vector<std::vector<node>> ccNodes;
  ConnectedTest::computeConnectedComponents(graph, ccNodes);

  vector<Rectangle<float>> rectangles;
  rectangles.resize(ccNodes.size());
  std::vector<std::vector<edge>> ccEdges;
  ccEdges.resize(ccNodes.size());

  for (unsigned int i = 0; i < ccNodes.size(); ++i) {
    std::vector<edge> &edges = ccEdges[i];
    MutableContainer<bool> visited;
    visited.setAll(false);
    const std::vector<node> &nodes = ccNodes[i];
    unsigned int nbNodes = nodes.size();

    for (unsigned int j = 0; j < nbNodes; ++j) {
      for (auto e : graph->getInOutEdges(nodes[j])) {
        if (!visited.get(e.id)) {
          visited.set(e.id, true);
          edges.push_back(e);
        }
      }
    }

    BoundingBox tmp = tlp::computeBoundingBox(nodes, edges, layout, size, rotation);
    Rectangle<float> &tmpRec = rectangles[i];
    tmpRec[1][0] = tmp[1][0] + spacing;
    tmpRec[1][1] = tmp[1][1] + spacing;
    tmpRec[0][0] = tmp[0][0] + spacing;
    tmpRec[0][1] = tmp[0][1] + spacing;
    assert(tmpRec.isValid());
  }

  if (complexity == "auto") {
    if (rectangles.size() < 25) {
      complexity = "n5";
    } else if (rectangles.size() < 50) {
      complexity = "n4logn";
    } else if (rectangles.size() < 100) {
      complexity = "n4";
    } else if (rectangles.size() < 150) {
      complexity = "n3logn";
    } else if (rectangles.size() < 250) {
      complexity = "n3";
    } else if (rectangles.size() < 500) {
      complexity = "n2logn";
    } else if (rectangles.size() < 1000) {
      complexity = "n2";
    } else if (rectangles.size() < 5000) {
      complexity = "nlogn";
    } else
      complexity = "n";
  }

  vector<Rectangle<float>> rectanglesBackup(rectangles);
  if (!RectanglePackingLimitRectangles(rectangles, complexity.c_str(), pluginProgress))
    return pluginProgress ? pluginProgress->state() != TLP_CANCEL : false;

  for (auto n : graph->nodes()) {
    result->setNodeValue(n, layout->getNodeValue(n));
  }

  for (auto e : graph->edges()) {
    result->setEdgeValue(e, layout->getEdgeValue(e));
  }

  for (unsigned int i = 0; i < ccNodes.size(); ++i) {
    Coord move(rectangles[i][0][0] - rectanglesBackup[i][0][0],
               rectangles[i][0][1] - rectanglesBackup[i][0][1], 0);
    const std::vector<node> &nodes = ccNodes[i];
    StlIterator<node, std::vector<node>::const_iterator> itN(nodes.begin(), nodes.end());
    const std::vector<edge> &edges = ccEdges[i];
    StlIterator<edge, std::vector<edge>::const_iterator> itE(edges.begin(), edges.end());
    result->translate(move, &itN, &itE);
  }

  return true;
}