| 146 | #endif |
| 147 | |
| 148 | Basis Basis::diagonalize() { |
| 149 | #ifdef MATH_CHECKS |
| 150 | ERR_FAIL_COND_V(!is_symmetric(), Basis()); |
| 151 | #endif |
| 152 | const int ite_max = 1024; |
| 153 | |
| 154 | real_t off_matrix_norm_2 = rows[0][1] * rows[0][1] + rows[0][2] * rows[0][2] + rows[1][2] * rows[1][2]; |
| 155 | |
| 156 | int ite = 0; |
| 157 | Basis acc_rot; |
| 158 | while (off_matrix_norm_2 > (real_t)CMP_EPSILON2 && ite++ < ite_max) { |
| 159 | real_t el01_2 = rows[0][1] * rows[0][1]; |
| 160 | real_t el02_2 = rows[0][2] * rows[0][2]; |
| 161 | real_t el12_2 = rows[1][2] * rows[1][2]; |
| 162 | // Find the pivot element |
| 163 | int i, j; |
| 164 | if (el01_2 > el02_2) { |
| 165 | if (el12_2 > el01_2) { |
| 166 | i = 1; |
| 167 | j = 2; |
| 168 | } else { |
| 169 | i = 0; |
| 170 | j = 1; |
| 171 | } |
| 172 | } else { |
| 173 | if (el12_2 > el02_2) { |
| 174 | i = 1; |
| 175 | j = 2; |
| 176 | } else { |
| 177 | i = 0; |
| 178 | j = 2; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Compute the rotation angle |
| 183 | real_t angle; |
| 184 | if (Math::is_equal_approx(rows[j][j], rows[i][i])) { |
| 185 | angle = Math::PI / 4; |
| 186 | } else { |
| 187 | angle = 0.5f * Math::atan(2 * rows[i][j] / (rows[j][j] - rows[i][i])); |
| 188 | } |
| 189 | |
| 190 | // Compute the rotation matrix |
| 191 | Basis rot; |
| 192 | rot.rows[i][i] = rot.rows[j][j] = Math::cos(angle); |
| 193 | rot.rows[i][j] = -(rot.rows[j][i] = Math::sin(angle)); |
| 194 | |
| 195 | // Update the off matrix norm |
| 196 | off_matrix_norm_2 -= rows[i][j] * rows[i][j]; |
| 197 | |
| 198 | // Apply the rotation |
| 199 | *this = rot * *this * rot.transposed(); |
| 200 | acc_rot = rot * acc_rot; |
| 201 | } |
| 202 | |
| 203 | return acc_rot; |
| 204 | } |
| 205 |
no test coverage detected