Skip to content
Browse files

Restructure the tests. Write some tests for the new plane functions

  • Loading branch information...
1 parent 3c898a1 commit 5d22574e268bb657b81f78e22b20951113e14692 @Kazade committed Jul 28, 2012
Showing with 75 additions and 33 deletions.
  1. +8 −5 kazmath/plane.c
  2. +1 −1 kazmath/plane.h
  3. +4 −0 kazmath/utility.h
  4. +4 −12 tests/CMakeLists.txt
  5. +5 −0 tests/main.cpp
  6. +1 −3 tests/test_aabb.cpp
  7. +1 −3 tests/test_mat3.cpp
  8. +0 −3 tests/test_mat4.cpp
  9. +51 −0 tests/test_plane.cpp
  10. +0 −3 tests/test_ray2.cpp
  11. +0 −3 tests/test_vec2.cpp
View
13 kazmath/plane.c
@@ -174,11 +174,14 @@ const POINT_CLASSIFICATION kmPlaneClassifyPoint(const kmPlane* pIn, const kmVec3
return POINT_ON_PLANE;
}
-kmPlane* kmPlaneExtractFromMat4(kmPlane* pOut, const struct kmMat4* pIn, kmUint row) {
- pOut->a = pIn->mat[3] + pIn->mat[row];
- pOut->b = pIn->mat[7] + pIn->mat[row + 4];
- pOut->c = pIn->mat[11] + pIn->mat[row + 8];
- pOut->d = pIn->mat[15] + pIn->mat[row + 12];
+kmPlane* kmPlaneExtractFromMat4(kmPlane* pOut, const struct kmMat4* pIn, kmInt row) {
+ int scale = (row < 0) ? -1 : 1;
+ row = abs(row) - 1;
+
+ pOut->a = pIn->mat[3] + scale * pIn->mat[row];
+ pOut->b = pIn->mat[7] + scale * pIn->mat[row + 4];
+ pOut->c = pIn->mat[11] + scale * pIn->mat[row + 8];
+ pOut->d = pIn->mat[15] + scale * pIn->mat[row + 12];
return kmPlaneNormalize(pOut, pOut);
}
View
2 kazmath/plane.h
@@ -63,7 +63,7 @@ kmPlane* const kmPlaneNormalize(kmPlane* pOut, const kmPlane* pP);
kmPlane* const kmPlaneScale(kmPlane* pOut, const kmPlane* pP, kmScalar s);
const POINT_CLASSIFICATION kmPlaneClassifyPoint(const kmPlane* pIn, const kmVec3* pP); /** Classifys a point against a plane */
-kmPlane* kmPlaneExtractFromMat4(kmPlane* pOut, const struct kmMat4* pIn, kmUint row);
+kmPlane* kmPlaneExtractFromMat4(kmPlane* pOut, const struct kmMat4* pIn, kmInt row);
kmVec3* kmPlaneGetIntersection(kmVec3* pOut, const kmPlane* p1, const kmPlane* p2, const kmPlane* p3);
#ifdef __cplusplus
View
4 kazmath/utility.h
@@ -49,6 +49,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define kmUint unsigned int
#endif
+#ifndef kmInt
+#define kmInt int
+#endif
+
#ifndef KM_FALSE
#define KM_FALSE 0
#endif
View
16 tests/CMakeLists.txt
@@ -2,18 +2,10 @@ INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/include )
LINK_LIBRARIES(libunittest++.a)
-ADD_EXECUTABLE(test_mat4 ${KAZMATH_SOURCES} test_mat4.cpp)
-ADD_TEST(test_mat4 test_mat4)
+FILE(GLOB_RECURSE TEST_FILES *.cpp)
-ADD_EXECUTABLE(test_mat3 ${KAZMATH_SOURCES} test_mat3.cpp)
-ADD_TEST(test_mat3 test_mat3)
+LINK_LIBRARIES(kazmath)
-ADD_EXECUTABLE(test_ray2 ${KAZMATH_SOURCES} test_ray2.cpp)
-ADD_TEST(test_ray2 test_ray2)
-
-ADD_EXECUTABLE(test_vec2 ${KAZMATH_SOURCES} test_vec2.cpp)
-ADD_TEST(test_vec2 test_vec2)
-
-ADD_EXECUTABLE(test_aabb ${KAZMATH_SOURCES} test_aabb.cpp)
-ADD_TEST(test_aabb test_aabb)
+ADD_EXECUTABLE(kazmath_tests ${TEST_FILES})
+ADD_TEST(kazmath_suite kazmath_tests)
View
5 tests/main.cpp
@@ -0,0 +1,5 @@
+#include <unittest++/UnitTest++.h>
+
+int main(int argc, char* argv[]) {
+ return UnitTest::RunAllTests();
+}
View
4 tests/test_aabb.cpp
@@ -51,6 +51,4 @@ TEST(test_aabb_triangle_intersection) {
CHECK(kmAABBIntersectsTriangle(&box, &tri4[0], &tri4[1], &tri4[2]));
}
-int main() {
- return UnitTest::RunAllTests();
-}
+
View
4 tests/test_mat3.cpp
@@ -142,6 +142,4 @@ TEST(test_mat3_translation) {
CHECK(kmMat3AreEqual(&expected, &actual));
}
-int main() {
- return UnitTest::RunAllTests();
-}
+
View
3 tests/test_mat4.cpp
@@ -51,6 +51,3 @@ TEST(test_mat4_transpose) {
CHECK(kmMat4AreEqual(&transpose, &result));
}
-int main() {
- return UnitTest::RunAllTests();
-}
View
51 tests/test_plane.cpp
@@ -0,0 +1,51 @@
+#include <unittest++/UnitTest++.h>
+
+#include "../kazmath/kazmath.h"
+
+TEST(test_plane_extraction_from_matrix) {
+
+ kmMat4 ortho;
+ kmMat4OrthographicProjection(&ortho, -1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
+
+ kmPlane p;
+ kmPlaneExtractFromMat4(&p, &ortho, -2); //-2 == TOP
+
+ CHECK_EQUAL(p.d, 1.0); //Plane's distance from the origin should be 1
+
+ CHECK_EQUAL(p.a, 0.0); //Plane's normal should be directly down
+ CHECK_EQUAL(p.b, -1.0);
+ CHECK_EQUAL(p.c, 0.0);
+
+ kmPlaneExtractFromMat4(&p, &ortho, 2); //Bottom
+ CHECK_EQUAL(p.d, 1.0); //Plane's distance from the origin should be 1
+
+ CHECK_EQUAL(p.a, 0.0); //Plane's normal should be directly up
+ CHECK_EQUAL(p.b, 1.0);
+ CHECK_EQUAL(p.c, 0.0);
+
+/*
+ ExtractPlane(view_frustum[LEFT], MV, 1);
+ ExtractPlane(view_frustum[RIGHT], MV, -1);
+ ExtractPlane(view_frustum[BOTTOM], MV, 2);
+ ExtractPlane(view_frustum[TOP], MV, -2);
+ ExtractPlane(view_frustum[NEAR], MV, 3);
+ ExtractPlane(view_frustum[FAR], MV, -3);
+*/
+}
+
+TEST(test_three_plane_intersection) {
+
+ kmPlane p1, p2, p3;
+
+ p1.a = 0.0; p1.b = 1.0; p1.c = 0.0; p1.d = 1.0; //Floor
+ p2.a = 1.0; p2.b = 0.0; p2.c = 0.0; p2.d = 1.0; //Left
+ p3.a = 0.0; p3.b = 0.0; p3.c = 1.0; p3.d = 1.0; //Back
+
+ kmVec3 p;
+ CHECK(kmPlaneGetIntersection(&p, &p1, &p2, &p3));
+
+ CHECK_EQUAL(-1.0, p.x);
+ CHECK_EQUAL(-1.0, p.y);
+ CHECK_EQUAL(-1.0, p.z);
+
+}
View
3 tests/test_ray2.cpp
@@ -69,6 +69,3 @@ TEST(test_triangle_intersection) {
CHECK_CLOSE(1.0f, normal.y, 0.001f);
}
-int main() {
- return UnitTest::RunAllTests();
-}
View
3 tests/test_vec2.cpp
@@ -18,6 +18,3 @@ TEST(test_transform) {
CHECK_CLOSE(0.0f, rotated.y, 0.001f);
}
-int main() {
- return UnitTest::RunAllTests();
-}

0 comments on commit 5d22574

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