VTKWriter_dist_graph.hpp 28.4 KB
Newer Older
tonynsyde's avatar
tonynsyde committed
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
/*
 * VTKWriter_graph.hpp
 *
 *  Created on: May 5, 2015
 *      Author: Pietro Incardona
 */

#ifndef VTKWRITER_DIST_GRAPH_HPP_
#define VTKWRITER_DIST_GRAPH_HPP_

/*! Property data store for scalar and vector
 *
 */
template<bool is_array>
struct vtk_dist_vertex_node_array_scalar_selector
{
	/*! /brief Print the geometric informations in case it is not an array
	 *
	 * \tparam T Type of the vertex
	 * \tparam ele_v Attribute element to check
	 * \tparam G Graph of reference
	 * \tparam s_type Vertex spatial type information
	 *
	 * \param vo Vertex object container
	 * \param x Array to store geometric informations
	 * \param z_set Value set to true id z axis is in use
	 */
	template<typename T, typename ele_v, typename G, typename s_type>
	static inline void move(typename G::V_container &vo, s_type (&x)[3], bool &z_set)
	{
		if (G::V_type::attributes::name[T::value] == "x")
		{
			x[0] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());
		}
		else if (G::V_type::attributes::name[T::value] == "y")
		{
			x[1] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());
		}
		else if (G::V_type::attributes::name[T::value] == "z")
		{
			x[2] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>())>::type>::template to<s_type>(vo.template get<T::value>());
			z_set = true;
		}
	}
};

/*! Template specialization in the case of array type attribute
 *
 */
template<>
struct vtk_dist_vertex_node_array_scalar_selector<true>
{
	/*! \brief Store the geometric informations in case it is an array
	 *
	 * \tparam T Type of the vertex
	 * \tparam ele_v Attribute element to check
	 * \tparam G Graph of reference
	 * \tparam s_type Vertex spatial type information
	 *
	 * \param vo Vertex object container
	 * \param x Array to store geometric informations
	 * \param z_set Value set to true id z axis is in use
	 */
	template<typename T, typename ele_v, typename G, typename s_type>
	static inline void move(typename G::V_container &vo, s_type (&x)[3], bool &z_set)
	{
		if (std::extent<ele_v>::value == 3)
			z_set = true;

		for (size_t i = 0; i < std::extent<ele_v>::value; i++)
			x[i] = convert<typename boost::remove_reference<decltype(vo.template get<T::value>()[i])>::type>::template to<s_type>(vo.template get<T::value>()[i]);

	}
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to create a string containing all the vertex
 * properties
 *
 * \tparam G graph type
 * \tparam attr has the vertex attributes
 *
 */

template<typename G, bool attr>
struct vtk_dist_vertex_node
{
	// Vertex spatial type information
	typedef typename G::V_type::s_type s_type;

	bool z_set;

	s_type (&x)[3];

	// Vertex object container
	typename G::V_container & vo;

	// vertex node string
	std::string & v_node;

	/*! \brief Constructor
	 *
	 * Create a vertex properties list
	 *
	 * \param v_node std::string that is filled with the graph properties in the GraphML format
	 * \param n_obj object container to access its properties for example encapc<...>
	 *
	 */
	vtk_dist_vertex_node(std::string & v_node, typename G::V_container & n_obj, s_type (&x)[3]) :
			x(x), vo(n_obj), v_node(v_node), z_set(false)
	{
	}
	;

	//! \brief Write collected information
	void write()
	{
		v_node += std::to_string(x[0]) + " " + std::to_string(x[1]) + " " + std::to_string(x[2]) + "\n";
	}

	//! It call the functor for each member
	template<typename T>
	void operator()(T& t)
	{
		typedef typename boost::mpl::at<typename G::V_type::type, boost::mpl::int_<T::value>>::type ele_v;

		// if the attribute name is x y or z, create a string with the value of the properties and store it

		vtk_dist_vertex_node_array_scalar_selector<std::is_array<ele_v>::value>::template move<T, ele_v, G, s_type>(vo, x, z_set);

	}
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to create a string containing all the vertex
 * properties
 *
 * Specialization when we do not have vertex attributes
 *
 * \tparam G graph type
 *
 */

template<typename G>
struct vtk_dist_vertex_node<G, false>
{
	// Vertex object container
	typename G::V_container & vo;

	// vertex node string
	std::string & v_node;

	/*! \brief Constructor
	 *
	 * Create a vertex properties list
	 *
	 * \param v_node std::string that is filled with the graph properties in the GraphML format
	 * \param n_obj object container to access its properties for example encapc<...>
	 *
	 */
	vtk_dist_vertex_node(std::string & v_node, typename G::V_container & n_obj) :
			vo(n_obj), v_node(v_node)
	{
	}
	;

	//! It call the functor for each member
	template<typename T>
	void operator()(T& t)
	{
		v_node += "0 0 0\n";
	}
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to create a string containing all the edge
 * properties
 *
 */

template<typename G>
struct vtk_dist_edge_node
{
	// Vertex object container
	typename G::E_container & vo;

	// edge node string
	std::string & e_node;

	/*! \brief Constructor
	 *
	 * Create an edge node
	 *
	 * \param e_node std::string that is filled with the graph properties in the GraphML format
	 * \param n_obj object container to access the object properties for example encapc<...>
	 * \param n_prop number of properties
	 *
	 */
	vtk_dist_edge_node(std::string & e_node, typename G::E_container & n_obj) :
			vo(n_obj), e_node(e_node)
	{
	}
	;

	/*! \brief Create a new node
	 *
	 * \param vc node number
	 *
	 */
	void new_node(size_t v_c, size_t s, size_t d)
	{
		// start a new node
		e_node += "2 " + std::to_string(s) + " " + std::to_string(d) + "\n";
	}
};

/*! \brief Property writer for scalar and vector
 *
 */
template<bool is_array>
struct dist_prop_output_array_scalar_selector_vertex
{
	/*! \brief Writer in case the property is not an array
	 *
	 * \tparam ele_v Property element
	 * \tparam Graph Graph of reference
	 * \tparam i Property id
	 *
	 * \param v_out Buffer to write into
	 * \param g Graph
	 * \param p Property id
	 */
	template<typename ele_v, typename Graph, unsigned int i>
	static inline void write(std::string &v_out, const Graph &g, size_t p)
	{
		v_out += std::to_string(g.vertex(p).template get<i>()) + "\n";
	}
};

/*! \brief Property writer for vector
 *
 */
template<>
struct dist_prop_output_array_scalar_selector_vertex<true>
{
	/*! \brief Writer in case the property is an array
	 *
	 * \tparam ele_v Property element
	 * \tparam Graph Graph of reference
	 * \tparam i Property id
	 *
	 * \param v_out Buffer to write into
	 * \param g Graph
	 * \param p Property id
	 */
	template<typename ele_v, typename Graph, unsigned int i>
	static inline void write(std::string &v_out, const Graph &g, size_t p)
	{
		for (size_t j = 0; j < 2; j++)
		{
			v_out += std::to_string(g.vertex(p).template get<i>()[j]) + " ";
		}

		if (std::extent<ele_v>::value == 2)
			v_out += "0";
		else
			v_out += std::to_string(g.vertex(p).template get<i>()[2]);

		v_out += "\n";
	}
};

/*! \brief Property writer for scalar and vector
 *
 */
template<bool is_array>
struct dist_prop_output_array_scalar_selector_edge
{
	/*! \brief Writer in case the property is not an array
	 *
	 * \tparam ele_v Property element
	 * \tparam Graph Graph of reference
	 * \tparam i Property id
	 *
	 * \param v_out Buffer to write into
	 * \param g Graph
	 * \param p Property id
	 */
	template<typename ele_v, typename Graph, unsigned int i>
	static inline void write(std::string &v_out, const Graph &g, const typename Graph::E_container &edge)
	{
		v_out += std::to_string(edge.template get<i>()) + "\n";
	}
};

/*! \brief Property writer for vector
 *
 */
template<>
struct dist_prop_output_array_scalar_selector_edge<true>
{
	/*! \brief Writer in case the property is an array
	 *
	 * \tparam ele_v Property element
	 * \tparam Graph Graph of reference
	 * \tparam i Property id
	 *
	 * \param v_out Buffer to write into
	 * \param g Graph
	 * \param p Property id
	 */
	template<typename ele_v, typename Graph, unsigned int i>
	static inline void write(std::string &v_out, const Graph &g, const typename Graph::E_container &edge)
	{
		for (size_t j = 0; j < 2; j++)
		{
			v_out += std::to_string(edge.template get<i>()[j]) + " ";
		}

		if (std::extent<ele_v>::value == 2)
			v_out += "0";
		else
			v_out += std::to_string(edge.template get<i>()[2]);

		v_out += "\n";
	}
};

/*! \brief Property writer for scalar and vector, it fill the vertex data (needed for edge representation in vtk)
 *
 */
template<bool is_array>
struct dist_prop_output_array_scalar_selector_edge_fill_vertex
{
	/*! \brief Writer in case the property is not an array
	 *
	 * \param v_out Buffer to write into
	 */
	static inline void write(std::string &v_out)
	{
		v_out += "0\n";
	}
};

/*! \brief Property writer for vector
 *
 */
template<>
struct dist_prop_output_array_scalar_selector_edge_fill_vertex<true>
{
	/*! \brief Writer in case the property is an array
	 *
	 * \param v_out Buffer to write into
	 */
	static inline void write(std::string &v_out)
	{
		v_out += "0 0 0\n";
	}
};

/*! \brief This class specialize functions in the case the type T
 * has or not defined attributes
 *
 * In C++ partial specialization of a function is not allowed so we have to
 * encapsulate this function in a class
 *
 * \tparam has_attributes parameter that specialize the function in case the vertex
 *         define or not attributes name
 *
 * \tparam Graph type of graph we are processing
 * \tparam i the property we are going to write
 *
 */

template<bool has_attributes, typename Graph, unsigned int i>
class dist_prop_output
{
public:

	/*! \brief For each vertex set the value
	 *
	 * \tparam i vertex property to print
	 *
	 */

	static std::string get_point_data(const Graph & g)
	{
		//! vertex node output string
		std::string v_out;

		//! Get a vertex iterator
		auto it = g.getVertexIterator();

		// if there is the next element
		while (it.isNext())
		{
			typedef typename boost::mpl::at<typename Graph::V_type::type, boost::mpl::int_<i>>::type ele_v;
			dist_prop_output_array_scalar_selector_vertex<std::is_array<ele_v>::value>::template write<ele_v, Graph, i>(v_out, g, it.get());

			// increment the iterator and counter
			++it;
		}

		return v_out;
	}

	/*! \brief For each edge set the value, set 1 on vertices, needed by vtk file format
	 *
	 * \tparam i edge property to print
	 *
	 */

	static std::string get_cell_data(const Graph & g)
	{
		//! vertex node output string
		std::string e_out;

		//! Get a vertex iterator
		auto it_v = g.getVertexIterator();

		// if there is the next element
		while (it_v.isNext())
		{
			// Print the property
			typedef typename boost::mpl::at<typename Graph::E_type::type, boost::mpl::int_<i>>::type ele_v;
			dist_prop_output_array_scalar_selector_edge_fill_vertex<std::is_array<ele_v>::value>::write(e_out);

			// increment the iterator and counter
			++it_v;
		}

		//! Get an edge iterator
		auto it_e = g.getEdgeIterator();

		// if there is the next element
		while (it_e.isNext())
		{
			typedef typename boost::mpl::at<typename Graph::E_type::type, boost::mpl::int_<i>>::type ele_v;
			dist_prop_output_array_scalar_selector_edge<std::is_array<ele_v>::value>::template write<ele_v, Graph, i>(e_out, g, g.edge(it_e.get()));

			// increment the iterator and counter
			++it_e;
		}

		return e_out;
	}

	/*! \brief Given a Graph return the point data header for a typename T
	 *
	 * \tparam T type to write
	 * \param n_node number of the node
	 *
	 */

	static std::string get_point_property_header(size_t prop)
	{
		//! vertex node output string
		std::string v_out;

		// Type of the property
		std::string type;

		typedef typename boost::mpl::at<typename Graph::V_type::type, boost::mpl::int_<i>>::type T;

		// Check if T is a supported format
		// for now we support only scalar of native type
		if (std::is_array<T>::value == true && std::is_array<typename std::remove_extent<T>::type>::value == false)
		{
			//Get type of the property
			type = getType<typename std::remove_all_extents<T>::type>();

			// if the type is not supported return
			if (type.size() == 0)
				return v_out;

			// Create point data properties
			v_out += "VECTORS " + get_attributes_vertex() + " " + type + "\n";
		}
		else
		{
			type = getType<T>();

			// if the type is not supported return
			if (type.size() == 0)
				return v_out;

			// Create point data properties
			v_out += "SCALARS " + get_attributes_vertex() + " " + type + "\n";

			// Default lookup table
			v_out += "LOOKUP_TABLE default\n";

		}

		// return the vertex list
		return v_out;
	}

	/*! \brief Given a Graph return the cell data header for a typename T
	 *
	 * \tparam T type to write
	 * \param n_node number of the node
	 *
	 */

	static std::string get_cell_property_header(size_t prop)
	{
		//! edge node output string
		std::string e_out;

		// Type of the property
		std::string type;

		typedef typename boost::mpl::at<typename Graph::E_type::type, boost::mpl::int_<i>>::type T;

		// Check if T is a supported format
		// for now we support only scalar of native type
		if (std::is_array<T>::value == true && std::is_array<typename std::remove_extent<T>::type>::value == false)
		{
			//Get type of the property
			type = getType<typename std::remove_all_extents<T>::type>();

			// if the type is not supported return
			if (type.size() == 0)
				return e_out;

			// Create point data properties
			e_out += "VECTORS " + get_attributes_edge() + " " + type + "\n";
		}
		else
		{
			type = getType<T>();

			// if the type is not supported return
			if (type.size() == 0)
				return e_out;

			// Create point data properties
			e_out += "SCALARS " + get_attributes_edge() + " " + type + "\n";

			// Default lookup table
			e_out += "LOOKUP_TABLE default\n";

		}

		// return the vertex list
		return e_out;
	}

	/*! \brief Get the attributes name for vertex
	 *
	 */

	static std::string get_attributes_vertex()
	{
		return Graph::V_type::attributes::name[i];
	}

	/*! \brief Get the attributes name for edge
	 *
	 */

	static std::string get_attributes_edge()
	{
		return Graph::E_type::attributes::name[i];
	}
};

/*! \brief This class specialize functions in the case the type T
 * has not defined attributes
 *
 * In C++ partial specialization of a function is not allowed so we have to
 * encapsulate this function in a class
 *
 * \tparam has_attributes parameter that specialize the function in case the vertex
 *         define or not attributes name
 *
 * \tparam i id of the property we are going to write
 *
 */

template<typename Graph, unsigned int i>
class dist_prop_output<false, Graph, i>
{
	/*! \brief For each vertex set the value
	 *
	 * \tparam i vertex property to print
	 *
	 */

	static std::string get_point_data(Graph & g)
	{
		//! vertex node output string
		std::string v_out;

		//! Get a vertex iterator
		auto it = g.getVertexIterator();

		// if there is the next element
		while (it.isNext())
		{
			// Print the property
			v_out += std::to_string(g.vertex(it.get()).template get<i>()) + "\n";

			// increment the iterator and counter
			++it;
		}

		return v_out;
	}

	/*! \brief For each edge set the value
	 *
	 * \tparam i edge property to print
	 *
	 */

	static std::string get_cell_data(const Graph & g)
	{
		//! vertex node output string
		std::string e_out;

		//! Get a vertex iterator
		auto it_v = g.getVertexIterator();

		// if there is the next element
		while (it_v.isNext())
		{
			// Print the property
			e_out += std::to_string(0) + "\n";

			// increment the iterator and counter
			++it_v;
		}

		//! Get an edge iterator
		auto it_e = g.getEdgeIterator();

		// if there is the next element
		while (it_e.isNext())
		{
			// Print the property
			e_out += std::to_string(g.edge(it_e.get()).template get<i>()) + "\n";

			// increment the iterator and counter
			++it_e;
		}

		return e_out;
	}

	/*! \brief Given a Graph return the point data header for a typename T
	 *
	 * \tparam T type to write
	 *
	 * \param n_node number of the node
	 * \param prop id of the property
	 *
	 */

	static std::string get_point_property_header(size_t prop)
	{
		//! vertex node output string
		std::string v_out;

		// Check if T is a supported format
		// for now we support only scalar of native type

		std::string type = getType<boost::fusion::result_of::at<typename Graph::V_type::type, boost::mpl::int_<i>>>("attr" + std::to_string(prop));

		// if the type is not supported return
		if (type.size() == 0)
		{
			return v_out;
		}

		// Create point data properties
		v_out += "SCALARS " + get_attributes_vertex() + " " + type + "\n";

		// Default lookup table
		v_out += "LOOKUP_TABLE default\n";

		// return the vertex list
		return v_out;
	}

	/*! \brief Given a Graph return the cell data header for a typename T
	 *
	 * \param n_node number of the node
	 *
	 */

	static std::string get_cell_property_header(size_t prop)
	{
		//! edge node output string
		std::string e_out;

		// Type of the property
		std::string type;

		typedef typename boost::mpl::at<typename Graph::E_type::type, boost::mpl::int_<i>>::type T;

		// Check if T is a supported format
		// for now we support only scalar of native type
		if (std::is_array<T>::value == true && std::is_array<typename std::remove_extent<T>::type>::value == false)
		{
			//Get type of the property
			type = getType<typename std::remove_all_extents<T>::type>();

			// if the type is not supported return
			if (type.size() == 0)
				return e_out;

			// Create point data properties
			e_out += "VECTORS " + get_attributes_edge() + " " + type + "\n";
		}
		else
		{
			type = getType<T>();

			// if the type is not supported return
			if (type.size() == 0)
				return e_out;

			// Create point data properties
			e_out += "SCALARS " + get_attributes_edge() + " " + type + "\n";

			// Default lookup table
			e_out += "LOOKUP_TABLE default\n";

		}

		// return the vertex list
		return e_out;
	}

	/*! \brief Get the attributes name for vertex
	 *
	 */

	static std::string get_attributes_vertex()
	{
		return Graph::V_type::attributes::name[i];
	}

	/*! \brief Get the attributes name for edge
	 *
	 */

	static std::string get_attributes_edge()
	{
		return Graph::E_type::attributes::name[i];
	}
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to produce at output for each property
 *
 * \tparam Graph graph we are processing
 *
 * \param dim Dimensionality
 * \param S type of grid
 *
 */

template<typename Graph>
struct dist_prop_out_vertex
{
	// property output string
	std::string & v_out;

	// Graph that we are processing
	const Graph & g;

	/*! \brief constructor
	 *
	 * \param v_out string to fill with the vertex properties
	 *
	 */
	dist_prop_out_vertex(std::string & v_out, const Graph & g) :
			v_out(v_out), g(g)
	{
	}
	;

	//! It produce an output for each property
	template<typename T>
	void operator()(T& t) const
	{
		// actual string size
		size_t sz = v_out.size();

		// Produce the point properties header
		v_out += dist_prop_output<has_attributes<typename Graph::V_type>::value, Graph, T::value>::get_point_property_header(t);

		// If the output has changed, we have to write the properties
		if (v_out.size() != sz)
		{
			std::string attr = dist_prop_output<has_attributes<typename Graph::V_type>::value, Graph, T::value>::get_attributes_vertex();

			// Produce point data
			v_out += dist_prop_output<has_attributes<typename Graph::V_type>::value, Graph, T::value>::get_point_data(g);
		}
	}
};

/*! \brief this class is a functor for "for_each" algorithm
 *
 * This class is a functor for "for_each" algorithm. For each
 * element of the boost::vector the operator() is called.
 * Is mainly used to produce at output for each property
 *
 * \tparam Graph graph we are processing
 *
 * \param dim Dimensionality
 * \param S type of grid
 *
 */

template<typename Graph>
struct dist_prop_out_edge
{
	// property output string
	std::string & e_out;

	// Graph that we are processing
	const Graph & g;

	/*! \brief constructor
	 *
	 * \param v_out string to fill with the vertex properties
	 *
	 */
	dist_prop_out_edge(std::string & e_out, const Graph & g) :
			e_out(e_out), g(g)
	{
	}
	;

	//! It produce an output for each property
	template<typename T>
	void operator()(T& t) const
	{
		// actual string size
		size_t sz = e_out.size();

		// Produce the point properties header
		e_out += dist_prop_output<has_attributes<typename Graph::E_type>::value, Graph, T::value>::get_cell_property_header(t);

		// If the output has changed, we have to write the properties
		if (e_out.size() != sz)
		{
			std::string attr = dist_prop_output<has_attributes<typename Graph::E_type>::value, Graph, T::value>::get_attributes_edge();

			// Produce cell data
			e_out += dist_prop_output<has_attributes<typename Graph::E_type>::value, Graph, T::value>::get_cell_data(g);
		}
	}
};

/*!
 *
 * It write a VTK format file in case for a graph
 *
 * \tparam Type of graph
 *
 */

template<typename Graph>
class VTKWriter<Graph, DIST_GRAPH>
{
	const Graph & g;

	/*! \brief It get the vertex properties list
	 *
	 * It get the vertex properties list of the vertex defined as VTK header
	 *
	 * \return a string that define the vertex properties in graphML format
	 *
	 */

	std::string get_vertex_properties_list()
	{
		//! vertex property output string
		std::string v_out;

		// write the number of vertex
		v_out += "VERTICES " + std::to_string(g.getNVertex()) + " " + std::to_string(g.getNVertex() * 2) + "\n";

		// return the vertex properties string
		return v_out;
	}

	/*! \brief It get the vertex properties list
	 *
	 * It get the vertex properties list of the vertex defined as a VTK header
	 *
	 * \return a string that define the vertex properties in graphML format
	 *
	 */

	std::string get_point_properties_list()
	{
		//! vertex property output string
		std::string v_out;

		// write the number of vertex
		v_out += "POINTS " + std::to_string(g.getNVertex()) + " float" + "\n";

		// return the vertex properties string
		return v_out;
	}

	/*! \brief It get the edge properties list
	 *
	 * It get the edge properties list of the edge defined as a GraphML header
	 *
	 * \return a string that define the edge properties in graphML format
	 *
	 */

	std::string get_edge_properties_list()
	{
		//! vertex property output string
		std::string e_out;

		// write the number of lines
		e_out += "LINES " + std::to_string(g.getNEdge()) + " " + std::to_string(3 * g.getNEdge()) + "\n";

		// return the vertex properties string
		return e_out;
	}

	/*! \brief Create the VTK point definition
	 *
	 * \tparam s_type spatial type of the data
	 * \tparam attr false x,y,z are set to 0 for each vertex
	 *
	 */

	template<bool attr> std::string get_point_list()
	{
		//! VTK spatial information
		typename Graph::V_type::s_type x[3] = { 0, 0, 0 };

		//! vertex node output string
		std::string v_out;

		//! Get a vertex iterator
		auto it = g.getVertexIterator();

		// if there is the next element
		while (it.isNext())
		{
			// Get vtk vertex node
			auto obj = g.vertex(it.get());

			// create a vertex list functor
			vtk_dist_vertex_node<Graph, attr> vn(v_out, obj, x);

			// Iterate through all the vertex and create the vertex list
			boost::mpl::for_each<boost::mpl::range_c<int, 0, Graph::V_type::max_prop - 1> >(vn);

			// write the node string
			vn.write();

			// increment the iterator and counter
			++it;
		}

		// return the vertex list
		return v_out;
	}

	/*! \brief Create the VTK vertex definition
	 *
	 * \tparam s_type spatial type of the data
	 * \tparam attr false x,y,z are set to 0 for each vertex
	 *
	 */

	std::string get_vertex_list()
	{
		//! vertex node output string
		std::string v_out;

		//! For each point create a vertex
		for (size_t i = 0; i < g.getNVertex(); i++)
		{
			v_out += "1 " + std::to_string(i) + "\n";
		}

		// return the vertex list
		return v_out;
	}

	/*! \brief Get the point data header
	 *
	 * \return a string with the point data header for VTK format
	 *
	 */

	std::string get_point_data_header()
	{
		std::string v_out;

		v_out += "POINT_DATA " + std::to_string(g.getNVertex()) + "\n";

		return v_out;
	}

	/*! \brief Get the point data header
	 *
	 * \return a string with the point data header for VTK format
	 *
	 */

	std::string get_cell_data_header()
	{
		std::string v_out;

		v_out += "CELL_DATA " + std::to_string(g.getNVertex() + g.getNEdge()) + "\n";

		return v_out;
	}

	/*! \brief Return the edge list
	 *
	 * \return the edge list
	 *
	 */

	std::string get_edge_list()
	{
		//! edge node output string
		std::string e_out;

		//! Get an edge iterator
		auto it = g.getEdgeIterator();

		// if there is the next element
		while (it.isNext())
		{
			// create an edge list functor
//			edge_node<Graph> en(e_out,g.edge(it.get()));

			e_out += "2 " + std::to_string(it.source()) + " " + std::to_string(g.nodeById(it.target())) + "\n";

			// increment the operator
			++it;
		}

		// return the edge list
		return e_out;
	}

public:

	/*!
	 *
	 * VTKWriter constructor, it take a graph and write a GraphML format
	 *
	 * \param g Graph to write
	 *
	 */
	VTKWriter(const Graph & g) :
			g(g)
	{
	}

	/*! \brief It write a VTK file from a graph
	 *
	 * \tparam prp_out which properties to output [default = -1 (all)]
	 *
	 * \param file path where to write
	 * \param name of the graph
	 * \param file_type specify if it is a VTK BINARY or ASCII file [default = ASCII]
	 *
	 */

	template<int prp = -1> bool write(std::string file, std::string graph_name = "Graph", file_type ft = file_type::ASCII)
	{
		// Check that the Vertex type define x y and z attributes

		if (has_attributes<typename Graph::V_type>::value == false)
		{
			std::cerr << "Error writing a graph: Vertex must has defines x,y,z properties" << "\n";
			return false;
		}

		// Header for the vtk
		std::string vtk_header;
		// Point list of the VTK
		std::string point_list;
		// Vertex list of the VTK
		std::string vertex_list;
		// Graph header
		std::string vtk_binary_or_ascii;
		// Edge list of the GraphML
		std::string edge_list;
		// vertex properties header
		std::string point_prop_header;
		// edge properties header
		std::string vertex_prop_header;
		// edge properties header
		std::string edge_prop_header;
		// Data point header
		std::string point_data_header;
		// Data point
		std::string point_data;
		// Cell data header
		std::string cell_data_header;
		// Cell data
		std::string cell_data;

		// VTK header
		vtk_header = "# vtk DataFile Version 3.0\n" + graph_name + "\n";

		// Choose if binary or ASCII
		if (ft == file_type::ASCII)
		{
			vtk_header += "ASCII\n";
		}
		else
		{
			vtk_header += "BINARY\n";
		}

		// Data type for graph is DATASET POLYDATA
		vtk_header += "DATASET POLYDATA\n";

		// point properties header
		point_prop_header = get_point_properties_list();

		// Get point list
		point_list = get_point_list<has_attributes<typename Graph::V_type>::value>();

		// vertex properties header
		vertex_prop_header = get_vertex_properties_list();

		// Get vertex list
		vertex_list = get_vertex_list();

		// Edge properties header
		edge_prop_header = get_edge_properties_list();

		// Get the edge graph list
		edge_list = get_edge_list();

		// Get the point data header
		point_data_header = get_point_data_header();

		// Get the cell data header
		cell_data_header = get_cell_data_header();

		// For each property in the vertex type produce a point data

		dist_prop_out_vertex<Graph> pp(point_data, g);

		if (prp == -1)
			boost::mpl::for_each<boost::mpl::range_c<int, 0, Graph::V_type::max_prop> >(pp);
		else
			boost::mpl::for_each<boost::mpl::range_c<int, prp, prp> >(pp);

		// For each property in the edge type produce a point data

		dist_prop_out_edge<Graph> ep(cell_data, g);

		if (prp == -1)
			boost::mpl::for_each<boost::mpl::range_c<int, 0, Graph::E_type::max_prop> >(ep);
		else
			boost::mpl::for_each<boost::mpl::range_c<int, prp, prp> >(ep);

		// write the file
		std::ofstream ofs(file);

		// Check if the file is open
		if (ofs.is_open() == false)
		{
			std::cerr << "Error cannot create the VTK file: " + file;
		}

		ofs << vtk_header << point_prop_header << point_list << vertex_prop_header << vertex_list << edge_prop_header << edge_list << point_data_header << point_data << cell_data_header << cell_data;

		// Close the file

		ofs.close();

		// Completed succefully
		return true;
	}
};

#endif /* VTKWRITER_GRAPH_HPP_ */