You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
pybindmagic/examples/test.ipynb

308 lines
6.8 KiB

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pybindmagic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -f hello_world\n",
"\n",
"void hello_world()\n",
"{\n",
" py::print(\"Hello World\");\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"hello_world()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -f callothercell -f callthiscell\n",
"\n",
"void hello_world();\n",
"\n",
"void callthiscell()\n",
"{\n",
" py::print(\"In this cell\");\n",
"}\n",
"\n",
"void callothercell()\n",
"{\n",
" py::print(\"From Other Cell:\");\n",
" hello_world();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"callthiscell()\n",
"\n",
"callothercell()\n",
"#try to update the first cell an run callothercell() again"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -f generateMesh\n",
"#include <pybind11/eigen.h>\n",
"\n",
"std::tuple<Eigen::MatrixXd,Eigen::MatrixXi> 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": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"V,F = generateMesh()\n",
"print(\"Vertices:\\n\",V)\n",
"print(\"Faces:\\n\",F)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp\n",
"#include <pybind11/eigen.h>\n",
"\n",
"\n",
"std::tuple<Eigen::MatrixXd,Eigen::MatrixXi> 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": null,
"metadata": {},
"outputs": [],
"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": null,
"metadata": {},
"outputs": [],
"source": [
"%%cpp -c pathtocmake -f viewMesh -rebuild\n",
"\n",
"#include <igl/opengl/glfw/Viewer.h>\n",
"#include <pybind11/eigen.h>\n",
"\n",
"\n",
"void viewMesh(Eigen::Matrix<double, Eigen::Dynamic, 3> V, Eigen::Matrix<int, Eigen::Dynamic, 3> 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": null,
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.3"
},
"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
}