Tests and fixes in math geometry.h module
parent
b735913deb
commit
b8027ce9a7
|
@ -1,21 +1,39 @@
|
|||
/* math/conv.h
|
||||
|
||||
Temporary conversion functions for D3DVECTOR and FPOINT */
|
||||
Temporary conversion functions for D3DVECTOR and D3DMATRIX */
|
||||
|
||||
#pragma once
|
||||
|
||||
#define STRICT
|
||||
#define D3D_OVERLOADS
|
||||
#include <d3d.h>
|
||||
|
||||
#include "vector.h"
|
||||
#include "matrix.h"
|
||||
|
||||
inline D3DVECTOR V_TO_D3D(Math::Vector vec)
|
||||
inline D3DVECTOR VEC_TO_D3DVEC(Math::Vector vec)
|
||||
{
|
||||
return D3DVECTOR(vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
||||
inline Math::Vector D3D_TO_V(D3DVECTOR vec)
|
||||
inline Math::Vector D3DVEC_TO_VEC(D3DVECTOR vec)
|
||||
{
|
||||
return Math::Vector(vec.x, vec.y, vec.z);
|
||||
}
|
||||
|
||||
inline D3DMATRIX MAT_TO_D3DMAT(Math::Matrix mat)
|
||||
{
|
||||
D3DMATRIX result;
|
||||
mat.Transpose();
|
||||
for (int r = 0; r < 4; ++r)
|
||||
{
|
||||
for (int c = 0; c < 16; ++c)
|
||||
result.m[r][c] = mat.m[4*c+r];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
inline Math::Matrix D3DMAT_TO_MAT(D3DMATRIX mat)
|
||||
{
|
||||
Math::Matrix result(mat.m);
|
||||
result.Transpose();
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -140,7 +140,7 @@ inline void RotatePoint(float cx, float cy, float angle, float &px, float &py)
|
|||
\a angleH,angleV rotation angles in radians (positive is counterclockwise (CCW) ) )
|
||||
\a p the point
|
||||
\returns the rotated point */
|
||||
inline Vector RotatePoint(const Vector ¢er, float angleH, float angleV, Vector p)
|
||||
inline void RotatePoint(const Vector ¢er, float angleH, float angleV, Vector &p)
|
||||
{
|
||||
p.x -= center.x;
|
||||
p.y -= center.y;
|
||||
|
@ -151,7 +151,7 @@ inline Vector RotatePoint(const Vector ¢er, float angleH, float angleV, Vect
|
|||
b.y = p.z*sinf(angleV) + p.y*cosf(angleV);
|
||||
b.z = p.x*sinf(angleH) + p.z*cosf(angleH);
|
||||
|
||||
return center + b;
|
||||
p = center + b;
|
||||
}
|
||||
|
||||
//! Rotates a point around a center in space.
|
||||
|
@ -159,7 +159,7 @@ inline Vector RotatePoint(const Vector ¢er, float angleH, float angleV, Vect
|
|||
\a angleH,angleV rotation angles in radians (positive is counterclockwise (CCW) ) )
|
||||
\a p the point
|
||||
\returns the rotated point */
|
||||
inline Vector RotatePoint2(const Vector center, float angleH, float angleV, Vector p)
|
||||
inline void RotatePoint2(const Vector center, float angleH, float angleV, Vector &p)
|
||||
{
|
||||
p.x -= center.x;
|
||||
p.y -= center.y;
|
||||
|
@ -175,7 +175,7 @@ inline Vector RotatePoint2(const Vector center, float angleH, float angleV, Vect
|
|||
b.y = a.z*sinf(angleV) + a.y*cosf(angleV);
|
||||
b.z = a.z*cosf(angleV) - a.y*sinf(angleV);
|
||||
|
||||
return center + b;
|
||||
p = center + b;
|
||||
}
|
||||
|
||||
//! Returns the angle between point (x,y) and (0,0)
|
||||
|
@ -301,8 +301,8 @@ inline void LoadProjectionMatrix(Matrix &mat, float fov = 1.570795f, float aspec
|
|||
/* (1,1) */ mat.m[0 ] = w;
|
||||
/* (2,2) */ mat.m[5 ] = h;
|
||||
/* (3,3) */ mat.m[10] = q;
|
||||
/* (3,4) */ mat.m[14] = 1.0f;
|
||||
/* (4,3) */ mat.m[11] = -q * nearPlane;
|
||||
/* (4,3) */ mat.m[11] = 1.0f;
|
||||
/* (3,4) */ mat.m[14] = -q * nearPlane;
|
||||
}
|
||||
|
||||
//! Loads a translation matrix from given vector
|
||||
|
@ -317,7 +317,7 @@ inline void LoadTranslationMatrix(Matrix &mat, const Vector &trans)
|
|||
|
||||
//! Loads a scaling matrix fom given vector
|
||||
/** \a scale vector with scaling factors for X, Y, Z */
|
||||
inline void LoadScaleMatix(Matrix &mat, const Vector &scale)
|
||||
inline void LoadScaleMatrix(Matrix &mat, const Vector &scale)
|
||||
{
|
||||
mat.LoadIdentity();
|
||||
/* (1,1) */ mat.m[0 ] = scale.x;
|
||||
|
@ -385,27 +385,27 @@ inline void LoadRotationMatrix(Matrix &mat, const Vector &dir, float angle)
|
|||
//! Calculates the matrix to make three rotations in the order X, Z and Y
|
||||
inline void LoadRotationXZYMatrix(Matrix &mat, const Vector &angle)
|
||||
{
|
||||
LoadRotationXMatrix(mat, angle.x);
|
||||
|
||||
Matrix temp;
|
||||
LoadRotationZMatrix(temp, angle.z);
|
||||
mat.Multiply(temp);
|
||||
LoadRotationXMatrix(temp, angle.x);
|
||||
|
||||
LoadRotationZMatrix(mat, angle.z);
|
||||
mat = Math::MultiplyMatrices(temp, mat);
|
||||
|
||||
LoadRotationYMatrix(temp, angle.y);
|
||||
mat.Multiply(temp);
|
||||
mat = Math::MultiplyMatrices(temp, mat);
|
||||
}
|
||||
|
||||
//! Calculates the matrix to make three rotations in the order Z, X and Y
|
||||
inline void LoadRotationZXYMatrix(Matrix &mat, const Vector &angle)
|
||||
{
|
||||
LoadRotationZMatrix(mat, angle.z);
|
||||
|
||||
Matrix temp;
|
||||
LoadRotationXMatrix(temp, angle.x);
|
||||
mat.Multiply(temp);
|
||||
LoadRotationZMatrix(temp, angle.z);
|
||||
|
||||
LoadRotationXMatrix(mat, angle.x);
|
||||
mat = Math::MultiplyMatrices(temp, mat);
|
||||
|
||||
LoadRotationYMatrix(temp, angle.y);
|
||||
mat.Multiply(temp);
|
||||
mat = Math::MultiplyMatrices(temp, mat);
|
||||
}
|
||||
|
||||
//! Returns the distance between projections on XZ plane of two vectors
|
||||
|
@ -536,7 +536,7 @@ inline Vector RotateView(Vector center, float angleH, float angleV, float dist)
|
|||
LoadRotationZMatrix(mat1, -angleV);
|
||||
LoadRotationYMatrix(mat2, -angleH);
|
||||
|
||||
Matrix mat = MultiplyMatrices(mat1, mat2);
|
||||
Matrix mat = MultiplyMatrices(mat2, mat1);
|
||||
|
||||
Vector eye;
|
||||
eye.x = 0.0f+dist;
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include <stdio.h>
|
||||
#include <d3d.h>
|
||||
|
||||
#include "common/struct.h"
|
||||
#include "graphics/d3d/d3dengine.h"
|
||||
#include "math/old/d3dmath.h"
|
||||
#include "graphics/d3d/d3dutil.h"
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
|
||||
#include <math.h>
|
||||
|
||||
#include "common/struct.h"
|
||||
|
||||
|
||||
//>>> geometry.h SegmentPoint()
|
||||
D3DVECTOR SegmentDist(const D3DVECTOR &p1, const D3DVECTOR &p2, float dist);
|
||||
|
|
|
@ -5,7 +5,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0")
|
|||
|
||||
add_executable(matrix_test matrix_test.cpp)
|
||||
add_executable(vector_test vector_test.cpp)
|
||||
add_executable(geometry_test geometry_test.cpp)
|
||||
add_executable(geometry_test geometry_test.cpp ../old/math3d.cpp ../old/d3dmath.cpp ../../graphics/d3d/d3dutil.cpp)
|
||||
|
||||
enable_testing()
|
||||
|
||||
|
@ -13,6 +13,10 @@ add_test(matrix_test ./matrix_test)
|
|||
add_test(vector_test ./vector_test)
|
||||
add_test(geometry_test ./geometry_test)
|
||||
|
||||
include_directories(c:/dxsdk/include)
|
||||
|
||||
add_definitions(-DSTRICT -DD3D_OVERLOADS)
|
||||
|
||||
# 'make check' will compile the required test programs
|
||||
# Note that 'make test' will still fail without compiled programs
|
||||
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS matrix_test vector_test)
|
||||
|
|
|
@ -20,13 +20,18 @@
|
|||
|
||||
#include "../func.h"
|
||||
#include "../geometry.h"
|
||||
#include "../conv.h"
|
||||
#include "../old/math3d.h"
|
||||
#include "../../graphics/d3d/d3dutil.h"
|
||||
|
||||
#include <d3d.h>
|
||||
#include <cstdio>
|
||||
|
||||
using namespace std;
|
||||
|
||||
const float TEST_TOLERANCE = 1e-5;
|
||||
|
||||
|
||||
// Test for rewritten function RotateAngle()
|
||||
int TestRotateAngle()
|
||||
{
|
||||
|
@ -60,12 +65,322 @@ int TestRotateAngle()
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Tests for other altered, complex or uncertain functions
|
||||
|
||||
int TestAngle()
|
||||
{
|
||||
const Math::Vector u(-0.0786076246943884, 0.2231249091714256, -1.1601361718477805);
|
||||
const Math::Vector v(-1.231228742001907, -1.720549809950561, -0.690468438834111);
|
||||
|
||||
float mathResult = Math::Angle(u, v);
|
||||
float oldMathResult = Angle(VEC_TO_D3DVEC(u), VEC_TO_D3DVEC(v));
|
||||
|
||||
if (! Math::IsEqual(mathResult, oldMathResult, TEST_TOLERANCE) )
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestRotateView()
|
||||
{
|
||||
const Math::Vector center(0.617909142705555, 0.896939729454538, -0.615041943652284);
|
||||
const float angleH = 44.5;
|
||||
const float angleV = 12.3;
|
||||
const float dist = 34.76;
|
||||
|
||||
Math::Vector mathResult = Math::RotateView(center, angleH, angleV, dist);
|
||||
Math::Vector oldMathResult = D3DVEC_TO_VEC(RotateView(VEC_TO_D3DVEC(center), angleH, angleV, dist));
|
||||
|
||||
if (! Math::VectorsEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLookatPoint()
|
||||
{
|
||||
const Math::Vector eye(-2.451183170579471, 0.241270270546559, -0.490677411454893);
|
||||
const float angleH = 48.4;
|
||||
const float angleV = 32.4;
|
||||
const float length = 74.44;
|
||||
|
||||
Math::Vector mathResult = Math::LookatPoint(eye, angleH, angleV, length);
|
||||
Math::Vector oldMathResult = D3DVEC_TO_VEC(LookatPoint(VEC_TO_D3DVEC(eye), angleH, angleV, length));
|
||||
|
||||
if (! Math::VectorsEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestProjection()
|
||||
{
|
||||
const Math::Vector a(0.852064846846319, -0.794279497087496, -0.655779805476688);
|
||||
const Math::Vector b(-0.245838834102304, -0.841115596038861, 0.470457161487799);
|
||||
const Math::Vector p(2.289326061164255, -0.505511362271196, 0.660204551169491);
|
||||
|
||||
Math::Vector mathResult = Math::Projection(a, b, p);
|
||||
Math::Vector oldMathResult = D3DVEC_TO_VEC(Projection(VEC_TO_D3DVEC(a), VEC_TO_D3DVEC(b), VEC_TO_D3DVEC(p)));
|
||||
|
||||
if (! Math::VectorsEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadViewMatrix()
|
||||
{
|
||||
const Math::Vector from(2.5646013154868874, -0.6058794133917031, -0.0441195127419744);
|
||||
const Math::Vector at(0.728044925765569, -0.206343977871841, 2.543158236935463);
|
||||
const Math::Vector worldUp(-1.893738133660711, -1.009584441407070, 0.521745988225582);
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadViewMatrix(mathResult, from, at, worldUp);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DVECTOR fromD3D = VEC_TO_D3DVEC(from);
|
||||
D3DVECTOR atD3D = VEC_TO_D3DVEC(at);
|
||||
D3DVECTOR worldUpD3D = VEC_TO_D3DVEC(worldUp);
|
||||
D3DUtil_SetViewMatrix(mat, fromD3D, atD3D, worldUpD3D);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadProjectionMatrix()
|
||||
{
|
||||
const float fov = 76.3f;
|
||||
const float aspect = 0.891f;
|
||||
const float nearPlane = 12.3f;
|
||||
const float farPlane = 1238.9f;
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadProjectionMatrix(mathResult, fov, aspect, nearPlane, farPlane);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DUtil_SetProjectionMatrix(mat, fov, aspect, nearPlane, farPlane);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadTranslationMatrix()
|
||||
{
|
||||
const Math::Vector translation(-0.3631590720995237, 1.6976327614875211, 0.0148815191502145);
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadTranslationMatrix(mathResult, translation);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DUtil_SetTranslateMatrix(mat, translation.x, translation.y, translation.z);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadScaleMatrix()
|
||||
{
|
||||
const Math::Vector scale(0.612236460285503, -0.635566935025364, -0.254321375332065);
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadScaleMatrix(mathResult, scale);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DUtil_SetScaleMatrix(mat, scale.x, scale.y, scale.z);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadRotationXMatrix()
|
||||
{
|
||||
const float angle = 0.513790685774275;
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadRotationXMatrix(mathResult, angle);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DUtil_SetRotateXMatrix(mat, angle);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadRotationYMatrix()
|
||||
{
|
||||
const float angle = -0.569166650127303;
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadRotationYMatrix(mathResult, angle);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DUtil_SetRotateYMatrix(mat, angle);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadRotationZMatrix()
|
||||
{
|
||||
const float angle = 0.380448034347452;
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadRotationZMatrix(mathResult, angle);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DUtil_SetRotateZMatrix(mat, angle);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadRotationMatrix()
|
||||
{
|
||||
const float angle = -0.987747190637790;
|
||||
const Math::Vector dir(-0.113024727688331, -0.781265998072571, 1.838972397076884);
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadRotationMatrix(mathResult, dir, angle);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
D3DVECTOR dirD3D = VEC_TO_D3DVEC(dir);
|
||||
D3DUtil_SetRotationMatrix(mat, dirD3D, angle);
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadRotationXZYMatrix()
|
||||
{
|
||||
const Math::Vector angles(-0.841366567984597, -0.100543315396357, 1.610647811559988);
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadRotationXZYMatrix(mathResult, angles);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
MatRotateXZY(mat, VEC_TO_D3DVEC(angles));
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestLoadRotationZXYMatrix()
|
||||
{
|
||||
const Math::Vector angles(0.275558495480206, -0.224328265970090, 0.943077216574253);
|
||||
|
||||
Math::Matrix mathResult;
|
||||
Math::LoadRotationZXYMatrix(mathResult, angles);
|
||||
|
||||
Math::Matrix oldMathResult;
|
||||
{
|
||||
D3DMATRIX mat;
|
||||
MatRotateZXY(mat, VEC_TO_D3DVEC(angles));
|
||||
oldMathResult = D3DMAT_TO_MAT(mat);
|
||||
}
|
||||
|
||||
if (! Math::MatricesEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int TestTransform()
|
||||
{
|
||||
Math::Matrix transformMatrix(
|
||||
(float[4][4])
|
||||
{
|
||||
{ -0.9282074720977896, 0.6794734970319730, -1.3234304946882685, 0.0925294727863890 },
|
||||
{ -0.0395527963683484, 0.2897634352353881, 1.9144398570315440, -1.4062267508968478 },
|
||||
{ 0.9133323625282361, -0.6741836434774530, -0.2188812951424338, -1.0089184339952666 },
|
||||
{ 0.0f, 0.0f, 0.0f, 1.0f }
|
||||
}
|
||||
);
|
||||
Math::Vector vector(-0.314596433318370, -0.622681232583150, -0.371307535743574);
|
||||
|
||||
Math::Vector mathResult = Math::Transform(transformMatrix, vector);
|
||||
Math::Vector oldMathResult = Transform(transformMatrix, vector);
|
||||
|
||||
if (! Math::VectorsEqual(mathResult, oldMathResult, TEST_TOLERANCE))
|
||||
return __LINE__;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Functions to test
|
||||
int (*TESTS[])() =
|
||||
{
|
||||
TestRotateAngle
|
||||
TestRotateAngle,
|
||||
TestAngle,
|
||||
TestRotateView,
|
||||
TestLookatPoint,
|
||||
TestProjection,
|
||||
TestLoadViewMatrix,
|
||||
TestLoadProjectionMatrix,
|
||||
TestLoadTranslationMatrix,
|
||||
TestLoadScaleMatrix,
|
||||
TestLoadRotationXMatrix,
|
||||
TestLoadRotationYMatrix,
|
||||
TestLoadRotationZMatrix,
|
||||
TestLoadRotationMatrix,
|
||||
TestLoadRotationXZYMatrix,
|
||||
TestLoadRotationZXYMatrix,
|
||||
TestTransform
|
||||
};
|
||||
const int TESTS_SIZE = sizeof(TESTS) / sizeof(*TESTS);
|
||||
|
||||
|
@ -74,7 +389,10 @@ int main()
|
|||
{
|
||||
result = TESTS[i]();
|
||||
if (result != 0)
|
||||
{
|
||||
fprintf(stderr, "Test function %d failed at line %d\n", i+1, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "All tests successful\n");
|
||||
|
|
Loading…
Reference in New Issue