Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/ClientMath11Tests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

1#include "../Rendering/ClientMath.h"
2#include <d3dx9.h>
3#include <iostream>
4#include <random>
5#include <stdexcept>
6
7namespace M=RiseMath;
8namespace
9{
10 unsigned checks=0;float maximum=0;
11 void Check(bool value,const char* message){++checks;if(!value)throw std::runtime_error(message);}
12 void Near(const float* a,const float* b,size_t count,const char* message,float tolerance=3e-5f)
13 {
14 for(size_t i=0;i<count;++i)
15 {
16 const float scale=(std::max)(1.f,std::abs(a[i]));const float error=std::abs(a[i]-b[i])/scale;
17 maximum=(std::max)(maximum,error);
18 if(!(error<=tolerance)){std::cerr<<message<<" component "<<i<<" reference="<<a[i]<<" native="<<b[i]<<'\n';Check(false,message);}
19 ++checks;
20 }
21 }
22 void Exercise()
23 {
24 std::mt19937 random(114711);std::uniform_real_distribution<float> value(-4,4);
25 for(unsigned i=0;i<10000;++i)
26 {
27 const float yaw=value(random),pitch=value(random),roll=value(random),t=value(random)*.25f;
28 const D3DXVECTOR3 a(value(random),value(random),value(random)),b(value(random),value(random),value(random));
29 const M::Vector3 na(a.x,a.y,a.z),nb(b.x,b.y,b.z);
30 D3DXVECTOR3 vr;M::Vector3 vn;D3DXMATRIX mr,ir;M::Matrix mn,in;
31 D3DXQUATERNION qr,br,pr;M::Quaternion qn,bn,pn;
32 D3DXVec3Normalize(&vr,&a);M::Vec3Normalize(&vn,&na);Near(vr,vn,3,"normalize");
33 D3DXVec3Cross(&vr,&a,&b);M::Vec3Cross(&vn,&na,&nb);Near(vr,vn,3,"cross");
34 D3DXVec3Lerp(&vr,&a,&b,t);M::Vec3Lerp(&vn,&na,&nb,t);Near(vr,vn,3,"lerp");
35 D3DXMatrixRotationYawPitchRoll(&mr,yaw,pitch,roll);M::MatrixRotationYawPitchRoll(&mn,yaw,pitch,roll);Near(mr,mn,16,"rotation matrix");
36 mr._41=mn._41=a.x;mr._42=mn._42=a.y;mr._43=mn._43=a.z;
37 D3DXVec3TransformCoord(&vr,&b,&mr);M::Vec3TransformCoord(&vn,&nb,&mn);Near(vr,vn,3,"point transform");
38 D3DXVec3TransformNormal(&vr,&b,&mr);M::Vec3TransformNormal(&vn,&nb,&mn);Near(vr,vn,3,"direction transform");
39 float dr=0,dn=0;D3DXMatrixInverse(&ir,&dr,&mr);M::MatrixInverse(&in,&dn,&mn);Near(ir,in,16,"inverse");Near(&dr,&dn,1,"determinant");
40 D3DXQuaternionRotationYawPitchRoll(&qr,yaw,pitch,roll);M::QuaternionRotationYawPitchRoll(&qn,yaw,pitch,roll);Near(qr,qn,4,"quaternion angles");
41 D3DXQuaternionRotationYawPitchRoll(&br,roll,yaw,pitch);M::QuaternionRotationYawPitchRoll(&bn,roll,yaw,pitch);
42 D3DXQuaternionMultiply(&pr,&qr,&br);M::QuaternionMultiply(&pn,&qn,&bn);Near(pr,pn,4,"quaternion order");
43 D3DXQuaternionSlerp(&pr,&qr,&br,t);M::QuaternionSlerp(&pn,&qn,&bn,t);Near(pr,pn,4,"slerp");
44 const D3DXVECTOR3 c(0,0,1),origin=(a+b)*.25f+c*.5f+D3DXVECTOR3(0,0,4),direction(0,0,-1);
45 const M::Vector3 nc(0,0,1),no(origin.x,origin.y,origin.z),nd(0,0,-1);
46 float ur=0,uv=0,distR=0,un=0,vnHit=0,distN=0;
47 const bool hitR=D3DXIntersectTri(&a,&b,&c,&origin,&direction,&ur,&uv,&distR)!=FALSE;
48 const bool hitN=M::IntersectTri(&na,&nb,&nc,&no,&nd,&un,&vnHit,&distN);
49 Check(hitR==hitN,"triangle hit classification");
50 // The float reference loses precision as the projected area goes
51 // to zero. Compare it on well-conditioned triangles; analytical
52 // fixtures below cover grazing rays and very small geometry.
53 const float projectedArea=std::abs((b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x));
54 if(hitR && projectedArea>.1f){Near(&ur,&un,1,"triangle U");Near(&uv,&vnHit,1,"triangle V");Near(&distR,&distN,1,"triangle distance");}
55 D3DXPLANE planeR;M::Plane planeN;
56 D3DXPlaneFromPoints(&planeR,&a,&b,&c);M::PlaneFromPoints(&planeN,&na,&nb,&nc);Near(planeR,planeN,4,"plane equation");
57 D3DXCOLOR color(yaw,pitch,roll,t);M::Color nativeColor(yaw,pitch,roll,t);
58 Check(static_cast<DWORD>(color)==static_cast<uint32_t>(nativeColor),"packed color rounding");
59 const float nearZ=std::pow(10.f,float(i%12)-9),farZ=nearZ*1000;
60 const float viewWidth=nearZ*(.1f+std::abs(yaw)),viewHeight=nearZ*(.1f+std::abs(pitch));
61 D3DXMatrixPerspectiveRH(&mr,viewWidth,viewHeight,nearZ,farZ);M::MatrixPerspectiveRH(&mn,viewWidth,viewHeight,nearZ,farZ);
62 Near(mr,mn,16,"projection across cockpit and orbital scales");
63 D3DXMatrixPerspectiveFovRH(&mr,.5f+std::abs(yaw)*.1f,.5f+std::abs(pitch),nearZ,farZ);
64 M::MatrixPerspectiveFovRH(&mn,.5f+std::abs(yaw)*.1f,.5f+std::abs(pitch),nearZ,farZ);Near(mr,mn,16,"FOV projection at every scale");
65 D3DXMatrixRotationAxis(&mr,&a,roll);M::MatrixRotationAxis(&mn,&na,roll);Near(mr,mn,16,"arbitrary rotation axis");
66 }
67 for(float scale:{1e-9f,1e-4f,1.f,1000.f})
68 {
69 const M::Vector3 a(0,0,0),b(scale,0,0),c(0,scale,0),down(0,0,-1),parallel(1,0,0);
70 for(const M::Vector3 point:{a,b,c,M::Vector3(scale*.25f,scale*.25f,0)})
71 {
72 const M::Vector3 origin=point+M::Vector3(0,0,4);float u,v,distance;
73 Check(M::IntersectTri(&a,&b,&c,&origin,&down,&u,&v,&distance),"triangle vertices and interior at every scale");
74 const float expected=4;Near(&expected,&distance,1,"analytical triangle distance");
75 Check(!M::IntersectTri(&a,&b,&c,&origin,&parallel,&u,&v,&distance),"parallel ray misses");
76 }
77 const M::Vector3 outside(scale*2,scale*2,4),behind(scale*.25f,scale*.25f,-4);
78 Check(!M::IntersectTri(&a,&b,&c,&outside,&down,nullptr,nullptr,nullptr),"outside triangle misses");
79 Check(!M::IntersectTri(&a,&b,&c,&behind,&down,nullptr,nullptr,nullptr),"triangle behind ray misses");
80 }
81 const D3DXVECTOR3 zero(0,0,0);const M::Vector3 nativeZero;D3DXVECTOR3 zr;M::Vector3 zn;
82 D3DXVec3Normalize(&zr,&zero);M::Vec3Normalize(&zn,&nativeZero);Near(zr,zn,3,"zero normalization");
83 for(float angle:{0.f,.25f,-1.f,3.f})
84 {
85 D3DXMATRIX mr;M::Matrix mn;D3DXMatrixRotationAxis(&mr,&zero,angle);M::MatrixRotationAxis(&mn,&nativeZero,angle);
86 Near(mr,mn,16,"authored zero-axis hinge");
87 }
88 M::Matrix singular,untouched;std::memset(&untouched,0x35,sizeof(untouched));const auto before=untouched;
89 Check(M::MatrixInverse(&untouched,nullptr,&singular)==nullptr && untouched==before,"singular inverse leaves output untouched");
90 Check(sizeof(M::Vector3)==sizeof(D3DXVECTOR3) && sizeof(M::Quaternion)==sizeof(D3DXQUATERNION) && sizeof(M::Matrix)==sizeof(D3DXMATRIX),"authored and wire math sizes");
91 }
92}
93int main()
94{
95 try {Exercise();std::cout<<"PASS: "<<checks<<" math comparisons; maximum scaled error="<<maximum<<'\n';return 0;}
96 catch(const std::exception& error){std::cerr<<"FAIL: "<<error.what()<<'\n';return 1;}
97}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141