如何计算CGAL中直线上的点

发布于 2024-09-14 22:11:48 字数 40 浏览 2 评论 0原文

给定 CGAL 中的 3D 线,如何计算该线上距端点已知距离的点?

Given a 3D line in CGAL, how do I compute a point on that line that is some known distance from an endpoint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2024-09-21 22:11:48

如果有两个点 P0 和 P1,则可以创建向量 V = P1 - P0 >。

给定距 P0 的距离 D,您可以得到结果点 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;

注意我什至找不到一种获取直线起点的方法,因此由您决定。 (lp 部分是组成的。)

If you have two points P0 and P1, you can make a vector V = P1 - P0.

Given distance D from P0, you can get the resulting point R = P0 + (D ÷ ||V||) ⋅ V.

(Linearly interpolate between the lines, changing D into a percentage by dividing by the full length of the line.)


I don't know CGAL (and the documentation kind of sucks), but I assume it'd be something like this:

Line_3<K> l = /* ... */;
Vector_3<K> v = l.to_vector();
Point_3<K> r = l.p + (d * d / v.squared_length()) * v;

Note I can't even find a way to get the starting point of a line, so that one is up to you. (The l.p part is made up.)

怀中猫帐中妖 2024-09-21 22:11:48

您只需减去您感兴趣的点即可获得斜率向量,然后沿着它行走。 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

You can just subtract the points you're interested in to get a slope vector and then walk along it. A MWE is below:

// 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;
}

Output:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文