cgv
Loading...
Searching...
No Matches
line.h
1#pragma once
2
3#include <cgv/math/lin_solve.h>
4
5namespace cgv{
6namespace math{
7
12template <typename T>
13bool line_line_intersection(const vec<T>& x1, const vec<T>& d1, const vec<T>& x2, const vec<T>& d2, T& t1, T& t2, T eps = 0.00001)
14{
15 mat<T> A(x1.dim(), 2);
16 A.set_col(0, d1);
17 A.set_col(1, d2);
18 vec<T> b = transpose(A) * (x1 - x2);
19 A = transpose(A) * A;
20 vec<T> t;
21 solve(A, b, t);
22 vec<T> s1 = x1 + t(0) * d1;
23 vec<T> s2 = x2 + t(1) * d2;
24 t1 = t(0);
25 t2 = t(1);
26 return length(s1 - s2) <= eps;
27}
28
29template <typename T>
30bool line_circle_intersection(const vec<T>& x, const vec<T>& d, const vec<T>& center, const vec<T>& normal,
31 const T& radius, vec<T>& nearest_point, T eps = 0.00001)
32{
33 T s = dot(normal, d);
34 if(s == T(0))
35 return false;
36
37 T t = dot(normal, x) - dot(center, normal);
38 t /= -s;
39
40 vec<T> v1 = x + t * d - center;
41 nearest_point = radius * normalize(v1) + center;
42
43 if(t < T(0))
44 return false;
45
46 return std::fabs(length(v1) - radius) <= eps;
47}
48
49} // namespace math
50} // namespace cgv
the cgv namespace
Definition print.h:11