TrueReality  v0.1.1912
Matrixd.h
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 #pragma once
22 
23 #include <trBase/Export.h>
24 
25 #include <osg/Matrixd>
26 #include <osg/Matrixf>
27 
28 #include <trBase/Quat.h>
29 
30 #include <iostream>
31 #include <string>
32 
33 namespace trBase
34 {
35  class Matrixf;
36 
41  {
42  public:
43 
44  using value_type = double;
45 
51  Matrixd();
52 
60  Matrixd(const osg::Matrixd& mat);
61 
69  Matrixd(const osg::Matrixf& mat);
70 
78  Matrixd(const Matrixd& mat);
79 
87  Matrixd(const Matrixf& mat);
88 
96  explicit Matrixd(float const * const ptr);
97 
105  explicit Matrixd(double const * const ptr);
106 
114  explicit Matrixd(const Quat& quat);
115 
139  value_type a10, value_type a11, value_type a12, value_type a13,
140  value_type a20, value_type a21, value_type a22, value_type a23,
141  value_type a30, value_type a31, value_type a32, value_type a33);
142 
148  ~Matrixd();
149 
157  osg::Matrixd& GetOSGMatrix();
158 
166  const osg::Matrixd& GetOSGMatrix() const;
167 
177  int Compare(const Matrixd& m) const;
178 
186  bool Valid() const;
187 
195  bool IsNaN() const;
196 
204  void Set(const Matrixd& rhs);
205 
213  void Set(const Matrixf& rhs);
214 
222  void Set(float const * const ptr);
223 
227  void Set(double const * const ptr);
228 
251  void Set(value_type a00, value_type a01, value_type a02, value_type a03,
252  value_type a10, value_type a11, value_type a12, value_type a13,
253  value_type a20, value_type a21, value_type a22, value_type a23,
254  value_type a30, value_type a31, value_type a32, value_type a33);
255 
263  value_type* Ptr();
264 
272  const value_type* Ptr() const;
273 
279  void MakeIdentity();
280 
288  void MakeScale(const Vec3f& v);
289 
297  void MakeScale(const Vec3d& v);
298 
308  void MakeScale(value_type x, value_type y, value_type z);
309 
317  void MakeTranslate(const Vec3f& v);
318 
326  void MakeTranslate(const Vec3d& v);
327 
337  void MakeTranslate(value_type x, value_type y, value_type z);
338 
347  void MakeRotate(const Vec3f& from, const Vec3f& to);
348 
357  void MakeRotate(const Vec3d& from, const Vec3d& to);
358 
367  void MakeRotate(value_type angle, const Vec3f& axis);
368 
377  void MakeRotate(value_type angle, const Vec3d& axis);
378 
389  void MakeRotate(value_type angle, value_type x, value_type y, value_type z);
390 
398  void MakeRotate(const Quat& q);
399 
412  void MakeRotate(value_type angle1, const Vec3f& axis1, value_type angle2, const Vec3f& axis2, value_type angle3, const Vec3f& axis3);
413 
426  void MakeRotate(value_type angle1, const Vec3d& axis1, value_type angle2, const Vec3d& axis2, value_type angle3, const Vec3d& axis3);
427 
438  void Decompose(Vec3f& translation, Quat& rotation, Vec3f& scale, Quat& so) const;
439 
450  void Decompose(Vec3d& translation, Quat& rotation, Vec3d& scale, Quat& so) const;
451 
464  void MakeOrtho(double left, double right, double bottom, double top, double zNear, double zFar);
465 
478  void MakeOrtho(float left, float right, float bottom, float top, float zNear, float zFar);
479 
495  bool GetOrtho(double& left, double& right, double& bottom, double& top, double& zNear, double& zFar) const;
496 
513  bool GetOrtho(float& left, float& right, float& bottom, float& top, float& zNear, float& zFar) const;
514 
525  void MakeOrtho2D(double left, double right, double bottom, double top);
526 
537  void MakeOrtho2D(float left, float right, float bottom, float top);
538 
551  void MakeFrustum(double left, double right, double bottom, double top, double zNear, double zFar);
552 
565  void MakeFrustum(float left, float right, float bottom, float top, float zNear, float zFar);
566 
582  bool GetFrustum(double& left, double& right, double& bottom, double& top, double& zNear, double& zFar) const;
583 
600  bool GetFrustum(float& left, float& right, float& bottom, float& top, float& zNear, float& zFar) const;
601 
613  void MakePerspective(double fovy, double aspectRatio, double zNear, double zFar);
614 
626  void MakePerspective(float fovy, float aspectRatio, float zNear, float zFar);
627 
642  bool GetPerspective(double& fovy, double& aspectRatio, double& zNear, double& zFar) const;
643 
659  bool GetPerspective(float& fovy, float& aspectRatio, float& zNear, float& zFar) const;
660 
671  void MakeLookAt(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
672 
683  void MakeLookAt(const Vec3d& eye, const Vec3d& center, const Vec3d& up);
684 
696  void GetLookAt(Vec3f& eye, Vec3f& center, Vec3f& up, value_type lookDistance = 1.0f) const;
697 
709  void GetLookAt(Vec3d& eye, Vec3d& center, Vec3d& up, value_type lookDistance = 1.0f) const;
710 
720  bool Invert(const Matrixd& rhs);
721 
731  bool Invert_4x3(const Matrixd& rhs);
732 
742  bool Invert_4x4(const Matrixd& rhs);
743 
751  void OrthoNormalize(const Matrixd& rhs);
752 
760  void SetRotate(const Quat& q);
761 
771  Quat GetRotate() const;
772 
782  void SetTrans(value_type tx, value_type ty, value_type tz);
783 
791  void SetTrans(const Vec3f& v);
792 
800  void SetTrans(const Vec3d& v);
801 
809  Vec3d GetTrans() const;
810 
818  Vec3d GetScale() const;
819 
829  void SetScale(value_type x, value_type y, value_type z);
830 
838  void SetScale(const Vec3f& v);
839 
847  void SetScale(const Vec3d& v);
848 
858  Vec3f PreMult(const Vec3f& v) const;
859 
869  Vec3d PreMult(const Vec3d& v) const;
870 
880  Vec3f PostMult(const Vec3f& v) const;
881 
891  Vec3d PostMult(const Vec3d& v) const;
892 
902  Vec4f PreMult(const Vec4f& v) const;
903 
913  Vec4d PreMult(const Vec4d& v) const;
914 
924  Vec4f PostMult(const Vec4f& v) const;
925 
935  Vec4d PostMult(const Vec4d& v) const;
936 
945  void Mult(const Matrixd& m1, const Matrixd& m2);
946 
954  void PreMult(const Matrixd& m);
955 
963  void PostMult(const Matrixd& m);
964 
972  void PreMultTranslate(const Vec3d& v);
973 
981  void PreMultTranslate(const Vec3f& v);
982 
990  void PostMultTranslate(const Vec3d& v);
991 
999  void PostMultTranslate(const Vec3f& v);
1000 
1008  void PreMultScale(const Vec3d& v);
1009 
1017  void PreMultScale(const Vec3f& v);
1018 
1026  void PostMultScale(const Vec3d& v);
1027 
1035  void PostMultScale(const Vec3f& v);
1036 
1044  void PreMultRotate(const Quat& q);
1045 
1053  void PostMultRotate(const Quat& q);
1054 
1064  std::string ToString(int precision = -1);
1065 
1073  static Matrixd Identity(void);
1074 
1084  static Matrixd Scale(const Vec3f& sv);
1085 
1095  static Matrixd Scale(const Vec3d& sv);
1096 
1108  static Matrixd Scale(value_type sx, value_type sy, value_type sz);
1109 
1119  static Matrixd Translate(const Vec3f& dv);
1120 
1130  static Matrixd Translate(const Vec3d& dv);
1131 
1143  static Matrixd Translate(value_type x, value_type y, value_type z);
1144 
1155  static Matrixd Rotate(const Vec3f& from, const Vec3f& to);
1156 
1167  static Matrixd Rotate(const Vec3d& from, const Vec3d& to);
1168 
1181  static Matrixd Rotate(value_type angle, value_type x, value_type y, value_type z);
1182 
1193  static Matrixd Rotate(value_type angle, const Vec3f& axis);
1194 
1205  static Matrixd Rotate(value_type angle, const Vec3d& axis);
1206 
1221  static Matrixd Rotate(value_type angle1, const Vec3f& axis1, value_type angle2, const Vec3f& axis2, value_type angle3, const Vec3f& axis3);
1222 
1237  static Matrixd Rotate(value_type angle1, const Vec3d& axis1, value_type angle2, const Vec3d& axis2, value_type angle3, const Vec3d& axis3);
1238 
1248  static Matrixd Rotate(const Quat& quat);
1249 
1259  static Matrixd Inverse(const Matrixd& matrix);
1260 
1270  static Matrixd OrthoNormal(const Matrixd& matrix);
1271 
1286  static Matrixd Ortho(double left, double right, double bottom, double top, double zNear, double zFar);
1287 
1302  static Matrixd Ortho(float left, float right, float bottom, float top, float zNear, float zFar);
1303 
1316  static Matrixd Ortho2D(double left, double right, double bottom, double top);
1317 
1330  static Matrixd Ortho2D(float left, float right, float bottom, float top);
1331 
1346  static Matrixd Frustum(double left, double right, double bottom, double top, double zNear, double zFar);
1347 
1362  static Matrixd Frustum(float left, float right, float bottom, float top, float zNear, float zFar);
1363 
1377  static Matrixd Perspective(float fovy, float aspectRatio, float zNear, float zFar);
1378 
1392  static Matrixd Perspective(double fovy, double aspectRatio, double zNear, double zFar);
1393 
1406  static Matrixd LookAt(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
1407 
1420  static Matrixd LookAt(const Vec3d& eye, const Vec3d& center, const Vec3d& up);
1421 
1432  static Vec3f Transform3x3(const Vec3f& v, const Matrixd& m);
1433 
1444  static Vec3d Transform3x3(const Vec3d& v, const Matrixd& m);
1445 
1456  static Vec3f Transform3x3(const Matrixd& m, const Vec3f& v);
1457 
1468  static Vec3d Transform3x3(const Matrixd& m, const Vec3d& v);
1469 
1479  Matrixd& operator = (const Matrixd& rhs);
1480 
1490  Matrixd& operator = (const Matrixf& rhs);
1491 
1501  bool operator > (const Matrixd& m) const;
1502 
1512  bool operator < (const Matrixd& m) const;
1513 
1523  bool operator == (const Matrixd& m) const;
1524 
1534  bool operator != (const Matrixd& m) const;
1535 
1545  Vec3f operator* (const Vec3f& v) const;
1546 
1556  Vec3d operator* (const Vec3d& v) const;
1557 
1567  Vec4f operator* (const Vec4f& v) const;
1568 
1578  Vec4d operator* (const Vec4d& v) const;
1579 
1587  void operator *= (const Matrixd& other);
1588 
1598  Matrixd operator * (const Matrixd &m) const;
1599 
1609  Matrixd operator * (value_type rhs) const;
1610 
1620  Matrixd& operator *= (value_type rhs);
1621 
1631  Matrixd operator / (value_type rhs) const;
1632 
1642  Matrixd& operator /= (value_type rhs);
1643 
1653  Matrixd operator + (const Matrixd& rhs) const;
1654 
1664  Matrixd& operator += (const Matrixd& rhs);
1665 
1675  Matrixd operator - (const Matrixd& rhs) const;
1676 
1686  Matrixd& operator -= (const Matrixd& rhs);
1687 
1698  value_type& operator ()(int row, int col);
1699 
1710  value_type operator ()(int row, int col) const;
1711 
1719  operator osg::Matrixd () const;
1720 
1728  operator osg::Matrixd& ();
1729 
1737  operator const osg::Matrixd& () const;
1738 
1746  operator osg::Matrixd* ();
1747 
1748  protected:
1749 
1750  osg::Matrixd mMatrix;
1751  };
1752 
1758  TR_BASE_EXPORT std::ostream& operator << (std::ostream& ios, const Matrixd& q);
1759 }
#define TR_BASE_EXPORT
Definition: trBase/Export.h:34
double value_type
Definition: Matrixd.h:44
std::string operator+(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:193
bool operator==(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:187
General purpose 3D float Vector.
Definition: Vec4f.h:47
bool operator!=(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:199
TR_BASE_EXPORT std::ostream & operator<<(std::ostream &ios, const Matrixd &q)
Stream insertion operator.
A float Matrix class to be used for generic matrix operations.
Definition: Matrixd.h:40
General purpose 3D double Vector.
Definition: Vec3d.h:47
General purpose 3D double Vector.
Definition: Vec4d.h:47
std::string ToString(const T &t, int precision=-1)
A utility function to convert a basic type into a string.
Definition: StringUtils.h:334
Represents a quaternion, that is used for angular calculations and transformations.
Definition: Quat.h:44
A float Matrix class to be used for generic matrix operations.
Definition: Matrixf.h:41
osg::Matrixd mMatrix
Definition: Matrixd.h:1750
General purpose 3D float Vector.
Definition: Vec3f.h:47