如何在CGAL中计算一条线上的一个点

6

在CGAL中,给定一条三维线段,如何计算该线段上距离某个端点已知距离的点?

2个回答

4
如果您有两个点P0和P1,您可以创建一个向量V = P1 - P0
给定距离D从P0,您可以得到结果点R = P0 + (D ÷ ||V||) ⋅ V。
(在线性插值线之间进行插值,通过将D除以线的全长来将其转换为百分比。)
我不知道CGAL(文档有点糟糕),但我认为它应该是这样的:
Line_3<K> l = /* ... */;
Vector_3<K> v = l.to_vector();
Point_3<K> r = l.p + (d * d / v.squared_length()) * v;

注意,我甚至找不到一种方法来获取行的起始点,所以这取决于你。(l.p部分是虚构的。)

获取一条直线上的两个点:typedef Kernel::Line_3 Line;Line l = Line(point1, point2);cout << l.point(0) << l.point(1) << "\n"; - Max Harris
好吧,它无法编译 - CGAL未为点和向量定义运算符之一。我不太确定是哪一个,因为gcc是人类所知最糟糕的编译器。但它非常接近,所以你得到了一个检查 :) - Max Harris
@Max:啊,如果您发布错误信息,我相信我们可以提供帮助。(我的意思是编辑问题。)谢谢。 - GManNickG

0

您可以减去您感兴趣的点以获得斜率向量,然后沿着它走。以下是一个 MWE:

// Compile with: clang++ -DBOOST_ALL_NO_LIB -DCGAL_USE_GMPXX=1 -O2 -g -DNDEBUG -Wall -Wextra -pedantic -march=native -frounding-math main.cpp -lgmpxx -lmpfr -lgmp

#include <CGAL/Exact_predicates_exact_constructions_kernel.h>

using K = CGAL::Exact_predicates_exact_constructions_kernel;
using Point_3 = K::Point_3;

class InterPointInterpolator {
 public:
  InterPointInterpolator(const Point_3 &a, const Point_3 &b) : a(a), b(b) {}
  // Returns points interpolated from a at t=0 to b at t=1
  Point_3 operator()(const double t) const {
    const auto m = b - a;
    return a + t * m;
  }
 private:
  Point_3 a;
  Point_3 b;
};

int main(){
  InterPointInterpolator ipi(Point_3(0, 0, 0), Point_3(10, 5, 20));
  for(int i=0;i<=10;i++){
    const auto interpolated_point = ipi(i/10.0);
    std::cout<<interpolated_point<<std::endl;
  }

  return 0;
}

输出:

0  0   0
1  0.5 2
2  1   4
3  1.5 6
4  2   8
5  2.5 10
6  3   12
7  3.5 14
8  4   16
9  4.5 18
10 5   20

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接