-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDiscreteExteriorCalculus.jl
2016 lines (1661 loc) · 76.4 KB
/
DiscreteExteriorCalculus.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
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
""" The discrete exterior calculus (DEC) for simplicial sets.
This module provides the dual complex associated with a delta set (the primal
complex), which is a discrete incarnation of Hodge duality, as well as the many
operators of the DEC that depend on it, such as the Hodge star, codifferential,
wedge product, interior product, and Lie derivative. The main reference for this
module is Hirani's 2003 PhD thesis.
"""
module DiscreteExteriorCalculus
export DualSimplex, DualV, DualE, DualTri, DualTet, DualChain, DualForm,
PrimalVectorField, DualVectorField,
AbstractDeltaDualComplex1D, DeltaDualComplex1D, SchDeltaDualComplex1D,
OrientedDeltaDualComplex1D, SchOrientedDeltaDualComplex1D,
EmbeddedDeltaDualComplex1D, SchEmbeddedDeltaDualComplex1D,
AbstractDeltaDualComplex2D, DeltaDualComplex2D, SchDeltaDualComplex2D,
OrientedDeltaDualComplex2D, SchOrientedDeltaDualComplex2D,
EmbeddedDeltaDualComplex2D, SchEmbeddedDeltaDualComplex2D,
AbstractDeltaDualComplex3D, DeltaDualComplex3D, SchDeltaDualComplex3D,
OrientedDeltaDualComplex3D, SchOrientedDeltaDualComplex3D,
EmbeddedDeltaDualComplex3D, SchEmbeddedDeltaDualComplex3D,
DeltaDualComplex, EmbeddedDeltaDualComplex, OrientedDeltaDualComplex,
SimplexCenter, Barycenter, Circumcenter, Incenter, geometric_center,
subsimplices, primal_vertex, elementary_duals, dual_boundary, dual_derivative,
⋆, hodge_star, inv_hodge_star, δ, codifferential, ∇², laplace_beltrami, Δ, laplace_de_rham,
♭, flat, ♭_mat, ♯, ♯_mat, sharp, ∧, wedge_product, interior_product, interior_product_flat,
ℒ, lie_derivative, lie_derivative_flat,
vertex_center, edge_center, triangle_center, tetrahedron_center, dual_tetrahedron_vertices, dual_triangle_vertices, dual_edge_vertices,
dual_point, dual_volume, subdivide_duals!, DiagonalHodge, GeometricHodge,
subdivide, PPSharp, AltPPSharp, DesbrunSharp, LLSDDSharp, de_sign,
DPPFlat, PPFlat,
♭♯, ♭♯_mat, flat_sharp, flat_sharp_mat, dualize,
p2_d2_interpolation
import Base: ndims
import Base: *
import LinearAlgebra: mul!
using LinearAlgebra: Diagonal, dot, norm, cross, pinv, qr, ColumnNorm
using SparseArrays
using StaticArrays: @SVector, SVector, SMatrix, MVector, MMatrix
using Statistics: mean
using GeometryBasics: Point2, Point3
const Point2D = SVector{2,Float64}
const Point3D = SVector{3,Float64}
using ACSets.DenseACSets: attrtype_type
using Catlab, Catlab.CategoricalAlgebra.CSets
using Catlab.CategoricalAlgebra.FinSets: deleteat
using Catlab.CategoricalAlgebra.FunctorialDataMigrations: DeltaMigration, migrate
import Catlab.CategoricalAlgebra.CSets: ∧
import Catlab.Theories: Δ
using DataMigrations: @migrate
using ..ArrayUtils, ..SimplicialSets
using ..SimplicialSets: CayleyMengerDet, operator_nz, ∂_nz, d_nz,
cayley_menger, negate, numeric_sign
import ..SimplicialSets: ∂, d, volume
abstract type DiscreteFlat end
struct DPPFlat <: DiscreteFlat end
struct PPFlat <: DiscreteFlat end
abstract type DiscreteSharp end
struct PPSharp <: DiscreteSharp end
struct AltPPSharp <: DiscreteSharp end
struct DesbrunSharp <: DiscreteSharp end
struct LLSDDSharp <: DiscreteSharp end
abstract type DiscreteHodge end
struct GeometricHodge <: DiscreteHodge end
struct DiagonalHodge <: DiscreteHodge end
# Euclidean geometry
####################
""" A notion of "geometric center" of a simplex.
See also: [`geometric_center`](@ref).
"""
abstract type SimplexCenter end
""" Calculate the center of simplex spanned by given points.
The first argument is a list of points and the second specifies the notion of
"center", via an instance of [`SimplexCenter`](@ref).
"""
function geometric_center end
""" Barycenter, aka centroid, of a simplex.
"""
struct Barycenter <: SimplexCenter end
function geometric_center(points, ::Barycenter)
sum(points) / length(points)
end
""" Circumcenter, or center of circumscribed circle, of a simplex.
The circumcenter is calculated by inverting the Cayley-Menger matrix, as
explained by
[Westdendorp](https://westy31.home.xs4all.nl/Circumsphere/ncircumsphere.htm).
This method of calculation is also used in the package
[AlphaShapes.jl](https://github.com/harveydevereux/AlphaShapes.jl).
"""
struct Circumcenter <: SimplexCenter end
function geometric_center(points, ::Circumcenter)
CM = cayley_menger(points...)
barycentric_coords = inv(CM)[1,2:end]
mapreduce(*, +, barycentric_coords, points)
end
""" Incenter, or center of inscribed circle, of a simplex.
"""
struct Incenter <: SimplexCenter end
function geometric_center(points, ::Incenter)
length(points) > 2 || return geometric_center(points, Barycenter())
face_volumes = map(i -> volume(deleteat(points, i)), eachindex(points))
barycentric_coords = face_volumes / sum(face_volumes)
mapreduce(*, +, barycentric_coords, points)
end
# 1D dual complex
#################
# Should be expressed using a coproduct of two copies of `SchDeltaSet1D`.
@present SchDeltaDualComplex1D <: SchDeltaSet1D begin
# Dual vertices and edges.
(DualV, DualE)::Ob
(D_∂v0, D_∂v1)::Hom(DualE, DualV)
# Centers of primal simplices are dual vertices.
vertex_center::Hom(V, DualV)
edge_center::Hom(E, DualV)
# Every primal edge is subdivided into two dual edges.
#
# (∂v0_dual, ∂v1_dual)::Hom(E,DualE)
#
# ∂v0_dual ⋅ D_∂v1 == ∂v0 ⋅ vertex_center
# ∂v1_dual ⋅ D_∂v1 == ∂v1 ⋅ vertex_center
# ∂v0_dual ⋅ D_∂v0 == edge_center
# ∂v1_dual ⋅ D_∂v0 == edge_center
#
# We could, and arguably should, track these through dedicated morphisms, as
# in the commented code above. We don't because it scales badly in dimension:
# an ``n``-simplex is subdivided into ``n!`` sub-simplices. So we would need 6
# morphisms for triangles and 24 for tets, and an even larger number of
# equations to say how everything fits together. Moreover, in practice, the
# incidence data for the dual complex suffices to construct the dual cells of
# primal simplices.
end
""" Abstract type for dual complex of a 1D delta set.
"""
@abstract_acset_type AbstractDeltaDualComplex1D <: HasDeltaSet1D
""" Dual complex of a one-dimensional delta set.
The data structure includes both the primal complex and the dual complex, as
well as the mapping between them.
"""
@acset_type DeltaDualComplex1D(SchDeltaDualComplex1D,
index=[:∂v0,:∂v1,:D_∂v0,:D_∂v1]) <: AbstractDeltaDualComplex1D
""" Dual vertex corresponding to center of primal vertex.
"""
vertex_center(s::HasDeltaSet, args...) = s[args..., :vertex_center]
""" Dual vertex corresponding to center of primal edge.
"""
edge_center(s::HasDeltaSet1D, args...) = s[args..., :edge_center]
subsimplices(::Type{Val{1}}, s::HasDeltaSet1D, e::Int) =
SVector{2}(incident(s, edge_center(s, e), :D_∂v0))
primal_vertex(::Type{Val{1}}, s::HasDeltaSet1D, e...) = s[e..., :D_∂v1]
elementary_duals(::Type{Val{0}}, s::AbstractDeltaDualComplex1D, v::Int) =
incident(s, vertex_center(s,v), :D_∂v1)
elementary_duals(::Type{Val{1}}, s::AbstractDeltaDualComplex1D, e::Int) =
SVector(edge_center(s,e))
""" Boundary dual vertices of a dual edge.
This accessor assumes that the simplicial identities for the dual hold.
"""
function dual_edge_vertices(s::HasDeltaSet1D, t...)
SVector(s[t..., :D_∂v0],
s[t..., :D_∂v1])
end
""" Boundary dual vertices of a dual triangle.
This accessor assumes that the simplicial identities for the dual hold.
"""
function dual_triangle_vertices(s::HasDeltaSet1D, t...)
SVector(s[s[t..., :D_∂e1], :D_∂v1],
s[s[t..., :D_∂e0], :D_∂v1],
s[s[t..., :D_∂e0], :D_∂v0])
end
# 1D oriented dual complex
#-------------------------
@present SchOrientedDeltaDualComplex1D <: SchDeltaDualComplex1D begin
Orientation::AttrType
edge_orientation::Attr(E, Orientation)
D_edge_orientation::Attr(DualE, Orientation)
end
""" Oriented dual complex of an oriented 1D delta set.
"""
@acset_type OrientedDeltaDualComplex1D(SchOrientedDeltaDualComplex1D,
index=[:∂v0,:∂v1,:D_∂v0,:D_∂v1]) <: AbstractDeltaDualComplex1D
dual_boundary_nz(::Type{Val{1}}, s::AbstractDeltaDualComplex1D, x::Int) =
# Boundary vertices of dual 1-cell ↔
# Dual vertices for cofaces of (i.e. edges incident to) primal vertex.
d_nz(Val{0}, s, x)
dual_derivative_nz(::Type{Val{0}}, s::AbstractDeltaDualComplex1D, x::Int) =
negatenz(∂_nz(Val{1}, s, x))
negatenz((I, V)) = (I, negate.(V))
""" Construct 1D dual complex from 1D delta set.
"""
function (::Type{S})(s::AbstractDeltaSet1D) where S <: AbstractDeltaDualComplex1D
t = S()
copy_primal_1D!(t, s) # TODO: Revert to copy_parts! when performance is improved
make_dual_simplices_1d!(t)
return t
end
function copy_primal_1D!(t::HasDeltaSet1D, s::HasDeltaSet1D)
@assert nv(t) == 0
@assert ne(t) == 0
v_range = add_parts!(t, :V, nv(s))
e_range = add_parts!(t, :E, ne(s))
if has_subpart(s, :point)
@inbounds for v in v_range
t[v, :point] = s[v, :point]
end
end
@inbounds for e in e_range
t[e, :∂v0] = s[e, :∂v0]
t[e, :∂v1] = s[e, :∂v1]
end
if has_subpart(s, :edge_orientation)
@inbounds for e in e_range
t[e, :edge_orientation] = s[e, :edge_orientation]
end
end
end
make_dual_simplices_1d!(s::AbstractDeltaDualComplex1D) = make_dual_simplices_1d!(s, E)
""" Make dual vertices and edges for dual complex of dimension ≧ 1.
Although zero-dimensional duality is geometrically trivial (subdividing a vertex
gives back the same vertex), we treat the dual vertices as disjoint from the
primal vertices. Thus, a dual vertex is created for every primal vertex.
If the primal complex is oriented, an orientation is induced on the dual
complex. The dual edges are oriented relative to the primal edges they subdivide
(Hirani 2003, PhD thesis, Ch. 2, last sentence of Remark 2.5.1).
"""
function make_dual_simplices_1d!(s::HasDeltaSet1D, ::Type{Simplex{n}}) where n
# Make dual vertices and edges.
s[:vertex_center] = vcenters = add_parts!(s, :DualV, nv(s))
s[:edge_center] = ecenters = add_parts!(s, :DualV, ne(s))
D_edges = map((0,1)) do i
add_parts!(s, :DualE, ne(s);
D_∂v0 = ecenters, D_∂v1 = view(vcenters, ∂(1,i,s)))
end
# Orient elementary dual edges.
if has_subpart(s, :edge_orientation)
# If orientations are not set, then set them here.
if any(isnothing, s[:edge_orientation])
# 1-simplices only need to be orientable if the delta set is 1D.
# (The 1-simplices in a 2D delta set need not represent a valid 1-Manifold.)
if n == 1
orient!(s, E) || error("The 1-simplices of the given 1D delta set are non-orientable.")
else
s[findall(isnothing, s[:edge_orientation]), :edge_orientation] = zero(attrtype_type(s, :Orientation))
end
end
edge_orient = s[:edge_orientation]
s[D_edges[1], :D_edge_orientation] = negate.(edge_orient)
s[D_edges[2], :D_edge_orientation] = edge_orient
end
D_edges
end
# 1D embedded dual complex
#-------------------------
@present SchEmbeddedDeltaDualComplex1D <: SchOrientedDeltaDualComplex1D begin
(Real, Point)::AttrType
point::Attr(V, Point)
length::Attr(E, Real)
dual_point::Attr(DualV, Point)
dual_length::Attr(DualE, Real)
end
""" Embedded dual complex of an embedded 1D delta set.
Although they are redundant information, the lengths of the primal and dual
edges are precomputed and stored.
"""
@acset_type EmbeddedDeltaDualComplex1D(SchEmbeddedDeltaDualComplex1D,
index=[:∂v0,:∂v1,:D_∂v0,:D_∂v1]) <: AbstractDeltaDualComplex1D
""" Point associated with dual vertex of complex.
"""
dual_point(s::HasDeltaSet, args...) = s[args..., :dual_point]
struct PrecomputedVol end
volume(::Type{Val{n}}, s::EmbeddedDeltaDualComplex1D, x) where n =
volume(Val{n}, s, x, PrecomputedVol())
dual_volume(::Type{Val{n}}, s::EmbeddedDeltaDualComplex1D, x) where n =
dual_volume(Val{n}, s, x, PrecomputedVol())
volume(::Type{Val{1}}, s::HasDeltaSet1D, e, ::PrecomputedVol) = s[e, :length]
dual_volume(::Type{Val{1}}, s::HasDeltaSet1D, e, ::PrecomputedVol) =
s[e, :dual_length]
dual_volume(::Type{Val{1}}, s::HasDeltaSet1D, e::Int, ::CayleyMengerDet) =
volume(dual_point(s, SVector(s[e,:D_∂v0], s[e,:D_∂v1])))
hodge_diag(::Type{Val{0}}, s::AbstractDeltaDualComplex1D, v::Int) =
sum(dual_volume(Val{1}, s, elementary_duals(Val{0},s,v)))
hodge_diag(::Type{Val{1}}, s::AbstractDeltaDualComplex1D, e::Int) =
1 / volume(Val{1},s,e)
""" Compute geometric subdivision for embedded dual complex.
Supports different methods of subdivision through the choice of geometric
center, as defined by [`geometric_center`](@ref). In particular, barycentric
subdivision and circumcentric subdivision are supported.
"""
function subdivide_duals!(sd::EmbeddedDeltaDualComplex1D{_o, _l, point_type} where {_o, _l}, alg) where point_type
subdivide_duals_1d!(sd, point_type, alg)
precompute_volumes_1d!(sd, point_type)
end
# TODO: Replace the individual accesses with vector accesses
function subdivide_duals_1d!(sd::HasDeltaSet1D, ::Type{point_type}, alg) where point_type
point_arr = MVector{2, point_type}(undef)
@inbounds for v in vertices(sd)
sd[v, :dual_point] = sd[v, :point]
end
@inbounds for e in edges(sd)
p1, p2 = edge_vertices(sd, e)
point_arr[1] = sd[p1, :point]
point_arr[2] = sd[p2, :point]
sd[sd[e, :edge_center], :dual_point] = geometric_center(point_arr, alg)
end
end
# TODO: Replace the individual accesses with vector accesses
function precompute_volumes_1d!(sd::HasDeltaSet1D, ::Type{point_type}) where point_type
point_arr = MVector{2, point_type}(undef)
@inbounds for e in edges(sd)
p1, p2 = edge_vertices(sd, e)
point_arr[1] = sd[p1, :point]
point_arr[2] = sd[p2, :point]
sd[e, :length] = volume(point_arr)
end
@inbounds for e in parts(sd, :DualE)
p1, p2 = dual_edge_vertices(sd, e)
point_arr[1] = sd[p1, :dual_point]
point_arr[2] = sd[p2, :dual_point]
sd[e, :dual_length] = volume(point_arr)
end
end
# TODO: Orientation on subdivisions
# 2D dual complex
#################
# Should be expressed using a coproduct of two copies of `SchDeltaSet2D` or
# perhaps a pushout of `SchDeltaDualComplex2D` and `SchDeltaSet1D`.
@present SchDeltaDualComplex2D <: SchDeltaSet2D begin
# Dual vertices, edges, and triangles.
(DualV, DualE, DualTri)::Ob
(D_∂v0, D_∂v1)::Hom(DualE, DualV)
(D_∂e0, D_∂e1, D_∂e2)::Hom(DualTri, DualE)
# Simplicial identities for dual simplices.
D_∂e1 ⋅ D_∂v1 == D_∂e2 ⋅ D_∂v1
D_∂e0 ⋅ D_∂v1 == D_∂e2 ⋅ D_∂v0
D_∂e0 ⋅ D_∂v0 == D_∂e1 ⋅ D_∂v0
# Centers of primal simplices are dual vertices.
vertex_center::Hom(V, DualV)
edge_center::Hom(E, DualV)
tri_center::Hom(Tri, DualV)
end
""" Abstract type for dual complex of a 2D delta set.
"""
@abstract_acset_type AbstractDeltaDualComplex2D <: HasDeltaSet2D
""" Dual complex of a two-dimensional delta set.
"""
@acset_type DeltaDualComplex2D(SchDeltaDualComplex2D,
index=[:∂v0,:∂v1,:∂e0,:∂e1,:∂e2,:D_∂v0,:D_∂v1,:D_∂e0,:D_∂e1,:D_∂e2]) <: AbstractDeltaDualComplex2D
""" Dual vertex corresponding to center of primal triangle.
"""
triangle_center(s::HasDeltaSet2D, args...) = s[args..., :tri_center]
subsimplices(::Type{Val{2}}, s::HasDeltaSet2D, t::Int) =
SVector{6}(incident(s, triangle_center(s,t), @SVector [:D_∂e1, :D_∂v0]))
primal_vertex(::Type{Val{2}}, s::HasDeltaSet2D, t...) =
primal_vertex(Val{1}, s, s[t..., :D_∂e2])
elementary_duals(::Type{Val{0}}, s::AbstractDeltaDualComplex2D, v::Int) =
incident(s, vertex_center(s,v), @SVector [:D_∂e1, :D_∂v1])
elementary_duals(::Type{Val{1}}, s::AbstractDeltaDualComplex2D, e::Int) =
incident(s, edge_center(s,e), :D_∂v1)
elementary_duals(::Type{Val{2}}, s::AbstractDeltaDualComplex2D, t::Int) =
SVector(triangle_center(s,t))
# 2D oriented dual complex
#-------------------------
@present SchOrientedDeltaDualComplex2D <: SchDeltaDualComplex2D begin
Orientation::AttrType
edge_orientation::Attr(E, Orientation)
tri_orientation::Attr(Tri, Orientation)
D_edge_orientation::Attr(DualE, Orientation)
D_tri_orientation::Attr(DualTri, Orientation)
end
""" Oriented dual complex of an oriented 2D delta set.
"""
@acset_type OrientedDeltaDualComplex2D(SchOrientedDeltaDualComplex2D,
index=[:∂v0,:∂v1,:∂e0,:∂e1,:∂e2,:D_∂v0,:D_∂v1,:D_∂e0,:D_∂e1,:D_∂e2]) <: AbstractDeltaDualComplex2D
dual_boundary_nz(::Type{Val{1}}, s::AbstractDeltaDualComplex2D, x::Int) =
# Boundary vertices of dual 1-cell ↔
# Dual vertices for cofaces of (triangles incident to) primal edge.
negatenz(d_nz(Val{1}, s, x))
dual_boundary_nz(::Type{Val{2}}, s::AbstractDeltaDualComplex2D, x::Int) =
# Boundary edges of dual 2-cell ↔
# Dual edges for cofaces of (edges incident to) primal vertex.
d_nz(Val{0}, s, x)
dual_derivative_nz(::Type{Val{0}}, s::AbstractDeltaDualComplex2D, x::Int) =
∂_nz(Val{2}, s, x)
dual_derivative_nz(::Type{Val{1}}, s::AbstractDeltaDualComplex2D, x::Int) =
negatenz(∂_nz(Val{1}, s, x))
""" Construct 2D dual complex from 2D delta set.
"""
function (::Type{S})(s::AbstractDeltaSet2D) where S <: AbstractDeltaDualComplex2D
t = S()
copy_primal_2D!(t, s) # TODO: Revert to copy_parts! when performance is improved
make_dual_simplices_2d!(t)
return t
end
function copy_primal_2D!(t::HasDeltaSet2D, s::HasDeltaSet2D)
@assert ntriangles(t) == 0
copy_primal_1D!(t, s)
tri_range = add_parts!(t, :Tri, ntriangles(s))
@inbounds for tri in tri_range
t[tri, :∂e0] = s[tri, :∂e0]
t[tri, :∂e1] = s[tri, :∂e1]
t[tri, :∂e2] = s[tri, :∂e2]
end
if has_subpart(s, :tri_orientation)
@inbounds for tri in tri_range
t[tri, :tri_orientation] = s[tri, :tri_orientation]
end
end
end
make_dual_simplices_1d!(s::AbstractDeltaDualComplex2D) = make_dual_simplices_1d!(s, Tri)
make_dual_simplices_2d!(s::AbstractDeltaDualComplex2D) = make_dual_simplices_2d!(s, Tri)
""" Make dual simplices for dual complex of dimension ≧ 2.
If the primal complex is oriented, an orientation is induced on the dual
complex. The elementary dual edges are oriented following (Hirani, 2003, Example
2.5.2) or (Desbrun et al, 2005, Table 1) and the dual triangles are oriented
relative to the primal triangles they subdivide.
"""
function make_dual_simplices_2d!(s::HasDeltaSet2D, ::Type{Simplex{n}}) where n
# Make dual vertices and edges.
D_edges01 = make_dual_simplices_1d!(s)
s[:tri_center] = tri_centers = add_parts!(s, :DualV, ntriangles(s))
D_edges12 = map((0,1,2)) do e
add_parts!(s, :DualE, ntriangles(s);
D_∂v0=tri_centers, D_∂v1=edge_center(s, ∂(2,e,s)))
end
D_edges02 = map(triangle_vertices(s)) do vs
add_parts!(s, :DualE, ntriangles(s);
D_∂v0=tri_centers, D_∂v1=vertex_center(s, vs))
end
# Make dual triangles.
# Counterclockwise order in drawing with vertices 0, 1, 2 from left to right.
D_triangle_schemas = ((0,1,1),(0,2,1),(1,2,0),(1,0,1),(2,0,0),(2,1,0))
D_triangles = map(D_triangle_schemas) do (v,e,ev)
add_parts!(s, :DualTri, ntriangles(s);
D_∂e0=D_edges12[e+1], D_∂e1=D_edges02[v+1],
D_∂e2=view(D_edges01[ev+1], ∂(2,e,s)))
end
if has_subpart(s, :tri_orientation)
# If orientations are not set, then set them here.
if any(isnothing, s[:tri_orientation])
# 2-simplices only need to be orientable if the delta set is 2D.
# (The 2-simplices in a 3D delta set need not represent a valid 2-Manifold.)
if n == 2
orient!(s, Tri) || error("The 2-simplices of the given 2D delta set are non-orientable.")
else
s[findall(isnothing, s[:tri_orientation]), :tri_orientation] = zero(attrtype_type(s, :Orientation))
end
end
# Orient elementary dual triangles.
tri_orient = s[:tri_orientation]
rev_tri_orient = negate.(tri_orient)
for (i, D_tris) in enumerate(D_triangles)
s[D_tris, :D_tri_orientation] = isodd(i) ? rev_tri_orient : tri_orient
end
# Orient elementary dual edges.
for e in (0,1,2)
s[D_edges12[e+1], :D_edge_orientation] = relative_sign.(
s[∂(2,e,s), :edge_orientation],
isodd(e) ? rev_tri_orient : tri_orient)
end
# Remaining dual edges are oriented arbitrarily.
s[lazy(vcat, D_edges02...), :D_edge_orientation] = one(attrtype_type(s, :Orientation))
end
D_triangles
end
relative_sign(x, y) = sign(x*y)
relative_sign(x::Bool, y::Bool) = (x && y) || (!x && !y)
# 2D embedded dual complex
#-------------------------
@present SchEmbeddedDeltaDualComplex2D <: SchOrientedDeltaDualComplex2D begin
(Real, Point)::AttrType
point::Attr(V, Point)
length::Attr(E, Real)
area::Attr(Tri, Real)
dual_point::Attr(DualV, Point)
dual_length::Attr(DualE, Real)
dual_area::Attr(DualTri, Real)
end
""" Embedded dual complex of an embedded 2D delta set.
Although they are redundant information, the lengths and areas of the
primal/dual edges and triangles are precomputed and stored.
"""
@acset_type EmbeddedDeltaDualComplex2D(SchEmbeddedDeltaDualComplex2D,
index=[:∂v0,:∂v1,:∂e0,:∂e1,:∂e2,:D_∂v0,:D_∂v1,:D_∂e0,:D_∂e1,:D_∂e2]) <: AbstractDeltaDualComplex2D
volume(::Type{Val{n}}, s::EmbeddedDeltaDualComplex2D, x) where n =
volume(Val{n}, s, x, PrecomputedVol())
dual_volume(::Type{Val{n}}, s::EmbeddedDeltaDualComplex2D, x) where n =
dual_volume(Val{n}, s, x, PrecomputedVol())
volume(::Type{Val{2}}, s::HasDeltaSet2D, t, ::PrecomputedVol) = s[t, :area]
dual_volume(::Type{Val{2}}, s::HasDeltaSet2D, t, ::PrecomputedVol) =
s[t, :dual_area]
function dual_volume(::Type{Val{2}}, s::HasDeltaSet2D, t::Int, ::CayleyMengerDet)
dual_vs = SVector(s[s[t, :D_∂e1], :D_∂v1],
s[s[t, :D_∂e2], :D_∂v0],
s[s[t, :D_∂e0], :D_∂v0])
volume(dual_point(s, dual_vs))
end
hodge_diag(::Type{Val{0}}, s::AbstractDeltaDualComplex2D, v::Int) =
sum(dual_volume(Val{2}, s, elementary_duals(Val{0},s,v)))
hodge_diag(::Type{Val{1}}, s::AbstractDeltaDualComplex2D, e::Int) =
sum(dual_volume(Val{1}, s, elementary_duals(Val{1},s,e))) / volume(Val{1},s,e)
hodge_diag(::Type{Val{2}}, s::AbstractDeltaDualComplex2D, t::Int) =
1 / volume(Val{2},s,t) * sign(2,s,t)
function ♭(s::AbstractDeltaDualComplex2D, X::AbstractVector, ::DPPFlat)
# XXX: Creating this lookup table shouldn't be necessary. Of course, we could
# index `tri_center` but that shouldn't be necessary either. Rather, we should
# loop over incident triangles instead of the elementary duals, which just
# happens to be inconvenient.
tri_map = Dict{Int,Int}(triangle_center(s,t) => t for t in triangles(s))
# TODO: Remove these comments before merging.
# For each primal edge:
map(edges(s)) do e
# Get the vector from src to tgt (oriented correctly).
e_vec = (point(s, tgt(s,e)) - point(s, src(s,e))) * sign(1,s,e)
# Grab all the dual edges of this primal edge.
dual_edges = elementary_duals(1,s,e)
# And the corresponding lengths.
dual_lengths = dual_volume(1, s, dual_edges)
# For each of these dual edges:
mapreduce(+, dual_edges, dual_lengths) do dual_e, dual_length
# Get the vector at the center of the triangle this edge is pointing at.
X_vec = X[tri_map[s[dual_e, :D_∂v0]]]
# Take their dot product and multiply by the length of this dual edge.
dual_length * dot(X_vec, e_vec)
# When done, sum these weights up and divide by the total length.
end / sum(dual_lengths)
end
end
♭_mat(s::AbstractDeltaDualComplex2D, f::DPPFlat) =
♭_mat(s, ∂(2,s), f)
function ♭_mat(s::AbstractDeltaDualComplex2D, p2s, ::DPPFlat)
mat_type = SMatrix{1, length(eltype(s[:point])), eltype(eltype(s[:point])), length(eltype(s[:point]))}
♭_mat = spzeros(mat_type, ne(s), ntriangles(s))
for e in edges(s)
# The vector associated with this primal edge.
e_vec = (point(s, tgt(s,e)) - point(s, src(s,e))) * sign(1,s,e)
# The triangles associated with this primal edge.
tris = p2s[e,:].nzind
# The dual vertex at the center of this primal edge.
center = edge_center(s, e)
# The centers of the triangles associated with this primal edge.
dvs = triangle_center(s, tris)
# The dual edge pointing to each triangle.
des = map(dvs) do dv
# (This is the edges(s,src,tgt) function.)
only(de for de in incident(s, dv, :D_∂v0) if s[de, :D_∂v1] == center)
end
# The lengths of those dual edges.
dels = volume(s, DualE(des))
# The sum of the lengths of the dual edges at each primal edge.
dels_sum = sum(dels)
for (tri, del) in zip(tris, dels)
♭_mat[e, tri] = del * mat_type(e_vec) / dels_sum
end
end
♭_mat
end
function ♭(s::AbstractDeltaDualComplex2D, X::AbstractVector, ::PPFlat)
map(edges(s)) do e
vs = edge_vertices(s,e)
l_vec = mean(X[vs])
e_vec = (point(s, tgt(s,e)) - point(s, src(s,e))) * sign(1,s,e)
dot(l_vec, e_vec)
end
end
function ♭_mat(s::AbstractDeltaDualComplex2D, ::PPFlat)
mat_type = SMatrix{1, length(eltype(s[:point])), eltype(eltype(s[:point])), length(eltype(s[:point]))}
♭_mat = spzeros(mat_type, ne(s), nv(s))
for e in edges(s)
e_vec = (point(s, tgt(s,e)) - point(s, src(s,e))) * sign(1,s,e)
vs = edge_vertices(s,e)
♭_mat[e, vs[1]] = 0.5 * mat_type(e_vec)
♭_mat[e, vs[2]] = 0.5 * mat_type(e_vec)
end
♭_mat
end
function ♯(s::AbstractDeltaDualComplex2D, α::AbstractVector, DS::DiscreteSharp)
α♯ = zeros(attrtype_type(s, :Point), nv(s))
for t in triangles(s)
tri_center, tri_edges = triangle_center(s,t), triangle_edges(s,t)
tri_point = dual_point(s, tri_center)
for (i, (v₀, e₀)) in enumerate(zip(triangle_vertices(s,t), tri_edges))
e_vec = point(s, tgt(s, e₀)) - point(s, src(s, e₀))
e_vec /= norm(e_vec)
e2_vec = point(s, v₀) - point(s, src(s, e₀))
out_vec = e2_vec - dot(e2_vec, e_vec)*e_vec
h = norm(out_vec)
out_vec /= h^2 # length == 1/h
for e in deleteat(tri_edges, i)
v, sgn = src(s,e) == v₀ ? (tgt(s,e), -1) : (src(s,e), +1)
dual_area = sum(dual_volume(2,s,d) for d in elementary_duals(0,s,v)
if s[s[d, :D_∂e0], :D_∂v0] == tri_center)
area = ♯_denominator(s, v, t, DS)
α♯[v] += sgn * sign(1,s,e) * α[e] * (dual_area / area) * out_vec
end
end
end
α♯
end
function ♯(s::AbstractDeltaDualComplex2D, α::AbstractVector, ::LLSDDSharp)
♯_m = ♯_mat(s, LLSDDSharp())
♯_m * α
end
""" Divided weighted normals by | σⁿ | .
This weighting is that used in equation 5.8.1 from Hirani.
See Hirani §5.8.
"""
♯_denominator(s::AbstractDeltaDualComplex2D, _::Int, t::Int, ::DiscreteSharp) =
volume(2,s,t)
""" Divided weighted normals by | ⋆v | .
This weighting is NOT that of equation 5.8.1, but a different weighting scheme.
We essentially replace the denominator in equation 5.8.1 with | ⋆v | . This
may be what Hirani intended, and perhaps the denominator | σⁿ | in that equation
is either a mistake or clerical error.
See Hirani §5.8.
"""
♯_denominator(s::AbstractDeltaDualComplex2D, v::Int, _::Int, ::AltPPSharp) =
sum(dual_volume(2,s, elementary_duals(0,s,v)))
""" function get_orthogonal_vector(s::AbstractDeltaDualComplex2D, v::Int, e::Int)
Find a vector orthogonal to e pointing into the triangle shared with v.
"""
function get_orthogonal_vector(s::AbstractDeltaDualComplex2D, v::Int, e::Int)
e_vec = point(s, tgt(s, e)) - point(s, src(s, e))
e_vec /= norm(e_vec)
e2_vec = point(s, v) - point(s, src(s, e))
e2_vec - dot(e2_vec, e_vec)*e_vec
end
function ♯_assign!(♯_mat::AbstractSparseMatrix, s::AbstractDeltaDualComplex2D,
v₀::Int, _::Int, t::Int, i::Int, tri_edges::SVector{3, Int}, tri_center::Int,
out_vec, DS::DiscreteSharp)
for e in deleteat(tri_edges, i)
v, sgn = src(s,e) == v₀ ? (tgt(s,e), -1) : (src(s,e), +1)
# | ⋆vₓ ∩ σⁿ |
dual_area = sum(dual_volume(2,s,d) for d in elementary_duals(0,s,v)
if s[s[d, :D_∂e0], :D_∂v0] == tri_center)
area = ♯_denominator(s, v, t, DS)
♯_mat[v,e] += sgn * sign(1,s,e) * (dual_area / area) * out_vec
end
end
function ♯_assign!(♯_mat::AbstractSparseMatrix, s::AbstractDeltaDualComplex2D,
_::Int, e₀::Int, t::Int, _::Int, _::SVector{3, Int}, tri_center::Int,
out_vec, DS::DesbrunSharp)
for v in edge_vertices(s, e₀)
sgn = v == tgt(s,e₀) ? -1 : +1
# | ⋆vₓ ∩ σⁿ |
dual_area = sum(dual_volume(2,s,d) for d in elementary_duals(0,s,v)
if s[s[d, :D_∂e0], :D_∂v0] == tri_center)
area = ♯_denominator(s, v, t, DS)
♯_mat[v,e₀] += sgn * sign(1,s,e₀) * (dual_area / area) * out_vec
end
end
""" function ♯_mat(s::AbstractDeltaDualComplex2D, DS::DiscreteSharp)
Sharpen a 1-form into a vector field.
3 primal-primal methods are supported. See [`♯_denominator`](@ref) for the distinction between Hirani's method and and an "Alternative" method. Desbrun's definition is selected with `DesbrunSharp`, and is like Hirani's, save for dividing by the norm twice.
A dual-dual method which uses linear least squares to estimate a vector field is selected with `LLSDDSharp`.
"""
function ♯_mat(s::AbstractDeltaDualComplex2D, DS::DiscreteSharp)
♯_mat = spzeros(attrtype_type(s, :Point), (nv(s), ne(s)))
for t in triangles(s)
tri_center, tri_edges = triangle_center(s,t), triangle_edges(s,t)
for (i, (v₀, e₀)) in enumerate(zip(triangle_vertices(s,t), tri_edges))
out_vec = get_orthogonal_vector(s, v₀, e₀)
h = norm(out_vec)
out_vec /= DS == DesbrunSharp() ? h : h^2
♯_assign!(♯_mat, s, v₀, e₀, t, i, tri_edges, tri_center, out_vec, DS)
end
end
♯_mat
end
de_sign(s,de) = s[de, :D_edge_orientation] ? +1 : -1
""" function ♯_mat(s::AbstractDeltaDualComplex2D, ::LLSDDSharp)
Sharpen a dual 1-form into a DualVectorField, using linear least squares.
Up to floating point error, this method perfectly produces fields which are constant over any triangle in the domain. Assume that the contribution of each half-edge to the value stored on the entire dual edge is proportional to their lengths. Since this least squares method does not perform pre-normalization, the contribution of each half-edge value is proportional to its length on the given triangle. Satisfying the continuous exterior calculus, sharpened vectors are constrained to lie on their triangle (i.e. they are indeed tangent).
It is not known whether this method has been exploited previously in the DEC literature, or defined in code elsewhere.
"""
function ♯_mat(s::AbstractDeltaDualComplex2D, ::LLSDDSharp)
# TODO: Grab point information out of s at the type level.
pt = attrtype_type(s, :Point)
♯_m = spzeros(SVector{length(pt), eltype(pt)},
findnz(d(1,s))[[1,2]]...)
for t in triangles(s)
tri_center, tri_edges = triangle_center(s,t), sort(triangle_edges(s,t))
# | ⋆eₓ ∩ σⁿ |
star_e_cap_t = map(tri_edges) do e
only(filter(elementary_duals(1,s,e)) do de
s[de, :D_∂v0] == tri_center
end)
end
de_vecs = map(star_e_cap_t) do de
de_sign(s,de) *
(dual_point(s,s[de, :D_∂v0]) - dual_point(s,s[de, :D_∂v1]))
end
weights = s[star_e_cap_t, :dual_length] ./
map(tri_edges) do e
sum(s[elementary_duals(1,s,e), :dual_length])
end
# TODO: Move around ' as appropriate to minimize transposing.
X = stack(de_vecs)'
# See: https://arxiv.org/abs/1102.1845
#QRX = qr(X, ColumnNorm())
#LLS = (inv(QRX.R) * QRX.Q')[QRX.p,:]
#LLS = pinv(X'*(X))*(X')
LLS = pinv(X)
for (i,e) in enumerate(tri_edges)
♯_m[t, e] = LLS[:,i]'*weights[i]
end
end
♯_m
end
function ∧(::Type{Tuple{1,1}}, s::HasDeltaSet2D, α, β, x::Int)
# XXX: This calculation of the volume coefficients is awkward due to the
# design decision described in `SchDeltaDualComplex1D`.
dual_vs = vertex_center(s, triangle_vertices(s, x))
dual_es = sort(SVector{6}(incident(s, triangle_center(s, x), :D_∂v0)),
by=e -> s[e,:D_∂v1] .== dual_vs, rev=true)[1:3]
coeffs = map(dual_es) do e
sum(dual_volume(2, s, SVector{2}(incident(s, e, :D_∂e1))))
end / volume(2, s, x)
# Wedge product of two primal 1-forms, as in (Hirani 2003, Example 7.1.2).
# This formula is not the same as (Hirani 2003, Equation 7.1.2) but it is
# equivalent.
e0, e1, e2 = ∂(2,0,s,x), ∂(2,1,s,x), ∂(2,2,s,x)
dot(coeffs, SVector(α[e2] * β[e1] - α[e1] * β[e2],
α[e2] * β[e0] - α[e0] * β[e2],
α[e1] * β[e0] - α[e0] * β[e1])) / 2
end
function subdivide_duals!(sd::EmbeddedDeltaDualComplex2D{_o, _l, point_type} where {_o, _l}, alg) where point_type
subdivide_duals_2d!(sd, point_type, alg)
precompute_volumes_2d!(sd, point_type)
end
# TODO: Replace the individual accesses with vector accesses
function subdivide_duals_2d!(sd::HasDeltaSet2D, ::Type{point_type}, alg) where point_type
subdivide_duals_1d!(sd, point_type, alg)
point_arr = MVector{3, point_type}(undef)
@inbounds for t in triangles(sd)
p1, p2, p3 = triangle_vertices(sd, t)
point_arr[1] = sd[p1, :point]
point_arr[2] = sd[p2, :point]
point_arr[3] = sd[p3, :point]
sd[sd[t, :tri_center], :dual_point] = geometric_center(point_arr, alg)
end
end
function precompute_volumes_2d!(sd::HasDeltaSet2D, p::Type{point_type}) where point_type
precompute_volumes_1d!(sd, point_type)
set_volumes_2d!(Val{2}, sd, p)
set_dual_volumes_2d!(Val{2}, sd, p)
end
# TODO: Replace the individual accesses with vector accesses
function set_volumes_2d!(::Type{Val{2}}, sd::HasDeltaSet2D, ::Type{point_type}) where point_type
point_arr = MVector{3, point_type}(undef)
@inbounds for t in triangles(sd)
p1, p2, p3 = triangle_vertices(sd, t)
point_arr[1] = sd[p1, :point]
point_arr[2] = sd[p2, :point]
point_arr[3] = sd[p3, :point]
sd[t, :area] = volume(point_arr)
end
end
# TODO: Replace the individual accesses with vector accesses
function set_dual_volumes_2d!(::Type{Val{2}}, sd::HasDeltaSet2D, ::Type{point_type}) where point_type
point_arr = MVector{3, point_type}(undef)
@inbounds for t in parts(sd, :DualTri)
p1, p2, p3 = dual_triangle_vertices(sd, t)
point_arr[1] = sd[p1, :dual_point]
point_arr[2] = sd[p2, :dual_point]
point_arr[3] = sd[p3, :dual_point]
sd[t, :dual_area] = volume(point_arr)
end
end
# 3D dual complex
#################
# Should be expressed using a coproduct of two copies of `SchDeltaSet3D`...
@present SchDeltaDualComplex3D <: SchDeltaSet3D begin
# Dual vertices, edges, triangles, and tetrahedra.
(DualV, DualE, DualTri, DualTet)::Ob
(D_∂v0, D_∂v1)::Hom(DualE, DualV)
(D_∂e0, D_∂e1, D_∂e2)::Hom(DualTri, DualE)
(D_∂t0, D_∂t1, D_∂t2, D_∂t3)::Hom(DualTet, DualTri)
# Simplicial identities for dual simplices.
D_∂t3 ⋅ D_∂e2 == D_∂t2 ⋅ D_∂e2
D_∂t3 ⋅ D_∂e1 == D_∂t1 ⋅ D_∂e2
D_∂t3 ⋅ D_∂e0 == D_∂t0 ⋅ D_∂e2
D_∂t2 ⋅ D_∂e1 == D_∂t1 ⋅ D_∂e1
D_∂t2 ⋅ D_∂e0 == D_∂t0 ⋅ D_∂e1
D_∂t1 ⋅ D_∂e0 == D_∂t0 ⋅ D_∂e0
# Centers of primal simplices are dual vertices.
vertex_center::Hom(V, DualV)
edge_center::Hom(E, DualV)
tri_center::Hom(Tri, DualV)
tet_center::Hom(Tet, DualV)
end
""" Abstract type for dual complex of a 3D delta set.
"""
@abstract_acset_type AbstractDeltaDualComplex3D <: HasDeltaSet3D
const AbstractDeltaDualComplex = Union{AbstractDeltaDualComplex1D, AbstractDeltaDualComplex2D, AbstractDeltaDualComplex3D}
""" Dual complex of a three-dimensional delta set.
"""
@acset_type DeltaDualComplex3D(SchDeltaDualComplex3D,
index=[:∂v0,:∂v1,:∂e0,:∂e1,:∂e2,:D_∂v0,:D_∂v1,:D_∂e0,:D_∂e1,:D_∂e2,:D_∂t0,:D_∂t1,:D_∂t2,:D_∂t3]) <: AbstractDeltaDualComplex3D
""" Dual vertex corresponding to center of primal tetrahedron.
"""
tetrahedron_center(s::HasDeltaSet3D, args...) = s[args..., :tet_center]
subsimplices(::Type{Val{3}}, s::HasDeltaSet3D, tet::Int) =
SVector{24}(incident(s, tetrahedron_center(s,tet), @SVector [:D_∂t1, :D_∂e1, :D_∂v0]))
primal_vertex(::Type{Val{3}}, s::HasDeltaSet3D, tet...) =
primal_vertex(Val{2}, s, s[tet..., :D_∂t1])
elementary_duals(::Type{Val{0}}, s::AbstractDeltaDualComplex3D, v::Int) =
incident(s, vertex_center(s,v), @SVector [:D_∂t1, :D_∂e1, :D_∂v1])
elementary_duals(::Type{Val{1}}, s::AbstractDeltaDualComplex3D, e::Int) =
incident(s, edge_center(s,e), @SVector [:D_∂e1, :D_∂v1])
elementary_duals(::Type{Val{2}}, s::AbstractDeltaDualComplex3D, t::Int) =
incident(s, triangle_center(s,t), :D_∂v1)
elementary_duals(::Type{Val{3}}, s::AbstractDeltaDualComplex3D, tet::Int) =
SVector(tetrahedron_center(s,tet))
""" Boundary dual vertices of a dual tetrahedron.
This accessor assumes that the simplicial identities for the dual hold.
"""
function dual_tetrahedron_vertices(s::HasDeltaSet3D, t...)
SVector(s[s[s[t..., :D_∂t2], :D_∂e2], :D_∂v1],
s[s[s[t..., :D_∂t2], :D_∂e2], :D_∂v0],
s[s[s[t..., :D_∂t0], :D_∂e0], :D_∂v1],
s[s[s[t..., :D_∂t0], :D_∂e0], :D_∂v0])
end
# 3D oriented dual complex
#-------------------------