作図ソフト dia の改良版
修訂 | 59eb35dbbc417dbce3359da492870f210c73ab7c (tree) |
---|---|
時間 | 2012-09-03 03:13:53 |
作者 | Hans Breuer <hans@breu...> |
Commiter | Hans Breuer |
[layout] missing files
@@ -0,0 +1,181 @@ | ||
1 | +/* Dia -- an diagram creation/manipulation program | |
2 | + * Copyright (C) 1998 Alexander Larsson | |
3 | + * | |
4 | + * dia-graph.cpp - simple graph algortihms without external help | |
5 | + * | |
6 | + * Copyright (c) 2012 Hans Breuer <hans@breuer.org> | |
7 | + * | |
8 | + * This program is free software; you can redistribute it and/or modify | |
9 | + * it under the terms of the GNU General Public License as published by | |
10 | + * the Free Software Foundation; either version 2 of the License, or | |
11 | + * (at your option) any later version. | |
12 | + * | |
13 | + * This program is distributed in the hope that it will be useful, | |
14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | + * GNU General Public License for more details. | |
17 | + * | |
18 | + * You should have received a copy of the GNU General Public License | |
19 | + * along with this program; if not, write to the Free Software | |
20 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
21 | + */ | |
22 | +#include <config.h> | |
23 | + | |
24 | +#include "ogdf-simple.h" | |
25 | + | |
26 | +#include "dia-graph.h" | |
27 | + | |
28 | +#include <stdio.h> | |
29 | +#include <vector> | |
30 | + | |
31 | +struct Point | |
32 | +{ | |
33 | + double x; | |
34 | + double y; | |
35 | + Point (double _x, double _y) : x(_x), y(_y) {} | |
36 | +}; | |
37 | + | |
38 | +struct Node | |
39 | +{ | |
40 | + Point center; | |
41 | + double width, height; | |
42 | + Node (double x, double y, double w, double h) : | |
43 | + center(x, y), width(w), height(h) {} | |
44 | +}; | |
45 | + | |
46 | +typedef std::vector<Node> Nodes; | |
47 | +typedef std::vector<Point> Edge; | |
48 | +typedef std::vector<Edge> Edges; | |
49 | + | |
50 | +class DiaGraph : public IGraph | |
51 | +{ | |
52 | +public : | |
53 | + void Release (); | |
54 | + int AddNode (double left, double top, double right, double bottom); | |
55 | + int AddEdge (int srcNode, int destNode, double* points, int len); | |
56 | + eResult Layout (const char *module); | |
57 | + bool GetNodePosition (int node, double* x, double* y); | |
58 | + int GetEdgeBends (int e, double *coords, int len); | |
59 | +protected : | |
60 | + bool Scale (double xfactor, double yfactor); | |
61 | +private : | |
62 | + Nodes m_nodes; | |
63 | + Edges m_edges; | |
64 | +}; | |
65 | + | |
66 | +IGraph * | |
67 | +dia_graph_create () | |
68 | +{ | |
69 | + return new DiaGraph (); | |
70 | +} | |
71 | + | |
72 | +void | |
73 | +DiaGraph::Release () | |
74 | +{ | |
75 | + delete this; | |
76 | +} | |
77 | + | |
78 | +int | |
79 | +DiaGraph::AddNode (double left, double top, double right, double bottom) | |
80 | +{ | |
81 | + m_nodes.push_back (Node ((left + right) / 2, (top + bottom) / 2, right - left, bottom - top)); | |
82 | + return m_nodes.size() - 1; | |
83 | +} | |
84 | + | |
85 | +int | |
86 | +DiaGraph::AddEdge (int srcNode, int destNode, double* points, int len) | |
87 | +{ | |
88 | + int pos; | |
89 | + m_edges.push_back (Edge()); | |
90 | + pos = m_edges.size() - 1; | |
91 | + for (int i = 0; i < len; i+=2) | |
92 | + m_edges[pos].push_back (Point(points[i], points[i+1])); | |
93 | + return pos; | |
94 | +} | |
95 | + | |
96 | +IGraph::eResult | |
97 | +DiaGraph::Layout (const char *module) | |
98 | +{ | |
99 | + double p1, p2; | |
100 | + int n; | |
101 | + | |
102 | + if (strcmp(module, "Grow") == 0) | |
103 | + return Scale (1.4142, 1.4142) ? SUCCESS : FAILED_ALGORITHM; | |
104 | + else if (strcmp(module, "Shrink") == 0) | |
105 | + return Scale (0.7071, 0.7071) ? SUCCESS : FAILED_ALGORITHM; | |
106 | + else if (strcmp(module, "Heighten") == 0) | |
107 | + return Scale (1.0, 1.4142) ? SUCCESS : FAILED_ALGORITHM; | |
108 | + else if (strcmp(module, "Widen") == 0) | |
109 | + return Scale (1.4142, 1.0) ? SUCCESS : FAILED_ALGORITHM; | |
110 | + | |
111 | + return NO_MODULE; | |
112 | +} | |
113 | + | |
114 | +bool | |
115 | +DiaGraph::GetNodePosition (int node, double* x, double* y) | |
116 | +{ | |
117 | + if (node >= 0 && node < m_nodes.size()) { | |
118 | + Node &n = m_nodes[node]; | |
119 | + if (x) | |
120 | + *x = n.center.x - n.width / 2; | |
121 | + if (y) | |
122 | + *y = n.center.y - n.height / 2; | |
123 | + return true; | |
124 | + } | |
125 | + return false; | |
126 | +} | |
127 | + | |
128 | +int | |
129 | +DiaGraph::GetEdgeBends (int e, double *coords, int len) | |
130 | +{ | |
131 | + if (e >= m_edges.size() || e < 0) | |
132 | + return 0; | |
133 | + Edge &edge = m_edges[e]; | |
134 | + if (coords && len > 0) { | |
135 | + for (int i = 0, j = 0; i < len && j < edge.size(); i+=2, ++j) { | |
136 | + coords[i ] = edge[j].x; | |
137 | + coords[i+1] = edge[j].y; | |
138 | + } | |
139 | + } | |
140 | + return edge.size(); | |
141 | +} | |
142 | + | |
143 | +/*! | |
144 | + * \brief Just resizing the graph according to the given scale | |
145 | + * | |
146 | + * First the center of gravity is calculated, than nodes and | |
147 | + * edge bends are moved according to the given scale. | |
148 | + */ | |
149 | +bool | |
150 | +DiaGraph::Scale (double xfactor, double yfactor) | |
151 | +{ | |
152 | + Point cog(0,0); | |
153 | + double weight(0); | |
154 | + Nodes::iterator itn; | |
155 | + Edges::iterator ite; | |
156 | + | |
157 | + for (itn = m_nodes.begin(); itn != m_nodes.end(); ++itn) { | |
158 | + double w = (*itn).width * (*itn).height; | |
159 | + | |
160 | + cog.x += (w * (*itn).center.x); | |
161 | + cog.y += (w * (*itn).center.y); | |
162 | + weight += w; | |
163 | + } | |
164 | + cog.x /= weight; | |
165 | + cog.y /= weight; | |
166 | + | |
167 | + for (itn = m_nodes.begin(); itn != m_nodes.end(); ++itn) { | |
168 | + (*itn).center.x = cog.x + ((*itn).center.x - cog.x) * xfactor; | |
169 | + (*itn).center.y = cog.y + ((*itn).center.y - cog.y) * yfactor; | |
170 | + } | |
171 | + for (ite = m_edges.begin(); ite != m_edges.end(); ++ite) { | |
172 | + Edge &e = (*ite); | |
173 | + Edge::iterator it; | |
174 | + for (it = e.begin(); it != e.end(); ++it) { | |
175 | + (*it).x = cog.x + ((*it).x - cog.x) * xfactor; | |
176 | + (*it).y = cog.y + ((*it).y - cog.y) * yfactor; | |
177 | + } | |
178 | + } | |
179 | + | |
180 | + return true; | |
181 | +} |
@@ -0,0 +1,9 @@ | ||
1 | +#ifndef DIA_GRAPH_H | |
2 | +#define DIA_GRAPH_H | |
3 | + | |
4 | +#include "ogdf-simple.h" | |
5 | + | |
6 | +//! just the factory function for built-in graph algorithms | |
7 | +IGraph *dia_graph_create (); | |
8 | + | |
9 | +#endif |