-
Notifications
You must be signed in to change notification settings - Fork 2
/
Param.hxx
1650 lines (1477 loc) · 59.7 KB
/
Param.hxx
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
////////////////////////////////////////////////////////////////////
//
// $Id: Param.hxx 2021/06/13 22:13:22 kanai Exp $
//
// Copyright (c) 2021 Takashi Kanai
// Released under the MIT license
//
////////////////////////////////////////////////////////////////////
#ifndef _PARAM_HXX
#define _PARAM_HXX 1
#include "MyMesh.hxx"
#define EIGEN_NO_DEBUG
#include <Eigen/Sparse>
#include <Eigen/OrderingMethods>
typedef Eigen::Triplet<double> T;
#define RECTANGLE 4
#define SPARSELU 1
#define BICGSTAB 2
int sol = SPARSELU;
#define COTW 1
#define MVW 2
int wei = MVW;
#define SAVE_VERTEX 1
#define SAVE_TEXCOORD 2
int sav = SAVE_VERTEX;
#if 0
// Boundary vertex is fixed or not
class Fixed {
public:
Fixed(): isFixed_(false) {};
~Fixed(){};
bool isFixed() const { return isFixed_; };
void setIsFixed( bool f ) { isFixed_ = f; };
private:
bool isFixed_;
};
OpenMesh::VPropHandleT<Fixed> ffs;
#endif
#define FIXED_BOUNDARY 1
#define NATURAL_BOUNDARY 2
int bou = FIXED_BOUNDARY;
// 0: vertex i, 1-N: its neighbor vertices j
class VVWeights {
public:
VVWeights(){};
~VVWeights(){ w_.clear(); };
double w( int i ) { return w_[i]; };
int size() const { return w_.size(); };
std::vector<double>& ws() { return w_; };
void addWeight( double d ) { w_.push_back(d); };
// for orbifold
// void addWeight( double d, int pid ) {
// w_.push_back(d);
// param_id_.push_back( pid );
// };
void addParamID( MyMesh::VertexHandle& vh, int id ) {
param_id_.insert( make_pair( vh, id ) );
};
int param_id( MyMesh::VertexHandle& vh ) { return param_id_[vh]; };
bool setWeight( int i, double d ) {
if ( i >= (int) w_.size() ) return false;
w_[i] = d;
return true;
};
void Print() {
cout << "vvw" << endl;
cout << "weight size " << size() << endl;
for ( int i = 0; i < w_.size(); ++i )
{
cout << w_[i] << endl;
}
cout << "param_id " << endl;
for ( std::map<MyMesh::VertexHandle,int>::iterator pi = param_id_.begin();
pi != param_id_.end(); ++pi )
{
cout << "vt " << pi->first.idx() << " id " << pi->second << endl;
}
};
private:
std::vector<double> w_;
// parameter id for orbifold
// for OF_BOUNDARY vertex, values are 1 (true), or -1 (false)
std::map<MyMesh::VertexHandle,int> param_id_;
};
//////////////////////////////////////////////////////////////////////////////
#include "Orbifold.hxx"
class Param {
public:
double cotw( MyMesh::Point& p0, MyMesh::Point& p1, MyMesh::Point& p2 ) {
OMVector3d c1( p1 - p0 );
OMVector3d c2( p2 - p0 );
//double sin = OpenMesh::cross( c1, c2 ).length();
double sin = c1.cross( c2 ).norm();
//double angle = std::fabs( std::atan2(sin, OpenMesh::dot(c1, c2)) );
double angle = std::fabs( std::atan2(sin, c1.dot(c2)) );
// std::cout << "\tcot " << 1.0 / std::tan( angle ) << " tan " << std::tan( M_PI_2 - angle ) << std::endl;
// if ( 1.0 / std::tan( angle ) - std::tan( M_PI_2 - angle ) > 1.0e-05 )
// std::cout << "diff!" << std::endl;
return std::tan( M_PI_2 - angle );
};
double tan2w( MyMesh::Point& p0, MyMesh::Point& p1, MyMesh::Point& p2 ) {
OMVector3d c1( p1 - p0 );
OMVector3d c2( p2 - p0 );
//double sin = OpenMesh::cross( c1, c2 ).length();
double sin = c1.cross( c2 ).norm();
//double angle = std::fabs( std::atan2(sin, OpenMesh::dot(c1, c2)) );
double angle = std::fabs( std::atan2(sin, c1.dot(c2)) );
// std::cout << "\tcot " << 1.0 / std::tan( angle ) << " tan " << std::tan( M_PI_2 - angle ) << std::endl;
// if ( 1.0 / std::tan( angle ) - std::tan( M_PI_2 - angle ) > 1.0e-05 )
// std::cout << "diff!" << std::endl;
return std::tan( angle / 2.0 );
};
//
// A weight has to be doubled for natural boundary as described in [Karni 05]
//
double computeCotangentWeight( MyMesh& mesh, MyMesh::VertexIHalfedgeIter& vih_it ) {
OpenMesh::SmartHalfedgeHandle a( *vih_it );
return computeCotangentWeight( mesh, a );
// return computeCotangentWeight( mesh, (OpenMesh::SmartHalfedgeHandle&) *vih_it );
};
double computeCotangentWeight( MyMesh& mesh, OpenMesh::SmartHalfedgeHandle& iheh ) {
// right face
//auto iheh( *vih_it ); // vih_it.handle();
//MyMesh::HalfedgeHandle iheh( *vih_it ); // vih_it.handle();
// if ( mesh.from_vertex_handle( iheh ) == vh ) std::cout << "i from" << std::endl;
// else if ( mesh.to_vertex_handle( iheh ) == vh ) std::cout << "i to" << std::endl;
//auto next_iheh = iheh.next();
//MyMesh::HalfedgeHandle next_iheh = mesh.next_halfedge_handle( *vih_it );
// if ( mesh.from_vertex_handle( next_iheh ) == vh ) std::cout << "next i from" << std::endl;
// else if ( mesh.to_vertex_handle( next_iheh ) == vh ) std::cout << "next i to" << std::endl
// MyMesh::Point p0, p1, p2;
auto p0 = mesh.point( iheh.next().to() );
auto p1 = mesh.point( iheh.from() );
auto p2 = mesh.point( iheh.to() );
// auto p0 = mesh.point( mesh.to_vertex_handle( next_iheh ) );
// auto p1 = mesh.point( mesh.from_vertex_handle( iheh ) );
// auto p2 = mesh.point( mesh.to_vertex_handle( iheh ) );
double al = cotw( p0, p1, p2 );
// left face
// MyMesh::HalfedgeHandle mheh = mesh.opposite_halfedge_handle( iheh );
// if ( mesh.from_vertex_handle( mheh ) == vh ) std::cout << "m from" << std::endl;
// else if ( mesh.to_vertex_handle( mheh ) == vh ) std::cout << "m to" << std::endl;
// MyMesh::HalfedgeHandle next_mheh = mesh.next_halfedge_handle( mheh );
auto mheh = iheh.opp();
auto p3 = mesh.point( mheh.next().to() );
auto p4 = mesh.point( mheh.from() );
auto p5 = mesh.point( mheh.to() );
// p0 = mesh.point( mesh.to_vertex_handle( next_mheh ) );
// p1 = mesh.point( mesh.from_vertex_handle( mheh ) );
// p2 = mesh.point( mesh.to_vertex_handle( mheh ) );
double be = cotw( p3, p4, p5 );
//return (al + be) / 2.0;
return (al + be);
};
double computeMeanValueWeight( MyMesh& mesh, MyMesh::VertexIHalfedgeIter& vih_it ) {
OpenMesh::SmartHalfedgeHandle a( *vih_it );
return computeMeanValueWeight( mesh, a );
//return computeMeanValueWeight( mesh, (OpenMesh::SmartHalfedgeHandle&) *vih_it );
};
double computeMeanValueWeight( MyMesh& mesh, OpenMesh::SmartHalfedgeHandle& iheh ) {
// gamma
//auto iheh( *vih_it ); // vih_it.handle();
// auto next_iheh = iheh.next();
//MyMesh::HalfedgeHandle iheh( *vih_it ); // vih_it.handle();
//MyMesh::HalfedgeHandle next_iheh = mesh.next_halfedge_handle( iheh );
//MyMesh::Point p0, p1, p2;
auto p0 = mesh.point( iheh.next().from() );
auto p1 = mesh.point( iheh.next().to() );
auto p2 = mesh.point( iheh.from() );
// auto p0 = mesh.point( mesh.from_vertex_handle( next_iheh ) );
// auto p1 = mesh.point( mesh.to_vertex_handle( next_iheh ) );
// auto p2 = mesh.point( mesh.from_vertex_handle( iheh ) );
double ga = tan2w( p0, p1, p2 );
#if 0
std::cout << "\t gamma p0 " << mesh.from_vertex_handle( next_iheh ).idx() << " "
<< " p1 " << mesh.to_vertex_handle( next_iheh ).idx() << " "
<< " p2 " << mesh.from_vertex_handle( iheh ).idx() << std::endl;
#endif
// delta
auto mheh = iheh.opp();
// MyMesh::HalfedgeHandle mheh = mesh.opposite_halfedge_handle( iheh );
// MyMesh::HalfedgeHandle next_mheh = mesh.next_halfedge_handle( mheh );
auto p3 = mesh.point( mheh.from() ); // i
auto p4 = mesh.point( mheh.next().from() ); //j
auto p5 = mesh.point( mheh.next().to() );
// p0 = mesh.point( mesh.from_vertex_handle( mheh ) ); // i
// p1 = mesh.point( mesh.from_vertex_handle( next_mheh ) ); //j
// p2 = mesh.point( mesh.to_vertex_handle( next_mheh ) );
double de = tan2w( p3, p4, p5 );
#if 0
std::cout << "\t delta p0 " << mesh.from_vertex_handle( mheh ).idx() << " "
<< " p1 " << mesh.from_vertex_handle( next_mheh ).idx() << " "
<< " p2 " << mesh.to_vertex_handle( next_mheh ).idx() << std::endl;
#endif
// return (ga + de) / (p0 - p1).length();
return (ga + de) / (p3 - p4).norm();
};
void constructBoundary( MyMesh& mesh, std::vector<MyMesh::VertexHandle>& bverts ) {
// find a boundary vertex
MyMesh::VertexHandle vh0;
for ( auto vh : mesh.vertices() )
{
if ( mesh.is_boundary( vh ) )
{
vh0 = vh; // v_it.handle();
//std::cout << "boundary vertex " << vh0.idx() << std::endl;
break;
}
}
#if 0
MyMesh::VertexIter v_it, v_end(mesh.vertices_end());
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
{
MyMesh::VertexHandle vh = *v_it;
if ( mesh.is_boundary( vh ) )
{
vh0 = vh; // v_it.handle();
//std::cout << "boundary vertex " << vh0.idx() << std::endl;
break;
}
}
#endif
std::vector<MyMesh::VertexHandle> bv_tmp;
// construct boundary vertices
MyMesh::VertexHandle vh = vh0, prev_vh;
do {
bv_tmp.push_back( vh );
MyMesh::VertexVertexIter vv_it;
for ( vv_it = mesh.vv_iter( vh ); vv_it.is_valid(); ++vv_it )
{
MyMesh::VertexHandle vc = *vv_it; // vv_it.handle();
if ( mesh.is_boundary( vc ) && (prev_vh != vc) )
{
//std::cout << "\tfound! " << vc.idx() << std::endl;
prev_vh = vh;
vh = vc;
break;
}
}
} while ( vh != vh0 );
std::cout << "#boundary vertices: " << bv_tmp.size() << std::endl;
#if 1
// reverse direction
bverts.push_back( bv_tmp[0] );
for ( int i = bv_tmp.size()-1; i > 0; --i )
bverts.push_back( bv_tmp[i] );
#endif
#if 0
for ( int i = 0; i < bv_tmp.size(); ++i )
bverts.push_back( bv_tmp[i] );
#endif
};
void computeBoundaryMapping( MyMesh& mesh,
std::vector<double>& paramx, std::vector<double>& paramy ) {
// property handle to store a flag
mesh.add_property(fffs);
// construct boundary vertices
std::vector<MyMesh::VertexHandle> bverts;
constructBoundary( mesh, bverts );
// corner coordinates
std::vector<double> cornerx(RECTANGLE);
std::vector<double> cornery(RECTANGLE);
std::vector<MyMesh::VertexHandle> cornerv;
// fix two vertices of the coordinates (0,0) or (1,1)
cornerx[0] = 0.0; cornerx[1] = 1.0; cornerx[2] = 1.0; cornerx[3] = 0.0;
cornery[0] = 0.0; cornery[1] = 0.0; cornery[2] = 1.0; cornery[3] = 1.0;
// compute boundary length
double blength = 0.0;
int bvn = bverts.size();
for ( int i = 0; i < bvn-1; ++i )
{
// blength += (mesh.point( bverts[i] ) - mesh.point( bverts[i+1] )).length();
blength += (mesh.point( bverts[i] ) - mesh.point( bverts[i+1] )).norm();
}
// blength += (mesh.point( bverts[bvn-1] ) - mesh.point( bverts[0] )).length();
blength += (mesh.point( bverts[bvn-1] ) - mesh.point( bverts[0] )).norm();
std::cout << "boundary length " << blength << std::endl;
// determine corner vertices
double l4 = blength / (double) RECTANGLE;
double tl = 0.0;
cornerv.push_back( bverts[0] );
paramx[ bverts[0].idx() ] = cornerx[0];
paramy[ bverts[0].idx() ] = cornery[0];
// (0,0)
Fixed& ff = mesh.property( fffs, bverts[0] );
ff.setIsFixed( true );
int count = 1;
for ( int i = 0; i < bvn-1; ++i )
{
// tl += (mesh.point( bverts[i] ) - mesh.point( bverts[i+1] )).length();
tl += (mesh.point( bverts[i] ) - mesh.point( bverts[i+1] )).norm();
if ( (tl > l4) && (count < RECTANGLE) )
{
cornerv.push_back( bverts[i] );
paramx[ bverts[i].idx() ] = cornerx[count];
paramy[ bverts[i].idx() ] = cornery[count];
if ( count == 2 ) // (1,1)
{
Fixed& ff = mesh.property( fffs, bverts[i] );
ff.setIsFixed( true );
}
tl = 0.0;
++count;
}
}
std::cout << "corner vertices: ";
for ( int i = 0; i < 4; ++i ) std::cout << cornerv[i].idx() << " ";
std::cout << std::endl;
// compute inner boundary coordinates
std::vector<MyMesh::VertexHandle> innerv;
count = 1;
for ( int i = 0; i < bvn; ++i )
{
innerv.push_back( bverts[i] );
if ( cornerv[count] == bverts[i] )
{
// compute the length of poly line
double pll = 0.0;
for ( int j = 0; j < (int) innerv.size()-1; ++j )
{
// pll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).length();
pll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).norm();
}
// compute the ratio of length and compute parameter
double cll = 0.0;
// std::cout << "count " << count << std::endl;
for ( int j = 0; j < (int) innerv.size()-1; ++j )
{
double t = cll / pll;
double px = (1 - t) * cornerx[count-1] + t * cornerx[count];
double py = (1 - t) * cornery[count-1] + t * cornery[count];
paramx[ innerv[j].idx() ] = px;
paramy[ innerv[j].idx() ] = py;
// std::cout << "\t t " << t << " param " << px << " " << py << std::endl;
// cll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).length();
cll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).norm();
}
// next poly line
++count;
innerv.clear();
innerv.push_back( bverts[i] );
}
}
//
// last poly line
//
innerv.push_back( bverts[0] );
double pll = 0.0;
for ( int j = 0; j < (int) innerv.size()-1; ++j )
{
// pll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).length();
pll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).norm();
}
// compute the ratio of length and compute parameter
double cll = 0.0;
// std::cout << "count " << count << std::endl;
for ( int j = 0; j < (int) innerv.size()-1; ++j )
{
double t = cll / pll;
double px = (1 - t) * cornerx[RECTANGLE-1] + t * cornerx[0];
double py = (1 - t) * cornery[RECTANGLE-1] + t * cornery[0];
paramx[ innerv[j].idx() ] = px;
paramy[ innerv[j].idx() ] = py;
// std::cout << "\t t " << t << " param " << px << " " << py << std::endl;
// cll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).length();
cll += (mesh.point( innerv[j] ) - mesh.point( bverts[j+1] )).norm();
}
};
////////////////////////////////////////////////////////////////////////////////////////
void applyParam_FixedBoundary( MyMesh& mesh,
std::vector<double>& paramx, std::vector<double>& paramy,
int sol_, int wei_ ) {
int n_vt = mesh.n_vertices();
//
// boundary mapping
// compute 2D parameters for boundary
//
computeBoundaryMapping( mesh, paramx, paramy );
// property handle to store weights
OpenMesh::VPropHandleT<VVWeights> vvws;
mesh.add_property(vvws);
//
// compute weights
//
// MyMesh::VertexIter v_it, v_end(mesh.vertices_end());
// for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
for ( auto vh : mesh.vertices() )
{
// for check
//std::cout << i << " " << v_it.handle().idx() << std::endl;
//if ( i != v_it.handle().idx() ) std::cout << "ng" << std::endl;
VVWeights& vvw = mesh.property(vvws, vh);
//VVWeights& vvw = mesh.property(vvws,*v_it);
double wd = 0.0;
vvw.addWeight( wd );
//MyMesh::VertexHandle vh = v_it.handle();
//std::cout << "v " << i << std::endl;
#if 0
MyMesh::VertexHandle vh = *v_it;
std::cout << "i = " << vh.idx() << std::endl;
#endif
//OpenMesh::SmartVertexHandle vh(*v_it);
for ( auto vih : vh.incoming_halfedges() )
{
double wdc;
if ( wei_ == COTW )
wdc = computeCotangentWeight( mesh, vih );
else if ( wei_ == MVW )
wdc = computeMeanValueWeight( mesh, vih );
vvw.addWeight( wdc );
wd -= wdc;
}
#if 0
MyMesh::VertexIHalfedgeIter vih_it;
for (vih_it=mesh.vih_iter( *v_it ); vih_it.is_valid(); ++vih_it)
{
double wdc;
if ( wei_ == COTW )
wdc = computeCotangentWeight( mesh, vih_it );
else if ( wei_ == MVW )
wdc = computeMeanValueWeight( mesh, vih_it );
vvw.addWeight( wdc );
wd -= wdc;
}
#endif
vvw.setWeight( 0, wd );
#if 0
std::vector<double>& ws = vvw.ws();
for ( int j = 0; j < ws.size(); ++j )
std::cout << "\t " << ws[j] << std::endl;
std::cout << std::endl;
#endif
}
//
// compute inner parameters
//
// build up sparse matrix A
std::cout << "Setup sparse matrix ..." << std::endl;
std::vector<Eigen::Triplet<double> > tripletList;
// MyMesh::VertexIter v_it, v_end(mesh.vertices_end());
// for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
for ( auto vh : mesh.vertices() )
{
//MyMesh::VertexHandle vh = *v_it;
int i = vh.idx();
// if ( !(mesh.is_boundary(vh)) )
if ( !(vh.is_boundary()) )
{
VVWeights& vvw = mesh.property(vvws, vh);
tripletList.push_back( Eigen::Triplet<double>(i, i, vvw.w(0)) );
int k=1;
// MyMesh::VertexVertexIter vv_it;
// for (k=1, vv_it=mesh.vv_iter( vh ); vv_it.is_valid(); ++vv_it, ++k)
for ( auto vvh : vh.vertices() )
{
// MyMesh::VertexHandle vvh = *vv_it;
int j = vvh.idx();
tripletList.push_back( Eigen::Triplet<double>(i, j, vvw.w(k)) );
++k;
}
}
else
tripletList.push_back( Eigen::Triplet<double>(i, i, 1.0) );
}
Eigen::SparseMatrix<double> spmat( n_vt, n_vt );
spmat.setFromTriplets( tripletList.begin(), tripletList.end() );
spmat.makeCompressed();
// setup vector b
Eigen::VectorXd xx(n_vt), xy(n_vt), bx(n_vt), by(n_vt);
for ( int i = 0; i < n_vt; ++i )
{
bx[i] = paramx[i];
by[i] = paramy[i];
}
// to symmetric positive-definite matrix
// toSpd( spmat, bverts, bx, by );
// solve x
if ( sol_ == BICGSTAB )
{
Eigen::BiCGSTAB<Eigen::SparseMatrix<double> > solver(spmat);
std::cout << "solve xcoords ..." << std::endl;
xx = solver.solve(bx);
std::cout << "solve ycoords ..." << std::endl;
xy = solver.solve(by);
}
else if ( sol_ == SPARSELU )
{
Eigen::SparseLU<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int> > solver;
solver.analyzePattern(spmat);
// Compute the numerical factorization
solver.factorize(spmat);
//Use the factors to solve the linear system
std::cout << "solve xcoords ..." << std::endl;
xx = solver.solve(bx);
std::cout << "solve ycoords ..." << std::endl;
xy = solver.solve(by);
}
for ( int i = 0; i < n_vt; ++i )
{
paramx[i] = xx[i];
paramy[i] = xy[i];
}
#if 0
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
{
MyMesh::VertexHandle vh = *v_it;
int i = vh.idx();
Fixed& ff = mesh.property( fffs, vh );
if ( ff.isFixed() )
{
std::cout << "i " << i << " param " << paramx[i] << " " << paramy[i] << std::endl;
}
}
#endif
mesh.remove_property(vvws);
std::cout << "done." << std::endl;
};
void applyParam_NaturalBoundary( MyMesh& mesh,
std::vector<double>& paramx, std::vector<double>& paramy,
int sol_, int wei_ ) {
const int n_vt = mesh.n_vertices();
#if 0
//
// boundary mapping
//
// compute 2D parameters for boundary
computeBoundaryMapping( mesh, paramx, paramy );
#endif
#if 1
//for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
for ( auto vh : mesh.vertices() )
{
//MyMesh::VertexHandle vh = *v_it;
//if ( !(mesh.is_boundary(vh)) )
Fixed& ff = mesh.property( fffs, vh );
if ( !(ff.isFixed()) ) // not fixed
{
paramx[vh.idx()] = paramy[vh.idx()] = 0.0;
}
}
#endif
// property handle to store weights
OpenMesh::VPropHandleT<VVWeights> vvws;
mesh.add_property(vvws);
//
// compute weights
//
//MyMesh::VertexIter v_it, v_end(mesh.vertices_end());
// for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
for ( auto vh : mesh.vertices() )
{
// for check
//std::cout << i << " " << v_it.handle().idx() << std::endl;
//if ( i != v_it.handle().idx() ) std::cout << "ng" << std::endl;
VVWeights& vvw = mesh.property(vvws, vh);
double wd = 0.0;
vvw.addWeight( wd );
//MyMesh::VertexHandle vh = v_it.handle();
//std::cout << "v " << i << std::endl;
// OpenMesh::SmartVertexHandle vh(*v_it);
for ( auto vih : vh.incoming_halfedges() )
{
double wdc;
if ( wei_ == COTW )
wdc = computeCotangentWeight( mesh, vih );
else if ( wei_ == MVW )
wdc = computeMeanValueWeight( mesh, vih );
vvw.addWeight( wdc );
wd -= wdc;
}
#if 0
MyMesh::VertexIHalfedgeIter vih_it;
for (vih_it=mesh.vih_iter( *v_it ); vih_it.is_valid(); ++vih_it)
{
double wdc;
if ( wei_ == COTW )
wdc = computeCotangentWeight( mesh, vih_it );
else if ( wei_ == MVW )
wdc = computeMeanValueWeight( mesh, vih_it );
vvw.addWeight( wdc );
wd -= wdc;
}
#endif
vvw.setWeight( 0, wd );
#if 0
std::vector<double>& ws = vvw.ws();
for ( int j = 0; j < ws.size(); ++j )
std::cout << "\t " << ws[j] << std::endl;
std::cout << std::endl;
#endif
}
//
// compute inner parameters
//
// build up sparse matrix A
std::cout << "Setup sparse matrix ..." << std::endl;
std::vector<Eigen::Triplet<double> > tripletList;
for ( auto vh : mesh.vertices() )
// MyMesh::VertexIter v_it, v_end(mesh.vertices_end());
// for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
{
//MyMesh::VertexHandle vh = *v_it;
int i = vh.idx();
Fixed& ff = mesh.property( fffs, vh );
if ( !(ff.isFixed()) ) // not fixed
{
VVWeights& vvw = mesh.property(vvws, vh);
// set for x coord
tripletList.push_back( Eigen::Triplet<double>(i, i, vvw.w(0)) );
// set for y coord
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i+n_vt, vvw.w(0)) );
int k=1;
for ( auto vvh : vh.vertices() )
// MyMesh::VertexVertexIter vv_it;
// for (k=1, vv_it=mesh.vv_iter( *v_it ); vv_it.is_valid(); ++vv_it, ++k)
{
// MyMesh::VertexHandle vvh = *vv_it;
int j = vvh.idx();
// set for x coord
tripletList.push_back( Eigen::Triplet<double>(i, j, vvw.w(k)) );
// set for y coord
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, j+n_vt, vvw.w(k)) );
++k;
}
// boundary vertices
// if ( mesh.is_boundary(*v_it) )
if ( vh.is_boundary() )
{
#if 0
std::cout << "id " << i << std::endl;
for ( int j = 0; j < vvw.size(); ++j )
{
std::cout << "\t" << vvw.w(j) << std::endl;
}
#endif
// get neighbor boundary vertices
MyMesh::VertexVertexIter vv_it;
vv_it=mesh.vv_iter( vh );
MyMesh::VertexHandle sbvh = *vv_it, ebvh;
while ( vv_it.is_valid() )
{
ebvh = *vv_it;
++vv_it;
}
int i1 = sbvh.idx();
int i2 = ebvh.idx();
if ( wei == COTW )
{
// std::cout << "i1 " << i1 << " " << mesh.is_boundary( sbvh ) << std::endl;
// std::cout << "i2 " << i2 << " " << mesh.is_boundary( ebvh ) << std::endl;
// for x coords
tripletList.push_back( Eigen::Triplet<double>(i, i2+n_vt, 1.0) );
tripletList.push_back( Eigen::Triplet<double>(i, i1+n_vt, -1.0) );
// tripletList.push_back( Eigen::Triplet<double>(i, i2+n_vt, -1.0) );
// tripletList.push_back( Eigen::Triplet<double>(i, i1+n_vt, 1.0) );
// for y coords
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i1, 1.0) );
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i2, -1.0) );
// tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i1, -1.0) );
// tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i2, 1.0) );
}
else if ( wei == MVW )
{
MyMesh::Point p0 = mesh.point( vh );
MyMesh::Point p1 = mesh.point( sbvh );
MyMesh::Point p2 = mesh.point( ebvh );
// double ir1 = 1.0/((p1 - p0).length());
double ir1 = 1.0/((p1 - p0).norm());
// double ir2 = 1.0/((p2 - p0).length());
double ir2 = 1.0/((p2 - p0).norm());
// std::cout << ir1 << " " << ir2 << std::endl;
// for x coords
tripletList.push_back( Eigen::Triplet<double>(i, i2+n_vt, ir2) );
tripletList.push_back( Eigen::Triplet<double>(i, i1+n_vt, -ir1) );
tripletList.push_back( Eigen::Triplet<double>(i, i+n_vt, ir1 - ir2) );
// for y coords
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i1, ir1) );
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i2, -ir2) );
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i, ir2 - ir1) );
}
}
}
else // fixed
{
// std::cout << "i = " << i << " param " << paramx[i] << " " << paramy[i] << std::endl;
tripletList.push_back( Eigen::Triplet<double>(i, i, 1.0) );
tripletList.push_back( Eigen::Triplet<double>(i+n_vt, i+n_vt, 1.0) );
}
}
Eigen::SparseMatrix<double> spmat( 2*n_vt, 2*n_vt );
spmat.setFromTriplets( tripletList.begin(), tripletList.end() );
spmat.makeCompressed();
// setup vector b
//Eigen::VectorXd xx(n_vt), xy(n_vt), bx(n_vt), by(n_vt);
Eigen::VectorXd xx(2*n_vt), bx(2*n_vt);
for ( int i = 0; i < n_vt; ++i )
{
// set for x coord
bx[i] = paramx[i];
// set for y coord
bx[i+n_vt] = paramy[i];
}
// solve x
if ( sol_ == BICGSTAB )
{
Eigen::BiCGSTAB<Eigen::SparseMatrix<double> > solver(spmat);
std::cout << "solve x and y coords ..." << std::endl;
xx = solver.solve(bx);
// std::cout << "solve ycoords ..." << std::endl;
// xy = solver.solve(by);
}
else if ( sol_ == SPARSELU )
{
Eigen::SparseLU<Eigen::SparseMatrix<double>, Eigen::COLAMDOrdering<int> > solver;
solver.analyzePattern(spmat);
// Compute the numerical factorization
solver.factorize(spmat);
//Use the factors to solve the linear system
std::cout << "solve x and y coords ..." << std::endl;
xx = solver.solve(bx);
// std::cout << "solve ycoords ..." << std::endl;
// xy = solver.solve(by);
}
for ( int i = 0; i < n_vt; ++i )
{
paramx[i] = xx[i];
paramy[i] = xx[i+n_vt];
}
#if 0
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
{
MyMesh::VertexHandle vh = *v_it;
int i = vh.idx();
Fixed& ff = mesh.property( fffs, vh );
if ( ff.isFixed() )
{
std::cout << "i " << i << " param " << paramx[i] << " " << paramy[i] << std::endl;
}
}
#endif
mesh.remove_property(vvws);
std::cout << "done." << std::endl;
};
MyMesh::VertexIHalfedgeCWIter start_vih_cwiter( MyMesh::VertexHandle& vh,
MyMesh::VertexHandle& pvh,
MyMesh& mesh ) {
int c = 0;
MyMesh::VertexIHalfedgeCWIter vih_cwit = mesh.vih_cwiter( vh );
while (1)
{
if ( mesh.from_vertex_handle( *vih_cwit ) == pvh )
return vih_cwit;
++vih_cwit; ++c;
}
};
MyMesh::VertexOHalfedgeCWIter start_voh_cwiter( MyMesh::VertexHandle& vh,
MyMesh::VertexHandle& pvh,
MyMesh& mesh ) {
int c = 0;
MyMesh::VertexOHalfedgeCWIter voh_cwit = mesh.voh_cwiter( vh );
while (1)
{
if ( mesh.to_vertex_handle( *voh_cwit ) == pvh )
return voh_cwit;
++voh_cwit; ++c;
}
};
MyMesh::VertexOHalfedgeCCWIter start_voh_ccwiter( MyMesh::VertexHandle& vh,
MyMesh::VertexHandle& pvh,
MyMesh& mesh ) {
int c = 0;
MyMesh::VertexOHalfedgeCCWIter voh_ccwit = mesh.voh_ccwiter( vh );
while (1)
{
if ( mesh.to_vertex_handle( *voh_ccwit ) == pvh )
return voh_ccwit;
++voh_ccwit; ++c;
}
};
MyMesh::VertexVertexIter start_vv_iter( MyMesh::VertexVertexIter vv_it,
MyMesh::VertexHandle& pvt ) {
int c = 0;
while (1)
{
if ( *vv_it == pvt )
{
// cout << "count " << c << endl;
return vv_it;
}
++vv_it; ++c;
}
};
//
// set param_id to vvw of 1-ring neighbor vertices for boundary vertices
//
void setParamID_I( MyMesh::VertexHandle& vh,
VVWeights& vvw,
std::map<MyMesh::VertexHandle,int>& alt_param_id,
MyMesh& mesh,
std::vector<unsigned int>& vertex_type,
bool reverse ) {
// if vh is OF_INTERNAL, apply add param_id to only 1-ring neighbor points
// with OF_BOUNDARY or OF_CONESINGULARITY_PI2.
// cout << "internal vt " << vh.idx() << " 1-ring neighbor start" << endl;
for ( MyMesh::VertexVertexIter vv_it = mesh.vv_iter( vh ); vv_it.is_valid(); ++vv_it )
{
MyMesh::VertexHandle vvh = *vv_it;
int id = vvh.idx();
int rev_id = alt_param_id[vvh];
// cout << "\tvertex " << id << endl;
if ( (vertex_type[ id ] == OF_BOUNDARY) ||
(vertex_type[ id ] == OF_CONESINGULARITY_PI2) )
{
// if a 1-ring neighbor vertex is OF_BOUNDARY or OF_CONESINGULARITY_PI2,
// set param_id to its original id or alt_param_id
// TODO: 11/29 ここのところもう一度見直す
if ( reverse == false )
{
// 11/29 下と交換
// vvw.addParamID( vvh, id );
vvw.addParamID( vvh, rev_id );
// cout << "\tvt " << id << " type " << vertex_type[id] << " reverse " << reverse << " param " << rev_id << endl;
}
else
{
// 11/29 上と交換
// vvw.addParamID( vvh, rev_id );
vvw.addParamID( vvh, id );
// cout << "\tvt " << id << " type " << vertex_type[id] << " reverse " << reverse << " param " << id << endl;
}
}
}
// cout << "internal 1-ring neighbor end" << endl;
};
void setParamID_B( MyMesh::VertexHandle& vh,
MyMesh::VertexHandle& pvh,
MyMesh::VertexHandle& nvh,
VVWeights& vvw,
std::map<MyMesh::VertexHandle,int>& alt_param_id,
MyMesh& mesh,
std::vector<unsigned int>& vertex_type,
bool reverse ) {
// if vh is OF_BOUNDARY or OF_CONESINGULARITY_PI2, apply add param_id
/// to all 1-ring neighbor points.
// TODO: reverse と rev の関係について見直す
// cout << "boundary 1-ring neighbor start" << endl;
bool rev = reverse;
MyMesh::VertexVertexIter vv_it = start_vv_iter( mesh.vv_iter( vh ), pvh );
MyMesh::VertexHandle svh = *vv_it;
// int i;
// for ( i = 0, vv_it = mesh.vv_iter( vh ); vv_it.is_valid(); ++vv_it, ++i ) {
do {
MyMesh::VertexHandle vvh = *vv_it;
int id = vvh.idx();
// cout << "vertex " << id << endl;
// set param_id to 1.0 (reverse=true) or -1.0 (reverse=false)
if ( rev == false )
{
// 11/30 下と交換
// vvw.addParamID( vvh, -1 );
vvw.addParamID( vvh, 1 );
// cout << "\tvt " << id << " type " << vertex_type[id] << " reverse " << rev << " param " << 1.0 << endl;
}
else // rev == true
{
// 11/30 上と交換
// vvw.addParamID( vvh, 1 );
vvw.addParamID( vvh, -1 );
// cout << "\tvt " << id << " type " << vertex_type[id] << " reverse " << rev << " param " << -1.0 << endl;
}
++vv_it;
if ( !(vv_it.is_valid()) ) vv_it = mesh.vv_iter( vh ); // reset vv_it to start
if ( *vv_it == nvh )
{
rev = ( rev == true ) ? false : true; // parameter is changed from nvt
}
} while ( *vv_it != svh );
// cout << "boundary 1-ring neighbor end" << endl;
};
int applyParam_Orbifold( MyMesh& mesh,
Orbifold& orbi,
// std::vector<MyMesh::VertexHandle>& cs_vertices,
std::vector<double>& paramx, std::vector<double>& paramy,
int sol_, int wei_ ) {
cout << "compute orbifold parameterization ... " << endl;
int n_vt = mesh.n_vertices();
// set orbifold boundary
orbi.calcBoundaries();
// cone singularity vertices
std::vector<MyMesh::VertexHandle>& cs_vertices = orbi.cs_vertices();
// parameters setup
std::vector<std::vector<MyMesh::VertexHandle> >& path = orbi.path();