-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiberMesh.cpp
More file actions
1362 lines (975 loc) · 33.5 KB
/
FiberMesh.cpp
File metadata and controls
1362 lines (975 loc) · 33.5 KB
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
/*************************************************************
* Trevor O'Brien, trevor@cs.brown.edu
*
* March 20, 2009
* FiberMesh.cpp
*
************************************************************/
#include "FiberMesh.h"
FiberMesh::FiberMesh(ControlCurve curve, Vector2 screenSize, Camera *cam, Vector3 color)
{
m_boundary = curve;
m_3Dmesh = NULL;
m_screenSize = screenSize;
m_camera = cam;
// We haven't initialized any of our linear solvers to start.
m_cSolveInit = false;
m_eSolveInit = false;
m_vSolveInit = false;
m_dSolveInit = false;
m_handleRow = 0;
m_At = SparseMatrix(0,0);
m_Et = SparseMatrix(0,0);
m_Vt = SparseMatrix(0,0);
m_Dt = SparseMatrix(0,0);
m_color = color;
// Project the points we've got into 3D and use our triangulation
// to generate a pretty mesh we can look at.
Vector2List all2Dverts = m_boundary.GetBoundsAndDups();
ProjectVertsToWorldSpace(all2Dverts);
std::vector<MeshTriangle> triangles = m_boundary.GetDuplicateTriangulation();
m_meshCreated = Create3DMesh(triangles);
if (!m_meshCreated) {
cerr << "Uh, oh. Something went wrong creating 3D mesh at Frontend::mouseReleaseEvent"
<< endl;
}
// Ok. Here we go. Let's do the fancy stuff to optimize
// our nice surface.
if (!OptimizeSurface())
cerr << "Problem optimizing surface. Frontend::mouseReleaseEvent()" << endl;
m_maxMag = 0.0f;
ComputeCentroid();
ComputeCentroidPoint();
m_contractIters = 0;
m_contractSL = 6.0f;
m_avgFaceArea = 0;
}
FiberMesh::FiberMesh(Mesh *mesh, Vector3 color)
{
m_screenSize = Vector2::zero();
m_camera = NULL;
m_color = color;
m_3Dmesh = mesh;
ComputeCentroid();
ComputeCentroidPoint();
// cout << "The center of this mesh is: " << GetCentroid() << endl;
MoveMesh(Vector3::zero());
ComputeCentroid();
ComputeCentroidPoint();
// cout << "The new center of this mesh is: " << GetCentroid() << endl;
// if (NormalizeMesh())
// cout << "Mesh normalized" << endl;
// else
// cerr << "Problem normalizing mesh." << endl;
m_contractIters = 0;
m_contractSL = 2.0f;
m_avgFaceArea = 0;
}
void FiberMesh::ClearMeshData()
{
m_3DmeshData.vertices.clear();
m_3DmeshData.normals.clear();
m_3DmeshData.uvs.clear();
m_3DmeshData.triangles.clear();
m_3DmeshData.fileName = "";
}
void FiberMesh::InitiallyInflate()
{
if (m_3Dmesh == NULL) {
cerr << "Can't inflate a mesh that doesn't exist! Frontend::InitiallyInflate()"
<< endl;
return;
}
unsigned numNorms;
m_3Dmesh->computeNormals();
Normal *norms = m_3Dmesh->getNormals(numNorms);
unsigned numVerts;
Vertex *verts = m_3Dmesh->getVertices(numVerts);
if (norms == NULL)
cerr << "Umm, no norms here." << endl;
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
std::vector<float> edgeLengths = GetAverageEdgeLengths();
float avgLen = 0.0f;
for (unsigned int i = 0; i < edgeLengths.size(); i++)
avgLen += edgeLengths[i];
//cout << "Average edge length before: " << avgLen << endl;
avgLen /= edgeLengths.size();
//cout << "Average edge length: " << avgLen << endl;
for (unsigned int i = m_boundary.GetUniformBoundary().size();
i < numVerts; i++) {
verts[i] += norms[i] * avgLen * 2.0f;
if (!norms[i].isUnit())
cerr << "Bad normal here! Frontend::InitiallyInflate()" << endl;
}
m_3Dmesh->computeNormals();
}
bool FiberMesh::OptimizeSurface()
{
if (m_3Dmesh == NULL) {
cerr << "Bad mesh! Frontend::OptimizeSurface()" << endl;
return false;
}
cout << "Optimizing surface" << endl;
int numIters = 25;
for (int i = 0; i < numIters; i++) {
float percentage = (float)(i)/numIters;
cout << (int)(percentage * 100) << "% complete";
unsigned int numVerts = m_3Dmesh->getNoVertices();
cout << ".";
double *c = new double[numVerts];
if (!GetTargetCurvatures(c)) {
cerr << "Couldn't get target curvatures" << endl;
return false;
} else {
//cout << c[0] << endl;
}
cout << ".";
double *e = new double[numVerts];
if (!GetTargetEdges(e)) {
cerr << "Couldn't get target edges" << endl;
return false;
} else {
//cout << e[0] << endl;
}
cout << "." << endl;
Vector3List newVerts;
if (!GetTargetVerts(newVerts, c, e)) {
cerr << "Couldn't get new vertex positions" << endl;
return false;
}
delete [] c;
c = NULL;
delete [] e;
e = NULL;
}
cout << "100% complete." << endl;
return true;
}
bool FiberMesh::GetTargetCurvatures(double *x)
{
if (m_3Dmesh == NULL) {
cerr << "Bad mesh. Frontend::GetTargetCurvatures()" << endl;
return false;
}
unsigned numVerts = m_3Dmesh->getNoVertices();
if (numVerts <= 0) {
cerr << "No vertices to work with. Frontend::GetTargetCurvatures()" << endl;
return false;
}
SparseMatrix b(2 * numVerts, 1);
// Fill in b with curvatures we know from our boundary.
// (Fill zero everywhere else.)
std::vector<float> curve;
// if (m_cSolveInit == false) {
// curve = m_boundary.Curvatures();
//} else {
//cout << "Getting curr curve" << endl;
curve = GetCurrentCurvatures();
//cout << "Got curr curve" << endl;
//}
unsigned numBounds = GetNum2D();
for (unsigned int i = 0; i < curve.size(); i++) {
if (isnan(curve[i])) {
b.setValue(i + numVerts, 0, 0);
} else {
if (i < numBounds)
b.setValue(i + numVerts, 0, curve[i]);
else
b.setValue(i + numVerts, 0, 0.1f * curve[i]);
}
// cout << "Curve: " << curve[i] << endl;
}
if (m_cSolveInit == false) {
SparseMatrix A(2 * numVerts, numVerts);
// Fill in our matrix A.
// Start by filling in the top half...
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
// for (int i = 0; i < 2 * numVerts; i++) {
// for (int j = 0; j < numVerts; j++) {
// A.setValue(i, j, 1.0f);
// }
// }
for (unsigned int i = 0; i < numVerts; i++) {
// For each vertex, find its neighbors.
unsigned numNeighbs = barrios[i].noVertices;
double ngbrValue = (double)(-1.0f/numNeighbs);
for (unsigned int j = 0; j < numNeighbs; j++) {
// // cout << "Vert: " << barrios[i].vertices[j]<< " out of " << numVerts << endl;
if ( barrios[i].vertices[j] > numVerts)
cerr << "Bad matrix indexing" << endl;
else
A.setValue(i, barrios[i].vertices[j], ngbrValue);
}
if ( i > numVerts)
cerr << "Bad matrix indexing" << endl;
else
A.setValue(i, i, 1.0f);
}
// And now fill the bottom half.
// Use this weighting value for interior points, as suggested by Andrew
// Nealan. (see e-mail Travis fwded)
float nonBoundWeight = 0.1f;
unsigned numBounds = m_boundary.GetUniformBoundary().size();
for (unsigned int i = 0; i < numBounds; i++) {
if ( i + numVerts > 2 * numVerts)
cerr << "Bad matrix indexing" << endl;
else
A.setValue(i + numVerts, i, 1.0f);
}
for (unsigned int i = numBounds; i < numVerts; i++) {
if ( i + numVerts > 2 * numVerts)
cerr << "Bad matrix indexing" << endl;
else
A.setValue(i + numVerts, i, nonBoundWeight);
}
m_At = A.getTranspose();
SparseMatrix AtA = m_At * A;
m_curveSolver.setA(AtA);
}
SparseMatrix AtB = m_At * b;
//cout << "aTa: " << AtA.getM() << " by " << AtA.getN() << endl;
//cout << "aTb: " << AtB.getM() << " by " << AtB.getN() << endl;
double B[numVerts];
for (unsigned int i = 0; i < numVerts; i++) {
B[i] = AtB.getValue(i, 0);
}
if (!m_curveSolver.solve(B, x)) {
cerr << "Couldn't solve for target curvatures. Frontend::ComputeTargetCurvatures()"
<< endl;
return false;
}
m_cSolveInit = true;
return true;
}
bool FiberMesh::GetTargetEdges(double *x)
{
// Solve equation 8 from the FiberMesh paper.
// Very similar to what we have in equation 6, except here we are aiming
// for target edge lengths.
std::vector<float> avgEdges = GetAverageEdgeLengths();
unsigned int numVerts = m_3Dmesh->getNoVertices();
SparseMatrix e(2 * numVerts, 1);
// Fill in e with edge lengths we know.
for (unsigned int i = 0; i < avgEdges.size(); i++) {
e.setValue(i + numVerts, 0, avgEdges[i]);
}
if (m_eSolveInit == false) {
SparseMatrix A(2 * numVerts, numVerts);
// Fill in our matrix A.
// Start by filling in the top half...
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
// for (int i = 0; i < 2 * numVerts; i++) {
// for (int j = 0; j < numVerts; j++) {
// A.setValue(i, j, 1.0f);
// }
// }
for (unsigned int i = 0; i < numVerts; i++) {
// For each vertex, find its neighbors.
unsigned numNeighbs = barrios[i].noVertices;
double ngbrValue = (double)(-1.0f/numNeighbs);
for (unsigned int j = 0; j < numNeighbs; j++) {
// // cout << "Vert: " << barrios[i].vertices[j]<< " out of " << numVerts << endl;
if ( barrios[i].vertices[j] > numVerts)
cerr << "Bad matrix indexing" << endl;
else
A.setValue(i, barrios[i].vertices[j], ngbrValue);
}
if ( i > numVerts)
cerr << "Bad matrix indexing" << endl;
else
A.setValue(i, i, 1.0f);
}
// And now fill the bottom half.
// Use this weighting value for interior points, as suggested by Andrew
// Nealan. (see e-mail Travis fwded)
// float nonBoundWeight = 0.1f;
unsigned numBounds = m_boundary.GetUniformBoundary().size();
for (unsigned int i = 0; i < numVerts; i++) {
if (i < numBounds)
A.setValue(i + numVerts, i, 1.0f);
else
A.setValue(i + numVerts, i, 0.1f);
}
// for (unsigned int i = numBounds; i < numVerts; i++) {
// if ( i + numVerts > 2 * numVerts)
// cerr << "Bad matrix indexing" << endl;
// else
// A.setValue(i + numVerts, i, nonBoundWeight);
// }
m_Et = A.getTranspose();
SparseMatrix AtA = m_Et * A;
m_edgeSolver.setA(AtA);
}
SparseMatrix AtB = m_Et * e;
double B[numVerts];
for (unsigned int i = 0; i < numVerts; i++) {
B[i] = AtB.getValue(i, 0);
}
if (!m_edgeSolver.solve(B, x)) {
cerr << "Couldn't solve for target edge lengths. Frontend::ComputeTargetEdges()"
<< endl;
return false;
}
m_eSolveInit = true;
return true;
}
std::vector<float> FiberMesh::GetCurrentCurvatures()
{
std::vector<float> currCurves;
unsigned numVerts;
Vertex *verts = m_3Dmesh->getVertices(numVerts);
unsigned numNorms;
Normal *norms = m_3Dmesh->getNormals(numNorms);
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
// First loop through, and for each vertex, sum up its
// neighbors.
for (unsigned int i = 0; i < numVerts; i++) {
Vertex neighbs(0.0f, 0.0f, 0.0f);
Vertex neighNorms(0.0f, 0.0f, 0.0f);
for (unsigned int j = 0; j < barrios[i].noVertices; j++) {
neighbs += verts[barrios[i].vertices[j]];
neighNorms += norms[barrios[i].vertices[j]];
}
// Divide by numNeighbors to get average of neighbors.
if (barrios[i].noVertices > 0) {
neighbs /= (double)barrios[i].noVertices;
neighNorms /= (double)barrios[i].noVertices;
}
currCurves.push_back((verts[i] - neighbs).dot(neighNorms));
}
// Now we need to divide by one ring area to get correct curvature
// magnitude.
unsigned numTris;
MeshTriangle *tris = m_3Dmesh->getTriangles(numTris);
for (unsigned int i = 0; i < numVerts; i++) {
float area = 0.0f;
for (unsigned int j = 0; j < barrios[i].noTriangles; j++) {
area += tris[barrios[i].triangles[j]].getSurfaceArea();
}
if (area > 0)
currCurves[i] /= area;
}
return currCurves;
}
std::vector<float> FiberMesh::GetAverageEdgeLengths()
{
std::vector<float> edgeLengths;
// Get the average edge lengths
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
unsigned int numVerts;
Vertex *verts = m_3Dmesh->getVertices(numVerts);
unsigned int nanCount = 0;
for (unsigned int i = 0; i < numVerts; i++) {
float avg = 0.0f;
for (unsigned int j = 0; j < barrios[i].noVertices; j++) {
double thisEdge = verts[i].getDistance(verts[barrios[i].vertices[j]]);
if (isnan(thisEdge))
nanCount++;
else
avg += thisEdge;
}
if (barrios[i].noVertices == nanCount) {
edgeLengths.push_back(0);
} else {
avg /= (barrios[i].noVertices - nanCount);
edgeLengths.push_back(avg);
}
}
return edgeLengths;
}
bool FiberMesh::GetTargetVerts(Vector3List verts, const double *curves,
const double *edges)
{
unsigned numBandN;
std::vector<unsigned int> boundAndNgb = GetBoundaryAndNeighbs(numBandN);
unsigned int numBounds = m_boundary.GetUniformBoundary().size();
unsigned numVerts;
Vertex *vertices = m_3Dmesh->getVertices(numVerts);
double etaWeight = 0.005f;
// If we haven't been here before, construct the A matrix we'll need.
if (m_vSolveInit == false) {
m_vSolveInit = true;
SparseMatrix A(numVerts + numBounds + numBandN, numVerts);
// Fill in our matrix A.
// Start by filling in the top portion...
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
for (unsigned int i = 0; i < numVerts; i++) {
// For each vertex, find its neighbors.
unsigned numNeighbs = barrios[i].noVertices;
double ngbrValue = (double)(-1.0f/numNeighbs);
for (unsigned int j = 0; j < numNeighbs; j++) {
A.setValue(i, barrios[i].vertices[j], ngbrValue);
}
A.setValue(i, i, 1.0f);
}
// Fill in the bottom.
for (unsigned int i = 0; i < numBounds; i++) {
A.setValue(i + numVerts, i, 100.0f);
}
// Fill in the very bottom.
for (unsigned int i = 0; i < numBounds; i++) {
for (unsigned int j = 0; j < barrios[i].noVertices; j++) {
A.setValue(i + j + numVerts + numBounds, i, etaWeight);
A.setValue(i + j + numVerts + numBounds,
barrios[i].vertices[j], -etaWeight);
}
}
m_Vt = A.getTranspose();
SparseMatrix AtA = m_Vt * A;
m_vertSolver.setA(AtA);
}
SparseMatrix b(numVerts + numBounds + numBandN, 3);
// Fill in the first numVerts elements with delta values.
Vector3List delta = GetDeltas(curves);
for (unsigned int i = 0; i < numVerts; i++) {
b.setValue(i, 0, delta[i][0]);
b.setValue(i, 1, delta[i][1]);
b.setValue(i, 2, delta[i][2]);
}
// Fill in the next numBounds elements with current boundary
// positions.
for (unsigned int i = 0; i < numBounds; i++) {
b.setValue(numVerts + i, 0, 100 * vertices[i][0]);
b.setValue(numVerts + i, 1, 100 * vertices[i][1]);
b.setValue(numVerts + i, 2, 100 * vertices[i][2]);
}
// Fill in the last numBandN elements with eta values.
Vector3List eta = GetEtas(edges);
for (unsigned int i = 0; i < numBandN; i++) {
b.setValue(numVerts + numBounds + i, 0, eta[i][0] * etaWeight);
b.setValue(numVerts + numBounds + i, 1, eta[i][1] * etaWeight);
b.setValue(numVerts + numBounds + i, 2, eta[i][2] * etaWeight);
}
SparseMatrix AtB = m_Vt * b;
double x[numVerts];
double y[numVerts];
double z[numVerts];
// Solve for x coordinate.
double X[numVerts];
for (unsigned int i = 0; i < numVerts; i++) {
X[i] = AtB.getValue(i, 0);
}
if (!m_vertSolver.solve(X, x)) {
cerr << "Couldn't solve for target x coords."
<< endl;
return false;
}
// Solve for y coordinate.
double Y[numVerts];
for (unsigned int i = 0; i < numVerts; i++) {
Y[i] = AtB.getValue(i, 1);
}
if (!m_vertSolver.solve(Y, y)) {
cerr << "Couldn't solve for target x coords."
<< endl;
return false;
}
// Solve for z coordinate.
double Z[numVerts];
for (unsigned int i = 0; i < numVerts; i++) {
Z[i] = AtB.getValue(i, 2);
}
if (!m_vertSolver.solve(Z, z)) {
cerr << "Couldn't solve for target x coords."
<< endl;
return false;
}
// Update mesh.
for (unsigned int i = 0; i < numVerts; i++) {
//cout << "Old pos: " << vertices[i] << endl;
//cout << "New pos: " << x[i] << " " << y[i] << " " << z[i] << endl;
vertices[i][0] = x[i];
vertices[i][1] = y[i];
vertices[i][2] = z[i];
}
m_3Dmesh->computeNormals();
return true;
}
std::vector<unsigned int> FiberMesh::GetBoundaryAndNeighbs(unsigned &numPts)
{
std::vector<unsigned int> boundAndNgb;
numPts = 0;
// Start by getting the boundary.
unsigned numBounds = m_boundary.GetUniformBoundary().size();
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
for (unsigned int i = 0; i < numBounds; i++) {
for (unsigned int j = 0; j < barrios[i].noVertices; j++) {
boundAndNgb.push_back(barrios[i].vertices[j]);
}
}
numPts = boundAndNgb.size();
return boundAndNgb;
}
bool FiberMesh::Create3DMesh(const std::vector<MeshTriangle> &faces)
{
if (m_3Dverts.size() <= 0)
return false;
if (faces.size() <= 0)
return false;
// Clear up any previous garbage stored in our MeshData structure.
ClearMeshData();
m_3DmeshData.triangles = faces;
m_3DmeshData.vertices = m_3Dverts;
// Remove any bad triangles that aren't fully contained within our original
// boundary curve.
RemoveBadTriangles();
m_3Dmesh = new Mesh(m_3DmeshData);
if (m_3Dmesh == NULL) {
return false;
}
m_3Dmesh->init();
// Pump our mesh up just a touch, so initial curvature values are ok.
InitiallyInflate();
m_3Dmesh->init(true);
return true;
}
void FiberMesh::RemoveBadTriangles()
{
//std::vector<MeshTriangle> triangles = m_3DmeshData.triangles;
Vector2List positions = m_boundary.GetBoundsAndDups();
std::vector<unsigned int> forRemoval;
for (unsigned int i = 0; i < m_3DmeshData.triangles.size(); i++) {
// Get 2D positions for each triangle's vertices
Vector2 aPos = positions[m_3DmeshData.triangles[i].A];
Vector2 bPos = positions[m_3DmeshData.triangles[i].B];
Vector2 cPos = positions[m_3DmeshData.triangles[i].C];
Vector2 center = (aPos + bPos + cPos) / 3;
if (!MeshUtils::isPointInPolygon(center, m_boundary.GetUniformBoundary())) {
m_3DmeshData.triangles.erase(m_3DmeshData.triangles.begin() + i);
i--;
//cout << "Removed a bad triangle" << endl;
}
}
}
void FiberMesh::ProjectVertsToWorldSpace(const Vector2List &verts)
{
// Don't want to append to a non-empty list. Clear this sucker out.
m_3Dverts.clear();
Vector2 screenPt(Vector2::zero());
Vector3 worldPt(Vector3::zero());
for (unsigned int i = 0; i < verts.size(); i++) {
screenPt = Vector2(verts[i][0]/m_screenSize[0], verts[i][1]/m_screenSize[1]);
worldPt = MeshUtils::getWorldVertex(m_camera, screenPt);
m_3Dverts.push_back(worldPt);
}
}
void FiberMesh::ComputeCentroid() {
unsigned numVerts;
Vertex *verts = m_3Dmesh->getVertices(numVerts);
Vector3 centerRot(Vector3::zero());
m_maxMag = 0.0f;
for (unsigned int i = 0; i < numVerts; i++) {
centerRot += verts[i];
if (verts[i].getMagnitude() > m_maxMag)
m_maxMag = verts[i].getMagnitude();
}
centerRot /= numVerts;
if (isnan(centerRot[0]) || isnan(centerRot[1]) || isnan(centerRot[1])) {
m_centroid = Vector3::zero();
return;
}
//cout << "Max magnitude is " << m_maxMag << endl;
m_centroid = centerRot;
}
void FiberMesh::ComputeCentroidPoint()
{
Vector3 centerRot = GetCentroid();
m_centroidPt = Point3(centerRot[0], centerRot[1], centerRot[2]);
}
Vector3List FiberMesh::GetDeltas(const double *c)
{
//cout << "Getting deltas" << endl;
Vector3List deltas;
unsigned numVerts = m_3Dmesh->getNoVertices();
unsigned numTris;
MeshTriangle *triangles = m_3Dmesh->getTriangles(numTris);
unsigned numNorms;
Normal *norms = m_3Dmesh->getNormals(numNorms);
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
for (unsigned int i = 0; i < numVerts; i++) {
// Get average normal of neighbors
Vector3 normal = norms[i];
for (unsigned int j = 0; j < barrios[i].noVertices; j++) {
normal += norms[barrios[i].vertices[j]];
}
if (barrios[i].noVertices > 0) {
normal /= barrios[i].noVertices;
}
// Get one-ring triangle area
float area = 0.0f;
for (unsigned int j = 0; j < barrios[i].noTriangles; j++) {
area += triangles[barrios[i].triangles[j]].getSurfaceArea();
}
area /= 3.0f;
// cout << "Area: " << area << "C_i: " << c[i] << "Normal_i: " << normal << endl;
deltas.push_back(area * c[i] * normal);
}
// cout << "Got deltas." << endl;
return deltas;
}
Vector3List FiberMesh::GetEtas(const double *e)
{
//cout << "Getting etas" << endl;
Vector3List etas;
// Compute eta values
unsigned numVerts;
Vertex *verts = m_3Dmesh->getVertices(numVerts);
unsigned numBounds = m_boundary.GetUniformBoundary().size();
std::vector<float> avgEdges = GetAverageEdgeLengths();
Neighborhood *barrio;
MeshUtils::getNeighbors(m_3Dmesh, &barrio);
for (unsigned int i = 0; i < numBounds; i++) {
for (unsigned int j = 0; j < barrio[i].noVertices; j++) {
float edge = 0.5f * (avgEdges[i] - avgEdges[barrio[i].vertices[j]]);
Vector3 vec = verts[i] - verts[barrio[i].vertices[j]];
vec.normalize();
etas.push_back(edge * vec);
}
}
//cout << "Got etas" << endl;
return etas;
}
bool FiberMesh::DeformCurve(Vector2 mousePos, unsigned int vertID,
bool done)
{
unsigned numIters = 1;
if (done) {
numIters = 15;
}
unsigned numVerts = m_3Dmesh->getNoVertices();
// Determine where we should be moving this handle point.
Vector2 screenPt(mousePos[0]/m_screenSize[0], mousePos[1]/m_screenSize[1]);
Vector3 new3Dpt(MeshUtils::getWorldVertex(m_camera, screenPt));
if (!m_dSolveInit) {
// Determine which boundary points we need to keep
// fixed.
// For a guesstimate, let's use a quarter of the boundary
// points in our deformation.
unsigned int deformNbhd = 10;
std::vector<unsigned int> deformers;
deformers.push_back(vertID);
for (unsigned int i = 1; i < deformNbhd; i++) {
unsigned int ptFwd = vertID + i;
unsigned int ptBwd = vertID - i;
if (vertID + i >= GetNum2D()) {
ptFwd = vertID + i - GetNum2D();
}
if (vertID - i < 0) {
ptBwd = GetNum2D() + (vertID - i);
}
deformers.push_back(ptFwd);
deformers.push_back(ptBwd);
}
for (unsigned int i = 0; i < GetNum2D(); i++) {
bool found = false;
for (unsigned int j = 0; j < deformers.size(); j++) {
if (i == deformers[j])
found = true;
}
if (!found)
m_nonDeformers.push_back(i);
}
//cout << "Num non deformers " << m_nonDeformers.size() << endl;
// Ok, now we have our fixed points. Let's create
// our matrices using the weight Andy Nealan suggested.
SparseMatrix A(numVerts + m_nonDeformers.size() + 1, numVerts);
// Fill in our matrix A.
// Start by filling in the top half...
Neighborhood *barrios;
MeshUtils::getNeighbors(m_3Dmesh, &barrios);
numVerts = m_3Dmesh->getNoVertices();
for (unsigned int i = 0; i < numVerts; i++) {
// For each vertex, find its neighbors.
unsigned numNeighbs = barrios[i].noVertices;
double ngbrValue = (double)(-1.0f/numNeighbs);
for (unsigned int j = 0; j < numNeighbs; j++) {
A.setValue(i, barrios[i].vertices[j], ngbrValue);
}
A.setValue(i, i, 1.0f);
}
// And now fill the bottom half.
for (unsigned int i = 0; i < m_nonDeformers.size(); i++) {
A.setValue(i + numVerts, m_nonDeformers[i], 100.0f);
}
A.setValue(m_nonDeformers.size() + numVerts, vertID, 100.0f);;
m_Dt = A.getTranspose();
SparseMatrix AtA = m_Dt * A;
m_deformSolver.setA(AtA);
m_dSolveInit = true;
}
// Do solving.
for (unsigned int i = 0; i < numIters; i++) {
Vertex *verts = m_3Dmesh->getVertices(numVerts);
SparseMatrix b(numVerts + m_nonDeformers.size() + 1, 3);
double *curves = new double[numVerts];
if (!GetTargetCurvatures(curves)) {
cerr << "GetTargetCurvatures failed" << endl;
} else {
Vector3List deltas = GetDeltas(curves);