Browse files

Start implementing kmRay3 and associated functions

  • Loading branch information...
1 parent 96153bf commit 59a1f970381afad920e803c304d324906278470d @Kazade committed Feb 15, 2013
Showing with 131 additions and 2 deletions.
  1. +2 −0 CMakeLists.txt
  2. +3 −0 kazmath.files
  3. +10 −1 kazmath/plane.c
  4. +2 −1 kazmath/plane.h
  5. +36 −0 kazmath/ray3.c
  6. +26 −0 kazmath/ray3.h
  7. +7 −0 kazmath/vec3.c
  8. +7 −0 kazmath/vec3.h
  9. +38 −0 tests/test_ray3.cpp
View
2 CMakeLists.txt
@@ -16,6 +16,7 @@ SET(KAZMATH_HEADERS
${CMAKE_SOURCE_DIR}/kazmath/mat4.h
${CMAKE_SOURCE_DIR}/kazmath/mat3.h
${CMAKE_SOURCE_DIR}/kazmath/ray2.h
+ ${CMAKE_SOURCE_DIR}/kazmath/ray3.h
${CMAKE_SOURCE_DIR}/kazmath/plane.h
${CMAKE_SOURCE_DIR}/kazmath/utility.h
${CMAKE_SOURCE_DIR}/kazmath/quaternion.h
@@ -39,6 +40,7 @@ SET(KAZMATH_SOURCES
${CMAKE_SOURCE_DIR}/kazmath/utility.c
${CMAKE_SOURCE_DIR}/kazmath/aabb.c
${CMAKE_SOURCE_DIR}/kazmath/ray2.c
+ ${CMAKE_SOURCE_DIR}/kazmath/ray3.c
${CMAKE_SOURCE_DIR}/kazmath/GL/mat4stack.c
${CMAKE_SOURCE_DIR}/kazmath/GL/matrix.c
)
View
3 kazmath.files
@@ -80,3 +80,6 @@ ChangeLog
CMakeLists.txt
README
tests/test_quaternion.cpp
+kazmath/ray3.h
+kazmath/ray3.c
+tests/test_ray3.cpp
View
11 kazmath/plane.c
@@ -55,7 +55,16 @@ kmScalar kmPlaneDotNormal(const kmPlane* pP, const kmVec3* pV)
pP->c * pV->z);
}
-kmPlane* kmPlaneFromPointNormal(kmPlane* pOut, const kmVec3* pPoint, const kmVec3* pNormal)
+kmPlane* kmPlaneFromNormalAndDistance(kmPlane* plane, const struct kmVec3* normal, const kmScalar dist) {
+ plane->a = normal->x;
+ plane->b = normal->y;
+ plane->c = normal->z;
+ plane->d = dist;
+
+ return plane;
+}
+
+kmPlane* kmPlaneFromPointAndNormal(kmPlane* pOut, const kmVec3* pPoint, const kmVec3* pNormal)
{
/*
Planea = Nx
View
3 kazmath/plane.h
@@ -56,7 +56,8 @@ typedef enum POINT_CLASSIFICATION {
kmScalar kmPlaneDot(const kmPlane* pP, const struct kmVec4* pV);
kmScalar kmPlaneDotCoord(const kmPlane* pP, const struct kmVec3* pV);
kmScalar kmPlaneDotNormal(const kmPlane* pP, const struct kmVec3* pV);
-kmPlane* kmPlaneFromPointNormal(kmPlane* pOut, const struct kmVec3* pPoint, const struct kmVec3* pNormal);
+kmPlane* kmPlaneFromNormalAndDistance(kmPlane* plane, const struct kmVec3* normal, const kmScalar dist);
+kmPlane* kmPlaneFromPointAndNormal(kmPlane* pOut, const struct kmVec3* pPoint, const struct kmVec3* pNormal);
kmPlane* kmPlaneFromPoints(kmPlane* pOut, const struct kmVec3* p1, const struct kmVec3* p2, const struct kmVec3* p3);
struct kmVec3* kmPlaneIntersectLine(struct kmVec3* pOut, const kmPlane* pP, const struct kmVec3* pV1, const struct kmVec3* pV2);
kmPlane* kmPlaneNormalize(kmPlane* pOut, const kmPlane* pP);
View
36 kazmath/ray3.c
@@ -0,0 +1,36 @@
+#include "plane.h"
+#include "ray3.h"
+
+kmRay3* kmRay3Fill(kmRay3* ray, kmScalar px, kmScalar py, kmScalar pz, kmScalar vx, kmScalar vy, kmScalar vz) {
+ ray->start.x = px;
+ ray->start.y = py;
+ ray->start.z = pz;
+
+ ray->dir.x = vx;
+ ray->dir.y = vy;
+ ray->dir.z = vz;
+
+ return ray;
+}
+
+kmRay3* kmRay3FromPointAndDirection(kmRay3* ray, const kmVec3* point, const kmVec3* direction) {
+ kmVec3Assign(&ray->start, point);
+ kmVec3Assign(&ray->dir, direction);
+ return ray;
+}
+
+kmVec3* kmRay3IntersectPlane(kmVec3* pOut, const kmRay3* ray, const kmPlane* plane) {
+ //t = - (A*org.x + B*org.y + C*org.z + D) / (A*dir.x + B*dir.y + C*dir.z )
+ double t = -(plane->a * ray->start.x +
+ plane->b * ray->start.y +
+ plane->c * ray->start.z + plane->d) / (
+ plane->a * ray->dir.x +
+ plane->b * ray->dir.y +
+ plane->c * ray->dir.z);
+
+
+ kmVec3 scaled_dir;
+ kmVec3Scale(&scaled_dir, &ray->dir, t);
+ kmVec3Add(pOut, &ray->start, &scaled_dir);
+ return pOut;
+}
View
26 kazmath/ray3.h
@@ -0,0 +1,26 @@
+#ifndef RAY3_H
+#define RAY3_H
+
+#include "utility.h"
+#include "vec3.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct kmRay3 {
+ kmVec3 start;
+ kmVec3 dir;
+} kmRay3;
+
+struct kmPlane;
+
+kmRay3* kmRay3Fill(kmRay3* ray, kmScalar px, kmScalar py, kmScalar pz, kmScalar vx, kmScalar vy, kmScalar vz);
+kmRay3* kmRay3FromPointAndDirection(kmRay3* ray, const kmVec3* point, const kmVec3* direction);
+kmVec3* kmRay3IntersectPlane(kmVec3* pOut, const kmRay3* ray, const struct kmPlane* plane);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // RAY3_H
View
7 kazmath/vec3.c
@@ -36,6 +36,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "mat3.h"
#include "vec3.h"
+kmVec3 KM_VEC3_FORWARD = { 0, 0, 1 };
+kmVec3 KM_VEC3_BACKWARD = { 0, 0, -1 };
+kmVec3 KM_VEC3_UP = { 0, 1, 0 };
+kmVec3 KM_VEC3_DOWN = { 0, -1, 0 };
+kmVec3 KM_VEC3_LEFT = { -1, 0, 0 };
+kmVec3 KM_VEC3_RIGHT = { 1, 0, 0 };
+
/**
* Fill a kmVec3 structure using 3 floating point values
* The result is store in pOut, returns pOut
View
7 kazmath/vec3.h
@@ -67,6 +67,13 @@ kmVec3* kmVec3Zero(kmVec3* pOut);
kmVec3* kmVec3GetHorizontalAngle(kmVec3* pOut, const kmVec3 *pIn); /** Get the rotations that would make a (0,0,1) direction vector point in the same direction as this direction vector. */
kmVec3* kmVec3RotationToDirection(kmVec3* pOut, const kmVec3* pIn, const kmVec3* forwards); /** Builds a direction vector from input vector. */
+extern kmVec3 KM_VEC3_FORWARD;
+extern kmVec3 KM_VEC3_BACKWARD;
+extern kmVec3 KM_VEC3_UP;
+extern kmVec3 KM_VEC3_DOWN;
+extern kmVec3 KM_VEC3_LEFT;
+extern kmVec3 KM_VEC3_RIGHT;
+
#ifdef __cplusplus
}
#endif
View
38 tests/test_ray3.cpp
@@ -0,0 +1,38 @@
+#include <unittest++/UnitTest++.h>
+
+#include "../kazmath/ray3.h"
+#include "../kazmath/plane.h"
+
+TEST(test_ray_plane_intersection) {
+ kmPlane p;
+ kmPlaneFromNormalAndDistance(&p, &KM_VEC3_FORWARD, 0);
+
+ CHECK_CLOSE(1.0, p.c, 0.0001);
+
+ kmRay3 r;
+ kmVec3 start;
+ kmVec3Fill(&start, 0, 10, 10);
+ kmRay3FromPointAndDirection(&r, &start, &KM_VEC3_BACKWARD);
+
+ kmVec3 result;
+ kmRay3IntersectPlane(&result, &r, &p);
+
+ CHECK_CLOSE(0.0, result.x, 0.0001);
+ CHECK_CLOSE(10.0, result.y, 0.0001);
+ CHECK_CLOSE(0.0, result.z, 0.0001);
+
+
+ //Now move the ray behind the origin and along the x-axis
+ //and fire it forward into the plane.
+ kmVec3Assign(&r.dir, &KM_VEC3_FORWARD);
+
+ start.x = 10;
+ start.z = 10;
+ kmVec3Assign(&r.start, &start);
+
+ kmRay3IntersectPlane(&result, &r, &p);
+
+ CHECK_CLOSE(10.0, result.x, 0.0001);
+ CHECK_CLOSE(10.0, result.y, 0.0001);
+ CHECK_CLOSE(0.0, result.z, 0.0001);
+}

0 comments on commit 59a1f97

Please sign in to comment.