BIMHome v1.0.0
BIMHome接口文档说明
Matrix.h
浏览该文件的文档.
1/************************************************************************
2* @file Matrix.h
3*
4* @brief 矩阵类
5*
6* @details 矩阵类
7*
8* @author dixu
9*
10* @version 1.0
11*
12* @date 2014-9-11
13*
14* @license 北京华科软科技有限公司
15*
16*************************************************************************/
17
18#ifndef BIMHOMEBASE_MATRIX_H
19#define BIMHOMEBASE_MATRIX_H
20
21#include <string>
22
23#include "Vector3D.h"
24#ifndef BH_GLOBAL_H
25#include <BHGlobal.h>
26#endif
27
28namespace Base
29{
39 enum class ScaleType
40 {
41 Other = -1,
42 NoScaling = 0,
45 Uniform = 3
46 };
47
52 class BaseExport Matrix4D
53 {
56 public:
57
63
69 Matrix4D(float a11, float a12, float a13, float a14,
70 float a21, float a22, float a23, float a24,
71 float a31, float a32, float a33, float a34,
72 float a41, float a42, float a43, float a44);
73
79 Matrix4D(double a11, double a12, double a13, double a14,
80 double a21, double a22, double a23, double a24,
81 double a31, double a32, double a33, double a34,
82 double a41, double a42, double a43, double a44);
83
89 Matrix4D(const Matrix4D& rclMtrx);
90
98 Matrix4D(const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
99
107 Matrix4D(const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
108
114 {
115 }
116
117
124 inline void multVec(const Vector3d& src, Vector3d& dst) const;
125
132 inline void multVec(const Vector3f& src, Vector3f& dst) const;
133
140 inline Vector3d getRow(unsigned short usNdx) const;
141
148 inline Vector3d getCol(unsigned short usNdx) const;
149
155 inline Vector3d trace() const;
156
163 inline void setRow(unsigned short usNdx, const Vector3d& v);
164
171 inline void setCol(unsigned short usNdx, const Vector3d& v);
172
178 inline void setTrace(const Vector3d& v);
179
185 double determinant() const;
186
192 double determinant3() const;
193
199 std::string analyse() const;
200
208 Matrix4D& Outer(const Vector3d& rV1, const Vector3d& rV2);
209
217 Matrix4D& Outer(const Vector3f& rV1, const Vector3f& rV2);
218
225 Matrix4D& Hat(const Vector3f& rV);
226
233 Matrix4D& Hat(const Vector3d& rV);
239 void getMatrix(double dMtrx[16]) const;
240
246 void setMatrix(const double dMtrx[16]);
247
253 void getGLMatrix(double dMtrx[16]) const;
254
260 void setGLMatrix(const double dMtrx[16]);
261
267 unsigned long getMemSpace();
268
274
280 bool isUnity() const;
281
286 void nullify();
287
293 bool isNull() const;
294
302 void move(float x, float y, float z)
303 {
304 move(Vector3f(x, y, z));
305 }
306
314 void move(double x, double y, double z)
315 {
316 move(Vector3d(x, y, z));
317 }
318
324 void move(const Vector3f& rclVct);
325
331 void move(const Vector3d& rclVct);
332
340 void scale(float x, float y, float z)
341 {
342 scale(Vector3f(x, y, z));
343 }
344
352 void scale(double x, double y, double z)
353 {
354 scale(Vector3d(x, y, z));
355 }
356
362 void scale(const Vector3f& rclVct);
363
369 void scale(const Vector3d& rclVct);
370
376 void scale(float scalexyz)
377 {
378 scale(Vector3f(scalexyz, scalexyz, scalexyz));
379 }
380
386 void scale(double scalexyz)
387 {
388 scale(Vector3d(scalexyz, scalexyz, scalexyz));
389 }
390
397 ScaleType hasScale(double tol = 0.0) const;
398
404 void rotX(double fAngle);
405
411 void rotY(double fAngle);
412
418 void rotZ(double fAngle);
419
426 void rotLine(const Vector3f& rclVct, float fAngle);
427
434 void rotLine(const Vector3d& rclVct, double fAngle);
435
443 void rotLine(const Vector3f& rclBase, const Vector3f& rclDir, float fAngle);
444
452 void rotLine(const Vector3d& rclBase, const Vector3d& rclDir, double fAngle);
453
463 bool toAxisAngle(Vector3f& rclBase, Vector3f& rclDir, float& fAngle, float& fTranslation) const;
464
474 bool toAxisAngle(Vector3d& rclBase, Vector3d& rclDir, double& fAngle, double& fTranslation) const;
475
482 void transform(const Vector3f& rclVct, const Matrix4D& rclMtrx);
483
490 void transform(const Vector3d& rclVct, const Matrix4D& rclMtrx);
491
496 void inverse();
497
503
509
514 void transpose();
515
520 void Print() const;
521
527 std::string toString() const;
528
534 void fromString(const std::string& str);
535
542 inline Matrix4D operator + (const Matrix4D& rclMtrx) const;
543
544
551 inline Matrix4D& operator += (const Matrix4D& rclMtrx);
552
559 inline Matrix4D operator - (const Matrix4D& rclMtrx) const;
560
567 inline Matrix4D& operator -= (const Matrix4D& rclMtrx);
568
575 inline Matrix4D operator * (const Matrix4D& rclMtrx) const;
576
583 inline Matrix4D& operator *= (const Matrix4D& rclMtrx);
584
591 inline Vector3f operator * (const Vector3f& rclVct) const;
592
599 inline Vector3d operator * (const Vector3d& rclVct) const;
600
607 inline Matrix4D operator * (double scalar) const;
608
615 inline Matrix4D& operator *= (double scalar);
616
623 inline bool operator != (const Matrix4D& rclMtrx) const;
624
631 inline bool operator == (const Matrix4D& rclMtrx) const;
632
639 inline Matrix4D& operator = (const Matrix4D& rclMtrx);
640
647 inline double* operator [] (unsigned short usNdx);
648
655 inline const double* operator[] (unsigned short usNdx) const;
656
657 private:
658 double dMtrx4D[4][4];
659 };
660
661 inline Matrix4D Matrix4D::operator + (const Matrix4D& rclMtrx) const
662 {
663 Matrix4D clMat;
664 unsigned short iz, is;
665
666 for (iz = 0; iz < 4; iz++)
667 {
668 for (is = 0; is < 4; is++)
669 {
670 clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] + rclMtrx[iz][is];
671 }
672 }
673
674 return clMat;
675 }
676
678 {
679 unsigned short iz, is;
680
681 for (iz = 0; iz < 4; iz++)
682 {
683 for (is = 0; is < 4; is++)
684 {
685 dMtrx4D[iz][is] += rclMtrx[iz][is];
686 }
687 }
688
689 return *this;
690 }
691
692 inline Matrix4D Matrix4D::operator - (const Matrix4D& rclMtrx) const
693 {
694 Matrix4D clMat;
695 unsigned short iz, is;
696
697 for (iz = 0; iz < 4; iz++)
698 {
699 for (is = 0; is < 4; is++)
700 {
701 clMat.dMtrx4D[iz][is] = dMtrx4D[iz][is] - rclMtrx[iz][is];
702 }
703 }
704
705 return clMat;
706 }
707
709 {
710 unsigned short iz, is;
711
712 for (iz = 0; iz < 4; iz++)
713 {
714 for (is = 0; is < 4; is++)
715 {
716 dMtrx4D[iz][is] -= rclMtrx[iz][is];
717 }
718 }
719
720 return *this;
721 }
722
724 {
725 Matrix4D clMat;
726 unsigned short ie, iz, is;
727
728 for (iz = 0; iz < 4; iz++)
729 for (is = 0; is < 4; is++)
730 {
731 clMat.dMtrx4D[iz][is] = 0;
732 for (ie = 0; ie < 4; ie++)
733 clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] *
734 rclMtrx.dMtrx4D[ie][is];
735 }
736
737 (*this) = clMat;
738
739 return *this;
740 }
741
742 inline Matrix4D Matrix4D::operator * (const Matrix4D& rclMtrx) const
743 {
744 Matrix4D clMat;
745 unsigned short ie, iz, is;
746
747 for (iz = 0; iz < 4; iz++)
748 for (is = 0; is < 4; is++)
749 {
750 clMat.dMtrx4D[iz][is] = 0;
751 for (ie = 0; ie < 4; ie++)
752 clMat.dMtrx4D[iz][is] += dMtrx4D[iz][ie] *
753 rclMtrx.dMtrx4D[ie][is];
754 }
755
756 return clMat;
757 }
758
760 {
761 unsigned short iz, is;
762
763 for (iz = 0; iz < 4; iz++)
764 {
765 for (is = 0; is < 4; is++)
766 {
767 dMtrx4D[iz][is] = rclMtrx.dMtrx4D[iz][is];
768 }
769 }
770
771 return *this;
772 }
773
774 inline Vector3f Matrix4D::operator * (const Vector3f& rclVct) const
775 {
776 double x = static_cast<double>(rclVct.x);
777 double y = static_cast<double>(rclVct.y);
778 double z = static_cast<double>(rclVct.z);
779 return Vector3f(
780 static_cast<float>(dMtrx4D[0][0] * x + dMtrx4D[0][1] * y +
781 dMtrx4D[0][2] * z + dMtrx4D[0][3]),
782 static_cast<float>(dMtrx4D[1][0] * x + dMtrx4D[1][1] * y +
783 dMtrx4D[1][2] * z + dMtrx4D[1][3]),
784 static_cast<float>(dMtrx4D[2][0] * x + dMtrx4D[2][1] * y +
785 dMtrx4D[2][2] * z + dMtrx4D[2][3])
786 );
787 }
788
789 inline Vector3d Matrix4D::operator * (const Vector3d& rclVct) const
790 {
791 return Vector3d((dMtrx4D[0][0] * rclVct.x + dMtrx4D[0][1] * rclVct.y +
792 dMtrx4D[0][2] * rclVct.z + dMtrx4D[0][3]),
793 (dMtrx4D[1][0] * rclVct.x + dMtrx4D[1][1] * rclVct.y +
794 dMtrx4D[1][2] * rclVct.z + dMtrx4D[1][3]),
795 (dMtrx4D[2][0] * rclVct.x + dMtrx4D[2][1] * rclVct.y +
796 dMtrx4D[2][2] * rclVct.z + dMtrx4D[2][3]));
797 }
798
799 inline void Matrix4D::multVec(const Vector3d& src, Vector3d& dst) const
800 {
801 double x = (dMtrx4D[0][0] * src.x + dMtrx4D[0][1] * src.y +
802 dMtrx4D[0][2] * src.z + dMtrx4D[0][3]);
803 double y = (dMtrx4D[1][0] * src.x + dMtrx4D[1][1] * src.y +
804 dMtrx4D[1][2] * src.z + dMtrx4D[1][3]);
805 double z = (dMtrx4D[2][0] * src.x + dMtrx4D[2][1] * src.y +
806 dMtrx4D[2][2] * src.z + dMtrx4D[2][3]);
807 dst.Set(x, y, z);
808 }
809
810 inline void Matrix4D::multVec(const Vector3f& src, Vector3f& dst) const
811 {
812 double sx = static_cast<double>(src.x);
813 double sy = static_cast<double>(src.y);
814 double sz = static_cast<double>(src.z);
815
816 double x = (dMtrx4D[0][0] * sx + dMtrx4D[0][1] * sy +
817 dMtrx4D[0][2] * sz + dMtrx4D[0][3]);
818 double y = (dMtrx4D[1][0] * sx + dMtrx4D[1][1] * sy +
819 dMtrx4D[1][2] * sz + dMtrx4D[1][3]);
820 double z = (dMtrx4D[2][0] * sx + dMtrx4D[2][1] * sy +
821 dMtrx4D[2][2] * sz + dMtrx4D[2][3]);
822 dst.Set(static_cast<float>(x),
823 static_cast<float>(y),
824 static_cast<float>(z));
825 }
826
827 inline Matrix4D Matrix4D::operator * (double scalar) const
828 {
829 Matrix4D matrix;
830 for (unsigned short i = 0; i < 4; i++)
831 {
832 for (unsigned short j = 0; j < 4; j++)
833 {
834 matrix.dMtrx4D[i][j] = dMtrx4D[i][j] * scalar;
835 }
836 }
837
838 return matrix;
839 }
840
841 inline Matrix4D& Matrix4D::operator *= (double scalar)
842 {
843 for (unsigned short i = 0; i < 4; i++)
844 {
845 for (unsigned short j = 0; j < 4; j++)
846 {
847 dMtrx4D[i][j] *= scalar;
848 }
849 }
850 return *this;
851 }
852
853 inline bool Matrix4D::operator == (const Matrix4D& rclMtrx) const
854 {
855 unsigned short iz, is;
856
857 for (iz = 0; iz < 4; iz++)
858 {
859 for (is = 0; is < 4; is++)
860 {
861 if (fabs(dMtrx4D[iz][is] - rclMtrx.dMtrx4D[iz][is]) > traits_type::epsilon())
862 return false;
863 }
864 }
865
866 return true;
867 }
868
869 inline bool Matrix4D::operator != (const Matrix4D& rclMtrx) const
870 {
871 return !((*this) == rclMtrx);
872 }
873
875 const Matrix4D& rclMtrx)
876 {
877 rclVect = rclMtrx * rclVect;
878 return rclVect;
879 }
880
881 inline double* Matrix4D::operator [] (unsigned short usNdx)
882 {
883 return dMtrx4D[usNdx];
884 }
885
886 inline const double* Matrix4D::operator [] (unsigned short usNdx) const
887 {
888 return dMtrx4D[usNdx];
889 }
890
891 inline Vector3d Matrix4D::getRow(unsigned short usNdx) const
892 {
893 return Vector3d(dMtrx4D[usNdx][0], dMtrx4D[usNdx][1], dMtrx4D[usNdx][2]);
894 }
895
896 inline Vector3d Matrix4D::getCol(unsigned short usNdx) const
897 {
898 return Vector3d(dMtrx4D[0][usNdx], dMtrx4D[1][usNdx], dMtrx4D[2][usNdx]);
899 }
900
902 {
903 return Vector3d(dMtrx4D[0][0], dMtrx4D[1][1], dMtrx4D[2][2]);
904 }
905
906 inline void Matrix4D::setRow(unsigned short usNdx, const Vector3d& v)
907 {
908 dMtrx4D[usNdx][0] = v.x;
909 dMtrx4D[usNdx][1] = v.y;
910 dMtrx4D[usNdx][2] = v.z;
911 }
912
913 inline void Matrix4D::setCol(unsigned short usNdx, const Vector3d& v)
914 {
915 dMtrx4D[0][usNdx] = v.x;
916 dMtrx4D[1][usNdx] = v.y;
917 dMtrx4D[2][usNdx] = v.z;
918 }
919
920 inline void Matrix4D::setTrace(const Vector3d& v)
921 {
922 dMtrx4D[0][0] = v.x;
923 dMtrx4D[1][1] = v.y;
924 dMtrx4D[2][2] = v.z;
925 }
926
927} // namespace Base
928
929
930#endif // BIMHOMEBASE_MATRIX_H
931
932
~Matrix4D()
析构函数
Definition Matrix.h:113
Matrix4D operator-(const Matrix4D &rclMtrx) const
矩阵减法运算符重载
Definition Matrix.h:692
Matrix4D(float a11, float a12, float a13, float a14, float a21, float a22, float a23, float a24, float a31, float a32, float a33, float a34, float a41, float a42, float a43, float a44)
构造函数,创建一个 4x4 矩阵
unsigned long getMemSpace()
获取矩阵占用的内存空间大小
double * operator[](unsigned short usNdx)
提供对矩阵行的直接访问
Definition Matrix.h:881
Vector3d trace() const
计算矩阵主对角线上的元素
Definition Matrix.h:901
std::string toString() const
将矩阵转换为字符串
float_traits< double > traits_type
定义了矩阵元素的数值类型特性
Definition Matrix.h:54
void scale(float scalexyz)
均匀缩放矩阵
Definition Matrix.h:376
void inverse()
求取矩阵的逆矩阵
void scale(const Vector3f &rclVct)
缩放矩阵
bool operator!=(const Matrix4D &rclMtrx) const
矩阵不等运算符重载
Definition Matrix.h:869
void multVec(const Vector3d &src, Vector3d &dst) const
将矩阵与一个三维向量相乘
Definition Matrix.h:799
void rotLine(const Vector3d &rclVct, double fAngle)
绕任意轴旋转
void move(float x, float y, float z)
沿 x、y、z 轴移动矩阵
Definition Matrix.h:302
Matrix4D operator*(const Matrix4D &rclMtrx) const
矩阵乘法运算符重载
Definition Matrix.h:742
Matrix4D & operator-=(const Matrix4D &rclMtrx)
矩阵减法赋值运算符重载
Definition Matrix.h:708
bool toAxisAngle(Vector3d &rclBase, Vector3d &rclDir, double &fAngle, double &fTranslation) const
将矩阵转换为轴角表示
void move(const Vector3f &rclVct)
沿指定向量移动矩阵
double determinant() const
计算矩阵的行列式
void nullify()
将矩阵设置为零矩阵
void getMatrix(double dMtrx[16]) const
将矩阵数据复制到一维数组中
void transform(const Vector3d &rclVct, const Matrix4D &rclMtrx)
围绕一个点进行变换
Matrix4D & Outer(const Vector3d &rV1, const Vector3d &rV2)
创建一个由两个三维向量的外积构成的矩阵
std::string analyse() const
分析矩阵的类型和特性
void rotLine(const Vector3f &rclBase, const Vector3f &rclDir, float fAngle)
绕任意轴旋转
Matrix4D()
默认构造函数,初始化为单位矩阵
Matrix4D & Hat(const Vector3f &rV)
将一个三维向量转换为斜对称矩阵
Matrix4D & operator*=(const Matrix4D &rclMtrx)
矩阵乘法赋值运算符重载
Definition Matrix.h:723
Matrix4D & operator=(const Matrix4D &rclMtrx)
矩阵赋值运算符重载
Definition Matrix.h:759
void setGLMatrix(const double dMtrx[16])
设置 OpenGL 风格的矩阵
void scale(const Vector3d &rclVct)
缩放矩阵
void setRow(unsigned short usNdx, const Vector3d &v)
设置矩阵的指定行
Definition Matrix.h:906
Matrix4D & Outer(const Vector3f &rV1, const Vector3f &rV2)
创建一个由两个三维向量的外积构成的矩阵
Matrix4D(const Matrix4D &rclMtrx)
复制构造函数
bool toAxisAngle(Vector3f &rclBase, Vector3f &rclDir, float &fAngle, float &fTranslation) const
将矩阵转换为轴角表示
bool isUnity() const
检查矩阵是否为单位矩阵
Matrix4D(const Vector3d &rclBase, const Vector3d &rclDir, double fAngle)
构造函数,创建一个围绕特定轴旋转特定角度的变换矩阵
double determinant3() const
计算 3x3 子矩阵的行列式
void setCol(unsigned short usNdx, const Vector3d &v)
设置矩阵的指定列
Definition Matrix.h:913
void move(const Vector3d &rclVct)
沿指定向量移动矩阵
void setMatrix(const double dMtrx[16])
将一维数组中的矩阵数据复制到矩阵对象中
void inverseGauss()
通过高斯-约当消元法计算矩阵的逆
bool isNull() const
检查矩阵是否为零矩阵
void scale(double x, double y, double z)
缩放矩阵
Definition Matrix.h:352
bool operator==(const Matrix4D &rclMtrx) const
矩阵等运算符重载
Definition Matrix.h:853
void inverseOrthogonal()
求取正交变换矩阵的逆矩阵
void getGLMatrix(double dMtrx[16]) const
获取 OpenGL 风格的矩阵
ScaleType hasScale(double tol=0.0) const
检查矩阵是否包含缩放成分
Vector3d getRow(unsigned short usNdx) const
获取矩阵的指定行
Definition Matrix.h:891
void transform(const Vector3f &rclVct, const Matrix4D &rclMtrx)
围绕一个点进行变换
void fromString(const std::string &str)
从字符串中读取矩阵
Matrix4D operator+(const Matrix4D &rclMtrx) const
矩阵加法运算符重载
Definition Matrix.h:661
void scale(float x, float y, float z)
缩放矩阵
Definition Matrix.h:340
void setToUnity()
设置矩阵为单位矩阵
void Print() const
打印矩阵的每一行
void rotY(double fAngle)
绕 Y 轴旋转
Matrix4D(double a11, double a12, double a13, double a14, double a21, double a22, double a23, double a24, double a31, double a32, double a33, double a34, double a41, double a42, double a43, double a44)
构造函数,创建一个 4x4 矩阵
void rotZ(double fAngle)
绕 Z 轴旋转
void scale(double scalexyz)
均匀缩放矩阵
Definition Matrix.h:386
void rotLine(const Vector3f &rclVct, float fAngle)
绕任意轴旋转
Matrix4D & Hat(const Vector3d &rV)
将一个三维向量 rV 转换成一个 4x4 旋转矩阵的斜对称矩阵
void move(double x, double y, double z)
沿 x、y、z 轴移动矩阵
Definition Matrix.h:314
double dMtrx4D[4][4]
矩阵数据
Definition Matrix.h:658
void rotLine(const Vector3d &rclBase, const Vector3d &rclDir, double fAngle)
绕任意轴旋转
void setTrace(const Vector3d &v)
设置矩阵的主对角线元素
Definition Matrix.h:920
Matrix4D & operator+=(const Matrix4D &rclMtrx)
矩阵加法赋值运算符重载
Definition Matrix.h:677
void transpose()
求取矩阵的转置
Vector3d getCol(unsigned short usNdx) const
获取矩阵的指定列
Definition Matrix.h:896
void rotX(double fAngle)
绕 X 轴旋转
Matrix4D(const Vector3f &rclBase, const Vector3f &rclDir, float fAngle)
构造函数,创建一个围绕特定轴旋转特定角度的变换矩阵
Matrix4D 类,表示一个 4x4 矩阵
Definition Matrix.h:53
void Set(_Precision fX, _Precision fY, _Precision fZ)
设置向量的 x, y, 和 z 分量为给定的值 fX, fY, 和 fZ
_Precision x
x-coordinate
Definition Vector3D.h:714
_Precision z
z-coordinate
Definition Vector3D.h:716
_Precision y
y-coordinate
Definition Vector3D.h:715
Vector3 类,表示三维空间中的向量
Definition Vector3D.h:149
Vector3< double > Vector3d
定义一个基于 double 的三维向量类型
Definition Vector3D.h:909
Vector3f & operator*=(Vector3f &rclVect, const Matrix4D &rclMtrx)
Definition Matrix.h:874
ScaleType
ScaleType 用于区分矩阵是否包含缩放成分,以及缩放的类型
Definition Matrix.h:40
Vector3< float > Vector3f
定义一个基于 float 的三维向量类型
Definition Vector3D.h:904
Definition BaseFigureFactory.h:24
static float_type epsilon()
提供双精度浮点数精度的最小值
Definition Vector3D.h:127
提供浮点数类型的特性
Definition Vector3D.h:60