Triton
Visual simulation library for ocean rendering.
Matrix4.h
Go to the documentation of this file.
1// Copyright (c) 2004-2013 Sundog Software, LLC All rights reserved worldwide.
2
7#ifdef SWIG
8%module TritonMatrix4
9%include "Vector4.h"
10#define TRITONAPI
11%{
12#include "Matrix4.h"
13using namespace Triton;
14%}
15#endif
16
17#ifndef TRITON_MATRIX4_H
18#define TRITON_MATRIX4_H
19
20#include "MemAlloc.h"
21#include "Vector4.h"
22
23#pragma pack(push)
24#pragma pack(8)
25
26namespace Triton
27{
29class Matrix4 : public MemObject
30{
31public:
32 static Matrix4 Identity;
33
36 elem[0][0] = 1;
37 elem[0][1] = 0;
38 elem[0][2] = 0;
39 elem[0][3] = 0;
40 elem[1][0] = 0;
41 elem[1][1] = 1;
42 elem[1][2] = 0;
43 elem[1][3] = 0;
44 elem[2][0] = 0;
45 elem[2][1] = 0;
46 elem[2][2] = 1;
47 elem[2][3] = 0;
48 elem[3][0] = 0;
49 elem[3][1] = 0;
50 elem[3][2] = 0;
51 elem[3][3] = 1;
52 }
53
55 Matrix4(double e11, double e12, double e13, double e14,
56 double e21, double e22, double e23, double e24,
57 double e31, double e32, double e33, double e34,
58 double e41, double e42, double e43, double e44) {
59 elem[0][0] = e11;
60 elem[0][1] = e12;
61 elem[0][2] = e13;
62 elem[0][3] = e14;
63 elem[1][0] = e21;
64 elem[1][1] = e22;
65 elem[1][2] = e23;
66 elem[1][3] = e24;
67 elem[2][0] = e31;
68 elem[2][1] = e32;
69 elem[2][2] = e33;
70 elem[2][3] = e34;
71 elem[3][0] = e41;
72 elem[3][1] = e42;
73 elem[3][2] = e43;
74 elem[3][3] = e44;
75 }
76
78 Matrix4(const double *m) {
79 for (int row = 0; row < 4; row++) {
80 for (int col = 0; col < 4; col++) {
81 elem[row][col] = *m++;
82 }
83 }
84 }
85
88 }
89
91 double TRITONAPI operator () (int x, int y) const {
92 return elem[x][y];
93 }
94
96 void TRITONAPI ToFloatArray(float val[16]) const {
97 int i = 0;
98 for (int row = 0; row < 4; row++) {
99 for (int col = 0; col < 4; col++) {
100 val[i++] = (float)elem[row][col];
101 }
102 }
103 }
104
106 Matrix4 TRITONAPI operator * (const Matrix4& mat) const;
107
109 Vector4 TRITONAPI operator * (const Vector4& vec) const;
110
112 Vector3 TRITONAPI operator * (const Vector3& vec) const;
113
115 friend Vector4 TRITONAPI operator * (const Vector4& vec, const Matrix4& mat);
116
118 void TRITONAPI Transpose() {
119 Matrix4 m = *this;
120 for (int row = 0; row < 4; row++) {
121 for (int col = 0; col < 4; col++) {
122 elem[row][col] = m.elem[col][row];
123 }
124 }
125 }
126
128 Matrix4 TRITONAPI InverseCramers(double epsilon = 1E-12) const {
129 Matrix4 inverse;
130
131 double a0 = elem[0][0]*elem[1][1] - elem[0][1]*elem[1][0];
132 double a1 = elem[0][0]*elem[1][2] - elem[0][2]*elem[1][0];
133 double a2 = elem[0][0]*elem[1][3] - elem[0][3]*elem[1][0];
134 double a3 = elem[0][1]*elem[1][2] - elem[0][2]*elem[1][1];
135 double a4 = elem[0][1]*elem[1][3] - elem[0][3]*elem[1][1];
136 double a5 = elem[0][2]*elem[1][3] - elem[0][3]*elem[1][2];
137 double b0 = elem[2][0]*elem[3][1] - elem[2][1]*elem[3][0];
138 double b1 = elem[2][0]*elem[3][2] - elem[2][2]*elem[3][0];
139 double b2 = elem[2][0]*elem[3][3] - elem[2][3]*elem[3][0];
140 double b3 = elem[2][1]*elem[3][2] - elem[2][2]*elem[3][1];
141 double b4 = elem[2][1]*elem[3][3] - elem[2][3]*elem[3][1];
142 double b5 = elem[2][2]*elem[3][3] - elem[2][3]*elem[3][2];
143
144 double det = a0*b5 - a1*b4 + a2*b3 + a3*b2 - a4*b1 + a5*b0;
145 if (fabs(det) > epsilon) {
146 inverse.elem[0][0] = +elem[1][1]*b5 - elem[1][2]*b4 + elem[1][3]*b3;
147 inverse.elem[1][0] = -elem[1][0]*b5 + elem[1][2]*b2 - elem[1][3]*b1;
148 inverse.elem[2][0] = +elem[1][0]*b4 - elem[1][1]*b2 + elem[1][3]*b0;
149 inverse.elem[3][0] = -elem[1][0]*b3 + elem[1][1]*b1 - elem[1][2]*b0;
150 inverse.elem[0][1] = -elem[0][1]*b5 + elem[0][2]*b4 - elem[0][3]*b3;
151 inverse.elem[1][1] = +elem[0][0]*b5 - elem[0][2]*b2 + elem[0][3]*b1;
152 inverse.elem[2][1] = -elem[0][0]*b4 + elem[0][1]*b2 - elem[0][3]*b0;
153 inverse.elem[3][1] = +elem[0][0]*b3 - elem[0][1]*b1 + elem[0][2]*b0;
154 inverse.elem[0][2] = +elem[3][1]*a5 - elem[3][2]*a4 + elem[3][3]*a3;
155 inverse.elem[1][2] = -elem[3][0]*a5 + elem[3][2]*a2 - elem[3][3]*a1;
156 inverse.elem[2][2] = +elem[3][0]*a4 - elem[3][1]*a2 + elem[3][3]*a0;
157 inverse.elem[3][2] = -elem[3][0]*a3 + elem[3][1]*a1 - elem[3][2]*a0;
158 inverse.elem[0][3] = -elem[2][1]*a5 + elem[2][2]*a4 - elem[2][3]*a3;
159 inverse.elem[1][3] = +elem[2][0]*a5 - elem[2][2]*a2 + elem[2][3]*a1;
160 inverse.elem[2][3] = -elem[2][0]*a4 + elem[2][1]*a2 - elem[2][3]*a0;
161 inverse.elem[3][3] = +elem[2][0]*a3 - elem[2][1]*a1 + elem[2][2]*a0;
162
163 double invDet = ((double)1)/det;
164 inverse.elem[0][0] *= invDet;
165 inverse.elem[0][1] *= invDet;
166 inverse.elem[0][2] *= invDet;
167 inverse.elem[0][3] *= invDet;
168 inverse.elem[1][0] *= invDet;
169 inverse.elem[1][1] *= invDet;
170 inverse.elem[1][2] *= invDet;
171 inverse.elem[1][3] *= invDet;
172 inverse.elem[2][0] *= invDet;
173 inverse.elem[2][1] *= invDet;
174 inverse.elem[2][2] *= invDet;
175 inverse.elem[2][3] *= invDet;
176 inverse.elem[3][0] *= invDet;
177 inverse.elem[3][1] *= invDet;
178 inverse.elem[3][2] *= invDet;
179 inverse.elem[3][3] *= invDet;
180
181 }
182
183 return inverse;
184 }
185
187 double * TRITONAPI GetRow(int row) const {
188 if (row < 4) {
189 static double sRow[4];
190 sRow[0] = elem[row][0];
191 sRow[1] = elem[row][1];
192 sRow[2] = elem[row][2];
193 sRow[3] = elem[row][3];
194 return sRow;
195 } else {
196 return 0;
197 }
198 }
199
201 double elem[4][4];
202};
203}
204
205#pragma pack(pop)
206
207#endif
An implementation of a 4x4 matrix and some simple operations on it.
Memory allocation interface for SilverLining.
A simple 4D vector class.
An implementation of a 4x4 matrix and some simple operations on it.
Definition: Matrix4.h:30
Matrix4 TRITONAPI InverseCramers(double epsilon=1E-12) const
Computes the inverse of the matrix using Cramer's rule.
Definition: Matrix4.h:128
double *TRITONAPI GetRow(int row) const
Retrieves a pointer into the requested row of the matrix.
Definition: Matrix4.h:187
Matrix4(const double *m)
Initializes the matrix from an array of 16 double-precision values (row-major).
Definition: Matrix4.h:78
friend Vector4 TRITONAPI operator*(const Vector4 &vec, const Matrix4 &mat)
Multiplies a 1x3 vector by a matrix, yielding a 1x3 vector.
Matrix4(double e11, double e12, double e13, double e14, double e21, double e22, double e23, double e24, double e31, double e32, double e33, double e34, double e41, double e42, double e43, double e44)
This constructor allows you to initialize the matrix as you please.
Definition: Matrix4.h:55
~Matrix4()
Destructor.
Definition: Matrix4.h:87
void TRITONAPI ToFloatArray(float val[16]) const
Populates a static array of 16 floats with the contents of the matrix.
Definition: Matrix4.h:96
double elem[4][4]
Data members are public for convenience.
Definition: Matrix4.h:201
void TRITONAPI Transpose()
Transposes the matrix in-place.
Definition: Matrix4.h:118
double TRITONAPI operator()(int x, int y) const
Retrieve a specific matrix element.
Definition: Matrix4.h:91
Matrix4()
Default constructor; initializes the matrix to an identity transform.
Definition: Matrix4.h:35
This base class for all Triton objects intercepts the new and delete operators, routing them through ...
Definition: MemAlloc.h:71
A 3D double-precision Vector class and its operations.
Definition: Vector3.h:36
A simple double-precision 4D vector class with no operations defined.
Definition: Vector4.h:31