ScummVM API documentation
hplMatrix.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of HPL1 Engine.
26  */
27 
28 #ifndef HPL_MATRIX_H
29 #define HPL_MATRIX_H
30 
31 #include "hpl1/engine/math/Vector3.h"
32 
33 namespace hpl {
34 
35 template<class T>
36 class cMatrix {
37 public:
38  // The ways to reference the matrix
39  // format is [row][col]
40  union {
41  T m[4][4];
42  T v[16];
43  };
44 
46  // Constructors
48 
49  inline cMatrix() {
50  }
51 
52  //-----------------------------------------------------------------------
53 
54  inline cMatrix(T *pA) {
55  FromVec(pA);
56  }
57 
58  //-----------------------------------------------------------------------
59 
60  inline cMatrix(double *pA) {
61  m[0][0] = (T)pA[0];
62  m[0][1] = (T)pA[1];
63  m[0][2] = (T)pA[2];
64  m[0][3] = (T)pA[3];
65  m[1][0] = (T)pA[4];
66  m[1][1] = (T)pA[5];
67  m[1][2] = (T)pA[6];
68  m[1][3] = (T)pA[7];
69  m[2][0] = (T)pA[8];
70  m[2][1] = (T)pA[9];
71  m[2][2] = (T)pA[10];
72  m[2][3] = (T)pA[11];
73  m[3][0] = (T)pA[12];
74  m[3][1] = (T)pA[13];
75  m[3][2] = (T)pA[14];
76  m[3][3] = (T)pA[15];
77  }
78 
79  //-----------------------------------------------------------------------
80 
81  inline constexpr cMatrix(
82  T a00, T a01, T a02, T a03,
83  T a10, T a11, T a12, T a13,
84  T a20, T a21, T a22, T a23,
85  T a30, T a31, T a32, T a33) : m{
86  {a00, a01, a02, a03},
87  {a10, a11, a12, a13},
88  {a20, a21, a22, a23},
89  {a30, a31, a32, a33}} {
90  }
91 
92  static const cMatrix<T> Identity;
93  static const cMatrix<T> Zero;
94 
95  //-----------------------------------------------------------------------
96 
97  inline bool operator==(const cMatrix<T> &aMtx) const {
98  if (m[0][0] == aMtx.m[0][0] &&
99  m[0][1] == aMtx.m[0][1] &&
100  m[0][2] == aMtx.m[0][2] &&
101  m[0][3] == aMtx.m[0][3] &&
102  m[1][0] == aMtx.m[1][0] &&
103  m[1][1] == aMtx.m[1][1] &&
104  m[1][2] == aMtx.m[1][2] &&
105  m[1][3] == aMtx.m[1][3] &&
106  m[2][0] == aMtx.m[2][0] &&
107  m[2][1] == aMtx.m[2][1] &&
108  m[2][2] == aMtx.m[2][2] &&
109  m[2][3] == aMtx.m[2][3] &&
110  m[3][0] == aMtx.m[3][0] &&
111  m[3][1] == aMtx.m[3][1] &&
112  m[3][2] == aMtx.m[3][2] &&
113  m[3][3] == aMtx.m[3][3]) {
114  return true;
115  }
116 
117  return false;
118  }
119 
120  //-----------------------------------------------------------------------
121 
122  inline bool operator!=(const cMatrix<T> &aMtx) {
123  return !(*this == aMtx);
124  }
125 
126  //-----------------------------------------------------------------------
127 
128  inline void FromVec(const T *pA) {
129  m[0][0] = pA[0];
130  m[0][1] = pA[1];
131  m[0][2] = pA[2];
132  m[0][3] = pA[3];
133  m[1][0] = pA[4];
134  m[1][1] = pA[5];
135  m[1][2] = pA[6];
136  m[1][3] = pA[7];
137  m[2][0] = pA[8];
138  m[2][1] = pA[9];
139  m[2][2] = pA[10];
140  m[2][3] = pA[11];
141  m[3][0] = pA[12];
142  m[3][1] = pA[13];
143  m[3][2] = pA[14];
144  m[3][3] = pA[15];
145  }
146 
147  //-----------------------------------------------------------------------
148 
149  inline void FromTranspose(const T *pA) {
150  m[0][0] = pA[0];
151  m[1][0] = pA[1];
152  m[2][0] = pA[2];
153  m[3][0] = pA[3];
154 
155  m[0][1] = pA[4];
156  m[1][1] = pA[5];
157  m[2][1] = pA[6];
158  m[3][1] = pA[7];
159 
160  m[0][2] = pA[8];
161  m[1][2] = pA[9];
162  m[2][2] = pA[10];
163  m[3][2] = pA[11];
164 
165  m[0][3] = pA[12];
166  m[1][3] = pA[13];
167  m[2][3] = pA[14];
168  m[3][3] = pA[15];
169  }
170 
171  //-----------------------------------------------------------------------
172 
173  inline cVector3<T> GetRight() const {
174  /* @todo ??? */
175  return cVector3<T>(m[0][0], m[0][1], m[0][2]);
176  }
177 
178  inline void SetRight(const cVector3<T> &avVec) {
179  m[0][0] = avVec.x;
180  m[0][1] = avVec.y;
181  m[0][2] = avVec.z;
182  }
183 
184  //-----------------------------------------------------------------------
185 
186  inline cVector3<T> GetUp() const {
187  /* @todo ??? */
188  return cVector3<T>(m[1][0], m[1][1], m[1][2]);
189  }
190 
191  inline void SetUp(const cVector3<T> &avVec) {
192  m[1][0] = avVec.x;
193  m[1][1] = avVec.y;
194  m[1][2] = avVec.z;
195  }
196 
197  //-----------------------------------------------------------------------
198 
199  inline cVector3<T> GetForward() const {
200  /* @todo ??? */
201  return cVector3<T>(m[2][0], m[2][1], m[2][2]);
202  }
203 
204  inline void SetForward(const cVector3<T> &avVec) {
205  m[2][0] = avVec.x;
206  m[2][1] = avVec.y;
207  m[2][2] = avVec.z;
208  }
209 
210  //-----------------------------------------------------------------------
211 
212  inline cVector3<T> GetTranslation() const {
213  /* @todo ??? */
214  return cVector3<T>(m[0][3], m[1][3], m[2][3]);
215  }
216 
217  inline void SetTranslation(const cVector3<T> &avTrans) {
218  m[0][3] = avTrans.x;
219  m[1][3] = avTrans.y;
220  m[2][3] = avTrans.z;
221  }
222 
223  //-----------------------------------------------------------------------
224 
225  inline cMatrix<T> GetRotation() const {
226  return cMatrix<T>(m[0][0], m[0][1], m[0][2], 0,
227  m[1][0], m[1][1], m[1][2], 0,
228  m[2][0], m[2][1], m[2][2], 0,
229  0, 0, 0, 1);
230  }
231 
232  //-----------------------------------------------------------------------
233 
234  inline cMatrix<T> GetTranspose() const {
235  return cMatrix<T>(m[0][0], m[1][0], m[2][0], m[3][0],
236  m[0][1], m[1][1], m[2][1], m[3][1],
237  m[0][2], m[1][2], m[2][2], m[3][2],
238  m[0][3], m[1][3], m[2][3], m[3][3]);
239  }
240 
241  //-----------------------------------------------------------------------
242 
243  tString ToString() {
244  char buf[512];
245  snprintf(buf, 512, "[%f : %f : %f : %f] [%f : %f : %f : %f] [%f : %f : %f : %f] [%f : %f : %f : %f]",
246  m[0][0], m[0][1], m[0][2], m[0][3],
247  m[1][0], m[1][1], m[1][2], m[1][3],
248  m[2][0], m[2][1], m[2][2], m[2][3],
249  m[3][0], m[3][1], m[3][2], m[3][3]);
250  return buf;
251  }
252 
253  tString ToFileString() {
254  char buf[512];
255  snprintf(buf, 512, "%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g",
256  m[0][0], m[0][1], m[0][2], m[0][3],
257  m[1][0], m[1][1], m[1][2], m[1][3],
258  m[2][0], m[2][1], m[2][2], m[2][3],
259  m[3][0], m[3][1], m[3][2], m[3][3]);
260  return buf;
261  }
262 };
263 
264 } // namespace hpl
265 
266 #endif // HPL_MATRIX_H
Definition: AI.h:36
Definition: str.h:59
Definition: Vector3.h:38
Definition: hplMatrix.h:36