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

1 parent 3c898a1 commit 5d22574e268bb657b81f78e22b20951113e14692 committed Jul 28, 2012
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); }
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
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
16 tests/CMakeLists.txt
5 tests/main.cpp
 @@ -0,0 +1,5 @@ +#include + +int main(int argc, char* argv[]) { + return UnitTest::RunAllTests(); +}
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(); -} +
4 tests/test_mat3.cpp
 @@ -142,6 +142,4 @@ TEST(test_mat3_translation) { CHECK(kmMat3AreEqual(&expected, &actual)); } -int main() { - return UnitTest::RunAllTests(); -} +
3 tests/test_mat4.cpp
 @@ -51,6 +51,3 @@ TEST(test_mat4_transpose) { CHECK(kmMat4AreEqual(&transpose, &result)); } -int main() { - return UnitTest::RunAllTests(); -}
51 tests/test_plane.cpp
 @@ -0,0 +1,51 @@ +#include + +#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); + +}
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(); -}
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(); -}