1 //
2 //  mat3.h
3 //  CubicVR2
4 //
5 //  Created by Charles J. Cliffe on 2013-02-22.
6 //  Copyright (c) 2013 Charles J. Cliffe. All rights reserved.
7 //
8 
9 #ifndef __CubicVR2__mat3__
10 #define __CubicVR2__mat3__
11 
12 #include <iostream>
13 #include "vec3.h"
14 #include <cstring>
15 #include <stddef.h>
16 namespace CubicVR {
17 
18     #define mat3SG(c,x,y) \
19         mat3 COMBINE(get,x)() { return y; } \
20         c & COMBINE(set,x)(mat3 value) { y = value; return *this; }
21 
22     struct mat3 {
23 
24         __float a,b,c,d,e,f,g,h,i;
25 
26         //access as-array:
27         inline __float& operator [] (size_t i) {
28             __float* as_array = (__float*)this;
29             return (as_array[i]);
30         }
31 
32         inline const __float& operator [] (size_t i) const {
33             __float* as_array = (__float*)this;
34             return (as_array[i]);
35         }
36 
mat3mat337         mat3(__float ai,__float bi,__float ci,__float di,__float ei,__float fi,__float gi,__float hi,__float ii) {
38             a = ai; b = bi; c = ci; d = di; e = ei; f = fi; g = gi; h = hi; i = ii;
39         };
40 
mat3mat341         mat3() { memset((__float *)this, 0, sizeof(mat3)); }
42         //        mat3 operator* (mat4 m) { return mat3::multiply(*this,m); };
43         //        void operator*= (mat4 m) { *this = mat3::multiply(*this,m); };
44 
45 
identitymat346         static mat3 identity() {
47             return mat3(1.0f, 0.0f, 0.0f,
48                         0.0f, 1.0f, 0.0f,
49                         0.0f, 0.0f, 1.0f);
50         }
51 
transpose_inlinemat352         static void transpose_inline(mat3 &mat) {
53             __float a01 = mat[1], a02 = mat[2], a12 = mat[5];
54 
55             mat[1] = mat[3];
56             mat[2] = mat[6];
57             mat[3] = a01;
58             mat[5] = mat[7];
59             mat[6] = a02;
60             mat[7] = a12;
61         };
62 
transposemat363         static mat3 transpose(mat3 mat_in) {
64             __float a01 = mat_in[1], a02 = mat_in[2], a12 = mat_in[5];
65 
66             mat3 mat;
67 
68             mat[1] = mat_in[3];
69             mat[2] = mat_in[6];
70             mat[3] = a01;
71             mat[5] = mat_in[7];
72             mat[6] = a02;
73             mat[7] = a12;
74 
75             return mat;
76         };
77 
multiplymat378         static vec3 multiply(mat3 m1, vec3 m2) {
79             vec3 mOut;
80 
81             mOut[0] = m2[0] * m1[0] + m2[3] * m1[1] + m2[6] * m1[2] ;
82             mOut[1] = m2[1] * m1[0] + m2[4] * m1[1] + m2[7] * m1[2] ;
83             mOut[2] = m2[2] * m1[0] + m2[5] * m1[1] + m2[8] * m1[2];
84 
85             return mOut;
86         };
87     };
88 
89 
90 }
91 
92 #endif /* defined(__CubicVR2__mat3__) */
93