python调用c++类方法(2)
testpy.cpp:
#include<iostream>
#include<vector> struct point{
float pointx;
float pointy;
float pointz;
}; struct pose{
float x;
float y;
float z;
int* data; point location;
}; class MyTest
{
public:
MyTest();
~MyTest();
int myTest(int* myData, int dataNum, pose& mypose); private: }; MyTest::MyTest()
{
} MyTest::~MyTest()
{
} int MyTest::myTest(int* myData, int dataNum, pose& mypose)
{
if (dataNum>)
{
for (size_t i = ; i < dataNum; i++)
{
*myData = i + ;
myData += ;
}
}
mypose.x=1.0;
mypose.y=2.0;
mypose.z=3.0;
mypose.location.pointx=4.0;
mypose.location.pointy=5.0;
mypose.location.pointz=6.0; if (dataNum>)
{
for (size_t i = ; i < dataNum; i++)
{
*(mypose.data) = i + ;
mypose.data += ;
}
} return ;
} extern "C" { MyTest myObj; int myTest(int* myData, int dataNum, pose& mypose)
{
return myObj.myTest(myData, dataNum, mypose);
} }
pythonCallCpp.py:
#!/usr/bin/python
# -*- coding: utf-8 -*- from ctypes import *
import ctypes
import numpy INPUT = c_int * 100
myinput = INPUT() INPUT01 = c_int * 100
myinput01 = INPUT01() #arr = numpy.array([1, 2, 3, 4],dtype = numpy.int)
arr1 = numpy.zeros(100, dtype=ctypes.c_int) #if not arr.flags['C_CONTIGUOUS']: # 若不是C连续内存,强制转换
#arr = numpy.ascontiguous(arr, dtype = numpy.dtype)
if not arr1.flags['C_CONTIGUOUS']:
arr1 = numpy.ascontiguous(arr1, dtype = numpy.dtype)
#c_arr = numpy.ctypeslib.as_ctypes(arr) # array 转为ctypes类型
#c_arr1 = numpy.ctypeslib.as_ctypes(arr1) cptr = arr1.ctypes.data_as(POINTER(ctypes.c_int)) # 取指针 so = ctypes.cdll.LoadLibrary
lib = so("/opt/fp100/libtest.so")
print 'myTest(int* myData, int dataNum, pose& mypos)' class point(ctypes.Structure):
_fields_ = [("pointx", ctypes.c_float),("pointy", ctypes.c_float),("pointz", ctypes.c_float)] pointtemp = point(0.0, 0.0, 0.0) class pose(ctypes.Structure):
_fields_ = [("x", ctypes.c_float),("y", ctypes.c_float),("z", ctypes.c_float),("data", POINTER(ctypes.c_int)),("location", point)] #POINTER(ctypes.c_int) posetemp = pose()
posetemp.x=0.0
posetemp.y=0.0
posetemp.z=0.0 posetemp.data = myinput01
posetemp.location = pointtemp #posetemp = pose() lib.myTest(myinput, 100, ctypes.byref(posetemp)) print myinput[0]
print myinput[1]
print myinput[2]
print myinput[3]
print posetemp.x
print posetemp.y
print posetemp.z
print posetemp.location.pointx
print posetemp.location.pointy
print posetemp.location.pointz print posetemp.data[0]
结论:存在一个问题,结构体内套指针时,出现地址紊乱,不知如何解决?
python调用c++类方法(2)的更多相关文章
- python 调用c++类方法(1)
myTest.cpp: #include<iostream> #include<vector> class MyTest { public: MyTest(); ~MyTest ...
- python 静态方法、类方法(二)
<Python静态方法.类方法>一文中曾用在类之外生成函数的方式,来计算类的实例的个数.本文将探讨用静态方法和类方法来实现此功能. 一使用静态方法统计实例 例1.static.py # - ...
- python类:类方法和静态方法
http://blog.csdn.net/pipisorry/article/details/49516185 面相对象程序设计中,类方法和静态方法是经常用到的两个术语.逻辑上讲:类方法是只能由类名调 ...
- Python 实例方法、类方法、静态方法的区别与作用
Python中至少有三种比较常见的方法类型,即实例方法,类方法.静态方法.它们是如何定义的呢?如何调用的呢?它们又有何区别和作用呢?且看下文. 首先,这三种方法都定义在类中.下面我先简单说一下怎么定义 ...
- python静态方法和类方法
静态方法和类方法在python2.2中被引用,经典类和新式类都可以使用.同时,一对内建函数:staticmethod和classmethod被引入,用来转化类中某一方法为这两种方法之一. 静态方法: ...
- 【面试必问】python实例方法、类方法@classmethod、静态方法@staticmethod和属性方法@property区别
[面试必问]python实例方法.类方法@classmethod.静态方法@staticmethod和属性方法@property区别 1.#类方法@classmethod,只能访问类变量,不能访问实例 ...
- Python 静态方法、类方法和属性方法
Python 静态方法.类方法和属性方法 静态方法(staticmethod) staticmethod不与类或者对象绑定,类和实例对象都可以调用,没有自动传值效果,Python内置函数staticm ...
- 【初学python】使用python调用monkey测试
目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...
- python调用py中rar的路径问题。
1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...
随机推荐
- centos:mysql主从同步配置(2018)
centos:mysql主从同步配置(2018) https://blog.csdn.net/liubo_2016/article/details/82379115 主服务器:10.1.1.144; ...
- vue中出现 There are multiple modules with names that only differ in casing的问题
import时,文件引入的路径描述不统一,所以保留一种引入风格即可解决. 第一种,我选择统一用第一种 import GoTop from '@/components/layout/goTop' 第二种 ...
- 目标检测数据库 PASCAL 格式的 Ground Truth 的解析函数
最近在做一个目标检测算法,训练时用到了 bootstrap 策略,于是我将PASCAL的 Ground Truth 格式的读取函数从 Matlab 改写为 C++.PASCAL 的标注格式为: # P ...
- Ubuntu下Cmake编译C++程序Helloworld
1.首选新建工程目录 mkdir helloworld 2.新建文件目录 cd helloworld mkdir bin mkdir lib mkdir src mkdir include mkdir ...
- Spring AOP 在XML中声明切面
转载地址:http://www.jianshu.com/p/43a0bc21805f 在XML中将一个Java类配置成一个切面: AOP元素 用途 <aop:advisor> 定义AOP通 ...
- UEditor富文本编辑器简单使用
UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...
- es6 Object.assign(target, ...sources)
Object.assign() 方法用于将所有可枚举属性(对象属性)的值从一个或多个源对象复制到目标对象.它将返回目标对象. 语法 Object.assign(target, ...sources) ...
- linux服务器外网内网(双网络)搭建
一共有2台服务器,分别用a,b表示.a双网卡,即有外网也有内网.b只有内网环境.a,b的内网是通过交换机组建.至于外网怎么搭建我就不说了.关键说一说内网是怎么组建的. 如果你对linux不熟悉,对网卡 ...
- jvm——NIO
https://blog.csdn.net/Evankaka/article/details/48464013 https://www.cnblogs.com/aspirant/p/9166944.h ...
- ApplicationRunner接口
ApplicationRunner 和 CommandLineRunner 功能一致,用法也基本一致,唯一的区别主要体现在对参数的处理上,ApplicationRunner 可以接收更多类型的参数(A ...