![]() |
ITK
4.2.0
Insight Segmentation and Registration Toolkit
|
00001 /*========================================================================= 00002 * 00003 * Copyright Insight Software Consortium 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0.txt 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 * 00017 *=========================================================================*/ 00018 #ifndef __VNLIterativeSparseSolverTraits_h 00019 #define __VNLIterativeSparseSolverTraits_h 00020 00021 #include "vnl/vnl_vector.h" 00022 #include "vnl/vnl_sparse_matrix.h" 00023 #include "vnl/vnl_sparse_matrix_linear_system.h" 00024 #include "vnl/algo/vnl_lsqr.h" 00025 00026 template< typename T = double > 00027 class VNLIterativeSparseSolverTraits 00028 { 00029 public: 00030 typedef T ValueType; 00031 typedef vnl_sparse_matrix< ValueType > MatrixType; 00032 typedef vnl_vector< ValueType > VectorType; 00033 typedef vnl_lsqr SolverType; 00034 00035 VNLIterativeSparseSolverTraits() {} 00036 ~VNLIterativeSparseSolverTraits() {} 00037 00038 bool IsDirectSolver() const 00039 { 00040 return false; 00041 } 00042 00043 MatrixType InitializeSparseMatrix(const unsigned int & iN) const 00044 { 00045 return MatrixType(iN, iN); 00046 } 00047 00048 MatrixType InitializeSparseMatrix(const unsigned int & iRow, const unsigned int& iCol) const 00049 { 00050 return MatrixType(iRow, iCol); 00051 } 00052 00053 VectorType InitializeVector(const unsigned int & iN) const 00054 { 00055 return VectorType(iN); 00056 } 00057 00058 void FillMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV) const 00059 { 00060 iA(iR, iC) = iV; 00061 } 00062 00063 void AddToMatrix(MatrixType & iA, const unsigned int & iR, const unsigned int & iC, const ValueType & iV) const 00064 { 00065 iA(iR, iC) += iV; 00066 } 00067 00068 bool Solve(const MatrixType & iA, const VectorType & iB, VectorType & oX) const 00069 { 00070 typedef vnl_sparse_matrix_linear_system< ValueType > SparseLinearSystemType; 00071 SparseLinearSystemType system(iA, iB); 00072 00073 SolverType solver(system); 00074 return solver.minimize(oX); 00075 } 00076 00077 bool Solve(const MatrixType & iA, 00078 const VectorType & iBx, const VectorType & iBy, const VectorType & iBz, 00079 VectorType & oX, VectorType & oY, VectorType & oZ ) const 00080 { 00081 bool result1 = Solve(iA, iBx, 100000, oX); 00082 bool result2 = Solve(iA, iBy, 100000, oY); 00083 bool result3 = Solve(iA, iBz, 100000, oZ); 00084 00085 return ( result1 && result2 && result3 ); 00086 } 00087 00088 // no interest to use this method... 00089 bool Solve(const MatrixType & iA, 00090 const VectorType & iBx, const VectorType & iBy, 00091 VectorType & oX, VectorType & oY) const 00092 { 00093 bool result1 = Solve(iA, iBx, oX); 00094 bool result2 = Solve(iA, iBy, oY); 00095 00096 return ( result1 && result2 ); 00097 } 00098 00099 bool Solve(const MatrixType & iA, 00100 const VectorType & iB, 00101 const long & iNbIter, 00102 VectorType & oX) const 00103 { 00104 typedef vnl_sparse_matrix_linear_system< ValueType > SparseLinearSystemType; 00105 SparseLinearSystemType system(iA, iB); 00106 00107 SolverType solver(system); 00108 solver.set_max_iterations(iNbIter); 00109 return solver.minimize(oX); 00110 } 00111 }; 00112 00113 #endif 00114
1.7.6.1