-
Notifications
You must be signed in to change notification settings - Fork 2
/
Assignment3.py
executable file
·1202 lines (946 loc) · 41.4 KB
/
Assignment3.py
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
# Basic application to load a mesh from file and view it in a window
# Python imports
import sys, os
import euclid as eu
import time
import numpy as np
import scipy.sparse as sps
import scipy.sparse.linalg
from scipy.sparse.linalg.dsolve import linsolve
from scipy.sparse.linalg import dsolve
from scipy.sparse import csr_matrix
## Imports from this project
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'core')) # hack to allow local imports without creaing a module or modifying the path variable
from InputOutput import *
from MeshDisplay import MeshDisplay
#from HalfEdgeMesh import *
from HalfEdgeMesh_ListImplementation import *
from Utilities import *
"""
an important misunderstanding about
the cotan-Laplace matrix L:
it does not represent the Laplace operator.
Instead, it represents the 'conformal Laplacian'
it gives you the laplacian integrated over each dual cell
TLM says:
We are computing the Laplace operator on a surface
We are not solving for some potential-flow satisfying various constraints
The only 'constraint' is the shape of the space. --- A closed manifold.
No extra conditions. Just compute
d*d(u) = rho
"""
"""
import pydec
import numpy as np
from scipy.linalg import lu
A = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]])
p, l, u = lu(A)
from scipy.sparse import csr_matrix # row format
#"""
def main(inputfile, show=False,
StaticGeometry=False, partString='part1'):
# Get the path for the mesh to load from the program argument
if(len(sys.argv) == 3):
partString = sys.argv[1]
if partString not in ['part1','part2','part3']:
print("ERROR part specifier not recognized. Should be one of 'part1', 'part2', or 'part3'")
exit()
filename = sys.argv[2]
elif inputfile is not None:
filename = inputfile
else:
print("ERROR: Incorrect call syntax. Proper syntax is 'python Assignment3.py partN path/to/your/mesh.obj'.")
exit()
# Read in the mesh
mesh = HalfEdgeMesh(readMesh(filename),
staticGeometry=StaticGeometry)
# Create a viewer object
winName = 'DDG Assignment3 ' + partString + '-- ' + os.path.basename(filename)
meshDisplay = MeshDisplay(windowTitle=winName)
meshDisplay.setMesh(mesh)
###################### BEGIN YOUR CODE
# implement the body of each of these functions
############################
# assignment 2 code:
############################
@property
@cacheGeometry
def faceArea(self):
"""
Compute the area of a face.
Though not directly requested, this will be
useful when computing face-area weighted normals below.
This method gets called on a face,
so 'self' is a reference to the
face at which we will compute the area.
"""
v = list(self.adjacentVerts())
a = 0.5 * norm(cross(v[1].position - v[0].position,
v[2].position - v[0].position))
return a
@property
@cacheGeometry
def vertexNormal_EquallyWeighted(self):
"""
Compute a vertex normal using the 'equally weighted' method.
This method gets called on a vertex,
so 'self' is a reference to the
vertex at which we will compute the normal.
http://brickisland.net/cs177/?p=217
Perhaps the simplest way to get vertex normals
is to just add up the neighboring face normals:
"""
normalSum = np.array([0.0,0.0,0.0])
for face in self.adjacentFaces():
normalSum += face.normal
n = normalize(normalSum)
#issue:
# two different tessellations of the same geometry
# can produce very different vertex normals
return n
@property
@cacheGeometry
def vertexNormal_AreaWeighted(self):
"""
Compute a vertex normal using
the 'face area weights' method.
This method gets called on a vertex,
so 'self' is a reference to the
vertex at which we will compute the normal.
The area-weighted normal vector for this vertex"""
normalSum = np.array([0.0,0.0,0.0])
for face in self.adjacentFaces():
normalSum += face.normal * face.area
n = normalize(normalSum)
#print 'computed vertexNormal_AreaWeighted n = ',n
return n
@property
@cacheGeometry
def vertexNormal_AngleWeighted(self):
"""
element type : vertex
Compute a vertex normal using the
'Tip-Angle Weights' method.
This method gets called on a vertex,
so 'self' is a reference to the
vertex at which we will compute the normal.
A simple way to reduce dependence
on the tessellation is to weigh face normals
by their corresponding tip angles theta, i.e.,
the interior angles incident on the vertex of interest:
"""
normalSum = np.array([0.0,0.0,0.0])
for face in self.adjacentFaces():
vl = list(face.adjacentVerts())
vl.remove(self)
v1 = vl[0].position - self.position
v2 = vl[1].position - self.position
# norm ->no need for check:
# it doesn not matter what the sign is?
#area = norm(cross(v1, v2))
##if area < 0.0000000001*max((norm(v1),norm(v2))):
#if area < 0.:
# area *= -1.
alpha = np.arctan2(norm(cross(v1,v2)),
dot(v1,v2))
#print v1
#print v2
#print alpha
#print ''
normalSum += face.normal * alpha
n = normalize(normalSum)
return n
@property
@cacheGeometry
def faceNormal(self):
"""
Compute normal at a face of the mesh.
Unlike at vertices, there is one very
obvious way to do this, since a face
uniquely defines a plane.
This method gets called on a face,
so 'self' is a reference to the
face at which we will compute the normal.
"""
v = list(self.adjacentVerts())
n = normalize(cross(v[1].position - v[0].position,
v[2].position - v[0].position))
return n
@property
@cacheGeometry
def cotan(self):
"""
element type : halfedge
Compute the cotangent of
the angle OPPOSITE this halfedge.
This is not directly required,
but will be useful
when computing the mean curvature
normals below.
This method gets called
on a halfedge,
so 'self' is a reference to the
halfedge at which we will compute the cotangent.
https://math.stackexchange.com/questions/2041099/
angle-between-vectors-given-cross-and-dot-product
see half edge here:
Users/lukemcculloch/Documents/Coding/Python/
DifferentialGeometry/course-master/libddg_userguide.pdf
"""
# Validate that this is on a triangle
if self.next.next.next is not self:
raise ValueError("ERROR: halfedge.cotan() is only well-defined on a triangle")
if self.isReal:
# Relevant vectors
A = -self.next.vector
B = self.next.next.vector
# Nifty vector equivalent of cot(theta)
val = np.dot(A,B) / norm(cross(A,B))
return val
else:
return 0.0
@property
@cacheGeometry
def angleDefect(self):
"""
angleDefect <=> local Gaussian Curvature
element type : vertex
Compute the angle defect of a vertex,
d(v) (see Assignment 1 Exercise 8).
This method gets called on a vertex,
so 'self' is a reference to the
vertex at which we will compute the angle defect.
"""
"""
el = list(self.adjacentEdges())
evpl = list(self.adjacentEdgeVertexPairs())
fl = list(self.adjacentFaces())
vl = list(self.adjacentVerts())
https://scicomp.stackexchange.com/questions/27689/
numerically-stable-way-of-computing-angles-between-vectors
#"""
hl = list(self.adjacentHalfEdges())
lenhl = len(hl)
hl.append(hl[0])
alpha = 0.
for i in range(lenhl):
v1 = hl[i].vector
v2 = hl[i+1].vector
alpha += np.arctan2(norm(cross(v1,v2)),
dot(v1,v2))
#dv = 2.*np.pi - alpha
return 2.*np.pi - alpha
def totalGaussianCurvature():
"""
Compute the total Gaussian curvature
in the mesh,
meaning the sum of Gaussian
curvature at each vertex.
Note that you can access
the mesh with the 'mesh' variable.
"""
tot = 0.
for vel in mesh.verts:
tot += vel.angleDefect
return tot
def gaussianCurvatureFromGaussBonnet():
"""
Compute the total Gaussian curvature
that the mesh should have, given that the
Gauss-Bonnet theorem holds
(see Assignment 1 Exercise 9).
Note that you can access
the mesh with the 'mesh' variable.
The mesh includes members like
'mesh.verts' and 'mesh.faces', which are
sets of the vertices (resp. faces) in the mesh.
"""
V = len(mesh.verts)
E = len(mesh.edges)
F = len(mesh.faces)
EulerChar = V-E+F
return 2.*np.pi*EulerChar
############################
# Part 0: Helper functions #
############################
# Implement a few useful functions that you will want in the remainder of
# the assignment.
@property
@cacheGeometry
def cotanWeight(self):
"""
Return the cotangent weight for an edge. Since this gets called on
an edge, 'self' will be a reference to an edge.
This will be useful in the problems below.
Don't forget, everything you implemented for the last homework is now
available as part of the library (normals, areas, etc). (Moving forward,
Vertex.normal will mean area-weighted normals, unless otherwise specified)
"""
val = 0.0
if self.anyHalfEdge.isReal:
val += self.anyHalfEdge.cotan
if self.anyHalfEdge.twin.isReal:
val += self.anyHalfEdge.twin.cotan
val *= 0.5
return val
@property
@cacheGeometry
def vertex_Laplace(self):
"""
element type : vertex
Compute a vertex normal
using the 'mean curvature' method.
del del phi = 2NH
-picked up negative sign due to
cross products pointing into the page?
-no they are normalized.
-picked up a negative sign due to
the cotan(s) being defined
for pj, instead of pi.
But how did it change anything?
"""
hl = list(self.adjacentHalfEdges())
pi = self.position
sumj = 0.
ot = 1./3.
for hlfedge in hl:
pj = hlfedge.vertex.position
ct1 = hlfedge.cotan
ct2 = hlfedge.twin.cotan
sumj += (ct1+ct2)*(pj-pi)
#laplace = .5*sumj
return normalize(-.5*sumj)
##
##*******************************************************
##
@property
@cacheGeometry
def dualArea(self):
"""
Return the dual area associated with a vertex.
Since this gets called on
a vertex, 'self' will be a
reference to a vertex.
Recall that the dual area can be
defined as 1/3 the area of the surrounding
faces.
http://brickisland.net/DDGFall2017/
'the barycentric dual area associated
with a vertex i is equal to one-third the area
of all triangles ijk touching i.'
"""
fl = list(self.adjacentFaces())
area_star = 0.
for ff in fl:
area_star += ff.area/3.
return area_star
def enumerateVertices(mesh):
"""
Assign a unique index from 0 to (N-1) to each vertex in the mesh. Should
return a dictionary containing mappings {vertex ==> index}.
You will want to use this function in your solutions below.
"""
# index_map = {}
# index = 0
# for vv in mesh.verts:
# index_map[vv] = index
# index += 1
return mesh.enumerateVertices
@property
@cacheGeometry
def adjacency(self):
index_map = enumerateVertices(self)
nrows = ncols = len(mesh.verts)
adjacency = np.zeros((nrows,ncols),int)
for vv in mesh.verts:
ith = index_map[vv]
avlist = list(vv.adjacentVerts())
for av in avlist:
jth = index_map[av]
adjacency[ith,jth] = 1
return adjacency
#################################
# Part 1: Dense Poisson Problem #
#################################
# Solve a Poisson problem on the mesh. The primary function here
# is solvePoissonProblem_dense(), it will get called when you run
# python Assignment3.py part1 /path/to/your/mesh.obj
# and specify density values with the mouse (the press space to solve).
#
# Note that this code will be VERY slow on large meshes, because it uses
# dense matrices.
def buildLaplaceMatrix_dense(mesh, index_map=None):
"""
Build a Laplace operator for the mesh, with a dense representation
'index' is a dictionary mapping {vertex ==> index}
TLM renamed to index_map
Returns the resulting matrix.
"""
if index_map is None:
# index_map = mesh.enumerateVertices()
index_map = enumerateVertices(mesh)
nrows = ncols = len(mesh.verts)
adjacency = np.zeros((nrows,ncols),int)
for vv in mesh.verts:
ith = index_map[vv]
avlist = list(vv.adjacentVerts())
for av in avlist:
jth = index_map[av]
adjacency[ith,jth] = 1
Laplacian = np.zeros((nrows,ncols),float)
for vi in mesh.verts:
ith = index_map[vi]
ll = list(vi.adjacentEdgeVertexPairs())
for edge, vj in ll:
jth = index_map[vj]
# Laplacian[ith,jth] = np.dot(vj.normal,
# edge.cotanWeight*(vj.position -
# vi.position)
# )
if ith == jth:
pass #Laplacian[ith,jth] = edge.cotanWeight
else:
Laplacian[ith,jth] = edge.cotanWeight
Laplacian[ith,ith] = -sum(Laplacian[ith])
return Laplacian
def buildMassMatrix_dense(mesh, index):
"""
Build a mass matrix for the mesh.
Returns the resulting matrix.
"""
nrows = ncols = len(mesh.verts)
#MassMatrix = np.zeros((nrows),float)
MassMatrix = np.zeros((nrows,ncols),float)
for i,vert in enumerate(mesh.verts):
#MassMatrix[i,i] = 1./vert.dualArea
MassMatrix[i,i] = vert.dualArea
return MassMatrix
def solvePoissonProblem_dense(mesh, densityValues):
"""
Solve a Poisson problem on the mesh. The results should be stored on the
vertices in a variable named 'solutionVal'. You will want to make use
of your buildLaplaceMatrix_dense() function from above.
densityValues is a dictionary mapping {vertex ==> value} that specifies
densities. The density is implicitly zero at every vertex not in this
dictionary.
When you run this program with 'python Assignment3.py part1 path/to/your/mesh.obj',
you will get to click on vertices to specify density conditions. See the
assignment document for more details.
"""
index_map = enumerateVertices(mesh)
L = buildLaplaceMatrix_dense(mesh, index_map)
M = buildMassMatrix_dense(mesh,index_map) #M <= 2D
rho = np.zeros((len(mesh.verts)),float)
for key in densityValues:
#index_val = index_map[key]
print 'key dual area = ', key.dualArea
rho[index_map[key]] = densityValues[key]#*key.dualArea
#
# SwissArmyLaplacian,
# page 179 Cu = Mf is better conditioned
sol_vec = np.linalg.solve(L, np.dot(M,rho) )
#sparse attempts:
#sol_vec = linsolve.spsolve(L, rho)
#sol_vec = dsolve.spsolve(L, rho, use_umfpack=False)
#sol_vec = dsolve.spsolve(L, rho, use_umfpack=True)
for vert in mesh.verts:
key = index_map[vert]
#print 'TLM sol_vec = ',sol_vec[key]
vert.solutionVal = sol_vec[key]
if rho[key]:
vert.densityVal = rho[key]
else:
vert.densityVal = 0.
return
##################################
# Part 2: Sparse Poisson Problem #
##################################
# Solve a Poisson problem on the mesh. The primary function here
# is solvePoissonProblem_sparse(), it will get called when you run
# python Assignment3.py part2 /path/to/your/mesh.obj
# and specify density values with the mouse (the press space to solve).
#
# This will be very similar to the previous part. Be sure to see the wiki
# for notes about the nuances of sparse matrix computation. Now, your code
# should scale well to larger meshes!
def buildLaplaceMatrix_sparse(mesh, index_map=None):
"""
Build a laplace operator for the mesh, with a sparse representation.
This will be nearly identical to the dense method.
'index' is a dictionary mapping {vertex ==> index}
Returns the resulting sparse matrix.
"""
if index_map is None:
# index_map = mesh.enumerateVertices()
index_map = enumerateVertices(mesh)
nrows = ncols = len(mesh.verts)
adjacency = np.zeros((nrows,ncols),int)
for vv in mesh.verts:
ith = index_map[vv]
avlist = list(vv.adjacentVerts())
for av in avlist:
jth = index_map[av]
adjacency[ith,jth] = 1
Laplacian = np.zeros((nrows,ncols),float)
for vi in mesh.verts:
ith = index_map[vi]
ll = list(vi.adjacentEdgeVertexPairs())
for edge, vj in ll:
jth = index_map[vj]
# Laplacian[ith,jth] = np.dot(vj.normal,
# edge.cotanWeight*(vj.position -
# vi.position)
# )
if ith == jth:
pass #Laplacian[ith,jth] = edge.cotanWeight
else:
Laplacian[ith,jth] = edge.cotanWeight
Laplacian[ith,ith] = -sum(Laplacian[ith])
return csr_matrix(Laplacian)
def buildMassMatrix_sparse(mesh, index):
"""
Build a sparse mass matrix for the system.
Returns the resulting sparse matrix.
"""
nrows = ncols = len(mesh.verts)
MassMatrix = np.zeros((nrows),float)
#for i,vert in enumerate(mesh.verts):
# MassMatrix[i] = vert.dualArea
return MassMatrix
def solvePoissonProblem_sparse(mesh, densityValues):
"""
Solve a Poisson problem on the mesh, using sparse matrix operations.
This will be nearly identical to the dense method.
The results should be stored on the vertices in a variable named 'solutionVal'.
densityValues is a dictionary mapping {vertex ==> value} that specifies any
densities. The density is implicitly zero at every vertex not in this dictionary.
Note: Be sure to look at the notes on the github wiki about sparse matrix
computation in Python.
When you run this program with 'python Assignment3.py part2 path/to/your/mesh.obj',
you will get to click on vertices to specify density conditions. See the
assignment document for more details.
"""
index_map = enumerateVertices(mesh)
L = buildLaplaceMatrix_sparse(mesh, index_map)
M = buildMassMatrix_dense(mesh,index_map) #M <= 2D
rho = np.zeros((len(mesh.verts)),float)
for key in densityValues:
#index_val = index_map[key]
print 'key dual area = ', key.dualArea
rho[index_map[key]] = densityValues[key]#*key.dualArea
# convert to sparse matrix (CSR method)
#Lsparse = csr_matrix(L)
#iL = np.linalg.inv(L)
#sol_vec = np.dot(iL,rho)
#sol_vec = np.linalg.solve(L, rho)
#sol_vec = linsolve.spsolve(L, rho)
#sol_vec = linsolve.spsolve(L, np.dot(M,rho) )
#sol_vec = dsolve.spsolve(L, rho, use_umfpack=False)
sol_vec = dsolve.spsolve(L, np.dot(M,rho) , use_umfpack=True)
for vert in mesh.verts:
key = index_map[vert]
#print 'TLM sol_vec = ',sol_vec[key]
vert.solutionVal = sol_vec[key]
if rho[key]:
vert.densityVal = rho[key]
else:
vert.densityVal = 0.
return
###############################
# Part 3: Mean Curvature Flow #
###############################
# Perform mean curvature flow on the mesh. The primary function here
# is meanCurvatureFlow(), which will get called when you run
# python Assignment3.py part3 /path/to/your/mesh.obj
# You can adjust the step size with the 'z' and 'x' keys, and press space
# to perform one step of flow.
#
# Of course, you will want to use sparse matrices here, so your code
# scales well to larger meshes.
def buildMeanCurvatureFlowOperator(mesh,
index=None,
h=None):
"""
Construct the (sparse) mean curvature operator matrix for the mesh.
It might be helpful to use your buildLaplaceMatrix_sparse() and
buildMassMatrix_sparse() methods from before.
Returns the resulting matrix.
"""
nrows = ncols = len(mesh.verts)
##MassMatrix = np.zeros((nrows),float)
#MassMatrix = np.zeros((nrows,ncols),float)
#for i,vert in enumerate(mesh.verts):
# MassMatrix[i] = 1./vert.dualArea
# #MassMatrix[i,i] = 1./vert.dualArea
Laplacian = np.zeros((nrows,ncols),float)
for vi in mesh.verts:
ith = index[vi]
ll = list(vi.adjacentEdgeVertexPairs())
for edge, vj in ll:
jth = index[vj]
# Laplacian[ith,jth] = np.dot(vj.normal,
# edge.cotanWeight*(vj.position -
# vi.position)
# )
if ith == jth:
pass #Laplacian[ith,jth] = edge.cotanWeight
else:
Laplacian[ith,jth] = edge.cotanWeight
Laplacian[ith,ith] = -sum(Laplacian[ith])
return csr_matrix(Laplacian)
def meanCurvatureFlow_use_numpy_solve(mesh, h):
"""
Perform mean curvature flow on the mesh. The result of this operation
is updated positions for the vertices; you should conclude by modifying
the position variables for the mesh vertices.
h is the step size for the backwards euler integration.
When you run this program with 'python Assignment3.py part3 path/to/your/mesh.obj',
you can press the space bar to perform this operation and z/x to change
the step size.
Recall that before you modify the positions of the mesh, you will need
to set mesh.staticGeometry = False, which disables caching optimizations
but allows you to modfiy the geometry. After you are done modfiying
positions, you should set mesh.staticGeometry = True to re-enable these
optimizations. You should probably have mesh.staticGeometry = True while
you assemble your operator, or it will be very slow.
"""
# index_map = mesh.enumerateVertices()
index_map = enumerateVertices(mesh)
nrows = ncols = len(mesh.verts)
Id = np.identity(nrows,float)
M = buildMassMatrix_dense(mesh,index_map) #M <= 2D
MCF = buildMeanCurvatureFlowOperator(mesh,
index=index_map,
h=h)
#
# SwissArmyLaplacian,
# page 181 (I-hC)u = u is not symmetric
# (M-hC)u = Mu is better conditioned
#----------------------------------------------
Mi = np.linalg.inv(M)
L = np.matmul(Mi,MCF)
#UpdateOperator = np.linalg.inv(Id-h*L)
#----------------------------------------------
#UpdateOperator = np.linalg.inv(M-h*MCF)
LHS = M-h*MCF
UpdateOperator = np.linalg.inv(LHS)
#UpdateOperator = np.matmul(UpdateOperator,M)
vertices = np.zeros((nrows,3),float)
for i,vert in enumerate(mesh.verts):
vertices[i] = vert.position
LHS = Id-h*L
UpdateOperator = np.linalg.solve(LHS, vertices)
vertices = UpdateOperator
for i,vert in enumerate(mesh.verts):
#key = index_map[vert]
vert.position = vertices[i]
#
# vertices = np.dot(UpdateOperator,vertices)
# for i,vert in enumerate(mesh.verts):
# key = index_map[vert]
# vert.position = vertices[i]
return
def meanCurvatureFlow(mesh, h):
"""
Perform mean curvature flow on the mesh. The result of this operation
is updated positions for the vertices; you should conclude by modifying
the position variables for the mesh vertices.
h is the step size for the backwards euler integration.
When you run this program with 'python Assignment3.py part3 path/to/your/mesh.obj',
you can press the space bar to perform this operation and z/x to change
the step size.
Recall that before you modify the positions of the mesh, you will need
to set mesh.staticGeometry = False, which disables caching optimizations
but allows you to modfiy the geometry. After you are done modfiying
positions, you should set mesh.staticGeometry = True to re-enable these
optimizations. You should probably have mesh.staticGeometry = True while
you assemble your operator, or it will be very slow.
"""
# index_map = mesh.enumerateVertices()
index_map = enumerateVertices(mesh)
nrows = ncols = len(mesh.verts)
#Id = np.identity(nrows,float)
M = buildMassMatrix_dense(mesh,index_map) #M <= 2D
Msp = csr_matrix(M)
#pure cotan operator:
MCF = buildMeanCurvatureFlowOperator(mesh,
index=index_map,
h=h)
#
# SwissArmyLaplacian,
# page 181 (I-hC)u = u is not symmetric
# (M-hC)u = Mu is better conditioned
#----------------------------------------------
#Mi = np.linalg.inv(M)
#L = np.matmul(Mi,MCF)
#UpdateOperator = np.linalg.inv(Id-h*L)
#----------------------------------------------
#LHS = M-h*MCF
LHS = Msp - MCF.multiply(h)
#UpdateOperator = np.linalg.inv(LHS)
#UpdateOperator = np.matmul(UpdateOperator,M)
UpdateOperator = dsolve.spsolve(LHS,
M ,
use_umfpack=True)
vertices = np.zeros((nrows,3),float)
for i,vert in enumerate(mesh.verts):
vertices[i] = vert.position
#https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.linalg.cho_solve.html
#UpdateOperator = scipy.linalg.cho_solve(
# scipy.linalg.cho_factor(LHS),
# np.dot(M,vertices))
#P, L, U = scipy.linalg.lu(LHS)
# for non symmetric, numpy solve, style:
# LHS = Id-h*L
# UpdateOperator = np.linalg.solve(LHS, vertices)
# vertices = UpdateOperator
# for i,vert in enumerate(mesh.verts):
# #key = index_map[vert]
# vert.position = vertices[i]
#
vertices = np.dot(UpdateOperator,vertices)
for i,vert in enumerate(mesh.verts):
#key = index_map[vert]
vert.position = vertices[i]
return
###################### END YOUR CODE
# from assignment 2:
Face.normal = faceNormal
Face.area = faceArea
Vertex.normal = vertexNormal_AreaWeighted
Vertex.vertexNormal_EquallyWeighted = vertexNormal_EquallyWeighted
Vertex.vertexNormal_AreaWeighted = vertexNormal_AreaWeighted
Vertex.vertexNormal_AngleWeighted = vertexNormal_AngleWeighted
##
Vertex.vertex_Laplace = vertex_Laplace
#
#Vertex.vertexNormal_SphereInscribed = vertexNormal_SphereInscribed
Vertex.angleDefect = angleDefect
HalfEdge.cotan = cotan
def toggleDefect():
print("\nToggling angle defect display")
if toggleDefect.val:
toggleDefect.val = False
meshDisplay.setShapeColorToDefault()
else:
toggleDefect.val = True
meshDisplay.setShapeColorFromScalar("angleDefect",
cmapName="seismic")
#,vMinMax=[-pi/8,pi/8])
meshDisplay.generateFaceData()
toggleDefect.val = False
meshDisplay.registerKeyCallback('3',
toggleDefect,
docstring="Toggle drawing angle defect coloring")
def computeDiscreteGaussBonnet():
print("\nComputing total curvature:")
computed = totalGaussianCurvature()
predicted = gaussianCurvatureFromGaussBonnet()
print(" Total computed curvature: " + str(computed))
print(" Predicted value from Gauss-Bonnet is: " + str(predicted))
print(" Error is: " + str(abs(computed - predicted)))
meshDisplay.registerKeyCallback('z',
computeDiscreteGaussBonnet,
docstring="Compute total curvature")
###################### Assignment 3 stuff
Edge.cotanWeight = cotanWeight
Vertex.dualArea = dualArea
# A pick function for choosing density conditions
densityValues = dict()
def pickVertBoundary(vert):
"""
See MeshDisplay callbacks,
pickVertexCallback
for how this works!
self.pickVertexCallback <== pickVertBoundary(vert)
self.pickVertexCallback(pickObject = your_vertex)
"""
value = 1.0 if pickVertBoundary.isHigh else -1.0
print(" Selected vertex at position:" + printVec3(vert.position))
print(" as a density with value = " + str(value))
densityValues[vert] = value
print 'densityValues = ',densityValues
pickVertBoundary.isHigh = not pickVertBoundary.isHigh
pickVertBoundary.isHigh = True
# Run in part1 mode
if partString == 'part1':
print("\n\n === Executing assignment 2 part 1")
print("""
Please click on vertices of the mesh to specify density conditions.
Alternating clicks will specify high-value (= 1.0) and low-value (= -1.0)
density conditions. You may select as many density vertices as you want,
but >= 2 are necessary to yield an interesting solution. When you are done,
press the space bar to execute your solver and view the results.
""")
meshDisplay.pickVertexCallback = pickVertBoundary
meshDisplay.drawVertices = True
def executePart1Callback():
print("\n=== Solving Poisson problem with your dense solver\n")
# Print and check the density values
print("Density values:")
for key in densityValues:
print(" " + str(key) + " = " + str(densityValues[key]))
#if len(densityValues) < 2: