diff --git a/Build.PL b/Build.PL index ef17ec046b..1c3b0e3a7f 100644 --- a/Build.PL +++ b/Build.PL @@ -16,6 +16,8 @@ my %prereqs = qw( ExtUtils::MakeMaker 6.80 ExtUtils::ParseXS 3.22 ExtUtils::XSpp 0 + ExtUtils::XSpp::Cmd 0 + ExtUtils::CppGuess 0 ExtUtils::Typemaps 0 ExtUtils::Typemaps::Basic 0 File::Basename 0 diff --git a/CMakeLists.txt b/CMakeLists.txt index 719bdd04e7..ba264c8c36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,7 +60,7 @@ if (MSVC) # /bigobj (Increase Number of Sections in .Obj file) # error C3859: virtual memory range for PCH exceeded; please recompile with a command line option of '-Zm90' or greater # Generate symbols at every build target, even for the release. - add_compile_options(-bigobj -Zm316 /Zi) + add_compile_options(-bigobj -Zm520 /Zi) endif () # Display and check CMAKE_PREFIX_PATH @@ -89,8 +89,6 @@ enable_testing () # Enable C++11 language standard. set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_C_STANDARD 11) -set(CMAKE_C_STANDARD_REQUIRED ON) if(NOT WIN32) # Add DEBUG flags to debug builds. @@ -172,7 +170,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STRE add_compile_options(-Werror=return-type) #removes LOTS of extraneous Eigen warnings - add_compile_options(-Wno-ignored-attributes) + # add_compile_options(-Wno-ignored-attributes) # Tamas: Eigen include dirs are marked as SYSTEM if (SLIC3R_ASAN) add_compile_options(-fsanitize=address -fno-omit-frame-pointer) @@ -240,15 +238,34 @@ if(NOT WIN32) set(MINIMUM_BOOST_VERSION "1.64.0") endif() find_package(Boost ${MINIMUM_BOOST_VERSION} REQUIRED COMPONENTS system filesystem thread log locale regex) -if(Boost_FOUND) -# include_directories(SYSTEM ${Boost_INCLUDE_DIRS}) - if (APPLE) - # BOOST_ASIO_DISABLE_KQUEUE : prevents a Boost ASIO bug on OS X: https://svn.boost.org/trac/boost/ticket/5339 - add_definitions(-DBOOST_ASIO_DISABLE_KQUEUE) - endif() - if(NOT SLIC3R_STATIC) - add_definitions(-DBOOST_LOG_DYN_LINK) - endif() + +add_library(boost_libs INTERFACE) +add_library(boost_headeronly INTERFACE) + +if (APPLE) + # BOOST_ASIO_DISABLE_KQUEUE : prevents a Boost ASIO bug on OS X: https://svn.boost.org/trac/boost/ticket/5339 + target_compile_definitions(boost_headeronly INTERFACE BOOST_ASIO_DISABLE_KQUEUE) +endif() + +if(NOT SLIC3R_STATIC) + target_compile_definitions(boost_headeronly INTERFACE BOOST_LOG_DYN_LINK) +endif() + +if(TARGET Boost::system) + message(STATUS "Boost::boost exists") + target_link_libraries(boost_headeronly INTERFACE Boost::boost) + target_link_libraries(boost_libs INTERFACE + boost_headeronly # includes the custom compile definitions as well + Boost::system + Boost::filesystem + Boost::thread + Boost::log + Boost::locale + Boost::regex + ) +else() + target_include_directories(boost_headeronly INTERFACE ${Boost_INCLUDE_DIRS}) + target_link_libraries(boost_libs INTERFACE boost_headeronly ${Boost_LIBRARIES}) endif() # Find and configure intel-tbb @@ -295,7 +312,7 @@ if (NOT Eigen3_FOUND) set(Eigen3_FOUND 1) set(EIGEN3_INCLUDE_DIR ${LIBDIR}/eigen/) endif () -include_directories(${EIGEN3_INCLUDE_DIR}) +include_directories(BEFORE SYSTEM ${EIGEN3_INCLUDE_DIR}) # Find expat or use bundled version # Always use the system libexpat on Linux. diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index 5720901578..5bc33c896d 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -36,6 +36,11 @@ set(DESTDIR "${CMAKE_CURRENT_BINARY_DIR}/destdir" CACHE PATH "Destination direct option(DEP_DEBUG "Build debug variants (only applicable on Windows)" ON) option(DEP_WX_STABLE "Build against wxWidgets stable 3.0 as opposed to default 3.1 (Linux only)" OFF) +# IGL static library in release mode produces 50MB binary. On the build server, it should be +# disabled and used in header-only mode. On developer machines, it can be enabled to speed +# up conpilation and suppress warnings coming from IGL. +option(DEP_BUILD_IGL_STATIC "Build IGL as a static library. Might cause link errors and increase binary size." OFF) + message(STATUS "PrusaSlicer deps DESTDIR: ${DESTDIR}") message(STATUS "PrusaSlicer deps debug build: ${DEP_DEBUG}") @@ -84,6 +89,7 @@ if (MSVC) dep_wxwidgets dep_gtest dep_nlopt + # dep_qhull # Experimental dep_zlib # on Windows we still need zlib ) @@ -97,6 +103,8 @@ else() dep_wxwidgets dep_gtest dep_nlopt + dep_qhull + dep_libigl ) endif() diff --git a/deps/deps-unix-common.cmake b/deps/deps-unix-common.cmake index 3cf843f73d..c44a6ec205 100644 --- a/deps/deps-unix-common.cmake +++ b/deps/deps-unix-common.cmake @@ -32,3 +32,44 @@ ExternalProject_Add(dep_nlopt -DCMAKE_INSTALL_PREFIX=${DESTDIR}/usr/local ${DEP_CMAKE_OPTS} ) +find_package(Git REQUIRED) + +ExternalProject_Add(dep_qhull + EXCLUDE_FROM_ALL 1 + URL "https://github.com/qhull/qhull/archive/v7.2.1.tar.gz" + URL_HASH SHA256=6fc251e0b75467e00943bfb7191e986fce0e1f8f6f0251f9c6ce5a843821ea78 + CMAKE_ARGS + -DBUILD_SHARED_LIBS=OFF + -DCMAKE_INSTALL_PREFIX=${DESTDIR}/usr/local + ${DEP_CMAKE_OPTS} + PATCH_COMMAND ${GIT_EXECUTABLE} apply --ignore-space-change --ignore-whitespace ${CMAKE_CURRENT_SOURCE_DIR}/qhull-mods.patch +) + +ExternalProject_Add(dep_libigl + EXCLUDE_FROM_ALL 1 + URL "https://github.com/libigl/libigl/archive/v2.0.0.tar.gz" + URL_HASH SHA256=42518e6b106c7209c73435fd260ed5d34edeb254852495b4c95dce2d95401328 + CMAKE_ARGS + -DCMAKE_INSTALL_PREFIX=${DESTDIR}/usr/local + -DLIBIGL_BUILD_PYTHON=OFF + -DLIBIGL_BUILD_TESTS=OFF + -DLIBIGL_BUILD_TUTORIALS=OFF + -DLIBIGL_USE_STATIC_LIBRARY=${DEP_BUILD_IGL_STATIC} + -DLIBIGL_WITHOUT_COPYLEFT=OFF + -DLIBIGL_WITH_CGAL=OFF + -DLIBIGL_WITH_COMISO=OFF + -DLIBIGL_WITH_CORK=OFF + -DLIBIGL_WITH_EMBREE=OFF + -DLIBIGL_WITH_MATLAB=OFF + -DLIBIGL_WITH_MOSEK=OFF + -DLIBIGL_WITH_OPENGL=OFF + -DLIBIGL_WITH_OPENGL_GLFW=OFF + -DLIBIGL_WITH_OPENGL_GLFW_IMGUI=OFF + -DLIBIGL_WITH_PNG=OFF + -DLIBIGL_WITH_PYTHON=OFF + -DLIBIGL_WITH_TETGEN=OFF + -DLIBIGL_WITH_TRIANGLE=OFF + -DLIBIGL_WITH_XML=OFF + PATCH_COMMAND ${GIT_EXECUTABLE} apply --ignore-space-change --ignore-whitespace ${CMAKE_CURRENT_SOURCE_DIR}/igl-fixes.patch +) + diff --git a/deps/deps-windows.cmake b/deps/deps-windows.cmake index b3b31e5f37..d7daf84253 100644 --- a/deps/deps-windows.cmake +++ b/deps/deps-windows.cmake @@ -1,21 +1,34 @@ - +# https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html if (MSVC_VERSION EQUAL 1800) +# 1800 = VS 12.0 (v120 toolset) set(DEP_VS_VER "12") set(DEP_BOOST_TOOLSET "msvc-12.0") elseif (MSVC_VERSION EQUAL 1900) +# 1900 = VS 14.0 (v140 toolset) set(DEP_VS_VER "14") set(DEP_BOOST_TOOLSET "msvc-14.0") -elseif (MSVC_VERSION GREATER 1900) +elseif (MSVC_VERSION LESS 1920) +# 1910-1919 = VS 15.0 (v141 toolset) set(DEP_VS_VER "15") set(DEP_BOOST_TOOLSET "msvc-14.1") +elseif (MSVC_VERSION LESS 1930) +# 1920-1929 = VS 16.0 (v142 toolset) + set(DEP_VS_VER "16") + set(DEP_BOOST_TOOLSET "msvc-14.2") else () message(FATAL_ERROR "Unsupported MSVC version") endif () if (${DEPS_BITS} EQUAL 32) set(DEP_MSVC_GEN "Visual Studio ${DEP_VS_VER}") + set(DEP_PLATFORM "Win32") else () - set(DEP_MSVC_GEN "Visual Studio ${DEP_VS_VER} Win64") + if (DEP_VS_VER LESS 16) + set(DEP_MSVC_GEN "Visual Studio ${DEP_VS_VER} Win64") + else () + set(DEP_MSVC_GEN "Visual Studio ${DEP_VS_VER}") + endif () + set(DEP_PLATFORM "x64") endif () @@ -28,8 +41,8 @@ endif () ExternalProject_Add(dep_boost EXCLUDE_FROM_ALL 1 - URL "https://dl.bintray.com/boostorg/release/1.66.0/source/boost_1_66_0.tar.gz" - URL_HASH SHA256=bd0df411efd9a585e5a2212275f8762079fed8842264954675a4fddc46cfcf60 + URL "https://dl.bintray.com/boostorg/release/1.70.0/source/boost_1_70_0.tar.gz" + URL_HASH SHA256=882b48708d211a5f48e60b0124cf5863c1534cd544ecd0664bb534a4b5d506e9 BUILD_IN_SOURCE 1 CONFIGURE_COMMAND bootstrap.bat BUILD_COMMAND b2.exe @@ -57,6 +70,7 @@ ExternalProject_Add(dep_tbb URL "https://github.com/wjakob/tbb/archive/a0dc9bf76d0120f917b641ed095360448cabc85b.tar.gz" URL_HASH SHA256=0545cb6033bd1873fcae3ea304def720a380a88292726943ae3b9b207f322efe CMAKE_GENERATOR "${DEP_MSVC_GEN}" + CMAKE_GENERATOR_PLATFORM "${DEP_PLATFORM}" CMAKE_ARGS -DCMAKE_DEBUG_POSTFIX=_debug -DTBB_BUILD_SHARED=OFF @@ -81,6 +95,7 @@ ExternalProject_Add(dep_gtest URL "https://github.com/google/googletest/archive/release-1.8.1.tar.gz" URL_HASH SHA256=9bf1fe5182a604b4135edc1a425ae356c9ad15e9b23f9f12a02e80184c3a249c CMAKE_GENERATOR "${DEP_MSVC_GEN}" + CMAKE_GENERATOR_PLATFORM "${DEP_PLATFORM}" CMAKE_ARGS -DBUILD_GMOCK=OFF -Dgtest_force_shared_crt=ON @@ -105,6 +120,7 @@ ExternalProject_Add(dep_nlopt URL "https://github.com/stevengj/nlopt/archive/v2.5.0.tar.gz" URL_HASH SHA256=c6dd7a5701fff8ad5ebb45a3dc8e757e61d52658de3918e38bab233e7fd3b4ae CMAKE_GENERATOR "${DEP_MSVC_GEN}" + CMAKE_GENERATOR_PLATFORM "${DEP_PLATFORM}" CMAKE_ARGS -DBUILD_SHARED_LIBS=OFF -DNLOPT_PYTHON=OFF @@ -133,6 +149,7 @@ ExternalProject_Add(dep_zlib URL "https://zlib.net/zlib-1.2.11.tar.xz" URL_HASH SHA256=4ff941449631ace0d4d203e3483be9dbc9da454084111f97ea0a2114e19bf066 CMAKE_GENERATOR "${DEP_MSVC_GEN}" + CMAKE_GENERATOR_PLATFORM "${DEP_PLATFORM}" CMAKE_ARGS -DSKIP_INSTALL_FILES=ON # Prevent installation of man pages et al. "-DINSTALL_BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}\\fallout" # I found no better way of preventing zlib from creating & installing DLLs :-/ @@ -199,6 +216,33 @@ if (${DEP_DEBUG}) ) endif () +find_package(Git REQUIRED) + +ExternalProject_Add(dep_qhull + EXCLUDE_FROM_ALL 1 + URL "https://github.com/qhull/qhull/archive/v7.2.1.tar.gz" + URL_HASH SHA256=6fc251e0b75467e00943bfb7191e986fce0e1f8f6f0251f9c6ce5a843821ea78 + CMAKE_GENERATOR "${DEP_MSVC_GEN}" + CMAKE_ARGS + -DCMAKE_INSTALL_PREFIX=${DESTDIR}/usr/local + -DBUILD_SHARED_LIBS=OFF + -DCMAKE_POSITION_INDEPENDENT_CODE=ON + -DCMAKE_DEBUG_POSTFIX=d + PATCH_COMMAND ${GIT_EXECUTABLE} apply --ignore-space-change --ignore-whitespace ${CMAKE_CURRENT_SOURCE_DIR}/qhull-mods.patch + BUILD_COMMAND msbuild /m /P:Configuration=Release INSTALL.vcxproj + INSTALL_COMMAND "" +) + +if (${DEP_DEBUG}) + ExternalProject_Get_Property(dep_qhull BINARY_DIR) + ExternalProject_Add_Step(dep_qhull build_debug + DEPENDEES build + DEPENDERS install + COMMAND msbuild /m /P:Configuration=Debug INSTALL.vcxproj + WORKING_DIRECTORY "${BINARY_DIR}" + ) +endif () + if (${DEPS_BITS} EQUAL 32) set(DEP_WXWIDGETS_TARGET "") @@ -208,6 +252,51 @@ else () set(DEP_WXWIDGETS_LIBDIR "vc_x64_lib") endif () +find_package(Git REQUIRED) + +ExternalProject_Add(dep_libigl + EXCLUDE_FROM_ALL 1 + URL "https://github.com/libigl/libigl/archive/v2.0.0.tar.gz" + URL_HASH SHA256=42518e6b106c7209c73435fd260ed5d34edeb254852495b4c95dce2d95401328 + CMAKE_GENERATOR "${DEP_MSVC_GEN}" + CMAKE_ARGS + -DCMAKE_INSTALL_PREFIX=${DESTDIR}/usr/local + -DLIBIGL_BUILD_PYTHON=OFF + -DLIBIGL_BUILD_TESTS=OFF + -DLIBIGL_BUILD_TUTORIALS=OFF + -DLIBIGL_USE_STATIC_LIBRARY=${DEP_BUILD_IGL_STATIC} + -DLIBIGL_WITHOUT_COPYLEFT=OFF + -DLIBIGL_WITH_CGAL=OFF + -DLIBIGL_WITH_COMISO=OFF + -DLIBIGL_WITH_CORK=OFF + -DLIBIGL_WITH_EMBREE=OFF + -DLIBIGL_WITH_MATLAB=OFF + -DLIBIGL_WITH_MOSEK=OFF + -DLIBIGL_WITH_OPENGL=OFF + -DLIBIGL_WITH_OPENGL_GLFW=OFF + -DLIBIGL_WITH_OPENGL_GLFW_IMGUI=OFF + -DLIBIGL_WITH_PNG=OFF + -DLIBIGL_WITH_PYTHON=OFF + -DLIBIGL_WITH_TETGEN=OFF + -DLIBIGL_WITH_TRIANGLE=OFF + -DLIBIGL_WITH_XML=OFF + -DCMAKE_POSITION_INDEPENDENT_CODE=ON + -DCMAKE_DEBUG_POSTFIX=d + PATCH_COMMAND ${GIT_EXECUTABLE} apply --ignore-space-change --ignore-whitespace ${CMAKE_CURRENT_SOURCE_DIR}/igl-fixes.patch + BUILD_COMMAND msbuild /m /P:Configuration=Release INSTALL.vcxproj + INSTALL_COMMAND "" +) + +if (${DEP_DEBUG}) + ExternalProject_Get_Property(dep_libigl BINARY_DIR) + ExternalProject_Add_Step(dep_libigl build_debug + DEPENDEES build + DEPENDERS install + COMMAND msbuild /m /P:Configuration=Debug INSTALL.vcxproj + WORKING_DIRECTORY "${BINARY_DIR}" + ) +endif () + ExternalProject_Add(dep_wxwidgets EXCLUDE_FROM_ALL 1 GIT_REPOSITORY "https://github.com/prusa3d/wxWidgets" diff --git a/deps/igl-fixes.patch b/deps/igl-fixes.patch new file mode 100644 index 0000000000..b0ff9205d0 --- /dev/null +++ b/deps/igl-fixes.patch @@ -0,0 +1,128 @@ +diff --git a/cmake/libigl-config.cmake.in b/cmake/libigl-config.cmake.in +index 317c745c..f9808e1e 100644 +--- a/cmake/libigl-config.cmake.in ++++ b/cmake/libigl-config.cmake.in +@@ -2,28 +2,28 @@ + + include(${CMAKE_CURRENT_LIST_DIR}/libigl-export.cmake) + +-if (TARGET igl::core) +- if (NOT TARGET Eigen3::Eigen) +- find_package(Eigen3 QUIET) +- if (NOT Eigen3_FOUND) +- # try with PkgCOnfig +- find_package(PkgConfig REQUIRED) +- pkg_check_modules(Eigen3 QUIET IMPORTED_TARGET eigen3) +- endif() +- +- if (NOT Eigen3_FOUND) +- message(FATAL_ERROR "Could not find required dependency Eigen3") +- set(libigl_core_FOUND FALSE) +- else() +- target_link_libraries(igl::core INTERFACE PkgConfig::Eigen3) +- set(libigl_core_FOUND TRUE) +- endif() +- else() +- target_link_libraries(igl::core INTERFACE Eigen3::Eigen) +- set(libigl_core_FOUND TRUE) +- endif() +- +-endif() ++# if (TARGET igl::core) ++# if (NOT TARGET Eigen3::Eigen) ++# find_package(Eigen3 QUIET) ++# if (NOT Eigen3_FOUND) ++# # try with PkgCOnfig ++# find_package(PkgConfig REQUIRED) ++# pkg_check_modules(Eigen3 QUIET IMPORTED_TARGET eigen3) ++# endif() ++# ++# if (NOT Eigen3_FOUND) ++# message(FATAL_ERROR "Could not find required dependency Eigen3") ++# set(libigl_core_FOUND FALSE) ++# else() ++# target_link_libraries(igl::core INTERFACE PkgConfig::Eigen3) ++# set(libigl_core_FOUND TRUE) ++# endif() ++# else() ++# target_link_libraries(igl::core INTERFACE Eigen3::Eigen) ++# set(libigl_core_FOUND TRUE) ++# endif() ++# ++# endif() + + check_required_components(libigl) + +diff --git a/cmake/libigl.cmake b/cmake/libigl.cmake +index 4b11007a..47e6c395 100644 +--- a/cmake/libigl.cmake ++++ b/cmake/libigl.cmake +@@ -445,6 +445,7 @@ function(install_dir_files dir_name) + if(NOT LIBIGL_USE_STATIC_LIBRARY) + file(GLOB public_sources + ${CMAKE_CURRENT_SOURCE_DIR}/include/igl${subpath}/*.cpp ++ ${CMAKE_CURRENT_SOURCE_DIR}/include/igl${subpath}/*.c + ) + endif() + list(APPEND files_to_install ${public_sources}) +diff --git a/include/igl/AABB.cpp b/include/igl/AABB.cpp +index 09537335..92e90cb7 100644 +--- a/include/igl/AABB.cpp ++++ b/include/igl/AABB.cpp +@@ -1071,5 +1071,11 @@ template void igl::AABB, 3>::init, 2>::init >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&); + template double igl::AABB, 3>::squared_distance >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::Matrix const&, double, int&, Eigen::PlainObjectBase >&) const; ++template float igl::AABB const, 0, Eigen::Stride<0, 0> >, 3>::squared_distance const, 0, Eigen::Stride<0, 0> > >(Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::Matrix const&, int&, Eigen::PlainObjectBase >&) const; + template bool igl::AABB, 3>::intersect_ray >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::Matrix const&, Eigen::Matrix const&, igl::Hit&) const; ++template bool igl::AABB, 3>::intersect_ray >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::Matrix const&, Eigen::Matrix const&, std::vector&) const; ++ ++template void igl::AABB const, 0, Eigen::Stride<0, 0> >, 3>::init const, 0, Eigen::Stride<0, 0> > >(Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&); ++ ++template bool igl::AABB const, 0, Eigen::Stride<0, 0> >, 3>::intersect_ray const, 0, Eigen::Stride<0, 0> > >(Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::Matrix const&, Eigen::Matrix const&, std::vector >&) const; + #endif +diff --git a/include/igl/barycenter.cpp b/include/igl/barycenter.cpp +index 065f82aa..ec2d96cd 100644 +--- a/include/igl/barycenter.cpp ++++ b/include/igl/barycenter.cpp +@@ -54,4 +54,6 @@ template void igl::barycenter, Eigen::M + template void igl::barycenter, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&); + template void igl::barycenter, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&); + template void igl::barycenter, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&); ++ ++template void igl::barycenter const, 0, Eigen::Stride<0, 0> >, Eigen::Map const, 0, Eigen::Stride<0, 0> >, Eigen::Matrix >(Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::PlainObjectBase >&); + #endif +diff --git a/include/igl/point_simplex_squared_distance.cpp b/include/igl/point_simplex_squared_distance.cpp +index 2b98bd28..c66d9ae1 100644 +--- a/include/igl/point_simplex_squared_distance.cpp ++++ b/include/igl/point_simplex_squared_distance.cpp +@@ -178,4 +178,6 @@ template void igl::point_simplex_squared_distance<3, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, double, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::Matrix::Index, double&, Eigen::MatrixBase >&, Eigen::PlainObjectBase >&); + template void igl::point_simplex_squared_distance<2, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, double, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::Matrix::Index, double&, Eigen::MatrixBase >&, Eigen::PlainObjectBase >&); + template void igl::point_simplex_squared_distance<2, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix, double, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::Matrix::Index, double&, Eigen::MatrixBase >&, Eigen::PlainObjectBase >&); ++ ++template void igl::point_simplex_squared_distance<3, Eigen::Matrix, Eigen::Map const, 0, Eigen::Stride<0, 0> >, Eigen::Map const, 0, Eigen::Stride<0, 0> >, float, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::Map const, 0, Eigen::Stride<0, 0> >::Index, float&, Eigen::MatrixBase >&); + #endif +diff --git a/include/igl/ray_box_intersect.cpp b/include/igl/ray_box_intersect.cpp +index 4a88b89e..b547f8f8 100644 +--- a/include/igl/ray_box_intersect.cpp ++++ b/include/igl/ray_box_intersect.cpp +@@ -147,4 +147,6 @@ IGL_INLINE bool igl::ray_box_intersect( + #ifdef IGL_STATIC_LIBRARY + // Explicit template instantiation + template bool igl::ray_box_intersect, Eigen::Matrix, double>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::AlignedBox const&, double const&, double const&, double&, double&); ++ ++template bool igl::ray_box_intersect, Eigen::Matrix, float>(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::AlignedBox const&, float const&, float const&, float&, float&); + #endif +diff --git a/include/igl/ray_mesh_intersect.cpp b/include/igl/ray_mesh_intersect.cpp +index 9a70a22b..4233e722 100644 +--- a/include/igl/ray_mesh_intersect.cpp ++++ b/include/igl/ray_mesh_intersect.cpp +@@ -83,4 +83,7 @@ IGL_INLINE bool igl::ray_mesh_intersect( + template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, std::vector >&); + template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, igl::Hit&); + template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Block const, 1, -1, false> >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase const, 1, -1, false> > const&, igl::Hit&); ++template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Matrix, Eigen::Block const, 1, -1, false> >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase const, 1, -1, false> > const&, std::vector >&); ++ ++template bool igl::ray_mesh_intersect, Eigen::Matrix, Eigen::Map const, 0, Eigen::Stride<0, 0> >, Eigen::Block const, 0, Eigen::Stride<0, 0> > const, 1, -1, true> >(Eigen::MatrixBase > const&, Eigen::MatrixBase > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > > const&, Eigen::MatrixBase const, 0, Eigen::Stride<0, 0> > const, 1, -1, true> > const&, std::vector >&); + #endif diff --git a/deps/qhull-mods.patch b/deps/qhull-mods.patch new file mode 100644 index 0000000000..94aeeca2f5 --- /dev/null +++ b/deps/qhull-mods.patch @@ -0,0 +1,121 @@ +From a31ae4781a4afa60e21c70e5b4ae784bcd447c8a Mon Sep 17 00:00:00 2001 +From: tamasmeszaros +Date: Thu, 6 Jun 2019 15:41:43 +0200 +Subject: [PATCH] prusa-slicer changes + +--- + CMakeLists.txt | 44 +++++++++++++++++++++++++++++++++++--- + Config.cmake.in | 2 ++ + src/libqhull_r/qhull_r-exports.def | 2 ++ + src/libqhull_r/user_r.h | 2 +- + 4 files changed, 46 insertions(+), 4 deletions(-) + create mode 100644 Config.cmake.in + +diff --git a/CMakeLists.txt b/CMakeLists.txt +index 59dff41..20c2ec5 100644 +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -61,7 +61,7 @@ + # $DateTime: 2016/01/18 19:29:17 $$Author: bbarber $ + + project(qhull) +-cmake_minimum_required(VERSION 2.6) ++cmake_minimum_required(VERSION 3.0) + + # Define qhull_VERSION in CMakeLists.txt, Makefile, qhull-exports.def, qhull_p-exports.def, qhull_r-exports.def, qhull-warn.pri + set(qhull_VERSION2 "2015.2 2016/01/18") # not used, See global.c, global_r.c, rbox.c, rbox_r.c +@@ -610,10 +610,48 @@ add_test(NAME user_eg3 + # Define install + # --------------------------------------- + +-install(TARGETS ${qhull_TARGETS_INSTALL} ++install(TARGETS ${qhull_TARGETS_INSTALL} EXPORT QhullTargets + RUNTIME DESTINATION ${BIN_INSTALL_DIR} + LIBRARY DESTINATION ${LIB_INSTALL_DIR} +- ARCHIVE DESTINATION ${LIB_INSTALL_DIR}) ++ ARCHIVE DESTINATION ${LIB_INSTALL_DIR} ++ INCLUDES DESTINATION include) ++ ++include(CMakePackageConfigHelpers) ++ ++write_basic_package_version_file( ++ "${CMAKE_CURRENT_BINARY_DIR}/Qhull/QhullConfigVersion.cmake" ++ VERSION ${qhull_VERSION} ++ COMPATIBILITY AnyNewerVersion ++) ++ ++export(EXPORT QhullTargets ++ FILE "${CMAKE_CURRENT_BINARY_DIR}/Qhull/QhullTargets.cmake" ++ NAMESPACE Qhull:: ++) ++ ++configure_file(Config.cmake.in ++ "${CMAKE_CURRENT_BINARY_DIR}/Qhull/QhullConfig.cmake" ++ @ONLY ++) ++ ++set(ConfigPackageLocation lib/cmake/Qhull) ++install(EXPORT QhullTargets ++ FILE ++ QhullTargets.cmake ++ NAMESPACE ++ Qhull:: ++ DESTINATION ++ ${ConfigPackageLocation} ++) ++install( ++ FILES ++ "${CMAKE_CURRENT_BINARY_DIR}/Qhull/QhullConfig.cmake" ++ "${CMAKE_CURRENT_BINARY_DIR}/Qhull/QhullConfigVersion.cmake" ++ DESTINATION ++ ${ConfigPackageLocation} ++ COMPONENT ++ Devel ++) + + install(FILES ${libqhull_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/libqhull) + install(FILES ${libqhull_DOC} DESTINATION ${INCLUDE_INSTALL_DIR}/libqhull) +diff --git a/Config.cmake.in b/Config.cmake.in +new file mode 100644 +index 0000000..bc92bfe +--- /dev/null ++++ b/Config.cmake.in +@@ -0,0 +1,2 @@ ++include("${CMAKE_CURRENT_LIST_DIR}/QhullTargets.cmake") ++ +diff --git a/src/libqhull_r/qhull_r-exports.def b/src/libqhull_r/qhull_r-exports.def +index 325d57c..72f6ad0 100644 +--- a/src/libqhull_r/qhull_r-exports.def ++++ b/src/libqhull_r/qhull_r-exports.def +@@ -185,6 +185,7 @@ qh_memsetup + qh_memsize + qh_memstatistics + qh_memtotal ++qh_memcheck + qh_merge_degenredundant + qh_merge_nonconvex + qh_mergecycle +@@ -372,6 +373,7 @@ qh_settruncate + qh_setunique + qh_setvoronoi_all + qh_setzero ++qh_setendpointer + qh_sharpnewfacets + qh_skipfacet + qh_skipfilename +diff --git a/src/libqhull_r/user_r.h b/src/libqhull_r/user_r.h +index fc105b9..7cca65a 100644 +--- a/src/libqhull_r/user_r.h ++++ b/src/libqhull_r/user_r.h +@@ -139,7 +139,7 @@ Code flags -- + REALfloat = 1 all numbers are 'float' type + = 0 all numbers are 'double' type + */ +-#define REALfloat 0 ++#define REALfloat 1 + + #if (REALfloat == 1) + #define realT float +-- +2.16.2.windows.1 + diff --git a/resources/icons/mirroring_off.png b/resources/icons/mirroring_off.png new file mode 100644 index 0000000000..c16655271a Binary files /dev/null and b/resources/icons/mirroring_off.png differ diff --git a/resources/icons/mirroring_on.png b/resources/icons/mirroring_on.png new file mode 100644 index 0000000000..6ddeccbe0a Binary files /dev/null and b/resources/icons/mirroring_on.png differ diff --git a/resources/icons/mirroring_transparent.png b/resources/icons/mirroring_transparent.png new file mode 100644 index 0000000000..841010fcc1 Binary files /dev/null and b/resources/icons/mirroring_transparent.png differ diff --git a/resources/shaders/variable_layer_height.vs b/resources/shaders/variable_layer_height.vs index 9763859d04..4f98dfa56e 100644 --- a/resources/shaders/variable_layer_height.vs +++ b/resources/shaders/variable_layer_height.vs @@ -15,6 +15,7 @@ const vec3 LIGHT_FRONT_DIR = vec3(0.6985074, 0.1397015, 0.6985074); #define INTENSITY_AMBIENT 0.3 uniform mat4 volume_world_matrix; +uniform float object_max_z; // x = tainted, y = specular; varying vec2 intensity; @@ -42,6 +43,12 @@ void main() intensity.x += NdotL * LIGHT_FRONT_DIFFUSE; // Scaled to widths of the Z texture. - object_z = (volume_world_matrix * gl_Vertex).z; + if (object_max_z > 0.0) + // when rendering the overlay + object_z = object_max_z * gl_MultiTexCoord0.y; + else + // when rendering the volumes + object_z = (volume_world_matrix * gl_Vertex).z; + gl_Position = ftransform(); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4f6959623b..61faa05710 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -12,14 +12,12 @@ add_subdirectory(poly2tri) add_subdirectory(qhull) add_subdirectory(Shiny) add_subdirectory(semver) +add_subdirectory(libigl) # Adding libnest2d project for bin packing... set(LIBNEST2D_UNITTESTS ON CACHE BOOL "Force generating unittests for libnest2d") add_subdirectory(libnest2d) -include_directories(${LIBDIR}/qhull/src) -#message(STATUS ${LIBDIR}/qhull/src) - add_subdirectory(libslic3r) if (SLIC3R_GUI) @@ -142,7 +140,7 @@ if (MSVC) target_compile_definitions(PrusaSlicer_app_gui PRIVATE -DSLIC3R_WRAPPER_NOCONSOLE) add_dependencies(PrusaSlicer_app_gui PrusaSlicer) set_target_properties(PrusaSlicer_app_gui PROPERTIES OUTPUT_NAME "prusa-slicer") - target_include_directories(PrusaSlicer_app_gui SYSTEM PUBLIC ${Boost_INCLUDE_DIRS}) + target_link_libraries(PrusaSlicer_app_gui PRIVATE boost_headeronly) add_executable(PrusaSlicer_app_console PrusaSlicer_app_msvc.cpp ${CMAKE_CURRENT_BINARY_DIR}/PrusaSlicer.rc) # Generate debug symbols even in release mode. @@ -150,7 +148,7 @@ if (MSVC) target_compile_definitions(PrusaSlicer_app_console PRIVATE -DSLIC3R_WRAPPER_CONSOLE) add_dependencies(PrusaSlicer_app_console PrusaSlicer) set_target_properties(PrusaSlicer_app_console PROPERTIES OUTPUT_NAME "prusa-slicer-console") - target_include_directories(PrusaSlicer_app_console SYSTEM PUBLIC ${Boost_INCLUDE_DIRS}) + target_link_libraries(PrusaSlicer_app_console PRIVATE boost_headeronly) endif () # Link the resources dir to where Slic3r GUI expects it diff --git a/src/PrusaSlicer.cpp b/src/PrusaSlicer.cpp index fef1f6e7f0..2becb8071a 100644 --- a/src/PrusaSlicer.cpp +++ b/src/PrusaSlicer.cpp @@ -7,10 +7,13 @@ #include #include #ifdef SLIC3R_GUI + extern "C" + { // Let the NVIDIA and AMD know we want to use their graphics card // on a dual graphics card system. __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; + } #endif /* SLIC3R_GUI */ #endif /* WIN32 */ @@ -241,8 +244,7 @@ int CLI::run(int argc, char **argv) } else if (opt_key == "cut" || opt_key == "cut_x" || opt_key == "cut_y") { std::vector new_models; for (auto &model : m_models) { - model.repair(); - model.translate(0, 0, -model.bounding_box().min.z()); // align to z = 0 + model.translate(0, 0, -model.bounding_box().min.z()); // align to z = 0 size_t num_objects = model.objects.size(); for (size_t i = 0; i < num_objects; ++ i) { @@ -301,8 +303,9 @@ int CLI::run(int argc, char **argv) } } } else if (opt_key == "repair") { - for (auto &model : m_models) - model.repair(); + // Models are repaired by default. + //for (auto &model : m_models) + // model.repair(); } else { boost::nowide::cerr << "error: option not implemented yet: " << opt_key << std::endl; return 1; diff --git a/src/PrusaSlicer_app_msvc.cpp b/src/PrusaSlicer_app_msvc.cpp index 5b01751b9d..95dd4fb075 100644 --- a/src/PrusaSlicer_app_msvc.cpp +++ b/src/PrusaSlicer_app_msvc.cpp @@ -8,10 +8,13 @@ #include #ifdef SLIC3R_GUI +extern "C" +{ // Let the NVIDIA and AMD know we want to use their graphics card // on a dual graphics card system. __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001; __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; +} #endif /* SLIC3R_GUI */ #include diff --git a/src/admesh/CMakeLists.txt b/src/admesh/CMakeLists.txt index 941a7eeb55..7d0177782f 100644 --- a/src/admesh/CMakeLists.txt +++ b/src/admesh/CMakeLists.txt @@ -11,4 +11,4 @@ add_library(admesh STATIC util.cpp ) -target_include_directories(admesh SYSTEM PRIVATE ${Boost_INCLUDE_DIRS}) +target_link_libraries(admesh PRIVATE boost_headeronly) diff --git a/src/admesh/connect.cpp b/src/admesh/connect.cpp index fb32132194..e729c89229 100644 --- a/src/admesh/connect.cpp +++ b/src/admesh/connect.cpp @@ -28,894 +28,729 @@ #include #include -#include +#include +#include +// Boost pool: Don't use mutexes to synchronize memory allocation. +#define BOOST_POOL_NO_MT +#include #include "stl.h" +struct HashEdge { + // Key of a hash edge: sorted vertices of the edge. + uint32_t key[6]; + // Compare two keys. + bool operator==(const HashEdge &rhs) const { return memcmp(key, rhs.key, sizeof(key)) == 0; } + bool operator!=(const HashEdge &rhs) const { return ! (*this == rhs); } + int hash(int M) const { return ((key[0] / 11 + key[1] / 7 + key[2] / 3) ^ (key[3] / 11 + key[4] / 7 + key[5] / 3)) % M; } -static void stl_match_neighbors_nearby(stl_file *stl, - stl_hash_edge *edge_a, stl_hash_edge *edge_b); -static void stl_record_neighbors(stl_file *stl, - stl_hash_edge *edge_a, stl_hash_edge *edge_b); -static void stl_initialize_facet_check_exact(stl_file *stl); -static void stl_initialize_facet_check_nearby(stl_file *stl); -static void stl_load_edge_exact(stl_file *stl, stl_hash_edge *edge, const stl_vertex *a, const stl_vertex *b); -static int stl_load_edge_nearby(stl_file *stl, stl_hash_edge *edge, - stl_vertex *a, stl_vertex *b, float tolerance); -static void insert_hash_edge(stl_file *stl, stl_hash_edge edge, - void (*match_neighbors)(stl_file *stl, - stl_hash_edge *edge_a, stl_hash_edge *edge_b)); -static int stl_compare_function(stl_hash_edge *edge_a, stl_hash_edge *edge_b); -static void stl_free_edges(stl_file *stl); -static void stl_remove_facet(stl_file *stl, int facet_number); -static void stl_change_vertices(stl_file *stl, int facet_num, int vnot, - stl_vertex new_vertex); -static void stl_which_vertices_to_change(stl_file *stl, stl_hash_edge *edge_a, - stl_hash_edge *edge_b, int *facet1, int *vertex1, - int *facet2, int *vertex2, - stl_vertex *new_vertex1, stl_vertex *new_vertex2); -static void stl_remove_degenerate(stl_file *stl, int facet); -extern int stl_check_normal_vector(stl_file *stl, - int facet_num, int normal_fix_flag); -static void stl_update_connects_remove_1(stl_file *stl, int facet_num); + // Index of a facet owning this edge. + int facet_number; + // Index of this edge inside the facet with an index of facet_number. + // If this edge is stored backwards, which_edge is increased by 3. + int which_edge; + HashEdge *next; + + void load_exact(stl_file *stl, const stl_vertex *a, const stl_vertex *b) + { + { + stl_vertex diff = (*a - *b).cwiseAbs(); + float max_diff = std::max(diff(0), std::max(diff(1), diff(2))); + stl->stats.shortest_edge = std::min(max_diff, stl->stats.shortest_edge); + } + + // Ensure identical vertex ordering of equal edges. + // This method is numerically robust. + if (vertex_lower(*a, *b)) { + } else { + // This edge is loaded backwards. + std::swap(a, b); + this->which_edge += 3; + } + memcpy(&this->key[0], a->data(), sizeof(stl_vertex)); + memcpy(&this->key[3], b->data(), sizeof(stl_vertex)); + // Switch negative zeros to positive zeros, so memcmp will consider them to be equal. + for (size_t i = 0; i < 6; ++ i) { + unsigned char *p = (unsigned char*)(this->key + i); + #if BOOST_ENDIAN_LITTLE_BYTE + if (p[0] == 0 && p[1] == 0 && p[2] == 0 && p[3] == 0x80) + // Negative zero, switch to positive zero. + p[3] = 0; + #else /* BOOST_ENDIAN_LITTLE_BYTE */ + if (p[0] == 0x80 && p[1] == 0 && p[2] == 0 && p[3] == 0) + // Negative zero, switch to positive zero. + p[0] = 0; + #endif /* BOOST_ENDIAN_LITTLE_BYTE */ + } + } + + bool load_nearby(const stl_file *stl, const stl_vertex &a, const stl_vertex &b, float tolerance) + { + // Index of a grid cell spaced by tolerance. + typedef Eigen::Matrix Vec3i; + Vec3i vertex1 = ((a - stl->stats.min) / tolerance).cast(); + Vec3i vertex2 = ((b - stl->stats.min) / tolerance).cast(); + static_assert(sizeof(Vec3i) == 12, "size of Vec3i incorrect"); + + if (vertex1 == vertex2) + // Both vertices hash to the same value + return false; + + // Ensure identical vertex ordering of edges, which vertices land into equal grid cells. + // This method is numerically robust. + if ((vertex1[0] != vertex2[0]) ? + (vertex1[0] < vertex2[0]) : + ((vertex1[1] != vertex2[1]) ? + (vertex1[1] < vertex2[1]) : + (vertex1[2] < vertex2[2]))) { + memcpy(&this->key[0], vertex1.data(), sizeof(stl_vertex)); + memcpy(&this->key[3], vertex2.data(), sizeof(stl_vertex)); + } else { + memcpy(&this->key[0], vertex2.data(), sizeof(stl_vertex)); + memcpy(&this->key[3], vertex1.data(), sizeof(stl_vertex)); + this->which_edge += 3; /* this edge is loaded backwards */ + } + return true; + } + +private: + inline bool vertex_lower(const stl_vertex &a, const stl_vertex &b) { + return (a(0) != b(0)) ? (a(0) < b(0)) : + ((a(1) != b(1)) ? (a(1) < b(1)) : (a(2) < b(2))); + } +}; + +struct HashTableEdges { + HashTableEdges(size_t number_of_faces) { + this->M = (int)hash_size_from_nr_faces(number_of_faces); + this->heads.assign(this->M, nullptr); + this->tail = pool.construct(); + this->tail->next = this->tail; + for (int i = 0; i < this->M; ++ i) + this->heads[i] = this->tail; + } + ~HashTableEdges() { +#ifndef NDEBUG + for (int i = 0; i < this->M; ++ i) + for (HashEdge *temp = this->heads[i]; this->heads[i] != this->tail; temp = this->heads[i]) + ++ this->freed; + this->tail = nullptr; +#endif /* NDEBUG */ + } + + void insert_edge_exact(stl_file *stl, const HashEdge &edge) + { + this->insert_edge(stl, edge, [stl](const HashEdge& edge1, const HashEdge& edge2) { record_neighbors(stl, edge1, edge2); }); + } + + void insert_edge_nearby(stl_file *stl, const HashEdge &edge) + { + this->insert_edge(stl, edge, [stl](const HashEdge& edge1, const HashEdge& edge2) { match_neighbors_nearby(stl, edge1, edge2); }); + } + + // Hash table on edges + std::vector heads; + HashEdge* tail; + int M; + boost::object_pool pool; + +#ifndef NDEBUG + size_t malloced = 0; + size_t freed = 0; + size_t collisions = 0; +#endif /* NDEBUG */ + +private: + static inline size_t hash_size_from_nr_faces(const size_t nr_faces) + { + // Good primes for addressing a cca. 30 bit space. + // https://planetmath.org/goodhashtableprimes + static std::vector primes{ 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741 }; + // Find a prime number for 50% filling of the shared triangle edges in the mesh. + auto it = std::upper_bound(primes.begin(), primes.end(), nr_faces * 3 * 2 - 1); + return (it == primes.end()) ? primes.back() : *it; + } + + + // MatchNeighbors(stl_file *stl, const HashEdge &edge_a, const HashEdge &edge_b) + template + void insert_edge(stl_file *stl, const HashEdge &edge, MatchNeighbors match_neighbors) + { + int chain_number = edge.hash(this->M); + HashEdge *link = this->heads[chain_number]; + if (link == this->tail) { + // This list doesn't have any edges currently in it. Add this one. + HashEdge *new_edge = pool.construct(edge); +#ifndef NDEBUG + ++ this->malloced; +#endif /* NDEBUG */ + new_edge->next = this->tail; + this->heads[chain_number] = new_edge; + } else if (edges_equal(edge, *link)) { + // This is a match. Record result in neighbors list. + match_neighbors(edge, *link); + // Delete the matched edge from the list. + this->heads[chain_number] = link->next; + // pool.destroy(link); +#ifndef NDEBUG + ++ this->freed; +#endif /* NDEBUG */ + } else { + // Continue through the rest of the list. + for (;;) { + if (link->next == this->tail) { + // This is the last item in the list. Insert a new edge. + HashEdge *new_edge = pool.construct(); +#ifndef NDEBUG + ++ this->malloced; +#endif /* NDEBUG */ + *new_edge = edge; + new_edge->next = this->tail; + link->next = new_edge; +#ifndef NDEBUG + ++ this->collisions; +#endif /* NDEBUG */ + break; + } + if (edges_equal(edge, *link->next)) { + // This is a match. Record result in neighbors list. + match_neighbors(edge, *link->next); + // Delete the matched edge from the list. + HashEdge *temp = link->next; + link->next = link->next->next; + // pool.destroy(temp); +#ifndef NDEBUG + ++ this->freed; +#endif /* NDEBUG */ + break; + } + // This is not a match. Go to the next link. + link = link->next; +#ifndef NDEBUG + ++ this->collisions; +#endif /* NDEBUG */ + } + } + } + + // Edges equal for hashing. Edgesof different facet are allowed to be matched. + static inline bool edges_equal(const HashEdge &edge_a, const HashEdge &edge_b) + { + return edge_a.facet_number != edge_b.facet_number && edge_a == edge_b; + } + + static void record_neighbors(stl_file *stl, const HashEdge &edge_a, const HashEdge &edge_b) + { + // Facet a's neighbor is facet b + stl->neighbors_start[edge_a.facet_number].neighbor[edge_a.which_edge % 3] = edge_b.facet_number; /* sets the .neighbor part */ + stl->neighbors_start[edge_a.facet_number].which_vertex_not[edge_a.which_edge % 3] = (edge_b.which_edge + 2) % 3; /* sets the .which_vertex_not part */ + + // Facet b's neighbor is facet a + stl->neighbors_start[edge_b.facet_number].neighbor[edge_b.which_edge % 3] = edge_a.facet_number; /* sets the .neighbor part */ + stl->neighbors_start[edge_b.facet_number].which_vertex_not[edge_b.which_edge % 3] = (edge_a.which_edge + 2) % 3; /* sets the .which_vertex_not part */ + + if (((edge_a.which_edge < 3) && (edge_b.which_edge < 3)) || ((edge_a.which_edge > 2) && (edge_b.which_edge > 2))) { + // These facets are oriented in opposite directions, their normals are probably messed up. + stl->neighbors_start[edge_a.facet_number].which_vertex_not[edge_a.which_edge % 3] += 3; + stl->neighbors_start[edge_b.facet_number].which_vertex_not[edge_b.which_edge % 3] += 3; + } + + // Count successful connects: + // Total connects: + stl->stats.connected_edges += 2; + // Count individual connects: + switch (stl->neighbors_start[edge_a.facet_number].num_neighbors()) { + case 1: ++ stl->stats.connected_facets_1_edge; break; + case 2: ++ stl->stats.connected_facets_2_edge; break; + case 3: ++ stl->stats.connected_facets_3_edge; break; + default: assert(false); + } + switch (stl->neighbors_start[edge_b.facet_number].num_neighbors()) { + case 1: ++ stl->stats.connected_facets_1_edge; break; + case 2: ++ stl->stats.connected_facets_2_edge; break; + case 3: ++ stl->stats.connected_facets_3_edge; break; + default: assert(false); + } + } + + static void match_neighbors_nearby(stl_file *stl, const HashEdge &edge_a, const HashEdge &edge_b) + { + record_neighbors(stl, edge_a, edge_b); + + // Which vertices to change + int facet1 = -1; + int facet2 = -1; + int vertex1, vertex2; + stl_vertex new_vertex1, new_vertex2; + { + int v1a; // pair 1, facet a + int v1b; // pair 1, facet b + int v2a; // pair 2, facet a + int v2b; // pair 2, facet b + // Find first pair. + if (edge_a.which_edge < 3) { + v1a = edge_a.which_edge; + v2a = (edge_a.which_edge + 1) % 3; + } else { + v2a = edge_a.which_edge % 3; + v1a = (edge_a.which_edge + 1) % 3; + } + if (edge_b.which_edge < 3) { + v1b = edge_b.which_edge; + v2b = (edge_b.which_edge + 1) % 3; + } else { + v2b = edge_b.which_edge % 3; + v1b = (edge_b.which_edge + 1) % 3; + } + + // Of the first pair, which vertex, if any, should be changed + if (stl->facet_start[edge_a.facet_number].vertex[v1a] != stl->facet_start[edge_b.facet_number].vertex[v1b]) { + // These facets are different. + if ( (stl->neighbors_start[edge_a.facet_number].neighbor[v1a] == -1) + && (stl->neighbors_start[edge_a.facet_number].neighbor[(v1a + 2) % 3] == -1)) { + // This vertex has no neighbors. This is a good one to change. + facet1 = edge_a.facet_number; + vertex1 = v1a; + new_vertex1 = stl->facet_start[edge_b.facet_number].vertex[v1b]; + } else { + facet1 = edge_b.facet_number; + vertex1 = v1b; + new_vertex1 = stl->facet_start[edge_a.facet_number].vertex[v1a]; + } + } + + // Of the second pair, which vertex, if any, should be changed. + if (stl->facet_start[edge_a.facet_number].vertex[v2a] == stl->facet_start[edge_b.facet_number].vertex[v2b]) { + // These facets are different. + if ( (stl->neighbors_start[edge_a.facet_number].neighbor[v2a] == -1) + && (stl->neighbors_start[edge_a.facet_number].neighbor[(v2a + 2) % 3] == -1)) { + // This vertex has no neighbors. This is a good one to change. + facet2 = edge_a.facet_number; + vertex2 = v2a; + new_vertex2 = stl->facet_start[edge_b.facet_number].vertex[v2b]; + } else { + facet2 = edge_b.facet_number; + vertex2 = v2b; + new_vertex2 = stl->facet_start[edge_a.facet_number].vertex[v2a]; + } + } + } + + auto change_vertices = [stl](int facet_num, int vnot, stl_vertex new_vertex) + { + int first_facet = facet_num; + bool direction = false; + + for (;;) { + int pivot_vertex; + int next_edge; + if (vnot > 2) { + if (direction) { + pivot_vertex = (vnot + 1) % 3; + next_edge = vnot % 3; + } + else { + pivot_vertex = (vnot + 2) % 3; + next_edge = pivot_vertex; + } + direction = !direction; + } + else { + if (direction) { + pivot_vertex = (vnot + 2) % 3; + next_edge = pivot_vertex; + } + else { + pivot_vertex = (vnot + 1) % 3; + next_edge = vnot; + } + } + #if 0 + if (stl->facet_start[facet_num].vertex[pivot_vertex](0) == new_vertex(0) && + stl->facet_start[facet_num].vertex[pivot_vertex](1) == new_vertex(1) && + stl->facet_start[facet_num].vertex[pivot_vertex](2) == new_vertex(2)) + printf("Changing vertex %f,%f,%f: Same !!!\r\n", new_vertex(0), new_vertex(1), new_vertex(2)); + else { + if (stl->facet_start[facet_num].vertex[pivot_vertex](0) != new_vertex(0)) + printf("Changing coordinate x, vertex %e (0x%08x) to %e(0x%08x)\r\n", + stl->facet_start[facet_num].vertex[pivot_vertex](0), + *reinterpret_cast(&stl->facet_start[facet_num].vertex[pivot_vertex](0)), + new_vertex(0), + *reinterpret_cast(&new_vertex(0))); + if (stl->facet_start[facet_num].vertex[pivot_vertex](1) != new_vertex(1)) + printf("Changing coordinate x, vertex %e (0x%08x) to %e(0x%08x)\r\n", + stl->facet_start[facet_num].vertex[pivot_vertex](1), + *reinterpret_cast(&stl->facet_start[facet_num].vertex[pivot_vertex](1)), + new_vertex(1), + *reinterpret_cast(&new_vertex(1))); + if (stl->facet_start[facet_num].vertex[pivot_vertex](2) != new_vertex(2)) + printf("Changing coordinate x, vertex %e (0x%08x) to %e(0x%08x)\r\n", + stl->facet_start[facet_num].vertex[pivot_vertex](2), + *reinterpret_cast(&stl->facet_start[facet_num].vertex[pivot_vertex](2)), + new_vertex(2), + *reinterpret_cast(&new_vertex(2))); + } + #endif + stl->facet_start[facet_num].vertex[pivot_vertex] = new_vertex; + vnot = stl->neighbors_start[facet_num].which_vertex_not[next_edge]; + facet_num = stl->neighbors_start[facet_num].neighbor[next_edge]; + if (facet_num == -1) + break; + + if (facet_num == first_facet) { + // back to the beginning + BOOST_LOG_TRIVIAL(info) << "Back to the first facet changing vertices: probably a mobius part. Try using a smaller tolerance or don't do a nearby check."; + return; + } + } + }; + + if (facet1 != -1) { + int vnot1 = (facet1 == edge_a.facet_number) ? + (edge_a.which_edge + 2) % 3 : + (edge_b.which_edge + 2) % 3; + if (((vnot1 + 2) % 3) == vertex1) + vnot1 += 3; + change_vertices(facet1, vnot1, new_vertex1); + } + if (facet2 != -1) { + int vnot2 = (facet2 == edge_a.facet_number) ? + (edge_a.which_edge + 2) % 3 : + (edge_b.which_edge + 2) % 3; + if (((vnot2 + 2) % 3) == vertex2) + vnot2 += 3; + change_vertices(facet2, vnot2, new_vertex2); + } + stl->stats.edges_fixed += 2; + } +}; // This function builds the neighbors list. No modifications are made // to any of the facets. The edges are said to match only if all six // floats of the first edge matches all six floats of the second edge. void stl_check_facets_exact(stl_file *stl) { - if (stl->error) - return; + stl->stats.connected_edges = 0; + stl->stats.connected_facets_1_edge = 0; + stl->stats.connected_facets_2_edge = 0; + stl->stats.connected_facets_3_edge = 0; - stl->stats.connected_edges = 0; - stl->stats.connected_facets_1_edge = 0; - stl->stats.connected_facets_2_edge = 0; - stl->stats.connected_facets_3_edge = 0; + // If any two of the three vertices are found to be exactally the same, call them degenerate and remove the facet. + // Do it before the next step, as the next step stores references to the face indices in the hash tables and removing a facet + // will break the references. + for (uint32_t i = 0; i < stl->stats.number_of_facets;) { + stl_facet &facet = stl->facet_start[i]; + if (facet.vertex[0] == facet.vertex[1] || facet.vertex[1] == facet.vertex[2] || facet.vertex[0] == facet.vertex[2]) { + // Remove the degenerate facet. + facet = stl->facet_start[-- stl->stats.number_of_facets]; + stl->facet_start.pop_back(); + stl->neighbors_start.pop_back(); + stl->stats.facets_removed += 1; + stl->stats.degenerate_facets += 1; + } else + ++ i; + } - // If any two of the three vertices are found to be exactally the same, call them degenerate and remove the facet. - // Do it before the next step, as the next step stores references to the face indices in the hash tables and removing a facet - // will break the references. - for (int i = 0; i < stl->stats.number_of_facets;) { - stl_facet &facet = stl->facet_start[i]; - if (facet.vertex[0] == facet.vertex[1] || facet.vertex[1] == facet.vertex[2] || facet.vertex[0] == facet.vertex[2]) { - // Remove the degenerate facet. - facet = stl->facet_start[--stl->stats.number_of_facets]; - stl->stats.facets_removed += 1; - stl->stats.degenerate_facets += 1; - } else - ++ i; - } + // Initialize hash table. + HashTableEdges hash_table(stl->stats.number_of_facets); + for (auto &neighbor : stl->neighbors_start) + neighbor.reset(); - // Connect neighbor edges. - stl_initialize_facet_check_exact(stl); - for (int i = 0; i < stl->stats.number_of_facets; i++) { - const stl_facet &facet = stl->facet_start[i]; - for (int j = 0; j < 3; j++) { - stl_hash_edge edge; - edge.facet_number = i; - edge.which_edge = j; - stl_load_edge_exact(stl, &edge, &facet.vertex[j], &facet.vertex[(j + 1) % 3]); - insert_hash_edge(stl, edge, stl_record_neighbors); - } - } - stl_free_edges(stl); + // Connect neighbor edges. + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + const stl_facet &facet = stl->facet_start[i]; + for (int j = 0; j < 3; ++ j) { + HashEdge edge; + edge.facet_number = i; + edge.which_edge = j; + edge.load_exact(stl, &facet.vertex[j], &facet.vertex[(j + 1) % 3]); + hash_table.insert_edge_exact(stl, edge); + } + } #if 0 - printf("Number of faces: %d, number of manifold edges: %d, number of connected edges: %d, number of unconnected edges: %d\r\n", - stl->stats.number_of_facets, stl->stats.number_of_facets * 3, - stl->stats.connected_edges, stl->stats.number_of_facets * 3 - stl->stats.connected_edges); + printf("Number of faces: %d, number of manifold edges: %d, number of connected edges: %d, number of unconnected edges: %d\r\n", + stl->stats.number_of_facets, stl->stats.number_of_facets * 3, + stl->stats.connected_edges, stl->stats.number_of_facets * 3 - stl->stats.connected_edges); #endif } -static void stl_load_edge_exact(stl_file *stl, stl_hash_edge *edge, const stl_vertex *a, const stl_vertex *b) { - - if (stl->error) return; - - { - stl_vertex diff = (*a - *b).cwiseAbs(); - float max_diff = std::max(diff(0), std::max(diff(1), diff(2))); - stl->stats.shortest_edge = std::min(max_diff, stl->stats.shortest_edge); - } - - // Ensure identical vertex ordering of equal edges. - // This method is numerically robust. - if (stl_vertex_lower(*a, *b)) { - } else { - std::swap(a, b); - edge->which_edge += 3; /* this edge is loaded backwards */ - } - memcpy(&edge->key[0], a->data(), sizeof(stl_vertex)); - memcpy(&edge->key[3], b->data(), sizeof(stl_vertex)); - // Switch negative zeros to positive zeros, so memcmp will consider them to be equal. - for (size_t i = 0; i < 6; ++ i) { - unsigned char *p = (unsigned char*)(edge->key + i); -#ifdef BOOST_LITTLE_ENDIAN - if (p[0] == 0 && p[1] == 0 && p[2] == 0 && p[3] == 0x80) - // Negative zero, switch to positive zero. - p[3] = 0; -#else /* BOOST_LITTLE_ENDIAN */ - if (p[0] == 0x80 && p[1] == 0 && p[2] == 0 && p[3] == 0) - // Negative zero, switch to positive zero. - p[0] = 0; -#endif /* BOOST_LITTLE_ENDIAN */ - } -} - -static inline size_t hash_size_from_nr_faces(const size_t nr_faces) -{ - // Good primes for addressing a cca. 30 bit space. - // https://planetmath.org/goodhashtableprimes - static std::vector primes{ 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843, 50331653, 100663319, 201326611, 402653189, 805306457, 1610612741 }; - // Find a prime number for 50% filling of the shared triangle edges in the mesh. - auto it = std::upper_bound(primes.begin(), primes.end(), nr_faces * 3 * 2 - 1); - return (it == primes.end()) ? primes.back() : *it; -} - -static void -stl_initialize_facet_check_exact(stl_file *stl) { - int i; - - if (stl->error) return; - - stl->stats.malloced = 0; - stl->stats.freed = 0; - stl->stats.collisions = 0; - - stl->M = hash_size_from_nr_faces(stl->stats.number_of_facets); - - for (i = 0; i < stl->stats.number_of_facets ; i++) { - /* initialize neighbors list to -1 to mark unconnected edges */ - stl->neighbors_start[i].neighbor[0] = -1; - stl->neighbors_start[i].neighbor[1] = -1; - stl->neighbors_start[i].neighbor[2] = -1; - } - - stl->heads = (stl_hash_edge**)calloc(stl->M, sizeof(*stl->heads)); - if(stl->heads == NULL) perror("stl_initialize_facet_check_exact"); - - stl->tail = (stl_hash_edge*)malloc(sizeof(stl_hash_edge)); - if(stl->tail == NULL) perror("stl_initialize_facet_check_exact"); - - stl->tail->next = stl->tail; - - for(i = 0; i < stl->M; i++) { - stl->heads[i] = stl->tail; - } -} - -static void insert_hash_edge(stl_file *stl, stl_hash_edge edge, - void (*match_neighbors)(stl_file *stl, - stl_hash_edge *edge_a, stl_hash_edge *edge_b)) -{ - if (stl->error) return; - - int chain_number = edge.hash(stl->M); - stl_hash_edge *link = stl->heads[chain_number]; - - stl_hash_edge *new_edge; - stl_hash_edge *temp; - if(link == stl->tail) { - /* This list doesn't have any edges currently in it. Add this one. */ - new_edge = (stl_hash_edge*)malloc(sizeof(stl_hash_edge)); - if(new_edge == NULL) perror("insert_hash_edge"); - stl->stats.malloced++; - *new_edge = edge; - new_edge->next = stl->tail; - stl->heads[chain_number] = new_edge; - return; - } else if(!stl_compare_function(&edge, link)) { - /* This is a match. Record result in neighbors list. */ - match_neighbors(stl, &edge, link); - /* Delete the matched edge from the list. */ - stl->heads[chain_number] = link->next; - free(link); - stl->stats.freed++; - return; - } else { - /* Continue through the rest of the list */ - for(;;) { - if(link->next == stl->tail) { - /* This is the last item in the list. Insert a new edge. */ - new_edge = (stl_hash_edge*)malloc(sizeof(stl_hash_edge)); - if(new_edge == NULL) perror("insert_hash_edge"); - stl->stats.malloced++; - *new_edge = edge; - new_edge->next = stl->tail; - link->next = new_edge; - stl->stats.collisions++; - return; - } else if(!stl_compare_function(&edge, link->next)) { - /* This is a match. Record result in neighbors list. */ - match_neighbors(stl, &edge, link->next); - - /* Delete the matched edge from the list. */ - temp = link->next; - link->next = link->next->next; - free(temp); - stl->stats.freed++; - return; - } else { - /* This is not a match. Go to the next link */ - link = link->next; - stl->stats.collisions++; - } - } - } -} - -// Return 1 if the edges are not matched. -static inline int stl_compare_function(stl_hash_edge *edge_a, stl_hash_edge *edge_b) -{ - // Don't match edges of the same facet - return (edge_a->facet_number == edge_b->facet_number) || (*edge_a != *edge_b); -} - void stl_check_facets_nearby(stl_file *stl, float tolerance) { - if (stl->error) - return; + if ( (stl->stats.connected_facets_1_edge == stl->stats.number_of_facets) + && (stl->stats.connected_facets_2_edge == stl->stats.number_of_facets) + && (stl->stats.connected_facets_3_edge == stl->stats.number_of_facets)) { + // No need to check any further. All facets are connected. + return; + } - if( (stl->stats.connected_facets_1_edge == stl->stats.number_of_facets) - && (stl->stats.connected_facets_2_edge == stl->stats.number_of_facets) - && (stl->stats.connected_facets_3_edge == stl->stats.number_of_facets)) { - /* No need to check any further. All facets are connected */ - return; - } - - stl_initialize_facet_check_nearby(stl); - - for (int i = 0; i < stl->stats.number_of_facets; ++ i) { - //FIXME is the copy necessary? - stl_facet facet = stl->facet_start[i]; - for (int j = 0; j < 3; j++) { - if(stl->neighbors_start[i].neighbor[j] == -1) { - stl_hash_edge edge; - edge.facet_number = i; - edge.which_edge = j; - if(stl_load_edge_nearby(stl, &edge, &facet.vertex[j], - &facet.vertex[(j + 1) % 3], - tolerance)) { - /* only insert edges that have different keys */ - insert_hash_edge(stl, edge, stl_match_neighbors_nearby); - } - } - } - } - - stl_free_edges(stl); -} - -static int stl_load_edge_nearby(stl_file *stl, stl_hash_edge *edge, stl_vertex *a, stl_vertex *b, float tolerance) -{ - // Index of a grid cell spaced by tolerance. - typedef Eigen::Matrix Vec3i; - Vec3i vertex1 = ((*a - stl->stats.min) / tolerance).cast(); - Vec3i vertex2 = ((*b - stl->stats.min) / tolerance).cast(); - static_assert(sizeof(Vec3i) == 12, "size of Vec3i incorrect"); - - if (vertex1 == vertex2) - // Both vertices hash to the same value - return 0; - - // Ensure identical vertex ordering of edges, which vertices land into equal grid cells. - // This method is numerically robust. - if ((vertex1[0] != vertex2[0]) ? - (vertex1[0] < vertex2[0]) : - ((vertex1[1] != vertex2[1]) ? - (vertex1[1] < vertex2[1]) : - (vertex1[2] < vertex2[2]))) { - memcpy(&edge->key[0], vertex1.data(), sizeof(stl_vertex)); - memcpy(&edge->key[3], vertex2.data(), sizeof(stl_vertex)); - } else { - memcpy(&edge->key[0], vertex2.data(), sizeof(stl_vertex)); - memcpy(&edge->key[3], vertex1.data(), sizeof(stl_vertex)); - edge->which_edge += 3; /* this edge is loaded backwards */ - } - return 1; -} - -static void stl_free_edges(stl_file *stl) -{ - if (stl->error) - return; - - if(stl->stats.malloced != stl->stats.freed) { - for (int i = 0; i < stl->M; i++) { - for (stl_hash_edge *temp = stl->heads[i]; stl->heads[i] != stl->tail; temp = stl->heads[i]) { - stl->heads[i] = stl->heads[i]->next; - free(temp); - ++ stl->stats.freed; - } - } - } - free(stl->heads); - stl->heads = nullptr; - free(stl->tail); - stl->tail = nullptr; -} - -static void stl_initialize_facet_check_nearby(stl_file *stl) -{ - int i; - - if (stl->error) return; - - stl->stats.malloced = 0; - stl->stats.freed = 0; - stl->stats.collisions = 0; - - /* tolerance = STL_MAX(stl->stats.shortest_edge, tolerance);*/ - /* tolerance = STL_MAX((stl->stats.bounding_diameter / 500000.0), tolerance);*/ - /* tolerance *= 0.5;*/ - - stl->M = hash_size_from_nr_faces(stl->stats.number_of_facets); - - stl->heads = (stl_hash_edge**)calloc(stl->M, sizeof(*stl->heads)); - if(stl->heads == NULL) perror("stl_initialize_facet_check_nearby"); - - stl->tail = (stl_hash_edge*)malloc(sizeof(stl_hash_edge)); - if(stl->tail == NULL) perror("stl_initialize_facet_check_nearby"); - - stl->tail->next = stl->tail; - - for(i = 0; i < stl->M; i++) { - stl->heads[i] = stl->tail; - } -} - - - -static void -stl_record_neighbors(stl_file *stl, - stl_hash_edge *edge_a, stl_hash_edge *edge_b) { - int i; - int j; - - if (stl->error) return; - - /* Facet a's neighbor is facet b */ - stl->neighbors_start[edge_a->facet_number].neighbor[edge_a->which_edge % 3] = - edge_b->facet_number; /* sets the .neighbor part */ - - stl->neighbors_start[edge_a->facet_number]. - which_vertex_not[edge_a->which_edge % 3] = - (edge_b->which_edge + 2) % 3; /* sets the .which_vertex_not part */ - - /* Facet b's neighbor is facet a */ - stl->neighbors_start[edge_b->facet_number].neighbor[edge_b->which_edge % 3] = - edge_a->facet_number; /* sets the .neighbor part */ - - stl->neighbors_start[edge_b->facet_number]. - which_vertex_not[edge_b->which_edge % 3] = - (edge_a->which_edge + 2) % 3; /* sets the .which_vertex_not part */ - - if( ((edge_a->which_edge < 3) && (edge_b->which_edge < 3)) - || ((edge_a->which_edge > 2) && (edge_b->which_edge > 2))) { - /* these facets are oriented in opposite directions. */ - /* their normals are probably messed up. */ - stl->neighbors_start[edge_a->facet_number]. - which_vertex_not[edge_a->which_edge % 3] += 3; - stl->neighbors_start[edge_b->facet_number]. - which_vertex_not[edge_b->which_edge % 3] += 3; - } - - - /* Count successful connects */ - /* Total connects */ - stl->stats.connected_edges += 2; - /* Count individual connects */ - i = ((stl->neighbors_start[edge_a->facet_number].neighbor[0] == -1) + - (stl->neighbors_start[edge_a->facet_number].neighbor[1] == -1) + - (stl->neighbors_start[edge_a->facet_number].neighbor[2] == -1)); - j = ((stl->neighbors_start[edge_b->facet_number].neighbor[0] == -1) + - (stl->neighbors_start[edge_b->facet_number].neighbor[1] == -1) + - (stl->neighbors_start[edge_b->facet_number].neighbor[2] == -1)); - if(i == 2) { - stl->stats.connected_facets_1_edge +=1; - } else if(i == 1) { - stl->stats.connected_facets_2_edge +=1; - } else { - stl->stats.connected_facets_3_edge +=1; - } - if(j == 2) { - stl->stats.connected_facets_1_edge +=1; - } else if(j == 1) { - stl->stats.connected_facets_2_edge +=1; - } else { - stl->stats.connected_facets_3_edge +=1; - } -} - -static void stl_match_neighbors_nearby(stl_file *stl, stl_hash_edge *edge_a, stl_hash_edge *edge_b) -{ - int facet1; - int facet2; - int vertex1; - int vertex2; - int vnot1; - int vnot2; - stl_vertex new_vertex1; - stl_vertex new_vertex2; - - if (stl->error) return; - - stl_record_neighbors(stl, edge_a, edge_b); - stl_which_vertices_to_change(stl, edge_a, edge_b, &facet1, &vertex1, - &facet2, &vertex2, &new_vertex1, &new_vertex2); - if(facet1 != -1) { - if(facet1 == edge_a->facet_number) { - vnot1 = (edge_a->which_edge + 2) % 3; - } else { - vnot1 = (edge_b->which_edge + 2) % 3; - } - if(((vnot1 + 2) % 3) == vertex1) { - vnot1 += 3; - } - stl_change_vertices(stl, facet1, vnot1, new_vertex1); - } - if(facet2 != -1) { - if(facet2 == edge_a->facet_number) { - vnot2 = (edge_a->which_edge + 2) % 3; - } else { - vnot2 = (edge_b->which_edge + 2) % 3; - } - if(((vnot2 + 2) % 3) == vertex2) { - vnot2 += 3; - } - stl_change_vertices(stl, facet2, vnot2, new_vertex2); - } - stl->stats.edges_fixed += 2; -} - - -static void stl_change_vertices(stl_file *stl, int facet_num, int vnot, stl_vertex new_vertex) { - int first_facet; - int direction; - int next_edge; - int pivot_vertex; - - if (stl->error) return; - - first_facet = facet_num; - direction = 0; - - for(;;) { - if(vnot > 2) { - if(direction == 0) { - pivot_vertex = (vnot + 2) % 3; - next_edge = pivot_vertex; - direction = 1; - } else { - pivot_vertex = (vnot + 1) % 3; - next_edge = vnot % 3; - direction = 0; - } - } else { - if(direction == 0) { - pivot_vertex = (vnot + 1) % 3; - next_edge = vnot; - } else { - pivot_vertex = (vnot + 2) % 3; - next_edge = pivot_vertex; - } - } -#if 0 - if (stl->facet_start[facet_num].vertex[pivot_vertex](0) == new_vertex(0) && - stl->facet_start[facet_num].vertex[pivot_vertex](1) == new_vertex(1) && - stl->facet_start[facet_num].vertex[pivot_vertex](2) == new_vertex(2)) - printf("Changing vertex %f,%f,%f: Same !!!\r\n", - new_vertex(0), new_vertex(1), new_vertex(2)); - else { - if (stl->facet_start[facet_num].vertex[pivot_vertex](0) != new_vertex(0)) - printf("Changing coordinate x, vertex %e (0x%08x) to %e(0x%08x)\r\n", - stl->facet_start[facet_num].vertex[pivot_vertex](0), - *reinterpret_cast(&stl->facet_start[facet_num].vertex[pivot_vertex](0)), - new_vertex(0), - *reinterpret_cast(&new_vertex(0))); - if (stl->facet_start[facet_num].vertex[pivot_vertex](1) != new_vertex(1)) - printf("Changing coordinate x, vertex %e (0x%08x) to %e(0x%08x)\r\n", - stl->facet_start[facet_num].vertex[pivot_vertex](1), - *reinterpret_cast(&stl->facet_start[facet_num].vertex[pivot_vertex](1)), - new_vertex(1), - *reinterpret_cast(&new_vertex(1))); - if (stl->facet_start[facet_num].vertex[pivot_vertex](2) != new_vertex(2)) - printf("Changing coordinate x, vertex %e (0x%08x) to %e(0x%08x)\r\n", - stl->facet_start[facet_num].vertex[pivot_vertex](2), - *reinterpret_cast(&stl->facet_start[facet_num].vertex[pivot_vertex](2)), - new_vertex(2), - *reinterpret_cast(&new_vertex(2))); - } -#endif - stl->facet_start[facet_num].vertex[pivot_vertex] = new_vertex; - vnot = stl->neighbors_start[facet_num].which_vertex_not[next_edge]; - facet_num = stl->neighbors_start[facet_num].neighbor[next_edge]; - - if(facet_num == -1) { - break; - } - - if(facet_num == first_facet) { - /* back to the beginning */ - printf("\ -Back to the first facet changing vertices: probably a mobius part.\n\ -Try using a smaller tolerance or don't do a nearby check\n"); - return; - } - } -} - -static void -stl_which_vertices_to_change(stl_file *stl, stl_hash_edge *edge_a, - stl_hash_edge *edge_b, int *facet1, int *vertex1, - int *facet2, int *vertex2, - stl_vertex *new_vertex1, stl_vertex *new_vertex2) { - int v1a; /* pair 1, facet a */ - int v1b; /* pair 1, facet b */ - int v2a; /* pair 2, facet a */ - int v2b; /* pair 2, facet b */ - - /* Find first pair */ - if(edge_a->which_edge < 3) { - v1a = edge_a->which_edge; - v2a = (edge_a->which_edge + 1) % 3; - } else { - v2a = edge_a->which_edge % 3; - v1a = (edge_a->which_edge + 1) % 3; - } - if(edge_b->which_edge < 3) { - v1b = edge_b->which_edge; - v2b = (edge_b->which_edge + 1) % 3; - } else { - v2b = edge_b->which_edge % 3; - v1b = (edge_b->which_edge + 1) % 3; - } - - // Of the first pair, which vertex, if any, should be changed - if(stl->facet_start[edge_a->facet_number].vertex[v1a] == - stl->facet_start[edge_b->facet_number].vertex[v1b]) { - // These facets are already equal. No need to change. - *facet1 = -1; - } else { - if( (stl->neighbors_start[edge_a->facet_number].neighbor[v1a] == -1) - && (stl->neighbors_start[edge_a->facet_number]. - neighbor[(v1a + 2) % 3] == -1)) { - /* This vertex has no neighbors. This is a good one to change */ - *facet1 = edge_a->facet_number; - *vertex1 = v1a; - *new_vertex1 = stl->facet_start[edge_b->facet_number].vertex[v1b]; - } else { - *facet1 = edge_b->facet_number; - *vertex1 = v1b; - *new_vertex1 = stl->facet_start[edge_a->facet_number].vertex[v1a]; - } - } - - /* Of the second pair, which vertex, if any, should be changed */ - if(stl->facet_start[edge_a->facet_number].vertex[v2a] == - stl->facet_start[edge_b->facet_number].vertex[v2b]) { - // These facets are already equal. No need to change. - *facet2 = -1; - } else { - if( (stl->neighbors_start[edge_a->facet_number].neighbor[v2a] == -1) - && (stl->neighbors_start[edge_a->facet_number]. - neighbor[(v2a + 2) % 3] == -1)) { - /* This vertex has no neighbors. This is a good one to change */ - *facet2 = edge_a->facet_number; - *vertex2 = v2a; - *new_vertex2 = stl->facet_start[edge_b->facet_number].vertex[v2b]; - } else { - *facet2 = edge_b->facet_number; - *vertex2 = v2b; - *new_vertex2 = stl->facet_start[edge_a->facet_number].vertex[v2a]; - } - } -} - -static void -stl_remove_facet(stl_file *stl, int facet_number) { - int neighbor[3]; - int vnot[3]; - int i; - int j; - - if (stl->error) return; - - stl->stats.facets_removed += 1; - /* Update list of connected edges */ - j = ((stl->neighbors_start[facet_number].neighbor[0] == -1) + - (stl->neighbors_start[facet_number].neighbor[1] == -1) + - (stl->neighbors_start[facet_number].neighbor[2] == -1)); - if(j == 2) { - stl->stats.connected_facets_1_edge -= 1; - } else if(j == 1) { - stl->stats.connected_facets_2_edge -= 1; - stl->stats.connected_facets_1_edge -= 1; - } else if(j == 0) { - stl->stats.connected_facets_3_edge -= 1; - stl->stats.connected_facets_2_edge -= 1; - stl->stats.connected_facets_1_edge -= 1; - } - - stl->facet_start[facet_number] = - stl->facet_start[stl->stats.number_of_facets - 1]; - /* I could reallocate at this point, but it is not really necessary. */ - stl->neighbors_start[facet_number] = - stl->neighbors_start[stl->stats.number_of_facets - 1]; - stl->stats.number_of_facets -= 1; - - for(i = 0; i < 3; i++) { - neighbor[i] = stl->neighbors_start[facet_number].neighbor[i]; - vnot[i] = stl->neighbors_start[facet_number].which_vertex_not[i]; - } - - for(i = 0; i < 3; i++) { - if(neighbor[i] != -1) { - if(stl->neighbors_start[neighbor[i]].neighbor[(vnot[i] + 1)% 3] != - stl->stats.number_of_facets) { - printf("\ -in stl_remove_facet: neighbor = %d numfacets = %d this is wrong\n", - stl->neighbors_start[neighbor[i]].neighbor[(vnot[i] + 1)% 3], - stl->stats.number_of_facets); - return; - } - stl->neighbors_start[neighbor[i]].neighbor[(vnot[i] + 1)% 3] - = facet_number; - } - } + HashTableEdges hash_table(stl->stats.number_of_facets); + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + //FIXME is the copy necessary? + stl_facet facet = stl->facet_start[i]; + for (int j = 0; j < 3; j++) { + if (stl->neighbors_start[i].neighbor[j] == -1) { + HashEdge edge; + edge.facet_number = i; + edge.which_edge = j; + if (edge.load_nearby(stl, facet.vertex[j], facet.vertex[(j + 1) % 3], tolerance)) + // Only insert edges that have different keys. + hash_table.insert_edge_nearby(stl, edge); + } + } + } } void stl_remove_unconnected_facets(stl_file *stl) { - /* A couple of things need to be done here. One is to remove any */ - /* completely unconnected facets (0 edges connected) since these are */ - /* useless and could be completely wrong. The second thing that needs to */ - /* be done is to remove any degenerate facets that were created during */ - /* stl_check_facets_nearby(). */ - if (stl->error) - return; + // A couple of things need to be done here. One is to remove any completely unconnected facets (0 edges connected) since these are + // useless and could be completely wrong. The second thing that needs to be done is to remove any degenerate facets that were created during + // stl_check_facets_nearby(). + auto remove_facet = [stl](int facet_number) + { + ++ stl->stats.facets_removed; + /* Update list of connected edges */ + stl_neighbors &neighbors = stl->neighbors_start[facet_number]; + // Update statistics on unconnected triangle edges. + switch ((neighbors.neighbor[0] == -1) + (neighbors.neighbor[1] == -1) + (neighbors.neighbor[2] == -1)) { + case 0: // Facet has 3 neighbors + -- stl->stats.connected_facets_3_edge; + -- stl->stats.connected_facets_2_edge; + -- stl->stats.connected_facets_1_edge; + break; + case 1: // Facet has 2 neighbors + -- stl->stats.connected_facets_2_edge; + -- stl->stats.connected_facets_1_edge; + break; + case 2: // Facet has 1 neighbor + -- stl->stats.connected_facets_1_edge; + case 3: // Facet has 0 neighbors + break; + default: + assert(false); + } - // remove degenerate facets - for (int i = 0; i < stl->stats.number_of_facets; ++ i) { - if(stl->facet_start[i].vertex[0] == stl->facet_start[i].vertex[1] || - stl->facet_start[i].vertex[0] == stl->facet_start[i].vertex[2] || - stl->facet_start[i].vertex[1] == stl->facet_start[i].vertex[2]) { - stl_remove_degenerate(stl, i); - i--; - } - } + if (facet_number < -- stl->stats.number_of_facets) { + // Removing a face, which was not the last one. + // Copy the face and neighborship from the last face to facet_number. + stl->facet_start[facet_number] = stl->facet_start[stl->stats.number_of_facets]; + neighbors = stl->neighbors_start[stl->stats.number_of_facets]; + // Update neighborship of faces, which used to point to the last face, now moved to facet_number. + for (int i = 0; i < 3; ++ i) + if (neighbors.neighbor[i] != -1) { + int &other_face_idx = stl->neighbors_start[neighbors.neighbor[i]].neighbor[(neighbors.which_vertex_not[i] + 1) % 3]; + if (other_face_idx != stl->stats.number_of_facets) { + BOOST_LOG_TRIVIAL(info) << "in remove_facet: neighbor = " << other_face_idx << " numfacets = " << stl->stats.number_of_facets << " this is wrong"; + return; + } + other_face_idx = facet_number; + } + } - if(stl->stats.connected_facets_1_edge < stl->stats.number_of_facets) { - // remove completely unconnected facets - for (int i = 0; i < stl->stats.number_of_facets; i++) { - if (stl->neighbors_start[i].neighbor[0] == -1 && - stl->neighbors_start[i].neighbor[1] == -1 && - stl->neighbors_start[i].neighbor[2] == -1) { - // This facet is completely unconnected. Remove it. - stl_remove_facet(stl, i); - -- i; - } - } - } + stl->facet_start.pop_back(); + stl->neighbors_start.pop_back(); + }; + + auto remove_degenerate = [stl, remove_facet](int facet) + { + // Update statistics on face connectivity. + auto stl_update_connects_remove_1 = [stl](int facet_num) { + //FIXME when decreasing 3_edge, should I increase 2_edge etc? + switch ((stl->neighbors_start[facet_num].neighbor[0] == -1) + (stl->neighbors_start[facet_num].neighbor[1] == -1) + (stl->neighbors_start[facet_num].neighbor[2] == -1)) { + case 0: // Facet has 3 neighbors + -- stl->stats.connected_facets_3_edge; break; + case 1: // Facet has 2 neighbors + -- stl->stats.connected_facets_2_edge; break; + case 2: // Facet has 1 neighbor + -- stl->stats.connected_facets_1_edge; break; + case 3: // Facet has 0 neighbors + break; + default: + assert(false); + } + }; + + int edge_to_collapse = 0; + if (stl->facet_start[facet].vertex[0] == stl->facet_start[facet].vertex[1]) { + if (stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) { + // All 3 vertices are equal. Collapse the edge with no neighbor if it exists. + const int *nbr = stl->neighbors_start[facet].neighbor; + edge_to_collapse = (nbr[0] == -1) ? 0 : (nbr[1] == -1) ? 1 : 2; + } else { + edge_to_collapse = 0; + } + } else if (stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) { + edge_to_collapse = 1; + } else if (stl->facet_start[facet].vertex[2] == stl->facet_start[facet].vertex[0]) { + edge_to_collapse = 2; + } else { + // No degenerate. Function shouldn't have been called. + return; + } + + int edge[3] = { (edge_to_collapse + 1) % 3, (edge_to_collapse + 2) % 3, edge_to_collapse }; + int neighbor[] = { + stl->neighbors_start[facet].neighbor[edge[0]], + stl->neighbors_start[facet].neighbor[edge[1]], + stl->neighbors_start[facet].neighbor[edge[2]] + }; + int vnot[] = { + stl->neighbors_start[facet].which_vertex_not[edge[0]], + stl->neighbors_start[facet].which_vertex_not[edge[1]], + stl->neighbors_start[facet].which_vertex_not[edge[2]] + }; + // Update statistics on edge connectivity. + if (neighbor[0] == -1) + stl_update_connects_remove_1(neighbor[1]); + if (neighbor[1] == -1) + stl_update_connects_remove_1(neighbor[0]); + + if (neighbor[0] >= 0) { + if (neighbor[1] >= 0) { + // Adjust the "flip" flag for the which_vertex_not values. + if (vnot[0] > 2) { + if (vnot[1] > 2) { + // The face to be removed has its normal flipped compared to the left & right neighbors, therefore after removing this face + // the two remaining neighbors will be oriented correctly. + vnot[0] -= 3; + vnot[1] -= 3; + } else + // One neighbor has its normal inverted compared to the face to be removed, the other is oriented equally. + // After removal, the two neighbors will have their normals flipped. + vnot[1] += 3; + } else if (vnot[1] > 2) + // One neighbor has its normal inverted compared to the face to be removed, the other is oriented equally. + // After removal, the two neighbors will have their normals flipped. + vnot[0] += 3; + } + stl->neighbors_start[neighbor[0]].neighbor[(vnot[0] + 1) % 3] = (neighbor[0] == neighbor[1]) ? -1 : neighbor[1]; + stl->neighbors_start[neighbor[0]].which_vertex_not[(vnot[0] + 1) % 3] = vnot[1]; + } + if (neighbor[1] >= 0) { + stl->neighbors_start[neighbor[1]].neighbor[(vnot[1] + 1) % 3] = (neighbor[0] == neighbor[1]) ? -1 : neighbor[0]; + stl->neighbors_start[neighbor[1]].which_vertex_not[(vnot[1] + 1) % 3] = vnot[0]; + } + if (neighbor[2] >= 0) { + stl_update_connects_remove_1(neighbor[2]); + stl->neighbors_start[neighbor[2]].neighbor[(vnot[2] + 1) % 3] = -1; + } + + remove_facet(facet); + }; + + // remove degenerate facets + for (uint32_t i = 0; i < stl->stats.number_of_facets;) + if (stl->facet_start[i].vertex[0] == stl->facet_start[i].vertex[1] || + stl->facet_start[i].vertex[0] == stl->facet_start[i].vertex[2] || + stl->facet_start[i].vertex[1] == stl->facet_start[i].vertex[2]) { + remove_degenerate(i); +// assert(stl_validate(stl)); + } else + ++ i; + + if (stl->stats.connected_facets_1_edge < (int)stl->stats.number_of_facets) { + // remove completely unconnected facets + for (uint32_t i = 0; i < stl->stats.number_of_facets;) + if (stl->neighbors_start[i].neighbor[0] == -1 && + stl->neighbors_start[i].neighbor[1] == -1 && + stl->neighbors_start[i].neighbor[2] == -1) { + // This facet is completely unconnected. Remove it. + remove_facet(i); + assert(stl_validate(stl)); + } else + ++ i; + } } -static void -stl_remove_degenerate(stl_file *stl, int facet) { - int edge1; - int edge2; - int edge3; - int neighbor1; - int neighbor2; - int neighbor3; - int vnot1; - int vnot2; - int vnot3; +void stl_fill_holes(stl_file *stl) +{ + // Insert all unconnected edges into hash list. + HashTableEdges hash_table(stl->stats.number_of_facets); + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + stl_facet facet = stl->facet_start[i]; + for (int j = 0; j < 3; ++ j) { + if(stl->neighbors_start[i].neighbor[j] != -1) + continue; + HashEdge edge; + edge.facet_number = i; + edge.which_edge = j; + edge.load_exact(stl, &facet.vertex[j], &facet.vertex[(j + 1) % 3]); + hash_table.insert_edge_exact(stl, edge); + } + } - if (stl->error) return; + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + stl_facet facet = stl->facet_start[i]; + int neighbors_initial[3] = { stl->neighbors_start[i].neighbor[0], stl->neighbors_start[i].neighbor[1], stl->neighbors_start[i].neighbor[2] }; + int first_facet = i; + for (int j = 0; j < 3; ++ j) { + if (stl->neighbors_start[i].neighbor[j] != -1) + continue; - if (stl->facet_start[facet].vertex[0] == stl->facet_start[facet].vertex[1] && - stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) { - /* all 3 vertices are equal. Just remove the facet. I don't think*/ - /* this is really possible, but just in case... */ - printf("removing a facet in stl_remove_degenerate\n"); - stl_remove_facet(stl, facet); - return; - } + stl_facet new_facet; + new_facet.vertex[0] = facet.vertex[j]; + new_facet.vertex[1] = facet.vertex[(j + 1) % 3]; + bool direction = neighbors_initial[(j + 2) % 3] == -1; + int facet_num = i; + int vnot = (j + 2) % 3; - if (stl->facet_start[facet].vertex[0] == stl->facet_start[facet].vertex[1]) { - edge1 = 1; - edge2 = 2; - edge3 = 0; - } else if (stl->facet_start[facet].vertex[1] == stl->facet_start[facet].vertex[2]) { - edge1 = 0; - edge2 = 2; - edge3 = 1; - } else if (stl->facet_start[facet].vertex[2] == stl->facet_start[facet].vertex[0]) { - edge1 = 0; - edge2 = 1; - edge3 = 2; - } else { - /* No degenerate. Function shouldn't have been called. */ - return; - } - neighbor1 = stl->neighbors_start[facet].neighbor[edge1]; - neighbor2 = stl->neighbors_start[facet].neighbor[edge2]; + for (;;) { + int pivot_vertex = 0; + int next_edge = 0; + if (vnot > 2) { + if (direction) { + pivot_vertex = (vnot + 1) % 3; + next_edge = vnot % 3; + } else { + pivot_vertex = (vnot + 2) % 3; + next_edge = pivot_vertex; + } + direction = ! direction; + } else { + if(direction == 0) { + pivot_vertex = (vnot + 1) % 3; + next_edge = vnot; + } else { + pivot_vertex = (vnot + 2) % 3; + next_edge = pivot_vertex; + } + } - if(neighbor1 == -1) { - stl_update_connects_remove_1(stl, neighbor2); - } - if(neighbor2 == -1) { - stl_update_connects_remove_1(stl, neighbor1); - } + int next_facet = stl->neighbors_start[facet_num].neighbor[next_edge]; + if (next_facet == -1) { + new_facet.vertex[2] = stl->facet_start[facet_num].vertex[vnot % 3]; + stl_add_facet(stl, &new_facet); + for (int k = 0; k < 3; ++ k) { + HashEdge edge; + edge.facet_number = stl->stats.number_of_facets - 1; + edge.which_edge = k; + edge.load_exact(stl, &new_facet.vertex[k], &new_facet.vertex[(k + 1) % 3]); + hash_table.insert_edge_exact(stl, edge); + } + break; + } + vnot = stl->neighbors_start[facet_num].which_vertex_not[next_edge]; + facet_num = next_facet; - neighbor3 = stl->neighbors_start[facet].neighbor[edge3]; - vnot1 = stl->neighbors_start[facet].which_vertex_not[edge1]; - vnot2 = stl->neighbors_start[facet].which_vertex_not[edge2]; - vnot3 = stl->neighbors_start[facet].which_vertex_not[edge3]; - - if(neighbor1 >= 0){ - stl->neighbors_start[neighbor1].neighbor[(vnot1 + 1) % 3] = neighbor2; - stl->neighbors_start[neighbor1].which_vertex_not[(vnot1 + 1) % 3] = vnot2; - } - if(neighbor2 >= 0){ - stl->neighbors_start[neighbor2].neighbor[(vnot2 + 1) % 3] = neighbor1; - stl->neighbors_start[neighbor2].which_vertex_not[(vnot2 + 1) % 3] = vnot1; - } - - stl_remove_facet(stl, facet); - - if(neighbor3 >= 0) { - stl_update_connects_remove_1(stl, neighbor3); - stl->neighbors_start[neighbor3].neighbor[(vnot3 + 1) % 3] = -1; - } + if (facet_num == first_facet) { + // back to the beginning + BOOST_LOG_TRIVIAL(info) << "Back to the first facet filling holes: probably a mobius part. Try using a smaller tolerance or don't do a nearby check."; + return; + } + } + } + } } -void -stl_update_connects_remove_1(stl_file *stl, int facet_num) { - int j; - - if (stl->error) return; - /* Update list of connected edges */ - j = ((stl->neighbors_start[facet_num].neighbor[0] == -1) + - (stl->neighbors_start[facet_num].neighbor[1] == -1) + - (stl->neighbors_start[facet_num].neighbor[2] == -1)); - if(j == 0) { /* Facet has 3 neighbors */ - stl->stats.connected_facets_3_edge -= 1; - } else if(j == 1) { /* Facet has 2 neighbors */ - stl->stats.connected_facets_2_edge -= 1; - } else if(j == 2) { /* Facet has 1 neighbor */ - stl->stats.connected_facets_1_edge -= 1; - } -} - -void -stl_fill_holes(stl_file *stl) { - stl_facet facet; - stl_facet new_facet; - int neighbors_initial[3]; - stl_hash_edge edge; - int first_facet; - int direction; - int facet_num; - int vnot; - int next_edge; - int pivot_vertex; - int next_facet; - int i; - int j; - int k; - - if (stl->error) return; - - /* Insert all unconnected edges into hash list */ - stl_initialize_facet_check_nearby(stl); - for(i = 0; i < stl->stats.number_of_facets; i++) { - facet = stl->facet_start[i]; - for(j = 0; j < 3; j++) { - if(stl->neighbors_start[i].neighbor[j] != -1) continue; - edge.facet_number = i; - edge.which_edge = j; - stl_load_edge_exact(stl, &edge, &facet.vertex[j], - &facet.vertex[(j + 1) % 3]); - - insert_hash_edge(stl, edge, stl_record_neighbors); - } - } - - for(i = 0; i < stl->stats.number_of_facets; i++) { - facet = stl->facet_start[i]; - neighbors_initial[0] = stl->neighbors_start[i].neighbor[0]; - neighbors_initial[1] = stl->neighbors_start[i].neighbor[1]; - neighbors_initial[2] = stl->neighbors_start[i].neighbor[2]; - first_facet = i; - for(j = 0; j < 3; j++) { - if(stl->neighbors_start[i].neighbor[j] != -1) continue; - - new_facet.vertex[0] = facet.vertex[j]; - new_facet.vertex[1] = facet.vertex[(j + 1) % 3]; - if(neighbors_initial[(j + 2) % 3] == -1) { - direction = 1; - } else { - direction = 0; - } - - facet_num = i; - vnot = (j + 2) % 3; - - for(;;) { - if(vnot > 2) { - if(direction == 0) { - pivot_vertex = (vnot + 2) % 3; - next_edge = pivot_vertex; - direction = 1; - } else { - pivot_vertex = (vnot + 1) % 3; - next_edge = vnot % 3; - direction = 0; - } - } else { - if(direction == 0) { - pivot_vertex = (vnot + 1) % 3; - next_edge = vnot; - } else { - pivot_vertex = (vnot + 2) % 3; - next_edge = pivot_vertex; - } - } - next_facet = stl->neighbors_start[facet_num].neighbor[next_edge]; - - if(next_facet == -1) { - new_facet.vertex[2] = stl->facet_start[facet_num]. - vertex[vnot % 3]; - stl_add_facet(stl, &new_facet); - for(k = 0; k < 3; k++) { - edge.facet_number = stl->stats.number_of_facets - 1; - edge.which_edge = k; - stl_load_edge_exact(stl, &edge, &new_facet.vertex[k], - &new_facet.vertex[(k + 1) % 3]); - - insert_hash_edge(stl, edge, stl_record_neighbors); - } - break; - } else { - vnot = stl->neighbors_start[facet_num]. - which_vertex_not[next_edge]; - facet_num = next_facet; - } - - if(facet_num == first_facet) { - /* back to the beginning */ - printf("\ -Back to the first facet filling holes: probably a mobius part.\n\ -Try using a smaller tolerance or don't do a nearby check\n"); - return; - } - } - } - } -} - -void -stl_add_facet(stl_file *stl, stl_facet *new_facet) { - if (stl->error) return; - - stl->stats.facets_added += 1; - if(stl->stats.facets_malloced < stl->stats.number_of_facets + 1) { - stl->facet_start = (stl_facet*)realloc(stl->facet_start, - (sizeof(stl_facet) * (stl->stats.facets_malloced + 256))); - if(stl->facet_start == NULL) perror("stl_add_facet"); - stl->neighbors_start = (stl_neighbors*)realloc(stl->neighbors_start, - (sizeof(stl_neighbors) * (stl->stats.facets_malloced + 256))); - if(stl->neighbors_start == NULL) perror("stl_add_facet"); - stl->stats.facets_malloced += 256; - } - stl->facet_start[stl->stats.number_of_facets] = *new_facet; - - /* note that the normal vector is not set here, just initialized to 0 */ - stl->facet_start[stl->stats.number_of_facets].normal = stl_normal::Zero(); - - stl->neighbors_start[stl->stats.number_of_facets].neighbor[0] = -1; - stl->neighbors_start[stl->stats.number_of_facets].neighbor[1] = -1; - stl->neighbors_start[stl->stats.number_of_facets].neighbor[2] = -1; - stl->stats.number_of_facets += 1; +void stl_add_facet(stl_file *stl, const stl_facet *new_facet) +{ + assert(stl->facet_start.size() == stl->stats.number_of_facets); + assert(stl->neighbors_start.size() == stl->stats.number_of_facets); + stl->facet_start.emplace_back(*new_facet); + // note that the normal vector is not set here, just initialized to 0. + stl->facet_start[stl->stats.number_of_facets].normal = stl_normal::Zero(); + stl->neighbors_start.emplace_back(); + ++ stl->stats.facets_added; + ++ stl->stats.number_of_facets; } diff --git a/src/admesh/normals.cpp b/src/admesh/normals.cpp index ecf08b59c9..16bb3daab5 100644 --- a/src/admesh/normals.cpp +++ b/src/admesh/normals.cpp @@ -25,271 +25,214 @@ #include #include +// Boost pool: Don't use mutexes to synchronize memory allocation. +#define BOOST_POOL_NO_MT +#include + #include "stl.h" -static int stl_check_normal_vector(stl_file *stl, int facet_num, int normal_fix_flag); +static void reverse_facet(stl_file *stl, int facet_num) +{ + ++ stl->stats.facets_reversed; -static void -stl_reverse_facet(stl_file *stl, int facet_num) { - stl_vertex tmp_vertex; - /* int tmp_neighbor;*/ - int neighbor[3]; - int vnot[3]; + int neighbor[3] = { stl->neighbors_start[facet_num].neighbor[0], stl->neighbors_start[facet_num].neighbor[1], stl->neighbors_start[facet_num].neighbor[2] }; + int vnot[3] = { stl->neighbors_start[facet_num].which_vertex_not[0], stl->neighbors_start[facet_num].which_vertex_not[1], stl->neighbors_start[facet_num].which_vertex_not[2] }; - stl->stats.facets_reversed += 1; + // reverse the facet + stl_vertex tmp_vertex = stl->facet_start[facet_num].vertex[0]; + stl->facet_start[facet_num].vertex[0] = stl->facet_start[facet_num].vertex[1]; + stl->facet_start[facet_num].vertex[1] = tmp_vertex; - neighbor[0] = stl->neighbors_start[facet_num].neighbor[0]; - neighbor[1] = stl->neighbors_start[facet_num].neighbor[1]; - neighbor[2] = stl->neighbors_start[facet_num].neighbor[2]; - vnot[0] = stl->neighbors_start[facet_num].which_vertex_not[0]; - vnot[1] = stl->neighbors_start[facet_num].which_vertex_not[1]; - vnot[2] = stl->neighbors_start[facet_num].which_vertex_not[2]; + // fix the vnots of the neighboring facets + if (neighbor[0] != -1) + stl->neighbors_start[neighbor[0]].which_vertex_not[(vnot[0] + 1) % 3] = (stl->neighbors_start[neighbor[0]].which_vertex_not[(vnot[0] + 1) % 3] + 3) % 6; + if (neighbor[1] != -1) + stl->neighbors_start[neighbor[1]].which_vertex_not[(vnot[1] + 1) % 3] = (stl->neighbors_start[neighbor[1]].which_vertex_not[(vnot[1] + 1) % 3] + 4) % 6; + if (neighbor[2] != -1) + stl->neighbors_start[neighbor[2]].which_vertex_not[(vnot[2] + 1) % 3] = (stl->neighbors_start[neighbor[2]].which_vertex_not[(vnot[2] + 1) % 3] + 2) % 6; - /* reverse the facet */ - tmp_vertex = stl->facet_start[facet_num].vertex[0]; - stl->facet_start[facet_num].vertex[0] = - stl->facet_start[facet_num].vertex[1]; - stl->facet_start[facet_num].vertex[1] = tmp_vertex; + // swap the neighbors of the facet that is being reversed + stl->neighbors_start[facet_num].neighbor[1] = neighbor[2]; + stl->neighbors_start[facet_num].neighbor[2] = neighbor[1]; - /* fix the vnots of the neighboring facets */ - if(neighbor[0] != -1) - stl->neighbors_start[neighbor[0]].which_vertex_not[(vnot[0] + 1) % 3] = - (stl->neighbors_start[neighbor[0]]. - which_vertex_not[(vnot[0] + 1) % 3] + 3) % 6; - if(neighbor[1] != -1) - stl->neighbors_start[neighbor[1]].which_vertex_not[(vnot[1] + 1) % 3] = - (stl->neighbors_start[neighbor[1]]. - which_vertex_not[(vnot[1] + 1) % 3] + 4) % 6; - if(neighbor[2] != -1) - stl->neighbors_start[neighbor[2]].which_vertex_not[(vnot[2] + 1) % 3] = - (stl->neighbors_start[neighbor[2]]. - which_vertex_not[(vnot[2] + 1) % 3] + 2) % 6; + // swap the vnots of the facet that is being reversed + stl->neighbors_start[facet_num].which_vertex_not[1] = vnot[2]; + stl->neighbors_start[facet_num].which_vertex_not[2] = vnot[1]; - /* swap the neighbors of the facet that is being reversed */ - stl->neighbors_start[facet_num].neighbor[1] = neighbor[2]; - stl->neighbors_start[facet_num].neighbor[2] = neighbor[1]; - - /* swap the vnots of the facet that is being reversed */ - stl->neighbors_start[facet_num].which_vertex_not[1] = vnot[2]; - stl->neighbors_start[facet_num].which_vertex_not[2] = vnot[1]; - - /* reverse the values of the vnots of the facet that is being reversed */ - stl->neighbors_start[facet_num].which_vertex_not[0] = - (stl->neighbors_start[facet_num].which_vertex_not[0] + 3) % 6; - stl->neighbors_start[facet_num].which_vertex_not[1] = - (stl->neighbors_start[facet_num].which_vertex_not[1] + 3) % 6; - stl->neighbors_start[facet_num].which_vertex_not[2] = - (stl->neighbors_start[facet_num].which_vertex_not[2] + 3) % 6; + // reverse the values of the vnots of the facet that is being reversed + stl->neighbors_start[facet_num].which_vertex_not[0] = (stl->neighbors_start[facet_num].which_vertex_not[0] + 3) % 6; + stl->neighbors_start[facet_num].which_vertex_not[1] = (stl->neighbors_start[facet_num].which_vertex_not[1] + 3) % 6; + stl->neighbors_start[facet_num].which_vertex_not[2] = (stl->neighbors_start[facet_num].which_vertex_not[2] + 3) % 6; } -void -stl_fix_normal_directions(stl_file *stl) { - char *norm_sw; - /* int edge_num;*/ - /* int vnot;*/ - int checked = 0; - int facet_num; - /* int next_facet;*/ - int i; - int j; - struct stl_normal { - int facet_num; - struct stl_normal *next; - }; - struct stl_normal *head; - struct stl_normal *tail; - struct stl_normal *newn; - struct stl_normal *temp; +// Returns true if the normal was flipped. +static bool check_normal_vector(stl_file *stl, int facet_num, int normal_fix_flag) +{ + stl_facet *facet = &stl->facet_start[facet_num]; - int* reversed_ids; - int reversed_count = 0; - int id; - int force_exit = 0; + stl_normal normal; + stl_calculate_normal(normal, facet); + stl_normalize_vector(normal); + stl_normal normal_dif = (normal - facet->normal).cwiseAbs(); - if (stl->error) return; + const float eps = 0.001f; + if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { + // Normal is within tolerance. It is not really necessary to change the values here, but just for consistency, I will. + facet->normal = normal; + return false; + } - // this may happen for malformed models, see: https://github.com/prusa3d/PrusaSlicer/issues/2209 - if (stl->stats.number_of_facets == 0) return; + stl_normal test_norm = facet->normal; + stl_normalize_vector(test_norm); + normal_dif = (normal - test_norm).cwiseAbs(); + if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { + // The normal is not within tolerance, but direction is OK. + if (normal_fix_flag) { + facet->normal = normal; + ++ stl->stats.normals_fixed; + } + return false; + } - /* Initialize linked list. */ - head = (struct stl_normal*)malloc(sizeof(struct stl_normal)); - if(head == NULL) perror("stl_fix_normal_directions"); - tail = (struct stl_normal*)malloc(sizeof(struct stl_normal)); - if(tail == NULL) perror("stl_fix_normal_directions"); - head->next = tail; - tail->next = tail; - - /* Initialize list that keeps track of already fixed facets. */ - norm_sw = (char*)calloc(stl->stats.number_of_facets, sizeof(char)); - if(norm_sw == NULL) perror("stl_fix_normal_directions"); - - /* Initialize list that keeps track of reversed facets. */ - reversed_ids = (int*)calloc(stl->stats.number_of_facets, sizeof(int)); - if (reversed_ids == NULL) perror("stl_fix_normal_directions reversed_ids"); - - facet_num = 0; - /* If normal vector is not within tolerance and backwards: - Arbitrarily starts at face 0. If this one is wrong, we're screwed. Thankfully, the chances - of it being wrong randomly are low if most of the triangles are right: */ - if (stl_check_normal_vector(stl, 0, 0) == 2) { - stl_reverse_facet(stl, 0); - reversed_ids[reversed_count++] = 0; - } - - /* Say that we've fixed this facet: */ - norm_sw[facet_num] = 1; - checked++; - - for(;;) { - /* Add neighbors_to_list. - Add unconnected neighbors to the list:a */ - for(j = 0; j < 3; j++) { - /* Reverse the neighboring facets if necessary. */ - if(stl->neighbors_start[facet_num].which_vertex_not[j] > 2) { - /* If the facet has a neighbor that is -1, it means that edge isn't shared by another facet */ - if(stl->neighbors_start[facet_num].neighbor[j] != -1) { - if (norm_sw[stl->neighbors_start[facet_num].neighbor[j]] == 1) { - /* trying to modify a facet already marked as fixed, revert all changes made until now and exit (fixes: #716, #574, #413, #269, #262, #259, #230, #228, #206) */ - for (id = reversed_count - 1; id >= 0; --id) { - stl_reverse_facet(stl, reversed_ids[id]); - } - force_exit = 1; - break; - } else { - stl_reverse_facet(stl, stl->neighbors_start[facet_num].neighbor[j]); - reversed_ids[reversed_count++] = stl->neighbors_start[facet_num].neighbor[j]; - } - } - } - /* If this edge of the facet is connected: */ - if(stl->neighbors_start[facet_num].neighbor[j] != -1) { - /* If we haven't fixed this facet yet, add it to the list: */ - if(norm_sw[stl->neighbors_start[facet_num].neighbor[j]] != 1) { - /* Add node to beginning of list. */ - newn = (struct stl_normal*)malloc(sizeof(struct stl_normal)); - if(newn == NULL) perror("stl_fix_normal_directions"); - newn->facet_num = stl->neighbors_start[facet_num].neighbor[j]; - newn->next = head->next; - head->next = newn; - } - } - } - - /* an error occourred, quit the for loop and exit */ - if (force_exit) break; - - /* Get next facet to fix from top of list. */ - if(head->next != tail) { - facet_num = head->next->facet_num; - if(norm_sw[facet_num] != 1) { /* If facet is in list mutiple times */ - norm_sw[facet_num] = 1; /* Record this one as being fixed. */ - checked++; - } - temp = head->next; /* Delete this facet from the list. */ - head->next = head->next->next; - free(temp); - } else { /* if we ran out of facets to fix: */ - /* All of the facets in this part have been fixed. */ - stl->stats.number_of_parts += 1; - if(checked >= stl->stats.number_of_facets) { - /* All of the facets have been checked. Bail out. */ - break; - } else { - /* There is another part here. Find it and continue. */ - for(i = 0; i < stl->stats.number_of_facets; i++) { - if(norm_sw[i] == 0) { - /* This is the first facet of the next part. */ - facet_num = i; - if(stl_check_normal_vector(stl, i, 0) == 2) { - stl_reverse_facet(stl, i); - reversed_ids[reversed_count++] = i; - } - - norm_sw[facet_num] = 1; - checked++; - break; - } - } - } - } - } - free(head); - free(tail); - free(reversed_ids); - free(norm_sw); + test_norm *= -1.f; + normal_dif = (normal - test_norm).cwiseAbs(); + if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { + // The normal is not within tolerance and backwards. + if (normal_fix_flag) { + facet->normal = normal; + ++ stl->stats.normals_fixed; + } + return true; + } + if (normal_fix_flag) { + facet->normal = normal; + ++ stl->stats.normals_fixed; + } + // Status is unknown. + return false; } -static int stl_check_normal_vector(stl_file *stl, int facet_num, int normal_fix_flag) { - /* Returns 0 if the normal is within tolerance */ - /* Returns 1 if the normal is not within tolerance, but direction is OK */ - /* Returns 2 if the normal is not within tolerance and backwards */ - /* Returns 4 if the status is unknown. */ +void stl_fix_normal_directions(stl_file *stl) +{ + // This may happen for malformed models, see: https://github.com/prusa3d/PrusaSlicer/issues/2209 + if (stl->stats.number_of_facets == 0) + return; - stl_facet *facet; + struct stl_normal { + int facet_num; + stl_normal *next; + }; - facet = &stl->facet_start[facet_num]; + // Initialize linked list. + boost::object_pool pool; + stl_normal *head = pool.construct(); + stl_normal *tail = pool.construct(); + head->next = tail; + tail->next = tail; - stl_normal normal; - stl_calculate_normal(normal, facet); - stl_normalize_vector(normal); - stl_normal normal_dif = (normal - facet->normal).cwiseAbs(); + // Initialize list that keeps track of already fixed facets. + std::vector norm_sw(stl->stats.number_of_facets, 0); + // Initialize list that keeps track of reversed facets. + std::vector reversed_ids(stl->stats.number_of_facets, 0); - const float eps = 0.001f; - if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { - /* It is not really necessary to change the values here */ - /* but just for consistency, I will. */ - facet->normal = normal; - return 0; - } + int facet_num = 0; + int reversed_count = 0; + // If normal vector is not within tolerance and backwards: + // Arbitrarily starts at face 0. If this one is wrong, we're screwed. Thankfully, the chances + // of it being wrong randomly are low if most of the triangles are right: + if (check_normal_vector(stl, 0, 0)) { + reverse_facet(stl, 0); + reversed_ids[reversed_count ++] = 0; + } - stl_normal test_norm = facet->normal; - stl_normalize_vector(test_norm); - normal_dif = (normal - test_norm).cwiseAbs(); - if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { - if(normal_fix_flag) { - facet->normal = normal; - stl->stats.normals_fixed += 1; - } - return 1; - } + // Say that we've fixed this facet: + norm_sw[facet_num] = 1; + int checked = 1; - test_norm *= -1.f; - normal_dif = (normal - test_norm).cwiseAbs(); - if (normal_dif(0) < eps && normal_dif(1) < eps && normal_dif(2) < eps) { - // Facet is backwards. - if(normal_fix_flag) { - facet->normal = normal; - stl->stats.normals_fixed += 1; - } - return 2; - } - if(normal_fix_flag) { - facet->normal = normal; - stl->stats.normals_fixed += 1; - } - return 4; + for (;;) { + // Add neighbors_to_list. Add unconnected neighbors to the list. + bool force_exit = false; + for (int j = 0; j < 3; ++ j) { + // Reverse the neighboring facets if necessary. + if (stl->neighbors_start[facet_num].which_vertex_not[j] > 2) { + // If the facet has a neighbor that is -1, it means that edge isn't shared by another facet + if (stl->neighbors_start[facet_num].neighbor[j] != -1) { + if (norm_sw[stl->neighbors_start[facet_num].neighbor[j]] == 1) { + // trying to modify a facet already marked as fixed, revert all changes made until now and exit (fixes: #716, #574, #413, #269, #262, #259, #230, #228, #206) + for (int id = reversed_count - 1; id >= 0; -- id) + reverse_facet(stl, reversed_ids[id]); + force_exit = true; + break; + } + reverse_facet(stl, stl->neighbors_start[facet_num].neighbor[j]); + reversed_ids[reversed_count ++] = stl->neighbors_start[facet_num].neighbor[j]; + } + } + // If this edge of the facet is connected: + if (stl->neighbors_start[facet_num].neighbor[j] != -1) { + // If we haven't fixed this facet yet, add it to the list: + if (norm_sw[stl->neighbors_start[facet_num].neighbor[j]] != 1) { + // Add node to beginning of list. + stl_normal *newn = pool.construct(); + newn->facet_num = stl->neighbors_start[facet_num].neighbor[j]; + newn->next = head->next; + head->next = newn; + } + } + } + + // an error occourred, quit the for loop and exit + if (force_exit) + break; + + // Get next facet to fix from top of list. + if (head->next != tail) { + facet_num = head->next->facet_num; + if (norm_sw[facet_num] != 1) { // If facet is in list mutiple times + norm_sw[facet_num] = 1; // Record this one as being fixed. + ++ checked; + } + stl_normal *temp = head->next; // Delete this facet from the list. + head->next = head->next->next; + // pool.destroy(temp); + } else { // If we ran out of facets to fix: All of the facets in this part have been fixed. + ++ stl->stats.number_of_parts; + if (checked >= stl->stats.number_of_facets) + // All of the facets have been checked. Bail out. + break; + // There is another part here. Find it and continue. + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + if (norm_sw[i] == 0) { + // This is the first facet of the next part. + facet_num = i; + if (check_normal_vector(stl, i, 0)) { + reverse_facet(stl, i); + reversed_ids[reversed_count++] = i; + } + norm_sw[facet_num] = 1; + ++ checked; + break; + } + } + } + + // pool.destroy(head); + // pool.destroy(tail); } -void stl_fix_normal_values(stl_file *stl) { - int i; - - if (stl->error) return; - - for(i = 0; i < stl->stats.number_of_facets; i++) { - stl_check_normal_vector(stl, i, 1); - } +void stl_fix_normal_values(stl_file *stl) +{ + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + check_normal_vector(stl, i, 1); } void stl_reverse_all_facets(stl_file *stl) { - if (stl->error) - return; - - stl_normal normal; - for(int i = 0; i < stl->stats.number_of_facets; i++) { - stl_reverse_facet(stl, i); - stl_calculate_normal(normal, &stl->facet_start[i]); - stl_normalize_vector(normal); - stl->facet_start[i].normal = normal; - } + stl_normal normal; + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + reverse_facet(stl, i); + stl_calculate_normal(normal, &stl->facet_start[i]); + stl_normalize_vector(normal); + stl->facet_start[i].normal = normal; + } } diff --git a/src/admesh/shared.cpp b/src/admesh/shared.cpp index c8c17ccd59..902bbfc9b8 100644 --- a/src/admesh/shared.cpp +++ b/src/admesh/shared.cpp @@ -23,242 +23,237 @@ #include #include +#include + +#include #include #include "stl.h" -void -stl_invalidate_shared_vertices(stl_file *stl) { - if (stl->error) return; +void stl_generate_shared_vertices(stl_file *stl, indexed_triangle_set &its) +{ + // 3 indices to vertex per face + its.indices.assign(stl->stats.number_of_facets, stl_triangle_vertex_indices(-1, -1, -1)); + // Shared vertices (3D coordinates) + its.vertices.clear(); + its.vertices.reserve(stl->stats.number_of_facets / 2); - if (stl->v_indices != NULL) { - free(stl->v_indices); - stl->v_indices = NULL; - } - if (stl->v_shared != NULL) { - free(stl->v_shared); - stl->v_shared = NULL; - } + // A degenerate mesh may contain loops: Traversing a fan will end up in an endless loop + // while never reaching the starting face. To avoid these endless loops, traversed faces at each fan traversal + // are marked with a unique fan_traversal_stamp. + unsigned int fan_traversal_stamp = 0; + std::vector fan_traversal_facet_visited(stl->stats.number_of_facets, 0); + + for (uint32_t facet_idx = 0; facet_idx < stl->stats.number_of_facets; ++ facet_idx) { + for (int j = 0; j < 3; ++ j) { + if (its.indices[facet_idx][j] != -1) + // Shared vertex was already assigned. + continue; + // Create a new shared vertex. + its.vertices.emplace_back(stl->facet_start[facet_idx].vertex[j]); + // Traverse the fan around the j-th vertex of the i-th face, assign the newly created shared vertex index to all the neighboring triangles in the triangle fan. + int facet_in_fan_idx = facet_idx; + bool edge_direction = false; + bool traversal_reversed = false; + int vnot = (j + 2) % 3; + // Increase the + ++ fan_traversal_stamp; + for (;;) { + // Next edge on facet_in_fan_idx to be traversed. The edge is indexed by its starting vertex index. + int next_edge = 0; + // Vertex index in facet_in_fan_idx, which is being pivoted around, and which is being assigned a new shared vertex. + int pivot_vertex = 0; + if (vnot > 2) { + // The edge of facet_in_fan_idx opposite to vnot is equally oriented, therefore + // the neighboring facet is flipped. + if (! edge_direction) { + pivot_vertex = (vnot + 2) % 3; + next_edge = pivot_vertex; + } else { + pivot_vertex = (vnot + 1) % 3; + next_edge = vnot % 3; + } + edge_direction = ! edge_direction; + } else { + // The neighboring facet is correctly oriented. + if (! edge_direction) { + pivot_vertex = (vnot + 1) % 3; + next_edge = vnot; + } else { + pivot_vertex = (vnot + 2) % 3; + next_edge = pivot_vertex; + } + } + its.indices[facet_in_fan_idx][pivot_vertex] = its.vertices.size() - 1; + fan_traversal_facet_visited[facet_in_fan_idx] = fan_traversal_stamp; + + // next_edge is an index of the starting vertex of the edge, not an index of the opposite vertex to the edge! + int next_facet = stl->neighbors_start[facet_in_fan_idx].neighbor[next_edge]; + if (next_facet == -1) { + // No neighbor going in the current direction. + if (traversal_reversed) { + // Went to one limit, then turned back and reached the other limit. Quit the fan traversal. + break; + } else { + // Reached the first limit. Now try to reverse and traverse up to the other limit. + edge_direction = true; + vnot = (j + 1) % 3; + traversal_reversed = true; + facet_in_fan_idx = facet_idx; + } + } else if (next_facet == facet_idx) { + // Traversed a closed fan all around. +// assert(! traversal_reversed); + break; + } else if (next_facet >= (int)stl->stats.number_of_facets) { + // The mesh is not valid! + // assert(false); + break; + } else if (fan_traversal_facet_visited[next_facet] == fan_traversal_stamp) { + // Traversed a closed fan all around, but did not reach the starting face. + // This indicates an invalid geometry (non-manifold). + //assert(false); + break; + } else { + // Continue traversal. + // next_edge is an index of the starting vertex of the edge, not an index of the opposite vertex to the edge! + vnot = stl->neighbors_start[facet_in_fan_idx].which_vertex_not[next_edge]; + facet_in_fan_idx = next_facet; + } + } + } + } } -void -stl_generate_shared_vertices(stl_file *stl) { - int i; - int j; - int first_facet; - int direction; - int facet_num; - int vnot; - int next_edge; - int pivot_vertex; - int next_facet; - int reversed; +bool its_write_off(const indexed_triangle_set &its, const char *file) +{ + /* Open the file */ + FILE *fp = boost::nowide::fopen(file, "w"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_write_ascii: Couldn't open " << file << " for writing"; + return false; + } - if (stl->error) return; + fprintf(fp, "OFF\n"); + fprintf(fp, "%d %d 0\n", (int)its.vertices.size(), (int)its.indices.size()); + for (int i = 0; i < its.vertices.size(); ++ i) + fprintf(fp, "\t%f %f %f\n", its.vertices[i](0), its.vertices[i](1), its.vertices[i](2)); + for (uint32_t i = 0; i < its.indices.size(); ++ i) + fprintf(fp, "\t3 %d %d %d\n", its.indices[i][0], its.indices[i][1], its.indices[i][2]); + fclose(fp); + return true; +} - /* make sure this function is idempotent and does not leak memory */ - stl_invalidate_shared_vertices(stl); +bool its_write_vrml(const indexed_triangle_set &its, const char *file) +{ + /* Open the file */ + FILE *fp = boost::nowide::fopen(file, "w"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_write_vrml: Couldn't open " << file << " for writing"; + return false; + } - stl->v_indices = (v_indices_struct*) - calloc(stl->stats.number_of_facets, sizeof(v_indices_struct)); - if(stl->v_indices == NULL) perror("stl_generate_shared_vertices"); - stl->v_shared = (stl_vertex*) - calloc((stl->stats.number_of_facets / 2), sizeof(stl_vertex)); - if(stl->v_shared == NULL) perror("stl_generate_shared_vertices"); - stl->stats.shared_malloced = stl->stats.number_of_facets / 2; - stl->stats.shared_vertices = 0; + fprintf(fp, "#VRML V1.0 ascii\n\n"); + fprintf(fp, "Separator {\n"); + fprintf(fp, "\tDEF STLShape ShapeHints {\n"); + fprintf(fp, "\t\tvertexOrdering COUNTERCLOCKWISE\n"); + fprintf(fp, "\t\tfaceType CONVEX\n"); + fprintf(fp, "\t\tshapeType SOLID\n"); + fprintf(fp, "\t\tcreaseAngle 0.0\n"); + fprintf(fp, "\t}\n"); + fprintf(fp, "\tDEF STLModel Separator {\n"); + fprintf(fp, "\t\tDEF STLColor Material {\n"); + fprintf(fp, "\t\t\temissiveColor 0.700000 0.700000 0.000000\n"); + fprintf(fp, "\t\t}\n"); + fprintf(fp, "\t\tDEF STLVertices Coordinate3 {\n"); + fprintf(fp, "\t\t\tpoint [\n"); - for(i = 0; i < stl->stats.number_of_facets; i++) { - stl->v_indices[i].vertex[0] = -1; - stl->v_indices[i].vertex[1] = -1; - stl->v_indices[i].vertex[2] = -1; - } + int i = 0; + for (; i + 1 < its.vertices.size(); ++ i) + fprintf(fp, "\t\t\t\t%f %f %f,\n", its.vertices[i](0), its.vertices[i](1), its.vertices[i](2)); + fprintf(fp, "\t\t\t\t%f %f %f]\n", its.vertices[i](0), its.vertices[i](1), its.vertices[i](2)); + fprintf(fp, "\t\t}\n"); + fprintf(fp, "\t\tDEF STLTriangles IndexedFaceSet {\n"); + fprintf(fp, "\t\t\tcoordIndex [\n"); + + for (size_t i = 0; i + 1 < its.indices.size(); ++ i) + fprintf(fp, "\t\t\t\t%d, %d, %d, -1,\n", its.indices[i][0], its.indices[i][1], its.indices[i][2]); + fprintf(fp, "\t\t\t\t%d, %d, %d, -1]\n", its.indices[i][0], its.indices[i][1], its.indices[i][2]); + fprintf(fp, "\t\t}\n"); + fprintf(fp, "\t}\n"); + fprintf(fp, "}\n"); + fclose(fp); + return true; +} + +bool its_write_obj(const indexed_triangle_set &its, const char *file) +{ + + FILE *fp = boost::nowide::fopen(file, "w"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_write_obj: Couldn't open " << file << " for writing"; + return false; + } + + for (size_t i = 0; i < its.vertices.size(); ++ i) + fprintf(fp, "v %f %f %f\n", its.vertices[i](0), its.vertices[i](1), its.vertices[i](2)); + for (size_t i = 0; i < its.indices.size(); ++ i) + fprintf(fp, "f %d %d %d\n", its.indices[i][0]+1, its.indices[i][1]+1, its.indices[i][2]+1); + fclose(fp); + return true; +} - for(i = 0; i < stl->stats.number_of_facets; i++) { - first_facet = i; - for(j = 0; j < 3; j++) { - if(stl->v_indices[i].vertex[j] != -1) { - continue; - } - if(stl->stats.shared_vertices == stl->stats.shared_malloced) { - stl->stats.shared_malloced += 1024; - stl->v_shared = (stl_vertex*)realloc(stl->v_shared, - stl->stats.shared_malloced * sizeof(stl_vertex)); - if(stl->v_shared == NULL) perror("stl_generate_shared_vertices"); - } +// Check validity of the mesh, assert on error. +bool stl_validate(const stl_file *stl, const indexed_triangle_set &its) +{ + assert(! stl->facet_start.empty()); + assert(stl->facet_start.size() == stl->stats.number_of_facets); + assert(stl->neighbors_start.size() == stl->stats.number_of_facets); + assert(stl->facet_start.size() == stl->neighbors_start.size()); + assert(! stl->neighbors_start.empty()); + assert((its.indices.empty()) == (its.vertices.empty())); + assert(stl->stats.number_of_facets > 0); + assert(its.vertices.empty() || its.indices.size() == stl->stats.number_of_facets); - stl->v_shared[stl->stats.shared_vertices] = - stl->facet_start[i].vertex[j]; - - direction = 0; - reversed = 0; - facet_num = i; - vnot = (j + 2) % 3; - - for(;;) { - if(vnot > 2) { - if(direction == 0) { - pivot_vertex = (vnot + 2) % 3; - next_edge = pivot_vertex; - direction = 1; - } else { - pivot_vertex = (vnot + 1) % 3; - next_edge = vnot % 3; - direction = 0; - } - } else { - if(direction == 0) { - pivot_vertex = (vnot + 1) % 3; - next_edge = vnot; - } else { - pivot_vertex = (vnot + 2) % 3; - next_edge = pivot_vertex; - } +#ifdef _DEBUG + // Verify validity of neighborship data. + for (int facet_idx = 0; facet_idx < (int)stl->stats.number_of_facets; ++ facet_idx) { + const stl_neighbors &nbr = stl->neighbors_start[facet_idx]; + const int *vertices = its.indices.empty() ? nullptr : its.indices[facet_idx].data(); + for (int nbr_idx = 0; nbr_idx < 3; ++ nbr_idx) { + int nbr_face = stl->neighbors_start[facet_idx].neighbor[nbr_idx]; + assert(nbr_face < (int)stl->stats.number_of_facets); + if (nbr_face != -1) { + int nbr_vnot = nbr.which_vertex_not[nbr_idx]; + assert(nbr_vnot >= 0 && nbr_vnot < 6); + // Neighbor of the neighbor is the original face. + assert(stl->neighbors_start[nbr_face].neighbor[(nbr_vnot + 1) % 3] == facet_idx); + int vnot_back = stl->neighbors_start[nbr_face].which_vertex_not[(nbr_vnot + 1) % 3]; + assert(vnot_back >= 0 && vnot_back < 6); + assert((nbr_vnot < 3) == (vnot_back < 3)); + assert(vnot_back % 3 == (nbr_idx + 2) % 3); + if (vertices != nullptr) { + // Has shared vertices. + if (nbr_vnot < 3) { + // Faces facet_idx and nbr_face share two vertices accross the common edge. Faces are correctly oriented. + assert((its.indices[nbr_face][(nbr_vnot + 1) % 3] == vertices[(nbr_idx + 1) % 3] && its.indices[nbr_face][(nbr_vnot + 2) % 3] == vertices[nbr_idx])); + } else { + // Faces facet_idx and nbr_face share two vertices accross the common edge. Faces are incorrectly oriented, one of them is flipped. + assert((its.indices[nbr_face][(nbr_vnot + 2) % 3] == vertices[(nbr_idx + 1) % 3] && its.indices[nbr_face][(nbr_vnot + 1) % 3] == vertices[nbr_idx])); + } + } + } } - stl->v_indices[facet_num].vertex[pivot_vertex] = - stl->stats.shared_vertices; - - next_facet = stl->neighbors_start[facet_num].neighbor[next_edge]; - if(next_facet == -1) { - if(reversed) { - break; - } else { - direction = 1; - vnot = (j + 1) % 3; - reversed = 1; - facet_num = first_facet; - } - } else if(next_facet != first_facet) { - vnot = stl->neighbors_start[facet_num]. - which_vertex_not[next_edge]; - facet_num = next_facet; - } else { - break; - } - } - stl->stats.shared_vertices += 1; } - } +#endif /* _DEBUG */ + + return true; } -void -stl_write_off(stl_file *stl, const char *file) { - int i; - FILE *fp; - char *error_msg; - - if (stl->error) return; - - /* Open the file */ - fp = boost::nowide::fopen(file, "w"); - if(fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - - fprintf(fp, "OFF\n"); - fprintf(fp, "%d %d 0\n", - stl->stats.shared_vertices, stl->stats.number_of_facets); - - for(i = 0; i < stl->stats.shared_vertices; i++) { - fprintf(fp, "\t%f %f %f\n", - stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2)); - } - for(i = 0; i < stl->stats.number_of_facets; i++) { - fprintf(fp, "\t3 %d %d %d\n", stl->v_indices[i].vertex[0], - stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]); - } - fclose(fp); -} - -void -stl_write_vrml(stl_file *stl, const char *file) { - int i; - FILE *fp; - char *error_msg; - - if (stl->error) return; - - /* Open the file */ - fp = boost::nowide::fopen(file, "w"); - if(fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - - fprintf(fp, "#VRML V1.0 ascii\n\n"); - fprintf(fp, "Separator {\n"); - fprintf(fp, "\tDEF STLShape ShapeHints {\n"); - fprintf(fp, "\t\tvertexOrdering COUNTERCLOCKWISE\n"); - fprintf(fp, "\t\tfaceType CONVEX\n"); - fprintf(fp, "\t\tshapeType SOLID\n"); - fprintf(fp, "\t\tcreaseAngle 0.0\n"); - fprintf(fp, "\t}\n"); - fprintf(fp, "\tDEF STLModel Separator {\n"); - fprintf(fp, "\t\tDEF STLColor Material {\n"); - fprintf(fp, "\t\t\temissiveColor 0.700000 0.700000 0.000000\n"); - fprintf(fp, "\t\t}\n"); - fprintf(fp, "\t\tDEF STLVertices Coordinate3 {\n"); - fprintf(fp, "\t\t\tpoint [\n"); - - for(i = 0; i < (stl->stats.shared_vertices - 1); i++) { - fprintf(fp, "\t\t\t\t%f %f %f,\n", - stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2)); - } - fprintf(fp, "\t\t\t\t%f %f %f]\n", - stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2)); - fprintf(fp, "\t\t}\n"); - fprintf(fp, "\t\tDEF STLTriangles IndexedFaceSet {\n"); - fprintf(fp, "\t\t\tcoordIndex [\n"); - - for(i = 0; i < (stl->stats.number_of_facets - 1); i++) { - fprintf(fp, "\t\t\t\t%d, %d, %d, -1,\n", stl->v_indices[i].vertex[0], - stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]); - } - fprintf(fp, "\t\t\t\t%d, %d, %d, -1]\n", stl->v_indices[i].vertex[0], - stl->v_indices[i].vertex[1], stl->v_indices[i].vertex[2]); - fprintf(fp, "\t\t}\n"); - fprintf(fp, "\t}\n"); - fprintf(fp, "}\n"); - fclose(fp); -} - -void stl_write_obj (stl_file *stl, const char *file) { - int i; - FILE* fp; - - if (stl->error) return; - - /* Open the file */ - fp = boost::nowide::fopen(file, "w"); - if (fp == NULL) { - char* error_msg = (char*)malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - - for (i = 0; i < stl->stats.shared_vertices; i++) { - fprintf(fp, "v %f %f %f\n", stl->v_shared[i](0), stl->v_shared[i](1), stl->v_shared[i](2)); - } - for (i = 0; i < stl->stats.number_of_facets; i++) { - fprintf(fp, "f %d %d %d\n", stl->v_indices[i].vertex[0]+1, stl->v_indices[i].vertex[1]+1, stl->v_indices[i].vertex[2]+1); - } - - fclose(fp); +// Check validity of the mesh, assert on error. +bool stl_validate(const stl_file *stl) +{ + indexed_triangle_set its; + return stl_validate(stl, its); } diff --git a/src/admesh/stl.h b/src/admesh/stl.h index f867e197bd..2ac6c7fd28 100644 --- a/src/admesh/stl.h +++ b/src/admesh/stl.h @@ -27,6 +27,7 @@ #include #include +#include #include // Size of the binary STL header, free form. @@ -40,22 +41,23 @@ typedef Eigen::Matrix stl_vertex; typedef Eigen::Matrix stl_normal; +typedef Eigen::Matrix stl_triangle_vertex_indices; static_assert(sizeof(stl_vertex) == 12, "size of stl_vertex incorrect"); static_assert(sizeof(stl_normal) == 12, "size of stl_normal incorrect"); struct stl_facet { - stl_normal normal; - stl_vertex vertex[3]; - char extra[2]; + stl_normal normal; + stl_vertex vertex[3]; + char extra[2]; - stl_facet rotated(const Eigen::Quaternion &rot) { - stl_facet out; - out.normal = rot * this->normal; - out.vertex[0] = rot * this->vertex[0]; - out.vertex[1] = rot * this->vertex[1]; - out.vertex[2] = rot * this->vertex[2]; - return out; - } + stl_facet rotated(const Eigen::Quaternion &rot) const { + stl_facet out; + out.normal = rot * this->normal; + out.vertex[0] = rot * this->vertex[0]; + out.vertex[1] = rot * this->vertex[1]; + out.vertex[2] = rot * this->vertex[2]; + return out; + } }; #define SIZEOF_STL_FACET 50 @@ -67,104 +69,94 @@ static_assert(sizeof(stl_facet) >= SIZEOF_STL_FACET, "size of stl_facet incorrec typedef enum {binary, ascii, inmemory} stl_type; -typedef struct { - stl_vertex p1; - stl_vertex p2; - int facet_number; -} stl_edge; +struct stl_neighbors { + stl_neighbors() { reset(); } + void reset() { + neighbor[0] = -1; + neighbor[1] = -1; + neighbor[2] = -1; + which_vertex_not[0] = -1; + which_vertex_not[1] = -1; + which_vertex_not[2] = -1; + } + int num_neighbors_missing() const { return (this->neighbor[0] == -1) + (this->neighbor[1] == -1) + (this->neighbor[2] == -1); } + int num_neighbors() const { return 3 - this->num_neighbors_missing(); } -typedef struct stl_hash_edge { - // Key of a hash edge: sorted vertices of the edge. - uint32_t key[6]; - // Compare two keys. - bool operator==(const stl_hash_edge &rhs) { return memcmp(key, rhs.key, sizeof(key)) == 0; } - bool operator!=(const stl_hash_edge &rhs) { return ! (*this == rhs); } - int hash(int M) const { return ((key[0] / 11 + key[1] / 7 + key[2] / 3) ^ (key[3] / 11 + key[4] / 7 + key[5] / 3)) % M; } - // Index of a facet owning this edge. - int facet_number; - // Index of this edge inside the facet with an index of facet_number. - // If this edge is stored backwards, which_edge is increased by 3. - int which_edge; - struct stl_hash_edge *next; -} stl_hash_edge; + // Index of a neighbor facet. + int neighbor[3]; + // Index of an opposite vertex at the neighbor face. + char which_vertex_not[3]; +}; -typedef struct { - // Index of a neighbor facet. - int neighbor[3]; - // Index of an opposite vertex at the neighbor face. - char which_vertex_not[3]; -} stl_neighbors; +struct stl_stats { + stl_stats() { this->reset(); } + void reset() { memset(this, 0, sizeof(stl_stats)); this->volume = -1.0; } + char header[81]; + stl_type type; + uint32_t number_of_facets; + stl_vertex max; + stl_vertex min; + stl_vertex size; + float bounding_diameter; + float shortest_edge; + float volume; + int connected_edges; + int connected_facets_1_edge; + int connected_facets_2_edge; + int connected_facets_3_edge; + int facets_w_1_bad_edge; + int facets_w_2_bad_edge; + int facets_w_3_bad_edge; + int original_num_facets; + int edges_fixed; + int degenerate_facets; + int facets_removed; + int facets_added; + int facets_reversed; + int backwards_edges; + int normals_fixed; + int number_of_parts; +}; -typedef struct { - int vertex[3]; -} v_indices_struct; +struct stl_file { + stl_file() {} -typedef struct { - char header[81]; - stl_type type; - uint32_t number_of_facets; - stl_vertex max; - stl_vertex min; - stl_vertex size; - float bounding_diameter; - float shortest_edge; - float volume; - unsigned number_of_blocks; - int connected_edges; - int connected_facets_1_edge; - int connected_facets_2_edge; - int connected_facets_3_edge; - int facets_w_1_bad_edge; - int facets_w_2_bad_edge; - int facets_w_3_bad_edge; - int original_num_facets; - int edges_fixed; - int degenerate_facets; - int facets_removed; - int facets_added; - int facets_reversed; - int backwards_edges; - int normals_fixed; - int number_of_parts; - int malloced; - int freed; - int facets_malloced; - int collisions; - int shared_vertices; - int shared_malloced; -} stl_stats; + void clear() { + this->facet_start.clear(); + this->neighbors_start.clear(); + this->stats.reset(); + } -typedef struct { - FILE *fp; - stl_facet *facet_start; - stl_hash_edge **heads; - stl_hash_edge *tail; - int M; - stl_neighbors *neighbors_start; - v_indices_struct *v_indices; - stl_vertex *v_shared; - stl_stats stats; - char error; -} stl_file; + std::vector facet_start; + std::vector neighbors_start; + // Statistics + stl_stats stats; +}; +struct indexed_triangle_set +{ + indexed_triangle_set() {} -extern void stl_open(stl_file *stl, const char *file); -extern void stl_close(stl_file *stl); + void clear() { indices.clear(); vertices.clear(); } + + std::vector indices; + std::vector vertices; + //FIXME add normals once we get rid of the stl_file from TriangleMesh completely. + //std::vector normals +}; + +extern bool stl_open(stl_file *stl, const char *file); extern void stl_stats_out(stl_file *stl, FILE *file, char *input_file); -extern void stl_print_neighbors(stl_file *stl, char *file); -extern void stl_put_little_int(FILE *fp, int value_in); -extern void stl_put_little_float(FILE *fp, float value_in); -extern void stl_write_ascii(stl_file *stl, const char *file, const char *label); -extern void stl_write_binary(stl_file *stl, const char *file, const char *label); -extern void stl_write_binary_block(stl_file *stl, FILE *fp); +extern bool stl_print_neighbors(stl_file *stl, char *file); +extern bool stl_write_ascii(stl_file *stl, const char *file, const char *label); +extern bool stl_write_binary(stl_file *stl, const char *file, const char *label); extern void stl_check_facets_exact(stl_file *stl); extern void stl_check_facets_nearby(stl_file *stl, float tolerance); extern void stl_remove_unconnected_facets(stl_file *stl); extern void stl_write_vertex(stl_file *stl, int facet, int vertex); extern void stl_write_facet(stl_file *stl, char *label, int facet); -extern void stl_write_edge(stl_file *stl, char *label, stl_hash_edge edge); extern void stl_write_neighbor(stl_file *stl, int facet); -extern void stl_write_quad_object(stl_file *stl, char *file); +extern bool stl_write_quad_object(stl_file *stl, char *file); extern void stl_verify_neighbors(stl_file *stl); extern void stl_fill_holes(stl_file *stl); extern void stl_fix_normal_directions(stl_file *stl); @@ -186,36 +178,30 @@ extern void stl_get_size(stl_file *stl); template extern void stl_transform(stl_file *stl, T *trafo3x4) { - if (stl->error) - return; + for (uint32_t i_face = 0; i_face < stl->stats.number_of_facets; ++ i_face) { + stl_facet &face = stl->facet_start[i_face]; + for (int i_vertex = 0; i_vertex < 3; ++ i_vertex) { + stl_vertex &v_dst = face.vertex[i_vertex]; + stl_vertex v_src = v_dst; + v_dst(0) = T(trafo3x4[0] * v_src(0) + trafo3x4[1] * v_src(1) + trafo3x4[2] * v_src(2) + trafo3x4[3]); + v_dst(1) = T(trafo3x4[4] * v_src(0) + trafo3x4[5] * v_src(1) + trafo3x4[6] * v_src(2) + trafo3x4[7]); + v_dst(2) = T(trafo3x4[8] * v_src(0) + trafo3x4[9] * v_src(1) + trafo3x4[10] * v_src(2) + trafo3x4[11]); + } + stl_vertex &v_dst = face.normal; + stl_vertex v_src = v_dst; + v_dst(0) = T(trafo3x4[0] * v_src(0) + trafo3x4[1] * v_src(1) + trafo3x4[2] * v_src(2)); + v_dst(1) = T(trafo3x4[4] * v_src(0) + trafo3x4[5] * v_src(1) + trafo3x4[6] * v_src(2)); + v_dst(2) = T(trafo3x4[8] * v_src(0) + trafo3x4[9] * v_src(1) + trafo3x4[10] * v_src(2)); + } - for (uint32_t i_face = 0; i_face < stl->stats.number_of_facets; ++ i_face) { - stl_facet &face = stl->facet_start[i_face]; - for (int i_vertex = 0; i_vertex < 3; ++ i_vertex) { - stl_vertex &v_dst = face.vertex[i_vertex]; - stl_vertex v_src = v_dst; - v_dst(0) = T(trafo3x4[0] * v_src(0) + trafo3x4[1] * v_src(1) + trafo3x4[2] * v_src(2) + trafo3x4[3]); - v_dst(1) = T(trafo3x4[4] * v_src(0) + trafo3x4[5] * v_src(1) + trafo3x4[6] * v_src(2) + trafo3x4[7]); - v_dst(2) = T(trafo3x4[8] * v_src(0) + trafo3x4[9] * v_src(1) + trafo3x4[10] * v_src(2) + trafo3x4[11]); - } - stl_vertex &v_dst = face.normal; - stl_vertex v_src = v_dst; - v_dst(0) = T(trafo3x4[0] * v_src(0) + trafo3x4[1] * v_src(1) + trafo3x4[2] * v_src(2)); - v_dst(1) = T(trafo3x4[4] * v_src(0) + trafo3x4[5] * v_src(1) + trafo3x4[6] * v_src(2)); - v_dst(2) = T(trafo3x4[8] * v_src(0) + trafo3x4[9] * v_src(1) + trafo3x4[10] * v_src(2)); - } - - stl_get_size(stl); + stl_get_size(stl); } template inline void stl_transform(stl_file *stl, const Eigen::Transform& t) { - if (stl->error) - return; - const Eigen::Matrix r = t.matrix().template block<3, 3>(0, 0); - for (size_t i = 0; i < stl->stats.number_of_facets; ++i) { + for (size_t i = 0; i < stl->stats.number_of_facets; ++ i) { stl_facet &f = stl->facet_start[i]; for (size_t j = 0; j < 3; ++j) f.vertex[j] = (t * f.vertex[j].template cast()).template cast().eval(); @@ -228,10 +214,7 @@ inline void stl_transform(stl_file *stl, const Eigen::Transform inline void stl_transform(stl_file *stl, const Eigen::Matrix& m) { - if (stl->error) - return; - - for (size_t i = 0; i < stl->stats.number_of_facets; ++i) { + for (size_t i = 0; i < stl->stats.number_of_facets; ++ i) { stl_facet &f = stl->facet_start[i]; for (size_t j = 0; j < 3; ++j) f.vertex[j] = (m * f.vertex[j].template cast()).template cast().eval(); @@ -241,13 +224,43 @@ inline void stl_transform(stl_file *stl, const Eigen::Matrix +extern void its_transform(indexed_triangle_set &its, T *trafo3x4) +{ + for (stl_vertex &v_dst : its.vertices) { + stl_vertex v_src = v_dst; + v_dst(0) = T(trafo3x4[0] * v_src(0) + trafo3x4[1] * v_src(1) + trafo3x4[2] * v_src(2) + trafo3x4[3]); + v_dst(1) = T(trafo3x4[4] * v_src(0) + trafo3x4[5] * v_src(1) + trafo3x4[6] * v_src(2) + trafo3x4[7]); + v_dst(2) = T(trafo3x4[8] * v_src(0) + trafo3x4[9] * v_src(1) + trafo3x4[10] * v_src(2) + trafo3x4[11]); + } +} + +template +inline void its_transform(indexed_triangle_set &its, const Eigen::Transform& t) +{ + const Eigen::Matrix r = t.matrix().template block<3, 3>(0, 0); + for (stl_vertex &v : its.vertices) + v = (t * v.template cast()).template cast().eval(); +} + +template +inline void its_transform(indexed_triangle_set &its, const Eigen::Matrix& m) +{ + for (stl_vertex &v : its.vertices) + v = (m * v.template cast()).template cast().eval(); +} + +extern void its_rotate_x(indexed_triangle_set &its, float angle); +extern void its_rotate_y(indexed_triangle_set &its, float angle); +extern void its_rotate_z(indexed_triangle_set &its, float angle); + +extern void stl_generate_shared_vertices(stl_file *stl, indexed_triangle_set &its); +extern bool its_write_obj(const indexed_triangle_set &its, const char *file); +extern bool its_write_off(const indexed_triangle_set &its, const char *file); +extern bool its_write_vrml(const indexed_triangle_set &its, const char *file); + +extern bool stl_write_dxf(stl_file *stl, const char *file, char *label); inline void stl_calculate_normal(stl_normal &normal, stl_facet *facet) { normal = (facet->vertex[1] - facet->vertex[0]).cross(facet->vertex[2] - facet->vertex[0]); } @@ -258,24 +271,18 @@ inline void stl_normalize_vector(stl_normal &normal) { else normal *= float(1.0 / length); } -inline bool stl_vertex_lower(const stl_vertex &a, const stl_vertex &b) { - return (a(0) != b(0)) ? (a(0) < b(0)) : - ((a(1) != b(1)) ? (a(1) < b(1)) : (a(2) < b(2))); -} extern void stl_calculate_volume(stl_file *stl); -extern void stl_repair(stl_file *stl, int fixall_flag, int exact_flag, int tolerance_flag, float tolerance, int increment_flag, float increment, int nearby_flag, int iterations, int remove_unconnected_flag, int fill_holes_flag, int normal_directions_flag, int normal_values_flag, int reverse_all_flag, int verbose_flag); +extern void stl_repair(stl_file *stl, bool fixall_flag, bool exact_flag, bool tolerance_flag, float tolerance, bool increment_flag, float increment, bool nearby_flag, int iterations, bool remove_unconnected_flag, bool fill_holes_flag, bool normal_directions_flag, bool normal_values_flag, bool reverse_all_flag, bool verbose_flag); -extern void stl_initialize(stl_file *stl); -extern void stl_count_facets(stl_file *stl, const char *file); extern void stl_allocate(stl_file *stl); extern void stl_read(stl_file *stl, int first_facet, bool first); extern void stl_facet_stats(stl_file *stl, stl_facet facet, bool &first); extern void stl_reallocate(stl_file *stl); -extern void stl_add_facet(stl_file *stl, stl_facet *new_facet); +extern void stl_add_facet(stl_file *stl, const stl_facet *new_facet); -extern void stl_clear_error(stl_file *stl); -extern int stl_get_error(stl_file *stl); -extern void stl_exit_on_error(stl_file *stl); +// Validate the mesh, assert on error. +extern bool stl_validate(const stl_file *stl); +extern bool stl_validate(const stl_file *stl, const indexed_triangle_set &its); #endif diff --git a/src/admesh/stl_io.cpp b/src/admesh/stl_io.cpp index 85f66785b3..464c98907a 100644 --- a/src/admesh/stl_io.cpp +++ b/src/admesh/stl_io.cpp @@ -22,159 +22,86 @@ #include #include + +#include +#include +#include + #include "stl.h" -#include -#include - -#if !defined(SEEK_SET) -#define SEEK_SET 0 -#define SEEK_CUR 1 -#define SEEK_END 2 -#endif - -void -stl_stats_out(stl_file *stl, FILE *file, char *input_file) { - if (stl->error) return; - - /* this is here for Slic3r, without our config.h - it won't use this part of the code anyway */ +void stl_stats_out(stl_file *stl, FILE *file, char *input_file) +{ + // This is here for Slic3r, without our config.h it won't use this part of the code anyway. #ifndef VERSION #define VERSION "unknown" #endif - fprintf(file, "\n\ -================= Results produced by ADMesh version " VERSION " ================\n"); - fprintf(file, "\ -Input file : %s\n", input_file); - if(stl->stats.type == binary) { - fprintf(file, "\ -File type : Binary STL file\n"); - } else { - fprintf(file, "\ -File type : ASCII STL file\n"); - } - fprintf(file, "\ -Header : %s\n", stl->stats.header); - fprintf(file, "============== Size ==============\n"); - fprintf(file, "Min X = % f, Max X = % f\n", - stl->stats.min(0), stl->stats.max(0)); - fprintf(file, "Min Y = % f, Max Y = % f\n", - stl->stats.min(1), stl->stats.max(1)); - fprintf(file, "Min Z = % f, Max Z = % f\n", - stl->stats.min(2), stl->stats.max(2)); - - fprintf(file, "\ -========= Facet Status ========== Original ============ Final ====\n"); - fprintf(file, "\ -Number of facets : %5d %5d\n", - stl->stats.original_num_facets, stl->stats.number_of_facets); - fprintf(file, "\ -Facets with 1 disconnected edge : %5d %5d\n", - stl->stats.facets_w_1_bad_edge, stl->stats.connected_facets_2_edge - - stl->stats.connected_facets_3_edge); - fprintf(file, "\ -Facets with 2 disconnected edges : %5d %5d\n", - stl->stats.facets_w_2_bad_edge, stl->stats.connected_facets_1_edge - - stl->stats.connected_facets_2_edge); - fprintf(file, "\ -Facets with 3 disconnected edges : %5d %5d\n", - stl->stats.facets_w_3_bad_edge, stl->stats.number_of_facets - - stl->stats.connected_facets_1_edge); - fprintf(file, "\ -Total disconnected facets : %5d %5d\n", - stl->stats.facets_w_1_bad_edge + stl->stats.facets_w_2_bad_edge + - stl->stats.facets_w_3_bad_edge, stl->stats.number_of_facets - - stl->stats.connected_facets_3_edge); - - fprintf(file, - "=== Processing Statistics === ===== Other Statistics =====\n"); - fprintf(file, "\ -Number of parts : %5d Volume : % f\n", - stl->stats.number_of_parts, stl->stats.volume); - fprintf(file, "\ -Degenerate facets : %5d\n", stl->stats.degenerate_facets); - fprintf(file, "\ -Edges fixed : %5d\n", stl->stats.edges_fixed); - fprintf(file, "\ -Facets removed : %5d\n", stl->stats.facets_removed); - fprintf(file, "\ -Facets added : %5d\n", stl->stats.facets_added); - fprintf(file, "\ -Facets reversed : %5d\n", stl->stats.facets_reversed); - fprintf(file, "\ -Backwards edges : %5d\n", stl->stats.backwards_edges); - fprintf(file, "\ -Normals fixed : %5d\n", stl->stats.normals_fixed); + fprintf(file, "\n================= Results produced by ADMesh version " VERSION " ================\n"); + fprintf(file, "Input file : %s\n", input_file); + if (stl->stats.type == binary) + fprintf(file, "File type : Binary STL file\n"); + else + fprintf(file, "File type : ASCII STL file\n"); + fprintf(file, "Header : %s\n", stl->stats.header); + fprintf(file, "============== Size ==============\n"); + fprintf(file, "Min X = % f, Max X = % f\n", stl->stats.min(0), stl->stats.max(0)); + fprintf(file, "Min Y = % f, Max Y = % f\n", stl->stats.min(1), stl->stats.max(1)); + fprintf(file, "Min Z = % f, Max Z = % f\n", stl->stats.min(2), stl->stats.max(2)); + fprintf(file, "========= Facet Status ========== Original ============ Final ====\n"); + fprintf(file, "Number of facets : %5d %5d\n", stl->stats.original_num_facets, stl->stats.number_of_facets); + fprintf(file, "Facets with 1 disconnected edge : %5d %5d\n", + stl->stats.facets_w_1_bad_edge, stl->stats.connected_facets_2_edge - stl->stats.connected_facets_3_edge); + fprintf(file, "Facets with 2 disconnected edges : %5d %5d\n", + stl->stats.facets_w_2_bad_edge, stl->stats.connected_facets_1_edge - stl->stats.connected_facets_2_edge); + fprintf(file, "Facets with 3 disconnected edges : %5d %5d\n", + stl->stats.facets_w_3_bad_edge, stl->stats.number_of_facets - stl->stats.connected_facets_1_edge); + fprintf(file, "Total disconnected facets : %5d %5d\n", + stl->stats.facets_w_1_bad_edge + stl->stats.facets_w_2_bad_edge + stl->stats.facets_w_3_bad_edge, stl->stats.number_of_facets - stl->stats.connected_facets_3_edge); + fprintf(file, "=== Processing Statistics === ===== Other Statistics =====\n"); + fprintf(file, "Number of parts : %5d Volume : %f\n", stl->stats.number_of_parts, stl->stats.volume); + fprintf(file, "Degenerate facets : %5d\n", stl->stats.degenerate_facets); + fprintf(file, "Edges fixed : %5d\n", stl->stats.edges_fixed); + fprintf(file, "Facets removed : %5d\n", stl->stats.facets_removed); + fprintf(file, "Facets added : %5d\n", stl->stats.facets_added); + fprintf(file, "Facets reversed : %5d\n", stl->stats.facets_reversed); + fprintf(file, "Backwards edges : %5d\n", stl->stats.backwards_edges); + fprintf(file, "Normals fixed : %5d\n", stl->stats.normals_fixed); } -void -stl_write_ascii(stl_file *stl, const char *file, const char *label) { - int i; - char *error_msg; +bool stl_write_ascii(stl_file *stl, const char *file, const char *label) +{ + FILE *fp = boost::nowide::fopen(file, "w"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_write_ascii: Couldn't open " << file << " for writing"; + return false; + } - if (stl->error) return; + fprintf(fp, "solid %s\n", label); - /* Open the file */ - FILE *fp = boost::nowide::fopen(file, "w"); - if(fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + fprintf(fp, " facet normal % .8E % .8E % .8E\n", stl->facet_start[i].normal(0), stl->facet_start[i].normal(1), stl->facet_start[i].normal(2)); + fprintf(fp, " outer loop\n"); + fprintf(fp, " vertex % .8E % .8E % .8E\n", stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), stl->facet_start[i].vertex[0](2)); + fprintf(fp, " vertex % .8E % .8E % .8E\n", stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), stl->facet_start[i].vertex[1](2)); + fprintf(fp, " vertex % .8E % .8E % .8E\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2)); + fprintf(fp, " endloop\n"); + fprintf(fp, " endfacet\n"); + } - fprintf(fp, "solid %s\n", label); - - for(i = 0; i < stl->stats.number_of_facets; i++) { - fprintf(fp, " facet normal % .8E % .8E % .8E\n", - stl->facet_start[i].normal(0), stl->facet_start[i].normal(1), - stl->facet_start[i].normal(2)); - fprintf(fp, " outer loop\n"); - fprintf(fp, " vertex % .8E % .8E % .8E\n", - stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), - stl->facet_start[i].vertex[0](2)); - fprintf(fp, " vertex % .8E % .8E % .8E\n", - stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), - stl->facet_start[i].vertex[1](2)); - fprintf(fp, " vertex % .8E % .8E % .8E\n", - stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), - stl->facet_start[i].vertex[2](2)); - fprintf(fp, " endloop\n"); - fprintf(fp, " endfacet\n"); - } - - fprintf(fp, "endsolid %s\n", label); - - fclose(fp); + fprintf(fp, "endsolid %s\n", label); + fclose(fp); + return true; } -void -stl_print_neighbors(stl_file *stl, char *file) { - int i; - FILE *fp; - char *error_msg; +bool stl_print_neighbors(stl_file *stl, char *file) +{ + FILE *fp = boost::nowide::fopen(file, "w"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_print_neighbors: Couldn't open " << file << " for writing"; + return false; + } - if (stl->error) return; - - /* Open the file */ - fp = boost::nowide::fopen(file, "w"); - if(fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_print_neighbors: Couldn't open %s for writing", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - - for(i = 0; i < stl->stats.number_of_facets; i++) { - fprintf(fp, "%d, %d,%d, %d,%d, %d,%d\n", + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + fprintf(fp, "%d, %d,%d, %d,%d, %d,%d\n", i, stl->neighbors_start[i].neighbor[0], (int)stl->neighbors_start[i].which_vertex_not[0], @@ -182,234 +109,142 @@ stl_print_neighbors(stl_file *stl, char *file) { (int)stl->neighbors_start[i].which_vertex_not[1], stl->neighbors_start[i].neighbor[2], (int)stl->neighbors_start[i].which_vertex_not[2]); - } - fclose(fp); + } + fclose(fp); + return true; } -#ifndef BOOST_LITTLE_ENDIAN +#if BOOST_ENDIAN_BIG_BYTE // Swap a buffer of 32bit data from little endian to big endian and vice versa. void stl_internal_reverse_quads(char *buf, size_t cnt) { - for (size_t i = 0; i < cnt; i += 4) { - std::swap(buf[i], buf[i+3]); - std::swap(buf[i+1], buf[i+2]); - } + for (size_t i = 0; i < cnt; i += 4) { + std::swap(buf[i], buf[i+3]); + std::swap(buf[i+1], buf[i+2]); + } } #endif -void -stl_write_binary(stl_file *stl, const char *file, const char *label) { - FILE *fp; - int i; - char *error_msg; +bool stl_write_binary(stl_file *stl, const char *file, const char *label) +{ + FILE *fp = boost::nowide::fopen(file, "wb"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_write_binary: Couldn't open " << file << " for writing"; + return false; + } - if (stl->error) return; + fprintf(fp, "%s", label); + for (size_t i = strlen(label); i < LABEL_SIZE; ++ i) + putc(0, fp); - /* Open the file */ - fp = boost::nowide::fopen(file, "wb"); - if(fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_write_binary: Couldn't open %s for writing", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - - fprintf(fp, "%s", label); - for(i = strlen(label); i < LABEL_SIZE; i++) putc(0, fp); - - fseek(fp, LABEL_SIZE, SEEK_SET); -#ifdef BOOST_LITTLE_ENDIAN - fwrite(&stl->stats.number_of_facets, 4, 1, fp); - for (i = 0; i < stl->stats.number_of_facets; ++ i) - fwrite(stl->facet_start + i, SIZEOF_STL_FACET, 1, fp); -#else /* BOOST_LITTLE_ENDIAN */ - char buffer[50]; - // Convert the number of facets to little endian. - memcpy(buffer, &stl->stats.number_of_facets, 4); - stl_internal_reverse_quads(buffer, 4); - fwrite(buffer, 4, 1, fp); - for (i = 0; i < stl->stats.number_of_facets; ++ i) { - memcpy(buffer, stl->facet_start + i, 50); - // Convert to little endian. - stl_internal_reverse_quads(buffer, 48); - fwrite(buffer, SIZEOF_STL_FACET, 1, fp); - } -#endif /* BOOST_LITTLE_ENDIAN */ - fclose(fp); +#if !defined(SEEK_SET) + #define SEEK_SET 0 +#endif + fseek(fp, LABEL_SIZE, SEEK_SET); +#if BOOST_ENDIAN_LITTLE_BYTE + fwrite(&stl->stats.number_of_facets, 4, 1, fp); + for (const stl_facet &facet : stl->facet_start) + fwrite(&facet, SIZEOF_STL_FACET, 1, fp); +#else /* BOOST_ENDIAN_LITTLE_BYTE */ + char buffer[50]; + // Convert the number of facets to little endian. + memcpy(buffer, &stl->stats.number_of_facets, 4); + stl_internal_reverse_quads(buffer, 4); + fwrite(buffer, 4, 1, fp); + for (i = 0; i < stl->stats.number_of_facets; ++ i) { + memcpy(buffer, stl->facet_start + i, 50); + // Convert to little endian. + stl_internal_reverse_quads(buffer, 48); + fwrite(buffer, SIZEOF_STL_FACET, 1, fp); + } +#endif /* BOOST_ENDIAN_LITTLE_BYTE */ + fclose(fp); + return true; } -void -stl_write_vertex(stl_file *stl, int facet, int vertex) { - if (stl->error) return; - printf(" vertex %d/%d % .8E % .8E % .8E\n", vertex, facet, +void stl_write_vertex(stl_file *stl, int facet, int vertex) +{ + printf(" vertex %d/%d % .8E % .8E % .8E\n", vertex, facet, stl->facet_start[facet].vertex[vertex](0), stl->facet_start[facet].vertex[vertex](1), stl->facet_start[facet].vertex[vertex](2)); } -void -stl_write_facet(stl_file *stl, char *label, int facet) { - if (stl->error) return; - printf("facet (%d)/ %s\n", facet, label); - stl_write_vertex(stl, facet, 0); - stl_write_vertex(stl, facet, 1); - stl_write_vertex(stl, facet, 2); +void stl_write_facet(stl_file *stl, char *label, int facet) +{ + printf("facet (%d)/ %s\n", facet, label); + stl_write_vertex(stl, facet, 0); + stl_write_vertex(stl, facet, 1); + stl_write_vertex(stl, facet, 2); } -void -stl_write_edge(stl_file *stl, char *label, stl_hash_edge edge) { - if (stl->error) return; - printf("edge (%d)/(%d) %s\n", edge.facet_number, edge.which_edge, label); - if(edge.which_edge < 3) { - stl_write_vertex(stl, edge.facet_number, edge.which_edge % 3); - stl_write_vertex(stl, edge.facet_number, (edge.which_edge + 1) % 3); - } else { - stl_write_vertex(stl, edge.facet_number, (edge.which_edge + 1) % 3); - stl_write_vertex(stl, edge.facet_number, edge.which_edge % 3); - } +void stl_write_neighbor(stl_file *stl, int facet) +{ + printf("Neighbors %d: %d, %d, %d ; %d, %d, %d\n", facet, + stl->neighbors_start[facet].neighbor[0], + stl->neighbors_start[facet].neighbor[1], + stl->neighbors_start[facet].neighbor[2], + stl->neighbors_start[facet].which_vertex_not[0], + stl->neighbors_start[facet].which_vertex_not[1], + stl->neighbors_start[facet].which_vertex_not[2]); } -void -stl_write_neighbor(stl_file *stl, int facet) { - if (stl->error) return; - printf("Neighbors %d: %d, %d, %d ; %d, %d, %d\n", facet, - stl->neighbors_start[facet].neighbor[0], - stl->neighbors_start[facet].neighbor[1], - stl->neighbors_start[facet].neighbor[2], - stl->neighbors_start[facet].which_vertex_not[0], - stl->neighbors_start[facet].which_vertex_not[1], - stl->neighbors_start[facet].which_vertex_not[2]); -} +bool stl_write_quad_object(stl_file *stl, char *file) +{ + stl_vertex connect_color = stl_vertex::Zero(); + stl_vertex uncon_1_color = stl_vertex::Zero(); + stl_vertex uncon_2_color = stl_vertex::Zero(); + stl_vertex uncon_3_color = stl_vertex::Zero(); + stl_vertex color; -void -stl_write_quad_object(stl_file *stl, char *file) { - FILE *fp; - int i; - int j; - char *error_msg; - stl_vertex connect_color = stl_vertex::Zero(); - stl_vertex uncon_1_color = stl_vertex::Zero(); - stl_vertex uncon_2_color = stl_vertex::Zero(); - stl_vertex uncon_3_color = stl_vertex::Zero(); - stl_vertex color; + FILE *fp = boost::nowide::fopen(file, "w"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_write_quad_object: Couldn't open " << file << " for writing"; + return false; + } - if (stl->error) return; - - /* Open the file */ - fp = boost::nowide::fopen(file, "w"); - if(fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_write_quad_object: Couldn't open %s for writing", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - - fprintf(fp, "CQUAD\n"); - for(i = 0; i < stl->stats.number_of_facets; i++) { - j = ((stl->neighbors_start[i].neighbor[0] == -1) + - (stl->neighbors_start[i].neighbor[1] == -1) + - (stl->neighbors_start[i].neighbor[2] == -1)); - if(j == 0) { - color = connect_color; - } else if(j == 1) { - color = uncon_1_color; - } else if(j == 2) { - color = uncon_2_color; - } else { - color = uncon_3_color; - } - fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", - stl->facet_start[i].vertex[0](0), - stl->facet_start[i].vertex[0](1), - stl->facet_start[i].vertex[0](2), color(0), color(1), color(2)); - fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", - stl->facet_start[i].vertex[1](0), - stl->facet_start[i].vertex[1](1), - stl->facet_start[i].vertex[1](2), color(0), color(1), color(2)); - fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", - stl->facet_start[i].vertex[2](0), - stl->facet_start[i].vertex[2](1), - stl->facet_start[i].vertex[2](2), color(0), color(1), color(2)); - fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", - stl->facet_start[i].vertex[2](0), - stl->facet_start[i].vertex[2](1), - stl->facet_start[i].vertex[2](2), color(0), color(1), color(2)); + fprintf(fp, "CQUAD\n"); + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + switch (stl->neighbors_start[i].num_neighbors_missing()) { + case 0: color = connect_color; break; + case 1: color = uncon_1_color; break; + case 2: color = uncon_2_color; break; + default: color = uncon_3_color; + } + fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), stl->facet_start[i].vertex[0](2), color(0), color(1), color(2)); + fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), stl->facet_start[i].vertex[1](2), color(0), color(1), color(2)); + fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2), color(0), color(1), color(2)); + fprintf(fp, "%f %f %f %1.1f %1.1f %1.1f 1\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2), color(0), color(1), color(2)); } fclose(fp); + return true; } -void -stl_write_dxf(stl_file *stl, const char *file, char *label) { - int i; - FILE *fp; - char *error_msg; +bool stl_write_dxf(stl_file *stl, const char *file, char *label) +{ + FILE *fp = boost::nowide::fopen(file, "w"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_write_quad_object: Couldn't open " << file << " for writing"; + return false; + } - if (stl->error) return; + fprintf(fp, "999\n%s\n", label); + fprintf(fp, "0\nSECTION\n2\nHEADER\n0\nENDSEC\n"); + fprintf(fp, "0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n1\n\ + 0\nLAYER\n2\n0\n70\n0\n62\n7\n6\nCONTINUOUS\n0\nENDTAB\n0\nENDSEC\n"); + fprintf(fp, "0\nSECTION\n2\nBLOCKS\n0\nENDSEC\n"); - /* Open the file */ - fp = boost::nowide::fopen(file, "w"); - if(fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_write_ascii: Couldn't open %s for writing", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } + fprintf(fp, "0\nSECTION\n2\nENTITIES\n"); - fprintf(fp, "999\n%s\n", label); - fprintf(fp, "0\nSECTION\n2\nHEADER\n0\nENDSEC\n"); - fprintf(fp, "0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLAYER\n70\n1\n\ -0\nLAYER\n2\n0\n70\n0\n62\n7\n6\nCONTINUOUS\n0\nENDTAB\n0\nENDSEC\n"); - fprintf(fp, "0\nSECTION\n2\nBLOCKS\n0\nENDSEC\n"); + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + fprintf(fp, "0\n3DFACE\n8\n0\n"); + fprintf(fp, "10\n%f\n20\n%f\n30\n%f\n", stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), stl->facet_start[i].vertex[0](2)); + fprintf(fp, "11\n%f\n21\n%f\n31\n%f\n", stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), stl->facet_start[i].vertex[1](2)); + fprintf(fp, "12\n%f\n22\n%f\n32\n%f\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2)); + fprintf(fp, "13\n%f\n23\n%f\n33\n%f\n", stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), stl->facet_start[i].vertex[2](2)); + } - fprintf(fp, "0\nSECTION\n2\nENTITIES\n"); - - for(i = 0; i < stl->stats.number_of_facets; i++) { - fprintf(fp, "0\n3DFACE\n8\n0\n"); - fprintf(fp, "10\n%f\n20\n%f\n30\n%f\n", - stl->facet_start[i].vertex[0](0), stl->facet_start[i].vertex[0](1), - stl->facet_start[i].vertex[0](2)); - fprintf(fp, "11\n%f\n21\n%f\n31\n%f\n", - stl->facet_start[i].vertex[1](0), stl->facet_start[i].vertex[1](1), - stl->facet_start[i].vertex[1](2)); - fprintf(fp, "12\n%f\n22\n%f\n32\n%f\n", - stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), - stl->facet_start[i].vertex[2](2)); - fprintf(fp, "13\n%f\n23\n%f\n33\n%f\n", - stl->facet_start[i].vertex[2](0), stl->facet_start[i].vertex[2](1), - stl->facet_start[i].vertex[2](2)); - } - - fprintf(fp, "0\nENDSEC\n0\nEOF\n"); - - fclose(fp); -} - -void -stl_clear_error(stl_file *stl) { - stl->error = 0; -} - -void -stl_exit_on_error(stl_file *stl) { - if (!stl->error) return; - stl->error = 0; - stl_close(stl); - exit(1); -} - -int -stl_get_error(stl_file *stl) { - return stl->error; + fprintf(fp, "0\nENDSEC\n0\nEOF\n"); + fclose(fp); + return true; } diff --git a/src/admesh/stlinit.cpp b/src/admesh/stlinit.cpp index 911f4f5e82..a328baa75f 100644 --- a/src/admesh/stlinit.cpp +++ b/src/admesh/stlinit.cpp @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -35,351 +36,236 @@ #error "SEEK_SET not defined" #endif -void -stl_open(stl_file *stl, const char *file) { - stl_initialize(stl); - stl_count_facets(stl, file); - stl_allocate(stl); - stl_read(stl, 0, true); - if (stl->fp != nullptr) { - fclose(stl->fp); - stl->fp = nullptr; - } +static FILE* stl_open_count_facets(stl_file *stl, const char *file) +{ + // Open the file in binary mode first. + FILE *fp = boost::nowide::fopen(file, "rb"); + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_open_count_facets: Couldn't open " << file << " for reading"; + return nullptr; + } + // Find size of file. + fseek(fp, 0, SEEK_END); + long file_size = ftell(fp); + + // Check for binary or ASCII file. + fseek(fp, HEADER_SIZE, SEEK_SET); + unsigned char chtest[128]; + if (! fread(chtest, sizeof(chtest), 1, fp)) { + BOOST_LOG_TRIVIAL(error) << "stl_open_count_facets: The input is an empty file: " << file; + fclose(fp); + return nullptr; + } + stl->stats.type = ascii; + for (size_t s = 0; s < sizeof(chtest); s++) { + if (chtest[s] > 127) { + stl->stats.type = binary; + break; + } + } + rewind(fp); + + uint32_t num_facets = 0; + + // Get the header and the number of facets in the .STL file. + // If the .STL file is binary, then do the following: + if (stl->stats.type == binary) { + // Test if the STL file has the right size. + if (((file_size - HEADER_SIZE) % SIZEOF_STL_FACET != 0) || (file_size < STL_MIN_FILE_SIZE)) { + BOOST_LOG_TRIVIAL(error) << "stl_open_count_facets: The file " << file << " has the wrong size."; + fclose(fp); + return nullptr; + } + num_facets = (file_size - HEADER_SIZE) / SIZEOF_STL_FACET; + + // Read the header. + if (fread(stl->stats.header, LABEL_SIZE, 1, fp) > 79) + stl->stats.header[80] = '\0'; + + // Read the int following the header. This should contain # of facets. + uint32_t header_num_facets; + bool header_num_faces_read = fread(&header_num_facets, sizeof(uint32_t), 1, fp) != 0; +#ifndef BOOST_LITTLE_ENDIAN + // Convert from little endian to big endian. + stl_internal_reverse_quads((char*)&header_num_facets, 4); +#endif /* BOOST_LITTLE_ENDIAN */ + if (! header_num_faces_read || num_facets != header_num_facets) + BOOST_LOG_TRIVIAL(info) << "stl_open_count_facets: Warning: File size doesn't match number of facets in the header: " << file; + } + // Otherwise, if the .STL file is ASCII, then do the following: + else + { + // Reopen the file in text mode (for getting correct newlines on Windows) + // fix to silence a warning about unused return value. + // obviously if it fails we have problems.... + fp = boost::nowide::freopen(file, "r", fp); + + // do another null check to be safe + if (fp == nullptr) { + BOOST_LOG_TRIVIAL(error) << "stl_open_count_facets: Couldn't open " << file << " for reading"; + fclose(fp); + return nullptr; + } + + // Find the number of facets. + char linebuf[100]; + int num_lines = 1; + while (fgets(linebuf, 100, fp) != nullptr) { + // Don't count short lines. + if (strlen(linebuf) <= 4) + continue; + // Skip solid/endsolid lines as broken STL file generators may put several of them. + if (strncmp(linebuf, "solid", 5) == 0 || strncmp(linebuf, "endsolid", 8) == 0) + continue; + ++ num_lines; + } + + rewind(fp); + + // Get the header. + int i = 0; + for (; i < 80 && (stl->stats.header[i] = getc(fp)) != '\n'; ++ i) ; + stl->stats.header[i] = '\0'; // Lose the '\n' + stl->stats.header[80] = '\0'; + + num_facets = num_lines / ASCII_LINES_PER_FACET; + } + + stl->stats.number_of_facets += num_facets; + stl->stats.original_num_facets = stl->stats.number_of_facets; + return fp; } -void -stl_initialize(stl_file *stl) { - memset(stl, 0, sizeof(stl_file)); - stl->stats.volume = -1.0; +/* Reads the contents of the file pointed to by fp into the stl structure, + starting at facet first_facet. The second argument says if it's our first + time running this for the stl and therefore we should reset our max and min stats. */ +static bool stl_read(stl_file *stl, FILE *fp, int first_facet, bool first) +{ + if (stl->stats.type == binary) + fseek(fp, HEADER_SIZE, SEEK_SET); + else + rewind(fp); + + char normal_buf[3][32]; + for (uint32_t i = first_facet; i < stl->stats.number_of_facets; ++ i) { + stl_facet facet; + + if (stl->stats.type == binary) { + // Read a single facet from a binary .STL file. We assume little-endian architecture! + if (fread(&facet, 1, SIZEOF_STL_FACET, fp) != SIZEOF_STL_FACET) + return false; +#ifndef BOOST_LITTLE_ENDIAN + // Convert the loaded little endian data to big endian. + stl_internal_reverse_quads((char*)&facet, 48); +#endif /* BOOST_LITTLE_ENDIAN */ + } else { + // Read a single facet from an ASCII .STL file + // skip solid/endsolid + // (in this order, otherwise it won't work when they are paired in the middle of a file) + fscanf(fp, "endsolid%*[^\n]\n"); + fscanf(fp, "solid%*[^\n]\n"); // name might contain spaces so %*s doesn't work and it also can be empty (just "solid") + // Leading space in the fscanf format skips all leading white spaces including numerous new lines and tabs. + int res_normal = fscanf(fp, " facet normal %31s %31s %31s", normal_buf[0], normal_buf[1], normal_buf[2]); + assert(res_normal == 3); + int res_outer_loop = fscanf(fp, " outer loop"); + assert(res_outer_loop == 0); + int res_vertex1 = fscanf(fp, " vertex %f %f %f", &facet.vertex[0](0), &facet.vertex[0](1), &facet.vertex[0](2)); + assert(res_vertex1 == 3); + int res_vertex2 = fscanf(fp, " vertex %f %f %f", &facet.vertex[1](0), &facet.vertex[1](1), &facet.vertex[1](2)); + assert(res_vertex2 == 3); + int res_vertex3 = fscanf(fp, " vertex %f %f %f", &facet.vertex[2](0), &facet.vertex[2](1), &facet.vertex[2](2)); + assert(res_vertex3 == 3); + int res_endloop = fscanf(fp, " endloop"); + assert(res_endloop == 0); + // There is a leading and trailing white space around endfacet to eat up all leading and trailing white spaces including numerous tabs and new lines. + int res_endfacet = fscanf(fp, " endfacet "); + if (res_normal != 3 || res_outer_loop != 0 || res_vertex1 != 3 || res_vertex2 != 3 || res_vertex3 != 3 || res_endloop != 0 || res_endfacet != 0) { + BOOST_LOG_TRIVIAL(error) << "Something is syntactically very wrong with this ASCII STL! "; + return false; + } + + // The facet normal has been parsed as a single string as to workaround for not a numbers in the normal definition. + if (sscanf(normal_buf[0], "%f", &facet.normal(0)) != 1 || + sscanf(normal_buf[1], "%f", &facet.normal(1)) != 1 || + sscanf(normal_buf[2], "%f", &facet.normal(2)) != 1) { + // Normal was mangled. Maybe denormals or "not a number" were stored? + // Just reset the normal and silently ignore it. + memset(&facet.normal, 0, sizeof(facet.normal)); + } + } + +#if 0 + // Report close to zero vertex coordinates. Due to the nature of the floating point numbers, + // close to zero values may be represented with singificantly higher precision than the rest of the vertices. + // It may be worth to round these numbers to zero during loading to reduce the number of errors reported + // during the STL import. + for (size_t j = 0; j < 3; ++ j) { + if (facet.vertex[j](0) > -1e-12f && facet.vertex[j](0) < 1e-12f) + printf("stl_read: facet %d(0) = %e\r\n", j, facet.vertex[j](0)); + if (facet.vertex[j](1) > -1e-12f && facet.vertex[j](1) < 1e-12f) + printf("stl_read: facet %d(1) = %e\r\n", j, facet.vertex[j](1)); + if (facet.vertex[j](2) > -1e-12f && facet.vertex[j](2) < 1e-12f) + printf("stl_read: facet %d(2) = %e\r\n", j, facet.vertex[j](2)); + } +#endif + + // Write the facet into memory. + stl->facet_start[i] = facet; + stl_facet_stats(stl, facet, first); + } + + stl->stats.size = stl->stats.max - stl->stats.min; + stl->stats.bounding_diameter = stl->stats.size.norm(); + return true; +} + +bool stl_open(stl_file *stl, const char *file) +{ + stl->clear(); + FILE *fp = stl_open_count_facets(stl, file); + if (fp == nullptr) + return false; + stl_allocate(stl); + bool result = stl_read(stl, fp, 0, true); + fclose(fp); + return result; } #ifndef BOOST_LITTLE_ENDIAN extern void stl_internal_reverse_quads(char *buf, size_t cnt); #endif /* BOOST_LITTLE_ENDIAN */ -void -stl_count_facets(stl_file *stl, const char *file) { - long file_size; - uint32_t header_num_facets; - uint32_t num_facets; - int i; - size_t s; - unsigned char chtest[128]; - int num_lines = 1; - char *error_msg; - - if (stl->error) return; - - /* Open the file in binary mode first */ - stl->fp = boost::nowide::fopen(file, "rb"); - if(stl->fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_initialize: Couldn't open %s for reading", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - /* Find size of file */ - fseek(stl->fp, 0, SEEK_END); - file_size = ftell(stl->fp); - - /* Check for binary or ASCII file */ - fseek(stl->fp, HEADER_SIZE, SEEK_SET); - if (!fread(chtest, sizeof(chtest), 1, stl->fp)) { - perror("The input is an empty file"); - stl->error = 1; - return; - } - stl->stats.type = ascii; - for(s = 0; s < sizeof(chtest); s++) { - if(chtest[s] > 127) { - stl->stats.type = binary; - break; - } - } - rewind(stl->fp); - - /* Get the header and the number of facets in the .STL file */ - /* If the .STL file is binary, then do the following */ - if(stl->stats.type == binary) { - /* Test if the STL file has the right size */ - if(((file_size - HEADER_SIZE) % SIZEOF_STL_FACET != 0) - || (file_size < STL_MIN_FILE_SIZE)) { - fprintf(stderr, "The file %s has the wrong size.\n", file); - stl->error = 1; - return; - } - num_facets = (file_size - HEADER_SIZE) / SIZEOF_STL_FACET; - - /* Read the header */ - if (fread(stl->stats.header, LABEL_SIZE, 1, stl->fp) > 79) { - stl->stats.header[80] = '\0'; - } - - /* Read the int following the header. This should contain # of facets */ - bool header_num_faces_read = fread(&header_num_facets, sizeof(uint32_t), 1, stl->fp) != 0; -#ifndef BOOST_LITTLE_ENDIAN - // Convert from little endian to big endian. - stl_internal_reverse_quads((char*)&header_num_facets, 4); -#endif /* BOOST_LITTLE_ENDIAN */ - if (! header_num_faces_read || num_facets != header_num_facets) { - fprintf(stderr, - "Warning: File size doesn't match number of facets in the header\n"); - } - } - /* Otherwise, if the .STL file is ASCII, then do the following */ - else { - /* Reopen the file in text mode (for getting correct newlines on Windows) */ - // fix to silence a warning about unused return value. - // obviously if it fails we have problems.... - stl->fp = boost::nowide::freopen(file, "r", stl->fp); - - // do another null check to be safe - if(stl->fp == NULL) { - error_msg = (char*) - malloc(81 + strlen(file)); /* Allow 80 chars+file size for message */ - sprintf(error_msg, "stl_initialize: Couldn't open %s for reading", - file); - perror(error_msg); - free(error_msg); - stl->error = 1; - return; - } - - /* Find the number of facets */ - char linebuf[100]; - while (fgets(linebuf, 100, stl->fp) != NULL) { - /* don't count short lines */ - if (strlen(linebuf) <= 4) continue; - - /* skip solid/endsolid lines as broken STL file generators may put several of them */ - if (strncmp(linebuf, "solid", 5) == 0 || strncmp(linebuf, "endsolid", 8) == 0) continue; - - ++num_lines; - } - - rewind(stl->fp); - - /* Get the header */ - for(i = 0; - (i < 80) && (stl->stats.header[i] = getc(stl->fp)) != '\n'; i++); - stl->stats.header[i] = '\0'; /* Lose the '\n' */ - stl->stats.header[80] = '\0'; - - num_facets = num_lines / ASCII_LINES_PER_FACET; - } - stl->stats.number_of_facets += num_facets; - stl->stats.original_num_facets = stl->stats.number_of_facets; +void stl_allocate(stl_file *stl) +{ + // Allocate memory for the entire .STL file. + stl->facet_start.assign(stl->stats.number_of_facets, stl_facet()); + // Allocate memory for the neighbors list. + stl->neighbors_start.assign(stl->stats.number_of_facets, stl_neighbors()); } -void -stl_allocate(stl_file *stl) { - if (stl->error) return; - - /* Allocate memory for the entire .STL file */ - stl->facet_start = (stl_facet*)calloc(stl->stats.number_of_facets, - sizeof(stl_facet)); - if(stl->facet_start == NULL) perror("stl_initialize"); - stl->stats.facets_malloced = stl->stats.number_of_facets; - - /* Allocate memory for the neighbors list */ - stl->neighbors_start = (stl_neighbors*) - calloc(stl->stats.number_of_facets, sizeof(stl_neighbors)); - if(stl->facet_start == NULL) perror("stl_initialize"); -} - -void -stl_open_merge(stl_file *stl, char *file_to_merge) { - int num_facets_so_far; - stl_type origStlType; - FILE *origFp; - stl_file stl_to_merge; - - if (stl->error) return; - - /* Record how many facets we have so far from the first file. We will start putting - facets in the next position. Since we're 0-indexed, it'l be the same position. */ - num_facets_so_far = stl->stats.number_of_facets; - - /* Record the file type we started with: */ - origStlType=stl->stats.type; - /* Record the file pointer too: */ - origFp=stl->fp; - - /* Initialize the sturucture with zero stats, header info and sizes: */ - stl_initialize(&stl_to_merge); - stl_count_facets(&stl_to_merge, file_to_merge); - - /* Copy what we need to into stl so that we can read the file_to_merge directly into it - using stl_read: Save the rest of the valuable info: */ - stl->stats.type=stl_to_merge.stats.type; - stl->fp=stl_to_merge.fp; - - /* Add the number of facets we already have in stl with what we we found in stl_to_merge but - haven't read yet. */ - stl->stats.number_of_facets=num_facets_so_far+stl_to_merge.stats.number_of_facets; - - /* Allocate enough room for stl->stats.number_of_facets facets and neighbors: */ - stl_reallocate(stl); - - /* Read the file to merge directly into stl, adding it to what we have already. - Start at num_facets_so_far, the index to the first unused facet. Also say - that this isn't our first time so we should augment stats like min and max - instead of erasing them. */ - stl_read(stl, num_facets_so_far, false); - - /* Restore the stl information we overwrote (for stl_read) so that it still accurately - reflects the subject part: */ - stl->stats.type=origStlType; - stl->fp=origFp; -} - -extern void -stl_reallocate(stl_file *stl) { - if (stl->error) return; - /* Reallocate more memory for the .STL file(s) */ - stl->facet_start = (stl_facet*)realloc(stl->facet_start, stl->stats.number_of_facets * - sizeof(stl_facet)); - if(stl->facet_start == NULL) perror("stl_initialize"); - stl->stats.facets_malloced = stl->stats.number_of_facets; - - /* Reallocate more memory for the neighbors list */ - stl->neighbors_start = (stl_neighbors*) - realloc(stl->neighbors_start, stl->stats.number_of_facets * - sizeof(stl_neighbors)); - if(stl->facet_start == NULL) perror("stl_initialize"); -} - - -/* Reads the contents of the file pointed to by stl->fp into the stl structure, - starting at facet first_facet. The second argument says if it's our first - time running this for the stl and therefore we should reset our max and min stats. */ -void stl_read(stl_file *stl, int first_facet, bool first) { - stl_facet facet; - - if (stl->error) return; - - if(stl->stats.type == binary) { - fseek(stl->fp, HEADER_SIZE, SEEK_SET); - } else { - rewind(stl->fp); - } - - char normal_buf[3][32]; - for(uint32_t i = first_facet; i < stl->stats.number_of_facets; i++) { - if(stl->stats.type == binary) - /* Read a single facet from a binary .STL file */ - { - /* we assume little-endian architecture! */ - if (fread(&facet, 1, SIZEOF_STL_FACET, stl->fp) != SIZEOF_STL_FACET) { - stl->error = 1; - return; - } -#ifndef BOOST_LITTLE_ENDIAN - // Convert the loaded little endian data to big endian. - stl_internal_reverse_quads((char*)&facet, 48); -#endif /* BOOST_LITTLE_ENDIAN */ - } else - /* Read a single facet from an ASCII .STL file */ - { - // skip solid/endsolid - // (in this order, otherwise it won't work when they are paired in the middle of a file) - fscanf(stl->fp, "endsolid%*[^\n]\n"); - fscanf(stl->fp, "solid%*[^\n]\n"); // name might contain spaces so %*s doesn't work and it also can be empty (just "solid") - // Leading space in the fscanf format skips all leading white spaces including numerous new lines and tabs. - int res_normal = fscanf(stl->fp, " facet normal %31s %31s %31s", normal_buf[0], normal_buf[1], normal_buf[2]); - assert(res_normal == 3); - int res_outer_loop = fscanf(stl->fp, " outer loop"); - assert(res_outer_loop == 0); - int res_vertex1 = fscanf(stl->fp, " vertex %f %f %f", &facet.vertex[0](0), &facet.vertex[0](1), &facet.vertex[0](2)); - assert(res_vertex1 == 3); - int res_vertex2 = fscanf(stl->fp, " vertex %f %f %f", &facet.vertex[1](0), &facet.vertex[1](1), &facet.vertex[1](2)); - assert(res_vertex2 == 3); - int res_vertex3 = fscanf(stl->fp, " vertex %f %f %f", &facet.vertex[2](0), &facet.vertex[2](1), &facet.vertex[2](2)); - assert(res_vertex3 == 3); - int res_endloop = fscanf(stl->fp, " endloop"); - assert(res_endloop == 0); - // There is a leading and trailing white space around endfacet to eat up all leading and trailing white spaces including numerous tabs and new lines. - int res_endfacet = fscanf(stl->fp, " endfacet "); - if (res_normal != 3 || res_outer_loop != 0 || res_vertex1 != 3 || res_vertex2 != 3 || res_vertex3 != 3 || res_endloop != 0 || res_endfacet != 0) { - perror("Something is syntactically very wrong with this ASCII STL!"); - stl->error = 1; - return; - } - - // The facet normal has been parsed as a single string as to workaround for not a numbers in the normal definition. - if (sscanf(normal_buf[0], "%f", &facet.normal(0)) != 1 || - sscanf(normal_buf[1], "%f", &facet.normal(1)) != 1 || - sscanf(normal_buf[2], "%f", &facet.normal(2)) != 1) { - // Normal was mangled. Maybe denormals or "not a number" were stored? - // Just reset the normal and silently ignore it. - memset(&facet.normal, 0, sizeof(facet.normal)); - } - } - -#if 0 - // Report close to zero vertex coordinates. Due to the nature of the floating point numbers, - // close to zero values may be represented with singificantly higher precision than the rest of the vertices. - // It may be worth to round these numbers to zero during loading to reduce the number of errors reported - // during the STL import. - for (size_t j = 0; j < 3; ++ j) { - if (facet.vertex[j](0) > -1e-12f && facet.vertex[j](0) < 1e-12f) - printf("stl_read: facet %d(0) = %e\r\n", j, facet.vertex[j](0)); - if (facet.vertex[j](1) > -1e-12f && facet.vertex[j](1) < 1e-12f) - printf("stl_read: facet %d(1) = %e\r\n", j, facet.vertex[j](1)); - if (facet.vertex[j](2) > -1e-12f && facet.vertex[j](2) < 1e-12f) - printf("stl_read: facet %d(2) = %e\r\n", j, facet.vertex[j](2)); - } -#endif - - /* Write the facet into memory. */ - stl->facet_start[i] = facet; - stl_facet_stats(stl, facet, first); - } - stl->stats.size = stl->stats.max - stl->stats.min; - stl->stats.bounding_diameter = stl->stats.size.norm(); +void stl_reallocate(stl_file *stl) +{ + stl->facet_start.resize(stl->stats.number_of_facets); + stl->neighbors_start.resize(stl->stats.number_of_facets); } void stl_facet_stats(stl_file *stl, stl_facet facet, bool &first) { - if (stl->error) - return; + // While we are going through all of the facets, let's find the + // maximum and minimum values for x, y, and z - // While we are going through all of the facets, let's find the - // maximum and minimum values for x, y, and z + if (first) { + // Initialize the max and min values the first time through + stl->stats.min = facet.vertex[0]; + stl->stats.max = facet.vertex[0]; + stl_vertex diff = (facet.vertex[1] - facet.vertex[0]).cwiseAbs(); + stl->stats.shortest_edge = std::max(diff(0), std::max(diff(1), diff(2))); + first = false; + } - if (first) { - // Initialize the max and min values the first time through - stl->stats.min = facet.vertex[0]; - stl->stats.max = facet.vertex[0]; - stl_vertex diff = (facet.vertex[1] - facet.vertex[0]).cwiseAbs(); - stl->stats.shortest_edge = std::max(diff(0), std::max(diff(1), diff(2))); - first = false; - } - - // Now find the max and min values. - for (size_t i = 0; i < 3; ++ i) { - stl->stats.min = stl->stats.min.cwiseMin(facet.vertex[i]); - stl->stats.max = stl->stats.max.cwiseMax(facet.vertex[i]); - } -} - -void stl_close(stl_file *stl) -{ - assert(stl->fp == nullptr); - assert(stl->heads == nullptr); - assert(stl->tail == nullptr); - - if (stl->facet_start != NULL) - free(stl->facet_start); - if (stl->neighbors_start != NULL) - free(stl->neighbors_start); - if (stl->v_indices != NULL) - free(stl->v_indices); - if (stl->v_shared != NULL) - free(stl->v_shared); - memset(stl, 0, sizeof(stl_file)); + // Now find the max and min values. + for (size_t i = 0; i < 3; ++ i) { + stl->stats.min = stl->stats.min.cwiseMin(facet.vertex[i]); + stl->stats.max = stl->stats.max.cwiseMax(facet.vertex[i]); + } } diff --git a/src/admesh/util.cpp b/src/admesh/util.cpp index 305a58e22b..029e44a28e 100644 --- a/src/admesh/util.cpp +++ b/src/admesh/util.cpp @@ -25,435 +25,375 @@ #include #include +#include + #include "stl.h" -static void stl_rotate(float *x, float *y, const double c, const double s); -static float get_area(stl_facet *facet); -static float get_volume(stl_file *stl); +void stl_verify_neighbors(stl_file *stl) +{ + stl->stats.backwards_edges = 0; - -void -stl_verify_neighbors(stl_file *stl) { - int i; - int j; - stl_edge edge_a; - stl_edge edge_b; - int neighbor; - int vnot; - - if (stl->error) return; - - stl->stats.backwards_edges = 0; - - for(i = 0; i < stl->stats.number_of_facets; i++) { - for(j = 0; j < 3; j++) { - edge_a.p1 = stl->facet_start[i].vertex[j]; - edge_a.p2 = stl->facet_start[i].vertex[(j + 1) % 3]; - neighbor = stl->neighbors_start[i].neighbor[j]; - vnot = stl->neighbors_start[i].which_vertex_not[j]; - - if(neighbor == -1) - continue; /* this edge has no neighbor... Continue. */ - if(vnot < 3) { - edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3]; - edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3]; - } else { - stl->stats.backwards_edges += 1; - edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3]; - edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3]; - } - if (edge_a.p1 != edge_b.p1 || edge_a.p2 != edge_b.p2) { - /* These edges should match but they don't. Print results. */ - printf("edge %d of facet %d doesn't match edge %d of facet %d\n", - j, i, vnot + 1, neighbor); - stl_write_facet(stl, (char*)"first facet", i); - stl_write_facet(stl, (char*)"second facet", neighbor); - } - } - } + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + for (int j = 0; j < 3; ++ j) { + struct stl_edge { + stl_vertex p1; + stl_vertex p2; + int facet_number; + }; + stl_edge edge_a; + edge_a.p1 = stl->facet_start[i].vertex[j]; + edge_a.p2 = stl->facet_start[i].vertex[(j + 1) % 3]; + int neighbor = stl->neighbors_start[i].neighbor[j]; + if (neighbor == -1) + continue; // this edge has no neighbor... Continue. + int vnot = stl->neighbors_start[i].which_vertex_not[j]; + stl_edge edge_b; + if (vnot < 3) { + edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3]; + edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3]; + } else { + stl->stats.backwards_edges += 1; + edge_b.p1 = stl->facet_start[neighbor].vertex[(vnot + 1) % 3]; + edge_b.p2 = stl->facet_start[neighbor].vertex[(vnot + 2) % 3]; + } + if (edge_a.p1 != edge_b.p1 || edge_a.p2 != edge_b.p2) { + // These edges should match but they don't. Print results. + BOOST_LOG_TRIVIAL(info) << "edge " << j << " of facet " << i << " doesn't match edge " << (vnot + 1) << " of facet " << neighbor; + stl_write_facet(stl, (char*)"first facet", i); + stl_write_facet(stl, (char*)"second facet", neighbor); + } + } + } } void stl_translate(stl_file *stl, float x, float y, float z) { - if (stl->error) - return; - - stl_vertex new_min(x, y, z); - stl_vertex shift = new_min - stl->stats.min; - for (int i = 0; i < stl->stats.number_of_facets; ++ i) - for (int j = 0; j < 3; ++ j) - stl->facet_start[i].vertex[j] += shift; - stl->stats.min = new_min; - stl->stats.max += shift; - stl_invalidate_shared_vertices(stl); + stl_vertex new_min(x, y, z); + stl_vertex shift = new_min - stl->stats.min; + for (int i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + stl->facet_start[i].vertex[j] += shift; + stl->stats.min = new_min; + stl->stats.max += shift; } /* Translates the stl by x,y,z, relatively from wherever it is currently */ void stl_translate_relative(stl_file *stl, float x, float y, float z) { - if (stl->error) - return; - - stl_vertex shift(x, y, z); - for (int i = 0; i < stl->stats.number_of_facets; ++ i) - for (int j = 0; j < 3; ++ j) - stl->facet_start[i].vertex[j] += shift; - stl->stats.min += shift; - stl->stats.max += shift; - stl_invalidate_shared_vertices(stl); + stl_vertex shift(x, y, z); + for (int i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + stl->facet_start[i].vertex[j] += shift; + stl->stats.min += shift; + stl->stats.max += shift; } void stl_scale_versor(stl_file *stl, const stl_vertex &versor) { - if (stl->error) - return; - - // Scale extents. - auto s = versor.array(); - stl->stats.min.array() *= s; - stl->stats.max.array() *= s; - // Scale size. - stl->stats.size.array() *= s; - // Scale volume. - if (stl->stats.volume > 0.0) - stl->stats.volume *= versor(0) * versor(1) * versor(2); - // Scale the mesh. - for (int i = 0; i < stl->stats.number_of_facets; ++ i) - for (int j = 0; j < 3; ++ j) - stl->facet_start[i].vertex[j].array() *= s; - stl_invalidate_shared_vertices(stl); + // Scale extents. + auto s = versor.array(); + stl->stats.min.array() *= s; + stl->stats.max.array() *= s; + // Scale size. + stl->stats.size.array() *= s; + // Scale volume. + if (stl->stats.volume > 0.0) + stl->stats.volume *= versor(0) * versor(1) * versor(2); + // Scale the mesh. + for (int i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + stl->facet_start[i].vertex[j].array() *= s; } static void calculate_normals(stl_file *stl) { - if (stl->error) - return; - - stl_normal normal; - for(uint32_t i = 0; i < stl->stats.number_of_facets; i++) { - stl_calculate_normal(normal, &stl->facet_start[i]); - stl_normalize_vector(normal); - stl->facet_start[i].normal = normal; - } + stl_normal normal; + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + stl_calculate_normal(normal, &stl->facet_start[i]); + stl_normalize_vector(normal); + stl->facet_start[i].normal = normal; + } } -void -stl_rotate_x(stl_file *stl, float angle) { - int i; - int j; - double radian_angle = (angle / 180.0) * M_PI; - double c = cos(radian_angle); - double s = sin(radian_angle); - - if (stl->error) return; - - for(i = 0; i < stl->stats.number_of_facets; i++) { - for(j = 0; j < 3; j++) { - stl_rotate(&stl->facet_start[i].vertex[j](1), - &stl->facet_start[i].vertex[j](2), c, s); - } - } - stl_get_size(stl); - calculate_normals(stl); +static inline void rotate_point_2d(float &x, float &y, const double c, const double s) +{ + double xold = x; + double yold = y; + x = float(c * xold - s * yold); + y = float(s * xold + c * yold); } -void -stl_rotate_y(stl_file *stl, float angle) { - int i; - int j; - double radian_angle = (angle / 180.0) * M_PI; - double c = cos(radian_angle); - double s = sin(radian_angle); - - if (stl->error) return; - - for(i = 0; i < stl->stats.number_of_facets; i++) { - for(j = 0; j < 3; j++) { - stl_rotate(&stl->facet_start[i].vertex[j](2), - &stl->facet_start[i].vertex[j](0), c, s); - } - } - stl_get_size(stl); - calculate_normals(stl); +void stl_rotate_x(stl_file *stl, float angle) +{ + double radian_angle = (angle / 180.0) * M_PI; + double c = cos(radian_angle); + double s = sin(radian_angle); + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + rotate_point_2d(stl->facet_start[i].vertex[j](1), stl->facet_start[i].vertex[j](2), c, s); + stl_get_size(stl); + calculate_normals(stl); } -void -stl_rotate_z(stl_file *stl, float angle) { - int i; - int j; - double radian_angle = (angle / 180.0) * M_PI; - double c = cos(radian_angle); - double s = sin(radian_angle); - - if (stl->error) return; - - for(i = 0; i < stl->stats.number_of_facets; i++) { - for(j = 0; j < 3; j++) { - stl_rotate(&stl->facet_start[i].vertex[j](0), - &stl->facet_start[i].vertex[j](1), c, s); - } - } - stl_get_size(stl); - calculate_normals(stl); +void stl_rotate_y(stl_file *stl, float angle) +{ + double radian_angle = (angle / 180.0) * M_PI; + double c = cos(radian_angle); + double s = sin(radian_angle); + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + rotate_point_2d(stl->facet_start[i].vertex[j](2), stl->facet_start[i].vertex[j](0), c, s); + stl_get_size(stl); + calculate_normals(stl); } +void stl_rotate_z(stl_file *stl, float angle) +{ + double radian_angle = (angle / 180.0) * M_PI; + double c = cos(radian_angle); + double s = sin(radian_angle); + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + rotate_point_2d(stl->facet_start[i].vertex[j](0), stl->facet_start[i].vertex[j](1), c, s); + stl_get_size(stl); + calculate_normals(stl); +} +void its_rotate_x(indexed_triangle_set &its, float angle) +{ + double radian_angle = (angle / 180.0) * M_PI; + double c = cos(radian_angle); + double s = sin(radian_angle); + for (stl_vertex &v : its.vertices) + rotate_point_2d(v(1), v(2), c, s); +} -static void -stl_rotate(float *x, float *y, const double c, const double s) { - double xold = *x; - double yold = *y; - *x = float(c * xold - s * yold); - *y = float(s * xold + c * yold); +void its_rotate_y(indexed_triangle_set& its, float angle) +{ + double radian_angle = (angle / 180.0) * M_PI; + double c = cos(radian_angle); + double s = sin(radian_angle); + for (stl_vertex& v : its.vertices) + rotate_point_2d(v(2), v(0), c, s); +} + +void its_rotate_z(indexed_triangle_set& its, float angle) +{ + double radian_angle = (angle / 180.0) * M_PI; + double c = cos(radian_angle); + double s = sin(radian_angle); + for (stl_vertex& v : its.vertices) + rotate_point_2d(v(0), v(1), c, s); } void stl_get_size(stl_file *stl) { - if (stl->error || stl->stats.number_of_facets == 0) - return; - stl->stats.min = stl->facet_start[0].vertex[0]; - stl->stats.max = stl->stats.min; - for (int i = 0; i < stl->stats.number_of_facets; ++ i) { - const stl_facet &face = stl->facet_start[i]; - for (int j = 0; j < 3; ++ j) { - stl->stats.min = stl->stats.min.cwiseMin(face.vertex[j]); - stl->stats.max = stl->stats.max.cwiseMax(face.vertex[j]); - } - } - stl->stats.size = stl->stats.max - stl->stats.min; - stl->stats.bounding_diameter = stl->stats.size.norm(); + if (stl->stats.number_of_facets == 0) + return; + stl->stats.min = stl->facet_start[0].vertex[0]; + stl->stats.max = stl->stats.min; + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + const stl_facet &face = stl->facet_start[i]; + for (int j = 0; j < 3; ++ j) { + stl->stats.min = stl->stats.min.cwiseMin(face.vertex[j]); + stl->stats.max = stl->stats.max.cwiseMax(face.vertex[j]); + } + } + stl->stats.size = stl->stats.max - stl->stats.min; + stl->stats.bounding_diameter = stl->stats.size.norm(); } void stl_mirror_xy(stl_file *stl) { - if (stl->error) - return; - - for(int i = 0; i < stl->stats.number_of_facets; i++) { - for(int j = 0; j < 3; j++) { - stl->facet_start[i].vertex[j](2) *= -1.0; - } - } - float temp_size = stl->stats.min(2); - stl->stats.min(2) = stl->stats.max(2); - stl->stats.max(2) = temp_size; - stl->stats.min(2) *= -1.0; - stl->stats.max(2) *= -1.0; - stl_reverse_all_facets(stl); - stl->stats.facets_reversed -= stl->stats.number_of_facets; /* for not altering stats */ + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + stl->facet_start[i].vertex[j](2) *= -1.0; + float temp_size = stl->stats.min(2); + stl->stats.min(2) = stl->stats.max(2); + stl->stats.max(2) = temp_size; + stl->stats.min(2) *= -1.0; + stl->stats.max(2) *= -1.0; + stl_reverse_all_facets(stl); + stl->stats.facets_reversed -= stl->stats.number_of_facets; /* for not altering stats */ } void stl_mirror_yz(stl_file *stl) { - if (stl->error) return; - - for (int i = 0; i < stl->stats.number_of_facets; i++) { - for (int j = 0; j < 3; j++) { - stl->facet_start[i].vertex[j](0) *= -1.0; - } - } - float temp_size = stl->stats.min(0); - stl->stats.min(0) = stl->stats.max(0); - stl->stats.max(0) = temp_size; - stl->stats.min(0) *= -1.0; - stl->stats.max(0) *= -1.0; - stl_reverse_all_facets(stl); - stl->stats.facets_reversed -= stl->stats.number_of_facets; /* for not altering stats */ + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; j++) + stl->facet_start[i].vertex[j](0) *= -1.0; + float temp_size = stl->stats.min(0); + stl->stats.min(0) = stl->stats.max(0); + stl->stats.max(0) = temp_size; + stl->stats.min(0) *= -1.0; + stl->stats.max(0) *= -1.0; + stl_reverse_all_facets(stl); + stl->stats.facets_reversed -= stl->stats.number_of_facets; /* for not altering stats */ } void stl_mirror_xz(stl_file *stl) { - if (stl->error) - return; - - for (int i = 0; i < stl->stats.number_of_facets; i++) { - for (int j = 0; j < 3; j++) { - stl->facet_start[i].vertex[j](1) *= -1.0; - } - } - float temp_size = stl->stats.min(1); - stl->stats.min(1) = stl->stats.max(1); - stl->stats.max(1) = temp_size; - stl->stats.min(1) *= -1.0; - stl->stats.max(1) *= -1.0; - stl_reverse_all_facets(stl); - stl->stats.facets_reversed -= stl->stats.number_of_facets; /* for not altering stats */ -} - -static float get_volume(stl_file *stl) -{ - if (stl->error) - return 0; - - // Choose a point, any point as the reference. - stl_vertex p0 = stl->facet_start[0].vertex[0]; - float volume = 0.f; - for(uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { - // Do dot product to get distance from point to plane. - float height = stl->facet_start[i].normal.dot(stl->facet_start[i].vertex[0] - p0); - float area = get_area(&stl->facet_start[i]); - volume += (area * height) / 3.0f; - } - return volume; -} - -void stl_calculate_volume(stl_file *stl) -{ - if (stl->error) return; - stl->stats.volume = get_volume(stl); - if(stl->stats.volume < 0.0) { - stl_reverse_all_facets(stl); - stl->stats.volume = -stl->stats.volume; - } + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) + for (int j = 0; j < 3; ++ j) + stl->facet_start[i].vertex[j](1) *= -1.0; + float temp_size = stl->stats.min(1); + stl->stats.min(1) = stl->stats.max(1); + stl->stats.max(1) = temp_size; + stl->stats.min(1) *= -1.0; + stl->stats.max(1) *= -1.0; + stl_reverse_all_facets(stl); + stl->stats.facets_reversed -= stl->stats.number_of_facets; // for not altering stats } static float get_area(stl_facet *facet) { - /* cast to double before calculating cross product because large coordinates - can result in overflowing product - (bad area is responsible for bad volume and bad facets reversal) */ - double cross[3][3]; - for (int i = 0; i < 3; i++) { - cross[i][0]=(((double)facet->vertex[i](1) * (double)facet->vertex[(i + 1) % 3](2)) - - ((double)facet->vertex[i](2) * (double)facet->vertex[(i + 1) % 3](1))); - cross[i][1]=(((double)facet->vertex[i](2) * (double)facet->vertex[(i + 1) % 3](0)) - - ((double)facet->vertex[i](0) * (double)facet->vertex[(i + 1) % 3](2))); - cross[i][2]=(((double)facet->vertex[i](0) * (double)facet->vertex[(i + 1) % 3](1)) - - ((double)facet->vertex[i](1) * (double)facet->vertex[(i + 1) % 3](0))); - } + /* cast to double before calculating cross product because large coordinates + can result in overflowing product + (bad area is responsible for bad volume and bad facets reversal) */ + double cross[3][3]; + for (int i = 0; i < 3; i++) { + cross[i][0]=(((double)facet->vertex[i](1) * (double)facet->vertex[(i + 1) % 3](2)) - + ((double)facet->vertex[i](2) * (double)facet->vertex[(i + 1) % 3](1))); + cross[i][1]=(((double)facet->vertex[i](2) * (double)facet->vertex[(i + 1) % 3](0)) - + ((double)facet->vertex[i](0) * (double)facet->vertex[(i + 1) % 3](2))); + cross[i][2]=(((double)facet->vertex[i](0) * (double)facet->vertex[(i + 1) % 3](1)) - + ((double)facet->vertex[i](1) * (double)facet->vertex[(i + 1) % 3](0))); + } - stl_normal sum; - sum(0) = cross[0][0] + cross[1][0] + cross[2][0]; - sum(1) = cross[0][1] + cross[1][1] + cross[2][1]; - sum(2) = cross[0][2] + cross[1][2] + cross[2][2]; + stl_normal sum; + sum(0) = cross[0][0] + cross[1][0] + cross[2][0]; + sum(1) = cross[0][1] + cross[1][1] + cross[2][1]; + sum(2) = cross[0][2] + cross[1][2] + cross[2][2]; - // This should already be done. But just in case, let's do it again. - //FIXME this is questionable. the "sum" normal should be accurate, while the normal "n" may be calculated with a low accuracy. - stl_normal n; - stl_calculate_normal(n, facet); - stl_normalize_vector(n); - return 0.5f * n.dot(sum); + // This should already be done. But just in case, let's do it again. + //FIXME this is questionable. the "sum" normal should be accurate, while the normal "n" may be calculated with a low accuracy. + stl_normal n; + stl_calculate_normal(n, facet); + stl_normalize_vector(n); + return 0.5f * n.dot(sum); } -void stl_repair(stl_file *stl, - int fixall_flag, - int exact_flag, - int tolerance_flag, - float tolerance, - int increment_flag, - float increment, - int nearby_flag, - int iterations, - int remove_unconnected_flag, - int fill_holes_flag, - int normal_directions_flag, - int normal_values_flag, - int reverse_all_flag, - int verbose_flag) { - - int i; - int last_edges_fixed = 0; - - if (stl->error) return; - - if(exact_flag || fixall_flag || nearby_flag || remove_unconnected_flag - || fill_holes_flag || normal_directions_flag) { - if (verbose_flag) - printf("Checking exact...\n"); - exact_flag = 1; - stl_check_facets_exact(stl); - stl->stats.facets_w_1_bad_edge = - (stl->stats.connected_facets_2_edge - - stl->stats.connected_facets_3_edge); - stl->stats.facets_w_2_bad_edge = - (stl->stats.connected_facets_1_edge - - stl->stats.connected_facets_2_edge); - stl->stats.facets_w_3_bad_edge = - (stl->stats.number_of_facets - - stl->stats.connected_facets_1_edge); - } - - if(nearby_flag || fixall_flag) { - if(!tolerance_flag) { - tolerance = stl->stats.shortest_edge; - } - if(!increment_flag) { - increment = stl->stats.bounding_diameter / 10000.0; - } - - if(stl->stats.connected_facets_3_edge < stl->stats.number_of_facets) { - for(i = 0; i < iterations; i++) { - if(stl->stats.connected_facets_3_edge < - stl->stats.number_of_facets) { - if (verbose_flag) - printf("\ -Checking nearby. Tolerance= %f Iteration=%d of %d...", - tolerance, i + 1, iterations); - stl_check_facets_nearby(stl, tolerance); - if (verbose_flag) - printf(" Fixed %d edges.\n", - stl->stats.edges_fixed - last_edges_fixed); - last_edges_fixed = stl->stats.edges_fixed; - tolerance += increment; - } else { - if (verbose_flag) - printf("\ -All facets connected. No further nearby check necessary.\n"); - break; - } - } - } else { - if (verbose_flag) - printf("All facets connected. No nearby check necessary.\n"); - } - } - - if(remove_unconnected_flag || fixall_flag || fill_holes_flag) { - if(stl->stats.connected_facets_3_edge < stl->stats.number_of_facets) { - if (verbose_flag) - printf("Removing unconnected facets...\n"); - stl_remove_unconnected_facets(stl); - } else - if (verbose_flag) - printf("No unconnected need to be removed.\n"); - } - - if(fill_holes_flag || fixall_flag) { - if(stl->stats.connected_facets_3_edge < stl->stats.number_of_facets) { - if (verbose_flag) - printf("Filling holes...\n"); - stl_fill_holes(stl); - } else - if (verbose_flag) - printf("No holes need to be filled.\n"); - } - - if(reverse_all_flag) { - if (verbose_flag) - printf("Reversing all facets...\n"); - stl_reverse_all_facets(stl); - } - - if(normal_directions_flag || fixall_flag) { - if (verbose_flag) - printf("Checking normal directions...\n"); - stl_fix_normal_directions(stl); - } - - if(normal_values_flag || fixall_flag) { - if (verbose_flag) - printf("Checking normal values...\n"); - stl_fix_normal_values(stl); - } - - /* Always calculate the volume. It shouldn't take too long */ - if (verbose_flag) - printf("Calculating volume...\n"); - stl_calculate_volume(stl); - - if(exact_flag) { - if (verbose_flag) - printf("Verifying neighbors...\n"); - stl_verify_neighbors(stl); - } +static float get_volume(stl_file *stl) +{ + // Choose a point, any point as the reference. + stl_vertex p0 = stl->facet_start[0].vertex[0]; + float volume = 0.f; + for (uint32_t i = 0; i < stl->stats.number_of_facets; ++ i) { + // Do dot product to get distance from point to plane. + float height = stl->facet_start[i].normal.dot(stl->facet_start[i].vertex[0] - p0); + float area = get_area(&stl->facet_start[i]); + volume += (area * height) / 3.0f; + } + return volume; +} + +void stl_calculate_volume(stl_file *stl) +{ + stl->stats.volume = get_volume(stl); + if (stl->stats.volume < 0.0) { + stl_reverse_all_facets(stl); + stl->stats.volume = -stl->stats.volume; + } +} + +void stl_repair( + stl_file *stl, + bool fixall_flag, + bool exact_flag, + bool tolerance_flag, + float tolerance, + bool increment_flag, + float increment, + bool nearby_flag, + int iterations, + bool remove_unconnected_flag, + bool fill_holes_flag, + bool normal_directions_flag, + bool normal_values_flag, + bool reverse_all_flag, + bool verbose_flag) +{ + if (exact_flag || fixall_flag || nearby_flag || remove_unconnected_flag || fill_holes_flag || normal_directions_flag) { + if (verbose_flag) + printf("Checking exact...\n"); + exact_flag = true; + stl_check_facets_exact(stl); + stl->stats.facets_w_1_bad_edge = (stl->stats.connected_facets_2_edge - stl->stats.connected_facets_3_edge); + stl->stats.facets_w_2_bad_edge = (stl->stats.connected_facets_1_edge - stl->stats.connected_facets_2_edge); + stl->stats.facets_w_3_bad_edge = (stl->stats.number_of_facets - stl->stats.connected_facets_1_edge); + } + + if (nearby_flag || fixall_flag) { + if (! tolerance_flag) + tolerance = stl->stats.shortest_edge; + if (! increment_flag) + increment = stl->stats.bounding_diameter / 10000.0; + } + + if (stl->stats.connected_facets_3_edge < stl->stats.number_of_facets) { + int last_edges_fixed = 0; + for (int i = 0; i < iterations; ++ i) { + if (stl->stats.connected_facets_3_edge < stl->stats.number_of_facets) { + if (verbose_flag) + printf("Checking nearby. Tolerance= %f Iteration=%d of %d...", tolerance, i + 1, iterations); + stl_check_facets_nearby(stl, tolerance); + if (verbose_flag) + printf(" Fixed %d edges.\n", stl->stats.edges_fixed - last_edges_fixed); + last_edges_fixed = stl->stats.edges_fixed; + tolerance += increment; + } else { + if (verbose_flag) + printf("All facets connected. No further nearby check necessary.\n"); + break; + } + } + } else if (verbose_flag) + printf("All facets connected. No nearby check necessary.\n"); + + if (remove_unconnected_flag || fixall_flag || fill_holes_flag) { + if (stl->stats.connected_facets_3_edge < stl->stats.number_of_facets) { + if (verbose_flag) + printf("Removing unconnected facets...\n"); + stl_remove_unconnected_facets(stl); + } else if (verbose_flag) + printf("No unconnected need to be removed.\n"); + } + + if (fill_holes_flag || fixall_flag) { + if (stl->stats.connected_facets_3_edge < stl->stats.number_of_facets) { + if (verbose_flag) + printf("Filling holes...\n"); + stl_fill_holes(stl); + } else if (verbose_flag) + printf("No holes need to be filled.\n"); + } + + if (reverse_all_flag) { + if (verbose_flag) + printf("Reversing all facets...\n"); + stl_reverse_all_facets(stl); + } + + if (normal_directions_flag || fixall_flag) { + if (verbose_flag) + printf("Checking normal directions...\n"); + stl_fix_normal_directions(stl); + } + + if (normal_values_flag || fixall_flag) { + if (verbose_flag) + printf("Checking normal values...\n"); + stl_fix_normal_values(stl); + } + + // Always calculate the volume. It shouldn't take too long. + if (verbose_flag) + printf("Calculating volume...\n"); + stl_calculate_volume(stl); + + if (exact_flag) { + if (verbose_flag) + printf("Verifying neighbors...\n"); + stl_verify_neighbors(stl); + } } diff --git a/src/boost/CMakeLists.txt b/src/boost/CMakeLists.txt index aae87340b3..12fe6b4e52 100644 --- a/src/boost/CMakeLists.txt +++ b/src/boost/CMakeLists.txt @@ -19,6 +19,6 @@ add_library(nowide STATIC nowide/windows.hpp ) -target_include_directories(nowide SYSTEM PUBLIC ${Boost_INCLUDE_DIRS}) +target_link_libraries(nowide PUBLIC boost_headeronly) diff --git a/src/libigl/CMakeLists.txt b/src/libigl/CMakeLists.txt new file mode 100644 index 0000000000..0852fad729 --- /dev/null +++ b/src/libigl/CMakeLists.txt @@ -0,0 +1,14 @@ +project(libigl) +cmake_minimum_required(VERSION 3.0) + +add_library(libigl INTERFACE) + +find_package(libigl QUIET) + +if(libigl_FOUND) + message(STATUS "IGL found, using system version...") + target_link_libraries(libigl INTERFACE igl::core) +else() + message(STATUS "IGL NOT found, using bundled version...") + target_include_directories(libigl INTERFACE SYSTEM ${LIBDIR}/libigl) +endif() diff --git a/src/igl/AABB.cpp b/src/libigl/igl/AABB.cpp similarity index 100% rename from src/igl/AABB.cpp rename to src/libigl/igl/AABB.cpp diff --git a/src/igl/AABB.h b/src/libigl/igl/AABB.h similarity index 100% rename from src/igl/AABB.h rename to src/libigl/igl/AABB.h diff --git a/src/igl/ARAPEnergyType.h b/src/libigl/igl/ARAPEnergyType.h similarity index 100% rename from src/igl/ARAPEnergyType.h rename to src/libigl/igl/ARAPEnergyType.h diff --git a/src/igl/AtA_cached.cpp b/src/libigl/igl/AtA_cached.cpp similarity index 100% rename from src/igl/AtA_cached.cpp rename to src/libigl/igl/AtA_cached.cpp diff --git a/src/igl/AtA_cached.h b/src/libigl/igl/AtA_cached.h similarity index 100% rename from src/igl/AtA_cached.h rename to src/libigl/igl/AtA_cached.h diff --git a/src/igl/C_STR.h b/src/libigl/igl/C_STR.h similarity index 100% rename from src/igl/C_STR.h rename to src/libigl/igl/C_STR.h diff --git a/src/igl/Camera.h b/src/libigl/igl/Camera.h similarity index 100% rename from src/igl/Camera.h rename to src/libigl/igl/Camera.h diff --git a/src/igl/EPS.cpp b/src/libigl/igl/EPS.cpp similarity index 100% rename from src/igl/EPS.cpp rename to src/libigl/igl/EPS.cpp diff --git a/src/igl/EPS.h b/src/libigl/igl/EPS.h similarity index 100% rename from src/igl/EPS.h rename to src/libigl/igl/EPS.h diff --git a/src/igl/HalfEdgeIterator.cpp b/src/libigl/igl/HalfEdgeIterator.cpp similarity index 100% rename from src/igl/HalfEdgeIterator.cpp rename to src/libigl/igl/HalfEdgeIterator.cpp diff --git a/src/igl/HalfEdgeIterator.h b/src/libigl/igl/HalfEdgeIterator.h similarity index 100% rename from src/igl/HalfEdgeIterator.h rename to src/libigl/igl/HalfEdgeIterator.h diff --git a/src/igl/Hit.h b/src/libigl/igl/Hit.h similarity index 100% rename from src/igl/Hit.h rename to src/libigl/igl/Hit.h diff --git a/src/igl/IO b/src/libigl/igl/IO similarity index 100% rename from src/igl/IO rename to src/libigl/igl/IO diff --git a/src/igl/IndexComparison.h b/src/libigl/igl/IndexComparison.h similarity index 100% rename from src/igl/IndexComparison.h rename to src/libigl/igl/IndexComparison.h diff --git a/src/igl/LinSpaced.h b/src/libigl/igl/LinSpaced.h similarity index 100% rename from src/igl/LinSpaced.h rename to src/libigl/igl/LinSpaced.h diff --git a/src/igl/MeshBooleanType.h b/src/libigl/igl/MeshBooleanType.h similarity index 100% rename from src/igl/MeshBooleanType.h rename to src/libigl/igl/MeshBooleanType.h diff --git a/src/igl/NormalType.h b/src/libigl/igl/NormalType.h similarity index 100% rename from src/igl/NormalType.h rename to src/libigl/igl/NormalType.h diff --git a/src/igl/ONE.h b/src/libigl/igl/ONE.h similarity index 100% rename from src/igl/ONE.h rename to src/libigl/igl/ONE.h diff --git a/src/igl/PI.h b/src/libigl/igl/PI.h similarity index 100% rename from src/igl/PI.h rename to src/libigl/igl/PI.h diff --git a/src/igl/REDRUM.h b/src/libigl/igl/REDRUM.h similarity index 100% rename from src/igl/REDRUM.h rename to src/libigl/igl/REDRUM.h diff --git a/src/igl/STR.h b/src/libigl/igl/STR.h similarity index 100% rename from src/igl/STR.h rename to src/libigl/igl/STR.h diff --git a/src/igl/Singular_Value_Decomposition_Givens_QR_Factorization_Kernel.hpp b/src/libigl/igl/Singular_Value_Decomposition_Givens_QR_Factorization_Kernel.hpp similarity index 100% rename from src/igl/Singular_Value_Decomposition_Givens_QR_Factorization_Kernel.hpp rename to src/libigl/igl/Singular_Value_Decomposition_Givens_QR_Factorization_Kernel.hpp diff --git a/src/igl/Singular_Value_Decomposition_Jacobi_Conjugation_Kernel.hpp b/src/libigl/igl/Singular_Value_Decomposition_Jacobi_Conjugation_Kernel.hpp similarity index 100% rename from src/igl/Singular_Value_Decomposition_Jacobi_Conjugation_Kernel.hpp rename to src/libigl/igl/Singular_Value_Decomposition_Jacobi_Conjugation_Kernel.hpp diff --git a/src/igl/Singular_Value_Decomposition_Kernel_Declarations.hpp b/src/libigl/igl/Singular_Value_Decomposition_Kernel_Declarations.hpp similarity index 100% rename from src/igl/Singular_Value_Decomposition_Kernel_Declarations.hpp rename to src/libigl/igl/Singular_Value_Decomposition_Kernel_Declarations.hpp diff --git a/src/igl/Singular_Value_Decomposition_Main_Kernel_Body.hpp b/src/libigl/igl/Singular_Value_Decomposition_Main_Kernel_Body.hpp similarity index 100% rename from src/igl/Singular_Value_Decomposition_Main_Kernel_Body.hpp rename to src/libigl/igl/Singular_Value_Decomposition_Main_Kernel_Body.hpp diff --git a/src/igl/Singular_Value_Decomposition_Preamble.hpp b/src/libigl/igl/Singular_Value_Decomposition_Preamble.hpp similarity index 100% rename from src/igl/Singular_Value_Decomposition_Preamble.hpp rename to src/libigl/igl/Singular_Value_Decomposition_Preamble.hpp diff --git a/src/igl/SolverStatus.h b/src/libigl/igl/SolverStatus.h similarity index 100% rename from src/igl/SolverStatus.h rename to src/libigl/igl/SolverStatus.h diff --git a/src/igl/SortableRow.h b/src/libigl/igl/SortableRow.h similarity index 100% rename from src/igl/SortableRow.h rename to src/libigl/igl/SortableRow.h diff --git a/src/igl/Timer.h b/src/libigl/igl/Timer.h similarity index 100% rename from src/igl/Timer.h rename to src/libigl/igl/Timer.h diff --git a/src/igl/Viewport.h b/src/libigl/igl/Viewport.h similarity index 100% rename from src/igl/Viewport.h rename to src/libigl/igl/Viewport.h diff --git a/src/igl/WindingNumberAABB.h b/src/libigl/igl/WindingNumberAABB.h similarity index 100% rename from src/igl/WindingNumberAABB.h rename to src/libigl/igl/WindingNumberAABB.h diff --git a/src/igl/WindingNumberMethod.h b/src/libigl/igl/WindingNumberMethod.h similarity index 100% rename from src/igl/WindingNumberMethod.h rename to src/libigl/igl/WindingNumberMethod.h diff --git a/src/igl/WindingNumberTree.h b/src/libigl/igl/WindingNumberTree.h similarity index 100% rename from src/igl/WindingNumberTree.h rename to src/libigl/igl/WindingNumberTree.h diff --git a/src/igl/ZERO.h b/src/libigl/igl/ZERO.h similarity index 100% rename from src/igl/ZERO.h rename to src/libigl/igl/ZERO.h diff --git a/src/igl/active_set.cpp b/src/libigl/igl/active_set.cpp similarity index 100% rename from src/igl/active_set.cpp rename to src/libigl/igl/active_set.cpp diff --git a/src/igl/active_set.h b/src/libigl/igl/active_set.h similarity index 100% rename from src/igl/active_set.h rename to src/libigl/igl/active_set.h diff --git a/src/igl/adjacency_list.cpp b/src/libigl/igl/adjacency_list.cpp similarity index 100% rename from src/igl/adjacency_list.cpp rename to src/libigl/igl/adjacency_list.cpp diff --git a/src/igl/adjacency_list.h b/src/libigl/igl/adjacency_list.h similarity index 100% rename from src/igl/adjacency_list.h rename to src/libigl/igl/adjacency_list.h diff --git a/src/igl/adjacency_matrix.cpp b/src/libigl/igl/adjacency_matrix.cpp similarity index 100% rename from src/igl/adjacency_matrix.cpp rename to src/libigl/igl/adjacency_matrix.cpp diff --git a/src/igl/adjacency_matrix.h b/src/libigl/igl/adjacency_matrix.h similarity index 100% rename from src/igl/adjacency_matrix.h rename to src/libigl/igl/adjacency_matrix.h diff --git a/src/igl/all.cpp b/src/libigl/igl/all.cpp similarity index 100% rename from src/igl/all.cpp rename to src/libigl/igl/all.cpp diff --git a/src/igl/all.h b/src/libigl/igl/all.h similarity index 100% rename from src/igl/all.h rename to src/libigl/igl/all.h diff --git a/src/igl/all_edges.cpp b/src/libigl/igl/all_edges.cpp similarity index 100% rename from src/igl/all_edges.cpp rename to src/libigl/igl/all_edges.cpp diff --git a/src/igl/all_edges.h b/src/libigl/igl/all_edges.h similarity index 100% rename from src/igl/all_edges.h rename to src/libigl/igl/all_edges.h diff --git a/src/igl/all_pairs_distances.cpp b/src/libigl/igl/all_pairs_distances.cpp similarity index 100% rename from src/igl/all_pairs_distances.cpp rename to src/libigl/igl/all_pairs_distances.cpp diff --git a/src/igl/all_pairs_distances.h b/src/libigl/igl/all_pairs_distances.h similarity index 100% rename from src/igl/all_pairs_distances.h rename to src/libigl/igl/all_pairs_distances.h diff --git a/src/igl/ambient_occlusion.cpp b/src/libigl/igl/ambient_occlusion.cpp similarity index 100% rename from src/igl/ambient_occlusion.cpp rename to src/libigl/igl/ambient_occlusion.cpp diff --git a/src/igl/ambient_occlusion.h b/src/libigl/igl/ambient_occlusion.h similarity index 100% rename from src/igl/ambient_occlusion.h rename to src/libigl/igl/ambient_occlusion.h diff --git a/src/igl/angular_distance.cpp b/src/libigl/igl/angular_distance.cpp similarity index 100% rename from src/igl/angular_distance.cpp rename to src/libigl/igl/angular_distance.cpp diff --git a/src/igl/angular_distance.h b/src/libigl/igl/angular_distance.h similarity index 100% rename from src/igl/angular_distance.h rename to src/libigl/igl/angular_distance.h diff --git a/src/igl/anttweakbar/ReAntTweakBar.cpp b/src/libigl/igl/anttweakbar/ReAntTweakBar.cpp similarity index 100% rename from src/igl/anttweakbar/ReAntTweakBar.cpp rename to src/libigl/igl/anttweakbar/ReAntTweakBar.cpp diff --git a/src/igl/anttweakbar/ReAntTweakBar.h b/src/libigl/igl/anttweakbar/ReAntTweakBar.h similarity index 100% rename from src/igl/anttweakbar/ReAntTweakBar.h rename to src/libigl/igl/anttweakbar/ReAntTweakBar.h diff --git a/src/igl/anttweakbar/cocoa_key_to_anttweakbar_key.cpp b/src/libigl/igl/anttweakbar/cocoa_key_to_anttweakbar_key.cpp similarity index 100% rename from src/igl/anttweakbar/cocoa_key_to_anttweakbar_key.cpp rename to src/libigl/igl/anttweakbar/cocoa_key_to_anttweakbar_key.cpp diff --git a/src/igl/anttweakbar/cocoa_key_to_anttweakbar_key.h b/src/libigl/igl/anttweakbar/cocoa_key_to_anttweakbar_key.h similarity index 100% rename from src/igl/anttweakbar/cocoa_key_to_anttweakbar_key.h rename to src/libigl/igl/anttweakbar/cocoa_key_to_anttweakbar_key.h diff --git a/src/igl/any.cpp b/src/libigl/igl/any.cpp similarity index 100% rename from src/igl/any.cpp rename to src/libigl/igl/any.cpp diff --git a/src/igl/any.h b/src/libigl/igl/any.h similarity index 100% rename from src/igl/any.h rename to src/libigl/igl/any.h diff --git a/src/igl/any_of.cpp b/src/libigl/igl/any_of.cpp similarity index 100% rename from src/igl/any_of.cpp rename to src/libigl/igl/any_of.cpp diff --git a/src/igl/any_of.h b/src/libigl/igl/any_of.h similarity index 100% rename from src/igl/any_of.h rename to src/libigl/igl/any_of.h diff --git a/src/igl/arap.cpp b/src/libigl/igl/arap.cpp similarity index 100% rename from src/igl/arap.cpp rename to src/libigl/igl/arap.cpp diff --git a/src/igl/arap.h b/src/libigl/igl/arap.h similarity index 100% rename from src/igl/arap.h rename to src/libigl/igl/arap.h diff --git a/src/igl/arap_dof.cpp b/src/libigl/igl/arap_dof.cpp similarity index 100% rename from src/igl/arap_dof.cpp rename to src/libigl/igl/arap_dof.cpp diff --git a/src/igl/arap_dof.h b/src/libigl/igl/arap_dof.h similarity index 100% rename from src/igl/arap_dof.h rename to src/libigl/igl/arap_dof.h diff --git a/src/igl/arap_linear_block.cpp b/src/libigl/igl/arap_linear_block.cpp similarity index 100% rename from src/igl/arap_linear_block.cpp rename to src/libigl/igl/arap_linear_block.cpp diff --git a/src/igl/arap_linear_block.h b/src/libigl/igl/arap_linear_block.h similarity index 100% rename from src/igl/arap_linear_block.h rename to src/libigl/igl/arap_linear_block.h diff --git a/src/igl/arap_rhs.cpp b/src/libigl/igl/arap_rhs.cpp similarity index 100% rename from src/igl/arap_rhs.cpp rename to src/libigl/igl/arap_rhs.cpp diff --git a/src/igl/arap_rhs.h b/src/libigl/igl/arap_rhs.h similarity index 100% rename from src/igl/arap_rhs.h rename to src/libigl/igl/arap_rhs.h diff --git a/src/igl/average_onto_faces.cpp b/src/libigl/igl/average_onto_faces.cpp similarity index 100% rename from src/igl/average_onto_faces.cpp rename to src/libigl/igl/average_onto_faces.cpp diff --git a/src/igl/average_onto_faces.h b/src/libigl/igl/average_onto_faces.h similarity index 100% rename from src/igl/average_onto_faces.h rename to src/libigl/igl/average_onto_faces.h diff --git a/src/igl/average_onto_vertices.cpp b/src/libigl/igl/average_onto_vertices.cpp similarity index 100% rename from src/igl/average_onto_vertices.cpp rename to src/libigl/igl/average_onto_vertices.cpp diff --git a/src/igl/average_onto_vertices.h b/src/libigl/igl/average_onto_vertices.h similarity index 100% rename from src/igl/average_onto_vertices.h rename to src/libigl/igl/average_onto_vertices.h diff --git a/src/igl/avg_edge_length.cpp b/src/libigl/igl/avg_edge_length.cpp similarity index 100% rename from src/igl/avg_edge_length.cpp rename to src/libigl/igl/avg_edge_length.cpp diff --git a/src/igl/avg_edge_length.h b/src/libigl/igl/avg_edge_length.h similarity index 100% rename from src/igl/avg_edge_length.h rename to src/libigl/igl/avg_edge_length.h diff --git a/src/igl/axis_angle_to_quat.cpp b/src/libigl/igl/axis_angle_to_quat.cpp similarity index 100% rename from src/igl/axis_angle_to_quat.cpp rename to src/libigl/igl/axis_angle_to_quat.cpp diff --git a/src/igl/axis_angle_to_quat.h b/src/libigl/igl/axis_angle_to_quat.h similarity index 100% rename from src/igl/axis_angle_to_quat.h rename to src/libigl/igl/axis_angle_to_quat.h diff --git a/src/igl/barycenter.cpp b/src/libigl/igl/barycenter.cpp similarity index 100% rename from src/igl/barycenter.cpp rename to src/libigl/igl/barycenter.cpp diff --git a/src/igl/barycenter.h b/src/libigl/igl/barycenter.h similarity index 100% rename from src/igl/barycenter.h rename to src/libigl/igl/barycenter.h diff --git a/src/igl/barycentric_coordinates.cpp b/src/libigl/igl/barycentric_coordinates.cpp similarity index 100% rename from src/igl/barycentric_coordinates.cpp rename to src/libigl/igl/barycentric_coordinates.cpp diff --git a/src/igl/barycentric_coordinates.h b/src/libigl/igl/barycentric_coordinates.h similarity index 100% rename from src/igl/barycentric_coordinates.h rename to src/libigl/igl/barycentric_coordinates.h diff --git a/src/igl/barycentric_to_global.cpp b/src/libigl/igl/barycentric_to_global.cpp similarity index 100% rename from src/igl/barycentric_to_global.cpp rename to src/libigl/igl/barycentric_to_global.cpp diff --git a/src/igl/barycentric_to_global.h b/src/libigl/igl/barycentric_to_global.h similarity index 100% rename from src/igl/barycentric_to_global.h rename to src/libigl/igl/barycentric_to_global.h diff --git a/src/igl/basename.cpp b/src/libigl/igl/basename.cpp similarity index 100% rename from src/igl/basename.cpp rename to src/libigl/igl/basename.cpp diff --git a/src/igl/basename.h b/src/libigl/igl/basename.h similarity index 100% rename from src/igl/basename.h rename to src/libigl/igl/basename.h diff --git a/src/igl/bbw.cpp b/src/libigl/igl/bbw.cpp similarity index 100% rename from src/igl/bbw.cpp rename to src/libigl/igl/bbw.cpp diff --git a/src/igl/bbw.h b/src/libigl/igl/bbw.h similarity index 100% rename from src/igl/bbw.h rename to src/libigl/igl/bbw.h diff --git a/src/igl/bfs.cpp b/src/libigl/igl/bfs.cpp similarity index 100% rename from src/igl/bfs.cpp rename to src/libigl/igl/bfs.cpp diff --git a/src/igl/bfs.h b/src/libigl/igl/bfs.h similarity index 100% rename from src/igl/bfs.h rename to src/libigl/igl/bfs.h diff --git a/src/igl/bfs_orient.cpp b/src/libigl/igl/bfs_orient.cpp similarity index 100% rename from src/igl/bfs_orient.cpp rename to src/libigl/igl/bfs_orient.cpp diff --git a/src/igl/bfs_orient.h b/src/libigl/igl/bfs_orient.h similarity index 100% rename from src/igl/bfs_orient.h rename to src/libigl/igl/bfs_orient.h diff --git a/src/igl/biharmonic_coordinates.cpp b/src/libigl/igl/biharmonic_coordinates.cpp similarity index 100% rename from src/igl/biharmonic_coordinates.cpp rename to src/libigl/igl/biharmonic_coordinates.cpp diff --git a/src/igl/biharmonic_coordinates.h b/src/libigl/igl/biharmonic_coordinates.h similarity index 100% rename from src/igl/biharmonic_coordinates.h rename to src/libigl/igl/biharmonic_coordinates.h diff --git a/src/igl/bijective_composite_harmonic_mapping.cpp b/src/libigl/igl/bijective_composite_harmonic_mapping.cpp similarity index 100% rename from src/igl/bijective_composite_harmonic_mapping.cpp rename to src/libigl/igl/bijective_composite_harmonic_mapping.cpp diff --git a/src/igl/bijective_composite_harmonic_mapping.h b/src/libigl/igl/bijective_composite_harmonic_mapping.h similarity index 100% rename from src/igl/bijective_composite_harmonic_mapping.h rename to src/libigl/igl/bijective_composite_harmonic_mapping.h diff --git a/src/igl/bone_parents.cpp b/src/libigl/igl/bone_parents.cpp similarity index 100% rename from src/igl/bone_parents.cpp rename to src/libigl/igl/bone_parents.cpp diff --git a/src/igl/bone_parents.h b/src/libigl/igl/bone_parents.h similarity index 100% rename from src/igl/bone_parents.h rename to src/libigl/igl/bone_parents.h diff --git a/src/igl/boundary_conditions.cpp b/src/libigl/igl/boundary_conditions.cpp similarity index 100% rename from src/igl/boundary_conditions.cpp rename to src/libigl/igl/boundary_conditions.cpp diff --git a/src/igl/boundary_conditions.h b/src/libigl/igl/boundary_conditions.h similarity index 100% rename from src/igl/boundary_conditions.h rename to src/libigl/igl/boundary_conditions.h diff --git a/src/igl/boundary_facets.cpp b/src/libigl/igl/boundary_facets.cpp similarity index 100% rename from src/igl/boundary_facets.cpp rename to src/libigl/igl/boundary_facets.cpp diff --git a/src/igl/boundary_facets.h b/src/libigl/igl/boundary_facets.h similarity index 100% rename from src/igl/boundary_facets.h rename to src/libigl/igl/boundary_facets.h diff --git a/src/igl/boundary_loop.cpp b/src/libigl/igl/boundary_loop.cpp similarity index 100% rename from src/igl/boundary_loop.cpp rename to src/libigl/igl/boundary_loop.cpp diff --git a/src/igl/boundary_loop.h b/src/libigl/igl/boundary_loop.h similarity index 100% rename from src/igl/boundary_loop.h rename to src/libigl/igl/boundary_loop.h diff --git a/src/igl/bounding_box.cpp b/src/libigl/igl/bounding_box.cpp similarity index 100% rename from src/igl/bounding_box.cpp rename to src/libigl/igl/bounding_box.cpp diff --git a/src/igl/bounding_box.h b/src/libigl/igl/bounding_box.h similarity index 100% rename from src/igl/bounding_box.h rename to src/libigl/igl/bounding_box.h diff --git a/src/igl/bounding_box_diagonal.cpp b/src/libigl/igl/bounding_box_diagonal.cpp similarity index 100% rename from src/igl/bounding_box_diagonal.cpp rename to src/libigl/igl/bounding_box_diagonal.cpp diff --git a/src/igl/bounding_box_diagonal.h b/src/libigl/igl/bounding_box_diagonal.h similarity index 100% rename from src/igl/bounding_box_diagonal.h rename to src/libigl/igl/bounding_box_diagonal.h diff --git a/src/igl/canonical_quaternions.cpp b/src/libigl/igl/canonical_quaternions.cpp similarity index 100% rename from src/igl/canonical_quaternions.cpp rename to src/libigl/igl/canonical_quaternions.cpp diff --git a/src/igl/canonical_quaternions.h b/src/libigl/igl/canonical_quaternions.h similarity index 100% rename from src/igl/canonical_quaternions.h rename to src/libigl/igl/canonical_quaternions.h diff --git a/src/igl/cat.cpp b/src/libigl/igl/cat.cpp similarity index 100% rename from src/igl/cat.cpp rename to src/libigl/igl/cat.cpp diff --git a/src/igl/cat.h b/src/libigl/igl/cat.h similarity index 100% rename from src/igl/cat.h rename to src/libigl/igl/cat.h diff --git a/src/igl/ceil.cpp b/src/libigl/igl/ceil.cpp similarity index 100% rename from src/igl/ceil.cpp rename to src/libigl/igl/ceil.cpp diff --git a/src/igl/ceil.h b/src/libigl/igl/ceil.h similarity index 100% rename from src/igl/ceil.h rename to src/libigl/igl/ceil.h diff --git a/src/igl/centroid.cpp b/src/libigl/igl/centroid.cpp similarity index 100% rename from src/igl/centroid.cpp rename to src/libigl/igl/centroid.cpp diff --git a/src/igl/centroid.h b/src/libigl/igl/centroid.h similarity index 100% rename from src/igl/centroid.h rename to src/libigl/igl/centroid.h diff --git a/src/igl/circulation.cpp b/src/libigl/igl/circulation.cpp similarity index 100% rename from src/igl/circulation.cpp rename to src/libigl/igl/circulation.cpp diff --git a/src/igl/circulation.h b/src/libigl/igl/circulation.h similarity index 100% rename from src/igl/circulation.h rename to src/libigl/igl/circulation.h diff --git a/src/igl/circumradius.cpp b/src/libigl/igl/circumradius.cpp similarity index 100% rename from src/igl/circumradius.cpp rename to src/libigl/igl/circumradius.cpp diff --git a/src/igl/circumradius.h b/src/libigl/igl/circumradius.h similarity index 100% rename from src/igl/circumradius.h rename to src/libigl/igl/circumradius.h diff --git a/src/igl/collapse_edge.cpp b/src/libigl/igl/collapse_edge.cpp similarity index 100% rename from src/igl/collapse_edge.cpp rename to src/libigl/igl/collapse_edge.cpp diff --git a/src/igl/collapse_edge.h b/src/libigl/igl/collapse_edge.h similarity index 100% rename from src/igl/collapse_edge.h rename to src/libigl/igl/collapse_edge.h diff --git a/src/igl/collapse_small_triangles.cpp b/src/libigl/igl/collapse_small_triangles.cpp similarity index 100% rename from src/igl/collapse_small_triangles.cpp rename to src/libigl/igl/collapse_small_triangles.cpp diff --git a/src/igl/collapse_small_triangles.h b/src/libigl/igl/collapse_small_triangles.h similarity index 100% rename from src/igl/collapse_small_triangles.h rename to src/libigl/igl/collapse_small_triangles.h diff --git a/src/igl/colon.cpp b/src/libigl/igl/colon.cpp similarity index 100% rename from src/igl/colon.cpp rename to src/libigl/igl/colon.cpp diff --git a/src/igl/colon.h b/src/libigl/igl/colon.h similarity index 100% rename from src/igl/colon.h rename to src/libigl/igl/colon.h diff --git a/src/igl/colormap.cpp b/src/libigl/igl/colormap.cpp similarity index 100% rename from src/igl/colormap.cpp rename to src/libigl/igl/colormap.cpp diff --git a/src/igl/colormap.h b/src/libigl/igl/colormap.h similarity index 100% rename from src/igl/colormap.h rename to src/libigl/igl/colormap.h diff --git a/src/igl/column_to_quats.cpp b/src/libigl/igl/column_to_quats.cpp similarity index 100% rename from src/igl/column_to_quats.cpp rename to src/libigl/igl/column_to_quats.cpp diff --git a/src/igl/column_to_quats.h b/src/libigl/igl/column_to_quats.h similarity index 100% rename from src/igl/column_to_quats.h rename to src/libigl/igl/column_to_quats.h diff --git a/src/igl/columnize.cpp b/src/libigl/igl/columnize.cpp similarity index 100% rename from src/igl/columnize.cpp rename to src/libigl/igl/columnize.cpp diff --git a/src/igl/columnize.h b/src/libigl/igl/columnize.h similarity index 100% rename from src/igl/columnize.h rename to src/libigl/igl/columnize.h diff --git a/src/igl/comb_cross_field.cpp b/src/libigl/igl/comb_cross_field.cpp similarity index 100% rename from src/igl/comb_cross_field.cpp rename to src/libigl/igl/comb_cross_field.cpp diff --git a/src/igl/comb_cross_field.h b/src/libigl/igl/comb_cross_field.h similarity index 100% rename from src/igl/comb_cross_field.h rename to src/libigl/igl/comb_cross_field.h diff --git a/src/igl/comb_frame_field.cpp b/src/libigl/igl/comb_frame_field.cpp similarity index 100% rename from src/igl/comb_frame_field.cpp rename to src/libigl/igl/comb_frame_field.cpp diff --git a/src/igl/comb_frame_field.h b/src/libigl/igl/comb_frame_field.h similarity index 100% rename from src/igl/comb_frame_field.h rename to src/libigl/igl/comb_frame_field.h diff --git a/src/igl/comb_line_field.cpp b/src/libigl/igl/comb_line_field.cpp similarity index 100% rename from src/igl/comb_line_field.cpp rename to src/libigl/igl/comb_line_field.cpp diff --git a/src/igl/comb_line_field.h b/src/libigl/igl/comb_line_field.h similarity index 100% rename from src/igl/comb_line_field.h rename to src/libigl/igl/comb_line_field.h diff --git a/src/igl/combine.cpp b/src/libigl/igl/combine.cpp similarity index 100% rename from src/igl/combine.cpp rename to src/libigl/igl/combine.cpp diff --git a/src/igl/combine.h b/src/libigl/igl/combine.h similarity index 100% rename from src/igl/combine.h rename to src/libigl/igl/combine.h diff --git a/src/igl/components.cpp b/src/libigl/igl/components.cpp similarity index 100% rename from src/igl/components.cpp rename to src/libigl/igl/components.cpp diff --git a/src/igl/components.h b/src/libigl/igl/components.h similarity index 100% rename from src/igl/components.h rename to src/libigl/igl/components.h diff --git a/src/igl/compute_frame_field_bisectors.cpp b/src/libigl/igl/compute_frame_field_bisectors.cpp similarity index 100% rename from src/igl/compute_frame_field_bisectors.cpp rename to src/libigl/igl/compute_frame_field_bisectors.cpp diff --git a/src/igl/compute_frame_field_bisectors.h b/src/libigl/igl/compute_frame_field_bisectors.h similarity index 100% rename from src/igl/compute_frame_field_bisectors.h rename to src/libigl/igl/compute_frame_field_bisectors.h diff --git a/src/igl/connect_boundary_to_infinity.cpp b/src/libigl/igl/connect_boundary_to_infinity.cpp similarity index 100% rename from src/igl/connect_boundary_to_infinity.cpp rename to src/libigl/igl/connect_boundary_to_infinity.cpp diff --git a/src/igl/connect_boundary_to_infinity.h b/src/libigl/igl/connect_boundary_to_infinity.h similarity index 100% rename from src/igl/connect_boundary_to_infinity.h rename to src/libigl/igl/connect_boundary_to_infinity.h diff --git a/src/igl/copyleft/README.md b/src/libigl/igl/copyleft/README.md similarity index 100% rename from src/igl/copyleft/README.md rename to src/libigl/igl/copyleft/README.md diff --git a/src/igl/copyleft/cgal/BinaryWindingNumberOperations.h b/src/libigl/igl/copyleft/cgal/BinaryWindingNumberOperations.h similarity index 100% rename from src/igl/copyleft/cgal/BinaryWindingNumberOperations.h rename to src/libigl/igl/copyleft/cgal/BinaryWindingNumberOperations.h diff --git a/src/igl/copyleft/cgal/CGAL_includes.hpp b/src/libigl/igl/copyleft/cgal/CGAL_includes.hpp similarity index 100% rename from src/igl/copyleft/cgal/CGAL_includes.hpp rename to src/libigl/igl/copyleft/cgal/CGAL_includes.hpp diff --git a/src/igl/copyleft/cgal/CSGTree.h b/src/libigl/igl/copyleft/cgal/CSGTree.h similarity index 100% rename from src/igl/copyleft/cgal/CSGTree.h rename to src/libigl/igl/copyleft/cgal/CSGTree.h diff --git a/src/igl/copyleft/cgal/RemeshSelfIntersectionsParam.h b/src/libigl/igl/copyleft/cgal/RemeshSelfIntersectionsParam.h similarity index 100% rename from src/igl/copyleft/cgal/RemeshSelfIntersectionsParam.h rename to src/libigl/igl/copyleft/cgal/RemeshSelfIntersectionsParam.h diff --git a/src/igl/copyleft/cgal/SelfIntersectMesh.h b/src/libigl/igl/copyleft/cgal/SelfIntersectMesh.h similarity index 100% rename from src/igl/copyleft/cgal/SelfIntersectMesh.h rename to src/libigl/igl/copyleft/cgal/SelfIntersectMesh.h diff --git a/src/igl/copyleft/cgal/assign.cpp b/src/libigl/igl/copyleft/cgal/assign.cpp similarity index 100% rename from src/igl/copyleft/cgal/assign.cpp rename to src/libigl/igl/copyleft/cgal/assign.cpp diff --git a/src/igl/copyleft/cgal/assign.h b/src/libigl/igl/copyleft/cgal/assign.h similarity index 100% rename from src/igl/copyleft/cgal/assign.h rename to src/libigl/igl/copyleft/cgal/assign.h diff --git a/src/igl/copyleft/cgal/assign_scalar.cpp b/src/libigl/igl/copyleft/cgal/assign_scalar.cpp similarity index 100% rename from src/igl/copyleft/cgal/assign_scalar.cpp rename to src/libigl/igl/copyleft/cgal/assign_scalar.cpp diff --git a/src/igl/copyleft/cgal/assign_scalar.h b/src/libigl/igl/copyleft/cgal/assign_scalar.h similarity index 100% rename from src/igl/copyleft/cgal/assign_scalar.h rename to src/libigl/igl/copyleft/cgal/assign_scalar.h diff --git a/src/igl/copyleft/cgal/barycenter.cpp b/src/libigl/igl/copyleft/cgal/barycenter.cpp similarity index 100% rename from src/igl/copyleft/cgal/barycenter.cpp rename to src/libigl/igl/copyleft/cgal/barycenter.cpp diff --git a/src/igl/copyleft/cgal/cell_adjacency.cpp b/src/libigl/igl/copyleft/cgal/cell_adjacency.cpp similarity index 100% rename from src/igl/copyleft/cgal/cell_adjacency.cpp rename to src/libigl/igl/copyleft/cgal/cell_adjacency.cpp diff --git a/src/igl/copyleft/cgal/cell_adjacency.h b/src/libigl/igl/copyleft/cgal/cell_adjacency.h similarity index 100% rename from src/igl/copyleft/cgal/cell_adjacency.h rename to src/libigl/igl/copyleft/cgal/cell_adjacency.h diff --git a/src/igl/copyleft/cgal/closest_facet.cpp b/src/libigl/igl/copyleft/cgal/closest_facet.cpp similarity index 100% rename from src/igl/copyleft/cgal/closest_facet.cpp rename to src/libigl/igl/copyleft/cgal/closest_facet.cpp diff --git a/src/igl/copyleft/cgal/closest_facet.h b/src/libigl/igl/copyleft/cgal/closest_facet.h similarity index 100% rename from src/igl/copyleft/cgal/closest_facet.h rename to src/libigl/igl/copyleft/cgal/closest_facet.h diff --git a/src/igl/copyleft/cgal/complex_to_mesh.cpp b/src/libigl/igl/copyleft/cgal/complex_to_mesh.cpp similarity index 100% rename from src/igl/copyleft/cgal/complex_to_mesh.cpp rename to src/libigl/igl/copyleft/cgal/complex_to_mesh.cpp diff --git a/src/igl/copyleft/cgal/complex_to_mesh.h b/src/libigl/igl/copyleft/cgal/complex_to_mesh.h similarity index 100% rename from src/igl/copyleft/cgal/complex_to_mesh.h rename to src/libigl/igl/copyleft/cgal/complex_to_mesh.h diff --git a/src/igl/copyleft/cgal/component_inside_component.cpp b/src/libigl/igl/copyleft/cgal/component_inside_component.cpp similarity index 100% rename from src/igl/copyleft/cgal/component_inside_component.cpp rename to src/libigl/igl/copyleft/cgal/component_inside_component.cpp diff --git a/src/igl/copyleft/cgal/component_inside_component.h b/src/libigl/igl/copyleft/cgal/component_inside_component.h similarity index 100% rename from src/igl/copyleft/cgal/component_inside_component.h rename to src/libigl/igl/copyleft/cgal/component_inside_component.h diff --git a/src/igl/copyleft/cgal/convex_hull.cpp b/src/libigl/igl/copyleft/cgal/convex_hull.cpp similarity index 100% rename from src/igl/copyleft/cgal/convex_hull.cpp rename to src/libigl/igl/copyleft/cgal/convex_hull.cpp diff --git a/src/igl/copyleft/cgal/convex_hull.h b/src/libigl/igl/copyleft/cgal/convex_hull.h similarity index 100% rename from src/igl/copyleft/cgal/convex_hull.h rename to src/libigl/igl/copyleft/cgal/convex_hull.h diff --git a/src/igl/copyleft/cgal/delaunay_triangulation.cpp b/src/libigl/igl/copyleft/cgal/delaunay_triangulation.cpp similarity index 100% rename from src/igl/copyleft/cgal/delaunay_triangulation.cpp rename to src/libigl/igl/copyleft/cgal/delaunay_triangulation.cpp diff --git a/src/igl/copyleft/cgal/delaunay_triangulation.h b/src/libigl/igl/copyleft/cgal/delaunay_triangulation.h similarity index 100% rename from src/igl/copyleft/cgal/delaunay_triangulation.h rename to src/libigl/igl/copyleft/cgal/delaunay_triangulation.h diff --git a/src/igl/copyleft/cgal/extract_cells.cpp b/src/libigl/igl/copyleft/cgal/extract_cells.cpp similarity index 100% rename from src/igl/copyleft/cgal/extract_cells.cpp rename to src/libigl/igl/copyleft/cgal/extract_cells.cpp diff --git a/src/igl/copyleft/cgal/extract_cells.h b/src/libigl/igl/copyleft/cgal/extract_cells.h similarity index 100% rename from src/igl/copyleft/cgal/extract_cells.h rename to src/libigl/igl/copyleft/cgal/extract_cells.h diff --git a/src/igl/copyleft/cgal/extract_feature.cpp b/src/libigl/igl/copyleft/cgal/extract_feature.cpp similarity index 100% rename from src/igl/copyleft/cgal/extract_feature.cpp rename to src/libigl/igl/copyleft/cgal/extract_feature.cpp diff --git a/src/igl/copyleft/cgal/extract_feature.h b/src/libigl/igl/copyleft/cgal/extract_feature.h similarity index 100% rename from src/igl/copyleft/cgal/extract_feature.h rename to src/libigl/igl/copyleft/cgal/extract_feature.h diff --git a/src/igl/copyleft/cgal/fast_winding_number.cpp b/src/libigl/igl/copyleft/cgal/fast_winding_number.cpp similarity index 100% rename from src/igl/copyleft/cgal/fast_winding_number.cpp rename to src/libigl/igl/copyleft/cgal/fast_winding_number.cpp diff --git a/src/igl/copyleft/cgal/fast_winding_number.h b/src/libigl/igl/copyleft/cgal/fast_winding_number.h similarity index 100% rename from src/igl/copyleft/cgal/fast_winding_number.h rename to src/libigl/igl/copyleft/cgal/fast_winding_number.h diff --git a/src/igl/copyleft/cgal/half_space_box.cpp b/src/libigl/igl/copyleft/cgal/half_space_box.cpp similarity index 100% rename from src/igl/copyleft/cgal/half_space_box.cpp rename to src/libigl/igl/copyleft/cgal/half_space_box.cpp diff --git a/src/igl/copyleft/cgal/half_space_box.h b/src/libigl/igl/copyleft/cgal/half_space_box.h similarity index 100% rename from src/igl/copyleft/cgal/half_space_box.h rename to src/libigl/igl/copyleft/cgal/half_space_box.h diff --git a/src/igl/copyleft/cgal/hausdorff.cpp b/src/libigl/igl/copyleft/cgal/hausdorff.cpp similarity index 100% rename from src/igl/copyleft/cgal/hausdorff.cpp rename to src/libigl/igl/copyleft/cgal/hausdorff.cpp diff --git a/src/igl/copyleft/cgal/hausdorff.h b/src/libigl/igl/copyleft/cgal/hausdorff.h similarity index 100% rename from src/igl/copyleft/cgal/hausdorff.h rename to src/libigl/igl/copyleft/cgal/hausdorff.h diff --git a/src/igl/copyleft/cgal/incircle.cpp b/src/libigl/igl/copyleft/cgal/incircle.cpp similarity index 100% rename from src/igl/copyleft/cgal/incircle.cpp rename to src/libigl/igl/copyleft/cgal/incircle.cpp diff --git a/src/igl/copyleft/cgal/incircle.h b/src/libigl/igl/copyleft/cgal/incircle.h similarity index 100% rename from src/igl/copyleft/cgal/incircle.h rename to src/libigl/igl/copyleft/cgal/incircle.h diff --git a/src/igl/copyleft/cgal/insert_into_cdt.cpp b/src/libigl/igl/copyleft/cgal/insert_into_cdt.cpp similarity index 100% rename from src/igl/copyleft/cgal/insert_into_cdt.cpp rename to src/libigl/igl/copyleft/cgal/insert_into_cdt.cpp diff --git a/src/igl/copyleft/cgal/insert_into_cdt.h b/src/libigl/igl/copyleft/cgal/insert_into_cdt.h similarity index 100% rename from src/igl/copyleft/cgal/insert_into_cdt.h rename to src/libigl/igl/copyleft/cgal/insert_into_cdt.h diff --git a/src/igl/copyleft/cgal/insphere.cpp b/src/libigl/igl/copyleft/cgal/insphere.cpp similarity index 100% rename from src/igl/copyleft/cgal/insphere.cpp rename to src/libigl/igl/copyleft/cgal/insphere.cpp diff --git a/src/igl/copyleft/cgal/insphere.h b/src/libigl/igl/copyleft/cgal/insphere.h similarity index 100% rename from src/igl/copyleft/cgal/insphere.h rename to src/libigl/igl/copyleft/cgal/insphere.h diff --git a/src/igl/copyleft/cgal/intersect_other.cpp b/src/libigl/igl/copyleft/cgal/intersect_other.cpp similarity index 100% rename from src/igl/copyleft/cgal/intersect_other.cpp rename to src/libigl/igl/copyleft/cgal/intersect_other.cpp diff --git a/src/igl/copyleft/cgal/intersect_other.h b/src/libigl/igl/copyleft/cgal/intersect_other.h similarity index 100% rename from src/igl/copyleft/cgal/intersect_other.h rename to src/libigl/igl/copyleft/cgal/intersect_other.h diff --git a/src/igl/copyleft/cgal/intersect_with_half_space.cpp b/src/libigl/igl/copyleft/cgal/intersect_with_half_space.cpp similarity index 100% rename from src/igl/copyleft/cgal/intersect_with_half_space.cpp rename to src/libigl/igl/copyleft/cgal/intersect_with_half_space.cpp diff --git a/src/igl/copyleft/cgal/intersect_with_half_space.h b/src/libigl/igl/copyleft/cgal/intersect_with_half_space.h similarity index 100% rename from src/igl/copyleft/cgal/intersect_with_half_space.h rename to src/libigl/igl/copyleft/cgal/intersect_with_half_space.h diff --git a/src/igl/copyleft/cgal/lexicographic_triangulation.cpp b/src/libigl/igl/copyleft/cgal/lexicographic_triangulation.cpp similarity index 100% rename from src/igl/copyleft/cgal/lexicographic_triangulation.cpp rename to src/libigl/igl/copyleft/cgal/lexicographic_triangulation.cpp diff --git a/src/igl/copyleft/cgal/lexicographic_triangulation.h b/src/libigl/igl/copyleft/cgal/lexicographic_triangulation.h similarity index 100% rename from src/igl/copyleft/cgal/lexicographic_triangulation.h rename to src/libigl/igl/copyleft/cgal/lexicographic_triangulation.h diff --git a/src/igl/copyleft/cgal/list_to_matrix.cpp b/src/libigl/igl/copyleft/cgal/list_to_matrix.cpp similarity index 100% rename from src/igl/copyleft/cgal/list_to_matrix.cpp rename to src/libigl/igl/copyleft/cgal/list_to_matrix.cpp diff --git a/src/igl/copyleft/cgal/mesh_boolean.cpp b/src/libigl/igl/copyleft/cgal/mesh_boolean.cpp similarity index 100% rename from src/igl/copyleft/cgal/mesh_boolean.cpp rename to src/libigl/igl/copyleft/cgal/mesh_boolean.cpp diff --git a/src/igl/copyleft/cgal/mesh_boolean.h b/src/libigl/igl/copyleft/cgal/mesh_boolean.h similarity index 100% rename from src/igl/copyleft/cgal/mesh_boolean.h rename to src/libigl/igl/copyleft/cgal/mesh_boolean.h diff --git a/src/igl/copyleft/cgal/mesh_boolean_type_to_funcs.cpp b/src/libigl/igl/copyleft/cgal/mesh_boolean_type_to_funcs.cpp similarity index 100% rename from src/igl/copyleft/cgal/mesh_boolean_type_to_funcs.cpp rename to src/libigl/igl/copyleft/cgal/mesh_boolean_type_to_funcs.cpp diff --git a/src/igl/copyleft/cgal/mesh_boolean_type_to_funcs.h b/src/libigl/igl/copyleft/cgal/mesh_boolean_type_to_funcs.h similarity index 100% rename from src/igl/copyleft/cgal/mesh_boolean_type_to_funcs.h rename to src/libigl/igl/copyleft/cgal/mesh_boolean_type_to_funcs.h diff --git a/src/igl/copyleft/cgal/mesh_to_cgal_triangle_list.cpp b/src/libigl/igl/copyleft/cgal/mesh_to_cgal_triangle_list.cpp similarity index 100% rename from src/igl/copyleft/cgal/mesh_to_cgal_triangle_list.cpp rename to src/libigl/igl/copyleft/cgal/mesh_to_cgal_triangle_list.cpp diff --git a/src/igl/copyleft/cgal/mesh_to_cgal_triangle_list.h b/src/libigl/igl/copyleft/cgal/mesh_to_cgal_triangle_list.h similarity index 100% rename from src/igl/copyleft/cgal/mesh_to_cgal_triangle_list.h rename to src/libigl/igl/copyleft/cgal/mesh_to_cgal_triangle_list.h diff --git a/src/igl/copyleft/cgal/mesh_to_polyhedron.cpp b/src/libigl/igl/copyleft/cgal/mesh_to_polyhedron.cpp similarity index 100% rename from src/igl/copyleft/cgal/mesh_to_polyhedron.cpp rename to src/libigl/igl/copyleft/cgal/mesh_to_polyhedron.cpp diff --git a/src/igl/copyleft/cgal/mesh_to_polyhedron.h b/src/libigl/igl/copyleft/cgal/mesh_to_polyhedron.h similarity index 100% rename from src/igl/copyleft/cgal/mesh_to_polyhedron.h rename to src/libigl/igl/copyleft/cgal/mesh_to_polyhedron.h diff --git a/src/igl/copyleft/cgal/minkowski_sum.cpp b/src/libigl/igl/copyleft/cgal/minkowski_sum.cpp similarity index 100% rename from src/igl/copyleft/cgal/minkowski_sum.cpp rename to src/libigl/igl/copyleft/cgal/minkowski_sum.cpp diff --git a/src/igl/copyleft/cgal/minkowski_sum.h b/src/libigl/igl/copyleft/cgal/minkowski_sum.h similarity index 100% rename from src/igl/copyleft/cgal/minkowski_sum.h rename to src/libigl/igl/copyleft/cgal/minkowski_sum.h diff --git a/src/igl/copyleft/cgal/order_facets_around_edge.cpp b/src/libigl/igl/copyleft/cgal/order_facets_around_edge.cpp similarity index 100% rename from src/igl/copyleft/cgal/order_facets_around_edge.cpp rename to src/libigl/igl/copyleft/cgal/order_facets_around_edge.cpp diff --git a/src/igl/copyleft/cgal/order_facets_around_edge.h b/src/libigl/igl/copyleft/cgal/order_facets_around_edge.h similarity index 100% rename from src/igl/copyleft/cgal/order_facets_around_edge.h rename to src/libigl/igl/copyleft/cgal/order_facets_around_edge.h diff --git a/src/igl/copyleft/cgal/order_facets_around_edges.cpp b/src/libigl/igl/copyleft/cgal/order_facets_around_edges.cpp similarity index 100% rename from src/igl/copyleft/cgal/order_facets_around_edges.cpp rename to src/libigl/igl/copyleft/cgal/order_facets_around_edges.cpp diff --git a/src/igl/copyleft/cgal/order_facets_around_edges.h b/src/libigl/igl/copyleft/cgal/order_facets_around_edges.h similarity index 100% rename from src/igl/copyleft/cgal/order_facets_around_edges.h rename to src/libigl/igl/copyleft/cgal/order_facets_around_edges.h diff --git a/src/igl/copyleft/cgal/orient2D.cpp b/src/libigl/igl/copyleft/cgal/orient2D.cpp similarity index 100% rename from src/igl/copyleft/cgal/orient2D.cpp rename to src/libigl/igl/copyleft/cgal/orient2D.cpp diff --git a/src/igl/copyleft/cgal/orient2D.h b/src/libigl/igl/copyleft/cgal/orient2D.h similarity index 100% rename from src/igl/copyleft/cgal/orient2D.h rename to src/libigl/igl/copyleft/cgal/orient2D.h diff --git a/src/igl/copyleft/cgal/orient3D.cpp b/src/libigl/igl/copyleft/cgal/orient3D.cpp similarity index 100% rename from src/igl/copyleft/cgal/orient3D.cpp rename to src/libigl/igl/copyleft/cgal/orient3D.cpp diff --git a/src/igl/copyleft/cgal/orient3D.h b/src/libigl/igl/copyleft/cgal/orient3D.h similarity index 100% rename from src/igl/copyleft/cgal/orient3D.h rename to src/libigl/igl/copyleft/cgal/orient3D.h diff --git a/src/igl/copyleft/cgal/outer_element.cpp b/src/libigl/igl/copyleft/cgal/outer_element.cpp similarity index 100% rename from src/igl/copyleft/cgal/outer_element.cpp rename to src/libigl/igl/copyleft/cgal/outer_element.cpp diff --git a/src/igl/copyleft/cgal/outer_element.h b/src/libigl/igl/copyleft/cgal/outer_element.h similarity index 100% rename from src/igl/copyleft/cgal/outer_element.h rename to src/libigl/igl/copyleft/cgal/outer_element.h diff --git a/src/igl/copyleft/cgal/outer_facet.cpp b/src/libigl/igl/copyleft/cgal/outer_facet.cpp similarity index 100% rename from src/igl/copyleft/cgal/outer_facet.cpp rename to src/libigl/igl/copyleft/cgal/outer_facet.cpp diff --git a/src/igl/copyleft/cgal/outer_facet.h b/src/libigl/igl/copyleft/cgal/outer_facet.h similarity index 100% rename from src/igl/copyleft/cgal/outer_facet.h rename to src/libigl/igl/copyleft/cgal/outer_facet.h diff --git a/src/igl/copyleft/cgal/outer_hull.cpp b/src/libigl/igl/copyleft/cgal/outer_hull.cpp similarity index 100% rename from src/igl/copyleft/cgal/outer_hull.cpp rename to src/libigl/igl/copyleft/cgal/outer_hull.cpp diff --git a/src/igl/copyleft/cgal/outer_hull.h b/src/libigl/igl/copyleft/cgal/outer_hull.h similarity index 100% rename from src/igl/copyleft/cgal/outer_hull.h rename to src/libigl/igl/copyleft/cgal/outer_hull.h diff --git a/src/igl/copyleft/cgal/peel_outer_hull_layers.cpp b/src/libigl/igl/copyleft/cgal/peel_outer_hull_layers.cpp similarity index 100% rename from src/igl/copyleft/cgal/peel_outer_hull_layers.cpp rename to src/libigl/igl/copyleft/cgal/peel_outer_hull_layers.cpp diff --git a/src/igl/copyleft/cgal/peel_outer_hull_layers.h b/src/libigl/igl/copyleft/cgal/peel_outer_hull_layers.h similarity index 100% rename from src/igl/copyleft/cgal/peel_outer_hull_layers.h rename to src/libigl/igl/copyleft/cgal/peel_outer_hull_layers.h diff --git a/src/igl/copyleft/cgal/peel_winding_number_layers.cpp b/src/libigl/igl/copyleft/cgal/peel_winding_number_layers.cpp similarity index 100% rename from src/igl/copyleft/cgal/peel_winding_number_layers.cpp rename to src/libigl/igl/copyleft/cgal/peel_winding_number_layers.cpp diff --git a/src/igl/copyleft/cgal/peel_winding_number_layers.h b/src/libigl/igl/copyleft/cgal/peel_winding_number_layers.h similarity index 100% rename from src/igl/copyleft/cgal/peel_winding_number_layers.h rename to src/libigl/igl/copyleft/cgal/peel_winding_number_layers.h diff --git a/src/igl/copyleft/cgal/piecewise_constant_winding_number.cpp b/src/libigl/igl/copyleft/cgal/piecewise_constant_winding_number.cpp similarity index 100% rename from src/igl/copyleft/cgal/piecewise_constant_winding_number.cpp rename to src/libigl/igl/copyleft/cgal/piecewise_constant_winding_number.cpp diff --git a/src/igl/copyleft/cgal/piecewise_constant_winding_number.h b/src/libigl/igl/copyleft/cgal/piecewise_constant_winding_number.h similarity index 100% rename from src/igl/copyleft/cgal/piecewise_constant_winding_number.h rename to src/libigl/igl/copyleft/cgal/piecewise_constant_winding_number.h diff --git a/src/igl/copyleft/cgal/point_areas.cpp b/src/libigl/igl/copyleft/cgal/point_areas.cpp similarity index 100% rename from src/igl/copyleft/cgal/point_areas.cpp rename to src/libigl/igl/copyleft/cgal/point_areas.cpp diff --git a/src/igl/copyleft/cgal/point_areas.h b/src/libigl/igl/copyleft/cgal/point_areas.h similarity index 100% rename from src/igl/copyleft/cgal/point_areas.h rename to src/libigl/igl/copyleft/cgal/point_areas.h diff --git a/src/igl/copyleft/cgal/point_mesh_squared_distance.cpp b/src/libigl/igl/copyleft/cgal/point_mesh_squared_distance.cpp similarity index 100% rename from src/igl/copyleft/cgal/point_mesh_squared_distance.cpp rename to src/libigl/igl/copyleft/cgal/point_mesh_squared_distance.cpp diff --git a/src/igl/copyleft/cgal/point_mesh_squared_distance.h b/src/libigl/igl/copyleft/cgal/point_mesh_squared_distance.h similarity index 100% rename from src/igl/copyleft/cgal/point_mesh_squared_distance.h rename to src/libigl/igl/copyleft/cgal/point_mesh_squared_distance.h diff --git a/src/igl/copyleft/cgal/point_segment_squared_distance.cpp b/src/libigl/igl/copyleft/cgal/point_segment_squared_distance.cpp similarity index 100% rename from src/igl/copyleft/cgal/point_segment_squared_distance.cpp rename to src/libigl/igl/copyleft/cgal/point_segment_squared_distance.cpp diff --git a/src/igl/copyleft/cgal/point_segment_squared_distance.h b/src/libigl/igl/copyleft/cgal/point_segment_squared_distance.h similarity index 100% rename from src/igl/copyleft/cgal/point_segment_squared_distance.h rename to src/libigl/igl/copyleft/cgal/point_segment_squared_distance.h diff --git a/src/igl/copyleft/cgal/point_solid_signed_squared_distance.cpp b/src/libigl/igl/copyleft/cgal/point_solid_signed_squared_distance.cpp similarity index 100% rename from src/igl/copyleft/cgal/point_solid_signed_squared_distance.cpp rename to src/libigl/igl/copyleft/cgal/point_solid_signed_squared_distance.cpp diff --git a/src/igl/copyleft/cgal/point_solid_signed_squared_distance.h b/src/libigl/igl/copyleft/cgal/point_solid_signed_squared_distance.h similarity index 100% rename from src/igl/copyleft/cgal/point_solid_signed_squared_distance.h rename to src/libigl/igl/copyleft/cgal/point_solid_signed_squared_distance.h diff --git a/src/igl/copyleft/cgal/point_triangle_squared_distance.cpp b/src/libigl/igl/copyleft/cgal/point_triangle_squared_distance.cpp similarity index 100% rename from src/igl/copyleft/cgal/point_triangle_squared_distance.cpp rename to src/libigl/igl/copyleft/cgal/point_triangle_squared_distance.cpp diff --git a/src/igl/copyleft/cgal/point_triangle_squared_distance.h b/src/libigl/igl/copyleft/cgal/point_triangle_squared_distance.h similarity index 100% rename from src/igl/copyleft/cgal/point_triangle_squared_distance.h rename to src/libigl/igl/copyleft/cgal/point_triangle_squared_distance.h diff --git a/src/igl/copyleft/cgal/points_inside_component.cpp b/src/libigl/igl/copyleft/cgal/points_inside_component.cpp similarity index 100% rename from src/igl/copyleft/cgal/points_inside_component.cpp rename to src/libigl/igl/copyleft/cgal/points_inside_component.cpp diff --git a/src/igl/copyleft/cgal/points_inside_component.h b/src/libigl/igl/copyleft/cgal/points_inside_component.h similarity index 100% rename from src/igl/copyleft/cgal/points_inside_component.h rename to src/libigl/igl/copyleft/cgal/points_inside_component.h diff --git a/src/igl/copyleft/cgal/polyhedron_to_mesh.cpp b/src/libigl/igl/copyleft/cgal/polyhedron_to_mesh.cpp similarity index 100% rename from src/igl/copyleft/cgal/polyhedron_to_mesh.cpp rename to src/libigl/igl/copyleft/cgal/polyhedron_to_mesh.cpp diff --git a/src/igl/copyleft/cgal/polyhedron_to_mesh.h b/src/libigl/igl/copyleft/cgal/polyhedron_to_mesh.h similarity index 100% rename from src/igl/copyleft/cgal/polyhedron_to_mesh.h rename to src/libigl/igl/copyleft/cgal/polyhedron_to_mesh.h diff --git a/src/igl/copyleft/cgal/projected_cdt.cpp b/src/libigl/igl/copyleft/cgal/projected_cdt.cpp similarity index 100% rename from src/igl/copyleft/cgal/projected_cdt.cpp rename to src/libigl/igl/copyleft/cgal/projected_cdt.cpp diff --git a/src/igl/copyleft/cgal/projected_cdt.h b/src/libigl/igl/copyleft/cgal/projected_cdt.h similarity index 100% rename from src/igl/copyleft/cgal/projected_cdt.h rename to src/libigl/igl/copyleft/cgal/projected_cdt.h diff --git a/src/igl/copyleft/cgal/projected_delaunay.cpp b/src/libigl/igl/copyleft/cgal/projected_delaunay.cpp similarity index 100% rename from src/igl/copyleft/cgal/projected_delaunay.cpp rename to src/libigl/igl/copyleft/cgal/projected_delaunay.cpp diff --git a/src/igl/copyleft/cgal/projected_delaunay.h b/src/libigl/igl/copyleft/cgal/projected_delaunay.h similarity index 100% rename from src/igl/copyleft/cgal/projected_delaunay.h rename to src/libigl/igl/copyleft/cgal/projected_delaunay.h diff --git a/src/igl/copyleft/cgal/propagate_winding_numbers.cpp b/src/libigl/igl/copyleft/cgal/propagate_winding_numbers.cpp similarity index 100% rename from src/igl/copyleft/cgal/propagate_winding_numbers.cpp rename to src/libigl/igl/copyleft/cgal/propagate_winding_numbers.cpp diff --git a/src/igl/copyleft/cgal/propagate_winding_numbers.h b/src/libigl/igl/copyleft/cgal/propagate_winding_numbers.h similarity index 100% rename from src/igl/copyleft/cgal/propagate_winding_numbers.h rename to src/libigl/igl/copyleft/cgal/propagate_winding_numbers.h diff --git a/src/igl/copyleft/cgal/read_triangle_mesh.cpp b/src/libigl/igl/copyleft/cgal/read_triangle_mesh.cpp similarity index 100% rename from src/igl/copyleft/cgal/read_triangle_mesh.cpp rename to src/libigl/igl/copyleft/cgal/read_triangle_mesh.cpp diff --git a/src/igl/copyleft/cgal/read_triangle_mesh.h b/src/libigl/igl/copyleft/cgal/read_triangle_mesh.h similarity index 100% rename from src/igl/copyleft/cgal/read_triangle_mesh.h rename to src/libigl/igl/copyleft/cgal/read_triangle_mesh.h diff --git a/src/igl/copyleft/cgal/relabel_small_immersed_cells.cpp b/src/libigl/igl/copyleft/cgal/relabel_small_immersed_cells.cpp similarity index 100% rename from src/igl/copyleft/cgal/relabel_small_immersed_cells.cpp rename to src/libigl/igl/copyleft/cgal/relabel_small_immersed_cells.cpp diff --git a/src/igl/copyleft/cgal/relabel_small_immersed_cells.h b/src/libigl/igl/copyleft/cgal/relabel_small_immersed_cells.h similarity index 100% rename from src/igl/copyleft/cgal/relabel_small_immersed_cells.h rename to src/libigl/igl/copyleft/cgal/relabel_small_immersed_cells.h diff --git a/src/igl/copyleft/cgal/remesh_intersections.cpp b/src/libigl/igl/copyleft/cgal/remesh_intersections.cpp similarity index 100% rename from src/igl/copyleft/cgal/remesh_intersections.cpp rename to src/libigl/igl/copyleft/cgal/remesh_intersections.cpp diff --git a/src/igl/copyleft/cgal/remesh_intersections.h b/src/libigl/igl/copyleft/cgal/remesh_intersections.h similarity index 100% rename from src/igl/copyleft/cgal/remesh_intersections.h rename to src/libigl/igl/copyleft/cgal/remesh_intersections.h diff --git a/src/igl/copyleft/cgal/remesh_self_intersections.cpp b/src/libigl/igl/copyleft/cgal/remesh_self_intersections.cpp similarity index 100% rename from src/igl/copyleft/cgal/remesh_self_intersections.cpp rename to src/libigl/igl/copyleft/cgal/remesh_self_intersections.cpp diff --git a/src/igl/copyleft/cgal/remesh_self_intersections.h b/src/libigl/igl/copyleft/cgal/remesh_self_intersections.h similarity index 100% rename from src/igl/copyleft/cgal/remesh_self_intersections.h rename to src/libigl/igl/copyleft/cgal/remesh_self_intersections.h diff --git a/src/igl/copyleft/cgal/remove_unreferenced.cpp b/src/libigl/igl/copyleft/cgal/remove_unreferenced.cpp similarity index 100% rename from src/igl/copyleft/cgal/remove_unreferenced.cpp rename to src/libigl/igl/copyleft/cgal/remove_unreferenced.cpp diff --git a/src/igl/copyleft/cgal/resolve_intersections.cpp b/src/libigl/igl/copyleft/cgal/resolve_intersections.cpp similarity index 100% rename from src/igl/copyleft/cgal/resolve_intersections.cpp rename to src/libigl/igl/copyleft/cgal/resolve_intersections.cpp diff --git a/src/igl/copyleft/cgal/resolve_intersections.h b/src/libigl/igl/copyleft/cgal/resolve_intersections.h similarity index 100% rename from src/igl/copyleft/cgal/resolve_intersections.h rename to src/libigl/igl/copyleft/cgal/resolve_intersections.h diff --git a/src/igl/copyleft/cgal/row_to_point.cpp b/src/libigl/igl/copyleft/cgal/row_to_point.cpp similarity index 100% rename from src/igl/copyleft/cgal/row_to_point.cpp rename to src/libigl/igl/copyleft/cgal/row_to_point.cpp diff --git a/src/igl/copyleft/cgal/row_to_point.h b/src/libigl/igl/copyleft/cgal/row_to_point.h similarity index 100% rename from src/igl/copyleft/cgal/row_to_point.h rename to src/libigl/igl/copyleft/cgal/row_to_point.h diff --git a/src/igl/copyleft/cgal/segment_segment_squared_distance.cpp b/src/libigl/igl/copyleft/cgal/segment_segment_squared_distance.cpp similarity index 100% rename from src/igl/copyleft/cgal/segment_segment_squared_distance.cpp rename to src/libigl/igl/copyleft/cgal/segment_segment_squared_distance.cpp diff --git a/src/igl/copyleft/cgal/segment_segment_squared_distance.h b/src/libigl/igl/copyleft/cgal/segment_segment_squared_distance.h similarity index 100% rename from src/igl/copyleft/cgal/segment_segment_squared_distance.h rename to src/libigl/igl/copyleft/cgal/segment_segment_squared_distance.h diff --git a/src/igl/copyleft/cgal/signed_distance_isosurface.cpp b/src/libigl/igl/copyleft/cgal/signed_distance_isosurface.cpp similarity index 100% rename from src/igl/copyleft/cgal/signed_distance_isosurface.cpp rename to src/libigl/igl/copyleft/cgal/signed_distance_isosurface.cpp diff --git a/src/igl/copyleft/cgal/signed_distance_isosurface.h b/src/libigl/igl/copyleft/cgal/signed_distance_isosurface.h similarity index 100% rename from src/igl/copyleft/cgal/signed_distance_isosurface.h rename to src/libigl/igl/copyleft/cgal/signed_distance_isosurface.h diff --git a/src/igl/copyleft/cgal/slice.cpp b/src/libigl/igl/copyleft/cgal/slice.cpp similarity index 100% rename from src/igl/copyleft/cgal/slice.cpp rename to src/libigl/igl/copyleft/cgal/slice.cpp diff --git a/src/igl/copyleft/cgal/slice_mask.cpp b/src/libigl/igl/copyleft/cgal/slice_mask.cpp similarity index 100% rename from src/igl/copyleft/cgal/slice_mask.cpp rename to src/libigl/igl/copyleft/cgal/slice_mask.cpp diff --git a/src/igl/copyleft/cgal/snap_rounding.cpp b/src/libigl/igl/copyleft/cgal/snap_rounding.cpp similarity index 100% rename from src/igl/copyleft/cgal/snap_rounding.cpp rename to src/libigl/igl/copyleft/cgal/snap_rounding.cpp diff --git a/src/igl/copyleft/cgal/snap_rounding.h b/src/libigl/igl/copyleft/cgal/snap_rounding.h similarity index 100% rename from src/igl/copyleft/cgal/snap_rounding.h rename to src/libigl/igl/copyleft/cgal/snap_rounding.h diff --git a/src/igl/copyleft/cgal/string_to_mesh_boolean_type.cpp b/src/libigl/igl/copyleft/cgal/string_to_mesh_boolean_type.cpp similarity index 100% rename from src/igl/copyleft/cgal/string_to_mesh_boolean_type.cpp rename to src/libigl/igl/copyleft/cgal/string_to_mesh_boolean_type.cpp diff --git a/src/igl/copyleft/cgal/string_to_mesh_boolean_type.h b/src/libigl/igl/copyleft/cgal/string_to_mesh_boolean_type.h similarity index 100% rename from src/igl/copyleft/cgal/string_to_mesh_boolean_type.h rename to src/libigl/igl/copyleft/cgal/string_to_mesh_boolean_type.h diff --git a/src/igl/copyleft/cgal/subdivide_segments.cpp b/src/libigl/igl/copyleft/cgal/subdivide_segments.cpp similarity index 100% rename from src/igl/copyleft/cgal/subdivide_segments.cpp rename to src/libigl/igl/copyleft/cgal/subdivide_segments.cpp diff --git a/src/igl/copyleft/cgal/subdivide_segments.h b/src/libigl/igl/copyleft/cgal/subdivide_segments.h similarity index 100% rename from src/igl/copyleft/cgal/subdivide_segments.h rename to src/libigl/igl/copyleft/cgal/subdivide_segments.h diff --git a/src/igl/copyleft/cgal/submesh_aabb_tree.cpp b/src/libigl/igl/copyleft/cgal/submesh_aabb_tree.cpp similarity index 100% rename from src/igl/copyleft/cgal/submesh_aabb_tree.cpp rename to src/libigl/igl/copyleft/cgal/submesh_aabb_tree.cpp diff --git a/src/igl/copyleft/cgal/submesh_aabb_tree.h b/src/libigl/igl/copyleft/cgal/submesh_aabb_tree.h similarity index 100% rename from src/igl/copyleft/cgal/submesh_aabb_tree.h rename to src/libigl/igl/copyleft/cgal/submesh_aabb_tree.h diff --git a/src/igl/copyleft/cgal/triangle_triangle_squared_distance.cpp b/src/libigl/igl/copyleft/cgal/triangle_triangle_squared_distance.cpp similarity index 100% rename from src/igl/copyleft/cgal/triangle_triangle_squared_distance.cpp rename to src/libigl/igl/copyleft/cgal/triangle_triangle_squared_distance.cpp diff --git a/src/igl/copyleft/cgal/triangle_triangle_squared_distance.h b/src/libigl/igl/copyleft/cgal/triangle_triangle_squared_distance.h similarity index 100% rename from src/igl/copyleft/cgal/triangle_triangle_squared_distance.h rename to src/libigl/igl/copyleft/cgal/triangle_triangle_squared_distance.h diff --git a/src/igl/copyleft/cgal/trim_with_solid.cpp b/src/libigl/igl/copyleft/cgal/trim_with_solid.cpp similarity index 100% rename from src/igl/copyleft/cgal/trim_with_solid.cpp rename to src/libigl/igl/copyleft/cgal/trim_with_solid.cpp diff --git a/src/igl/copyleft/cgal/trim_with_solid.h b/src/libigl/igl/copyleft/cgal/trim_with_solid.h similarity index 100% rename from src/igl/copyleft/cgal/trim_with_solid.h rename to src/libigl/igl/copyleft/cgal/trim_with_solid.h diff --git a/src/igl/copyleft/cgal/unique.cpp b/src/libigl/igl/copyleft/cgal/unique.cpp similarity index 100% rename from src/igl/copyleft/cgal/unique.cpp rename to src/libigl/igl/copyleft/cgal/unique.cpp diff --git a/src/igl/copyleft/cgal/unique_rows.cpp b/src/libigl/igl/copyleft/cgal/unique_rows.cpp similarity index 100% rename from src/igl/copyleft/cgal/unique_rows.cpp rename to src/libigl/igl/copyleft/cgal/unique_rows.cpp diff --git a/src/igl/copyleft/cgal/wire_mesh.cpp b/src/libigl/igl/copyleft/cgal/wire_mesh.cpp similarity index 100% rename from src/igl/copyleft/cgal/wire_mesh.cpp rename to src/libigl/igl/copyleft/cgal/wire_mesh.cpp diff --git a/src/igl/copyleft/cgal/wire_mesh.h b/src/libigl/igl/copyleft/cgal/wire_mesh.h similarity index 100% rename from src/igl/copyleft/cgal/wire_mesh.h rename to src/libigl/igl/copyleft/cgal/wire_mesh.h diff --git a/src/igl/copyleft/comiso/frame_field.cpp b/src/libigl/igl/copyleft/comiso/frame_field.cpp similarity index 100% rename from src/igl/copyleft/comiso/frame_field.cpp rename to src/libigl/igl/copyleft/comiso/frame_field.cpp diff --git a/src/igl/copyleft/comiso/frame_field.h b/src/libigl/igl/copyleft/comiso/frame_field.h similarity index 100% rename from src/igl/copyleft/comiso/frame_field.h rename to src/libigl/igl/copyleft/comiso/frame_field.h diff --git a/src/igl/copyleft/comiso/miq.cpp b/src/libigl/igl/copyleft/comiso/miq.cpp similarity index 100% rename from src/igl/copyleft/comiso/miq.cpp rename to src/libigl/igl/copyleft/comiso/miq.cpp diff --git a/src/igl/copyleft/comiso/miq.h b/src/libigl/igl/copyleft/comiso/miq.h similarity index 100% rename from src/igl/copyleft/comiso/miq.h rename to src/libigl/igl/copyleft/comiso/miq.h diff --git a/src/igl/copyleft/comiso/nrosy.cpp b/src/libigl/igl/copyleft/comiso/nrosy.cpp similarity index 100% rename from src/igl/copyleft/comiso/nrosy.cpp rename to src/libigl/igl/copyleft/comiso/nrosy.cpp diff --git a/src/igl/copyleft/comiso/nrosy.h b/src/libigl/igl/copyleft/comiso/nrosy.h similarity index 100% rename from src/igl/copyleft/comiso/nrosy.h rename to src/libigl/igl/copyleft/comiso/nrosy.h diff --git a/src/igl/copyleft/cork/from_cork_mesh.cpp b/src/libigl/igl/copyleft/cork/from_cork_mesh.cpp similarity index 100% rename from src/igl/copyleft/cork/from_cork_mesh.cpp rename to src/libigl/igl/copyleft/cork/from_cork_mesh.cpp diff --git a/src/igl/copyleft/cork/from_cork_mesh.h b/src/libigl/igl/copyleft/cork/from_cork_mesh.h similarity index 100% rename from src/igl/copyleft/cork/from_cork_mesh.h rename to src/libigl/igl/copyleft/cork/from_cork_mesh.h diff --git a/src/igl/copyleft/cork/mesh_boolean.cpp b/src/libigl/igl/copyleft/cork/mesh_boolean.cpp similarity index 100% rename from src/igl/copyleft/cork/mesh_boolean.cpp rename to src/libigl/igl/copyleft/cork/mesh_boolean.cpp diff --git a/src/igl/copyleft/cork/mesh_boolean.h b/src/libigl/igl/copyleft/cork/mesh_boolean.h similarity index 100% rename from src/igl/copyleft/cork/mesh_boolean.h rename to src/libigl/igl/copyleft/cork/mesh_boolean.h diff --git a/src/igl/copyleft/cork/to_cork_mesh.cpp b/src/libigl/igl/copyleft/cork/to_cork_mesh.cpp similarity index 100% rename from src/igl/copyleft/cork/to_cork_mesh.cpp rename to src/libigl/igl/copyleft/cork/to_cork_mesh.cpp diff --git a/src/igl/copyleft/cork/to_cork_mesh.h b/src/libigl/igl/copyleft/cork/to_cork_mesh.h similarity index 100% rename from src/igl/copyleft/cork/to_cork_mesh.h rename to src/libigl/igl/copyleft/cork/to_cork_mesh.h diff --git a/src/igl/copyleft/marching_cubes.cpp b/src/libigl/igl/copyleft/marching_cubes.cpp similarity index 100% rename from src/igl/copyleft/marching_cubes.cpp rename to src/libigl/igl/copyleft/marching_cubes.cpp diff --git a/src/igl/copyleft/marching_cubes.h b/src/libigl/igl/copyleft/marching_cubes.h similarity index 100% rename from src/igl/copyleft/marching_cubes.h rename to src/libigl/igl/copyleft/marching_cubes.h diff --git a/src/igl/copyleft/marching_cubes_tables.h b/src/libigl/igl/copyleft/marching_cubes_tables.h similarity index 100% rename from src/igl/copyleft/marching_cubes_tables.h rename to src/libigl/igl/copyleft/marching_cubes_tables.h diff --git a/src/igl/copyleft/offset_surface.cpp b/src/libigl/igl/copyleft/offset_surface.cpp similarity index 100% rename from src/igl/copyleft/offset_surface.cpp rename to src/libigl/igl/copyleft/offset_surface.cpp diff --git a/src/igl/copyleft/offset_surface.h b/src/libigl/igl/copyleft/offset_surface.h similarity index 100% rename from src/igl/copyleft/offset_surface.h rename to src/libigl/igl/copyleft/offset_surface.h diff --git a/src/igl/copyleft/opengl2/render_to_tga.cpp b/src/libigl/igl/copyleft/opengl2/render_to_tga.cpp similarity index 100% rename from src/igl/copyleft/opengl2/render_to_tga.cpp rename to src/libigl/igl/copyleft/opengl2/render_to_tga.cpp diff --git a/src/igl/copyleft/opengl2/render_to_tga.h b/src/libigl/igl/copyleft/opengl2/render_to_tga.h similarity index 100% rename from src/igl/copyleft/opengl2/render_to_tga.h rename to src/libigl/igl/copyleft/opengl2/render_to_tga.h diff --git a/src/igl/copyleft/opengl2/texture_from_tga.cpp b/src/libigl/igl/copyleft/opengl2/texture_from_tga.cpp similarity index 100% rename from src/igl/copyleft/opengl2/texture_from_tga.cpp rename to src/libigl/igl/copyleft/opengl2/texture_from_tga.cpp diff --git a/src/igl/copyleft/opengl2/texture_from_tga.h b/src/libigl/igl/copyleft/opengl2/texture_from_tga.h similarity index 100% rename from src/igl/copyleft/opengl2/texture_from_tga.h rename to src/libigl/igl/copyleft/opengl2/texture_from_tga.h diff --git a/src/igl/copyleft/opengl2/tga.cpp b/src/libigl/igl/copyleft/opengl2/tga.cpp similarity index 100% rename from src/igl/copyleft/opengl2/tga.cpp rename to src/libigl/igl/copyleft/opengl2/tga.cpp diff --git a/src/igl/copyleft/opengl2/tga.h b/src/libigl/igl/copyleft/opengl2/tga.h similarity index 100% rename from src/igl/copyleft/opengl2/tga.h rename to src/libigl/igl/copyleft/opengl2/tga.h diff --git a/src/igl/copyleft/progressive_hulls.cpp b/src/libigl/igl/copyleft/progressive_hulls.cpp similarity index 100% rename from src/igl/copyleft/progressive_hulls.cpp rename to src/libigl/igl/copyleft/progressive_hulls.cpp diff --git a/src/igl/copyleft/progressive_hulls.h b/src/libigl/igl/copyleft/progressive_hulls.h similarity index 100% rename from src/igl/copyleft/progressive_hulls.h rename to src/libigl/igl/copyleft/progressive_hulls.h diff --git a/src/igl/copyleft/progressive_hulls_cost_and_placement.cpp b/src/libigl/igl/copyleft/progressive_hulls_cost_and_placement.cpp similarity index 100% rename from src/igl/copyleft/progressive_hulls_cost_and_placement.cpp rename to src/libigl/igl/copyleft/progressive_hulls_cost_and_placement.cpp diff --git a/src/igl/copyleft/progressive_hulls_cost_and_placement.h b/src/libigl/igl/copyleft/progressive_hulls_cost_and_placement.h similarity index 100% rename from src/igl/copyleft/progressive_hulls_cost_and_placement.h rename to src/libigl/igl/copyleft/progressive_hulls_cost_and_placement.h diff --git a/src/igl/copyleft/quadprog.cpp b/src/libigl/igl/copyleft/quadprog.cpp similarity index 100% rename from src/igl/copyleft/quadprog.cpp rename to src/libigl/igl/copyleft/quadprog.cpp diff --git a/src/igl/copyleft/quadprog.h b/src/libigl/igl/copyleft/quadprog.h similarity index 100% rename from src/igl/copyleft/quadprog.h rename to src/libigl/igl/copyleft/quadprog.h diff --git a/src/igl/copyleft/swept_volume.cpp b/src/libigl/igl/copyleft/swept_volume.cpp similarity index 100% rename from src/igl/copyleft/swept_volume.cpp rename to src/libigl/igl/copyleft/swept_volume.cpp diff --git a/src/igl/copyleft/swept_volume.h b/src/libigl/igl/copyleft/swept_volume.h similarity index 100% rename from src/igl/copyleft/swept_volume.h rename to src/libigl/igl/copyleft/swept_volume.h diff --git a/src/igl/copyleft/tetgen/README b/src/libigl/igl/copyleft/tetgen/README similarity index 100% rename from src/igl/copyleft/tetgen/README rename to src/libigl/igl/copyleft/tetgen/README diff --git a/src/igl/copyleft/tetgen/cdt.cpp b/src/libigl/igl/copyleft/tetgen/cdt.cpp similarity index 100% rename from src/igl/copyleft/tetgen/cdt.cpp rename to src/libigl/igl/copyleft/tetgen/cdt.cpp diff --git a/src/igl/copyleft/tetgen/cdt.h b/src/libigl/igl/copyleft/tetgen/cdt.h similarity index 100% rename from src/igl/copyleft/tetgen/cdt.h rename to src/libigl/igl/copyleft/tetgen/cdt.h diff --git a/src/igl/copyleft/tetgen/mesh_to_tetgenio.cpp b/src/libigl/igl/copyleft/tetgen/mesh_to_tetgenio.cpp similarity index 100% rename from src/igl/copyleft/tetgen/mesh_to_tetgenio.cpp rename to src/libigl/igl/copyleft/tetgen/mesh_to_tetgenio.cpp diff --git a/src/igl/copyleft/tetgen/mesh_to_tetgenio.h b/src/libigl/igl/copyleft/tetgen/mesh_to_tetgenio.h similarity index 100% rename from src/igl/copyleft/tetgen/mesh_to_tetgenio.h rename to src/libigl/igl/copyleft/tetgen/mesh_to_tetgenio.h diff --git a/src/igl/copyleft/tetgen/mesh_with_skeleton.cpp b/src/libigl/igl/copyleft/tetgen/mesh_with_skeleton.cpp similarity index 100% rename from src/igl/copyleft/tetgen/mesh_with_skeleton.cpp rename to src/libigl/igl/copyleft/tetgen/mesh_with_skeleton.cpp diff --git a/src/igl/copyleft/tetgen/mesh_with_skeleton.h b/src/libigl/igl/copyleft/tetgen/mesh_with_skeleton.h similarity index 100% rename from src/igl/copyleft/tetgen/mesh_with_skeleton.h rename to src/libigl/igl/copyleft/tetgen/mesh_with_skeleton.h diff --git a/src/igl/copyleft/tetgen/read_into_tetgenio.cpp b/src/libigl/igl/copyleft/tetgen/read_into_tetgenio.cpp similarity index 100% rename from src/igl/copyleft/tetgen/read_into_tetgenio.cpp rename to src/libigl/igl/copyleft/tetgen/read_into_tetgenio.cpp diff --git a/src/igl/copyleft/tetgen/read_into_tetgenio.h b/src/libigl/igl/copyleft/tetgen/read_into_tetgenio.h similarity index 100% rename from src/igl/copyleft/tetgen/read_into_tetgenio.h rename to src/libigl/igl/copyleft/tetgen/read_into_tetgenio.h diff --git a/src/igl/copyleft/tetgen/tetgenio_to_tetmesh.cpp b/src/libigl/igl/copyleft/tetgen/tetgenio_to_tetmesh.cpp similarity index 100% rename from src/igl/copyleft/tetgen/tetgenio_to_tetmesh.cpp rename to src/libigl/igl/copyleft/tetgen/tetgenio_to_tetmesh.cpp diff --git a/src/igl/copyleft/tetgen/tetgenio_to_tetmesh.h b/src/libigl/igl/copyleft/tetgen/tetgenio_to_tetmesh.h similarity index 100% rename from src/igl/copyleft/tetgen/tetgenio_to_tetmesh.h rename to src/libigl/igl/copyleft/tetgen/tetgenio_to_tetmesh.h diff --git a/src/igl/copyleft/tetgen/tetrahedralize.cpp b/src/libigl/igl/copyleft/tetgen/tetrahedralize.cpp similarity index 100% rename from src/igl/copyleft/tetgen/tetrahedralize.cpp rename to src/libigl/igl/copyleft/tetgen/tetrahedralize.cpp diff --git a/src/igl/copyleft/tetgen/tetrahedralize.h b/src/libigl/igl/copyleft/tetgen/tetrahedralize.h similarity index 100% rename from src/igl/copyleft/tetgen/tetrahedralize.h rename to src/libigl/igl/copyleft/tetgen/tetrahedralize.h diff --git a/src/igl/cotmatrix.cpp b/src/libigl/igl/cotmatrix.cpp similarity index 100% rename from src/igl/cotmatrix.cpp rename to src/libigl/igl/cotmatrix.cpp diff --git a/src/igl/cotmatrix.h b/src/libigl/igl/cotmatrix.h similarity index 100% rename from src/igl/cotmatrix.h rename to src/libigl/igl/cotmatrix.h diff --git a/src/igl/cotmatrix_entries.cpp b/src/libigl/igl/cotmatrix_entries.cpp similarity index 100% rename from src/igl/cotmatrix_entries.cpp rename to src/libigl/igl/cotmatrix_entries.cpp diff --git a/src/igl/cotmatrix_entries.h b/src/libigl/igl/cotmatrix_entries.h similarity index 100% rename from src/igl/cotmatrix_entries.h rename to src/libigl/igl/cotmatrix_entries.h diff --git a/src/igl/count.cpp b/src/libigl/igl/count.cpp similarity index 100% rename from src/igl/count.cpp rename to src/libigl/igl/count.cpp diff --git a/src/igl/count.h b/src/libigl/igl/count.h similarity index 100% rename from src/igl/count.h rename to src/libigl/igl/count.h diff --git a/src/igl/covariance_scatter_matrix.cpp b/src/libigl/igl/covariance_scatter_matrix.cpp similarity index 100% rename from src/igl/covariance_scatter_matrix.cpp rename to src/libigl/igl/covariance_scatter_matrix.cpp diff --git a/src/igl/covariance_scatter_matrix.h b/src/libigl/igl/covariance_scatter_matrix.h similarity index 100% rename from src/igl/covariance_scatter_matrix.h rename to src/libigl/igl/covariance_scatter_matrix.h diff --git a/src/igl/cross.cpp b/src/libigl/igl/cross.cpp similarity index 100% rename from src/igl/cross.cpp rename to src/libigl/igl/cross.cpp diff --git a/src/igl/cross.h b/src/libigl/igl/cross.h similarity index 100% rename from src/igl/cross.h rename to src/libigl/igl/cross.h diff --git a/src/igl/cross_field_missmatch.cpp b/src/libigl/igl/cross_field_missmatch.cpp similarity index 100% rename from src/igl/cross_field_missmatch.cpp rename to src/libigl/igl/cross_field_missmatch.cpp diff --git a/src/igl/cross_field_missmatch.h b/src/libigl/igl/cross_field_missmatch.h similarity index 100% rename from src/igl/cross_field_missmatch.h rename to src/libigl/igl/cross_field_missmatch.h diff --git a/src/igl/crouzeix_raviart_cotmatrix.cpp b/src/libigl/igl/crouzeix_raviart_cotmatrix.cpp similarity index 100% rename from src/igl/crouzeix_raviart_cotmatrix.cpp rename to src/libigl/igl/crouzeix_raviart_cotmatrix.cpp diff --git a/src/igl/crouzeix_raviart_cotmatrix.h b/src/libigl/igl/crouzeix_raviart_cotmatrix.h similarity index 100% rename from src/igl/crouzeix_raviart_cotmatrix.h rename to src/libigl/igl/crouzeix_raviart_cotmatrix.h diff --git a/src/igl/crouzeix_raviart_massmatrix.cpp b/src/libigl/igl/crouzeix_raviart_massmatrix.cpp similarity index 100% rename from src/igl/crouzeix_raviart_massmatrix.cpp rename to src/libigl/igl/crouzeix_raviart_massmatrix.cpp diff --git a/src/igl/crouzeix_raviart_massmatrix.h b/src/libigl/igl/crouzeix_raviart_massmatrix.h similarity index 100% rename from src/igl/crouzeix_raviart_massmatrix.h rename to src/libigl/igl/crouzeix_raviart_massmatrix.h diff --git a/src/igl/cumsum.cpp b/src/libigl/igl/cumsum.cpp similarity index 100% rename from src/igl/cumsum.cpp rename to src/libigl/igl/cumsum.cpp diff --git a/src/igl/cumsum.h b/src/libigl/igl/cumsum.h similarity index 100% rename from src/igl/cumsum.h rename to src/libigl/igl/cumsum.h diff --git a/src/igl/cut_mesh.cpp b/src/libigl/igl/cut_mesh.cpp similarity index 100% rename from src/igl/cut_mesh.cpp rename to src/libigl/igl/cut_mesh.cpp diff --git a/src/igl/cut_mesh.h b/src/libigl/igl/cut_mesh.h similarity index 100% rename from src/igl/cut_mesh.h rename to src/libigl/igl/cut_mesh.h diff --git a/src/igl/cut_mesh_from_singularities.cpp b/src/libigl/igl/cut_mesh_from_singularities.cpp similarity index 100% rename from src/igl/cut_mesh_from_singularities.cpp rename to src/libigl/igl/cut_mesh_from_singularities.cpp diff --git a/src/igl/cut_mesh_from_singularities.h b/src/libigl/igl/cut_mesh_from_singularities.h similarity index 100% rename from src/igl/cut_mesh_from_singularities.h rename to src/libigl/igl/cut_mesh_from_singularities.h diff --git a/src/igl/cylinder.cpp b/src/libigl/igl/cylinder.cpp similarity index 100% rename from src/igl/cylinder.cpp rename to src/libigl/igl/cylinder.cpp diff --git a/src/igl/cylinder.h b/src/libigl/igl/cylinder.h similarity index 100% rename from src/igl/cylinder.h rename to src/libigl/igl/cylinder.h diff --git a/src/igl/dated_copy.cpp b/src/libigl/igl/dated_copy.cpp similarity index 100% rename from src/igl/dated_copy.cpp rename to src/libigl/igl/dated_copy.cpp diff --git a/src/igl/dated_copy.h b/src/libigl/igl/dated_copy.h similarity index 100% rename from src/igl/dated_copy.h rename to src/libigl/igl/dated_copy.h diff --git a/src/igl/decimate.cpp b/src/libigl/igl/decimate.cpp similarity index 100% rename from src/igl/decimate.cpp rename to src/libigl/igl/decimate.cpp diff --git a/src/igl/decimate.h b/src/libigl/igl/decimate.h similarity index 100% rename from src/igl/decimate.h rename to src/libigl/igl/decimate.h diff --git a/src/igl/deform_skeleton.cpp b/src/libigl/igl/deform_skeleton.cpp similarity index 100% rename from src/igl/deform_skeleton.cpp rename to src/libigl/igl/deform_skeleton.cpp diff --git a/src/igl/deform_skeleton.h b/src/libigl/igl/deform_skeleton.h similarity index 100% rename from src/igl/deform_skeleton.h rename to src/libigl/igl/deform_skeleton.h diff --git a/src/igl/delaunay_triangulation.cpp b/src/libigl/igl/delaunay_triangulation.cpp similarity index 100% rename from src/igl/delaunay_triangulation.cpp rename to src/libigl/igl/delaunay_triangulation.cpp diff --git a/src/igl/delaunay_triangulation.h b/src/libigl/igl/delaunay_triangulation.h similarity index 100% rename from src/igl/delaunay_triangulation.h rename to src/libigl/igl/delaunay_triangulation.h diff --git a/src/igl/deprecated.h b/src/libigl/igl/deprecated.h similarity index 100% rename from src/igl/deprecated.h rename to src/libigl/igl/deprecated.h diff --git a/src/igl/dfs.cpp b/src/libigl/igl/dfs.cpp similarity index 100% rename from src/igl/dfs.cpp rename to src/libigl/igl/dfs.cpp diff --git a/src/igl/dfs.h b/src/libigl/igl/dfs.h similarity index 100% rename from src/igl/dfs.h rename to src/libigl/igl/dfs.h diff --git a/src/igl/diag.cpp b/src/libigl/igl/diag.cpp similarity index 100% rename from src/igl/diag.cpp rename to src/libigl/igl/diag.cpp diff --git a/src/igl/diag.h b/src/libigl/igl/diag.h similarity index 100% rename from src/igl/diag.h rename to src/libigl/igl/diag.h diff --git a/src/igl/dihedral_angles.cpp b/src/libigl/igl/dihedral_angles.cpp similarity index 100% rename from src/igl/dihedral_angles.cpp rename to src/libigl/igl/dihedral_angles.cpp diff --git a/src/igl/dihedral_angles.h b/src/libigl/igl/dihedral_angles.h similarity index 100% rename from src/igl/dihedral_angles.h rename to src/libigl/igl/dihedral_angles.h diff --git a/src/igl/dijkstra.cpp b/src/libigl/igl/dijkstra.cpp similarity index 100% rename from src/igl/dijkstra.cpp rename to src/libigl/igl/dijkstra.cpp diff --git a/src/igl/dijkstra.h b/src/libigl/igl/dijkstra.h similarity index 100% rename from src/igl/dijkstra.h rename to src/libigl/igl/dijkstra.h diff --git a/src/igl/directed_edge_orientations.cpp b/src/libigl/igl/directed_edge_orientations.cpp similarity index 100% rename from src/igl/directed_edge_orientations.cpp rename to src/libigl/igl/directed_edge_orientations.cpp diff --git a/src/igl/directed_edge_orientations.h b/src/libigl/igl/directed_edge_orientations.h similarity index 100% rename from src/igl/directed_edge_orientations.h rename to src/libigl/igl/directed_edge_orientations.h diff --git a/src/igl/directed_edge_parents.cpp b/src/libigl/igl/directed_edge_parents.cpp similarity index 100% rename from src/igl/directed_edge_parents.cpp rename to src/libigl/igl/directed_edge_parents.cpp diff --git a/src/igl/directed_edge_parents.h b/src/libigl/igl/directed_edge_parents.h similarity index 100% rename from src/igl/directed_edge_parents.h rename to src/libigl/igl/directed_edge_parents.h diff --git a/src/igl/dirname.cpp b/src/libigl/igl/dirname.cpp similarity index 100% rename from src/igl/dirname.cpp rename to src/libigl/igl/dirname.cpp diff --git a/src/igl/dirname.h b/src/libigl/igl/dirname.h similarity index 100% rename from src/igl/dirname.h rename to src/libigl/igl/dirname.h diff --git a/src/igl/dot.cpp b/src/libigl/igl/dot.cpp similarity index 100% rename from src/igl/dot.cpp rename to src/libigl/igl/dot.cpp diff --git a/src/igl/dot.h b/src/libigl/igl/dot.h similarity index 100% rename from src/igl/dot.h rename to src/libigl/igl/dot.h diff --git a/src/igl/dot_row.cpp b/src/libigl/igl/dot_row.cpp similarity index 100% rename from src/igl/dot_row.cpp rename to src/libigl/igl/dot_row.cpp diff --git a/src/igl/dot_row.h b/src/libigl/igl/dot_row.h similarity index 100% rename from src/igl/dot_row.h rename to src/libigl/igl/dot_row.h diff --git a/src/igl/doublearea.cpp b/src/libigl/igl/doublearea.cpp similarity index 100% rename from src/igl/doublearea.cpp rename to src/libigl/igl/doublearea.cpp diff --git a/src/igl/doublearea.h b/src/libigl/igl/doublearea.h similarity index 100% rename from src/igl/doublearea.h rename to src/libigl/igl/doublearea.h diff --git a/src/igl/dqs.cpp b/src/libigl/igl/dqs.cpp similarity index 100% rename from src/igl/dqs.cpp rename to src/libigl/igl/dqs.cpp diff --git a/src/igl/dqs.h b/src/libigl/igl/dqs.h similarity index 100% rename from src/igl/dqs.h rename to src/libigl/igl/dqs.h diff --git a/src/igl/ears.cpp b/src/libigl/igl/ears.cpp similarity index 100% rename from src/igl/ears.cpp rename to src/libigl/igl/ears.cpp diff --git a/src/igl/ears.h b/src/libigl/igl/ears.h similarity index 100% rename from src/igl/ears.h rename to src/libigl/igl/ears.h diff --git a/src/igl/edge_collapse_is_valid.cpp b/src/libigl/igl/edge_collapse_is_valid.cpp similarity index 100% rename from src/igl/edge_collapse_is_valid.cpp rename to src/libigl/igl/edge_collapse_is_valid.cpp diff --git a/src/igl/edge_collapse_is_valid.h b/src/libigl/igl/edge_collapse_is_valid.h similarity index 100% rename from src/igl/edge_collapse_is_valid.h rename to src/libigl/igl/edge_collapse_is_valid.h diff --git a/src/igl/edge_flaps.cpp b/src/libigl/igl/edge_flaps.cpp similarity index 100% rename from src/igl/edge_flaps.cpp rename to src/libigl/igl/edge_flaps.cpp diff --git a/src/igl/edge_flaps.h b/src/libigl/igl/edge_flaps.h similarity index 100% rename from src/igl/edge_flaps.h rename to src/libigl/igl/edge_flaps.h diff --git a/src/igl/edge_lengths.cpp b/src/libigl/igl/edge_lengths.cpp similarity index 100% rename from src/igl/edge_lengths.cpp rename to src/libigl/igl/edge_lengths.cpp diff --git a/src/igl/edge_lengths.h b/src/libigl/igl/edge_lengths.h similarity index 100% rename from src/igl/edge_lengths.h rename to src/libigl/igl/edge_lengths.h diff --git a/src/igl/edge_topology.cpp b/src/libigl/igl/edge_topology.cpp similarity index 100% rename from src/igl/edge_topology.cpp rename to src/libigl/igl/edge_topology.cpp diff --git a/src/igl/edge_topology.h b/src/libigl/igl/edge_topology.h similarity index 100% rename from src/igl/edge_topology.h rename to src/libigl/igl/edge_topology.h diff --git a/src/igl/edges.cpp b/src/libigl/igl/edges.cpp similarity index 100% rename from src/igl/edges.cpp rename to src/libigl/igl/edges.cpp diff --git a/src/igl/edges.h b/src/libigl/igl/edges.h similarity index 100% rename from src/igl/edges.h rename to src/libigl/igl/edges.h diff --git a/src/igl/edges_to_path.cpp b/src/libigl/igl/edges_to_path.cpp similarity index 100% rename from src/igl/edges_to_path.cpp rename to src/libigl/igl/edges_to_path.cpp diff --git a/src/igl/edges_to_path.h b/src/libigl/igl/edges_to_path.h similarity index 100% rename from src/igl/edges_to_path.h rename to src/libigl/igl/edges_to_path.h diff --git a/src/igl/eigs.cpp b/src/libigl/igl/eigs.cpp similarity index 100% rename from src/igl/eigs.cpp rename to src/libigl/igl/eigs.cpp diff --git a/src/igl/eigs.h b/src/libigl/igl/eigs.h similarity index 100% rename from src/igl/eigs.h rename to src/libigl/igl/eigs.h diff --git a/src/igl/embree/EmbreeIntersector.h b/src/libigl/igl/embree/EmbreeIntersector.h similarity index 100% rename from src/igl/embree/EmbreeIntersector.h rename to src/libigl/igl/embree/EmbreeIntersector.h diff --git a/src/igl/embree/Embree_convenience.h b/src/libigl/igl/embree/Embree_convenience.h similarity index 100% rename from src/igl/embree/Embree_convenience.h rename to src/libigl/igl/embree/Embree_convenience.h diff --git a/src/igl/embree/ambient_occlusion.cpp b/src/libigl/igl/embree/ambient_occlusion.cpp similarity index 100% rename from src/igl/embree/ambient_occlusion.cpp rename to src/libigl/igl/embree/ambient_occlusion.cpp diff --git a/src/igl/embree/ambient_occlusion.h b/src/libigl/igl/embree/ambient_occlusion.h similarity index 100% rename from src/igl/embree/ambient_occlusion.h rename to src/libigl/igl/embree/ambient_occlusion.h diff --git a/src/igl/embree/bone_heat.cpp b/src/libigl/igl/embree/bone_heat.cpp similarity index 100% rename from src/igl/embree/bone_heat.cpp rename to src/libigl/igl/embree/bone_heat.cpp diff --git a/src/igl/embree/bone_heat.h b/src/libigl/igl/embree/bone_heat.h similarity index 100% rename from src/igl/embree/bone_heat.h rename to src/libigl/igl/embree/bone_heat.h diff --git a/src/igl/embree/bone_visible.cpp b/src/libigl/igl/embree/bone_visible.cpp similarity index 100% rename from src/igl/embree/bone_visible.cpp rename to src/libigl/igl/embree/bone_visible.cpp diff --git a/src/igl/embree/bone_visible.h b/src/libigl/igl/embree/bone_visible.h similarity index 100% rename from src/igl/embree/bone_visible.h rename to src/libigl/igl/embree/bone_visible.h diff --git a/src/igl/embree/embree2/rtcore.h b/src/libigl/igl/embree/embree2/rtcore.h similarity index 100% rename from src/igl/embree/embree2/rtcore.h rename to src/libigl/igl/embree/embree2/rtcore.h diff --git a/src/igl/embree/embree2/rtcore.isph b/src/libigl/igl/embree/embree2/rtcore.isph similarity index 100% rename from src/igl/embree/embree2/rtcore.isph rename to src/libigl/igl/embree/embree2/rtcore.isph diff --git a/src/igl/embree/embree2/rtcore_geometry.h b/src/libigl/igl/embree/embree2/rtcore_geometry.h similarity index 100% rename from src/igl/embree/embree2/rtcore_geometry.h rename to src/libigl/igl/embree/embree2/rtcore_geometry.h diff --git a/src/igl/embree/embree2/rtcore_geometry.isph b/src/libigl/igl/embree/embree2/rtcore_geometry.isph similarity index 100% rename from src/igl/embree/embree2/rtcore_geometry.isph rename to src/libigl/igl/embree/embree2/rtcore_geometry.isph diff --git a/src/igl/embree/embree2/rtcore_geometry_user.h b/src/libigl/igl/embree/embree2/rtcore_geometry_user.h similarity index 100% rename from src/igl/embree/embree2/rtcore_geometry_user.h rename to src/libigl/igl/embree/embree2/rtcore_geometry_user.h diff --git a/src/igl/embree/embree2/rtcore_geometry_user.isph b/src/libigl/igl/embree/embree2/rtcore_geometry_user.isph similarity index 100% rename from src/igl/embree/embree2/rtcore_geometry_user.isph rename to src/libigl/igl/embree/embree2/rtcore_geometry_user.isph diff --git a/src/igl/embree/embree2/rtcore_ray.h b/src/libigl/igl/embree/embree2/rtcore_ray.h similarity index 100% rename from src/igl/embree/embree2/rtcore_ray.h rename to src/libigl/igl/embree/embree2/rtcore_ray.h diff --git a/src/igl/embree/embree2/rtcore_ray.isph b/src/libigl/igl/embree/embree2/rtcore_ray.isph similarity index 100% rename from src/igl/embree/embree2/rtcore_ray.isph rename to src/libigl/igl/embree/embree2/rtcore_ray.isph diff --git a/src/igl/embree/embree2/rtcore_scene.h b/src/libigl/igl/embree/embree2/rtcore_scene.h similarity index 100% rename from src/igl/embree/embree2/rtcore_scene.h rename to src/libigl/igl/embree/embree2/rtcore_scene.h diff --git a/src/igl/embree/embree2/rtcore_scene.isph b/src/libigl/igl/embree/embree2/rtcore_scene.isph similarity index 100% rename from src/igl/embree/embree2/rtcore_scene.isph rename to src/libigl/igl/embree/embree2/rtcore_scene.isph diff --git a/src/igl/embree/line_mesh_intersection.cpp b/src/libigl/igl/embree/line_mesh_intersection.cpp similarity index 100% rename from src/igl/embree/line_mesh_intersection.cpp rename to src/libigl/igl/embree/line_mesh_intersection.cpp diff --git a/src/igl/embree/line_mesh_intersection.h b/src/libigl/igl/embree/line_mesh_intersection.h similarity index 100% rename from src/igl/embree/line_mesh_intersection.h rename to src/libigl/igl/embree/line_mesh_intersection.h diff --git a/src/igl/embree/reorient_facets_raycast.cpp b/src/libigl/igl/embree/reorient_facets_raycast.cpp similarity index 100% rename from src/igl/embree/reorient_facets_raycast.cpp rename to src/libigl/igl/embree/reorient_facets_raycast.cpp diff --git a/src/igl/embree/reorient_facets_raycast.h b/src/libigl/igl/embree/reorient_facets_raycast.h similarity index 100% rename from src/igl/embree/reorient_facets_raycast.h rename to src/libigl/igl/embree/reorient_facets_raycast.h diff --git a/src/igl/embree/shape_diameter_function.cpp b/src/libigl/igl/embree/shape_diameter_function.cpp similarity index 100% rename from src/igl/embree/shape_diameter_function.cpp rename to src/libigl/igl/embree/shape_diameter_function.cpp diff --git a/src/igl/embree/shape_diameter_function.h b/src/libigl/igl/embree/shape_diameter_function.h similarity index 100% rename from src/igl/embree/shape_diameter_function.h rename to src/libigl/igl/embree/shape_diameter_function.h diff --git a/src/igl/embree/unproject_in_mesh.cpp b/src/libigl/igl/embree/unproject_in_mesh.cpp similarity index 100% rename from src/igl/embree/unproject_in_mesh.cpp rename to src/libigl/igl/embree/unproject_in_mesh.cpp diff --git a/src/igl/embree/unproject_in_mesh.h b/src/libigl/igl/embree/unproject_in_mesh.h similarity index 100% rename from src/igl/embree/unproject_in_mesh.h rename to src/libigl/igl/embree/unproject_in_mesh.h diff --git a/src/igl/embree/unproject_onto_mesh.cpp b/src/libigl/igl/embree/unproject_onto_mesh.cpp similarity index 100% rename from src/igl/embree/unproject_onto_mesh.cpp rename to src/libigl/igl/embree/unproject_onto_mesh.cpp diff --git a/src/igl/embree/unproject_onto_mesh.h b/src/libigl/igl/embree/unproject_onto_mesh.h similarity index 100% rename from src/igl/embree/unproject_onto_mesh.h rename to src/libigl/igl/embree/unproject_onto_mesh.h diff --git a/src/igl/euler_characteristic.cpp b/src/libigl/igl/euler_characteristic.cpp similarity index 100% rename from src/igl/euler_characteristic.cpp rename to src/libigl/igl/euler_characteristic.cpp diff --git a/src/igl/euler_characteristic.h b/src/libigl/igl/euler_characteristic.h similarity index 100% rename from src/igl/euler_characteristic.h rename to src/libigl/igl/euler_characteristic.h diff --git a/src/igl/exact_geodesic.cpp b/src/libigl/igl/exact_geodesic.cpp similarity index 100% rename from src/igl/exact_geodesic.cpp rename to src/libigl/igl/exact_geodesic.cpp diff --git a/src/igl/exact_geodesic.h b/src/libigl/igl/exact_geodesic.h similarity index 100% rename from src/igl/exact_geodesic.h rename to src/libigl/igl/exact_geodesic.h diff --git a/src/igl/example_fun.cpp b/src/libigl/igl/example_fun.cpp similarity index 100% rename from src/igl/example_fun.cpp rename to src/libigl/igl/example_fun.cpp diff --git a/src/igl/example_fun.h b/src/libigl/igl/example_fun.h similarity index 100% rename from src/igl/example_fun.h rename to src/libigl/igl/example_fun.h diff --git a/src/igl/exterior_edges.cpp b/src/libigl/igl/exterior_edges.cpp similarity index 100% rename from src/igl/exterior_edges.cpp rename to src/libigl/igl/exterior_edges.cpp diff --git a/src/igl/exterior_edges.h b/src/libigl/igl/exterior_edges.h similarity index 100% rename from src/igl/exterior_edges.h rename to src/libigl/igl/exterior_edges.h diff --git a/src/igl/extract_manifold_patches.cpp b/src/libigl/igl/extract_manifold_patches.cpp similarity index 100% rename from src/igl/extract_manifold_patches.cpp rename to src/libigl/igl/extract_manifold_patches.cpp diff --git a/src/igl/extract_manifold_patches.h b/src/libigl/igl/extract_manifold_patches.h similarity index 100% rename from src/igl/extract_manifold_patches.h rename to src/libigl/igl/extract_manifold_patches.h diff --git a/src/igl/extract_non_manifold_edge_curves.cpp b/src/libigl/igl/extract_non_manifold_edge_curves.cpp similarity index 100% rename from src/igl/extract_non_manifold_edge_curves.cpp rename to src/libigl/igl/extract_non_manifold_edge_curves.cpp diff --git a/src/igl/extract_non_manifold_edge_curves.h b/src/libigl/igl/extract_non_manifold_edge_curves.h similarity index 100% rename from src/igl/extract_non_manifold_edge_curves.h rename to src/libigl/igl/extract_non_manifold_edge_curves.h diff --git a/src/igl/face_areas.cpp b/src/libigl/igl/face_areas.cpp similarity index 100% rename from src/igl/face_areas.cpp rename to src/libigl/igl/face_areas.cpp diff --git a/src/igl/face_areas.h b/src/libigl/igl/face_areas.h similarity index 100% rename from src/igl/face_areas.h rename to src/libigl/igl/face_areas.h diff --git a/src/igl/face_occurrences.cpp b/src/libigl/igl/face_occurrences.cpp similarity index 100% rename from src/igl/face_occurrences.cpp rename to src/libigl/igl/face_occurrences.cpp diff --git a/src/igl/face_occurrences.h b/src/libigl/igl/face_occurrences.h similarity index 100% rename from src/igl/face_occurrences.h rename to src/libigl/igl/face_occurrences.h diff --git a/src/igl/faces_first.cpp b/src/libigl/igl/faces_first.cpp similarity index 100% rename from src/igl/faces_first.cpp rename to src/libigl/igl/faces_first.cpp diff --git a/src/igl/faces_first.h b/src/libigl/igl/faces_first.h similarity index 100% rename from src/igl/faces_first.h rename to src/libigl/igl/faces_first.h diff --git a/src/igl/facet_components.cpp b/src/libigl/igl/facet_components.cpp similarity index 100% rename from src/igl/facet_components.cpp rename to src/libigl/igl/facet_components.cpp diff --git a/src/igl/facet_components.h b/src/libigl/igl/facet_components.h similarity index 100% rename from src/igl/facet_components.h rename to src/libigl/igl/facet_components.h diff --git a/src/igl/false_barycentric_subdivision.cpp b/src/libigl/igl/false_barycentric_subdivision.cpp similarity index 100% rename from src/igl/false_barycentric_subdivision.cpp rename to src/libigl/igl/false_barycentric_subdivision.cpp diff --git a/src/igl/false_barycentric_subdivision.h b/src/libigl/igl/false_barycentric_subdivision.h similarity index 100% rename from src/igl/false_barycentric_subdivision.h rename to src/libigl/igl/false_barycentric_subdivision.h diff --git a/src/igl/fast_winding_number.cpp b/src/libigl/igl/fast_winding_number.cpp similarity index 100% rename from src/igl/fast_winding_number.cpp rename to src/libigl/igl/fast_winding_number.cpp diff --git a/src/igl/fast_winding_number.h b/src/libigl/igl/fast_winding_number.h similarity index 100% rename from src/igl/fast_winding_number.h rename to src/libigl/igl/fast_winding_number.h diff --git a/src/igl/file_contents_as_string.cpp b/src/libigl/igl/file_contents_as_string.cpp similarity index 100% rename from src/igl/file_contents_as_string.cpp rename to src/libigl/igl/file_contents_as_string.cpp diff --git a/src/igl/file_contents_as_string.h b/src/libigl/igl/file_contents_as_string.h similarity index 100% rename from src/igl/file_contents_as_string.h rename to src/libigl/igl/file_contents_as_string.h diff --git a/src/igl/file_dialog_open.cpp b/src/libigl/igl/file_dialog_open.cpp similarity index 100% rename from src/igl/file_dialog_open.cpp rename to src/libigl/igl/file_dialog_open.cpp diff --git a/src/igl/file_dialog_open.h b/src/libigl/igl/file_dialog_open.h similarity index 100% rename from src/igl/file_dialog_open.h rename to src/libigl/igl/file_dialog_open.h diff --git a/src/igl/file_dialog_save.cpp b/src/libigl/igl/file_dialog_save.cpp similarity index 100% rename from src/igl/file_dialog_save.cpp rename to src/libigl/igl/file_dialog_save.cpp diff --git a/src/igl/file_dialog_save.h b/src/libigl/igl/file_dialog_save.h similarity index 100% rename from src/igl/file_dialog_save.h rename to src/libigl/igl/file_dialog_save.h diff --git a/src/igl/file_exists.cpp b/src/libigl/igl/file_exists.cpp similarity index 100% rename from src/igl/file_exists.cpp rename to src/libigl/igl/file_exists.cpp diff --git a/src/igl/file_exists.h b/src/libigl/igl/file_exists.h similarity index 100% rename from src/igl/file_exists.h rename to src/libigl/igl/file_exists.h diff --git a/src/igl/find.cpp b/src/libigl/igl/find.cpp similarity index 100% rename from src/igl/find.cpp rename to src/libigl/igl/find.cpp diff --git a/src/igl/find.h b/src/libigl/igl/find.h similarity index 100% rename from src/igl/find.h rename to src/libigl/igl/find.h diff --git a/src/igl/find_cross_field_singularities.cpp b/src/libigl/igl/find_cross_field_singularities.cpp similarity index 100% rename from src/igl/find_cross_field_singularities.cpp rename to src/libigl/igl/find_cross_field_singularities.cpp diff --git a/src/igl/find_cross_field_singularities.h b/src/libigl/igl/find_cross_field_singularities.h similarity index 100% rename from src/igl/find_cross_field_singularities.h rename to src/libigl/igl/find_cross_field_singularities.h diff --git a/src/igl/find_zero.cpp b/src/libigl/igl/find_zero.cpp similarity index 100% rename from src/igl/find_zero.cpp rename to src/libigl/igl/find_zero.cpp diff --git a/src/igl/find_zero.h b/src/libigl/igl/find_zero.h similarity index 100% rename from src/igl/find_zero.h rename to src/libigl/igl/find_zero.h diff --git a/src/igl/fit_plane.cpp b/src/libigl/igl/fit_plane.cpp similarity index 100% rename from src/igl/fit_plane.cpp rename to src/libigl/igl/fit_plane.cpp diff --git a/src/igl/fit_plane.h b/src/libigl/igl/fit_plane.h similarity index 100% rename from src/igl/fit_plane.h rename to src/libigl/igl/fit_plane.h diff --git a/src/igl/fit_rotations.cpp b/src/libigl/igl/fit_rotations.cpp similarity index 100% rename from src/igl/fit_rotations.cpp rename to src/libigl/igl/fit_rotations.cpp diff --git a/src/igl/fit_rotations.h b/src/libigl/igl/fit_rotations.h similarity index 100% rename from src/igl/fit_rotations.h rename to src/libigl/igl/fit_rotations.h diff --git a/src/igl/flip_avoiding_line_search.cpp b/src/libigl/igl/flip_avoiding_line_search.cpp similarity index 100% rename from src/igl/flip_avoiding_line_search.cpp rename to src/libigl/igl/flip_avoiding_line_search.cpp diff --git a/src/igl/flip_avoiding_line_search.h b/src/libigl/igl/flip_avoiding_line_search.h similarity index 100% rename from src/igl/flip_avoiding_line_search.h rename to src/libigl/igl/flip_avoiding_line_search.h diff --git a/src/igl/flip_edge.cpp b/src/libigl/igl/flip_edge.cpp similarity index 100% rename from src/igl/flip_edge.cpp rename to src/libigl/igl/flip_edge.cpp diff --git a/src/igl/flip_edge.h b/src/libigl/igl/flip_edge.h similarity index 100% rename from src/igl/flip_edge.h rename to src/libigl/igl/flip_edge.h diff --git a/src/igl/flipped_triangles.cpp b/src/libigl/igl/flipped_triangles.cpp similarity index 100% rename from src/igl/flipped_triangles.cpp rename to src/libigl/igl/flipped_triangles.cpp diff --git a/src/igl/flipped_triangles.h b/src/libigl/igl/flipped_triangles.h similarity index 100% rename from src/igl/flipped_triangles.h rename to src/libigl/igl/flipped_triangles.h diff --git a/src/igl/flood_fill.cpp b/src/libigl/igl/flood_fill.cpp similarity index 100% rename from src/igl/flood_fill.cpp rename to src/libigl/igl/flood_fill.cpp diff --git a/src/igl/flood_fill.h b/src/libigl/igl/flood_fill.h similarity index 100% rename from src/igl/flood_fill.h rename to src/libigl/igl/flood_fill.h diff --git a/src/igl/floor.cpp b/src/libigl/igl/floor.cpp similarity index 100% rename from src/igl/floor.cpp rename to src/libigl/igl/floor.cpp diff --git a/src/igl/floor.h b/src/libigl/igl/floor.h similarity index 100% rename from src/igl/floor.h rename to src/libigl/igl/floor.h diff --git a/src/igl/for_each.h b/src/libigl/igl/for_each.h similarity index 100% rename from src/igl/for_each.h rename to src/libigl/igl/for_each.h diff --git a/src/igl/forward_kinematics.cpp b/src/libigl/igl/forward_kinematics.cpp similarity index 100% rename from src/igl/forward_kinematics.cpp rename to src/libigl/igl/forward_kinematics.cpp diff --git a/src/igl/forward_kinematics.h b/src/libigl/igl/forward_kinematics.h similarity index 100% rename from src/igl/forward_kinematics.h rename to src/libigl/igl/forward_kinematics.h diff --git a/src/igl/frame_field_deformer.cpp b/src/libigl/igl/frame_field_deformer.cpp similarity index 100% rename from src/igl/frame_field_deformer.cpp rename to src/libigl/igl/frame_field_deformer.cpp diff --git a/src/igl/frame_field_deformer.h b/src/libigl/igl/frame_field_deformer.h similarity index 100% rename from src/igl/frame_field_deformer.h rename to src/libigl/igl/frame_field_deformer.h diff --git a/src/igl/frame_to_cross_field.cpp b/src/libigl/igl/frame_to_cross_field.cpp similarity index 100% rename from src/igl/frame_to_cross_field.cpp rename to src/libigl/igl/frame_to_cross_field.cpp diff --git a/src/igl/frame_to_cross_field.h b/src/libigl/igl/frame_to_cross_field.h similarity index 100% rename from src/igl/frame_to_cross_field.h rename to src/libigl/igl/frame_to_cross_field.h diff --git a/src/igl/frustum.cpp b/src/libigl/igl/frustum.cpp similarity index 100% rename from src/igl/frustum.cpp rename to src/libigl/igl/frustum.cpp diff --git a/src/igl/frustum.h b/src/libigl/igl/frustum.h similarity index 100% rename from src/igl/frustum.h rename to src/libigl/igl/frustum.h diff --git a/src/igl/gaussian_curvature.cpp b/src/libigl/igl/gaussian_curvature.cpp similarity index 100% rename from src/igl/gaussian_curvature.cpp rename to src/libigl/igl/gaussian_curvature.cpp diff --git a/src/igl/gaussian_curvature.h b/src/libigl/igl/gaussian_curvature.h similarity index 100% rename from src/igl/gaussian_curvature.h rename to src/libigl/igl/gaussian_curvature.h diff --git a/src/igl/get_seconds.cpp b/src/libigl/igl/get_seconds.cpp similarity index 100% rename from src/igl/get_seconds.cpp rename to src/libigl/igl/get_seconds.cpp diff --git a/src/igl/get_seconds.h b/src/libigl/igl/get_seconds.h similarity index 100% rename from src/igl/get_seconds.h rename to src/libigl/igl/get_seconds.h diff --git a/src/igl/get_seconds_hires.cpp b/src/libigl/igl/get_seconds_hires.cpp similarity index 100% rename from src/igl/get_seconds_hires.cpp rename to src/libigl/igl/get_seconds_hires.cpp diff --git a/src/igl/get_seconds_hires.h b/src/libigl/igl/get_seconds_hires.h similarity index 100% rename from src/igl/get_seconds_hires.h rename to src/libigl/igl/get_seconds_hires.h diff --git a/src/igl/grad.cpp b/src/libigl/igl/grad.cpp similarity index 100% rename from src/igl/grad.cpp rename to src/libigl/igl/grad.cpp diff --git a/src/igl/grad.h b/src/libigl/igl/grad.h similarity index 100% rename from src/igl/grad.h rename to src/libigl/igl/grad.h diff --git a/src/igl/grid.cpp b/src/libigl/igl/grid.cpp similarity index 100% rename from src/igl/grid.cpp rename to src/libigl/igl/grid.cpp diff --git a/src/igl/grid.h b/src/libigl/igl/grid.h similarity index 100% rename from src/igl/grid.h rename to src/libigl/igl/grid.h diff --git a/src/igl/grid_search.cpp b/src/libigl/igl/grid_search.cpp similarity index 100% rename from src/igl/grid_search.cpp rename to src/libigl/igl/grid_search.cpp diff --git a/src/igl/grid_search.h b/src/libigl/igl/grid_search.h similarity index 100% rename from src/igl/grid_search.h rename to src/libigl/igl/grid_search.h diff --git a/src/igl/group_sum_matrix.cpp b/src/libigl/igl/group_sum_matrix.cpp similarity index 100% rename from src/igl/group_sum_matrix.cpp rename to src/libigl/igl/group_sum_matrix.cpp diff --git a/src/igl/group_sum_matrix.h b/src/libigl/igl/group_sum_matrix.h similarity index 100% rename from src/igl/group_sum_matrix.h rename to src/libigl/igl/group_sum_matrix.h diff --git a/src/igl/guess_extension.cpp b/src/libigl/igl/guess_extension.cpp similarity index 100% rename from src/igl/guess_extension.cpp rename to src/libigl/igl/guess_extension.cpp diff --git a/src/igl/guess_extension.h b/src/libigl/igl/guess_extension.h similarity index 100% rename from src/igl/guess_extension.h rename to src/libigl/igl/guess_extension.h diff --git a/src/igl/harmonic.cpp b/src/libigl/igl/harmonic.cpp similarity index 100% rename from src/igl/harmonic.cpp rename to src/libigl/igl/harmonic.cpp diff --git a/src/igl/harmonic.h b/src/libigl/igl/harmonic.h similarity index 100% rename from src/igl/harmonic.h rename to src/libigl/igl/harmonic.h diff --git a/src/igl/harwell_boeing.cpp b/src/libigl/igl/harwell_boeing.cpp similarity index 100% rename from src/igl/harwell_boeing.cpp rename to src/libigl/igl/harwell_boeing.cpp diff --git a/src/igl/harwell_boeing.h b/src/libigl/igl/harwell_boeing.h similarity index 100% rename from src/igl/harwell_boeing.h rename to src/libigl/igl/harwell_boeing.h diff --git a/src/igl/hausdorff.cpp b/src/libigl/igl/hausdorff.cpp similarity index 100% rename from src/igl/hausdorff.cpp rename to src/libigl/igl/hausdorff.cpp diff --git a/src/igl/hausdorff.h b/src/libigl/igl/hausdorff.h similarity index 100% rename from src/igl/hausdorff.h rename to src/libigl/igl/hausdorff.h diff --git a/src/igl/hessian.cpp b/src/libigl/igl/hessian.cpp similarity index 100% rename from src/igl/hessian.cpp rename to src/libigl/igl/hessian.cpp diff --git a/src/igl/hessian.h b/src/libigl/igl/hessian.h similarity index 100% rename from src/igl/hessian.h rename to src/libigl/igl/hessian.h diff --git a/src/igl/hessian_energy.cpp b/src/libigl/igl/hessian_energy.cpp similarity index 100% rename from src/igl/hessian_energy.cpp rename to src/libigl/igl/hessian_energy.cpp diff --git a/src/igl/hessian_energy.h b/src/libigl/igl/hessian_energy.h similarity index 100% rename from src/igl/hessian_energy.h rename to src/libigl/igl/hessian_energy.h diff --git a/src/igl/histc.cpp b/src/libigl/igl/histc.cpp similarity index 100% rename from src/igl/histc.cpp rename to src/libigl/igl/histc.cpp diff --git a/src/igl/histc.h b/src/libigl/igl/histc.h similarity index 100% rename from src/igl/histc.h rename to src/libigl/igl/histc.h diff --git a/src/igl/hsv_to_rgb.cpp b/src/libigl/igl/hsv_to_rgb.cpp similarity index 100% rename from src/igl/hsv_to_rgb.cpp rename to src/libigl/igl/hsv_to_rgb.cpp diff --git a/src/igl/hsv_to_rgb.h b/src/libigl/igl/hsv_to_rgb.h similarity index 100% rename from src/igl/hsv_to_rgb.h rename to src/libigl/igl/hsv_to_rgb.h diff --git a/src/igl/igl_inline.h b/src/libigl/igl/igl_inline.h similarity index 100% rename from src/igl/igl_inline.h rename to src/libigl/igl/igl_inline.h diff --git a/src/igl/in_element.cpp b/src/libigl/igl/in_element.cpp similarity index 100% rename from src/igl/in_element.cpp rename to src/libigl/igl/in_element.cpp diff --git a/src/igl/in_element.h b/src/libigl/igl/in_element.h similarity index 100% rename from src/igl/in_element.h rename to src/libigl/igl/in_element.h diff --git a/src/igl/infinite_cost_stopping_condition.cpp b/src/libigl/igl/infinite_cost_stopping_condition.cpp similarity index 100% rename from src/igl/infinite_cost_stopping_condition.cpp rename to src/libigl/igl/infinite_cost_stopping_condition.cpp diff --git a/src/igl/infinite_cost_stopping_condition.h b/src/libigl/igl/infinite_cost_stopping_condition.h similarity index 100% rename from src/igl/infinite_cost_stopping_condition.h rename to src/libigl/igl/infinite_cost_stopping_condition.h diff --git a/src/igl/inradius.cpp b/src/libigl/igl/inradius.cpp similarity index 100% rename from src/igl/inradius.cpp rename to src/libigl/igl/inradius.cpp diff --git a/src/igl/inradius.h b/src/libigl/igl/inradius.h similarity index 100% rename from src/igl/inradius.h rename to src/libigl/igl/inradius.h diff --git a/src/igl/internal_angles.cpp b/src/libigl/igl/internal_angles.cpp similarity index 100% rename from src/igl/internal_angles.cpp rename to src/libigl/igl/internal_angles.cpp diff --git a/src/igl/internal_angles.h b/src/libigl/igl/internal_angles.h similarity index 100% rename from src/igl/internal_angles.h rename to src/libigl/igl/internal_angles.h diff --git a/src/igl/intersect.cpp b/src/libigl/igl/intersect.cpp similarity index 100% rename from src/igl/intersect.cpp rename to src/libigl/igl/intersect.cpp diff --git a/src/igl/intersect.h b/src/libigl/igl/intersect.h similarity index 100% rename from src/igl/intersect.h rename to src/libigl/igl/intersect.h diff --git a/src/igl/invert_diag.cpp b/src/libigl/igl/invert_diag.cpp similarity index 100% rename from src/igl/invert_diag.cpp rename to src/libigl/igl/invert_diag.cpp diff --git a/src/igl/invert_diag.h b/src/libigl/igl/invert_diag.h similarity index 100% rename from src/igl/invert_diag.h rename to src/libigl/igl/invert_diag.h diff --git a/src/igl/is_border_vertex.cpp b/src/libigl/igl/is_border_vertex.cpp similarity index 100% rename from src/igl/is_border_vertex.cpp rename to src/libigl/igl/is_border_vertex.cpp diff --git a/src/igl/is_border_vertex.h b/src/libigl/igl/is_border_vertex.h similarity index 100% rename from src/igl/is_border_vertex.h rename to src/libigl/igl/is_border_vertex.h diff --git a/src/igl/is_boundary_edge.cpp b/src/libigl/igl/is_boundary_edge.cpp similarity index 100% rename from src/igl/is_boundary_edge.cpp rename to src/libigl/igl/is_boundary_edge.cpp diff --git a/src/igl/is_boundary_edge.h b/src/libigl/igl/is_boundary_edge.h similarity index 100% rename from src/igl/is_boundary_edge.h rename to src/libigl/igl/is_boundary_edge.h diff --git a/src/igl/is_dir.cpp b/src/libigl/igl/is_dir.cpp similarity index 100% rename from src/igl/is_dir.cpp rename to src/libigl/igl/is_dir.cpp diff --git a/src/igl/is_dir.h b/src/libigl/igl/is_dir.h similarity index 100% rename from src/igl/is_dir.h rename to src/libigl/igl/is_dir.h diff --git a/src/igl/is_edge_manifold.cpp b/src/libigl/igl/is_edge_manifold.cpp similarity index 100% rename from src/igl/is_edge_manifold.cpp rename to src/libigl/igl/is_edge_manifold.cpp diff --git a/src/igl/is_edge_manifold.h b/src/libigl/igl/is_edge_manifold.h similarity index 100% rename from src/igl/is_edge_manifold.h rename to src/libigl/igl/is_edge_manifold.h diff --git a/src/igl/is_file.cpp b/src/libigl/igl/is_file.cpp similarity index 100% rename from src/igl/is_file.cpp rename to src/libigl/igl/is_file.cpp diff --git a/src/igl/is_file.h b/src/libigl/igl/is_file.h similarity index 100% rename from src/igl/is_file.h rename to src/libigl/igl/is_file.h diff --git a/src/igl/is_irregular_vertex.cpp b/src/libigl/igl/is_irregular_vertex.cpp similarity index 100% rename from src/igl/is_irregular_vertex.cpp rename to src/libigl/igl/is_irregular_vertex.cpp diff --git a/src/igl/is_irregular_vertex.h b/src/libigl/igl/is_irregular_vertex.h similarity index 100% rename from src/igl/is_irregular_vertex.h rename to src/libigl/igl/is_irregular_vertex.h diff --git a/src/igl/is_planar.cpp b/src/libigl/igl/is_planar.cpp similarity index 100% rename from src/igl/is_planar.cpp rename to src/libigl/igl/is_planar.cpp diff --git a/src/igl/is_planar.h b/src/libigl/igl/is_planar.h similarity index 100% rename from src/igl/is_planar.h rename to src/libigl/igl/is_planar.h diff --git a/src/igl/is_readable.cpp b/src/libigl/igl/is_readable.cpp similarity index 100% rename from src/igl/is_readable.cpp rename to src/libigl/igl/is_readable.cpp diff --git a/src/igl/is_readable.h b/src/libigl/igl/is_readable.h similarity index 100% rename from src/igl/is_readable.h rename to src/libigl/igl/is_readable.h diff --git a/src/igl/is_sparse.cpp b/src/libigl/igl/is_sparse.cpp similarity index 100% rename from src/igl/is_sparse.cpp rename to src/libigl/igl/is_sparse.cpp diff --git a/src/igl/is_sparse.h b/src/libigl/igl/is_sparse.h similarity index 100% rename from src/igl/is_sparse.h rename to src/libigl/igl/is_sparse.h diff --git a/src/igl/is_stl.cpp b/src/libigl/igl/is_stl.cpp similarity index 100% rename from src/igl/is_stl.cpp rename to src/libigl/igl/is_stl.cpp diff --git a/src/igl/is_stl.h b/src/libigl/igl/is_stl.h similarity index 100% rename from src/igl/is_stl.h rename to src/libigl/igl/is_stl.h diff --git a/src/igl/is_symmetric.cpp b/src/libigl/igl/is_symmetric.cpp similarity index 100% rename from src/igl/is_symmetric.cpp rename to src/libigl/igl/is_symmetric.cpp diff --git a/src/igl/is_symmetric.h b/src/libigl/igl/is_symmetric.h similarity index 100% rename from src/igl/is_symmetric.h rename to src/libigl/igl/is_symmetric.h diff --git a/src/igl/is_vertex_manifold.cpp b/src/libigl/igl/is_vertex_manifold.cpp similarity index 100% rename from src/igl/is_vertex_manifold.cpp rename to src/libigl/igl/is_vertex_manifold.cpp diff --git a/src/igl/is_vertex_manifold.h b/src/libigl/igl/is_vertex_manifold.h similarity index 100% rename from src/igl/is_vertex_manifold.h rename to src/libigl/igl/is_vertex_manifold.h diff --git a/src/igl/is_writable.cpp b/src/libigl/igl/is_writable.cpp similarity index 100% rename from src/igl/is_writable.cpp rename to src/libigl/igl/is_writable.cpp diff --git a/src/igl/is_writable.h b/src/libigl/igl/is_writable.h similarity index 100% rename from src/igl/is_writable.h rename to src/libigl/igl/is_writable.h diff --git a/src/igl/isdiag.cpp b/src/libigl/igl/isdiag.cpp similarity index 100% rename from src/igl/isdiag.cpp rename to src/libigl/igl/isdiag.cpp diff --git a/src/igl/isdiag.h b/src/libigl/igl/isdiag.h similarity index 100% rename from src/igl/isdiag.h rename to src/libigl/igl/isdiag.h diff --git a/src/igl/ismember.cpp b/src/libigl/igl/ismember.cpp similarity index 100% rename from src/igl/ismember.cpp rename to src/libigl/igl/ismember.cpp diff --git a/src/igl/ismember.h b/src/libigl/igl/ismember.h similarity index 100% rename from src/igl/ismember.h rename to src/libigl/igl/ismember.h diff --git a/src/igl/isolines.cpp b/src/libigl/igl/isolines.cpp similarity index 100% rename from src/igl/isolines.cpp rename to src/libigl/igl/isolines.cpp diff --git a/src/igl/isolines.h b/src/libigl/igl/isolines.h similarity index 100% rename from src/igl/isolines.h rename to src/libigl/igl/isolines.h diff --git a/src/igl/jet.cpp b/src/libigl/igl/jet.cpp similarity index 100% rename from src/igl/jet.cpp rename to src/libigl/igl/jet.cpp diff --git a/src/igl/jet.h b/src/libigl/igl/jet.h similarity index 100% rename from src/igl/jet.h rename to src/libigl/igl/jet.h diff --git a/src/igl/knn.cpp b/src/libigl/igl/knn.cpp similarity index 100% rename from src/igl/knn.cpp rename to src/libigl/igl/knn.cpp diff --git a/src/igl/knn.h b/src/libigl/igl/knn.h similarity index 100% rename from src/igl/knn.h rename to src/libigl/igl/knn.h diff --git a/src/igl/launch_medit.cpp b/src/libigl/igl/launch_medit.cpp similarity index 100% rename from src/igl/launch_medit.cpp rename to src/libigl/igl/launch_medit.cpp diff --git a/src/igl/launch_medit.h b/src/libigl/igl/launch_medit.h similarity index 100% rename from src/igl/launch_medit.h rename to src/libigl/igl/launch_medit.h diff --git a/src/igl/lbs_matrix.cpp b/src/libigl/igl/lbs_matrix.cpp similarity index 100% rename from src/igl/lbs_matrix.cpp rename to src/libigl/igl/lbs_matrix.cpp diff --git a/src/igl/lbs_matrix.h b/src/libigl/igl/lbs_matrix.h similarity index 100% rename from src/igl/lbs_matrix.h rename to src/libigl/igl/lbs_matrix.h diff --git a/src/igl/lexicographic_triangulation.cpp b/src/libigl/igl/lexicographic_triangulation.cpp similarity index 100% rename from src/igl/lexicographic_triangulation.cpp rename to src/libigl/igl/lexicographic_triangulation.cpp diff --git a/src/igl/lexicographic_triangulation.h b/src/libigl/igl/lexicographic_triangulation.h similarity index 100% rename from src/igl/lexicographic_triangulation.h rename to src/libigl/igl/lexicographic_triangulation.h diff --git a/src/igl/lim/lim.cpp b/src/libigl/igl/lim/lim.cpp similarity index 100% rename from src/igl/lim/lim.cpp rename to src/libigl/igl/lim/lim.cpp diff --git a/src/igl/lim/lim.h b/src/libigl/igl/lim/lim.h similarity index 100% rename from src/igl/lim/lim.h rename to src/libigl/igl/lim/lim.h diff --git a/src/igl/limit_faces.cpp b/src/libigl/igl/limit_faces.cpp similarity index 100% rename from src/igl/limit_faces.cpp rename to src/libigl/igl/limit_faces.cpp diff --git a/src/igl/limit_faces.h b/src/libigl/igl/limit_faces.h similarity index 100% rename from src/igl/limit_faces.h rename to src/libigl/igl/limit_faces.h diff --git a/src/igl/line_field_missmatch.cpp b/src/libigl/igl/line_field_missmatch.cpp similarity index 100% rename from src/igl/line_field_missmatch.cpp rename to src/libigl/igl/line_field_missmatch.cpp diff --git a/src/igl/line_field_missmatch.h b/src/libigl/igl/line_field_missmatch.h similarity index 100% rename from src/igl/line_field_missmatch.h rename to src/libigl/igl/line_field_missmatch.h diff --git a/src/igl/line_search.cpp b/src/libigl/igl/line_search.cpp similarity index 100% rename from src/igl/line_search.cpp rename to src/libigl/igl/line_search.cpp diff --git a/src/igl/line_search.h b/src/libigl/igl/line_search.h similarity index 100% rename from src/igl/line_search.h rename to src/libigl/igl/line_search.h diff --git a/src/igl/line_segment_in_rectangle.cpp b/src/libigl/igl/line_segment_in_rectangle.cpp similarity index 100% rename from src/igl/line_segment_in_rectangle.cpp rename to src/libigl/igl/line_segment_in_rectangle.cpp diff --git a/src/igl/line_segment_in_rectangle.h b/src/libigl/igl/line_segment_in_rectangle.h similarity index 100% rename from src/igl/line_segment_in_rectangle.h rename to src/libigl/igl/line_segment_in_rectangle.h diff --git a/src/igl/linprog.cpp b/src/libigl/igl/linprog.cpp similarity index 100% rename from src/igl/linprog.cpp rename to src/libigl/igl/linprog.cpp diff --git a/src/igl/linprog.h b/src/libigl/igl/linprog.h similarity index 100% rename from src/igl/linprog.h rename to src/libigl/igl/linprog.h diff --git a/src/igl/list_to_matrix.cpp b/src/libigl/igl/list_to_matrix.cpp similarity index 100% rename from src/igl/list_to_matrix.cpp rename to src/libigl/igl/list_to_matrix.cpp diff --git a/src/igl/list_to_matrix.h b/src/libigl/igl/list_to_matrix.h similarity index 100% rename from src/igl/list_to_matrix.h rename to src/libigl/igl/list_to_matrix.h diff --git a/src/igl/local_basis.cpp b/src/libigl/igl/local_basis.cpp similarity index 100% rename from src/igl/local_basis.cpp rename to src/libigl/igl/local_basis.cpp diff --git a/src/igl/local_basis.h b/src/libigl/igl/local_basis.h similarity index 100% rename from src/igl/local_basis.h rename to src/libigl/igl/local_basis.h diff --git a/src/igl/look_at.cpp b/src/libigl/igl/look_at.cpp similarity index 100% rename from src/igl/look_at.cpp rename to src/libigl/igl/look_at.cpp diff --git a/src/igl/look_at.h b/src/libigl/igl/look_at.h similarity index 100% rename from src/igl/look_at.h rename to src/libigl/igl/look_at.h diff --git a/src/igl/loop.cpp b/src/libigl/igl/loop.cpp similarity index 100% rename from src/igl/loop.cpp rename to src/libigl/igl/loop.cpp diff --git a/src/igl/loop.h b/src/libigl/igl/loop.h similarity index 100% rename from src/igl/loop.h rename to src/libigl/igl/loop.h diff --git a/src/igl/lscm.cpp b/src/libigl/igl/lscm.cpp similarity index 100% rename from src/igl/lscm.cpp rename to src/libigl/igl/lscm.cpp diff --git a/src/igl/lscm.h b/src/libigl/igl/lscm.h similarity index 100% rename from src/igl/lscm.h rename to src/libigl/igl/lscm.h diff --git a/src/igl/map_vertices_to_circle.cpp b/src/libigl/igl/map_vertices_to_circle.cpp similarity index 100% rename from src/igl/map_vertices_to_circle.cpp rename to src/libigl/igl/map_vertices_to_circle.cpp diff --git a/src/igl/map_vertices_to_circle.h b/src/libigl/igl/map_vertices_to_circle.h similarity index 100% rename from src/igl/map_vertices_to_circle.h rename to src/libigl/igl/map_vertices_to_circle.h diff --git a/src/igl/massmatrix.cpp b/src/libigl/igl/massmatrix.cpp similarity index 100% rename from src/igl/massmatrix.cpp rename to src/libigl/igl/massmatrix.cpp diff --git a/src/igl/massmatrix.h b/src/libigl/igl/massmatrix.h similarity index 100% rename from src/igl/massmatrix.h rename to src/libigl/igl/massmatrix.h diff --git a/src/igl/mat_max.cpp b/src/libigl/igl/mat_max.cpp similarity index 100% rename from src/igl/mat_max.cpp rename to src/libigl/igl/mat_max.cpp diff --git a/src/igl/mat_max.h b/src/libigl/igl/mat_max.h similarity index 100% rename from src/igl/mat_max.h rename to src/libigl/igl/mat_max.h diff --git a/src/igl/mat_min.cpp b/src/libigl/igl/mat_min.cpp similarity index 100% rename from src/igl/mat_min.cpp rename to src/libigl/igl/mat_min.cpp diff --git a/src/igl/mat_min.h b/src/libigl/igl/mat_min.h similarity index 100% rename from src/igl/mat_min.h rename to src/libigl/igl/mat_min.h diff --git a/src/igl/mat_to_quat.cpp b/src/libigl/igl/mat_to_quat.cpp similarity index 100% rename from src/igl/mat_to_quat.cpp rename to src/libigl/igl/mat_to_quat.cpp diff --git a/src/igl/mat_to_quat.h b/src/libigl/igl/mat_to_quat.h similarity index 100% rename from src/igl/mat_to_quat.h rename to src/libigl/igl/mat_to_quat.h diff --git a/src/igl/material_colors.h b/src/libigl/igl/material_colors.h similarity index 100% rename from src/igl/material_colors.h rename to src/libigl/igl/material_colors.h diff --git a/src/igl/matlab/MatlabWorkspace.h b/src/libigl/igl/matlab/MatlabWorkspace.h similarity index 100% rename from src/igl/matlab/MatlabWorkspace.h rename to src/libigl/igl/matlab/MatlabWorkspace.h diff --git a/src/igl/matlab/MexStream.h b/src/libigl/igl/matlab/MexStream.h similarity index 100% rename from src/igl/matlab/MexStream.h rename to src/libigl/igl/matlab/MexStream.h diff --git a/src/igl/matlab/matlabinterface.cpp b/src/libigl/igl/matlab/matlabinterface.cpp similarity index 100% rename from src/igl/matlab/matlabinterface.cpp rename to src/libigl/igl/matlab/matlabinterface.cpp diff --git a/src/igl/matlab/matlabinterface.h b/src/libigl/igl/matlab/matlabinterface.h similarity index 100% rename from src/igl/matlab/matlabinterface.h rename to src/libigl/igl/matlab/matlabinterface.h diff --git a/src/igl/matlab/mexErrMsgTxt.cpp b/src/libigl/igl/matlab/mexErrMsgTxt.cpp similarity index 100% rename from src/igl/matlab/mexErrMsgTxt.cpp rename to src/libigl/igl/matlab/mexErrMsgTxt.cpp diff --git a/src/igl/matlab/mexErrMsgTxt.h b/src/libigl/igl/matlab/mexErrMsgTxt.h similarity index 100% rename from src/igl/matlab/mexErrMsgTxt.h rename to src/libigl/igl/matlab/mexErrMsgTxt.h diff --git a/src/igl/matlab/parse_rhs.cpp b/src/libigl/igl/matlab/parse_rhs.cpp similarity index 100% rename from src/igl/matlab/parse_rhs.cpp rename to src/libigl/igl/matlab/parse_rhs.cpp diff --git a/src/igl/matlab/parse_rhs.h b/src/libigl/igl/matlab/parse_rhs.h similarity index 100% rename from src/igl/matlab/parse_rhs.h rename to src/libigl/igl/matlab/parse_rhs.h diff --git a/src/igl/matlab/prepare_lhs.cpp b/src/libigl/igl/matlab/prepare_lhs.cpp similarity index 100% rename from src/igl/matlab/prepare_lhs.cpp rename to src/libigl/igl/matlab/prepare_lhs.cpp diff --git a/src/igl/matlab/prepare_lhs.h b/src/libigl/igl/matlab/prepare_lhs.h similarity index 100% rename from src/igl/matlab/prepare_lhs.h rename to src/libigl/igl/matlab/prepare_lhs.h diff --git a/src/igl/matlab/requires_arg.cpp b/src/libigl/igl/matlab/requires_arg.cpp similarity index 100% rename from src/igl/matlab/requires_arg.cpp rename to src/libigl/igl/matlab/requires_arg.cpp diff --git a/src/igl/matlab/requires_arg.h b/src/libigl/igl/matlab/requires_arg.h similarity index 100% rename from src/igl/matlab/requires_arg.h rename to src/libigl/igl/matlab/requires_arg.h diff --git a/src/igl/matlab/validate_arg.cpp b/src/libigl/igl/matlab/validate_arg.cpp similarity index 100% rename from src/igl/matlab/validate_arg.cpp rename to src/libigl/igl/matlab/validate_arg.cpp diff --git a/src/igl/matlab/validate_arg.h b/src/libigl/igl/matlab/validate_arg.h similarity index 100% rename from src/igl/matlab/validate_arg.h rename to src/libigl/igl/matlab/validate_arg.h diff --git a/src/igl/matlab_format.cpp b/src/libigl/igl/matlab_format.cpp similarity index 100% rename from src/igl/matlab_format.cpp rename to src/libigl/igl/matlab_format.cpp diff --git a/src/igl/matlab_format.h b/src/libigl/igl/matlab_format.h similarity index 100% rename from src/igl/matlab_format.h rename to src/libigl/igl/matlab_format.h diff --git a/src/igl/matrix_to_list.cpp b/src/libigl/igl/matrix_to_list.cpp similarity index 100% rename from src/igl/matrix_to_list.cpp rename to src/libigl/igl/matrix_to_list.cpp diff --git a/src/igl/matrix_to_list.h b/src/libigl/igl/matrix_to_list.h similarity index 100% rename from src/igl/matrix_to_list.h rename to src/libigl/igl/matrix_to_list.h diff --git a/src/igl/max.cpp b/src/libigl/igl/max.cpp similarity index 100% rename from src/igl/max.cpp rename to src/libigl/igl/max.cpp diff --git a/src/igl/max.h b/src/libigl/igl/max.h similarity index 100% rename from src/igl/max.h rename to src/libigl/igl/max.h diff --git a/src/igl/max_faces_stopping_condition.cpp b/src/libigl/igl/max_faces_stopping_condition.cpp similarity index 100% rename from src/igl/max_faces_stopping_condition.cpp rename to src/libigl/igl/max_faces_stopping_condition.cpp diff --git a/src/igl/max_faces_stopping_condition.h b/src/libigl/igl/max_faces_stopping_condition.h similarity index 100% rename from src/igl/max_faces_stopping_condition.h rename to src/libigl/igl/max_faces_stopping_condition.h diff --git a/src/igl/max_size.cpp b/src/libigl/igl/max_size.cpp similarity index 100% rename from src/igl/max_size.cpp rename to src/libigl/igl/max_size.cpp diff --git a/src/igl/max_size.h b/src/libigl/igl/max_size.h similarity index 100% rename from src/igl/max_size.h rename to src/libigl/igl/max_size.h diff --git a/src/igl/median.cpp b/src/libigl/igl/median.cpp similarity index 100% rename from src/igl/median.cpp rename to src/libigl/igl/median.cpp diff --git a/src/igl/median.h b/src/libigl/igl/median.h similarity index 100% rename from src/igl/median.h rename to src/libigl/igl/median.h diff --git a/src/igl/min.cpp b/src/libigl/igl/min.cpp similarity index 100% rename from src/igl/min.cpp rename to src/libigl/igl/min.cpp diff --git a/src/igl/min.h b/src/libigl/igl/min.h similarity index 100% rename from src/igl/min.h rename to src/libigl/igl/min.h diff --git a/src/igl/min_quad_dense.cpp b/src/libigl/igl/min_quad_dense.cpp similarity index 100% rename from src/igl/min_quad_dense.cpp rename to src/libigl/igl/min_quad_dense.cpp diff --git a/src/igl/min_quad_dense.h b/src/libigl/igl/min_quad_dense.h similarity index 100% rename from src/igl/min_quad_dense.h rename to src/libigl/igl/min_quad_dense.h diff --git a/src/igl/min_quad_with_fixed.cpp b/src/libigl/igl/min_quad_with_fixed.cpp similarity index 100% rename from src/igl/min_quad_with_fixed.cpp rename to src/libigl/igl/min_quad_with_fixed.cpp diff --git a/src/igl/min_quad_with_fixed.h b/src/libigl/igl/min_quad_with_fixed.h similarity index 100% rename from src/igl/min_quad_with_fixed.h rename to src/libigl/igl/min_quad_with_fixed.h diff --git a/src/igl/min_size.cpp b/src/libigl/igl/min_size.cpp similarity index 100% rename from src/igl/min_size.cpp rename to src/libigl/igl/min_size.cpp diff --git a/src/igl/min_size.h b/src/libigl/igl/min_size.h similarity index 100% rename from src/igl/min_size.h rename to src/libigl/igl/min_size.h diff --git a/src/igl/mod.cpp b/src/libigl/igl/mod.cpp similarity index 100% rename from src/igl/mod.cpp rename to src/libigl/igl/mod.cpp diff --git a/src/igl/mod.h b/src/libigl/igl/mod.h similarity index 100% rename from src/igl/mod.h rename to src/libigl/igl/mod.h diff --git a/src/igl/mode.cpp b/src/libigl/igl/mode.cpp similarity index 100% rename from src/igl/mode.cpp rename to src/libigl/igl/mode.cpp diff --git a/src/igl/mode.h b/src/libigl/igl/mode.h similarity index 100% rename from src/igl/mode.h rename to src/libigl/igl/mode.h diff --git a/src/igl/mosek/bbw.cpp b/src/libigl/igl/mosek/bbw.cpp similarity index 100% rename from src/igl/mosek/bbw.cpp rename to src/libigl/igl/mosek/bbw.cpp diff --git a/src/igl/mosek/bbw.h b/src/libigl/igl/mosek/bbw.h similarity index 100% rename from src/igl/mosek/bbw.h rename to src/libigl/igl/mosek/bbw.h diff --git a/src/igl/mosek/mosek_guarded.cpp b/src/libigl/igl/mosek/mosek_guarded.cpp similarity index 100% rename from src/igl/mosek/mosek_guarded.cpp rename to src/libigl/igl/mosek/mosek_guarded.cpp diff --git a/src/igl/mosek/mosek_guarded.h b/src/libigl/igl/mosek/mosek_guarded.h similarity index 100% rename from src/igl/mosek/mosek_guarded.h rename to src/libigl/igl/mosek/mosek_guarded.h diff --git a/src/igl/mosek/mosek_linprog.cpp b/src/libigl/igl/mosek/mosek_linprog.cpp similarity index 100% rename from src/igl/mosek/mosek_linprog.cpp rename to src/libigl/igl/mosek/mosek_linprog.cpp diff --git a/src/igl/mosek/mosek_linprog.h b/src/libigl/igl/mosek/mosek_linprog.h similarity index 100% rename from src/igl/mosek/mosek_linprog.h rename to src/libigl/igl/mosek/mosek_linprog.h diff --git a/src/igl/mosek/mosek_quadprog.cpp b/src/libigl/igl/mosek/mosek_quadprog.cpp similarity index 100% rename from src/igl/mosek/mosek_quadprog.cpp rename to src/libigl/igl/mosek/mosek_quadprog.cpp diff --git a/src/igl/mosek/mosek_quadprog.h b/src/libigl/igl/mosek/mosek_quadprog.h similarity index 100% rename from src/igl/mosek/mosek_quadprog.h rename to src/libigl/igl/mosek/mosek_quadprog.h diff --git a/src/igl/mvc.cpp b/src/libigl/igl/mvc.cpp similarity index 100% rename from src/igl/mvc.cpp rename to src/libigl/igl/mvc.cpp diff --git a/src/igl/mvc.h b/src/libigl/igl/mvc.h similarity index 100% rename from src/igl/mvc.h rename to src/libigl/igl/mvc.h diff --git a/src/igl/nchoosek.cpp b/src/libigl/igl/nchoosek.cpp similarity index 100% rename from src/igl/nchoosek.cpp rename to src/libigl/igl/nchoosek.cpp diff --git a/src/igl/nchoosek.h b/src/libigl/igl/nchoosek.h similarity index 100% rename from src/igl/nchoosek.h rename to src/libigl/igl/nchoosek.h diff --git a/src/igl/next_filename.cpp b/src/libigl/igl/next_filename.cpp similarity index 100% rename from src/igl/next_filename.cpp rename to src/libigl/igl/next_filename.cpp diff --git a/src/igl/next_filename.h b/src/libigl/igl/next_filename.h similarity index 100% rename from src/igl/next_filename.h rename to src/libigl/igl/next_filename.h diff --git a/src/igl/normal_derivative.cpp b/src/libigl/igl/normal_derivative.cpp similarity index 100% rename from src/igl/normal_derivative.cpp rename to src/libigl/igl/normal_derivative.cpp diff --git a/src/igl/normal_derivative.h b/src/libigl/igl/normal_derivative.h similarity index 100% rename from src/igl/normal_derivative.h rename to src/libigl/igl/normal_derivative.h diff --git a/src/igl/normalize_quat.cpp b/src/libigl/igl/normalize_quat.cpp similarity index 100% rename from src/igl/normalize_quat.cpp rename to src/libigl/igl/normalize_quat.cpp diff --git a/src/igl/normalize_quat.h b/src/libigl/igl/normalize_quat.h similarity index 100% rename from src/igl/normalize_quat.h rename to src/libigl/igl/normalize_quat.h diff --git a/src/igl/normalize_row_lengths.cpp b/src/libigl/igl/normalize_row_lengths.cpp similarity index 100% rename from src/igl/normalize_row_lengths.cpp rename to src/libigl/igl/normalize_row_lengths.cpp diff --git a/src/igl/normalize_row_lengths.h b/src/libigl/igl/normalize_row_lengths.h similarity index 100% rename from src/igl/normalize_row_lengths.h rename to src/libigl/igl/normalize_row_lengths.h diff --git a/src/igl/normalize_row_sums.cpp b/src/libigl/igl/normalize_row_sums.cpp similarity index 100% rename from src/igl/normalize_row_sums.cpp rename to src/libigl/igl/normalize_row_sums.cpp diff --git a/src/igl/normalize_row_sums.h b/src/libigl/igl/normalize_row_sums.h similarity index 100% rename from src/igl/normalize_row_sums.h rename to src/libigl/igl/normalize_row_sums.h diff --git a/src/igl/null.cpp b/src/libigl/igl/null.cpp similarity index 100% rename from src/igl/null.cpp rename to src/libigl/igl/null.cpp diff --git a/src/igl/null.h b/src/libigl/igl/null.h similarity index 100% rename from src/igl/null.h rename to src/libigl/igl/null.h diff --git a/src/igl/octree.cpp b/src/libigl/igl/octree.cpp similarity index 100% rename from src/igl/octree.cpp rename to src/libigl/igl/octree.cpp diff --git a/src/igl/octree.h b/src/libigl/igl/octree.h similarity index 100% rename from src/igl/octree.h rename to src/libigl/igl/octree.h diff --git a/src/igl/on_boundary.cpp b/src/libigl/igl/on_boundary.cpp similarity index 100% rename from src/igl/on_boundary.cpp rename to src/libigl/igl/on_boundary.cpp diff --git a/src/igl/on_boundary.h b/src/libigl/igl/on_boundary.h similarity index 100% rename from src/igl/on_boundary.h rename to src/libigl/igl/on_boundary.h diff --git a/src/igl/opengl/MeshGL.cpp b/src/libigl/igl/opengl/MeshGL.cpp similarity index 100% rename from src/igl/opengl/MeshGL.cpp rename to src/libigl/igl/opengl/MeshGL.cpp diff --git a/src/igl/opengl/MeshGL.h b/src/libigl/igl/opengl/MeshGL.h similarity index 100% rename from src/igl/opengl/MeshGL.h rename to src/libigl/igl/opengl/MeshGL.h diff --git a/src/igl/opengl/ViewerCore.cpp b/src/libigl/igl/opengl/ViewerCore.cpp similarity index 100% rename from src/igl/opengl/ViewerCore.cpp rename to src/libigl/igl/opengl/ViewerCore.cpp diff --git a/src/igl/opengl/ViewerCore.h b/src/libigl/igl/opengl/ViewerCore.h similarity index 100% rename from src/igl/opengl/ViewerCore.h rename to src/libigl/igl/opengl/ViewerCore.h diff --git a/src/igl/opengl/ViewerData.cpp b/src/libigl/igl/opengl/ViewerData.cpp similarity index 100% rename from src/igl/opengl/ViewerData.cpp rename to src/libigl/igl/opengl/ViewerData.cpp diff --git a/src/igl/opengl/ViewerData.h b/src/libigl/igl/opengl/ViewerData.h similarity index 100% rename from src/igl/opengl/ViewerData.h rename to src/libigl/igl/opengl/ViewerData.h diff --git a/src/igl/opengl/bind_vertex_attrib_array.cpp b/src/libigl/igl/opengl/bind_vertex_attrib_array.cpp similarity index 100% rename from src/igl/opengl/bind_vertex_attrib_array.cpp rename to src/libigl/igl/opengl/bind_vertex_attrib_array.cpp diff --git a/src/igl/opengl/bind_vertex_attrib_array.h b/src/libigl/igl/opengl/bind_vertex_attrib_array.h similarity index 100% rename from src/igl/opengl/bind_vertex_attrib_array.h rename to src/libigl/igl/opengl/bind_vertex_attrib_array.h diff --git a/src/igl/opengl/create_index_vbo.cpp b/src/libigl/igl/opengl/create_index_vbo.cpp similarity index 100% rename from src/igl/opengl/create_index_vbo.cpp rename to src/libigl/igl/opengl/create_index_vbo.cpp diff --git a/src/igl/opengl/create_index_vbo.h b/src/libigl/igl/opengl/create_index_vbo.h similarity index 100% rename from src/igl/opengl/create_index_vbo.h rename to src/libigl/igl/opengl/create_index_vbo.h diff --git a/src/igl/opengl/create_mesh_vbo.cpp b/src/libigl/igl/opengl/create_mesh_vbo.cpp similarity index 100% rename from src/igl/opengl/create_mesh_vbo.cpp rename to src/libigl/igl/opengl/create_mesh_vbo.cpp diff --git a/src/igl/opengl/create_mesh_vbo.h b/src/libigl/igl/opengl/create_mesh_vbo.h similarity index 100% rename from src/igl/opengl/create_mesh_vbo.h rename to src/libigl/igl/opengl/create_mesh_vbo.h diff --git a/src/igl/opengl/create_shader_program.cpp b/src/libigl/igl/opengl/create_shader_program.cpp similarity index 100% rename from src/igl/opengl/create_shader_program.cpp rename to src/libigl/igl/opengl/create_shader_program.cpp diff --git a/src/igl/opengl/create_shader_program.h b/src/libigl/igl/opengl/create_shader_program.h similarity index 100% rename from src/igl/opengl/create_shader_program.h rename to src/libigl/igl/opengl/create_shader_program.h diff --git a/src/igl/opengl/create_vector_vbo.cpp b/src/libigl/igl/opengl/create_vector_vbo.cpp similarity index 100% rename from src/igl/opengl/create_vector_vbo.cpp rename to src/libigl/igl/opengl/create_vector_vbo.cpp diff --git a/src/igl/opengl/create_vector_vbo.h b/src/libigl/igl/opengl/create_vector_vbo.h similarity index 100% rename from src/igl/opengl/create_vector_vbo.h rename to src/libigl/igl/opengl/create_vector_vbo.h diff --git a/src/igl/opengl/destroy_shader_program.cpp b/src/libigl/igl/opengl/destroy_shader_program.cpp similarity index 100% rename from src/igl/opengl/destroy_shader_program.cpp rename to src/libigl/igl/opengl/destroy_shader_program.cpp diff --git a/src/igl/opengl/destroy_shader_program.h b/src/libigl/igl/opengl/destroy_shader_program.h similarity index 100% rename from src/igl/opengl/destroy_shader_program.h rename to src/libigl/igl/opengl/destroy_shader_program.h diff --git a/src/igl/opengl/gl.h b/src/libigl/igl/opengl/gl.h similarity index 100% rename from src/igl/opengl/gl.h rename to src/libigl/igl/opengl/gl.h diff --git a/src/igl/opengl/gl_type_size.cpp b/src/libigl/igl/opengl/gl_type_size.cpp similarity index 100% rename from src/igl/opengl/gl_type_size.cpp rename to src/libigl/igl/opengl/gl_type_size.cpp diff --git a/src/igl/opengl/gl_type_size.h b/src/libigl/igl/opengl/gl_type_size.h similarity index 100% rename from src/igl/opengl/gl_type_size.h rename to src/libigl/igl/opengl/gl_type_size.h diff --git a/src/igl/opengl/glfw/Viewer.cpp b/src/libigl/igl/opengl/glfw/Viewer.cpp similarity index 100% rename from src/igl/opengl/glfw/Viewer.cpp rename to src/libigl/igl/opengl/glfw/Viewer.cpp diff --git a/src/igl/opengl/glfw/Viewer.h b/src/libigl/igl/opengl/glfw/Viewer.h similarity index 100% rename from src/igl/opengl/glfw/Viewer.h rename to src/libigl/igl/opengl/glfw/Viewer.h diff --git a/src/igl/opengl/glfw/ViewerPlugin.h b/src/libigl/igl/opengl/glfw/ViewerPlugin.h similarity index 100% rename from src/igl/opengl/glfw/ViewerPlugin.h rename to src/libigl/igl/opengl/glfw/ViewerPlugin.h diff --git a/src/igl/opengl/glfw/background_window.cpp b/src/libigl/igl/opengl/glfw/background_window.cpp similarity index 100% rename from src/igl/opengl/glfw/background_window.cpp rename to src/libigl/igl/opengl/glfw/background_window.cpp diff --git a/src/igl/opengl/glfw/background_window.h b/src/libigl/igl/opengl/glfw/background_window.h similarity index 100% rename from src/igl/opengl/glfw/background_window.h rename to src/libigl/igl/opengl/glfw/background_window.h diff --git a/src/igl/opengl/glfw/imgui/ImGuiHelpers.h b/src/libigl/igl/opengl/glfw/imgui/ImGuiHelpers.h similarity index 100% rename from src/igl/opengl/glfw/imgui/ImGuiHelpers.h rename to src/libigl/igl/opengl/glfw/imgui/ImGuiHelpers.h diff --git a/src/igl/opengl/glfw/imgui/ImGuiMenu.cpp b/src/libigl/igl/opengl/glfw/imgui/ImGuiMenu.cpp similarity index 100% rename from src/igl/opengl/glfw/imgui/ImGuiMenu.cpp rename to src/libigl/igl/opengl/glfw/imgui/ImGuiMenu.cpp diff --git a/src/igl/opengl/glfw/imgui/ImGuiMenu.h b/src/libigl/igl/opengl/glfw/imgui/ImGuiMenu.h similarity index 100% rename from src/igl/opengl/glfw/imgui/ImGuiMenu.h rename to src/libigl/igl/opengl/glfw/imgui/ImGuiMenu.h diff --git a/src/igl/opengl/glfw/map_texture.cpp b/src/libigl/igl/opengl/glfw/map_texture.cpp similarity index 100% rename from src/igl/opengl/glfw/map_texture.cpp rename to src/libigl/igl/opengl/glfw/map_texture.cpp diff --git a/src/igl/opengl/glfw/map_texture.h b/src/libigl/igl/opengl/glfw/map_texture.h similarity index 100% rename from src/igl/opengl/glfw/map_texture.h rename to src/libigl/igl/opengl/glfw/map_texture.h diff --git a/src/igl/opengl/init_render_to_texture.cpp b/src/libigl/igl/opengl/init_render_to_texture.cpp similarity index 100% rename from src/igl/opengl/init_render_to_texture.cpp rename to src/libigl/igl/opengl/init_render_to_texture.cpp diff --git a/src/igl/opengl/init_render_to_texture.h b/src/libigl/igl/opengl/init_render_to_texture.h similarity index 100% rename from src/igl/opengl/init_render_to_texture.h rename to src/libigl/igl/opengl/init_render_to_texture.h diff --git a/src/igl/opengl/load_shader.cpp b/src/libigl/igl/opengl/load_shader.cpp similarity index 100% rename from src/igl/opengl/load_shader.cpp rename to src/libigl/igl/opengl/load_shader.cpp diff --git a/src/igl/opengl/load_shader.h b/src/libigl/igl/opengl/load_shader.h similarity index 100% rename from src/igl/opengl/load_shader.h rename to src/libigl/igl/opengl/load_shader.h diff --git a/src/igl/opengl/print_program_info_log.cpp b/src/libigl/igl/opengl/print_program_info_log.cpp similarity index 100% rename from src/igl/opengl/print_program_info_log.cpp rename to src/libigl/igl/opengl/print_program_info_log.cpp diff --git a/src/igl/opengl/print_program_info_log.h b/src/libigl/igl/opengl/print_program_info_log.h similarity index 100% rename from src/igl/opengl/print_program_info_log.h rename to src/libigl/igl/opengl/print_program_info_log.h diff --git a/src/igl/opengl/print_shader_info_log.cpp b/src/libigl/igl/opengl/print_shader_info_log.cpp similarity index 100% rename from src/igl/opengl/print_shader_info_log.cpp rename to src/libigl/igl/opengl/print_shader_info_log.cpp diff --git a/src/igl/opengl/print_shader_info_log.h b/src/libigl/igl/opengl/print_shader_info_log.h similarity index 100% rename from src/igl/opengl/print_shader_info_log.h rename to src/libigl/igl/opengl/print_shader_info_log.h diff --git a/src/igl/opengl/report_gl_error.cpp b/src/libigl/igl/opengl/report_gl_error.cpp similarity index 100% rename from src/igl/opengl/report_gl_error.cpp rename to src/libigl/igl/opengl/report_gl_error.cpp diff --git a/src/igl/opengl/report_gl_error.h b/src/libigl/igl/opengl/report_gl_error.h similarity index 100% rename from src/igl/opengl/report_gl_error.h rename to src/libigl/igl/opengl/report_gl_error.h diff --git a/src/igl/opengl/uniform_type_to_string.cpp b/src/libigl/igl/opengl/uniform_type_to_string.cpp similarity index 100% rename from src/igl/opengl/uniform_type_to_string.cpp rename to src/libigl/igl/opengl/uniform_type_to_string.cpp diff --git a/src/igl/opengl/uniform_type_to_string.h b/src/libigl/igl/opengl/uniform_type_to_string.h similarity index 100% rename from src/igl/opengl/uniform_type_to_string.h rename to src/libigl/igl/opengl/uniform_type_to_string.h diff --git a/src/igl/opengl/vertex_array.cpp b/src/libigl/igl/opengl/vertex_array.cpp similarity index 100% rename from src/igl/opengl/vertex_array.cpp rename to src/libigl/igl/opengl/vertex_array.cpp diff --git a/src/igl/opengl/vertex_array.h b/src/libigl/igl/opengl/vertex_array.h similarity index 100% rename from src/igl/opengl/vertex_array.h rename to src/libigl/igl/opengl/vertex_array.h diff --git a/src/igl/opengl2/MouseController.h b/src/libigl/igl/opengl2/MouseController.h similarity index 100% rename from src/igl/opengl2/MouseController.h rename to src/libigl/igl/opengl2/MouseController.h diff --git a/src/igl/opengl2/RotateWidget.h b/src/libigl/igl/opengl2/RotateWidget.h similarity index 100% rename from src/igl/opengl2/RotateWidget.h rename to src/libigl/igl/opengl2/RotateWidget.h diff --git a/src/igl/opengl2/TranslateWidget.h b/src/libigl/igl/opengl2/TranslateWidget.h similarity index 100% rename from src/igl/opengl2/TranslateWidget.h rename to src/libigl/igl/opengl2/TranslateWidget.h diff --git a/src/igl/opengl2/draw_beach_ball.cpp b/src/libigl/igl/opengl2/draw_beach_ball.cpp similarity index 100% rename from src/igl/opengl2/draw_beach_ball.cpp rename to src/libigl/igl/opengl2/draw_beach_ball.cpp diff --git a/src/igl/opengl2/draw_beach_ball.h b/src/libigl/igl/opengl2/draw_beach_ball.h similarity index 100% rename from src/igl/opengl2/draw_beach_ball.h rename to src/libigl/igl/opengl2/draw_beach_ball.h diff --git a/src/igl/opengl2/draw_floor.cpp b/src/libigl/igl/opengl2/draw_floor.cpp similarity index 100% rename from src/igl/opengl2/draw_floor.cpp rename to src/libigl/igl/opengl2/draw_floor.cpp diff --git a/src/igl/opengl2/draw_floor.h b/src/libigl/igl/opengl2/draw_floor.h similarity index 100% rename from src/igl/opengl2/draw_floor.h rename to src/libigl/igl/opengl2/draw_floor.h diff --git a/src/igl/opengl2/draw_mesh.cpp b/src/libigl/igl/opengl2/draw_mesh.cpp similarity index 100% rename from src/igl/opengl2/draw_mesh.cpp rename to src/libigl/igl/opengl2/draw_mesh.cpp diff --git a/src/igl/opengl2/draw_mesh.h b/src/libigl/igl/opengl2/draw_mesh.h similarity index 100% rename from src/igl/opengl2/draw_mesh.h rename to src/libigl/igl/opengl2/draw_mesh.h diff --git a/src/igl/opengl2/draw_point.cpp b/src/libigl/igl/opengl2/draw_point.cpp similarity index 100% rename from src/igl/opengl2/draw_point.cpp rename to src/libigl/igl/opengl2/draw_point.cpp diff --git a/src/igl/opengl2/draw_point.h b/src/libigl/igl/opengl2/draw_point.h similarity index 100% rename from src/igl/opengl2/draw_point.h rename to src/libigl/igl/opengl2/draw_point.h diff --git a/src/igl/opengl2/draw_rectangular_marquee.cpp b/src/libigl/igl/opengl2/draw_rectangular_marquee.cpp similarity index 100% rename from src/igl/opengl2/draw_rectangular_marquee.cpp rename to src/libigl/igl/opengl2/draw_rectangular_marquee.cpp diff --git a/src/igl/opengl2/draw_rectangular_marquee.h b/src/libigl/igl/opengl2/draw_rectangular_marquee.h similarity index 100% rename from src/igl/opengl2/draw_rectangular_marquee.h rename to src/libigl/igl/opengl2/draw_rectangular_marquee.h diff --git a/src/igl/opengl2/draw_skeleton_3d.cpp b/src/libigl/igl/opengl2/draw_skeleton_3d.cpp similarity index 100% rename from src/igl/opengl2/draw_skeleton_3d.cpp rename to src/libigl/igl/opengl2/draw_skeleton_3d.cpp diff --git a/src/igl/opengl2/draw_skeleton_3d.h b/src/libigl/igl/opengl2/draw_skeleton_3d.h similarity index 100% rename from src/igl/opengl2/draw_skeleton_3d.h rename to src/libigl/igl/opengl2/draw_skeleton_3d.h diff --git a/src/igl/opengl2/draw_skeleton_vector_graphics.cpp b/src/libigl/igl/opengl2/draw_skeleton_vector_graphics.cpp similarity index 100% rename from src/igl/opengl2/draw_skeleton_vector_graphics.cpp rename to src/libigl/igl/opengl2/draw_skeleton_vector_graphics.cpp diff --git a/src/igl/opengl2/draw_skeleton_vector_graphics.h b/src/libigl/igl/opengl2/draw_skeleton_vector_graphics.h similarity index 100% rename from src/igl/opengl2/draw_skeleton_vector_graphics.h rename to src/libigl/igl/opengl2/draw_skeleton_vector_graphics.h diff --git a/src/igl/opengl2/flare_textures.h b/src/libigl/igl/opengl2/flare_textures.h similarity index 100% rename from src/igl/opengl2/flare_textures.h rename to src/libigl/igl/opengl2/flare_textures.h diff --git a/src/igl/opengl2/gl.h b/src/libigl/igl/opengl2/gl.h similarity index 100% rename from src/igl/opengl2/gl.h rename to src/libigl/igl/opengl2/gl.h diff --git a/src/igl/opengl2/glext.h b/src/libigl/igl/opengl2/glext.h similarity index 100% rename from src/igl/opengl2/glext.h rename to src/libigl/igl/opengl2/glext.h diff --git a/src/igl/opengl2/glu.h b/src/libigl/igl/opengl2/glu.h similarity index 100% rename from src/igl/opengl2/glu.h rename to src/libigl/igl/opengl2/glu.h diff --git a/src/igl/opengl2/lens_flare.cpp b/src/libigl/igl/opengl2/lens_flare.cpp similarity index 100% rename from src/igl/opengl2/lens_flare.cpp rename to src/libigl/igl/opengl2/lens_flare.cpp diff --git a/src/igl/opengl2/lens_flare.h b/src/libigl/igl/opengl2/lens_flare.h similarity index 100% rename from src/igl/opengl2/lens_flare.h rename to src/libigl/igl/opengl2/lens_flare.h diff --git a/src/igl/opengl2/model_proj_viewport.cpp b/src/libigl/igl/opengl2/model_proj_viewport.cpp similarity index 100% rename from src/igl/opengl2/model_proj_viewport.cpp rename to src/libigl/igl/opengl2/model_proj_viewport.cpp diff --git a/src/igl/opengl2/model_proj_viewport.h b/src/libigl/igl/opengl2/model_proj_viewport.h similarity index 100% rename from src/igl/opengl2/model_proj_viewport.h rename to src/libigl/igl/opengl2/model_proj_viewport.h diff --git a/src/igl/opengl2/print_gl_get.cpp b/src/libigl/igl/opengl2/print_gl_get.cpp similarity index 100% rename from src/igl/opengl2/print_gl_get.cpp rename to src/libigl/igl/opengl2/print_gl_get.cpp diff --git a/src/igl/opengl2/print_gl_get.h b/src/libigl/igl/opengl2/print_gl_get.h similarity index 100% rename from src/igl/opengl2/print_gl_get.h rename to src/libigl/igl/opengl2/print_gl_get.h diff --git a/src/igl/opengl2/project.cpp b/src/libigl/igl/opengl2/project.cpp similarity index 100% rename from src/igl/opengl2/project.cpp rename to src/libigl/igl/opengl2/project.cpp diff --git a/src/igl/opengl2/project.h b/src/libigl/igl/opengl2/project.h similarity index 100% rename from src/igl/opengl2/project.h rename to src/libigl/igl/opengl2/project.h diff --git a/src/igl/opengl2/right_axis.cpp b/src/libigl/igl/opengl2/right_axis.cpp similarity index 100% rename from src/igl/opengl2/right_axis.cpp rename to src/libigl/igl/opengl2/right_axis.cpp diff --git a/src/igl/opengl2/right_axis.h b/src/libigl/igl/opengl2/right_axis.h similarity index 100% rename from src/igl/opengl2/right_axis.h rename to src/libigl/igl/opengl2/right_axis.h diff --git a/src/igl/opengl2/shine_textures.h b/src/libigl/igl/opengl2/shine_textures.h similarity index 100% rename from src/igl/opengl2/shine_textures.h rename to src/libigl/igl/opengl2/shine_textures.h diff --git a/src/igl/opengl2/sort_triangles.cpp b/src/libigl/igl/opengl2/sort_triangles.cpp similarity index 100% rename from src/igl/opengl2/sort_triangles.cpp rename to src/libigl/igl/opengl2/sort_triangles.cpp diff --git a/src/igl/opengl2/sort_triangles.h b/src/libigl/igl/opengl2/sort_triangles.h similarity index 100% rename from src/igl/opengl2/sort_triangles.h rename to src/libigl/igl/opengl2/sort_triangles.h diff --git a/src/igl/opengl2/unproject.cpp b/src/libigl/igl/opengl2/unproject.cpp similarity index 100% rename from src/igl/opengl2/unproject.cpp rename to src/libigl/igl/opengl2/unproject.cpp diff --git a/src/igl/opengl2/unproject.h b/src/libigl/igl/opengl2/unproject.h similarity index 100% rename from src/igl/opengl2/unproject.h rename to src/libigl/igl/opengl2/unproject.h diff --git a/src/igl/opengl2/unproject_to_zero_plane.cpp b/src/libigl/igl/opengl2/unproject_to_zero_plane.cpp similarity index 100% rename from src/igl/opengl2/unproject_to_zero_plane.cpp rename to src/libigl/igl/opengl2/unproject_to_zero_plane.cpp diff --git a/src/igl/opengl2/unproject_to_zero_plane.h b/src/libigl/igl/opengl2/unproject_to_zero_plane.h similarity index 100% rename from src/igl/opengl2/unproject_to_zero_plane.h rename to src/libigl/igl/opengl2/unproject_to_zero_plane.h diff --git a/src/igl/opengl2/up_axis.cpp b/src/libigl/igl/opengl2/up_axis.cpp similarity index 100% rename from src/igl/opengl2/up_axis.cpp rename to src/libigl/igl/opengl2/up_axis.cpp diff --git a/src/igl/opengl2/up_axis.h b/src/libigl/igl/opengl2/up_axis.h similarity index 100% rename from src/igl/opengl2/up_axis.h rename to src/libigl/igl/opengl2/up_axis.h diff --git a/src/igl/opengl2/view_axis.cpp b/src/libigl/igl/opengl2/view_axis.cpp similarity index 100% rename from src/igl/opengl2/view_axis.cpp rename to src/libigl/igl/opengl2/view_axis.cpp diff --git a/src/igl/opengl2/view_axis.h b/src/libigl/igl/opengl2/view_axis.h similarity index 100% rename from src/igl/opengl2/view_axis.h rename to src/libigl/igl/opengl2/view_axis.h diff --git a/src/igl/orient_outward.cpp b/src/libigl/igl/orient_outward.cpp similarity index 100% rename from src/igl/orient_outward.cpp rename to src/libigl/igl/orient_outward.cpp diff --git a/src/igl/orient_outward.h b/src/libigl/igl/orient_outward.h similarity index 100% rename from src/igl/orient_outward.h rename to src/libigl/igl/orient_outward.h diff --git a/src/igl/orientable_patches.cpp b/src/libigl/igl/orientable_patches.cpp similarity index 100% rename from src/igl/orientable_patches.cpp rename to src/libigl/igl/orientable_patches.cpp diff --git a/src/igl/orientable_patches.h b/src/libigl/igl/orientable_patches.h similarity index 100% rename from src/igl/orientable_patches.h rename to src/libigl/igl/orientable_patches.h diff --git a/src/igl/oriented_facets.cpp b/src/libigl/igl/oriented_facets.cpp similarity index 100% rename from src/igl/oriented_facets.cpp rename to src/libigl/igl/oriented_facets.cpp diff --git a/src/igl/oriented_facets.h b/src/libigl/igl/oriented_facets.h similarity index 100% rename from src/igl/oriented_facets.h rename to src/libigl/igl/oriented_facets.h diff --git a/src/igl/orth.cpp b/src/libigl/igl/orth.cpp similarity index 100% rename from src/igl/orth.cpp rename to src/libigl/igl/orth.cpp diff --git a/src/igl/orth.h b/src/libigl/igl/orth.h similarity index 100% rename from src/igl/orth.h rename to src/libigl/igl/orth.h diff --git a/src/igl/ortho.cpp b/src/libigl/igl/ortho.cpp similarity index 100% rename from src/igl/ortho.cpp rename to src/libigl/igl/ortho.cpp diff --git a/src/igl/ortho.h b/src/libigl/igl/ortho.h similarity index 100% rename from src/igl/ortho.h rename to src/libigl/igl/ortho.h diff --git a/src/igl/outer_element.cpp b/src/libigl/igl/outer_element.cpp similarity index 100% rename from src/igl/outer_element.cpp rename to src/libigl/igl/outer_element.cpp diff --git a/src/igl/outer_element.h b/src/libigl/igl/outer_element.h similarity index 100% rename from src/igl/outer_element.h rename to src/libigl/igl/outer_element.h diff --git a/src/igl/parallel_for.h b/src/libigl/igl/parallel_for.h similarity index 100% rename from src/igl/parallel_for.h rename to src/libigl/igl/parallel_for.h diff --git a/src/igl/parallel_transport_angles.cpp b/src/libigl/igl/parallel_transport_angles.cpp similarity index 100% rename from src/igl/parallel_transport_angles.cpp rename to src/libigl/igl/parallel_transport_angles.cpp diff --git a/src/igl/parallel_transport_angles.h b/src/libigl/igl/parallel_transport_angles.h similarity index 100% rename from src/igl/parallel_transport_angles.h rename to src/libigl/igl/parallel_transport_angles.h diff --git a/src/igl/partition.cpp b/src/libigl/igl/partition.cpp similarity index 100% rename from src/igl/partition.cpp rename to src/libigl/igl/partition.cpp diff --git a/src/igl/partition.h b/src/libigl/igl/partition.h similarity index 100% rename from src/igl/partition.h rename to src/libigl/igl/partition.h diff --git a/src/igl/parula.cpp b/src/libigl/igl/parula.cpp similarity index 100% rename from src/igl/parula.cpp rename to src/libigl/igl/parula.cpp diff --git a/src/igl/parula.h b/src/libigl/igl/parula.h similarity index 100% rename from src/igl/parula.h rename to src/libigl/igl/parula.h diff --git a/src/igl/path_to_executable.cpp b/src/libigl/igl/path_to_executable.cpp similarity index 100% rename from src/igl/path_to_executable.cpp rename to src/libigl/igl/path_to_executable.cpp diff --git a/src/igl/path_to_executable.h b/src/libigl/igl/path_to_executable.h similarity index 100% rename from src/igl/path_to_executable.h rename to src/libigl/igl/path_to_executable.h diff --git a/src/igl/pathinfo.cpp b/src/libigl/igl/pathinfo.cpp similarity index 100% rename from src/igl/pathinfo.cpp rename to src/libigl/igl/pathinfo.cpp diff --git a/src/igl/pathinfo.h b/src/libigl/igl/pathinfo.h similarity index 100% rename from src/igl/pathinfo.h rename to src/libigl/igl/pathinfo.h diff --git a/src/igl/per_corner_normals.cpp b/src/libigl/igl/per_corner_normals.cpp similarity index 100% rename from src/igl/per_corner_normals.cpp rename to src/libigl/igl/per_corner_normals.cpp diff --git a/src/igl/per_corner_normals.h b/src/libigl/igl/per_corner_normals.h similarity index 100% rename from src/igl/per_corner_normals.h rename to src/libigl/igl/per_corner_normals.h diff --git a/src/igl/per_edge_normals.cpp b/src/libigl/igl/per_edge_normals.cpp similarity index 100% rename from src/igl/per_edge_normals.cpp rename to src/libigl/igl/per_edge_normals.cpp diff --git a/src/igl/per_edge_normals.h b/src/libigl/igl/per_edge_normals.h similarity index 100% rename from src/igl/per_edge_normals.h rename to src/libigl/igl/per_edge_normals.h diff --git a/src/igl/per_face_normals.cpp b/src/libigl/igl/per_face_normals.cpp similarity index 100% rename from src/igl/per_face_normals.cpp rename to src/libigl/igl/per_face_normals.cpp diff --git a/src/igl/per_face_normals.h b/src/libigl/igl/per_face_normals.h similarity index 100% rename from src/igl/per_face_normals.h rename to src/libigl/igl/per_face_normals.h diff --git a/src/igl/per_vertex_attribute_smoothing.cpp b/src/libigl/igl/per_vertex_attribute_smoothing.cpp similarity index 100% rename from src/igl/per_vertex_attribute_smoothing.cpp rename to src/libigl/igl/per_vertex_attribute_smoothing.cpp diff --git a/src/igl/per_vertex_attribute_smoothing.h b/src/libigl/igl/per_vertex_attribute_smoothing.h similarity index 100% rename from src/igl/per_vertex_attribute_smoothing.h rename to src/libigl/igl/per_vertex_attribute_smoothing.h diff --git a/src/igl/per_vertex_normals.cpp b/src/libigl/igl/per_vertex_normals.cpp similarity index 100% rename from src/igl/per_vertex_normals.cpp rename to src/libigl/igl/per_vertex_normals.cpp diff --git a/src/igl/per_vertex_normals.h b/src/libigl/igl/per_vertex_normals.h similarity index 100% rename from src/igl/per_vertex_normals.h rename to src/libigl/igl/per_vertex_normals.h diff --git a/src/igl/per_vertex_point_to_plane_quadrics.cpp b/src/libigl/igl/per_vertex_point_to_plane_quadrics.cpp similarity index 100% rename from src/igl/per_vertex_point_to_plane_quadrics.cpp rename to src/libigl/igl/per_vertex_point_to_plane_quadrics.cpp diff --git a/src/igl/per_vertex_point_to_plane_quadrics.h b/src/libigl/igl/per_vertex_point_to_plane_quadrics.h similarity index 100% rename from src/igl/per_vertex_point_to_plane_quadrics.h rename to src/libigl/igl/per_vertex_point_to_plane_quadrics.h diff --git a/src/igl/piecewise_constant_winding_number.cpp b/src/libigl/igl/piecewise_constant_winding_number.cpp similarity index 100% rename from src/igl/piecewise_constant_winding_number.cpp rename to src/libigl/igl/piecewise_constant_winding_number.cpp diff --git a/src/igl/piecewise_constant_winding_number.h b/src/libigl/igl/piecewise_constant_winding_number.h similarity index 100% rename from src/igl/piecewise_constant_winding_number.h rename to src/libigl/igl/piecewise_constant_winding_number.h diff --git a/src/igl/pinv.cpp b/src/libigl/igl/pinv.cpp similarity index 100% rename from src/igl/pinv.cpp rename to src/libigl/igl/pinv.cpp diff --git a/src/igl/pinv.h b/src/libigl/igl/pinv.h similarity index 100% rename from src/igl/pinv.h rename to src/libigl/igl/pinv.h diff --git a/src/igl/planarize_quad_mesh.cpp b/src/libigl/igl/planarize_quad_mesh.cpp similarity index 100% rename from src/igl/planarize_quad_mesh.cpp rename to src/libigl/igl/planarize_quad_mesh.cpp diff --git a/src/igl/planarize_quad_mesh.h b/src/libigl/igl/planarize_quad_mesh.h similarity index 100% rename from src/igl/planarize_quad_mesh.h rename to src/libigl/igl/planarize_quad_mesh.h diff --git a/src/igl/ply.h b/src/libigl/igl/ply.h similarity index 100% rename from src/igl/ply.h rename to src/libigl/igl/ply.h diff --git a/src/igl/png/readPNG.cpp b/src/libigl/igl/png/readPNG.cpp similarity index 100% rename from src/igl/png/readPNG.cpp rename to src/libigl/igl/png/readPNG.cpp diff --git a/src/igl/png/readPNG.h b/src/libigl/igl/png/readPNG.h similarity index 100% rename from src/igl/png/readPNG.h rename to src/libigl/igl/png/readPNG.h diff --git a/src/igl/png/render_to_png.cpp b/src/libigl/igl/png/render_to_png.cpp similarity index 100% rename from src/igl/png/render_to_png.cpp rename to src/libigl/igl/png/render_to_png.cpp diff --git a/src/igl/png/render_to_png.h b/src/libigl/igl/png/render_to_png.h similarity index 100% rename from src/igl/png/render_to_png.h rename to src/libigl/igl/png/render_to_png.h diff --git a/src/igl/png/render_to_png_async.cpp b/src/libigl/igl/png/render_to_png_async.cpp similarity index 100% rename from src/igl/png/render_to_png_async.cpp rename to src/libigl/igl/png/render_to_png_async.cpp diff --git a/src/igl/png/render_to_png_async.h b/src/libigl/igl/png/render_to_png_async.h similarity index 100% rename from src/igl/png/render_to_png_async.h rename to src/libigl/igl/png/render_to_png_async.h diff --git a/src/igl/png/texture_from_file.cpp b/src/libigl/igl/png/texture_from_file.cpp similarity index 100% rename from src/igl/png/texture_from_file.cpp rename to src/libigl/igl/png/texture_from_file.cpp diff --git a/src/igl/png/texture_from_file.h b/src/libigl/igl/png/texture_from_file.h similarity index 100% rename from src/igl/png/texture_from_file.h rename to src/libigl/igl/png/texture_from_file.h diff --git a/src/igl/png/texture_from_png.cpp b/src/libigl/igl/png/texture_from_png.cpp similarity index 100% rename from src/igl/png/texture_from_png.cpp rename to src/libigl/igl/png/texture_from_png.cpp diff --git a/src/igl/png/texture_from_png.h b/src/libigl/igl/png/texture_from_png.h similarity index 100% rename from src/igl/png/texture_from_png.h rename to src/libigl/igl/png/texture_from_png.h diff --git a/src/igl/png/writePNG.cpp b/src/libigl/igl/png/writePNG.cpp similarity index 100% rename from src/igl/png/writePNG.cpp rename to src/libigl/igl/png/writePNG.cpp diff --git a/src/igl/png/writePNG.h b/src/libigl/igl/png/writePNG.h similarity index 100% rename from src/igl/png/writePNG.h rename to src/libigl/igl/png/writePNG.h diff --git a/src/igl/point_in_circle.cpp b/src/libigl/igl/point_in_circle.cpp similarity index 100% rename from src/igl/point_in_circle.cpp rename to src/libigl/igl/point_in_circle.cpp diff --git a/src/igl/point_in_circle.h b/src/libigl/igl/point_in_circle.h similarity index 100% rename from src/igl/point_in_circle.h rename to src/libigl/igl/point_in_circle.h diff --git a/src/igl/point_in_poly.cpp b/src/libigl/igl/point_in_poly.cpp similarity index 100% rename from src/igl/point_in_poly.cpp rename to src/libigl/igl/point_in_poly.cpp diff --git a/src/igl/point_in_poly.h b/src/libigl/igl/point_in_poly.h similarity index 100% rename from src/igl/point_in_poly.h rename to src/libigl/igl/point_in_poly.h diff --git a/src/igl/point_mesh_squared_distance.cpp b/src/libigl/igl/point_mesh_squared_distance.cpp similarity index 100% rename from src/igl/point_mesh_squared_distance.cpp rename to src/libigl/igl/point_mesh_squared_distance.cpp diff --git a/src/igl/point_mesh_squared_distance.h b/src/libigl/igl/point_mesh_squared_distance.h similarity index 100% rename from src/igl/point_mesh_squared_distance.h rename to src/libigl/igl/point_mesh_squared_distance.h diff --git a/src/igl/point_simplex_squared_distance.cpp b/src/libigl/igl/point_simplex_squared_distance.cpp similarity index 100% rename from src/igl/point_simplex_squared_distance.cpp rename to src/libigl/igl/point_simplex_squared_distance.cpp diff --git a/src/igl/point_simplex_squared_distance.h b/src/libigl/igl/point_simplex_squared_distance.h similarity index 100% rename from src/igl/point_simplex_squared_distance.h rename to src/libigl/igl/point_simplex_squared_distance.h diff --git a/src/igl/polar_dec.cpp b/src/libigl/igl/polar_dec.cpp similarity index 100% rename from src/igl/polar_dec.cpp rename to src/libigl/igl/polar_dec.cpp diff --git a/src/igl/polar_dec.h b/src/libigl/igl/polar_dec.h similarity index 100% rename from src/igl/polar_dec.h rename to src/libigl/igl/polar_dec.h diff --git a/src/igl/polar_svd.cpp b/src/libigl/igl/polar_svd.cpp similarity index 100% rename from src/igl/polar_svd.cpp rename to src/libigl/igl/polar_svd.cpp diff --git a/src/igl/polar_svd.h b/src/libigl/igl/polar_svd.h similarity index 100% rename from src/igl/polar_svd.h rename to src/libigl/igl/polar_svd.h diff --git a/src/igl/polar_svd3x3.cpp b/src/libigl/igl/polar_svd3x3.cpp similarity index 100% rename from src/igl/polar_svd3x3.cpp rename to src/libigl/igl/polar_svd3x3.cpp diff --git a/src/igl/polar_svd3x3.h b/src/libigl/igl/polar_svd3x3.h similarity index 100% rename from src/igl/polar_svd3x3.h rename to src/libigl/igl/polar_svd3x3.h diff --git a/src/igl/polygon_mesh_to_triangle_mesh.cpp b/src/libigl/igl/polygon_mesh_to_triangle_mesh.cpp similarity index 100% rename from src/igl/polygon_mesh_to_triangle_mesh.cpp rename to src/libigl/igl/polygon_mesh_to_triangle_mesh.cpp diff --git a/src/igl/polygon_mesh_to_triangle_mesh.h b/src/libigl/igl/polygon_mesh_to_triangle_mesh.h similarity index 100% rename from src/igl/polygon_mesh_to_triangle_mesh.h rename to src/libigl/igl/polygon_mesh_to_triangle_mesh.h diff --git a/src/igl/principal_curvature.cpp b/src/libigl/igl/principal_curvature.cpp similarity index 100% rename from src/igl/principal_curvature.cpp rename to src/libigl/igl/principal_curvature.cpp diff --git a/src/igl/principal_curvature.h b/src/libigl/igl/principal_curvature.h similarity index 100% rename from src/igl/principal_curvature.h rename to src/libigl/igl/principal_curvature.h diff --git a/src/igl/print_ijv.cpp b/src/libigl/igl/print_ijv.cpp similarity index 100% rename from src/igl/print_ijv.cpp rename to src/libigl/igl/print_ijv.cpp diff --git a/src/igl/print_ijv.h b/src/libigl/igl/print_ijv.h similarity index 100% rename from src/igl/print_ijv.h rename to src/libigl/igl/print_ijv.h diff --git a/src/igl/print_vector.cpp b/src/libigl/igl/print_vector.cpp similarity index 100% rename from src/igl/print_vector.cpp rename to src/libigl/igl/print_vector.cpp diff --git a/src/igl/print_vector.h b/src/libigl/igl/print_vector.h similarity index 100% rename from src/igl/print_vector.h rename to src/libigl/igl/print_vector.h diff --git a/src/igl/procrustes.cpp b/src/libigl/igl/procrustes.cpp similarity index 100% rename from src/igl/procrustes.cpp rename to src/libigl/igl/procrustes.cpp diff --git a/src/igl/procrustes.h b/src/libigl/igl/procrustes.h similarity index 100% rename from src/igl/procrustes.h rename to src/libigl/igl/procrustes.h diff --git a/src/igl/project.cpp b/src/libigl/igl/project.cpp similarity index 100% rename from src/igl/project.cpp rename to src/libigl/igl/project.cpp diff --git a/src/igl/project.h b/src/libigl/igl/project.h similarity index 100% rename from src/igl/project.h rename to src/libigl/igl/project.h diff --git a/src/igl/project_isometrically_to_plane.cpp b/src/libigl/igl/project_isometrically_to_plane.cpp similarity index 100% rename from src/igl/project_isometrically_to_plane.cpp rename to src/libigl/igl/project_isometrically_to_plane.cpp diff --git a/src/igl/project_isometrically_to_plane.h b/src/libigl/igl/project_isometrically_to_plane.h similarity index 100% rename from src/igl/project_isometrically_to_plane.h rename to src/libigl/igl/project_isometrically_to_plane.h diff --git a/src/igl/project_to_line.cpp b/src/libigl/igl/project_to_line.cpp similarity index 100% rename from src/igl/project_to_line.cpp rename to src/libigl/igl/project_to_line.cpp diff --git a/src/igl/project_to_line.h b/src/libigl/igl/project_to_line.h similarity index 100% rename from src/igl/project_to_line.h rename to src/libigl/igl/project_to_line.h diff --git a/src/igl/project_to_line_segment.cpp b/src/libigl/igl/project_to_line_segment.cpp similarity index 100% rename from src/igl/project_to_line_segment.cpp rename to src/libigl/igl/project_to_line_segment.cpp diff --git a/src/igl/project_to_line_segment.h b/src/libigl/igl/project_to_line_segment.h similarity index 100% rename from src/igl/project_to_line_segment.h rename to src/libigl/igl/project_to_line_segment.h diff --git a/src/igl/pseudonormal_test.cpp b/src/libigl/igl/pseudonormal_test.cpp similarity index 100% rename from src/igl/pseudonormal_test.cpp rename to src/libigl/igl/pseudonormal_test.cpp diff --git a/src/igl/pseudonormal_test.h b/src/libigl/igl/pseudonormal_test.h similarity index 100% rename from src/igl/pseudonormal_test.h rename to src/libigl/igl/pseudonormal_test.h diff --git a/src/igl/pso.cpp b/src/libigl/igl/pso.cpp similarity index 100% rename from src/igl/pso.cpp rename to src/libigl/igl/pso.cpp diff --git a/src/igl/pso.h b/src/libigl/igl/pso.h similarity index 100% rename from src/igl/pso.h rename to src/libigl/igl/pso.h diff --git a/src/igl/qslim.cpp b/src/libigl/igl/qslim.cpp similarity index 100% rename from src/igl/qslim.cpp rename to src/libigl/igl/qslim.cpp diff --git a/src/igl/qslim.h b/src/libigl/igl/qslim.h similarity index 100% rename from src/igl/qslim.h rename to src/libigl/igl/qslim.h diff --git a/src/igl/qslim_optimal_collapse_edge_callbacks.cpp b/src/libigl/igl/qslim_optimal_collapse_edge_callbacks.cpp similarity index 100% rename from src/igl/qslim_optimal_collapse_edge_callbacks.cpp rename to src/libigl/igl/qslim_optimal_collapse_edge_callbacks.cpp diff --git a/src/igl/qslim_optimal_collapse_edge_callbacks.h b/src/libigl/igl/qslim_optimal_collapse_edge_callbacks.h similarity index 100% rename from src/igl/qslim_optimal_collapse_edge_callbacks.h rename to src/libigl/igl/qslim_optimal_collapse_edge_callbacks.h diff --git a/src/igl/quad_planarity.cpp b/src/libigl/igl/quad_planarity.cpp similarity index 100% rename from src/igl/quad_planarity.cpp rename to src/libigl/igl/quad_planarity.cpp diff --git a/src/igl/quad_planarity.h b/src/libigl/igl/quad_planarity.h similarity index 100% rename from src/igl/quad_planarity.h rename to src/libigl/igl/quad_planarity.h diff --git a/src/igl/quadric_binary_plus_operator.cpp b/src/libigl/igl/quadric_binary_plus_operator.cpp similarity index 100% rename from src/igl/quadric_binary_plus_operator.cpp rename to src/libigl/igl/quadric_binary_plus_operator.cpp diff --git a/src/igl/quadric_binary_plus_operator.h b/src/libigl/igl/quadric_binary_plus_operator.h similarity index 100% rename from src/igl/quadric_binary_plus_operator.h rename to src/libigl/igl/quadric_binary_plus_operator.h diff --git a/src/igl/quat_conjugate.cpp b/src/libigl/igl/quat_conjugate.cpp similarity index 100% rename from src/igl/quat_conjugate.cpp rename to src/libigl/igl/quat_conjugate.cpp diff --git a/src/igl/quat_conjugate.h b/src/libigl/igl/quat_conjugate.h similarity index 100% rename from src/igl/quat_conjugate.h rename to src/libigl/igl/quat_conjugate.h diff --git a/src/igl/quat_mult.cpp b/src/libigl/igl/quat_mult.cpp similarity index 100% rename from src/igl/quat_mult.cpp rename to src/libigl/igl/quat_mult.cpp diff --git a/src/igl/quat_mult.h b/src/libigl/igl/quat_mult.h similarity index 100% rename from src/igl/quat_mult.h rename to src/libigl/igl/quat_mult.h diff --git a/src/igl/quat_to_axis_angle.cpp b/src/libigl/igl/quat_to_axis_angle.cpp similarity index 100% rename from src/igl/quat_to_axis_angle.cpp rename to src/libigl/igl/quat_to_axis_angle.cpp diff --git a/src/igl/quat_to_axis_angle.h b/src/libigl/igl/quat_to_axis_angle.h similarity index 100% rename from src/igl/quat_to_axis_angle.h rename to src/libigl/igl/quat_to_axis_angle.h diff --git a/src/igl/quat_to_mat.cpp b/src/libigl/igl/quat_to_mat.cpp similarity index 100% rename from src/igl/quat_to_mat.cpp rename to src/libigl/igl/quat_to_mat.cpp diff --git a/src/igl/quat_to_mat.h b/src/libigl/igl/quat_to_mat.h similarity index 100% rename from src/igl/quat_to_mat.h rename to src/libigl/igl/quat_to_mat.h diff --git a/src/igl/quats_to_column.cpp b/src/libigl/igl/quats_to_column.cpp similarity index 100% rename from src/igl/quats_to_column.cpp rename to src/libigl/igl/quats_to_column.cpp diff --git a/src/igl/quats_to_column.h b/src/libigl/igl/quats_to_column.h similarity index 100% rename from src/igl/quats_to_column.h rename to src/libigl/igl/quats_to_column.h diff --git a/src/igl/ramer_douglas_peucker.cpp b/src/libigl/igl/ramer_douglas_peucker.cpp similarity index 100% rename from src/igl/ramer_douglas_peucker.cpp rename to src/libigl/igl/ramer_douglas_peucker.cpp diff --git a/src/igl/ramer_douglas_peucker.h b/src/libigl/igl/ramer_douglas_peucker.h similarity index 100% rename from src/igl/ramer_douglas_peucker.h rename to src/libigl/igl/ramer_douglas_peucker.h diff --git a/src/igl/random_dir.cpp b/src/libigl/igl/random_dir.cpp similarity index 100% rename from src/igl/random_dir.cpp rename to src/libigl/igl/random_dir.cpp diff --git a/src/igl/random_dir.h b/src/libigl/igl/random_dir.h similarity index 100% rename from src/igl/random_dir.h rename to src/libigl/igl/random_dir.h diff --git a/src/igl/random_points_on_mesh.cpp b/src/libigl/igl/random_points_on_mesh.cpp similarity index 100% rename from src/igl/random_points_on_mesh.cpp rename to src/libigl/igl/random_points_on_mesh.cpp diff --git a/src/igl/random_points_on_mesh.h b/src/libigl/igl/random_points_on_mesh.h similarity index 100% rename from src/igl/random_points_on_mesh.h rename to src/libigl/igl/random_points_on_mesh.h diff --git a/src/igl/random_quaternion.cpp b/src/libigl/igl/random_quaternion.cpp similarity index 100% rename from src/igl/random_quaternion.cpp rename to src/libigl/igl/random_quaternion.cpp diff --git a/src/igl/random_quaternion.h b/src/libigl/igl/random_quaternion.h similarity index 100% rename from src/igl/random_quaternion.h rename to src/libigl/igl/random_quaternion.h diff --git a/src/igl/random_search.cpp b/src/libigl/igl/random_search.cpp similarity index 100% rename from src/igl/random_search.cpp rename to src/libigl/igl/random_search.cpp diff --git a/src/igl/random_search.h b/src/libigl/igl/random_search.h similarity index 100% rename from src/igl/random_search.h rename to src/libigl/igl/random_search.h diff --git a/src/igl/randperm.cpp b/src/libigl/igl/randperm.cpp similarity index 100% rename from src/igl/randperm.cpp rename to src/libigl/igl/randperm.cpp diff --git a/src/igl/randperm.h b/src/libigl/igl/randperm.h similarity index 100% rename from src/igl/randperm.h rename to src/libigl/igl/randperm.h diff --git a/src/igl/ray_box_intersect.cpp b/src/libigl/igl/ray_box_intersect.cpp similarity index 100% rename from src/igl/ray_box_intersect.cpp rename to src/libigl/igl/ray_box_intersect.cpp diff --git a/src/igl/ray_box_intersect.h b/src/libigl/igl/ray_box_intersect.h similarity index 100% rename from src/igl/ray_box_intersect.h rename to src/libigl/igl/ray_box_intersect.h diff --git a/src/igl/ray_mesh_intersect.cpp b/src/libigl/igl/ray_mesh_intersect.cpp similarity index 100% rename from src/igl/ray_mesh_intersect.cpp rename to src/libigl/igl/ray_mesh_intersect.cpp diff --git a/src/igl/ray_mesh_intersect.h b/src/libigl/igl/ray_mesh_intersect.h similarity index 100% rename from src/igl/ray_mesh_intersect.h rename to src/libigl/igl/ray_mesh_intersect.h diff --git a/src/igl/ray_sphere_intersect.cpp b/src/libigl/igl/ray_sphere_intersect.cpp similarity index 100% rename from src/igl/ray_sphere_intersect.cpp rename to src/libigl/igl/ray_sphere_intersect.cpp diff --git a/src/igl/ray_sphere_intersect.h b/src/libigl/igl/ray_sphere_intersect.h similarity index 100% rename from src/igl/ray_sphere_intersect.h rename to src/libigl/igl/ray_sphere_intersect.h diff --git a/src/igl/raytri.c b/src/libigl/igl/raytri.c similarity index 100% rename from src/igl/raytri.c rename to src/libigl/igl/raytri.c diff --git a/src/igl/readBF.cpp b/src/libigl/igl/readBF.cpp similarity index 100% rename from src/igl/readBF.cpp rename to src/libigl/igl/readBF.cpp diff --git a/src/igl/readBF.h b/src/libigl/igl/readBF.h similarity index 100% rename from src/igl/readBF.h rename to src/libigl/igl/readBF.h diff --git a/src/igl/readCSV.cpp b/src/libigl/igl/readCSV.cpp similarity index 100% rename from src/igl/readCSV.cpp rename to src/libigl/igl/readCSV.cpp diff --git a/src/igl/readCSV.h b/src/libigl/igl/readCSV.h similarity index 100% rename from src/igl/readCSV.h rename to src/libigl/igl/readCSV.h diff --git a/src/igl/readDMAT.cpp b/src/libigl/igl/readDMAT.cpp similarity index 100% rename from src/igl/readDMAT.cpp rename to src/libigl/igl/readDMAT.cpp diff --git a/src/igl/readDMAT.h b/src/libigl/igl/readDMAT.h similarity index 100% rename from src/igl/readDMAT.h rename to src/libigl/igl/readDMAT.h diff --git a/src/igl/readMESH.cpp b/src/libigl/igl/readMESH.cpp similarity index 100% rename from src/igl/readMESH.cpp rename to src/libigl/igl/readMESH.cpp diff --git a/src/igl/readMESH.h b/src/libigl/igl/readMESH.h similarity index 100% rename from src/igl/readMESH.h rename to src/libigl/igl/readMESH.h diff --git a/src/igl/readMSH.cpp b/src/libigl/igl/readMSH.cpp similarity index 100% rename from src/igl/readMSH.cpp rename to src/libigl/igl/readMSH.cpp diff --git a/src/igl/readMSH.h b/src/libigl/igl/readMSH.h similarity index 100% rename from src/igl/readMSH.h rename to src/libigl/igl/readMSH.h diff --git a/src/igl/readNODE.cpp b/src/libigl/igl/readNODE.cpp similarity index 100% rename from src/igl/readNODE.cpp rename to src/libigl/igl/readNODE.cpp diff --git a/src/igl/readNODE.h b/src/libigl/igl/readNODE.h similarity index 100% rename from src/igl/readNODE.h rename to src/libigl/igl/readNODE.h diff --git a/src/igl/readOBJ.cpp b/src/libigl/igl/readOBJ.cpp similarity index 100% rename from src/igl/readOBJ.cpp rename to src/libigl/igl/readOBJ.cpp diff --git a/src/igl/readOBJ.h b/src/libigl/igl/readOBJ.h similarity index 100% rename from src/igl/readOBJ.h rename to src/libigl/igl/readOBJ.h diff --git a/src/igl/readOFF.cpp b/src/libigl/igl/readOFF.cpp similarity index 100% rename from src/igl/readOFF.cpp rename to src/libigl/igl/readOFF.cpp diff --git a/src/igl/readOFF.h b/src/libigl/igl/readOFF.h similarity index 100% rename from src/igl/readOFF.h rename to src/libigl/igl/readOFF.h diff --git a/src/igl/readPLY.cpp b/src/libigl/igl/readPLY.cpp similarity index 100% rename from src/igl/readPLY.cpp rename to src/libigl/igl/readPLY.cpp diff --git a/src/igl/readPLY.h b/src/libigl/igl/readPLY.h similarity index 100% rename from src/igl/readPLY.h rename to src/libigl/igl/readPLY.h diff --git a/src/igl/readSTL.cpp b/src/libigl/igl/readSTL.cpp similarity index 100% rename from src/igl/readSTL.cpp rename to src/libigl/igl/readSTL.cpp diff --git a/src/igl/readSTL.h b/src/libigl/igl/readSTL.h similarity index 100% rename from src/igl/readSTL.h rename to src/libigl/igl/readSTL.h diff --git a/src/igl/readTGF.cpp b/src/libigl/igl/readTGF.cpp similarity index 100% rename from src/igl/readTGF.cpp rename to src/libigl/igl/readTGF.cpp diff --git a/src/igl/readTGF.h b/src/libigl/igl/readTGF.h similarity index 100% rename from src/igl/readTGF.h rename to src/libigl/igl/readTGF.h diff --git a/src/igl/readWRL.cpp b/src/libigl/igl/readWRL.cpp similarity index 100% rename from src/igl/readWRL.cpp rename to src/libigl/igl/readWRL.cpp diff --git a/src/igl/readWRL.h b/src/libigl/igl/readWRL.h similarity index 100% rename from src/igl/readWRL.h rename to src/libigl/igl/readWRL.h diff --git a/src/igl/read_triangle_mesh.cpp b/src/libigl/igl/read_triangle_mesh.cpp similarity index 100% rename from src/igl/read_triangle_mesh.cpp rename to src/libigl/igl/read_triangle_mesh.cpp diff --git a/src/igl/read_triangle_mesh.h b/src/libigl/igl/read_triangle_mesh.h similarity index 100% rename from src/igl/read_triangle_mesh.h rename to src/libigl/igl/read_triangle_mesh.h diff --git a/src/igl/redux.h b/src/libigl/igl/redux.h similarity index 100% rename from src/igl/redux.h rename to src/libigl/igl/redux.h diff --git a/src/igl/remesh_along_isoline.cpp b/src/libigl/igl/remesh_along_isoline.cpp similarity index 100% rename from src/igl/remesh_along_isoline.cpp rename to src/libigl/igl/remesh_along_isoline.cpp diff --git a/src/igl/remesh_along_isoline.h b/src/libigl/igl/remesh_along_isoline.h similarity index 100% rename from src/igl/remesh_along_isoline.h rename to src/libigl/igl/remesh_along_isoline.h diff --git a/src/igl/remove_duplicate_vertices.cpp b/src/libigl/igl/remove_duplicate_vertices.cpp similarity index 100% rename from src/igl/remove_duplicate_vertices.cpp rename to src/libigl/igl/remove_duplicate_vertices.cpp diff --git a/src/igl/remove_duplicate_vertices.h b/src/libigl/igl/remove_duplicate_vertices.h similarity index 100% rename from src/igl/remove_duplicate_vertices.h rename to src/libigl/igl/remove_duplicate_vertices.h diff --git a/src/igl/remove_duplicates.cpp b/src/libigl/igl/remove_duplicates.cpp similarity index 100% rename from src/igl/remove_duplicates.cpp rename to src/libigl/igl/remove_duplicates.cpp diff --git a/src/igl/remove_duplicates.h b/src/libigl/igl/remove_duplicates.h similarity index 100% rename from src/igl/remove_duplicates.h rename to src/libigl/igl/remove_duplicates.h diff --git a/src/igl/remove_unreferenced.cpp b/src/libigl/igl/remove_unreferenced.cpp similarity index 100% rename from src/igl/remove_unreferenced.cpp rename to src/libigl/igl/remove_unreferenced.cpp diff --git a/src/igl/remove_unreferenced.h b/src/libigl/igl/remove_unreferenced.h similarity index 100% rename from src/igl/remove_unreferenced.h rename to src/libigl/igl/remove_unreferenced.h diff --git a/src/igl/reorder.cpp b/src/libigl/igl/reorder.cpp similarity index 100% rename from src/igl/reorder.cpp rename to src/libigl/igl/reorder.cpp diff --git a/src/igl/reorder.h b/src/libigl/igl/reorder.h similarity index 100% rename from src/igl/reorder.h rename to src/libigl/igl/reorder.h diff --git a/src/igl/repdiag.cpp b/src/libigl/igl/repdiag.cpp similarity index 100% rename from src/igl/repdiag.cpp rename to src/libigl/igl/repdiag.cpp diff --git a/src/igl/repdiag.h b/src/libigl/igl/repdiag.h similarity index 100% rename from src/igl/repdiag.h rename to src/libigl/igl/repdiag.h diff --git a/src/igl/repmat.cpp b/src/libigl/igl/repmat.cpp similarity index 100% rename from src/igl/repmat.cpp rename to src/libigl/igl/repmat.cpp diff --git a/src/igl/repmat.h b/src/libigl/igl/repmat.h similarity index 100% rename from src/igl/repmat.h rename to src/libigl/igl/repmat.h diff --git a/src/igl/resolve_duplicated_faces.cpp b/src/libigl/igl/resolve_duplicated_faces.cpp similarity index 100% rename from src/igl/resolve_duplicated_faces.cpp rename to src/libigl/igl/resolve_duplicated_faces.cpp diff --git a/src/igl/resolve_duplicated_faces.h b/src/libigl/igl/resolve_duplicated_faces.h similarity index 100% rename from src/igl/resolve_duplicated_faces.h rename to src/libigl/igl/resolve_duplicated_faces.h diff --git a/src/igl/rgb_to_hsv.cpp b/src/libigl/igl/rgb_to_hsv.cpp similarity index 100% rename from src/igl/rgb_to_hsv.cpp rename to src/libigl/igl/rgb_to_hsv.cpp diff --git a/src/igl/rgb_to_hsv.h b/src/libigl/igl/rgb_to_hsv.h similarity index 100% rename from src/igl/rgb_to_hsv.h rename to src/libigl/igl/rgb_to_hsv.h diff --git a/src/igl/rotate_by_quat.cpp b/src/libigl/igl/rotate_by_quat.cpp similarity index 100% rename from src/igl/rotate_by_quat.cpp rename to src/libigl/igl/rotate_by_quat.cpp diff --git a/src/igl/rotate_by_quat.h b/src/libigl/igl/rotate_by_quat.h similarity index 100% rename from src/igl/rotate_by_quat.h rename to src/libigl/igl/rotate_by_quat.h diff --git a/src/igl/rotate_vectors.cpp b/src/libigl/igl/rotate_vectors.cpp similarity index 100% rename from src/igl/rotate_vectors.cpp rename to src/libigl/igl/rotate_vectors.cpp diff --git a/src/igl/rotate_vectors.h b/src/libigl/igl/rotate_vectors.h similarity index 100% rename from src/igl/rotate_vectors.h rename to src/libigl/igl/rotate_vectors.h diff --git a/src/igl/rotation_matrix_from_directions.cpp b/src/libigl/igl/rotation_matrix_from_directions.cpp similarity index 100% rename from src/igl/rotation_matrix_from_directions.cpp rename to src/libigl/igl/rotation_matrix_from_directions.cpp diff --git a/src/igl/rotation_matrix_from_directions.h b/src/libigl/igl/rotation_matrix_from_directions.h similarity index 100% rename from src/igl/rotation_matrix_from_directions.h rename to src/libigl/igl/rotation_matrix_from_directions.h diff --git a/src/igl/round.cpp b/src/libigl/igl/round.cpp similarity index 100% rename from src/igl/round.cpp rename to src/libigl/igl/round.cpp diff --git a/src/igl/round.h b/src/libigl/igl/round.h similarity index 100% rename from src/igl/round.h rename to src/libigl/igl/round.h diff --git a/src/igl/rows_to_matrix.cpp b/src/libigl/igl/rows_to_matrix.cpp similarity index 100% rename from src/igl/rows_to_matrix.cpp rename to src/libigl/igl/rows_to_matrix.cpp diff --git a/src/igl/rows_to_matrix.h b/src/libigl/igl/rows_to_matrix.h similarity index 100% rename from src/igl/rows_to_matrix.h rename to src/libigl/igl/rows_to_matrix.h diff --git a/src/igl/sample_edges.cpp b/src/libigl/igl/sample_edges.cpp similarity index 100% rename from src/igl/sample_edges.cpp rename to src/libigl/igl/sample_edges.cpp diff --git a/src/igl/sample_edges.h b/src/libigl/igl/sample_edges.h similarity index 100% rename from src/igl/sample_edges.h rename to src/libigl/igl/sample_edges.h diff --git a/src/igl/seam_edges.cpp b/src/libigl/igl/seam_edges.cpp similarity index 100% rename from src/igl/seam_edges.cpp rename to src/libigl/igl/seam_edges.cpp diff --git a/src/igl/seam_edges.h b/src/libigl/igl/seam_edges.h similarity index 100% rename from src/igl/seam_edges.h rename to src/libigl/igl/seam_edges.h diff --git a/src/igl/segment_segment_intersect.cpp b/src/libigl/igl/segment_segment_intersect.cpp similarity index 100% rename from src/igl/segment_segment_intersect.cpp rename to src/libigl/igl/segment_segment_intersect.cpp diff --git a/src/igl/segment_segment_intersect.h b/src/libigl/igl/segment_segment_intersect.h similarity index 100% rename from src/igl/segment_segment_intersect.h rename to src/libigl/igl/segment_segment_intersect.h diff --git a/src/igl/serialize.h b/src/libigl/igl/serialize.h similarity index 100% rename from src/igl/serialize.h rename to src/libigl/igl/serialize.h diff --git a/src/igl/setdiff.cpp b/src/libigl/igl/setdiff.cpp similarity index 100% rename from src/igl/setdiff.cpp rename to src/libigl/igl/setdiff.cpp diff --git a/src/igl/setdiff.h b/src/libigl/igl/setdiff.h similarity index 100% rename from src/igl/setdiff.h rename to src/libigl/igl/setdiff.h diff --git a/src/igl/setunion.cpp b/src/libigl/igl/setunion.cpp similarity index 100% rename from src/igl/setunion.cpp rename to src/libigl/igl/setunion.cpp diff --git a/src/igl/setunion.h b/src/libigl/igl/setunion.h similarity index 100% rename from src/igl/setunion.h rename to src/libigl/igl/setunion.h diff --git a/src/igl/setxor.cpp b/src/libigl/igl/setxor.cpp similarity index 100% rename from src/igl/setxor.cpp rename to src/libigl/igl/setxor.cpp diff --git a/src/igl/setxor.h b/src/libigl/igl/setxor.h similarity index 100% rename from src/igl/setxor.h rename to src/libigl/igl/setxor.h diff --git a/src/igl/shape_diameter_function.cpp b/src/libigl/igl/shape_diameter_function.cpp similarity index 100% rename from src/igl/shape_diameter_function.cpp rename to src/libigl/igl/shape_diameter_function.cpp diff --git a/src/igl/shape_diameter_function.h b/src/libigl/igl/shape_diameter_function.h similarity index 100% rename from src/igl/shape_diameter_function.h rename to src/libigl/igl/shape_diameter_function.h diff --git a/src/igl/shapeup.cpp b/src/libigl/igl/shapeup.cpp similarity index 100% rename from src/igl/shapeup.cpp rename to src/libigl/igl/shapeup.cpp diff --git a/src/igl/shapeup.h b/src/libigl/igl/shapeup.h similarity index 100% rename from src/igl/shapeup.h rename to src/libigl/igl/shapeup.h diff --git a/src/igl/shortest_edge_and_midpoint.cpp b/src/libigl/igl/shortest_edge_and_midpoint.cpp similarity index 100% rename from src/igl/shortest_edge_and_midpoint.cpp rename to src/libigl/igl/shortest_edge_and_midpoint.cpp diff --git a/src/igl/shortest_edge_and_midpoint.h b/src/libigl/igl/shortest_edge_and_midpoint.h similarity index 100% rename from src/igl/shortest_edge_and_midpoint.h rename to src/libigl/igl/shortest_edge_and_midpoint.h diff --git a/src/igl/signed_angle.cpp b/src/libigl/igl/signed_angle.cpp similarity index 100% rename from src/igl/signed_angle.cpp rename to src/libigl/igl/signed_angle.cpp diff --git a/src/igl/signed_angle.h b/src/libigl/igl/signed_angle.h similarity index 100% rename from src/igl/signed_angle.h rename to src/libigl/igl/signed_angle.h diff --git a/src/igl/signed_distance.cpp b/src/libigl/igl/signed_distance.cpp similarity index 100% rename from src/igl/signed_distance.cpp rename to src/libigl/igl/signed_distance.cpp diff --git a/src/igl/signed_distance.h b/src/libigl/igl/signed_distance.h similarity index 100% rename from src/igl/signed_distance.h rename to src/libigl/igl/signed_distance.h diff --git a/src/igl/simplify_polyhedron.cpp b/src/libigl/igl/simplify_polyhedron.cpp similarity index 100% rename from src/igl/simplify_polyhedron.cpp rename to src/libigl/igl/simplify_polyhedron.cpp diff --git a/src/igl/simplify_polyhedron.h b/src/libigl/igl/simplify_polyhedron.h similarity index 100% rename from src/igl/simplify_polyhedron.h rename to src/libigl/igl/simplify_polyhedron.h diff --git a/src/igl/slice.cpp b/src/libigl/igl/slice.cpp similarity index 100% rename from src/igl/slice.cpp rename to src/libigl/igl/slice.cpp diff --git a/src/igl/slice.h b/src/libigl/igl/slice.h similarity index 100% rename from src/igl/slice.h rename to src/libigl/igl/slice.h diff --git a/src/igl/slice_cached.cpp b/src/libigl/igl/slice_cached.cpp similarity index 100% rename from src/igl/slice_cached.cpp rename to src/libigl/igl/slice_cached.cpp diff --git a/src/igl/slice_cached.h b/src/libigl/igl/slice_cached.h similarity index 100% rename from src/igl/slice_cached.h rename to src/libigl/igl/slice_cached.h diff --git a/src/igl/slice_into.cpp b/src/libigl/igl/slice_into.cpp similarity index 100% rename from src/igl/slice_into.cpp rename to src/libigl/igl/slice_into.cpp diff --git a/src/igl/slice_into.h b/src/libigl/igl/slice_into.h similarity index 100% rename from src/igl/slice_into.h rename to src/libigl/igl/slice_into.h diff --git a/src/igl/slice_mask.cpp b/src/libigl/igl/slice_mask.cpp similarity index 100% rename from src/igl/slice_mask.cpp rename to src/libigl/igl/slice_mask.cpp diff --git a/src/igl/slice_mask.h b/src/libigl/igl/slice_mask.h similarity index 100% rename from src/igl/slice_mask.h rename to src/libigl/igl/slice_mask.h diff --git a/src/igl/slice_tets.cpp b/src/libigl/igl/slice_tets.cpp similarity index 100% rename from src/igl/slice_tets.cpp rename to src/libigl/igl/slice_tets.cpp diff --git a/src/igl/slice_tets.h b/src/libigl/igl/slice_tets.h similarity index 100% rename from src/igl/slice_tets.h rename to src/libigl/igl/slice_tets.h diff --git a/src/igl/slim.cpp b/src/libigl/igl/slim.cpp similarity index 100% rename from src/igl/slim.cpp rename to src/libigl/igl/slim.cpp diff --git a/src/igl/slim.h b/src/libigl/igl/slim.h similarity index 100% rename from src/igl/slim.h rename to src/libigl/igl/slim.h diff --git a/src/igl/snap_points.cpp b/src/libigl/igl/snap_points.cpp similarity index 100% rename from src/igl/snap_points.cpp rename to src/libigl/igl/snap_points.cpp diff --git a/src/igl/snap_points.h b/src/libigl/igl/snap_points.h similarity index 100% rename from src/igl/snap_points.h rename to src/libigl/igl/snap_points.h diff --git a/src/igl/snap_to_canonical_view_quat.cpp b/src/libigl/igl/snap_to_canonical_view_quat.cpp similarity index 100% rename from src/igl/snap_to_canonical_view_quat.cpp rename to src/libigl/igl/snap_to_canonical_view_quat.cpp diff --git a/src/igl/snap_to_canonical_view_quat.h b/src/libigl/igl/snap_to_canonical_view_quat.h similarity index 100% rename from src/igl/snap_to_canonical_view_quat.h rename to src/libigl/igl/snap_to_canonical_view_quat.h diff --git a/src/igl/snap_to_fixed_up.cpp b/src/libigl/igl/snap_to_fixed_up.cpp similarity index 100% rename from src/igl/snap_to_fixed_up.cpp rename to src/libigl/igl/snap_to_fixed_up.cpp diff --git a/src/igl/snap_to_fixed_up.h b/src/libigl/igl/snap_to_fixed_up.h similarity index 100% rename from src/igl/snap_to_fixed_up.h rename to src/libigl/igl/snap_to_fixed_up.h diff --git a/src/igl/solid_angle.cpp b/src/libigl/igl/solid_angle.cpp similarity index 100% rename from src/igl/solid_angle.cpp rename to src/libigl/igl/solid_angle.cpp diff --git a/src/igl/solid_angle.h b/src/libigl/igl/solid_angle.h similarity index 100% rename from src/igl/solid_angle.h rename to src/libigl/igl/solid_angle.h diff --git a/src/igl/sort.cpp b/src/libigl/igl/sort.cpp similarity index 100% rename from src/igl/sort.cpp rename to src/libigl/igl/sort.cpp diff --git a/src/igl/sort.h b/src/libigl/igl/sort.h similarity index 100% rename from src/igl/sort.h rename to src/libigl/igl/sort.h diff --git a/src/igl/sort_angles.cpp b/src/libigl/igl/sort_angles.cpp similarity index 100% rename from src/igl/sort_angles.cpp rename to src/libigl/igl/sort_angles.cpp diff --git a/src/igl/sort_angles.h b/src/libigl/igl/sort_angles.h similarity index 100% rename from src/igl/sort_angles.h rename to src/libigl/igl/sort_angles.h diff --git a/src/igl/sort_triangles.cpp b/src/libigl/igl/sort_triangles.cpp similarity index 100% rename from src/igl/sort_triangles.cpp rename to src/libigl/igl/sort_triangles.cpp diff --git a/src/igl/sort_triangles.h b/src/libigl/igl/sort_triangles.h similarity index 100% rename from src/igl/sort_triangles.h rename to src/libigl/igl/sort_triangles.h diff --git a/src/igl/sort_vectors_ccw.cpp b/src/libigl/igl/sort_vectors_ccw.cpp similarity index 100% rename from src/igl/sort_vectors_ccw.cpp rename to src/libigl/igl/sort_vectors_ccw.cpp diff --git a/src/igl/sort_vectors_ccw.h b/src/libigl/igl/sort_vectors_ccw.h similarity index 100% rename from src/igl/sort_vectors_ccw.h rename to src/libigl/igl/sort_vectors_ccw.h diff --git a/src/igl/sortrows.cpp b/src/libigl/igl/sortrows.cpp similarity index 100% rename from src/igl/sortrows.cpp rename to src/libigl/igl/sortrows.cpp diff --git a/src/igl/sortrows.h b/src/libigl/igl/sortrows.h similarity index 100% rename from src/igl/sortrows.h rename to src/libigl/igl/sortrows.h diff --git a/src/igl/sparse.cpp b/src/libigl/igl/sparse.cpp similarity index 100% rename from src/igl/sparse.cpp rename to src/libigl/igl/sparse.cpp diff --git a/src/igl/sparse.h b/src/libigl/igl/sparse.h similarity index 100% rename from src/igl/sparse.h rename to src/libigl/igl/sparse.h diff --git a/src/igl/sparse_cached.cpp b/src/libigl/igl/sparse_cached.cpp similarity index 100% rename from src/igl/sparse_cached.cpp rename to src/libigl/igl/sparse_cached.cpp diff --git a/src/igl/sparse_cached.h b/src/libigl/igl/sparse_cached.h similarity index 100% rename from src/igl/sparse_cached.h rename to src/libigl/igl/sparse_cached.h diff --git a/src/igl/speye.cpp b/src/libigl/igl/speye.cpp similarity index 100% rename from src/igl/speye.cpp rename to src/libigl/igl/speye.cpp diff --git a/src/igl/speye.h b/src/libigl/igl/speye.h similarity index 100% rename from src/igl/speye.h rename to src/libigl/igl/speye.h diff --git a/src/igl/squared_edge_lengths.cpp b/src/libigl/igl/squared_edge_lengths.cpp similarity index 100% rename from src/igl/squared_edge_lengths.cpp rename to src/libigl/igl/squared_edge_lengths.cpp diff --git a/src/igl/squared_edge_lengths.h b/src/libigl/igl/squared_edge_lengths.h similarity index 100% rename from src/igl/squared_edge_lengths.h rename to src/libigl/igl/squared_edge_lengths.h diff --git a/src/igl/stdin_to_temp.cpp b/src/libigl/igl/stdin_to_temp.cpp similarity index 100% rename from src/igl/stdin_to_temp.cpp rename to src/libigl/igl/stdin_to_temp.cpp diff --git a/src/igl/stdin_to_temp.h b/src/libigl/igl/stdin_to_temp.h similarity index 100% rename from src/igl/stdin_to_temp.h rename to src/libigl/igl/stdin_to_temp.h diff --git a/src/igl/straighten_seams.cpp b/src/libigl/igl/straighten_seams.cpp similarity index 100% rename from src/igl/straighten_seams.cpp rename to src/libigl/igl/straighten_seams.cpp diff --git a/src/igl/straighten_seams.h b/src/libigl/igl/straighten_seams.h similarity index 100% rename from src/igl/straighten_seams.h rename to src/libigl/igl/straighten_seams.h diff --git a/src/igl/sum.cpp b/src/libigl/igl/sum.cpp similarity index 100% rename from src/igl/sum.cpp rename to src/libigl/igl/sum.cpp diff --git a/src/igl/sum.h b/src/libigl/igl/sum.h similarity index 100% rename from src/igl/sum.h rename to src/libigl/igl/sum.h diff --git a/src/igl/svd3x3.cpp b/src/libigl/igl/svd3x3.cpp similarity index 100% rename from src/igl/svd3x3.cpp rename to src/libigl/igl/svd3x3.cpp diff --git a/src/igl/svd3x3.h b/src/libigl/igl/svd3x3.h similarity index 100% rename from src/igl/svd3x3.h rename to src/libigl/igl/svd3x3.h diff --git a/src/igl/svd3x3_avx.cpp b/src/libigl/igl/svd3x3_avx.cpp similarity index 100% rename from src/igl/svd3x3_avx.cpp rename to src/libigl/igl/svd3x3_avx.cpp diff --git a/src/igl/svd3x3_avx.h b/src/libigl/igl/svd3x3_avx.h similarity index 100% rename from src/igl/svd3x3_avx.h rename to src/libigl/igl/svd3x3_avx.h diff --git a/src/igl/svd3x3_sse.cpp b/src/libigl/igl/svd3x3_sse.cpp similarity index 100% rename from src/igl/svd3x3_sse.cpp rename to src/libigl/igl/svd3x3_sse.cpp diff --git a/src/igl/svd3x3_sse.h b/src/libigl/igl/svd3x3_sse.h similarity index 100% rename from src/igl/svd3x3_sse.h rename to src/libigl/igl/svd3x3_sse.h diff --git a/src/igl/swept_volume_bounding_box.cpp b/src/libigl/igl/swept_volume_bounding_box.cpp similarity index 100% rename from src/igl/swept_volume_bounding_box.cpp rename to src/libigl/igl/swept_volume_bounding_box.cpp diff --git a/src/igl/swept_volume_bounding_box.h b/src/libigl/igl/swept_volume_bounding_box.h similarity index 100% rename from src/igl/swept_volume_bounding_box.h rename to src/libigl/igl/swept_volume_bounding_box.h diff --git a/src/igl/swept_volume_signed_distance.cpp b/src/libigl/igl/swept_volume_signed_distance.cpp similarity index 100% rename from src/igl/swept_volume_signed_distance.cpp rename to src/libigl/igl/swept_volume_signed_distance.cpp diff --git a/src/igl/swept_volume_signed_distance.h b/src/libigl/igl/swept_volume_signed_distance.h similarity index 100% rename from src/igl/swept_volume_signed_distance.h rename to src/libigl/igl/swept_volume_signed_distance.h diff --git a/src/igl/trackball.cpp b/src/libigl/igl/trackball.cpp similarity index 100% rename from src/igl/trackball.cpp rename to src/libigl/igl/trackball.cpp diff --git a/src/igl/trackball.h b/src/libigl/igl/trackball.h similarity index 100% rename from src/igl/trackball.h rename to src/libigl/igl/trackball.h diff --git a/src/igl/transpose_blocks.cpp b/src/libigl/igl/transpose_blocks.cpp similarity index 100% rename from src/igl/transpose_blocks.cpp rename to src/libigl/igl/transpose_blocks.cpp diff --git a/src/igl/transpose_blocks.h b/src/libigl/igl/transpose_blocks.h similarity index 100% rename from src/igl/transpose_blocks.h rename to src/libigl/igl/transpose_blocks.h diff --git a/src/igl/triangle/cdt.cpp b/src/libigl/igl/triangle/cdt.cpp similarity index 100% rename from src/igl/triangle/cdt.cpp rename to src/libigl/igl/triangle/cdt.cpp diff --git a/src/igl/triangle/cdt.h b/src/libigl/igl/triangle/cdt.h similarity index 100% rename from src/igl/triangle/cdt.h rename to src/libigl/igl/triangle/cdt.h diff --git a/src/igl/triangle/triangulate.cpp b/src/libigl/igl/triangle/triangulate.cpp similarity index 100% rename from src/igl/triangle/triangulate.cpp rename to src/libigl/igl/triangle/triangulate.cpp diff --git a/src/igl/triangle/triangulate.h b/src/libigl/igl/triangle/triangulate.h similarity index 100% rename from src/igl/triangle/triangulate.h rename to src/libigl/igl/triangle/triangulate.h diff --git a/src/igl/triangle_fan.cpp b/src/libigl/igl/triangle_fan.cpp similarity index 100% rename from src/igl/triangle_fan.cpp rename to src/libigl/igl/triangle_fan.cpp diff --git a/src/igl/triangle_fan.h b/src/libigl/igl/triangle_fan.h similarity index 100% rename from src/igl/triangle_fan.h rename to src/libigl/igl/triangle_fan.h diff --git a/src/igl/triangle_triangle_adjacency.cpp b/src/libigl/igl/triangle_triangle_adjacency.cpp similarity index 100% rename from src/igl/triangle_triangle_adjacency.cpp rename to src/libigl/igl/triangle_triangle_adjacency.cpp diff --git a/src/igl/triangle_triangle_adjacency.h b/src/libigl/igl/triangle_triangle_adjacency.h similarity index 100% rename from src/igl/triangle_triangle_adjacency.h rename to src/libigl/igl/triangle_triangle_adjacency.h diff --git a/src/igl/triangles_from_strip.cpp b/src/libigl/igl/triangles_from_strip.cpp similarity index 100% rename from src/igl/triangles_from_strip.cpp rename to src/libigl/igl/triangles_from_strip.cpp diff --git a/src/igl/triangles_from_strip.h b/src/libigl/igl/triangles_from_strip.h similarity index 100% rename from src/igl/triangles_from_strip.h rename to src/libigl/igl/triangles_from_strip.h diff --git a/src/igl/two_axis_valuator_fixed_up.cpp b/src/libigl/igl/two_axis_valuator_fixed_up.cpp similarity index 100% rename from src/igl/two_axis_valuator_fixed_up.cpp rename to src/libigl/igl/two_axis_valuator_fixed_up.cpp diff --git a/src/igl/two_axis_valuator_fixed_up.h b/src/libigl/igl/two_axis_valuator_fixed_up.h similarity index 100% rename from src/igl/two_axis_valuator_fixed_up.h rename to src/libigl/igl/two_axis_valuator_fixed_up.h diff --git a/src/igl/uniformly_sample_two_manifold.cpp b/src/libigl/igl/uniformly_sample_two_manifold.cpp similarity index 100% rename from src/igl/uniformly_sample_two_manifold.cpp rename to src/libigl/igl/uniformly_sample_two_manifold.cpp diff --git a/src/igl/uniformly_sample_two_manifold.h b/src/libigl/igl/uniformly_sample_two_manifold.h similarity index 100% rename from src/igl/uniformly_sample_two_manifold.h rename to src/libigl/igl/uniformly_sample_two_manifold.h diff --git a/src/igl/unique.cpp b/src/libigl/igl/unique.cpp similarity index 100% rename from src/igl/unique.cpp rename to src/libigl/igl/unique.cpp diff --git a/src/igl/unique.h b/src/libigl/igl/unique.h similarity index 100% rename from src/igl/unique.h rename to src/libigl/igl/unique.h diff --git a/src/igl/unique_edge_map.cpp b/src/libigl/igl/unique_edge_map.cpp similarity index 100% rename from src/igl/unique_edge_map.cpp rename to src/libigl/igl/unique_edge_map.cpp diff --git a/src/igl/unique_edge_map.h b/src/libigl/igl/unique_edge_map.h similarity index 100% rename from src/igl/unique_edge_map.h rename to src/libigl/igl/unique_edge_map.h diff --git a/src/igl/unique_rows.cpp b/src/libigl/igl/unique_rows.cpp similarity index 100% rename from src/igl/unique_rows.cpp rename to src/libigl/igl/unique_rows.cpp diff --git a/src/igl/unique_rows.h b/src/libigl/igl/unique_rows.h similarity index 100% rename from src/igl/unique_rows.h rename to src/libigl/igl/unique_rows.h diff --git a/src/igl/unique_simplices.cpp b/src/libigl/igl/unique_simplices.cpp similarity index 100% rename from src/igl/unique_simplices.cpp rename to src/libigl/igl/unique_simplices.cpp diff --git a/src/igl/unique_simplices.h b/src/libigl/igl/unique_simplices.h similarity index 100% rename from src/igl/unique_simplices.h rename to src/libigl/igl/unique_simplices.h diff --git a/src/igl/unproject.cpp b/src/libigl/igl/unproject.cpp similarity index 100% rename from src/igl/unproject.cpp rename to src/libigl/igl/unproject.cpp diff --git a/src/igl/unproject.h b/src/libigl/igl/unproject.h similarity index 100% rename from src/igl/unproject.h rename to src/libigl/igl/unproject.h diff --git a/src/igl/unproject_in_mesh.cpp b/src/libigl/igl/unproject_in_mesh.cpp similarity index 100% rename from src/igl/unproject_in_mesh.cpp rename to src/libigl/igl/unproject_in_mesh.cpp diff --git a/src/igl/unproject_in_mesh.h b/src/libigl/igl/unproject_in_mesh.h similarity index 100% rename from src/igl/unproject_in_mesh.h rename to src/libigl/igl/unproject_in_mesh.h diff --git a/src/igl/unproject_onto_mesh.cpp b/src/libigl/igl/unproject_onto_mesh.cpp similarity index 100% rename from src/igl/unproject_onto_mesh.cpp rename to src/libigl/igl/unproject_onto_mesh.cpp diff --git a/src/igl/unproject_onto_mesh.h b/src/libigl/igl/unproject_onto_mesh.h similarity index 100% rename from src/igl/unproject_onto_mesh.h rename to src/libigl/igl/unproject_onto_mesh.h diff --git a/src/igl/unproject_ray.cpp b/src/libigl/igl/unproject_ray.cpp similarity index 100% rename from src/igl/unproject_ray.cpp rename to src/libigl/igl/unproject_ray.cpp diff --git a/src/igl/unproject_ray.h b/src/libigl/igl/unproject_ray.h similarity index 100% rename from src/igl/unproject_ray.h rename to src/libigl/igl/unproject_ray.h diff --git a/src/igl/unzip_corners.cpp b/src/libigl/igl/unzip_corners.cpp similarity index 100% rename from src/igl/unzip_corners.cpp rename to src/libigl/igl/unzip_corners.cpp diff --git a/src/igl/unzip_corners.h b/src/libigl/igl/unzip_corners.h similarity index 100% rename from src/igl/unzip_corners.h rename to src/libigl/igl/unzip_corners.h diff --git a/src/igl/upsample.cpp b/src/libigl/igl/upsample.cpp similarity index 100% rename from src/igl/upsample.cpp rename to src/libigl/igl/upsample.cpp diff --git a/src/igl/upsample.h b/src/libigl/igl/upsample.h similarity index 100% rename from src/igl/upsample.h rename to src/libigl/igl/upsample.h diff --git a/src/igl/vector_area_matrix.cpp b/src/libigl/igl/vector_area_matrix.cpp similarity index 100% rename from src/igl/vector_area_matrix.cpp rename to src/libigl/igl/vector_area_matrix.cpp diff --git a/src/igl/vector_area_matrix.h b/src/libigl/igl/vector_area_matrix.h similarity index 100% rename from src/igl/vector_area_matrix.h rename to src/libigl/igl/vector_area_matrix.h diff --git a/src/igl/verbose.h b/src/libigl/igl/verbose.h similarity index 100% rename from src/igl/verbose.h rename to src/libigl/igl/verbose.h diff --git a/src/igl/vertex_triangle_adjacency.cpp b/src/libigl/igl/vertex_triangle_adjacency.cpp similarity index 100% rename from src/igl/vertex_triangle_adjacency.cpp rename to src/libigl/igl/vertex_triangle_adjacency.cpp diff --git a/src/igl/vertex_triangle_adjacency.h b/src/libigl/igl/vertex_triangle_adjacency.h similarity index 100% rename from src/igl/vertex_triangle_adjacency.h rename to src/libigl/igl/vertex_triangle_adjacency.h diff --git a/src/igl/volume.cpp b/src/libigl/igl/volume.cpp similarity index 100% rename from src/igl/volume.cpp rename to src/libigl/igl/volume.cpp diff --git a/src/igl/volume.h b/src/libigl/igl/volume.h similarity index 100% rename from src/igl/volume.h rename to src/libigl/igl/volume.h diff --git a/src/igl/voxel_grid.cpp b/src/libigl/igl/voxel_grid.cpp similarity index 100% rename from src/igl/voxel_grid.cpp rename to src/libigl/igl/voxel_grid.cpp diff --git a/src/igl/voxel_grid.h b/src/libigl/igl/voxel_grid.h similarity index 100% rename from src/igl/voxel_grid.h rename to src/libigl/igl/voxel_grid.h diff --git a/src/igl/winding_number.cpp b/src/libigl/igl/winding_number.cpp similarity index 100% rename from src/igl/winding_number.cpp rename to src/libigl/igl/winding_number.cpp diff --git a/src/igl/winding_number.h b/src/libigl/igl/winding_number.h similarity index 100% rename from src/igl/winding_number.h rename to src/libigl/igl/winding_number.h diff --git a/src/igl/writeBF.cpp b/src/libigl/igl/writeBF.cpp similarity index 100% rename from src/igl/writeBF.cpp rename to src/libigl/igl/writeBF.cpp diff --git a/src/igl/writeBF.h b/src/libigl/igl/writeBF.h similarity index 100% rename from src/igl/writeBF.h rename to src/libigl/igl/writeBF.h diff --git a/src/igl/writeDMAT.cpp b/src/libigl/igl/writeDMAT.cpp similarity index 100% rename from src/igl/writeDMAT.cpp rename to src/libigl/igl/writeDMAT.cpp diff --git a/src/igl/writeDMAT.h b/src/libigl/igl/writeDMAT.h similarity index 100% rename from src/igl/writeDMAT.h rename to src/libigl/igl/writeDMAT.h diff --git a/src/igl/writeMESH.cpp b/src/libigl/igl/writeMESH.cpp similarity index 100% rename from src/igl/writeMESH.cpp rename to src/libigl/igl/writeMESH.cpp diff --git a/src/igl/writeMESH.h b/src/libigl/igl/writeMESH.h similarity index 100% rename from src/igl/writeMESH.h rename to src/libigl/igl/writeMESH.h diff --git a/src/igl/writeOBJ.cpp b/src/libigl/igl/writeOBJ.cpp similarity index 100% rename from src/igl/writeOBJ.cpp rename to src/libigl/igl/writeOBJ.cpp diff --git a/src/igl/writeOBJ.h b/src/libigl/igl/writeOBJ.h similarity index 100% rename from src/igl/writeOBJ.h rename to src/libigl/igl/writeOBJ.h diff --git a/src/igl/writeOFF.cpp b/src/libigl/igl/writeOFF.cpp similarity index 100% rename from src/igl/writeOFF.cpp rename to src/libigl/igl/writeOFF.cpp diff --git a/src/igl/writeOFF.h b/src/libigl/igl/writeOFF.h similarity index 100% rename from src/igl/writeOFF.h rename to src/libigl/igl/writeOFF.h diff --git a/src/igl/writePLY.cpp b/src/libigl/igl/writePLY.cpp similarity index 100% rename from src/igl/writePLY.cpp rename to src/libigl/igl/writePLY.cpp diff --git a/src/igl/writePLY.h b/src/libigl/igl/writePLY.h similarity index 100% rename from src/igl/writePLY.h rename to src/libigl/igl/writePLY.h diff --git a/src/igl/writeSTL.cpp b/src/libigl/igl/writeSTL.cpp similarity index 100% rename from src/igl/writeSTL.cpp rename to src/libigl/igl/writeSTL.cpp diff --git a/src/igl/writeSTL.h b/src/libigl/igl/writeSTL.h similarity index 100% rename from src/igl/writeSTL.h rename to src/libigl/igl/writeSTL.h diff --git a/src/igl/writeTGF.cpp b/src/libigl/igl/writeTGF.cpp similarity index 100% rename from src/igl/writeTGF.cpp rename to src/libigl/igl/writeTGF.cpp diff --git a/src/igl/writeTGF.h b/src/libigl/igl/writeTGF.h similarity index 100% rename from src/igl/writeTGF.h rename to src/libigl/igl/writeTGF.h diff --git a/src/igl/writeWRL.cpp b/src/libigl/igl/writeWRL.cpp similarity index 100% rename from src/igl/writeWRL.cpp rename to src/libigl/igl/writeWRL.cpp diff --git a/src/igl/writeWRL.h b/src/libigl/igl/writeWRL.h similarity index 100% rename from src/igl/writeWRL.h rename to src/libigl/igl/writeWRL.h diff --git a/src/igl/write_triangle_mesh.cpp b/src/libigl/igl/write_triangle_mesh.cpp similarity index 100% rename from src/igl/write_triangle_mesh.cpp rename to src/libigl/igl/write_triangle_mesh.cpp diff --git a/src/igl/write_triangle_mesh.h b/src/libigl/igl/write_triangle_mesh.h similarity index 100% rename from src/igl/write_triangle_mesh.h rename to src/libigl/igl/write_triangle_mesh.h diff --git a/src/igl/xml/ReAntTweakBarXMLSerialization.h b/src/libigl/igl/xml/ReAntTweakBarXMLSerialization.h similarity index 100% rename from src/igl/xml/ReAntTweakBarXMLSerialization.h rename to src/libigl/igl/xml/ReAntTweakBarXMLSerialization.h diff --git a/src/igl/xml/XMLSerializable.h b/src/libigl/igl/xml/XMLSerializable.h similarity index 100% rename from src/igl/xml/XMLSerializable.h rename to src/libigl/igl/xml/XMLSerializable.h diff --git a/src/igl/xml/serialization_test.skip b/src/libigl/igl/xml/serialization_test.skip similarity index 100% rename from src/igl/xml/serialization_test.skip rename to src/libigl/igl/xml/serialization_test.skip diff --git a/src/igl/xml/serialize_xml.cpp b/src/libigl/igl/xml/serialize_xml.cpp similarity index 100% rename from src/igl/xml/serialize_xml.cpp rename to src/libigl/igl/xml/serialize_xml.cpp diff --git a/src/igl/xml/serialize_xml.h b/src/libigl/igl/xml/serialize_xml.h similarity index 100% rename from src/igl/xml/serialize_xml.h rename to src/libigl/igl/xml/serialize_xml.h diff --git a/src/igl/xml/writeDAE.cpp b/src/libigl/igl/xml/writeDAE.cpp similarity index 100% rename from src/igl/xml/writeDAE.cpp rename to src/libigl/igl/xml/writeDAE.cpp diff --git a/src/igl/xml/writeDAE.h b/src/libigl/igl/xml/writeDAE.h similarity index 100% rename from src/igl/xml/writeDAE.h rename to src/libigl/igl/xml/writeDAE.h diff --git a/src/igl/xml/write_triangle_mesh.cpp b/src/libigl/igl/xml/write_triangle_mesh.cpp similarity index 100% rename from src/igl/xml/write_triangle_mesh.cpp rename to src/libigl/igl/xml/write_triangle_mesh.cpp diff --git a/src/igl/xml/write_triangle_mesh.h b/src/libigl/igl/xml/write_triangle_mesh.h similarity index 100% rename from src/igl/xml/write_triangle_mesh.h rename to src/libigl/igl/xml/write_triangle_mesh.h diff --git a/src/libnest2d/CMakeLists.txt b/src/libnest2d/CMakeLists.txt index 2508c984a7..587b814b24 100644 --- a/src/libnest2d/CMakeLists.txt +++ b/src/libnest2d/CMakeLists.txt @@ -48,6 +48,9 @@ set(LIBNEST2D_SRCFILES ${SRC_DIR}/libnest2d/optimizer.hpp ${SRC_DIR}/libnest2d/utils/metaloop.hpp ${SRC_DIR}/libnest2d/utils/rotfinder.hpp + ${SRC_DIR}/libnest2d/utils/rotcalipers.hpp + ${SRC_DIR}/libnest2d/utils/bigint.hpp + ${SRC_DIR}/libnest2d/utils/rational.hpp ${SRC_DIR}/libnest2d/placers/placer_boilerplate.hpp ${SRC_DIR}/libnest2d/placers/bottomleftplacer.hpp ${SRC_DIR}/libnest2d/placers/nfpplacer.hpp @@ -70,12 +73,13 @@ if(TBB_FOUND) # The Intel TBB library will use the std::exception_ptr feature of C++11. target_compile_definitions(libnest2d INTERFACE -DTBB_USE_CAPTURED_EXCEPTION=0) - target_link_libraries(libnest2d INTERFACE tbb) - # The following breaks compilation on Visual Studio in Debug mode. - #find_package(Threads REQUIRED) - #target_link_libraries(libnest2d INTERFACE ${TBB_LIBRARIES} ${CMAKE_DL_LIBS} - # Threads::Threads - # ) + find_package(Threads REQUIRED) + target_link_libraries(libnest2d INTERFACE + tbb # VS debug mode needs linking this way: + # ${TBB_LIBRARIES} + ${CMAKE_DL_LIBS} + Threads::Threads + ) else() find_package(OpenMP QUIET) @@ -92,10 +96,11 @@ endif() add_subdirectory(${SRC_DIR}/libnest2d/backends/${LIBNEST2D_GEOMETRIES}) target_link_libraries(libnest2d INTERFACE ${LIBNEST2D_GEOMETRIES}Backend) + add_subdirectory(${SRC_DIR}/libnest2d/optimizers/${LIBNEST2D_OPTIMIZER}) target_link_libraries(libnest2d INTERFACE ${LIBNEST2D_OPTIMIZER}Optimizer) -#target_sources(libnest2d INTERFACE ${LIBNEST2D_SRCFILES}) +# target_sources(libnest2d INTERFACE ${LIBNEST2D_SRCFILES}) target_include_directories(libnest2d INTERFACE ${SRC_DIR}) if(NOT LIBNEST2D_HEADER_ONLY) diff --git a/src/libnest2d/include/libnest2d.h b/src/libnest2d/include/libnest2d.h index 4ad7524219..a6eb36a4b0 100644 --- a/src/libnest2d/include/libnest2d.h +++ b/src/libnest2d/include/libnest2d.h @@ -47,6 +47,17 @@ using NfpPlacer = _NfpPlacer; // This supports only box shaped bins using BottomLeftPlacer = placers::_BottomLeftPlacer; +#ifdef LIBNEST2D_STATIC + +extern template class Nester; +extern template class Nester; +extern template PackGroup Nester::execute( + std::vector::iterator, std::vector::iterator); +extern template PackGroup Nester::execute( + std::vector::iterator, std::vector::iterator); + +#endif + template::iterator> @@ -60,19 +71,6 @@ PackGroup nest(Iterator from, Iterator to, return nester.execute(from, to); } -template> -PackGroup nest(Container&& cont, - const typename Placer::BinType& bin, - Coord dist = 0, - const typename Placer::Config& pconf = {}, - const typename Selector::Config& sconf = {}) -{ - return nest(cont.begin(), cont.end(), - bin, dist, pconf, sconf); -} - template::iterator> @@ -90,6 +88,42 @@ PackGroup nest(Iterator from, Iterator to, return nester.execute(from, to); } +#ifdef LIBNEST2D_STATIC + +extern template class Nester; +extern template class Nester; + +extern template PackGroup nest(std::vector::iterator from, + std::vector::iterator to, + const Box& bin, + Coord dist = 0, + const NfpPlacer::Config& pconf, + const FirstFitSelection::Config& sconf); + +extern template PackGroup nest(std::vector::iterator from, + std::vector::iterator to, + const Box& bin, + ProgressFunction prg, + StopCondition scond, + Coord dist = 0, + const NfpPlacer::Config& pconf, + const FirstFitSelection::Config& sconf); + +#endif + +template> +PackGroup nest(Container&& cont, + const typename Placer::BinType& bin, + Coord dist = 0, + const typename Placer::Config& pconf = {}, + const typename Selector::Config& sconf = {}) +{ + return nest(cont.begin(), cont.end(), + bin, dist, pconf, sconf); +} + template> @@ -105,71 +139,6 @@ PackGroup nest(Container&& cont, bin, prg, scond, dist, pconf, sconf); } -#ifdef LIBNEST2D_STATIC -extern template -PackGroup nest&>( - std::vector& cont, - const Box& bin, - Coord dist, - const NfpPlacer::Config& pcfg, - const FirstFitSelection::Config& scfg -); - -extern template -PackGroup nest&>( - std::vector& cont, - const Box& bin, - ProgressFunction prg, - StopCondition scond, - Coord dist, - const NfpPlacer::Config& pcfg, - const FirstFitSelection::Config& scfg -); - -extern template -PackGroup nest>( - std::vector&& cont, - const Box& bin, - Coord dist, - const NfpPlacer::Config& pcfg, - const FirstFitSelection::Config& scfg -); - -extern template -PackGroup nest>( - std::vector&& cont, - const Box& bin, - ProgressFunction prg, - StopCondition scond, - Coord dist, - const NfpPlacer::Config& pcfg, - const FirstFitSelection::Config& scfg -); - -extern template -PackGroup nest::iterator>( - std::vector::iterator from, - std::vector::iterator to, - const Box& bin, - Coord dist, - const NfpPlacer::Config& pcfg, - const FirstFitSelection::Config& scfg -); - -extern template -PackGroup nest::iterator>( - std::vector::iterator from, - std::vector::iterator to, - const Box& bin, - ProgressFunction prg, - StopCondition scond, - Coord dist, - const NfpPlacer::Config& pcfg, - const FirstFitSelection::Config& scfg -); - -#endif - } #endif // LIBNEST2D_H diff --git a/src/libnest2d/include/libnest2d/backends/clipper/CMakeLists.txt b/src/libnest2d/include/libnest2d/backends/clipper/CMakeLists.txt index e20cbc70da..2020893567 100644 --- a/src/libnest2d/include/libnest2d/backends/clipper/CMakeLists.txt +++ b/src/libnest2d/include/libnest2d/backends/clipper/CMakeLists.txt @@ -33,19 +33,18 @@ if(NOT TARGET clipper) # If there is a clipper target in the parent project we a # ${clipper_library_BINARY_DIR} # ) - add_library(ClipperBackend STATIC + add_library(clipperBackend STATIC ${clipper_library_SOURCE_DIR}/clipper.cpp ${clipper_library_SOURCE_DIR}/clipper.hpp) - target_include_directories(ClipperBackend INTERFACE - ${clipper_library_SOURCE_DIR}) + target_include_directories(clipperBackend INTERFACE ${clipper_library_SOURCE_DIR}) else() message(FATAL_ERROR "Can't find clipper library and no SVN client found to download. You can download the clipper sources and define a clipper target in your project, that will work for libnest2d.") endif() else() - add_library(ClipperBackend INTERFACE) - target_link_libraries(ClipperBackend INTERFACE Clipper::Clipper) + add_library(clipperBackend INTERFACE) + target_link_libraries(clipperBackend INTERFACE Clipper::Clipper) endif() else() # set(CLIPPER_INCLUDE_DIRS "" PARENT_SCOPE) @@ -56,12 +55,12 @@ endif() # Clipper backend is not enough on its own, it still needs some functions # from Boost geometry -if(NOT Boost_INCLUDE_DIRS_FOUND) +if(NOT Boost_FOUND) find_package(Boost 1.58 REQUIRED) # TODO automatic download of boost geometry headers endif() -target_include_directories(clipperBackend SYSTEM INTERFACE ${Boost_INCLUDE_DIRS} ) +target_link_libraries(clipperBackend INTERFACE Boost::boost ) #target_sources(ClipperBackend INTERFACE # ${CMAKE_CURRENT_SOURCE_DIR}/geometries.hpp # ${CMAKE_CURRENT_SOURCE_DIR}/clipper_polygon.hpp @@ -69,6 +68,6 @@ target_include_directories(clipperBackend SYSTEM INTERFACE ${Boost_INCLUDE_DIRS} target_compile_definitions(clipperBackend INTERFACE LIBNEST2D_BACKEND_CLIPPER) -# And finally plug the ClipperBackend into libnest2d -#target_link_libraries(libnest2d INTERFACE ClipperBackend) +# And finally plug the clipperBackend into libnest2d +# target_link_libraries(libnest2d INTERFACE clipperBackend) diff --git a/src/libnest2d/include/libnest2d/backends/clipper/clipper_polygon.hpp b/src/libnest2d/include/libnest2d/backends/clipper/clipper_polygon.hpp index e9fbfbd18a..6511fbb72a 100644 --- a/src/libnest2d/include/libnest2d/backends/clipper/clipper_polygon.hpp +++ b/src/libnest2d/include/libnest2d/backends/clipper/clipper_polygon.hpp @@ -12,13 +12,13 @@ struct Polygon { inline Polygon() = default; inline explicit Polygon(const Path& cont): Contour(cont) {} - inline explicit Polygon(const Paths& holes): - Holes(holes) {} +// inline explicit Polygon(const Paths& holes): +// Holes(holes) {} inline Polygon(const Path& cont, const Paths& holes): Contour(cont), Holes(holes) {} inline explicit Polygon(Path&& cont): Contour(std::move(cont)) {} - inline explicit Polygon(Paths&& holes): Holes(std::move(holes)) {} +// inline explicit Polygon(Paths&& holes): Holes(std::move(holes)) {} inline Polygon(Path&& cont, Paths&& holes): Contour(std::move(cont)), Holes(std::move(holes)) {} }; @@ -42,7 +42,7 @@ inline IntPoint& operator -=(IntPoint& p, const IntPoint& pa ) { return p; } -inline IntPoint operator -(IntPoint& p ) { +inline IntPoint operator -(const IntPoint& p ) { IntPoint ret = p; ret.X = -ret.X; ret.Y = -ret.Y; diff --git a/src/libnest2d/include/libnest2d/backends/clipper/geometries.hpp b/src/libnest2d/include/libnest2d/backends/clipper/geometries.hpp index 232668f610..e9fad405b6 100644 --- a/src/libnest2d/include/libnest2d/backends/clipper/geometries.hpp +++ b/src/libnest2d/include/libnest2d/backends/clipper/geometries.hpp @@ -20,43 +20,23 @@ using PathImpl = ClipperLib::Path; using HoleStore = ClipperLib::Paths; using PolygonImpl = ClipperLib::Polygon; -// Type of coordinate units used by Clipper -template<> struct CoordType { - using Type = ClipperLib::cInt; -}; - -// Type of point used by Clipper -template<> struct PointType { - using Type = PointImpl; -}; - -template<> struct PointType { - using Type = PointImpl; -}; - -template<> struct PointType { - using Type = PointImpl; -}; - -template<> struct CountourType { - using Type = PathImpl; -}; - template<> struct ShapeTag { using Type = PolygonTag; }; -template<> struct ShapeTag { using Type = PathTag; }; -template<> struct ShapeTag { using Type = PointTag; }; +template<> struct ShapeTag { using Type = PathTag; }; +template<> struct ShapeTag { using Type = PointTag; }; -template<> struct ShapeTag> { - using Type = MultiPolygonTag; -}; +// Type of coordinate units used by Clipper. Enough to specialize for point, +// the rest of the types will work (Path, Polygon) +template<> struct CoordType { using Type = ClipperLib::cInt; }; -template<> struct PointType> { - using Type = PointImpl; -}; +// Enough to specialize for path, it will work for multishape and Polygon +template<> struct PointType { using Type = PointImpl; }; -template<> struct HolesContainer { - using Type = ClipperLib::Paths; -}; +// This is crucial. CountourType refers to itself by default, so we don't have +// to secialize for clipper Path. ContourType::Type is PathImpl. +template<> struct ContourType { using Type = PathImpl; }; + +// The holes are contained in Clipper::Paths +template<> struct HolesContainer { using Type = ClipperLib::Paths; }; namespace pointlike { @@ -86,39 +66,11 @@ template<> inline TCoord& y(PointImpl& p) } +// Using the libnest2d default area implementation #define DISABLE_BOOST_AREA -namespace _smartarea { - -template -inline double area(const PolygonImpl& /*sh*/) { - return std::nan(""); -} - -template<> -inline double area(const PolygonImpl& sh) { - return std::accumulate(sh.Holes.begin(), sh.Holes.end(), - ClipperLib::Area(sh.Contour), - [](double a, const ClipperLib::Path& pt){ - return a + ClipperLib::Area(pt); - }); -} - -template<> -inline double area(const PolygonImpl& sh) { - return -area(sh); -} - -} - namespace shapelike { -// Tell libnest2d how to make string out of a ClipperPolygon object -template<> inline double area(const PolygonImpl& sh, const PolygonTag&) -{ - return _smartarea::area::Value>(sh); -} - template<> inline void offset(PolygonImpl& sh, TCoord distance) { #define DISABLE_BOOST_OFFSET @@ -200,43 +152,16 @@ inline PolygonImpl create(const PathImpl& path, const HoleStore& holes) { PolygonImpl p; p.Contour = path; - - // Expecting that the coordinate system Y axis is positive in upwards - // direction - if(ClipperLib::Orientation(p.Contour)) { - // Not clockwise then reverse the b*tch - ClipperLib::ReversePath(p.Contour); - } - p.Holes = holes; - for(auto& h : p.Holes) { - if(!ClipperLib::Orientation(h)) { - ClipperLib::ReversePath(h); - } - } - + return p; } template<> inline PolygonImpl create( PathImpl&& path, HoleStore&& holes) { PolygonImpl p; p.Contour.swap(path); - - // Expecting that the coordinate system Y axis is positive in upwards - // direction - if(ClipperLib::Orientation(p.Contour)) { - // Not clockwise then reverse the b*tch - ClipperLib::ReversePath(p.Contour); - } - p.Holes.swap(holes); - - for(auto& h : p.Holes) { - if(!ClipperLib::Orientation(h)) { - ClipperLib::ReversePath(h); - } - } - + return p; } @@ -314,13 +239,13 @@ inline void rotate(PolygonImpl& sh, const Radians& rads) } // namespace shapelike #define DISABLE_BOOST_NFP_MERGE -inline std::vector clipper_execute( +inline TMultiShape clipper_execute( ClipperLib::Clipper& clipper, ClipperLib::ClipType clipType, ClipperLib::PolyFillType subjFillType = ClipperLib::pftEvenOdd, ClipperLib::PolyFillType clipFillType = ClipperLib::pftEvenOdd) { - shapelike::Shapes retv; + TMultiShape retv; ClipperLib::PolyTree result; clipper.Execute(clipType, result, subjFillType, clipFillType); @@ -370,8 +295,8 @@ inline std::vector clipper_execute( namespace nfp { -template<> inline std::vector -merge(const std::vector& shapes) +template<> inline TMultiShape +merge(const TMultiShape& shapes) { ClipperLib::Clipper clipper(ClipperLib::ioReverseSolution); @@ -394,6 +319,8 @@ merge(const std::vector& shapes) } +#define DISABLE_BOOST_CONVEX_HULL + //#define DISABLE_BOOST_SERIALIZE //#define DISABLE_BOOST_UNSERIALIZE diff --git a/src/libnest2d/include/libnest2d/common.hpp b/src/libnest2d/include/libnest2d/common.hpp index 6867f76f3e..66e095ae27 100644 --- a/src/libnest2d/include/libnest2d/common.hpp +++ b/src/libnest2d/include/libnest2d/common.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #if defined(_MSC_VER) && _MSC_VER <= 1800 || __cplusplus < 201103L #define BP2D_NOEXCEPT @@ -197,6 +198,33 @@ public: } }; +struct ScalarTag {}; +struct BigIntTag {}; +struct RationalTag {}; + +template struct _NumTag { + using Type = + enable_if_t::value, ScalarTag>; +}; + +template using NumTag = typename _NumTag>::Type; + +/// A local version for abs that is garanteed to work with libnest2d types +template inline T abs(const T& v, ScalarTag) +{ + return std::abs(v); +} + +template inline T abs(const T& v) { return abs(v, NumTag()); } + +template inline T2 cast(const T1& v, ScalarTag, ScalarTag) +{ + return static_cast(v); +} + +template inline T2 cast(const T1& v) { + return cast(v, NumTag(), NumTag()); +} } #endif // LIBNEST2D_CONFIG_HPP diff --git a/src/libnest2d/include/libnest2d/geometry_traits.hpp b/src/libnest2d/include/libnest2d/geometry_traits.hpp index 917f5280d1..6c55d0e3f2 100644 --- a/src/libnest2d/include/libnest2d/geometry_traits.hpp +++ b/src/libnest2d/include/libnest2d/geometry_traits.hpp @@ -7,45 +7,125 @@ #include #include #include -#include #include #include +#include -#include "common.hpp" +#include namespace libnest2d { +// Meta tags for different geometry concepts. +struct PointTag {}; +struct PolygonTag {}; +struct PathTag {}; +struct MultiPolygonTag {}; +struct BoxTag {}; +struct CircleTag {}; + +/// Meta-function to derive the tag of a shape type. +template struct ShapeTag { using Type = typename Shape::Tag; }; + +/// Tag will be used instead of `typename ShapeTag::Type` +template using Tag = typename ShapeTag>::Type; + +/// Meta function to derive the contour type for a polygon which could be itself +template struct ContourType { using Type = RawShape; }; + +/// TContour instead of `typename ContourType::type` +template +using TContour = typename ContourType>::Type; + +/// Getting the type of point structure used by a shape. +template struct PointType { + using Type = typename PointType>::Type; +}; + +/// TPoint as shorthand for `typename PointType::Type`. +template +using TPoint = typename PointType>::Type; + /// Getting the coordinate data type for a geometry class. -template struct CoordType { using Type = long; }; +template struct CoordType { + using Type = typename CoordType>::Type; +}; /// TCoord as shorthand for typename `CoordType::Type`. template using TCoord = typename CoordType>::Type; -/// Getting the type of point structure used by a shape. -template struct PointType { using Type = typename Sh::PointType; }; +/// Getting the computation type for a certain geometry type. +/// It is the coordinate type by default but it is advised that a type with +/// larger precision and (or) range is specified. +template::value> struct ComputeType {}; -/// TPoint as shorthand for `typename PointType::Type`. -template -using TPoint = typename PointType>::Type; +/// A compute type is introduced to hold the results of computations on +/// coordinates and points. It should be larger in range than the coordinate +/// type or the range of coordinates should be limited to not loose precision. +template struct ComputeType { + using Type = typename ComputeType>::Type; +}; +/// libnest2d will choose a default compute type for various coordinate types +/// if the backend has not specified anything. +template struct DoublePrecision { using Type = T; }; +template<> struct DoublePrecision { using Type = int16_t; }; +template<> struct DoublePrecision { using Type = int32_t; }; +template<> struct DoublePrecision { using Type = int64_t; }; +template<> struct DoublePrecision { using Type = double; }; +template<> struct DoublePrecision { using Type = long double; }; +template struct ComputeType { + using Type = typename DoublePrecision::Type; +}; -template struct CountourType { using Type = RawShape; }; - -template -using TContour = typename CountourType>::Type; - +/// TCompute shorthand for `typename ComputeType::Type` +template using TCompute = typename ComputeType>::Type; +/// A meta function to derive a container type for holes in a polygon template struct HolesContainer { using Type = std::vector>; }; +/// Shorthand for `typename HolesContainer::Type` template using THolesContainer = typename HolesContainer>::Type; +/* + * TContour, TPoint, TCoord and TCompute should be usable for any type for which + * it makes sense. For example, the point type could be derived from the contour, + * the polygon and (or) the multishape as well. The coordinate type also and + * including the point type. TCoord, TCoord, TCoord are + * all valid types and derives the coordinate type of template argument Polygon, + * Path and Point. This is also true for TCompute, but it can also take the + * coordinate type as argument. + */ -template -struct LastPointIsFirst { static const bool Value = true; }; +/* + * A Multi shape concept is also introduced. A multi shape is something that + * can contain the result of an operation where the input is one polygon and + * the result could be many polygons or path -> paths. The MultiShape should be + * a container type. If the backend does not specialize the MultiShape template, + * a default multi shape container will be used. + */ + +/// The default multi shape container. +template struct DefaultMultiShape: public std::vector { + using Tag = MultiPolygonTag; + template DefaultMultiShape(Args&&...args): + std::vector(std::forward(args)...) {} +}; + +/// The MultiShape Type trait which gets the container type for a geometry type. +template struct MultiShape { using Type = DefaultMultiShape; }; + +/// use TMultiShape instead of `typename MultiShape::Type` +template +using TMultiShape = typename MultiShape>::Type; + +// A specialization of ContourType to work with the default multishape type +template struct ContourType> { + using Type = typename ContourType::Type; +}; enum class Orientation { CLOCKWISE, @@ -59,6 +139,11 @@ struct OrientationType { static const Orientation Value = Orientation::CLOCKWISE; }; +template inline /*constexpr*/ bool is_clockwise() { + return OrientationType>::Value == Orientation::CLOCKWISE; +} + + /** * \brief A point pair base class for other point pairs (segment, box, ...). * \tparam RawPoint The actual point type to use. @@ -69,21 +154,6 @@ struct PointPair { RawPoint p2; }; -struct PointTag {}; -struct PolygonTag {}; -struct PathTag {}; -struct MultiPolygonTag {}; -struct BoxTag {}; -struct CircleTag {}; - -/// Meta-functions to derive the tags -template struct ShapeTag { using Type = typename Shape::Tag; }; -template using Tag = typename ShapeTag>::Type; - -template struct MultiShape { using Type = std::vector; }; -template -using TMultiShape =typename MultiShape>::Type; - /** * \brief An abstraction of a box; */ @@ -114,11 +184,16 @@ public: inline RawPoint center() const BP2D_NOEXCEPT; - inline double area() const BP2D_NOEXCEPT { - return double(width()*height()); + template> + inline Unit area() const BP2D_NOEXCEPT { + return Unit(width())*height(); } }; +template struct PointType<_Box> { + using Type = typename _Box::PointType; +}; + template class _Circle { RawPoint center_; @@ -129,7 +204,6 @@ public: using PointType = RawPoint; _Circle() = default; - _Circle(const RawPoint& center, double r): center_(center), radius_(r) {} inline const RawPoint& center() const BP2D_NOEXCEPT { return center_; } @@ -137,12 +211,16 @@ public: inline double radius() const BP2D_NOEXCEPT { return radius_; } inline void radius(double r) { radius_ = r; } - + inline double area() const BP2D_NOEXCEPT { - return 2.0*Pi*radius_*radius_; + return Pi_2 * radius_ * radius_; } }; +template struct PointType<_Circle> { + using Type = typename _Circle::PointType; +}; + /** * \brief An abstraction of a directed line segment with two points. */ @@ -185,7 +263,12 @@ public: inline Radians angleToXaxis() const; /// The length of the segment in the measure of the coordinate system. - inline double length(); + template> inline Unit sqlength() const; + +}; + +template struct PointType<_Segment> { + using Type = typename _Circle::PointType; }; // This struct serves almost as a namespace. The only difference is that is can @@ -216,33 +299,56 @@ inline TCoord& y(RawPoint& p) return p.y(); } -template -inline double distance(const RawPoint& /*p1*/, const RawPoint& /*p2*/) +template> +inline Unit squaredDistance(const RawPoint& p1, const RawPoint& p2) { - static_assert(always_false::value, - "PointLike::distance(point, point) unimplemented!"); - return 0; + auto x1 = Unit(x(p1)), y1 = Unit(y(p1)), x2 = Unit(x(p2)), y2 = Unit(y(p2)); + Unit a = (x2 - x1), b = (y2 - y1); + return a * a + b * b; } template -inline double distance(const RawPoint& /*p1*/, - const _Segment& /*s*/) +inline double distance(const RawPoint& p1, const RawPoint& p2) { - static_assert(always_false::value, - "PointLike::distance(point, segment) unimplemented!"); - return 0; + return std::sqrt(squaredDistance(p1, p2)); } -template -inline std::pair, bool> horizontalDistance( +// create perpendicular vector +template inline Pt perp(const Pt& p) +{ + return Pt(y(p), -x(p)); +} + +template> +inline Unit dotperp(const Pt& a, const Pt& b) +{ + return Unit(x(a)) * Unit(y(b)) - Unit(y(a)) * Unit(x(b)); +} + +// dot product +template> +inline Unit dot(const Pt& a, const Pt& b) +{ + return Unit(x(a)) * x(b) + Unit(y(a)) * y(b); +} + +// squared vector magnitude +template> +inline Unit magnsq(const Pt& p) +{ + return Unit(x(p)) * x(p) + Unit(y(p)) * y(p); +} + +template> +inline std::pair horizontalDistance( const RawPoint& p, const _Segment& s) { - using Unit = TCoord; - auto x = pointlike::x(p), y = pointlike::y(p); - auto x1 = pointlike::x(s.first()), y1 = pointlike::y(s.first()); - auto x2 = pointlike::x(s.second()), y2 = pointlike::y(s.second()); + namespace pl = pointlike; + auto x = Unit(pl::x(p)), y = Unit(pl::y(p)); + auto x1 = Unit(pl::x(s.first())), y1 = Unit(pl::y(s.first())); + auto x2 = Unit(pl::x(s.second())), y2 = Unit(pl::y(s.second())); - TCoord ret; + Unit ret; if( (y < y1 && y < y2) || (y > y1 && y > y2) ) return {0, false}; @@ -250,8 +356,7 @@ inline std::pair, bool> horizontalDistance( ret = std::min( x-x1, x -x2); else if( (y == y1 && y == y2) && (x < x1 && x < x2)) ret = -std::min(x1 - x, x2 - x); - else if(std::abs(y - y1) <= std::numeric_limits::epsilon() && - std::abs(y - y2) <= std::numeric_limits::epsilon()) + else if(y == y1 && y == y2) ret = 0; else ret = x - x1 + (x1 - x2)*(y1 - y)/(y1 - y2); @@ -259,16 +364,16 @@ inline std::pair, bool> horizontalDistance( return {ret, true}; } -template -inline std::pair, bool> verticalDistance( +template> +inline std::pair verticalDistance( const RawPoint& p, const _Segment& s) { - using Unit = TCoord; - auto x = pointlike::x(p), y = pointlike::y(p); - auto x1 = pointlike::x(s.first()), y1 = pointlike::y(s.first()); - auto x2 = pointlike::x(s.second()), y2 = pointlike::y(s.second()); + namespace pl = pointlike; + auto x = Unit(pl::x(p)), y = Unit(pl::y(p)); + auto x1 = Unit(pl::x(s.first())), y1 = Unit(pl::y(s.first())); + auto x2 = Unit(pl::x(s.second())), y2 = Unit(pl::y(s.second())); - TCoord ret; + Unit ret; if( (x < x1 && x < x2) || (x > x1 && x > x2) ) return {0, false}; @@ -276,8 +381,7 @@ inline std::pair, bool> verticalDistance( ret = std::min( y-y1, y -y2); else if( (x == x1 && x == x2) && (y < y1 && y < y2)) ret = -std::min(y1 - y, y2 - y); - else if(std::abs(x - x1) <= std::numeric_limits::epsilon() && - std::abs(x - x2) <= std::numeric_limits::epsilon()) + else if(x == x1 && x == x2) ret = 0; else ret = y - y1 + (y1 - y2)*(x1 - x)/(x1 - x2); @@ -333,9 +437,10 @@ inline Radians _Segment::angleToXaxis() const } template -inline double _Segment::length() +template +inline Unit _Segment::sqlength() const { - return pointlike::distance(first(), second()); + return pointlike::squaredDistance(first(), second()); } template @@ -346,8 +451,8 @@ inline RawPoint _Box::center() const BP2D_NOEXCEPT { using Coord = TCoord; RawPoint ret = { // No rounding here, we dont know if these are int coords - static_cast( (getX(minc) + getX(maxc))/2.0 ), - static_cast( (getY(minc) + getY(maxc))/2.0 ) + Coord( (getX(minc) + getX(maxc)) / Coord(2) ), + Coord( (getY(minc) + getY(maxc)) / Coord(2) ) }; return ret; @@ -362,9 +467,6 @@ enum class Formats { // used in friend declarations and can be aliased at class scope. namespace shapelike { -template -using Shapes = TMultiShape; - template inline RawShape create(const TContour& contour, const THolesContainer& holes) @@ -449,7 +551,7 @@ inline void reserve(RawPath& p, size_t vertex_capacity, const PathTag&) template inline void addVertex(RawShape& sh, const PathTag&, Args...args) { - return sh.emplace_back(std::forward(args)...); + sh.emplace_back(std::forward(args)...); } template @@ -504,13 +606,8 @@ inline void unserialize(RawShape& /*sh*/, const std::string& /*str*/) "shapelike::unserialize() unimplemented!"); } -template -inline double area(const RawShape& /*sh*/, const PolygonTag&) -{ - static_assert(always_false::value, - "shapelike::area() unimplemented!"); - return 0; -} +template +inline Unit area(const Cntr& poly, const PathTag& ); template inline bool intersects(const RawShape& /*sh*/, const RawShape& /*sh*/) @@ -556,14 +653,14 @@ inline bool touches( const TPoint& /*point*/, template inline _Box> boundingBox(const RawShape& /*sh*/, - const PolygonTag&) + const PathTag&) { static_assert(always_false::value, "shapelike::boundingBox(shape) unimplemented!"); } template -inline _Box> +inline _Box> boundingBox(const RawShapes& /*sh*/, const MultiPolygonTag&) { static_assert(always_false::value, @@ -571,21 +668,10 @@ boundingBox(const RawShapes& /*sh*/, const MultiPolygonTag&) } template -inline RawShape convexHull(const RawShape& /*sh*/, const PolygonTag&) -{ - static_assert(always_false::value, - "shapelike::convexHull(shape) unimplemented!"); - return RawShape(); -} +inline RawShape convexHull(const RawShape& sh, const PathTag&); -template -inline typename RawShapes::value_type -convexHull(const RawShapes& /*sh*/, const MultiPolygonTag&) -{ - static_assert(always_false::value, - "shapelike::convexHull(shapes) unimplemented!"); - return typename RawShapes::value_type(); -} +template +inline S convexHull(const RawShapes& sh, const MultiPolygonTag&); template inline void rotate(RawShape& /*sh*/, const Radians& /*rads*/) @@ -745,13 +831,19 @@ inline void reserve(T& sh, size_t vertex_capacity) { template inline void addVertex(RawShape& sh, const PolygonTag&, Args...args) { - return addVertex(contour(sh), PathTag(), std::forward(args)...); + addVertex(contour(sh), PathTag(), std::forward(args)...); } template // Tag dispatcher inline void addVertex(RawShape& sh, Args...args) { - return addVertex(sh, Tag(), std::forward(args)...); + addVertex(sh, Tag(), std::forward(args)...); +} + +template +inline _Box> boundingBox(const RawShape& poly, const PolygonTag&) +{ + return boundingBox(contour(poly), PathTag()); } template @@ -786,7 +878,7 @@ inline _Box> boundingBox(const S& sh) template inline double area(const Box& box, const BoxTag& ) { - return box.area(); + return box.template area(); } template @@ -795,6 +887,35 @@ inline double area(const Circle& circ, const CircleTag& ) return circ.area(); } +template +inline Unit area(const Cntr& poly, const PathTag& ) +{ + namespace sl = shapelike; + if (sl::cend(poly) - sl::cbegin(poly) < 3) return 0.0; + + Unit a = 0; + for (auto i = sl::cbegin(poly), j = std::prev(sl::cend(poly)); + i < sl::cend(poly); ++i) + { + auto xj = Unit(getX(*j)), yj = Unit(getY(*j)); + auto xi = Unit(getX(*i)), yi = Unit(getY(*i)); + a += (xj + xi) * (yj - yi); + j = i; + } + a /= 2; + return is_clockwise() ? a : -a; +} + +template inline double area(const S& poly, const PolygonTag& ) +{ + auto hls = holes(poly); + return std::accumulate(hls.begin(), hls.end(), + area(contour(poly), PathTag()), + [](double a, const TContour &h){ + return a + area(h, PathTag()); + }); +} + template // Dispatching function inline double area(const RawShape& sh) { @@ -811,6 +932,12 @@ inline double area(const RawShapes& shapes, const MultiPolygonTag&) }); } +template +inline RawShape convexHull(const RawShape& sh, const PolygonTag&) +{ + return create(convexHull(contour(sh), PathTag())); +} + template inline auto convexHull(const RawShape& sh) -> decltype(convexHull(sh, Tag())) // TODO: C++14 could deduce @@ -818,11 +945,91 @@ inline auto convexHull(const RawShape& sh) return convexHull(sh, Tag()); } +template +inline RawShape convexHull(const RawShape& sh, const PathTag&) +{ + using Unit = TCompute; + using Point = TPoint; + namespace sl = shapelike; + + size_t edges = sl::cend(sh) - sl::cbegin(sh); + if(edges <= 3) return {}; + + bool closed = false; + std::vector U, L; + U.reserve(1 + edges / 2); L.reserve(1 + edges / 2); + + std::vector pts; pts.reserve(edges); + std::copy(sl::cbegin(sh), sl::cend(sh), std::back_inserter(pts)); + + auto fpt = pts.front(), lpt = pts.back(); + if(getX(fpt) == getX(lpt) && getY(fpt) == getY(lpt)) { + closed = true; pts.pop_back(); + } + + std::sort(pts.begin(), pts.end(), + [](const Point& v1, const Point& v2) + { + Unit x1 = getX(v1), x2 = getX(v2), y1 = getY(v1), y2 = getY(v2); + return x1 == x2 ? y1 < y2 : x1 < x2; + }); + + auto dir = [](const Point& p, const Point& q, const Point& r) { + return (Unit(getY(q)) - getY(p)) * (Unit(getX(r)) - getX(p)) - + (Unit(getX(q)) - getX(p)) * (Unit(getY(r)) - getY(p)); + }; + + auto ik = pts.begin(); + + while(ik != pts.end()) { + + while(U.size() > 1 && dir(U[U.size() - 2], U.back(), *ik) <= 0) + U.pop_back(); + while(L.size() > 1 && dir(L[L.size() - 2], L.back(), *ik) >= 0) + L.pop_back(); + + U.emplace_back(*ik); + L.emplace_back(*ik); + + ++ik; + } + + RawShape ret; reserve(ret, U.size() + L.size()); + if(is_clockwise()) { + for(auto it = U.begin(); it != std::prev(U.end()); ++it) + addVertex(ret, *it); + for(auto it = L.rbegin(); it != std::prev(L.rend()); ++it) + addVertex(ret, *it); + if(closed) addVertex(ret, *std::prev(L.rend())); + } else { + for(auto it = L.begin(); it != std::prev(L.end()); ++it) + addVertex(ret, *it); + for(auto it = U.rbegin(); it != std::prev(U.rend()); ++it) + addVertex(ret, *it); + if(closed) addVertex(ret, *std::prev(U.rend())); + } + + return ret; +} + +template +inline S convexHull(const RawShapes& sh, const MultiPolygonTag&) +{ + namespace sl = shapelike; + S cntr; + for(auto& poly : sh) + for(auto it = sl::cbegin(poly); it != sl::cend(poly); ++it) + addVertex(cntr, *it); + + return convexHull(cntr, Tag()); +} + template inline bool isInside(const TP& point, const TC& circ, const PointTag&, const CircleTag&) { - return pointlike::distance(point, circ.center()) < circ.radius(); + auto r = circ.radius(); + return pointlike::squaredDistance(point, circ.center()) < r * r; } template @@ -972,6 +1179,9 @@ template inline bool isConvex(const RawShape& sh) // dispatch using Segment = _Segment; \ using Polygons = TMultiShape +namespace sl = shapelike; +namespace pl = pointlike; + } #endif // GEOMETRY_TRAITS_HPP diff --git a/src/libnest2d/include/libnest2d/geometry_traits_nfp.hpp b/src/libnest2d/include/libnest2d/geometry_traits_nfp.hpp index cb0580ef4f..4a2c69bca2 100644 --- a/src/libnest2d/include/libnest2d/geometry_traits_nfp.hpp +++ b/src/libnest2d/include/libnest2d/geometry_traits_nfp.hpp @@ -1,26 +1,22 @@ #ifndef GEOMETRIES_NOFITPOLYGON_HPP #define GEOMETRIES_NOFITPOLYGON_HPP -#include "geometry_traits.hpp" #include #include #include #include +#include + namespace libnest2d { namespace __nfp { // Do not specialize this... -template +template> inline bool _vsort(const TPoint& v1, const TPoint& v2) { - using Coord = TCoord>; - Coord &&x1 = getX(v1), &&x2 = getX(v2), &&y1 = getY(v1), &&y2 = getY(v2); - auto diff = y1 - y2; - if(std::abs(diff) <= std::numeric_limits::epsilon()) - return x1 < x2; - - return diff < 0; + Unit x1 = getX(v1), x2 = getX(v2), y1 = getY(v1), y2 = getY(v2); + return y1 == y2 ? x1 < x2 : y1 < y2; } template> @@ -202,7 +198,7 @@ inline TPoint referenceVertex(const RawShape& sh) * convex as well in this case. * */ -template +template inline NfpResult nfpConvexOnly(const RawShape& sh, const RawShape& other) { @@ -238,12 +234,62 @@ inline NfpResult nfpConvexOnly(const RawShape& sh, ++first; ++next; } } - - // Sort the edges by angle to X axis. - std::sort(edgelist.begin(), edgelist.end(), - [](const Edge& e1, const Edge& e2) + + std::sort(edgelist.begin(), edgelist.end(), + [](const Edge& e1, const Edge& e2) { - return e1.angleToXaxis() > e2.angleToXaxis(); + Vertex ax(1, 0); // Unit vector for the X axis + + // get cectors from the edges + Vertex p1 = e1.second() - e1.first(); + Vertex p2 = e2.second() - e2.first(); + + // Quadrant mapping array. The quadrant of a vector can be determined + // from the dot product of the vector and its perpendicular pair + // with the unit vector X axis. The products will carry the values + // lcos = dot(p, ax) = l * cos(phi) and + // lsin = -dotperp(p, ax) = l * sin(phi) where + // l is the length of vector p. From the signs of these values we can + // construct an index which has the sign of lcos as MSB and the + // sign of lsin as LSB. This index can be used to retrieve the actual + // quadrant where vector p resides using the following map: + // (+ is 0, - is 1) + // cos | sin | decimal | quadrant + // + | + | 0 | 0 + // + | - | 1 | 3 + // - | + | 2 | 1 + // - | - | 3 | 2 + std::array quadrants {0, 3, 1, 2 }; + + std::array q {0, 0}; // Quadrant indices for p1 and p2 + + using TDots = std::array, 2>; + TDots lcos { pl::dot(p1, ax), pl::dot(p2, ax) }; + TDots lsin { -pl::dotperp(p1, ax), -pl::dotperp(p2, ax) }; + + // Construct the quadrant indices for p1 and p2 + for(size_t i = 0; i < 2; ++i) + if(lcos[i] == 0) q[i] = lsin[i] > 0 ? 1 : 3; + else if(lsin[i] == 0) q[i] = lcos[i] > 0 ? 0 : 2; + else q[i] = quadrants[((lcos[i] < 0) << 1) + (lsin[i] < 0)]; + + if(q[0] == q[1]) { // only bother if p1 and p2 are in the same quadrant + auto lsq1 = pl::magnsq(p1); // squared magnitudes, avoid sqrt + auto lsq2 = pl::magnsq(p2); // squared magnitudes, avoid sqrt + + // We will actually compare l^2 * cos^2(phi) which saturates the + // cos function. But with the quadrant info we can get the sign back + int sign = q[0] == 1 || q[0] == 2 ? -1 : 1; + + // If Ratio is an actual rational type, there is no precision loss + auto pcos1 = Ratio(lcos[0]) / lsq1 * sign * lcos[0]; + auto pcos2 = Ratio(lcos[1]) / lsq2 * sign * lcos[1]; + + return q[0] < 2 ? pcos1 < pcos2 : pcos1 > pcos2; + } + + // If in different quadrants, compare the quadrant indices only. + return q[0] > q[1]; }); __nfp::buildPolygon(edgelist, rsh, top_nfp); @@ -253,456 +299,9 @@ inline NfpResult nfpConvexOnly(const RawShape& sh, template NfpResult nfpSimpleSimple(const RawShape& cstationary, - const RawShape& cother) + const RawShape& cother) { - - // Algorithms are from the original algorithm proposed in paper: - // https://eprints.soton.ac.uk/36850/1/CORMSIS-05-05.pdf - - // ///////////////////////////////////////////////////////////////////////// - // Algorithm 1: Obtaining the minkowski sum - // ///////////////////////////////////////////////////////////////////////// - - // I guess this is not a full minkowski sum of the two input polygons by - // definition. This yields a subset that is compatible with the next 2 - // algorithms. - - using Result = NfpResult; - using Vertex = TPoint; - using Coord = TCoord; - using Edge = _Segment; - namespace sl = shapelike; - using std::signbit; - using std::sort; - using std::vector; - using std::ref; - using std::reference_wrapper; - - // TODO The original algorithms expects the stationary polygon in - // counter clockwise and the orbiter in clockwise order. - // So for preventing any further complication, I will make the input - // the way it should be, than make my way around the orientations. - - // Reverse the stationary contour to counter clockwise - auto stcont = sl::contour(cstationary); - { - std::reverse(sl::begin(stcont), sl::end(stcont)); - stcont.pop_back(); - auto it = std::min_element(sl::begin(stcont), sl::end(stcont), - [](const Vertex& v1, const Vertex& v2) { - return getY(v1) < getY(v2); - }); - std::rotate(sl::begin(stcont), it, sl::end(stcont)); - sl::addVertex(stcont, sl::front(stcont)); - } - RawShape stationary; - sl::contour(stationary) = stcont; - - // Reverse the orbiter contour to counter clockwise - auto orbcont = sl::contour(cother); - { - std::reverse(orbcont.begin(), orbcont.end()); - - // Step 1: Make the orbiter reverse oriented - - orbcont.pop_back(); - auto it = std::min_element(orbcont.begin(), orbcont.end(), - [](const Vertex& v1, const Vertex& v2) { - return getY(v1) < getY(v2); - }); - - std::rotate(orbcont.begin(), it, orbcont.end()); - orbcont.emplace_back(orbcont.front()); - - for(auto &v : orbcont) v = -v; - - } - - // Copy the orbiter (contour only), we will have to work on it - RawShape orbiter; - sl::contour(orbiter) = orbcont; - - // An edge with additional data for marking it - struct MarkedEdge { - Edge e; Radians turn_angle = 0; bool is_turning_point = false; - MarkedEdge() = default; - MarkedEdge(const Edge& ed, Radians ta, bool tp): - e(ed), turn_angle(ta), is_turning_point(tp) {} - - // debug - std::string label; - }; - - // Container for marked edges - using EdgeList = vector; - - EdgeList A, B; - - // This is how an edge list is created from the polygons - auto fillEdgeList = [](EdgeList& L, const RawShape& ppoly, int dir) { - auto& poly = sl::contour(ppoly); - - L.reserve(sl::contourVertexCount(poly)); - - if(dir > 0) { - auto it = poly.begin(); - auto nextit = std::next(it); - - double turn_angle = 0; - bool is_turn_point = false; - - while(nextit != poly.end()) { - L.emplace_back(Edge(*it, *nextit), turn_angle, is_turn_point); - it++; nextit++; - } - } else { - auto it = sl::rbegin(poly); - auto nextit = std::next(it); - - double turn_angle = 0; - bool is_turn_point = false; - - while(nextit != sl::rend(poly)) { - L.emplace_back(Edge(*it, *nextit), turn_angle, is_turn_point); - it++; nextit++; - } - } - - auto getTurnAngle = [](const Edge& e1, const Edge& e2) { - auto phi = e1.angleToXaxis(); - auto phi_prev = e2.angleToXaxis(); - auto turn_angle = phi-phi_prev; - if(turn_angle > Pi) turn_angle -= TwoPi; - if(turn_angle < -Pi) turn_angle += TwoPi; - return turn_angle; - }; - - auto eit = L.begin(); - auto enext = std::next(eit); - - eit->turn_angle = getTurnAngle(L.front().e, L.back().e); - - while(enext != L.end()) { - enext->turn_angle = getTurnAngle( enext->e, eit->e); - eit->is_turning_point = - signbit(enext->turn_angle) != signbit(eit->turn_angle); - ++eit; ++enext; - } - - L.back().is_turning_point = signbit(L.back().turn_angle) != - signbit(L.front().turn_angle); - - }; - - // Step 2: Fill the edgelists - fillEdgeList(A, stationary, 1); - fillEdgeList(B, orbiter, 1); - - int i = 1; - for(MarkedEdge& me : A) { - std::cout << "a" << i << ":\n\t" - << getX(me.e.first()) << " " << getY(me.e.first()) << "\n\t" - << getX(me.e.second()) << " " << getY(me.e.second()) << "\n\t" - << "Turning point: " << (me.is_turning_point ? "yes" : "no") - << std::endl; - - me.label = "a"; me.label += std::to_string(i); - i++; - } - - i = 1; - for(MarkedEdge& me : B) { - std::cout << "b" << i << ":\n\t" - << getX(me.e.first()) << " " << getY(me.e.first()) << "\n\t" - << getX(me.e.second()) << " " << getY(me.e.second()) << "\n\t" - << "Turning point: " << (me.is_turning_point ? "yes" : "no") - << std::endl; - me.label = "b"; me.label += std::to_string(i); - i++; - } - - // A reference to a marked edge that also knows its container - struct MarkedEdgeRef { - reference_wrapper eref; - reference_wrapper> container; - Coord dir = 1; // Direction modifier - - inline Radians angleX() const { return eref.get().e.angleToXaxis(); } - inline const Edge& edge() const { return eref.get().e; } - inline Edge& edge() { return eref.get().e; } - inline bool isTurningPoint() const { - return eref.get().is_turning_point; - } - inline bool isFrom(const vector& cont ) { - return &(container.get()) == &cont; - } - inline bool eq(const MarkedEdgeRef& mr) { - return &(eref.get()) == &(mr.eref.get()); - } - - MarkedEdgeRef(reference_wrapper er, - reference_wrapper> ec): - eref(er), container(ec), dir(1) {} - - MarkedEdgeRef(reference_wrapper er, - reference_wrapper> ec, - Coord d): - eref(er), container(ec), dir(d) {} - }; - - using EdgeRefList = vector; - - // Comparing two marked edges - auto sortfn = [](const MarkedEdgeRef& e1, const MarkedEdgeRef& e2) { - return e1.angleX() < e2.angleX(); - }; - - EdgeRefList Aref, Bref; // We create containers for the references - Aref.reserve(A.size()); Bref.reserve(B.size()); - - // Fill reference container for the stationary polygon - std::for_each(A.begin(), A.end(), [&Aref](MarkedEdge& me) { - Aref.emplace_back( ref(me), ref(Aref) ); - }); - - // Fill reference container for the orbiting polygon - std::for_each(B.begin(), B.end(), [&Bref](MarkedEdge& me) { - Bref.emplace_back( ref(me), ref(Bref) ); - }); - - auto mink = [sortfn] // the Mink(Q, R, direction) sub-procedure - (const EdgeRefList& Q, const EdgeRefList& R, bool positive) - { - - // Step 1 "merge sort_list(Q) and sort_list(R) to form merge_list(Q,R)" - // Sort the containers of edge references and merge them. - // Q could be sorted only once and be reused here but we would still - // need to merge it with sorted(R). - - EdgeRefList merged; - EdgeRefList S, seq; - merged.reserve(Q.size() + R.size()); - - merged.insert(merged.end(), R.begin(), R.end()); - std::stable_sort(merged.begin(), merged.end(), sortfn); - merged.insert(merged.end(), Q.begin(), Q.end()); - std::stable_sort(merged.begin(), merged.end(), sortfn); - - // Step 2 "set i = 1, k = 1, direction = 1, s1 = q1" - // we don't use i, instead, q is an iterator into Q. k would be an index - // into the merged sequence but we use "it" as an iterator for that - - // here we obtain references for the containers for later comparisons - const auto& Rcont = R.begin()->container.get(); - const auto& Qcont = Q.begin()->container.get(); - - // Set the initial direction - Coord dir = 1; - - // roughly i = 1 (so q = Q.begin()) and s1 = q1 so S[0] = q; - if(positive) { - auto q = Q.begin(); - S.emplace_back(*q); - - // Roughly step 3 - - std::cout << "merged size: " << merged.size() << std::endl; - auto mit = merged.begin(); - for(bool finish = false; !finish && q != Q.end();) { - ++q; // "Set i = i + 1" - - while(!finish && mit != merged.end()) { - if(mit->isFrom(Rcont)) { - auto s = *mit; - s.dir = dir; - S.emplace_back(s); - } - - if(mit->eq(*q)) { - S.emplace_back(*q); - if(mit->isTurningPoint()) dir = -dir; - if(q == Q.begin()) finish = true; - break; - } - - mit += dir; - // __nfp::advance(mit, merged, dir > 0); - } - } - } else { - auto q = Q.rbegin(); - S.emplace_back(*q); - - // Roughly step 3 - - std::cout << "merged size: " << merged.size() << std::endl; - auto mit = merged.begin(); - for(bool finish = false; !finish && q != Q.rend();) { - ++q; // "Set i = i + 1" - - while(!finish && mit != merged.end()) { - if(mit->isFrom(Rcont)) { - auto s = *mit; - s.dir = dir; - S.emplace_back(s); - } - - if(mit->eq(*q)) { - S.emplace_back(*q); - S.back().dir = -1; - if(mit->isTurningPoint()) dir = -dir; - if(q == Q.rbegin()) finish = true; - break; - } - - mit += dir; - // __nfp::advance(mit, merged, dir > 0); - } - } - } - - - // Step 4: - - // "Let starting edge r1 be in position si in sequence" - // whaaat? I guess this means the following: - auto it = S.begin(); - while(!it->eq(*R.begin())) ++it; - - // "Set j = 1, next = 2, direction = 1, seq1 = si" - // we don't use j, seq is expanded dynamically. - dir = 1; - auto next = std::next(R.begin()); seq.emplace_back(*it); - - // Step 5: - // "If all si edges have been allocated to seqj" should mean that - // we loop until seq has equal size with S - auto send = it; //it == S.begin() ? it : std::prev(it); - while(it != S.end()) { - ++it; if(it == S.end()) it = S.begin(); - if(it == send) break; - - if(it->isFrom(Qcont)) { - seq.emplace_back(*it); // "If si is from Q, j = j + 1, seqj = si" - - // "If si is a turning point in Q, - // direction = - direction, next = next + direction" - if(it->isTurningPoint()) { - dir = -dir; - next += dir; -// __nfp::advance(next, R, dir > 0); - } - } - - if(it->eq(*next) /*&& dir == next->dir*/) { // "If si = direction.rnext" - // "j = j + 1, seqj = si, next = next + direction" - seq.emplace_back(*it); - next += dir; -// __nfp::advance(next, R, dir > 0); - } - } - - return seq; - }; - - std::vector seqlist; - seqlist.reserve(Bref.size()); - - EdgeRefList Bslope = Bref; // copy Bref, we will make a slope diagram - - // make the slope diagram of B - std::sort(Bslope.begin(), Bslope.end(), sortfn); - - auto slopeit = Bslope.begin(); // search for the first turning point - while(!slopeit->isTurningPoint() && slopeit != Bslope.end()) slopeit++; - - if(slopeit == Bslope.end()) { - // no turning point means convex polygon. - seqlist.emplace_back(mink(Aref, Bref, true)); - } else { - int dir = 1; - - auto firstturn = Bref.begin(); - while(!firstturn->eq(*slopeit)) ++firstturn; - - assert(firstturn != Bref.end()); - - EdgeRefList bgroup; bgroup.reserve(Bref.size()); - bgroup.emplace_back(*slopeit); - - auto b_it = std::next(firstturn); - while(b_it != firstturn) { - if(b_it == Bref.end()) b_it = Bref.begin(); - - while(!slopeit->eq(*b_it)) { - __nfp::advance(slopeit, Bslope, dir > 0); - } - - if(!slopeit->isTurningPoint()) { - bgroup.emplace_back(*slopeit); - } else { - if(!bgroup.empty()) { - if(dir > 0) bgroup.emplace_back(*slopeit); - for(auto& me : bgroup) { - std::cout << me.eref.get().label << ", "; - } - std::cout << std::endl; - seqlist.emplace_back(mink(Aref, bgroup, dir == 1 ? true : false)); - bgroup.clear(); - if(dir < 0) bgroup.emplace_back(*slopeit); - } else { - bgroup.emplace_back(*slopeit); - } - - dir *= -1; - } - ++b_it; - } - } - -// while(it != Bref.end()) // This is step 3 and step 4 in one loop -// if(it->isTurningPoint()) { -// R = {R.last, it++}; -// auto seq = mink(Q, R, orientation); - -// // TODO step 6 (should be 5 shouldn't it?): linking edges from A -// // I don't get this step - -// seqlist.insert(seqlist.end(), seq.begin(), seq.end()); -// orientation = !orientation; -// } else ++it; - -// if(seqlist.empty()) seqlist = mink(Q, {Bref.begin(), Bref.end()}, true); - - // ///////////////////////////////////////////////////////////////////////// - // Algorithm 2: breaking Minkowski sums into track line trips - // ///////////////////////////////////////////////////////////////////////// - - - // ///////////////////////////////////////////////////////////////////////// - // Algorithm 3: finding the boundary of the NFP from track line trips - // ///////////////////////////////////////////////////////////////////////// - - - for(auto& seq : seqlist) { - std::cout << "seqlist size: " << seq.size() << std::endl; - for(auto& s : seq) { - std::cout << (s.dir > 0 ? "" : "-") << s.eref.get().label << ", "; - } - std::cout << std::endl; - } - - auto& seq = seqlist.front(); - RawShape rsh; - Vertex top_nfp; - std::vector edgelist; edgelist.reserve(seq.size()); - for(auto& s : seq) { - edgelist.emplace_back(s.eref.get().e); - } - - __nfp::buildPolygon(edgelist, rsh, top_nfp); - - return Result(rsh, top_nfp); + return {}; } // Specializable NFP implementation class. Specialize it if you have a faster diff --git a/src/libnest2d/include/libnest2d/libnest2d.hpp b/src/libnest2d/include/libnest2d/libnest2d.hpp index c7b252e5d5..5d74aa3d9d 100644 --- a/src/libnest2d/include/libnest2d/libnest2d.hpp +++ b/src/libnest2d/include/libnest2d/libnest2d.hpp @@ -8,13 +8,10 @@ #include #include -#include "geometry_traits.hpp" +#include namespace libnest2d { -namespace sl = shapelike; -namespace pl = pointlike; - /** * \brief An item to be placed on a bin. * @@ -422,13 +419,9 @@ private: static inline bool vsort(const Vertex& v1, const Vertex& v2) { - Coord &&x1 = getX(v1), &&x2 = getX(v2); - Coord &&y1 = getY(v1), &&y2 = getY(v2); - auto diff = y1 - y2; - if(std::abs(diff) <= std::numeric_limits::epsilon()) - return x1 < x2; - - return diff < 0; + TCompute x1 = getX(v1), x2 = getX(v2); + TCompute y1 = getY(v1), y2 = getY(v2); + return y1 == y2 ? x1 < x2 : y1 < y2; } }; diff --git a/src/libnest2d/include/libnest2d/optimizer.hpp b/src/libnest2d/include/libnest2d/optimizer.hpp index 962a47392f..e4c149f228 100644 --- a/src/libnest2d/include/libnest2d/optimizer.hpp +++ b/src/libnest2d/include/libnest2d/optimizer.hpp @@ -4,7 +4,8 @@ #include #include #include -#include "common.hpp" + +#include namespace libnest2d { namespace opt { @@ -60,6 +61,7 @@ enum class Method { L_SIMPLEX, L_SUBPLEX, G_GENETIC, + G_PARTICLE_SWARM //... }; diff --git a/src/libnest2d/include/libnest2d/optimizers/nlopt/CMakeLists.txt b/src/libnest2d/include/libnest2d/optimizers/nlopt/CMakeLists.txt index 4e16d4fc58..6f51718d8b 100644 --- a/src/libnest2d/include/libnest2d/optimizers/nlopt/CMakeLists.txt +++ b/src/libnest2d/include/libnest2d/optimizers/nlopt/CMakeLists.txt @@ -48,7 +48,7 @@ else() target_link_libraries(nloptOptimizer INTERFACE Nlopt::Nlopt) endif() -#target_sources( NloptOptimizer INTERFACE +#target_sources( nloptOptimizer INTERFACE #${CMAKE_CURRENT_SOURCE_DIR}/simplex.hpp #${CMAKE_CURRENT_SOURCE_DIR}/subplex.hpp #${CMAKE_CURRENT_SOURCE_DIR}/genetic.hpp @@ -57,5 +57,5 @@ endif() target_compile_definitions(nloptOptimizer INTERFACE LIBNEST2D_OPTIMIZER_NLOPT) -# And finally plug the NloptOptimizer into libnest2d -#target_link_libraries(libnest2d INTERFACE NloptOptimizer) +# And finally plug the nloptOptimizer into libnest2d +#target_link_libraries(libnest2d INTERFACE nloptOptimizer) diff --git a/src/libnest2d/include/libnest2d/optimizers/optimlib/CMakeLists.txt b/src/libnest2d/include/libnest2d/optimizers/optimlib/CMakeLists.txt deleted file mode 100644 index efbbd9cfb2..0000000000 --- a/src/libnest2d/include/libnest2d/optimizers/optimlib/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ -find_package(Armadillo REQUIRED) - -add_library(OptimlibOptimizer INTERFACE) -target_include_directories(OptimlibOptimizer INTERFACE ${ARMADILLO_INCLUDE_DIRS}) -target_link_libraries(OptimlibOptimizer INTERFACE ${ARMADILLO_LIBRARIES}) \ No newline at end of file diff --git a/src/libnest2d/include/libnest2d/placers/bottomleftplacer.hpp b/src/libnest2d/include/libnest2d/placers/bottomleftplacer.hpp index 7f10be7d73..28aaad5ce1 100644 --- a/src/libnest2d/include/libnest2d/placers/bottomleftplacer.hpp +++ b/src/libnest2d/include/libnest2d/placers/bottomleftplacer.hpp @@ -7,15 +7,15 @@ namespace libnest2d { namespace placers { -template struct Epsilon {}; +template struct DefaultEpsilon {}; template -struct Epsilon::value, T> > { +struct DefaultEpsilon::value, T> > { static const T Value = 1; }; template -struct Epsilon::value, T> > { +struct DefaultEpsilon::value, T> > { static const T Value = 1e-3; }; @@ -24,7 +24,7 @@ struct BLConfig { DECLARE_MAIN_TYPES(RawShape); Coord min_obj_distance = 0; - Coord epsilon = Epsilon::Value; + Coord epsilon = DefaultEpsilon::Value; bool allow_rotations = false; }; diff --git a/src/libnest2d/include/libnest2d/placers/nfpplacer.hpp b/src/libnest2d/include/libnest2d/placers/nfpplacer.hpp index 91affe9786..c1f15fe61f 100644 --- a/src/libnest2d/include/libnest2d/placers/nfpplacer.hpp +++ b/src/libnest2d/include/libnest2d/placers/nfpplacer.hpp @@ -103,14 +103,14 @@ Key hash(const _Item& item) { while(deg > N) { ms++; deg -= N; } ls += int(deg); ret.push_back(char(ms)); ret.push_back(char(ls)); - circ += seg.length(); + circ += std::sqrt(seg.template sqlength()); } it = ctr.begin(); nx = std::next(it); while(nx != ctr.end()) { Segment seg(*it++, *nx++); - auto l = int(M * seg.length() / circ); + auto l = int(M * std::sqrt(seg.template sqlength()) / circ); int ms = 'A', ls = 'A'; while(l > N) { ms++; l -= N; } ls += l; @@ -249,6 +249,11 @@ template class EdgeCache { std::vector holes_; double accuracy_ = 1.0; + + static double length(const Edge &e) + { + return std::sqrt(e.template sqlength()); + } void createCache(const RawShape& sh) { { // For the contour @@ -260,7 +265,7 @@ template class EdgeCache { while(next != endit) { contour_.emap.emplace_back(*(first++), *(next++)); - contour_.full_distance += contour_.emap.back().length(); + contour_.full_distance += length(contour_.emap.back()); contour_.distances.emplace_back(contour_.full_distance); } } @@ -275,7 +280,7 @@ template class EdgeCache { while(next != endit) { hc.emap.emplace_back(*(first++), *(next++)); - hc.full_distance += hc.emap.back().length(); + hc.full_distance += length(hc.emap.back()); hc.distances.emplace_back(hc.full_distance); } diff --git a/src/libnest2d/include/libnest2d/utils/boost_alg.hpp b/src/libnest2d/include/libnest2d/utils/boost_alg.hpp index baf1c6a105..16dee513b2 100644 --- a/src/libnest2d/include/libnest2d/utils/boost_alg.hpp +++ b/src/libnest2d/include/libnest2d/utils/boost_alg.hpp @@ -311,19 +311,19 @@ struct range_value { namespace libnest2d { // Now the algorithms that boost can provide... -namespace pointlike { -template<> -inline double distance(const PointImpl& p1, const PointImpl& p2 ) -{ - return boost::geometry::distance(p1, p2); -} +//namespace pointlike { +//template<> +//inline double distance(const PointImpl& p1, const PointImpl& p2 ) +//{ +// return boost::geometry::distance(p1, p2); +//} -template<> -inline double distance(const PointImpl& p, const bp2d::Segment& seg ) -{ - return boost::geometry::distance(p, seg); -} -} +//template<> +//inline double distance(const PointImpl& p, const bp2d::Segment& seg ) +//{ +// return boost::geometry::distance(p, seg); +//} +//} namespace shapelike { // Tell libnest2d how to make string out of a ClipperPolygon object @@ -382,16 +382,9 @@ inline bool touches( const PointImpl& point, const PolygonImpl& shape) } #ifndef DISABLE_BOOST_BOUNDING_BOX -template<> -inline bp2d::Box boundingBox(const PolygonImpl& sh, const PolygonTag&) -{ - bp2d::Box b; - boost::geometry::envelope(sh, b); - return b; -} template<> -inline bp2d::Box boundingBox(const PathImpl& sh, const PolygonTag&) +inline bp2d::Box boundingBox(const PathImpl& sh, const PathTag&) { bp2d::Box b; boost::geometry::envelope(sh, b); @@ -410,9 +403,9 @@ inline bp2d::Box boundingBox(const bp2d::Shapes& shapes, #ifndef DISABLE_BOOST_CONVEX_HULL template<> -inline PolygonImpl convexHull(const PolygonImpl& sh, const PolygonTag&) +inline PathImpl convexHull(const PathImpl& sh, const PathTag&) { - PolygonImpl ret; + PathImpl ret; boost::geometry::convex_hull(sh, ret); return ret; } diff --git a/src/libnest2d/include/libnest2d/utils/rotcalipers.hpp b/src/libnest2d/include/libnest2d/utils/rotcalipers.hpp new file mode 100644 index 0000000000..76f91ba0cf --- /dev/null +++ b/src/libnest2d/include/libnest2d/utils/rotcalipers.hpp @@ -0,0 +1,268 @@ +#ifndef ROTCALIPERS_HPP +#define ROTCALIPERS_HPP + +#include +#include +#include +#include + +#include + +namespace libnest2d { + +template> class RotatedBox { + Pt axis_; + Unit bottom_ = Unit(0), right_ = Unit(0); +public: + + RotatedBox() = default; + RotatedBox(const Pt& axis, Unit b, Unit r): + axis_(axis), bottom_(b), right_(r) {} + + inline long double area() const { + long double asq = pl::magnsq(axis_); + return cast(bottom_) * cast(right_) / asq; + } + + inline long double width() const { + return abs(bottom_) / std::sqrt(pl::magnsq(axis_)); + } + + inline long double height() const { + return abs(right_) / std::sqrt(pl::magnsq(axis_)); + } + + inline Unit bottom_extent() const { return bottom_; } + inline Unit right_extent() const { return right_; } + inline const Pt& axis() const { return axis_; } + + inline Radians angleToX() const { + double ret = std::atan2(getY(axis_), getX(axis_)); + auto s = std::signbit(ret); + if(s) ret += Pi_2; + return -ret; + } +}; + +template , class Unit = TCompute> +Poly removeCollinearPoints(const Poly& sh, Unit eps = Unit(0)) +{ + Poly ret; sl::reserve(ret, sl::contourVertexCount(sh)); + + Pt eprev = *sl::cbegin(sh) - *std::prev(sl::cend(sh)); + + auto it = sl::cbegin(sh); + auto itx = std::next(it); + if(itx != sl::cend(sh)) while (it != sl::cend(sh)) + { + Pt enext = *itx - *it; + + auto dp = pl::dotperp(eprev, enext); + if(abs(dp) > eps) sl::addVertex(ret, *it); + + eprev = enext; + if (++itx == sl::cend(sh)) itx = sl::cbegin(sh); + ++it; + } + + return ret; +} + +// The area of the bounding rectangle with the axis dir and support vertices +template, class R = TCompute> +inline R rectarea(const Pt& w, // the axis + const Pt& vb, const Pt& vr, + const Pt& vt, const Pt& vl) +{ + Unit a = pl::dot(w, vr - vl); + Unit b = pl::dot(-pl::perp(w), vt - vb); + R m = R(a) / pl::magnsq(w); + m = m * b; + return m; +}; + +template, + class R = TCompute, + class It = typename std::vector::const_iterator> +inline R rectarea(const Pt& w, const std::array& rect) +{ + return rectarea(w, *rect[0], *rect[1], *rect[2], *rect[3]); +} + +// This function is only applicable to counter-clockwise oriented convex +// polygons where only two points can be collinear witch each other. +template , + class Ratio = TCompute> +RotatedBox, Unit> minAreaBoundingBox(const RawShape& sh) +{ + using Point = TPoint; + using Iterator = typename TContour::const_iterator; + using pointlike::dot; using pointlike::magnsq; using pointlike::perp; + + // Get the first and the last vertex iterator + auto first = sl::cbegin(sh); + auto last = std::prev(sl::cend(sh)); + + // Check conditions and return undefined box if input is not sane. + if(last == first) return {}; + if(getX(*first) == getX(*last) && getY(*first) == getY(*last)) --last; + if(last - first < 2) return {}; + + RawShape shcpy; // empty at this point + { + Point p = *first, q = *std::next(first), r = *last; + + // Determine orientation from first 3 vertex (should be consistent) + Unit d = (Unit(getY(q)) - getY(p)) * (Unit(getX(r)) - getX(p)) - + (Unit(getX(q)) - getX(p)) * (Unit(getY(r)) - getY(p)); + + if(d > 0) { + // The polygon is clockwise. A flip is needed (for now) + sl::reserve(shcpy, last - first); + auto it = last; while(it != first) sl::addVertex(shcpy, *it--); + sl::addVertex(shcpy, *first); + first = sl::cbegin(shcpy); last = std::prev(sl::cend(shcpy)); + } + } + + // Cyclic iterator increment + auto inc = [&first, &last](Iterator& it) { + if(it == last) it = first; else ++it; + }; + + // Cyclic previous iterator + auto prev = [&first, &last](Iterator it) { + return it == first ? last : std::prev(it); + }; + + // Cyclic next iterator + auto next = [&first, &last](Iterator it) { + return it == last ? first : std::next(it); + }; + + // Establish initial (axis aligned) rectangle support verices by determining + // polygon extremes: + + auto it = first; + Iterator minX = it, maxX = it, minY = it, maxY = it; + + do { // Linear walk through the vertices and save the extreme positions + + Point v = *it, d = v - *minX; + if(getX(d) < 0 || (getX(d) == 0 && getY(d) < 0)) minX = it; + + d = v - *maxX; + if(getX(d) > 0 || (getX(d) == 0 && getY(d) > 0)) maxX = it; + + d = v - *minY; + if(getY(d) < 0 || (getY(d) == 0 && getX(d) > 0)) minY = it; + + d = v - *maxY; + if(getY(d) > 0 || (getY(d) == 0 && getX(d) < 0)) maxY = it; + + } while(++it != std::next(last)); + + // Update the vertices defining the bounding rectangle. The rectangle with + // the smallest rotation is selected and the supporting vertices are + // returned in the 'rect' argument. + auto update = [&next, &inc] + (const Point& w, std::array& rect) + { + Iterator B = rect[0], Bn = next(B); + Iterator R = rect[1], Rn = next(R); + Iterator T = rect[2], Tn = next(T); + Iterator L = rect[3], Ln = next(L); + + Point b = *Bn - *B, r = *Rn - *R, t = *Tn - *T, l = *Ln - *L; + Point pw = perp(w); + using Pt = Point; + + Unit dotwpb = dot( w, b), dotwpr = dot(-pw, r); + Unit dotwpt = dot(-w, t), dotwpl = dot( pw, l); + Unit dw = magnsq(w); + + std::array angles; + angles[0] = (Ratio(dotwpb) / magnsq(b)) * dotwpb; + angles[1] = (Ratio(dotwpr) / magnsq(r)) * dotwpr; + angles[2] = (Ratio(dotwpt) / magnsq(t)) * dotwpt; + angles[3] = (Ratio(dotwpl) / magnsq(l)) * dotwpl; + + using AngleIndex = std::pair; + std::vector A; A.reserve(4); + + for (size_t i = 3, j = 0; j < 4; i = j++) { + if(rect[i] != rect[j] && angles[i] < dw) { + auto iv = std::make_pair(angles[i], i); + auto it = std::lower_bound(A.begin(), A.end(), iv, + [](const AngleIndex& ai, + const AngleIndex& aj) + { + return ai.first > aj.first; + }); + + A.insert(it, iv); + } + } + + // The polygon is supposed to be a rectangle. + if(A.empty()) return false; + + auto amin = A.front().first; + auto imin = A.front().second; + for(auto& a : A) if(a.first == amin) inc(rect[a.second]); + + std::rotate(rect.begin(), rect.begin() + imin, rect.end()); + + return true; + }; + + Point w(1, 0); + Point w_min = w; + Ratio minarea((Unit(getX(*maxX)) - getX(*minX)) * + (Unit(getY(*maxY)) - getY(*minY))); + + std::array rect = {minY, maxX, maxY, minX}; + std::array minrect = rect; + + // An edge might be examined twice in which case the algorithm terminates. + size_t c = 0, count = last - first + 1; + std::vector edgemask(count, false); + + while(c++ < count) + { + // Update the support vertices, if cannot be updated, break the cycle. + if(! update(w, rect)) break; + + size_t eidx = size_t(rect[0] - first); + + if(edgemask[eidx]) break; + edgemask[eidx] = true; + + // get the unnormalized direction vector + w = *rect[0] - *prev(rect[0]); + + // get the area of the rotated rectangle + Ratio rarea = rectarea(w, rect); + + // Update min area and the direction of the min bounding box; + if(rarea <= minarea) { w_min = w; minarea = rarea; minrect = rect; } + } + + Unit a = dot(w_min, *minrect[1] - *minrect[3]); + Unit b = dot(-perp(w_min), *minrect[2] - *minrect[0]); + RotatedBox bb(w_min, a, b); + + return bb; +} + +template Radians minAreaBoundingBoxRotation(const RawShape& sh) +{ + return minAreaBoundingBox(sh).angleToX(); +} + + +} + +#endif // ROTCALIPERS_HPP diff --git a/src/libnest2d/src/libnest2d.cpp b/src/libnest2d/src/libnest2d.cpp new file mode 100644 index 0000000000..0214587874 --- /dev/null +++ b/src/libnest2d/src/libnest2d.cpp @@ -0,0 +1,23 @@ +#include + +namespace libnest2d { + +template class Nester; +template class Nester; + +template PackGroup nest(std::vector::iterator from, + std::vector::iterator to, + const Box& bin, + Coord dist = 0, + const NfpPlacer::Config& pconf, + const FirstFitSelection::Config& sconf); + +template PackGroup nest(std::vector::iterator from, + std::vector::iterator to, + const Box& bin, + ProgressFunction prg, + StopCondition scond, + Coord dist = 0, + const NfpPlacer::Config& pconf, + const FirstFitSelection::Config& sconf); +} diff --git a/src/libnest2d/tests/CMakeLists.txt b/src/libnest2d/tests/CMakeLists.txt index fc3cd309dd..1b7d8e3ae6 100644 --- a/src/libnest2d/tests/CMakeLists.txt +++ b/src/libnest2d/tests/CMakeLists.txt @@ -49,7 +49,12 @@ add_executable(tests_clipper_nlopt target_link_libraries(tests_clipper_nlopt libnest2d ${GTEST_LIBS_TO_LINK} ) -target_include_directories(tests_clipper_nlopt PRIVATE BEFORE - ${GTEST_INCLUDE_DIRS}) +target_include_directories(tests_clipper_nlopt PRIVATE BEFORE ${GTEST_INCLUDE_DIRS}) + +if(NOT LIBNEST2D_HEADER_ONLY) + target_link_libraries(tests_clipper_nlopt ${LIBNAME}) +else() + target_link_libraries(tests_clipper_nlopt libnest2d) +endif() add_test(libnest2d_tests tests_clipper_nlopt) diff --git a/src/libnest2d/tests/test.cpp b/src/libnest2d/tests/test.cpp index 3b0eae1618..5741e87b4f 100644 --- a/src/libnest2d/tests/test.cpp +++ b/src/libnest2d/tests/test.cpp @@ -3,11 +3,43 @@ #include #include "printer_parts.h" -#include +//#include #include "../tools/svgtools.hpp" +#include + +#include "boost/multiprecision/integer.hpp" +#include "boost/rational.hpp" + +//#include "../tools/Int128.hpp" + +//#include "gte/Mathematics/GteMinimumAreaBox2.h" + //#include "../tools/libnfpglue.hpp" //#include "../tools/nfp_svgnest_glue.hpp" +namespace libnest2d { +#if !defined(_MSC_VER) && defined(__SIZEOF_INT128__) && !defined(__APPLE__) +using LargeInt = __int128; +#else +using LargeInt = boost::multiprecision::int128_t; +template<> struct _NumTag { using Type = ScalarTag; }; +#endif +template struct _NumTag> { using Type = RationalTag; }; + +namespace nfp { + +template +struct NfpImpl +{ + NfpResult operator()(const S &sh, const S &other) + { + return nfpConvexOnly>(sh, other); + } +}; + +} +} + std::vector& prusaParts() { static std::vector ret; @@ -31,8 +63,8 @@ TEST(BasicFunctionality, Angles) ASSERT_DOUBLE_EQ(rad, Pi); ASSERT_DOUBLE_EQ(deg, 180); ASSERT_DOUBLE_EQ(deg2, 180); - ASSERT_DOUBLE_EQ(rad, (Radians) deg); - ASSERT_DOUBLE_EQ( (Degrees) rad, deg); + ASSERT_DOUBLE_EQ(rad, Radians(deg)); + ASSERT_DOUBLE_EQ( Degrees(rad), deg); ASSERT_TRUE(rad == deg); @@ -151,12 +183,12 @@ TEST(GeometryAlgorithms, Distance) { Segment seg(p1, p3); - ASSERT_DOUBLE_EQ(pointlike::distance(p2, seg), 7.0710678118654755); +// ASSERT_DOUBLE_EQ(pointlike::distance(p2, seg), 7.0710678118654755); auto result = pointlike::horizontalDistance(p2, seg); - auto check = [](Coord val, Coord expected) { - if(std::is_floating_point::value) + auto check = [](TCompute val, TCompute expected) { + if(std::is_floating_point>::value) ASSERT_DOUBLE_EQ(static_cast(val), static_cast(expected)); else @@ -415,7 +447,7 @@ TEST(GeometryAlgorithms, ArrangeRectanglesLoose) namespace { using namespace libnest2d; -template +template void exportSVG(std::vector>& result, const Bin& bin, int idx = 0) { @@ -500,6 +532,41 @@ TEST(GeometryAlgorithms, BottomLeftStressTest) { } } +TEST(GeometryAlgorithms, convexHull) { + using namespace libnest2d; + + ClipperLib::Path poly = PRINTER_PART_POLYGONS[0]; + + auto chull = sl::convexHull(poly); + + ASSERT_EQ(chull.size(), poly.size()); +} + + +TEST(GeometryAlgorithms, NestTest) { + std::vector input = prusaParts(); + + PackGroup result = libnest2d::nest(input, + Box(250000000, 210000000), + [](unsigned cnt) { + std::cout + << "parts left: " << cnt + << std::endl; + }); + + ASSERT_LE(result.size(), 2); + + int partsum = std::accumulate(result.begin(), + result.end(), + 0, + [](int s, + const decltype(result)::value_type &bin) { + return s += bin.size(); + }); + + ASSERT_EQ(input.size(), partsum); +} + namespace { struct ItemPair { @@ -713,7 +780,7 @@ void testNfp(const std::vector& testdata) { auto& exportfun = exportSVG; - auto onetest = [&](Item& orbiter, Item& stationary, unsigned testidx){ + auto onetest = [&](Item& orbiter, Item& stationary, unsigned /*testidx*/){ testcase++; orbiter.translate({210*SCALE, 0}); @@ -820,7 +887,7 @@ TEST(GeometryAlgorithms, mergePileWithPolygon) { rect2.translate({10, 0}); rect3.translate({25, 0}); - shapelike::Shapes pile; + TMultiShape pile; pile.push_back(rect1.transformedShape()); pile.push_back(rect2.transformedShape()); @@ -833,6 +900,126 @@ TEST(GeometryAlgorithms, mergePileWithPolygon) { ASSERT_EQ(shapelike::area(result.front()), ref.area()); } +namespace { + +long double refMinAreaBox(const PolygonImpl& p) { + + auto it = sl::cbegin(p), itx = std::next(it); + + long double min_area = std::numeric_limits::max(); + + + auto update_min = [&min_area, &it, &itx, &p]() { + Segment s(*it, *itx); + + PolygonImpl rotated = p; + sl::rotate(rotated, -s.angleToXaxis()); + auto bb = sl::boundingBox(rotated); + auto area = cast(sl::area(bb)); + if(min_area > area) min_area = area; + }; + + while(itx != sl::cend(p)) { + update_min(); + ++it; ++itx; + } + + it = std::prev(sl::cend(p)); itx = sl::cbegin(p); + update_min(); + + return min_area; +} + +template struct BoostGCD { + T operator()(const T &a, const T &b) { return boost::gcd(a, b); } +}; + +using Unit = int64_t; +using Ratio = boost::rational;// Rational; + +//double gteMinAreaBox(const PolygonImpl& p) { + +// using GteCoord = ClipperLib::cInt; +// using GtePoint = gte::Vector2; + +// gte::MinimumAreaBox2 mb; + +// std::vector points; +// points.reserve(p.Contour.size()); + +// for(auto& pt : p.Contour) points.emplace_back(GtePoint{GteCoord(pt.X), GteCoord(pt.Y)}); + +// mb(int(points.size()), points.data(), 0, nullptr, true); + +// auto min_area = double(mb.GetArea()); + +// return min_area; +//} + +} + +TEST(RotatingCalipers, MinAreaBBCClk) { +// PolygonImpl poly({{-50, 30}, {-50, -50}, {50, -50}, {50, 50}, {-40, 50}}); + +// PolygonImpl poly({{-50, 0}, {50, 0}, {0, 100}}); + + auto u = [](ClipperLib::cInt n) { return n*1000000; }; + PolygonImpl poly({ {u(0), u(0)}, {u(4), u(1)}, {u(2), u(4)}}); + + + long double arearef = refMinAreaBox(poly); + long double area = minAreaBoundingBox(poly).area(); +// double gtearea = gteMinAreaBox(poly); + + ASSERT_LE(std::abs(area - arearef), 500e6 ); +// ASSERT_LE(std::abs(gtearea - arearef), 500 ); +// ASSERT_DOUBLE_EQ(gtearea, arearef); +} + +TEST(RotatingCalipers, AllPrusaMinBB) { + size_t idx = 0; + long double err_epsilon = 500e6l; + + for(ClipperLib::Path rinput : PRINTER_PART_POLYGONS) { +// ClipperLib::Path rinput = PRINTER_PART_POLYGONS[idx]; +// rinput.pop_back(); +// std::reverse(rinput.begin(), rinput.end()); + +// PolygonImpl poly(removeCollinearPoints(rinput, 1000000)); + PolygonImpl poly(rinput); + + long double arearef = refMinAreaBox(poly); + auto bb = minAreaBoundingBox(rinput); + long double area = cast(bb.area()); +// double area = gteMinAreaBox(poly); + + bool succ = std::abs(arearef - area) < err_epsilon; + std::cout << idx++ << " " << (succ? "ok" : "failed") << " ref: " + << arearef << " actual: " << area << std::endl; + + ASSERT_TRUE(succ); + } + + for(ClipperLib::Path rinput : STEGOSAUR_POLYGONS) { + rinput.pop_back(); + std::reverse(rinput.begin(), rinput.end()); + + PolygonImpl poly(removeCollinearPoints(rinput, 1000000)); + + + long double arearef = refMinAreaBox(poly); + auto bb = minAreaBoundingBox(poly); + long double area = cast(bb.area()); +// double area = gteMinAreaBox(poly); + + bool succ = std::abs(arearef - area) < err_epsilon; + std::cout << idx++ << " " << (succ? "ok" : "failed") << " ref: " + << arearef << " actual: " << area << std::endl; + + ASSERT_TRUE(succ); + } +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff --git a/src/libslic3r/CMakeLists.txt b/src/libslic3r/CMakeLists.txt index 833b1ae629..dc52257aa2 100644 --- a/src/libslic3r/CMakeLists.txt +++ b/src/libslic3r/CMakeLists.txt @@ -130,13 +130,10 @@ add_library(libslic3r STATIC Print.hpp PrintBase.cpp PrintBase.hpp - PrintExport.hpp PrintConfig.cpp PrintConfig.hpp PrintObject.cpp PrintRegion.cpp - Rasterizer/Rasterizer.hpp - Rasterizer/Rasterizer.cpp SLAPrint.cpp SLAPrint.hpp SLA/SLAAutoSupports.hpp @@ -163,6 +160,8 @@ add_library(libslic3r STATIC MTUtils.hpp Zipper.hpp Zipper.cpp + MinAreaBoundingBox.hpp + MinAreaBoundingBox.cpp miniz_extension.hpp miniz_extension.cpp SLA/SLABoilerPlate.hpp @@ -175,6 +174,10 @@ add_library(libslic3r STATIC SLA/SLARotfinder.cpp SLA/SLABoostAdapter.hpp SLA/SLASpatIndex.hpp + SLA/SLARaster.hpp + SLA/SLARaster.cpp + SLA/SLARasterWriter.hpp + SLA/SLARasterWriter.cpp ) if (SLIC3R_PCH AND NOT SLIC3R_SYNTAXONLY) @@ -182,13 +185,13 @@ if (SLIC3R_PCH AND NOT SLIC3R_SYNTAXONLY) endif () target_compile_definitions(libslic3r PUBLIC -DUSE_TBB) -target_include_directories(libslic3r SYSTEM PUBLIC ${Boost_INCLUDE_DIRS}) target_include_directories(libslic3r PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${LIBNEST2D_INCLUDES} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) target_link_libraries(libslic3r libnest2d admesh + libigl miniz - ${Boost_LIBRARIES} + boost_libs clipper nowide ${EXPAT_LIBRARIES} diff --git a/src/libslic3r/Fill/FillRectilinear3.cpp b/src/libslic3r/Fill/FillRectilinear3.cpp index 8a0b90ead3..dab5842982 100644 --- a/src/libslic3r/Fill/FillRectilinear3.cpp +++ b/src/libslic3r/Fill/FillRectilinear3.cpp @@ -15,7 +15,7 @@ #include "FillRectilinear3.hpp" - #define SLIC3R_DEBUG +// #define SLIC3R_DEBUG // Make assert active if SLIC3R_DEBUG #ifdef SLIC3R_DEBUG diff --git a/src/libslic3r/Format/3mf.cpp b/src/libslic3r/Format/3mf.cpp index 38b34c4620..4793586e3c 100644 --- a/src/libslic3r/Format/3mf.cpp +++ b/src/libslic3r/Format/3mf.cpp @@ -1489,10 +1489,10 @@ namespace Slic3r { } // splits volume out of imported geometry - unsigned int triangles_count = volume_data.last_triangle_id - volume_data.first_triangle_id + 1; - ModelVolume* volume = object.add_volume(TriangleMesh()); - stl_file& stl = volume->mesh.stl; - stl.stats.type = inmemory; + TriangleMesh triangle_mesh; + stl_file &stl = triangle_mesh.stl; + unsigned int triangles_count = volume_data.last_triangle_id - volume_data.first_triangle_id + 1; + stl.stats.type = inmemory; stl.stats.number_of_facets = (uint32_t)triangles_count; stl.stats.original_num_facets = (int)stl.stats.number_of_facets; stl_allocate(&stl); @@ -1509,9 +1509,11 @@ namespace Slic3r { } } - stl_get_size(&stl); - volume->mesh.repair(); - volume->center_geometry(); + stl_get_size(&stl); + triangle_mesh.repair(); + + ModelVolume* volume = object.add_volume(std::move(triangle_mesh)); + volume->center_geometry_after_creation(); volume->calculate_convex_hull(); // apply volume's name and config data @@ -1879,29 +1881,28 @@ namespace Slic3r { if (volume == nullptr) continue; + if (!volume->mesh().repaired) + throw std::runtime_error("store_3mf() requires repair()"); + if (!volume->mesh().has_shared_vertices()) + throw std::runtime_error("store_3mf() requires shared vertices"); + volumes_offsets.insert(VolumeToOffsetsMap::value_type(volume, Offsets(vertices_count))).first; - if (!volume->mesh.repaired) - volume->mesh.repair(); - - stl_file& stl = volume->mesh.stl; - if (stl.v_shared == nullptr) - stl_generate_shared_vertices(&stl); - - if (stl.stats.shared_vertices == 0) + const indexed_triangle_set &its = volume->mesh().its; + if (its.vertices.empty()) { add_error("Found invalid mesh"); return false; } - vertices_count += stl.stats.shared_vertices; + vertices_count += its.vertices.size(); const Transform3d& matrix = volume->get_matrix(); - for (int i = 0; i < stl.stats.shared_vertices; ++i) + for (size_t i = 0; i < its.vertices.size(); ++i) { stream << " <" << VERTEX_TAG << " "; - Vec3f v = (matrix * stl.v_shared[i].cast()).cast(); + Vec3f v = (matrix * its.vertices[i].cast()).cast(); stream << "x=\"" << v(0) << "\" "; stream << "y=\"" << v(1) << "\" "; stream << "z=\"" << v(2) << "\" />\n"; @@ -1920,19 +1921,19 @@ namespace Slic3r { VolumeToOffsetsMap::iterator volume_it = volumes_offsets.find(volume); assert(volume_it != volumes_offsets.end()); - stl_file& stl = volume->mesh.stl; + const indexed_triangle_set &its = volume->mesh().its; // updates triangle offsets volume_it->second.first_triangle_id = triangles_count; - triangles_count += stl.stats.number_of_facets; + triangles_count += its.indices.size(); volume_it->second.last_triangle_id = triangles_count - 1; - for (uint32_t i = 0; i < stl.stats.number_of_facets; ++i) + for (size_t i = 0; i < its.indices.size(); ++ i) { stream << " <" << TRIANGLE_TAG << " "; for (int j = 0; j < 3; ++j) { - stream << "v" << j + 1 << "=\"" << stl.v_indices[i].vertex[j] + volume_it->second.first_vertex_id << "\" "; + stream << "v" << j + 1 << "=\"" << its.indices[i][j] + volume_it->second.first_vertex_id << "\" "; } stream << "/>\n"; } diff --git a/src/libslic3r/Format/AMF.cpp b/src/libslic3r/Format/AMF.cpp index d26b5f3ed9..a33d21c9fa 100644 --- a/src/libslic3r/Format/AMF.cpp +++ b/src/libslic3r/Format/AMF.cpp @@ -522,7 +522,8 @@ void AMFParserContext::endElement(const char * /* name */) case NODE_TYPE_VOLUME: { assert(m_object && m_volume); - stl_file &stl = m_volume->mesh.stl; + TriangleMesh mesh; + stl_file &stl = mesh.stl; stl.stats.type = inmemory; stl.stats.number_of_facets = int(m_volume_facets.size() / 3); stl.stats.original_num_facets = stl.stats.number_of_facets; @@ -533,8 +534,9 @@ void AMFParserContext::endElement(const char * /* name */) memcpy(facet.vertex[v].data(), &m_object_vertices[m_volume_facets[i ++] * 3], 3 * sizeof(float)); } stl_get_size(&stl); - m_volume->mesh.repair(); - m_volume->center_geometry(); + mesh.repair(); + m_volume->set_mesh(std::move(mesh)); + m_volume->center_geometry_after_creation(); m_volume->calculate_convex_hull(); m_volume_facets.clear(); m_volume = nullptr; @@ -923,23 +925,23 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config) int num_vertices = 0; for (ModelVolume *volume : object->volumes) { vertices_offsets.push_back(num_vertices); - if (! volume->mesh.repaired) + if (! volume->mesh().repaired) throw std::runtime_error("store_amf() requires repair()"); - auto &stl = volume->mesh.stl; - if (stl.v_shared == nullptr) - stl_generate_shared_vertices(&stl); + if (! volume->mesh().has_shared_vertices()) + throw std::runtime_error("store_amf() requires shared vertices"); + const indexed_triangle_set &its = volume->mesh().its; const Transform3d& matrix = volume->get_matrix(); - for (size_t i = 0; i < stl.stats.shared_vertices; ++i) { + for (size_t i = 0; i < its.vertices.size(); ++i) { stream << " \n"; stream << " \n"; - Vec3f v = (matrix * stl.v_shared[i].cast()).cast(); + Vec3f v = (matrix * its.vertices[i].cast()).cast(); stream << " " << v(0) << "\n"; stream << " " << v(1) << "\n"; stream << " " << v(2) << "\n"; stream << " \n"; stream << " \n"; } - num_vertices += stl.stats.shared_vertices; + num_vertices += its.vertices.size(); } stream << " \n"; for (size_t i_volume = 0; i_volume < object->volumes.size(); ++i_volume) { @@ -956,10 +958,11 @@ bool store_amf(const char *path, Model *model, const DynamicPrintConfig *config) if (volume->is_modifier()) stream << " 1\n"; stream << " " << ModelVolume::type_to_string(volume->type()) << "\n"; - for (int i = 0; i < (int)volume->mesh.stl.stats.number_of_facets; ++i) { + const indexed_triangle_set &its = volume->mesh().its; + for (size_t i = 0; i < (int)its.indices.size(); ++i) { stream << " \n"; for (int j = 0; j < 3; ++j) - stream << " " << volume->mesh.stl.v_indices[i].vertex[j] + vertices_offset << "\n"; + stream << " " << its.indices[i][j] + vertices_offset << "\n"; stream << " \n"; } stream << " \n"; diff --git a/src/libslic3r/Format/PRUS.cpp b/src/libslic3r/Format/PRUS.cpp index 502cac6e9e..03ea71a83e 100644 --- a/src/libslic3r/Format/PRUS.cpp +++ b/src/libslic3r/Format/PRUS.cpp @@ -161,16 +161,15 @@ static void extract_model_from_archive( else { // Header has been extracted. Now read the faces. stl_file &stl = mesh.stl; - stl.error = 0; stl.stats.type = inmemory; stl.stats.number_of_facets = header.nTriangles; stl.stats.original_num_facets = header.nTriangles; stl_allocate(&stl); if (header.nTriangles > 0 && data.size() == 50 * header.nTriangles + sizeof(StlHeader)) { - memcpy((char*)stl.facet_start, data.data() + sizeof(StlHeader), 50 * header.nTriangles); + memcpy((char*)stl.facet_start.data(), data.data() + sizeof(StlHeader), 50 * header.nTriangles); if (sizeof(stl_facet) > SIZEOF_STL_FACET) { // The stl.facet_start is not packed tightly. Unpack the array of stl_facets. - unsigned char *data = (unsigned char*)stl.facet_start; + unsigned char *data = (unsigned char*)stl.facet_start.data(); for (size_t i = header.nTriangles - 1; i > 0; -- i) memmove(data + i * sizeof(stl_facet), data + i * SIZEOF_STL_FACET, SIZEOF_STL_FACET); } @@ -257,7 +256,7 @@ static void extract_model_from_archive( stl.stats.number_of_facets = (uint32_t)facets.size(); stl.stats.original_num_facets = (int)facets.size(); stl_allocate(&stl); - memcpy((void*)stl.facet_start, facets.data(), facets.size() * 50); + memcpy((void*)stl.facet_start.data(), facets.data(), facets.size() * 50); stl_get_size(&stl); mesh.repair(); // Add a mesh to a model. diff --git a/src/libslic3r/Format/STL.cpp b/src/libslic3r/Format/STL.cpp index b00623d1d6..932906fe0e 100644 --- a/src/libslic3r/Format/STL.cpp +++ b/src/libslic3r/Format/STL.cpp @@ -17,8 +17,7 @@ namespace Slic3r { bool load_stl(const char *path, Model *model, const char *object_name_in) { TriangleMesh mesh; - mesh.ReadSTLFile(path); - if (mesh.stl.error) { + if (! mesh.ReadSTLFile(path)) { // die "Failed to open $file\n" if !-e $path; return false; } diff --git a/src/libslic3r/Int128.hpp b/src/libslic3r/Int128.hpp index 56dc5f461d..8dc9e012dd 100644 --- a/src/libslic3r/Int128.hpp +++ b/src/libslic3r/Int128.hpp @@ -37,6 +37,8 @@ * * *******************************************************************************/ +#ifndef SLIC3R_INT128_HPP +#define SLIC3R_INT128_HPP // #define SLIC3R_DEBUG // Make assert active if SLIC3R_DEBUG @@ -48,6 +50,8 @@ #endif #include +#include +#include #if ! defined(_MSC_VER) && defined(__SIZEOF_INT128__) #define HAS_INTRINSIC_128_TYPE @@ -293,3 +297,5 @@ public: return sign_determinant_2x2(p1, q1, p2, q2) * invert; } }; + +#endif // SLIC3R_INT128_HPP diff --git a/src/libslic3r/MTUtils.hpp b/src/libslic3r/MTUtils.hpp index 7e91ace328..0afe3563f9 100644 --- a/src/libslic3r/MTUtils.hpp +++ b/src/libslic3r/MTUtils.hpp @@ -5,6 +5,7 @@ #include // for std::lock_guard #include // for std::function #include // for std::forward +#include namespace Slic3r { @@ -182,6 +183,14 @@ public: inline bool empty() const { return size() == 0; } }; +template bool all_of(const C &container) { + return std::all_of(container.begin(), + container.end(), + [](const typename C::value_type &v) { + return static_cast(v); + }); +} + } #endif // MTUTILS_HPP diff --git a/src/libslic3r/MinAreaBoundingBox.cpp b/src/libslic3r/MinAreaBoundingBox.cpp new file mode 100644 index 0000000000..6fc1b3327a --- /dev/null +++ b/src/libslic3r/MinAreaBoundingBox.cpp @@ -0,0 +1,142 @@ +#include "MinAreaBoundingBox.hpp" + +#include +#include + +#include + +#if !defined(HAS_INTRINSIC_128_TYPE) || defined(__APPLE__) +#include +#endif + +#include +#include + +namespace libnest2d { + +template<> struct PointType { using Type = Slic3r::Point; }; +template<> struct CoordType { using Type = coord_t; }; +template<> struct ShapeTag { using Type = PolygonTag; }; +template<> struct ShapeTag { using Type = PolygonTag; }; +template<> struct ShapeTag { using Type = PathTag; }; +template<> struct ShapeTag { using Type = PointTag; }; +template<> struct ContourType { using Type = Slic3r::Points; }; +template<> struct ContourType { using Type = Slic3r::Points; }; + +namespace pointlike { + +template<> inline coord_t x(const Slic3r::Point& p) { return p.x(); } +template<> inline coord_t y(const Slic3r::Point& p) { return p.y(); } +template<> inline coord_t& x(Slic3r::Point& p) { return p.x(); } +template<> inline coord_t& y(Slic3r::Point& p) { return p.y(); } + +} // pointlike + +namespace shapelike { +template<> inline Slic3r::Points& contour(Slic3r::ExPolygon& sh) { return sh.contour.points; } +template<> inline const Slic3r::Points& contour(const Slic3r::ExPolygon& sh) { return sh.contour.points; } +template<> inline Slic3r::Points& contour(Slic3r::Polygon& sh) { return sh.points; } +template<> inline const Slic3r::Points& contour(const Slic3r::Polygon& sh) { return sh.points; } + +template<> Slic3r::Points::iterator begin(Slic3r::Points& pts, const PathTag&) { return pts.begin();} +template<> Slic3r::Points::const_iterator cbegin(const Slic3r::Points& pts, const PathTag&) { return pts.begin(); } +template<> Slic3r::Points::iterator end(Slic3r::Points& pts, const PathTag&) { return pts.end();} +template<> Slic3r::Points::const_iterator cend(const Slic3r::Points& pts, const PathTag&) { return pts.cend(); } + +template<> inline Slic3r::ExPolygon create(Slic3r::Points&& contour) +{ + Slic3r::ExPolygon expoly; expoly.contour.points.swap(contour); + return expoly; +} + +template<> inline Slic3r::Polygon create(Slic3r::Points&& contour) +{ + Slic3r::Polygon poly; poly.points.swap(contour); + return poly; +} + +} // shapelike +} // libnest2d + +namespace Slic3r { + +// Used as compute type. +using Unit = int64_t; + +#if !defined(HAS_INTRINSIC_128_TYPE) || defined(__APPLE__) +using Rational = boost::rational; +#else +using Rational = boost::rational<__int128>; +#endif + +MinAreaBoundigBox::MinAreaBoundigBox(const Polygon &p, PolygonLevel pc) +{ + const Polygon& chull = pc == pcConvex ? p : libnest2d::sl::convexHull(p); + + libnest2d::RotatedBox box = + libnest2d::minAreaBoundingBox(chull); + + m_right = box.right_extent(); + m_bottom = box.bottom_extent(); + m_axis = box.axis(); +} + +MinAreaBoundigBox::MinAreaBoundigBox(const ExPolygon &p, PolygonLevel pc) +{ + const ExPolygon& chull = pc == pcConvex ? p : libnest2d::sl::convexHull(p); + + libnest2d::RotatedBox box = + libnest2d::minAreaBoundingBox(chull); + + m_right = box.right_extent(); + m_bottom = box.bottom_extent(); + m_axis = box.axis(); +} + +MinAreaBoundigBox::MinAreaBoundigBox(const Points &pts, PolygonLevel pc) +{ + const Points& chull = pc == pcConvex ? pts : libnest2d::sl::convexHull(pts); + + libnest2d::RotatedBox box = + libnest2d::minAreaBoundingBox(chull); + + m_right = box.right_extent(); + m_bottom = box.bottom_extent(); + m_axis = box.axis(); +} + +double MinAreaBoundigBox::angle_to_X() const +{ + double ret = std::atan2(m_axis.y(), m_axis.x()); + auto s = std::signbit(ret); + if(s) ret += 2 * PI; + return -ret; +} + +long double MinAreaBoundigBox::width() const +{ + return std::abs(m_bottom) / std::sqrt(libnest2d::pl::magnsq(m_axis)); +} + +long double MinAreaBoundigBox::height() const +{ + return std::abs(m_right) / std::sqrt(libnest2d::pl::magnsq(m_axis)); +} + +long double MinAreaBoundigBox::area() const +{ + long double asq = libnest2d::pl::magnsq(m_axis); + return m_bottom * m_right / asq; +} + +void remove_collinear_points(Polygon &p) +{ + p = libnest2d::removeCollinearPoints(p, Unit(0)); +} + +void remove_collinear_points(ExPolygon &p) +{ + p = libnest2d::removeCollinearPoints(p, Unit(0)); +} + +} diff --git a/src/libslic3r/MinAreaBoundingBox.hpp b/src/libslic3r/MinAreaBoundingBox.hpp new file mode 100644 index 0000000000..30d0e9799d --- /dev/null +++ b/src/libslic3r/MinAreaBoundingBox.hpp @@ -0,0 +1,59 @@ +#ifndef MINAREABOUNDINGBOX_HPP +#define MINAREABOUNDINGBOX_HPP + +#include "libslic3r/Point.hpp" + +namespace Slic3r { + +class Polygon; +class ExPolygon; + +void remove_collinear_points(Polygon& p); +void remove_collinear_points(ExPolygon& p); + +/// A class that holds a rotated bounding box. If instantiated with a polygon +/// type it will hold the minimum area bounding box for the given polygon. +/// If the input polygon is convex, the complexity is linear to the number of +/// points. Otherwise a convex hull of O(n*log(n)) has to be performed. +class MinAreaBoundigBox { + Point m_axis; + long double m_bottom = 0.0l, m_right = 0.0l; +public: + + // Polygons can be convex or simple (convex or concave with possible holes) + enum PolygonLevel { + pcConvex, pcSimple + }; + + // Constructors with various types of geometry data used in Slic3r. + // If the convexity is known apriory, pcConvex can be used to skip + // convex hull calculation. It is very important that the input polygons + // do NOT have any collinear points (except for the first and the last + // vertex being the same -- meaning a closed polygon for boost) + // To make sure this constraint is satisfied, you can call + // remove_collinear_points on the input polygon before handing over here) + explicit MinAreaBoundigBox(const Polygon&, PolygonLevel = pcSimple); + explicit MinAreaBoundigBox(const ExPolygon&, PolygonLevel = pcSimple); + explicit MinAreaBoundigBox(const Points&, PolygonLevel = pcSimple); + + // Returns the angle in radians needed for the box to be aligned with the + // X axis. Rotate the polygon by this angle and it will be aligned. + double angle_to_X() const; + + // The box width + long double width() const; + + // The box height + long double height() const; + + // The box area + long double area() const; + + // The axis of the rotated box. If the angle_to_X is not sufficiently + // precise, use this unnormalized direction vector. + const Point& axis() const { return m_axis; } +}; + +} + +#endif // MINAREABOUNDINGBOX_HPP diff --git a/src/libslic3r/Model.cpp b/src/libslic3r/Model.cpp index 3b1bd5df22..8e879a3e68 100644 --- a/src/libslic3r/Model.cpp +++ b/src/libslic3r/Model.cpp @@ -160,12 +160,6 @@ Model Model::read_from_archive(const std::string &input_file, DynamicPrintConfig return model; } -void Model::repair() -{ - for (ModelObject *o : this->objects) - o->repair(); -} - ModelObject* Model::add_object() { this->objects.emplace_back(new ModelObject(this)); @@ -472,7 +466,7 @@ bool Model::looks_like_multipart_object() const if (obj->volumes.size() > 1 || obj->config.keys().size() > 1) return false; for (const ModelVolume *vol : obj->volumes) { - double zmin_this = vol->mesh.bounding_box().min(2); + double zmin_this = vol->mesh().bounding_box().min(2); if (zmin == std::numeric_limits::max()) zmin = zmin_this; else if (std::abs(zmin - zmin_this) > EPSILON) @@ -679,7 +673,7 @@ ModelVolume* ModelObject::add_volume(const TriangleMesh &mesh) { ModelVolume* v = new ModelVolume(this, mesh); this->volumes.push_back(v); - v->center_geometry(); + v->center_geometry_after_creation(); this->invalidate_bounding_box(); return v; } @@ -688,7 +682,7 @@ ModelVolume* ModelObject::add_volume(TriangleMesh &&mesh) { ModelVolume* v = new ModelVolume(this, std::move(mesh)); this->volumes.push_back(v); - v->center_geometry(); + v->center_geometry_after_creation(); this->invalidate_bounding_box(); return v; } @@ -697,8 +691,9 @@ ModelVolume* ModelObject::add_volume(const ModelVolume &other) { ModelVolume* v = new ModelVolume(this, other); this->volumes.push_back(v); - v->center_geometry(); - this->invalidate_bounding_box(); + // The volume should already be centered at this point of time when copying shared pointers of the triangle mesh and convex hull. +// v->center_geometry_after_creation(); +// this->invalidate_bounding_box(); return v; } @@ -706,7 +701,7 @@ ModelVolume* ModelObject::add_volume(const ModelVolume &other, TriangleMesh &&me { ModelVolume* v = new ModelVolume(this, other, std::move(mesh)); this->volumes.push_back(v); - v->center_geometry(); + v->center_geometry_after_creation(); this->invalidate_bounding_box(); return v; } @@ -827,7 +822,7 @@ TriangleMesh ModelObject::raw_mesh() const for (const ModelVolume *v : this->volumes) if (v->is_model_part()) { - TriangleMesh vol_mesh(v->mesh); + TriangleMesh vol_mesh(v->mesh()); vol_mesh.transform(v->get_matrix()); mesh.merge(vol_mesh); } @@ -840,7 +835,7 @@ TriangleMesh ModelObject::full_raw_mesh() const TriangleMesh mesh; for (const ModelVolume *v : this->volumes) { - TriangleMesh vol_mesh(v->mesh); + TriangleMesh vol_mesh(v->mesh()); vol_mesh.transform(v->get_matrix()); mesh.merge(vol_mesh); } @@ -854,7 +849,7 @@ const BoundingBoxf3& ModelObject::raw_mesh_bounding_box() const m_raw_mesh_bounding_box.reset(); for (const ModelVolume *v : this->volumes) if (v->is_model_part()) - m_raw_mesh_bounding_box.merge(v->mesh.transformed_bounding_box(v->get_matrix())); + m_raw_mesh_bounding_box.merge(v->mesh().transformed_bounding_box(v->get_matrix())); } return m_raw_mesh_bounding_box; } @@ -863,7 +858,7 @@ BoundingBoxf3 ModelObject::full_raw_mesh_bounding_box() const { BoundingBoxf3 bb; for (const ModelVolume *v : this->volumes) - bb.merge(v->mesh.transformed_bounding_box(v->get_matrix())); + bb.merge(v->mesh().transformed_bounding_box(v->get_matrix())); return bb; } @@ -881,7 +876,7 @@ const BoundingBoxf3& ModelObject::raw_bounding_box() const for (const ModelVolume *v : this->volumes) { if (v->is_model_part()) - m_raw_bounding_box.merge(v->mesh.transformed_bounding_box(inst_matrix * v->get_matrix())); + m_raw_bounding_box.merge(v->mesh().transformed_bounding_box(inst_matrix * v->get_matrix())); } } return m_raw_bounding_box; @@ -895,7 +890,7 @@ BoundingBoxf3 ModelObject::instance_bounding_box(size_t instance_idx, bool dont_ for (ModelVolume *v : this->volumes) { if (v->is_model_part()) - bb.merge(v->mesh.transformed_bounding_box(inst_matrix * v->get_matrix())); + bb.merge(v->mesh().transformed_bounding_box(inst_matrix * v->get_matrix())); } return bb; } @@ -908,21 +903,20 @@ Polygon ModelObject::convex_hull_2d(const Transform3d &trafo_instance) const Points pts; for (const ModelVolume *v : this->volumes) if (v->is_model_part()) { - const stl_file &stl = v->mesh.stl; Transform3d trafo = trafo_instance * v->get_matrix(); - if (stl.v_shared == nullptr) { + const indexed_triangle_set &its = v->mesh().its; + if (its.vertices.empty()) { // Using the STL faces. - for (unsigned int i = 0; i < stl.stats.number_of_facets; ++ i) { - const stl_facet &facet = stl.facet_start[i]; + const stl_file& stl = v->mesh().stl; + for (const stl_facet &facet : stl.facet_start) for (size_t j = 0; j < 3; ++ j) { Vec3d p = trafo * facet.vertex[j].cast(); pts.emplace_back(coord_t(scale_(p.x())), coord_t(scale_(p.y()))); } - } } else { // Using the shared vertices should be a bit quicker than using the STL faces. - for (int i = 0; i < stl.stats.shared_vertices; ++ i) { - Vec3d p = trafo * stl.v_shared[i].cast(); + for (size_t i = 0; i < its.vertices.size(); ++ i) { + Vec3d p = trafo * its.vertices[i].cast(); pts.emplace_back(coord_t(scale_(p.x())), coord_t(scale_(p.y()))); } } @@ -1039,6 +1033,7 @@ void ModelObject::mirror(Axis axis) this->invalidate_bounding_box(); } +// This method could only be called before the meshes of this ModelVolumes are not shared! void ModelObject::scale_mesh(const Vec3d &versor) { for (ModelVolume *v : this->volumes) @@ -1062,14 +1057,14 @@ size_t ModelObject::facets_count() const size_t num = 0; for (const ModelVolume *v : this->volumes) if (v->is_model_part()) - num += v->mesh.stl.stats.number_of_facets; + num += v->mesh().stl.stats.number_of_facets; return num; } bool ModelObject::needed_repair() const { for (const ModelVolume *v : this->volumes) - if (v->is_model_part() && v->mesh.needed_repair()) + if (v->is_model_part() && v->mesh().needed_repair()) return true; return false; } @@ -1135,11 +1130,12 @@ ModelObjectPtrs ModelObject::cut(size_t instance, coordf_t z, bool keep_upper, b // Transform the mesh by the combined transformation matrix. // Flip the triangles in case the composite transformation is left handed. - volume->mesh.transform(instance_matrix * volume_matrix, true); + TriangleMesh mesh(volume->mesh()); + mesh.transform(instance_matrix * volume_matrix, true); + volume->reset_mesh(); // Perform cut - volume->mesh.require_shared_vertices(); // TriangleMeshSlicer needs this - TriangleMeshSlicer tms(&volume->mesh); + TriangleMeshSlicer tms(&mesh); tms.cut(float(z), &upper_mesh, &lower_mesh); // Reset volume transformation except for offset @@ -1158,14 +1154,14 @@ ModelObjectPtrs ModelObject::cut(size_t instance, coordf_t z, bool keep_upper, b if (keep_upper && upper_mesh.facets_count() > 0) { ModelVolume* vol = upper->add_volume(upper_mesh); - vol->name = volume->name; - vol->config = volume->config; + vol->name = volume->name; + vol->config = volume->config; vol->set_material(volume->material_id(), *volume->material()); } if (keep_lower && lower_mesh.facets_count() > 0) { ModelVolume* vol = lower->add_volume(lower_mesh); - vol->name = volume->name; - vol->config = volume->config; + vol->name = volume->name; + vol->config = volume->config; vol->set_material(volume->material_id(), *volume->material()); // Compute the lower part instances' bounding boxes to figure out where to place @@ -1233,7 +1229,7 @@ void ModelObject::split(ModelObjectPtrs* new_objects) } ModelVolume* volume = this->volumes.front(); - TriangleMeshPtrs meshptrs = volume->mesh.split(); + TriangleMeshPtrs meshptrs = volume->mesh().split(); for (TriangleMesh *mesh : meshptrs) { mesh->repair(); @@ -1260,12 +1256,6 @@ void ModelObject::split(ModelObjectPtrs* new_objects) return; } -void ModelObject::repair() -{ - for (ModelVolume *v : this->volumes) - v->mesh.repair(); -} - // Support for non-uniform scaling of instances. If an instance is rotated by angles, which are not multiples of ninety degrees, // then the scaling in world coordinate system is not representable by the Geometry::Transformation structure. // This situation is solved by baking in the instance transformation into the mesh vertices. @@ -1295,8 +1285,8 @@ void ModelObject::bake_xy_rotation_into_meshes(size_t instance_idx) // Adjust the meshes. // Transformation to be applied to the meshes. - Eigen::Matrix3d mesh_trafo_3x3 = reference_trafo.get_matrix(true, false, uniform_scaling, ! has_mirrorring).matrix().block<3, 3>(0, 0); - Transform3d volume_offset_correction = this->instances[instance_idx]->get_transformation().get_matrix().inverse() * reference_trafo.get_matrix(); + Eigen::Matrix3d mesh_trafo_3x3 = reference_trafo.get_matrix(true, false, uniform_scaling, ! has_mirrorring).matrix().block<3, 3>(0, 0); + Transform3d volume_offset_correction = this->instances[instance_idx]->get_transformation().get_matrix().inverse() * reference_trafo.get_matrix(); for (ModelVolume *model_volume : this->volumes) { const Geometry::Transformation volume_trafo = model_volume->get_transformation(); bool volume_left_handed = volume_trafo.is_left_handed(); @@ -1306,7 +1296,8 @@ void ModelObject::bake_xy_rotation_into_meshes(size_t instance_idx) double volume_new_scaling_factor = volume_uniform_scaling ? volume_trafo.get_scaling_factor().x() : 1.; // Transform the mesh. Matrix3d volume_trafo_3x3 = volume_trafo.get_matrix(true, false, volume_uniform_scaling, !volume_has_mirrorring).matrix().block<3, 3>(0, 0); - model_volume->transform_mesh(mesh_trafo_3x3 * volume_trafo_3x3, left_handed != volume_left_handed); + // Following method creates a new shared_ptr + model_volume->transform_this_mesh(mesh_trafo_3x3 * volume_trafo_3x3, left_handed != volume_left_handed); // Reset the rotation, scaling and mirroring. model_volume->set_rotation(Vec3d(0., 0., 0.)); model_volume->set_scaling_factor(Vec3d(volume_new_scaling_factor, volume_new_scaling_factor, volume_new_scaling_factor)); @@ -1347,13 +1338,9 @@ double ModelObject::get_instance_min_z(size_t instance_idx) const Transform3d mv = mi * v->get_matrix(); const TriangleMesh& hull = v->get_convex_hull(); - for (uint32_t f = 0; f < hull.stl.stats.number_of_facets; ++f) - { - const stl_facet* facet = hull.stl.facet_start + f; - min_z = std::min(min_z, Vec3d::UnitZ().dot(mv * facet->vertex[0].cast())); - min_z = std::min(min_z, Vec3d::UnitZ().dot(mv * facet->vertex[1].cast())); - min_z = std::min(min_z, Vec3d::UnitZ().dot(mv * facet->vertex[2].cast())); - } + for (const stl_facet &facet : hull.stl.facet_start) + for (int i = 0; i < 3; ++ i) + min_z = std::min(min_z, (mv * facet.vertex[i].cast()).z()); } return min_z + inst->get_offset(Z); @@ -1452,7 +1439,7 @@ std::string ModelObject::get_export_filename() const stl_stats ModelObject::get_object_stl_stats() const { if (this->volumes.size() == 1) - return this->volumes[0]->mesh.stl.stats; + return this->volumes[0]->mesh().stl.stats; stl_stats full_stats; memset(&full_stats, 0, sizeof(stl_stats)); @@ -1463,7 +1450,7 @@ stl_stats ModelObject::get_object_stl_stats() const if (volume->id() == this->volumes[0]->id()) continue; - const stl_stats& stats = volume->mesh.stl.stats; + const stl_stats& stats = volume->mesh().stl.stats; // initialize full_stats (for repaired errors) full_stats.degenerate_facets += stats.degenerate_facets; @@ -1531,30 +1518,30 @@ bool ModelVolume::is_splittable() const { // the call mesh.is_splittable() is expensive, so cache the value to calculate it only once if (m_is_splittable == -1) - m_is_splittable = (int)mesh.is_splittable(); + m_is_splittable = (int)this->mesh().is_splittable(); return m_is_splittable == 1; } -void ModelVolume::center_geometry() +void ModelVolume::center_geometry_after_creation() { - Vec3d shift = mesh.bounding_box().center(); + Vec3d shift = this->mesh().bounding_box().center(); if (!shift.isApprox(Vec3d::Zero())) { - mesh.translate(-(float)shift(0), -(float)shift(1), -(float)shift(2)); - m_convex_hull.translate(-(float)shift(0), -(float)shift(1), -(float)shift(2)); + m_mesh->translate(-(float)shift(0), -(float)shift(1), -(float)shift(2)); + m_convex_hull->translate(-(float)shift(0), -(float)shift(1), -(float)shift(2)); translate(shift); } } void ModelVolume::calculate_convex_hull() { - m_convex_hull = mesh.convex_hull_3d(); + m_convex_hull = std::make_shared(this->mesh().convex_hull_3d()); } int ModelVolume::get_mesh_errors_count() const { - const stl_stats& stats = this->mesh.stl.stats; + const stl_stats& stats = this->mesh().stl.stats; return stats.degenerate_facets + stats.edges_fixed + stats.facets_removed + stats.facets_added + stats.facets_reversed + stats.backwards_edges; @@ -1562,7 +1549,7 @@ int ModelVolume::get_mesh_errors_count() const const TriangleMesh& ModelVolume::get_convex_hull() const { - return m_convex_hull; + return *m_convex_hull.get(); } ModelVolumeType ModelVolume::type_from_string(const std::string &s) @@ -1602,7 +1589,7 @@ std::string ModelVolume::type_to_string(const ModelVolumeType t) // This is useful to assign different materials to different volumes of an object. size_t ModelVolume::split(unsigned int max_extruders) { - TriangleMeshPtrs meshptrs = this->mesh.split(); + TriangleMeshPtrs meshptrs = this->mesh().split(); if (meshptrs.size() <= 1) { delete meshptrs.front(); return 1; @@ -1619,7 +1606,7 @@ size_t ModelVolume::split(unsigned int max_extruders) mesh->repair(); if (idx == 0) { - this->mesh = std::move(*mesh); + this->set_mesh(std::move(*mesh)); this->calculate_convex_hull(); // Assign a new unique ID, so that a new GLVolume will be generated. this->set_new_unique_id(); @@ -1628,7 +1615,7 @@ size_t ModelVolume::split(unsigned int max_extruders) this->object->volumes.insert(this->object->volumes.begin() + (++ivolume), new ModelVolume(object, *this, std::move(*mesh))); this->object->volumes[ivolume]->set_offset(Vec3d::Zero()); - this->object->volumes[ivolume]->center_geometry(); + this->object->volumes[ivolume]->center_geometry_after_creation(); this->object->volumes[ivolume]->translate(offset); this->object->volumes[ivolume]->name = name + "_" + std::to_string(idx + 1); this->object->volumes[ivolume]->config.set_deserialize("extruder", Model::get_auto_extruder_id_as_string(max_extruders)); @@ -1694,24 +1681,33 @@ void ModelVolume::mirror(Axis axis) set_mirror(mirror); } +// This method could only be called before the meshes of this ModelVolumes are not shared! void ModelVolume::scale_geometry(const Vec3d& versor) { - mesh.scale(versor); - m_convex_hull.scale(versor); + m_mesh->scale(versor); + m_convex_hull->scale(versor); } -void ModelVolume::transform_mesh(const Transform3d &mesh_trafo, bool fix_left_handed) +void ModelVolume::transform_this_mesh(const Transform3d &mesh_trafo, bool fix_left_handed) { - this->mesh.transform(mesh_trafo, fix_left_handed); - this->m_convex_hull.transform(mesh_trafo, fix_left_handed); + TriangleMesh mesh = this->mesh(); + mesh.transform(mesh_trafo, fix_left_handed); + this->set_mesh(std::move(mesh)); + TriangleMesh convex_hull = this->get_convex_hull(); + convex_hull.transform(mesh_trafo, fix_left_handed); + this->m_convex_hull = std::make_shared(std::move(convex_hull)); // Let the rest of the application know that the geometry changed, so the meshes have to be reloaded. this->set_new_unique_id(); } -void ModelVolume::transform_mesh(const Matrix3d &matrix, bool fix_left_handed) +void ModelVolume::transform_this_mesh(const Matrix3d &matrix, bool fix_left_handed) { - this->mesh.transform(matrix, fix_left_handed); - this->m_convex_hull.transform(matrix, fix_left_handed); + TriangleMesh mesh = this->mesh(); + mesh.transform(matrix, fix_left_handed); + this->set_mesh(std::move(mesh)); + TriangleMesh convex_hull = this->get_convex_hull(); + convex_hull.transform(matrix, fix_left_handed); + this->m_convex_hull = std::make_shared(std::move(convex_hull)); // Let the rest of the application know that the geometry changed, so the meshes have to be reloaded. this->set_new_unique_id(); } diff --git a/src/libslic3r/Model.hpp b/src/libslic3r/Model.hpp index 41bf5bd4be..0fd1140f0a 100644 --- a/src/libslic3r/Model.hpp +++ b/src/libslic3r/Model.hpp @@ -7,7 +7,9 @@ #include "Point.hpp" #include "TriangleMesh.hpp" #include "Slicing.hpp" + #include +#include #include #include #include @@ -261,6 +263,7 @@ public: void rotate(double angle, const Vec3d& axis); void mirror(Axis axis); + // This method could only be called before the meshes of this ModelVolumes are not shared! void scale_mesh(const Vec3d& versor); size_t materials_count() const; @@ -268,7 +271,6 @@ public: bool needed_repair() const; ModelObjectPtrs cut(size_t instance, coordf_t z, bool keep_upper = true, bool keep_lower = true, bool rotate_lower = false); // Note: z is in world coordinates void split(ModelObjectPtrs* new_objects); - void repair(); // Support for non-uniform scaling of instances. If an instance is rotated by angles, which are not multiples of ninety degrees, // then the scaling in world coordinate system is not representable by the Geometry::Transformation structure. // This situation is solved by baking in the instance transformation into the mesh vertices. @@ -340,7 +342,12 @@ class ModelVolume : public ModelBase public: std::string name; // The triangular model. - TriangleMesh mesh; + const TriangleMesh& mesh() const { return *m_mesh.get(); } + void set_mesh(const TriangleMesh &mesh) { m_mesh = std::make_shared(mesh); } + void set_mesh(TriangleMesh &&mesh) { m_mesh = std::make_shared(std::move(mesh)); } + void set_mesh(std::shared_ptr &mesh) { m_mesh = mesh; } + void set_mesh(std::unique_ptr &&mesh) { m_mesh = std::move(mesh); } + void reset_mesh() { m_mesh = std::make_shared(); } // Configuration parameters specific to an object model geometry or a modifier volume, // overriding the global Slic3r settings and the ModelObject settings. DynamicPrintConfig config; @@ -377,13 +384,16 @@ public: void rotate(double angle, const Vec3d& axis); void mirror(Axis axis); + // This method could only be called before the meshes of this ModelVolumes are not shared! void scale_geometry(const Vec3d& versor); - // translates the mesh and the convex hull so that the origin of their vertices is in the center of this volume's bounding box - void center_geometry(); + // Translates the mesh and the convex hull so that the origin of their vertices is in the center of this volume's bounding box. + // Attention! This method may only be called just after ModelVolume creation! It must not be called once the TriangleMesh of this ModelVolume is shared! + void center_geometry_after_creation(); void calculate_convex_hull(); const TriangleMesh& get_convex_hull() const; + std::shared_ptr get_convex_hull_shared_ptr() const { return m_convex_hull; } // Get count of errors in the mesh int get_mesh_errors_count() const; @@ -430,18 +440,20 @@ protected: explicit ModelVolume(const ModelVolume &rhs) = default; void set_model_object(ModelObject *model_object) { object = model_object; } - void transform_mesh(const Transform3d& t, bool fix_left_handed); - void transform_mesh(const Matrix3d& m, bool fix_left_handed); + void transform_this_mesh(const Transform3d& t, bool fix_left_handed); + void transform_this_mesh(const Matrix3d& m, bool fix_left_handed); private: // Parent object owning this ModelVolume. - ModelObject* object; + ModelObject* object; + // The triangular model. + std::shared_ptr m_mesh; // Is it an object to be printed, or a modifier volume? - ModelVolumeType m_type; - t_model_material_id m_material_id; + ModelVolumeType m_type; + t_model_material_id m_material_id; // The convex hull of this model's mesh. - TriangleMesh m_convex_hull; - Geometry::Transformation m_transformation; + std::shared_ptr m_convex_hull; + Geometry::Transformation m_transformation; // flag to optimize the checking if the volume is splittable // -1 -> is unknown value (before first cheking) @@ -449,24 +461,24 @@ private: // 1 -> is splittable mutable int m_is_splittable{ -1 }; - ModelVolume(ModelObject *object, const TriangleMesh &mesh) : mesh(mesh), m_type(ModelVolumeType::MODEL_PART), object(object) + ModelVolume(ModelObject *object, const TriangleMesh &mesh) : m_mesh(new TriangleMesh(mesh)), m_type(ModelVolumeType::MODEL_PART), object(object) { if (mesh.stl.stats.number_of_facets > 1) calculate_convex_hull(); } ModelVolume(ModelObject *object, TriangleMesh &&mesh, TriangleMesh &&convex_hull) : - mesh(std::move(mesh)), m_convex_hull(std::move(convex_hull)), m_type(ModelVolumeType::MODEL_PART), object(object) {} + m_mesh(new TriangleMesh(std::move(mesh))), m_convex_hull(new TriangleMesh(std::move(convex_hull))), m_type(ModelVolumeType::MODEL_PART), object(object) {} // Copying an existing volume, therefore this volume will get a copy of the ID assigned. ModelVolume(ModelObject *object, const ModelVolume &other) : ModelBase(other), // copy the ID - name(other.name), mesh(other.mesh), m_convex_hull(other.m_convex_hull), config(other.config), m_type(other.m_type), object(object), m_transformation(other.m_transformation) + name(other.name), m_mesh(other.m_mesh), m_convex_hull(other.m_convex_hull), config(other.config), m_type(other.m_type), object(object), m_transformation(other.m_transformation) { this->set_material_id(other.material_id()); } // Providing a new mesh, therefore this volume will get a new unique ID assigned. ModelVolume(ModelObject *object, const ModelVolume &other, const TriangleMesh &&mesh) : - name(other.name), mesh(std::move(mesh)), config(other.config), m_type(other.m_type), object(object), m_transformation(other.m_transformation) + name(other.name), m_mesh(new TriangleMesh(std::move(mesh))), config(other.config), m_type(other.m_type), object(object), m_transformation(other.m_transformation) { this->set_material_id(other.material_id()); if (mesh.stl.stats.number_of_facets > 1) @@ -597,10 +609,6 @@ public: static Model read_from_file(const std::string &input_file, DynamicPrintConfig *config = nullptr, bool add_default_instances = true); static Model read_from_archive(const std::string &input_file, DynamicPrintConfig *config, bool add_default_instances = true); - /// Repair the ModelObjects of the current Model. - /// This function calls repair function on each TriangleMesh of each model object volume - void repair(); - // Add a new ModelObject to this Model, generate a new ID for this ModelObject. ModelObject* add_object(); ModelObject* add_object(const char *name, const char *path, const TriangleMesh &mesh); diff --git a/src/libslic3r/ModelArrange.cpp b/src/libslic3r/ModelArrange.cpp index b088a1f176..a440c3999d 100644 --- a/src/libslic3r/ModelArrange.cpp +++ b/src/libslic3r/ModelArrange.cpp @@ -9,6 +9,31 @@ #include #include +#include +#include + +namespace libnest2d { +#if !defined(_MSC_VER) && defined(__SIZEOF_INT128__) && !defined(__APPLE__) +using LargeInt = __int128; +#else +using LargeInt = boost::multiprecision::int128_t; +template<> struct _NumTag { using Type = ScalarTag; }; +#endif +template struct _NumTag> { using Type = RationalTag; }; + +namespace nfp { + +template +struct NfpImpl +{ + NfpResult operator()(const S &sh, const S &other) + { + return nfpConvexOnly>(sh, other); + } +}; + +} +} namespace Slic3r { @@ -130,7 +155,7 @@ Box boundingBox(const Box& pilebb, const Box& ibb ) { // at the same time, it has to provide reasonable results. std::tuple objfunc(const PointImpl& bincenter, - const shapelike::Shapes& merged_pile, + const TMultiShape& merged_pile, const Box& pilebb, const ItemGroup& items, const Item &item, @@ -293,7 +318,7 @@ class AutoArranger {}; // management and spatial index structures for acceleration. template class _ArrBase { -protected: +public: // Useful type shortcuts... using Placer = TPacker; @@ -301,7 +326,9 @@ protected: using Packer = Nester; using PConfig = typename Packer::PlacementConfig; using Distance = TCoord; - using Pile = sl::Shapes; + using Pile = TMultiShape; + +protected: Packer m_pck; PConfig m_pconf; // Placement configuration @@ -539,7 +566,10 @@ public: // 2D shape from top view. using ShapeData2D = std::vector>; -ShapeData2D projectModelFromTop(const Slic3r::Model &model, const WipeTowerInfo& wti) { +ShapeData2D projectModelFromTop(const Slic3r::Model &model, + const WipeTowerInfo &wti, + double tolerance) +{ ShapeData2D ret; // Count all the items on the bin (all the object's instances) @@ -561,21 +591,32 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model, const WipeTowerInfo& // Object instances should carry the same scaling and // x, y rotation that is why we use the first instance. { - ModelInstance *finst = objptr->instances.front(); - Vec3d rotation = finst->get_rotation(); - rotation.z() = 0.; - Transform3d trafo_instance = Geometry::assemble_transform(Vec3d::Zero(), rotation, finst->get_scaling_factor(), finst->get_mirror()); + ModelInstance *finst = objptr->instances.front(); + Vec3d rotation = finst->get_rotation(); + rotation.z() = 0.; + Transform3d trafo_instance = Geometry::assemble_transform( + Vec3d::Zero(), + rotation, + finst->get_scaling_factor(), + finst->get_mirror()); Polygon p = objptr->convex_hull_2d(trafo_instance); - assert(! p.points.empty()); - - // this may happen for malformed models, see: https://github.com/prusa3d/PrusaSlicer/issues/2209 - if (p.points.empty()) - continue; + + assert(!p.points.empty()); + // this may happen for malformed models, see: + // https://github.com/prusa3d/PrusaSlicer/issues/2209 + if (p.points.empty()) continue; + + if(tolerance > EPSILON) { + Polygons pp { p }; + pp = p.simplify(double(scaled(tolerance))); + if (!pp.empty()) p = pp.front(); + } + p.reverse(); assert(!p.is_counter_clockwise()); - p.append(p.first_point()); clpath = Slic3rMultiPoint_to_ClipperPath(p); + auto firstp = clpath.front(); clpath.emplace_back(firstp); } Vec3d rotation0 = objptr->instances.front()->get_rotation(); @@ -589,7 +630,7 @@ ShapeData2D projectModelFromTop(const Slic3r::Model &model, const WipeTowerInfo& // Invalid geometries would throw exceptions when arranging if(item.vertexCount() > 3) { - item.rotation(float(Geometry::rotation_diff_z(rotation0, objinst->get_rotation()))), + item.rotation(Geometry::rotation_diff_z(rotation0, objinst->get_rotation())); item.translation({ ClipperLib::cInt(objinst->get_offset(X)/SCALING_FACTOR), ClipperLib::cInt(objinst->get_offset(Y)/SCALING_FACTOR) @@ -741,6 +782,8 @@ BedShapeHint bedShape(const Polyline &bed) { return ret; } +static const SLIC3R_CONSTEXPR double SIMPLIFY_TOLERANCE_MM = 0.1; + // The final client function to arrange the Model. A progress indicator and // a stop predicate can be also be passed to control the process. bool arrange(Model &model, // The model with the geometries @@ -755,9 +798,9 @@ bool arrange(Model &model, // The model with the geometries std::function stopcondition) { bool ret = true; - + // Get the 2D projected shapes with their 3D model instance pointers - auto shapemap = arr::projectModelFromTop(model, wti); + auto shapemap = arr::projectModelFromTop(model, wti, SIMPLIFY_TOLERANCE_MM); // Copy the references for the shapes only as the arranger expects a // sequence of objects convertible to Item or ClipperPolygon @@ -782,7 +825,7 @@ bool arrange(Model &model, // The model with the geometries static_cast(bbb.min(0)), static_cast(bbb.min(1)) }, - { + { static_cast(bbb.max(0)), static_cast(bbb.max(1)) }); @@ -856,9 +899,9 @@ void find_new_position(const Model &model, coord_t min_obj_distance, const Polyline &bed, WipeTowerInfo& wti) -{ +{ // Get the 2D projected shapes with their 3D model instance pointers - auto shapemap = arr::projectModelFromTop(model, wti); + auto shapemap = arr::projectModelFromTop(model, wti, SIMPLIFY_TOLERANCE_MM); // Copy the references for the shapes only as the arranger expects a // sequence of objects convertible to Item or ClipperPolygon diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 87ea263012..89e21934a1 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -2258,6 +2258,20 @@ void PrintConfigDef::init_sla_params() def->min = 100; def->set_default_value(new ConfigOptionInt(1440)); + def = this->add("display_mirror_x", coBool); + def->full_label = L("Display horizontal mirroring"); + def->label = L("Mirror horizontally"); + def->tooltip = L("Enable horizontal mirroring of output images"); + def->mode = comExpert; + def->set_default_value(new ConfigOptionBool(true)); + + def = this->add("display_mirror_y", coBool); + def->full_label = L("Display vertical mirroring"); + def->label = L("Mirror vertically"); + def->tooltip = L("Enable vertical mirroring of output images"); + def->mode = comExpert; + def->set_default_value(new ConfigOptionBool(false)); + def = this->add("display_orientation", coEnum); def->label = L("Display orientation"); def->tooltip = L("Set the actual LCD display orientation inside the SLA printer." diff --git a/src/libslic3r/PrintConfig.hpp b/src/libslic3r/PrintConfig.hpp index 1da22b377f..248b89e321 100644 --- a/src/libslic3r/PrintConfig.hpp +++ b/src/libslic3r/PrintConfig.hpp @@ -1083,6 +1083,8 @@ public: ConfigOptionInt display_pixels_x; ConfigOptionInt display_pixels_y; ConfigOptionEnum display_orientation; + ConfigOptionBool display_mirror_x; + ConfigOptionBool display_mirror_y; ConfigOptionFloats relative_correction; ConfigOptionFloat absolute_correction; ConfigOptionFloat gamma_correction; @@ -1099,6 +1101,8 @@ protected: OPT_PTR(display_height); OPT_PTR(display_pixels_x); OPT_PTR(display_pixels_y); + OPT_PTR(display_mirror_x); + OPT_PTR(display_mirror_y); OPT_PTR(display_orientation); OPT_PTR(relative_correction); OPT_PTR(absolute_correction); diff --git a/src/libslic3r/PrintExport.hpp b/src/libslic3r/PrintExport.hpp deleted file mode 100644 index f6537ed324..0000000000 --- a/src/libslic3r/PrintExport.hpp +++ /dev/null @@ -1,327 +0,0 @@ -#ifndef PRINTEXPORT_HPP -#define PRINTEXPORT_HPP - -// For png export of the sliced model -#include -#include -#include - -#include -#include - -#include "Rasterizer/Rasterizer.hpp" -//#include -//#include //#include "tbb/mutex.h" - -namespace Slic3r { - -// Used for addressing parameters of FilePrinter::set_statistics() -enum ePrintStatistics -{ - psUsedMaterial = 0, - psNumFade, - psNumSlow, - psNumFast, - - psCnt -}; - -enum class FilePrinterFormat { - SLA_PNGZIP, - SVG -}; - -/* - * Interface for a file printer of the slices. Implementation can be an SVG - * or PNG printer or any other format. - * - * The format argument specifies the output format of the printer and it enables - * different implementations of this class template for each supported format. - * - */ -template -class FilePrinter { -public: - - // Draw a polygon which is a polygon inside a slice on the specified layer. - void draw_polygon(const ExPolygon& p, unsigned lyr); - void draw_polygon(const ClipperLib::Polygon& p, unsigned lyr); - - // Tell the printer how many layers should it consider. - void layers(unsigned layernum); - - // Get the number of layers in the print. - unsigned layers() const; - - /* Switch to a particular layer. If there where less layers then the - * specified layer number than an appropriate number of layers will be - * allocated in the printer. - */ - void begin_layer(unsigned layer); - - // Allocate a new layer on top of the last and switch to it. - void begin_layer(); - - /* - * Finish the selected layer. It means that no drawing is allowed on that - * layer anymore. This fact can be used to prepare the file system output - * data like png comprimation and so on. - */ - void finish_layer(unsigned layer); - - // Finish the top layer. - void finish_layer(); - - // Save all the layers into the file (or dir) specified in the path argument - // An optional project name can be added to be used for the layer file names - void save(const std::string& path, const std::string& projectname = ""); - - // Save only the selected layer to the file specified in path argument. - void save_layer(unsigned lyr, const std::string& path); -}; - -// Provokes static_assert in the right way. -template struct VeryFalse { static const bool value = false; }; - -// This can be explicitly implemented in the gui layer or the default Zipper -// API in libslic3r with minz. -template class LayerWriter { -public: - - LayerWriter(const std::string& /*zipfile_path*/) - { - static_assert(VeryFalse::value, - "No layer writer implementation provided!"); - } - - // Should create a new file within the zip with the given filename. It - // should also finish any previous entry. - void next_entry(const std::string& /*fname*/) {} - - // Should create a new file within the archive and write the provided data. - void binary_entry(const std::string& /*fname*/, - const std::uint8_t* buf, size_t len); - - // Test whether the object can still be used for writing. - bool is_ok() { return false; } - - // Write some data (text) into the current file (entry) within the archive. - template LayerWriter& operator<<(T&& /*arg*/) { - return *this; - } - - // Flush the current entry into the archive. - void finalize() {} -}; - -// Implementation for PNG raster output -// Be aware that if a large number of layers are allocated, it can very well -// exhaust the available memory especially on 32 bit platform. -template<> class FilePrinter -{ - struct Layer { - Raster raster; - RawBytes rawbytes; - - Layer() {} - - Layer(const Layer&) = delete; - Layer(Layer&& m): - raster(std::move(m.raster)) {} - }; - - // We will save the compressed PNG data into stringstreams which can be done - // in parallel. Later we can write every layer to the disk sequentially. - std::vector m_layers_rst; - Raster::Resolution m_res; - Raster::PixelDim m_pxdim; - double m_exp_time_s = .0, m_exp_time_first_s = .0; - double m_layer_height = .0; - Raster::Origin m_o = Raster::Origin::TOP_LEFT; - double m_gamma; - - double m_used_material = 0.0; - int m_cnt_fade_layers = 0; - int m_cnt_slow_layers = 0; - int m_cnt_fast_layers = 0; - - std::string createIniContent(const std::string& projectname) { - using std::string; - using std::to_string; - - auto expt_str = to_string(m_exp_time_s); - auto expt_first_str = to_string(m_exp_time_first_s); - auto layerh_str = to_string(m_layer_height); - - const std::string cnt_fade_layers = to_string(m_cnt_fade_layers); - const std::string cnt_slow_layers = to_string(m_cnt_slow_layers); - const std::string cnt_fast_layers = to_string(m_cnt_fast_layers); - const std::string used_material = to_string(m_used_material); - - return string( - "action = print\n" - "jobDir = ") + projectname + "\n" + - "expTime = " + expt_str + "\n" - "expTimeFirst = " + expt_first_str + "\n" - "numFade = " + cnt_fade_layers + "\n" - "layerHeight = " + layerh_str + "\n" - "usedMaterial = " + used_material + "\n" - "numSlow = " + cnt_slow_layers + "\n" - "numFast = " + cnt_fast_layers + "\n"; - } - -public: - - enum RasterOrientation { - RO_LANDSCAPE, - RO_PORTRAIT - }; - - // We will play with the raster's coordinate origin parameter. When the - // printer should print in landscape mode it should have the Y axis flipped - // because the layers should be displayed upside down. PNG has its - // coordinate origin in the top-left corner so normally the Raster objects - // should be instantiated with the TOP_LEFT flag. However, in landscape mode - // we do want the pictures to be upside down so we will make BOTTOM_LEFT - // type rasters and the PNG format will do the flipping automatically. - - // In case of portrait images, we have to rotate the image by a 90 degrees - // and flip the y axis. To get the correct upside-down orientation of the - // slice images, we can flip the x and y coordinates of the input polygons - // and do the Y flipping of the image. This will generate the correct - // orientation in portrait mode. - - inline FilePrinter(double width_mm, double height_mm, - unsigned width_px, unsigned height_px, - double layer_height, - double exp_time, double exp_time_first, - RasterOrientation ro = RO_PORTRAIT, - double gamma = 1.0): - m_res(width_px, height_px), - m_pxdim(width_mm/width_px, height_mm/height_px), - m_exp_time_s(exp_time), - m_exp_time_first_s(exp_time_first), - m_layer_height(layer_height), - - // Here is the trick with the orientation. - m_o(ro == RO_LANDSCAPE? Raster::Origin::BOTTOM_LEFT : - Raster::Origin::TOP_LEFT ), - m_gamma(gamma) - { - } - - FilePrinter(const FilePrinter& ) = delete; - FilePrinter(FilePrinter&& m): - m_layers_rst(std::move(m.m_layers_rst)), - m_res(m.m_res), - m_pxdim(m.m_pxdim) {} - - inline void layers(unsigned cnt) { if(cnt > 0) m_layers_rst.resize(cnt); } - inline unsigned layers() const { return unsigned(m_layers_rst.size()); } - - inline void draw_polygon(const ExPolygon& p, unsigned lyr) { - assert(lyr < m_layers_rst.size()); - m_layers_rst[lyr].raster.draw(p); - } - - inline void draw_polygon(const ClipperLib::Polygon& p, unsigned lyr) { - assert(lyr < m_layers_rst.size()); - m_layers_rst[lyr].raster.draw(p); - } - - inline void begin_layer(unsigned lyr) { - if(m_layers_rst.size() <= lyr) m_layers_rst.resize(lyr+1); - m_layers_rst[lyr].raster.reset(m_res, m_pxdim, m_o, m_gamma); - } - - inline void begin_layer() { - m_layers_rst.emplace_back(); - m_layers_rst.front().raster.reset(m_res, m_pxdim, m_o, m_gamma); - } - - inline void finish_layer(unsigned lyr_id) { - assert(lyr_id < m_layers_rst.size()); - m_layers_rst[lyr_id].rawbytes = - m_layers_rst[lyr_id].raster.save(Raster::Compression::PNG); - m_layers_rst[lyr_id].raster.reset(); - } - - inline void finish_layer() { - if(!m_layers_rst.empty()) { - m_layers_rst.back().rawbytes = - m_layers_rst.back().raster.save(Raster::Compression::PNG); - m_layers_rst.back().raster.reset(); - } - } - - template - inline void save(const std::string& fpath, const std::string& prjname = "") - { - try { - LayerWriter writer(fpath); - if(!writer.is_ok()) return; - - std::string project = prjname.empty()? - boost::filesystem::path(fpath).stem().string() : prjname; - - writer.next_entry("config.ini"); - if(!writer.is_ok()) return; - - writer << createIniContent(project); - - for(unsigned i = 0; i < m_layers_rst.size() && writer.is_ok(); i++) - { - if(m_layers_rst[i].rawbytes.size() > 0) { - char lyrnum[6]; - std::sprintf(lyrnum, "%.5d", i); - auto zfilename = project + lyrnum + ".png"; - if(!writer.is_ok()) break; - - writer.binary_entry(zfilename, - m_layers_rst[i].rawbytes.data(), - m_layers_rst[i].rawbytes.size()); - } - } - - writer.finalize(); - } catch(std::exception& e) { - BOOST_LOG_TRIVIAL(error) << e.what(); - // Rethrow the exception - throw; - } - } - - void save_layer(unsigned lyr, const std::string& path) { - unsigned i = lyr; - assert(i < m_layers_rst.size()); - - char lyrnum[6]; - std::sprintf(lyrnum, "%.5d", lyr); - std::string loc = path + "layer" + lyrnum + ".png"; - - std::fstream out(loc, std::fstream::out | std::fstream::binary); - if(out.good()) { - m_layers_rst[i].raster.save(out, Raster::Compression::PNG); - } else { - BOOST_LOG_TRIVIAL(error) << "Can't create file for layer"; - } - - out.close(); - m_layers_rst[i].raster.reset(); - } - - void set_statistics(const std::vector statistics) - { - if (statistics.size() != psCnt) - return; - - m_used_material = statistics[psUsedMaterial]; - m_cnt_fade_layers = int(statistics[psNumFade]); - m_cnt_slow_layers = int(statistics[psNumSlow]); - m_cnt_fast_layers = int(statistics[psNumFast]); - } -}; - -} - -#endif // PRINTEXPORT_HPP diff --git a/src/libslic3r/PrintObject.cpp b/src/libslic3r/PrintObject.cpp index 660a2d9398..d99aceabfe 100644 --- a/src/libslic3r/PrintObject.cpp +++ b/src/libslic3r/PrintObject.cpp @@ -1797,7 +1797,7 @@ std::vector PrintObject::_slice_volumes(const std::vector &z, if (! volumes.empty()) { // Compose mesh. //FIXME better to perform slicing over each volume separately and then to use a Boolean operation to merge them. - TriangleMesh mesh(volumes.front()->mesh); + TriangleMesh mesh(volumes.front()->mesh()); mesh.transform(volumes.front()->get_matrix(), true); assert(mesh.repaired); if (volumes.size() == 1 && mesh.repaired) { @@ -1806,7 +1806,7 @@ std::vector PrintObject::_slice_volumes(const std::vector &z, } for (size_t idx_volume = 1; idx_volume < volumes.size(); ++ idx_volume) { const ModelVolume &model_volume = *volumes[idx_volume]; - TriangleMesh vol_mesh(model_volume.mesh); + TriangleMesh vol_mesh(model_volume.mesh()); vol_mesh.transform(model_volume.get_matrix(), true); mesh.merge(vol_mesh); } @@ -1815,10 +1815,11 @@ std::vector PrintObject::_slice_volumes(const std::vector &z, // apply XY shift mesh.translate(- unscale(m_copies_shift(0)), - unscale(m_copies_shift(1)), 0); // perform actual slicing - TriangleMeshSlicer mslicer; const Print *print = this->print(); auto callback = TriangleMeshSlicer::throw_on_cancel_callback_type([print](){print->throw_if_canceled();}); - mesh.require_shared_vertices(); // TriangleMeshSlicer needs this + // TriangleMeshSlicer needs shared vertices, also this calls the repair() function. + mesh.require_shared_vertices(); + TriangleMeshSlicer mslicer; mslicer.init(&mesh, callback); mslicer.slice(z, float(m_config.slice_closing_radius.value), &layers, callback); m_print->throw_if_canceled(); @@ -1832,7 +1833,7 @@ std::vector PrintObject::_slice_volume(const std::vector &z, std::vector layers; // Compose mesh. //FIXME better to perform slicing over each volume separately and then to use a Boolean operation to merge them. - TriangleMesh mesh(volume.mesh); + TriangleMesh mesh(volume.mesh()); mesh.transform(volume.get_matrix(), true); if (mesh.repaired) { //FIXME The admesh repair function may break the face connectivity, rather refresh it here as the slicing code relies on it. @@ -1846,7 +1847,8 @@ std::vector PrintObject::_slice_volume(const std::vector &z, TriangleMeshSlicer mslicer; const Print *print = this->print(); auto callback = TriangleMeshSlicer::throw_on_cancel_callback_type([print](){print->throw_if_canceled();}); - mesh.require_shared_vertices(); // TriangleMeshSlicer needs this + // TriangleMeshSlicer needs the shared vertices. + mesh.require_shared_vertices(); mslicer.init(&mesh, callback); mslicer.slice(z, float(m_config.slice_closing_radius.value), &layers, callback); m_print->throw_if_canceled(); diff --git a/src/libslic3r/SLA/SLABasePool.cpp b/src/libslic3r/SLA/SLABasePool.cpp index 171d2b8d03..3b199c4ebb 100644 --- a/src/libslic3r/SLA/SLABasePool.cpp +++ b/src/libslic3r/SLA/SLABasePool.cpp @@ -53,7 +53,7 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, // Shorthand for the vertex arrays auto& upoints = upper.points, &lpoints = lower.points; - auto& rpts = ret.points; auto& rfaces = ret.indices; + auto& rpts = ret.points; auto& ind = ret.indices; // If the Z levels are flipped, or the offset difference is negative, we // will interpret that as the triangles normals should be inverted. @@ -61,10 +61,11 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, // Copy the points into the mesh, convert them from 2D to 3D rpts.reserve(upoints.size() + lpoints.size()); - rfaces.reserve(2*upoints.size() + 2*lpoints.size()); - const double sf = SCALING_FACTOR; - for(auto& p : upoints) rpts.emplace_back(p.x()*sf, p.y()*sf, upper_z_mm); - for(auto& p : lpoints) rpts.emplace_back(p.x()*sf, p.y()*sf, lower_z_mm); + ind.reserve(2 * upoints.size() + 2 * lpoints.size()); + for (auto &p : upoints) + rpts.emplace_back(unscaled(p.x()), unscaled(p.y()), upper_z_mm); + for (auto &p : lpoints) + rpts.emplace_back(unscaled(p.x()), unscaled(p.y()), lower_z_mm); // Create pointing indices into vertex arrays. u-upper, l-lower size_t uidx = 0, lidx = offs, unextidx = 1, lnextidx = offs + 1; @@ -121,9 +122,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, case Proceed::UPPER: if(!ustarted || uidx != uendidx) { // there are vertices remaining // Get the 3D vertices in order - const Vec3d& p_up1 = rpts[size_t(uidx)]; - const Vec3d& p_low = rpts[size_t(lidx)]; - const Vec3d& p_up2 = rpts[size_t(unextidx)]; + const Vec3d& p_up1 = rpts[uidx]; + const Vec3d& p_low = rpts[lidx]; + const Vec3d& p_up2 = rpts[unextidx]; // Calculate fitness: the average of the two connecting edges double a = offsdiff2 - (distfn(p_up1, p_low) - zdiff2); @@ -133,8 +134,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, if(current_fit > prev_fit) { // fit is worse than previously proceed = Proceed::LOWER; } else { // good to go, create the triangle - inverted? rfaces.emplace_back(unextidx, lidx, uidx) : - rfaces.emplace_back(uidx, lidx, unextidx) ; + inverted + ? ind.emplace_back(int(unextidx), int(lidx), int(uidx)) + : ind.emplace_back(int(uidx), int(lidx), int(unextidx)); // Increment the iterators, rotate if necessary ++uidx; ++unextidx; @@ -150,9 +152,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, case Proceed::LOWER: // Mode with lower segment, upper vertex. Same structure: if(!lstarted || lidx != lendidx) { - const Vec3d& p_low1 = rpts[size_t(lidx)]; - const Vec3d& p_low2 = rpts[size_t(lnextidx)]; - const Vec3d& p_up = rpts[size_t(uidx)]; + const Vec3d& p_low1 = rpts[lidx]; + const Vec3d& p_low2 = rpts[lnextidx]; + const Vec3d& p_up = rpts[uidx]; double a = offsdiff2 - (distfn(p_up, p_low1) - zdiff2); double b = offsdiff2 - (distfn(p_up, p_low2) - zdiff2); @@ -161,8 +163,9 @@ Contour3D walls(const Polygon& lower, const Polygon& upper, if(current_fit > prev_fit) { proceed = Proceed::UPPER; } else { - inverted? rfaces.emplace_back(uidx, lnextidx, lidx) : - rfaces.emplace_back(lidx, lnextidx, uidx); + inverted + ? ind.emplace_back(int(uidx), int(lnextidx), int(lidx)) + : ind.emplace_back(int(lidx), int(lnextidx), int(uidx)); ++lidx; ++lnextidx; if(lnextidx == rpts.size()) lnextidx = offs; @@ -200,7 +203,7 @@ void offset(ExPolygon& sh, coord_t distance) { } ClipperOffset offs; - offs.ArcTolerance = 0.01*mm(1); + offs.ArcTolerance = 0.01*scaled(1.0); Paths result; offs.AddPath(ctour, jtRound, etClosedPolygon); offs.AddPaths(holes, jtRound, etClosedPolygon); @@ -303,16 +306,6 @@ ExPolygons unify(const ExPolygons& shapes) { return retv; } -/// Only a debug function to generate top and bottom plates from a 2D shape. -/// It is not used in the algorithm directly. -inline Contour3D roofs(const ExPolygon& poly, coord_t z_distance) { - auto lower = triangulate_expolygon_3d(poly); - auto upper = triangulate_expolygon_3d(poly, z_distance*SCALING_FACTOR, true); - Contour3D ret; - ret.merge(lower); ret.merge(upper); - return ret; -} - /// This method will create a rounded edge around a flat polygon in 3d space. /// 'base_plate' parameter is the target plate. /// 'radius' is the radius of the edges. @@ -358,7 +351,7 @@ Contour3D round_edges(const ExPolygon& base_plate, double x2 = xx*xx; double stepy = std::sqrt(r2 - x2); - offset(ob, s*mm(xx)); + offset(ob, s*scaled(xx)); wh = ceilheight_mm - radius_mm + stepy; Contour3D pwalls; @@ -382,7 +375,7 @@ Contour3D round_edges(const ExPolygon& base_plate, double xx = radius_mm - i*stepx; double x2 = xx*xx; double stepy = std::sqrt(r2 - x2); - offset(ob, s*mm(xx)); + offset(ob, s*scaled(xx)); wh = ceilheight_mm - radius_mm - stepy; Contour3D pwalls; @@ -402,41 +395,6 @@ Contour3D round_edges(const ExPolygon& base_plate, return curvedwalls; } -/// Generating the concave part of the 3D pool with the bottom plate and the -/// side walls. -Contour3D inner_bed(const ExPolygon& poly, - double depth_mm, - double begin_h_mm = 0) -{ - Contour3D bottom; - Pointf3s triangles = triangulate_expolygon_3d(poly, -depth_mm + begin_h_mm); - bottom.merge(triangles); - - coord_t depth = mm(depth_mm); - coord_t begin_h = mm(begin_h_mm); - - auto lines = poly.lines(); - - // Generate outer walls - auto fp = [](const Point& p, Point::coord_type z) { - return unscale(x(p), y(p), z); - }; - - for(auto& l : lines) { - auto s = coord_t(bottom.points.size()); - - bottom.points.emplace_back(fp(l.a, -depth + begin_h)); - bottom.points.emplace_back(fp(l.b, -depth + begin_h)); - bottom.points.emplace_back(fp(l.a, begin_h)); - bottom.points.emplace_back(fp(l.b, begin_h)); - - bottom.indices.emplace_back(s + 3, s + 1, s); - bottom.indices.emplace_back(s + 2, s + 3, s); - } - - return bottom; -} - inline Point centroid(Points& pp) { Point c; switch(pp.size()) { @@ -518,7 +476,7 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50, double dx = x(c) - x(cc), dy = y(c) - y(cc); double l = std::sqrt(dx * dx + dy * dy); double nx = dx / l, ny = dy / l; - double max_dist = mm(max_dist_mm); + double max_dist = scaled(max_dist_mm); ExPolygon& expo = punion[idx++]; BoundingBox querybb(expo); @@ -534,10 +492,10 @@ ExPolygons concave_hull(const ExPolygons& polys, double max_dist_mm = 50, ctour.reserve(3); ctour.emplace_back(cc); - Point d(coord_t(mm(1)*nx), coord_t(mm(1)*ny)); + Point d(coord_t(scaled(1.)*nx), coord_t(scaled(1.)*ny)); ctour.emplace_back(c + Point( -y(d), x(d) )); ctour.emplace_back(c + Point( y(d), -x(d) )); - offset(r, mm(1)); + offset(r, scaled(1.)); return r; }); @@ -569,15 +527,16 @@ void base_plate(const TriangleMesh &mesh, ExPolygons &output, float h, // Now we have to unify all slice layers which can be an expensive operation // so we will try to simplify the polygons ExPolygons tmp; tmp.reserve(count); - for(ExPolygons& o : out) for(ExPolygon& e : o) { - auto&& exss = e.simplify(0.1/SCALING_FACTOR); - for(ExPolygon& ep : exss) tmp.emplace_back(std::move(ep)); - } + for(ExPolygons& o : out) + for(ExPolygon& e : o) { + auto&& exss = e.simplify(scaled(0.1)); + for(ExPolygon& ep : exss) tmp.emplace_back(std::move(ep)); + } ExPolygons utmp = unify(tmp); for(auto& o : utmp) { - auto&& smp = o.simplify(0.1/SCALING_FACTOR); + auto&& smp = o.simplify(scaled(0.1)); output.insert(output.end(), smp.begin(), smp.end()); } } @@ -607,11 +566,11 @@ Contour3D create_base_pool(const ExPolygons &ground_layer, const double bottom_offs = (thickness + wingheight) / std::tan(slope); // scaled values - const coord_t s_thickness = mm(thickness); - const coord_t s_eradius = mm(cfg.edge_radius_mm); + const coord_t s_thickness = scaled(thickness); + const coord_t s_eradius = scaled(cfg.edge_radius_mm); const coord_t s_safety_dist = 2*s_eradius + coord_t(0.8*s_thickness); - const coord_t s_wingdist = mm(wingdist); - const coord_t s_bottom_offs = mm(bottom_offs); + const coord_t s_wingdist = scaled(wingdist); + const coord_t s_bottom_offs = scaled(bottom_offs); auto& thrcl = cfg.throw_on_cancel; diff --git a/src/libslic3r/SLA/SLABoilerPlate.hpp b/src/libslic3r/SLA/SLABoilerPlate.hpp index 602121af9a..86e90f3b7f 100644 --- a/src/libslic3r/SLA/SLABoilerPlate.hpp +++ b/src/libslic3r/SLA/SLABoilerPlate.hpp @@ -11,11 +11,6 @@ namespace Slic3r { namespace sla { -using coord_t = Point::coord_type; - -/// get the scaled clipper units for a millimeter value -inline coord_t mm(double v) { return coord_t(v/SCALING_FACTOR); } - /// Get x and y coordinates (because we are eigenizing...) inline coord_t x(const Point& p) { return p(0); } inline coord_t y(const Point& p) { return p(1); } @@ -36,12 +31,10 @@ inline coord_t x(const Vec3crd& p) { return p(0); } inline coord_t y(const Vec3crd& p) { return p(1); } inline coord_t z(const Vec3crd& p) { return p(2); } -using Indices = std::vector; - /// Intermediate struct for a 3D mesh struct Contour3D { Pointf3s points; - Indices indices; + std::vector indices; void merge(const Contour3D& ctr) { auto s3 = coord_t(points.size()); diff --git a/src/libslic3r/Rasterizer/Rasterizer.cpp b/src/libslic3r/SLA/SLARaster.cpp similarity index 72% rename from src/libslic3r/Rasterizer/Rasterizer.cpp rename to src/libslic3r/SLA/SLARaster.cpp index e1213555c4..32a88b1b50 100644 --- a/src/libslic3r/Rasterizer/Rasterizer.cpp +++ b/src/libslic3r/SLA/SLARaster.cpp @@ -1,5 +1,10 @@ -#include "Rasterizer.hpp" -#include +#ifndef SLARASTER_CPP +#define SLARASTER_CPP + +#include + +#include "SLARaster.hpp" +#include "libslic3r/ExPolygon.hpp" #include // For rasterizing @@ -19,11 +24,13 @@ namespace Slic3r { -const Polygon& contour(const ExPolygon& p) { return p.contour; } -const ClipperLib::Path& contour(const ClipperLib::Polygon& p) { return p.Contour; } +inline const Polygon& contour(const ExPolygon& p) { return p.contour; } +inline const ClipperLib::Path& contour(const ClipperLib::Polygon& p) { return p.Contour; } -const Polygons& holes(const ExPolygon& p) { return p.holes; } -const ClipperLib::Paths& holes(const ClipperLib::Polygon& p) { return p.Holes; } +inline const Polygons& holes(const ExPolygon& p) { return p.holes; } +inline const ClipperLib::Paths& holes(const ClipperLib::Polygon& p) { return p.Holes; } + +namespace sla { class Raster::Impl { public: @@ -39,7 +46,7 @@ public: static const TPixel ColorWhite; static const TPixel ColorBlack; - using Origin = Raster::Origin; + using Format = Raster::Format; private: Raster::Resolution m_resolution; @@ -52,16 +59,21 @@ private: TRendererAA m_renderer; std::function m_gammafn; - Origin m_o; + std::array m_mirror; + Format m_fmt = Format::PNG; inline void flipy(agg::path_storage& path) const { path.flip_y(0, m_resolution.height_px); } + + inline void flipx(agg::path_storage& path) const { + path.flip_x(0, m_resolution.width_px); + } public: inline Impl(const Raster::Resolution& res, const Raster::PixelDim &pd, - Origin o, double gamma = 1.0): + const std::array& mirror, double gamma = 1.0): m_resolution(res), // m_pxdim(pd), m_pxdim_scaled(SCALING_FACTOR / pd.w_mm, SCALING_FACTOR / pd.h_mm), @@ -72,7 +84,7 @@ public: m_pixfmt(m_rbuf), m_raw_renderer(m_pixfmt), m_renderer(m_raw_renderer), - m_o(o) + m_mirror(mirror) { m_renderer.color(ColorWhite); @@ -81,6 +93,19 @@ public: clear(); } + + inline Impl(const Raster::Resolution& res, + const Raster::PixelDim &pd, + Format fmt, + double gamma = 1.0): + Impl(res, pd, {false, false}, gamma) + { + switch (fmt) { + case Format::PNG: m_mirror = {false, true}; break; + case Format::RAW: m_mirror = {false, false}; break; + } + m_fmt = fmt; + } template void draw(const P &poly) { agg::rasterizer_scanline_aa<> ras; @@ -89,14 +114,16 @@ public: ras.gamma(m_gammafn); auto&& path = to_path(contour(poly)); - - if(m_o == Origin::TOP_LEFT) flipy(path); + + if(m_mirror[X]) flipx(path); + if(m_mirror[Y]) flipy(path); ras.add_path(path); for(auto& h : holes(poly)) { auto&& holepath = to_path(h); - if(m_o == Origin::TOP_LEFT) flipy(holepath); + if(m_mirror[X]) flipx(holepath); + if(m_mirror[Y]) flipy(holepath); ras.add_path(holepath); } @@ -108,11 +135,11 @@ public: } inline TBuffer& buffer() { return m_buf; } + + inline Format format() const { return m_fmt; } inline const Raster::Resolution resolution() { return m_resolution; } - - inline Origin origin() const /*noexcept*/ { return m_o; } - + private: inline double getPx(const Point& p) { return p(0) * m_pxdim_scaled.w_mm; @@ -154,30 +181,30 @@ private: const Raster::Impl::TPixel Raster::Impl::ColorWhite = Raster::Impl::TPixel(255); const Raster::Impl::TPixel Raster::Impl::ColorBlack = Raster::Impl::TPixel(0); -Raster::Raster(const Resolution &r, const PixelDim &pd, Origin o, double g): - m_impl(new Impl(r, pd, o, g)) {} +template<> Raster::Raster() { reset(); }; +Raster::~Raster() = default; -Raster::Raster() {} +// Raster::Raster(Raster &&m) = default; +// Raster& Raster::operator=(Raster&&) = default; -Raster::~Raster() {} - -Raster::Raster(Raster &&m): - m_impl(std::move(m.m_impl)) {} - -void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd, - double g) -{ - // Free up the unnecessary memory and make sure it stays clear after - // an exception - auto o = m_impl? m_impl->origin() : Origin::TOP_LEFT; - reset(r, pd, o, g); +// FIXME: remove after migrating to higher version of windows compiler +Raster::Raster(Raster &&m): m_impl(std::move(m.m_impl)) {} +Raster& Raster::operator=(Raster &&m) { + m_impl = std::move(m.m_impl); return *this; } void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd, - Raster::Origin o, double gamma) + Format fmt, double gamma) { m_impl.reset(); - m_impl.reset(new Impl(r, pd, o, gamma)); + m_impl.reset(new Impl(r, pd, fmt, gamma)); +} + +void Raster::reset(const Raster::Resolution &r, const Raster::PixelDim &pd, + const std::array& mirror, double gamma) +{ + m_impl.reset(); + m_impl.reset(new Impl(r, pd, mirror, gamma)); } void Raster::reset() @@ -208,13 +235,13 @@ void Raster::draw(const ClipperLib::Polygon &poly) m_impl->draw(poly); } -void Raster::save(std::ostream& stream, Compression comp) +void Raster::save(std::ostream& stream, Format fmt) { assert(m_impl); if(!stream.good()) return; - switch(comp) { - case Compression::PNG: { + switch(fmt) { + case Format::PNG: { auto& b = m_impl->buffer(); size_t out_len = 0; void * rawdata = tdefl_write_image_to_png_file_in_memory( @@ -231,7 +258,7 @@ void Raster::save(std::ostream& stream, Compression comp) break; } - case Compression::RAW: { + case Format::RAW: { stream << "P5 " << m_impl->resolution().width_px << " " << m_impl->resolution().height_px << " " @@ -244,14 +271,19 @@ void Raster::save(std::ostream& stream, Compression comp) } } -RawBytes Raster::save(Raster::Compression comp) +void Raster::save(std::ostream &stream) +{ + save(stream, m_impl->format()); +} + +RawBytes Raster::save(Format fmt) { assert(m_impl); std::vector data; size_t s = 0; - switch(comp) { - case Compression::PNG: { + switch(fmt) { + case Format::PNG: { void *rawdata = tdefl_write_image_to_png_file_in_memory( m_impl->buffer().data(), int(resolution().width_px), @@ -265,7 +297,7 @@ RawBytes Raster::save(Raster::Compression comp) MZ_FREE(rawdata); break; } - case Compression::RAW: { + case Format::RAW: { auto header = std::string("P5 ") + std::to_string(m_impl->resolution().width_px) + " " + std::to_string(m_impl->resolution().height_px) + " " + "255 "; @@ -286,4 +318,12 @@ RawBytes Raster::save(Raster::Compression comp) return {std::move(data)}; } +RawBytes Raster::save() +{ + return save(m_impl->format()); } + +} +} + +#endif // SLARASTER_CPP diff --git a/src/libslic3r/Rasterizer/Rasterizer.hpp b/src/libslic3r/SLA/SLARaster.hpp similarity index 64% rename from src/libslic3r/Rasterizer/Rasterizer.hpp rename to src/libslic3r/SLA/SLARaster.hpp index 3fffe1a365..d3bd52d92c 100644 --- a/src/libslic3r/Rasterizer/Rasterizer.hpp +++ b/src/libslic3r/SLA/SLARaster.hpp @@ -1,17 +1,21 @@ -#ifndef RASTERIZER_HPP -#define RASTERIZER_HPP +#ifndef SLARASTER_HPP +#define SLARASTER_HPP #include #include #include +#include +#include #include namespace ClipperLib { struct Polygon; } -namespace Slic3r { +namespace Slic3r { class ExPolygon; +namespace sla { + // Raw byte buffer paired with its size. Suitable for compressed PNG data. class RawBytes { @@ -23,15 +27,18 @@ public: size_t size() const { return m_buffer.size(); } const uint8_t * data() { return m_buffer.data(); } + + RawBytes(const RawBytes&) = delete; + RawBytes& operator=(const RawBytes&) = delete; // ///////////////////////////////////////////////////////////////////////// // FIXME: the following is needed for MSVC2013 compatibility // ///////////////////////////////////////////////////////////////////////// - RawBytes(const RawBytes&) = delete; - RawBytes(RawBytes&& mv) : m_buffer(std::move(mv.m_buffer)) {} + // RawBytes(RawBytes&&) = default; + // RawBytes& operator=(RawBytes&&) = default; - RawBytes& operator=(const RawBytes&) = delete; + RawBytes(RawBytes&& mv) : m_buffer(std::move(mv.m_buffer)) {} RawBytes& operator=(RawBytes&& mv) { m_buffer = std::move(mv.m_buffer); return *this; @@ -54,28 +61,19 @@ class Raster { public: /// Supported compression types - enum class Compression { + enum class Format { RAW, //!> Uncompressed pixel data PNG //!> PNG compression }; - /// The Rasterizer expects the input polygons to have their coordinate - /// system origin in the bottom left corner. If the raster is then - /// configured with the TOP_LEFT origin parameter (in the constructor) than - /// it will flip the Y axis in output to maintain the correct orientation. - /// This is the default case with PNG images. They have the origin in the - /// top left corner. Without the flipping, the image would be upside down - /// with the scaled (clipper) coordinate system of the input polygons. - enum class Origin { - TOP_LEFT, - BOTTOM_LEFT - }; - /// Type that represents a resolution in pixels. struct Resolution { unsigned width_px; unsigned height_px; - inline Resolution(unsigned w, unsigned h): width_px(w), height_px(h) {} + + inline Resolution(unsigned w = 0, unsigned h = 0): + width_px(w), height_px(h) {} + inline unsigned pixels() const /*noexcept*/ { return width_px * height_px; } @@ -85,24 +83,34 @@ public: struct PixelDim { double w_mm; double h_mm; - inline PixelDim(double px_width_mm, double px_height_mm ): + inline PixelDim(double px_width_mm = 0.0, double px_height_mm = 0.0): w_mm(px_width_mm), h_mm(px_height_mm) {} }; /// Constructor taking the resolution and the pixel dimension. - Raster(const Resolution& r, const PixelDim& pd, - Origin o = Origin::BOTTOM_LEFT, double gamma = 1.0); + template Raster(Args...args) { + reset(std::forward(args)...); + } - Raster(); Raster(const Raster& cpy) = delete; Raster& operator=(const Raster& cpy) = delete; Raster(Raster&& m); + Raster& operator=(Raster&&); ~Raster(); /// Reallocated everything for the given resolution and pixel dimension. - void reset(const Resolution& r, const PixelDim& pd, double gamma = 1.0); - void reset(const Resolution& r, const PixelDim& pd, Origin o, double gamma); - + /// The third parameter is either the X, Y mirroring or a supported format + /// for which the correct mirroring will be configured. + void reset(const Resolution&, + const PixelDim&, + const std::array& mirror, + double gamma = 1.0); + + void reset(const Resolution& r, + const PixelDim& pd, + Format o, + double gamma = 1.0); + /** * Release the allocated resources. Drawing in this state ends in * unspecified behavior. @@ -119,11 +127,24 @@ public: void draw(const ExPolygon& poly); void draw(const ClipperLib::Polygon& poly); + // Saving the raster: + // It is possible to override the format given in the constructor but + // be aware that the mirroring will not be modified. + /// Save the raster on the specified stream. - void save(std::ostream& stream, Compression comp = Compression::RAW); + void save(std::ostream& stream, Format); + void save(std::ostream& stream); - RawBytes save(Compression comp = Compression::RAW); + /// Save into a continuous byte stream which is returned. + RawBytes save(Format fmt); + RawBytes save(); }; -} -#endif // RASTERIZER_HPP +// This prevents the duplicate default constructor warning on MSVC2013 +template<> Raster::Raster(); + + +} // sla +} // Slic3r + +#endif // SLARASTER_HPP diff --git a/src/libslic3r/SLA/SLARasterWriter.cpp b/src/libslic3r/SLA/SLARasterWriter.cpp new file mode 100644 index 0000000000..f7c3925ac7 --- /dev/null +++ b/src/libslic3r/SLA/SLARasterWriter.cpp @@ -0,0 +1,136 @@ +#include "SLARasterWriter.hpp" +#include "libslic3r/Zipper.hpp" +#include "ExPolygon.hpp" +#include + +#include +#include + +namespace Slic3r { namespace sla { + +std::string SLARasterWriter::createIniContent(const std::string& projectname) const +{ + auto expt_str = std::to_string(m_exp_time_s); + auto expt_first_str = std::to_string(m_exp_time_first_s); + auto layerh_str = std::to_string(m_layer_height); + + const std::string cnt_fade_layers = std::to_string(m_cnt_fade_layers); + const std::string cnt_slow_layers = std::to_string(m_cnt_slow_layers); + const std::string cnt_fast_layers = std::to_string(m_cnt_fast_layers); + const std::string used_material = std::to_string(m_used_material); + + return std::string( + "action = print\n" + "jobDir = ") + projectname + "\n" + + "expTime = " + expt_str + "\n" + "expTimeFirst = " + expt_first_str + "\n" + "numFade = " + cnt_fade_layers + "\n" + "layerHeight = " + layerh_str + "\n" + "usedMaterial = " + used_material + "\n" + "numSlow = " + cnt_slow_layers + "\n" + "numFast = " + cnt_fast_layers + "\n"; +} + +void SLARasterWriter::flpXY(ClipperLib::Polygon &poly) +{ + for(auto& p : poly.Contour) std::swap(p.X, p.Y); + std::reverse(poly.Contour.begin(), poly.Contour.end()); + + for(auto& h : poly.Holes) { + for(auto& p : h) std::swap(p.X, p.Y); + std::reverse(h.begin(), h.end()); + } +} + +void SLARasterWriter::flpXY(ExPolygon &poly) +{ + for(auto& p : poly.contour.points) p = Point(p.y(), p.x()); + std::reverse(poly.contour.points.begin(), poly.contour.points.end()); + + for(auto& h : poly.holes) { + for(auto& p : h.points) p = Point(p.y(), p.x()); + std::reverse(h.points.begin(), h.points.end()); + } +} + +SLARasterWriter::SLARasterWriter(const SLAPrinterConfig &cfg, + const SLAMaterialConfig &mcfg, + double layer_height) +{ + double w = cfg.display_width.getFloat(); + double h = cfg.display_height.getFloat(); + auto pw = unsigned(cfg.display_pixels_x.getInt()); + auto ph = unsigned(cfg.display_pixels_y.getInt()); + + m_mirror[X] = cfg.display_mirror_x.getBool(); + + // PNG raster will implicitly do an Y mirror + m_mirror[Y] = ! cfg.display_mirror_y.getBool(); + + auto ro = cfg.display_orientation.getInt(); + + if(ro == roPortrait) { + std::swap(w, h); + std::swap(pw, ph); + m_o = roPortrait; + + // XY flipping implicitly does an X mirror + m_mirror[X] = ! m_mirror[X]; + } else m_o = roLandscape; + + m_res = Raster::Resolution(pw, ph); + m_pxdim = Raster::PixelDim(w/pw, h/ph); + m_exp_time_s = mcfg.exposure_time.getFloat(); + m_exp_time_first_s = mcfg.initial_exposure_time.getFloat(); + m_layer_height = layer_height; + + m_gamma = cfg.gamma_correction.getFloat(); +} + +void SLARasterWriter::save(const std::string &fpath, const std::string &prjname) +{ + try { + Zipper zipper(fpath); // zipper with no compression + + std::string project = prjname.empty()? + boost::filesystem::path(fpath).stem().string() : prjname; + + zipper.add_entry("config.ini"); + + zipper << createIniContent(project); + + for(unsigned i = 0; i < m_layers_rst.size(); i++) + { + if(m_layers_rst[i].rawbytes.size() > 0) { + char lyrnum[6]; + std::sprintf(lyrnum, "%.5d", i); + auto zfilename = project + lyrnum + ".png"; + + // Add binary entry to the zipper + zipper.add_entry(zfilename, + m_layers_rst[i].rawbytes.data(), + m_layers_rst[i].rawbytes.size()); + } + } + + zipper.finalize(); + } catch(std::exception& e) { + BOOST_LOG_TRIVIAL(error) << e.what(); + // Rethrow the exception + throw; + } +} + +void SLARasterWriter::set_statistics(const std::vector statistics) +{ + if (statistics.size() != psCnt) + return; + + m_used_material = statistics[psUsedMaterial]; + m_cnt_fade_layers = int(statistics[psNumFade]); + m_cnt_slow_layers = int(statistics[psNumSlow]); + m_cnt_fast_layers = int(statistics[psNumFast]); +} + +} // namespace sla +} // namespace Slic3r diff --git a/src/libslic3r/SLA/SLARasterWriter.hpp b/src/libslic3r/SLA/SLARasterWriter.hpp new file mode 100644 index 0000000000..7133d2ddec --- /dev/null +++ b/src/libslic3r/SLA/SLARasterWriter.hpp @@ -0,0 +1,167 @@ +#ifndef SLARASTERWRITER_HPP +#define SLARASTERWRITER_HPP + +// For png export of the sliced model +#include +#include +#include +#include + +#include "libslic3r/PrintConfig.hpp" + +#include "SLARaster.hpp" + +namespace Slic3r { namespace sla { + +// Implementation for PNG raster output +// Be aware that if a large number of layers are allocated, it can very well +// exhaust the available memory especially on 32 bit platform. +// This class is designed to be used in parallel mode. Layers have an ID and +// each layer can be written and compressed independently (in parallel). +// At the end when all layers where written, the save method can be used to +// write out the result into a zipped archive. +class SLARasterWriter +{ +public: + enum RasterOrientation { + roLandscape, + roPortrait + }; + + // Used for addressing parameters of set_statistics() + enum ePrintStatistics + { + psUsedMaterial = 0, + psNumFade, + psNumSlow, + psNumFast, + + psCnt + }; + +private: + + // A struct to bind the raster image data and its compressed bytes together. + struct Layer { + Raster raster; + RawBytes rawbytes; + + Layer() = default; + Layer(const Layer&) = delete; // The image is big, do not copy by accident + Layer& operator=(const Layer&) = delete; + + // ///////////////////////////////////////////////////////////////////// + // FIXME: the following is needed for MSVC2013 compatibility + // ///////////////////////////////////////////////////////////////////// + + // Layer(Layer&& m) = default; + // Layer& operator=(Layer&&) = default; + Layer(Layer &&m): + raster(std::move(m.raster)), rawbytes(std::move(m.rawbytes)) {} + Layer& operator=(Layer &&m) { + raster = std::move(m.raster); rawbytes = std::move(m.rawbytes); + return *this; + } + }; + + // We will save the compressed PNG data into RawBytes type buffers in + // parallel. Later we can write every layer to the disk sequentially. + std::vector m_layers_rst; + Raster::Resolution m_res; + Raster::PixelDim m_pxdim; + double m_exp_time_s = .0, m_exp_time_first_s = .0; + double m_layer_height = .0; + RasterOrientation m_o = roPortrait; + std::array m_mirror; + + double m_gamma; + + double m_used_material = 0.0; + int m_cnt_fade_layers = 0; + int m_cnt_slow_layers = 0; + int m_cnt_fast_layers = 0; + + std::string createIniContent(const std::string& projectname) const; + + static void flpXY(ClipperLib::Polygon& poly); + static void flpXY(ExPolygon& poly); + +public: + + SLARasterWriter(const SLAPrinterConfig& cfg, + const SLAMaterialConfig& mcfg, + double layer_height); + + SLARasterWriter(const SLARasterWriter& ) = delete; + SLARasterWriter& operator=(const SLARasterWriter&) = delete; + + // ///////////////////////////////////////////////////////////////////////// + // FIXME: the following is needed for MSVC2013 compatibility + // ///////////////////////////////////////////////////////////////////////// + + // SLARasterWriter(SLARasterWriter&& m) = default; + // SLARasterWriter& operator=(SLARasterWriter&&) = default; + SLARasterWriter(SLARasterWriter&& m): + m_layers_rst(std::move(m.m_layers_rst)), + m_res(m.m_res), + m_pxdim(m.m_pxdim), + m_exp_time_s(m.m_exp_time_s), + m_exp_time_first_s(m.m_exp_time_first_s), + m_layer_height(m.m_layer_height), + m_o(m.m_o), + m_mirror(std::move(m.m_mirror)), + m_gamma(m.m_gamma), + m_used_material(m.m_used_material), + m_cnt_fade_layers(m.m_cnt_fade_layers), + m_cnt_slow_layers(m.m_cnt_slow_layers), + m_cnt_fast_layers(m.m_cnt_fast_layers) + {} + + // ///////////////////////////////////////////////////////////////////////// + + inline void layers(unsigned cnt) { if(cnt > 0) m_layers_rst.resize(cnt); } + inline unsigned layers() const { return unsigned(m_layers_rst.size()); } + + template void draw_polygon(const Poly& p, unsigned lyr) { + assert(lyr < m_layers_rst.size()); + if(m_o == roPortrait) { + Poly poly(p); flpXY(poly); + m_layers_rst[lyr].raster.draw(poly); + } + else m_layers_rst[lyr].raster.draw(p); + } + + inline void begin_layer(unsigned lyr) { + if(m_layers_rst.size() <= lyr) m_layers_rst.resize(lyr+1); + m_layers_rst[lyr].raster.reset(m_res, m_pxdim, m_mirror, m_gamma); + } + + inline void begin_layer() { + m_layers_rst.emplace_back(); + m_layers_rst.front().raster.reset(m_res, m_pxdim, m_mirror, m_gamma); + } + + inline void finish_layer(unsigned lyr_id) { + assert(lyr_id < m_layers_rst.size()); + m_layers_rst[lyr_id].rawbytes = + m_layers_rst[lyr_id].raster.save(Raster::Format::PNG); + m_layers_rst[lyr_id].raster.reset(); + } + + inline void finish_layer() { + if(!m_layers_rst.empty()) { + m_layers_rst.back().rawbytes = + m_layers_rst.back().raster.save(Raster::Format::PNG); + m_layers_rst.back().raster.reset(); + } + } + + void save(const std::string& fpath, const std::string& prjname = ""); + + void set_statistics(const std::vector statistics); +}; + +} // namespace sla +} // namespace Slic3r + +#endif // SLARASTERWRITER_HPP diff --git a/src/libslic3r/SLA/SLARotfinder.cpp b/src/libslic3r/SLA/SLARotfinder.cpp index 1a91041b78..2e64059d68 100644 --- a/src/libslic3r/SLA/SLARotfinder.cpp +++ b/src/libslic3r/SLA/SLARotfinder.cpp @@ -44,7 +44,7 @@ std::array find_best_rotation(const ModelObject& modelobj, // call the status callback in each iteration but the actual value may be // the same for subsequent iterations (status goes from 0 to 100 but // iterations can be many more) - auto objfunc = [&emesh, &status, &statuscb, max_tries] + auto objfunc = [&emesh, &status, &statuscb, &stopcond, max_tries] (double rx, double ry, double rz) { EigenMesh3D& m = emesh; @@ -91,7 +91,7 @@ std::array find_best_rotation(const ModelObject& modelobj, } // report status - statuscb( unsigned(++status * 100.0/max_tries) ); + if(!stopcond()) statuscb( unsigned(++status * 100.0/max_tries) ); return score; }; diff --git a/src/libslic3r/SLA/SLASupportTree.cpp b/src/libslic3r/SLA/SLASupportTree.cpp index cb2001024d..ae033c62fc 100644 --- a/src/libslic3r/SLA/SLASupportTree.cpp +++ b/src/libslic3r/SLA/SLASupportTree.cpp @@ -236,13 +236,13 @@ Contour3D cylinder(double r, double h, size_t ssteps, const Vec3d sp = {0,0,0}) // According to the slicing algorithms, we need to aid them with generating // a watertight body. So we create a triangle fan for the upper and lower // ending of the cylinder to close the geometry. - points.emplace_back(jp); size_t ci = points.size() - 1; + points.emplace_back(jp); int ci = int(points.size() - 1); for(int i = 0; i < steps - 1; ++i) indices.emplace_back(i + offs + 1, i + offs, ci); indices.emplace_back(offs, steps + offs - 1, ci); - points.emplace_back(endp); ci = points.size() - 1; + points.emplace_back(endp); ci = int(points.size() - 1); for(int i = 0; i < steps - 1; ++i) indices.emplace_back(ci, i, i + 1); diff --git a/src/libslic3r/SLA/SLASupportTreeIGL.cpp b/src/libslic3r/SLA/SLASupportTreeIGL.cpp index c368b8604d..1609b9ac41 100644 --- a/src/libslic3r/SLA/SLASupportTreeIGL.cpp +++ b/src/libslic3r/SLA/SLASupportTreeIGL.cpp @@ -121,19 +121,10 @@ EigenMesh3D::EigenMesh3D(const TriangleMesh& tmesh): m_aabb(new AABBImpl()) { V.resize(3*stl.stats.number_of_facets, 3); F.resize(stl.stats.number_of_facets, 3); for (unsigned int i = 0; i < stl.stats.number_of_facets; ++i) { - const stl_facet* facet = stl.facet_start+i; - V(3*i+0, 0) = double(facet->vertex[0](0)); - V(3*i+0, 1) = double(facet->vertex[0](1)); - V(3*i+0, 2) = double(facet->vertex[0](2)); - - V(3*i+1, 0) = double(facet->vertex[1](0)); - V(3*i+1, 1) = double(facet->vertex[1](1)); - V(3*i+1, 2) = double(facet->vertex[1](2)); - - V(3*i+2, 0) = double(facet->vertex[2](0)); - V(3*i+2, 1) = double(facet->vertex[2](1)); - V(3*i+2, 2) = double(facet->vertex[2](2)); - + const stl_facet &facet = stl.facet_start[i]; + V.block<1, 3>(3 * i + 0, 0) = facet.vertex[0].cast(); + V.block<1, 3>(3 * i + 1, 0) = facet.vertex[1].cast(); + V.block<1, 3>(3 * i + 2, 0) = facet.vertex[2].cast(); F(i, 0) = int(3*i+0); F(i, 1) = int(3*i+1); F(i, 2) = int(3*i+2); diff --git a/src/libslic3r/Rasterizer/bicubic.h b/src/libslic3r/SLA/bicubic.h similarity index 100% rename from src/libslic3r/Rasterizer/bicubic.h rename to src/libslic3r/SLA/bicubic.h diff --git a/src/libslic3r/SLAPrint.cpp b/src/libslic3r/SLAPrint.cpp index f6a1c429e5..c73ae5650d 100644 --- a/src/libslic3r/SLAPrint.cpp +++ b/src/libslic3r/SLAPrint.cpp @@ -28,14 +28,16 @@ namespace Slic3r { using SupportTreePtr = std::unique_ptr; -class SLAPrintObject::SupportData { +class SLAPrintObject::SupportData +{ public: - sla::EigenMesh3D emesh; // index-triangle representation - std::vector support_points; // all the support points (manual/auto) - SupportTreePtr support_tree_ptr; // the supports - SlicedSupports support_slices; // sliced supports + sla::EigenMesh3D emesh; // index-triangle representation + std::vector + support_points; // all the support points (manual/auto) + SupportTreePtr support_tree_ptr; // the supports + SlicedSupports support_slices; // sliced supports - inline SupportData(const TriangleMesh& trmesh): emesh(trmesh) {} + inline SupportData(const TriangleMesh &trmesh) : emesh(trmesh) {} }; namespace { @@ -666,11 +668,11 @@ void SLAPrint::process() double ilhd = m_material_config.initial_layer_height.getFloat(); auto ilh = float(ilhd); - auto ilhs = coord_t(ilhd / SCALING_FACTOR); + auto ilhs = scaled(ilhd); const size_t objcount = m_objects.size(); - const unsigned min_objstatus = 0; // where the per object operations start - const unsigned max_objstatus = 50; // where the per object operations end + static const unsigned min_objstatus = 0; // where the per object operations start + static const unsigned max_objstatus = 50; // where the per object operations end // the coefficient that multiplies the per object status values which // are set up for <0, 100>. They need to be scaled into the whole process @@ -687,31 +689,32 @@ void SLAPrint::process() // Slicing the model object. This method is oversimplified and needs to // be compared with the fff slicing algorithm for verification - auto slice_model = [this, ilhs, ilh, ilhd](SLAPrintObject& po) { + auto slice_model = [this, ilhs, ilh](SLAPrintObject& po) { const TriangleMesh& mesh = po.transformed_mesh(); // We need to prepare the slice index... double lhd = m_objects.front()->m_config.layer_height.getFloat(); float lh = float(lhd); - auto lhs = coord_t(lhd / SCALING_FACTOR); + auto lhs = scaled(lhd); - auto&& bb3d = mesh.bounding_box(); - double minZ = bb3d.min(Z) - po.get_elevation(); - double maxZ = bb3d.max(Z); + auto &&bb3d = mesh.bounding_box(); + double minZ = bb3d.min(Z) - po.get_elevation(); + double maxZ = bb3d.max(Z); + auto minZf = float(minZ); - auto minZs = coord_t(minZ / SCALING_FACTOR); - auto maxZs = coord_t(maxZ / SCALING_FACTOR); + auto minZs = scaled(minZ); + auto maxZs = scaled(maxZ); po.m_slice_index.clear(); size_t cap = size_t(1 + (maxZs - minZs - ilhs) / lhs); po.m_slice_index.reserve(cap); - po.m_slice_index.emplace_back(minZs + ilhs, minZ + ilhd / 2.0, ilh); + po.m_slice_index.emplace_back(minZs + ilhs, minZf + ilh / 2.f, ilh); - for(coord_t h = minZs + ilhs + lhs; h <= maxZs; h += lhs) - po.m_slice_index.emplace_back(h, h*SCALING_FACTOR - lhd / 2.0, lh); + for(coord_t h = minZs + ilhs + lhs; h <= maxZs; h += lhs) + po.m_slice_index.emplace_back(h, unscaled(h) - lh / 2.f, lh); // Just get the first record that is form the model: auto slindex_it = @@ -737,7 +740,7 @@ void SLAPrint::process() auto mit = slindex_it; double doffs = m_printer_config.absolute_correction.getFloat(); - coord_t clpr_offs = coord_t(doffs / SCALING_FACTOR); + coord_t clpr_offs = scaled(doffs); for(size_t id = 0; id < po.m_model_slices.size() && mit != po.m_slice_index.end(); id++) @@ -745,7 +748,7 @@ void SLAPrint::process() // We apply the printer correction offset here. if(clpr_offs != 0) po.m_model_slices[id] = - offset_ex(po.m_model_slices[id], clpr_offs); + offset_ex(po.m_model_slices[id], float(clpr_offs)); mit->set_model_slice_idx(po, id); ++mit; } @@ -949,15 +952,15 @@ void SLAPrint::process() } double doffs = m_printer_config.absolute_correction.getFloat(); - coord_t clpr_offs = coord_t(doffs / SCALING_FACTOR); + coord_t clpr_offs = scaled(doffs); for(size_t i = 0; i < sd->support_slices.size() && i < po.m_slice_index.size(); ++i) { // We apply the printer correction offset here. if(clpr_offs != 0) - sd->support_slices[i] = - offset_ex(sd->support_slices[i], clpr_offs); + sd->support_slices[i] = + offset_ex(sd->support_slices[i], float(clpr_offs)); po.m_slice_index[i].set_support_slice_idx(po, i); } @@ -1011,7 +1014,7 @@ void SLAPrint::process() namespace sl = libnest2d::shapelike; // For algorithms // If the raster has vertical orientation, we will flip the coordinates - bool flpXY = m_printer_config.display_orientation.getInt() == SLADisplayOrientation::sladoPortrait; +// bool flpXY = m_printer_config.display_orientation.getInt() == SLADisplayOrientation::sladoPortrait; // Set up custom union and diff functions for clipper polygons auto polyunion = [] (const ClipperPolygons& subjects) @@ -1063,15 +1066,15 @@ void SLAPrint::process() const int fade_layers_cnt = m_default_object_config.faded_layers.getInt();// 10 // [3;20] - const double width = m_printer_config.display_width.getFloat() / SCALING_FACTOR; - const double height = m_printer_config.display_height.getFloat() / SCALING_FACTOR; + const double width = scaled(m_printer_config.display_width.getFloat()); + const double height = scaled(m_printer_config.display_height.getFloat()); const double display_area = width*height; // get polygons for all instances in the object auto get_all_polygons = - [flpXY](const ExPolygons& input_polygons, - const std::vector& instances, - bool is_lefthanded) + [](const ExPolygons& input_polygons, + const std::vector& instances, + bool is_lefthanded) { ClipperPolygons polygons; polygons.reserve(input_polygons.size() * instances.size()); @@ -1085,7 +1088,7 @@ void SLAPrint::process() // We need to reverse if flpXY OR is_lefthanded is true but // not if both are true which is a logical inequality (XOR) - bool needreverse = flpXY != is_lefthanded; + bool needreverse = /*flpXY !=*/ is_lefthanded; // should be a move poly.Contour.reserve(polygon.contour.size() + 1); @@ -1120,10 +1123,10 @@ void SLAPrint::process() sl::translate(poly, ClipperPoint{instances[i].shift(X), instances[i].shift(Y)}); - if (flpXY) { - for(auto& p : poly.Contour) std::swap(p.X, p.Y); - for(auto& h : poly.Holes) for(auto& p : h) std::swap(p.X, p.Y); - } +// if (flpXY) { +// for(auto& p : poly.Contour) std::swap(p.X, p.Y); +// for(auto& h : poly.Holes) for(auto& p : h) std::swap(p.X, p.Y); +// } polygons.emplace_back(std::move(poly)); } @@ -1170,13 +1173,20 @@ void SLAPrint::process() ClipperPolygons model_polygons; ClipperPolygons supports_polygons; - size_t c = std::accumulate(layer.slices().begin(), layer.slices().end(), 0u, [](size_t a, const SliceRecord& sr) { - return a + sr.get_slice(soModel).size(); + size_t c = std::accumulate(layer.slices().begin(), + layer.slices().end(), + size_t(0), + [](size_t a, const SliceRecord &sr) { + return a + sr.get_slice(soModel) + .size(); }); model_polygons.reserve(c); - c = std::accumulate(layer.slices().begin(), layer.slices().end(), 0u, [](size_t a, const SliceRecord& sr) { + c = std::accumulate(layer.slices().begin(), + layer.slices().end(), + size_t(0), + [](size_t a, const SliceRecord &sr) { return a + sr.get_slice(soModel).size(); }); @@ -1264,8 +1274,9 @@ void SLAPrint::process() // for(size_t i = 0; i < m_printer_input.size(); ++i) printlayerfn(i); tbb::parallel_for(0, m_printer_input.size(), printlayerfn); - m_print_statistics.support_used_material = supports_volume * SCALING_FACTOR * SCALING_FACTOR; - m_print_statistics.objects_used_material = models_volume * SCALING_FACTOR * SCALING_FACTOR; + auto SCALING2 = SCALING_FACTOR * SCALING_FACTOR; + m_print_statistics.support_used_material = supports_volume * SCALING2; + m_print_statistics.objects_used_material = models_volume * SCALING2; // Estimated printing time // A layers count o the highest object @@ -1281,38 +1292,14 @@ void SLAPrint::process() }; // Rasterizing the model objects, and their supports - auto rasterize = [this, max_objstatus]() { + auto rasterize = [this]() { if(canceled()) return; - // collect all the keys - - // If the raster has vertical orientation, we will flip the coordinates - bool flpXY = m_printer_config.display_orientation.getInt() == - SLADisplayOrientation::sladoPortrait; - { // create a raster printer for the current print parameters - // I don't know any better - auto& ocfg = m_objects.front()->m_config; - auto& matcfg = m_material_config; - auto& printcfg = m_printer_config; - - double w = printcfg.display_width.getFloat(); - double h = printcfg.display_height.getFloat(); - auto pw = unsigned(printcfg.display_pixels_x.getInt()); - auto ph = unsigned(printcfg.display_pixels_y.getInt()); - double lh = ocfg.layer_height.getFloat(); - double exp_t = matcfg.exposure_time.getFloat(); - double iexp_t = matcfg.initial_exposure_time.getFloat(); - - double gamma = m_printer_config.gamma_correction.getFloat(); - - if(flpXY) { std::swap(w, h); std::swap(pw, ph); } - - m_printer.reset( - new SLAPrinter(w, h, pw, ph, lh, exp_t, iexp_t, - flpXY? SLAPrinter::RO_PORTRAIT : - SLAPrinter::RO_LANDSCAPE, - gamma)); + double layerh = m_default_object_config.layer_height.getFloat(); + m_printer.reset(new SLAPrinter(m_printer_config, + m_material_config, + layerh)); } // Allocate space for all the layers @@ -1376,11 +1363,12 @@ void SLAPrint::process() tbb::parallel_for(0, lvlcnt, lvlfn); // Set statistics values to the printer - m_printer->set_statistics({(m_print_statistics.objects_used_material + m_print_statistics.support_used_material)/1000, - double(m_default_object_config.faded_layers.getInt()), - double(m_print_statistics.slow_layers_count), - double(m_print_statistics.fast_layers_count) - }); + m_printer->set_statistics( + {(m_print_statistics.objects_used_material + + m_print_statistics.support_used_material) / 1000, + double(m_default_object_config.faded_layers.getInt()), + double(m_print_statistics.slow_layers_count), + double(m_print_statistics.fast_layers_count)}); }; using slaposFn = std::function; @@ -1408,25 +1396,36 @@ void SLAPrint::process() // TODO: this loop could run in parallel but should not exhaust all the CPU // power available - // Calculate the support structures first before slicing the supports, so that the preview will get displayed ASAP for all objects. - std::vector step_ranges = { slaposObjectSlice, slaposSliceSupports, slaposCount }; - for (size_t idx_range = 0; idx_range + 1 < step_ranges.size(); ++ idx_range) { - for(SLAPrintObject * po : m_objects) { + // Calculate the support structures first before slicing the supports, + // so that the preview will get displayed ASAP for all objects. + std::vector step_ranges = {slaposObjectSlice, + slaposSliceSupports, + slaposCount}; - BOOST_LOG_TRIVIAL(info) << "Slicing object " << po->model_object()->name; + for (size_t idx_range = 0; idx_range + 1 < step_ranges.size(); ++idx_range) { + for (SLAPrintObject *po : m_objects) { - for (int s = int(step_ranges[idx_range]); s < int(step_ranges[idx_range + 1]); ++s) { + BOOST_LOG_TRIVIAL(info) + << "Slicing object " << po->model_object()->name; + + for (int s = int(step_ranges[idx_range]); + s < int(step_ranges[idx_range + 1]); + ++s) { auto currentstep = static_cast(s); - // Cancellation checking. Each step will check for cancellation - // on its own and return earlier gracefully. Just after it returns - // execution gets to this point and throws the canceled signal. + // Cancellation checking. Each step will check for + // cancellation on its own and return earlier gracefully. + // Just after it returns execution gets to this point and + // throws the canceled signal. throw_if_canceled(); st += incr * ostepd; - if(po->m_stepmask[currentstep] && po->set_started(currentstep)) { - m_report_status(*this, st, OBJ_STEP_LABELS(currentstep)); + if (po->m_stepmask[currentstep] + && po->set_started(currentstep)) { + m_report_status(*this, + st, + OBJ_STEP_LABELS(currentstep)); pobj_program[currentstep](*po); throw_if_canceled(); po->set_done(currentstep); @@ -1488,6 +1487,8 @@ bool SLAPrint::invalidate_state_by_config_options(const std::vector SLAPrintObject::transformed_support_points() cons ret.reserve(spts.size()); for(sla::SupportPoint& sp : spts) { - Vec3d transformed_pos = trafo() * Vec3d(sp.pos(0), sp.pos(1), sp.pos(2)); - ret.emplace_back(transformed_pos(0), transformed_pos(1), transformed_pos(2), sp.head_front_radius, sp.is_new_island); + Vec3f transformed_pos = trafo().cast() * sp.pos; + ret.emplace_back(transformed_pos, sp.head_front_radius, sp.is_new_island); } return ret; diff --git a/src/libslic3r/SLAPrint.hpp b/src/libslic3r/SLAPrint.hpp index dea468e7a0..b0c5a8fdcc 100644 --- a/src/libslic3r/SLAPrint.hpp +++ b/src/libslic3r/SLAPrint.hpp @@ -3,11 +3,11 @@ #include #include "PrintBase.hpp" -#include "PrintExport.hpp" +//#include "PrintExport.hpp" +#include "SLA/SLARasterWriter.hpp" #include "Point.hpp" #include "MTUtils.hpp" #include -#include "Zipper.hpp" namespace Slic3r { @@ -54,15 +54,15 @@ public: bool is_left_handed() const { return m_left_handed; } struct Instance { - Instance(ModelID instance_id, const Point &shift, float rotation) : instance_id(instance_id), shift(shift), rotation(rotation) {} - bool operator==(const Instance &rhs) const { return this->instance_id == rhs.instance_id && this->shift == rhs.shift && this->rotation == rhs.rotation; } - // ID of the corresponding ModelInstance. - ModelID instance_id; - // Slic3r::Point objects in scaled G-code coordinates - Point shift; - // Rotation along the Z axis, in radians. - float rotation; - }; + Instance(ModelID instance_id, const Point &shift, float rotation) : instance_id(instance_id), shift(shift), rotation(rotation) {} + bool operator==(const Instance &rhs) const { return this->instance_id == rhs.instance_id && this->shift == rhs.shift && this->rotation == rhs.rotation; } + // ID of the corresponding ModelInstance. + ModelID instance_id; + // Slic3r::Point objects in scaled G-code coordinates + Point shift; + // Rotation along the Z axis, in radians. + float rotation; + }; const std::vector& instances() const { return m_instances; } bool has_mesh(SLAPrintObjectStep step) const; @@ -142,15 +142,19 @@ public: }; private: - - template inline static T level(const SliceRecord& sr) { + template inline static T level(const SliceRecord &sr) + { static_assert(std::is_arithmetic::value, "Arithmetic only!"); - return std::is_integral::value ? T(sr.print_level()) : T(sr.slice_level()); + return std::is_integral::value ? T(sr.print_level()) + : T(sr.slice_level()); } - template inline static SliceRecord create_slice_record(T val) { + template inline static SliceRecord create_slice_record(T val) + { static_assert(std::is_arithmetic::value, "Arithmetic only!"); - return std::is_integral::value ? SliceRecord{ coord_t(val), 0.f, 0.f } : SliceRecord{ 0, float(val), 0.f }; + return std::is_integral::value + ? SliceRecord{coord_t(val), 0.f, 0.f} + : SliceRecord{0, float(val), 0.f}; } // This is a template method for searching the slice index either by @@ -241,11 +245,11 @@ protected: ~SLAPrintObject(); void config_apply(const ConfigBase &other, bool ignore_nonexistent = false) { this->m_config.apply(other, ignore_nonexistent); } - void config_apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent = false) - { this->m_config.apply_only(other, keys, ignore_nonexistent); } + void config_apply_only(const ConfigBase &other, const t_config_option_keys &keys, bool ignore_nonexistent = false) + { this->m_config.apply_only(other, keys, ignore_nonexistent); } void set_trafo(const Transform3d& trafo, bool left_handed) { - m_transformed_rmesh.invalidate([this, &trafo, left_handed](){ m_trafo = trafo; m_left_handed = left_handed; }); + m_transformed_rmesh.invalidate([this, &trafo, left_handed](){ m_trafo = trafo; m_left_handed = left_handed; }); } template inline void set_instances(InstVec&& instances) { m_instances = std::forward(instances); } @@ -322,37 +326,6 @@ struct SLAPrintStatistics } }; -// The implementation of creating zipped archives with wxWidgets -template<> class LayerWriter { - Zipper m_zip; -public: - - LayerWriter(const std::string& zipfile_path): m_zip(zipfile_path) {} - - void next_entry(const std::string& fname) { m_zip.add_entry(fname); } - - void binary_entry(const std::string& fname, - const std::uint8_t* buf, - size_t l) - { - m_zip.add_entry(fname, buf, l); - } - - template inline LayerWriter& operator<<(T&& arg) { - m_zip << std::forward(arg); return *this; - } - - bool is_ok() const { - return true; // m_zip blows up if something goes wrong... - } - - // After finalize, no writing to the archive will have an effect. The only - // valid operation is to dispose the object calling the destructor which - // should close the file. This method can throw and signal potential errors - // when flushing the archive. This is why its present. - void finalize() { m_zip.finalize(); } -}; - /** * @brief This class is the high level FSM for the SLA printing process. * @@ -380,16 +353,15 @@ public: void set_task(const TaskParams ¶ms) override; void process() override; void finalize() override; - // Returns true if an object step is done on all objects and there's at least one object. + // Returns true if an object step is done on all objects and there's at least one object. bool is_step_done(SLAPrintObjectStep step) const; // Returns true if the last step was finished with success. bool finished() const override { return this->is_step_done(slaposSliceSupports) && this->Inherited::is_step_done(slapsRasterize); } - template inline void export_raster(const std::string& fpath, - const std::string& projectname = "") + const std::string& projectname = "") { - if(m_printer) m_printer->save(fpath, projectname); + if(m_printer) m_printer->save(fpath, projectname); } const PrintObjects& objects() const { return m_objects; } @@ -450,7 +422,7 @@ public: const std::vector& print_layers() const { return m_printer_input; } private: - using SLAPrinter = FilePrinter; + using SLAPrinter = sla::SLARasterWriter; using SLAPrinterPtr = std::unique_ptr; // Implement same logic as in SLAPrintObject diff --git a/src/libslic3r/Slicing.cpp b/src/libslic3r/Slicing.cpp index 3a05e9d8af..e1bb4b3130 100644 --- a/src/libslic3r/Slicing.cpp +++ b/src/libslic3r/Slicing.cpp @@ -227,7 +227,7 @@ std::vector layer_height_profile_adaptive( as.set_slicing_parameters(slicing_params); for (const ModelVolume *volume : volumes) if (volume->is_model_part()) - as.add_mesh(&volume->mesh); + as.add_mesh(&volume->mesh()); as.prepare(); // 2) Generate layers using the algorithm of @platsch diff --git a/src/libslic3r/SlicingAdaptive.cpp b/src/libslic3r/SlicingAdaptive.cpp index 2ef4aec8c6..ad03b550b9 100644 --- a/src/libslic3r/SlicingAdaptive.cpp +++ b/src/libslic3r/SlicingAdaptive.cpp @@ -27,8 +27,8 @@ void SlicingAdaptive::prepare() nfaces_total += (*it_mesh)->stl.stats.number_of_facets; m_faces.reserve(nfaces_total); for (std::vector::const_iterator it_mesh = m_meshes.begin(); it_mesh != m_meshes.end(); ++ it_mesh) - for (int i = 0; i < (*it_mesh)->stl.stats.number_of_facets; ++ i) - m_faces.push_back((*it_mesh)->stl.facet_start + i); + for (const stl_facet &face : (*it_mesh)->stl.facet_start) + m_faces.emplace_back(&face); // 2) Sort faces lexicographically by their Z span. std::sort(m_faces.begin(), m_faces.end(), [](const stl_facet *f1, const stl_facet *f2) { diff --git a/src/libslic3r/TriangleMesh.cpp b/src/libslic3r/TriangleMesh.cpp index 4d35cabca2..56accfefa9 100644 --- a/src/libslic3r/TriangleMesh.cpp +++ b/src/libslic3r/TriangleMesh.cpp @@ -42,20 +42,17 @@ namespace Slic3r { -TriangleMesh::TriangleMesh(const Pointf3s &points, const std::vector& facets) - : repaired(false) +TriangleMesh::TriangleMesh(const Pointf3s &points, const std::vector& facets) : repaired(false) { - stl_initialize(&this->stl); stl_file &stl = this->stl; - stl.error = 0; stl.stats.type = inmemory; // count facets and allocate memory - stl.stats.number_of_facets = facets.size(); + stl.stats.number_of_facets = (uint32_t)facets.size(); stl.stats.original_num_facets = stl.stats.number_of_facets; stl_allocate(&stl); - for (uint32_t i = 0; i < stl.stats.number_of_facets; i++) { + for (uint32_t i = 0; i < stl.stats.number_of_facets; ++ i) { stl_facet facet; facet.vertex[0] = points[facets[i](0)].cast(); facet.vertex[1] = points[facets[i](1)].cast(); @@ -73,57 +70,37 @@ TriangleMesh::TriangleMesh(const Pointf3s &points, const std::vector& f stl_get_size(&stl); } -TriangleMesh& TriangleMesh::operator=(const TriangleMesh &other) -{ - stl_close(&this->stl); - this->stl = other.stl; - this->repaired = other.repaired; - this->stl.heads = nullptr; - this->stl.tail = nullptr; - this->stl.error = other.stl.error; - if (other.stl.facet_start != nullptr) { - this->stl.facet_start = (stl_facet*)calloc(other.stl.stats.number_of_facets, sizeof(stl_facet)); - std::copy(other.stl.facet_start, other.stl.facet_start + other.stl.stats.number_of_facets, this->stl.facet_start); - } - if (other.stl.neighbors_start != nullptr) { - this->stl.neighbors_start = (stl_neighbors*)calloc(other.stl.stats.number_of_facets, sizeof(stl_neighbors)); - std::copy(other.stl.neighbors_start, other.stl.neighbors_start + other.stl.stats.number_of_facets, this->stl.neighbors_start); - } - if (other.stl.v_indices != nullptr) { - this->stl.v_indices = (v_indices_struct*)calloc(other.stl.stats.number_of_facets, sizeof(v_indices_struct)); - std::copy(other.stl.v_indices, other.stl.v_indices + other.stl.stats.number_of_facets, this->stl.v_indices); - } - if (other.stl.v_shared != nullptr) { - this->stl.v_shared = (stl_vertex*)calloc(other.stl.stats.shared_vertices, sizeof(stl_vertex)); - std::copy(other.stl.v_shared, other.stl.v_shared + other.stl.stats.shared_vertices, this->stl.v_shared); - } - return *this; -} - // #define SLIC3R_TRACE_REPAIR -void TriangleMesh::repair() +void TriangleMesh::repair(bool update_shared_vertices) { - if (this->repaired) return; - + if (this->repaired) { + if (update_shared_vertices) + this->require_shared_vertices(); + return; + } + // admesh fails when repairing empty meshes - if (this->stl.stats.number_of_facets == 0) return; + if (this->stl.stats.number_of_facets == 0) + return; BOOST_LOG_TRIVIAL(debug) << "TriangleMesh::repair() started"; - + // checking exact #ifdef SLIC3R_TRACE_REPAIR BOOST_LOG_TRIVIAL(trace) << "\tstl_check_faces_exact"; #endif /* SLIC3R_TRACE_REPAIR */ + assert(stl_validate(&this->stl)); stl_check_facets_exact(&stl); + assert(stl_validate(&this->stl)); stl.stats.facets_w_1_bad_edge = (stl.stats.connected_facets_2_edge - stl.stats.connected_facets_3_edge); stl.stats.facets_w_2_bad_edge = (stl.stats.connected_facets_1_edge - stl.stats.connected_facets_2_edge); stl.stats.facets_w_3_bad_edge = (stl.stats.number_of_facets - stl.stats.connected_facets_1_edge); // checking nearby //int last_edges_fixed = 0; - float tolerance = stl.stats.shortest_edge; - float increment = stl.stats.bounding_diameter / 10000.0; + float tolerance = (float)stl.stats.shortest_edge; + float increment = (float)stl.stats.bounding_diameter / 10000.0f; int iterations = 2; if (stl.stats.connected_facets_3_edge < (int)stl.stats.number_of_facets) { for (int i = 0; i < iterations; i++) { @@ -141,6 +118,7 @@ void TriangleMesh::repair() } } } + assert(stl_validate(&this->stl)); // remove_unconnected if (stl.stats.connected_facets_3_edge < (int)stl.stats.number_of_facets) { @@ -148,6 +126,7 @@ void TriangleMesh::repair() BOOST_LOG_TRIVIAL(trace) << "\tstl_remove_unconnected_facets"; #endif /* SLIC3R_TRACE_REPAIR */ stl_remove_unconnected_facets(&stl); + assert(stl_validate(&this->stl)); } // fill_holes @@ -168,28 +147,38 @@ void TriangleMesh::repair() BOOST_LOG_TRIVIAL(trace) << "\tstl_fix_normal_directions"; #endif /* SLIC3R_TRACE_REPAIR */ stl_fix_normal_directions(&stl); + assert(stl_validate(&this->stl)); // normal_values #ifdef SLIC3R_TRACE_REPAIR BOOST_LOG_TRIVIAL(trace) << "\tstl_fix_normal_values"; #endif /* SLIC3R_TRACE_REPAIR */ stl_fix_normal_values(&stl); + assert(stl_validate(&this->stl)); // always calculate the volume and reverse all normals if volume is negative #ifdef SLIC3R_TRACE_REPAIR BOOST_LOG_TRIVIAL(trace) << "\tstl_calculate_volume"; #endif /* SLIC3R_TRACE_REPAIR */ stl_calculate_volume(&stl); + assert(stl_validate(&this->stl)); // neighbors #ifdef SLIC3R_TRACE_REPAIR BOOST_LOG_TRIVIAL(trace) << "\tstl_verify_neighbors"; #endif /* SLIC3R_TRACE_REPAIR */ stl_verify_neighbors(&stl); + assert(stl_validate(&this->stl)); this->repaired = true; BOOST_LOG_TRIVIAL(debug) << "TriangleMesh::repair() finished"; + + // This call should be quite cheap, a lot of code requires the indexed_triangle_set data structure, + // and it is risky to generate such a structure once the meshes are shared. Do it now. + this->its.clear(); + if (update_shared_vertices) + this->require_shared_vertices(); } float TriangleMesh::volume() @@ -249,20 +238,24 @@ bool TriangleMesh::needed_repair() const void TriangleMesh::WriteOBJFile(const char* output_file) { - stl_generate_shared_vertices(&stl); - stl_write_obj(&stl, output_file); + its_write_obj(this->its, output_file); } void TriangleMesh::scale(float factor) { stl_scale(&(this->stl), factor); - stl_invalidate_shared_vertices(&this->stl); + for (stl_vertex& v : this->its.vertices) + v *= factor; } void TriangleMesh::scale(const Vec3d &versor) { stl_scale_versor(&this->stl, versor.cast()); - stl_invalidate_shared_vertices(&this->stl); + for (stl_vertex& v : this->its.vertices) { + v.x() *= versor.x(); + v.y() *= versor.y(); + v.z() *= versor.z(); + } } void TriangleMesh::translate(float x, float y, float z) @@ -270,7 +263,9 @@ void TriangleMesh::translate(float x, float y, float z) if (x == 0.f && y == 0.f && z == 0.f) return; stl_translate_relative(&(this->stl), x, y, z); - stl_invalidate_shared_vertices(&this->stl); + stl_vertex shift(x, y, z); + for (stl_vertex& v : this->its.vertices) + v += shift; } void TriangleMesh::translate(const Vec3f &displacement) @@ -287,13 +282,15 @@ void TriangleMesh::rotate(float angle, const Axis &axis) angle = Slic3r::Geometry::rad2deg(angle); if (axis == X) { - stl_rotate_x(&(this->stl), angle); + stl_rotate_x(&this->stl, angle); + its_rotate_x(this->its, angle); } else if (axis == Y) { - stl_rotate_y(&(this->stl), angle); + stl_rotate_y(&this->stl, angle); + its_rotate_y(this->its, angle); } else if (axis == Z) { - stl_rotate_z(&(this->stl), angle); + stl_rotate_z(&this->stl, angle); + its_rotate_z(this->its, angle); } - stl_invalidate_shared_vertices(&this->stl); } void TriangleMesh::rotate(float angle, const Vec3d& axis) @@ -305,39 +302,49 @@ void TriangleMesh::rotate(float angle, const Vec3d& axis) Transform3d m = Transform3d::Identity(); m.rotate(Eigen::AngleAxisd(angle, axis_norm)); stl_transform(&stl, m); + its_transform(its, m); } void TriangleMesh::mirror(const Axis &axis) { if (axis == X) { stl_mirror_yz(&this->stl); + for (stl_vertex &v : this->its.vertices) + v(0) *= -1.0; } else if (axis == Y) { stl_mirror_xz(&this->stl); + for (stl_vertex &v : this->its.vertices) + v(1) *= -1.0; } else if (axis == Z) { stl_mirror_xy(&this->stl); + for (stl_vertex &v : this->its.vertices) + v(2) *= -1.0; } - stl_invalidate_shared_vertices(&this->stl); } void TriangleMesh::transform(const Transform3d& t, bool fix_left_handed) { stl_transform(&stl, t); - stl_invalidate_shared_vertices(&stl); + its_transform(its, t); if (fix_left_handed && t.matrix().block(0, 0, 3, 3).determinant() < 0.) { // Left handed transformation is being applied. It is a good idea to flip the faces and their normals. - this->repair(); + this->repair(false); stl_reverse_all_facets(&stl); + this->its.clear(); + this->require_shared_vertices(); } } void TriangleMesh::transform(const Matrix3d& m, bool fix_left_handed) { stl_transform(&stl, m); - stl_invalidate_shared_vertices(&stl); + its_transform(its, m); if (fix_left_handed && m.determinant() < 0.) { // Left handed transformation is being applied. It is a good idea to flip the faces and their normals. - this->repair(); + this->repair(false); stl_reverse_all_facets(&stl); + this->its.clear(); + this->require_shared_vertices(); } } @@ -355,7 +362,8 @@ void TriangleMesh::rotate(double angle, Point* center) return; Vec2f c = center->cast(); this->translate(-c(0), -c(1), 0); - stl_rotate_z(&(this->stl), (float)angle); + stl_rotate_z(&this->stl, (float)angle); + its_rotate_z(this->its, (float)angle); this->translate(c(0), c(1), 0); } @@ -435,9 +443,8 @@ TriangleMeshPtrs TriangleMesh::split() const TriangleMesh* mesh = new TriangleMesh; meshes.emplace_back(mesh); mesh->stl.stats.type = inmemory; - mesh->stl.stats.number_of_facets = facets.size(); + mesh->stl.stats.number_of_facets = (uint32_t)facets.size(); mesh->stl.stats.original_num_facets = mesh->stl.stats.number_of_facets; - stl_clear_error(&mesh->stl); stl_allocate(&mesh->stl); // Assign the facets to the new mesh. @@ -455,7 +462,7 @@ void TriangleMesh::merge(const TriangleMesh &mesh) { // reset stats and metadata int number_of_facets = this->stl.stats.number_of_facets; - stl_invalidate_shared_vertices(&this->stl); + this->its.clear(); this->repaired = false; // update facet count and allocate more memory @@ -477,13 +484,12 @@ ExPolygons TriangleMesh::horizontal_projection() const { Polygons pp; pp.reserve(this->stl.stats.number_of_facets); - for (uint32_t i = 0; i < this->stl.stats.number_of_facets; ++ i) { - stl_facet* facet = &this->stl.facet_start[i]; + for (const stl_facet &facet : this->stl.facet_start) { Polygon p; p.points.resize(3); - p.points[0] = Point::new_scale(facet->vertex[0](0), facet->vertex[0](1)); - p.points[1] = Point::new_scale(facet->vertex[1](0), facet->vertex[1](1)); - p.points[2] = Point::new_scale(facet->vertex[2](0), facet->vertex[2](1)); + p.points[0] = Point::new_scale(facet.vertex[0](0), facet.vertex[0](1)); + p.points[1] = Point::new_scale(facet.vertex[1](0), facet.vertex[1](1)); + p.points[2] = Point::new_scale(facet.vertex[2](0), facet.vertex[2](1)); p.make_counter_clockwise(); // do this after scaling, as winding order might change while doing that pp.emplace_back(p); } @@ -495,11 +501,10 @@ ExPolygons TriangleMesh::horizontal_projection() const // 2D convex hull of a 3D mesh projected into the Z=0 plane. Polygon TriangleMesh::convex_hull() { - this->require_shared_vertices(); Points pp; - pp.reserve(this->stl.stats.shared_vertices); - for (int i = 0; i < this->stl.stats.shared_vertices; ++ i) { - const stl_vertex &v = this->stl.v_shared[i]; + pp.reserve(this->its.vertices.size()); + for (size_t i = 0; i < this->its.vertices.size(); ++ i) { + const stl_vertex &v = this->its.vertices[i]; pp.emplace_back(Point::new_scale(v(0), v(1))); } return Slic3r::Geometry::convex_hull(pp); @@ -517,49 +522,47 @@ BoundingBoxf3 TriangleMesh::bounding_box() const BoundingBoxf3 TriangleMesh::transformed_bounding_box(const Transform3d &trafo) const { BoundingBoxf3 bbox; - if (stl.v_shared == nullptr) { + if (this->its.vertices.empty()) { // Using the STL faces. - for (size_t i = 0; i < this->facets_count(); ++ i) { - const stl_facet &facet = this->stl.facet_start[i]; + for (const stl_facet &facet : this->stl.facet_start) for (size_t j = 0; j < 3; ++ j) bbox.merge(trafo * facet.vertex[j].cast()); - } } else { // Using the shared vertices should be a bit quicker than using the STL faces. - for (int i = 0; i < stl.stats.shared_vertices; ++ i) - bbox.merge(trafo * this->stl.v_shared[i].cast()); + for (const stl_vertex &v : this->its.vertices) + bbox.merge(trafo * v.cast()); } return bbox; } TriangleMesh TriangleMesh::convex_hull_3d() const { - // Helper struct for qhull: - struct PointForQHull{ - PointForQHull(float x_p, float y_p, float z_p) : x((realT)x_p), y((realT)y_p), z((realT)z_p) {} - realT x, y, z; - }; - std::vector src_vertices; - - // We will now fill the vector with input points for computation: - stl_facet* facet_ptr = stl.facet_start; - while (facet_ptr < stl.facet_start + stl.stats.number_of_facets) - { - for (int i = 0; i < 3; ++i) - { - const stl_vertex& v = facet_ptr->vertex[i]; - src_vertices.emplace_back(v(0), v(1), v(2)); - } - - facet_ptr += 1; - } - // The qhull call: orgQhull::Qhull qhull; qhull.disableOutputStream(); // we want qhull to be quiet - try + std::vector src_vertices; + try { - qhull.runQhull("", 3, (int)src_vertices.size(), (const realT*)(src_vertices.data()), "Qt"); + if (this->has_shared_vertices()) { +#if REALfloat + qhull.runQhull("", 3, (int)this->its.vertices.size(), (const realT*)(this->its.vertices.front().data()), "Qt"); +#else + src_vertices.reserve(this->its.vertices() * 3); + // We will now fill the vector with input points for computation: + for (const stl_vertex &v : ths->its.vertices.size()) + for (int i = 0; i < 3; ++ i) + src_vertices.emplace_back(v(i)); + qhull.runQhull("", 3, (int)src_vertices.size() / 3, src_vertices.data(), "Qt"); +#endif + } else { + src_vertices.reserve(this->stl.facet_start.size() * 9); + // We will now fill the vector with input points for computation: + for (const stl_facet &f : this->stl.facet_start) + for (int i = 0; i < 3; ++ i) + for (int j = 0; j < 3; ++ j) + src_vertices.emplace_back(f.vertex[i](j)); + qhull.runQhull("", 3, (int)src_vertices.size() / 3, src_vertices.data(), "Qt"); + } } catch (...) { @@ -578,7 +581,7 @@ TriangleMesh TriangleMesh::convex_hull_3d() const { // iterate through facet's vertices orgQhull::QhullPoint p = vertices[i].point(); - const float* coords = p.coordinates(); + const auto* coords = p.coordinates(); dst_vertices.emplace_back(coords[0], coords[1], coords[2]); } unsigned int size = (unsigned int)dst_vertices.size(); @@ -587,34 +590,20 @@ TriangleMesh TriangleMesh::convex_hull_3d() const TriangleMesh output_mesh(dst_vertices, facets); output_mesh.repair(); - output_mesh.require_shared_vertices(); return output_mesh; } void TriangleMesh::require_shared_vertices() { BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::require_shared_vertices - start"; - if (!this->repaired) + assert(stl_validate(&this->stl)); + if (! this->repaired) this->repair(); - if (this->stl.v_shared == NULL) { + if (this->its.vertices.empty()) { BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::require_shared_vertices - stl_generate_shared_vertices"; - stl_generate_shared_vertices(&(this->stl)); + stl_generate_shared_vertices(&this->stl, this->its); } -#ifdef _DEBUG - // Verify validity of neighborship data. - for (int facet_idx = 0; facet_idx < stl.stats.number_of_facets; ++facet_idx) { - const stl_neighbors &nbr = stl.neighbors_start[facet_idx]; - const int *vertices = stl.v_indices[facet_idx].vertex; - for (int nbr_idx = 0; nbr_idx < 3; ++nbr_idx) { - int nbr_face = this->stl.neighbors_start[facet_idx].neighbor[nbr_idx]; - if (nbr_face != -1) { - assert( - (stl.v_indices[nbr_face].vertex[(nbr.which_vertex_not[nbr_idx] + 1) % 3] == vertices[(nbr_idx + 1) % 3] && stl.v_indices[nbr_face].vertex[(nbr.which_vertex_not[nbr_idx] + 2) % 3] == vertices[nbr_idx]) || - (stl.v_indices[nbr_face].vertex[(nbr.which_vertex_not[nbr_idx] + 2) % 3] == vertices[(nbr_idx + 1) % 3] && stl.v_indices[nbr_face].vertex[(nbr.which_vertex_not[nbr_idx] + 1) % 3] == vertices[nbr_idx])); - } - } - } -#endif /* _DEBUG */ + assert(stl_validate(&this->stl, this->its)); BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::require_shared_vertices - end"; } @@ -626,10 +615,9 @@ void TriangleMeshSlicer::init(const TriangleMesh *_mesh, throw_on_cancel_callbac throw_on_cancel(); facets_edges.assign(_mesh->stl.stats.number_of_facets * 3, -1); - v_scaled_shared.assign(_mesh->stl.v_shared, _mesh->stl.v_shared + _mesh->stl.stats.shared_vertices); - // Scale the copied vertices. - for (int i = 0; i < this->mesh->stl.stats.shared_vertices; ++ i) - this->v_scaled_shared[i] *= float(1. / SCALING_FACTOR); + v_scaled_shared.assign(_mesh->its.vertices.size(), stl_vertex()); + for (size_t i = 0; i < v_scaled_shared.size(); ++ i) + this->v_scaled_shared[i] = _mesh->its.vertices[i] / float(SCALING_FACTOR); // Create a mapping from triangle edge into face. struct EdgeToFace { @@ -649,8 +637,8 @@ void TriangleMeshSlicer::init(const TriangleMesh *_mesh, throw_on_cancel_callbac for (uint32_t facet_idx = 0; facet_idx < this->mesh->stl.stats.number_of_facets; ++ facet_idx) for (int i = 0; i < 3; ++ i) { EdgeToFace &e2f = edges_map[facet_idx*3+i]; - e2f.vertex_low = this->mesh->stl.v_indices[facet_idx].vertex[i]; - e2f.vertex_high = this->mesh->stl.v_indices[facet_idx].vertex[(i + 1) % 3]; + e2f.vertex_low = this->mesh->its.indices[facet_idx][i]; + e2f.vertex_high = this->mesh->its.indices[facet_idx][(i + 1) % 3]; e2f.face = facet_idx; // 1 based indexing, to be always strictly positive. e2f.face_edge = i + 1; @@ -818,7 +806,7 @@ void TriangleMeshSlicer::slice(const std::vector &z, std::vector* lines, boost::mutex* lines_mutex, const std::vector &z) const { - const stl_facet &facet = m_use_quaternion ? this->mesh->stl.facet_start[facet_idx].rotated(m_quaternion) : this->mesh->stl.facet_start[facet_idx]; + const stl_facet &facet = m_use_quaternion ? (this->mesh->stl.facet_start.data() + facet_idx)->rotated(m_quaternion) : *(this->mesh->stl.facet_start.data() + facet_idx); // find facet extents const float min_z = fminf(facet.vertex[0](2), fminf(facet.vertex[1](2), facet.vertex[2](2))); @@ -887,7 +875,7 @@ TriangleMeshSlicer::FacetSliceType TriangleMeshSlicer::slice_facet( // Reorder vertices so that the first one is the one with lowest Z. // This is needed to get all intersection lines in a consistent order // (external on the right of the line) - const int *vertices = this->mesh->stl.v_indices[facet_idx].vertex; + const stl_triangle_vertex_indices &vertices = this->mesh->its.indices[facet_idx]; int i = (facet.vertex[1].z() == min_z) ? 1 : ((facet.vertex[2].z() == min_z) ? 2 : 0); // These are used only if the cut plane is tilted: @@ -1714,7 +1702,7 @@ void TriangleMeshSlicer::cut(float z, TriangleMesh* upper, TriangleMesh* lower) BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::cut - slicing object"; float scaled_z = scale_(z); for (uint32_t facet_idx = 0; facet_idx < this->mesh->stl.stats.number_of_facets; ++ facet_idx) { - stl_facet* facet = &this->mesh->stl.facet_start[facet_idx]; + const stl_facet* facet = &this->mesh->stl.facet_start[facet_idx]; // find facet extents float min_z = std::min(facet->vertex[0](2), std::min(facet->vertex[1](2), facet->vertex[2](2))); @@ -1736,10 +1724,12 @@ void TriangleMeshSlicer::cut(float z, TriangleMesh* upper, TriangleMesh* lower) if (min_z > z || (min_z == z && max_z > z)) { // facet is above the cut plane and does not belong to it - if (upper != NULL) stl_add_facet(&upper->stl, facet); + if (upper != nullptr) + stl_add_facet(&upper->stl, facet); } else if (max_z < z || (max_z == z && min_z < z)) { // facet is below the cut plane and does not belong to it - if (lower != NULL) stl_add_facet(&lower->stl, facet); + if (lower != nullptr) + stl_add_facet(&lower->stl, facet); } else if (min_z < z && max_z > z) { // Facet is cut by the slicing plane. @@ -1786,22 +1776,24 @@ void TriangleMeshSlicer::cut(float z, TriangleMesh* upper, TriangleMesh* lower) quadrilateral[1].vertex[2] = v0v1; if (v0(2) > z) { - if (upper != NULL) stl_add_facet(&upper->stl, &triangle); - if (lower != NULL) { + if (upper != nullptr) + stl_add_facet(&upper->stl, &triangle); + if (lower != nullptr) { stl_add_facet(&lower->stl, &quadrilateral[0]); stl_add_facet(&lower->stl, &quadrilateral[1]); } } else { - if (upper != NULL) { + if (upper != nullptr) { stl_add_facet(&upper->stl, &quadrilateral[0]); stl_add_facet(&upper->stl, &quadrilateral[1]); } - if (lower != NULL) stl_add_facet(&lower->stl, &triangle); + if (lower != nullptr) + stl_add_facet(&lower->stl, &triangle); } } } - if (upper != NULL) { + if (upper != nullptr) { BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::cut - triangulating upper part"; ExPolygons section; this->make_expolygons_simple(upper_lines, §ion); @@ -1815,7 +1807,7 @@ void TriangleMeshSlicer::cut(float z, TriangleMesh* upper, TriangleMesh* lower) } } - if (lower != NULL) { + if (lower != nullptr) { BOOST_LOG_TRIVIAL(trace) << "TriangleMeshSlicer::cut - triangulating lower part"; ExPolygons section; this->make_expolygons_simple(lower_lines, §ion); @@ -1905,10 +1897,10 @@ TriangleMesh make_cylinder(double r, double h, double fa) //FIXME better to discretize an Icosahedron recursively http://www.songho.ca/opengl/gl_sphere.html TriangleMesh make_sphere(double radius, double fa) { - int sectorCount = ceil(2. * M_PI / fa); - int stackCount = ceil(M_PI / fa); - float sectorStep = 2. * M_PI / sectorCount; - float stackStep = M_PI / stackCount; + int sectorCount = int(ceil(2. * M_PI / fa)); + int stackCount = int(ceil(M_PI / fa)); + float sectorStep = float(2. * M_PI / sectorCount); + float stackStep = float(M_PI / stackCount); Pointf3s vertices; vertices.reserve((stackCount - 1) * sectorCount + 2); diff --git a/src/libslic3r/TriangleMesh.hpp b/src/libslic3r/TriangleMesh.hpp index c284f6482b..054a98935a 100644 --- a/src/libslic3r/TriangleMesh.hpp +++ b/src/libslic3r/TriangleMesh.hpp @@ -21,19 +21,13 @@ typedef std::vector TriangleMeshPtrs; class TriangleMesh { public: - TriangleMesh() : repaired(false) { stl_initialize(&this->stl); } + TriangleMesh() : repaired(false) {} TriangleMesh(const Pointf3s &points, const std::vector &facets); - TriangleMesh(const TriangleMesh &other) : repaired(false) { stl_initialize(&this->stl); *this = other; } - TriangleMesh(TriangleMesh &&other) : repaired(false) { stl_initialize(&this->stl); this->swap(other); } - ~TriangleMesh() { clear(); } - TriangleMesh& operator=(const TriangleMesh &other); - TriangleMesh& operator=(TriangleMesh &&other) { this->swap(other); return *this; } - void clear() { stl_close(&this->stl); this->repaired = false; } - void swap(TriangleMesh &other) { std::swap(this->stl, other.stl); std::swap(this->repaired, other.repaired); } - void ReadSTLFile(const char* input_file) { stl_open(&stl, input_file); } - void write_ascii(const char* output_file) { stl_write_ascii(&this->stl, output_file, ""); } - void write_binary(const char* output_file) { stl_write_binary(&this->stl, output_file, ""); } - void repair(); + void clear() { this->stl.clear(); this->its.clear(); this->repaired = false; } + bool ReadSTLFile(const char* input_file) { return stl_open(&stl, input_file); } + bool write_ascii(const char* output_file) { return stl_write_ascii(&this->stl, output_file, ""); } + bool write_binary(const char* output_file) { return stl_write_binary(&this->stl, output_file, ""); } + void repair(bool update_shared_vertices = true); float volume(); void check_topology(); bool is_manifold() const { return this->stl.stats.connected_facets_3_edge == (int)this->stl.stats.number_of_facets; } @@ -58,7 +52,7 @@ public: TriangleMeshPtrs split() const; void merge(const TriangleMesh &mesh); ExPolygons horizontal_projection() const; - const float* first_vertex() const { return this->stl.facet_start ? &this->stl.facet_start->vertex[0](0) : nullptr; } + const float* first_vertex() const { return this->stl.facet_start.empty() ? nullptr : &this->stl.facet_start.front().vertex[0](0); } // 2D convex hull of a 3D mesh projected into the Z=0 plane. Polygon convex_hull(); BoundingBoxf3 bounding_box() const; @@ -69,12 +63,13 @@ public: void reset_repair_stats(); bool needed_repair() const; void require_shared_vertices(); - bool has_shared_vertices() const { return stl.v_shared != NULL; } + bool has_shared_vertices() const { return ! this->its.vertices.empty(); } size_t facets_count() const { return this->stl.stats.number_of_facets; } bool empty() const { return this->facets_count() == 0; } bool is_splittable() const; stl_file stl; + indexed_triangle_set its; bool repaired; private: diff --git a/src/libslic3r/libslic3r.h b/src/libslic3r/libslic3r.h index 560d746962..8cafae17cf 100644 --- a/src/libslic3r/libslic3r.h +++ b/src/libslic3r/libslic3r.h @@ -48,10 +48,33 @@ typedef double coordf_t; //FIXME Better to use an inline function with an explicit return type. //inline coord_t scale_(coordf_t v) { return coord_t(floor(v / SCALING_FACTOR + 0.5f)); } #define scale_(val) ((val) / SCALING_FACTOR) + #define SCALED_EPSILON scale_(EPSILON) #define SLIC3R_DEBUG_OUT_PATH_PREFIX "out/" +#if defined(_MSC_VER) && _MSC_VER < 1900 +# define SLIC3R_CONSTEXPR +# define SLIC3R_NOEXCEPT +#else +#define SLIC3R_CONSTEXPR constexpr +#define SLIC3R_NOEXCEPT noexcept +#endif + +template inline SLIC3R_CONSTEXPR coord_t scaled(Tf val) +{ + static_assert (std::is_floating_point::value, "Floating point only"); + return coord_t(val / Tf(SCALING_FACTOR)); +} + +template inline SLIC3R_CONSTEXPR Tf unscaled(coord_t val) +{ + static_assert (std::is_floating_point::value, "Floating point only"); + return Tf(val * Tf(SCALING_FACTOR)); +} + +inline SLIC3R_CONSTEXPR float unscaledf(coord_t val) { return unscaled(val); } + inline std::string debug_out_path(const char *name, ...) { char buffer[2048]; diff --git a/src/qhull/CMakeLists.txt b/src/qhull/CMakeLists.txt index a3e7da1264..9ca0bdff23 100644 --- a/src/qhull/CMakeLists.txt +++ b/src/qhull/CMakeLists.txt @@ -8,6 +8,22 @@ # Created by modification of the original qhull CMakeLists. # Lukas Matena (25.7.2018), lukasmatena@seznam.cz +# see bug report: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=925540 + +find_package(Qhull 7.2 QUIET) + +add_library(qhull INTERFACE) + +if(Qhull_FOUND) + +message(STATUS "Using qhull from system.") +if(SLIC3R_STATIC) + target_link_libraries(qhull INTERFACE Qhull::qhullcpp Qhull::qhullstatic_r) +else() + target_link_libraries(qhull INTERFACE Qhull::qhullcpp Qhull::qhull_r) +endif() + +else(Qhull_FOUND) project(qhull) cmake_minimum_required(VERSION 2.6) @@ -112,7 +128,7 @@ set(libqhull_SOURCES ################################################## # combined library (reentrant qhull and qhullcpp) for Slic3r: -set(qhull_STATIC qhull) +set(qhull_STATIC qhullstatic) add_library(${qhull_STATIC} STATIC ${libqhull_SOURCES}) set_target_properties(${qhull_STATIC} PROPERTIES VERSION ${qhull_VERSION}) @@ -123,4 +139,7 @@ endif(UNIX) ################################################## # LIBDIR is defined in the main xs CMake file: -target_include_directories(${qhull_STATIC} PRIVATE ${LIBDIR}/qhull/src) +target_include_directories(${qhull_STATIC} BEFORE PUBLIC ${LIBDIR}/qhull/src) +target_link_libraries(qhull INTERFACE ${qhull_STATIC}) + +endif() diff --git a/src/slic3r/Config/Version.cpp b/src/slic3r/Config/Version.cpp index 2eda135d6e..fe3adfd7f1 100644 --- a/src/slic3r/Config/Version.cpp +++ b/src/slic3r/Config/Version.cpp @@ -198,6 +198,11 @@ size_t Index::load(const boost::filesystem::path &path) size_t idx_line = 0; Version ver; while (std::getline(ifs, line)) { +#ifndef _MSVCVER + // On a Unix system, getline does not remove the trailing carriage returns, if the index is shared over a Windows filesystem. Remove them manually. + while (! line.empty() && line.back() == '\r') + line.pop_back(); +#endif ++ idx_line; // Skip the initial white spaces. char *key = left_trim(const_cast(line.data())); diff --git a/src/slic3r/GUI/2DBed.cpp b/src/slic3r/GUI/2DBed.cpp index ce20124612..a339f3e66a 100644 --- a/src/slic3r/GUI/2DBed.cpp +++ b/src/slic3r/GUI/2DBed.cpp @@ -19,8 +19,6 @@ wxPanel(parent, wxID_ANY, wxDefaultPosition, wxSize(25 * wxGetApp().em_unit(), - m_user_drawn_background = false; #endif /*__APPLE__*/ Bind(wxEVT_PAINT, ([this](wxPaintEvent &/* e */) { repaint(); })); - Bind(wxEVT_LEFT_DOWN, ([this](wxMouseEvent &event) { mouse_event(event); })); - Bind(wxEVT_MOTION, ([this](wxMouseEvent &event) { mouse_event(event); })); Bind(wxEVT_SIZE, ([this](wxSizeEvent & /* e */) { Refresh(); })); } void Bed_2D::repaint() @@ -43,22 +41,14 @@ void Bed_2D::repaint() dc.DrawRectangle(rect.GetLeft(), rect.GetTop(), rect.GetWidth(), rect.GetHeight()); } - // turn cw and ch from sizes to max coordinates - cw--; - ch--; + if (m_bed_shape.empty()) + return; + + // reduce size to have some space around the drawn shape + cw -= (2 * Border); + ch -= (2 * Border); auto cbb = BoundingBoxf(Vec2d(0, 0),Vec2d(cw, ch)); - // leave space for origin point - cbb.min(0) += 4; - cbb.max -= Vec2d(4., 4.); - - // leave space for origin label - cbb.max(1) -= 13; - - // read new size - cw = cbb.size()(0); - ch = cbb.size()(1); - auto ccenter = cbb.center(); // get bounding box of bed shape in G - code coordinates @@ -76,17 +66,17 @@ void Bed_2D::repaint() ccenter(0) - bcenter(0) * sfactor, ccenter(1) - bcenter(1) * sfactor ); + m_scale_factor = sfactor; - m_shift = Vec2d(shift(0) + cbb.min(0), - shift(1) - (cbb.max(1) - GetSize().GetHeight())); + m_shift = Vec2d(shift(0) + cbb.min(0), shift(1) - (cbb.max(1) - ch)); // draw bed fill dc.SetBrush(wxBrush(wxColour(255, 255, 255), wxBRUSHSTYLE_SOLID)); wxPointList pt_list; for (auto pt: m_bed_shape) { - Point pt_pix = to_pixels(pt); - pt_list.push_back(new wxPoint(pt_pix(0), pt_pix(1))); + Point pt_pix = to_pixels(pt, ch); + pt_list.push_back(new wxPoint(pt_pix(0), pt_pix(1))); } dc.DrawPolygon(&pt_list, 0, 0); @@ -105,9 +95,9 @@ void Bed_2D::repaint() for (auto pl : polylines) { for (size_t i = 0; i < pl.points.size()-1; i++) { - Point pt1 = to_pixels(unscale(pl.points[i])); - Point pt2 = to_pixels(unscale(pl.points[i+1])); - dc.DrawLine(pt1(0), pt1(1), pt2(0), pt2(1)); + Point pt1 = to_pixels(unscale(pl.points[i]), ch); + Point pt2 = to_pixels(unscale(pl.points[i + 1]), ch); + dc.DrawLine(pt1(0), pt1(1), pt2(0), pt2(1)); } } @@ -116,7 +106,7 @@ void Bed_2D::repaint() dc.SetBrush(wxBrush(wxColour(0, 0, 0), wxBRUSHSTYLE_TRANSPARENT)); dc.DrawPolygon(&pt_list, 0, 0); - auto origin_px = to_pixels(Vec2d(0, 0)); + auto origin_px = to_pixels(Vec2d(0, 0), ch); // draw axes auto axes_len = 50; @@ -153,7 +143,7 @@ void Bed_2D::repaint() // draw current position if (m_pos!= Vec2d(0, 0)) { - auto pos_px = to_pixels(m_pos); + auto pos_px = to_pixels(m_pos, ch); dc.SetPen(wxPen(wxColour(200, 0, 0), 2, wxPENSTYLE_SOLID)); dc.SetBrush(wxBrush(wxColour(200, 0, 0), wxBRUSHSTYLE_TRANSPARENT)); dc.DrawCircle(pos_px(0), pos_px(1), 5); @@ -161,35 +151,14 @@ void Bed_2D::repaint() dc.DrawLine(pos_px(0) - 15, pos_px(1), pos_px(0) + 15, pos_px(1)); dc.DrawLine(pos_px(0), pos_px(1) - 15, pos_px(0), pos_px(1) + 15); } - - m_painted = true; } + // convert G - code coordinates into pixels -Point Bed_2D::to_pixels(Vec2d point) +Point Bed_2D::to_pixels(Vec2d point, int height) { auto p = point * m_scale_factor + m_shift; - return Point(p(0), GetSize().GetHeight() - p(1)); -} - -void Bed_2D::mouse_event(wxMouseEvent event) -{ - if (!m_interactive) return; - if (!m_painted) return; - - auto pos = event.GetPosition(); - auto point = to_units(Point(pos.x, pos.y)); - if (event.LeftDown() || event.Dragging()) { - if (m_on_move) - m_on_move(point) ; - Refresh(); - } -} - -// convert pixels into G - code coordinates -Vec2d Bed_2D::to_units(Point point) -{ - return (Vec2d(point(0), GetSize().GetHeight() - point(1)) - m_shift) * (1. / m_scale_factor); + return Point(p(0) + Border, height - p(1) + Border); } void Bed_2D::set_pos(Vec2d pos) diff --git a/src/slic3r/GUI/2DBed.hpp b/src/slic3r/GUI/2DBed.hpp index 579ef44458..a61fb313d4 100644 --- a/src/slic3r/GUI/2DBed.hpp +++ b/src/slic3r/GUI/2DBed.hpp @@ -9,20 +9,17 @@ namespace GUI { class Bed_2D : public wxPanel { + static const int Border = 10; + bool m_user_drawn_background = true; - bool m_painted = false; - bool m_interactive = false; - double m_scale_factor; + double m_scale_factor; Vec2d m_shift = Vec2d::Zero(); Vec2d m_pos = Vec2d::Zero(); - std::function m_on_move = nullptr; - Point to_pixels(Vec2d point); - Vec2d to_units(Point point); - void repaint(); - void mouse_event(wxMouseEvent event); - void set_pos(Vec2d pos); + Point to_pixels(Vec2d point, int height); + void repaint(); + void set_pos(Vec2d pos); public: Bed_2D(wxWindow* parent); diff --git a/src/slic3r/GUI/3DScene.cpp b/src/slic3r/GUI/3DScene.cpp index 37e022329c..2e30f00cae 100644 --- a/src/slic3r/GUI/3DScene.cpp +++ b/src/slic3r/GUI/3DScene.cpp @@ -241,8 +241,6 @@ GLVolume::GLVolume(float r, float g, float b, float a) : m_transformed_bounding_box_dirty(true) , m_sla_shift_z(0.0) , m_transformed_convex_hull_bounding_box_dirty(true) - , m_convex_hull(nullptr) - , m_convex_hull_owned(false) // geometry_id == 0 -> invalid , geometry_id(std::pair(0, 0)) , extruder_id(0) @@ -268,12 +266,6 @@ GLVolume::GLVolume(float r, float g, float b, float a) set_render_color(r, g, b, a); } -GLVolume::~GLVolume() -{ - if (m_convex_hull_owned) - delete m_convex_hull; -} - void GLVolume::set_render_color(float r, float g, float b, float a) { render_color[0] = r; @@ -335,12 +327,6 @@ void GLVolume::set_color_from_model_volume(const ModelVolume *model_volume) color[3] = model_volume->is_model_part() ? 1.f : 0.5f; } -void GLVolume::set_convex_hull(const TriangleMesh *convex_hull, bool owned) -{ - m_convex_hull = convex_hull; - m_convex_hull_owned = owned; -} - Transform3d GLVolume::world_matrix() const { Transform3d m = m_instance_transformation.get_matrix() * m_volume_transformation.get_matrix(); @@ -377,7 +363,7 @@ const BoundingBoxf3& GLVolume::transformed_convex_hull_bounding_box() const BoundingBoxf3 GLVolume::transformed_convex_hull_bounding_box(const Transform3d &trafo) const { - return (m_convex_hull != nullptr && m_convex_hull->stl.stats.number_of_facets > 0) ? + return (m_convex_hull && m_convex_hull->stl.stats.number_of_facets > 0) ? m_convex_hull->transformed_bounding_box(trafo) : bounding_box.transformed(trafo); } @@ -587,7 +573,7 @@ int GLVolumeCollection::load_object_volume( const ModelVolume *model_volume = model_object->volumes[volume_idx]; const int extruder_id = model_volume->extruder_id(); const ModelInstance *instance = model_object->instances[instance_idx]; - const TriangleMesh& mesh = model_volume->mesh; + const TriangleMesh& mesh = model_volume->mesh(); float color[4]; memcpy(color, GLVolume::MODEL_COLOR[((color_by == "volume") ? volume_idx : obj_idx) % 4], sizeof(float) * 3); /* if (model_volume->is_support_blocker()) { @@ -613,7 +599,7 @@ int GLVolumeCollection::load_object_volume( if (model_volume->is_model_part()) { // GLVolume will reference a convex hull from model_volume! - v.set_convex_hull(&model_volume->get_convex_hull(), false); + v.set_convex_hull(model_volume->get_convex_hull_shared_ptr()); if (extruder_id != -1) v.extruder_id = extruder_id; } @@ -656,7 +642,10 @@ void GLVolumeCollection::load_object_auxiliary( v.composite_id = GLVolume::CompositeID(obj_idx, - int(milestone), (int)instance_idx.first); v.geometry_id = std::pair(timestamp, model_instance.id().id); // Create a copy of the convex hull mesh for each instance. Use a move operator on the last instance. - v.set_convex_hull((&instance_idx == &instances.back()) ? new TriangleMesh(std::move(convex_hull)) : new TriangleMesh(convex_hull), true); + if (&instance_idx == &instances.back()) + v.set_convex_hull(std::move(convex_hull)); + else + v.set_convex_hull(convex_hull); v.is_modifier = false; v.shader_outside_printer_detection_enabled = (milestone == slaposSupportTree); v.set_instance_transformation(model_instance.get_transformation()); diff --git a/src/slic3r/GUI/3DScene.hpp b/src/slic3r/GUI/3DScene.hpp index 2ca11073be..0414a1ed3c 100644 --- a/src/slic3r/GUI/3DScene.hpp +++ b/src/slic3r/GUI/3DScene.hpp @@ -10,6 +10,7 @@ #include "slic3r/GUI/GLCanvas3DManager.hpp" #include +#include #ifndef NDEBUG #define HAS_GLSAFE @@ -243,7 +244,6 @@ public: GLVolume(float r = 1.f, float g = 1.f, float b = 1.f, float a = 1.f); GLVolume(const float *rgba) : GLVolume(rgba[0], rgba[1], rgba[2], rgba[3]) {} - ~GLVolume(); private: Geometry::Transformation m_instance_transformation; @@ -255,10 +255,8 @@ private: mutable BoundingBoxf3 m_transformed_bounding_box; // Whether or not is needed to recalculate the transformed bounding box. mutable bool m_transformed_bounding_box_dirty; - // Pointer to convex hull of the original mesh, if any. - // This object may or may not own the convex hull instance based on m_convex_hull_owned - const TriangleMesh* m_convex_hull; - bool m_convex_hull_owned; + // Convex hull of the volume, if any. + std::shared_ptr m_convex_hull; // Bounding box of this volume, in unscaled coordinates. mutable BoundingBoxf3 m_transformed_convex_hull_bounding_box; // Whether or not is needed to recalculate the transformed convex hull bounding box. @@ -395,7 +393,9 @@ public: double get_sla_shift_z() const { return m_sla_shift_z; } void set_sla_shift_z(double z) { m_sla_shift_z = z; } - void set_convex_hull(const TriangleMesh *convex_hull, bool owned); + void set_convex_hull(std::shared_ptr convex_hull) { m_convex_hull = std::move(convex_hull); } + void set_convex_hull(const TriangleMesh &convex_hull) { m_convex_hull = std::make_shared(convex_hull); } + void set_convex_hull(TriangleMesh &&convex_hull) { m_convex_hull = std::make_shared(std::move(convex_hull)); } int object_idx() const { return this->composite_id.object_id; } int volume_idx() const { return this->composite_id.volume_id; } diff --git a/src/slic3r/GUI/BackgroundSlicingProcess.cpp b/src/slic3r/GUI/BackgroundSlicingProcess.cpp index 94fb6481b7..b77a272e2e 100644 --- a/src/slic3r/GUI/BackgroundSlicingProcess.cpp +++ b/src/slic3r/GUI/BackgroundSlicingProcess.cpp @@ -89,7 +89,7 @@ void BackgroundSlicingProcess::process_fff() // Perform the final post-processing of the export path by applying the print statistics over the file name. std::string export_path = m_fff_print->print_statistics().finalize_output_path(m_export_path); if (copy_file(m_temp_output_path, export_path) != 0) - throw std::runtime_error(_utf8(L("Copying of the temporary G-code to the output G-code failed"))); + throw std::runtime_error(_utf8(L("Copying of the temporary G-code to the output G-code failed. Maybe the SD card is write locked?"))); m_print->set_status(95, _utf8(L("Running post-processing scripts"))); run_post_process_scripts(export_path, m_fff_print->config()); m_print->set_status(100, (boost::format(_utf8(L("G-code file exported to %1%"))) % export_path).str()); diff --git a/src/slic3r/GUI/BedShapeDialog.cpp b/src/slic3r/GUI/BedShapeDialog.cpp index d471a46c90..cec0f50677 100644 --- a/src/slic3r/GUI/BedShapeDialog.cpp +++ b/src/slic3r/GUI/BedShapeDialog.cpp @@ -30,11 +30,9 @@ void BedShapeDialog::build_dialog(ConfigOptionPoints* default_pt) SetMinSize(GetSize()); main_sizer->SetSizeHints(this); - // needed to actually free memory - this->Bind(wxEVT_CLOSE_WINDOW, ([this](wxCloseEvent e) { - EndModal(wxID_OK); - Destroy(); - })); + this->Bind(wxEVT_CLOSE_WINDOW, ([this](wxCloseEvent& evt) { + EndModal(wxID_CANCEL); + })); } void BedShapeDialog::on_dpi_changed(const wxRect &suggested_rect) @@ -135,7 +133,7 @@ void BedShapePanel::build_panel(ConfigOptionPoints* default_pt) // Called from the constructor. // Create a panel for a rectangular / circular / custom bed shape. -ConfigOptionsGroupShp BedShapePanel::init_shape_options_page(wxString title) +ConfigOptionsGroupShp BedShapePanel::init_shape_options_page(const wxString& title) { auto panel = new wxPanel(m_shape_options_book); @@ -305,8 +303,9 @@ void BedShapePanel::update_shape() } m_canvas->m_bed_shape = points; } + else if (page_idx == SHAPE_CUSTOM) + m_canvas->m_bed_shape = m_loaded_bed_shape; -// $self->{on_change}->(); update_preview(); } @@ -351,8 +350,9 @@ void BedShapePanel::load_stl() std::vector points; for (auto pt : polygon.points) points.push_back(unscale(pt)); - m_canvas->m_bed_shape = points; - update_preview(); + + m_loaded_bed_shape = points; + update_shape(); } } // GUI diff --git a/src/slic3r/GUI/BedShapeDialog.hpp b/src/slic3r/GUI/BedShapeDialog.hpp index 72e50a05d5..6600a1c84d 100644 --- a/src/slic3r/GUI/BedShapeDialog.hpp +++ b/src/slic3r/GUI/BedShapeDialog.hpp @@ -16,7 +16,8 @@ namespace GUI { using ConfigOptionsGroupShp = std::shared_ptr; class BedShapePanel : public wxPanel { - Bed_2D* m_canvas; + Bed_2D* m_canvas; + std::vector m_loaded_bed_shape; public: BedShapePanel(wxWindow* parent) : wxPanel(parent, wxID_ANY) {} @@ -24,8 +25,8 @@ public: void build_panel(ConfigOptionPoints* default_pt); - ConfigOptionsGroupShp init_shape_options_page(wxString title); - void set_shape(ConfigOptionPoints* points); + ConfigOptionsGroupShp init_shape_options_page(const wxString& title); + void set_shape(ConfigOptionPoints* points); void update_preview(); void update_shape(); void load_stl(); diff --git a/src/slic3r/GUI/Camera.hpp b/src/slic3r/GUI/Camera.hpp index 6e1b539ab6..1c75ef4b69 100644 --- a/src/slic3r/GUI/Camera.hpp +++ b/src/slic3r/GUI/Camera.hpp @@ -56,9 +56,9 @@ public: Vec3d get_dir_right() const { return m_view_matrix.matrix().block(0, 0, 3, 3).row(0); } Vec3d get_dir_up() const { return m_view_matrix.matrix().block(0, 0, 3, 3).row(1); } - Vec3d get_dir_forward() const { return m_view_matrix.matrix().block(0, 0, 3, 3).row(2); } + Vec3d get_dir_forward() const { return -m_view_matrix.matrix().block(0, 0, 3, 3).row(2); } - Vec3d get_position() const { return m_view_matrix.matrix().block(0, 3, 3, 1); } + Vec3d get_position() const { return m_view_matrix.matrix().inverse().block(0, 3, 3, 1); } void apply_viewport(int x, int y, unsigned int w, unsigned int h) const; void apply_view_matrix() const; diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 90c21608e4..a71c030967 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -17,6 +17,7 @@ #include "slic3r/GUI/GUI.hpp" #include "slic3r/GUI/PresetBundle.hpp" #include "slic3r/GUI/Tab.hpp" +#include "slic3r/GUI/GUI_Preview.hpp" #include "GUI_App.hpp" #include "GUI_ObjectList.hpp" #include "GUI_ObjectManipulation.hpp" @@ -63,11 +64,6 @@ static const float GROUND_Z = -0.02f; static const float GIZMO_RESET_BUTTON_HEIGHT = 22.0f; static const float GIZMO_RESET_BUTTON_WIDTH = 70.f; -static const float UNIT_MATRIX[] = { 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f }; - static const float DEFAULT_BG_DARK_COLOR[3] = { 0.478f, 0.478f, 0.478f }; static const float DEFAULT_BG_LIGHT_COLOR[3] = { 0.753f, 0.753f, 0.753f }; static const float ERROR_BG_DARK_COLOR[3] = { 0.478f, 0.192f, 0.039f }; @@ -460,8 +456,7 @@ void GLCanvas3D::LayersEditing::_render_active_object_annotations(const GLCanvas m_shader.set_uniform("z_texture_row_to_normalized", 1.0f / (float)m_layers_texture.height); m_shader.set_uniform("z_cursor", m_object_max_z * this->get_cursor_z_relative(canvas)); m_shader.set_uniform("z_cursor_band_width", band_width); - // The shader requires the original model coordinates when rendering to the texture, so we pass it the unit matrix - m_shader.set_uniform("volume_world_matrix", UNIT_MATRIX); + m_shader.set_uniform("object_max_z", m_object_max_z); glsafe(::glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); glsafe(::glBindTexture(GL_TEXTURE_2D, m_z_texture_id)); @@ -474,10 +469,10 @@ void GLCanvas3D::LayersEditing::_render_active_object_annotations(const GLCanvas ::glBegin(GL_QUADS); ::glNormal3f(0.0f, 0.0f, 1.0f); - ::glVertex3f(l, b, 0.0f); - ::glVertex3f(r, b, 0.0f); - ::glVertex3f(r, t, m_object_max_z); - ::glVertex3f(l, t, m_object_max_z); + ::glTexCoord2f(0.0f, 0.0f); ::glVertex2f(l, b); + ::glTexCoord2f(1.0f, 0.0f); ::glVertex2f(r, b); + ::glTexCoord2f(1.0f, 1.0f); ::glVertex2f(r, t); + ::glTexCoord2f(0.0f, 1.0f); ::glVertex2f(l, t); glsafe(::glEnd()); glsafe(::glBindTexture(GL_TEXTURE_2D, 0)); @@ -530,6 +525,7 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G GLint z_cursor_id = ::glGetUniformLocation(shader_id, "z_cursor"); GLint z_cursor_band_width_id = ::glGetUniformLocation(shader_id, "z_cursor_band_width"); GLint world_matrix_id = ::glGetUniformLocation(shader_id, "volume_world_matrix"); + GLint object_max_z_id = ::glGetUniformLocation(shader_id, "object_max_z"); glcheck(); if (z_to_texture_row_id != -1 && z_texture_row_to_normalized_id != -1 && z_cursor_id != -1 && z_cursor_band_width_id != -1 && world_matrix_id != -1) @@ -556,7 +552,10 @@ void GLCanvas3D::LayersEditing::render_volumes(const GLCanvas3D& canvas, const G // Render the object using the layer editing shader and texture. if (! glvolume->is_active || glvolume->composite_id.object_id != this->last_object_id || glvolume->is_modifier) continue; - glsafe(::glUniformMatrix4fv(world_matrix_id, 1, GL_FALSE, (const GLfloat*)glvolume->world_matrix().cast().data())); + if (world_matrix_id != -1) + glsafe(::glUniformMatrix4fv(world_matrix_id, 1, GL_FALSE, (const GLfloat*)glvolume->world_matrix().cast().data())); + if (object_max_z_id != -1) + glsafe(::glUniform1f(object_max_z_id, GLfloat(0))); glvolume->render(); } // Revert back to the previous shader. @@ -1248,6 +1247,8 @@ wxDEFINE_EVENT(EVT_GLCANVAS_MOUSE_DRAGGING_FINISHED, SimpleEvent); wxDEFINE_EVENT(EVT_GLCANVAS_UPDATE_BED_SHAPE, SimpleEvent); wxDEFINE_EVENT(EVT_GLCANVAS_TAB, SimpleEvent); wxDEFINE_EVENT(EVT_GLCANVAS_RESETGIZMOS, SimpleEvent); +wxDEFINE_EVENT(EVT_GLCANVAS_MOVE_DOUBLE_SLIDER, wxKeyEvent); +wxDEFINE_EVENT(EVT_GLCANVAS_EDIT_COLOR_CHANGE, wxKeyEvent); GLCanvas3D::GLCanvas3D(wxGLCanvas* canvas, Bed3D& bed, Camera& camera, GLToolbar& view_toolbar) : m_canvas(canvas) @@ -1624,7 +1625,13 @@ void GLCanvas3D::update_volumes_colors_by_extruder() void GLCanvas3D::render() { - wxCHECK_RET(!m_in_render, "GLCanvas3D::render() called recursively"); + if (m_in_render) + { + // if called recursively, return + m_dirty = true; + return; + } + m_in_render = true; Slic3r::ScopeGuard in_render_guard([this]() { m_in_render = false; }); (void)in_render_guard; @@ -2443,8 +2450,18 @@ void GLCanvas3D::on_char(wxKeyEvent& evt) case '4': { select_view("rear"); break; } case '5': { select_view("left"); break; } case '6': { select_view("right"); break; } - case '+': { post_event(Event(EVT_GLCANVAS_INCREASE_INSTANCES, +1)); break; } - case '-': { post_event(Event(EVT_GLCANVAS_INCREASE_INSTANCES, -1)); break; } + case '+': { + if (dynamic_cast(m_canvas->GetParent()) != nullptr) + post_event(wxKeyEvent(EVT_GLCANVAS_EDIT_COLOR_CHANGE, evt)); + else + post_event(Event(EVT_GLCANVAS_INCREASE_INSTANCES, +1)); + break; } + case '-': { + if (dynamic_cast(m_canvas->GetParent()) != nullptr) + post_event(wxKeyEvent(EVT_GLCANVAS_EDIT_COLOR_CHANGE, evt)); + else + post_event(Event(EVT_GLCANVAS_INCREASE_INSTANCES, -1)); + break; } case '?': { post_event(SimpleEvent(EVT_GLCANVAS_QUESTION_MARK)); break; } case 'A': case 'a': { post_event(SimpleEvent(EVT_GLCANVAS_ARRANGE)); break; } @@ -2524,6 +2541,15 @@ void GLCanvas3D::on_key(wxKeyEvent& evt) } else if (keyCode == WXK_CONTROL) m_dirty = true; + // DoubleSlider navigation in Preview + else if (keyCode == WXK_LEFT || + keyCode == WXK_RIGHT || + keyCode == WXK_UP || + keyCode == WXK_DOWN ) + { + if (dynamic_cast(m_canvas->GetParent()) != nullptr) + post_event(wxKeyEvent(EVT_GLCANVAS_MOVE_DOUBLE_SLIDER, evt)); + } } } } @@ -5563,7 +5589,7 @@ void GLCanvas3D::_load_sla_shells() v.set_instance_offset(unscale(instance.shift(0), instance.shift(1), 0)); v.set_instance_rotation(Vec3d(0.0, 0.0, (double)instance.rotation)); v.set_instance_mirror(X, object.is_left_handed() ? -1. : 1.); - v.set_convex_hull(new TriangleMesh(std::move(mesh.convex_hull_3d())), true); + v.set_convex_hull(mesh.convex_hull_3d()); }; // adds objects' volumes diff --git a/src/slic3r/GUI/GLCanvas3D.hpp b/src/slic3r/GUI/GLCanvas3D.hpp index d7a4c6d22d..4b97124afb 100644 --- a/src/slic3r/GUI/GLCanvas3D.hpp +++ b/src/slic3r/GUI/GLCanvas3D.hpp @@ -124,6 +124,8 @@ wxDECLARE_EVENT(EVT_GLCANVAS_MOUSE_DRAGGING_FINISHED, SimpleEvent); wxDECLARE_EVENT(EVT_GLCANVAS_UPDATE_BED_SHAPE, SimpleEvent); wxDECLARE_EVENT(EVT_GLCANVAS_TAB, SimpleEvent); wxDECLARE_EVENT(EVT_GLCANVAS_RESETGIZMOS, SimpleEvent); +wxDECLARE_EVENT(EVT_GLCANVAS_MOVE_DOUBLE_SLIDER, wxKeyEvent); +wxDECLARE_EVENT(EVT_GLCANVAS_EDIT_COLOR_CHANGE, wxKeyEvent); class GLCanvas3D { diff --git a/src/slic3r/GUI/GUI_ObjectList.cpp b/src/slic3r/GUI/GUI_ObjectList.cpp index dffa02e95b..a95b71bcb3 100644 --- a/src/slic3r/GUI/GUI_ObjectList.cpp +++ b/src/slic3r/GUI/GUI_ObjectList.cpp @@ -261,7 +261,7 @@ wxString ObjectList::get_mesh_errors_list(const int obj_idx, const int vol_idx / const stl_stats& stats = vol_idx == -1 ? (*m_objects)[obj_idx]->get_object_stl_stats() : - (*m_objects)[obj_idx]->volumes[vol_idx]->mesh.stl.stats; + (*m_objects)[obj_idx]->volumes[vol_idx]->mesh().stl.stats; std::map error_msg = { { L("degenerate facets"), stats.degenerate_facets }, @@ -1415,13 +1415,18 @@ void ObjectList::update_opt_keys(t_config_option_keys& opt_keys) void ObjectList::load_subobject(ModelVolumeType type) { - auto item = GetSelection(); - if (!item || m_objects_model->GetParent(item) != wxDataViewItem(0)) + wxDataViewItem item = GetSelection(); + // we can add volumes for Object or Instance + if (!item || !(m_objects_model->GetItemType(item)&(itObject|itInstance))) return; - int obj_idx = m_objects_model->GetIdByItem(item); + const int obj_idx = m_objects_model->GetObjectIdByItem(item); if (obj_idx < 0) return; + // Get object item, if Instance is selected + if (m_objects_model->GetItemType(item)&itInstance) + item = m_objects_model->GetItemById(obj_idx); + std::vector> volumes_info; load_part((*m_objects)[obj_idx], volumes_info, type); @@ -1592,7 +1597,7 @@ void ObjectList::load_generic_subobject(const std::string& type_name, const Mode // First (any) GLVolume of the selected instance. They all share the same instance matrix. const GLVolume* v = selection.get_volume(*selection.get_volume_idxs().begin()); // Transform the new modifier to be aligned with the print bed. - const BoundingBoxf3 mesh_bb = new_volume->mesh.bounding_box(); + const BoundingBoxf3 mesh_bb = new_volume->mesh().bounding_box(); new_volume->set_transformation(volume_to_bed_transformation(v->get_instance_transformation(), mesh_bb)); // Set the modifier position. auto offset = (type_name == "Slab") ? @@ -2150,9 +2155,11 @@ void ObjectList::update_selections() if (GetSelectedItemsCount() == 1 && m_objects_model->GetItemType(GetSelection()) == itSettings ) { const auto item = GetSelection(); - if (selection.is_single_full_object() && - m_objects_model->GetIdByItem(m_objects_model->GetParent(item)) == selection.get_object_idx()) - return; + if (selection.is_single_full_object()) { + if ( m_objects_model->GetIdByItem(m_objects_model->GetParent(item)) == selection.get_object_idx()) + return; + sels.Add(m_objects_model->GetItemById(selection.get_object_idx())); + } if (selection.is_single_volume() || selection.is_any_modifier()) { const auto gl_vol = selection.get_volume(*selection.get_volume_idxs().begin()); if (m_objects_model->GetVolumeIdByItem(m_objects_model->GetParent(item)) == gl_vol->volume_idx()) diff --git a/src/slic3r/GUI/GUI_ObjectManipulation.cpp b/src/slic3r/GUI/GUI_ObjectManipulation.cpp index 5bf25f3fc0..372cd79efb 100644 --- a/src/slic3r/GUI/GUI_ObjectManipulation.cpp +++ b/src/slic3r/GUI/GUI_ObjectManipulation.cpp @@ -92,6 +92,7 @@ void msw_rescale_word_local_combo(wxBitmapComboBox* combo) combo->SetValue(selection); } + ObjectManipulation::ObjectManipulation(wxWindow* parent) : OG_Settings(parent, true) #ifndef __APPLE__ @@ -162,16 +163,71 @@ ObjectManipulation::ObjectManipulation(wxWindow* parent) : const int field_width = 5; + // Mirror button size: + const int mirror_btn_width = 3; + // Legend for object modification line = Line{ "", "" }; def.label = ""; def.type = coString; - def.width = field_width/*50*/; + def.width = field_width - mirror_btn_width;//field_width/*50*/; + + // Load bitmaps to be used for the mirroring buttons: + m_mirror_bitmap_on = ScalableBitmap(parent, "mirroring_on.png"); + m_mirror_bitmap_off = ScalableBitmap(parent, "mirroring_off.png"); + m_mirror_bitmap_hidden = ScalableBitmap(parent, "mirroring_transparent.png"); for (const std::string axis : { "x", "y", "z" }) { const std::string label = boost::algorithm::to_upper_copy(axis); def.set_default_value(new ConfigOptionString{ " " + label }); Option option = Option(def, axis + "_axis_legend"); + + unsigned int axis_idx = (axis[0] - 'x'); // 0, 1 or 2 + + // We will add a button to toggle mirroring to each axis: + auto mirror_button = [=](wxWindow* parent) { + wxSize btn_size(em_unit(parent) * mirror_btn_width, em_unit(parent) * mirror_btn_width); + auto btn = new ScalableButton(parent, wxID_ANY, "mirroring_off.png", wxEmptyString, btn_size, wxDefaultPosition, wxBU_EXACTFIT | wxNO_BORDER | wxTRANSPARENT_WINDOW); + btn->SetToolTip(wxString::Format(_(L("Toggle %s axis mirroring")), label)); + + m_mirror_buttons[axis_idx].first = btn; + m_mirror_buttons[axis_idx].second = mbShown; + auto sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(btn); + + btn->Bind(wxEVT_BUTTON, [=](wxCommandEvent &e) { + Axis axis = (Axis)(axis_idx + X); + if (m_mirror_buttons[axis_idx].second == mbHidden) + return; + + GLCanvas3D* canvas = wxGetApp().plater()->canvas3D(); + Selection& selection = canvas->get_selection(); + + if (selection.is_single_volume() || selection.is_single_modifier()) { + GLVolume* volume = const_cast(selection.get_volume(*selection.get_volume_idxs().begin())); + volume->set_volume_mirror(axis, -volume->get_volume_mirror(axis)); + } + else if (selection.is_single_full_instance()) { + for (unsigned int idx : selection.get_volume_idxs()){ + GLVolume* volume = const_cast(selection.get_volume(idx)); + volume->set_instance_mirror(axis, -volume->get_instance_mirror(axis)); + } + } + else + return; + + // Update mirroring at the GLVolumes. + selection.synchronize_unselected_instances(Selection::SYNC_ROTATION_GENERAL); + selection.synchronize_unselected_volumes(); + // Copy mirroring values from GLVolumes into Model (ModelInstance / ModelVolume), trigger background processing. + canvas->do_mirror(); + canvas->set_as_dirty(); + UpdateAndShow(true); + }); + return sizer; + }; + + option.side_widget = mirror_button; line.append_option(option); } line.near_label_widget = [this](wxWindow* parent) { @@ -190,8 +246,8 @@ ObjectManipulation::ObjectManipulation(wxWindow* parent) : def.set_default_value(new ConfigOptionFloat(0.0)); def.width = field_width/*50*/; - // Add "uniform scaling" button in front of "Scale" option if (option_name == "Scale") { + // Add "uniform scaling" button in front of "Scale" option line.near_label_widget = [this](wxWindow* parent) { auto btn = new LockButton(parent, wxID_ANY); btn->Bind(wxEVT_BUTTON, [btn, this](wxCommandEvent &event){ @@ -201,8 +257,59 @@ ObjectManipulation::ObjectManipulation(wxWindow* parent) : m_lock_bnt = btn; return btn; }; + // Add reset scale button + auto reset_scale_button = [=](wxWindow* parent) { + auto btn = new ScalableButton(parent, wxID_ANY, ScalableBitmap(parent, "undo")); + btn->SetToolTip(_(L("Reset scale"))); + m_reset_scale_button = btn; + auto sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(btn, wxBU_EXACTFIT); + btn->Bind(wxEVT_BUTTON, [=](wxCommandEvent &e) { + change_scale_value(0, 100.); + change_scale_value(1, 100.); + change_scale_value(2, 100.); + }); + return sizer; + }; + line.append_widget(reset_scale_button); } + else if (option_name == "Rotation") { + // Add reset rotation button + auto reset_rotation_button = [=](wxWindow* parent) { + auto btn = new ScalableButton(parent, wxID_ANY, ScalableBitmap(parent, "undo")); + btn->SetToolTip(_(L("Reset rotation"))); + m_reset_rotation_button = btn; + auto sizer = new wxBoxSizer(wxHORIZONTAL); + sizer->Add(btn, wxBU_EXACTFIT); + btn->Bind(wxEVT_BUTTON, [=](wxCommandEvent &e) { + GLCanvas3D* canvas = wxGetApp().plater()->canvas3D(); + Selection& selection = canvas->get_selection(); + if (selection.is_single_volume() || selection.is_single_modifier()) { + GLVolume* volume = const_cast(selection.get_volume(*selection.get_volume_idxs().begin())); + volume->set_volume_rotation(Vec3d::Zero()); + } + else if (selection.is_single_full_instance()) { + for (unsigned int idx : selection.get_volume_idxs()){ + GLVolume* volume = const_cast(selection.get_volume(idx)); + volume->set_instance_rotation(Vec3d::Zero()); + } + } + else + return; + + // Update rotation at the GLVolumes. + selection.synchronize_unselected_instances(Selection::SYNC_ROTATION_GENERAL); + selection.synchronize_unselected_volumes(); + // Copy rotation values from GLVolumes into Model (ModelInstance / ModelVolume), trigger background processing. + canvas->do_rotate(); + + UpdateAndShow(true); + }); + return sizer; + }; + line.append_widget(reset_rotation_button); + } // Add empty bmp (Its size have to be equal to PrusaLockButton) in front of "Size" option to label alignment else if (option_name == "Size") { line.near_label_widget = [this](wxWindow* parent) { @@ -224,8 +331,8 @@ ObjectManipulation::ObjectManipulation(wxWindow* parent) : return line; }; - // Settings table + m_og->sidetext_width = 3; m_og->append_line(add_og_to_object_settings(L("Position"), L("mm")), &m_move_Label); m_og->append_line(add_og_to_object_settings(L("Rotation"), "°"), &m_rotate_Label); m_og->append_line(add_og_to_object_settings(L("Scale"), "%"), &m_scale_Label); @@ -239,6 +346,8 @@ ObjectManipulation::ObjectManipulation(wxWindow* parent) : ctrl->msw_rescale(); }; } + + void ObjectManipulation::Show(const bool show) { @@ -390,11 +499,13 @@ void ObjectManipulation::update_if_dirty() if (selection.requires_uniform_scale()) { m_lock_bnt->SetLock(true); - m_lock_bnt->Disable(); + m_lock_bnt->SetToolTip(_(L("You cann't use non-uniform scaling mode for multiple objects/parts selection"))); + m_lock_bnt->disable(); } else { m_lock_bnt->SetLock(m_uniform_scale); - m_lock_bnt->Enable(); + m_lock_bnt->SetToolTip(wxEmptyString); + m_lock_bnt->enable(); } { @@ -408,9 +519,95 @@ void ObjectManipulation::update_if_dirty() else m_og->disable(); + update_reset_buttons_visibility(); + update_mirror_buttons_visibility(); + m_dirty = false; } + + +void ObjectManipulation::update_reset_buttons_visibility() +{ + GLCanvas3D* canvas = wxGetApp().plater()->canvas3D(); + if (!canvas) + return; + const Selection& selection = canvas->get_selection(); + + bool show_rotation = false; + bool show_scale = false; + + if (selection.is_single_full_instance() || selection.is_single_modifier() || selection.is_single_volume()) { + const GLVolume* volume = selection.get_volume(*selection.get_volume_idxs().begin()); + Vec3d rotation; + Vec3d scale; + + if (selection.is_single_full_instance()) { + rotation = volume->get_instance_rotation(); + scale = volume->get_instance_scaling_factor(); + } + else { + rotation = volume->get_volume_rotation(); + scale = volume->get_volume_scaling_factor(); + } + show_rotation = !rotation.isApprox(Vec3d::Zero()); + show_scale = !scale.isApprox(Vec3d::Ones()); + } + + wxGetApp().CallAfter([this, show_rotation, show_scale]{ + m_reset_rotation_button->Show(show_rotation); + m_reset_scale_button->Show(show_scale); + }); +} + + + +void ObjectManipulation::update_mirror_buttons_visibility() +{ + GLCanvas3D* canvas = wxGetApp().plater()->canvas3D(); + Selection& selection = canvas->get_selection(); + std::array new_states = {mbHidden, mbHidden, mbHidden}; + + if (!m_world_coordinates) { + if (selection.is_single_full_instance() || selection.is_single_modifier() || selection.is_single_volume()) { + const GLVolume* volume = selection.get_volume(*selection.get_volume_idxs().begin()); + Vec3d mirror; + + if (selection.is_single_full_instance()) + mirror = volume->get_instance_mirror(); + else + mirror = volume->get_volume_mirror(); + + for (unsigned char i=0; i<3; ++i) + new_states[i] = (mirror[i] < 0. ? mbActive : mbShown); + } + } + else { + // the mirroring buttons should be hidden in world coordinates, + // unless we make it actually mirror in world coords. + } + + // Hiding the buttons through Hide() always messed up the sizers. As a workaround, the button + // is assigned a transparent bitmap. We must of course remember the actual state. + wxGetApp().CallAfter([this, new_states]{ + for (int i=0; i<3; ++i) { + if (new_states[i] != m_mirror_buttons[i].second) { + const wxBitmap* bmp; + switch (new_states[i]) { + case mbHidden : bmp = &m_mirror_bitmap_hidden.bmp(); m_mirror_buttons[i].first->Enable(false); break; + case mbShown : bmp = &m_mirror_bitmap_off.bmp(); m_mirror_buttons[i].first->Enable(true); break; + case mbActive : bmp = &m_mirror_bitmap_on.bmp(); m_mirror_buttons[i].first->Enable(true); break; + } + m_mirror_buttons[i].first->SetBitmap(*bmp); + m_mirror_buttons[i].second = new_states[i]; + } + } + }); +} + + + + #ifndef __APPLE__ void ObjectManipulation::emulate_kill_focus() { @@ -493,7 +690,7 @@ void ObjectManipulation::change_rotation_value(int axis, double value) m_cache.rotation = rotation; m_cache.rotation_rounded(axis) = DBL_MAX; - this->UpdateAndShow(true); + this->UpdateAndShow(true); } void ObjectManipulation::change_scale_value(int axis, double value) @@ -511,6 +708,7 @@ void ObjectManipulation::change_scale_value(int axis, double value) this->UpdateAndShow(true); } + void ObjectManipulation::change_size_value(int axis, double value) { if (std::abs(m_cache.size_rounded(axis) - value) < EPSILON) @@ -666,6 +864,12 @@ void ObjectManipulation::msw_rescale() m_manifold_warning_bmp.msw_rescale(); m_fix_throught_netfab_bitmap->SetBitmap(m_manifold_warning_bmp.bmp()); + m_mirror_bitmap_on.msw_rescale(); + m_mirror_bitmap_off.msw_rescale(); + m_mirror_bitmap_hidden.msw_rescale(); + m_reset_scale_button->msw_rescale(); + m_reset_rotation_button->msw_rescale(); + get_og()->msw_rescale(); } diff --git a/src/slic3r/GUI/GUI_ObjectManipulation.hpp b/src/slic3r/GUI/GUI_ObjectManipulation.hpp index 7c359f3d72..cc2154514d 100644 --- a/src/slic3r/GUI/GUI_ObjectManipulation.hpp +++ b/src/slic3r/GUI/GUI_ObjectManipulation.hpp @@ -53,6 +53,23 @@ class ObjectManipulation : public OG_Settings wxStaticText* m_scale_Label = nullptr; wxStaticText* m_rotate_Label = nullptr; + // Non-owning pointers to the reset buttons, so we can hide and show them. + ScalableButton* m_reset_scale_button = nullptr; + ScalableButton* m_reset_rotation_button = nullptr; + + // Mirroring buttons and their current state + enum MirrorButtonState { + mbHidden, + mbShown, + mbActive + }; + std::array, 3> m_mirror_buttons; + + // Bitmaps for the mirroring buttons. + ScalableBitmap m_mirror_bitmap_on; + ScalableBitmap m_mirror_bitmap_off; + ScalableBitmap m_mirror_bitmap_hidden; + // Needs to be updated from OnIdle? bool m_dirty = false; // Cached labels for the delayed update, not localized! @@ -111,10 +128,10 @@ private: void reset_settings_value(); void update_settings_value(const Selection& selection); - // update size values after scale unit changing or "gizmos" - void update_size_value(const Vec3d& size); - // update rotation value after "gizmos" - void update_rotation_value(const Vec3d& rotation); + // Show or hide scale/rotation reset buttons if needed + void update_reset_buttons_visibility(); + //Show or hide mirror buttons + void update_mirror_buttons_visibility(); // change values void change_position_value(int axis, double value); diff --git a/src/slic3r/GUI/GUI_Preview.cpp b/src/slic3r/GUI/GUI_Preview.cpp index d28b921d92..2f5e10962e 100644 --- a/src/slic3r/GUI/GUI_Preview.cpp +++ b/src/slic3r/GUI/GUI_Preview.cpp @@ -414,6 +414,18 @@ void Preview::msw_rescale() refresh_print(); } +void Preview::move_double_slider(wxKeyEvent& evt) +{ + if (m_slider) + m_slider->OnKeyDown(evt); +} + +void Preview::edit_double_slider(wxKeyEvent& evt) +{ + if (m_slider) + m_slider->OnChar(evt); +} + void Preview::bind_event_handlers() { this->Bind(wxEVT_SIZE, &Preview::on_size, this); diff --git a/src/slic3r/GUI/GUI_Preview.hpp b/src/slic3r/GUI/GUI_Preview.hpp index 1838082c38..93038b0e56 100644 --- a/src/slic3r/GUI/GUI_Preview.hpp +++ b/src/slic3r/GUI/GUI_Preview.hpp @@ -122,6 +122,8 @@ public: void refresh_print(); void msw_rescale(); + void move_double_slider(wxKeyEvent& evt); + void edit_double_slider(wxKeyEvent& evt); private: bool init(wxWindow* parent, Bed3D& bed, Camera& camera, GLToolbar& view_toolbar, Model* model); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp index d118a6877f..cfe07a61cf 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.cpp @@ -27,6 +27,7 @@ GLGizmoSlaSupports::GLGizmoSlaSupports(GLCanvas3D& parent, unsigned int sprite_i : GLGizmoBase(parent, sprite_id) #endif // ENABLE_SVG_ICONS , m_quadric(nullptr) + , m_its(nullptr) { m_quadric = ::gluNewQuadric(); if (m_quadric != nullptr) @@ -379,36 +380,23 @@ bool GLGizmoSlaSupports::is_point_clipped(const Vec3d& point) const bool GLGizmoSlaSupports::is_mesh_update_necessary() const { return ((m_state == On) && (m_model_object != nullptr) && !m_model_object->instances.empty()) - && ((m_model_object->id() != m_current_mesh_model_id) || m_V.size()==0); + && ((m_model_object->id() != m_current_mesh_model_id) || m_its == nullptr); } void GLGizmoSlaSupports::update_mesh() { wxBusyCursor wait; - Eigen::MatrixXf& V = m_V; - Eigen::MatrixXi& F = m_F; - // We rely on SLA model object having a single volume, // this way we can use that mesh directly. // This mesh does not account for the possible Z up SLA offset. - m_mesh = &m_model_object->volumes.front()->mesh; - const_cast(m_mesh)->require_shared_vertices(); // TriangleMeshSlicer needs this - const stl_file& stl = m_mesh->stl; - V.resize(3 * stl.stats.number_of_facets, 3); - F.resize(stl.stats.number_of_facets, 3); - for (unsigned int i=0; ivertex[0](0); V(3*i+0, 1) = facet->vertex[0](1); V(3*i+0, 2) = facet->vertex[0](2); - V(3*i+1, 0) = facet->vertex[1](0); V(3*i+1, 1) = facet->vertex[1](1); V(3*i+1, 2) = facet->vertex[1](2); - V(3*i+2, 0) = facet->vertex[2](0); V(3*i+2, 1) = facet->vertex[2](1); V(3*i+2, 2) = facet->vertex[2](2); - F(i, 0) = 3*i+0; - F(i, 1) = 3*i+1; - F(i, 2) = 3*i+2; - } + m_mesh = &m_model_object->volumes.front()->mesh(); + m_its = &m_mesh->its; m_current_mesh_model_id = m_model_object->id(); m_editing_mode = false; - m_AABB = igl::AABB(); - m_AABB.init(m_V, m_F); + m_AABB.deinit(); + m_AABB.init( + MapMatrixXfUnaligned(m_its->vertices.front().data(), m_its->vertices.size(), 3), + MapMatrixXiUnaligned(m_its->indices.front().data(), m_its->indices.size(), 3)); } // Unprojects the mouse position on the mesh and return the hit point and normal of the facet. @@ -416,7 +404,7 @@ void GLGizmoSlaSupports::update_mesh() std::pair GLGizmoSlaSupports::unproject_on_mesh(const Vec2d& mouse_pos) { // if the gizmo doesn't have the V, F structures for igl, calculate them first: - if (m_V.size() == 0) + if (m_its == nullptr) update_mesh(); const Camera& camera = m_parent.get_camera(); @@ -442,7 +430,10 @@ std::pair GLGizmoSlaSupports::unproject_on_mesh(const Vec2d& mouse point1 = inv * point1; point2 = inv * point2; - if (!m_AABB.intersect_ray(m_V, m_F, point1.cast(), (point2-point1).cast(), hits)) + if (!m_AABB.intersect_ray( + MapMatrixXfUnaligned(m_its->vertices.front().data(), m_its->vertices.size(), 3), + MapMatrixXiUnaligned(m_its->indices.front().data(), m_its->indices.size(), 3), + point1.cast(), (point2-point1).cast(), hits)) throw std::invalid_argument("unproject_on_mesh(): No intersection found."); std::sort(hits.begin(), hits.end(), [](const igl::Hit& a, const igl::Hit& b) { return a.t < b.t; }); @@ -457,9 +448,9 @@ std::pair GLGizmoSlaSupports::unproject_on_mesh(const Vec2d& mouse igl::Hit& hit = hits[i]; int fid = hit.id; // facet id bc = Vec3f(1-hit.u-hit.v, hit.u, hit.v); // barycentric coordinates of the hit - a = (m_V.row(m_F(fid, 1)) - m_V.row(m_F(fid, 0))); - b = (m_V.row(m_F(fid, 2)) - m_V.row(m_F(fid, 0))); - result = bc(0) * m_V.row(m_F(fid, 0)) + bc(1) * m_V.row(m_F(fid, 1)) + bc(2)*m_V.row(m_F(fid, 2)); + a = (m_its->vertices[m_its->indices[fid](1)] - m_its->vertices[m_its->indices[fid](0)]); + b = (m_its->vertices[m_its->indices[fid](2)] - m_its->vertices[m_its->indices[fid](0)]); + result = bc(0) * m_its->vertices[m_its->indices[fid](0)] + bc(1) * m_its->vertices[m_its->indices[fid](1)] + bc(2)*m_its->vertices[m_its->indices[fid](2)]; if (m_clipping_plane_distance == 0.f || !is_point_clipped(result.cast())) break; } @@ -550,7 +541,7 @@ bool GLGizmoSlaSupports::gizmo_event(SLAGizmoEventType action, const Vec2d& mous const Selection& selection = m_parent.get_selection(); const GLVolume* volume = selection.get_volume(*selection.get_volume_idxs().begin()); const Transform3d& instance_matrix_no_translation_no_scaling = volume->get_instance_transformation().get_matrix(true,false,true); - Vec3f direction_to_camera = camera.get_dir_forward().cast(); + Vec3f direction_to_camera = -camera.get_dir_forward().cast(); Vec3f direction_to_camera_mesh = (instance_matrix_no_translation_no_scaling.inverse().cast() * direction_to_camera).normalized().eval(); Vec3f scaling = volume->get_instance_scaling_factor().cast(); direction_to_camera_mesh = Vec3f(direction_to_camera_mesh(0)*scaling(0), direction_to_camera_mesh(1)*scaling(1), direction_to_camera_mesh(2)*scaling(2)); @@ -564,15 +555,18 @@ bool GLGizmoSlaSupports::gizmo_event(SLAGizmoEventType action, const Vec2d& mous // Cast a ray in the direction of the camera and look for intersection with the mesh: std::vector hits; // Offset the start of the ray to the front of the ball + EPSILON to account for numerical inaccuracies. - if (m_AABB.intersect_ray(m_V, m_F, support_point.pos + direction_to_camera_mesh * (support_point.head_front_radius + EPSILON), direction_to_camera_mesh, hits)) { + if (m_AABB.intersect_ray( + MapMatrixXfUnaligned(m_its->vertices.front().data(), m_its->vertices.size(), 3), + MapMatrixXiUnaligned(m_its->indices.front().data(), m_its->indices.size(), 3), + support_point.pos + direction_to_camera_mesh * (support_point.head_front_radius + EPSILON), direction_to_camera_mesh, hits)) { std::sort(hits.begin(), hits.end(), [](const igl::Hit& h1, const igl::Hit& h2) { return h1.t < h2.t; }); if (m_clipping_plane_distance != 0.f) { // If the closest hit facet normal points in the same direction as the ray, // we are looking through the mesh and should therefore discard the point: int fid = hits.front().id; // facet id - Vec3f a = (m_V.row(m_F(fid, 1)) - m_V.row(m_F(fid, 0))); - Vec3f b = (m_V.row(m_F(fid, 2)) - m_V.row(m_F(fid, 0))); + Vec3f a = (m_its->vertices[m_its->indices[fid](1)] - m_its->vertices[m_its->indices[fid](0)]); + Vec3f b = (m_its->vertices[m_its->indices[fid](2)] - m_its->vertices[m_its->indices[fid](0)]); if ((a.cross(b)).dot(direction_to_camera_mesh) > 0.f) is_obscured = true; @@ -582,7 +576,7 @@ bool GLGizmoSlaSupports::gizmo_event(SLAGizmoEventType action, const Vec2d& mous int fid = hit.id; // facet id Vec3f bc = Vec3f(1-hit.u-hit.v, hit.u, hit.v); // barycentric coordinates of the hit - Vec3f hit_pos = bc(0) * m_V.row(m_F(fid, 0)) + bc(1) * m_V.row(m_F(fid, 1)) + bc(2)*m_V.row(m_F(fid, 2)); + Vec3f hit_pos = bc(0) * m_its->vertices[m_its->indices[fid](0)] + bc(1) * m_its->vertices[m_its->indices[fid](1)] + bc(2)*m_its->vertices[m_its->indices[fid](2)]; if (is_point_clipped(hit_pos.cast())) { hits.erase(hits.begin()+j); --j; @@ -759,9 +753,12 @@ void GLGizmoSlaSupports::update_cache_entry_normal(unsigned int i) const int idx = 0; Eigen::Matrix pp = m_editing_mode_cache[i].support_point.pos; Eigen::Matrix cc; - m_AABB.squared_distance(m_V, m_F, pp, idx, cc); - Vec3f a = (m_V.row(m_F(idx, 1)) - m_V.row(m_F(idx, 0))); - Vec3f b = (m_V.row(m_F(idx, 2)) - m_V.row(m_F(idx, 0))); + m_AABB.squared_distance( + MapMatrixXfUnaligned(m_its->vertices.front().data(), m_its->vertices.size(), 3), + MapMatrixXiUnaligned(m_its->indices.front().data(), m_its->indices.size(), 3), + pp, idx, cc); + Vec3f a = (m_its->vertices[m_its->indices[idx](1)] - m_its->vertices[m_its->indices[idx](0)]); + Vec3f b = (m_its->vertices[m_its->indices[idx](2)] - m_its->vertices[m_its->indices[idx](0)]); m_editing_mode_cache[i].normal = a.cross(b); } @@ -1067,8 +1064,7 @@ void GLGizmoSlaSupports::on_set_state() m_clipping_plane_distance = 0.f; // Release triangle mesh slicer and the AABB spatial search structure. m_AABB.deinit(); - m_V = Eigen::MatrixXf(); - m_F = Eigen::MatrixXi(); + m_its = nullptr; m_tms.reset(); m_supports_tms.reset(); }); diff --git a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp index fb758d2404..30238cc9dd 100644 --- a/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp +++ b/src/slic3r/GUI/Gizmos/GLGizmoSlaSupports.hpp @@ -35,10 +35,11 @@ private: const float RenderPointScale = 1.f; GLUquadricObj* m_quadric; - Eigen::MatrixXf m_V; // vertices - Eigen::MatrixXi m_F; // facets indices - igl::AABB m_AABB; + typedef Eigen::Map> MapMatrixXfUnaligned; + typedef Eigen::Map> MapMatrixXiUnaligned; + igl::AABB m_AABB; const TriangleMesh* m_mesh; + const indexed_triangle_set* m_its; mutable const TriangleMesh* m_supports_mesh; mutable std::vector m_triangles; mutable std::vector m_supports_triangles; @@ -131,6 +132,11 @@ private: protected: void on_set_state() override; + virtual void on_set_hover_id() + { + if ((int)m_editing_mode_cache.size() <= m_hover_id) + m_hover_id = -1; + } void on_start_dragging(const Selection& selection) override; virtual void on_render_input_window(float x, float y, float bottom_limit, const Selection& selection) override; diff --git a/src/slic3r/GUI/MainFrame.cpp b/src/slic3r/GUI/MainFrame.cpp index 6091f10a14..667dcd899d 100644 --- a/src/slic3r/GUI/MainFrame.cpp +++ b/src/slic3r/GUI/MainFrame.cpp @@ -54,7 +54,7 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_S #endif // _WIN32 // initialize status bar - m_statusbar = new ProgressStatusBar(this); + m_statusbar.reset(new ProgressStatusBar(this)); m_statusbar->embed(this); m_statusbar->set_status_text(_(L("Version")) + " " + SLIC3R_VERSION + @@ -103,6 +103,8 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_S event.Veto(); return; } + + if(m_plater) m_plater->stop_jobs(); // Weird things happen as the Paint messages are floating around the windows being destructed. // Avoid the Paint messages by hiding the main window. @@ -138,6 +140,8 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_S update_ui_from_settings(); // FIXME (?) } +MainFrame::~MainFrame() = default; + void MainFrame::update_title() { wxString title = wxEmptyString; @@ -976,7 +980,8 @@ void MainFrame::load_config(const DynamicPrintConfig& config) if (! boost::algorithm::ends_with(opt_key, "_settings_id")) tab->get_config()->option(opt_key)->set(config.option(opt_key)); } - wxGetApp().load_current_presets(); + + wxGetApp().load_current_presets(); #endif } diff --git a/src/slic3r/GUI/MainFrame.hpp b/src/slic3r/GUI/MainFrame.hpp index a5de947388..805b663bb2 100644 --- a/src/slic3r/GUI/MainFrame.hpp +++ b/src/slic3r/GUI/MainFrame.hpp @@ -89,7 +89,7 @@ protected: public: MainFrame(); - ~MainFrame() {} + ~MainFrame(); Plater* plater() { return m_plater; } @@ -126,7 +126,7 @@ public: Plater* m_plater { nullptr }; wxNotebook* m_tabpanel { nullptr }; wxProgressDialog* m_progress_dialog { nullptr }; - ProgressStatusBar* m_statusbar { nullptr }; + std::unique_ptr m_statusbar; }; } // GUI diff --git a/src/slic3r/GUI/OptionsGroup.cpp b/src/slic3r/GUI/OptionsGroup.cpp index 2ac6b00af6..0149329005 100644 --- a/src/slic3r/GUI/OptionsGroup.cpp +++ b/src/slic3r/GUI/OptionsGroup.cpp @@ -276,7 +276,7 @@ void OptionsGroup::append_line(const Line& line, wxStaticText** full_Label/* = n // add sidetext if any if (option.sidetext != "") { auto sidetext = new wxStaticText( this->ctrl_parent(), wxID_ANY, _(option.sidetext), wxDefaultPosition, - /*wxSize(sidetext_width*wxGetApp().em_unit(), -1)*/wxDefaultSize, wxALIGN_LEFT); + wxSize(sidetext_width != -1 ? sidetext_width*wxGetApp().em_unit() : -1, -1) /*wxDefaultSize*/, wxALIGN_LEFT); sidetext->SetBackgroundStyle(wxBG_STYLE_PAINT); sidetext->SetFont(wxGetApp().normal_font()); sizer_tmp->Add(sidetext, 0, wxLEFT | wxALIGN_CENTER_VERTICAL, 4); diff --git a/src/slic3r/GUI/Plater.cpp b/src/slic3r/GUI/Plater.cpp index 44f77b3f78..f8385c0b58 100644 --- a/src/slic3r/GUI/Plater.cpp +++ b/src/slic3r/GUI/Plater.cpp @@ -5,9 +5,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -37,7 +39,12 @@ #include "libslic3r/SLA/SLARotfinder.hpp" #include "libslic3r/Utils.hpp" -#include "libnest2d/optimizers/nlopt/genetic.hpp" +//#include "libslic3r/ClipperUtils.hpp" + +// #include "libnest2d/optimizers/nlopt/genetic.hpp" +// #include "libnest2d/backends/clipper/geometries.hpp" +// #include "libnest2d/utils/rotcalipers.hpp" +#include "libslic3r/MinAreaBoundingBox.hpp" #include "GUI.hpp" #include "GUI_App.hpp" @@ -1253,8 +1260,243 @@ struct Plater::priv Preview *preview; BackgroundSlicingProcess background_process; - bool arranging; - bool rotoptimizing; + + // A class to handle UI jobs like arranging and optimizing rotation. + // These are not instant jobs, the user has to be informed about their + // state in the status progress indicator. On the other hand they are + // separated from the background slicing process. Ideally, these jobs should + // run when the background process is not running. + // + // TODO: A mechanism would be useful for blocking the plater interactions: + // objects would be frozen for the user. In case of arrange, an animation + // could be shown, or with the optimize orientations, partial results + // could be displayed. + class Job: public wxEvtHandler { + int m_range = 100; + std::future m_ftr; + priv *m_plater = nullptr; + std::atomic m_running {false}, m_canceled {false}; + bool m_finalized = false; + + void run() { + m_running.store(true); process(); m_running.store(false); + + // ensure to call the last status to finalize the job + update_status(status_range(), ""); + } + + protected: + + // status range for a particular job + virtual int status_range() const { return 100; } + + // status update, to be used from the work thread (process() method) + void update_status(int st, const wxString& msg = "") { + auto evt = new wxThreadEvent(); evt->SetInt(st); evt->SetString(msg); + wxQueueEvent(this, evt); + } + + priv& plater() { return *m_plater; } + bool was_canceled() const { return m_canceled.load(); } + + // Launched just before start(), a job can use it to prepare internals + virtual void prepare() {} + + // Launched when the job is finished. It refreshes the 3dscene by def. + virtual void finalize() { + // Do a full refresh of scene tree, including regenerating + // all the GLVolumes. FIXME The update function shall just + // reload the modified matrices. + if(! was_canceled()) + plater().update(true); + } + + public: + + Job(priv *_plater): m_plater(_plater) + { + Bind(wxEVT_THREAD, [this](const wxThreadEvent& evt){ + auto msg = evt.GetString(); + if(! msg.empty()) plater().statusbar()->set_status_text(msg); + + if(m_finalized) return; + + plater().statusbar()->set_progress(evt.GetInt()); + if(evt.GetInt() == status_range()) { + + // set back the original range and cancel callback + plater().statusbar()->set_range(m_range); + plater().statusbar()->set_cancel_callback(); + wxEndBusyCursor(); + + finalize(); + + // dont do finalization again for the same process + m_finalized = true; + } + }); + } + + // TODO: use this when we all migrated to VS2019 + // Job(const Job&) = delete; + // Job(Job&&) = default; + // Job& operator=(const Job&) = delete; + // Job& operator=(Job&&) = default; + Job(const Job&) = delete; + Job& operator=(const Job&) = delete; + Job(Job &&o) : + m_range(o.m_range), + m_ftr(std::move(o.m_ftr)), + m_plater(o.m_plater), + m_finalized(o.m_finalized) + { + m_running.store(o.m_running.load()); + m_canceled.store(o.m_canceled.load()); + } + + virtual void process() = 0; + + void start() { // Start the job. No effect if the job is already running + if(! m_running.load()) { + + prepare(); + + // Save the current status indicatior range and push the new one + m_range = plater().statusbar()->get_range(); + plater().statusbar()->set_range(status_range()); + + // init cancellation flag and set the cancel callback + m_canceled.store(false); + plater().statusbar()->set_cancel_callback( [this](){ + m_canceled.store(true); + }); + + m_finalized = false; + + // Changing cursor to busy + wxBeginBusyCursor(); + + try { // Execute the job + m_ftr = std::async(std::launch::async, &Job::run, this); + } catch(std::exception& ) { + update_status(status_range(), + _(L("ERROR: not enough resources to execute a new job."))); + } + + // The state changes will be undone when the process hits the + // last status value, in the status update handler (see ctor) + } + } + + // To wait for the running job and join the threads. False is returned + // if the timeout has been reached and the job is still running. Call + // cancel() before this fn if you want to explicitly end the job. + bool join(int timeout_ms = 0) const { + if(!m_ftr.valid()) return true; + + if(timeout_ms <= 0) + m_ftr.wait(); + else if(m_ftr.wait_for(std::chrono::milliseconds(timeout_ms)) == + std::future_status::timeout) + return false; + + return true; + } + + bool is_running() const { return m_running.load(); } + void cancel() { m_canceled.store(true); } + }; + + enum class Jobs : size_t { + Arrange, + Rotoptimize + }; + + // Jobs defined inside the group class will be managed so that only one can + // run at a time. Also, the background process will be stopped if a job is + // started. + class ExclusiveJobGroup { + + static const int ABORT_WAIT_MAX_MS = 10000; + + priv * m_plater; + + class ArrangeJob : public Job + { + int count = 0; + + protected: + void prepare() override + { + count = 0; + for (auto obj : plater().model.objects) + count += int(obj->instances.size()); + } + + public: + //using Job::Job; + ArrangeJob(priv * pltr): Job(pltr) {} + int status_range() const override { return count; } + void set_count(int c) { count = c; } + void process() override; + } arrange_job/*{m_plater}*/; + + class RotoptimizeJob : public Job + { + public: + //using Job::Job; + RotoptimizeJob(priv * pltr): Job(pltr) {} + void process() override; + } rotoptimize_job/*{m_plater}*/; + + // To create a new job, just define a new subclass of Job, implement + // the process and the optional prepare() and finalize() methods + // Register the instance of the class in the m_jobs container + // if it cannot run concurrently with other jobs in this group + + std::vector> m_jobs/*{arrange_job, + rotoptimize_job}*/; + + public: + ExclusiveJobGroup(priv *_plater) + : m_plater(_plater) + , arrange_job(m_plater) + , rotoptimize_job(m_plater) + , m_jobs({arrange_job, rotoptimize_job}) + {} + + void start(Jobs jid) { + m_plater->background_process.stop(); + stop_all(); + m_jobs[size_t(jid)].get().start(); + } + + void cancel_all() { for (Job& j : m_jobs) j.cancel(); } + + void join_all(int wait_ms = 0) + { + std::vector aborted(m_jobs.size(), false); + + for (size_t jid = 0; jid < m_jobs.size(); ++jid) + aborted[jid] = m_jobs[jid].get().join(wait_ms); + + if (!all_of(aborted)) + BOOST_LOG_TRIVIAL(error) << "Could not abort a job!"; + } + + void stop_all() { cancel_all(); join_all(ABORT_WAIT_MAX_MS); } + + const Job& get(Jobs jobid) const { return m_jobs[size_t(jobid)]; } + + bool is_any_running() const + { + return std::any_of(m_jobs.begin(), + m_jobs.end(), + [](const Job &j) { return j.is_running(); }); + } + + } m_ui_jobs{this}; + bool delayed_scene_refresh; std::string delayed_error_message; @@ -1429,8 +1671,6 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) { this->q->SetFont(Slic3r::GUI::wxGetApp().normal_font()); - arranging = false; - rotoptimizing = false; background_process.set_fff_print(&fff_print); background_process.set_sla_print(&sla_print); background_process.set_gcode_preview_data(&gcode_preview_data); @@ -1517,6 +1757,8 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_QUESTION_MARK, [this](SimpleEvent&) { wxGetApp().keyboard_shortcuts(); }); preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_UPDATE_BED_SHAPE, [this](SimpleEvent&) { set_bed_shape(config->option("bed_shape")->values); }); preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_TAB, [this](SimpleEvent&) { select_next_view_3D(); }); + preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_MOVE_DOUBLE_SLIDER, [this](wxKeyEvent& evt) { preview->move_double_slider(evt); }); + preview->get_wxglcanvas()->Bind(EVT_GLCANVAS_EDIT_COLOR_CHANGE, [this](wxKeyEvent& evt) { preview->edit_double_slider(evt); }); q->Bind(EVT_SLICING_COMPLETED, &priv::on_slicing_completed, this); q->Bind(EVT_PROCESS_COMPLETED, &priv::on_process_completed, this); @@ -1534,7 +1776,8 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame) void Plater::priv::update(bool force_full_scene_refresh) { - wxWindowUpdateLocker freeze_guard(q); + // the following line, when enabled, causes flickering on NVIDIA graphics cards +// wxWindowUpdateLocker freeze_guard(q); if (get_config("autocenter") == "1") { // auto *bed_shape_opt = config->opt("bed_shape"); // const auto bed_shape = Slic3r::Polygon::new_scale(bed_shape_opt->values); @@ -1604,7 +1847,7 @@ void Plater::priv::update_ui_from_settings() ProgressStatusBar* Plater::priv::statusbar() { - return main_frame->m_statusbar; + return main_frame->m_statusbar.get(); } std::string Plater::priv::get_config(const std::string &key) const @@ -1670,6 +1913,22 @@ std::vector Plater::priv::load_files(const std::vector& input_ if (load_config && !config_loaded.empty()) { // Based on the printer technology field found in the loaded config, select the base for the config, PrinterTechnology printer_technology = Preset::printer_technology(config_loaded); + + // We can't to load SLA project if there is at least one multi-part object on the bed + if (printer_technology == ptSLA) + { + const ModelObjectPtrs& objects = q->model().objects; + for (auto object : objects) + if (object->volumes.size() > 1) + { + Slic3r::GUI::show_info(nullptr, + _(L("You can't to load SLA project if there is at least one multi-part object on the bed")) + "\n\n" + + _(L("Please check your object list before preset changing.")), + _(L("Attention!"))); + return obj_idxs; + } + } + config.apply(printer_technology == ptFFF ? static_cast(FullPrintConfig::defaults()) : static_cast(SLAFullPrintConfig::defaults())); @@ -2125,59 +2384,45 @@ void Plater::priv::mirror(Axis axis) void Plater::priv::arrange() { - if (arranging) { return; } - arranging = true; - Slic3r::ScopeGuard arranging_guard([this]() { arranging = false; }); + m_ui_jobs.start(Jobs::Arrange); +} - wxBusyCursor wait; +// This method will find an optimal orientation for the currently selected item +// Very similar in nature to the arrange method above... +void Plater::priv::sla_optimize_rotation() { + m_ui_jobs.start(Jobs::Rotoptimize); +} - this->background_process.stop(); +void Plater::priv::ExclusiveJobGroup::ArrangeJob::process() { + // TODO: we should decide whether to allow arrange when the search is + // running we should probably disable explicit slicing and background + // processing - unsigned count = 0; - for(auto obj : model.objects) count += obj->instances.size(); + static const auto arrangestr = _(L("Arranging")); - auto prev_range = statusbar()->get_range(); - statusbar()->set_range(count); - - auto statusfn = [this, count] (unsigned st, const std::string& msg) { - /* // In case we would run the arrange asynchronously - wxCommandEvent event(EVT_PROGRESS_BAR); - event.SetInt(st); - event.SetString(msg); - wxQueueEvent(this->q, event.Clone()); */ - statusbar()->set_progress(count - st); - statusbar()->set_status_text(_(msg)); - - // ok, this is dangerous, but we are protected by the flag - // 'arranging' and the arrange button is also disabled. - // This call is needed for the cancel button to work. - wxYieldIfNeeded(); - }; - - statusbar()->set_cancel_callback([this, statusfn](){ - arranging = false; - statusfn(0, L("Arranging canceled")); - }); - - static const std::string arrangestr = L("Arranging"); + auto &config = plater().config; + auto &view3D = plater().view3D; + auto &model = plater().model; // FIXME: I don't know how to obtain the minimum distance, it depends // on printer technology. I guess the following should work but it crashes. - double dist = 6; //PrintConfig::min_object_distance(config); - if(printer_technology == ptFFF) { + double dist = 6; // PrintConfig::min_object_distance(config); + if (plater().printer_technology == ptFFF) { dist = PrintConfig::min_object_distance(config); } - auto min_obj_distance = coord_t(dist/SCALING_FACTOR); + auto min_obj_distance = coord_t(dist / SCALING_FACTOR); - const auto *bed_shape_opt = config->opt("bed_shape"); + const auto *bed_shape_opt = config->opt( + "bed_shape"); assert(bed_shape_opt); - auto& bedpoints = bed_shape_opt->values; - Polyline bed; bed.points.reserve(bedpoints.size()); - for(auto& v : bedpoints) bed.append(Point::new_scale(v(0), v(1))); + auto & bedpoints = bed_shape_opt->values; + Polyline bed; + bed.points.reserve(bedpoints.size()); + for (auto &v : bedpoints) bed.append(Point::new_scale(v(0), v(1))); - statusfn(0, arrangestr); + update_status(0, arrangestr); arr::WipeTowerInfo wti = view3D->get_canvas3d()->get_wipe_tower_info(); @@ -2193,129 +2438,87 @@ void Plater::priv::arrange() bed, hint, false, // create many piles not just one pile - [statusfn](unsigned st) { statusfn(st, arrangestr); }, - [this] () { return !arranging; }); - } catch(std::exception& /*e*/) { - GUI::show_error(this->q, L("Could not arrange model objects! " - "Some geometries may be invalid.")); + [this](unsigned st) { + if (st > 0) + update_status(count - int(st), arrangestr); + }, + [this]() { return was_canceled(); }); + } catch (std::exception & /*e*/) { + GUI::show_error(plater().q, + L("Could not arrange model objects! " + "Some geometries may be invalid.")); } + update_status(count, + was_canceled() ? _(L("Arranging canceled.")) + : _(L("Arranging done."))); + // it remains to move the wipe tower: view3D->get_canvas3d()->arrange_wipe_tower(wti); - - statusfn(0, L("Arranging done.")); - statusbar()->set_range(prev_range); - statusbar()->set_cancel_callback(); // remove cancel button - - // Do a full refresh of scene tree, including regenerating all the GLVolumes. - //FIXME The update function shall just reload the modified matrices. - update(true); } -// This method will find an optimal orientation for the currently selected item -// Very similar in nature to the arrange method above... -void Plater::priv::sla_optimize_rotation() { - - // TODO: we should decide whether to allow arrange when the search is - // running we should probably disable explicit slicing and background - // processing - - if (rotoptimizing) { return; } - rotoptimizing = true; - Slic3r::ScopeGuard rotoptimizing_guard([this]() { rotoptimizing = false; }); - - int obj_idx = get_selected_object_idx(); +void Plater::priv::ExclusiveJobGroup::RotoptimizeJob::process() +{ + int obj_idx = plater().get_selected_object_idx(); if (obj_idx < 0) { return; } - ModelObject * o = model.objects[size_t(obj_idx)]; - - background_process.stop(); - - auto prev_range = statusbar()->get_range(); - statusbar()->set_range(100); - - auto stfn = [this] (unsigned st, const std::string& msg) { - statusbar()->set_progress(int(st)); - statusbar()->set_status_text(msg); - - // could be problematic, but we need the cancel button. - wxYieldIfNeeded(); - }; - - statusbar()->set_cancel_callback([this, stfn](){ - rotoptimizing = false; - stfn(0, L("Orientation search canceled")); - }); + ModelObject *o = plater().model.objects[size_t(obj_idx)]; auto r = sla::find_best_rotation( - *o, .005f, - [stfn](unsigned s) { stfn(s, L("Searching for optimal orientation")); }, - [this](){ return !rotoptimizing; } - ); + *o, + .005f, + [this](unsigned s) { + if (s < 100) + update_status(int(s), + _(L("Searching for optimal orientation"))); + }, + [this]() { return was_canceled(); }); - const auto *bed_shape_opt = config->opt("bed_shape"); + const auto *bed_shape_opt = + plater().config->opt("bed_shape"); + assert(bed_shape_opt); - auto& bedpoints = bed_shape_opt->values; - Polyline bed; bed.points.reserve(bedpoints.size()); - for(auto& v : bedpoints) bed.append(Point::new_scale(v(0), v(1))); + auto & bedpoints = bed_shape_opt->values; + Polyline bed; + bed.points.reserve(bedpoints.size()); + for (auto &v : bedpoints) bed.append(Point::new_scale(v(0), v(1))); double mindist = 6.0; // FIXME - double offs = mindist / 2.0 - EPSILON; - - if(rotoptimizing) // wasn't canceled - for(ModelInstance * oi : o->instances) { - oi->set_rotation({r[X], r[Y], r[Z]}); - - auto trchull = o->convex_hull_2d(oi->get_transformation().get_matrix()); - - namespace opt = libnest2d::opt; - opt::StopCriteria stopcr; - stopcr.relative_score_difference = 0.01; - stopcr.max_iterations = 10000; - stopcr.stop_score = 0.0; - opt::GeneticOptimizer solver(stopcr); - Polygon pbed(bed); - - auto bin = pbed.bounding_box(); - double binw = bin.size()(X) * SCALING_FACTOR - offs; - double binh = bin.size()(Y) * SCALING_FACTOR - offs; - - auto result = solver.optimize_min([&trchull, binw, binh](double rot){ - auto chull = trchull; - chull.rotate(rot); - - auto bb = chull.bounding_box(); - double bbw = bb.size()(X) * SCALING_FACTOR; - double bbh = bb.size()(Y) * SCALING_FACTOR; - - auto wdiff = bbw - binw; - auto hdiff = bbh - binh; - double diff = 0; - if(wdiff < 0 && hdiff < 0) diff = wdiff + hdiff; - if(wdiff > 0) diff += wdiff; - if(hdiff > 0) diff += hdiff; - - return diff; - }, opt::initvals(0.0), opt::bound(-PI/2, PI/2)); - - double r = std::get<0>(result.optimum); - - Vec3d rt = oi->get_rotation(); rt(Z) += r; - oi->set_rotation(rt); + + if (!was_canceled()) { + for(ModelInstance * oi : o->instances) { + oi->set_rotation({r[X], r[Y], r[Z]}); + + auto trmatrix = oi->get_transformation().get_matrix(); + Polygon trchull = o->convex_hull_2d(trmatrix); + + MinAreaBoundigBox rotbb(trchull, MinAreaBoundigBox::pcConvex); + double r = rotbb.angle_to_X(); + + // The box should be landscape + if(rotbb.width() < rotbb.height()) r += PI / 2; + + Vec3d rt = oi->get_rotation(); rt(Z) += r; + + oi->set_rotation(rt); + } + + arr::WipeTowerInfo wti; // useless in SLA context + arr::find_new_position(plater().model, + o->instances, + coord_t(mindist / SCALING_FACTOR), + bed, + wti); + + // Correct the z offset of the object which was corrupted be + // the rotation + o->ensure_on_bed(); } - arr::WipeTowerInfo wti; // useless in SLA context - arr::find_new_position(model, o->instances, coord_t(mindist/SCALING_FACTOR), bed, wti); - - // Correct the z offset of the object which was corrupted be the rotation - o->ensure_on_bed(); - - stfn(0, L("Orientation found.")); - statusbar()->set_range(prev_range); - statusbar()->set_cancel_callback(); - - update(true); + update_status(100, + was_canceled() ? _(L("Orientation search canceled.")) + : _(L("Orientation found."))); } void Plater::priv::split_object() @@ -2496,7 +2699,7 @@ unsigned int Plater::priv::update_background_process(bool force_validation) // Restart background processing thread based on a bitmask of UpdateBackgroundProcessReturnState. bool Plater::priv::restart_background_process(unsigned int state) { - if (arranging || rotoptimizing) { + if (m_ui_jobs.is_any_running()) { // Avoid a race condition return false; } @@ -2727,7 +2930,7 @@ void Plater::priv::on_select_preset(wxCommandEvent &evt) void Plater::priv::on_slicing_update(SlicingStatusEvent &evt) { if (evt.status.percent >= -1) { - if (arranging || rotoptimizing) { + if (m_ui_jobs.is_any_running()) { // Avoid a race condition return; } @@ -3208,7 +3411,7 @@ bool Plater::priv::can_fix_through_netfabb() const bool Plater::priv::can_increase_instances() const { - if (arranging || rotoptimizing) { + if (m_ui_jobs.is_any_running()) { return false; } @@ -3218,7 +3421,7 @@ bool Plater::priv::can_increase_instances() const bool Plater::priv::can_decrease_instances() const { - if (arranging || rotoptimizing) { + if (m_ui_jobs.is_any_running()) { return false; } @@ -3238,7 +3441,7 @@ bool Plater::priv::can_split_to_volumes() const bool Plater::priv::can_arrange() const { - return !model.objects.empty() && !arranging; + return !model.objects.empty() && !m_ui_jobs.is_any_running(); } bool Plater::priv::can_layers_editing() const @@ -3305,6 +3508,7 @@ SLAPrint& Plater::sla_print() { return p->sla_print; } void Plater::new_project() { + p->select_view_3D("3D"); wxPostEvent(p->view3D->get_wxglcanvas(), SimpleEvent(EVT_GLTOOLBAR_DELETE_ALL)); } @@ -3365,6 +3569,8 @@ void Plater::load_files(const std::vector& input_files, bool load_m void Plater::update() { p->update(); } +void Plater::stop_jobs() { p->m_ui_jobs.stop_all(); } + void Plater::update_ui_from_settings() { p->update_ui_from_settings(); } void Plater::select_view(const std::string& direction) { p->select_view(direction); } @@ -3565,7 +3771,7 @@ void Plater::export_stl(bool extended, bool selection_only) else { const GLVolume* volume = selection.get_volume(*selection.get_volume_idxs().begin()); - mesh = model_object->volumes[volume->volume_idx()]->mesh; + mesh = model_object->volumes[volume->volume_idx()]->mesh(); mesh.transform(volume->get_volume_transformation().get_matrix()); mesh.translate(-model_object->origin_translation.cast()); } @@ -3671,7 +3877,7 @@ void Plater::export_3mf(const boost::filesystem::path& output_path) if (!path.Lower().EndsWith(".3mf")) return; - DynamicPrintConfig cfg = wxGetApp().preset_bundle->full_config_secure(); + DynamicPrintConfig cfg = wxGetApp().preset_bundle->full_config_secure(); const std::string path_u8 = into_u8(path); wxBusyCursor wait; if (Slic3r::store_3mf(path_u8.c_str(), &p->model, export_config ? &cfg : nullptr)) { @@ -3687,6 +3893,9 @@ void Plater::export_3mf(const boost::filesystem::path& output_path) void Plater::reslice() { + // Stop arrange and (or) optimize rotation tasks. + this->stop_jobs(); + //FIXME Don't reslice if export of G-code or sending to OctoPrint is running. // bitmask of UpdateBackgroundProcessReturnState unsigned int state = this->p->update_background_process(true); @@ -3722,7 +3931,7 @@ void Plater::reslice_SLA_supports(const ModelObject &object) if (state & priv::UPDATE_BACKGROUND_PROCESS_REFRESH_SCENE) this->p->view3D->reload_scene(false); - if (this->p->background_process.empty() || (state & priv::UPDATE_BACKGROUND_PROCESS_INVALID)) + if (this->p->background_process.empty() || (state & priv::UPDATE_BACKGROUND_PROCESS_INVALID)) // Nothing to do on empty input or invalid configuration. return; diff --git a/src/slic3r/GUI/Plater.hpp b/src/slic3r/GUI/Plater.hpp index 16c9cbe649..761bf7a052 100644 --- a/src/slic3r/GUI/Plater.hpp +++ b/src/slic3r/GUI/Plater.hpp @@ -144,6 +144,7 @@ public: void load_files(const std::vector& input_files, bool load_model = true, bool load_config = true); void update(); + void stop_jobs(); void select_view(const std::string& direction); void select_view_3D(const std::string& name); diff --git a/src/slic3r/GUI/Preset.cpp b/src/slic3r/GUI/Preset.cpp index 22ddf3449e..7192d485ca 100644 --- a/src/slic3r/GUI/Preset.cpp +++ b/src/slic3r/GUI/Preset.cpp @@ -509,6 +509,7 @@ const std::vector& Preset::sla_printer_options() "printer_technology", "bed_shape", "max_print_height", "display_width", "display_height", "display_pixels_x", "display_pixels_y", + "display_mirror_x", "display_mirror_y", "display_orientation", "fast_tilt_time", "slow_tilt_time", "area_fill", "relative_correction", diff --git a/src/slic3r/GUI/PresetBundle.cpp b/src/slic3r/GUI/PresetBundle.cpp index fb3b6f7a47..b28cb2eda7 100644 --- a/src/slic3r/GUI/PresetBundle.cpp +++ b/src/slic3r/GUI/PresetBundle.cpp @@ -781,7 +781,7 @@ void PresetBundle::load_config_file_config(const std::string &name_or_path, bool if (i == 0) suffix[0] = 0; else - sprintf(suffix, "%d", i); + sprintf(suffix, "%d", (int)i); std::string new_name = name + suffix; loaded = &this->filaments.load_preset(this->filaments.path_from_name(new_name), new_name, std::move(cfg), i == 0); @@ -837,7 +837,7 @@ void PresetBundle::load_config_file_config_bundle(const std::string &path, const return preset_name_dst; // Try to generate another name. char buf[64]; - sprintf(buf, " (%d)", i); + sprintf(buf, " (%d)", (int)i); preset_name_dst = preset_name_src + buf + bundle_name; } } @@ -1379,7 +1379,7 @@ void PresetBundle::export_configbundle(const std::string &path, bool export_syst for (size_t i = 0; i < this->filament_presets.size(); ++ i) { char suffix[64]; if (i > 0) - sprintf(suffix, "_%d", i); + sprintf(suffix, "_%d", (int)i); else suffix[0] = 0; c << "filament" << suffix << " = " << this->filament_presets[i] << std::endl; diff --git a/src/slic3r/GUI/ProgressStatusBar.cpp b/src/slic3r/GUI/ProgressStatusBar.cpp index b48c5732b5..f848e663d2 100644 --- a/src/slic3r/GUI/ProgressStatusBar.cpp +++ b/src/slic3r/GUI/ProgressStatusBar.cpp @@ -168,6 +168,11 @@ void ProgressStatusBar::set_status_text(const char *txt) this->set_status_text(wxString::FromUTF8(txt)); } +wxString ProgressStatusBar::get_status_text() const +{ + return self->GetStatusText(); +} + void ProgressStatusBar::show_cancel_button() { if(m_cancelbutton) m_cancelbutton->Show(); diff --git a/src/slic3r/GUI/ProgressStatusBar.hpp b/src/slic3r/GUI/ProgressStatusBar.hpp index 225b0331ef..7d624af90b 100644 --- a/src/slic3r/GUI/ProgressStatusBar.hpp +++ b/src/slic3r/GUI/ProgressStatusBar.hpp @@ -3,6 +3,7 @@ #include #include +#include class wxTimer; class wxGauge; @@ -51,6 +52,7 @@ public: void set_status_text(const wxString& txt); void set_status_text(const std::string& txt); void set_status_text(const char *txt); + wxString get_status_text() const; // Temporary methods to satisfy Perl side void show_cancel_button(); diff --git a/src/slic3r/GUI/Selection.cpp b/src/slic3r/GUI/Selection.cpp index ff994c32d5..480b59ba03 100644 --- a/src/slic3r/GUI/Selection.cpp +++ b/src/slic3r/GUI/Selection.cpp @@ -296,6 +296,9 @@ void Selection::clear() if (!m_valid) return; + if (m_list.empty()) + return; + for (unsigned int i : m_list) { (*m_volumes)[i]->selected = false; diff --git a/src/slic3r/GUI/Selection.hpp b/src/slic3r/GUI/Selection.hpp index 54bf52706b..5da1e477bc 100644 --- a/src/slic3r/GUI/Selection.hpp +++ b/src/slic3r/GUI/Selection.hpp @@ -333,6 +333,8 @@ private: void render_sidebar_rotation_hint(Axis axis) const; void render_sidebar_scale_hint(Axis axis) const; void render_sidebar_size_hint(Axis axis, double length) const; + +public: enum SyncRotationType { // Do not synchronize rotation. Either not rotating at all, or rotating by world Z axis. SYNC_ROTATION_NONE = 0, @@ -343,6 +345,8 @@ private: }; void synchronize_unselected_instances(SyncRotationType sync_rotation_type); void synchronize_unselected_volumes(); + +private: void ensure_on_bed(); bool is_from_fully_selected_instance(unsigned int volume_idx) const; diff --git a/src/slic3r/GUI/Tab.cpp b/src/slic3r/GUI/Tab.cpp index 6cd270e5b3..22aecf38d8 100644 --- a/src/slic3r/GUI/Tab.cpp +++ b/src/slic3r/GUI/Tab.cpp @@ -1854,13 +1854,17 @@ void TabPrinter::build_fff() btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e) { - auto dlg = new BedShapeDialog(this); - dlg->build_dialog(m_config->option("bed_shape")); - if (dlg->ShowModal() == wxID_OK) { - load_key_value("bed_shape", dlg->GetValue()); - update_changed_ui(); - } - })); + BedShapeDialog dlg(this); + dlg.build_dialog(m_config->option("bed_shape")); + if (dlg.ShowModal() == wxID_OK) { + std::vector shape = dlg.GetValue(); + if (!shape.empty()) + { + load_key_value("bed_shape", shape); + update_changed_ui(); + } + } + })); return sizer; }; @@ -2056,11 +2060,15 @@ void TabPrinter::build_sla() btn->Bind(wxEVT_BUTTON, ([this](wxCommandEvent e) { - auto dlg = new BedShapeDialog(this); - dlg->build_dialog(m_config->option("bed_shape")); - if (dlg->ShowModal() == wxID_OK) { - load_key_value("bed_shape", dlg->GetValue()); - update_changed_ui(); + BedShapeDialog dlg(this); + dlg.build_dialog(m_config->option("bed_shape")); + if (dlg.ShowModal() == wxID_OK) { + std::vector shape = dlg.GetValue(); + if (!shape.empty()) + { + load_key_value("bed_shape", shape); + update_changed_ui(); + } } })); @@ -2079,6 +2087,10 @@ void TabPrinter::build_sla() line.append_option(optgroup->get_option("display_pixels_y")); optgroup->append_line(line); optgroup->append_single_option_line("display_orientation"); + + // FIXME: This should be on one line in the UI + optgroup->append_single_option_line("display_mirror_x"); + optgroup->append_single_option_line("display_mirror_y"); optgroup = page->new_optgroup(_(L("Tilt"))); line = { _(L("Tilt time")), "" }; diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 76ba853dc3..406daccf55 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -586,7 +586,7 @@ wxDataViewItem ObjectDataViewModel::AddVolumeChild( const wxDataViewItem &parent ItemAdded(parent_item, child); root->m_volumes_cnt++; - if (insert_position > 0) insert_position++; + if (insert_position >= 0) insert_position++; } const auto node = new ObjectDataViewModelNode(root, name, GetVolumeIcon(volume_type, has_errors), extruder_str, root->m_volumes_cnt); @@ -1587,6 +1587,7 @@ DoubleSlider::DoubleSlider( wxWindow *parent, // slider events Bind(wxEVT_PAINT, &DoubleSlider::OnPaint, this); + Bind(wxEVT_CHAR, &DoubleSlider::OnChar, this); Bind(wxEVT_LEFT_DOWN, &DoubleSlider::OnLeftDown, this); Bind(wxEVT_MOTION, &DoubleSlider::OnMotion, this); Bind(wxEVT_LEFT_UP, &DoubleSlider::OnLeftUp, this); @@ -2366,9 +2367,9 @@ void DoubleSlider::OnWheel(wxMouseEvent& event) void DoubleSlider::OnKeyDown(wxKeyEvent &event) { const int key = event.GetKeyCode(); - if (key == '+' || key == WXK_NUMPAD_ADD) + if (key == WXK_NUMPAD_ADD) action_tick(taAdd); - else if (key == '-' || key == 390 || key == WXK_DELETE || key == WXK_BACK) + else if (key == 390 || key == WXK_DELETE || key == WXK_BACK) action_tick(taDel); else if (is_horizontal()) { @@ -2387,6 +2388,8 @@ void DoubleSlider::OnKeyDown(wxKeyEvent &event) else if (key == WXK_UP || key == WXK_DOWN) move_current_thumb(key == WXK_UP); } + + event.Skip(); // !Needed to have EVT_CHAR generated as well } void DoubleSlider::OnKeyUp(wxKeyEvent &event) @@ -2398,6 +2401,15 @@ void DoubleSlider::OnKeyUp(wxKeyEvent &event) event.Skip(); } +void DoubleSlider::OnChar(wxKeyEvent& event) +{ + const int key = event.GetKeyCode(); + if (key == '+') + action_tick(taAdd); + else if (key == '-') + action_tick(taDel); +} + void DoubleSlider::OnRightDown(wxMouseEvent& event) { this->CaptureMouse(); @@ -2460,6 +2472,9 @@ LockButton::LockButton( wxWindow *parent, void LockButton::OnButton(wxCommandEvent& event) { + if (m_disabled) + return; + m_is_pushed = !m_is_pushed; enter_button(true); diff --git a/src/slic3r/GUI/wxExtensions.hpp b/src/slic3r/GUI/wxExtensions.hpp index 78fb7be55b..dd035690ad 100644 --- a/src/slic3r/GUI/wxExtensions.hpp +++ b/src/slic3r/GUI/wxExtensions.hpp @@ -727,6 +727,7 @@ public: void OnWheel(wxMouseEvent& event); void OnKeyDown(wxKeyEvent &event); void OnKeyUp(wxKeyEvent &event); + void OnChar(wxKeyEvent &event); void OnRightDown(wxMouseEvent& event); void OnRightUp(wxMouseEvent& event); @@ -837,9 +838,13 @@ public: void OnEnterBtn(wxMouseEvent& event) { enter_button(true); event.Skip(); } void OnLeaveBtn(wxMouseEvent& event) { enter_button(false); event.Skip(); } - bool IsLocked() const { return m_is_pushed; } + bool IsLocked() const { return m_is_pushed; } void SetLock(bool lock); + // create its own Enable/Disable functions to not really disabled button because of tooltip enabling + void enable() { m_disabled = false; } + void disable() { m_disabled = true; } + void msw_rescale(); protected: @@ -847,6 +852,7 @@ protected: private: bool m_is_pushed = false; + bool m_disabled = false; ScalableBitmap m_bmp_lock_on; ScalableBitmap m_bmp_lock_off; diff --git a/src/slic3r/Utils/FixModelByWin10.cpp b/src/slic3r/Utils/FixModelByWin10.cpp index 1daeaff269..710f19090b 100644 --- a/src/slic3r/Utils/FixModelByWin10.cpp +++ b/src/slic3r/Utils/FixModelByWin10.cpp @@ -389,10 +389,10 @@ void fix_model_by_win10_sdk_gui(ModelObject &model_object, int volume_idx) throw std::runtime_error(L("Repaired 3MF file does not contain any volume")); if (model.objects.front()->volumes.size() > 1) throw std::runtime_error(L("Repaired 3MF file contains more than one volume")); - meshes_repaired.emplace_back(std::move(model.objects.front()->volumes.front()->mesh)); + meshes_repaired.emplace_back(std::move(model.objects.front()->volumes.front()->mesh())); } for (size_t i = 0; i < volumes.size(); ++ i) { - volumes[i]->mesh = std::move(meshes_repaired[i]); + volumes[i]->set_mesh(std::move(meshes_repaired[i])); volumes[i]->set_new_unique_id(); } model_object.invalidate_bounding_box(); diff --git a/src/slic3r/Utils/Serial.cpp b/src/slic3r/Utils/Serial.cpp index 601719b50e..cd2a01cbfd 100644 --- a/src/slic3r/Utils/Serial.cpp +++ b/src/slic3r/Utils/Serial.cpp @@ -384,7 +384,13 @@ void Serial::reset_line_num() bool Serial::read_line(unsigned timeout, std::string &line, error_code &ec) { - auto &io_service = get_io_service(); + auto& io_service = +#if BOOST_VERSION >= 107000 + //FIXME this is most certainly wrong! + (boost::asio::io_context&)this->get_executor().context(); + #else + this->get_io_service(); +#endif asio::deadline_timer timer(io_service); char c = 0; bool fail = false; diff --git a/xs/CMakeLists.txt b/xs/CMakeLists.txt index 4696badc49..aaf943970e 100644 --- a/xs/CMakeLists.txt +++ b/xs/CMakeLists.txt @@ -119,8 +119,6 @@ target_include_directories(XS PRIVATE src ${LIBDIR}/libslic3r) target_compile_definitions(XS PRIVATE -DSLIC3RXS) set_target_properties(XS PROPERTIES PREFIX "") # Prevent cmake from generating libXS.so instead of XS.so -target_link_libraries(XS ${Boost_LIBRARIES}) - if (APPLE) # -liconv: boost links to libiconv by default target_link_libraries(XS "-liconv -framework IOKit" "-framework CoreFoundation" -lc++) @@ -156,12 +154,6 @@ if (WIN32) target_link_libraries(XS ${PERL_LIBRARY}) endif() -target_link_libraries(XS ${Boost_LIBRARIES}) -target_link_libraries(XS ${TBB_LIBRARIES}) -# target_link_libraries(XS ${wxWidgets_LIBRARIES}) -target_link_libraries(XS ${EXPAT_LIBRARIES}) -# target_link_libraries(XS ${GLEW_LIBRARIES}) - # Install the XS.pm and XS.{so,dll,bundle} into the local-lib directory. set(PERL_LOCAL_LIB_DIR "../../local-lib/lib/perl5/${PerlEmbed_ARCHNAME}") add_custom_command( @@ -181,10 +173,6 @@ if(APPLE) ) endif() -if(SLIC3R_PROFILE) - target_link_libraries(Shiny) -endif() - if (MSVC) # Here we associate some additional properties with the MSVC project to enable compilation and debugging out of the box. get_filename_component(PROPS_PERL_BIN_PATH "${PERL_EXECUTABLE}" DIRECTORY) diff --git a/xs/src/xsinit.h b/xs/src/xsinit.h index 506ef0a0be..f14e1262dc 100644 --- a/xs/src/xsinit.h +++ b/xs/src/xsinit.h @@ -69,7 +69,10 @@ extern "C" { #undef fwrite #undef fclose #undef sleep + #undef snprintf + #undef strerror #undef test + #undef times #undef accept #undef wait diff --git a/xs/xsp/Model.xsp b/xs/xsp/Model.xsp index a1c8890ef5..6a2cc60802 100644 --- a/xs/xsp/Model.xsp +++ b/xs/xsp/Model.xsp @@ -253,7 +253,7 @@ ModelMaterial::attributes() Ref config() %code%{ RETVAL = &THIS->config; %}; Ref mesh() - %code%{ RETVAL = &THIS->mesh; %}; + %code%{ RETVAL = &THIS->mesh(); %}; bool modifier() %code%{ RETVAL = THIS->is_modifier(); %}; diff --git a/xs/xsp/TriangleMesh.xsp b/xs/xsp/TriangleMesh.xsp index e519f9210c..f3153665cb 100644 --- a/xs/xsp/TriangleMesh.xsp +++ b/xs/xsp/TriangleMesh.xsp @@ -46,7 +46,6 @@ TriangleMesh::ReadFromPerl(vertices, facets) SV* facets CODE: stl_file &stl = THIS->stl; - stl.error = 0; stl.stats.type = inmemory; // count facets and allocate memory @@ -99,20 +98,18 @@ SV* TriangleMesh::vertices() CODE: if (!THIS->repaired) CONFESS("vertices() requires repair()"); - - if (THIS->stl.v_shared == NULL) - stl_generate_shared_vertices(&(THIS->stl)); + THIS->require_shared_vertices(); // vertices AV* vertices = newAV(); - av_extend(vertices, THIS->stl.stats.shared_vertices); - for (int i = 0; i < THIS->stl.stats.shared_vertices; i++) { + av_extend(vertices, THIS->its.vertices.size()); + for (size_t i = 0; i < THIS->its.vertices.size(); i++) { AV* vertex = newAV(); av_store(vertices, i, newRV_noinc((SV*)vertex)); av_extend(vertex, 2); - av_store(vertex, 0, newSVnv(THIS->stl.v_shared[i](0))); - av_store(vertex, 1, newSVnv(THIS->stl.v_shared[i](1))); - av_store(vertex, 2, newSVnv(THIS->stl.v_shared[i](2))); + av_store(vertex, 0, newSVnv(THIS->its.vertices[i](0))); + av_store(vertex, 1, newSVnv(THIS->its.vertices[i](1))); + av_store(vertex, 2, newSVnv(THIS->its.vertices[i](2))); } RETVAL = newRV_noinc((SV*)vertices); @@ -123,9 +120,7 @@ SV* TriangleMesh::facets() CODE: if (!THIS->repaired) CONFESS("facets() requires repair()"); - - if (THIS->stl.v_shared == NULL) - stl_generate_shared_vertices(&(THIS->stl)); + THIS->require_shared_vertices(); // facets AV* facets = newAV(); @@ -134,9 +129,9 @@ TriangleMesh::facets() AV* facet = newAV(); av_store(facets, i, newRV_noinc((SV*)facet)); av_extend(facet, 2); - av_store(facet, 0, newSVnv(THIS->stl.v_indices[i].vertex[0])); - av_store(facet, 1, newSVnv(THIS->stl.v_indices[i].vertex[1])); - av_store(facet, 2, newSVnv(THIS->stl.v_indices[i].vertex[2])); + av_store(facet, 0, newSVnv(THIS->its.indices[i][0])); + av_store(facet, 1, newSVnv(THIS->its.indices[i][1])); + av_store(facet, 2, newSVnv(THIS->its.indices[i][2])); } RETVAL = newRV_noinc((SV*)facets);