模板点类型的boost :: geometry :: model :: segment构造函数?

时间:2015-12-18 12:34:31

标签: c++ templates boost point boost-geometry

如何将boost :: geometry :: model :: segment与模板点类型一起使用?

例如,我可以使用模板点类型来计算EuclideanDistanceDotProduct,但要计算PointSegmentDistance我需要使用boost::geometry::model::segment,但我不是知道如何初始化它。

此代码工作似乎我错了,这段代码也不适用于模板点类型。我们如何在模板点类型中使用此代码?

template <typename TPoint>
double EuclideanDistance(const TPoint &pt1, const TPoint &pt2)
{
    double distance= boost::geometry::distance(pt1, pt2);

    return distance;
}

template <typename TPoint>
double DotProduct(const TPoint &pt1, const TPoint &pt2)
{
    double product= boost::geometry::dot_product(pt1, pt2);

    return product;
}

此代码无法正常工作

template <typename TPoint>
double PointSegmentDistance(const TPoint &pt1, const TPoint &pt2, const TPoint &pt3)
{
    double distance= boost::geometry::distance(boost::geometry::model::segment(pt1, pt2), pt3);

    return distance;
}

另一种选择是将其重写为:

template <typename TPoint>
double PointSegmentDistance(const TPoint &pt1, const TPoint &pt2, const TPoint &pt3)
{
    boost::geometry::model::segment<TPoint> segment(pt1,pt2);

    double distance= boost::geometry::distance(segment, pt3);

    return distance;
}

template <typename TPoint>
bool SegmentSegmentIntersection(const TPoint &pt1, const TPoint &pt2, const TPoint &pt3, const TPoint &pt4)
{
    boost::geometry::model::segment<TPoint> segment1(pt1,pt2);
    boost::geometry::model::segment<TPoint> segment2(pt3,pt4);

    bool result= boost::geometry::intersects(segment1, segment2);

    return result;
}

1 个答案:

答案 0 :(得分:2)

您忘记了模板参数:

template <typename TPoint>
double PointSegmentDistance(const TPoint &pt1, const TPoint &pt2, const TPoint &pt3)
{
    double distance= boost::geometry::distance(boost::geometry::model::segment<TPoint>(pt1,pt2), pt3);
                                                                              // ^ template parameter
    return distance;
}