OpenLexocad  27.1
Matrix.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2005 Imetric 3D GmbH *
3  * *
4  * This file is part of the FreeCAD CAx development system. *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Library General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Library General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Library General Public *
17  * License along with this library; see the file COPYING.LIB. If not, *
18  * write to the Free Software Foundation, Inc., 59 Temple Place, *
19  * Suite 330, Boston, MA 02111-1307, USA *
20  * *
21  ***************************************************************************/
22 
23 
24 #ifndef BASE_MATRIX_H
25 #define BASE_MATRIX_H
26 
27 #include <cassert>
28 #include <cmath>
29 #include <string>
30 
31 #include <Base/Vector3D.h>
32 
33 namespace Base {
34 
38 class LX_BASE_EXPORT Matrix4D
39 {
41 
42 public:
44 
47  Matrix4D(void);
48 
50  Matrix4D (float a11, float a12, float a13, float a14,
51  float a21, float a22, float a23, float a24,
52  float a31, float a32, float a33, float a34,
53  float a41, float a42, float a43, float a44 );
55  Matrix4D (double a11, double a12, double a13, double a14,
56  double a21, double a22, double a23, double a24,
57  double a31, double a32, double a33, double a34,
58  double a41, double a42, double a43, double a44 );
60  Matrix4D (const Matrix4D& rclMtrx);
62  Matrix4D (const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
63  Matrix4D (const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
65  ~Matrix4D () {}
66 
69  inline Matrix4D operator + (const Matrix4D& rclMtrx) const;
71  inline Matrix4D& operator += (const Matrix4D& rclMtrx);
73  inline Matrix4D operator - (const Matrix4D& rclMtrx) const;
74  inline Matrix4D& operator -= (const Matrix4D& rclMtrx);
76  inline Matrix4D& operator *= (const Matrix4D& rclMtrx);
78  inline Matrix4D& operator = (const Matrix4D& rclMtrx);
80  inline Matrix4D operator * (const Matrix4D& rclMtrx) const;
82  inline Vector3f operator * (const Vector3f& rclVct) const;
83  inline Vector3d operator * (const Vector3d& rclVct) const;
84  inline void multVec(const Vector3d & src, Vector3d & dst) const;
85  inline void multVec(const Vector3f & src, Vector3f & dst) const;
87  inline bool operator != (const Matrix4D& rclMtrx) const;
89  inline bool operator == (const Matrix4D& rclMtrx) const;
91  inline double* operator [] (unsigned short usNdx);
93  inline const double* operator[] (unsigned short usNdx) const;
95  double determinant() const;
97  std::string analyse(void) const;
99  Matrix4D& Outer(const Vector3f& rV1, const Vector3f& rV2);
100  Matrix4D& Outer(const Vector3d& rV1, const Vector3d& rV2);
102  Matrix4D& Hat(const Vector3f& rV);
103  Matrix4D& Hat(const Vector3d& rV);
105 
106  void getMatrix (double dMtrx[16]) const;
107  void setMatrix (const double dMtrx[16]);
109  void getGLMatrix (double dMtrx[16]) const;
111  void setGLMatrix (const double dMtrx[16]);
112 
113  unsigned long getMemSpace (void);
114 
117  void setToUnity(void);
120  void nullify(void);
122  void move (float x, float y, float z)
123  { move(Vector3f(x,y,z)); }
124  void move (double x, double y, double z)
125  { move(Vector3d(x,y,z)); }
127  void move (const Vector3f& rclVct);
128  void move (const Vector3d& rclVct);
130  void scale (float x, float y, float z)
131  { scale(Vector3f(x,y,z)); }
132  void scale (double x, double y, double z)
133  { scale(Vector3d(x,y,z)); }
135  void scale (const Vector3f& rclVct);
136  void scale (const Vector3d& rclVct);
138  int hasScale(double tol=0.0) const;
140  void rotX (double fAngle);
142  void rotY (double fAngle);
144  void rotZ (double fAngle);
146  void rotLine (const Vector3f& rclVct, float fAngle);
148  void rotLine (const Vector3d& rclVct, double fAngle);
150  void rotLine (const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
152  void rotLine (const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
154  bool toAxisAngle (Vector3f& rclBase, Vector3f& rclDir, float& fAngle, float& fTranslation) const;
155  bool toAxisAngle (Vector3d& rclBase, Vector3d& rclDir, double& fAngle, double& fTranslation) const;
157  void transform (const Vector3f& rclVct, const Matrix4D& rclMtrx);
158  void transform (const Vector3d& rclVct, const Matrix4D& rclMtrx);
160  void inverse (void);
162  void inverseOrthogonal(void);
164  void inverseGauss (void);
165  void transpose (void);
167 
168  void Print (void) const;
170  std::string toString(void) const;
172  void fromString (const std::string &str);
173 
174 private:
175  double dMtrx4D[4][4];
176 };
177 
178 inline Matrix4D Matrix4D::operator + (const Matrix4D& rclMtrx) const
179 {
180  Matrix4D clMat;
181  unsigned short iz, is;
182 
183  for (iz = 0; iz < 4; iz++) {
184  for (is = 0; is < 4; is++) {
185  clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] + rclMtrx[iz][is];
186  }
187  }
188 
189  return clMat;
190 }
191 
192 inline Matrix4D& Matrix4D::operator += (const Matrix4D& rclMtrx)
193 {
194  unsigned short iz, is;
195 
196  for (iz = 0; iz < 4; iz++) {
197  for (is = 0; is < 4; is++) {
198  dMtrx4D[iz][is] += rclMtrx[iz][is];
199  }
200  }
201 
202  return *this;
203 }
204 
205 inline Matrix4D Matrix4D::operator - (const Matrix4D& rclMtrx) const
206 {
207  Matrix4D clMat;
208  unsigned short iz, is;
209 
210  for (iz = 0; iz < 4; iz++) {
211  for (is = 0; is < 4; is++) {
212  clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] - rclMtrx[iz][is];
213  }
214  }
215 
216  return clMat;
217 }
218 
219 inline Matrix4D& Matrix4D::operator -= (const Matrix4D& rclMtrx)
220 {
221  unsigned short iz, is;
222 
223  for (iz = 0; iz < 4; iz++) {
224  for (is = 0; is < 4; is++) {
225  dMtrx4D[iz][is] -= rclMtrx[iz][is];
226  }
227  }
228 
229  return *this;
230 }
231 
232 inline Matrix4D& Matrix4D::operator *= (const Matrix4D& rclMtrx)
233 {
234  Matrix4D clMat;
235  unsigned short ie, iz, is;
236 
237  for (iz = 0; iz < 4; iz++)
238  for (is = 0; is < 4; is++) {
239  clMat.dMtrx4D[iz][is] = 0;
240  for (ie = 0; ie < 4; ie++)
241  clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] *
242  rclMtrx.dMtrx4D[ie][is];
243  }
244 
245  (*this) = clMat;
246 
247  return *this;
248 }
249 
250 inline Matrix4D Matrix4D::operator * (const Matrix4D& rclMtrx) const
251 {
252  Matrix4D clMat;
253  unsigned short ie, iz, is;
254 
255  for (iz = 0; iz < 4; iz++)
256  for (is = 0; is < 4; is++) {
257  clMat.dMtrx4D[iz][is] = 0;
258  for (ie = 0; ie < 4; ie++)
259  clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] *
260  rclMtrx.dMtrx4D[ie][is];
261  }
262 
263  return clMat;
264 }
265 
266 inline Matrix4D& Matrix4D::operator= (const Matrix4D& rclMtrx)
267 {
268  unsigned short iz, is;
269 
270  for (iz = 0; iz < 4; iz++) {
271  for (is = 0; is < 4; is++) {
272  dMtrx4D[iz][is] = rclMtrx.dMtrx4D[iz][is];
273  }
274  }
275 
276  return *this;
277 }
278 
279 inline Vector3f Matrix4D::operator* (const Vector3f& rclVct) const
280 {
281  double x = static_cast<double>(rclVct.x);
282  double y = static_cast<double>(rclVct.y);
283  double z = static_cast<double>(rclVct.z);
284  return Vector3f(
285  static_cast<float>(dMtrx4D[0][0]*x + dMtrx4D[0][1]*y +
286  dMtrx4D[0][2]*z + dMtrx4D[0][3]),
287  static_cast<float>(dMtrx4D[1][0]*x + dMtrx4D[1][1]*y +
288  dMtrx4D[1][2]*z + dMtrx4D[1][3]),
289  static_cast<float>(dMtrx4D[2][0]*x + dMtrx4D[2][1]*y +
290  dMtrx4D[2][2]*z + dMtrx4D[2][3])
291  );
292 }
293 
294 inline Vector3d Matrix4D::operator* (const Vector3d& rclVct) const
295 {
296  return Vector3d((dMtrx4D[0][0]*rclVct.x + dMtrx4D[0][1]*rclVct.y +
297  dMtrx4D[0][2]*rclVct.z + dMtrx4D[0][3]),
298  (dMtrx4D[1][0]*rclVct.x + dMtrx4D[1][1]*rclVct.y +
299  dMtrx4D[1][2]*rclVct.z + dMtrx4D[1][3]),
300  (dMtrx4D[2][0]*rclVct.x + dMtrx4D[2][1]*rclVct.y +
301  dMtrx4D[2][2]*rclVct.z + dMtrx4D[2][3]));
302 }
303 
304 inline void Matrix4D::multVec(const Vector3d & src, Vector3d & dst) const
305 {
306  double x = (dMtrx4D[0][0]*src.x + dMtrx4D[0][1]*src.y +
307  dMtrx4D[0][2]*src.z + dMtrx4D[0][3]);
308  double y = (dMtrx4D[1][0]*src.x + dMtrx4D[1][1]*src.y +
309  dMtrx4D[1][2]*src.z + dMtrx4D[1][3]);
310  double z = (dMtrx4D[2][0]*src.x + dMtrx4D[2][1]*src.y +
311  dMtrx4D[2][2]*src.z + dMtrx4D[2][3]);
312  dst.Set(x,y,z);
313 }
314 
315 inline void Matrix4D::multVec(const Vector3f & src, Vector3f & dst) const
316 {
317  double sx = static_cast<double>(src.x);
318  double sy = static_cast<double>(src.y);
319  double sz = static_cast<double>(src.z);
320 
321  double x = (dMtrx4D[0][0]*sx + dMtrx4D[0][1]*sy +
322  dMtrx4D[0][2]*sz + dMtrx4D[0][3]);
323  double y = (dMtrx4D[1][0]*sx + dMtrx4D[1][1]*sy +
324  dMtrx4D[1][2]*sz + dMtrx4D[1][3]);
325  double z = (dMtrx4D[2][0]*sx + dMtrx4D[2][1]*sy +
326  dMtrx4D[2][2]*sz + dMtrx4D[2][3]);
327  dst.Set(static_cast<float>(x),
328  static_cast<float>(y),
329  static_cast<float>(z));
330 }
331 
332 inline bool Matrix4D::operator== (const Matrix4D& rclMtrx) const
333 {
334  unsigned short iz, is;
335 
336  for (iz = 0; iz < 4; iz++) {
337  for (is = 0; is < 4; is++) {
338  if (fabs(dMtrx4D[iz][is] - rclMtrx.dMtrx4D[iz][is]) > traits_type::epsilon())
339  return false;
340  }
341  }
342 
343  return true;
344 }
345 
346 inline bool Matrix4D::operator!= (const Matrix4D& rclMtrx) const
347 {
348  return !( (*this) == rclMtrx );
349 }
350 
351 inline Vector3f& operator*= (Vector3f& rclVect,
352  const Matrix4D& rclMtrx)
353 {
354  rclVect = rclMtrx * rclVect;
355  return rclVect;
356 }
357 
358 inline double* Matrix4D::operator[] (unsigned short usNdx)
359 {
360  return dMtrx4D[usNdx];
361 }
362 
363 inline const double* Matrix4D::operator[] (unsigned short usNdx) const
364 {
365  return dMtrx4D[usNdx];
366 }
367 
368 } // namespace Base
369 
370 
371 #endif // BASE_MATRIX_H
372 
373 
_Precision z
Definition: Vector3D.h:89
Matrix4D & operator+=(const Matrix4D &rclMtrx)
Definition: Matrix.h:192
_Precision y
Definition: Vector3D.h:88
bool operator==(const Matrix4D &rclMtrx) const
Comparison.
Definition: Matrix.h:332
Matrix4D operator *(const Matrix4D &rclMtrx) const
Matrix multiplication.
Definition: Matrix.h:250
const double * operator[](unsigned short usNdx) const
Index operator.
Definition: Matrix.h:358
Vector3f & operator *=(Vector3f &rclVect, const Matrix4D &rclMtrx)
Definition: Matrix.h:351
Definition: Vector3D.h:69
Matrix4D operator+(const Matrix4D &rclMtrx) const
Matrix addition.
Definition: Matrix.h:178
Matrix4D operator -(const Matrix4D &rclMtrx) const
Matrix subtraction.
Definition: Matrix.h:205
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) ENTT_NOEXCEPT
Compares two hashed strings.
Definition: entt.hpp:570
Matrix4D & operator *=(const Matrix4D &rclMtrx)
Matrix multiplication.
Definition: Matrix.h:232
Vector3< double > Vector3d
Definition: Vector3D.h:248
_Precision x
Definition: Vector3D.h:87
void transform(Container container, OutputIt out, BinaryFunction function)
Definition: Algorithms.h:29
Vector3< float > Vector3f
Definition: Vector3D.h:247
DualNumber operator+(DualNumber a, DualNumber b)
Definition: DualNumber.h:51
Matrix4D & operator=(const Matrix4D &rclMtrx)
Assignment.
Definition: Matrix.h:266
void scale(float x, float y, float z)
scale for the vector
Definition: Matrix.h:130
void move(float x, float y, float z)
moves the coordinatesystem for the x,y,z value
Definition: Matrix.h:122
Matrix4D & operator -=(const Matrix4D &rclMtrx)
Definition: Matrix.h:219
Definition: Matrix.h:38
Definition: AbstractXMLReader.h:5
void move(double x, double y, double z)
Definition: Matrix.h:124
Definition: Vector3D.h:78
void scale(double x, double y, double z)
Definition: Matrix.h:132
void Set(_Precision fX, _Precision fY, _Precision fZ)
static float_type epsilon()
Definition: Vector3D.h:72
~Matrix4D()
Destruction.
Definition: Matrix.h:65
DualNumber operator *(DualNumber a, DualNumber b)
Definition: DualNumber.h:71
void multVec(const Vector3d &src, Vector3d &dst) const
Definition: Matrix.h:304