Cythonic 方式包装 boost::geometry::P oint accessors

Cythonic way to wrap boost::geometry::Point accessors

本文关键字:oint accessors geometry Cythonic 包装 boost 方式      更新时间:2023-10-16
包装

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:XXXXx.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 );

有人想到了一个更优雅的解决方案吗?

您在处理非类型模板参数时遇到了问题。您可以使用字符串手动指定函数名称,这些字符串直接插入到生成的 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;
  }

和赛通密码

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看到set0被调用C时,它就会替换set<0>,直接调用模板函数。然后,您可以像尝试的那样使用属性。

这可能并不比创建帮助程序函数好得多,但可能会容易一些。