Browse Source

Merge branch 'main' of github.com:libigl/libigl

Alec Jacobson 2 years ago
parent
commit
dddfd92c7a

+ 20 - 0
.gitattributes

@@ -0,0 +1,20 @@
+# Set the default behavior, in case people don't have core.autocrlf set.
+* text=auto
+
+# Explicitly declare text files you want to always be normalized and converted
+# to native line endings on checkout.
+*.tex text
+*.bib text
+*.svg text
+*.py text
+*.vbs text
+*.cpp text
+*.hpp text
+Makefile text
+
+# Declare files that will always have CRLF line endings on checkout.
+*.sln text eol=crlf
+
+# Denote all files that are truly binary and should not be modified.
+*.png binary
+*.jpg binary

+ 9 - 7
include/igl/marching_tets.cpp

@@ -15,6 +15,7 @@
 #include <iostream>
 #include <algorithm>
 #include <utility>
+#include <cmath>
 
 template <typename DerivedTV,
           typename DerivedTT,
@@ -27,7 +28,7 @@ void igl::marching_tets(
     const Eigen::MatrixBase<DerivedTV>& TV,
     const Eigen::MatrixBase<DerivedTT>& TT,
     const Eigen::MatrixBase<DerivedS>& isovals,
-    double isovalue,
+    const typename DerivedS::Scalar isovalue,
     Eigen::PlainObjectBase<DerivedSV>& outV,
     Eigen::PlainObjectBase<DerivedSF>& outF,
     Eigen::PlainObjectBase<DerivedJ>& J,
@@ -163,11 +164,12 @@ void igl::marching_tets(
       {
         // Typedef to make sure we handle floats properly
         typedef Eigen::Matrix<typename DerivedTV::Scalar, 1, 3, Eigen::RowMajor, 1, 3> RowVector;
-        const RowVector v1 = TV.row(edge.first);
-        const RowVector v2 = TV.row(edge.second);
-        const double a = fabs(isovals(edge.first, 0) - isovalue);
-        const double b = fabs(isovals(edge.second, 0) - isovalue);
-        const double w = a / (a+b);
+        using Scalar = typename DerivedS::Scalar;
+        const RowVector v1 =  TV.row(edge.first).template cast<Scalar>();
+        const RowVector v2 = TV.row(edge.second).template cast<Scalar>();
+        const Scalar a = abs(isovals(edge.first, 0) - isovalue);
+        const Scalar b = abs(isovals(edge.second, 0) - isovalue);
+        const Scalar w = a / (a+b);
 
         // Create a casted copy in case BCType is a float and we need to downcast
         const BCType bc_w = static_cast<BCType>(w);
@@ -193,5 +195,5 @@ void igl::marching_tets(
 
 
 #ifdef IGL_STATIC_LIBRARY
-template void igl::marching_tets<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, double>(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&, double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::SparseMatrix<double, 0, int>&);
+template void igl::marching_tets<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, double>(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&, const double, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::SparseMatrix<double, 0, int>&);
 #endif // IGL_STATIC_LIBRARY

+ 6 - 6
include/igl/marching_tets.h

@@ -38,7 +38,7 @@ namespace igl {
       const Eigen::MatrixBase<DerivedTV>& TV,
       const Eigen::MatrixBase<DerivedTT>& TT,
       const Eigen::MatrixBase<DerivedS>& S,
-      double isovalue,
+      const typename DerivedS::Scalar isovalue,
       Eigen::PlainObjectBase<DerivedSV>& SV,
       Eigen::PlainObjectBase<DerivedSF>& SF,
       Eigen::PlainObjectBase<DerivedJ>& J,
@@ -73,11 +73,11 @@ namespace igl {
       const Eigen::MatrixBase<DerivedTV>& TV,
       const Eigen::MatrixBase<DerivedTT>& TT,
       const Eigen::MatrixBase<DerivedS>& S,
-      double isovalue,
+      const typename DerivedS::Scalar isovalue,
       Eigen::PlainObjectBase<DerivedSV>& SV,
       Eigen::PlainObjectBase<DerivedSF>& SF,
       Eigen::PlainObjectBase<DerivedJ>& J) {
-    Eigen::SparseMatrix<double> _BC;
+    Eigen::SparseMatrix<typename DerivedSV::Scalar> _BC;
     return igl::marching_tets(TV, TT, S, isovalue, SV, SF, J, _BC);
   }
   /// \overload
@@ -91,7 +91,7 @@ namespace igl {
       const Eigen::MatrixBase<DerivedTV>& TV,
       const Eigen::MatrixBase<DerivedTT>& TT,
       const Eigen::MatrixBase<DerivedS>& S,
-      double isovalue,
+      const typename DerivedS::Scalar isovalue,
       Eigen::PlainObjectBase<DerivedSV>& SV,
       Eigen::PlainObjectBase<DerivedSF>& SF,
       Eigen::SparseMatrix<BCType>& BC) {
@@ -108,11 +108,11 @@ namespace igl {
       const Eigen::MatrixBase<DerivedTV>& TV,
       const Eigen::MatrixBase<DerivedTT>& TT,
       const Eigen::MatrixBase<DerivedS>& S,
-      double isovalue,
+      const typename DerivedS::Scalar isovalue,
       Eigen::PlainObjectBase<DerivedSV>& SV,
       Eigen::PlainObjectBase<DerivedSF>& SF) {
     Eigen::VectorXi _J;
-    Eigen::SparseMatrix<double> _BC;
+    Eigen::SparseMatrix<typename DerivedSV::Scalar> _BC;
     return igl::marching_tets(TV, TT, S, isovalue, SV, SF, _J, _BC);
   }
 

+ 2 - 2
include/igl/writeOFF.cpp

@@ -29,7 +29,7 @@ IGL_INLINE bool igl::writeOFF(
   s<<
     "OFF\n"<<V.rows()<<" "<<F.rows()<<" 0\n"<<
     V.format(IOFormat(FullPrecision,DontAlignCols," ","\n","","","","\n"))<<
-    (F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n","3 ","","","\n"));
+    (F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n",std::to_string(F.cols()) + " ","","","\n"));
   return true;
 }
 
@@ -72,7 +72,7 @@ IGL_INLINE bool igl::writeOFF(
     s << unsigned(RGB_Array(i,0)) << " " << unsigned(RGB_Array(i,1)) << " " << unsigned(RGB_Array(i,2)) << " 255\n";
   }
 
-  s<<(F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n","3 ","","","\n"));
+  s<<(F.array()).format(IOFormat(FullPrecision,DontAlignCols," ","\n",std::to_string(F.cols()) + " ","","","\n"));
   return true;
 }
 

+ 32 - 0
tests/include/igl/writeOFF.cpp

@@ -0,0 +1,32 @@
+#include <test_common.h>
+#include <igl/readOFF.h>
+#include <igl/writeOFF.h>
+
+TEST_CASE("writeOFF: quads", "[igl]")
+{
+  // Cube
+  Eigen::MatrixXd V(8,3);
+  V <<
+    0,0,0,
+    1,0,0,
+    1,1,0,
+    0,1,0,
+    0,0,1,
+    1,0,1,
+    1,1,1,
+    0,1,1;
+  Eigen::MatrixXi Q(6,4);
+  Q <<
+    0,1,2,3,
+    1,5,6,2,
+    5,4,7,6,
+    4,0,3,7,
+    3,2,6,7,
+    1,0,4,5;
+  igl::writeOFF("cube.off",V,Q);
+  Eigen::MatrixXd rV;
+  Eigen::MatrixXi rQ;
+  igl::readOFF("cube.off",rV,rQ);
+  test_common::assert_eq(V,rV);
+  test_common::assert_eq(Q,rQ);
+}

+ 116 - 0
tutorial/114_CustomShader/main.cpp

@@ -0,0 +1,116 @@
+#include <igl/read_triangle_mesh.h>
+#include <igl/opengl/glfw/Viewer.h>
+#include <igl/opengl/create_shader_program.h>
+#include <igl/opengl/destroy_shader_program.h>
+#include <Eigen/Core>
+
+int main(int argc, char *argv[])
+{
+  using namespace Eigen;
+  using namespace std;
+  Eigen::MatrixXd V;
+  Eigen::MatrixXi F;
+  igl::opengl::glfw::Viewer v;
+  igl::read_triangle_mesh(
+    argc>1?argv[1]: TUTORIAL_SHARED_PATH "/bunny.off", V, F);
+  v.data().set_mesh(V,F);
+  v.data().set_face_based(false);
+  v.data().show_lines = false;
+  v.launch_init();
+
+  v.data().meshgl.init();
+  igl::opengl::destroy_shader_program(v.data().meshgl.shader_mesh);
+
+  // Custom shader that adds a Fresnel effect
+  {
+    std::string mesh_vertex_shader_string =
+R"(#version 150
+uniform mat4 view;
+uniform mat4 proj;
+uniform mat4 normal_matrix;
+in vec3 position;
+in vec3 normal;
+out vec3 position_eye;
+out vec3 normal_eye;
+in vec4 Ka;
+in vec4 Kd;
+in vec4 Ks;
+in vec2 texcoord;
+out vec2 texcoordi;
+out vec4 Kai;
+out vec4 Kdi;
+out vec4 Ksi;
+
+void main()
+{
+  position_eye = vec3 (view * vec4 (position, 1.0));
+  normal_eye = vec3 (normal_matrix * vec4 (normal, 0.0));
+  normal_eye = normalize(normal_eye);
+  gl_Position = proj * vec4 (position_eye, 1.0); //proj * view * vec4(position, 1.0);
+  Kai = Ka;
+  Kdi = Kd;
+  Ksi = Ks;
+  texcoordi = texcoord;
+})";
+
+    std::string mesh_fragment_shader_string =
+R"(#version 150
+uniform mat4 view;
+uniform mat4 proj;
+uniform vec4 fixed_color;
+in vec3 position_eye;
+in vec3 normal_eye;
+uniform vec3 light_position_eye;
+vec3 Ls = vec3 (1, 1, 1);
+vec3 Ld = vec3 (1, 1, 1);
+vec3 La = vec3 (1, 1, 1);
+in vec4 Ksi;
+in vec4 Kdi;
+in vec4 Kai;
+in vec2 texcoordi;
+uniform sampler2D tex;
+uniform float specular_exponent;
+uniform float lighting_factor;
+uniform float texture_factor;
+out vec4 outColor;
+void main()
+{
+vec3 Ia = La * vec3(Kai);    // ambient intensity
+
+vec3 vector_to_light_eye = light_position_eye - position_eye;
+vec3 direction_to_light_eye = normalize (vector_to_light_eye);
+float dot_prod = dot (direction_to_light_eye, normalize(normal_eye));
+float clamped_dot_prod = max (dot_prod, 0.0);
+vec3 Id = Ld * vec3(Kdi) * clamped_dot_prod;    // Diffuse intensity
+
+vec3 reflection_eye = reflect (-direction_to_light_eye, normalize(normal_eye));
+vec3 surface_to_viewer_eye = normalize (-position_eye);
+float dot_prod_specular = dot (reflection_eye, surface_to_viewer_eye);
+dot_prod_specular = float(abs(dot_prod)==dot_prod) * max (dot_prod_specular, 0.0);
+float specular_factor = pow (dot_prod_specular, specular_exponent);
+vec3 Kfi = 0.5*vec3(Ksi);
+vec3 Lf = Ls;
+float fresnel_exponent = 2*specular_exponent;
+float fresnel_factor = 0;
+{
+  float NE = max( 0., dot( normalize(normal_eye), surface_to_viewer_eye));
+  fresnel_factor = pow (max(sqrt(1. - NE*NE),0.0), fresnel_exponent);
+}
+vec3 Is = Ls * vec3(Ksi) * specular_factor;    // specular intensity
+vec3 If = Lf * vec3(Kfi) * fresnel_factor;     // fresnel intensity
+vec4 color = vec4(lighting_factor * (If + Is + Id) + Ia +
+  (1.0-lighting_factor) * vec3(Kdi),(Kai.a+Ksi.a+Kdi.a)/3);
+outColor = mix(vec4(1,1,1,1), texture(tex, texcoordi), texture_factor) * color;
+if (fixed_color != vec4(0.0)) outColor = fixed_color;
+})";
+
+    igl::opengl::create_shader_program(
+      mesh_vertex_shader_string,
+      mesh_fragment_shader_string,
+      {},
+      v.data().meshgl.shader_mesh);
+  }
+
+  v.launch_rendering(true);
+  v.launch_shut();
+}

+ 1 - 1
tutorial/906_TrimWithSolid/main.cpp

@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
   // set first mesh as selected
   vr.selected_data_index = 0;
 
-  vr.launch_init(false);
+  vr.launch_init();
   vr.core().draw(vr.data(),true);
   vr.callback_mouse_up(vr,0,0);
   vr.launch_rendering(true);

+ 1 - 0
tutorial/CMakeLists.txt

@@ -31,6 +31,7 @@ if(LIBIGL_TUTORIALS_CHAPTER1)
     igl_add_tutorial(111_MatCap igl::glfw igl::stb)
     igl_add_tutorial(112_Selection igl::imgui)
     igl_add_tutorial(113_Shadows igl::imgui igl::stb)
+    igl_add_tutorial(114_CustomShader igl::glfw)
 endif()
 
 # Chapter 2