【发布时间】:2017-07-30 17:28:36
【问题描述】:
我有一个带有两种方法的 C++ 类(大型项目的一部分)。这些方法的工作非常相似:第一个对向量进行归一化并返回它,而第二个则返回一个归一化的向量而不改变原始向量。
vector3.h:
class Vector3
{
public:
Vector3(double a = 0.0, double b = 0.0, double c = 0.0)
: x(a), y(b), z(c) {}
Vector3(const Vector3& other) : x(other.x), y(other.y), z(other.z) {}
double length() const { return sqrt(x*x+y*y+z*z); }
Vector3& normalize()
{
float_type n = length();
n = float_type(1.0) / ( n ? n : 1.0 );
this->x *= n;
this->y *= n;
this->z *= n;
return *this;
}
Vector3 normalized() const
{
Vector3 v(*this);
v.normalize();
return v;
}
std::string toString()
{
std::ostringstream strm;
strm << '(' << x << ", " << y << ", " << z << ')' ;
return strm.str();
}
Vector3 operator+(const Vector3& other) const
{
return Vector3(x + other.x, y + other.y, z + other.z);
}
private:
double x,y,z;
};
我使用 SWIG(通过 cmake)为这个类构建 Python 绑定。
vector3.i:
%include <stl.i>
%include <carrays.i>
%include <cpointer.i>
%module vectortest
%{
#include "vector3.h"
%}
%include "vector3.h"
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
add_executable("vtest" "vtest.cpp")
find_package(SWIG)
include(${SWIG_USE_FILE})
find_package(PythonLibs)
find_package(PythonInterp)
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${PYTHON_INCLUDE_DIRS})
SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(SOURCE vector3.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(vectortest_python python vector3.i )
SWIG_LINK_LIBRARIES(vectortest_python ${PYTHON_LIBRARIES} ${LIBS})
set_target_properties(_vectortest_python PROPERTIES PREFIX "_" OUTPUT_NAME "vectortest")
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import get_python_lib; print (get_python_lib())" OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE)
install(TARGETS ${SWIG_MODULE_vectortest_python_REAL_NAME} DESTINATION ${PYTHON_SITE_PACKAGES})
install(FILES ${CMAKE_BINARY_DIR}/vectortest.py DESTINATION ${PYTHON_SITE_PACKAGES})
C++ 中的行为很好。
vtest.cpp:
#include <iostream>
#include "vector3.h"
int main()
{
Vector3 v1(1,0,0);
Vector3 v2(0,1,0);
std::cout << (v1+v2).toString() << std::endl;
std::cout << (v1+v2).normalized().toString() << std::endl;
std::cout << (v1+v2).normalize().toString() << std::endl;
return 0;
}
输出:
(1, 1, 0)
(0.707107, 0.707107, 0)
(0.707107, 0.707107, 0)
但是,它在 Python 中的行为很奇怪:
vtest.py:
#!/usr/bin/python3
from vectortest import Vector3
v1 = Vector3(1,0,0)
v2 = Vector3(0,1,0)
print( (v1+v2).toString() )
print( (v1+v2).normalized().toString() )
print( (v1+v2).normalize().toString() )
输出:
(1, 1, 0)
(0.707107, 0.707107, 0)
(0, 0.707107, 0)
第二种方法(normalized())按预期工作,但第一种方法(normalize())没有。这是什么原因造成的?有关如何解决此问题的任何建议?
【问题讨论】:
-
这可能是由于您的 swigged 版本中
v1 + v2的早期破坏造成的 -
Vector3对象不是C++Vector3` 对象,而是 swigged 包装器对象。正如@donkopotamus 所建议的那样,我相信该对象很早就被销毁了。您是否尝试在调用normalize()之前存储矢量(v1+v2)? -
当然,存储
v1+v2有助于并“解决”问题。但据我所知,我的 C++ 和 Python 代码都是完全有效的,所以结果应该可以正确地执行。要么是我的代码无效,要么是 SWIG、Python 或 GCC 中存在错误。后者几乎不是这样,那我做错了什么?