Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
math.hpp
Go to the documentation of this file.
1 
23 #ifndef __MATH__
24 #define __MATH__
25 
26 #include <Eigen/Dense>
27 
28 namespace Tucano
29 {
30 
31 namespace Math
32 {
33 
34 
45 inline bool raySphereIntersection (const Eigen::Vector3f& ray_direction, const Eigen::Vector3f& ray_origin, const Eigen::Vector3f& sphere_center, float sphere_radius, Eigen::Vector3f& intersection)
46 {
47 
48  Eigen::Vector3f dir = ray_origin - sphere_center;
49  float b = ray_direction.dot(dir);
50  float c = dir.dot(dir) - sphere_radius*sphere_radius;
51  float disc = b*b - c;
52 
53  intersection = Eigen::Vector3f::Zero();
54  if (disc < 0.0)
55  return false;
56 
57  float e = sqrt(disc);
58  float t = min(-b-e, -b+e);
59 
60  // if by chance tmin < 0, we can still test for tmax
61  if (t <= 0.0)
62  t = max(-b-e, -b+e);
63 
64  if (t > 0.0)
65  {
66  intersection = ray_origin + ray_direction*t;
67  return true;
68  }
69 
70  return false;
71 }
72 
84 inline bool rayPlaneIntersection (const Eigen::Vector3f& ray_direction, const Eigen::Vector3f& ray_origin, const Eigen::Vector3f& plane_point, const Eigen::Vector3f& plane_normal, Eigen::Vector3f& intersection)
85 {
86  float t = (plane_point - ray_origin).dot(plane_normal) / ray_direction.dot(plane_normal);
87 
88  if (t == 0.0)
89  return false;
90 
91  intersection = ray_origin + ray_direction * t;
92  return true;
93 }
94 
110 inline bool rayRingIntersection (const Eigen::Vector3f& ray_direction, const Eigen::Vector3f& ray_origin, const Eigen::Vector3f& plane_point, const Eigen::Vector3f& plane_normal, float inner_radius, float outer_radius, Eigen::Vector3f& intersection)
111 {
112  Eigen::Vector3f p;
113  if (Tucano::Math::rayPlaneIntersection(ray_direction, ray_origin, plane_point, plane_normal, p))
114  {
115  float dist = (p - plane_point).norm();
116  if (dist >= inner_radius && dist <= outer_radius)
117  {
118  intersection = p;
119  return true;
120  }
121  }
122  return false;
123 }
124 
125 
126 
136 inline Eigen::Vector3f rayDirection(const Eigen::Vector2f& pixel, const Eigen::Vector2i& viewport_size, const Eigen::Matrix4f& projection_matrix, const Eigen::Affine3f& view_matrix)
137 {
138  // coordinates in range [-1, 1]
139  // z = near plane in NDC, but it really doesn't matter since we are interested only in the direction, and not a specific point
140  Eigen::Vector4f screen_pos = Eigen::Vector4f(2.0* pixel[0] / (float)viewport_size[0] - 1.0, 1.0 - 2.0*pixel[1] / (float)viewport_size[1], -1.0, 1.0);
141 
142  // ray in camera space (where camera is at origin)
143  Eigen::Vector4f eye_ray = projection_matrix.inverse() * screen_pos;
144  // set w =0.0 since we want the vector, so we dont worry about translation
145  eye_ray[3] = 0.0;
146 
147  // go to world space
148  eye_ray = view_matrix.inverse() * eye_ray;
149 
150 
151  Eigen::Vector3f ray = (eye_ray.head(3)).normalized();
152 
153 
154 
155  return ray;
156 }
157 
158 
159 
160 }
161 }
162 #endif
bool raySphereIntersection(const Eigen::Vector3f &ray_direction, const Eigen::Vector3f &ray_origin, const Eigen::Vector3f &sphere_center, float sphere_radius, Eigen::Vector3f &intersection)
Computes the intersection point between a ray and a sphere.
Definition: math.hpp:45
Definition: bufferobject.hpp:34
bool rayPlaneIntersection(const Eigen::Vector3f &ray_direction, const Eigen::Vector3f &ray_origin, const Eigen::Vector3f &plane_point, const Eigen::Vector3f &plane_normal, Eigen::Vector3f &intersection)
Computes the intersection between a ray and a plane.
Definition: math.hpp:84
Eigen::Vector3f rayDirection(const Eigen::Vector2f &pixel, const Eigen::Vector2i &viewport_size, const Eigen::Matrix4f &projection_matrix, const Eigen::Affine3f &view_matrix)
Computes the ray direction given a pixel position and camera matrices.
Definition: math.hpp:136
bool rayRingIntersection(const Eigen::Vector3f &ray_direction, const Eigen::Vector3f &ray_origin, const Eigen::Vector3f &plane_point, const Eigen::Vector3f &plane_normal, float inner_radius, float outer_radius, Eigen::Vector3f &intersection)
Computes the intersection between a ray and a ring in 3D space.
Definition: math.hpp:110