| 1 | /* |
| 2 | * Copyright (c) 2008, Carnegie Mellon University |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Code author: Jernej Barbic |
| 6 | * Research: Jernej Barbic, Doug L. James |
| 7 | * Funding: NSF, Link Foundation |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * * Neither the name of Carnegie Mellon University, nor the |
| 17 | * names of its contributors may be used to endorse or promote products |
| 18 | * derived from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND ANY |
| 21 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 23 | * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY |
| 24 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #include "mat3d.h" |
| 33 | #include "eig3.h" |
| 34 | |
| 35 | const Mat3d Mat3d::Identity(1.0); |
| 36 | const Mat3d Mat3d::Zero(0.0); |
| 37 | |
| 38 | // This routine calls a public domain routine (eigen_decomposition), which was |
| 39 | // downloaded from: |
| 40 | // http://barnesc.blogspot.com/2007/02/eigenvectors-of-3x3-symmetric-matrix.html |
| 41 | // According to the header file of eig3.h, the C version in eig3.{h,cpp} |
| 42 | // was obtained by copying code from the public domain Java library JAMA, |
| 43 | // and declaring the resulting C code to also be public domain. |
| 44 | // The eig3.{h,cpp} files are included in this package, intact as they were |
| 45 | // downloaded from the Internet. |
| 46 | |
| 47 | // This routine was written by Jernej Barbic. |
| 48 | void eigen_sym(Mat3d & a, Vec3d & eig_val, Vec3d eig_vec[3]) |
| 49 | { |
| 50 | double A[3][3] = { {a[0][0], a[0][1], a[0][2]}, |
| 51 | {a[1][0], a[1][1], a[1][2]}, |
| 52 | {a[2][0], a[2][1], a[2][2]} }; |
| 53 | double V[3][3]; |
| 54 | double d[3]; |
| 55 | eigen_decomposition(A, V, d); |
| 56 | |
| 57 | eig_val = Vec3d(d[2],d[1],d[0]); |
| 58 | if(eig_vec) |
| 59 | { |
| 60 | eig_vec[0] = Vec3d(V[0][2], V[1][2], V[2][2]); |
| 61 | eig_vec[1] = Vec3d(V[0][1], V[1][1], V[2][1]); |
| 62 | eig_vec[2] = Vec3d(V[0][0], V[1][0], V[2][0]); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | The implementation follows section 5 of |
| 68 | G. Irving, J. Teran, and R. Fedkiw. Invertible Finite Elements for Robust Simulation of Large Deformation. |
| 69 | In Symp. on Computer Animation (SCA), pages 131–140, 2004. |
| 70 | It computes a^T a, and computes its eigenvectors, a^T a = V Sigma^2 V^T . |
| 71 | Sigma is then recovered using sqrt from Sigma^2. |
| 72 | To recover U, compute U = F * V * diag(Sigma^{-1}). |
| 73 | Care must be taken when singular values of Sigma are small (this is |
| 74 | handled in the code). |
| 75 | |
| 76 | We have also added the ability to compute standard SVD (modifiedSVD = 0). |
| 77 | */ |
| 78 | |
| 79 | int SVD(Mat3d & F, Mat3d & U, Vec3d & Sigma, Mat3d & V, double singularValue_eps, int modifiedSVD) |
| 80 | { |
| 81 | // The code handles the following special situations: |
| 82 | |
| 83 | //--------------------------------------------------------- |
| 84 | // 1. det(V) == -1 |
| 85 | // - multiply the first column of V by -1 |
| 86 | //--------------------------------------------------------- |
| 87 | // 2. An entry of Sigma is near zero |
| 88 | //--------------------------------------------------------- |
| 89 | // (if modifiedSVD == 1) : |
| 90 | // 3. negative determinant (Tet is inverted in solid mechanics). |
| 91 | // - check if det(U) == -1 |
| 92 | // - If yes, then negate the minimal element of Sigma |
| 93 | // and the corresponding column of U |
| 94 | //--------------------------------------------------------- |
| 95 | |
| 96 | // form F^T F and do eigendecomposition |
| 97 | Mat3d normalEq = trans(F) * F; |
| 98 | Vec3d eigenValues; |
| 99 | Vec3d eigenVectors[3]; |
| 100 | |
| 101 | eigen_sym(normalEq, eigenValues, eigenVectors); |
| 102 | |
| 103 | V.set(eigenVectors[0][0], eigenVectors[1][0], eigenVectors[2][0], |
| 104 | eigenVectors[0][1], eigenVectors[1][1], eigenVectors[2][1], |
| 105 | eigenVectors[0][2], eigenVectors[1][2], eigenVectors[2][2]); |
| 106 | /* |
| 107 | printf("--- original V ---\n"); |
| 108 | V.print(); |
| 109 | printf("--- eigenValues ---\n"); |
| 110 | printf("%G %G %G\n", eigenValues[0], eigenValues[1], eigenValues[2]); |
| 111 | */ |
| 112 | |
| 113 | // Handle situation: |
| 114 | // 1. det(V) == -1 |
| 115 | // - multiply the first column of V by -1 |
| 116 | if (det(V) < 0.0) |
| 117 | { |
| 118 | // convert V into a rotation (multiply column 1 by -1) |
| 119 | V[0][0] *= -1.0; |
| 120 | V[1][0] *= -1.0; |
| 121 | V[2][0] *= -1.0; |
| 122 | } |
| 123 | |
| 124 | Sigma[0] = (eigenValues[0] > 0.0) ? sqrt(eigenValues[0]) : 0.0; |
| 125 | Sigma[1] = (eigenValues[1] > 0.0) ? sqrt(eigenValues[1]) : 0.0; |
| 126 | Sigma[2] = (eigenValues[2] > 0.0) ? sqrt(eigenValues[2]) : 0.0; |
| 127 | |
| 128 | //printf("--- Sigma ---\n"); |
| 129 | //printf("%G %G %G\n", Sigma[0][0], Sigma[1][1], Sigma[2][2]); |
| 130 | |
| 131 | // compute inverse of singular values |
| 132 | // also check if singular values are close to zero |
| 133 | Vec3d SigmaInverse; |
| 134 | SigmaInverse[0] = (Sigma[0] > singularValue_eps) ? (1.0 / Sigma[0]) : 0.0; |
| 135 | SigmaInverse[1] = (Sigma[1] > singularValue_eps) ? (1.0 / Sigma[1]) : 0.0; |
| 136 | SigmaInverse[2] = (Sigma[2] > singularValue_eps) ? (1.0 / Sigma[2]) : 0.0; |
| 137 | |
| 138 | // compute U using the formula: |
| 139 | // U = F * V * diag(SigmaInverse) |
| 140 | U = F * V; |
| 141 | U.multiplyDiagRight(SigmaInverse); |
| 142 | |
| 143 | // In theory, U is now orthonormal, U^T U = U U^T = I .. it may be a rotation or a reflection, depending on F. |
| 144 | // But in practice, if singular values are small or zero, it may not be orthonormal, so we need to fix it. |
| 145 | // Handle situation: |
| 146 | // 2. An entry of Sigma is near zero |
| 147 | // --------------------------------------------------------- |
| 148 | |
| 149 | /* |
| 150 | printf("--- SigmaInverse ---\n"); |
| 151 | SigmaInverse.print(); |
| 152 | printf(" --- U ---\n"); |
| 153 | U.print(); |
| 154 | */ |
| 155 | |
| 156 | if ((Sigma[0] < singularValue_eps) && (Sigma[1] < singularValue_eps) && (Sigma[2] < singularValue_eps)) |
| 157 | { |
| 158 | // extreme case, all singular values are small, material has collapsed almost to a point |
| 159 | // see [Irving 04], p. 4 |
| 160 | U.set(1.0, 0.0, 0.0, |
| 161 | 0.0, 1.0, 0.0, |
| 162 | 0.0, 0.0, 1.0); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | // handle the case where two singular values are small, but the third one is not |
| 167 | // handle it by computing two (arbitrary) vectors orthogonal to the eigenvector for the large singular value |
| 168 | int done = 0; |
| 169 | for(int dim=0; dim<3; dim++) |
| 170 | { |
| 171 | int dimA = dim; |
| 172 | int dimB = (dim + 1) % 3; |
| 173 | int dimC = (dim + 2) % 3; |
| 174 | if ((Sigma[dimB] < singularValue_eps) && (Sigma[dimC] < singularValue_eps)) |
| 175 | { |
| 176 | // only the column dimA can be trusted, columns dimB and dimC correspond to tiny singular values |
| 177 | Vec3d tmpVec1(U[0][dimA], U[1][dimA], U[2][dimA]); // column dimA |
| 178 | Vec3d tmpVec2; |
| 179 | tmpVec2 = tmpVec1.findOrthonormalVector(); |
| 180 | Vec3d tmpVec3 = norm(cross(tmpVec1, tmpVec2)); |
| 181 | U[0][dimB] = tmpVec2[0]; |
| 182 | U[1][dimB] = tmpVec2[1]; |
| 183 | U[2][dimB] = tmpVec2[2]; |
| 184 | U[0][dimC] = tmpVec3[0]; |
| 185 | U[1][dimC] = tmpVec3[1]; |
| 186 | U[2][dimC] = tmpVec3[2]; |
| 187 | if (det(U) < 0.0) |
| 188 | { |
| 189 | U[0][dimB] *= -1.0; |
| 190 | U[1][dimB] *= -1.0; |
| 191 | U[2][dimB] *= -1.0; |
| 192 | } |
| 193 | done = 1; |
| 194 | break; // out of for |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // handle the case where one singular value is small, but the other two are not |
| 199 | // handle it by computing the cross product of the two eigenvectors for the two large singular values |
| 200 | if (!done) |
| 201 | { |
| 202 | for(int dim=0; dim<3; dim++) |
| 203 | { |
| 204 | int dimA = dim; |
| 205 | int dimB = (dim + 1) % 3; |
| 206 | int dimC = (dim + 2) % 3; |
| 207 | |
| 208 | if (Sigma[dimA] < singularValue_eps) |
| 209 | { |
| 210 | // columns dimB and dimC are both good, but column dimA corresponds to a tiny singular value |
| 211 | Vec3d tmpVec1(U[0][dimB], U[1][dimB], U[2][dimB]); // column dimB |
| 212 | Vec3d tmpVec2(U[0][dimC], U[1][dimC], U[2][dimC]); // column dimC |
| 213 | Vec3d tmpVec3 = norm(cross(tmpVec1, tmpVec2)); |
| 214 | U[0][dimA] = tmpVec3[0]; |
| 215 | U[1][dimA] = tmpVec3[1]; |
| 216 | U[2][dimA] = tmpVec3[2]; |
| 217 | if (det(U) < 0.0) |
| 218 | { |
| 219 | U[0][dimA] *= -1.0; |
| 220 | U[1][dimA] *= -1.0; |
| 221 | U[2][dimA] *= -1.0; |
| 222 | } |
| 223 | done = 1; |
| 224 | break; // out of for |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if ((!done) && (modifiedSVD == 1)) |
| 230 | { |
| 231 | // Handle situation: |
| 232 | // 3. negative determinant (Tet is inverted in solid mechanics) |
| 233 | // - check if det(U) == -1 |
| 234 | // - If yes, then negate the minimal element of Sigma |
| 235 | // and the corresponding column of U |
| 236 | |
| 237 | double detU = det(U); |
| 238 | if (detU < 0.0) |
| 239 | { |
| 240 | // negative determinant |
| 241 | // find the smallest singular value (they are all non-negative) |
| 242 | int smallestSingularValueIndex = 0; |
| 243 | for(int dim=1; dim<3; dim++) |
| 244 | if (Sigma[dim] < Sigma[smallestSingularValueIndex]) |
| 245 | smallestSingularValueIndex = dim; |
| 246 | |
| 247 | // negate the smallest singular value |
| 248 | Sigma[smallestSingularValueIndex] *= -1.0; |
| 249 | U[0][smallestSingularValueIndex] *= -1.0; |
| 250 | U[1][smallestSingularValueIndex] *= -1.0; |
| 251 | U[2][smallestSingularValueIndex] *= -1.0; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | printf("U = \n"); |
| 258 | U.print(); |
| 259 | printf("Sigma = \n"); |
| 260 | Sigma.print(); |
| 261 | printf("V = \n"); |
| 262 | V.print(); |
| 263 | */ |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | // NOTE: This particular routine is not publicly released, as its implementation |
| 269 | // was taken from Numerical Recipes, which is not free software. |
| 270 | // (you can use the eigen_sym routine above which is public domain) |
| 271 | bool eigen_sym_NR(Mat3d & M, Vec3d & eig_val, Vec3d eig_vec[3], int maxIterations, double epsilon) |
| 272 | { |
| 273 | return false; |
| 274 | } |
| 275 |