Эх сурвалжийг харах

Write colors to WRL files.

Former-commit-id: e4777ee41d0ff94167b1644caebfcdb8d9dbb657
Jérémie Dumas 8 жил өмнө
parent
commit
c4270a84b2

+ 63 - 4
include/igl/writeWRL.cpp

@@ -1,9 +1,9 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #include "writeWRL.h"
 #include <iostream>
@@ -53,6 +53,64 @@ ccw TRUE
   return true;
 }
 
+// write mesh and colors-by-vertex to an ascii off file
+template <typename DerivedV, typename DerivedF, typename DerivedC>
+IGL_INLINE bool igl::writeWRL(
+  const std::string & str,
+  const Eigen::PlainObjectBase<DerivedV> & V,
+  const Eigen::PlainObjectBase<DerivedF> & F,
+  const Eigen::PlainObjectBase<DerivedC> & C)
+{
+  using namespace std;
+  using namespace Eigen;
+  assert(V.cols() == 3 && "V should have 3 columns");
+  assert(F.cols() == 3 && "F should have 3 columns");
+  ofstream s(str);
+  if(!s.is_open())
+  {
+    cerr<<"IOError: writeWRL() could not open "<<str<<endl;
+    return false;
+  }
+  // Append column of -1 to F
+  Matrix<typename DerivedF::Scalar,Dynamic,4> FF(F.rows(),4);
+  FF.leftCols(3) = F;
+  FF.col(3).setConstant(-1);
+
+
+  //Check if RGB values are in the range [0..1] or [0..255]
+  double rgbScale = (C.maxCoeff() <= 1.0)?1.0:1.0/255.0;
+  Eigen::MatrixXd RGB = rgbScale * C;
+
+  s<<R"(
+DEF default Transform {
+translation 0 0 0
+children [
+Shape {
+geometry DEF default-FACES IndexedFaceSet {
+ccw TRUE
+)"<<
+    V.format(
+      IOFormat(
+        FullPrecision,
+        DontAlignCols,
+        " ",",\n","","",
+        "coord DEF default-COORD Coordinate { point [ \n","]\n}\n"))<<
+    FF.format(
+      IOFormat(
+        FullPrecision,
+        DontAlignCols,
+        ",","\n","","",
+        "coordIndex [ \n"," ]\n"))<<
+    RGB.format(
+      IOFormat(
+        FullPrecision,
+        DontAlignCols,
+        ",","\n","","",
+        "colorPerVertex TRUE\ncolor Color { color [ \n"," ] }\n"))<<
+    "}\n}\n]\n}\n";
+  return true;
+}
+
 #ifdef IGL_STATIC_LIBRARY
 // Explicit template instantiation
 // generated by autoexplicit.sh
@@ -60,4 +118,5 @@ template bool igl::writeWRL<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matri
 // generated by autoexplicit.sh
 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&);
 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&);
+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::__cxx11::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&);
 #endif

+ 19 - 4
include/igl/writeWRL.h

@@ -1,9 +1,9 @@
 // This file is part of libigl, a simple c++ geometry processing library.
-// 
+//
 // Copyright (C) 2015 Alec Jacobson <alecjacobson@gmail.com>
-// 
-// This Source Code Form is subject to the terms of the Mozilla Public License 
-// v. 2.0. If a copy of the MPL was not distributed with this file, You can 
+//
+// This Source Code Form is subject to the terms of the Mozilla Public License
+// v. 2.0. If a copy of the MPL was not distributed with this file, You can
 // obtain one at http://mozilla.org/MPL/2.0/.
 #ifndef IGL_WRITE_WRL_H
 #define IGL_WRITE_WRL_H
@@ -24,6 +24,21 @@ namespace igl
     const std::string & str,
     const Eigen::PlainObjectBase<DerivedV> & V,
     const Eigen::PlainObjectBase<DerivedF> & F);
+
+  // Write mesh to a .wrl file
+  //
+  // Inputs:
+  //   str  path to .wrl file
+  //   V  #V by 3 list of vertex positions
+  //   F  #F by 3 list of triangle indices
+  //   C  double matrix of rgb values per vertex #V by 3
+  // Returns true iff succes
+  template <typename DerivedV, typename DerivedF, typename DerivedC>
+  IGL_INLINE bool writeWRL(
+    const std::string & str,
+    const Eigen::PlainObjectBase<DerivedV> & V,
+    const Eigen::PlainObjectBase<DerivedF> & F,
+    const Eigen::PlainObjectBase<DerivedC> & C);
 }
 #ifndef IGL_STATIC_LIBRARY
 #include "writeWRL.cpp"