writeDMAT.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "writeDMAT.h"
  9. #include <cstdio>
  10. #ifndef IGL_NO_EIGEN
  11. # include <Eigen/Core>
  12. #endif
  13. template <class Mat>
  14. IGL_INLINE bool igl::writeDMAT(
  15. const std::string file_name,
  16. const Mat & W,
  17. const bool ascii)
  18. {
  19. FILE * fp = fopen(file_name.c_str(),"w");
  20. if(fp == NULL)
  21. {
  22. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  23. return false;
  24. }
  25. if(ascii)
  26. {
  27. // first line contains number of rows and number of columns
  28. fprintf(fp,"%d %d\n",(int)W.cols(),(int)W.rows());
  29. // Loop over columns slowly
  30. for(int j = 0;j < W.cols();j++)
  31. {
  32. // loop over rows (down columns) quickly
  33. for(int i = 0;i < W.rows();i++)
  34. {
  35. fprintf(fp,"%0.17lg\n",(double)W(i,j));
  36. }
  37. }
  38. }else
  39. {
  40. // write header for ascii
  41. fprintf(fp,"0 0\n");
  42. // first line contains number of rows and number of columns
  43. fprintf(fp,"%d %d\n",(int)W.cols(),(int)W.rows());
  44. Eigen::MatrixXd Wd = W.template cast<double>();
  45. fwrite(Wd.data(),sizeof(double),Wd.size(),fp);
  46. //// Loop over columns slowly
  47. //for(int j = 0;j < W.cols();j++)
  48. //{
  49. // // loop over rows (down columns) quickly
  50. // for(int i = 0;i < W.rows();i++)
  51. // {
  52. // double d = (double)W(i,j);
  53. // fwrite(&d,sizeof(double),1,fp);
  54. // }
  55. //}
  56. }
  57. fclose(fp);
  58. return true;
  59. }
  60. template <typename Scalar>
  61. IGL_INLINE bool igl::writeDMAT(
  62. const std::string file_name,
  63. const std::vector<std::vector<Scalar> > W)
  64. {
  65. FILE * fp = fopen(file_name.c_str(),"w");
  66. if(fp == NULL)
  67. {
  68. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  69. return false;
  70. }
  71. int num_rows = (int)W.size();
  72. int num_cols = 0;
  73. if(num_rows > 0)
  74. {
  75. num_cols = W[0].size();
  76. }
  77. // first line contains number of columns and number of rows
  78. fprintf(fp,"%d %d\n",num_cols,num_rows);
  79. // Loop over columns slowly
  80. for(int j = 0;j < num_cols;j++)
  81. {
  82. // loop over rows (down columns) quickly
  83. for(int i = 0;i < num_rows;i++)
  84. {
  85. // better be rectangular
  86. assert((int)W[i].size() > j);
  87. fprintf(fp,"%0.15lf\n",(double)W[i][j]);
  88. }
  89. }
  90. fclose(fp);
  91. return true;
  92. }
  93. template <typename Scalar>
  94. IGL_INLINE bool igl::writeDMAT(
  95. const std::string file_name,
  96. const std::vector<Scalar > W)
  97. {
  98. FILE * fp = fopen(file_name.c_str(),"w");
  99. if(fp == NULL)
  100. {
  101. fprintf(stderr,"IOError: writeDMAT() could not open %s...",file_name.c_str());
  102. return false;
  103. }
  104. int num_rows = (int)W.size();
  105. int num_cols = 0;
  106. if(num_rows > 0)
  107. {
  108. num_cols = 1;
  109. }
  110. // first line contains number of columns and number of rows
  111. fprintf(fp,"%d %d\n",num_cols,num_rows);
  112. // loop over rows (down columns) quickly
  113. for(int i = 0;i < num_rows;i++)
  114. {
  115. fprintf(fp,"%0.15lf\n",(double)W[i]);
  116. }
  117. fclose(fp);
  118. return true;
  119. }
  120. #ifdef IGL_STATIC_LIBRARY
  121. // Explicit template specialization
  122. // generated by autoexplicit.sh
  123. template bool igl::writeDMAT<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<double, -1, -1, 0, -1, -1> const&,bool);
  124. template bool igl::writeDMAT<double>(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >);
  125. template bool igl::writeDMAT<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, bool);
  126. template bool igl::writeDMAT<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, bool);
  127. template bool igl::writeDMAT<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, bool);
  128. template bool igl::writeDMAT<Eigen::Matrix<double, -1, 3, 0, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Matrix<double, -1, 3, 0, -1, 3> const&, bool);
  129. template bool igl::writeDMAT<Eigen::Array<int, -1, 1, 0, -1, 1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::Array<int, -1, 1, 0, -1, 1> const&, bool);
  130. template bool igl::writeDMAT<Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, bool);
  131. template bool igl::writeDMAT<Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > >(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, bool);
  132. #endif