Skip to content
Browse files

Implement kmVec3ProjectOnToPlane() with a test

  • Loading branch information...
1 parent 59a1f97 commit 7447554694e07ff842de628eb73adc4d1ea03fdd @Kazade committed Feb 15, 2013
Showing with 47 additions and 0 deletions.
  1. +1 −0 kazmath.files
  2. +1 −0 kazmath/kazmath.h
  3. +8 −0 kazmath/plane.c
  4. +1 −0 kazmath/plane.h
  5. +13 −0 kazmath/vec3.c
  6. +3 −0 kazmath/vec3.h
  7. +20 −0 tests/test_vec3.cpp
View
1 kazmath.files
@@ -83,3 +83,4 @@ tests/test_quaternion.cpp
kazmath/ray3.h
kazmath/ray3.c
tests/test_ray3.cpp
+tests/test_vec3.cpp
View
1 kazmath/kazmath.h
@@ -35,5 +35,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "plane.h"
#include "aabb.h"
#include "ray2.h"
+#include "ray3.h"
#endif // KAZMATH_H_INCLUDED
View
8 kazmath/plane.c
@@ -244,3 +244,11 @@ kmVec3* kmPlaneGetIntersection(kmVec3* pOut, const kmPlane* p1, const kmPlane* p
return pOut;
}
+kmPlane* kmPlaneFill(kmPlane* plane, kmScalar a, kmScalar b, kmScalar c, kmScalar d) {
+ plane->a = a;
+ plane->b = b;
+ plane->c = c;
+ plane->d = d;
+
+ return plane;
+}
View
1 kazmath/plane.h
@@ -53,6 +53,7 @@ typedef enum POINT_CLASSIFICATION {
POINT_ON_PLANE,
} POINT_CLASSIFICATION;
+kmPlane* kmPlaneFill(kmPlane* plane, float a, float b, float c, float d);
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);
View
13 kazmath/vec3.c
@@ -35,6 +35,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "mat4.h"
#include "mat3.h"
#include "vec3.h"
+#include "plane.h"
+#include "ray3.h"
kmVec3 KM_VEC3_FORWARD = { 0, 0, 1 };
kmVec3 KM_VEC3_BACKWARD = { 0, 0, -1 };
@@ -404,3 +406,14 @@ kmVec3* kmVec3RotationToDirection(kmVec3* pOut, const kmVec3* pIn, const kmVec3*
return pOut;
}
+
+kmVec3* kmVec3ProjectOnToPlane(kmVec3* pOut, const kmVec3* point, const struct kmPlane* plane) {
+ kmRay3 ray;
+ kmVec3Assign(&ray.start, point);
+ ray.dir.x = -plane->a;
+ ray.dir.y = -plane->b;
+ ray.dir.z = -plane->c;
+
+ kmRay3IntersectPlane(pOut, &ray, plane);
+ return pOut;
+}
View
3 kazmath/vec3.h
@@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
struct kmMat4;
struct kmMat3;
+struct kmPlane;
typedef struct kmVec3 {
kmScalar x;
@@ -67,6 +68,8 @@ 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. */
+kmVec3* kmVec3ProjectOnToPlane(kmVec3* pOut, const kmVec3* point, const struct kmPlane* plane);
+
extern kmVec3 KM_VEC3_FORWARD;
extern kmVec3 KM_VEC3_BACKWARD;
extern kmVec3 KM_VEC3_UP;
View
20 tests/test_vec3.cpp
@@ -0,0 +1,20 @@
+#include <UnitTest++.h>
+
+#include "../kazmath/vec3.h"
+#include "../kazmath/plane.h"
+
+TEST(test_project_onto_plane) {
+
+ kmPlane xy;
+ kmPlaneFill(&xy, 0, 0, 1, 0);
+
+ kmVec3 point;
+ kmVec3Fill(&point, 0, 10, 10);
+
+ kmVec3 result;
+ kmVec3ProjectOnToPlane(&result, &point, &xy);
+
+ CHECK_CLOSE(0.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 7447554

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