-
Notifications
You must be signed in to change notification settings - Fork 7
/
V_TetMetric.cpp
1788 lines (1490 loc) · 54.9 KB
/
V_TetMetric.cpp
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
/*=========================================================================
Module: V_TetMetric.cpp
Copyright 2003,2006,2019 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
Under the terms of Contract DE-NA0003525 with NTESS,
the U.S. Government retains certain rights in this software.
See LICENSE for details.
=========================================================================*/
/*
*
* TetMetric.cpp contains quality calculations for Tets
*
* This file is part of VERDICT
*
*/
#include "V_GaussIntegration.hpp"
#include "VerdictVector.hpp"
#include "verdict.h"
#include "verdict_defines.hpp"
#include <algorithm>
#include <cmath> // for std::isnan
namespace VERDICT_NAMESPACE
{
static const double one_third = 1.0 / 3.0;
static const double two_thirds = 2.0 / 3.0;
static const double one_fourth = 1.0 / 4.0;
static const double four_ninths = 4.0 / 9.0;
static const double sqrt2 = std::sqrt(2.0);
static const double sqrt3 = std::sqrt(3.0);
static const double sqrt6 = std::sqrt(6.0);
static const double three_times_1plussqrt3 = 3.0 * (1 + sqrt3);
static const double normal_coeff = 180. * .3183098861837906715377675267450287;
static const double aspect_ratio_normal_coeff = sqrt6 / 12.;
double tet10_characteristic_length(const double coordinates[][3]);
static const int tet10_subtet_conn[12][4] = { { 0, 4, 6, 7 }, { 1, 5, 4, 8 }, { 2, 6, 5, 9 },
{ 3, 8, 7, 9 }, { 4, 8, 5, 10 }, { 5, 8, 9, 10 }, { 9, 8, 7, 10 }, { 7, 8, 4, 10 },
{ 4, 5, 6, 10 }, { 5, 9, 6, 10 }, { 9, 7, 6, 10 }, { 7, 4, 6, 10 } };
static double fix_range(double v)
{
if (std::isnan(v))
{
return VERDICT_DBL_MAX;
}
if (v >= VERDICT_DBL_MAX)
{
return VERDICT_DBL_MAX;
}
if (v <= -VERDICT_DBL_MAX)
{
return -VERDICT_DBL_MAX;
}
return v;
}
double tet_equiangle_skew(int /*num_nodes*/, const double coordinates[][3])
{
VerdictVector ab, ac, bc, bd, ad, cd;
ab.set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
coordinates[1][2] - coordinates[0][2]);
ab.normalize();
ac.set(coordinates[2][0] - coordinates[0][0], coordinates[2][1] - coordinates[0][1],
coordinates[2][2] - coordinates[0][2]);
ac.normalize();
ad.set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
coordinates[3][2] - coordinates[0][2]);
ad.normalize();
bc.set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
coordinates[2][2] - coordinates[1][2]);
bc.normalize();
bd.set(coordinates[3][0] - coordinates[1][0], coordinates[3][1] - coordinates[1][1],
coordinates[3][2] - coordinates[1][2]);
bd.normalize();
cd.set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
coordinates[3][2] - coordinates[2][2]);
cd.normalize();
VerdictVector abc = bc * ab;
abc.normalize();
VerdictVector abd = ab * ad;
abd.normalize();
VerdictVector acd = cd * ad;
acd.normalize();
VerdictVector bcd = bc * cd;
bcd.normalize();
double alpha = std::acos(-(abc % abd));
double beta = std::acos(-(abc % acd));
double gamma = std::acos(-(abc % bcd));
double delta = std::acos(-(abd % acd));
double epsilon = std::acos(-(abd % bcd));
double zeta = std::acos(-(acd % bcd));
double min_angle = alpha;
min_angle = min_angle < beta ? min_angle : beta;
min_angle = min_angle < gamma ? min_angle : gamma;
min_angle = min_angle < delta ? min_angle : delta;
min_angle = min_angle < epsilon ? min_angle : epsilon;
min_angle = min_angle < zeta ? min_angle : zeta;
min_angle *= normal_coeff;
double max_angle = alpha;
max_angle = max_angle > beta ? max_angle : beta;
max_angle = max_angle > gamma ? max_angle : gamma;
max_angle = max_angle > delta ? max_angle : delta;
max_angle = max_angle > epsilon ? max_angle : epsilon;
max_angle = max_angle > zeta ? max_angle : zeta;
max_angle *= normal_coeff;
double theta = std::acos(1 / 3.0) * normal_coeff; // 70.528779365509308630754000660038;
double dihedral_skew_max = (max_angle - theta) / (180 - theta);
double dihedral_skew_min = (theta - min_angle) / theta;
min_angle = 360.0;
max_angle = 0.0;
double angles[12];
angles[0] = std::acos(-(ab % bc));
angles[1] = std::acos((bc % ac));
angles[2] = std::acos((ac % ab));
angles[3] = std::acos(-(ab % bd));
angles[4] = std::acos((bd % ad));
angles[5] = std::acos((ad % ab));
angles[6] = std::acos(-(bc % cd));
angles[7] = std::acos((cd % bd));
angles[8] = std::acos((bd % bc));
angles[9] = std::acos((ad % cd));
angles[10] = std::acos(-(cd % ac));
angles[11] = std::acos((ac % ad));
for (int a = 0; a < 12; a++)
{
if (angles[a] < min_angle)
{
min_angle = angles[a];
}
if (angles[a] > max_angle)
{
max_angle = angles[a];
}
}
max_angle *= normal_coeff;
min_angle *= normal_coeff;
double skew_max = (max_angle - 60.0) / 120.0;
double skew_min = (60.0 - min_angle) / 60.0;
double max_skew = dihedral_skew_min;
max_skew = max_skew > dihedral_skew_max ? max_skew : dihedral_skew_max;
max_skew = max_skew > skew_min ? max_skew : skew_min;
max_skew = max_skew > skew_max ? max_skew : skew_max;
return max_skew;
}
/*!
get the weights based on the average size
of a tet
*/
static int tet_get_weight(
VerdictVector& w1, VerdictVector& w2, VerdictVector& w3, double average_tet_volume)
{
w1.set(1, 0, 0);
w2.set(0.5, 0.5 * sqrt3, 0);
w3.set(0.5, sqrt3 / 6.0, sqrt2 / sqrt3);
double scale = std::pow(6. * average_tet_volume / determinant(w1, w2, w3), 0.3333333333333);
w1 *= scale;
w2 *= scale;
w3 *= scale;
return 1;
}
/*!
the edge ratio of a tet
NB (P. Pebay 01/22/07):
Hmax / Hmin where Hmax and Hmin are respectively the maximum and the
minimum edge lengths
*/
double tet_edge_ratio(int /*num_nodes*/, const double coordinates[][3])
{
VerdictVector a, b, c, d, e, f;
a.set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
coordinates[1][2] - coordinates[0][2]);
b.set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
coordinates[2][2] - coordinates[1][2]);
c.set(coordinates[0][0] - coordinates[2][0], coordinates[0][1] - coordinates[2][1],
coordinates[0][2] - coordinates[2][2]);
d.set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
coordinates[3][2] - coordinates[0][2]);
e.set(coordinates[3][0] - coordinates[1][0], coordinates[3][1] - coordinates[1][1],
coordinates[3][2] - coordinates[1][2]);
f.set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
coordinates[3][2] - coordinates[2][2]);
double a2 = a.length_squared();
double b2 = b.length_squared();
double c2 = c.length_squared();
double d2 = d.length_squared();
double e2 = e.length_squared();
double f2 = f.length_squared();
double m2, M2, mab, mcd, mef, Mab, Mcd, Mef;
if (a2 < b2)
{
mab = a2;
Mab = b2;
}
else // b2 <= a2
{
mab = b2;
Mab = a2;
}
if (c2 < d2)
{
mcd = c2;
Mcd = d2;
}
else // d2 <= c2
{
mcd = d2;
Mcd = c2;
}
if (e2 < f2)
{
mef = e2;
Mef = f2;
}
else // f2 <= e2
{
mef = f2;
Mef = e2;
}
m2 = mab < mcd ? mab : mcd;
m2 = m2 < mef ? m2 : mef;
if (m2 < VERDICT_DBL_MIN)
{
return (double)VERDICT_DBL_MAX;
}
M2 = Mab > Mcd ? Mab : Mcd;
M2 = M2 > Mef ? M2 : Mef;
const double edge_ratio = std::sqrt(M2 / m2);
return fix_range(edge_ratio);
}
/*!
the scaled jacobian of a tet
minimum of the jacobian divided by the lengths of 3 edge vectors
*/
template <typename CoordsContainerType>
double tet_scaled_jacobian_impl(int /*num_nodes*/, const CoordsContainerType coordinates)
{
const VerdictVector side0{coordinates[0], coordinates[1]};
const VerdictVector side1{coordinates[1], coordinates[2]};
const VerdictVector side2{coordinates[2], coordinates[0]};
const VerdictVector side3{coordinates[0], coordinates[3]};
const VerdictVector side4{coordinates[1], coordinates[3]};
const VerdictVector side5{coordinates[2], coordinates[3]};
const double jacobi = side3 % (side2 * side0);
// products of lengths squared of each edge attached to a node.
const double side0_length_squared = side0.length_squared();
const double side1_length_squared = side1.length_squared();
const double side2_length_squared = side2.length_squared();
const double side3_length_squared = side3.length_squared();
const double side4_length_squared = side4.length_squared();
const double side5_length_squared = side5.length_squared();
const double length_squared[4] = {
side0_length_squared * side2_length_squared * side3_length_squared,
side0_length_squared * side1_length_squared * side4_length_squared,
side1_length_squared * side2_length_squared * side5_length_squared,
side3_length_squared * side4_length_squared * side5_length_squared };
int which_node = 0;
if (length_squared[1] > length_squared[which_node])
{
which_node = 1;
}
if (length_squared[2] > length_squared[which_node])
{
which_node = 2;
}
if (length_squared[3] > length_squared[which_node])
{
which_node = 3;
}
double length_product = std::sqrt(length_squared[which_node]);
if (length_product < std::abs(jacobi))
{
length_product = std::abs(jacobi);
}
if (length_product < VERDICT_DBL_MIN)
{
return (double)VERDICT_DBL_MAX;
}
return (double)(sqrt2 * jacobi / length_product);
}
double tet_scaled_jacobian(int num_nodes, const double coordinates[][3])
{
return tet_scaled_jacobian_impl(num_nodes, coordinates);
}
double tet_scaled_jacobian_from_loc_ptrs(int num_nodes, const double * const * coordinates)
{
return tet_scaled_jacobian_impl(num_nodes, coordinates);
}
/*!
The radius ratio of a tet
NB (P. Pebay 04/16/07):
CR / (3.0 * IR) where CR is the circumsphere radius and IR is the inscribed
sphere radius.
Note that this function is similar to the aspect beta of a tet, except that
it does not return VERDICT_DBL_MAX if the element has negative orientation.
*/
double tet_radius_ratio(int /*num_nodes*/, const double coordinates[][3])
{
// Determine side vectors
VerdictVector side[6];
side[0].set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
coordinates[1][2] - coordinates[0][2]);
side[1].set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
coordinates[2][2] - coordinates[1][2]);
side[2].set(coordinates[0][0] - coordinates[2][0], coordinates[0][1] - coordinates[2][1],
coordinates[0][2] - coordinates[2][2]);
side[3].set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
coordinates[3][2] - coordinates[0][2]);
side[4].set(coordinates[3][0] - coordinates[1][0], coordinates[3][1] - coordinates[1][1],
coordinates[3][2] - coordinates[1][2]);
side[5].set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
coordinates[3][2] - coordinates[2][2]);
VerdictVector numerator = side[3].length_squared() * (side[2] * side[0]) +
side[2].length_squared() * (side[3] * side[0]) + side[0].length_squared() * (side[3] * side[2]);
double area_sum;
area_sum = ((side[2] * side[0]).length() + (side[3] * side[0]).length() +
(side[4] * side[1]).length() + (side[3] * side[2]).length()) *
0.5;
double volume = tet_volume(4, coordinates);
if (std::abs(volume) < VERDICT_DBL_MIN)
{
return (double)VERDICT_DBL_MAX;
}
else
{
const double radius_ratio = numerator.length() * area_sum / (108 * volume * volume);
return fix_range(radius_ratio);
}
}
/*!
The aspect ratio of a tet
NB (P. Pebay 01/22/07):
Hmax / (2 sqrt(6) r) where Hmax and r respectively denote the greatest edge
length and the inradius of the tetrahedron
NB (J. Pouderoux 01/27/15)
This will return VERDICT_DBL_MAX when the volume of the tetrahedron is ill-
conditioned. Previously, this would only happen when the volume was small
and positive, but now ill-conditioned inverted tetrahedra are also included.
*/
template <typename CoordsContainerType>
double tet_aspect_ratio_impl(int /*num_nodes*/, const CoordsContainerType coordinates)
{
// Determine side vectors
const VerdictVector ab{coordinates[0], coordinates[1]};
const VerdictVector ac{coordinates[0], coordinates[2]};
const VerdictVector ad{coordinates[0], coordinates[3]};
double detTet = ab % (ac * ad);
if (std::abs(detTet) < VERDICT_DBL_MIN)
{
return (double)VERDICT_DBL_MAX;
}
VerdictVector bc{coordinates[1], coordinates[2]};
VerdictVector bd{coordinates[1], coordinates[3]};
VerdictVector cd{coordinates[2], coordinates[3]};
const double ab2 = ab.length_squared();
const double bc2 = bc.length_squared();
const double ac2 = ac.length_squared();
const double ad2 = ad.length_squared();
const double bd2 = bd.length_squared();
const double cd2 = cd.length_squared();
double A = ab2 > bc2 ? ab2 : bc2;
double B = ac2 > ad2 ? ac2 : ad2;
double C = bd2 > cd2 ? bd2 : cd2;
double D = A > B ? A : B;
const double hm = D > C ? std::sqrt(D) : std::sqrt(C);
bd = ab * bc;
A = bd.length();
bd = ab * ad;
B = bd.length();
bd = ac * ad;
C = bd.length();
bd = bc * cd;
D = bd.length();
const double aspect_ratio = aspect_ratio_normal_coeff * hm * (A + B + C + D) / std::abs(detTet);
return fix_range(aspect_ratio);
}
double tet_aspect_ratio(int num_nodes, const double coordinates[][3])
{
return tet_aspect_ratio_impl(num_nodes, coordinates);
}
double tet_aspect_ratio_from_loc_ptrs(int num_nodes, const double * const *coordinates)
{
return tet_aspect_ratio_impl(num_nodes, coordinates);
}
/*!
the aspect gamma of a tet
srms^3 / (8.48528137423857*V) where srms = sqrt(sum(Si^2)/6), where Si is the edge length
*/
double tet_aspect_gamma(int /*num_nodes*/, const double coordinates[][3])
{
// Determine side vectors
VerdictVector side0, side1, side2, side3, side4, side5;
side0.set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
coordinates[1][2] - coordinates[0][2]);
side1.set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
coordinates[2][2] - coordinates[1][2]);
side2.set(coordinates[0][0] - coordinates[2][0], coordinates[0][1] - coordinates[2][1],
coordinates[0][2] - coordinates[2][2]);
side3.set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
coordinates[3][2] - coordinates[0][2]);
side4.set(coordinates[3][0] - coordinates[1][0], coordinates[3][1] - coordinates[1][1],
coordinates[3][2] - coordinates[1][2]);
side5.set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
coordinates[3][2] - coordinates[2][2]);
double volume = std::abs(tet_volume(4, coordinates));
if (volume < VERDICT_DBL_MIN)
{
return (double)VERDICT_DBL_MAX;
}
else
{
double srms =
std::sqrt((side0.length_squared() + side1.length_squared() + side2.length_squared() +
side3.length_squared() + side4.length_squared() + side5.length_squared()) /
6.0);
double aspect_ratio_gamma = std::pow(srms, 3) / (8.48528137423857 * volume);
return (double)aspect_ratio_gamma;
}
}
/*!
The aspect frobenius of a tet
NB (P. Pebay 01/22/07):
Frobenius condition number when the reference element is regular
*/
double tet_aspect_frobenius(int /*num_nodes*/, const double coordinates[][3])
{
VerdictVector ab, ac, ad;
ab.set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
coordinates[1][2] - coordinates[0][2]);
ac.set(coordinates[2][0] - coordinates[0][0], coordinates[2][1] - coordinates[0][1],
coordinates[2][2] - coordinates[0][2]);
ad.set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
coordinates[3][2] - coordinates[0][2]);
double denominator = ab % (ac * ad);
denominator *= denominator;
denominator *= 2.;
denominator = 3. * std::pow(denominator, one_third);
if (denominator < VERDICT_DBL_MIN)
{
return (double)VERDICT_DBL_MAX;
}
double u[3];
ab.get_xyz(u);
double v[3];
ac.get_xyz(v);
double w[3];
ad.get_xyz(w);
double numerator = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];
numerator += v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
numerator += w[0] * w[0] + w[1] * w[1] + w[2] * w[2];
numerator *= 1.5;
numerator -= v[0] * u[0] + v[1] * u[1] + v[2] * u[2];
numerator -= w[0] * u[0] + w[1] * u[1] + w[2] * u[2];
numerator -= w[0] * v[0] + w[1] * v[1] + w[2] * v[2];
double aspect_frobenius = numerator / denominator;
return fix_range(aspect_frobenius);
}
/*!
The minimum angle of a tet
NB (P. Pebay 01/22/07):
minimum nonoriented dihedral angle
*/
double tet_minimum_angle(int /*num_nodes*/, const double coordinates[][3])
{
// Determine side vectors
VerdictVector ab, bc, ad, cd;
ab.set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
coordinates[1][2] - coordinates[0][2]);
ad.set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
coordinates[3][2] - coordinates[0][2]);
bc.set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
coordinates[2][2] - coordinates[1][2]);
cd.set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
coordinates[3][2] - coordinates[2][2]);
VerdictVector abc = ab * bc;
double nabc = abc.length();
VerdictVector abd = ab * ad;
double nabd = abd.length();
VerdictVector acd = ad * cd;
double nacd = acd.length();
VerdictVector bcd = bc * cd;
double nbcd = bcd.length();
double alpha = std::acos((abc % abd) / (nabc * nabd));
double beta = std::acos((abc % acd) / (nabc * nacd));
double gamma = std::acos((abc % bcd) / (nabc * nbcd));
double delta = std::acos((abd % acd) / (nabd * nacd));
double epsilon = std::acos((abd % bcd) / (nabd * nbcd));
double zeta = std::acos((acd % bcd) / (nacd * nbcd));
alpha = alpha < beta ? alpha : beta;
alpha = alpha < gamma ? alpha : gamma;
alpha = alpha < delta ? alpha : delta;
alpha = alpha < epsilon ? alpha : epsilon;
alpha = alpha < zeta ? alpha : zeta;
alpha *= normal_coeff;
return fix_range(alpha);
}
/*!
The collapse ratio of a tet
NB (J. Pouderoux 01/27/15)
This will return VERDICT_DBL_MAX when the volume of the tetrahedron is ill-
conditioned. Previously, this would only happen when the volume was small
and positive, but now ill-conditioned inverted tetrahedra are also included.
*/
double tet_collapse_ratio(int /*num_nodes*/, const double coordinates[][3])
{
// Determine side vectors
VerdictVector e01, e02, e03, e12, e13, e23;
e01.set(coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1],
coordinates[1][2] - coordinates[0][2]);
e02.set(coordinates[2][0] - coordinates[0][0], coordinates[2][1] - coordinates[0][1],
coordinates[2][2] - coordinates[0][2]);
e03.set(coordinates[3][0] - coordinates[0][0], coordinates[3][1] - coordinates[0][1],
coordinates[3][2] - coordinates[0][2]);
e12.set(coordinates[2][0] - coordinates[1][0], coordinates[2][1] - coordinates[1][1],
coordinates[2][2] - coordinates[1][2]);
e13.set(coordinates[3][0] - coordinates[1][0], coordinates[3][1] - coordinates[1][1],
coordinates[3][2] - coordinates[1][2]);
e23.set(coordinates[3][0] - coordinates[2][0], coordinates[3][1] - coordinates[2][1],
coordinates[3][2] - coordinates[2][2]);
double l[6];
l[0] = e01.length();
l[1] = e02.length();
l[2] = e03.length();
l[3] = e12.length();
l[4] = e13.length();
l[5] = e23.length();
// Find longest edge for each bounding triangle of tetrahedron
double l012 = l[4] > l[0] ? l[4] : l[0];
l012 = l[1] > l012 ? l[1] : l012;
double l031 = l[0] > l[2] ? l[0] : l[2];
l031 = l[3] > l031 ? l[3] : l031;
double l023 = l[2] > l[1] ? l[2] : l[1];
l023 = l[5] > l023 ? l[5] : l023;
double l132 = l[4] > l[3] ? l[4] : l[3];
l132 = l[5] > l132 ? l[5] : l132;
// Compute collapse ratio for each vertex/triangle pair
VerdictVector N;
double h, magN;
double cr;
double crMin;
N = e01 * e02;
magN = N.length();
h = (e03 % N) / magN; // height of vertex 3 above 0-1-2
crMin = h / l012; // ratio of height to longest edge of 0-1-2
N = e03 * e01;
magN = N.length();
h = (e02 % N) / magN; // height of vertex 2 above 0-3-1
cr = h / l031; // ratio of height to longest edge of 0-3-1
if (cr < crMin)
{
crMin = cr;
}
N = e02 * e03;
magN = N.length();
h = (e01 % N) / magN; // height of vertex 1 above 0-2-3
cr = h / l023; // ratio of height to longest edge of 0-2-3
if (cr < crMin)
{
crMin = cr;
}
N = e12 * e13;
magN = N.length();
h = (e01 % N) / magN; // height of vertex 0 above 1-3-2
cr = h / l132; // ratio of height to longest edge of 1-3-2
if (cr < crMin)
{
crMin = cr;
}
return fix_range(crMin);
}
double tet_equivolume_skew(int num_nodes, const double coordinates[][3])
{
//- Find the vectors from the origin to each of the nodes on the tet.
VerdictVector vectA(coordinates[0][0], coordinates[0][1], coordinates[0][2]);
VerdictVector vectB(coordinates[1][0], coordinates[1][1], coordinates[1][2]);
VerdictVector vectC(coordinates[2][0], coordinates[2][1], coordinates[2][2]);
VerdictVector vectD(coordinates[3][0], coordinates[3][1], coordinates[3][2]);
VerdictVector vectAB = vectB - vectA;
VerdictVector vectAC = vectC - vectA;
VerdictVector vectAD = vectD - vectA;
double sq_lengthAB = vectAB.length_squared();
double sq_lengthAC = vectAC.length_squared();
double sq_lengthAD = vectAD.length_squared();
VerdictVector cpBC = vectAB * vectAC;
VerdictVector cpDB = vectAD * vectAB;
VerdictVector cpCD = vectAC * vectAD;
VerdictVector num = sq_lengthAD * cpBC + sq_lengthAC * cpDB + sq_lengthAB * cpCD;
double den = 2 * vectAB % cpCD;
double circumradius = num.length() / den;
double volume = tet_volume(num_nodes, coordinates);
double optimal_length = circumradius / std::sqrt(double(3.0) / 8.0);
double optimal_volume = (1.0 / 12.0) * std::sqrt(double(2.0)) * std::pow(optimal_length, 3);
const double eq_v_skew = (optimal_volume - volume) / optimal_volume;
return fix_range(eq_v_skew);
}
double tet_squish_index(int /*num_nodes*/, const double coordinates[][3])
{
VerdictVector vectA(coordinates[0][0], coordinates[0][1], coordinates[0][2]);
VerdictVector vectB(coordinates[1][0], coordinates[1][1], coordinates[1][2]);
VerdictVector vectC(coordinates[2][0], coordinates[2][1], coordinates[2][2]);
VerdictVector vectD(coordinates[3][0], coordinates[3][1], coordinates[3][2]);
VerdictVector tetCenter = vectA + vectB + vectC + vectD;
tetCenter /= 4.0;
/* top view
C
/|\
/ 5 \
2 / D \ 1
/ 3/ \4 \
/_/ \_\
A-----------B
0
*/
VerdictVector side[6];
side[0].set(vectA, vectB);
side[1].set(vectB, vectC);
side[2].set(vectC, vectA);
side[3].set(vectA, vectD);
side[4].set(vectB, vectD);
side[5].set(vectC, vectD);
double maxSquishIndex = 0;
double squishIndex = 0;
VerdictVector faceCenter;
VerdictVector centerCenterVector;
VerdictVector faceAreaVector;
// face 1
faceCenter = (vectA + vectB + vectD) / 3.0;
centerCenterVector = faceCenter - tetCenter;
faceAreaVector = 0.5 * (side[0] * side[4]);
squishIndex = 1 -
(faceAreaVector % centerCenterVector) / (faceAreaVector.length() * centerCenterVector.length());
if (squishIndex > maxSquishIndex)
{
maxSquishIndex = squishIndex;
}
// face 2
faceCenter = (vectB + vectC + vectD) / 3.0;
centerCenterVector = faceCenter - tetCenter;
faceAreaVector = 0.5 * (side[1] * side[5]);
squishIndex = 1 -
(faceAreaVector % centerCenterVector) / (faceAreaVector.length() * centerCenterVector.length());
if (squishIndex > maxSquishIndex)
{
maxSquishIndex = squishIndex;
}
// face 3
faceCenter = (vectA + vectC + vectD) / 3.0;
centerCenterVector = faceCenter - tetCenter;
faceAreaVector = 0.5 * (side[2] * side[3]);
squishIndex = 1 -
(faceAreaVector % centerCenterVector) / (faceAreaVector.length() * centerCenterVector.length());
if (squishIndex > maxSquishIndex)
{
maxSquishIndex = squishIndex;
}
// face 4
faceCenter = (vectA + vectB + vectC) / 3.0;
centerCenterVector = faceCenter - tetCenter;
faceAreaVector = 0.5 * (side[1] * side[0]);
squishIndex = 1 -
(faceAreaVector % centerCenterVector) / (faceAreaVector.length() * centerCenterVector.length());
if (squishIndex > maxSquishIndex)
{
maxSquishIndex = squishIndex;
}
return maxSquishIndex;
}
static const double TET15_node_local_coord[15][3] = { { 0, 0, 0 }, { 1.0, 0, 0 }, { 0, 1.0, 0 },
{ 0, 0, 1.0 }, { .5, 0, 0 }, { .5, .5, 0 }, { 0, .5, 0 }, { 0, 0, .5 }, { .5, 0, .5 },
{ 0, .5, .5 }, { one_third, one_third, 0 }, { one_third, 0, one_third },
{ one_third, one_third, one_third }, { 0, one_third, one_third },
{ one_fourth, one_fourth, one_fourth } };
static void TET15_gradients_of_the_shape_functions_for_R_S_T(
const double rst[3], double dhdr[15], double dhds[15], double dhdt[15])
{
// dh/dr;
dhdr[0] = -1.0;
dhdr[1] = 1.0;
dhdr[2] = 0.0;
dhdr[3] = 0.0;
dhdr[4] = 4.0 * (1.0 - 2.0 * rst[0] - rst[1] - rst[2]);
dhdr[5] = 4.0 * rst[1];
dhdr[6] = -4.0 * rst[1];
dhdr[7] = -4.0 * rst[2];
dhdr[8] = 4.0 * rst[2];
dhdr[9] = 0.0;
dhdr[11] = 27.0 * (rst[1] - 2.0 * rst[0] * rst[1] - rst[1] * rst[1] - rst[1] * rst[2]);
dhdr[14] = 27.0 * (rst[2] - 2.0 * rst[0] * rst[2] - rst[1] * rst[2] - rst[2] * rst[2]);
dhdr[12] = 27.0 * rst[1] * rst[2];
dhdr[13] = -27.0 * rst[1] * rst[2];
dhdr[10] = 256.0 *
(rst[1] * rst[2] - 2.0 * rst[0] * rst[1] * rst[2] - rst[1] * rst[1] * rst[2] -
rst[1] * rst[2] * rst[2]);
// dh/ds;
dhds[0] = -1.0;
dhds[1] = 0.0;
dhds[2] = 1.0;
dhds[3] = 0.0;
dhds[4] = -4.0 * rst[0];
dhds[5] = 4.0 * rst[0];
dhds[6] = 4.0 * (1.0 - rst[0] - 2.0 * rst[1] - rst[2]);
dhds[7] = -4.0 * rst[2];
dhds[8] = 0.0;
dhds[9] = 4.0 * rst[2];
dhds[11] = 27.0 * (rst[0] - rst[0] * rst[0] - 2.0 * rst[0] * rst[1] - rst[0] * rst[2]);
dhds[14] = -27.0 * rst[0] * rst[2];
dhds[12] = 27.0 * rst[0] * rst[2];
dhds[13] = 27.0 * (rst[2] - rst[0] * rst[2] - 2.0 * rst[1] * rst[2] - rst[2] * rst[2]);
dhds[10] = 256.0 *
(rst[0] * rst[2] - rst[0] * rst[0] * rst[2] - 2.0 * rst[0] * rst[1] * rst[2] -
rst[0] * rst[2] * rst[2]);
// dh/dt;
dhdt[0] = -1.0;
dhdt[1] = 0.0;
dhdt[2] = 0.0;
dhdt[3] = 1.0;
dhdt[4] = -4.0 * rst[0];
dhdt[5] = 0.0;
dhdt[6] = -4.0 * rst[1];
dhdt[7] = 4.0 * (1.0 - rst[0] - rst[1] - 2.0 * rst[2]);
dhdt[8] = 4.0 * rst[0];
dhdt[9] = 4.0 * rst[1];
dhdt[11] = -27.0 * rst[0] * rst[1];
dhdt[14] = 27.0 * (rst[0] - rst[0] * rst[0] - rst[0] * rst[1] - 2.0 * rst[0] * rst[2]);
dhdt[12] = 27.0 * rst[0] * rst[1];
dhdt[13] = 27.0 * (rst[1] - rst[0] * rst[1] - rst[1] * rst[1] - 2.0 * rst[1] * rst[2]);
dhdt[10] = 256.0 *
(rst[0] * rst[1] - rst[0] * rst[0] * rst[1] - rst[0] * rst[1] * rst[1] -
2.0 * rst[0] * rst[1] * rst[2]);
// ----------------------------------------------;
// ADD CONTRIBUTIONS OF NODES 5-15 TO NODES 1-14;
// ----------------------------------------------;
// dh/dr;
dhdr[11] = dhdr[11] - 108.0 * dhdr[10] / 256.0;
dhdr[14] = dhdr[14] - 108.0 * dhdr[10] / 256.0;
dhdr[12] = dhdr[12] - 108.0 * dhdr[10] / 256.0;
dhdr[13] = dhdr[13] - 108.0 * dhdr[10] / 256.0;
dhdr[4] = dhdr[4] - four_ninths * (dhdr[11] + dhdr[14]) - .25 * dhdr[10];
dhdr[5] = dhdr[5] - four_ninths * (dhdr[11] + dhdr[12]) - .25 * dhdr[10];
dhdr[6] = dhdr[6] - four_ninths * (dhdr[11] + dhdr[13]) - .25 * dhdr[10];
dhdr[7] = dhdr[7] - four_ninths * (dhdr[14] + dhdr[13]) - .25 * dhdr[10];
dhdr[8] = dhdr[8] - four_ninths * (dhdr[14] + dhdr[12]) - .25 * dhdr[10];
dhdr[9] = dhdr[9] - four_ninths * (dhdr[12] + dhdr[13]) - .25 * dhdr[10];
dhdr[0] = dhdr[0] - .5 * (dhdr[4] + dhdr[6] + dhdr[7]) -
one_third * (dhdr[11] + dhdr[14] + dhdr[13]) - .25 * dhdr[10];
dhdr[1] = dhdr[1] - .5 * (dhdr[4] + dhdr[5] + dhdr[8]) -
one_third * (dhdr[11] + dhdr[14] + dhdr[12]) - .25 * dhdr[10];
dhdr[2] = dhdr[2] - .5 * (dhdr[5] + dhdr[6] + dhdr[9]) -
one_third * (dhdr[11] + dhdr[12] + dhdr[13]) - .25 * dhdr[10];
dhdr[3] = dhdr[3] - .5 * (dhdr[7] + dhdr[8] + dhdr[9]) -
one_third * (dhdr[14] + dhdr[12] + dhdr[13]) - .25 * dhdr[10];
// dh/ds;
dhds[11] = dhds[11] - 108.0 * dhds[10] / 256.0;
dhds[14] = dhds[14] - 108.0 * dhds[10] / 256.0;
dhds[12] = dhds[12] - 108.0 * dhds[10] / 256.0;
dhds[13] = dhds[13] - 108.0 * dhds[10] / 256.0;
dhds[4] = dhds[4] - four_ninths * (dhds[11] + dhds[14]) - .25 * dhds[10];
dhds[5] = dhds[5] - four_ninths * (dhds[11] + dhds[12]) - .25 * dhds[10];
dhds[6] = dhds[6] - four_ninths * (dhds[11] + dhds[13]) - .25 * dhds[10];
dhds[7] = dhds[7] - four_ninths * (dhds[14] + dhds[13]) - .25 * dhds[10];
dhds[8] = dhds[8] - four_ninths * (dhds[14] + dhds[12]) - .25 * dhds[10];
dhds[9] = dhds[9] - four_ninths * (dhds[12] + dhds[13]) - .25 * dhds[10];
dhds[0] = dhds[0] - .5 * (dhds[4] + dhds[6] + dhds[7]) -
one_third * (dhds[11] + dhds[14] + dhds[13]) - .25 * dhds[10];
dhds[1] = dhds[1] - .5 * (dhds[4] + dhds[5] + dhds[8]) -
one_third * (dhds[11] + dhds[14] + dhds[12]) - .25 * dhds[10];
dhds[2] = dhds[2] - .5 * (dhds[5] + dhds[6] + dhds[9]) -
one_third * (dhds[11] + dhds[12] + dhds[13]) - .25 * dhds[10];
dhds[3] = dhds[3] - .5 * (dhds[7] + dhds[8] + dhds[9]) -
one_third * (dhds[14] + dhds[12] + dhds[13]) - .25 * dhds[10];
// dh/dt;
dhdt[11] = dhdt[11] - 108.0 * dhdt[10] / 256.0;
dhdt[14] = dhdt[14] - 108.0 * dhdt[10] / 256.0;
dhdt[12] = dhdt[12] - 108.0 * dhdt[10] / 256.0;
dhdt[13] = dhdt[13] - 108.0 * dhdt[10] / 256.0;
dhdt[4] = dhdt[4] - four_ninths * (dhdt[11] + dhdt[14]) - .25 * dhdt[10];
dhdt[5] = dhdt[5] - four_ninths * (dhdt[11] + dhdt[12]) - .25 * dhdt[10];
dhdt[6] = dhdt[6] - four_ninths * (dhdt[11] + dhdt[13]) - .25 * dhdt[10];
dhdt[7] = dhdt[7] - four_ninths * (dhdt[14] + dhdt[13]) - .25 * dhdt[10];
dhdt[8] = dhdt[8] - four_ninths * (dhdt[14] + dhdt[12]) - .25 * dhdt[10];
dhdt[9] = dhdt[9] - four_ninths * (dhdt[12] + dhdt[13]) - .25 * dhdt[10];
dhdt[0] = dhdt[0] - .5 * (dhdt[4] + dhdt[6] + dhdt[7]) -
one_third * (dhdt[11] + dhdt[14] + dhdt[13]) - .25 * dhdt[10];
dhdt[1] = dhdt[1] - .5 * (dhdt[4] + dhdt[5] + dhdt[8]) -
one_third * (dhdt[11] + dhdt[14] + dhdt[12]) - .25 * dhdt[10];
dhdt[2] = dhdt[2] - .5 * (dhdt[5] + dhdt[6] + dhdt[9]) -
one_third * (dhdt[11] + dhdt[12] + dhdt[13]) - .25 * dhdt[10];
dhdt[3] = dhdt[3] - .5 * (dhdt[7] + dhdt[8] + dhdt[9]) -
one_third * (dhdt[14] + dhdt[12] + dhdt[13]) - .25 * dhdt[10];
}
double calculate_tet_volume_using_sides(
const VerdictVector& side0, const VerdictVector& side2, const VerdictVector& side3)
{
return (double)((side3 % (side2 * side0)) / 6.0);
}
/*!
the volume of a tet
1/6 * jacobian at a corner node
*/
template <typename CoordsContainerType>
double tet_volume_impl(int num_nodes, const CoordsContainerType coordinates)
{
// Determine side vectors
if (4 == num_nodes)
{
const VerdictVector side2{coordinates[0], coordinates[1]};
const VerdictVector side0{coordinates[0], coordinates[2]};
const VerdictVector side3{coordinates[0], coordinates[3]};
return calculate_tet_volume_using_sides(side0, side2, side3);
}
else
{
VerdictVector tet_pts[15];
VerdictVector side0, side2, side3;
// create a vector for each point
for (int k = 0; k < num_nodes; k++)
{
tet_pts[k].set(coordinates[k][0], coordinates[k][1], coordinates[k][2]);
}
// determine center point of the higher-order nodes
VerdictVector centroid(0.0, 0.0, 0.0);
for (int k = 4; k < num_nodes; k++)
{
centroid += VerdictVector(coordinates[k][0], coordinates[k][1], coordinates[k][2]);
}
centroid /= (num_nodes - 4);
if (8 == num_nodes)
{
double tet_volume = 0;
int tet_face_conn[4][4] = { { 0, 2, 1, 4 }, { 0, 1, 3, 7 }, { 1, 2, 3, 5 }, { 0, 3, 2, 6 } };
for (int i = 0; i < 4; i++)
{
VerdictVector& node0 = tet_pts[tet_face_conn[i][0]];
VerdictVector& node1 = tet_pts[tet_face_conn[i][1]];
VerdictVector& node2 = tet_pts[tet_face_conn[i][2]];