소스 검색

Merge branch 'master' of https://github.com/libigl/libigl

Former-commit-id: 28a20efc2e0dfbee70fac0b96d256ee41e97c70f
Daniele Panozzo 11 년 전
부모
커밋
889234511e

+ 4 - 0
.gitignore

@@ -57,3 +57,7 @@ external/tetgen/tetgen
 external/tinyxml2/test
 external/tinyxml2/tinyxml2.pc
 external/yimg/showpng
+README.html
+tutorial/readme.html
+tutorial/*/build/*
+tutorial/*/Makefile

+ 6 - 0
.mailmap

@@ -11,3 +11,9 @@ Alec Jacobson <alecjacobson@gmail.com> ajx <devnull@localhost>
 Alec Jacobson <alecjacobson@gmail.com> mangledorf <alecjacobson@gmail.com>
 Daniele Panozzo <panozzo@inf.ethz.ch> Daniele Panozzo <daniele.panozzo@gmail.com>
 Daniele Panozzo <panozzo@inf.ethz.ch> dpanozzo <devnull@localhost>
+Wenzel Jakob <wenzel@inf.ethz.ch> Wenzel Jakob <wenzel@cs.cornell.edu>
+Olga Diamanti <olga.diamanti@inf.ethz.ch> dolga <devnull@localhost>
+schuellc <schuellchr@gmail.com> schuellc <devnull@localhost>
+Kenshi Takayama <kenshi84@gmail.com> Kenshi Takayama (kenshi <Kenshi Takayama (kenshi@gmail.com)>
+Kenshi Takayama <kenshi84@gmail.com> kenshi <kenshi@jackal.ethz.ch>
+Kenshi Takayama <kenshi84@gmail.com> kenshi84 <kenshi84@gmail.com>

+ 4 - 0
Makefile

@@ -54,6 +54,10 @@ ifeq ($(IGL_WITH_TETGEN),1)
 	EXTRA_DIRS+=include/igl/tetgen
 	EXTRAS += tetgen
 endif
+ifeq ($(IGL_WITH_VIEWER),1)
+	EXTRA_DIRS+=include/igl/viewer
+	EXTRAS += viewer
+endif
 ifeq ($(IGL_WITH_XML),1)
 	EXTRA_DIRS+=include/igl/xml
 	EXTRAS += xml

+ 1 - 0
Makefile.conf

@@ -47,6 +47,7 @@ endif
 ifeq ($(IGL_USERNAME),ajx)
 	MOSEKPLATFORM=osx64x86
 	MOSEKVERSION=7
+	IGL_WITH_VIEWER=1
 	IGL_WITH_TETGEN=1
 	IGL_WITH_EMBREE=1
 	IGL_WITH_MATLAB=1

+ 1 - 1
include/igl/matlab/matlabinterface.h

@@ -30,7 +30,7 @@
 #include <string>
 #include <vector>
 
-#include "engine.h"  // Matlab engine header
+#include <engine.h>  // Matlab engine header
 
 namespace igl
 {

+ 32 - 0
include/igl/matlab/parse_rhs.cpp

@@ -0,0 +1,32 @@
+#include "parse_rhs.h"
+#include <algorithm>
+
+template <typename DerivedV>
+IGL_INLINE void igl::parse_rhs_double(
+    const mxArray *prhs[], 
+    Eigen::PlainObjectBase<DerivedV> & V)
+{
+  using namespace std;
+  // set number of mesh vertices
+  const int n = mxGetM(prhs[0]);
+  // set vertex position pointers
+  double * Vp = mxGetPr(prhs[0]);
+  const int dim = mxGetN(prhs[0]);
+  V.resize(n,dim);
+  copy(Vp,Vp+n*dim,&V.data()[0]);
+}
+
+template <typename DerivedV>
+IGL_INLINE void igl::parse_rhs_index(
+    const mxArray *prhs[], 
+    Eigen::PlainObjectBase<DerivedV> & V)
+{
+  parse_rhs_double(prhs,V);
+  V.array() -= 1;
+}
+
+#ifndef IGL_HEADER_ONLY
+template void igl::parse_rhs_index<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
+template void igl::parse_rhs_index<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
+template void igl::parse_rhs_double<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(mxArray_tag const**, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
+#endif

+ 27 - 0
include/igl/matlab/parse_rhs.h

@@ -0,0 +1,27 @@
+#ifndef IGL_PARSE_RHS_H
+#define IGL_PARSE_RHS_H
+#include <igl/igl_inline.h>
+#include <mex.h>
+#include <Eigen/Dense>
+namespace igl
+{
+  // Reads in a matrix as a double
+  //
+  // Inputs:
+  //   prhs  points to rhs argument
+  // Outputs:
+  //   V  M by N matrix 
+  template <typename DerivedV>
+  IGL_INLINE void parse_rhs_double(
+    const mxArray *prhs[], 
+    Eigen::PlainObjectBase<DerivedV> & V);
+  // Reads in a matrix and subtracts 1
+  template <typename DerivedV>
+  IGL_INLINE void parse_rhs_index(
+    const mxArray *prhs[], 
+    Eigen::PlainObjectBase<DerivedV> & V);
+};
+#ifdef IGL_HEADER_ONLY
+#  include "parse_rhs.cpp"
+#endif
+#endif

+ 29 - 0
include/igl/matlab/prepare_lhs.cpp

@@ -0,0 +1,29 @@
+#include "prepare_lhs.h"
+#include <algorithm>
+template <typename DerivedV>
+IGL_INLINE void igl::prepare_lhs_double(
+  const Eigen::PlainObjectBase<DerivedV> & V,
+  mxArray *plhs[])
+{
+  using namespace std;
+  plhs[0] = mxCreateDoubleMatrix(V.rows(),V.cols(), mxREAL);
+  double * Vp = mxGetPr(plhs[0]);
+  copy(&V.data()[0],&V.data()[0]+V.size(),Vp);
+}
+
+template <typename DerivedV>
+IGL_INLINE void igl::prepare_lhs_index(
+  const Eigen::PlainObjectBase<DerivedV> & V,
+  mxArray *plhs[])
+{
+  // Treat indices as reals
+  
+  const auto Vd = (V.template cast<double>().array()+1).eval();
+  return prepare_lhs_double(Vd,plhs);
+}
+
+#ifndef IGL_HEADER_ONLY
+template void igl::prepare_lhs_index<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
+template void igl::prepare_lhs_index<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, mxArray_tag**);
+template void igl::prepare_lhs_double<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, mxArray_tag**);
+#endif

+ 28 - 0
include/igl/matlab/prepare_lhs.h

@@ -0,0 +1,28 @@
+#ifndef IGL_PREPARE_LHS_H
+#define IGL_PREPARE_LHS_H
+#include <igl/igl_inline.h>
+#include <mex.h>
+#include <Eigen/Dense>
+namespace igl
+{
+  // Writes out a matrix as a double
+  //
+  // Inputs:
+  //   prhs  points to rhs argument
+  // Outputs:
+  //   V  M by N matrix 
+  template <typename DerivedV>
+  IGL_INLINE void prepare_lhs_double(
+    const Eigen::PlainObjectBase<DerivedV> & V,
+    mxArray *plhs[]);
+  // Writes out a matrix and adds 1
+  template <typename DerivedV>
+  IGL_INLINE void prepare_lhs_index(
+    const Eigen::PlainObjectBase<DerivedV> & V,
+    mxArray *plhs[]);
+};
+#ifdef IGL_HEADER_ONLY
+#  include "prepare_lhs.cpp"
+#endif
+#endif
+

+ 49 - 0
include/igl/viewer/Makefile

@@ -0,0 +1,49 @@
+include ../../../Makefile.conf
+all: CFLAGS += -O3 -DNDEBUG -fopenmp
+debug: CFLAGS += -g -Wall -Werror -fopenmp
+
+.PHONY: all
+all: libiglviewer
+debug: libiglviewer
+
+.PHONY: libviewer
+libiglviewer: obj ../../../lib/libiglviewer.a
+
+CPP_FILES=$(wildcard *.cpp)
+OBJ_FILES=$(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))
+
+# include igl headers
+INC+=-I../../../include/
+
+# EXPECTS THAT CFLAGS IS ALREADY SET APPROPRIATELY 
+
+# Eigen dependency
+EIGEN3_INC=-I$(DEFAULT_PREFIX)/include/eigen3 -I$(DEFAULT_PREFIX)/include/eigen3/unsupported
+INC+=$(EIGEN3_INC)
+
+# GLFW dependency
+ifndef GLFW
+	GLFW=../../../external/glfw/
+endif
+GLFW_INC=-I$(GLFW)/include
+INC+=$(GLFW_INC)
+ifndef ANTTWEAKBAR
+	ANTTWEAKBAR=../../../external/AntTweakBar/
+endif
+# Viewer also uses source files from AntTweakBar for font rendering
+ANTTWEAKBAR_INC=-I$(ANTTWEAKBAR)/include -I$(ANTTWEAKBAR)/src
+INC+=$(ANTTWEAKBAR_INC)
+
+obj: 
+	mkdir -p obj
+
+../../../lib/libiglviewer.a: $(OBJ_FILES)
+	rm -f $@
+	ar cqs $@ $(OBJ_FILES)
+
+obj/%.o: %.cpp %.h
+	g++ $(AFLAGS) $(CFLAGS) -c -o $@ $< $(INC)
+
+clean:
+	rm -f obj/*.o
+	rm -f ../../../lib/libiglmatlab.a

+ 25 - 0
include/igl/viewer/TODOs.txt

@@ -0,0 +1,25 @@
+- rewrite in libigl style
+- remove use of double underscores (http://stackoverflow.com/a/224420/148668)
+- document inputs and outputs to all functions
+- document all member fields
+- document all classes
+- light direction is backwards
+- remove global variables (not threadsafe)
+- encapsulate (in igl namespace) and move static/global functions, use lambdas?
+- preface macros with "IGL_"
+- trackball mouseup captured by tweakbar
+- zoom with pan rather than scaling
+- refresh draw while resizing
+- use constructor initializer list rather than complicated constructor
++ resize TwBar with window
++ trackball should be able to drag over TwBar
++ don't zoom on horizontal scale
++ remove global `using namespace std`
++ remove `#define IGL_HEADER_ONLY`
++ guard `#undef max`
++ fix all -Wsign-compare
++ missing `#include <iostream>`
++ missing `#include <fstream>`
++ fix all -Wunused-but-set-variable
++ makefile for libiglviewer.a
++ Viewer.h should include Viewer.cpp

+ 70 - 37
include/igl/viewer/Viewer.cpp

@@ -16,20 +16,21 @@
 #include <GLFW/glfw3.h>
 
 #ifdef _WIN32
-#include <windows.h>
-#include <GL/glew.h>
+#  include <windows.h>
+#  undef max
+#  undef min
+#  include <GL/glew.h>
 #endif
 
 #include <cmath>
 #include <cstdio>
-#include <string>
 #include <sstream>
 #include <iomanip>
+#include <iostream>
+#include <fstream>
 
 #include <algorithm>
 
-using namespace std;
-
 //OK NV
 Eigen::Vector3f project(const Eigen::Vector3f&  obj,
                         const Eigen::Matrix4f& model,
@@ -154,15 +155,9 @@ Eigen::Matrix4f translate(
 }
 
 
-// Undef Visual Studio macros...
-#undef max
-#undef min
-
 #include <limits>
 #include <cassert>
 
-#define IGL_HEADER_ONLY
-
 #ifdef ENABLE_XML_SERIALIZATION
   #include "igl/xml/XMLSerializer.h"
 #endif
@@ -320,20 +315,26 @@ static TextRenderer __font_renderer;
 
 static void glfw_mouse_press(GLFWwindow* window, int button, int action, int modifier)
 {
-  if (!TwEventMouseButtonGLFW(button, action))
-  {
-    igl::Viewer::MouseButton mb;
-    if (button == GLFW_MOUSE_BUTTON_1)
-      mb = igl::Viewer::IGL_LEFT;
-    else if (button == GLFW_MOUSE_BUTTON_2)
-      mb = igl::Viewer::IGL_RIGHT;
-    else //if (button == GLFW_MOUSE_BUTTON_3)
-      mb = igl::Viewer::IGL_MIDDLE;
+  bool tw_used = TwEventMouseButtonGLFW(button, action);
+  igl::Viewer::MouseButton mb;
 
-    if (action == GLFW_PRESS)
+  if (button == GLFW_MOUSE_BUTTON_1)
+    mb = igl::Viewer::IGL_LEFT;
+  else if (button == GLFW_MOUSE_BUTTON_2)
+    mb = igl::Viewer::IGL_RIGHT;
+  else //if (button == GLFW_MOUSE_BUTTON_3)
+    mb = igl::Viewer::IGL_MIDDLE;
+
+  if (action == GLFW_PRESS)
+  {
+    if(!tw_used)
+    {
       __viewer->mouse_down(mb,modifier);
-    else
-      __viewer->mouse_up(mb,modifier);
+    }
+  } else
+  {
+    // Always call mouse_up on up
+    __viewer->mouse_up(mb,modifier);
   }
 
 }
@@ -509,16 +510,34 @@ static void glfw_window_size(GLFWwindow* window, int width, int height)
   __viewer->resize(w, h);
 
   TwWindowSize(w, h);
+  const auto & bar = __viewer->bar;
+  // Keep AntTweakBar on right side of screen and height == opengl height
+  // get the current position of a bar
+  int size[2];
+  TwGetParam(bar, NULL, "size", TW_PARAM_INT32, 2, size);
+  int pos[2];
+  // Place bar on left side of opengl rect (padded by 10 pixels)
+  pos[0] = 10;//max(10,(int)width - size[0] - 10);
+  // place bar at top (padded by 10 pixels)
+  pos[1] = 10;
+  // Set height to new height of window (padded by 10 pixels on bottom)
+  size[1] = height-pos[1]-10;
+  TwSetParam(bar, NULL, "position", TW_PARAM_INT32, 2, pos);
+  TwSetParam(bar, NULL, "size", TW_PARAM_INT32, 2,size);
 }
 
 static void glfw_mouse_move(GLFWwindow* window, double x, double y)
 {
-  if (!TwEventMousePosGLFW(x*highdpi,y*highdpi))
+  if(!TwEventMousePosGLFW(x*highdpi,y*highdpi) || __viewer->down)
+  {
+    // Call if TwBar hasn't used or if down
     __viewer->mouse_move(x*highdpi, y*highdpi);
+  }
 }
 
 static void glfw_mouse_scroll(GLFWwindow* window, double x, double y)
 {
+  using namespace std;
   scroll_x += x;
   scroll_y += y;
 
@@ -636,7 +655,7 @@ namespace igl
     options.line_color << 0.0f, 0.0f, 0.0f;
 
     // Default lights settings
-    options.light_position << 0.0f, 0.30f, 5.0f;
+    options.light_position << 0.0f, -0.30f, -5.0f;
 
     // Default trackball
     options.trackball_angle << 0.0f, 0.0f, 0.0f, 1.0f;
@@ -814,8 +833,8 @@ namespace igl
       data.dirty |= DIRTY_TEXTURE;
     }
 
-    int size = 128;
-    int size2 = size/2;
+    unsigned size = 128;
+    unsigned size2 = size/2;
     data.texture_R.resize(size, size);
     for (unsigned i=0; i<size; ++i)
     {
@@ -1039,7 +1058,7 @@ namespace igl
                          mouse_x,
                          mouse_y,
                          options.trackball_angle.data());
-          Eigen::Vector4f snapq = options.trackball_angle;
+          //Eigen::Vector4f snapq = options.trackball_angle;
 
           break;
         }
@@ -1057,6 +1076,7 @@ namespace igl
         }
         case ZOOM:
         {
+          //float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
           float delta = 0.001f * (mouse_x - down_mouse_x + mouse_y - down_mouse_y);
           options.camera_zoom *= 1 + delta;
           down_mouse_x = mouse_x;
@@ -1084,13 +1104,19 @@ namespace igl
         if (plugin_manager->plugin_list[i]->mouse_scroll(delta_y))
           return true;
 
-    float mult = (delta_y>0)?1.1:0.9;
-    options.camera_zoom = (options.camera_zoom * mult > 0.1f ? options.camera_zoom * mult : 0.1f);
+    // Only zoom if there's actually a change
+    if(delta_y != 0)
+    {
+      float mult = (1.0+((delta_y>0)?1.:-1.)*0.05);
+      const float min_zoom = 0.1f;
+      options.camera_zoom = (options.camera_zoom * mult > min_zoom ? options.camera_zoom * mult : min_zoom);
+    }
     return true;
   }
 
   static GLuint create_shader_helper(GLint type, const std::string &shader_string)
   {
+    using namespace std;
     if (shader_string.empty())
       return (GLuint) 0;
 
@@ -1150,6 +1176,7 @@ namespace igl
     const std::string &geometry_shader_string,
     int geometry_shader_max_vertices)
   {
+    using namespace std;
     vertex_shader = create_shader_helper(GL_VERTEX_SHADER, vertex_shader_string);
     geometry_shader = create_shader_helper(GL_GEOMETRY_SHADER, geometry_shader_string);
     fragment_shader = create_shader_helper(GL_FRAGMENT_SHADER, fragment_shader_string);
@@ -1722,6 +1749,8 @@ namespace igl
 
   void Viewer::draw()
   {
+    using namespace std;
+    using namespace Eigen;
     glClearColor(options.background_color[0],
                  options.background_color[1],
                  options.background_color[2],
@@ -1802,7 +1831,8 @@ namespace igl
     GLint texture_factori       = opengl.shader_mesh.uniform("texture_factor");
 
     glUniform1f(specular_exponenti, options.shininess);
-    glUniform3fv(light_position_worldi, 1, options.light_position.data());
+    Vector3f rev_light = -1.*options.light_position;
+    glUniform3fv(light_position_worldi, 1, rev_light.data());
     glUniform1f(lighting_factori, 1.0f); // enables lighting
     glUniform4f(fixed_colori, 0.0, 0.0, 0.0, 0.0);
 
@@ -2107,6 +2137,7 @@ namespace igl
   // Helpers that draws the most common meshes
   void Viewer::draw_mesh(const Eigen::MatrixXd& V, const Eigen::MatrixXi& F)
   {
+    using namespace std;
     if (data.V.rows() == 0 && data.F.rows() == 0)
     {
       clear_mesh();
@@ -2137,6 +2168,7 @@ namespace igl
 
   void Viewer::draw_normals(const Eigen::MatrixXd& N)
   {
+    using namespace std;
     if (N.rows() == data.V.rows())
     {
       options.face_based = false;
@@ -2154,6 +2186,7 @@ namespace igl
 
   void Viewer::draw_colors(Eigen::MatrixXd C)
   {
+    using namespace std;
     if (C.rows() == 1)
     {
       for (unsigned i=0;i<data.V_material_diffuse.rows();++i)
@@ -2179,6 +2212,7 @@ namespace igl
 
   void Viewer::draw_UV(const Eigen::MatrixXd& UV)
   {
+    using namespace std;
     if (UV.rows() == data.V.rows())
     {
       options.face_based = false;
@@ -2233,7 +2267,7 @@ namespace igl
     data.labels_strings.push_back(str);
   }
 
-  void Viewer::launch(string filename)
+  void Viewer::launch(std::string filename)
   {
     GLFWwindow* window;
 
@@ -2255,12 +2289,11 @@ namespace igl
     }
     glfwMakeContextCurrent(window);
 
-    int major, minor, rev;
-    major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
-    minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
-    rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
-
     #ifdef DEBUG
+      int major, minor, rev;
+      major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
+      minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
+      rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
       printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
       printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
       printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));

+ 5 - 0
include/igl/viewer/Viewer.h

@@ -577,4 +577,9 @@ namespace igl
 
 
 } // end namespace
+
+#ifdef IGL_HEADER_ONLY
+#  include "Viewer.cpp"
+#endif
+
 #endif

+ 0 - 1
tutorial/102_DrawMesh/main.cpp

@@ -1,4 +1,3 @@
-#define IGL_HEADER_ONLY
 #include <igl/readOFF.h>
 #include <igl/viewer/Viewer.h>
 

+ 4 - 4
tutorial/cmake/FindLIBIGL.cmake

@@ -24,10 +24,10 @@ FIND_PATH(LIBIGL_INCLUDE_DIR igl/readOBJ.h
 
 if(LIBIGL_INCLUDE_DIR)
    set(LIBIGL_FOUND TRUE)
-#   add_definitions(-DIGL_HEADER_ONLY)
-   set(LIBIGL_SOURCES
-      ${LIBIGL_INCLUDE_DIR}/igl/viewer/Viewer.cpp
-   )
+   add_definitions(-DIGL_HEADER_ONLY)
+   #set(LIBIGL_SOURCES
+   #   ${LIBIGL_INCLUDE_DIR}/igl/viewer/Viewer.cpp
+   #)
 endif(LIBIGL_INCLUDE_DIR)
 
 if(LIBIGL_FOUND)

+ 1 - 0
tutorial/readme.md

@@ -1,4 +1,5 @@
 xhtml header:   <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
+css: default.css
 
 # Introduction