TrueReality  v0.1.1912
Matrixf.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/Matrixf>
26 #include <osg/Matrixd>
27 
28 #include <trBase/Matrixd.h>
29 #include <trBase/Quat.h>
30 
31 #include <iostream>
32 #include <string>
33 
34 namespace trBase
35 {
36  class Matrixd;
37 
42  {
43  public:
44 
45  using value_type = float;
46 
50  Matrixf();
51 
59  Matrixf(const osg::Matrixf& mat);
60 
68  Matrixf(const osg::Matrixd& mat);
69 
77  Matrixf(const Matrixf& mat);
78 
86  Matrixf(const Matrixd& mat);
87 
95  explicit Matrixf(float const * const ptr);
96 
104  explicit Matrixf(double const * const ptr);
105 
113  explicit Matrixf(const Quat& quat);
114 
138  value_type a10, value_type a11, value_type a12, value_type a13,
139  value_type a20, value_type a21, value_type a22, value_type a23,
140  value_type a30, value_type a31, value_type a32, value_type a33);
141 
147  ~Matrixf();
148 
156  osg::Matrixf& GetOSGMatrix();
157 
165  const osg::Matrixf& GetOSGMatrix() const;
166 
176  int Compare(const Matrixf& m) const;
177 
185  bool Valid() const;
186 
194  bool IsNaN() const;
195 
203  void Set(const Matrixf& rhs);
204 
212  void Set(const Matrixd& rhs);
213 
221  void Set(float const * const ptr);
222 
230  void Set(double const * const ptr);
231 
254  void Set(value_type a00, value_type a01, value_type a02, value_type a03,
255  value_type a10, value_type a11, value_type a12, value_type a13,
256  value_type a20, value_type a21, value_type a22, value_type a23,
257  value_type a30, value_type a31, value_type a32, value_type a33);
258 
266  value_type* Ptr();
267 
275  const value_type* Ptr() const;
276 
282  void MakeIdentity();
283 
291  void MakeScale(const Vec3f& v);
292 
300  void MakeScale(const Vec3d& v);
301 
311  void MakeScale(value_type x, value_type y, value_type z);
312 
320  void MakeTranslate(const Vec3f& v);
321 
329  void MakeTranslate(const Vec3d& v);
330 
340  void MakeTranslate(value_type x, value_type y, value_type z);
341 
350  void MakeRotate(const Vec3f& from, const Vec3f& to);
351 
360  void MakeRotate(const Vec3d& from, const Vec3d& to);
361 
370  void MakeRotate(value_type angle, const Vec3f& axis);
371 
380  void MakeRotate(value_type angle, const Vec3d& axis);
381 
392  void MakeRotate(value_type angle, value_type x, value_type y, value_type z);
393 
401  void MakeRotate(const Quat& q);
402 
415  void MakeRotate(value_type angle1, const Vec3f& axis1, value_type angle2, const Vec3f& axis2, value_type angle3, const Vec3f& axis3);
416 
429  void MakeRotate(value_type angle1, const Vec3d& axis1, value_type angle2, const Vec3d& axis2, value_type angle3, const Vec3d& axis3);
430 
441  void Decompose(Vec3f& translation, Quat& rotation, Vec3f& scale, Quat& so) const;
442 
453  void Decompose(Vec3d& translation, Quat& rotation, Vec3d& scale, Quat& so) const;
454 
467  void MakeOrtho(double left, double right, double bottom, double top, double zNear, double zFar);
468 
481  void MakeOrtho(float left, float right, float bottom, float top, float zNear, float zFar);
482 
498  bool GetOrtho(double& left, double& right, double& bottom, double& top, double& zNear, double& zFar) const;
499 
516  bool GetOrtho(float& left, float& right, float& bottom, float& top, float& zNear, float& zFar) const;
517 
528  void MakeOrtho2D(double left, double right, double bottom, double top);
529 
540  void MakeOrtho2D(float left, float right, float bottom, float top);
541 
554  void MakeFrustum(double left, double right, double bottom, double top, double zNear, double zFar);
555 
568  void MakeFrustum(float left, float right, float bottom, float top, float zNear, float zFar);
569 
585  bool GetFrustum(double& left, double& right, double& bottom, double& top, double& zNear, double& zFar) const;
586 
603  bool GetFrustum(float& left, float& right, float& bottom, float& top, float& zNear, float& zFar) const;
604 
616  void MakePerspective(double fovy, double aspectRatio, double zNear, double zFar);
617 
629  void MakePerspective(float fovy, float aspectRatio, float zNear, float zFar);
630 
645  bool GetPerspective(double& fovy, double& aspectRatio, double& zNear, double& zFar) const;
646 
662  bool GetPerspective(float& fovy, float& aspectRatio, float& zNear, float& zFar) const;
663 
674  void MakeLookAt(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
675 
686  void MakeLookAt(const Vec3d& eye, const Vec3d& center, const Vec3d& up);
687 
699  void GetLookAt(Vec3f& eye, Vec3f& center, Vec3f& up, value_type lookDistance = 1.0f) const;
700 
712  void GetLookAt(Vec3d& eye, Vec3d& center, Vec3d& up, value_type lookDistance = 1.0f) const;
713 
723  bool Invert(const Matrixf& rhs);
724 
734  bool Invert_4x3(const Matrixf& rhs);
735 
745  bool Invert_4x4(const Matrixf& rhs);
746 
754  void OrthoNormalize(const Matrixf& rhs);
755 
763  void SetRotate(const Quat& q);
764 
774  Quat GetRotate() const;
775 
785  void SetTrans(value_type tx, value_type ty, value_type tz);
786 
794  void SetTrans(const Vec3f& v);
795 
803  void SetTrans(const Vec3d& v);
804 
812  Vec3d GetTrans() const;
813 
821  Vec3d GetScale() const;
822 
832  void SetScale(value_type x, value_type y, value_type z);
833 
841  void SetScale(const Vec3f& v);
842 
850  void SetScale(const Vec3d& v);
851 
861  Vec3f PreMult(const Vec3f& v) const;
862 
872  Vec3d PreMult(const Vec3d& v) const;
873 
883  Vec3f PostMult(const Vec3f& v) const;
884 
894  Vec3d PostMult(const Vec3d& v) const;
895 
905  Vec4f PreMult(const Vec4f& v) const;
906 
916  Vec4d PreMult(const Vec4d& v) const;
917 
927  Vec4f PostMult(const Vec4f& v) const;
928 
938  Vec4d PostMult(const Vec4d& v) const;
939 
948  void Mult(const Matrixf& m1, const Matrixf& m2);
949 
957  void PreMult(const Matrixf& m);
958 
966  void PostMult(const Matrixf& m);
967 
975  void PreMultTranslate(const Vec3d& v);
976 
984  void PreMultTranslate(const Vec3f& v);
985 
993  void PostMultTranslate(const Vec3d& v);
994 
1002  void PostMultTranslate(const Vec3f& v);
1003 
1011  void PreMultScale(const Vec3d& v);
1012 
1020  void PreMultScale(const Vec3f& v);
1021 
1029  void PostMultScale(const Vec3d& v);
1030 
1038  void PostMultScale(const Vec3f& v);
1039 
1047  void PreMultRotate(const Quat& q);
1048 
1056  void PostMultRotate(const Quat& q);
1057 
1067  std::string ToString(int precision = -1);
1068 
1076  static Matrixf Identity(void);
1077 
1087  static Matrixf Scale(const Vec3f& sv);
1088 
1098  static Matrixf Scale(const Vec3d& sv);
1099 
1111  static Matrixf Scale(value_type sx, value_type sy, value_type sz);
1112 
1122  static Matrixf Translate(const Vec3f& dv);
1123 
1133  static Matrixf Translate(const Vec3d& dv);
1134 
1146  static Matrixf Translate(value_type x, value_type y, value_type z);
1147 
1158  static Matrixf Rotate(const Vec3f& from, const Vec3f& to);
1159 
1170  static Matrixf Rotate(const Vec3d& from, const Vec3d& to);
1171 
1184  static Matrixf Rotate(value_type angle, value_type x, value_type y, value_type z);
1185 
1196  static Matrixf Rotate(value_type angle, const Vec3f& axis);
1197 
1208  static Matrixf Rotate(value_type angle, const Vec3d& axis);
1209 
1224  static Matrixf Rotate(value_type angle1, const Vec3f& axis1, value_type angle2, const Vec3f& axis2, value_type angle3, const Vec3f& axis3);
1225 
1240  static Matrixf Rotate(value_type angle1, const Vec3d& axis1, value_type angle2, const Vec3d& axis2, value_type angle3, const Vec3d& axis3);
1241 
1251  static Matrixf Rotate(const Quat& quat);
1252 
1262  static Matrixf Inverse(const Matrixf& matrix);
1263 
1273  static Matrixf OrthoNormal(const Matrixf& matrix);
1274 
1289  static Matrixf Ortho(double left, double right, double bottom, double top, double zNear, double zFar);
1290 
1305  static Matrixf Ortho(float left, float right, float bottom, float top, float zNear, float zFar);
1306 
1319  static Matrixf Ortho2D(double left, double right, double bottom, double top);
1320 
1333  static Matrixf Ortho2D(float left, float right, float bottom, float top);
1334 
1349  static Matrixf Frustum(double left, double right, double bottom, double top, double zNear, double zFar);
1350 
1365  static Matrixf Frustum(float left, float right, float bottom, float top, float zNear, float zFar);
1366 
1380  static Matrixf Perspective(float fovy, float aspectRatio, float zNear, float zFar);
1381 
1395  static Matrixf Perspective(double fovy, double aspectRatio, double zNear, double zFar);
1396 
1409  static Matrixf LookAt(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
1410 
1423  static Matrixf LookAt(const Vec3d& eye, const Vec3d& center, const Vec3d& up);
1424 
1435  static Vec3f Transform3x3(const Vec3f& v, const Matrixf& m);
1436 
1447  static Vec3d Transform3x3(const Vec3d& v, const Matrixf& m);
1448 
1459  static Vec3f Transform3x3(const Matrixf& m, const Vec3f& v);
1460 
1471  static Vec3d Transform3x3(const Matrixf& m, const Vec3d& v);
1472 
1482  Matrixf& operator = (const Matrixf& rhs);
1483 
1493  Matrixf& operator = (const Matrixd& rhs);
1494 
1504  bool operator > (const Matrixf& m) const;
1505 
1515  bool operator < (const Matrixf& m) const;
1516 
1526  bool operator == (const Matrixf& m) const;
1527 
1537  bool operator != (const Matrixf& m) const;
1538 
1548  Vec3f operator* (const Vec3f& v) const;
1549 
1559  Vec3d operator* (const Vec3d& v) const;
1560 
1570  Vec4f operator* (const Vec4f& v) const;
1571 
1581  Vec4d operator* (const Vec4d& v) const;
1582 
1590  void operator *= (const Matrixf& other);
1591 
1601  Matrixf operator * (const Matrixf &m) const;
1602 
1612  Matrixf operator * (value_type rhs) const;
1613 
1623  Matrixf& operator *= (value_type rhs);
1624 
1634  Matrixf operator / (value_type rhs) const;
1635 
1645  Matrixf& operator /= (value_type rhs);
1646 
1656  Matrixf operator + (const Matrixf& rhs) const;
1657 
1667  Matrixf& operator += (const Matrixf& rhs);
1668 
1678  Matrixf operator - (const Matrixf& rhs) const;
1679 
1689  Matrixf& operator -= (const Matrixf& rhs);
1690 
1701  value_type& operator ()(int row, int col);
1702 
1713  value_type operator ()(int row, int col) const;
1714 
1722  operator osg::Matrixf () const;
1723 
1731  operator osg::Matrixf& ();
1732 
1740  operator const osg::Matrixf& () const;
1741 
1749  operator osg::Matrixf* ();
1750 
1751  protected:
1752 
1753  osg::Matrixf mMatrix;
1754  };
1755 
1761  TR_BASE_EXPORT std::ostream& operator << (std::ostream& ios, const Matrixf& q);
1762 }
#define TR_BASE_EXPORT
Definition: trBase/Export.h:34
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.
osg::Matrixf mMatrix
Definition: Matrixf.h:1753
A float Matrix class to be used for generic matrix operations.
Definition: Matrixd.h:40
General purpose 3D double Vector.
Definition: Vec3d.h:47
float value_type
Definition: Matrixf.h:45
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
General purpose 3D float Vector.
Definition: Vec3f.h:47