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/main.py

177 lines
2.8 KiB

import pybindmagic
#%%
%%cpp -f hello_world
void hello_world()
{
py::print("Hello World...");
}
#%%
hello_world()
#%%
%%cpp -f callothercell -f callthiscell
void hello_world();
void callthiscell()
{
py::print("In this cell");
}
void callothercell()
{
py::print("From Other Cell:");
hello_world();
}
#%%
callthiscell()
callothercell()
#try to update the first cell an run callothercell() again
#%%
%%cpp -f transitiv
void callothercell();
void transitiv()
{
py::print("Cell A:");
callothercell();
}
#%%
transitiv()
#%%
%%cpp -f generateMesh
#include <pybind11/eigen.h>
std::tuple<Eigen::MatrixXd,Eigen::MatrixXi> generateMesh()
{
// Inline mesh of a cube
const Eigen::MatrixXd V = (Eigen::MatrixXd(8,3)<<
0.0,0.0,0.0,
0.0,0.0,1.0,
0.0,1.0,0.0,
0.0,1.0,1.0,
1.0,0.0,0.0,
1.0,0.0,1.0,
1.0,1.0,0.0,
1.0,1.0,1.0).finished();
const Eigen::MatrixXi F = (Eigen::MatrixXi(12,3)<<
1,7,5,
1,3,7,
1,4,3,
1,2,4,
3,8,7,
3,4,8,
5,7,8,
5,8,6,
1,5,6,
1,6,2,
2,6,8,
2,8,4).finished().array()-1;
return std::make_tuple(V,F);
}
#%%
import JV9NK
V,F = generateMesh()
print("Vertices:\n",V)
print("Faces:\n",F)
#%%
%%cpp
#include <pybind11/eigen.h>
std::tuple<Eigen::MatrixXd,Eigen::MatrixXi> generateMesh()
{
// Inline mesh of a cube
const Eigen::MatrixXd V = (Eigen::MatrixXd(8,3)<<
0.0,0.0,0.0,
0.0,0.0,1.0,
0.0,1.0,0.0,
0.0,1.0,1.0,
1.0,0.0,0.0,
1.0,0.0,1.0,
1.0,1.0,0.0,
1.0,1.0,1.0).finished();
const Eigen::MatrixXi F = (Eigen::MatrixXi(12,3)<<
1,7,5,
1,3,7,
1,4,3,
1,2,4,
3,8,7,
3,4,8,
5,7,8,
5,8,6,
1,5,6,
1,6,2,
2,6,8,
2,8,4).finished().array()-1;
return std::make_tuple(V,F);
}
void printMesh(Eigen::MatrixXd V,Eigen::MatrixXi F)
{
py::print("Vertices:\n", V);
py::print("Faces:\n", F);
}
defs
m.def("generateMesh",&generateMesh);
m.def("printMesh",&printMesh);
#%%
printMesh(*generateMesh())
#%%
%%cpp -c pathtocmake -f viewMesh
#include <igl/opengl/glfw/Viewer.h>
#include <pybind11/eigen.h>
void viewMesh(Eigen::Matrix<double, Eigen::Dynamic, 3> V, Eigen::Matrix<int, Eigen::Dynamic, 3> F)
{
// Plot the mesh
igl::opengl::glfw::Viewer viewer;
viewer.data().set_mesh(V, F);
viewer.data().set_face_based(true);
viewer.launch();
}
#%%
import numpy as np
V = np.array([
[0.0,0.0,0.0],
[0.0,0.0,1.0],
[0.0,1.0,0.0],
[0.0,1.0,1.0],
[1.0,0.0,0.0],
[1.0,0.0,1.0],
[1.0,1.0,0.0],
[1.0,1.0,1.0]])
F = np.array(
[
[1,7,5],
[1,3,7],
[1,4,3],
[1,2,4],
[3,8,7],
[3,4,8],
[5,7,8],
[5,8,6],
[1,5,6],
[1,6,2],
[2,6,8],
[2,8,4]
], dtype=np.int32)-1
viewMesh(V,F)