OpenLexocad  28.0
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 #pragma once
25 
26 #include <string>
27 #include <Base/Vector3D.h>
28 
29 namespace Base {
30 
34 class LX_BASE_EXPORT Matrix4D
35 {
36  typedef float_traits<double> traits_type;
37 
38 public:
40 
43  Matrix4D(void);
44 
46  Matrix4D (float a11, float a12, float a13, float a14,
47  float a21, float a22, float a23, float a24,
48  float a31, float a32, float a33, float a34,
49  float a41, float a42, float a43, float a44 );
51  Matrix4D (double a11, double a12, double a13, double a14,
52  double a21, double a22, double a23, double a24,
53  double a31, double a32, double a33, double a34,
54  double a41, double a42, double a43, double a44 );
56  Matrix4D (const Matrix4D& rclMtrx);
58  Matrix4D (const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
59  Matrix4D (const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
61  ~Matrix4D () {}
62 
65  inline Matrix4D operator + (const Matrix4D& rclMtrx) const;
67  inline Matrix4D& operator += (const Matrix4D& rclMtrx);
69  inline Matrix4D operator - (const Matrix4D& rclMtrx) const;
70  inline Matrix4D& operator -= (const Matrix4D& rclMtrx);
72  inline Matrix4D& operator *= (const Matrix4D& rclMtrx);
74  inline Matrix4D& operator = (const Matrix4D& rclMtrx);
76  inline Matrix4D operator * (const Matrix4D& rclMtrx) const;
78  inline Vector3f operator * (const Vector3f& rclVct) const;
79  inline Vector3d operator * (const Vector3d& rclVct) const;
80  inline void multVec(const Vector3d & src, Vector3d & dst) const;
81  inline void multVec(const Vector3f & src, Vector3f & dst) const;
83  inline bool operator != (const Matrix4D& rclMtrx) const;
85  inline bool operator == (const Matrix4D& rclMtrx) const;
87  inline double* operator [] (unsigned short usNdx);
89  inline const double* operator[] (unsigned short usNdx) const;
91  double determinant() const;
93  std::string analyse(void) const;
95  Matrix4D& Outer(const Vector3f& rV1, const Vector3f& rV2);
96  Matrix4D& Outer(const Vector3d& rV1, const Vector3d& rV2);
98  Matrix4D& Hat(const Vector3f& rV);
99  Matrix4D& Hat(const Vector3d& rV);
101 
102  void getMatrix (double dMtrx[16]) const;
103  void setMatrix (const double dMtrx[16]);
105  void getGLMatrix (double dMtrx[16]) const;
107  void setGLMatrix (const double dMtrx[16]);
108 
109  unsigned long getMemSpace (void);
110 
113  void setToUnity(void);
116  void nullify(void);
118  void move (float x, float y, float z)
119  { move(Vector3f(x,y,z)); }
120  void move (double x, double y, double z)
121  { move(Vector3d(x,y,z)); }
123  void move (const Vector3f& rclVct);
124  void move (const Vector3d& rclVct);
126  void scale (float x, float y, float z)
127  { scale(Vector3f(x,y,z)); }
128  void scale (double x, double y, double z)
129  { scale(Vector3d(x,y,z)); }
131  void scale (const Vector3f& rclVct);
132  void scale (const Vector3d& rclVct);
134  int hasScale(double tol=0.0) const;
136  void rotX (double fAngle);
138  void rotY (double fAngle);
140  void rotZ (double fAngle);
142  void rotLine (const Vector3f& rclVct, float fAngle);
144  void rotLine (const Vector3d& rclVct, double fAngle);
146  void rotLine (const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
148  void rotLine (const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
150  bool toAxisAngle (Vector3f& rclBase, Vector3f& rclDir, float& fAngle, float& fTranslation) const;
151  bool toAxisAngle (Vector3d& rclBase, Vector3d& rclDir, double& fAngle, double& fTranslation) const;
153  void transform (const Vector3f& rclVct, const Matrix4D& rclMtrx);
154  void transform (const Vector3d& rclVct, const Matrix4D& rclMtrx);
156  void inverse (void);
158  void inverseOrthogonal(void);
160  void inverseGauss (void);
161  void transpose (void);
163 
164  void Print (void) const;
166  std::string toString(void) const;
168  void fromString (const std::string &str);
169 
170 private:
171  double dMtrx4D[4][4];
172 };
173 
174 inline Matrix4D Matrix4D::operator + (const Matrix4D& rclMtrx) const
175 {
176  Matrix4D clMat;
177  unsigned short iz, is;
178 
179  for (iz = 0; iz < 4; iz++) {
180  for (is = 0; is < 4; is++) {
181  clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] + rclMtrx[iz][is];
182  }
183  }
184 
185  return clMat;
186 }
187 
188 inline Matrix4D& Matrix4D::operator += (const Matrix4D& rclMtrx)
189 {
190  unsigned short iz, is;
191 
192  for (iz = 0; iz < 4; iz++) {
193  for (is = 0; is < 4; is++) {
194  dMtrx4D[iz][is] += rclMtrx[iz][is];
195  }
196  }
197 
198  return *this;
199 }
200 
201 inline Matrix4D Matrix4D::operator - (const Matrix4D& rclMtrx) const
202 {
203  Matrix4D clMat;
204  unsigned short iz, is;
205 
206  for (iz = 0; iz < 4; iz++) {
207  for (is = 0; is < 4; is++) {
208  clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] - rclMtrx[iz][is];
209  }
210  }
211 
212  return clMat;
213 }
214 
215 inline Matrix4D& Matrix4D::operator -= (const Matrix4D& rclMtrx)
216 {
217  unsigned short iz, is;
218 
219  for (iz = 0; iz < 4; iz++) {
220  for (is = 0; is < 4; is++) {
221  dMtrx4D[iz][is] -= rclMtrx[iz][is];
222  }
223  }
224 
225  return *this;
226 }
227 
228 inline Matrix4D& Matrix4D::operator *= (const Matrix4D& rclMtrx)
229 {
230  Matrix4D clMat;
231  unsigned short ie, iz, is;
232 
233  for (iz = 0; iz < 4; iz++)
234  for (is = 0; is < 4; is++) {
235  clMat.dMtrx4D[iz][is] = 0;
236  for (ie = 0; ie < 4; ie++)
237  clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] *
238  rclMtrx.dMtrx4D[ie][is];
239  }
240 
241  (*this) = clMat;
242 
243  return *this;
244 }
245 
246 inline Matrix4D Matrix4D::operator * (const Matrix4D& rclMtrx) const
247 {
248  Matrix4D clMat;
249  unsigned short ie, iz, is;
250 
251  for (iz = 0; iz < 4; iz++)
252  for (is = 0; is < 4; is++) {
253  clMat.dMtrx4D[iz][is] = 0;
254  for (ie = 0; ie < 4; ie++)
255  clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] *
256  rclMtrx.dMtrx4D[ie][is];
257  }
258 
259  return clMat;
260 }
261 
262 inline Matrix4D& Matrix4D::operator= (const Matrix4D& rclMtrx)
263 {
264  unsigned short iz, is;
265 
266  for (iz = 0; iz < 4; iz++) {
267  for (is = 0; is < 4; is++) {
268  dMtrx4D[iz][is] = rclMtrx.dMtrx4D[iz][is];
269  }
270  }
271 
272  return *this;
273 }
274 
275 inline Vector3f Matrix4D::operator* (const Vector3f& rclVct) const
276 {
277  double x = static_cast<double>(rclVct.x);
278  double y = static_cast<double>(rclVct.y);
279  double z = static_cast<double>(rclVct.z);
280  return Vector3f(
281  static_cast<float>(dMtrx4D[0][0]*x + dMtrx4D[0][1]*y +
282  dMtrx4D[0][2]*z + dMtrx4D[0][3]),
283  static_cast<float>(dMtrx4D[1][0]*x + dMtrx4D[1][1]*y +
284  dMtrx4D[1][2]*z + dMtrx4D[1][3]),
285  static_cast<float>(dMtrx4D[2][0]*x + dMtrx4D[2][1]*y +
286  dMtrx4D[2][2]*z + dMtrx4D[2][3])
287  );
288 }
289 
290 inline Vector3d Matrix4D::operator* (const Vector3d& rclVct) const
291 {
292  return Vector3d((dMtrx4D[0][0]*rclVct.x + dMtrx4D[0][1]*rclVct.y +
293  dMtrx4D[0][2]*rclVct.z + dMtrx4D[0][3]),
294  (dMtrx4D[1][0]*rclVct.x + dMtrx4D[1][1]*rclVct.y +
295  dMtrx4D[1][2]*rclVct.z + dMtrx4D[1][3]),
296  (dMtrx4D[2][0]*rclVct.x + dMtrx4D[2][1]*rclVct.y +
297  dMtrx4D[2][2]*rclVct.z + dMtrx4D[2][3]));
298 }
299 
300 inline void Matrix4D::multVec(const Vector3d & src, Vector3d & dst) const
301 {
302  double x = (dMtrx4D[0][0]*src.x + dMtrx4D[0][1]*src.y +
303  dMtrx4D[0][2]*src.z + dMtrx4D[0][3]);
304  double y = (dMtrx4D[1][0]*src.x + dMtrx4D[1][1]*src.y +
305  dMtrx4D[1][2]*src.z + dMtrx4D[1][3]);
306  double z = (dMtrx4D[2][0]*src.x + dMtrx4D[2][1]*src.y +
307  dMtrx4D[2][2]*src.z + dMtrx4D[2][3]);
308  dst.Set(x,y,z);
309 }
310 
311 inline void Matrix4D::multVec(const Vector3f & src, Vector3f & dst) const
312 {
313  double sx = static_cast<double>(src.x);
314  double sy = static_cast<double>(src.y);
315  double sz = static_cast<double>(src.z);
316 
317  double x = (dMtrx4D[0][0]*sx + dMtrx4D[0][1]*sy +
318  dMtrx4D[0][2]*sz + dMtrx4D[0][3]);
319  double y = (dMtrx4D[1][0]*sx + dMtrx4D[1][1]*sy +
320  dMtrx4D[1][2]*sz + dMtrx4D[1][3]);
321  double z = (dMtrx4D[2][0]*sx + dMtrx4D[2][1]*sy +
322  dMtrx4D[2][2]*sz + dMtrx4D[2][3]);
323  dst.Set(static_cast<float>(x),
324  static_cast<float>(y),
325  static_cast<float>(z));
326 }
327 
328 inline bool Matrix4D::operator== (const Matrix4D& rclMtrx) const
329 {
330  unsigned short iz, is;
331 
332  for (iz = 0; iz < 4; iz++) {
333  for (is = 0; is < 4; is++) {
334  if (fabs(dMtrx4D[iz][is] - rclMtrx.dMtrx4D[iz][is]) > traits_type::epsilon())
335  return false;
336  }
337  }
338 
339  return true;
340 }
341 
342 inline bool Matrix4D::operator!= (const Matrix4D& rclMtrx) const
343 {
344  return !( (*this) == rclMtrx );
345 }
346 
347 inline Vector3f& operator*= (Vector3f& rclVect,
348  const Matrix4D& rclMtrx)
349 {
350  rclVect = rclMtrx * rclVect;
351  return rclVect;
352 }
353 
354 inline double* Matrix4D::operator[] (unsigned short usNdx)
355 {
356  return dMtrx4D[usNdx];
357 }
358 
359 inline const double* Matrix4D::operator[] (unsigned short usNdx) const
360 {
361  return dMtrx4D[usNdx];
362 }
363 
364 } // namespace Base
365 
Base::Matrix4D::operator==
bool operator==(const Matrix4D &rclMtrx) const
Comparison.
Definition: Matrix.h:348
Base::Vector3::y
_Precision y
Definition: Vector3D.h:86
Base::transform
void transform(Container container, OutputIt out, BinaryFunction function)
Definition: Algorithms.h:30
Base::Vector3::z
_Precision z
Definition: Vector3D.h:87
Base::Matrix4D::operator-=
Matrix4D & operator-=(const Matrix4D &rclMtrx)
Definition: Matrix.h:235
Base::Matrix4D::operator[]
double * operator[](unsigned short usNdx)
Index operator.
Definition: Matrix.h:374
Base::Matrix4D::operator+=
Matrix4D & operator+=(const Matrix4D &rclMtrx)
Definition: Matrix.h:208
Base::float_traits< double >::epsilon
static float_type epsilon()
Definition: Vector3D.h:70
Base::operator*=
Vector3f & operator*=(Vector3f &rclVect, const Matrix4D &rclMtrx)
Definition: Matrix.h:367
Base::Vector3::x
_Precision x
Definition: Vector3D.h:85
Base::Matrix4D::operator+
Matrix4D operator+(const Matrix4D &rclMtrx) const
Matrix addition.
Definition: Matrix.h:194
Base::Vector3
Definition: Vector3D.h:77
Base::Matrix4D::operator!=
bool operator!=(const Matrix4D &rclMtrx) const
Comparison.
Definition: Matrix.h:362
Base::Vector3d
Vector3< double > Vector3d
Definition: Vector3D.h:246
Base::Matrix4D::operator*=
Matrix4D & operator*=(const Matrix4D &rclMtrx)
Matrix multiplication.
Definition: Matrix.h:248
Base::operator*
DualNumber operator*(DualNumber a, DualNumber b)
Definition: DualNumber.h:91
Base::operator-
DualNumber operator-(DualNumber a, DualNumber b)
Definition: DualNumber.h:81
Base::Matrix4D::Matrix4D
Matrix4D(void)
Default constructor.
entt::operator!=
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
Base::Vector3::Set
void Set(_Precision fX, _Precision fY, _Precision fZ)
Base::Matrix4D::operator=
Matrix4D & operator=(const Matrix4D &rclMtrx)
Assignment.
Definition: Matrix.h:282
Base::Matrix4D::operator-
Matrix4D operator-(const Matrix4D &rclMtrx) const
Matrix subtraction.
Definition: Matrix.h:221
Vector3D.h
Base::operator+
DualNumber operator+(DualNumber a, DualNumber b)
Definition: DualNumber.h:71
Base::Matrix4D
Definition: Matrix.h:55
Base::Matrix4D::operator*
Matrix4D operator*(const Matrix4D &rclMtrx) const
Matrix multiplication.
Definition: Matrix.h:266
Base::Matrix4D::multVec
void multVec(const Vector3d &src, Vector3d &dst) const
Definition: Matrix.h:320
Base::Vector3f
Vector3< float > Vector3f
Definition: Vector3D.h:245
Base
Definition: AbstractXMLReader.h:5