File: RadialTreeLayout.h

package info (click to toggle)
tulip 4.8.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 179,264 kB
  • ctags: 64,517
  • sloc: cpp: 600,444; ansic: 36,311; makefile: 22,136; python: 1,304; sh: 946; yacc: 522; xml: 337; pascal: 157; php: 66; lex: 55
file content (225 lines) | stat: -rw-r--r-- 6,866 bytes parent folder | download | duplicates (2)
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
/*
 * $Revision: 2566 $
 *
 * last checkin:
 *   $Author: gutwenger $
 *   $Date: 2012-07-07 23:10:08 +0200 (Sat, 07 Jul 2012) $
 ***************************************************************/

/** \file
 * \brief Declaration of linear time layout algorithm for free
 *        trees (class RadialTreeLayout).
 *
 * Based on chapter 3.1.1 Radial Drawings of Graph Drawing by
 * Di Battista, Eades, Tamassia, Tollis.
 *
 * \author Carsten Gutwenger
 *
 * \par License:
 * This file is part of the Open Graph Drawing Framework (OGDF).
 *
 * \par
 * Copyright (C)<br>
 * See README.txt in the root directory of the OGDF installation for details.
 *
 * \par
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 or 3 as published by the Free Software Foundation;
 * see the file LICENSE.txt included in the packaging of this file
 * for details.
 *
 * \par
 * 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.
 *
 * \par
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * \see  http://www.gnu.org/copyleft/gpl.html
 ***************************************************************/


#ifdef _MSC_VER
#pragma once
#endif

#ifndef OGDF_RADIAL_TREE_LAYOUT_H
#define OGDF_RADIAL_TREE_LAYOUT_H

#include <ogdf/module/LayoutModule.h>
#include <ogdf/basic/SList.h>


namespace ogdf {


//! The radial tree layout algorithm.
/**
 * <H3>Optional parameters</H3>
 * Radial tree layout provides the following optional parameters.
 *
 * <table>
 *   <tr>
 *     <th><i>Option</i><th><i>Type</i><th><i>Default</i><th><i>Description</i>
 *   </tr><tr>
 *     <td><i>levelDistance</i><td>double<td>50.0
 *     <td>The minimal vertical distance required between levels.
 *   </tr><tr>
 *     <td><i>connectedComponentDistance</i><td>double<td>50.0
 *     <td>The minimal horizontal distance required between trees in the forest.
 *   </tr><tr>
 *     <td><i>rootSelection</i><td> #RootSelectionType <td> #rootIsCenter
 *     <td>Specifies how to select the root of the tree.
 *   </tr>
 * </table>
*/
class OGDF_EXPORT RadialTreeLayout : public LayoutModule
{
public:
	//! Selection strategies for root of the tree.
	enum RootSelectionType {
		rootIsSource, //!< Select a source in the graph.
		rootIsSink,   //!< Select a sink in the graph.
		rootIsCenter  //!< Select the center of the tree.
	};

private:
	double m_levelDistance;          //!< The minimal distance between levels.
	double m_connectedComponentDistance; //!< The minimal distance between trees.

	RootSelectionType m_selectRoot;  //!< Specifies how to determine the root.

	node            m_root;          //!< The root of the tree.

	int             m_numLevels;     //!< The number of levels (root is on level 0).
	NodeArray<int>  m_level;         //!< The level of a node.
	NodeArray<node> m_parent;        //!< The parent of a node (0 if root).
	NodeArray<double> m_leaves;      //!< The weighted number of leaves in subtree.
	Array<SListPure<node> > m_nodes; //!< The nodes at a level.

	NodeArray<double> m_angle;       //!< The angle of node center (for placement).
	NodeArray<double> m_wedge;       //!< The wedge reserved for subtree.

	NodeArray<double> m_diameter;    //!< The diameter of a circle bounding a node.
	Array<double>     m_width;       //!< The width of a circle.

	Array<double>   m_radius;        //!< The width of a level.
	double          m_outerRadius;   //!< The radius of circle bounding the drawing.

	struct Group
	{
		RadialTreeLayout *m_data;

		bool            m_leafGroup;
		SListPure<node> m_nodes;
		double          m_sumD;
		double          m_sumW;
		double          m_leftAdd;
		double          m_rightAdd;

		Group(RadialTreeLayout *data, node v) {
			m_data = data;
			m_leafGroup = (v->degree() == 1);
			m_nodes.pushBack(v);
			m_sumD = m_data->diameter()[v] + m_data->levelDistance();
			m_sumW = m_data->leaves()[v];
			m_leftAdd = m_rightAdd = 0.0;
		}

		bool isSameType(node v) const {
			return (m_leafGroup == (v->degree() == 1));
		}

		void append(node v) {
			m_nodes.pushBack(v);
			m_sumD += m_data->diameter()[v] + m_data->levelDistance();
			m_sumW += m_data->leaves()[v];
		}

		double add() const { return m_leftAdd + m_rightAdd; }
		node leftVertex () const { return m_nodes.front(); }
		node rightVertex() const { return m_nodes.back (); }
	};

	class Grouping : public List<Group>
	{
	public:
		void computeAdd(double &D, double &W);
	};

	NodeArray<Grouping> m_grouping;

public:
	//! Creates an instance of radial tree layout and sets options to default values.
	RadialTreeLayout();

	//! Copy constructor.
	RadialTreeLayout(const RadialTreeLayout &tl);

	// destructor
	~RadialTreeLayout();

	//! Assignment operator.
	RadialTreeLayout &operator=(const RadialTreeLayout &tl);

	//! Calls the algorithm for graph attributes \a GA.
	/**
	 * The algorithm preserve the order of children which is given by
	 * the adjacency lists.
	 *
	 * \pre The graph is a tree.
	 * @param GA represents the input graph and is assigned the computed layout.
	 */
	void call(GraphAttributes &GA);


	// option that determines the minimal vertical distance
	// required between levels

	//! Returns the option <i>levelDistance</i>.
	double levelDistance() const { return m_levelDistance; }

	//! Sets the option <i>levelDistance</i> to \a x.
	void levelDistance(double x) { m_levelDistance = x; }

	// option that determines the minimal horizontal distance
	// required between trees in the forest

	//! Returns the option <i>connectedComponentDistance</i>.
	double connectedComponentDistance() const { return m_connectedComponentDistance; }

	//! Sets the option <i>connectedComponentDistance</i> to \a x.
	void connectedComponentDistance(double x) { m_connectedComponentDistance = x; }

	// option that determines if the root is on the top or on the bottom

	//! Returns the option <i>rootSelection</i>.
	RootSelectionType rootSelection() const { return m_selectRoot; }

	//! Sets the option <i>rootSelection</i> to \a sel.
	void rootSelection(RootSelectionType sel) { m_selectRoot = sel; }

	const NodeArray<double> &diameter() const { return m_diameter; }
	const NodeArray<double> &leaves()   const { return m_leaves;   }

private:
	void FindRoot(const Graph &G);
	void ComputeLevels(const Graph &G);
	void ComputeDiameters(GraphAttributes &AG);
	void ComputeAngles(const Graph &G);
	void ComputeCoordinates(GraphAttributes &AG);
	void ComputeGrouping(int i);

	OGDF_NEW_DELETE
};

} // end namespace ogdf

#endif