File: isotropic_remeshing.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (1359 lines) | stat: -rw-r--r-- 41,903 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2017                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef _VCG_ISOTROPICREMESHING_H
#define _VCG_ISOTROPICREMESHING_H

#include <vcg/complex/algorithms/update/quality.h>
#include <vcg/complex/algorithms/update/curvature.h>
#include <vcg/complex/algorithms/update/normal.h>
#include <vcg/complex/algorithms/refine.h>
#include <vcg/complex/algorithms/stat.h>
#include <vcg/complex/algorithms/smooth.h>
#include <vcg/complex/algorithms/local_optimization/tri_edge_collapse.h>
#include <vcg/space/index/spatial_hashing.h>
#include <vcg/complex/append.h>
#include <vcg/complex/allocate.h>
#include <wrap/io_trimesh/export.h>

namespace vcg {
namespace tri {
template<class TRI_MESH_TYPE>
class IsotropicRemeshing
{
public:
	typedef TRI_MESH_TYPE MeshType;
	typedef typename MeshType::FaceType FaceType;
	typedef typename MeshType::FacePointer FacePointer;
	typedef typename FaceType::VertexType VertexType;
	typedef typename FaceType::VertexPointer VertexPointer;
	typedef	typename VertexType::ScalarType ScalarType;
	typedef	typename VertexType::CoordType CoordType;
	typedef typename face::Pos<FaceType> PosType;
	typedef BasicVertexPair<VertexType> VertexPair;
	typedef EdgeCollapser<MeshType, VertexPair> Collapser;
	typedef GridStaticPtr<FaceType, ScalarType> StaticGrid;


	typedef struct Params {
		typedef struct Stat {
			int splitNum;
			int collapseNum;
			int flipNum;

			void Reset() {
				splitNum=0;
				collapseNum=0;
				flipNum=0;
			}
		} Stat;


		ScalarType minLength; // minimal admitted length: no edge should be shorter than this value (used when collapsing)
		ScalarType maxLength; // maximal admitted length: no edge should be longer than this value  (used when refining)
		ScalarType lengthThr;

		ScalarType minimalAdmittedArea;
		ScalarType maxSurfDist;

		ScalarType aspectRatioThr  = 0.05;                    //min aspect ratio: during relax bad triangles will be relaxed
		ScalarType foldAngleCosThr = cos(math::ToRad(140.));   //min angle to be considered folding: during relax folded triangles will be relaxed

		ScalarType creaseAngleRadThr = math::ToRad(10.0);
		ScalarType creaseAngleCosThr = cos(math::ToRad(10.0)); //min angle to be considered crease: two faces with normals diverging more than thr share a crease edge

		bool splitFlag    = true;
		bool swapFlag     = true;
		bool collapseFlag = true;
		bool smoothFlag=true;
		bool projectFlag=true;
		bool selectedOnly = false;
		bool cleanFlag = true;

		bool userSelectedCreases = false;
		bool surfDistCheck = true;

		bool adapt=false;
		int iter=1;
		Stat stat;

		void SetTargetLen(const ScalarType len)
		{
			minLength=len*4./5.;
			maxLength=len*4./3.;
			lengthThr=len*4./3.;
			minimalAdmittedArea = (minLength * minLength)/1000.0;
		}
		
		void SetFeatureAngleDeg(const ScalarType angle)
		{
			creaseAngleRadThr =  math::ToRad(angle);
			creaseAngleCosThr = cos(creaseAngleRadThr);
		}

		StaticGrid grid;
		MeshType* m;
		MeshType* mProject;

	} Params;

private:
	static void debug_crease (MeshType & toRemesh, std::string  prepend, int i)
	{
		ForEachVertex(toRemesh, [] (VertexType & v) {
			v.C() = Color4b::Gray;
			v.Q() = 0;
		});

		ForEachFacePos(toRemesh, [&](PosType &p){
			if (p.F()->IsFaceEdgeS(p.E()))
			{
				p.V()->Q() += 1;
				p.VFlip()->Q() += 1;
			}
		});

		ForEachVertex(toRemesh, [] (VertexType & v) {
			if (v.Q() >= 4)
				v.C() = Color4b::Green;
			else if (v.Q() >= 2)
				v.C() = Color4b::Red;
		});
		prepend += "_creases" + std::to_string(i) + ".ply";
		vcg::tri::io::Exporter<MeshType>::Save(toRemesh, prepend.c_str(), vcg::tri::io::Mask::IOM_ALL);
	}


	static void removeColinearFaces(MeshType & m, Params & params)
	{
		vcg::tri::UpdateTopology<MeshType>::FaceFace(m);

		int count = 0;
        int iter = 0;
		do
		{
			vcg::tri::UpdateTopology<MeshType>::FaceFace(m);
			vcg::tri::UnMarkAll(m);

			count = 0;
			for (size_t i = 0; i < size_t(m.FN()); ++i)
			{
				FaceType & f = m.face[i];

				ScalarType quality = vcg::QualityRadii(f.cP(0), f.cP(1), f.cP(2));

                if (quality <= 0.001)
				{
					//find longest edge
					double edges[3];
					edges[0] = vcg::Distance(f.cP(0), f.cP(1));
					edges[1] = vcg::Distance(f.cP(1), f.cP(2));
					edges[2] = vcg::Distance(f.cP(2), f.cP(0));

                    ScalarType smallestEdge = std::min(edges[0], std::min(edges[1], edges[2]));
                    int longestIdx = std::find(edges, edges+3, std::max(std::max(edges[0], edges[1]), edges[2])) - (edges);

					if (vcg::tri::IsMarked(m, f.V2(longestIdx)))
						continue;


					auto f1 = f.cFFp(longestIdx);
					vcg::tri::Mark(m,f.V2(longestIdx));
					if (!vcg::face::IsBorder(f, longestIdx) && vcg::face::IsManifold(f, longestIdx) && vcg::face::checkFlipEdgeNotManifold<FaceType>(f, longestIdx))  {

						// Check if EdgeFlipping improves quality
						FacePointer g = f.FFp(longestIdx); int k = f.FFi(longestIdx);
						vcg::Triangle3<ScalarType> t1(f.P(longestIdx), f.P1(longestIdx), f.P2(longestIdx)), t2(g->P(k), g->P1(k), g->P2(k)),
						        t3(f.P(longestIdx), g->P2(k), f.P2(longestIdx)), t4(g->P(k), f.P2(longestIdx), g->P2(k));

						if ( std::min( QualityFace(t1), QualityFace(t2) ) <= std::min( QualityFace(t3), QualityFace(t4) ))
						{
							ScalarType dist;
							CoordType closest;
                            auto fp0 = vcg::tri::GetClosestFaceBase(*params.mProject, params.grid, vcg::Barycenter(t3), smallestEdge/4., dist, closest);
							if (fp0 == NULL)
								continue;

                            auto fp1 = vcg::tri::GetClosestFaceBase(*params.mProject, params.grid, vcg::Barycenter(t4), smallestEdge/4., dist, closest);
							if (fp1 == NULL)
								continue;

							vcg::face::FlipEdgeNotManifold<FaceType>(f, longestIdx);
							++count;
						}
					}
				}
			}
                } while (count && ++iter < 50);
	}

	static void cleanMesh(MeshType & m, Params & params)
	{
		vcg::tri::Clean<MeshType>::RemoveDuplicateFace(m);
		vcg::tri::Clean<MeshType>::RemoveUnreferencedVertex(m);
		vcg::tri::Allocator<MeshType>::CompactEveryVector(m);

		vcg::tri::UpdateTopology<MeshType>::FaceFace(m);
		removeColinearFaces(m, params);
		vcg::tri::UpdateTopology<MeshType>::FaceFace(m);
	}

public:

	static void Do(MeshType &toRemesh, Params & params, vcg::CallBackPos * cb=0)
	{
		MeshType toProjectCopy;
		tri::UpdateBounding<MeshType>::Box(toRemesh);
		tri::UpdateNormal<MeshType>::PerVertexNormalizedPerFaceNormalized(toRemesh);

		tri::Append<MeshType,MeshType>::MeshCopy(toProjectCopy, toRemesh);

		Do(toRemesh,toProjectCopy,params,cb);
	}
	static void Do(MeshType &toRemesh, MeshType &toProject, Params & params, vcg::CallBackPos * cb=0)
	{
		assert(&toRemesh != &toProject);
		params.stat.Reset();


		tri::UpdateBounding<MeshType>::Box(toRemesh);

		{
			tri::UpdateBounding<MeshType>::Box(toProject);
			tri::UpdateNormal<MeshType>::PerFaceNormalized(toProject);
			params.m = &toRemesh;
			params.mProject = &toProject;
			params.grid.Set(toProject.face.begin(), toProject.face.end());
		}

		if (params.cleanFlag)
			cleanMesh(toRemesh, params);

		tri::UpdateTopology<MeshType>::FaceFace(toRemesh);
		tri::UpdateFlags<MeshType>::VertexBorderFromFaceAdj(toRemesh);
		tri::UpdateTopology<MeshType>::VertexFace(toRemesh);

		//		computeQuality(toRemesh);
		//		tri::UpdateQuality<MeshType>::VertexSaturate(toRemesh);

		tagCreaseEdges(toRemesh, params);

		for(int i=0; i < params.iter; ++i)
		{
			//			params.stat.Reset();
			if(cb) cb(100*i/params.iter, "Remeshing");

			if(params.splitFlag)
				SplitLongEdges(toRemesh, params);
#ifdef DEBUG_CREASE
			debug_crease(toRemesh, std::string("after_ref"), i);
#endif

			if(params.collapseFlag)
			{
				CollapseShortEdges(toRemesh, params);
				CollapseCrosses(toRemesh, params);
			}

			if(params.swapFlag)
				ImproveValence(toRemesh, params);

			if(params.smoothFlag)
				ImproveByLaplacian(toRemesh, params);

			if(params.projectFlag)
				ProjectToSurface(toRemesh, params);
		}
	}

private:
	/*
		TODO: Add better crease support: detect all creases at starting time, saving it on facedgesel flags
			  All operations must then preserve the faceedgesel flag accordingly:
				Refinement -> Check that refiner propagates faceedgesel [should be doing it]
				Collapse   -> Implement 1D edge collapse and better check on corners and creases
				Swap       -> Totally avoid swapping crease edges [ok]
				Smooth     -> Apply 1D smoothing to crease vertices + check on
								(http://www.cs.ubc.ca/labs/imager/tr/2009/eltopo/sisc2009.pdf)
	*/
	IsotropicRemeshing() {}
	// this returns the value of cos(a) where a is the angle between n0 and n1. (scalar prod is cos(a))
	static inline ScalarType fastAngle(Point3<ScalarType> n0, Point3<ScalarType> n1)
	{
		return math::Clamp(n0*n1,(ScalarType)-1.0,(ScalarType)1.0);
	}
	// compare the value of the scalar prod with the cos of the crease threshold
	static inline bool testCreaseEdge(PosType &p, ScalarType creaseCosineThr)
	{
		ScalarType angle = fastAngle(NormalizedTriangleNormal(*(p.F())), NormalizedTriangleNormal(*(p.FFlip())));
		return angle <= creaseCosineThr && angle >= -0.98;
		//        return (angle <= creaseCosineThr && angle >= -creaseCosineThr);
	}
	// this stores in minQ the value of the 10th percentile of the VertQuality distribution and in
	// maxQ the value of the 90th percentile.
	static inline void computeVQualityDistrMinMax(MeshType &m, ScalarType &minQ, ScalarType &maxQ)
	{
		Distribution<ScalarType> distr;
		tri::Stat<MeshType>::ComputePerVertexQualityDistribution(m,distr);

		maxQ = distr.Percentile(0.9f);
		minQ = distr.Percentile(0.1f);
	}

	//Computes PerVertexQuality as a function of the 'deviation' of the normals taken from
	//the faces incident to each vertex
	static void computeQuality(MeshType &m)
	{
		tri::RequirePerVertexQuality(m);
		tri::UpdateFlags<MeshType>::VertexClearV(m);

		for(auto vi=m.vert.begin(); vi!=m.vert.end(); ++vi)
			if(!(*vi).IsD())
			{
				vector<FaceType*> ff;
				face::VFExtendedStarVF(&*vi, 0, ff);

				ScalarType tot = 0.f;
				auto it = ff.begin();
				Point3<ScalarType> fNormal = NormalizedTriangleNormal(**it);
				++it;
				while(it != ff.end())
				{
					tot+= 1-math::Abs(fastAngle(fNormal, NormalizedTriangleNormal(**it)));
					++it;
				}
				vi->Q() = tot / (ScalarType)(std::max(1, ((int)ff.size()-1)));
				vi->SetV();
			}
	}

	/*
	 Computes the ideal valence for the vertex in pos p:
	 4 for border vertices
	 6 for internal vertices
	*/
	static inline int idealValence(PosType &p)
	{
		if(p.IsBorder()) return 4;
		return 6;
	}
	static inline int idealValence(VertexType &v)
	{
		if(v.IsB()) return 4;
		return 6;
	}
	static inline int idealValenceSlow(PosType &p)
	{
		std::vector<PosType> posVec;
		VFOrderedStarFF(p,posVec);
		float angleSumRad =0;
		for(PosType &ip : posVec)
		{
			angleSumRad += ip.AngleRad();
		}

		return (int)(std::ceil(angleSumRad / (M_PI/3.0f)));
	}

	static bool testHausdorff (MeshType & m, StaticGrid & grid, const std::vector<CoordType> & verts, const ScalarType maxD)
	{
		for (CoordType v : verts)
		{
			CoordType closest;
			ScalarType dist = 0;
			FaceType* fp = GetClosestFaceBase(m, grid, v, maxD, dist, closest);

			if (fp == NULL)
			{
				return false;
			}
		}
		return true;
	}

	static int tagCreaseEdges(MeshType &m, Params & params)
	{
		int count = 0;
		std::vector<char> creaseVerts(m.VN(), 0);

		vcg::tri::UpdateFlags<MeshType>::VertexClearV(m);
		std::queue<PosType> creaseQueue;
		ForEachFacePos(m, [&](PosType &p){

			if (p.IsBorder())
				p.F()->SetFaceEdgeS(p.E());

			//			if((p.FFlip() > p.F()))
			{
				FaceType *ff    = p.F();
				FaceType *ffAdj = p.FFlip();

				double quality    = vcg::QualityRadii(ff->cP(0), ff->cP(1), ff->cP(2));
				double qualityAdj = vcg::QualityRadii(ffAdj->cP(0), ffAdj->cP(1), ffAdj->cP(2));

				bool qualityCheck = quality > 0.00000001 && qualityAdj > 0.00000001;
//				bool areaCheck    = vcg::DoubleArea(*ff) > 0.000001 && vcg::DoubleArea(*ffAdj) > 0.000001;

				if (!params.userSelectedCreases && (testCreaseEdge(p, params.creaseAngleCosThr) /*&& areaCheck*//* && qualityCheck*/) || p.IsBorder())
				{
					PosType pp = p;
					do {
						pp.F()->SetFaceEdgeS(pp.E());
						pp.NextF();
					} while (pp != p);

					creaseQueue.push(p);
				}
			}
		});

		//		//now all creases are checked...
		//		//prune false positive (too small) (count + scale?)

		//		while (!creaseQueue.empty())
		//		{
		//			PosType & p = creaseQueue.front();
		//			creaseQueue.pop();

		//			std::stack<PosType> chainQueue;
		//			std::vector<size_t> chainVerts;

		//			if (!p.V()->IsV())
		//			{
		//				chainQueue.push(p);
		//			}

		//			p.FlipV();
		//			p.NextEdgeS();

		//			if (!p.V()->IsV())
		//			{
		//				chainQueue.push(p);
		//			}

		//			while (!chainQueue.empty())
		//			{
		//				PosType p = chainQueue.top();
		//				chainQueue.pop();

		//				p.V()->SetV();
		//				chainVerts.push_back(vcg::tri::Index(m, p.V()));

		//				PosType pp = p;

		//				//circle around vert in search for new crease edges
		//				do {
		//					pp.NextF(); //jump adj face
		//					pp.FlipE(); // this edge is already ok => jump to next
		//					if (pp.IsEdgeS())
		//					{
		//						PosType nextPos = pp;
		//						nextPos.FlipV(); // go to next vert in the chain
		//						if (!nextPos.V()->IsV()) // if already visited...ignore
		//						{
		//							chainQueue.push(nextPos);
		//						}
		//					}
		//				}
		//				while (pp != p);

		//			}

		//			if (chainVerts.size() > 5)
		//			{
		//				for (auto vp : chainVerts)
		//				{
		//					creaseVerts[vp] = 1;
		//				}
		//			}
		//		}
		//		//store crease on V()

		//		//this aspect ratio check doesn't work on cadish meshes (long thin triangles spanning whole mesh)
		//		ForEachFace(m, [&] (FaceType & f) {
		//			if (vcg::QualityRadii(f.cP(0), f.cP(1), f.cP(2)) < params.aspectRatioThr)
		//			{
		//				if (creaseVerts[vcg::tri::Index(m, f.V(0))] == 0)
		//					f.V(0)->SetS();
		//				if (creaseVerts[vcg::tri::Index(m, f.V(1))] == 0)
		//					f.V(1)->SetS();
		//				if (creaseVerts[vcg::tri::Index(m, f.V(2))] == 0)
		//					f.V(2)->SetS();
		//			}
		//		});

		//		ForEachFace(m, [&] (FaceType & f) {
		//			for (int i = 0; i < 3; ++i)
		//			{
		//				if (f.FFp(i) > &f)
		//				{
		//					ScalarType angle = fastAngle(NormalizedTriangleNormal(f), NormalizedTriangleNormal(*(f.FFp(i))));
		//					if (angle <= params.foldAngleCosThr)
		//					{
		//						//						if (creaseVerts[vcg::tri::Index(m, f.V0(i))] == 0)
		//						f.V0(i)->SetS();
		//						//						if (creaseVerts[vcg::tri::Index(m, f.V1(i))] == 0)
		//						f.V1(i)->SetS();
		//						//						if (creaseVerts[vcg::tri::Index(m, f.V2(i))] == 0)
		//						f.V2(i)->SetS();
		//						//						if (creaseVerts[vcg::tri::Index(m, f.FFp(i)->V2(f.FFi(i)))] == 0)
		//						f.FFp(i)->V2(f.FFi(i))->SetS();
		//					}
		//				}
		//			}
		//		});

		return count;
	}


	/*
		Edge Swap Step:
		This method optimizes the valence of each vertex.
		oldDist is the sum of the absolute distance of each vertex from its ideal valence
		newDist is the sum of the absolute distance of each vertex from its ideal valence after
		the edge swap.
		If the swap decreases the total absolute distance, then it's applied, preserving the triangle
		quality.                        +1
			   v1                     v1
			  /  \                   /|\
			 /    \                 / | \
			/      \               /  |  \
		   /     _*p\           -1/   |   \ -1
		  v2--------v0 ========> v2   |   v0
		   \        /             \   |   /
			\      /               \  |  /
			 \    /                 \ | /
			  \  /                   \|/ +1
			   v3                     v3
			Before Swap             After Swap
	*/
	static bool testSwap(PosType p, ScalarType creaseAngleCosThr)
	{
		//if border or feature, do not swap
		if (/*p.IsBorder() || */p.IsEdgeS()) return false;

		int oldDist = 0, newDist = 0, idealV, actualV;

		PosType tp=p;

		VertexType *v0=tp.V();

		std::vector<VertexType*> incident;

		vcg::face::VVStarVF<FaceType>(tp.V(), incident);
		idealV  = idealValence(tp); actualV = incident.size();
		oldDist += abs(idealV - actualV); newDist += abs(idealV - (actualV - 1));

		tp.NextF();tp.FlipE();tp.FlipV();
		VertexType *v1=tp.V();
		vcg::face::VVStarVF<FaceType>(tp.V(), incident);
		idealV  = idealValence(tp); actualV = incident.size();
		oldDist += abs(idealV - actualV); newDist += abs(idealV - (actualV + 1));

		tp.FlipE();tp.FlipV();tp.FlipE();
		VertexType *v2=tp.V();
		vcg::face::VVStarVF<FaceType>(tp.V(), incident);
		idealV  = idealValence(tp); actualV = incident.size();
		oldDist += abs(idealV - actualV); newDist += abs(idealV - (actualV - 1));

		tp.NextF();tp.FlipE();tp.FlipV();
		VertexType *v3=tp.V();
		vcg::face::VVStarVF<FaceType>(tp.V(), incident);
		idealV  = idealValence(tp); actualV = incident.size();
		oldDist += abs(idealV - actualV); newDist += abs(idealV - (actualV + 1));

		ScalarType qOld = std::min(Quality(v0->P(),v2->P(),v3->P()),Quality(v0->P(),v1->P(),v2->P()));
		ScalarType qNew = std::min(Quality(v0->P(),v1->P(),v3->P()),Quality(v2->P(),v3->P(),v1->P()));

		return (newDist < oldDist && qNew >= qOld * 0.50f) ||
		        (newDist == oldDist && qNew > qOld * 1.f) || qNew > 1.5f * qOld;
	}

	static bool checkManifoldness(FaceType & f, int z)
	{
		PosType pos(&f, (z+2)%3, f.V2(z));
		PosType start = pos;

		do {
			pos.FlipE();
			if (!face::IsManifold(*pos.F(), pos.E()))
				break;
			pos.FlipF();
		} while (pos!=start);

		return pos == start;
	}

	// Edge swap step: edges are flipped in order to optimize valence and triangle quality across the mesh
	static void ImproveValence(MeshType &m, Params &params)
	{
		static ScalarType foldCheckRad = math::ToRad(5.);
		tri::UpdateTopology<MeshType>::FaceFace(m);
		tri::UpdateTopology<MeshType>::VertexFace(m);
		ForEachFace(m, [&] (FaceType & f) {
//			if (face::IsManifold(f, 0) && face::IsManifold(f, 1) && face::IsManifold(f, 2))
				for (int i = 0; i < 3; ++i)
				{
					if (&f > f.cFFp(i))
					{
						PosType pi(&f, i);
						CoordType swapEdgeMidPoint = (f.cP2(i) + f.cFFp(i)->cP2(f.cFFi(i))) / 2.;
						std::vector<CoordType> toCheck(1, swapEdgeMidPoint);


						if(((!params.selectedOnly) || (f.IsS() && f.cFFp(i)->IsS())) &&
						   !face::IsBorder(f, i) &&
						   face::IsManifold(f, i) && /*checkManifoldness(f, i) &&*/
						   face::checkFlipEdgeNotManifold(f, i) &&
						   testSwap(pi, params.creaseAngleCosThr) &&
						   (!params.surfDistCheck || testHausdorff(*params.mProject, params.grid, toCheck, params.maxSurfDist)) &&
						   face::CheckFlipEdgeNormal(f, i, vcg::math::ToRad(5.)))
						{
							//When doing the swap we need to preserve and update the crease info accordingly
							FaceType* g = f.cFFp(i);
							int w = f.FFi(i);

							bool creaseF = g->IsFaceEdgeS((w + 1) % 3);
							bool creaseG = f.IsFaceEdgeS((i + 1) % 3);

							face::FlipEdgeNotManifold(f, i);

							f.ClearFaceEdgeS((i + 1) % 3);
							g->ClearFaceEdgeS((w + 1) % 3);

							if (creaseF)
								f.SetFaceEdgeS(i);
							if (creaseG)
								g->SetFaceEdgeS(w);

							++params.stat.flipNum;
							break;
						}
					}
				}
		});
	}

	// The predicate that defines which edges should be split
	class EdgeSplitAdaptPred
	{
	public:
		int count = 0;
		ScalarType length, lengthThr, minQ, maxQ;
		bool operator()(PosType &ep)
		{
			ScalarType mult = math::ClampedLerp((ScalarType)0.5,(ScalarType)1.5, (((math::Abs(ep.V()->Q())+math::Abs(ep.VFlip()->Q()))/(ScalarType)2.0)/(maxQ-minQ)));
			ScalarType dist = Distance(ep.V()->P(), ep.VFlip()->P());
			if(dist > std::max(mult*length,lengthThr*2))
			{
				++count;
				return true;
			}
			else
				return false;
		}
	};

	class EdgeSplitLenPred
	{
	public:
		int count = 0;
		ScalarType squaredlengthThr;
		bool operator()(PosType &ep)
		{
			if(SquaredDistance(ep.V()->P(), ep.VFlip()->P()) > squaredlengthThr)
			{
				++count;
				return true;
			}
			else
				return false;
		}
	};

	//Split pass: This pass uses the tri::RefineE from the vcglib to implement
	//the refinement step, using EdgeSplitPred as a predicate to decide whether to split or not
	static void SplitLongEdges(MeshType &m, Params &params)
	{
		tri::UpdateTopology<MeshType>::FaceFace(m);
		tri::MidPoint<MeshType> midFunctor(&m);

		ScalarType minQ,maxQ;
		if(params.adapt){
			computeVQualityDistrMinMax(m, minQ, maxQ);
			EdgeSplitAdaptPred ep;
			ep.minQ      = minQ;
			ep.maxQ      = maxQ;
			ep.length    = params.maxLength;
			ep.lengthThr = params.lengthThr;
			tri::RefineE(m,midFunctor,ep);
			params.stat.splitNum+=ep.count;
		}
		else {
			EdgeSplitLenPred ep;
			ep.squaredlengthThr = params.maxLength*params.maxLength;
			tri::RefineMidpoint(m, ep, params.selectedOnly);
			params.stat.splitNum+=ep.count;
		}
	}

	static int VtoE(const int v0, const int v1)
	{
		static /*constexpr*/ int Vmat[3][3] = { -1,  0,  2,
		                                        0, -1,  1,
		                                        2,  1, -1};
		return Vmat[v0][v1];
	}


	static bool checkCanMoveOnCollapse(PosType p, std::vector<FaceType*> & faces, std::vector<int> & vIdxes, Params &params)
	{
		bool allIncidentFaceSelected = true;

		PosType pi = p;

		CoordType dEdgeVector = (p.V()->cP() - p.VFlip()->cP()).Normalize();

		int incidentFeatures = 0;

		for (size_t i = 0; i < faces.size(); ++i)
//			if (faces[i] != p.F() && faces[i] != p.FFlip())
		    {
			    if (faces[i]->IsFaceEdgeS(VtoE(vIdxes[i], (vIdxes[i]+1)%3)))
				{
					incidentFeatures++;
					CoordType movingEdgeVector0 = (faces[i]->cP1(vIdxes[i]) - faces[i]->cP(vIdxes[i])).Normalize();
					if (std::fabs(movingEdgeVector0 * dEdgeVector) < .9f || !p.IsEdgeS())
						return false;
				}
				if (faces[i]->IsFaceEdgeS(VtoE(vIdxes[i], (vIdxes[i]+2)%3)))
				{
					incidentFeatures++;
					CoordType movingEdgeVector1 = (faces[i]->cP2(vIdxes[i]) - faces[i]->cP(vIdxes[i])).Normalize();
					if (std::fabs(movingEdgeVector1 * dEdgeVector) < .9f || !p.IsEdgeS())
						return false;
				}
				allIncidentFaceSelected &= faces[i]->IsS();
		    }

		if (incidentFeatures > 4)
			return false;

		return params.selectedOnly ? allIncidentFaceSelected : true;
	}

	static bool checkFacesAfterCollapse (std::vector<FaceType*> & faces, PosType p, const Point3<ScalarType> &mp, Params &params, bool relaxed)
	{
		for (FaceType* f : faces)
		{
			if(!(*f).IsD() && f != p.F()) //i'm not a deleted face
			{
				PosType pi(f, p.V()); //same vertex

				VertexType *v0 = pi.V();
				VertexType *v1 = pi.F()->V1(pi.VInd());
				VertexType *v2 = pi.F()->V2(pi.VInd());

				if( v1 == p.VFlip() || v2 == p.VFlip()) //i'm the other deleted face
					continue;

				//check on new face quality
				{
					ScalarType newQ = Quality(mp,      v1->P(), v2->P());
					ScalarType oldQ = Quality(v0->P(), v1->P(), v2->P());

					if( newQ <= 0.5*oldQ  )
						return false;
				}

				// we prevent collapse that makes edges too long (except for cross)
				if(!relaxed)
					if((Distance(mp, v1->P()) > params.maxLength || Distance(mp, v2->P()) > params.maxLength))
						return false;

				Point3<ScalarType> oldN = NormalizedTriangleNormal(*(pi.F()));
				Point3<ScalarType> newN = Normal(mp, v1->P(), v2->P()).Normalize();

				float div = fastAngle(oldN, newN);
				if(div < .0f ) return false;

				//				//				check on new face distance from original mesh
				if (params.surfDistCheck)
				{
					std::vector<CoordType> points(4);
					points[0] = (v1->cP() + v2->cP() + mp) / 3.;
					points[1] = (v1->cP() + mp) / 2.;
					points[2] = (v2->cP() + mp) / 2.;
					points[3] = mp;
					if (!testHausdorff(*(params.mProject), params.grid, points, params.maxSurfDist))
						return false;
				}
			}
		}
		return true;
	}


	//TODO: Refactor code and implement the correct set up of crease info when collapsing towards a crease edge
	static bool checkCollapseFacesAroundVert1(PosType &p, Point3<ScalarType> &mp, Params &params, bool relaxed)
	{
		PosType p0 = p, p1 = p;

		p1.FlipV();

		vector<int> vi0, vi1;
		vector<FaceType*> ff0, ff1;

		face::VFStarVF<FaceType>(p0.V(), ff0, vi0);
		face::VFStarVF<FaceType>(p1.V(), ff1, vi1);

		//check crease-moveability
		bool moveable0 = checkCanMoveOnCollapse(p0, ff0, vi0, params) && !p0.V()->IsS();
		bool moveable1 = checkCanMoveOnCollapse(p1, ff1, vi1, params) && !p1.V()->IsS();

		//if both moveable => go to midpoint
		// else collapse on movable one
		if (!moveable0 && !moveable1)
			return false;

		//casting int(true) is always 1 and int(false) = =0
		assert(int(true) == 1);
		assert(int(false) == 0);
		mp = (p0.V()->cP() * int(moveable1) + p1.V()->cP() * int(moveable0)) / (int(moveable0) + int(moveable1));

		if (!moveable0)
			p = p0;
		else
			p = p1;

		if (checkFacesAfterCollapse(ff0, p0, mp, params, relaxed))
			return checkFacesAfterCollapse(ff1, p1, mp, params, relaxed);

		return false;
	}

	static bool testCollapse1(PosType &p, Point3<ScalarType> &mp, ScalarType minQ, ScalarType maxQ, Params &params, bool relaxed = false)
	{
		ScalarType mult = (params.adapt) ? math::ClampedLerp((ScalarType)0.5,(ScalarType)1.5, (((math::Abs(p.V()->Q())+math::Abs(p.VFlip()->Q()))/(ScalarType)2.0)/(maxQ-minQ))) : (ScalarType)1;
		ScalarType dist = Distance(p.V()->P(), p.VFlip()->P());
		ScalarType thr = mult*params.minLength;
		ScalarType area = DoubleArea(*(p.F()))/2.f;
		if(relaxed || (dist < thr || area < params.minLength*params.minLength/100.f))//if to collapse
		{
			return checkCollapseFacesAroundVert1(p, mp, params, relaxed);
		}
		return false;
	}

	//This function is especially useful to enforce feature preservation during collapses
	//of boundary edges in planar or near planar section of the mesh
	static bool chooseBoundaryCollapse(PosType &p, VertexPair &pair)
	{
		Point3<ScalarType> collapseNV, collapsedNV0, collapsedNV1;
		collapseNV = (p.V()->P() - p.VFlip()->P()).normalized();

		vector<VertexType*> vv;
		face::VVStarVF<FaceType>(p.V(), vv);

		for(VertexType *v: vv)
			if(!(*v).IsD() && (*v).IsB() && v != p.VFlip()) //ignore non border
				collapsedNV0 = ((*v).P() - p.VFlip()->P()).normalized(); //edge vector after collapse

		face::VVStarVF<FaceType>(p.VFlip(), vv);

		for(VertexType *v: vv)
			if(!(*v).IsD() && (*v).IsB() && v != p.V()) //ignore non border
				collapsedNV1 = ((*v).P() - p.V()->P()).normalized(); //edge vector after collapse

		float cosine = cos(math::ToRad(1.5f));
		float angle0 = fabs(fastAngle(collapseNV, collapsedNV0));
		float angle1 = fabs(fastAngle(collapseNV, collapsedNV1));
		//if on both sides we deviate too much after collapse => don't collapse
		if(angle0 <= cosine && angle1 <= cosine)
			return false;
		//choose the best collapse (the more parallel one to the previous edge..)
		pair = (angle0 >= angle1) ? VertexPair(p.V(), p.VFlip()) : VertexPair(p.VFlip(), p.V());
		return true;
	}

	//The actual collapse step: foreach edge it is collapse iff TestCollapse returns true AND
	// the linkConditions are preserved
	static void CollapseShortEdges(MeshType &m, Params &params)
	{
		ScalarType minQ, maxQ;
		int candidates = 0;

		if(params.adapt)
			computeVQualityDistrMinMax(m, minQ, maxQ);

		tri::UpdateTopology<MeshType>::VertexFace(m);
		tri::UpdateFlags<MeshType>::FaceBorderFromVF(m);
		tri::UpdateFlags<MeshType>::VertexBorderFromFaceBorder(m);

		SelectionStack<MeshType> ss(m);
		ss.push();

		{
			tri::UpdateTopology<MeshType>::FaceFace(m);
			Clean<MeshType>::CountNonManifoldVertexFF(m,true);

			//FROM NOW ON VSelection is NotManifold

			for(auto fi=m.face.begin(); fi!=m.face.end(); ++fi)
				if(!(*fi).IsD() && (params.selectedOnly == false || fi->IsS()))
				{
					for(auto i=0; i<3; ++i)
					{
						PosType pi(&*fi, i);
						++candidates;
						VertexPair  bp = VertexPair(pi.V(), pi.VFlip());
						Point3<ScalarType> mp = (pi.V()->P()+pi.VFlip()->P())/2.f;

						if(testCollapse1(pi, mp, minQ, maxQ, params) && Collapser::LinkConditions(bp))
						{
							//collapsing on pi.V()
							bp = VertexPair(pi.VFlip(), pi.V());

							Collapser::Do(m, bp, mp, true);
							++params.stat.collapseNum;
							break;
						}

					}
				}
		}

		ss.pop();
	}


	//Here I just need to check the faces of the cross, since the other faces are not
	//affected by the collapse of the internal faces of the cross.
	static bool testCrossCollapse(PosType &p, std::vector<FaceType*> ff, std::vector<int> vi, Point3<ScalarType> &mp, Params &params)
	{
		if(!checkFacesAfterCollapse(ff, p, mp, params, true))
			return false;
		return true;
	}

	/*
	 *Choose the best way to collapse a cross based on the (external) cross vertices valence
	 *and resulting face quality
	 *                                      +0                   -1
	 *             v1                    v1                    v1
	 *            /| \                   /|\                  / \
	 *           / |  \                 / | \                /   \
	 *          /  |   \               /  |  \              /     \
	 *         / *p|    \           -1/   |   \ -1       +0/       \+0
	 *       v0-------- v2 ========> v0   |   v2    OR    v0-------v2
	 *        \    |    /             \   |   /            \       /
	 *         \   |   /               \  |  /              \     /
	 *          \  |  /                 \ | /                \   /
	 *           \ | /                   \|/ +0               \ / -1
	 *             v3                     v3                   v3
	 */
	static bool chooseBestCrossCollapse(PosType &p, VertexPair& bp, vector<FaceType*> &ff)
	{
		vector<VertexType*> vv0, vv1, vv2, vv3;
		VertexType *v0, *v1, *v2, *v3;

		v0 = p.F()->V1(p.VInd());
		v1 = p.F()->V2(p.VInd());


		bool crease[4] = {false, false, false, false};

		crease[0] = p.F()->IsFaceEdgeS(VtoE(p.VInd(), (p.VInd()+1)%3));
		crease[1] = p.F()->IsFaceEdgeS(VtoE(p.VInd(), (p.VInd()+2)%3));

		for(FaceType *f: ff)
			if(!(*f).IsD() && f != p.F())
			{
				PosType pi(f, p.V());
				VertexType *fv1 = pi.F()->V1(pi.VInd());
				VertexType *fv2 = pi.F()->V2(pi.VInd());

				if(fv1 == v0 || fv2 == v0)
				{
					if (fv1 == 0)
					{
						v3 = fv2;
						crease[3] = f->IsFaceEdgeS(VtoE(pi.VInd(), (pi.VInd()+2)%3));
					}
					else
					{
						v3 = fv1;
						crease[3] = f->IsFaceEdgeS(VtoE(pi.VInd(), (pi.VInd()+1)%3));
					}
					//					v3 = (fv1 == v0) ? fv2 : fv1;
				}

				if(fv1 == v1 || fv2 == v1)
				{
					if (fv1 == v1)
					{
						v2 = fv2;
						crease[2] = f->IsFaceEdgeS(VtoE(pi.VInd(), (pi.VInd()+2)%3));
					}
					else
					{
						v2 = fv1;
						crease[2] = f->IsFaceEdgeS(VtoE(pi.VInd(), (pi.VInd()+1)%3));
					}
					//					v2 = (fv1 == v1) ? fv2 : fv1;
				}
			}

		face::VVStarVF<FaceType>(v0, vv0);
		face::VVStarVF<FaceType>(v1, vv1);
		face::VVStarVF<FaceType>(v2, vv2);
		face::VVStarVF<FaceType>(v3, vv3);

		int nv0 = vv0.size(), nv1 = vv1.size();
		int nv2 = vv2.size(), nv3 = vv3.size();

		int delta1 = (idealValence(*v0) - nv0) + (idealValence(*v2) - nv2);
		int delta2 = (idealValence(*v1) - nv1) + (idealValence(*v3) - nv3);

		ScalarType Q1 = std::min(Quality(v0->P(), v1->P(), v3->P()), Quality(v1->P(), v2->P(), v3->P()));
		ScalarType Q2 = std::min(Quality(v0->P(), v1->P(), v2->P()), Quality(v2->P(), v3->P(), v0->P()));

		if (crease[0] || crease[1] || crease[2] || crease[3])
			return false;
		//		if (crease[0] && crease[1] && crease[2] && crease[3])
		//		{
		//			return false;
		//		}

		//		if (crease[0] || crease[2])
		//		{
		//			bp = VertexPair(p.V(), v0);
		//			return true;
		//		}

		//		if (crease[1] || crease[3])
		//		{
		//			bp = VertexPair(p.V(), v1);
		//			return true;
		//		}

		//no crease
		if(delta1 < delta2 && Q1 >= 0.6f*Q2)
		{
			bp = VertexPair(p.V(), v1);
			return true;
		}
		else
		{
			bp = VertexPair(p.V(), v0);
			return true;
		}
	}
	//Cross Collapse pass: This pass cleans the mesh from cross vertices, keeping in mind the link conditions
	//and feature preservations tests.
	static void CollapseCrosses(MeshType &m , Params &params)
	{
		tri::UpdateTopology<MeshType>::VertexFace(m);
		tri::UpdateFlags<MeshType>::VertexBorderFromNone(m);
		int count = 0;

		SelectionStack<MeshType> ss(m);
		ss.push();


		{
			tri::UpdateTopology<MeshType>::FaceFace(m);
			Clean<MeshType>::CountNonManifoldVertexFF(m,true);

			//From now on Selection on vertices is not manifoldness

			for(auto fi=m.face.begin(); fi!=m.face.end(); ++fi)
				if(!(*fi).IsD() && (!params.selectedOnly || fi->IsS()))
				{
					for(auto i=0; i<3; ++i)
					{
						PosType pi(&*fi, i);
						if(!pi.V()->IsB())
						{
							vector<FaceType*> ff;
							vector<int> vi;
							face::VFStarVF<FaceType>(pi.V(), ff, vi);

							//if cross need to check what creases you have and decide where to collapse accordingly
							//if tricuspidis need whenever you have at least one crease => can't collapse anywhere
							if(ff.size() == 4 || ff.size() == 3)
							{
								//							VertexPair bp;
								VertexPair  bp = VertexPair(pi.V(), pi.VFlip());
								Point3<ScalarType> mp = (pi.V()->P()+pi.VFlip()->P())/2.f;

								if(testCollapse1(pi, mp, 0, 0, params, true) && Collapser::LinkConditions(bp))
								{
									bp = VertexPair(pi.VFlip(), pi.V());
									Collapser::Do(m, bp, mp, true);
									++params.stat.collapseNum;
									++count;
									break;
								}
							}
						}
					}
				}
		}

		ss.pop();
		Allocator<MeshType>::CompactEveryVector(m);
	}

	// This function sets the selection bit on vertices that lie on creases
	static int selectVertexFromCrease(MeshType &m, ScalarType creaseThr)
	{
		int count = 0;
		Clean<MeshType>::CountNonManifoldVertexFF(m, true, false);

		ForEachFacePos(m, [&](PosType &p){
			if(p.IsBorder() || p.IsEdgeS()/*testCreaseEdge(p, creaseThr)*/)
			{
				p.V()->SetS();
				p.VFlip()->SetS();
				++count;
			}
		});
		return count;
	}

	static int selectVertexFromFold(MeshType &m, Params & params)
	{
		std::vector<char> creaseVerts(m.VN(), 0);
		ForEachFacePos(m, [&] (PosType & p) {
			if (p.IsEdgeS())
			{
				creaseVerts[vcg::tri::Index(m, p.V())] = 1;
				creaseVerts[vcg::tri::Index(m, p.VFlip())] = 1;
			}
		});


		//this aspect ratio check doesn't work on cadish meshes (long thin triangles spanning whole mesh)
		ForEachFace(m, [&] (FaceType & f) {
			if (vcg::Quality(f.cP(0), f.cP(1), f.cP(2)) < params.aspectRatioThr || vcg::DoubleArea(f) < 0.00001)
			{
				if (creaseVerts[vcg::tri::Index(m, f.V(0))] == 0)
					f.V(0)->SetS();
				if (creaseVerts[vcg::tri::Index(m, f.V(1))] == 0)
					f.V(1)->SetS();
				if (creaseVerts[vcg::tri::Index(m, f.V(2))] == 0)
					f.V(2)->SetS();
			}
		});


		ForEachFace(m, [&] (FaceType & f) {
			for (int i = 0; i < 3; ++i)
			{
				if (f.FFp(i) > &f)
				{
					ScalarType angle = fastAngle(NormalizedTriangleNormal(f), NormalizedTriangleNormal(*(f.FFp(i))));
					if (angle <= params.foldAngleCosThr)
					{
						if (creaseVerts[vcg::tri::Index(m, f.V0(i))] == 0)
							f.V0(i)->SetS();
						if (creaseVerts[vcg::tri::Index(m, f.V1(i))] == 0)
							f.V1(i)->SetS();
						if (creaseVerts[vcg::tri::Index(m, f.V2(i))] == 0)
							f.V2(i)->SetS();
						if (creaseVerts[vcg::tri::Index(m, f.FFp(i)->V2(f.FFi(i)))] == 0)
							f.FFp(i)->V2(f.FFi(i))->SetS();
					}
				}
			}
		});

		return 0;
	}




	static void FoldRelax(MeshType &m, Params params, const int step, const bool strict = true)
	{
		typename vcg::tri::Smooth<MeshType>::LaplacianInfo lpz(CoordType(0, 0, 0), 0);
		SimpleTempData<typename MeshType::VertContainer, typename vcg::tri::Smooth<MeshType>::LaplacianInfo> TD(m.vert, lpz);
		const ScalarType maxDist = (strict) ? params.maxSurfDist / 1000. : params.maxSurfDist;
		for (int i = 0; i < step; ++i)
		{
			TD.Init(lpz);
			vcg::tri::Smooth<MeshType>::AccumulateLaplacianInfo(m, TD, false);

			for (auto fi = m.face.begin(); fi != m.face.end(); ++fi)
			{
				std::vector<CoordType> newPos(4);
				bool moving = false;

				for (int j = 0; j < 3; ++j)
				{
					newPos[j] = fi->cP(j);
					if (!fi->V(j)->IsD() && TD[fi->V(j)].cnt > 0)
					{
						if (fi->V(j)->IsS())
						{
							newPos[j] = (fi->V(j)->P() + TD[fi->V(j)].sum) / (TD[fi->V(j)].cnt + 1);
							moving = true;
						}
					}
				}

				if (moving)
				{
//					const CoordType oldN = vcg::NormalizedTriangleNormal(*fi);
//					const CoordType newN = vcg::Normal(newPos[0], newPos[1], newPos[2]).Normalize();

					newPos[3] = (newPos[0] + newPos[1] + newPos[2]) / 3.;
					if (/*(strict || oldN * newN > 0.99) &&*/ (!params.surfDistCheck || testHausdorff(*params.mProject, params.grid, newPos, maxDist)))
					{
						for (int j = 0; j < 3; ++j)
							fi->V(j)->P() = newPos[j];
					}
				}
			}
		}
	}

	static void VertexCoordPlanarLaplacian(MeshType &m, Params & params, int step, ScalarType delta = 0.2)
	{
		typename vcg::tri::Smooth<MeshType>::LaplacianInfo lpz(CoordType(0, 0, 0), 0);
		SimpleTempData<typename MeshType::VertContainer, typename vcg::tri::Smooth<MeshType>::LaplacianInfo> TD(m.vert, lpz);
		for (int i = 0; i < step; ++i)
		{
			TD.Init(lpz);
			vcg::tri::Smooth<MeshType>::AccumulateLaplacianInfo(m, TD, false);
			// First normalize the AccumulateLaplacianInfo
			for (auto vi = m.vert.begin(); vi != m.vert.end(); ++vi)
				if (!(*vi).IsD() && TD[*vi].cnt > 0)
				{
					if ((*vi).IsS())
						TD[*vi].sum = ((*vi).P() + TD[*vi].sum) / (TD[*vi].cnt + 1);
				}

			for (auto fi = m.face.begin(); fi != m.face.end(); ++fi)
			{
				if (!(*fi).IsD())
				{
					for (int j = 0; j < 3; ++j)
					{
						if (Angle(Normal(TD[(*fi).V0(j)].sum, (*fi).P1(j), (*fi).P2(j)),
						          Normal((*fi).P0(j), (*fi).P1(j), (*fi).P2(j))) > M_PI/2.)
							TD[(*fi).V0(j)].sum = (*fi).P0(j);
					}
				}
			}
			for (auto fi = m.face.begin(); fi != m.face.end(); ++fi)
			{
				if (!(*fi).IsD())
				{
					for (int j = 0; j < 3; ++j)
					{
						if (Angle(Normal(TD[(*fi).V0(j)].sum, TD[(*fi).V1(j)].sum, (*fi).P2(j)),
						          Normal((*fi).P0(j), (*fi).P1(j), (*fi).P2(j))) > M_PI/2.)
						{
							TD[(*fi).V0(j)].sum = (*fi).P0(j);
							TD[(*fi).V1(j)].sum = (*fi).P1(j);
						}
					}
				}
			}

			for (auto vi = m.vert.begin(); vi != m.vert.end(); ++vi)
				if (!(*vi).IsD() && TD[*vi].cnt > 0)
				{
					std::vector<CoordType> newPos(1, TD[*vi].sum);
					if ((*vi).IsS() && testHausdorff(*params.mProject, params.grid, newPos, params.maxSurfDist))
						(*vi).P() = (*vi).P() * (1-delta) + TD[*vi].sum * (delta);
				}
		} // end step
	}

	//	static int
	/**
	  * Simple Laplacian Smoothing step
	  * Border and crease vertices are kept fixed.
	  * If there are selected faces and the param.onlySelected is true we compute
	  * the set of internal vertices to the selection and we combine it in and with
	  * the vertexes not on border or creases
	*/
	static void ImproveByLaplacian(MeshType &m, Params params)
	{
		SelectionStack<MeshType> ss(m);

		if(params.selectedOnly) {
			ss.push();
			tri::UpdateSelection<MeshType>::VertexFromFaceStrict(m);
			ss.push();
		}
		tri::UpdateTopology<MeshType>::FaceFace(m);
		tri::UpdateFlags<MeshType>::VertexBorderFromFaceAdj(m);
		tri::UpdateSelection<MeshType>::VertexFromBorderFlag(m);
		selectVertexFromCrease(m, params.creaseAngleCosThr);
		tri::UpdateSelection<MeshType>::VertexInvert(m);
		if(params.selectedOnly) {
			ss.popAnd();
		}

		VertexCoordPlanarLaplacian(m, params, 1);

		tri::UpdateSelection<MeshType>::VertexClear(m);

		selectVertexFromFold(m, params);
		FoldRelax(m, params, 2);

		tri::UpdateSelection<MeshType>::VertexClear(m);

		if(params.selectedOnly) {
			ss.pop();
		}
	}
	/*
		Reprojection step, this method reprojects each vertex on the original surface
		sampling the nearest Point3 onto it using a uniform grid StaticGrid t
	*/
	//TODO: improve crease reprojection:
	//		crease verts should reproject only on creases.
	static void ProjectToSurface(MeshType &m, Params & params)
	{
		for(auto vi=m.vert.begin();vi!=m.vert.end();++vi)
			if(!(*vi).IsD())
			{
				Point3<ScalarType> newP, normP, barP;
				ScalarType maxDist = params.maxSurfDist * 1.5f, minDist = 0.f;
				FaceType* fp = GetClosestFaceBase(*params.mProject, params.grid, vi->cP(), maxDist, minDist, newP, normP, barP);

				if (fp != NULL)
				{
					vi->P() = newP;
				}
			}
	}
};
} // end namespace tri
} // end namespace vcg
#endif