File: GraphAttributes.cpp

package info (click to toggle)
bandage 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,684 kB
  • sloc: cpp: 45,359; sh: 491; makefile: 12
file content (1256 lines) | stat: -rw-r--r-- 31,201 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
/*
 * $Revision: 2571 $
 *
 * last checkin:
 *   $Author: gutwenger $
 *   $Date: 2012-07-10 17:25:20 +0200 (Di, 10. Jul 2012) $
 ***************************************************************/

/** \file
 * \brief Implementation of class GraphAttributes.
 *
 * Class GraphAttributes extends a graph by graphical attributes like
 * node position, color, etc.
 *
 * \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
 ***************************************************************/


#include "GraphAttributes.h"
#include "../fileformats/GmlParser.h"
#include "../fileformats/XmlParser.h"


namespace ogdf {

//---------------------------------------------------------
// GraphAttributes
// graph topology + graphical attributes
//---------------------------------------------------------

GraphAttributes::GraphAttributes() : m_pGraph(0), m_directed(true) { }



GraphAttributes::GraphAttributes(const Graph &G, long initAttr) :
	m_pGraph(&G), m_directed(true), m_attributes(0)
{
	initAttributes(m_attributes = initAttr);
}



void GraphAttributes::initAttributes(long attr)
{
	m_attributes |= attr;

	//no color without graphics
	OGDF_ASSERT( (m_attributes & nodeGraphics) != 0 || (m_attributes & nodeColor) == 0);
	//no fill and linewithout graphics
	OGDF_ASSERT( (m_attributes & nodeGraphics) != 0 || (attr & nodeStyle) == 0);
	//no color without graphics
	OGDF_ASSERT( (m_attributes & edgeGraphics) != 0 || (attr & edgeColor) == 0);

	if (attr & nodeGraphics) {
		m_x     .init(*m_pGraph,0.0);
		m_y     .init(*m_pGraph,0.0);
		m_width .init(*m_pGraph,0.0);
		m_height.init(*m_pGraph,0.0);
		m_nodeShape.init(*m_pGraph,rectangle);
	}

	if (attr & nodeColor)
	{
		m_nodeColor.init(*m_pGraph, "");
		m_nodeLine.init(*m_pGraph, "");
	}

	if (attr & nodeStyle)
	{
		m_nodePattern.init(*m_pGraph, bpNone);
		m_nodeStyle.init(*m_pGraph, esSolid);
		m_nodeLineWidth.init(*m_pGraph, 1);
		//images should not be added here as nodestyle, purely experimental
		//such that it fits PG code
		//images:
		m_imageUri.init(*m_pGraph, "");
		m_imageStyle.init(*m_pGraph, GraphAttributes::FreeScale);
		m_imageAlign.init(*m_pGraph, GraphAttributes::Center);
		m_imageDrawLine.init(*m_pGraph, false);
		m_imageWidth.init(*m_pGraph, 0);
		m_imageHeight.init(*m_pGraph, 0);
	}

	if (attr & edgeGraphics) {
		m_bends.init(*m_pGraph,DPolyline());
	}

	if (attr & edgeColor)
		m_edgeColor.init(*m_pGraph);

	if (attr & edgeStyle)
	{
		m_edgeStyle.init(*m_pGraph, esSolid);
		m_edgeWidth.init(*m_pGraph, 1.0);
	}

	if (attr & nodeLevel) {
		m_level.init(*m_pGraph,0);
	}
	if (attr & nodeWeight) {
		m_nodeIntWeight.init(*m_pGraph,0);
	}
	if (attr & edgeIntWeight) {
		m_intWeight.init(*m_pGraph,1);
	}
	if (attr & edgeDoubleWeight) {
		m_doubleWeight.init(*m_pGraph,1.0);
	}
	if (attr & nodeLabel) {
		m_nodeLabel.init(*m_pGraph);
	}
	if (attr & edgeLabel) {
		m_edgeLabel.init(*m_pGraph);
	}
	if (attr & edgeType) {
		m_eType.init(*m_pGraph,Graph::association);//should be Graph::standard end explicitly set
	}
	if (attr & nodeType) {
		m_vType.init(*m_pGraph,Graph::vertex);
	}
	if (attr & nodeId) {
		m_nodeId.init(*m_pGraph, -1);
	}
	if (attr & edgeArrow) {
		m_edgeArrow.init(*m_pGraph, undefined);
	}
	if (attr & nodeTemplate) {
		m_nodeTemplate.init(*m_pGraph);
	}
	if (attr & edgeSubGraph) {
		m_subGraph.init(*m_pGraph,0);
	}
}

void GraphAttributes::destroyAttributes(long attr)
{
	m_attributes &= ~attr;

	if (attr & nodeGraphics) {
		m_x     .init();
		m_y     .init();
		m_width .init();
		m_height.init();
		m_nodeShape.init();
		if (attr & nodeColor)
			m_nodeColor.init();
		if (attr & nodeStyle)
		{
			m_nodePattern.init();
			m_nodeLine.init();
			m_nodeLineWidth.init();
			//should have its own trigger attribute
			//images
			m_imageUri.init();
			m_imageStyle.init();
			m_imageAlign.init();
			m_imageDrawLine.init();
			m_imageWidth.init();
			m_imageHeight.init();
		}
	}

	if (attr & edgeGraphics) {
		m_bends.init();
	}
	if (attr & edgeColor)
	{
		m_edgeColor.init();
	}
	if (attr & edgeStyle)
	{
		m_edgeStyle.init();
		m_edgeWidth.init();
	}

	if (attr & nodeLevel) {
		m_level.init();
	}
	if (attr & nodeWeight) {
		m_nodeIntWeight.init();
	}
	if (attr & edgeIntWeight) {
		m_intWeight.init();
	}
	if (attr & edgeDoubleWeight) {
		m_doubleWeight.init();
	}
	if (attr & nodeLabel) {
		m_nodeLabel.init();
	}
	if (attr & edgeLabel) {
		m_edgeLabel.init();
	}
	if (attr & nodeId) {
		m_nodeId.init();
	}
	if (attr & edgeArrow) {
		m_edgeArrow.init();
	}
	if (attr & nodeTemplate) {
		m_nodeTemplate.init();
	}
	if (attr & edgeSubGraph) {
		m_subGraph.init();
	}
}


void GraphAttributes::init(const Graph &G, long initAttr)
{
	m_pGraph = &G;
	destroyAttributes(m_attributes);
	m_attributes = 0;
	initAttributes(m_attributes = initAttr);
}

void GraphAttributes::setAllWidth(double w)
{
	node v;
	forall_nodes(v,*m_pGraph)
		m_width[v] = w;
}


void GraphAttributes::setAllHeight(double h)
{
	node v;
	forall_nodes(v,*m_pGraph)
		m_height[v] = h;
}


void GraphAttributes::clearAllBends()
{
	edge e;
	forall_edges(e,*m_pGraph)
		m_bends[e].clear();
}


bool GraphAttributes::readGML(Graph &G, const String &fileName)
{
	ifstream is(fileName.cstr());
	if (!is)
		return false; // couldn't open file

	return readGML(G,is);
}


bool GraphAttributes::readGML(Graph &G, istream &is)
{
	GmlParser gml(is);
	if (gml.error())
		return false;

	return gml.read(G,*this);
}


bool GraphAttributes::readRudy(Graph &G, const String &fileName)
{
	ifstream is(fileName.cstr());
	if (!is)
		return false;
	return readRudy(G,is);
}


bool GraphAttributes::readRudy(Graph &G, istream &is)
{
	if (!is)
		return false;
	int i;
	int n, m;
	int src, tgt;
	double weight;
	edge e;

	is >> n >> m;

	G.clear();
	Array<node> mapToNode(0,n-1,0);

	if (attributes() & edgeDoubleWeight){
		for(i=0; i<m; i++) {
			is >> src >> tgt >> weight;
			src--;
			tgt--;
			if(mapToNode[src] == 0) mapToNode[src] = G.newNode(src);
			if(mapToNode[tgt] == 0) mapToNode[tgt] = G.newNode(tgt);
			e = G.newEdge(mapToNode[src],mapToNode[tgt]);
			this->doubleWeight(e)=weight;
		}
	}
	return true;
}


void GraphAttributes::writeRudy(const String &fileName) const
{
	ofstream os(fileName.cstr());
	writeRudy(os);
}


void GraphAttributes::writeRudy(ostream &os) const
{
	const Graph &G = this->constGraph();
	os << G.numberOfNodes() << " " << G.numberOfEdges() << endl;

	edge e;
	if (attributes() & edgeDoubleWeight){
		forall_edges(e,G) {
			os << (e->source()->index())+1 << " " << (e->target()->index())+1;
			os << " " << this->doubleWeight(e) << endl;
		}
	}
	else forall_edges(e,G) os << (e->source()->index())+1 << " " << (e->target()->index())+1 << endl;
}


const int c_maxLengthPerLine = 200;

void GraphAttributes::writeLongString(ostream &os, const String &str) const
{
	os << "\"";

	int num = 1;
	const char *p = str.cstr();
	while(*p != 0)
	{
		switch(*p) {
		case '\\':
			os << "\\\\";
			num += 2;
			break;
		case '\"':
			os << "\\\"";
			num += 2;
			break;

		// ignored white space
		case '\r':
		case '\n':
		case '\t':
			break;

		default:
			os << *p;
			++num;
		}

		if(num >= c_maxLengthPerLine) {
			os << "\\\n";
			num = 0;
		}

		++p;
	}

	os << "\"";
}


void GraphAttributes::writeGML(const String &fileName) const
{
	ofstream os(fileName.cstr());
	writeGML(os);
}


void GraphAttributes::writeGML(ostream &os) const
{
	NodeArray<int> id(*m_pGraph);
	int nextId = 0;

	os.setf(ios::showpoint);
	os.precision(10);

	os << "Creator \"ogdf::GraphAttributes::writeGML\"\n";

	os << "graph [\n";

	os << (m_directed ? "  directed 1\n" : "  directed 0\n");

	node v;
	forall_nodes(v,*m_pGraph) {
		os << "  node [\n";

		os << "    id " << (id[v] = nextId++) << "\n";

		if (attributes() & nodeTemplate) {
			os << "    template ";
			writeLongString(os, templateNode(v));
			os << "\n";
		}

		if (attributes() & nodeLabel) {
			//os << "label \"" << labelNode(v) << "\"\n";
			os << "    label ";
			writeLongString(os, labelNode(v));
			os << "\n";
		}

		if (m_attributes & nodeGraphics) {
			os << "    graphics [\n";
			os << "      x " << m_x[v] << "\n";
			os << "      y " << m_y[v] << "\n";
			os << "      w " << m_width[v] << "\n";
			os << "      h " << m_height[v] << "\n";
			if (m_attributes & nodeColor)
			{
				os << "      fill \"" << m_nodeColor[v] << "\"\n";
				os << "      line \"" << m_nodeLine[v] << "\"\n";
			}//color
			if (m_attributes & nodeStyle)
			{
				os << "      pattern \"" << m_nodePattern[v] << "\"\n";
				os << "      stipple " << styleNode(v) << "\n";
				os << "      lineWidth " << lineWidthNode(v) << "\n";
			}
			switch (m_nodeShape[v])
			{
				case rectangle: os << "      type \"rectangle\"\n"; break;
				case oval: os << "      type \"oval\"\n"; break;
			}
			os << "      width 1.0\n";
			os << "    ]\n"; // graphics
		}

		os << "  ]\n"; // node
	}

	edge e;
	forall_edges(e,*m_pGraph) {
		os << "  edge [\n";

		os << "    source " << id[e->source()] << "\n";
		os << "    target " << id[e->target()] << "\n";

		if (attributes() & edgeLabel){
			os << "    label ";
			writeLongString(os, labelEdge(e));
			os << "\n";
		}
		if (attributes() & edgeType)
			os << "    generalization " << type(e) << "\n";

		if (attributes() & edgeSubGraph)
			os << "    subgraph " << subGraphBits(e) << "\n";

		if (m_attributes & edgeGraphics) {
			os << "    graphics [\n";

			os << "      type \"line\"\n";

			if (attributes() & GraphAttributes::edgeType) {
				if (attributes() & GraphAttributes::edgeArrow) {
					switch(arrowEdge(e)) {
						case GraphAttributes::none:
							os << "      arrow \"none\"\n";
							break;

						case GraphAttributes::last:
							os << "      arrow \"last\"\n";
							break;

						case GraphAttributes::first:
							os << "      arrow \"first\"\n";
							break;

						case GraphAttributes::both:
							os << "      arrow \"both\"\n";
							break;

						case GraphAttributes::undefined:
							// do nothing
							break;

						default:
							// do nothing
							break;
					}
				} else {
					if (type(e) == Graph::generalization)
						os << "      arrow \"last\"\n";
					else
						os << "      arrow \"none\"\n";
				}

			} else { // GraphAttributes::edgeType not used
				if (m_directed) {
					os << "      arrow \"last\"\n";
				} else {
					os << "      arrow \"none\"\n";
				}
			}

			if (attributes() & GraphAttributes::edgeStyle)
			{
				os << "      stipple " << styleEdge(e) << "\n";
				os << "      lineWidth " << edgeWidth(e) << "\n";
			}//edgestyle is gml graphlet extension!!!
			if (attributes() & edgeDoubleWeight)
			{
				os << "      weight " << doubleWeight(e) << "\n";
			}
			//hier noch Unterscheidung Knotentypen, damit die Berechnung
			//fuer Ellipsen immer bis zum echten Rand rechnet
			const DPolyline &dpl = m_bends[e];
			if (!dpl.empty()) {
				os << "      Line [\n";

				node v = e->source();
				if(dpl.front().m_x < m_x[v] - m_width[v]/2 ||
					dpl.front().m_x > m_x[v] + m_width[v]/2 ||
					dpl.front().m_y < m_y[v] - m_height[v]/2 ||
					dpl.front().m_y > m_y[v] + m_height[v]/2)
				{
					os << "        point [ x " << m_x[e->source()] << " y " <<
						m_y[e->source()] << " ]\n";
				}

				ListConstIterator<DPoint> it;
				for(it = dpl.begin(); it.valid(); ++it)
					os << "        point [ x " << (*it).m_x << " y " << (*it).m_y << " ]\n";

				v = e->target();
				if(dpl.back().m_x < m_x[v] - m_width[v]/2 ||
					dpl.back().m_x > m_x[v] + m_width[v]/2 ||
					dpl.back().m_y < m_y[v] - m_height[v]/2 ||
					dpl.back().m_y > m_y[v] + m_height[v]/2)
				{
					os << "        point [ x " << m_x[e->target()] << " y " <<
						m_y[e->target()] << " ]\n";
				}

				os << "      ]\n"; // Line
			}//bends

			//output width and color
			if ((m_attributes & edgeColor) &&
				(m_edgeColor[e].length() != 0))
				os << "      fill \"" << m_edgeColor[e] << "\"\n";

			os << "    ]\n"; // graphics
		}

		os << "  ]\n"; // edge
	}

	os << "]\n"; // graph
}


bool GraphAttributes::readXML(Graph &G, const String &fileName)
{
	ifstream is(fileName.cstr());
	return readXML(G,is);
}


bool GraphAttributes::readXML(Graph &G, istream &is)
{
	// need at least these attributes
	initAttributes(~m_attributes &
		(nodeGraphics | edgeGraphics | nodeLabel | edgeLabel));

	XmlParser xml(is);
	if (xml.error()) return false;

	return xml.read(G,*this);
}



//
// calculates the bounding box of the graph
const DRect GraphAttributes::boundingBox() const
{
	double minx, maxx, miny, maxy;
	const Graph           &G  = constGraph();
	const GraphAttributes &AG = *this;
	node v = G.firstNode();

	if (v == 0) {
		minx = maxx = miny = maxy = 0.0;
	}
	else {
		minx = AG.x(v) - AG.width(v)/2;
		maxx = AG.x(v) + AG.width(v)/2;
		miny = AG.y(v) - AG.height(v)/2;
		maxy = AG.y(v) + AG.height(v)/2;

		forall_nodes(v, G) {
			double x1 = AG.x(v) - AG.width(v)/2;
			double x2 = AG.x(v) + AG.width(v)/2;
			double y1 = AG.y(v) - AG.height(v)/2;
			double y2 = AG.y(v) + AG.height(v)/2;

			if (x1 < minx) minx = x1;
			if (x2 > maxx) maxx = x2;
			if (y1 < miny) miny = y1;
			if (y2 > maxy) maxy = y2;
		}
	}

	edge e;
	forall_edges(e, G) {
		const DPolyline &dpl = AG.bends(e);
		ListConstIterator<DPoint> iter;
		for (iter = dpl.begin(); iter.valid(); ++iter) {
			if ((*iter).m_x < minx) minx = (*iter).m_x;
			if ((*iter).m_x > maxx) maxx = (*iter).m_x;
			if ((*iter).m_y < miny) miny = (*iter).m_y;
			if ((*iter).m_y > maxy) maxy = (*iter).m_y;
		}
	}

	return DRect(minx, miny, maxx, maxy);
}


//
// returns a list of all hierachies in the graph (a hierachy consists of a set of nodes)
// at least one list is returned, which is the list of all nodes not belonging to any hierachy
// this is always the first list
// the return-value of this function is the number of hierachies
int GraphAttributes::hierarchyList(List<List<node>* > &list) const
{
	// list must be empty during startup
	OGDF_ASSERT(list.empty());

	const Graph &G = constGraph();
	Array<bool> processed(0, G.maxNodeIndex(), false);
	node v;
	edge e;

	// initialize the first list of all single nodes
	List<node> *firstList = OGDF_NEW List<node>;
	list.pushBack(firstList);

	forall_nodes(v, G) { // scan all nodes

		// skip, if already processed
		if (processed[v->index()])
			continue;

		List<node> nodeSet;                    // set of nodes in this hierachy,
		// whose neighbours have to be processed
		List<node> *hierachy = OGDF_NEW List<node>; // holds all nodes in this hierachy

		nodeSet.pushBack(v);           // push the unprocessed node to the list
		processed[v->index()] = true;  // and mark it as processed

		do { // scan all neighbours of nodes in 'nodeSet'
			node v = nodeSet.popFrontRet();
			hierachy->pushBack(v); // push v to the list of nodes in this hierachy

			// process all the neighbours of v, e.g. push them into 'nodeSet'
			forall_adj_edges(e, v) {
				if (type(e) == Graph::generalization) {
					node w = e->source() == v ? e->target() : e->source();
					if (!processed[w->index()]) {
						nodeSet.pushBack(w);
						processed[w->index()] = true;
					}
				}
			}
		} while (!nodeSet.empty());

		// skip adding 'hierachy', if it contains only one node
		if (hierachy->size() == 1) {
			firstList->conc(*hierachy);
			delete hierachy;
		}
		else
			list.pushBack(hierachy);
	}

	return list.size() - 1 + (*list.begin())->size();
}


//
// returns a list of all hierarchies in the graph (in this case, a hierarchy consists of a set of edges)
// list may be empty, if no generalizations are used
// the return-value of this function is the number of hierarchies with generalizations
int GraphAttributes::hierarchyList(List<List<edge>* > &list) const
{
	// list must be empty during startup
	OGDF_ASSERT(list.empty());

	const Graph &G = constGraph();
	Array<bool> processed(0, G.maxNodeIndex(), false);
	node v;
	edge e;

	forall_nodes(v, G) { // scan all nodes

		// skip, if already processed
		if (processed[v->index()])
			continue;

		List<node> nodeSet;                    // set of nodes in this hierarchy,
		// whose neighbours have to be processed
		List<edge> *hierarchy = OGDF_NEW List<edge>; // holds all edges in this hierarchy

		nodeSet.pushBack(v);           // push the unprocessed node to the list
		processed[v->index()] = true;  // and mark it as processed

		do { // scan all neighbours of nodes in 'nodeSet'
			node v = nodeSet.popFrontRet();

			// process all the neighbours of v, e.g. push them into 'nodeSet'
			forall_adj_edges(e, v) {
				if (type(e) == Graph::generalization) {
					node w = e->source() == v ? e->target() : e->source();
					if (!processed[w->index()]) {
						nodeSet.pushBack(w);
						processed[w->index()] = true;
						hierarchy->pushBack(e); // push e to the list of edges in this hierarchy
					}
				}
			}
		} while (!nodeSet.empty());

		// skip adding 'hierarchy', if it contains only one node
		if (hierarchy->empty())
			delete hierarchy;
		else
			list.pushBack(hierarchy);
	}

	return list.size();
}



void GraphAttributes::removeUnnecessaryBendsHV()
{
	edge e;
	forall_edges(e,*m_pGraph)
	{
		DPolyline &dpl = m_bends[e];

		if(dpl.size() < 3)
			continue;

		ListIterator<DPoint> it1, it2, it3;

		it1 = dpl.begin();
		it2 = it1.succ();
		it3 = it2.succ();

		do {
			if(((*it1).m_x == (*it2).m_x && (*it2).m_x == (*it3).m_x) ||
				((*it1).m_y == (*it2).m_y && (*it2).m_y == (*it3).m_y))
			{
				dpl.del(it2);
				it2 = it3;
			} else {
				it1 = it2;
				it2 = it3;
			}

			it3 = it2.succ();
		} while(it3.valid());
	}
}


void GraphAttributes::writeXML(
	const String &fileName,
	const char* delimiter,
	const char* offset) const
{
	ofstream os(fileName.cstr());
	writeXML(os,delimiter,offset);
}


void GraphAttributes::writeXML(
	ostream &os,
	const char* delimiter,
	const char* offset) const
{
	NodeArray<int> id(*m_pGraph);

	int nextId = 0;

	os.setf(ios::showpoint);
	os.precision(10);

	os << "<GRAPH TYPE=\"SSJ\">" << delimiter;

	node v;
	forall_nodes(v,*m_pGraph) {
		if (m_attributes & nodeLabel)
		{
			os << "<NODE NAME=\"" << m_nodeLabel[v] << "\">" << delimiter;
		}
		id[v] = nextId++;

		if (m_attributes & nodeGraphics) {
			os << offset << "<POSITION X=\"" << m_x[v] << "\" ";
			os << "Y=\"" << m_y[v] << "\" /> " << delimiter;
			os << offset << "<SIZE WIDTH=\"" << m_width[v] << "\" ";
			os << "HEIGHT=\"" << m_height[v] << "\" />"  << delimiter;

		}
		os << "</NODE>" << delimiter;
	}

	edge e;
	forall_edges(e,*m_pGraph) {
		if (m_attributes & edgeLabel)
		{
			os << "<EDGE NAME=\"" << m_edgeLabel[e] << "\" ";
		}
		if (m_attributes & nodeLabel)
		{
			os << "SOURCE=\"" << m_nodeLabel[e->source()] << "\" ";
			os << "TARGET=\"" << m_nodeLabel[e->target()] << "\" ";
			os << "GENERALIZATION=\"" << (m_eType[e]==Graph::generalization?1:0) << "\">" << delimiter;
		}

		if (m_attributes & edgeGraphics) {

			const DPolyline &dpl = m_bends[e];
			if (!dpl.empty()) {
				os << offset << "<PATH TYPE=\"polyline\">" << delimiter;
/*				if (m_backward[e])
				{
					os << (*dpl.rbegin()).m_x  << " " <<  (*dpl.rbegin()).m_y << " ";
					if (dpl.size() > 1)
						os << (*dpl.begin()).m_x << " " << (*dpl.begin()).m_y << " ";
				}
				else
				{
*/
				ListConstIterator<DPoint> iter;
				for (iter = dpl.begin(); iter.valid(); ++iter)
					os << offset << offset << "<POSITION X=\"" << (*iter).m_x << "\" "
						<< "Y=\"" << (*iter).m_y << "\" />" << delimiter;
//					if (dpl.size() > 1)
//						os << "<POSITION X=\"" << (*dpl.rbegin()).m_x << "\" "
//						   << "Y=\"" << (*dpl.rbegin()).m_y << "\" />";
//				}
				os << offset << "</PATH>" << delimiter;
			}

		}

		os << "</EDGE>" << delimiter; // edge
	}

	os << "</GRAPH>";
}

void GraphAttributes::addNodeCenter2Bends(int mode)
{
	edge e;
	forall_edges(e, *m_pGraph) {
		node v = e->source();
		node w = e->target();
		DPolyline &bendpoints = bends(e);
		switch (mode) {
		case 0 : // push center to the bends and return
			bendpoints.pushFront(DPoint(x(v), y(v)));
			bendpoints.pushBack (DPoint(x(w), y(w)));
			break;
		case 1 : // determine intersection with node and [center, last-bend-point]
			bendpoints.pushFront(DPoint(x(v), y(v)));
			bendpoints.pushBack (DPoint(x(w), y(w)));
		case 2 : // determine intersection between node and last bend-segment
			{
				DPoint sp1(x(v) - width(v)/2, y(v) - height(v)/2);
				DPoint sp2(x(v) - width(v)/2, y(v) + height(v)/2);
				DPoint sp3(x(v) + width(v)/2, y(v) + height(v)/2);
				DPoint sp4(x(v) + width(v)/2, y(v) - height(v)/2);
				DLine sourceRect[4] = {
					DLine(sp1, sp2),
					DLine(sp2, sp3),
					DLine(sp3, sp4),
					DLine(sp4, sp1)
				};

				DPoint tp1(x(w) - width(w)/2, y(w) - height(w)/2);
				DPoint tp2(x(w) - width(w)/2, y(w) + height(w)/2);
				DPoint tp3(x(w) + width(w)/2, y(w) + height(w)/2);
				DPoint tp4(x(w) + width(w)/2, y(w) - height(w)/2);
				DLine targetRect[4] = {
					DLine(tp1, tp2),
					DLine(tp2, tp3),
					DLine(tp3, tp4),
					DLine(tp4, tp1)
				};

				DRect source(sp1, sp3);
				DRect target(tp1, tp3);

				DPoint c1 = bendpoints.popFrontRet();
				DPoint c2 = bendpoints.popBackRet();

				while (!bendpoints.empty() && source.contains(bendpoints.front()))
					c1 = bendpoints.popFrontRet();
				while (!bendpoints.empty() && target.contains(bendpoints.back()))
					c2 = bendpoints.popBackRet();

				DPoint a1, a2;
				int i;
				if (bendpoints.size() == 0) {
					DLine cross(c1, c2);
					for (i = 0; i < 4; i++)
						if (cross.intersection(sourceRect[i], a1)) break;
					for (i = 0; i < 4; i++)
						if (cross.intersection(targetRect[i], a2)) break;
				}
				else {
					DLine cross1(c1, bendpoints.front());
					for (i = 0; i < 4; i++)
						if (cross1.intersection(sourceRect[i], a1)) break;
					DLine cross2(bendpoints.back(), c2);
					for (i = 0; i < 4; i++)
						if (cross2.intersection(targetRect[i], a2)) break;
				}
				bendpoints.pushFront(a1);
				bendpoints.pushBack(a2);
				break;
			}
			OGDF_NODEFAULT
		}
		bendpoints.normalize();
	}
}

/* Methods for OGML serialization */

// static helper method for mapping edge styles to ogml
const char * GraphAttributes::edgeStyleToOGML(const GraphAttributes::EdgeStyle & edgeStyle)
{
	switch (edgeStyle)  {
	case GraphAttributes::esNoPen:
		return "esNoPen";
	case GraphAttributes::esSolid:
		return "esSolid";
	case GraphAttributes::esDash:
		return "esDash";
	case GraphAttributes::esDot:
		return "esDot";
	case GraphAttributes::esDashdot:
		return "esDashdot";
	case GraphAttributes::esDashdotdot:
		return "esDashdotdot";
	default:
		return "esSolid";
	}//switch
}

// static helper method for mapping image alignments to ogml
const char * GraphAttributes::imageAlignmentToOGML(const GraphAttributes::ImageAlignment &imgAlign)
{
	switch (imgAlign)   {
	case GraphAttributes::TopLeft:
		return "topLeft";
	case GraphAttributes::TopCenter:
		return "topCenter";
	case GraphAttributes::TopRight:
		return "topRight";
	case GraphAttributes::CenterLeft:
		return "centerLeft";
	case GraphAttributes::Center:
		return "center";
	case GraphAttributes::CenterRight:
		return "centerRight";
	case GraphAttributes::BottomLeft:
		return "bottomLeft";
	case GraphAttributes::BottomCenter:
		return "bottomCenter";
	case GraphAttributes::BottomRight:
		return "bottomRight";
	default:
		return "center";
	}//switch
}


// static helper method for mapping image style to ogml
const char * GraphAttributes::imageStyleToOGML(const GraphAttributes::ImageStyle &imgStyle)
{
	switch (imgStyle)   {
	case GraphAttributes::FreeScale: return "freeScale";
	case GraphAttributes::FixScale:  return "fixScale";
	default: return "freeScale";
	}//switch
}


// static helper method for mapping brush patterns styles to ogml
const char * GraphAttributes::brushPatternToOGML(const GraphAttributes::BrushPattern & brushPattern)
{
	switch (brushPattern) {
	case GraphAttributes::bpNone:
		return "bpNone";
	case GraphAttributes::bpSolid:
		return "bpSolid";
	case GraphAttributes::bpDense1:
		return "bpDense1";
	case GraphAttributes::bpDense2:
		return "bpDense2";
	case GraphAttributes::bpDense3:
		return "bpDense3";
	case GraphAttributes::bpDense4:
		return "bpDense4";
	case GraphAttributes::bpDense5:
		return "bpDense5";
	case GraphAttributes::bpDense6:
		return "bpDense6";
	case GraphAttributes::bpDense7:
		return "bpDense7";
	case GraphAttributes::bpHorizontal:
		return "bpHorizontal";
	case GraphAttributes::bpVertical:
		return "bpVertical";
	case GraphAttributes::bpCross:
		return "bpCross";
	case GraphAttributes::BackwardDiagonal:
		return "BackwardDiagonal";
	case GraphAttributes::ForwardDiagonal:
		return "ForwardDiagonal";
	case GraphAttributes::DiagonalCross:
		return "DiagonalCross";
	default:
		return "bpSolid";
	}//switch
}


// static helper method for exchanging X(HT)ML-tag specific chars
String GraphAttributes::formatLabel(const String& labelText)
{
	size_t length = labelText.length();
	String formattedString;

	for (size_t i = 0; i < length; ++i) {
		char c = labelText[i];
		if (c == '<') {
			formattedString += "&lt;";
		} else {
			if (c == '>') {
				formattedString += "&gt;";
				if ((i+1 < length) && (labelText[i+1] != '\n'))
					formattedString += '\n';
			} else {
				formattedString += c;
			}
		}
	}
	return formattedString;
}

void GraphAttributes::writeSVG(const String &fileName, int fontSize, const String &fontColor) const
	{
		ofstream os(fileName.cstr());
		writeSVG(os, fontSize, fontColor);
	}

void GraphAttributes::writeSVG(ostream &os, int fontSize, const String &fontColor) const
{
	os.setf(ios::showpoint);
	os.precision(10);

	os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	os << "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:ev=\"http://www.w3.org/2001/xml-events\" version=\"1.1\" baseProfile=\"full\" ";

	// determine bounding box of svg
	OGDF_ASSERT((*m_pGraph).numberOfNodes() > 0);
	double maxX = x((*m_pGraph).firstNode());
	double maxY = y((*m_pGraph).firstNode());
	double minX = x((*m_pGraph).firstNode());
	double minY = y((*m_pGraph).firstNode());
	double nodeStrokeWidth;

	node v;
	forall_nodes(v, *m_pGraph) {
		if (m_attributes & nodeStyle) {
			nodeStrokeWidth = lineWidthNode(v);
		} else {
			nodeStrokeWidth = 1.0;
		}
		maxX = max(maxX, x(v) + m_width[v]/2 + nodeStrokeWidth);
		maxY = max(maxY, y(v) + m_height[v]/2 + nodeStrokeWidth);
		minX = min(minX, x(v) - m_width[v]/2 - nodeStrokeWidth);
		minY = min(minY, y(v) - m_height[v]/2 - nodeStrokeWidth);
	}

	edge e;
	ListConstIterator<DPoint> it;
	double edgeStrokeWidth;
	forall_edges(e, *m_pGraph) {
		if (m_attributes & edgeGraphics) {
			if (attributes() & GraphAttributes::edgeStyle) {
				edgeStrokeWidth = edgeWidth(e);
			} else {
				edgeStrokeWidth = 1.0;
			}
			const DPolyline &dpl = m_bends[e];
			if (!dpl.empty()) {
				for(it = dpl.begin(); it.valid(); ++it) {
					maxX = max(maxX, (*it).m_x + edgeStrokeWidth);
					maxY = max(maxY, (*it).m_y + edgeStrokeWidth);
					minX = min(minX, (*it).m_x - edgeStrokeWidth);
					minY = min(minY, (*it).m_y - edgeStrokeWidth);
				}
			}
		}
	}

	os << "width=\"" << (maxX - minX) << "px\" ";
	os << "height=\"" << (maxY - minY) << "px\" ";
	os << "viewBox=\"" << 0 << " " << 0 << " " << (maxX - minX) << " " << (maxY - minY) << "\">\n";

	forall_edges(e, *m_pGraph) {

		const DPolyline &dpl = m_bends[e];
		if (m_attributes & edgeGraphics) {
			if (!dpl.empty()) { //polyline
				os << "<polyline fill=\"none\" ";

				if ((m_attributes & edgeColor) && (m_edgeColor[e].length() != 0)) {
					os << "stroke=\"" << m_edgeColor[e] << "\" ";
				}

				if (attributes() & GraphAttributes::edgeStyle) {
					os << "stroke-width=\"" << edgeWidth(e) << "px\" ";
				} else {
					os << "stroke=\"#000000\" ";
				}

				os << "points=\"";
				node v = e->source();
				if(dpl.front().m_x < m_x[v] - m_width[v]/2 ||
						dpl.front().m_x > m_x[v] + m_width[v]/2 ||
						dpl.front().m_y < m_y[v] - m_height[v]/2 ||
						dpl.front().m_y > m_y[v] + m_height[v]/2)
				{
					os << (m_x[e->source()] - minX) << "," << (m_y[e->source()] - minY) << " ";
				}

				for(it = dpl.begin(); it.valid(); ++it)
				os << ((*it).m_x - minX) << "," << ((*it).m_y - minY) << " ";

				v = e->target();
				if(dpl.back().m_x < m_x[v] - m_width[v]/2 ||
						dpl.back().m_x > m_x[v] + m_width[v]/2 ||
						dpl.back().m_y < m_y[v] - m_height[v]/2 ||
						dpl.back().m_y > m_y[v] + m_height[v]/2)
				{
					os << (m_x[e->target()] - minX) << "," << (m_y[e->target()] - minY) << " ";
				}

				os << "\"/>\n";
			} else { // single line
				os << "<line ";
				os << "x1=\"" << x(e->source()) - minX << "\" ";
				os << "y1=\"" << y(e->source()) - minY << "\" ";
				os << "x2=\"" << x(e->target()) - minX << "\" ";
				os << "y2=\"" << y(e->target()) - minY<< "\" ";

				if ((m_attributes & edgeColor) && (m_edgeColor[e].length() != 0)) {
					os << "stroke=\"" << m_edgeColor[e] << "\" ";
				} else {
					os << "stroke=\"#000000\" ";
				}

				if (attributes() & GraphAttributes::edgeStyle) {
					os << "stroke-width=\"" << edgeWidth(e) << "px\" ";
				}

				os << "/>\n";
			}
		}
	}

	forall_nodes(v,*m_pGraph) {
		if (m_attributes & nodeGraphics) {
			switch (m_nodeShape[v])
			{
				case rectangle:
				os << "<rect ";
				os << "x=\"" << m_x[v] - minX - m_width[v]/2 << "\" ";
				os << "y=\"" << m_y[v] - minY - m_height[v]/2 << "\" ";
				os << "width=\"" << m_width[v] << "\" ";
				os << "height=\"" << m_height[v] << "\" ";
				break;
				case oval:
				os << "<ellipse ";
				os << "cx=\"" << m_x[v] - minX << "\" ";
				os << "cy=\"" << m_y[v] - minY << "\" ";
				os << "rx=\"" << m_width[v]/2 << "\" ";
				os << "ry=\"" << m_height[v]/2 << "\" ";
				break;
			}

			if (m_attributes & nodeColor) {
				os << "fill=\"" << m_nodeColor[v] << "\" ";
				os << "stroke=\"" << m_nodeLine[v] << "\" ";
			}

			if (m_attributes & nodeStyle)
			{
				os << "stroke-width=\"" << lineWidthNode(v) << "px\" ";
			}

			os << "/>\n";

			if(m_attributes & nodeLabel){
				os << "<text x=\"" << m_x[v] - minX - m_width[v]/2 << "\" y=\"" << m_y[v] - minY << "\" textLength=\"" << m_width[v] << "\" font-size=\"" << fontSize << "\" fill=\"" << fontColor << "\" lengthAdjust=\"spacingAndGlyphs\">" << m_nodeLabel[v] << "</text>\n";
			}
		}
	}

	os << "</svg>\n";
}

} // end namespace ogdf