How to compute a point on a line in CGAL
An answer to this question on Stack Overflow.
Question
Given a 3D line in CGAL, how do I compute a point on that line that is some known distance from an endpoint?
Answer
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