[英]Cythonic way to wrap boost::geometry::Point accessors
包含boost::geometry::Point
的以下成員函數的正確cythonic方法是什么? 代碼片段來自這里 。
/// @brief Get a coordinate
/// @tparam K coordinate to get
/// @return the coordinate
template <std::size_t K>
inline CoordinateType const& get() const
{
#if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
BOOST_GEOMETRY_ASSERT(m_created == 1);
BOOST_GEOMETRY_ASSERT(m_values_initialized[K] == 1);
#endif
BOOST_STATIC_ASSERT(K < DimensionCount);
return m_values[K];
}
/// @brief Set a coordinate
/// @tparam K coordinate to set
/// @param value value to set
template <std::size_t K>
inline void set(CoordinateType const& value)
{
#if defined(BOOST_GEOMETRY_ENABLE_ACCESS_DEBUGGING)
BOOST_GEOMETRY_ASSERT(m_created == 1);
m_values_initialized[K] = 1;
#endif
BOOST_STATIC_ASSERT(K < DimensionCount);
m_values[K] = value;
}
我首先嘗試使用:
cdef extern from "s57/data/geometries.h" namespace "bg::model":
cdef cppclass _Geo2 "bg::model::point<double, 2, bg::cs::spherical_equatorial<bg::degree>>":
_Geo2()
_Geo2( const _Geo2& other )
const double get[size_t]() except +
void set[size_t](double) except +
但后來我不知道去哪里,因為這樣的事情:
property x:
def __set__(self, double value):
deref(self._p).set[0](value)
給了我這個失敗:
Error compiling Cython file:
------------------------------------------------------------
...
property x:
def __set__(self, double value):
deref(self._p).set[0](value)
^
------------------------------------------------------------
c:\XXXX\x.pyx:24:31: not parsable as a type
我目前的工作解決方案是創建一些輔助函數,如:
double get_geo2_x( geo2& pnt );
double get_geo2_y( geo2& pnt );
void set_geo2_x( geo2& pnt, double value );
void set_geo2_y( geo2& pnt, double value );
有人想到一個更優雅的解決方案嗎?
你遇到了Cython處理非類型模板參數的問題。 您可以使用字符串手動指定函數名稱,這些字符串直接插入到生成的C代碼中(請參閱http://docs.cython.org/src/userguide/external_C_code.html#resolving-naming-conflicts-c-name-specifications )
作為一個非常簡單的例子
// example.hpp
#include <iostream>
class C {
template <int K>
void set(double value) {
std::cout << K << ": " << value << std::endl;
}
和Cython代碼
cdef extern from "example.hpp":
cdef cppclass C:
void set0 "set<0>"(double) except +
void set1 "set<1>"(double) except +
def do_set():
# very simple illustrative example
cdef C c
c.set0(1.0)
c.set1(1.5)
}
每當Cython看到在C
上調用set0
它會替換set<0>
,直接調用模板函數。 然后,您可以像嘗試一樣使用屬性。
這可能並不比創建輔助函數好得多,但它可能會更容易一些。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.