writeWRL.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <[email protected]>
  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 "writeWRL.h"
  9. #include <iostream>
  10. #include <fstream>
  11. template <typename DerivedV, typename DerivedF>
  12. IGL_INLINE bool igl::writeWRL(
  13. const std::string & str,
  14. const Eigen::MatrixBase<DerivedV> & V,
  15. const Eigen::MatrixBase<DerivedF> & F)
  16. {
  17. assert(V.cols() == 3 && "V should have 3 columns");
  18. assert(F.cols() == 3 && "F should have 3 columns");
  19. std::ofstream s(str);
  20. if(!s.is_open())
  21. {
  22. std::cerr<<"IOError: writeWRL() could not open "<<str<<std::endl;
  23. return false;
  24. }
  25. // Append column of -1 to F
  26. Eigen::Matrix<typename DerivedF::Scalar ,Eigen::Dynamic,4> FF(F.rows(),4);
  27. FF.leftCols(3) = F;
  28. FF.col(3).setConstant(-1);
  29. s<<R"(#VRML V2.0 utf8
  30. DEF default Transform {
  31. translation 0 0 0
  32. children [
  33. Shape {
  34. geometry DEF default-FACES IndexedFaceSet {
  35. ccw TRUE
  36. )"<<
  37. V.format(
  38. Eigen::IOFormat(
  39. Eigen::FullPrecision,
  40. Eigen::DontAlignCols,
  41. " ",",\n","","",
  42. "coord DEF default-COORD Coordinate { point [ \n","]\n}\n"))<<
  43. FF.format(
  44. Eigen::IOFormat(
  45. Eigen::FullPrecision,
  46. Eigen::DontAlignCols,
  47. ",","\n","","",
  48. "coordIndex [ \n"," ]\n"))<<
  49. "}\n}\n]\n}\n";
  50. return true;
  51. }
  52. // write mesh and colors-by-vertex to an ascii off file
  53. template <typename DerivedV, typename DerivedF, typename DerivedC>
  54. IGL_INLINE bool igl::writeWRL(
  55. const std::string & str,
  56. const Eigen::MatrixBase<DerivedV> & V,
  57. const Eigen::MatrixBase<DerivedF> & F,
  58. const Eigen::MatrixBase<DerivedC> & C)
  59. {
  60. assert(V.cols() == 3 && "V should have 3 columns");
  61. assert(F.cols() == 3 && "F should have 3 columns");
  62. std::ofstream s(str);
  63. if(!s.is_open())
  64. {
  65. std::cerr<<"IOError: writeWRL() could not open "<<str<<std::endl;
  66. return false;
  67. }
  68. // Append column of -1 to F
  69. Eigen::Matrix<typename DerivedF::Scalar ,Eigen::Dynamic,4> FF(F.rows(),4);
  70. FF.leftCols(3) = F;
  71. FF.col(3).setConstant(-1);
  72. //Check if RGB values are in the range [0..1] or [0..255]
  73. double rgbScale = (C.maxCoeff() <= 1.0)?1.0:1.0/255.0;
  74. Eigen::MatrixXd RGB = rgbScale * C;
  75. s<<R"(#VRML V2.0 utf8
  76. DEF default Transform {
  77. translation 0 0 0
  78. children [
  79. Shape {
  80. geometry DEF default-FACES IndexedFaceSet {
  81. ccw TRUE
  82. )"<<
  83. V.format(
  84. Eigen::IOFormat(
  85. Eigen::FullPrecision,
  86. Eigen::DontAlignCols,
  87. " ",",\n","","",
  88. "coord DEF default-COORD Coordinate { point [ \n","]\n}\n"))<<
  89. FF.format(
  90. Eigen::IOFormat(
  91. Eigen::FullPrecision,
  92. Eigen::DontAlignCols,
  93. ",","\n","","",
  94. "coordIndex [ \n"," ]\n"))<<
  95. RGB.format(
  96. Eigen::IOFormat(
  97. Eigen::FullPrecision,
  98. Eigen::DontAlignCols,
  99. ",","\n","","",
  100. "colorPerVertex TRUE\ncolor Color { color [ \n"," ] }\n"))<<
  101. "}\n}\n]\n}\n";
  102. return true;
  103. }
  104. #ifdef IGL_STATIC_LIBRARY
  105. // Explicit template instantiation
  106. // generated by autoexplicit.sh
  107. template bool igl::writeWRL<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
  108. // generated by autoexplicit.sh
  109. template bool igl::writeWRL<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> > const&);
  110. // generated by autoexplicit.sh
  111. template bool igl::writeWRL<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&);
  112. // generated by autoexplicit.sh
  113. template bool igl::writeWRL<Eigen::Matrix<double, 8, 3, 0, 8, 3>, Eigen::Matrix<int, 12, 3, 0, 12, 3> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 8, 3, 0, 8, 3> > const&, Eigen::MatrixBase<Eigen::Matrix<int, 12, 3, 0, 12, 3> > const&);
  114. template bool igl::writeWRL<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  115. template bool igl::writeWRL<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&);
  116. #endif