summaryrefslogtreecommitdiff
path: root/src/intersect.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/intersect.h')
-rw-r--r--src/intersect.h69
1 files changed, 25 insertions, 44 deletions
diff --git a/src/intersect.h b/src/intersect.h
index 26e1d7e..978bde8 100644
--- a/src/intersect.h
+++ b/src/intersect.h
@@ -3,30 +3,32 @@
#ifndef __INTERSECT_H__
#define __INTERSECT_H__
-#include <math_constants.h>
-
#include "linalg.h"
#include "matrix.h"
-#include "rotate.h"
+#include "geometry.h"
#define EPSILON 0.0f
-/* Test the intersection between a ray starting from `origin` traveling in the
- direction `direction` and a triangle defined by the vertices `v0`, `v1`, and
- `v2`. If the ray intersects the triangle, set `distance` to the distance
- between `origin` and the intersection and return true, else return false.
-
- `direction` must be normalized. */
-__device__ bool intersect_triangle(const float3 &origin, const float3 &direction, const float3 &v0, const float3 &v1, const float3 &v2, float &distance)
+/* Tests the intersection between a ray and a triangle.
+ If the ray intersects the triangle, set `distance` to the distance from
+ `origin` to the intersection and return true, else return false.
+ `direction` must be normalized to one. */
+__device__ bool
+intersect_triangle(const float3 &origin, const float3 &direction,
+ const Triangle &triangle, float &distance)
{
- Matrix m = make_matrix(v1-v0, v2-v0, -direction);
+ float3 m1 = triangle.v1-triangle.v0;
+ float3 m2 = triangle.v2-triangle.v0;
+ float3 m3 = -direction;
+
+ Matrix m = make_matrix(m1, m2, m3);
float determinant = det(m);
if (determinant == 0.0f)
return false;
- float3 b = origin-v0;
+ float3 b = origin-triangle.v0;
float u1 = ((m.a11*m.a22 - m.a12*m.a21)*b.x +
(m.a02*m.a21 - m.a01*m.a22)*b.y +
@@ -54,38 +56,17 @@ __device__ bool intersect_triangle(const float3 &origin, const float3 &direction
return true;
}
-/* Return the 32 bit color associated with the intersection between a ray
- starting from `origin` traveling in the direction `direction` and the
- plane defined by the points `v0`, `v1`, and `v2` using the cosine of the
- angle between the ray and the plane normal to determine the brightness.
-
- `direction` must be normalized. */
-__device__ unsigned int get_color(const float3 &direction, const float3 &v0, const float3& v1, const float3 &v2, unsigned int base_color=0xFFFFFF)
-{
- float scale = dot(normalize(cross(v1-v0,v2-v1)),-direction);
-
- if (scale < 0.0f)
- {
- base_color = 0xff0000;
- scale = dot(-normalize(cross(v1-v0,v2-v1)),-direction);
- }
-
- unsigned int r = 0xFF & (base_color >> 16);
- unsigned int g = 0xFF & (base_color >> 8);
- unsigned int b = 0xFF & base_color;
-
- r = floorf(r*scale);
- g = floorf(g*scale);
- b = floorf(b*scale);
-
- return r << 16 | g << 8 | b;
-}
-
-/* Test the intersection between a ray starting from `origin` traveling in the
- direction `direction` and the axis-aligned box defined by the opposite
- vertices `lower_bound` and `upper_bound`. If the ray intersects the box
- return True, else return False. */
-__device__ bool intersect_box(const float3 &origin, const float3 &direction, const float3 &lower_bound, const float3 &upper_bound, float& distance_to_box)
+/* Tests the intersection between a ray and an axis-aligned box defined by
+ an upper and lower bound. If the ray intersects the box, set
+ `distance_to_box` to the distance from `origin` to the intersection and
+ return true, else return false. `direction` must be normalized to one.
+
+ Source: "An Efficient and Robust Ray-Box Intersection Algorithm."
+ by Williams, et. al. */
+__device__ bool
+intersect_box(const float3 &origin, const float3 &direction,
+ const float3 &lower_bound, const float3 &upper_bound,
+ float& distance_to_box)
{
float kmin, kmax, kymin, kymax, kzmin, kzmax;