混合VTK和swigpython

2024-10-01 22:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的班级:

#include <vtkPolyData>

class VTKUtilities

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

我试着用SWIG把它包装成python 但是我尝试在python中调用我的函数,如下所示:

^{pr2}$

我的错误包括:

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

我一直在读一些关于排版图的东西,但我想我不必把它搞得一团糟,因为SWIG应该帮我处理这个问题?
有人能告诉我在我的流量广告中遗漏了什么吗?在


Tags: toincludemycodedoclassswig班级
2条回答

这里有一个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文件将包括:

^{pr2}$

在API中,每个VTK类都会有一个宏调用。在

示例用法

如果定义从vtkObject派生的类或其子类之一dataadapter,SWIG.i文件将包括:

VTK_DERIVED(DataAdaptor)

注意,对于类的API中的任何VTK类,包括vtkObjectBase,还需要调用VTK_SWIG_INTEROP。在

我通过以下方法成功地包装了参数为vtkPolyData的函数:

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

%{

#include <vtkPythonUtil.h>

}%

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

^{pr2}$

我在ITK itkVTKGlue找到了。在

最后,您需要将模块与库vtkPythonCore链接起来

相关问题 更多 >

    热门问题