/
mesh.jl
978 lines (729 loc) · 22 KB
/
mesh.jl
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
export Mesh
using Combinatorics
using Compat.Iterators
export vertexarray, cellarray
"""
`U::Int`: indicating dimension of the embedding space
`D1::Int`: one plus the manifold dimension
`T<:AbstractFloat`: the type of a vertex coordinate
"""
abstract type AbstractMesh{U,D1,T} end
mutable struct Mesh{U,D1,T} <: AbstractMesh{U,D1,T}
vertices::Vector{SVector{U,T}}
faces::Vector{SVector{D1,Int}}
"""
maps a face on its index in enumeration
"""
dict::Dict{SVector{D1,Int},Int}
end
function Mesh(vertices, faces)
# dict = Dict((f,i) for (i,f) in enumerate(faces))
dict = Dict{Int,Int}()
T = eltype(eltype(vertices))
U = length(eltype(vertices))
D1 = length(eltype(faces))
Mesh{U,D1,T}(vertices, faces, dict)
end
function indices(m::Mesh, cell)
return m.faces[cell]
end
vertexarray(m::Mesh) = [ v[i] for v in m.vertices, i in 1:universedimension(m) ]
cellarray(m::Mesh) = [ k[i] for k in m.faces, i in 1:dimension(m)+1 ]
"""
mesh(type, mdim, udim=mdim+1)
Returns an empty mesh with `coordtype` equal to `type`, of dimension `mdim`
and embedded in a universe of dimension `udim`
"""
mesh(T, mdim, udim=mdim+1) = Mesh(Pt{udim,T}[], SVector{mdim+1,Int}[])
"""
vt = vertextype(mesh)
Returns type of the vertices used to define the cells of the mesh.
"""
vertextype(m::Mesh) = eltype(m.vertices)
"""
celltype(mesh)
Returns the type of the index tuples stored in the mesh.
"""
celltype(m::Mesh) = eltype(m.faces)
"""
coordtype(mesh)
Returns `eltype(vertextype(mesh))`
"""
coordtype(m::AbstractMesh{U,D1,T}) where {U,D1,T} = T
# coordtype(m::Mesh) = eltype(vertextype(m))
"""
dim = dimension(mesh)
Returns the dimension of the mesh. Note that this is
the dimension of the cells, not of the surrounding space.
"""
dimension(m::AbstractMesh{U,D1}) where {U,D1} = D1-1
# dimension(m::Mesh) = length(celltype(m)) - 1
"""
universedimension(mesh)
Returns the dimension of the surrounding space. Equals
the number of coordinates required to describe a vertex.
"""
universedimension(m::Mesh) = length(vertextype(m))
"""
vertices(mesh)
Returns an indexable iterable to the vertices of the mesh
"""
vertices(m::Mesh) = m.vertices
# vertices(mesh, i)
# vertices(mesh, I)
#
# Select one or multiple vertices from the mesh. In the second
# form, only statically sized arrays are allowed to discourage
# memory allocation. The returned vector in that case will also
# be statically typed and of the same size as `I`.
vertices(m::Mesh, i::Number) = m.vertices[i]
cellvertices(m::AbstractMesh, cell) = vertices(m, indices(m, cell))
@generated function vertices(m::Mesh, I::SVector)
N = length(I)
xp = :(())
for i in 1:N
push!(xp.args, :(m.vertices[I[$i]]))
end
:(SVector($xp))
end
"""
numvertices(mesh)
Returns the number of vertices in the mesh.
*Note*: this is the number of vertices in the vertex buffer and might include floatin vertices
or vertices not appearing in any cell. In other words the following is not necessarily true:
```julia
numvertices(mesh) == numcells(skeleton(mesh,0))
```
"""
numvertices(m::Mesh) = length(m.vertices)
"""
numcells(mesh)
Returns the number of cells in the mesh.
"""
numcells(m::AbstractMesh) = length(m.faces)
"""
cells(mesh)
Return an iterable collection containing the cells making up the mesh.
"""
cells(mesh::Mesh) = mesh.faces
# function cells(mesh) eachindex(mesh.faces) end
Base.IteratorSize(::AbstractMesh) = Base.HasLength()
Base.length(m::AbstractMesh) = length(cells(m))
# Base.iterate(m::AbstractMesh, state=0) = iterate(cells(m), state)
Base.iterate(m::AbstractMesh, state=0) = iterate(eachindex(cells(m)), state)
# """
# cellvertices(mesh, i)
#
# Return an indexable collection containing the vertices of cell `i`.
# Shorthand for `vertices(mesh, cells(mesh, i))`.
# """
# cellvertices(mesh,i) = vertices(mesh, cells(mesh, i))
"""
translate(mesh, v)
Creates a new mesh by translating `mesh` over vector `v`
"""
translate(Γ::AbstractMesh, v) = Mesh([w + v for w in vertices(Γ)], deepcopy(cells(Γ)))
"""
translate!(mesh, v)
Translates `mesh` over vector `v` inplace.
"""
function translate!(Γ::Mesh, v)
for i in 1:length(Γ.vertices)
Γ.vertices[i] += v
end
Γ
end
"""
flip(cell)
Change the orientation of a cell by interchanging the first to indices.
"""
@generated function flip(cell)
# generate `T(cell[2],cell[1],cell[3],...)`, with `T = typeof(cell)`
N = length(cell)
if N <= 1
return :(cell)
end
xp = :($cell(cell[2], cell[1]))
for i in 3:N
push!(xp.args, :(cell[$i]))
end
xp
end
"""
flipmesh!(mesh)
Change the orientation of a mesh
"""
function flipmesh!(mesh)
mesh.faces .= flip.(mesh.faces)
mesh
end
flipmesh(mesh) = flipmesh!(deepcopy(mesh))
export mirrormesh, mirrormesh!
"""
mirror(vertex, normal, anchor)
Mirror vertex across a plane defined by its normal and a containing point.
"""
function mirror(vertex, normal, anchor)
h = dot(vertex - anchor, normal)
vertex - 2*h * normal
end
function mirrormesh!(mesh, normal, anchor)
#mesh.vertices .= mirror.(mesh.vertices, normal, anchor)
for i in eachindex(mesh.vertices)
mesh.vertices[i] = mirror(mesh.vertices[i], normal, anchor)
end
mesh
end
mirrormesh(mesh, args...) = mirrormesh!(deepcopy(mesh), args...)
function rotate!(Γ::Mesh{3}, v)
α = norm(v)
u = v/α
cα = cos(α/2)
sα = sin(α/2)
p = point(cα, sα*u[1], sα*u[2], sα*u[3])
q = point(cα, -sα*u[1], -sα*u[2], -sα*u[3])
P = @SMatrix [
+p[1] -p[2] -p[3] -p[4];
+p[2] +p[1] -p[4] +p[3];
+p[3] +p[4] +p[1] -p[2];
+p[4] -p[3] +p[2] +p[1]]
Q = @SMatrix [
+q[1] -q[2] -q[3] -q[4];
+q[2] +q[1] +q[4] -q[3];
+q[3] -q[4] +q[1] +q[2];
+q[4] +q[3] -q[2] +q[1]]
for i in 1:numvertices(Γ)
r = vertices(Γ,i)
t = point(0, r[1], r[2], r[3])
s = P * Q * t
Γ.vertices[i] = point(s[2], s[3], s[4])
end
Γ
end
function rotate(Γ::Mesh{3}, v)
R = deepcopy(Γ)
rotate!(R,v)
R
end
"""
fliporientation(mesh)
Changes the mesh orientation inplace. If non-orientatble, undefined.
"""
function fliporientation!(m::Mesh)
for i in 1:numcells(m)
m.faces[i] = fliporientation(m.faces[i])
end
return m
end
"""
fliporientation(mesh)
Returns a mesh of opposite orientation.
"""
function fliporientation(m::Mesh)
n = deepcopy(m)
fliporientation!(n)
end
@generated function fliporientation(I::SVector{N,T}) where {N,T}
@assert N >= 2
xp = :(SVector{N,T}(I[2],I[1]))
for i in 3:N
push!(xp.args, :(I[$i]))
end
return xp
end
"""
-mesh -> flipped_mesh
Create a mesh with opposite orientation.
"""
Base.:-(m::Mesh) = fliporientation(m)
Base.getindex(m::AbstractMesh, I::Vector{Int}) = Mesh(vertices(m), cells(m)[I])
"""
boundary(mesh)
Returns the boundary of `mesh` as a mesh of lower dimension.
"""
function boundary(mesh)
D = dimension(mesh)
# vertices have no boundary
@assert 0 < D
I = eltype(cells(mesh))
length(mesh) == 0 && return Mesh(vertices(mesh), I[])
# build a list of D-1 cells
edges = skeleton_fast(mesh, D-1)
faces = skeleton_fast(mesh, D)
# get the edge-face connection matrix
conn = connectivity(edges, faces, identity)
# find the edges that only have one adjacent face
#i = find(x -> x < 2, sum(abs.(conn), dims=1))
rows = rowvals(conn)
vals = nonzeros(conn)
I = celltype(edges)
bnd_edges = Vector{I}(undef, length(edges))
i = 1
for (e,edge) in enumerate(edges)
nzr = nzrange(conn,e)
length(nzr) != 1 && continue
relop = vals[nzr[1]]
inds = indices(edges, edge)
bnd_edges[i] = (relop > 0) ? inds : flip(inds)
i += 1
end
resize!(bnd_edges, i-1)
bnd = Mesh(vertices(mesh), bnd_edges)
end
"""
Complement to boundary. This function selects those edges that have at
least two faces adjacent. The case with more than two neighboring faces
occurs on non-manifold structures (e.g. containing junctions)
"""
function interior(mesh::Mesh, edges=skeleton(mesh,1))
@assert dimension(mesh) == 2
@assert vertices(mesh) === vertices(edges)
C = connectivity(edges, mesh)
@assert size(C) == (numcells(mesh), numcells(edges))
nn = vec(sum(abs.(C), dims=1))
T = CompScienceMeshes.celltype(edges)
interior_edges = Vector{T}()
for (i,edge) in pairs(cells(edges))
nn[i] > 1 && push!(interior_edges, edge)
end
Mesh(vertices(mesh), interior_edges)
end
"""
vertextocellmap(mesh) -> vertextocells, numneighbors
Computed an V×M array `vertextocells` where V is the number of vertices
and M is the maximum number of cells adjacent to any given vertex such
that `vertextocells[v,i]` is the index in the cells of `mesh` of the `i`th
cell adjacent to teh `v`-th vertex. `numneighbors[v]` contains the number
of cells adjacent to the `v`-th vertex.
This method allows e.g. for the efficient computation of the connectivity
matrix of the mesh.
"""
function vertextocellmap(mesh)
numverts = numvertices(mesh)
numcells = length(mesh)
numneighbors = zeros(Int, numverts)
for i in mesh
cell = indices(mesh, i)
for v in cell
numneighbors[v] += 1
end
end
npos = -1
vertstocells = fill(npos, numverts, maximum(numneighbors))
numneighbors = zeros(Int, numverts)
for i in mesh
cell = indices(mesh, i)
for v in cell
k = (numneighbors[v] += 1)
vertstocells[v,k] = i
end
end
vertstocells, numneighbors
end
function vertextocell(mesh)
row = Dict{Int,Tuple{Int,Int}}()
k = 1
for cell in mesh
inds = indices(mesh, cell)
for v in inds
if haskey(row,v)
(i,n) = row[v]
row[v] = (i,n+1)
else
row[v] = (k,1)
k += 1
end
end
end
NC = fill(-1, length(row))
for (i,n) in values(row)
NC[i] = n
end
@assert !any(NC .== - 1)
VC = fill(-1, length(row), maximum(NC))
fill!(NC, 0)
for (c,cell) in enumerate(mesh)
inds = indices(mesh, cell)
for v in inds
i, _ = row[v]
k = NC[i] + 1
VC[i,k] = c
NC[i] = k
end
end
LG = fill(-1, length(row))
for (v,(i,n)) in row
LG[i] = v
end
@assert !any(LG .== -1)
GL = Dict{Int,Int}()
for (v,(i,n)) in row
GL[v] = i
end
return VC, NC, LG, GL
end
# function faces(s::SVector{4,Int})
# return [
# @SVector[4,3,2],
# @SVector[1,3,4],
# @SVector[1,4,2],
# @SVector[1,2,3]
# ]
# end
#
# function faces(s::SVector{3,Int})
# return [
# @SVector[2,3],
# @SVector[3,1],
# @SVector[1,2],
# ]
# end
#
# function faces(s::SVector{2,Int})
# return[
# @SVector[2],
# @SVector[1]
# ]
# end
"""
skeleton(mesh, dim)
Returns a mesh comprising the `dim`-dimensional sub cells of `mesh`. For example to retrieve
the edges of a given surface `mesh`,
```julia
edges = skelton(mesh, 1)
```
"""
function skeleton(mesh, dim::Int; sort=:spacefillingcurve)
meshdim = dimension(mesh)
@assert 0 <= dim <= meshdim
if dim == meshdim
return mesh
end
sk = skeleton_fast(mesh,dim)
sort != :spacefillingcurve && return sk
# sort the simplices on a SFC
simplices = cells(sk)
ctrs = [sum(cellvertices(sk,c))/(dim+1) for c in sk]
if length(sk) > 0
simplices = simplices[sort_sfc(ctrs)]
end
Mesh(vertices(mesh), simplices)
end
function skeleton_fast(mesh, dim::Int)
meshdim = dimension(mesh)
@assert 0 <= dim <= meshdim
if dim == meshdim
return mesh
end
nc = numcells(mesh)
C = SVector{dim+1,Int}
simplices = zeros(C, nc*binomial(meshdim+1,dim+1))
n = 1
for c = 1 : nc
# cell = cells(mesh)[c]
cell = indices(mesh, c)
for simplex in combinations(cell,dim+1)
simplices[n] = sort(simplex)
n += 1
end
end
simplices = unique(simplices)
Mesh(vertices(mesh), simplices)
end
"""
skeleton(pred, mesh, dim)
Like `skeleton(mesh, dim)`, but only cells for which `pred(cell)`
returns true are withheld.
"""
# function skeleton(pred, mesh, dim)
# meshdim = dimension(mesh)
# @assert 0 <= dim <= meshdim
# nc = numcells(mesh)
# C = SVector{dim+1,Int}
# simplices = zeros(C, nc*binomial(meshdim+1,dim+1))
# n = 1
# for c = 1 : nc
# cell = mesh.faces[c]
# for simplex in combinations(cell,dim+1)
# if pred(mesh, SVector{dim+1,Int}(simplex))
# simplices[n] = sort(simplex)
# n += 1
# end
# end
# end
# simplices = simplices[1:n-1]
# simplices = unique(simplices)
# Mesh(mesh.vertices, simplices)
# end
"""
connectivity(faces, cells, op=sign)
Create a sparse matrix `D` of size `numcells(cells)` by `numcells(faces)` that
contiains the connectivity info of the mesh. In particular `D[m,k]` is `op(r)`
where `r` is the local index of face `k` in cell `m`. The sign of `r` is
positive or negative depending on the relative orientation of face `k` in cell
`m`.
For `op=sign`, the matrix returned is the classic connectivity matrix, i.e.
the graph version of the exterior derivative.
"""
function connectivity(kcells::AbstractMesh, mcells::AbstractMesh, op = sign)
vtok, _, lgk, glk = vertextocell(kcells)
vtom, _, lgm, glm = vertextocell(mcells)
npos = -1
dimk = numcells(kcells)
dimm = numcells(mcells)
Rows = Int[]
Cols = Int[]
Vals = Int[]
sh = 2 * max(dimm, dimk)
# sizehint!(Rows, sh)
# sizehint!(Cols, sh)
# sizehint!(Vals, sh)
for vk in axes(vtok,1)
V = lgk[vk]
haskey(glm,V) || continue
vm = glm[V]
for q in axes(vtok,2)
i = vtok[vk,q]
i == npos && break
# kcell = cells(kcells)[i]
kcell = indices(kcells,i)
for s in axes(vtom,2)
j = vtom[vm,s]
j == npos && break
# mcell = cells(mcells)[j]
mcell = indices(mcells, j)
val = op(relorientation(kcell, mcell))
iszero(val) && continue
push!(Rows, j)
push!(Cols, i)
push!(Vals, op(relorientation(kcell, mcell)))
end
end
end
D = sparse(Rows, Cols, Vals, dimm, dimk, (x,y)->y)
return D
end
# function connectivity2(kcells::AbstractMesh, mcells::AbstractMesh, op = sign)
# # if dimension(kcells) > dimension(mcells)
# # C = connectivity_impl(mcells, kcells, op)
# # return copy(transpose(C))
# # end
# return connectivity_impl(kcells, mcells, op)
# end
# import InteractiveUtils
function connectivity2(kcells::AbstractMesh, mcells::AbstractMesh)
@assert dimension(kcells) < dimension(mcells)
npos = -1
MCells = cells(mcells)
vtom, _ = vertextocellmap(mcells)
Rows = Int[]
Cols = Int[]
Vals = Int[]
for (j,kcell) in enumerate(kcells)
for v in kcell
for i in vtom[v,:]
i == npos && break
mcell = MCells[i]
issubset(kcell, mcell) || continue
push!(Rows, i)
push!(Cols, j)
σ = relorientation(kcell, mcell)
push!(Vals, σ)
end
end
end
sparse(Rows, Cols, Vals, length(mcells), length(kcells))
end
"""
pairs = cellpairs(mesh, edges, dropjunctionpair=false)
Given a mesh and set of oriented edges from that mesh (as generated by `skeleton`),
`cellpairs` will generate a 2 x K matrix, where K is the number of pairs
and each column contains a pair of indices in the cell array of `mesh` that have
one of the supplied edges in common.
Returns an array of pairs of indices, each pair corresponding to a pair of adjacent faces.
(If the mesh is oriented, the first row of `facepairs` will contain indices to the cell
for which the corresponding edge has a positive relative orientation.
If a edge lies on the boundary of the mesh, and only has one neighboring cell, the
second row of `facepairs` will contain `-k` with `k` the local index of the corresponding
edge in its neighboring triangle.
If an edge has more than two neighboring cells (i.e. the edge is on a junction),
all possible pairs of cells that have the junction edge in common are supplied. if
`dropjunctionpair == false` then one of the possible pairs of cells is not recorded.
This is done to avoid the creation of linearly dependent basis functions in the
construction of boundary element methods for Maxwell's equations.)
"""
function cellpairs(mesh, edges; dropjunctionpair=false)
ndrops = dropjunctionpair ? 1 : 0
@assert dimension(edges)+1 == dimension(mesh)
numedges = numcells(edges)
# perform a dry run to determine the number of cellpairs
v2e, nn = vertextocellmap(mesh)
k = 0
for e in edges
edge = indices(edges, e)
v = edge[1]; n = nn[v]; nbd1 = v2e[v,1:n]
v = edge[2]; n = nn[v]; nbd2 = v2e[v,1:n]
nbd = intersect(nbd1, nbd2)
n = length(nbd)
if n < 3
k += 1
else
k += (n-ndrops)
end
end
facepairs = zeros(Int, 2, k)
k = 1
Cells = cells(mesh)
for e in edges
edge = indices(edges, e)
# neighborhood of startvertex
v = edge[1]
n = nn[v]
nbd1 = v2e[v,1:n]
# neighborhood of endvertex
v = edge[2]
n = nn[v]
nbd2 = v2e[v,1:n]
# get the intersection
nbd = intersect(nbd1, nbd2)
n = length(nbd)
@assert 0 < n
if n == 1 # boundary edge
c = nbd[1]
cell = Cells[c]
s = relorientation(edge, cell)
facepairs[1,k] = c
facepairs[2,k] = -abs(s)
k += 1
elseif n == 2
c = nbd
cell1 = Cells[c[1]] #mesh.faces[c[1]]
cell2 = Cells[c[2]] #mesh.faces[c[2]]
r1 = relorientation(edge, cell1)
r2 = relorientation(edge, cell2)
if r1 > 0 && r2 < 0
c1 = c[2]
c2 = c[1]
else
c1 = c[1]
c2 = c[2]
end
facepairs[1,k] = c1
facepairs[2,k] = c2
k += 1
else
for c in drop(combinations(nbd,2), ndrops)
cell1 = Cells[c[1]] #mesh.faces[c[1]]
cell2 = Cells[c[2]] #mesh.faces[c[2]]
r1 = relorientation(edge, cell1)
r2 = relorientation(edge, cell2)
if r1 > 0 && r2 < 0
c1 = c[2]
c2 = c[1]
else
c1 = c[1]
c2 = c[2]
end
facepairs[1,k] = c1
facepairs[2,k] = c2
k += 1
end
end
end
facepairs
end
"""
chart(mesh, cell) -> cell_chart
Return a chart describing the supplied cell of `mesh`.
"""
chart(mesh::Mesh, cell) = simplex(vertices(mesh, indices(mesh,cell)))
parent(mesh::AbstractMesh) = nothing
# parent(mesh::Mesh) = nothing
"""
isoriented(mesh) -> Bool
Returns true is all cells are consistently oriented, false otherwise.
"""
function isoriented(m::AbstractMesh)
@assert dimension(m) >= 0
edges = skeleton(m, dimension(m)-1)
D = connectivity(edges, m)
S = (abs.(sum(D,dims=1)) .<= 1)
return all(S)
end
"""
True if m1 is a direct refinement of m2.
"""
function refines(m1::AbstractMesh, m2::AbstractMesh)
parent(m1) == nothing && return false
return parent(m1) == m2
end
"""
union(mesh1, mesh2, ...)
Create the topological union of two meshes. This requires them to be
defined on the same vertex set. No geometric considerations are taken
into account.
"""
function Base.union(m1::AbstractMesh, m2::AbstractMesh)
@assert dimension(m1) == dimension(m2)
@assert vertices(m1) == vertices(m2)
Verts = vertices(m1)
Cells1 = cells(m1)
Cells2 = cells(m2)
Mesh(Verts, vcat(Cells1, Cells2))
end
function Base.union(m1::AbstractMesh, ms::Vararg{AbstractMesh})
return union(m1, union(ms...))
end
function breadthfirst(op, mesh, root)
dim = dimension(mesh)
edges = skeleton(mesh, dim-1)
D = connectivity(edges, mesh)
A = D*D'
rows = SparseArrays.rowvals(A)
vals = SparseArrays.nonzeros(A)
discovered = falses(length(mesh))
queue = DataStructures.Queue{typeof(root)}()
facing = DataStructures.Queue{Symbol}()
DataStructures.enqueue!(queue,root)
DataStructures.enqueue!(facing,:up)
discovered[root] = true
while !isempty(queue)
x = DataStructures.dequeue!(queue)
s = DataStructures.dequeue!(facing)
op(mesh, x, s)
for k in SparseArrays.nzrange(A,x)
r = rows[k]
v = vals[k]
r == x && continue
discovered[r] && continue
if v != -1
q = (s == :up) ? :down : :up
else
q = (s == :up) ? :up : :down
end
DataStructures.enqueue!(queue,r)
DataStructures.enqueue!(facing,q)
discovered[r] = true
end
end
@assert all(discovered)
end
function orient(mesh::Mesh)
root = 1
C = collect(cells(mesh))
function f(mesh,i,v)
v == :up && return
cell = C[i]
mesh.faces[i] = fliporientation(cell)
end
breadthfirst(f, mesh, root)
end
import Base.convert
"""
convert(::Type{NewT}, mesh::Mesh{U,D1,T}) where {NewT<:Real,U,D1,T}
Converts a Mesh with coordtype T to a Mesh with coordtype NewT.
"""
function convert(::Type{NewT}, mesh::Mesh{U,D1,T}) where {NewT<:Real,U,D1,T}
vertices=[convert(SVector{U,NewT}, vertex) for vertex in mesh.vertices]
return Mesh(vertices, mesh.faces)
end