23 #include <vnl/vnl_matrix.h> 24 #include <vnl/algo/vnl_symmetric_eigensystem.h> 25 #include <vnl/algo/vnl_svd.h> 37 utlAssert(in.columns()==3 || in.rows()==3,
"wrong dimension");
38 vnl_matrix<T> out(in);
41 for (
int i = 0; i < in.rows(); i += 1 )
42 utl::spherical2Cartesian(in(i,0), in(i,1), in(i,2), out(i,0), out(i,1), out(i,2));
46 for (
int i = 0; i < in.columns(); i += 1 )
47 utl::spherical2Cartesian(in(0,i), in(1,i), in(2,i), out(0,i), out(1,i), out(2,i));
56 utlAssert(in.columns()==3 || in.rows()==3,
"wrong dimension");
57 vnl_matrix<T> out(in);
60 for (
int i = 0; i < in.rows(); i += 1 )
61 utl::cartesian2Spherical(in(i,0), in(i,1), in(i,2), out(i,0), out(i,1), out(i,2));
65 for (
int i = 0; i < in.columns(); i += 1 )
66 utl::cartesian2Spherical(in(0,i), in(1,i), in(2,i), out(0,i), out(1,i), out(2,i));
76 std::vector<T> vec(matrix.rows());
77 vnl_matrix<T> result(matrix);
78 for (
int i = 0; i < matrix.columns(); i += 1 )
80 for (
int j = 0; j < matrix.rows(); j += 1 )
83 for (
int j = 0; j < matrix.rows(); j += 1 )
91 ConnectVnlMatrix (
const vnl_matrix<T>& m1,
const vnl_matrix<T>& m2,
const bool isConnectRow )
95 utlException(m1.columns()!=m2.columns(),
"wrong column size! m1.columns()="<<m1.columns()<<
", m2.columns()"<<m2.columns());
96 vnl_matrix<T> result(m1.rows()+m2.rows(), m1.columns());
97 int m1Rows = m1.rows();
98 for (
int j = 0; j < m1.columns(); j += 1 )
100 for (
int i = 0; i < m1.rows(); i += 1 )
101 result(i,j) = m1(i,j);
102 for (
int i = 0; i < m2.rows(); i += 1 )
103 result(i+m1Rows,j) = m2(i,j);
109 utlException(m1.rows()!=m2.rows(),
"wrong row size! m1.rows()="<<m1.rows()<<
", m2.rows()"<<m2.rows());
110 vnl_matrix<T> result(m1.rows(), m1.columns()+m2.columns());
111 int m1Columns = m1.columns();
112 for (
int i = 0; i < m1.rows(); i += 1 )
114 for (
int j = 0; j < m1.columns(); j += 1 )
115 result(i,j) = m1(i,j);
116 for (
int j = 0; j < m2.columns(); j += 1 )
117 result(i,j+m1Columns) = m2(i,j);
127 vnl_vector<T> result(m1.size()+m2.size());
128 int m1Size = m1.size();
129 for (
int i = 0; i < m1Size; i += 1 )
131 for (
int i = 0; i < m2.size(); i += 1 )
132 result[i+m1Size] = m2[i];
140 vnl_vector<T> result(vec);
141 for (
int i = 0; i < vec.size(); i += 1 )
142 result[i] = std::fabs(vec[i]);
150 vnl_vector<T> vec(values);
151 const unsigned int N = vec.size();
152 std::nth_element(vec.begin(),vec.begin()+N/2,vec.end());
175 utlException(grad.rows()<2 || grad.columns()!=3,
"grad has wrong size!");
179 for(
unsigned int i = 1; i < grad.rows(); i++)
181 double dot = grad(0,0)*grad(i,0) + grad(0,1)*grad(i,1) + grad(0,2)*grad(i,2);
183 if(dot >= -1.0 -
M_EPS && dot <= -1.0 +
M_EPS)
185 else if(dot <= 1.0 + M_EPS && dot >= 1.0 -
M_EPS)
188 dot = 180*std::acos(dot)/
M_PI;
201 utlException(mat.rows()!=mat.columns(),
"wrong size! mat.rows()="<<mat.rows()<<
", mat.columns()="<<mat.columns());
202 typedef vnl_symmetric_eigensystem< T > SymEigenSystemType;
203 SymEigenSystemType eig (mat);
204 unsigned n = eig.D.rows();
205 vnl_diag_matrix<T> invD(eig.D);
206 for (
unsigned i=0; i<n; ++i)
208 if ( eig.D(i,i)>eps || eig.D(i,i)<-eps )
209 invD(i,i) = 1.0/eig.D(i,i);
213 return eig.V * invD * eig.V.transpose();
222 svd.zero_out_absolute(eps);
223 return svd.pinverse();
230 int NumberRows = matrix.rows(), NumberColumns=matrix.columns();
231 utl::SaveMatrix<vnl_matrix<T> >(matrix, NumberRows, NumberColumns, file);
237 PrintVnlMatrixStats (
const vnl_matrix<T>& matrix,
const std::string& str=
"",
const char* separate=
" ", std::ostream& os=std::cout )
239 int NumberRows = matrix.rows(), NumberColumns=matrix.columns();
240 utl::PrintMatrixStats<vnl_matrix<T> >(matrix, NumberRows, NumberColumns, str, separate, os);
245 PrintVnlMatrix (
const vnl_matrix<T>& matrix,
const std::string& str=
"",
const char* separate=
" ", std::ostream& os=std::cout )
247 int NumberRows = matrix.rows(), NumberColumns=matrix.columns();
248 utl::PrintMatrix<vnl_matrix<T> >(matrix, NumberRows, NumberColumns, str, separate, os);
253 PrintVnlVector (
const vnl_vector<T>& vec,
const std::string& str=
"",
const char* separate=
" ", std::ostream& os=std::cout)
255 int NSize = vec.size();
263 vnl_vector<T> result(n);
264 for (
int i = 0; i < n; i += 1 )
265 result[i] = vec[i+colstart];
273 vnl_vector<T> result(index.size());
274 for (
int i = 0; i < index.size(); i += 1 )
275 result[i] = vec[index[i]];
283 utlException(subvec.size()+colstart>vec.size(),
"wrong size! subvec.size()="<<subvec.size() <<
", vec.size()="<<vec.size() );
284 for (
int i = 0; i < subvec.size(); i += 1 )
285 vec[i+colstart] = subvec[i];
290 SetValuesVnlVector (
const vnl_vector<T>& subvec, vnl_vector<T>& vec,
const std::vector<int>& index )
292 utlException(subvec.size()!=index.size(),
"wrong size!");
293 for (
int i = 0; i < index.size(); i += 1 )
294 vec[index[i]] = subvec[i];
302 vnl_matrix<T> result(index.size(), mat.columns());
303 for (
int i = 0; i < index.size(); i += 1 )
304 for (
int j = 0; j < mat.columns(); j += 1 )
305 result(i, j) = mat(index[i], j);
313 utlException(index.size()>mat.columns(),
"wrong size!");
314 vnl_matrix<T> result(mat.rows(), index.size());
315 for (
int i = 0; i < mat.rows(); i += 1 )
316 for (
int j = 0; j < index.size(); j += 1 )
317 result(i, j) = mat(i, index[j]);
323 SetRowsVnlMatrix (
const vnl_matrix<T>& submat, vnl_matrix<T>& mat,
const std::vector<int>& index )
325 utlException(submat.columns()!=mat.columns(),
"wrong size!");
326 utlException(submat.rows()!=index.size(),
"wrong size!");
327 for (
int i = 0; i < index.size(); i += 1 )
328 for (
int j = 0; j < mat.columns(); j += 1 )
329 mat(index[i], j) = submat(i,j);
337 utlException(submat.columns()!=index.size(),
"wrong size!");
338 for (
int i = 0; i < mat.rows(); i += 1 )
339 for (
int j = 0; j < index.size(); j += 1 )
340 mat(i, index[j]) = submat(i,j);
347 std::vector<T> v(vec.size());
348 for (
int i = 0; i < vec.size(); i += 1 )
357 vnl_vector<T> v(vec.size());
358 for (
int i = 0; i < vec.size(); i += 1 )
367 vnl_matrix<T> mat(vec.size(), vec.size());
369 mat.set_diagonal(vec);
377 if (mat.rows()!=mat.columns())
379 for (
int i = 1; i < mat.rows(); i += 1 )
381 for (
int j = 0; j < i; j += 1 )
383 if ( std::fabs(mat(i,j)- mat(j,i))>eps )
void PrintContainer(IteratorType v1, IteratorType v2, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout, bool showStats=true)
void PrintVnlVector(const vnl_vector< T > &vec, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
vnl_matrix< T > GetDiagonalMatrix(const vnl_vector< T > &vec)
std::vector< double > GetContainerStats(IteratorType v1, IteratorType v2)
void PrintVnlMatrix(const vnl_matrix< T > &matrix, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)
#define utlException(cond, expout)
void spherical2Cartesian(const T r, const T theta, const T phi, T &x, T &y, T &z)
void SetColumnsVnlMatrix(const vnl_matrix< T > &submat, vnl_matrix< T > &mat, const std::vector< int > &index)
vnl_matrix< T > GetVnlSymmericMatrixPInverse(const vnl_matrix< T > &mat, const double eps=1e-8)
void SetRowsVnlMatrix(const vnl_matrix< T > &submat, vnl_matrix< T > &mat, const std::vector< int > &index)
vnl_matrix< T > GetVnlMatrixPInverse(const vnl_matrix< T > &mat, const double eps=1e-8)
NDArray< T, 2 > CartesianToSpherical(const NDArray< T, 2 > &in)
bool IsMatrixSymmetric(const vnl_matrix< T > &mat, const double eps=1e-10)
vnl_matrix< T > GetRowsVnlMatrix(const vnl_matrix< T > &mat, const std::vector< int > &index)
std::vector< T > GetVnlVectorStats(const vnl_vector< T > &values)
double GetMinAngle(const vnl_matrix< T > &grad)
NDArray< T, 2 > SphericalToCartesian(const NDArray< T, 2 > &in)
T GetMedianVnlVector(const vnl_vector< T > &values)
#define utlAssert(cond, expout)
std::vector< T > GetVnlMatrixStats(const vnl_matrix< T > &values)
VectorType NormalizeMinMax(const VectorType &v, const int nSize)
vnl_matrix< T > GetColumnsVnlMatrix(const vnl_matrix< T > &mat, const std::vector< int > &index)
vnl_vector< T > StdVectorToVnlVector(const std::vector< T > &vec)
vnl_vector< T > GetValuesVnlVector(const vnl_vector< T > &vec, const int colstart, const int n)
void cartesian2Spherical(const T x, const T y, const T z, T &r, T &theta, T &phi)
void SaveVnlMatrix(const vnl_matrix< T > &matrix, const std::string &file)
vnl_vector< T > ConnectVnlVector(const vnl_vector< T > &m1, const vnl_vector< T > &m2)
std::vector< T > VnlVectorToStdVector(const vnl_vector< T > &vec)
vnl_vector< T > GetAbsoluteVnlVector(const vnl_vector< T > &vec)
void SetValuesVnlVector(const vnl_vector< T > &subvec, vnl_vector< T > &vec, const int colstart)
vnl_matrix< T > ConnectVnlMatrix(const vnl_matrix< T > &m1, const vnl_matrix< T > &m2, const bool isConnectRow)
void PrintVnlMatrixStats(const vnl_matrix< T > &matrix, const std::string &str="", const char *separate=" ", std::ostream &os=std::cout)