繁体   English   中英

混合使用VTK和SWIG Python

[英]mix VTK and SWIG Python

这是我的课:

#include <vtkPolyData>

class VTKUtilities

Mesh3D MeshfromVTKPolyData(vtkPolyData* pdmesh)
{
   Mesh3D mesh;
   //...
   //my conversion code to do the actual conversion
   //...
   return mesh;
}

我试图通过SWIG将其包装到python中,但是我尝试像这样在python中调用我的函数:

import VTKUtilities
import vtk

pd = vtk.vtkPolyData()
VTKUtilities.MeshfromVTKPolyData(pd)

我收到如下错误:

NotImplementedError: Wrong number of arguments... for VTKUtilities_MeshfromVTKPolyData
...
Possible prototypes are VTKUtilities::MeshfromVTKPolyData(vtkPolyData *);

我一直在阅读有关类型映射的内容,但是我认为我不必为此烦恼,因为SWIG应该为我处理这个问题?
有人可以告诉我流程广告中缺少的内容吗?

通过这种方式,我成功包装了参数为vtkPolyData的函数:

首先,必须在swig .i文件中包括vtkPythonUtil:

%{

#include <vtkPythonUtil.h>

}%

然后,您必须在swig .i文件中映射vtkPolydata:

 %typemap(out) vtkPolyData* {

    PyImport_ImportModule("vtk");

    $result = vtkPythonUtil::GetObjectFromPointer ( (vtkPolyData*)$1 );
 }

%typemap(in) vtkPolyData* {

    $1 = (vtkPolyData*) vtkPythonUtil::GetPointerFromObject ( $input, "vtkPolyData" );

    if ( $1 == NULL ) { SWIG_fail; }
}

我在ITK中找到了itkVTKGlue。

最后,您需要将模块与库vtkPythonCore

这是一个vtk.i,它为VTK的类提供SWIG类型映射,并为项目中定义的类提供内存管理挂钩,这些类从SWIG包装的VTK对象派生而来。

这是完整的代码。 这已通过VTK 8和SWIG 3.7进行了测试。 请注意,以上链接包含示例,并且可能包含将来的更新。

/*
vtk.i a SWIG interface to VTK classes
Copyright (C)  2017 Burlen Loring

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
%{
#include <vtkPythonUtil.h>
%}

%include "exception.i"

/*---------------------------------------------------------------------------
macro: VTK_SWIG_INTEROP(vtk_t)

arguments:
  vtk_t - a VTK class name that is used in the SWIG generated API.

The macro defines the typemaps needed for SWIG to convert to and
from VTK's Python bindings. Use this when your API containes pointers
to classes defined in VTK.
---------------------------------------------------------------------------*/
%define VTK_SWIG_INTEROP(vtk_t)
%{
#include <vtk_t##.h>
%}
%typemap(out) vtk_t*
{
  $result = vtkPythonUtil::GetObjectFromPointer(
    static_cast<vtkObjectBase*>($1));
}
%typemap(in) vtk_t*
{
  $1 = static_cast<vtk_t*>(
    vtkPythonUtil::GetPointerFromObject($input,#vtk_t));
  if (!$1)
  {
    SWIG_exception(SWIG_TypeError,
      "an object of type " #vtk_t " is required");
  }
}
%typemap(typecheck, precedence=SWIG_TYPECHECK_POINTER) vtk_t*
{
  $1 = vtkPythonUtil::GetPointerFromObject($input,#vtk_t) ? 1 : 0;
}
%enddef

/*---------------------------------------------------------------------------
macro: VTK_DERIVED(derived_t)

arguments:
  derived_t - name of a class that derives from vtkObjectBase.

The macro causes SWIG to wrap the class and defines memory management hooks
that prevent memory leaks when SWIG creates the objects. Use this to wrap
VTK classes defined in your project.
---------------------------------------------------------------------------*/
%define VTK_DERIVED(derived_t)
%{
#include <derived_t##.h>
%}
%feature("ref") derived_t "$this->Register(nullptr);"
%feature("unref") derived_t "$this->UnRegister(nullptr);"
%newobject derived_t##::New();
%include <derived_t##.h>
%enddef

如果您的API使用vtkObjectBase和vtkDataObject,则SWIG .i文件将包括:

VTK_SWIG_INTEROP(vtkObjectBase)
VTK_SWIG_INTEROP(vtkDataObject)

API中将出现每个VTK类一个宏调用。

用法示例

如果定义从vtkObject或其子类之一(称为DataAdaptor)派生的类,则SWIG .i文件将包括:

VTK_DERIVED(DataAdaptor)

请注意,您还需要为类的API中的任何VTK类(包括vtkObjectBase)调用VTK_SWIG_INTEROP。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM