writeWRL.cpp 5.2 KB

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