cgv
Loading...
Searching...
No Matches
intersections.h
1#pragma once
2
3#include <cgv/math/lin_solve.h>
4
5
6namespace cgv{
7 namespace math{
8
9template <typename T>
10bool 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)
11{
12 mat<T> A(x1.dim(),2);
13 A.set_col(0,d1);
14 A.set_col(1,d2);
15 vec<T> b = transpose(A)*(x1-x2);
16 A=transpose(A)*A;
17 vec<T> t;
18 solve(A,b,t);
19 vec<T> s1 = x1 + t(0)*d1;
20 vec<T> s2 = x2 + t(1)*d2;
21 t1 = t(0);
22 t2 = t(1);
23 return length(s1-s2)<= eps;
24}
25
26template <typename T>
27bool line_circle_intersection(const vec<T>& x,const vec<T>& d,const vec<T>& center,const vec<T>& normal,
28 const T& radius, vec<T>& nearest_point, T eps=0.00001)
29{
30
31 T s = dot(normal,d);
32
33 if(s == 0)
34 return false;
35 T t = dot(normal,x)-dot(center,normal);
36
37 t/=-s;
38
39 vec<T> v1 = x+t*d-center;
40 nearest_point = radius*normalize(v1)+center;
41
42
43 if(t < 0)
44 return false;
45 return ( fabs(length(v1)-radius) <= eps);
46
47}
48
49template <typename T>
50bool ray_plane_intersection(const vec<T>& x,const vec<T>& d,const vec<T>& plane, T& t)
51{
52
53 t =0;
54 T s =0;
55 unsigned n = x.dim();
56 for(unsigned i = 0; i < n; i++)
57 {
58 t+=plane(i)*x(i);
59 s+=plane(i)*d(i);
60 }
61 if(s == 0)
62 return false;
63 t+=plane(n);
64 t/=-s;
65 return ( t >= 0);
66
67}
68
69 }
70}
71
the cgv namespace
Definition print.h:11