以前,http://shitohichiumaya.blogspot.com/2010/08/boostpython-how-to-pass-python-object.htmlにて, python の dict や list,つまり python の buildin の class のobject を C++ のコードに渡すという話をした.
今回は user の作った python の class の object を渡すということをしてみよう.また,numpy.array の numpy.int64 を extract< int > しようとすると起こる TypeError (TypeError: No registered converter was able to produce aC++ rvalue of type int from this Python object of type numpy.int64())をどうやって避けたら良いかについて述べよう.
まずは python 側のコードである.
Ameba ではコードを正しく表示できないので,コードに関してはこちらの記事をご覧下さい.
---python code ---
このコードは単純な三次元の三角形メッシュのクラスを定義している.各三角形の頂点は 3つの float値を持つ numpy.array を使って表現されており,面は 頂点の index を使って表現されている. index が 1 から始まっているのは,wavefront obj ファイルという三次元の形状を定義するファイルと同じ構成のためである.また,メッシュは material の名前を private なメンバに持っている.この TriMesh の object にアクセスするための boost.python C++ コードは以下である.
---Code C++ ---
コンパイル方法は以下の通り.もちろん python boost が install されていることが前提だ.
---sh-script---
この python code を走らせると,結果は以下のようになる.
@[34]bash % python test_passobj2_mod.py
triangle mesh info
# materialname = diffuse_material
# vertices = 4
# faces = 2
trimesh.get_material_name() = diffuse_material
--- vertex_list ---
len(numpy_list_obj) = 4
float[3] = 552.8 0 0
float[3] = 0 0 0
float[3] = 0 0 559.2
float[3] = 549.6 0 559.2
--- face_idx_list ---
len(numpy_list_obj) = 2
int[3] = 1 2 3
int[3] = 1 3 4
C++ の出力が python で入力したものと一致することがわかる.今回はコードを示した,次回からは少し詳し内容を見ていこう.