混合 VTK 和 SWIG Python

发布于 2024-12-27 12:00:40 字数 718 浏览 0 评论 0原文

这是我的课程:

#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 应该为我处理这个问题?
有人可以告诉我我的流量广告中缺少什么吗?可能需要一些修复吗?

Here is my class:

#include <vtkPolyData>

class VTKUtilities

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

I tried wrapping this to python by SWIG
but I try to call my function in python like this:

import VTKUtilities
import vtk

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

I get errors like :

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

I have been reading something about typemaps but I thought I didn't have to mess with that as SWIG should handle this for me ?
Can someone tell me what is missing in my flow ad possibly some fix ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘑菇王子 2025-01-03 12:00:40

我通过以下方式成功包装了参数为 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 链接

I succeed to wrapping functions whose arguments are vtkPolyData by doing this way:

First, you have to include vtkPythonUtil in swig .i file:

%{

#include <vtkPythonUtil.h>

}%

Then, you have to map the vtkPolydata in the swig .i file:

 %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; }
}

I find that in ITK itkVTKGlue.

Finally, you need to link your module with the library vtkPythonCore

昵称有卵用 2025-01-03 12:00:40

这是 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。

Here is a vtk.i providing SWIG typemaps for VTK's classes and memory management hooks for classes defined within your project derived from VTK Objects wrapped by SWIG.

Code

here is the code in its entirety. this is tested with VTK 8 and SWIG 3.7. Note that the above link includes examples and may include future updates.

/*
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

If your API makes use of vtkObjectBase and vtkDataObject your SWIG .i file would include:

VTK_SWIG_INTEROP(vtkObjectBase)
VTK_SWIG_INTEROP(vtkDataObject)

There will be one macro invocation per VTK class appearing in your API.

Example usage

If you define a class derived from vtkObject or one of its subclasses called DataAdaptor your SWIG .i file would include:

VTK_DERIVED(DataAdaptor)

Note that you will also need to call VTK_SWIG_INTEROP for any VTK classes in your class's API including vtkObjectBase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文