|
21 | 21 | * |
22 | 22 | */ |
23 | 23 |
|
24 | | -// Implementation inspired from https://github.com/sandialabs/verdict |
25 | | - |
26 | 24 | #include <geode/geometry/quality.hpp> |
27 | 25 |
|
28 | 26 | #include <limits> |
29 | 27 |
|
30 | 28 | #include <geode/geometry/basic_objects/tetrahedron.hpp> |
| 29 | +#include <geode/geometry/basic_objects/triangle.hpp> |
31 | 30 | #include <geode/geometry/mensuration.hpp> |
| 31 | +#include <geode/geometry/square_matrix.hpp> |
32 | 32 | #include <geode/geometry/vector.hpp> |
33 | 33 |
|
| 34 | +namespace |
| 35 | +{ |
| 36 | + |
| 37 | + std::array< geode::local_index_t, 3 > lu_decomposition( |
| 38 | + geode::SquareMatrix3D& matrix ) |
| 39 | + { |
| 40 | + std::array< geode::local_index_t, 3 > indices{ 0, 0, 0 }; |
| 41 | + double inverse_biggest[3]; |
| 42 | + for( const auto i : geode::LRange{ 3 } ) |
| 43 | + { |
| 44 | + double biggest{ 0.0 }; |
| 45 | + for( const auto j : geode::LRange{ 3 } ) |
| 46 | + { |
| 47 | + const auto t = std::fabs( matrix.value( i, j ) ); |
| 48 | + if( biggest < t ) |
| 49 | + biggest = t; |
| 50 | + } |
| 51 | + if( biggest == 0.0 ) |
| 52 | + { |
| 53 | + return indices; |
| 54 | + } |
| 55 | + inverse_biggest[i] = 1.0 / biggest; |
| 56 | + indices[i] = i; |
| 57 | + } |
| 58 | + geode::local_index_t pivot_id{ 0 }; |
| 59 | + for( const auto k : geode::LRange{ 2 } ) |
| 60 | + { |
| 61 | + double biggest{ 0.0 }; |
| 62 | + for( const auto i : geode::LRange{ k, 3 } ) |
| 63 | + { |
| 64 | + const double t = std::fabs( matrix.value( indices[i], k ) ) |
| 65 | + * inverse_biggest[indices[i]]; |
| 66 | + if( biggest < t ) |
| 67 | + { |
| 68 | + biggest = t; |
| 69 | + pivot_id = i; |
| 70 | + } |
| 71 | + } |
| 72 | + if( biggest == 0.0 ) |
| 73 | + { |
| 74 | + return indices; |
| 75 | + } |
| 76 | + if( pivot_id != k ) |
| 77 | + { |
| 78 | + const int j = indices[k]; |
| 79 | + indices[k] = indices[pivot_id]; |
| 80 | + indices[pivot_id] = j; |
| 81 | + } |
| 82 | + const auto pivot = matrix.value( indices[k], k ); |
| 83 | + for( const auto i : geode::LRange{ k + 1, 3 } ) |
| 84 | + { |
| 85 | + const double m = matrix.value( indices[i], k ) / pivot; |
| 86 | + matrix.set_value( indices[i], k, m ); |
| 87 | + if( m != 0.0 ) |
| 88 | + { |
| 89 | + for( const auto j : geode::LRange{ k + 1, 3 } ) |
| 90 | + { |
| 91 | + matrix.set_value( indices[i], j, |
| 92 | + matrix.value( indices[i], j ) |
| 93 | + - m * matrix.value( indices[k], j ) ); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return indices; |
| 99 | + } |
| 100 | + std::array< double, 3 > lu_solving( const geode::SquareMatrix3D& lu_matrix, |
| 101 | + const std::array< double, 3 >& rhs, |
| 102 | + const std::array< geode::local_index_t, 3 >& indices ) |
| 103 | + { |
| 104 | + std::array< double, 3 > result{ 0., 0., 0. }; |
| 105 | + for( const auto i : geode::LRange{ 3 } ) |
| 106 | + { |
| 107 | + double s{ 0.0 }; |
| 108 | + for( const auto j : geode::LRange{ 0, i } ) |
| 109 | + { |
| 110 | + s += lu_matrix.value( indices[i], j ) * result[j]; |
| 111 | + } |
| 112 | + result[i] = rhs[indices[i]] - s; |
| 113 | + } |
| 114 | + for( const auto i : |
| 115 | + geode::LReverseRange{ 3 } ) // for( int i = 2; i >= 0; --i ) |
| 116 | + { |
| 117 | + double s{ 0.0 }; |
| 118 | + for( const auto j : geode::LRange{ i + 1, 3 } ) |
| 119 | + { |
| 120 | + s += lu_matrix.value( indices[i], j ) * result[j]; |
| 121 | + } |
| 122 | + result[i] = ( result[i] - s ) / lu_matrix.value( indices[i], i ); |
| 123 | + } |
| 124 | + |
| 125 | + return result; |
| 126 | + } |
| 127 | + |
| 128 | +} // namespace |
| 129 | + |
34 | 130 | namespace geode |
35 | 131 | { |
| 132 | + // Implementation inspired from https://github.com/sandialabs/verdict |
36 | 133 | double tetrahedron_aspect_ratio( const Tetrahedron& tetra ) |
37 | 134 | { |
38 | 135 | const auto& vertices = tetra.vertices(); |
@@ -87,4 +184,57 @@ namespace geode |
87 | 184 | const auto l_rms = std::sqrt( sq_len / 6 ); |
88 | 185 | return 6 * std::sqrt( 2 ) * signed_volume / ( l_rms * l_rms * l_rms ); |
89 | 186 | } |
| 187 | + |
| 188 | + double tetrahedron_collapse_aspect_ratio( const Tetrahedron& tetra ) |
| 189 | + { |
| 190 | + if( geode::tetrahedron_volume( tetra ) < geode::GLOBAL_EPSILON ) |
| 191 | + { |
| 192 | + return std::numeric_limits< double >::max(); |
| 193 | + } |
| 194 | + |
| 195 | + const auto& vertices = tetra.vertices(); |
| 196 | + const Vector3D edge_ab{ vertices[0], vertices[1] }; |
| 197 | + const Vector3D edge_ac{ vertices[0], vertices[2] }; |
| 198 | + const Vector3D edge_ad{ vertices[0], vertices[3] }; |
| 199 | + const Vector3D edge_bc{ vertices[1], vertices[2] }; |
| 200 | + const Vector3D edge_bd{ vertices[1], vertices[3] }; |
| 201 | + const Vector3D edge_cd{ vertices[2], vertices[3] }; |
| 202 | + const auto edge_ab_l2 = edge_ab.length2(); |
| 203 | + const auto edge_bc_l2 = edge_bc.length2(); |
| 204 | + const auto edge_ac_l2 = edge_ac.length2(); |
| 205 | + const auto edge_ad_l2 = edge_ad.length2(); |
| 206 | + const auto edge_bd_l2 = edge_bd.length2(); |
| 207 | + const auto edge_cd_l2 = edge_cd.length2(); |
| 208 | + const auto longest_edge_length = std::sqrt( std::max( { edge_ab_l2, |
| 209 | + edge_bc_l2, edge_ac_l2, edge_ad_l2, edge_bd_l2, edge_cd_l2 } ) ); |
| 210 | + |
| 211 | + geode::SquareMatrix3D matrix{ { edge_ad, edge_bd, edge_cd } }; |
| 212 | + const auto row_indices = lu_decomposition( matrix ); |
| 213 | + |
| 214 | + std::array< Vector3D, 4 > N; |
| 215 | + for( const auto j : geode::LRange{ 3 } ) |
| 216 | + { |
| 217 | + std::array< double, 3 > b{ 0., 0., 0. }; |
| 218 | + b[j] = 1.0; // Positive means the inside direction |
| 219 | + N[j] = Vector3D{ lu_solving( matrix, b, row_indices ) }; |
| 220 | + } |
| 221 | + N[3] = ( N[0] + N[1] + N[2] ) * -1.; |
| 222 | + |
| 223 | + std::array< double, 4 > |
| 224 | + H; // H[i] = inverse of the height of its corresponding face |
| 225 | + for( const auto i : geode::LRange{ 4 } ) |
| 226 | + { |
| 227 | + H[i] = N[i].length(); |
| 228 | + } |
| 229 | + |
| 230 | + auto heightinv = H[0]; |
| 231 | + for( const auto i : geode::LRange{ 1, 4 } ) |
| 232 | + { // Get the biggest H[i] (corresponding to the smallest height) |
| 233 | + if( H[i] > heightinv ) |
| 234 | + { |
| 235 | + heightinv = H[i]; |
| 236 | + } |
| 237 | + } |
| 238 | + return ( longest_edge_length * heightinv ) / std::sqrt( 3. / 2. ); |
| 239 | + } |
90 | 240 | } // namespace geode |
0 commit comments