Skip to content
Browse files

Add test that fails and needs fixing

  • Loading branch information...
1 parent f0950c2 commit af847736438a6c5fa4b8399268ed76276e441f93 @Kazade committed Jan 23, 2011
Showing with 54 additions and 38 deletions.
  1. +1 −1 include/kazmath/ray2.h
  2. +40 −35 src/ray2.c
  3. +13 −2 tests/test_ray2.cpp
View
2 include/kazmath/ray2.h
@@ -40,7 +40,7 @@ typedef struct kmRay2 {
void kmRay2Fill(kmRay2* ray, kmScalar px, kmScalar py, kmScalar vx, kmScalar vy);
kmBool kmRay2IntersectLineSegment(const kmRay2* ray, const kmVec2 p1, const kmVec2 p2, kmVec2* intersection);
-kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2 p1, const kmVec2 p2, const kmVec2 p3, kmVec2* intersection);
+kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2 p1, const kmVec2 p2, const kmVec2 p3, kmVec2* intersection, kmVec2* normal_out);
kmBool kmRay2IntersectCircle(const kmRay2* ray, const kmVec2 centre, const kmScalar radius, kmVec2* intersection);
#ifdef __cplusplus
View
75 src/ray2.c
@@ -13,8 +13,8 @@ kmBool kmRay2IntersectLineSegment(const kmRay2* ray, const kmVec2 p1, const kmVe
kmScalar A1, B1, C1;
kmScalar A2, B2, C2;
- A1 = (ray->start.y + ray->dir.y) - (ray->start.y);
- B1 = (ray->start.x + ray->dir.x) - (ray->start.x);
+ A1 = ray->dir.y;
+ B1 = ray->dir.x;
C1 = A1 * ray->start.x + B1 * ray->start.y;
A2 = p2.y - p1.y;
@@ -23,7 +23,7 @@ kmBool kmRay2IntersectLineSegment(const kmRay2* ray, const kmVec2 p1, const kmVe
double det = (A1 * B2) - (A2 * B1);
if(det == 0) {
- //printf("Parallel\n");
+ printf("Parallel\n");
return KM_FALSE;
}
@@ -33,7 +33,7 @@ kmBool kmRay2IntersectLineSegment(const kmRay2* ray, const kmVec2 p1, const kmVe
if(x < min(p1.x, p2.x) - kmEpsilon || x > max(p1.x, p2.x) + kmEpsilon ||
y < min(p1.y, p2.y) - kmEpsilon || y > max(p1.y, p2.y) + kmEpsilon) {
//Outside of line
- //printf("Outside of line\n");
+ printf("Outside of line, %f %f (%f %f)(%f, %f)\n", x, y, p1.x, p1.y, p2.x, p2.y);
return KM_FALSE;
}
@@ -42,7 +42,7 @@ kmBool kmRay2IntersectLineSegment(const kmRay2* ray, const kmVec2 p1, const kmVe
if(x < min(x1, x2) - kmEpsilon || x > max(x1, x2) + kmEpsilon ||
y < min(y1, y2) - kmEpsilon || y > max(y1, y2) + kmEpsilon) {
- //printf("Outside of ray, %f %f (%f %f)(%f, %f)\n", x, y, x1, y1, x2, y2);
+ printf("Outside of ray, %f %f (%f %f)(%f, %f)\n", x, y, x1, y1, x2, y2);
return KM_FALSE;
}
@@ -52,45 +52,50 @@ kmBool kmRay2IntersectLineSegment(const kmRay2* ray, const kmVec2 p1, const kmVe
return KM_TRUE;
}
-kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2 p1, const kmVec2 p2, const kmVec2 p3, kmVec2* intersection) {
+void calculate_line_normal(kmVec2 p1, kmVec2 p2, kmVec2* normal_out) {
+ kmVec2 tmp;
+ kmVec2Subtract(&tmp, &p2, &p1); //Get direction vector
+
+ normal_out->x = -tmp.y;
+ normal_out->y = tmp.x;
+ kmVec2Normalize(normal_out, normal_out);
+
+ //TODO: should check that the normal is pointing out of the triangle
+}
+
+kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2 p1, const kmVec2 p2, const kmVec2 p3, kmVec2* intersection, kmVec2* normal_out) {
kmVec2 intersect;
- kmBool intersected = KM_FALSE;
- float dist = 100000;
- /*printf("p1 %f, %f\n", p1.x, p1.y);
- printf("p2 %f, %f\n", p2.x, p2.y);
- printf("ray start %f, %f\n", ray->start.x, ray->start.y);
- printf("ray dir %f, %f\n", ray->dir.x, ray->dir.y);*/
+ kmVec2 normal;
+ kmBool intersected = KM_FALSE;
+
if(kmRay2IntersectLineSegment(ray, p1, p2, &intersect)) {
- kmVec2 tmp;
- kmVec2Subtract(&tmp, &intersect, &ray->start);
- if(fabs(kmVec2Length(&tmp)) < dist) {
- intersection->x = intersect.x;
- intersection->y = intersect.y;
- intersected = KM_TRUE;
- dist = fabs(kmVec2Length(&tmp));
- }
+ intersection->x = intersect.x;
+ intersection->y = intersect.y;
+ intersected = KM_TRUE;
+ calculate_line_normal(p1, p2, &normal);
}
if(kmRay2IntersectLineSegment(ray, p2, p3, &intersect)) {
- kmVec2 tmp;
- kmVec2Subtract(&tmp, &intersect, &ray->start);
- if(fabs(kmVec2Length(&tmp)) < dist) {
- intersection->x = intersect.x;
- intersection->y = intersect.y;
- intersected = KM_TRUE;
- dist = fabs(kmVec2Length(&tmp));
- }
+ intersection->x = intersect.x;
+ intersection->y = intersect.y;
+ intersected = KM_TRUE;
+ calculate_line_normal(p2, p3, &normal);
}
if(kmRay2IntersectLineSegment(ray, p3, p1, &intersect)) {
- kmVec2 tmp;
- kmVec2Subtract(&tmp, &intersect, &ray->start);
- if(fabs(kmVec2Length(&tmp)) < dist) {
- intersection->x = intersect.x;
- intersection->y = intersect.y;
- intersected = KM_TRUE;
- dist = fabs(kmVec2Length(&tmp));
+ intersection->x = intersect.x;
+ intersection->y = intersect.y;
+ intersected = KM_TRUE;
+ calculate_line_normal(p3, p1, &normal);
+ }
+
+ if(intersected) {
+ intersection->x = intersect.x;
+ intersection->y = intersect.y;
+ if(normal_out) {
+ normal_out->x = normal.x;
+ normal_out->y = normal.y;
}
}
View
15 tests/test_ray2.cpp
@@ -30,6 +30,15 @@ TEST(test_line_segment_intersection) {
line_start.x = -150.0f;
//Should now hit the line as it's been extended
CHECK(kmRay2IntersectLineSegment(&ray, line_start, line_end, &intersect));
+
+ //Check diagonal case
+
+ kmRay2Fill(&ray, 0.0, 0.7f, 0.0f, -1.0f);
+ kmVec2Fill(&line_start, -1.0f, 0.0f);
+ kmVec2Fill(&line_end, 10.0f, 5.0f);
+
+ CHECK(kmRay2IntersectLineSegment(&ray, line_start, line_end, &intersect));
+ CHECK(intersect.y > 0.0f);
}
TEST(test_triangle_intersection) {
@@ -50,10 +59,12 @@ TEST(test_triangle_intersection) {
ray.dir.x = 0.0f;
ray.dir.y = -0.5f;
- kmVec2 intersect;
- CHECK(kmRay2IntersectTriangle(&ray, p1, p2, p3, &intersect));
+ kmVec2 intersect, normal;
+ CHECK(kmRay2IntersectTriangle(&ray, p1, p2, p3, &intersect, &normal));
CHECK_CLOSE(0.0f, intersect.x, 0.001f);
CHECK_CLOSE(0.0f, intersect.y, 0.001f);
+ CHECK_CLOSE(0.0f, normal.x, 0.001f);
+ CHECK_CLOSE(1.0f, normal.y, 0.001f);
}
int main() {

0 comments on commit af84773

Please sign in to comment.
Something went wrong with that request. Please try again.