{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pybindmagic" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "%%cpp -f generateMesh\n", "#include \n", "\n", "\n", "std::tuple generateMesh()\n", "{\n", " // Inline mesh of a cube\n", " const Eigen::MatrixXd V = (Eigen::MatrixXd(8,3)<<\n", " 0.0,0.0,0.0,\n", " 0.0,0.0,1.0,\n", " 0.0,1.0,0.0,\n", " 0.0,1.0,1.0,\n", " 1.0,0.0,0.0,\n", " 1.0,0.0,1.0,\n", " 1.0,1.0,0.0,\n", " 1.0,1.0,1.0).finished();\n", " const Eigen::MatrixXi F = (Eigen::MatrixXi(12,3)<<\n", " 1,7,5,\n", " 1,3,7,\n", " 1,4,3,\n", " 1,2,4,\n", " 3,8,7,\n", " 3,4,8,\n", " 5,7,8,\n", " 5,8,6,\n", " 1,5,6,\n", " 1,6,2,\n", " 2,6,8,\n", " 2,8,4).finished().array()-1;\n", " return std::make_tuple(V,F);\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vertices:\n", " [[0. 0. 0.]\n", " [0. 0. 1.]\n", " [0. 1. 0.]\n", " [0. 1. 1.]\n", " [1. 0. 0.]\n", " [1. 0. 1.]\n", " [1. 1. 0.]\n", " [1. 1. 1.]]\n", "Faces:\n", " [[0 6 4]\n", " [0 2 6]\n", " [0 3 2]\n", " [0 1 3]\n", " [2 7 6]\n", " [2 3 7]\n", " [4 6 7]\n", " [4 7 5]\n", " [0 4 5]\n", " [0 5 1]\n", " [1 5 7]\n", " [1 7 3]]\n" ] } ], "source": [ "V,F = generateMesh()\n", "print(\"Vertices:\\n\",V)\n", "print(\"Faces:\\n\",F)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "%%cpp\n", "#include \n", "\n", "\n", "std::tuple generateMesh()\n", "{\n", " // Inline mesh of a cube\n", " const Eigen::MatrixXd V = (Eigen::MatrixXd(8,3)<<\n", " 0.0,0.0,0.0,\n", " 0.0,0.0,1.0,\n", " 0.0,1.0,0.0,\n", " 0.0,1.0,1.0,\n", " 1.0,0.0,0.0,\n", " 1.0,0.0,1.0,\n", " 1.0,1.0,0.0,\n", " 1.0,1.0,1.0).finished();\n", " const Eigen::MatrixXi F = (Eigen::MatrixXi(12,3)<<\n", " 1,7,5,\n", " 1,3,7,\n", " 1,4,3,\n", " 1,2,4,\n", " 3,8,7,\n", " 3,4,8,\n", " 5,7,8,\n", " 5,8,6,\n", " 1,5,6,\n", " 1,6,2,\n", " 2,6,8,\n", " 2,8,4).finished().array()-1;\n", " return std::make_tuple(V,F);\n", "}\n", "\n", "void printMesh(Eigen::MatrixXd V,Eigen::MatrixXi F)\n", "{\n", " py::print(\"Vertices:\\n\", V);\n", " py::print(\"Faces:\\n\", F);\n", "}\n", "\n", "defs\n", " m.def(\"generateMesh\",&generateMesh);\n", " m.def(\"printMesh\",&printMesh);" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vertices:\n", " [[0. 0. 0.]\n", " [0. 0. 1.]\n", " [0. 1. 0.]\n", " [0. 1. 1.]\n", " [1. 0. 0.]\n", " [1. 0. 1.]\n", " [1. 1. 0.]\n", " [1. 1. 1.]]\n", "Faces:\n", " [[0 6 4]\n", " [0 2 6]\n", " [0 3 2]\n", " [0 1 3]\n", " [2 7 6]\n", " [2 3 7]\n", " [4 6 7]\n", " [4 7 5]\n", " [0 4 5]\n", " [0 5 1]\n", " [1 5 7]\n", " [1 7 3]]\n" ] } ], "source": [ "printMesh(*generateMesh())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# CMAKE example\n", "\n", "Take a look at _pathtocmake/CMAKEList.txt_. Its a template cmake file. Make sure that the following lines are present:\n", "\n", "```\n", "project({name} LANGUAGES CXX)\n", "\n", "find_package(Python COMPONENTS Interpreter Development REQUIRED)\n", "add_subdirectory(pybind11)\n", "pybind11_add_module(${PROJECT_NAME} ${SRCFILES})\n", "```\n", "\n", "This example needs libigl in the pathtocmake folder. Clone it using:\n", "\n", "`git clone https://github.com/libigl/libigl.git`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "cmake:\n", "-- The CXX compiler identification is GNU 9.3.0\n", "-- Check for working CXX compiler: /usr/bin/c++\n", "-- Check for working CXX compiler: /usr/bin/c++ -- works\n", "-- Detecting CXX compiler ABI info\n", "-- Detecting CXX compiler ABI info - done\n", "-- Detecting CXX compile features\n", "-- Detecting CXX compile features - done\n", "-- Found Python: /home/ugo/anaconda3/bin/python3.7 (found version \"3.7.6\") found components: Interpreter Development \n", "-- pybind11 v2.6.3 dev1\n", "-- Performing Test HAS_FLTO\n", "-- Performing Test HAS_FLTO - Success\n", "-- Looking for C++ include pthread.h\n", "-- Looking for C++ include pthread.h - found\n", "-- Performing Test CMAKE_HAVE_LIBC_PTHREAD\n", "-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed\n", "-- Looking for pthread_create in pthreads\n", "-- Looking for pthread_create in pthreads - not found\n", "-- Looking for pthread_create in pthread\n", "-- Looking for pthread_create in pthread - found\n", "-- Found Threads: TRUE \n", "-- Creating target: igl::core (igl)\n", "-- Creating target: igl::opengl (igl_opengl)\n", "-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so found components: OpenGL \n", "-- The C compiler identification is GNU 9.3.0\n", "-- Check for working C compiler: /usr/bin/cc\n", "-- Check for working C compiler: /usr/bin/cc -- works\n", "-- Detecting C compiler ABI info\n", "-- Detecting C compiler ABI info - done\n", "-- Detecting C compile features\n", "-- Detecting C compile features - done\n", "-- Creating target: igl::opengl_glfw (igl_opengl_glfw)\n", "-- Using X11 for window creation\n", "-- Found X11: /usr/include \n", "-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so\n", "-- Looking for XOpenDisplay in /usr/lib/x86_64-linux-gnu/libX11.so;/usr/lib/x86_64-linux-gnu/libXext.so - found\n", "-- Looking for gethostbyname\n", "-- Looking for gethostbyname - found\n", "-- Looking for connect\n", "-- Looking for connect - found\n", "-- Looking for remove\n", "-- Looking for remove - found\n", "-- Looking for shmat\n", "-- Looking for shmat - found\n", "-- Looking for IceConnectionNumber in ICE\n", "-- Looking for IceConnectionNumber in ICE - found\n", "-- Configuring done\n", "-- Generating done\n", "-- Build files have been written to: /home/ugo/work/pybindipynb/examples/pathtocmake/build\n", "\n", "make:\n", "Scanning dependencies of target glad\n", "[ 4%] Building C object glad/CMakeFiles/glad.dir/src/glad.c.o\n", "[ 9%] Linking C static library libglad.a\n", "[ 9%] Built target glad\n", "Scanning dependencies of target glfw\n", "[ 14%] Building C object glfw/src/CMakeFiles/glfw.dir/context.c.o\n", "[ 19%] Building C object glfw/src/CMakeFiles/glfw.dir/init.c.o\n", "[ 23%] Building C object glfw/src/CMakeFiles/glfw.dir/input.c.o\n", "[ 28%] Building C object glfw/src/CMakeFiles/glfw.dir/monitor.c.o\n", "[ 33%] Building C object glfw/src/CMakeFiles/glfw.dir/vulkan.c.o\n", "[ 38%] Building C object glfw/src/CMakeFiles/glfw.dir/window.c.o\n", "[ 42%] Building C object glfw/src/CMakeFiles/glfw.dir/x11_init.c.o\n", "[ 47%] Building C object glfw/src/CMakeFiles/glfw.dir/x11_monitor.c.o\n", "[ 52%] Building C object glfw/src/CMakeFiles/glfw.dir/x11_window.c.o\n", "[ 57%] Building C object glfw/src/CMakeFiles/glfw.dir/xkb_unicode.c.o\n", "[ 61%] Building C object glfw/src/CMakeFiles/glfw.dir/posix_time.c.o\n", "[ 66%] Building C object glfw/src/CMakeFiles/glfw.dir/posix_thread.c.o\n", "[ 71%] Building C object glfw/src/CMakeFiles/glfw.dir/glx_context.c.o\n", "[ 76%] Building C object glfw/src/CMakeFiles/glfw.dir/egl_context.c.o\n", "[ 80%] Building C object glfw/src/CMakeFiles/glfw.dir/osmesa_context.c.o\n", "[ 85%] Building C object glfw/src/CMakeFiles/glfw.dir/linux_joystick.c.o\n", "[ 90%] Linking C static library libglfw3.a\n", "[ 90%] Built target glfw\n", "Scanning dependencies of target cpp_magic_5329071abe\n", "[ 95%] Building CXX object CMakeFiles/cpp_magic_5329071abe.dir/cpp_magic_5329071abe.cpp.o\n", "[100%] Linking CXX shared module cpp_magic_5329071abe.cpython-37m-x86_64-linux-gnu.so\n", "[100%] Built target cpp_magic_5329071abe\n", "\n" ] } ], "source": [ "%%cpp -c pathtocmake -f viewMesh -rebuild\n", "\n", "#include \n", "#include \n", "\n", "\n", "void viewMesh(Eigen::Matrix V, Eigen::Matrix F)\n", "{\n", " // Plot the mesh\n", " igl::opengl::glfw::Viewer viewer;\n", " viewer.data().set_mesh(V, F);\n", " viewer.data().set_face_based(true);\n", " viewer.launch();\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "dynamic module does not define module export function (PyInit_cpp_magic_f96b7fce74)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mcpp_magic_f96b7fce74\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;31m#viewMesh(V,F)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mImportError\u001b[0m: dynamic module does not define module export function (PyInit_cpp_magic_f96b7fce74)" ] } ], "source": [ "import cpp_magic_f96b7fce74\n", "#viewMesh(V,F)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "V = np.array([\n", " [0.0,0.0,0.0],\n", " [0.0,0.0,1.0],\n", " [0.0,1.0,0.0],\n", " [0.0,1.0,1.0],\n", " [1.0,0.0,0.0],\n", " [1.0,0.0,1.0],\n", " [1.0,1.0,0.0],\n", " [1.0,1.0,1.0]])\n", "\n", "F = np.array(\n", " [\n", " [1,7,5],\n", " [1,3,7],\n", " [1,4,3],\n", " [1,2,4],\n", " [3,8,7],\n", " [3,4,8],\n", " [5,7,8],\n", " [5,8,6],\n", " [1,5,6],\n", " [1,6,2],\n", " [2,6,8],\n", " [2,8,4]\n", " ], dtype=np.int32)-1\n", "viewMesh(V,F)" ] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false } }, "nbformat": 4, "nbformat_minor": 2 }