File: ConeTreeExtended.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 (251 lines) | stat: -rwxr-xr-x 8,031 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
/**
 *
 * 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 <tulip/Circle.h>

#include "ConeTreeExtended.h"
#include "DatasetTools.h"

LAYOUTPLUGINOFGROUP(ConeTreeExtended,"Cone Tree","David Auber","01/04/2001","Stable","1.0","Tree");

using namespace std;
using namespace tlp;

//===============================================================
float sqr(float x) {
  return x*x;
}
//===============================================================
float minRadius(float radius1,float alpha1,float radius2,float alpha2) {
  return sqrt(sqr(radius1+radius2)/(sqr(cos(alpha1)-cos(alpha2)) + sqr(sin(alpha1)-sin(alpha2))));
}
//===============================================================
void ConeTreeExtended::computeLayerSize(tlp::node n, unsigned int level) {
  if (levelSize.size() < level+1 )
    levelSize.push_back(0);

  levelSize[level] = std::max(levelSize[level], nodeSize->getNodeValue(n)[1]);
  node i;
  forEach(i, tree->getOutNodes(n)) {
    computeLayerSize(i, level + 1);
  }
}
//===============================================================
void ConeTreeExtended::computeYCoodinates(tlp::node root) {
  levelSize.clear();
  yCoordinates.clear();
  computeLayerSize(root, 0);
  yCoordinates.resize(levelSize.size());
  yCoordinates[0] = 0;

  for (unsigned int i = 1; i < levelSize.size(); ++i) {
    yCoordinates[i] = yCoordinates[i-1] + levelSize[i] / 2.0f + levelSize[i-1] / 2.0f;
  }
}
//===============================================================
double ConeTreeExtended::treePlace3D(tlp::node n,
                                     TLP_HASH_MAP<tlp::node,double> *posRelX,
                                     TLP_HASH_MAP<tlp::node,double> *posRelY) {
  (*posRelX)[n]=0;
  (*posRelY)[n]=0;

  if (tree->outdeg(n)==0) {
    const Coord& tmp = Coord(nodeSize->getNodeValue(n));
    return sqrt(tmp[0]*tmp[0] + tmp[2]*tmp[2])/2.0;
  }

  if (tree->outdeg(n)==1) {
    Iterator<node> *itN=tree->getOutNodes(n);
    node itn=itN->next();
    delete itN;
    return treePlace3D(itn,posRelX,posRelY);
  }

  double sumRadius=0;
  double maxRadius=0;
  float newRadius;

  vector<double> subCircleRadius(tree->outdeg(n));
  Iterator<node> *itN=tree->getOutNodes(n);

  for (int i=0; itN->hasNext(); ++i)  {
    node itn = itN->next();
    subCircleRadius[i] = treePlace3D(itn,posRelX,posRelY);
    sumRadius += 2*subCircleRadius[i];
    maxRadius = std::max(maxRadius , subCircleRadius[i]);
  }

  delete itN;

  double radius=sumRadius/(2*M_PI);

  //treat angles
  vector<double> vangles(subCircleRadius.size());
  double angle=0;
  vangles[0]=0;

  for (unsigned int i=1; i<subCircleRadius.size(); ++i) {
    angle+=(subCircleRadius[i-1]+subCircleRadius[i])/radius;
    vangles[i]=angle;
  }

  // compute new radius
  newRadius=0;

  for (unsigned int i=0; i<subCircleRadius.size()-1; ++i) {
    for (unsigned int j=i+1; j<subCircleRadius.size(); ++j) {
      newRadius = std::max(newRadius , minRadius(static_cast<float>(subCircleRadius[i]),static_cast<float>(vangles[i]),static_cast<float>(subCircleRadius[j]),static_cast<float>(vangles[j])));
    }
  }

  if (newRadius==0) newRadius=static_cast<float>(radius);

  //compute Circle Hull
  vector<tlp::Circle<float> > circles(subCircleRadius.size());

  for (unsigned int i=0; i<subCircleRadius.size(); ++i) {
    circles[i][0]=newRadius*static_cast<float>(cos(vangles[i]));
    circles[i][1]=newRadius*static_cast<float>(sin(vangles[i]));
    circles[i].radius=static_cast<float>(subCircleRadius[i]);
  }

  tlp::Circle<float> circleH=tlp::enclosingCircle(circles);

  //Place relative position
  itN = tree->getOutNodes(n);

  for (unsigned int i=0; i<subCircleRadius.size(); ++i) {
    node itn = itN->next();
    (*posRelX)[itn]=newRadius*cos(vangles[i])-circleH[0];
    (*posRelY)[itn]=newRadius*sin(vangles[i])-circleH[1];
  }

  delete itN;
  return circleH.radius;
}
//===============================================================
void ConeTreeExtended::calcLayout(tlp::node n, TLP_HASH_MAP<tlp::node,double> *px,
                                  TLP_HASH_MAP<tlp::node,double> *py,
                                  double x, double y, int level) {
  layoutResult->setNodeValue(n,Coord(static_cast<float>(x+(*px)[n]), - static_cast<float>(yCoordinates[level]),static_cast<float>(y+(*py)[n])));
  node itn;
  forEach(itn, tree->getOutNodes(n)) {
    calcLayout(itn, px, py, x+(*px)[n], y+(*py)[n], level + 1);
  }
}
//===============================================================
namespace {
const char * paramHelp[] = {
  //Orientation
  HTML_HELP_OPEN()         \
  HTML_HELP_DEF( "type", "String Collection" ) \
  HTML_HELP_DEF( "default", "horizontal" )   \
  HTML_HELP_BODY() \
  "This parameter enables to choose the orientation of the drawing" \
  HTML_HELP_CLOSE()
};
}
#define ORIENTATION "vertical;horizontal;"
//===============================================================
ConeTreeExtended::ConeTreeExtended(const tlp::PropertyContext &context):LayoutAlgorithm(context) {
  addNodeSizePropertyParameter(this);
  addParameter<StringCollection> ("orientation", paramHelp[0], ORIENTATION );
}
//===============================================================
ConeTreeExtended::~ConeTreeExtended() {}
//===============================================================
bool ConeTreeExtended::run() {
  nodeSize = NULL;
  string orientation = "vertical";

  if (dataSet!=0) {
    getNodeSizePropertyParameter(dataSet, nodeSize);
    StringCollection tmp;

    if (dataSet->get("orientation", tmp)) {
      orientation = tmp.getCurrentString();
    }
  }

  if (!nodeSize)
    nodeSize = graph->getProperty<SizeProperty>("viewSize");

  //=========================================================
  //rotate size if needed
  //will be undone at then end
  if (orientation == "horizontal") {
    node n;
    forEach(n, graph->getNodes()) {
      const Size& tmp = nodeSize->getNodeValue(n);
      nodeSize->setNodeValue(n, Size(tmp[1], tmp[0], tmp[2]));
    }
  }

  //===========================================================
  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 root = tree->getSource();
  assert(root.isValid());
  TLP_HASH_MAP<node,double> posX;
  TLP_HASH_MAP<node,double> posY;
  treePlace3D(root,&posX,&posY);
  computeYCoodinates(root);
  calcLayout(root,&posX,&posY,0,0,0);

  //rotate layout and size
  if (orientation == "horizontal") {
    node n;
    forEach(n, graph->getNodes()) {
      // if not in tulip gui, ensure cleanup
      LayoutProperty* elementLayout;

      if (!graph->getAttribute("viewLayout", elementLayout)) {
        const Size&  tmp = nodeSize->getNodeValue(n);
        nodeSize->setNodeValue(n, Size(tmp[1], tmp[0], tmp[2]));
      }

      const Coord& tmpC = layoutResult->getNodeValue(n);

      layoutResult->setNodeValue(n, Coord(-tmpC[1], tmpC[0], tmpC[2]));
    }
  }

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

  return true;
}