File: iso_parametrization.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (2009 lines) | stat: -rw-r--r-- 55,853 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
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
#ifndef TRI_PARAMETRIZATION
#define TRI_PARAMETRIZATION	

// stuff to define the mesh
 #include <wrap/io_trimesh/export_ply.h>
#include <wrap/io_trimesh/import_ply.h>
#include <vcg/space/triangle3.h>
#include <vcg/simplex/face/component_rt.h>
#include <vcg/complex/complex.h>
#include "local_parametrization.h"
#include "uv_grid.h"
#include <vcg/complex/algorithms/stat.h>
#include "param_mesh.h"

///ABSTRACT MESH THAT MAINTAINS THE WHOLE PARAMETERIZATION
class AbstractVertex;
class AbstractFace;

class AbstractUsedTypes: public vcg::UsedTypes < vcg::Use<AbstractVertex>::AsVertexType,
                                          vcg::Use<AbstractFace  >::AsFaceType >{};


class AbstractVertex  : public vcg::Vertex< AbstractUsedTypes,
	vcg::vertex::VFAdj, 
	vcg::vertex::Coord3f,
	vcg::vertex::TexCoord2f,
	vcg::vertex::BitFlags>
  //vcg::face::Normal3f>
{
public:
	CoordType RPos;

  template < class LeftV>
	void ImportData(const LeftV  & left )
  {
			vcg::Vertex< AbstractUsedTypes, vcg::vertex::VFAdj, vcg::vertex::Coord3f,vcg::vertex::TexCoord2f,vcg::vertex::BitFlags>::ImportData( left);
      this->RPos = left.RPos;
  }
};

class AbstractFace    : public vcg::Face  < AbstractUsedTypes,
	vcg::face::VFAdj, 
	vcg::face::FFAdj,
	vcg::face::VertexRef,
	vcg::face::Color4b,
	vcg::face::BitFlags,
	vcg::face::Quality3f>
//vcg::face::Normal3f>
{};

class AbstractMesh: public vcg::tri::TriMesh<std::vector<AbstractVertex>, std::vector<AbstractFace> > {
//public:
//	static const bool Has_Auxiliary(){return false;}
};

///HIGH RESOLUTION MESH THAT HAS TO BE PARAMETERIZED
class ParamVertex;
class ParamFace;


class ParamUsedTypes: public vcg::UsedTypes < vcg::Use<ParamVertex>::AsVertexType,
                                          vcg::Use<ParamFace  >::AsFaceType >{};


class ParamVertex: public vcg::Vertex< ParamUsedTypes,
											vcg::vertex::Normal3f, vcg::vertex::VFAdj, 
											vcg::vertex::Coord3f,vcg::vertex::Color4b,
											vcg::vertex::TexCoord2f,vcg::vertex::BitFlags,
											vcg::vertex::CurvatureDirf,
											vcg::vertex::Qualityf>
{
public:
    template < class LeftV>
		void ImportData(const LeftV  & left )
    {
				vcg::Vertex< ParamUsedTypes, vcg::vertex::Normal3f, vcg::vertex::VFAdj, vcg::vertex::Coord3f,vcg::vertex::Color4b, vcg::vertex::TexCoord2f,vcg::vertex::BitFlags, vcg::vertex::CurvatureDirf,vcg::vertex::Qualityf >::ImportData( left);
    }
	
	
		void ImportData(const ParamVertex  & left )
    {
				vcg::Vertex< ParamUsedTypes, vcg::vertex::Normal3f, vcg::vertex::VFAdj, vcg::vertex::Coord3f,vcg::vertex::Color4b, vcg::vertex::TexCoord2f,vcg::vertex::BitFlags, vcg::vertex::CurvatureDirf,vcg::vertex::Qualityf >::ImportData( left);
        this->RPos = left.RPos;
    }
	
	void ImportData(const BaseVertex  & left )
    {
				vcg::Vertex< ParamUsedTypes, vcg::vertex::Normal3f, vcg::vertex::VFAdj, vcg::vertex::Coord3f,vcg::vertex::Color4b, vcg::vertex::TexCoord2f,vcg::vertex::BitFlags, vcg::vertex::CurvatureDirf,vcg::vertex::Qualityf >::ImportData( left);
        this->RPos = left.RPos;
    }

	CoordType RPos;
  static bool Has_Auxiliary(){return false;}
};

class ParamFace: public vcg::Face <  ParamUsedTypes,
	vcg::face::VFAdj,vcg::face::FFAdj,vcg::face::VertexRef,
  vcg::face::Color4b,vcg::face::BitFlags,
  vcg::face::Normal3f,
	vcg::face::WedgeTexCoord2f,
	vcg::face::Mark,
	vcg::face::Quality3f>
{
public:
	/*template < class LeftV>
		void ImportData(const LeftV  & left )
    {
       vcg::FaceSimp2  <  ParamVertex, ParamEdge, ParamFace, 
	vcg::face::VFAdj,vcg::face::FFAdj,vcg::face::VertexRef,
  vcg::face::Color4b,vcg::face::BitFlags,
  vcg::face::WedgeTexCoord2f,vcg::face::Normal3f,
	vcg::face::Qualityf>::ImportData( left);
    }*/
};
class ParamMesh: public vcg::tri::TriMesh<std::vector<ParamVertex>, std::vector<ParamFace> > 
{
//public:
	//static const bool Has_Auxiliary(){return false;}
};

template <class MeshType>
void CopyMeshFromFacesAbs(const std::vector<typename MeshType::FaceType*> &faces,
						  std::vector<typename MeshType::VertexType*> &orderedVertex,
						  MeshType & new_mesh)
{
	typedef typename MeshType::CoordType CoordType;
	typedef typename MeshType::ScalarType ScalarType;
	typedef typename MeshType::VertexType VertexType;
	typedef typename MeshType::FaceType FaceType;

	///get set of faces
	std::map<VertexType*,VertexType*> vertexmap;
        std::vector<VertexType*> vertices;
	FindVertices(faces,vertices);

	///initialization of new mesh
	new_mesh.Clear();
	new_mesh.vn=0;
	new_mesh.fn=0;
	new_mesh.face.resize(faces.size());
	new_mesh.vert.resize(vertices.size());
	new_mesh.vn=vertices.size();
	new_mesh.fn=faces.size();

	///add new vertices
        typename std::vector<VertexType*>::const_iterator iteV;
	int i=0;
	for (iteV=vertices.begin();iteV!=vertices.end();iteV++)
	{
		///copy position
		assert(!(*iteV)->IsD());
		new_mesh.vert[i].P()=(*iteV)->P();
		new_mesh.vert[i].RPos=(*iteV)->RPos;
		new_mesh.vert[i].T().P()=(*iteV)->T().P();
		new_mesh.vert[i].T().N()=(*iteV)->T().N();
		//new_mesh.vert[i].N()=(*iteV)->N();
		///*assert(new_mesh.vert[i].brother!=NULL);*/
		////if (MeshType::Has_Auxiliary())
		//new_mesh.vert[i].brother=(*iteV)->brother;
		new_mesh.vert[i].ClearFlags();
		
		orderedVertex.push_back((*iteV));
		vertexmap.insert(std::pair<VertexType*,VertexType*>((*iteV),&new_mesh.vert[i]));
		i++;
	}

	///setting of new faces
        typename std::vector<FaceType*>::const_iterator iteF;
        typename std::vector<FaceType>::iterator iteF1;
	for (iteF=faces.begin(),iteF1=new_mesh.face.begin()
		;iteF!=faces.end();iteF++,iteF1++)
	{
		//(*iteF1).areadelta=(*iteF)->areadelta;
	/*	if ((*iteF1).areadelta>1)
			assert(0);*/
		///for each vertex get new reference
		///and associate face-vertex
		for (int j=0;j<3;j++)
		{
			VertexType* v=(*iteF)->V(j);
                        typename std::map<VertexType*,VertexType*>::iterator iteMap=vertexmap.find(v);
			assert(iteMap!=vertexmap.end());
			(*iteF1).V(j)=(*iteMap).second;
		}
	}
}

///create a mesh considering just the faces that share all three vertex
template <class MeshType>
void CopyMeshFromVerticesAbs(std::vector<typename MeshType::VertexType*> &vertices,
						  std::vector<typename MeshType::VertexType*> &OrderedVertices,
						  std::vector<typename MeshType::FaceType*> &OrderedFaces,
					      MeshType & new_mesh)
{
        typedef typename MeshType::CoordType CoordType;
        typedef typename MeshType::ScalarType ScalarType;
        typedef typename MeshType::VertexType VertexType;
        typedef typename MeshType::FaceType FaceType;

        typename std::vector<VertexType*>::const_iterator iteV;
	for (iteV=vertices.begin();iteV!=vertices.end();iteV++)
		(*iteV)->ClearV();



	OrderedVertices.clear();

	///vertex-vertex reference
	std::map<VertexType*,VertexType*> vertexmap;

	///get set of faces
	std::vector<typename MeshType::FaceType*> faces;

	getSharedFace<MeshType>(vertices,faces);
	
	///initialization of new mesh
	new_mesh.Clear();
	new_mesh.vn=0;
	new_mesh.fn=0;

	///set vertices as selected
	
	for (iteV=vertices.begin();iteV!=vertices.end();iteV++)
		(*iteV)->SetV();

	///getting inside faces
        typename std::vector<FaceType*>::const_iterator iteF;
	for (iteF=faces.begin();iteF!=faces.end();iteF++)
	{
		///for each vertex get new reference
		///if there isn't one reference means the face does not appartain to group
		VertexType* v0=(*iteF)->V(0);
		VertexType* v1=(*iteF)->V(1);
		VertexType* v2=(*iteF)->V(2);
		bool inside=((*v0).IsV()&&(*v1).IsV()&&(*v2).IsV());
		if (inside)
			OrderedFaces.push_back((*iteF));
	}
	
	///find internal vertices
	FindVertices(OrderedFaces,OrderedVertices);

	///setting size
	new_mesh.face.resize(OrderedFaces.size());
	new_mesh.vert.resize(OrderedVertices.size());
	new_mesh.vn=OrderedVertices.size();
	new_mesh.fn=OrderedFaces.size();

	///setting of internal vertices
	int i=0;
        typename std::vector<typename MeshType::VertexType*>::iterator iteVI;
	for (iteVI=OrderedVertices.begin();iteVI!=OrderedVertices.end();iteVI++)
	{
		///copy position
		assert(!(*iteVI)->IsD());
		new_mesh.vert[i].P()=(*iteVI)->P();
		new_mesh.vert[i].RPos=(*iteVI)->RPos;
		new_mesh.vert[i].T().P()=(*iteVI)->T().P();
		new_mesh.vert[i].T().N()=(*iteVI)->T().N();
		new_mesh.vert[i].C()=(*iteVI)->C();
		/*new_mesh.vert[i].father=(*iteVI)->father;
		new_mesh.vert[i].Bary=(*iteVI)->Bary;*/
		//new_mesh.vert[i].Damp=(*iteVI)->Damp;
		/*new_mesh.vert[i].RestUV=(*iteVI)->RestUV;*/
		//new_mesh.vert[i].N()=(*iteVI)->N();
		//new_mesh.vert[i].C()=(*iteVI)->C();
		/*new_mesh.vert[i].OriginalCol=(*iteVI)->OriginalCol;*/
		new_mesh.vert[i].ClearFlags();
		///map setting
		vertexmap.insert(std::pair<VertexType*,VertexType*>((*iteVI),&new_mesh.vert[i]));
		i++;
	}

	///setting of new faces
        typename std::vector<FaceType>::iterator iteF1;
	for (iteF=OrderedFaces.begin(),iteF1=new_mesh.face.begin()
		;iteF!=OrderedFaces.end();iteF++,iteF1++)
	{
		///for each vertex get new reference
		///and associate face-vertex
		for (int j=0;j<3;j++)
		{
			VertexType* v=(*iteF)->V(j);
                        typename std::map<VertexType*,VertexType*>::iterator iteMap=vertexmap.find(v);
			assert(iteMap!=vertexmap.end());
			(*iteF1).V(j)=(*iteMap).second;
		}
	}

	///clear flags
	for (iteV=vertices.begin();iteV!=vertices.end();iteV++)
		(*iteV)->ClearV();

}




//template <class InputMesh>
//static int Parametrize(InputMesh &to_param,
//					   AbstractMesh &AbsMesh,
//					   InputMesh &Parametrized,
//					   int &approx_face_num)
//{
//	vcg::tri::UpdateTopology<InputMesh>::FaceFace(to_param);
//
//	///test input conditions
//	bool b=vcg::tri::Clean<InputMesh>::IsTwoManifoldFace(to_param);
//	b&=vcg::tri::Clean<InputMesh>::IsTwoManifoldVertexFF(to_param);
//	int n=vcg::tri::Clean<InputMesh>::BorderEdges(to_param);
//	b&=(n==0);
//	int num=vcg::tri::Clean<InputMesh>::ConnectedComponents(to_param);
//	b&=(num==1);
//	if (!b)
//		return -1;
//
//	///then parameteterize
//	IsoParametrizator TrImage;
//	TrImage.Parametrize<MyMesh>(&mesh,num1,num2);
//	TrImage.ExportMeshes(to_param,AbsMesh);
//	return 0;
//}

///class that maintains the whole meh parametrerization with relitive operators
class IsoParametrization
{
	AbstractMesh * abstract_mesh;
	ParamMesh	 * param_mesh;

public:
	typedef ParamMesh::ScalarType ScalarType;
	typedef ParamMesh::CoordType CoordType;

private:

	///this class maintains submeshes and hresolution meshes
	///for the selected parametrization domain
	struct param_domain{
		AbstractMesh *domain;
		std::vector<int> local_to_global;

		ParamMesh *HresDomain;
		UVGrid<ParamMesh> grid;
		std::vector<ParamFace*> ordered_faces;

		int Local2Global(const int &localFace)
		{return local_to_global[localFace];}

		int Global2Local(const int &GlobalFace)
		{
			int ret=-1;
			for (unsigned int i=0;i<local_to_global.size();i++)
				if (local_to_global[i]==GlobalFace)
					return i;
			return ret;
		}

		void InitGrid()
		{grid.Init(*HresDomain);}

		bool Project(vcg::Point2<ScalarType> UV,
					std::vector<ParamFace*> &face,
					std::vector<CoordType> &baryVal)
		{
			std::vector<ParamFace*> faceParam;
			bool found=grid.ProjectPoint(UV,faceParam,baryVal);
			if (!found)
				return false;
			///calculate the index corresponding to the face
			for (unsigned int i=0;i<faceParam.size();i++)
			{
				ParamFace* f=faceParam[i];
				int index=f-&(*HresDomain->face.begin());
				assert(index<HresDomain->fn);
				face.push_back(ordered_faces[index]);
			}
			return true;
		}

		bool getClosest(vcg::Point2<ScalarType> UV,
						std::vector<ParamFace*> &face,
						std::vector<CoordType> &baryVal)
		{
			face.resize(1);
			baryVal.resize(1);
			bool found=grid.getClosest(UV,face[0],baryVal[0]);
			int index=face[0]-&(*HresDomain->face.begin());
			assert(index<HresDomain->fn);
			face[0]=ordered_faces[index];
			return found;
		}
	};

	///summeshes and subdomains
	std::vector<param_domain> star_meshes;
	std::vector<param_domain> diamond_meshes;
	std::vector<param_domain> face_meshes;

	typedef std::pair<AbstractVertex*,AbstractVertex*> keyEdgeType;
	std::map<keyEdgeType,int> EdgeTab;

	///temporary structure for face_to_vert adjacency
	std::vector<std::vector<ParamVertex*> > face_to_vert;

	void InitFaceToVert()
	{
		face_to_vert.resize(abstract_mesh->face.size());
		for (unsigned int i=0;i<param_mesh->vert.size();i++)
		{
			int I=param_mesh->vert[i].T().N();
			face_to_vert[I].push_back(&param_mesh->vert[i]);
		}
	}


	void GetHresVert(int &I,std::vector<ParamVertex*> &HresVert)
	{
		for (unsigned int k=0;k<face_to_vert[I].size();k++)
		{
			ParamVertex* v=face_to_vert[I][k];
			HresVert.push_back(v);
		}
	}

	///initialize star parametrization
	void InitStar()
	{
		///for each vertex
		int index=0;
		for (unsigned int i=0;i<abstract_mesh->vert.size();i++)
		{
			if (!(abstract_mesh->vert[i].IsD()))
			{
				std::vector<AbstractVertex*> starCenter;
				starCenter.push_back(&abstract_mesh->vert[i]);

				star_meshes[index].domain=new AbstractMesh();
				star_meshes[index].HresDomain=new ParamMesh();

				///create star
				std::vector<AbstractFace*> ordered_faces;
				std::vector<AbstractVertex*> ordered_vert;
				//CreateMeshVertexStar(starCenter,ordered_faces,*star_meshes[index].domain);
				///get faces referenced by vertices
				getSharedFace<AbstractMesh>(starCenter,ordered_faces);
				
				CopyMeshFromFacesAbs<AbstractMesh>(ordered_faces,ordered_vert,*star_meshes[index].domain);

				UpdateTopologies(star_meshes[index].domain);

				///and parametrize it
				ParametrizeStarEquilateral<AbstractMesh>(*star_meshes[index].domain,1.0);
				
				///set other components as reefrence to original faces
				star_meshes[index].local_to_global.resize(star_meshes[index].domain->face.size());
				std::vector<ParamVertex*> HresVert;
				for (unsigned int k=0;k<star_meshes[index].domain->face.size();k++)
				{
					int IndexF;
					getFaceIndexFromPointer(ordered_faces[k],IndexF);
					star_meshes[index].local_to_global[k]=IndexF;
					///get H res vertex
					GetHresVert(IndexF,HresVert);
				}
				
				///copy Hres mesh
				std::vector<ParamVertex*> OrderedVertices;
				CopyMeshFromVerticesAbs(HresVert,OrderedVertices,star_meshes[index].ordered_faces,*star_meshes[index].HresDomain);
				///set new parametrization values
				for (unsigned int k=0;k<star_meshes[index].HresDomain->vert.size();k++)
				{
					ParamVertex * v=&star_meshes[index].HresDomain->vert[k];
					CoordType bary=CoordType(v->T().U(),v->T().V(),1-v->T().U()-v->T().V());
					AbstractMesh *paramDomain=star_meshes[index].domain;
					///get the right face on the parametrized domain
					int Father=v->T().N();
					int faceNum=-1;
					for (unsigned int i=0;i<star_meshes[index].local_to_global.size();i++)
					{
						if (star_meshes[index].local_to_global[i]==Father)
							faceNum=i;
					}
					AbstractFace *faceDom=&paramDomain->face[faceNum];
					v->T().P()=(faceDom->V(0)->T().P())*bary.X()+(faceDom->V(1)->T().P())*bary.Y()+(faceDom->V(2)->T().P())*bary.Z();
					assert(faceNum!=-1);
				}
				star_meshes[index].InitGrid();
				index++;
				
			}
		}		
	}



	void InitDiamond(const ScalarType &edge_len=1.0)
	{
		
		///for each face
		int index=0;
		EdgeTab.clear();
		for (unsigned int i=0;i<abstract_mesh->face.size();i++)
		{
			if (!(abstract_mesh->face[i].IsD()))
			{
				AbstractFace *f0=&abstract_mesh->face[i];
				//for each edge
				for (int j=0;j<3;j++)
				{
					AbstractFace * f1=f0->FFp(j);
					if (f1>f0)
					{
						
						int num0=j;
						int num1=f0->FFi(j);

						///add to domain map
						AbstractVertex *v0,*v1;
						v0=f0->V(j);
						v1=f0->V1(j);
						keyEdgeType k;
						if (v0<v1)
							k=keyEdgeType(v0,v1);
						else
							k=keyEdgeType(v1,v0);

						std::pair<keyEdgeType,int> entry=std::pair<keyEdgeType,int>(k,index);
						EdgeTab.insert(entry);



						///copy the mesh
						std::vector<AbstractFace*> faces;
						faces.push_back(f0);
						faces.push_back(f1);

						diamond_meshes[index].domain=new AbstractMesh();
						diamond_meshes[index].HresDomain=new ParamMesh();

						///create a copy of the mesh
						std::vector<AbstractVertex*> orderedVertex;
						CopyMeshFromFacesAbs<AbstractMesh>(faces,orderedVertex,*diamond_meshes[index].domain);
						UpdateTopologies<AbstractMesh>(diamond_meshes[index].domain);

						///set other components
						int index0,index1;
						getFaceIndexFromPointer(f0,index0);
						getFaceIndexFromPointer(f1,index1);
						diamond_meshes[index].local_to_global.resize(2);
						diamond_meshes[index].local_to_global[0]=index0;
						diamond_meshes[index].local_to_global[1]=index1;

						///parametrize locally
						ParametrizeDiamondEquilateral<AbstractMesh>(*diamond_meshes[index].domain,num0,num1,edge_len);
						///add h resolution vertices 
						std::vector<ParamVertex*> HresVert;
						GetHresVert(index0,HresVert);
						GetHresVert(index1,HresVert);
						std::vector<ParamVertex*> OrderedVertices;
						CopyMeshFromVerticesAbs(HresVert,OrderedVertices,diamond_meshes[index].ordered_faces,*diamond_meshes[index].HresDomain);
						///set new parametrization values
						for (unsigned int k=0;k<diamond_meshes[index].HresDomain->vert.size();k++)
						{
							ParamVertex * v=&diamond_meshes[index].HresDomain->vert[k];
							CoordType bary=CoordType(v->T().U(),v->T().V(),1-v->T().U()-v->T().V());
							AbstractMesh *paramDomain=diamond_meshes[index].domain;
							///get the rigth face on the parametrized domain
							int Father=v->T().N();
							int faceNum=-1;
							for (unsigned int i=0;i<diamond_meshes[index].local_to_global.size();i++)
							{
								if (diamond_meshes[index].local_to_global[i]==Father)
									faceNum=i;
							}
							assert(faceNum!=-1);
							AbstractFace *faceDom=&paramDomain->face[faceNum];
							v->T().P()=(faceDom->V(0)->T().P())*bary.X()+(faceDom->V(1)->T().P())*bary.Y()+(faceDom->V(2)->T().P())*bary.Z();
							
							
						}
						diamond_meshes[index].InitGrid();
						index++;
					}
				}
			}
		}
	}


	void InitFace(const ScalarType &edge_len=1)
	{
		///for each face
		int index=0;
		for (unsigned int i=0;i<abstract_mesh->face.size();i++)
		{
			if (!(abstract_mesh->face[i].IsD()))
			{
				AbstractFace *f0=&abstract_mesh->face[i];

				std::vector<AbstractFace*> faces;
				faces.push_back(f0);

				///create the mesh
				face_meshes[index].domain=new AbstractMesh();
				face_meshes[index].HresDomain=new ParamMesh();

				std::vector<AbstractVertex*> orderedVertex;
				CopyMeshFromFacesAbs<AbstractMesh>(faces,orderedVertex,*face_meshes[index].domain);

				assert(face_meshes[index].domain->vn==3);
				assert(face_meshes[index].domain->fn==1);

				///initialize auxiliary structures
				face_meshes[index].local_to_global.resize(1);
				face_meshes[index].local_to_global[0]=i;

				///parametrize it
				ParametrizeFaceEquilateral<AbstractMesh>(*face_meshes[index].domain,edge_len);

				///add h resolution vertices 
				std::vector<ParamVertex*> HresVert;
				GetHresVert(index,HresVert);
				std::vector<ParamVertex*> OrderedVertices;
				CopyMeshFromVerticesAbs(HresVert,OrderedVertices,face_meshes[index].ordered_faces,*face_meshes[index].HresDomain);
				///set new parametrization values
				for (unsigned int k=0;k<face_meshes[index].HresDomain->vert.size();k++)
				{
					ParamVertex * v=&face_meshes[index].HresDomain->vert[k];
					CoordType bary=CoordType(v->T().U(),v->T().V(),1-v->T().U()-v->T().V());
					AbstractMesh *paramDomain=face_meshes[index].domain;
					AbstractFace *faceDom=&paramDomain->face[0];
					v->T().P()=(faceDom->V(0)->T().P())*bary.X()+(faceDom->V(1)->T().P())*bary.Y()+(faceDom->V(2)->T().P())*bary.Z();
				}

				face_meshes[index].InitGrid();
				index++;
			}
		}
	}

	void getFaceIndexFromPointer(AbstractFace * f,int &index)
	{
		index=f-&(*abstract_mesh->face.begin());
	}

	void getStarFromPointer(AbstractVertex * center,int &index)
	{
		index=center-&(*abstract_mesh->vert.begin());
	}

	void getDiamondFromPointer(AbstractVertex * v0,AbstractVertex * v1,int &index)
	{
		assert(v0!=v1);
		keyEdgeType key;
		if (v0<v1)
			key=keyEdgeType(v0,v1);
		else
			key=keyEdgeType(v1,v0);

		std::map<keyEdgeType,int>::iterator k=EdgeTab.find(key);
		assert(k!=EdgeTab.end());
		index=((*k).second);
	}
	
	
	bool Test()
	{
		/*int index=0;*/
		for (unsigned int i=0;i<abstract_mesh->face.size();i++)
		{
			if (!(abstract_mesh->face[i].IsD()))
			{
				AbstractFace *f0=&abstract_mesh->face[i];
				//for each edge
				for (int j=0;j<3;j++)
				{
					AbstractFace * f1=f0->FFp(j);
					if (f1>f0)
					{
						///add to domain map
						AbstractVertex *v0,*v1;
						v0=f0->V(j);
						v1=f0->V1(j);
						keyEdgeType k;
						if (v0<v1)
							k=keyEdgeType(v0,v1);
						else
							k=keyEdgeType(v1,v0);

						//std::pair<keyEdgeType,int> entry=std::pair<keyEdgeType,int>(k,index);
						std::map<keyEdgeType,int>::iterator iteE=EdgeTab.find(k);
						
						int index0F,index1F;
						getFaceIndexFromPointer(f0,index0F);
						getFaceIndexFromPointer(f1,index1F);
#ifndef NDEBUG
						int edgeIndex=(*iteE).second;
						assert(diamond_meshes[edgeIndex].local_to_global[0]==index0F);
						assert(diamond_meshes[edgeIndex].local_to_global[1]==index1F);
#endif
					}
				}
			}
		}
		///test if for each face there is a right domain
		for (unsigned int i=0;i<param_mesh->face.size();i++)
		{
			ParamFace * f=&param_mesh->face[i];	
			vcg::Point2f uvI0,uvI1,uvI2;
			int IndexDomain=-1;
			int ret=InterpolationSpace(f,uvI0,uvI1,uvI2,IndexDomain);
			if (ret==-1)
				return false;
		}
	return true;
	}

	int getSharedVertices(AbstractFace *f0,AbstractFace *f1,AbstractFace *f2,
						  AbstractVertex *shared[3])
	{
		AbstractVertex *vert0[3],*vert1[3],*vert2[3];
		
		vert0[0]=f0->V(0);
		vert0[1]=f0->V(1);
		vert0[2]=f0->V(2);

		vert1[0]=f1->V(0);
		vert1[1]=f1->V(1);
		vert1[2]=f1->V(2);

		vert2[0]=f2->V(0);
		vert2[1]=f2->V(1);
		vert2[2]=f2->V(2);
		
		int num=0;
		for (int i=0;i<3;i++)
		{
			AbstractVertex * test=vert0[i];
			bool found0=false,found1=false;
			if ((vert1[0]==test)||(vert1[1]==test)||(vert1[2]==test))
					found0=true;
			if (found0)
			{
				if ((vert2[0]==test)||(vert2[1]==test)||(vert2[2]==test))
					found1=true;
			}
			if ((found0)&&(found1))
			{
				shared[num]=test;
				num++;
			}
		}

		return num;
	}
	
	int getSharedVertices(AbstractFace *f0,AbstractFace *f1,AbstractVertex *shared[3])
	{
		AbstractVertex *vert0[3],*vert1[3];
		
		vert0[0]=f0->V(0);
		vert0[1]=f0->V(1);
		vert0[2]=f0->V(2);

		vert1[0]=f1->V(0);
		vert1[1]=f1->V(1);
		vert1[2]=f1->V(2);

		
		int num=0;
		for (int i=0;i<3;i++)
		{
			AbstractVertex * test=vert0[i];
			bool found0=false;
			if ((vert1[0]==test)||(vert1[1]==test)||(vert1[2]==test))
					found0=true;
			if (found0)
			{
				shared[num]=test;
				num++;
			}
		}

		return num;
	}
	
	int getSharedVertices(const std::vector<int> &I,AbstractVertex *shared[3],int *_num=NULL)
	{	
		///else test the number of vertices shared 
		AbstractVertex * shared_vert[3];
		bool sharedB[3];
		int size;
		if (_num==NULL)
			size=I.size();
		else
			size=*_num;

		///quick test for 2 or 3 cases
		if (size==2)
			return getSharedVertices(&AbsMesh()->face[I[0]],&AbsMesh()->face[I[1]],shared);
		else
		if (size==3)
			return getSharedVertices(&AbsMesh()->face[I[0]],&AbsMesh()->face[I[1]],&AbsMesh()->face[I[2]],shared);

		AbstractFace* f0=&AbsMesh()->face[I[0]];
		AbstractVertex *v0=f0->V(0);
		AbstractVertex *v1=f0->V(1);
		AbstractVertex *v2=f0->V(2);
		shared_vert[0]=v0;
		shared_vert[1]=v1;
		shared_vert[2]=v2;
		sharedB[0]=true;
		sharedB[1]=true;
		sharedB[2]=true;
		//for each face
		for (int i=1;i<size;i++)
		{
			AbstractFace* f=&AbsMesh()->face[I[i]];
			//for each vertex
			for (int j=0;j<3;j++)
			{
				if (sharedB[j])
				{
					AbstractVertex *v_test=shared_vert[j];
					if (!((v_test==f->V(0))||(v_test==f->V(1))||(v_test==f->V(2))))
						sharedB[j]=false;
				}	
			}
		}

		///return vertices correctly
		int num=0;
		for (int i=0;i<3;i++)
			if (sharedB[i])
			{
				shared[num]=shared_vert[i];
				num++;
			}
		return num;
	}


	void Clamp(vcg::Point2f &UV)
	{
		float eps=0.00001f;
    if (UV.X() < eps) UV.X()=0;
    if (UV.X()>1-eps) UV.X()=1;
    if (UV.Y() < eps) UV.Y()=0;
    if (UV.Y()>1-eps) UV.Y()=1;
	}
	
	
	float Area3d;
	float AbstractArea;

public:
	
	///return the minimum interpolation space shared by a face changing coordinates
	///return 0 if is a face 1 is a diamaond and 2 is a star
	int InterpolationSpace(ParamFace *f,
		vcg::Point2f &uvI0,
		vcg::Point2f &uvI1,
		vcg::Point2f &uvI2,
		int &IndexDomain)
	{
		ParamVertex *v0=f->V(0);
		ParamVertex *v1=f->V(1);
		ParamVertex *v2=f->V(2);

		int I0=v0->T().N();
		int I1=v1->T().N();
		int I2=v2->T().N();

		vcg::Point2f UV0=v0->T().P();
		vcg::Point2f UV1=v1->T().P();
		vcg::Point2f UV2=v2->T().P();

		///if they are equal it's is triavially the interpolation of UV coords
		///and the same face I as the domain
		if ((I0==I1)&&(I1==I2))
		{
			GE2(I0,UV0,uvI0);
			GE2(I1,UV1,uvI1);
			GE2(I2,UV2,uvI2);
			IndexDomain=I0;
			return 0;
		}

		///else find the right interpolation domain in which the face belongs
		///test if they share an edge then use half diamond
		AbstractFace *f0=&abstract_mesh->face[I0];
		AbstractFace *f1=&abstract_mesh->face[I1];
		AbstractFace *f2=&abstract_mesh->face[I2];
		AbstractVertex *shared[3];

		int num=getSharedVertices(f0,f1,f2,shared);
		if (num<1)
		{
			return -1;
		}
		//assert((num==1)||(num==2));
		if (num==2)///ude diamond
		{
			AbstractVertex* v0=shared[0];
			AbstractVertex* v1=shared[1];
			int EdgeIndex;

			getDiamondFromPointer(v0,v1,EdgeIndex);			

			GE1(I0,UV0,EdgeIndex,uvI0);
			GE1(I1,UV1,EdgeIndex,uvI1);
			GE1(I2,UV2,EdgeIndex,uvI2);

			IndexDomain=EdgeIndex;
			return 1;
		}

		///use the star domain

		AbstractVertex* center=shared[0];
		int StarIndex;
		getStarFromPointer(center,StarIndex);
		bool b0=GE0(I0,UV0,StarIndex,uvI0);
		bool b1=GE0(I1,UV1,StarIndex,uvI1);
		bool b2=GE0(I2,UV2,StarIndex,uvI2);
		IndexDomain=StarIndex;
		if ((!b0)||(!b1)||(!b2))
		{
			printf("AZZZ 1\n");
			return -1;
		}
		assert((uvI0.X()>=-1)&&(uvI0.Y()>=-1)&&(uvI0.X()<=1)&&(uvI0.Y()<=1));
		assert((uvI1.X()>=-1)&&(uvI1.Y()>=-1)&&(uvI1.X()<=1)&&(uvI1.Y()<=1));
		assert((uvI2.X()>=-1)&&(uvI2.Y()>=-1)&&(uvI2.X()<=1)&&(uvI2.Y()<=1)); 

		

		return 2;

	}
	
	///return the minimum interpolation space shared by two faces of abstarct domain
	///return 0 if is a face 1 is a diamaond and 2 is a star
	int InterpolationSpace(const int &I0,const int &I1,int &IndexDomain)
	{
		///that case is the face itself
		if (I0==I1)
		{
			IndexDomain=I0;
			return 0;
		}
		AbstractFace* f0=&AbsMesh()->face[I0];
		AbstractFace* f1=&AbsMesh()->face[I1];
	/*	AbstractVertex *v0=f0->V(0);
		AbstractVertex *v1=f0->V(1);
		AbstractVertex *v2=f0->V(2);
		AbstractVertex *v3=f1->V(0);
		AbstractVertex *v4=f1->V(1);
		AbstractVertex *v5=f1->V(2);*/

		
		AbstractVertex *shared[3];

		int num=getSharedVertices(f0,f1,shared);
		if (!((num==1)||(num==2)))
			return -1;
		if (num==2)///use diamond
		{
			AbstractVertex* v0=shared[0];
			AbstractVertex* v1=shared[1];
			int EdgeIndex;

			getDiamondFromPointer(v0,v1,EdgeIndex);

			IndexDomain=EdgeIndex;
			return 1;
		}

		///use the star domain

		AbstractVertex* center=shared[0];
		int StarIndex;
		getStarFromPointer(center,StarIndex);
		IndexDomain=StarIndex;

		return 2;

	}
	
	///return 0 if is a face 1 is a diamaond and 2 is a star
	int InterpolationSpace(const std::vector<int> &I,int &IndexDomain,int *_num=NULL)
	{
		int size;
		if (_num==NULL)
			size=I.size();
		else
			size=*_num;

		///simple cases
		assert(I.size()>0);
		///1 element
		if (size==1)
		{
			IndexDomain=I[0];
			return 0;
		}
		///2 elements
		if (size==2)
			return InterpolationSpace(I[0],I[1],IndexDomain);

		///test if they all are the same
		///that case is the face itself
		bool sameface=true;
		int i=1;
		int I0=I[0];
		while ((i<size)&&(sameface))
		{
			sameface&=(I[i]==I0);
			i++;
		}
		if (sameface)
		{
			IndexDomain=I0;
			return 0;
		}

		///else test the number of vertices shared 
		AbstractVertex *shared[3];
		int num=getSharedVertices(I,shared,_num);
		assert(num!=3); ///no same yet face possible
		if (num==0)
			return -1;  ///no interpolation space exists

		if (num==2)///ude diamond
		{
			AbstractVertex* v0=shared[0];
			AbstractVertex* v1=shared[1];
			int EdgeIndex;

			getDiamondFromPointer(v0,v1,EdgeIndex);

			IndexDomain=EdgeIndex;
			return 1;
		}

		///use the star domain

		AbstractVertex* center=shared[0];
		int StarIndex;
		getStarFromPointer(center,StarIndex);
		IndexDomain=StarIndex;

		return 2;

	}

	int getHStarIndex(const int &I,
		const vcg::Point2<ScalarType> &UV)
	{
		int vertStar=2;
		CoordType bary=CoordType(UV.X(),UV.Y(),(ScalarType)1.0-UV.X()-UV.Y());

		if ((bary.X()>bary.Y())&&(bary.X()>bary.Z()))
			vertStar=0;
		else
			if ((bary.Y()>bary.X())&&(bary.Y()>bary.Z()))
				vertStar=1;

		AbstractFace *f=&abstract_mesh->face[I];
		AbstractVertex *Center=f->V(vertStar);

		int StarIndex;
		getStarFromPointer(Center,StarIndex);
		return StarIndex;
	}

	int getHDiamIndex(const int &I,
		const vcg::Point2<ScalarType> &UV)
	{
		CoordType bary=CoordType(UV.X(),UV.Y(),1-UV.X()-UV.Y());
		ScalarType sum0=bary.X()+bary.Y();
		ScalarType sum1=bary.Y()+bary.Z();
		ScalarType sum2=bary.X()+bary.Z();
		int edge=2;
		if ((sum0>sum1)&&(sum0>sum2))
			edge=0;
		else
			if ((sum1>sum0)&&(sum1>sum2))
				edge=1;

		///get the half-diamond mesh
		int DiamIndex;
		getDiamondFromPointer(abstract_mesh->face[I].V(edge),abstract_mesh->face[I].V1(edge),DiamIndex);
		return(DiamIndex);
	}

	///give the face and barycentric coordinate return I and UV coordinates
	void Phi(const ParamFace *f,
		const CoordType &bary3D,
		int &I,
		vcg::Point2<ScalarType> &UV)
	{
#ifndef NDEBUG
		float eps=0.00001f;
#endif
		///control right domain
		int I0=f->V(0)->T().N();
		int I1=f->V(1)->T().N();
		int I2=f->V(2)->T().N();

		///if they are equal it's is triavially the interpolation of UV coords
		///and the same face I as the domain
		if ((I0==I1)&&(I1==I2))
		{
			UV=bary3D.X()*f->V(0)->T().P()+bary3D.Y()*f->V(1)->T().P()+bary3D.Z()*f->V(2)->T().P();
			Clamp(UV);
			assert((UV.X()>=0)&&(UV.Y()>=0)&&(UV.X()<=1)&&(UV.Y()<=1)&&(UV.X()+UV.Y()<=1));
			I=I0;
			return;
		}
		

		///else find the right interpolation domain in which the face belongs
		///test if they share an edge then use half diamond
		AbstractFace *f0=&abstract_mesh->face[I0];
		AbstractFace *f1=&abstract_mesh->face[I1];
		AbstractFace *f2=&abstract_mesh->face[I2];
		AbstractVertex *shared[3];

		int num=getSharedVertices(f0,f1,f2,shared);

		/*///if num==1
		if (num==0)
		{
			float alpha=bary3D.X();
			float beta =bary3D.Y();
			float gamma=bary3D.Z();
#ifndef _MESHLAB
			printf("problems with interpolation, solved...");
#endif
			if ((alpha>beta)&&(alpha>gamma))
			{
				I=I0;
				UV=f->V(0)->T().P();
			}
			else
			if ((beta>alpha)&&(beta>gamma))
			{
				I=I1;
				UV=f->V(1)->T().P();
			}
			else
			{
				I=I2;
				UV=f->V(2)->T().P();
			}
			
			return;
		}*/

		assert((num==1)||(num==2));
		if ((num!=1)&&(num!=2))
		{
			printf("DOMAIN %d\n",num);
			assert(0);
		}
		if (num==1)///use the star domain
		{
			//printf("phi 0\n");
			AbstractVertex* center=shared[0];
			int StarIndex;
			getStarFromPointer(center,StarIndex);
			vcg::Point2<ScalarType> UVs0,UVs1,UVs2;
			vcg::Point2<ScalarType> UV0=f->V(0)->T().P();
			vcg::Point2<ScalarType> UV1=f->V(1)->T().P();
			vcg::Point2<ScalarType> UV2=f->V(2)->T().P();

			GE0(I0,UV0,StarIndex,UVs0);
			GE0(I1,UV1,StarIndex,UVs1);
			GE0(I2,UV2,StarIndex,UVs2);
			assert((UVs0.X()>=-1)&&(UVs0.Y()>=-1)&&(UVs0.X()<=1)&&(UVs0.Y()<=1));
			assert((UVs1.X()>=-1)&&(UVs1.Y()>=-1)&&(UVs1.X()<=1)&&(UVs1.Y()<=1));
			assert((UVs2.X()>=-1)&&(UVs2.Y()>=-1)&&(UVs2.X()<=1)&&(UVs2.Y()<=1));

			///interpolate star value
			vcg::Point2<ScalarType> UV_interp=bary3D.X()*UVs0+bary3D.Y()*UVs1+bary3D.Z()*UVs2;
			inv_GE0(StarIndex,UV_interp,I,UV);
			Clamp(UV);
			assert((UV.X()>=0)&&(UV.Y()>=0)&&(UV.X()<=1)&&(UV.Y()<=1)&&(UV.X()+UV.Y()<=1+eps));
			return;
		}
		else			///use the diamond domain
		{
			//printf("phi 1\n");
			//std::set<AbstractVertex*>::iterator it=temp2.begin();
			//std::vector<AbstractVertex*>::iterator it=shared[0];
			AbstractVertex* v0=shared[0];
			//it++;
			AbstractVertex* v1=shared[1];
			int EdgeIndex;
			getDiamondFromPointer(v0,v1,EdgeIndex);

			vcg::Point2<ScalarType> UVd0,UVd1,UVd2;
			vcg::Point2<ScalarType> UV0=f->V(0)->T().P();
			vcg::Point2<ScalarType> UV1=f->V(1)->T().P();
			vcg::Point2<ScalarType> UV2=f->V(2)->T().P();

			GE1(I0,UV0,EdgeIndex,UVd0);
			GE1(I1,UV1,EdgeIndex,UVd1);
			GE1(I2,UV2,EdgeIndex,UVd2);

			///interpolate star value
			vcg::Point2<ScalarType> UV_interp=bary3D.X()*UVd0+bary3D.Y()*UVd1+bary3D.Z()*UVd2;
			inv_GE1(EdgeIndex,UV_interp,I,UV);
			Clamp(UV);
      //assert((UV.X()>=0)&&(UV.Y()>=0)&&(UV.X()<=1)&&(UV.Y()<=1)&&(UV.X()+UV.Y()<=1+eps));
			assert((I==I0)||(I==I1)||(I==I2));
			return;
		}

	}

	///given the I and UV coordinates return the face and barycentric coords
	///return the domain used for interpolation 0=face 1=half diam 2=half star
	int Theta(const int &I,
		const vcg::Point2<ScalarType> &UV, // alphaBeta
		std::vector<ParamFace*> &face,
		std::vector<CoordType> &baryVal)
	{
		#ifndef NDEBUG
		const ScalarType eps=(ScalarType)0.0001;
		#endif
		assert(UV.X()<=1);
		assert(UV.Y()<=1);
		assert(UV.X()>=0);
		assert(UV.Y()>=0);
		//printf("%f,%f \n",UV.X(),UV.Y());
		assert((UV.X()+UV.Y())<=(1+eps));
		
		//printf("%f,%f \n",UV.X(),UV.Y());
		///FACE SEARCH
		///first test if is in the face domain 
		//printf("face\n");
		bool found=false;
		int indexFace=I;
		/*if (indexFace>=face_meshes.size())
			printf("B");*/

		vcg::Point2<ScalarType> UVFace;
		GE2(indexFace,UV,UVFace);
		found=face_meshes[indexFace].Project(UVFace,face,baryVal);
		if (found)
			return 0;
		
		///DIAMOND SEARCH
		///search in the diamond domain
		//printf("diam\n");
		int indexDiam=getHDiamIndex(I,UV);///get diamond index
		vcg::Point2<ScalarType> UVDiam;
		///transform UV coordids in diamond
		GE1(I,UV,indexDiam,UVDiam);
		/*if (indexDiam>=diamond_meshes.size())*/
			/*printf("C");*/
		found=diamond_meshes[indexDiam].Project(UVDiam,face,baryVal);
		if (found)
			return 1;

		///STAR SEARCH
		//printf("star\n");
		int indexStar=getHStarIndex(I,UV);///get star index
		vcg::Point2<ScalarType> UVStar;
		///transform UV coords in star
		GE0(I,UV,indexStar,UVStar);
		found=star_meshes[indexStar].Project(UVStar,face,baryVal);
		if (!found)
		{
			//printf("\n problems projecting u,v:%lf,%lf (RESOLVED)\n",UV.X(),UV.Y());
#ifndef NDEBUG
			bool found=star_meshes[indexStar].getClosest(UVStar,face,baryVal);
			assert(found);
#else
			star_meshes[indexStar].getClosest(UVStar,face,baryVal);
			/*printf("D");*/
#endif
		}
		return 2;
	}
	
	///given the I and UV coordinates return the face and barycentric coords
	///return the domain used for interpolation 0=face 1=half diam 2=half star
	int Theta(const int &I,
		const vcg::Point2<ScalarType> &UV, // alphaBeta
		ParamFace* &face,
		CoordType &baryVal)
	{
		std::vector<ParamFace*> faces;
		std::vector<CoordType>  baryVals;
		int ret=Theta(I,UV,faces,baryVals);
		if (ret!=-1)
		{
			face=faces[0];
			baryVal=baryVals[0];
		}
		return ret;
	}

	///given the I and UV coordinates return the face and barycentric coords
	///return the domain used for interpolation 0=face 1=half diam 2=half star
	///and 3D Coordinates
	int Theta(const int &I,
		const vcg::Point2<ScalarType> &UV,
		CoordType &pos3D)
	{
		std::vector<ParamFace*> face;
		std::vector<CoordType> baryVal;
			
		int ret=Theta(I,UV,face,baryVal);
			
		pos3D=CoordType(0,0,0);
		for (int i=0;i<(int)face.size();i++)
		{
			CoordType pos=face[i]->V(0)->P()*baryVal[i].X()+
						  face[i]->V(1)->P()*baryVal[i].Y()+
						  face[i]->V(2)->P()*baryVal[i].Z();
			pos3D+=pos;
		}
		
		pos3D/=(ScalarType)face.size();
		return ret;
	}

	///return the coordinate on the half star domain
	bool GE0(const int &I,
		const vcg::Point2<ScalarType> &UV,
		const int &StarIndex,
		vcg::Point2<ScalarType> &UVHstar)
	{
		//int vertStar=2;
		CoordType bary=CoordType(UV.X(),UV.Y(),1-UV.X()-UV.Y());

		///then transform to the star domain

		///get star mesh
		AbstractMesh* star_domain=star_meshes[StarIndex].domain;
		int LocalIndex=star_meshes[StarIndex].Global2Local(I);
		if (LocalIndex==-1)
			return false;

    InterpolateUV<AbstractMesh>(&star_domain->face[LocalIndex],bary,UVHstar.X(),UVHstar.Y());
		return true;
	}

	///return the coordinate on the half star domain
	bool inv_GE0(int &StarIndex,
		vcg::Point2<ScalarType> &UVHstar,
		int &I,
		vcg::Point2<ScalarType> &UV)
	{
		AbstractMesh* star_domain=star_meshes[StarIndex].domain;
		CoordType bary;
		int index;
		bool done=GetBaryFaceFromUV(*star_domain,UVHstar.X(),UVHstar.Y(),bary,index);
		if (!done)
			return false;
		UV.X()=bary.X();
		UV.Y()=bary.Y();
		I=star_meshes[StarIndex].Local2Global(index);
		return done;
	}


	void InterpParam(vcg::Point2f p1,vcg::Point2f p2,vcg::Point2f p3,vcg::Point2f p_test,
					 ScalarType &b1,ScalarType &b2,ScalarType &b3)
	{
		ScalarType x0=p_test.X();
		ScalarType y0=p_test.Y();
		ScalarType x1=p1.X();
		ScalarType y1=p1.Y();
		ScalarType x2=p2.X();
		ScalarType y2=p2.Y();
		ScalarType x3=p3.X();
		ScalarType y3=p3.Y();
		
		ScalarType b0 =  (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1);
		b1 = ((x2 - x0) * (y3 - y0) - (x3 - x0) * (y2 - y0)) / b0;
		b2 = ((x3 - x0) * (y1 - y0) - (x1 - x0) * (y3 - y0)) / b0;
		b3 = ((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) / b0;
	}

	///return the coordinate on the half diamond domain
	void GE1(const int &I,
		const vcg::Point2<ScalarType> &UV,
		const int &DiamIndex,
		vcg::Point2<ScalarType> &UVDiam)
	{
		///get the right edge index
		CoordType bary=CoordType(UV.X(),UV.Y(),1-UV.X()-UV.Y());
		
		///then transform to the half-diamond domain
		///get egde mesh
		AbstractMesh* diam_domain=diamond_meshes[DiamIndex].domain;
		int LocalIndex=diamond_meshes[DiamIndex].Global2Local(I);
		if(LocalIndex!=-1)
		{
      InterpolateUV<AbstractMesh>(&diam_domain->face[LocalIndex],bary,UVDiam.X(),UVDiam.Y());
			return;
		}
		///if ! found seach in the star space
		else
		{
			AbstractFace *f_diam=&diam_domain->face[0];
			int indexF0=diamond_meshes[DiamIndex].Local2Global(0);
			int indexF1=diamond_meshes[DiamIndex].Local2Global(1);

			//getFaceIndexFromPointer(f0,indexF0);
			vcg::Point2<ScalarType> UVHstar;
			//int StarIndex=getHStarIndex(I,UV);
			int StarIndex=getHStarIndex(I,UV);
			///get star coordinates
#ifndef NDEBUG
			bool found=GE0(I,UV,StarIndex,UVHstar);
			assert(found);
#else
			GE0(I,UV,StarIndex,UVHstar);
#endif

			///then find which face is in the star
			int indexParam;
			int indexParam0=star_meshes[StarIndex].Global2Local(indexF0);
			int indexParam1=star_meshes[StarIndex].Global2Local(indexF1);
			if(indexParam0==-1)
				indexParam=indexParam1;
			else
				indexParam=indexParam0;
			
			AbstractFace *f_param=&star_meshes[StarIndex].domain->face[indexParam];
			vcg::Point2f p0=f_param->V(0)->T().P();
			vcg::Point2f p1=f_param->V(1)->T().P();
			vcg::Point2f p2=f_param->V(2)->T().P();
			
			vcg::Point3f coord;
			//bool inside=vcg::InterpolationParameters2<float>(p0,p1,p2,UVHstar,coord);
			InterpParam(p0,p1,p2,UVHstar,coord.X(),coord.Y(),coord.Z());
			///finally reinterpolate in diamond space
			UVDiam=coord.X()*f_diam->V(0)->T().P()+coord.Y()*f_diam->V(1)->T().P()+coord.Z()*f_diam->V(2)->T().P();
			return;
		}
		//CoordType bary=CoordType(UV.X(),UV.Y(),1-UV.X()-UV.Y());
		
	}

	///given the diamond coordinates return the coordinates in the parametrization space
	void inv_GE1(const int &DiamIndex,
		const vcg::Point2<ScalarType> &UVDiam,
		int &I,
		vcg::Point2<ScalarType> &UV)
	{
		AbstractMesh* diam_domain=diamond_meshes[DiamIndex].domain;
		CoordType bary;
		int index;
#ifndef NDEBUG
		bool done=GetBaryFaceFromUV(*diam_domain,UVDiam.X(),UVDiam.Y(),bary,index);
		assert(done);
#else
		GetBaryFaceFromUV(*diam_domain,UVDiam.X(),UVDiam.Y(),bary,index);
#endif
		UV.X()=bary.X();
		UV.Y()=bary.Y();
		I=diamond_meshes[DiamIndex].Local2Global(index);
		
	}
	
	///given the diamond coordinates return the coordinates in the parametrization space
	///in this case I is fixed and should falls also outside the face I
	void inv_GE1_fixedI(const int &DiamIndex,
		const vcg::Point2<ScalarType> &UVDiam,
		const int &I,
		vcg::Point2<ScalarType> &bary)
	{
		/*AbstractMesh* diam_domain=diamond_meshes[DiamIndex].domain;*/
		//int index;
		CoordType bary3d;
		///then find barycentryc coordinates with respect to such face
		int local_face=diamond_meshes[DiamIndex].Global2Local(I);
		AbstractFace* f=&diamond_meshes[DiamIndex].domain->face[local_face];
		vcg::Point2<ScalarType> p0=f->V(0)->T().P();
		vcg::Point2<ScalarType> p1=f->V(1)->T().P();
		vcg::Point2<ScalarType> p2=f->V(2)->T().P();
		InterpParam(p0,p1,p2,UVDiam,bary3d.X(),bary3d.Y(),bary3d.Z());
		bary.X()=bary3d.X();
		bary.Y()=bary3d.Y();
	}
	
	///given the star return the coordinates in the parametrization space
	///in this case I is fixed and should falls also outside the face I
	void inv_GE0_fixedI(const int &StarIndex,
		const vcg::Point2<ScalarType> &UVStar,
		const int &I,
		vcg::Point2<ScalarType> &bary)
	{
		//AbstractMesh* star_domain=star_meshes[StarIndex].domain;
		/*int index;*/
		CoordType bary3d;
		///then find barycentryc coordinates with respect to such face
		int local_face=star_meshes[StarIndex].Global2Local(I);
		AbstractFace* f=&star_meshes[StarIndex].domain->face[local_face];
		vcg::Point2<ScalarType> p0=f->V(0)->T().P();
		vcg::Point2<ScalarType> p1=f->V(1)->T().P();
		vcg::Point2<ScalarType> p2=f->V(2)->T().P();
		InterpParam(p0,p1,p2,UVStar,bary3d.X(),bary3d.Y(),bary3d.Z());
		bary.X()=bary3d.X();
		bary.Y()=bary3d.Y();
	}

	///given the diamond coordinates AS A QUAD return the coordinates in the parametrization space
	void inv_GE1Quad(const int &DiamIndex,
					const vcg::Point2<ScalarType> &UVQuad,
					int &I,
					vcg::Point2<ScalarType> &UV)
	{
		assert((UVQuad.X()>=0)&&(UVQuad.X()<=1));
		assert((UVQuad.Y()>=0)&&(UVQuad.Y()<=1));

		///get the abstract mesh
		AbstractMesh* diam_domain=diamond_meshes[DiamIndex].domain;
		///get the 4 vertices on the full diamond

		///transform in diamond coordinates
		vcg::Point2<ScalarType> c0=vcg::Point2<ScalarType>(0,(ScalarType)-0.5);
		vcg::Point2<ScalarType> c1=vcg::Point2<ScalarType>((sqrt((ScalarType)3.0)/(ScalarType)6.0),0);
		vcg::Point2<ScalarType> c2=vcg::Point2<ScalarType>(0,(ScalarType)0.5);
		vcg::Point2<ScalarType> c3=vcg::Point2<ScalarType>(-(sqrt((ScalarType)3.0)/(ScalarType)6.0),0);

		///get 2 directions
		vcg::Point2<ScalarType> v0=c1-c0;
		//v0.Normalize();
		vcg::Point2<ScalarType> v1=c3-c0;
		//v1.Normalize();
		
		///value in diamond coordinates
		vcg::Point2<ScalarType> UVDiam=c0+UVQuad.X()*v0;
		vcg::Point2<ScalarType> diry=UVQuad.Y()*v1;
		UVDiam=UVDiam+diry;
		
		//printf("Diamond: %d,%f,%f \n",DiamIndex,UVDiam.X(),UVDiam.Y());

		int index;
		CoordType bary;
#ifndef NDEBUG
		bool done=GetBaryFaceFromUV(*diam_domain,UVDiam.X(),UVDiam.Y(),bary,index);
#else
		GetBaryFaceFromUV(*diam_domain,UVDiam.X(),UVDiam.Y(),bary,index);
#endif
		UV.X()=bary.X();
		UV.Y()=bary.Y();

		I=diamond_meshes[DiamIndex].Local2Global(index);
		assert(done);
	}
	
	///TO RESCALE?
	///given the coordinates in the parametrization space return return the coordinates AS A QUAD 
	void GE1Quad(const int &I,
				 const vcg::Point2<ScalarType> &UV,
				 int &DiamIndex,
				 vcg::Point2<ScalarType> &UVQuad)
	{
		///first get the right half diamond index
		DiamIndex=getHDiamIndex(I,UV);
		
		///transform in diamond space
		vcg::Point2<ScalarType> UVDiam;
		GE1(I,UV,DiamIndex,UVDiam);
		
		///transform in diamond coordinates
		vcg::Point2<ScalarType> c0=vcg::Point2<ScalarType>(0,(ScalarType)-0.5);
		vcg::Point2<ScalarType> c1=vcg::Point2<ScalarType>((sqrt((ScalarType)3.0)/(ScalarType)6.0),0);
		vcg::Point2<ScalarType> c2=vcg::Point2<ScalarType>(0,(ScalarType)0.5);
		vcg::Point2<ScalarType> c3=vcg::Point2<ScalarType>(-(sqrt((ScalarType)3.0)/(ScalarType)6.0),0);

		///get 2 directions
		vcg::Point2<ScalarType> v0=c1-c0;
		//v0.Normalize();
		vcg::Point2<ScalarType> v1=c3-c0;
		//v1.Normalize();
		
		///translate respect to zero
		UVDiam-=c0;

		///then transform in new coordspace
		UVQuad.X()=v0.X()*UVDiam.X()+v0.Y()*UVDiam.Y();
		UVQuad.Y()=v1.X()*UVDiam.X()+v1.Y()*UVDiam.Y();

	}
	
	///given the diamond coordinates return return the coordinates AS A QUAD 
	void GE1Quad(const int &/*DiamIndex*/,
				 const vcg::Point2<ScalarType> &UVDiam,
				 vcg::Point2<ScalarType> &UVQuad)
	{
		//assert(UVDiam.Y()<0);
		///transform in diamond coordinates
		vcg::Point2<ScalarType> c0=vcg::Point2<ScalarType>(0,(ScalarType)-0.5);
		vcg::Point2<ScalarType> c1=vcg::Point2<ScalarType>((sqrt((ScalarType)3.0)/(ScalarType)6.0),0);
		vcg::Point2<ScalarType> c2=vcg::Point2<ScalarType>(0,(ScalarType)0.5);
		vcg::Point2<ScalarType> c3=vcg::Point2<ScalarType>(-(sqrt((ScalarType)3.0)/(ScalarType)6.0),0);
		
		///get 2 directions
		vcg::Point2<ScalarType> v0=c1-c0;
		vcg::Point2<ScalarType> v1=c3-c0;
		
		///translate respect to zero
		vcg::Point2<ScalarType> temp=UVDiam;
		//temp=c3;
		temp-=c0;
		ScalarType det=(ScalarType)1.0/(v0.X()*v1.Y()-v1.X()*v0.Y());
		///then transform in new coordspace
		UVQuad.X()=v1.Y()*temp.X()-v1.X()*temp.Y();
		UVQuad.Y()=-v0.Y()*temp.X()+v0.X()*temp.Y();
		UVQuad.X()*=det;
		UVQuad.Y()*=det;
	/*	printf("UVtr1=%f, %f \n",UVQuad.X(),UVQuad.Y());
		system("pause");*/
	}

	///return the coordinate on the face domain
	void GE2(const int &I,
		const vcg::Point2<ScalarType> &UV,
		vcg::Point2<ScalarType> &UVFace)
	{
		///get the right edge index
		CoordType bary=CoordType(UV.X(),UV.Y(),1-UV.X()-UV.Y());
		
		///then transform to the face domain
		///get face mesh
		AbstractMesh* face_domain=face_meshes[I].domain;
		AbstractFace* f=&(face_domain->face[0]);

		UVFace=bary.X()*f->V(0)->T().P()+bary.Y()*f->V(1)->T().P()+bary.Z()*f->V(2)->T().P();
	}

	///given the face coordinates return the coordinates in the parametrization space
	void inv_GE2(const int &FaceIndex,
		const vcg::Point2<ScalarType> &UVFace,
		vcg::Point2<ScalarType> &UV)
	{
		AbstractMesh* face_domain=face_meshes[FaceIndex].domain;
		CoordType bary;
		int index;
#ifndef NDEBUG
		bool done=GetBaryFaceFromUV(*face_domain,UVFace.X(),UVFace.Y(),bary,index);
		assert(done);
#else
		GetBaryFaceFromUV(*face_domain,UVFace.X(),UVFace.Y(),bary,index);
#endif
		UV.X()=bary.X();
		UV.Y()=bary.Y();

	}

	void Clear()
	{
		face_to_vert.clear();
		star_meshes.clear();
		face_meshes.clear();
		diamond_meshes.clear();
	}

	bool Update(bool test=true)
	{
		UpdateTopologies(abstract_mesh);
		UpdateTopologies(param_mesh);
		float fix_num=sqrt((ScalarType)3.0)/(ScalarType)4.0;

		int edge_count=0;
		///cont number of edges
		for (unsigned int i=0;i<abstract_mesh->face.size();i++)
		{
			if (!(abstract_mesh->face[i].IsD()))
			{
				AbstractFace *f0=&abstract_mesh->face[i];
				//for each edge
				for (int j=0;j<3;j++)
				{
					AbstractFace * f1=f0->FFp(j);
					if (f1>f0)
						edge_count++;
				}
			}
		}

		///test param mesh
		for (unsigned int i=0;i<param_mesh->vert.size();i++)
		{
			if (!(param_mesh->vert[i].IsD()))
			{
				ParamVertex *v0=&param_mesh->vert[i];
				ParamMesh::CoordType bary=ParamMesh::CoordType(v0->T().U(),v0->T().V(),1-v0->T().U()-v0->T().V());
				int patchNum=v0->T().N();
				if (!testBaryCoords(bary))
					return false;
				if (patchNum<0)
					return false;
				if (patchNum>AbsMesh()->fn)
					return false;
			}
		}
		Area3d=vcg::tri::Stat<ParamMesh>::ComputeMeshArea(*param_mesh);
		AbstractArea=(ScalarType)abstract_mesh->fn*fix_num;
		
		face_to_vert.clear();
		star_meshes.clear();
		face_meshes.clear();
		diamond_meshes.clear();
		star_meshes.resize(abstract_mesh->vn);
		face_meshes.resize(abstract_mesh->fn);
		diamond_meshes.resize(edge_count);
		InitFaceToVert();
		InitFace();
		InitDiamond();
		InitStar();
		if (test)
			return (Test());
		return true;
	}

	bool Init(AbstractMesh * _abstract_mesh,
			  ParamMesh	 * _param_mesh,bool test=true)
	{
		
		abstract_mesh=_abstract_mesh;
		param_mesh=_param_mesh;

		UpdateTopologies(abstract_mesh);
		UpdateTopologies(param_mesh);
		
		return (Update(test));
	}
	
	AbstractMesh *&AbsMesh(){return abstract_mesh;} 
	ParamMesh	 *&ParaMesh(){return param_mesh;} 

	///given the index of face and the index of the edge return the 
	///index of diamond
	int GetDiamond(const int &I,const int & edge)
	{
		AbstractVertex *v0=AbsMesh()->face[I].V0(edge);
		AbstractVertex *v1=AbsMesh()->face[I].V1(edge);
		int index;
		getDiamondFromPointer(v0,v1,index);
		return index;
	}

	int GetStarIndex(const int &I,const int & indexV)
	{
		AbstractVertex *v=AbsMesh()->face[I].V(indexV);
		int index;
		getStarFromPointer(v,index);
		return index;
	}

	void SaveBaseDomain(char *pathname)
	{
		/*vcg::tri::io::ExporterPLY<AbstractMesh>::Save(*AbsMesh(),pathname);*/
		/*Warp(0);*/
		FILE *f;
		f=fopen(pathname,"w+");
	
		std::map<AbstractVertex*,int> vertexmap;
		typedef std::map<AbstractVertex*,int>::iterator iteMapVert;

		///add vertices
		fprintf(f,"%d,%d \n",AbsMesh()->fn,AbsMesh()->vn);
		int index=0;
		for (unsigned int i=0;i<AbsMesh()->vert.size();i++)
		{
			AbstractVertex* vert=&AbsMesh()->vert[i];
			if (!vert->IsD())
			{
				vertexmap.insert(std::pair<AbstractVertex*,int>(vert,index));
				CoordType pos=vert->P();
				CoordType RPos=vert->RPos;
				fprintf(f,"%f,%f,%f;\n",pos.X(),pos.Y(),pos.Z());
				index++;
			}
		}

		///add faces
		for (unsigned int i=0;i<AbsMesh()->face.size();i++)
		{
			AbstractFace* face=&AbsMesh()->face[i];
			if (!face->IsD())
			{
				AbstractVertex* v0=face->V(0);
				AbstractVertex* v1=face->V(1);
				AbstractVertex* v2=face->V(2);
				iteMapVert vertIte;
				vertIte=vertexmap.find(v0);
				assert(vertIte!=vertexmap.end());
				int index0=(*vertIte).second;
				vertIte=vertexmap.find(v1);
				assert(vertIte!=vertexmap.end());
				int index1=(*vertIte).second;
				vertIte=vertexmap.find(v2);
				assert(vertIte!=vertexmap.end());
				int index2=(*vertIte).second;
				assert((index0!=index1)&&(index1!=index2));
				fprintf(f,"%d,%d,%d \n",index0,index1,index2);
			}
		}
		fclose(f);
	}

	template <class MeshType>
	bool SetParamMesh(MeshType	*_input_mesh,
										ParamMesh	 * _param_mesh)
	{
		param_mesh=_param_mesh;
		param_mesh->Clear();
		vcg::tri::Append<ParamMesh,MeshType>::Mesh(*param_mesh,*_input_mesh);
		return(Update(true));
	}

	template <class MeshType>
	bool LoadBaseDomain(char *pathname,
						MeshType	*_input_mesh,
						ParamMesh	 * _param_mesh,
						AbstractMesh *_absMesh,
            bool test=true)
//						bool use_quality=true)
	{
		param_mesh=_param_mesh;
		param_mesh->Clear();
		/*vcg::tri::Allocator<MeshType>::CompactVertexVector(*_input_mesh);
		vcg::tri::Allocator<MeshType>::CompactFaceVector(*_input_mesh);*/
		vcg::tri::Append<ParamMesh,MeshType>::Mesh(*param_mesh,*_input_mesh);
		/*UpdateStructures<ParamMesh>(param_mesh);*/

		///quality copy to index of texture
    for (size_t i=0;i<param_mesh->vert.size();i++)
		{
			int val0=(int)param_mesh->vert[i].Q();
			//int val1=param_mesh->vert[i].T().N();*/
			param_mesh->vert[i].T().N()=val0;
			assert(param_mesh->vert[i].T().N()>=0);
		}
		/*if (AbsMesh()!=NULL)
			delete(AbsMesh());*/

		//AbsMesh()=new AbstractMesh();
		AbsMesh()=_absMesh;
		AbsMesh()->Clear();

		FILE *f=NULL;              
		f=fopen(pathname,"r");
		if (f==NULL)
			return -1;
		
		///read vertices
		fscanf(f,"%d,%d \n",&abstract_mesh->fn,&abstract_mesh->vn);
		abstract_mesh->vert.resize(abstract_mesh->vn);
		abstract_mesh->face.resize(abstract_mesh->fn);

		for (unsigned int i=0;i<abstract_mesh->vert.size();i++)
		{
			AbstractVertex* vert=&abstract_mesh->vert[i];
			CoordType pos;
			fscanf(f,"%f,%f,%f;\n",&pos.X(),&pos.Y(),&pos.Z());
			vert->P()=pos;
		}

		///add faces
		for (unsigned int i=0;i<abstract_mesh->face.size();i++)
		{
			AbstractFace* face=&abstract_mesh->face[i];
			if (!face->IsD())
			{
				int index0,index1,index2;
				fscanf(f,"%d,%d,%d \n",&index0,&index1,&index2);
				abstract_mesh->face[i].V(0)=&abstract_mesh->vert[index0];
				abstract_mesh->face[i].V(1)=&abstract_mesh->vert[index1];
				abstract_mesh->face[i].V(2)=&abstract_mesh->vert[index2];
			}
		}
		UpdateTopologies<AbstractMesh>(AbsMesh());
		fclose(f);
		
		return (Update(test));
	}
	
	template <class MeshType>
	void CopyParametrization(MeshType	* _param_mesh)
	{
    for (size_t i=0;i<_param_mesh->vert.size();i++)
		{
			_param_mesh->vert[i].T().P()=ParaMesh()->vert[i].T().P();
			_param_mesh->vert[i].T().N()=ParaMesh()->vert[i].T().N();
      _param_mesh->vert[i].Q()=(typename MeshType::ScalarType)ParaMesh()->vert[i].T().N();
		}
	}

	template <class MeshType>
	int LoadMCP(AbstractMesh * _abstract_mesh,
			  ParamMesh	 * _param_mesh,
				char* filename,MeshType *coloredMesh=NULL)
	{
		abstract_mesh=_abstract_mesh;
		param_mesh=_param_mesh;

		FILE *f=NULL;              
		f=fopen(filename,"r");
		if (f==NULL)
			return -1;
		

		///add vertices
		abstract_mesh->Clear();
		fscanf(f,"%d,%d \n",&abstract_mesh->fn,&abstract_mesh->vn);
		abstract_mesh->vert.resize(abstract_mesh->vn);
		abstract_mesh->face.resize(abstract_mesh->fn);

		for (unsigned int i=0;i<abstract_mesh->vert.size();i++)
		{
			AbstractVertex* vert=&abstract_mesh->vert[i];
			CoordType pos;
			CoordType RPos;
			fscanf(f,"%f,%f,%f;%f,%f,%f \n",&pos.X(),&pos.Y(),&pos.Z(),&RPos.X(),&RPos.Y(),&RPos.Z());
			vert->P()=pos;
			//vert->RPos=RPos;
		}

		

		///add faces
		for (unsigned int i=0;i<abstract_mesh->face.size();i++)
		{
			AbstractFace* face=&abstract_mesh->face[i];
			if (!face->IsD())
			{
				int index0,index1,index2;
				fscanf(f,"%d,%d,%d \n",&index0,&index1,&index2);
				abstract_mesh->face[i].V(0)=&abstract_mesh->vert[index0];
				abstract_mesh->face[i].V(1)=&abstract_mesh->vert[index1];
				abstract_mesh->face[i].V(2)=&abstract_mesh->vert[index2];
			}
		}
		
		///high resolution mesh
		fscanf(f,"%d,%d \n",&param_mesh->fn,&param_mesh->vn);
		param_mesh->vert.resize(param_mesh->vn);
		param_mesh->face.resize(param_mesh->fn);

		///add vertices
		for (unsigned int i=0;i<param_mesh->vert.size();i++)
		{
			ParamVertex* vert=&param_mesh->vert[i];
			CoordType pos;
			CoordType bary;
			vcg::Color4b col;
			int index_face;
			int col0,col1,col2;
			fscanf(f,"%f,%f,%f;%f,%f,%f;%d,%d,%d;%d \n",
				  &pos.X(),&pos.Y(),&pos.Z(),
				  &bary.X(),&bary.Y(),&bary.Z(),
				  &col0,&col1,&col2,
				  &index_face);
			vert->P()=pos;
			//vert->RPos=pos;
			vert->T().P()=vcg::Point2<ScalarType>(bary.X(),bary.Y());
			vert->T().N()=index_face;
			if (coloredMesh!=NULL)
			{
				vcg::Color4b col=coloredMesh->vert[i].C();
				vert->C()=col;
			}
		}

		///add faces
		for (unsigned int i=0;i<param_mesh->face.size();i++)
		{
			//BaseFace* face=&final_mesh.face[i];
			
			int index0,index1,index2;
			fscanf(f,"%d,%d,%d \n",&index0,&index1,&index2);
			param_mesh->face[i].V(0)=&param_mesh->vert[index0];
			param_mesh->face[i].V(1)=&param_mesh->vert[index1];
			param_mesh->face[i].V(2)=&param_mesh->vert[index2];
			
		}
		fclose(f);
		
		///update structures
		vcg::tri::UpdateBounding<AbstractMesh>::Box(*abstract_mesh);
		vcg::tri::UpdateTopology<AbstractMesh>::FaceFace(*abstract_mesh);
		vcg::tri::UpdateTopology<AbstractMesh>::VertexFace(*abstract_mesh);

		vcg::tri::UpdateBounding<ParamMesh>::Box(*param_mesh);
		vcg::tri::UpdateTopology<ParamMesh>::FaceFace(*param_mesh);
		vcg::tri::UpdateTopology<ParamMesh>::VertexFace(*param_mesh);

		
		Update();

		return 0;
	}
};


#endif