source: trunk/test/unit/matrix.cpp @ 1257

Last change on this file since 1257 was 1257, checked in by sam, 8 years ago

math: add inversion code for 2×2 and 3×3 matrices, and transposition
code for all matrices.

  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include "core.h"
16#include "lol/unit.h"
17
18namespace lol
19{
20
21LOLUNIT_FIXTURE(MatrixTest)
22{
23    void SetUp()
24    {
25        id2 = mat2(1.0f);
26        tri2 = mat2(vec2(1.0f, 0.0f),
27                    vec2(7.0f, 2.0f));
28        inv2 = mat2(vec2(4.0f, 3.0f),
29                    vec2(3.0f, 2.0f));
30
31        id3 = mat3(1.0f);
32        tri3 = mat3(vec3(1.0f, 0.0f, 0.0f),
33                    vec3(7.0f, 2.0f, 0.0f),
34                    vec3(1.0f, 5.0f, 3.0f));
35        inv3 = mat3(vec3(2.0f, 3.0f, 5.0f),
36                    vec3(3.0f, 2.0f, 3.0f),
37                    vec3(9.0f, 5.0f, 7.0f));
38
39        id4 = mat4(1.0f);
40        tri4 = mat4(vec4(1.0f, 0.0f, 0.0f, 0.0f),
41                    vec4(7.0f, 2.0f, 0.0f, 0.0f),
42                    vec4(1.0f, 5.0f, 3.0f, 0.0f),
43                    vec4(8.0f, 9.0f, 2.0f, 4.0f));
44        inv4 = mat4(vec4( 1.0f,  1.0f,  2.0f, -1.0f),
45                    vec4(-2.0f, -1.0f, -2.0f,  2.0f),
46                    vec4( 4.0f,  2.0f,  5.0f, -4.0f),
47                    vec4( 5.0f, -3.0f, -7.0f, -6.0f));
48    }
49
50    void TearDown() {}
51
52    LOLUNIT_TEST(Determinant)
53    {
54        float d1, d2;
55
56        d1 = determinant(tri2);
57        LOLUNIT_ASSERT_EQUAL(d1, 2.0f);
58        d2 = determinant(inv2);
59        LOLUNIT_ASSERT_EQUAL(d2, -1.0f);
60
61        d1 = determinant(tri3);
62        LOLUNIT_ASSERT_EQUAL(d1, 6.0f);
63        d2 = determinant(inv3);
64        LOLUNIT_ASSERT_EQUAL(d2, 1.0f);
65
66        d1 = determinant(tri4);
67        LOLUNIT_ASSERT_EQUAL(d1, 24.0f);
68        d2 = determinant(inv4);
69        LOLUNIT_ASSERT_EQUAL(d2, -1.0f);
70    }
71
72    LOLUNIT_TEST(Multiplication)
73    {
74        mat4 m0 = id4;
75        mat4 m1 = id4;
76        mat4 m2 = m0 * m1;
77
78        LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
79        LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
80        LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f);
81        LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f);
82
83        LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
84        LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
85        LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f);
86        LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f);
87
88        LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f);
89        LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f);
90        LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f);
91        LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f);
92
93        LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f);
94        LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f);
95        LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f);
96        LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f);
97    }
98
99    LOLUNIT_TEST(Inverse2x2)
100    {
101        mat2 m0 = inv2;
102        mat2 m1 = inverse(m0);
103
104        mat2 m2 = m0 * m1;
105
106        LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
107        LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
108
109        LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
110        LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
111    }
112
113    LOLUNIT_TEST(Inverse3x3)
114    {
115        mat3 m0 = inv3;
116        mat3 m1 = inverse(m0);
117
118        mat3 m2 = m0 * m1;
119
120        LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
121        LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
122        LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f);
123
124        LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
125        LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
126        LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f);
127
128        LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f);
129        LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f);
130        LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f);
131    }
132
133    LOLUNIT_TEST(Inverse4x4)
134    {
135        mat4 m0 = inv4;
136        mat4 m1 = inverse(m0);
137
138        mat4 m2 = m0 * m1;
139
140        LOLUNIT_ASSERT_EQUAL(m2[0][0], 1.0f);
141        LOLUNIT_ASSERT_EQUAL(m2[1][0], 0.0f);
142        LOLUNIT_ASSERT_EQUAL(m2[2][0], 0.0f);
143        LOLUNIT_ASSERT_EQUAL(m2[3][0], 0.0f);
144
145        LOLUNIT_ASSERT_EQUAL(m2[0][1], 0.0f);
146        LOLUNIT_ASSERT_EQUAL(m2[1][1], 1.0f);
147        LOLUNIT_ASSERT_EQUAL(m2[2][1], 0.0f);
148        LOLUNIT_ASSERT_EQUAL(m2[3][1], 0.0f);
149
150        LOLUNIT_ASSERT_EQUAL(m2[0][2], 0.0f);
151        LOLUNIT_ASSERT_EQUAL(m2[1][2], 0.0f);
152        LOLUNIT_ASSERT_EQUAL(m2[2][2], 1.0f);
153        LOLUNIT_ASSERT_EQUAL(m2[3][2], 0.0f);
154
155        LOLUNIT_ASSERT_EQUAL(m2[0][3], 0.0f);
156        LOLUNIT_ASSERT_EQUAL(m2[1][3], 0.0f);
157        LOLUNIT_ASSERT_EQUAL(m2[2][3], 0.0f);
158        LOLUNIT_ASSERT_EQUAL(m2[3][3], 1.0f);
159    }
160
161    mat2 tri2, id2, inv2;
162    mat3 tri3, id3, inv3;
163    mat4 tri4, id4, inv4;
164};
165
166} /* namespace lol */
167
Note: See TracBrowser for help on using the repository browser.