quartz: Exclude unused headers.
[wine] / dlls / d3drm / tests / vector.c
1 /*
2  * Copyright 2007 Vijay Kiran Kamuju
3  * Copyright 2007 David Adam
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <assert.h>
21 #include "d3drmdef.h"
22 #include <math.h>
23
24 #include "wine/test.h"
25
26 #define PI (4*atan(1.0))
27 #define admit_error 0.000001
28
29 #define expect_mat( expectedmat, gotmat)\
30 { \
31     int i,j,equal=1; \
32     for (i=0; i<4; i++)\
33         {\
34          for (j=0; j<4; j++)\
35              {\
36               if (fabs(expectedmat[i][j]-gotmat[i][j])>admit_error)\
37                  {\
38                   equal=0;\
39                  }\
40              }\
41         }\
42     ok(equal, "Expected matrix=\n(%f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f\n)\n\n" \
43        "Got matrix=\n(%f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f)\n", \
44        expectedmat[0][0],expectedmat[0][1],expectedmat[0][2],expectedmat[0][3], \
45        expectedmat[1][0],expectedmat[1][1],expectedmat[1][2],expectedmat[1][3], \
46        expectedmat[2][0],expectedmat[2][1],expectedmat[2][2],expectedmat[2][3], \
47        expectedmat[3][0],expectedmat[3][1],expectedmat[3][2],expectedmat[3][3], \
48        gotmat[0][0],gotmat[0][1],gotmat[0][2],gotmat[0][3], \
49        gotmat[1][0],gotmat[1][1],gotmat[1][2],gotmat[1][3], \
50        gotmat[2][0],gotmat[2][1],gotmat[2][2],gotmat[2][3], \
51        gotmat[3][0],gotmat[3][1],gotmat[3][2],gotmat[3][3] ); \
52 }
53
54 #define expect_quat(expectedquat,gotquat) \
55   ok( (fabs(U1(expectedquat.v).x-U1(gotquat.v).x)<admit_error) &&     \
56       (fabs(U2(expectedquat.v).y-U2(gotquat.v).y)<admit_error) &&     \
57       (fabs(U3(expectedquat.v).z-U3(gotquat.v).z)<admit_error) &&     \
58       (fabs(expectedquat.s-gotquat.s)<admit_error), \
59   "Expected Quaternion %f %f %f %f , Got Quaternion %f %f %f %f\n", \
60       expectedquat.s,U1(expectedquat.v).x,U2(expectedquat.v).y,U3(expectedquat.v).z, \
61       gotquat.s,U1(gotquat.v).x,U2(gotquat.v).y,U3(gotquat.v).z);
62
63 #define expect_vec(expectedvec,gotvec) \
64   ok( ((fabs(U1(expectedvec).x-U1(gotvec).x)<admit_error)&&(fabs(U2(expectedvec).y-U2(gotvec).y)<admit_error)&&(fabs(U3(expectedvec).z-U3(gotvec).z)<admit_error)), \
65   "Expected Vector= (%f, %f, %f)\n , Got Vector= (%f, %f, %f)\n", \
66   U1(expectedvec).x,U2(expectedvec).y,U3(expectedvec).z, U1(gotvec).x, U2(gotvec).y, U3(gotvec).z);
67
68 static HMODULE d3drm_handle = 0;
69
70 static void (WINAPI * pD3DRMMatrixFromQuaternion)(D3DRMMATRIX4D, LPD3DRMQUATERNION);
71 static LPD3DVECTOR (WINAPI* pD3DRMVectorAdd)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
72 static LPD3DVECTOR (WINAPI* pD3DRMVectorCrossProduct)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
73 static D3DVALUE (WINAPI* pD3DRMVectorDotProduct)(LPD3DVECTOR, LPD3DVECTOR);
74 static D3DVALUE (WINAPI* pD3DRMVectorModulus)(LPD3DVECTOR);
75 static LPD3DVECTOR (WINAPI * pD3DRMVectorNormalize)(LPD3DVECTOR);
76 static LPD3DVECTOR (WINAPI * pD3DRMVectorReflect)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
77 static LPD3DVECTOR (WINAPI * pD3DRMVectorRotate)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR, D3DVALUE);
78 static LPD3DVECTOR (WINAPI * pD3DRMVectorScale)(LPD3DVECTOR, LPD3DVECTOR, D3DVALUE);
79 static LPD3DVECTOR (WINAPI * pD3DRMVectorSubtract)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
80 static LPD3DRMQUATERNION (WINAPI * pD3DRMQuaternionFromRotation)(LPD3DRMQUATERNION, LPD3DVECTOR, D3DVALUE);
81 static LPD3DRMQUATERNION (WINAPI * pD3DRMQuaternionSlerp)(LPD3DRMQUATERNION, LPD3DRMQUATERNION, LPD3DRMQUATERNION, D3DVALUE);
82
83 #define D3DRM_GET_PROC(func) \
84     p ## func = (void*)GetProcAddress(d3drm_handle, #func); \
85     if(!p ## func) { \
86       trace("GetProcAddress(%s) failed\n", #func); \
87       FreeLibrary(d3drm_handle); \
88       return FALSE; \
89     }
90
91 static BOOL InitFunctionPtrs(void)
92 {
93     d3drm_handle = LoadLibraryA("d3drm.dll");
94
95     if(!d3drm_handle)
96     {
97         skip("Could not load d3drm.dll\n");
98         return FALSE;
99     }
100
101     D3DRM_GET_PROC(D3DRMMatrixFromQuaternion)
102     D3DRM_GET_PROC(D3DRMVectorAdd)
103     D3DRM_GET_PROC(D3DRMVectorCrossProduct)
104     D3DRM_GET_PROC(D3DRMVectorDotProduct)
105     D3DRM_GET_PROC(D3DRMVectorModulus)
106     D3DRM_GET_PROC(D3DRMVectorNormalize)
107     D3DRM_GET_PROC(D3DRMVectorReflect)
108     D3DRM_GET_PROC(D3DRMVectorRotate)
109     D3DRM_GET_PROC(D3DRMVectorScale)
110     D3DRM_GET_PROC(D3DRMVectorSubtract)
111     D3DRM_GET_PROC(D3DRMQuaternionFromRotation)
112     D3DRM_GET_PROC(D3DRMQuaternionSlerp)
113
114     return TRUE;
115 }
116
117
118 static void VectorTest(void)
119 {
120     D3DVALUE mod,par,theta;
121     D3DVECTOR e,r,u,v,w,axis,casnul,norm,ray;
122
123     U1(u).x=2.0;U2(u).y=2.0;U3(u).z=1.0;
124     U1(v).x=4.0;U2(v).y=4.0;U3(v).z=0.0;
125
126 /*______________________VectorAdd_________________________________*/
127     pD3DRMVectorAdd(&r,&u,&v);
128     U1(e).x=6.0;U2(e).y=6.0;U3(e).z=1.0;
129     expect_vec(e,r);
130
131 /*_______________________VectorSubtract__________________________*/
132     pD3DRMVectorSubtract(&r,&u,&v);
133     U1(e).x=-2.0;U2(e).y=-2.0;U3(e).z=1.0;
134     expect_vec(e,r);
135
136 /*_______________________VectorCrossProduct_______________________*/
137     pD3DRMVectorCrossProduct(&r,&u,&v);
138     U1(e).x=-4.0;U2(e).y=4.0;U3(e).z=0.0;
139     expect_vec(e,r);
140
141 /*_______________________VectorDotProduct__________________________*/
142     mod=pD3DRMVectorDotProduct(&u,&v);
143     ok((mod == 16.0), "Expected 16.0, Got %f\n",mod);
144
145 /*_______________________VectorModulus_____________________________*/
146     mod=pD3DRMVectorModulus(&u);
147     ok((mod == 3.0), "Expected 3.0, Got %f\n",mod);
148
149 /*_______________________VectorNormalize___________________________*/
150     pD3DRMVectorNormalize(&u);
151     U1(e).x=2.0/3.0;U2(e).y=2.0/3.0;U3(e).z=1.0/3.0;
152     expect_vec(e,u);
153
154 /* If u is the NULL vector, MSDN says that the return vector is NULL. In fact, the returned vector is (1,0,0). The following test case prove it. */
155
156     U1(casnul).x=0.0; U2(casnul).y=0.0; U3(casnul).z=0.0;
157     pD3DRMVectorNormalize(&casnul);
158     U1(e).x=1.0; U2(e).y=0.0; U3(e).z=0.0;
159     expect_vec(e,casnul);
160
161 /*____________________VectorReflect_________________________________*/
162     U1(ray).x=3.0; U2(ray).y=-4.0; U3(ray).z=5.0;
163     U1(norm).x=1.0; U2(norm).y=-2.0; U3(norm).z=6.0;
164     U1(e).x=79.0; U2(e).y=-160.0; U3(e).z=487.0;
165     pD3DRMVectorReflect(&r,&ray,&norm);
166     expect_vec(e,r);
167
168 /*_______________________VectorRotate_______________________________*/
169     U1(w).x=3.0; U2(w).y=4.0; U3(w).z=0.0;
170     U1(axis).x=0.0; U2(axis).y=0.0; U3(axis).z=1.0;
171     theta=2.0*PI/3.0;
172     pD3DRMVectorRotate(&r,&w,&axis,theta);
173     U1(e).x=-0.3-0.4*sqrt(3.0); U2(e).y=0.3*sqrt(3.0)-0.4; U3(e).z=0.0;
174     expect_vec(e,r);
175
176 /* The same formula gives D3DRMVectorRotate, for theta in [-PI/2;+PI/2] or not. The following test proves this fact.*/
177     theta=-PI/4.0;
178     pD3DRMVectorRotate(&r,&w,&axis,-PI/4);
179     U1(e).x=1.4/sqrt(2.0); U2(e).y=0.2/sqrt(2.0); U3(e).z=0.0;
180     expect_vec(e,r);
181
182 /*_______________________VectorScale__________________________*/
183     par=2.5;
184     pD3DRMVectorScale(&r,&v,par);
185     U1(e).x=10.0; U2(e).y=10.0; U3(e).z=0.0;
186     expect_vec(e,r);
187 }
188
189 static void MatrixTest(void)
190 {
191     D3DRMQUATERNION q;
192     D3DRMMATRIX4D exp,mat;
193
194     exp[0][0]=-49.0; exp[0][1]=4.0;   exp[0][2]=22.0;  exp[0][3]=0.0;
195     exp[1][0]=20.0;  exp[1][1]=-39.0; exp[1][2]=20.0;  exp[1][3]=0.0;
196     exp[2][0]=10.0;  exp[2][1]=28.0;  exp[2][2]=-25.0; exp[2][3]=0.0;
197     exp[3][0]=0.0;   exp[3][1]=0.0;   exp[3][2]=0.0;   exp[3][3]=1.0;
198     q.s=1.0; U1(q.v).x=2.0; U2(q.v).y=3.0; U3(q.v).z=4.0;
199
200    pD3DRMMatrixFromQuaternion(mat,&q);
201    expect_mat(exp,mat);
202 }
203
204 static void QuaternionTest(void)
205 {
206     D3DVECTOR axis;
207     D3DVALUE g,h,epsilon,par,theta;
208     D3DRMQUATERNION q,q1,q2,r;
209
210 /*_________________QuaternionFromRotation___________________*/
211     U1(axis).x=1.0; U2(axis).y=1.0; U3(axis).z=1.0;
212     theta=2.0*PI/3.0;
213     pD3DRMQuaternionFromRotation(&r,&axis,theta);
214     q.s=0.5; U1(q.v).x=0.5; U2(q.v).y=0.5; U3(q.v).z=0.5;
215     expect_quat(q,r);
216
217 /*_________________QuaternionSlerp_________________________*/
218 /* Interpolation slerp is in fact a linear interpolation, not a spherical linear
219  * interpolation. Moreover, if the angle of the two quaternions is in ]PI/2;3PI/2[, QuaternionSlerp
220  * interpolates between the first quaternion and the opposite of the second one. The test proves
221  * these two facts. */
222     par=0.31;
223     q1.s=1.0; U1(q1.v).x=2.0; U2(q1.v).y=3.0; U3(q1.v).z=50.0;
224     q2.s=-4.0; U1(q2.v).x=6.0; U2(q2.v).y=7.0; U3(q2.v).z=8.0;
225 /* The angle between q1 and q2 is in [-PI/2,PI/2]. So, one interpolates between q1 and q2. */
226     epsilon=1.0;
227     g=1.0-par; h=epsilon*par;
228 /* Part of the test proving that the interpolation is linear. */
229     q.s=g*q1.s+h*q2.s;
230     U1(q.v).x=g*U1(q1.v).x+h*U1(q2.v).x;
231     U2(q.v).y=g*U2(q1.v).y+h*U2(q2.v).y;
232     U3(q.v).z=g*U3(q1.v).z+h*U3(q2.v).z;
233     pD3DRMQuaternionSlerp(&r,&q1,&q2,par);
234     expect_quat(q,r);
235
236     q1.s=1.0; U1(q1.v).x=2.0; U2(q1.v).y=3.0; U3(q1.v).z=50.0;
237     q2.s=-94.0; U1(q2.v).x=6.0; U2(q2.v).y=7.0; U3(q2.v).z=-8.0;
238 /* The angle between q1 and q2 is not in [-PI/2,PI/2]. So, one interpolates between q1 and -q2. */
239     epsilon=-1.0;
240     g=1.0-par; h=epsilon*par;
241     q.s=g*q1.s+h*q2.s;
242     U1(q.v).x=g*U1(q1.v).x+h*U1(q2.v).x;
243     U2(q.v).y=g*U2(q1.v).y+h*U2(q2.v).y;
244     U3(q.v).z=g*U3(q1.v).z+h*U3(q2.v).z;
245     pD3DRMQuaternionSlerp(&r,&q1,&q2,par);
246     expect_quat(q,r);
247 }
248
249 START_TEST(vector)
250 {
251     if(!InitFunctionPtrs())
252         return;
253
254     VectorTest();
255     MatrixTest();
256     QuaternionTest();
257
258     FreeLibrary(d3drm_handle);
259 }