File: dia-graph.cpp

package info (click to toggle)
dia 0.98%2Bgit20250126-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 52,072 kB
  • sloc: ansic: 155,381; xml: 14,056; python: 6,250; cpp: 3,598; sh: 439; perl: 137; makefile: 25
file content (209 lines) | stat: -rw-r--r-- 5,117 bytes parent folder | download | duplicates (3)
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
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * dia-graph.cpp -  simple graph algortihms without external help
 *
 * Copyright (c) 2012 Hans Breuer <hans@breuer.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include <config.h>

#include "ogdf-simple.h"

#include "dia-graph.h"

#include <stdio.h>
#include <string.h>
#include <vector>

/*!
 * \brief A simple x,y coodinate
 * \ingroup LayoutPlugin
 */
struct Point
{
  double x;
  double y;
  Point (double _x, double _y) : x(_x), y(_y) {}
};

/*!
 * \brief Another node representation
 * \ingroup LayoutPlugin
 */
struct Node
{
  Point center;
  double width, height;
  Node (double x, double y, double w, double h) :
    center(x, y), width(w), height(h) {}
};

typedef std::vector<Node> Nodes;
/*!
 * \brief The Edge object is just stroing bends here
 * \ingroup LayoutPlugin
 */
typedef std::vector<Point> Edge;
typedef std::vector<Edge> Edges;

/*!
 * \brief Implementing the IGraph interface for simple layout algorithms
 *
 * \ingroup LayoutPlugin
 */
class DiaGraph : public IGraph
{
public :
  void Release ();
  int AddNode (double left, double top, double right, double bottom);
  int AddEdge (int srcNode, int destNode, double* points, int len);
  eResult Layout (const char *module);
  bool GetNodePosition (int node, double* x, double* y);
  int GetEdgeBends (int e, double *coords, int len);
  virtual ~DiaGraph();
protected :
  bool Scale (double xfactor, double yfactor);
private :
  Nodes m_nodes;
  Edges m_edges;
};

DiaGraph::~DiaGraph() {

}

IGraph *
dia_graph_create ()
{
  return new DiaGraph ();
}

void
DiaGraph::Release ()
{
  delete this;
}

int
DiaGraph::AddNode (double left, double top, double right, double bottom)
{
  m_nodes.push_back (Node ((left + right) / 2, (top + bottom) / 2, right - left, bottom - top));
  return m_nodes.size() - 1;
}

int
DiaGraph::AddEdge (int srcNode, int destNode, double* points, int len)
{
  int pos;
  m_edges.push_back (Edge());
  pos = m_edges.size() - 1;
  for (int i = 0; i < len; i+=2)
    m_edges[pos].push_back (Point(points[i], points[i+1]));
  return pos;
}

/*!
 * \brief Invoke the given layout algoritm
 *
 * \ingroup LayoutPlugin
 */
IGraph::eResult
DiaGraph::Layout (const char *module)
{
  // double p1, p2;
  // int n;

  if (strcmp(module, "Grow") == 0)
    return Scale (1.4142, 1.4142) ? SUCCESS : FAILED_ALGORITHM;
  else if (strcmp(module, "Shrink") == 0)
    return Scale (0.7071, 0.7071) ? SUCCESS : FAILED_ALGORITHM;
  else if (strcmp(module, "Heighten") == 0)
    return Scale (1.0, 1.4142) ? SUCCESS : FAILED_ALGORITHM;
  else if (strcmp(module, "Widen") == 0)
    return Scale (1.4142, 1.0) ? SUCCESS : FAILED_ALGORITHM;

  return NO_MODULE;
}

bool
DiaGraph::GetNodePosition (int node, double* x, double* y)
{
  if (node >= 0 && ((size_t) node) < m_nodes.size()) {
    Node &n = m_nodes[node];
    if (x)
      *x = n.center.x - n.width / 2;
    if (y)
      *y = n.center.y - n.height / 2;
    return true;
  }
  return false;
}

int
DiaGraph::GetEdgeBends (int e, double *coords, int len)
{
  if (((size_t) e) >= m_edges.size() || e < 0)
    return 0;
  Edge &edge = m_edges[e];
  if (coords && len > 0) {
    for (size_t i = 0, j = 0; i < ((size_t) len) && j < edge.size(); i+=2, ++j) {
      coords[i  ] = edge[j].x;
      coords[i+1] = edge[j].y;
    }
  }
  return edge.size();
}

/*!
 * \brief Just resizing the graph according to the given scale
 *
 * First the center of gravity is calculated, than nodes and
 * edge bends are moved according to the given scale.
 */
bool
DiaGraph::Scale (double xfactor, double yfactor)
{
  Point cog(0,0);
  double weight(0);
  Nodes::iterator itn;
  Edges::iterator ite;

  for (itn = m_nodes.begin(); itn != m_nodes.end(); ++itn) {
    double w = (*itn).width * (*itn).height;

    cog.x += (w * (*itn).center.x);
    cog.y += (w * (*itn).center.y);
    weight += w;
  }
  cog.x /= weight;
  cog.y /= weight;

  for (itn = m_nodes.begin(); itn != m_nodes.end(); ++itn) {
    (*itn).center.x = cog.x + ((*itn).center.x - cog.x) * xfactor;
    (*itn).center.y = cog.y + ((*itn).center.y - cog.y) * yfactor;
  }
  for (ite = m_edges.begin(); ite != m_edges.end(); ++ite) {
    Edge &e = (*ite);
    Edge::iterator it;
    for (it = e.begin(); it != e.end(); ++it) {
      (*it).x = cog.x + ((*it).x - cog.x) * xfactor;
      (*it).y = cog.y + ((*it).y - cog.y) * yfactor;
    }
  }

  return true;
}