BIMHome v1.0.0
BIMHome接口文档说明
Vector3D.h
浏览该文件的文档.
1/************************************************************************
2* @file Vector3D.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_VECTOR3D_H
19#define BIMHOMEBASE_VECTOR3D_H
20
21#include <cmath>
22#include <cfloat>
23#include <stdexcept>
24#ifndef F_PI
25# define F_PI 3.1415926f
26#endif
27
28#ifndef D_PI
29# define D_PI 3.141592653589793
30#endif
31
32#ifndef FLOAT_MAX
33# define FLOAT_MAX 3.402823466E+38F
34#endif
35
36#ifndef FLOAT_MIN
37# define FLOAT_MIN 1.175494351E-38F
38#endif
39
40#ifndef DOUBLE_MAX
41# define DOUBLE_MAX 1.7976931348623157E+308
42#endif
43
44#ifndef DOUBLE_MIN
45# define DOUBLE_MIN 2.2250738585072014E-308
46#endif
47
48
49namespace Base
50{
51
52
58 template <class numT>
60 {
61 };
62
67 template <>
68 struct float_traits<float>
69 {
70 typedef float float_type;
77 static inline float_type pi()
78 {
79 return F_PI;
80 }
81
87 static inline float_type epsilon()
88 {
89 return FLT_EPSILON;
90 }
91
97 static inline float_type maximum()
98 {
99 return FLT_MAX;
100 }
101 };
102
107 template <>
108 struct float_traits<double>
109 {
110 typedef double float_type;
117 static inline float_type pi()
118 {
119 return D_PI;
120 }
121
127 static inline float_type epsilon()
128 {
129 return DBL_EPSILON;
130 }
131
137 static inline float_type maximum()
138 {
139 return DBL_MAX;
140 }
141 };
147 template <class _Precision>
149 {
150 public:
151
152 typedef _Precision num_type;
154
162 explicit Vector3(_Precision fx = 0.0, _Precision fy = 0.0, _Precision fz = 0.0);
163
169 Vector3(const Vector3<_Precision>& v) = default;
170
177
178 ~Vector3() = default;
179
185 void SetX(_Precision fX);
186
192 void SetY(_Precision fY);
193
199 void SetZ(_Precision fZ);
200
206 _Precision magnitude2D() const;
207
215 _Precision getDotProduct(const Vector3<_Precision>& v1, const Vector3<_Precision>& v2) const;
216
222 _Precision angle() const;
223
231 static Vector3 minimum(const Vector3& v1, const Vector3& v2);
232
240 static Vector3 maximum(const Vector3& v1, const Vector3& v2);
241
250 int IsOnLineSide(const Vector3<_Precision>& p1, const Vector3<_Precision>& p2, const Vector3<_Precision>& pot) const;
251
261
273
279 static inline num_type epsilon()
280 {
281 return traits_type::epsilon();
282 }
283
290 _Precision Dot(const Vector3<_Precision>& rcVct) const;
291
298 Vector3 Cross(const Vector3<_Precision>& rcVct) const;
299
306
314 bool IsOnLineSegment(const Vector3<_Precision>& startVct, const Vector3<_Precision>& endVct) const;
315
321 void ScaleX(_Precision f);
322
328 void ScaleY(_Precision f);
329
335 void ScaleZ(_Precision f);
336
344 void Scale(_Precision fX, _Precision fY, _Precision fZ);
345
351 void MoveX(_Precision f);
352
358 void MoveY(_Precision f);
359
365 void MoveZ(_Precision f);
366
374 void Move(_Precision fX, _Precision fY, _Precision fZ);
375
381 void RotateX(_Precision f);
382
388 void RotateY(_Precision f);
389
395 void RotateZ(_Precision f);
396
404 void Set(_Precision fX, _Precision fY, _Precision fZ);
405
411 _Precision Length() const;
412
418 _Precision Sqr() const;
419
426
432 bool IsNull() const;
433
440 _Precision GetAngle(const Vector3& rcVect) const;
441
449 void TransformToCoordinateSystem(const Vector3& rclBase, const Vector3& rclDirX, const Vector3& rclDirY);
450
458 bool IsEqual(const Vector3& rclPnt, _Precision tol) const;
459
467 Vector3& ProjectToPlane(const Vector3& rclBase, const Vector3& rclNorm);
468
476 void ProjectToPlane(const Vector3& rclBase, const Vector3& rclNorm, Vector3& rclProj) const;
477
485 Vector3& ProjectToLine(const Vector3& rclPoint, const Vector3& rclLine);
486
494 Vector3 Perpendicular(const Vector3& rclBase, const Vector3& rclDir) const;
495
503 _Precision DistanceToPlane(const Vector3& rclBase, const Vector3& rclNorm) const;
504
512 _Precision DistanceToLine(const Vector3& rclBase, const Vector3& rclDirect) const;
513
521 Vector3 DistanceToLineSegment(const Vector3& rclP1, const Vector3& rclP2) const;
522
530 Vector3 moveAlongLinePt(const Vector3& rclP1, const double& d) const;
531
539 Vector3 findPointOnLine(const Vector3& dir, const double& d) const;
540
546 bool isParallelX() const;
547
553 bool isParallelZ() const;
554
560 bool isParallelY() const;
561
568 _Precision& operator [] (unsigned short usIndex);
569
576 const _Precision& operator [] (unsigned short usIndex) const;
577
585
593
601
608
616
624
631 Vector3 operator * (_Precision fScale) const;
632
639 Vector3 operator / (_Precision fDiv) const;
640
647 Vector3& operator *= (_Precision fScale);
648
655 Vector3& operator /= (_Precision fDiv);
656
664
672
679 _Precision operator * (const Vector3<_Precision>& rcVct) const;
680
688
695 bool operator != (const Vector3<_Precision>& rcVct) const;
696
703 bool operator == (const Vector3<_Precision>& rcVct) const;
704
711 bool operator < (const Vector3<_Precision>& rcVct) const;
712
713 public:
714 _Precision x;
715 _Precision y;
716 _Precision z;
717 };
718
719
720
733 template <class _Precision>
735 {
738 Base::Vector3<_Precision> normAB(-dirAB.y, dirAB.x); // 法向量(右手螺旋规则)
739
740 Base::Vector3<_Precision> dirNew(cos(angle), sin(angle), 0);
741
742 double denominator = dirNew.Dot(normAB);
743 if (std::abs(denominator) < 1e-10) {
744 throw std::invalid_argument("Direction is parallel to AB. No unique solution.");
745 }
746
747 double t = d / denominator;
748 return C + dirNew * t;
749 }
750
761 template <class _Precision>
763 {
765 double abDotAb = AB.Dot(AB);
766
767 if (abDotAb == 0) {
768 throw std::invalid_argument("A and B are the same point. Cannot define a line.");
769 }
770
772 double t = AC.Dot(AB) / abDotAb;
773
774 return A + AB * t;
775 }
776
785 template <class _Precision>
787 {
788 double k = d / dir.Length();
790 point.x = this->x + k * dir.x;
791 point.y = this->y + k * dir.y;
792 point.z = this->z + k * dir.z;
793 return point;
794 }
795
804 template <class _Precision>
806 {
807 double dx = rclP1.x - this->x;
808 double dy = rclP1.y - this->y;
809 double dz = rclP1.z - this->z;
810 double L = std::sqrt(std::pow(rclP1.x - this->x, 2) + std::pow(rclP1.y - this->y, 2) + std::pow(rclP1.z - this->z, 2));
811 double unit_dx = dx / L;
812 double unit_dy = dy / L;
813 double unit_dz = dz / L;
814 double xNew = this->x + unit_dx * d;
815 double yNew = this->y + unit_dy * d;
816 double zNew = this->z + unit_dz * d;
817 return Base::Vector3<_Precision>(xNew, yNew, zNew);
818 }
819
829 template <class _Precision>
831 {
832 int A = p2.y - p1.y;
833 int B = p1.x - p2.x;
834 int C = A * (p1.x) + B * (p1.y);
835 int F = A * (pot.x - p1.x) + B * (pot.y - p1.y);
836
837 if (F > FLT_EPSILON) return 1;
838 if (F < FLT_EPSILON) return -1;
839 return 0; // 在直线上
840 }
841
850 template <class _Precision>
851 inline _Precision Distance(const Vector3<_Precision>& v1, const Vector3<_Precision>& v2)
852 {
853 _Precision x = v1.x - v2.x, y = v1.y - v2.y, z = v1.z - v2.z;
854 return static_cast<_Precision>(sqrt((x * x) + (y * y) + (z * z)));
855 }
856
865 template <class _Precision>
866 inline _Precision DistanceP2(const Vector3<_Precision>& v1, const Vector3<_Precision>& v2)
867 {
868 _Precision x = v1.x - v2.x, y = v1.y - v2.y, z = v1.z - v2.z;
869 return x * x + y * y + z * z;
870 }
871
880 template <class _Precision>
881 inline Vector3<_Precision> operator * (_Precision fFac, const Vector3<_Precision>& rcVct)
882 {
883 return Vector3<_Precision>(rcVct.x * fFac, rcVct.y * fFac, rcVct.z * fFac);
884 }
885
894 template <class _Pr1, class _Pr2>
896 {
897 return Vector3<_Pr1>(static_cast<_Pr1>(v.x), static_cast<_Pr1>(v.y), static_cast<_Pr1>(v.z));
898 }
899
910} // namespace Base
911
912#endif // BIMHOMEBASE_VECTOR3D_H
#define F_PI
Definition BoundBox2D.h:28
#define D_PI
Definition BoundBox2D.h:32
F
Definition ISnapProcessedBase.h:49
A
Definition ISnapProcessedBase.h:49
L
Definition ISnapProcessedBase.h:49
B
Definition ISnapProcessedBase.h:49
C
Definition ISnapProcessedBase.h:49
static Vector3 calFixedAngleAndDistanceAndOnLinePoint(const Vector3< _Precision > &A, const Vector3< _Precision > &B, const Vector3< _Precision > &C, double angle, double d)
计算AB线上点C,沿着角度angle方向,距离直线AB的距离为d的点,d的正负满足右手螺旋法则
Definition Vector3D.h:734
Vector3 Perpendicular(const Vector3 &rclBase, const Vector3 &rclDir) const
计算从当前向量到由点 rclBase 和方向向量 rclDir 定义的直线的垂足
_Precision & operator[](unsigned short usIndex)
返回对坐标的引用。
_Precision DistanceToLine(const Vector3 &rclBase, const Vector3 &rclDirect) const
计算当前向量到一条直线的距离
static Vector3 minimum(const Vector3 &v1, const Vector3 &v2)
返回两个三维向量(v1 和 v2)中对应分量的最小值组成的一个新向量
Vector3 & operator/=(_Precision fDiv)
实现向量与标量的除法赋值操作
bool isParallelZ() const
判断当前向量是否平行于世界坐标系的Z轴(0,0,1)
void MoveX(_Precision f)
将向量的 x 分量按给定的值 f 进行平移
Vector3 Cross(const Vector3< _Precision > &rcVct) const
实现向量的叉乘
_Precision GetAngle(const Vector3 &rcVect) const
计算当前向量与另一个向量之间的夹角
Vector3 & operator=(const Vector3< _Precision > &v)=default
实现向量之间的赋值操作
Vector3 & operator*=(_Precision fScale)
实现向量与标量的乘法赋值操作
void SetY(_Precision fY)
设置 Vector3 类中 y 成员变量的值
void Set(_Precision fX, _Precision fY, _Precision fZ)
设置向量的 x, y, 和 z 分量为给定的值 fX, fY, 和 fZ
static num_type epsilon()
获取数值的精度(epsilon)
Definition Vector3D.h:279
static Vector3 maximum(const Vector3 &v1, const Vector3 &v2)
返回两个三维向量(v1 和 v2)中对应分量的最大值组成的一个新向量
Vector3 operator+(const Vector3< _Precision > &rcVct) const
实现两个三维向量的相加
_Precision angle() const
计算当前向量与 X 轴正方向之间的夹角,角度范围为 [0, 2π)
bool operator<(const Vector3< _Precision > &rcVct) const
实现了向量之间的“小于”比较
Vector3 absoluteValue() const
取绝对值
Vector3(_Precision fx=0.0, _Precision fy=0.0, _Precision fz=0.0)
初始化向量的三个分量 x、y 和 z
Vector3 & operator-=(const Vector3< _Precision > &rcVct)
实现向量的减法赋值操作
void RotateX(_Precision f)
将向量绕 x 轴旋转给定的角度 f
void ScaleX(_Precision f)
将向量的 x 分量按给定的比例因子 f 进行缩放
void TransformToCoordinateSystem(const Vector3 &rclBase, const Vector3 &rclDirX, const Vector3 &rclDirY)
将当前向量从一个坐标系转换到另一个坐标系
_Precision x
x-coordinate
Definition Vector3D.h:714
Vector3 findPointOnLine(const Vector3 &dir, const double &d) const
计算当前向量在指定方向上移动指定距离后的点
Definition Vector3D.h:786
Vector3 DistanceToLineSegment(const Vector3 &rclP1, const Vector3 &rclP2) const
计算当前向量到线段的距离
void RotateY(_Precision f)
将向量绕 y 轴旋转给定的角度 f
void MoveY(_Precision f)
将向量的 y 分量按给定的值 f 进行平移
void SetZ(_Precision fZ)
设置 Vector3 类中 z 成员变量的值
Vector3 operator%(const Vector3< _Precision > &rcVct) const
用于实现两个向量的叉积
bool operator!=(const Vector3< _Precision > &rcVct) const
实现了向量的不等于运算符
_Precision getDotProduct(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2) const
计算并返回两个三维向量 v1 和 v2 的点积
bool IsEqual(const Vector3 &rclPnt, _Precision tol) const
检查当前向量与另一个向量 rclPnt 是否在给定的容差 tol 范围内相等
_Precision z
z-coordinate
Definition Vector3D.h:716
void Scale(_Precision fX, _Precision fY, _Precision fZ)
将向量的每个分量(x, y, z)分别按给定的比例因子 fX, fY, fZ 进行缩放
Vector3 & ProjectToPlane(const Vector3 &rclBase, const Vector3 &rclNorm)
将当前向量投影到一个平面上
Vector3 & operator+=(const Vector3< _Precision > &rcVct)
实现向量的加法赋值操作
int IsOnLineSide(const Vector3< _Precision > &p1, const Vector3< _Precision > &p2, const Vector3< _Precision > &pot) const
判断一个点(pot)是否在由两个点(p1 和 p2)定义的直线的某一侧,或者是否在这条直线上
Definition Vector3D.h:830
void MoveZ(_Precision f)
将向量的 z 分量按给定的值 f 进行平移
bool isParallelY() const
判断当前向量是否平行于世界坐标系的Y轴(0,1,0)
void ProjectToPlane(const Vector3 &rclBase, const Vector3 &rclNorm, Vector3 &rclProj) const
将当前向量投影到一个平面上,并将结果存储在另一个向量 rclProj 中
void SetX(_Precision fX)
设置 Vector3 类中 x 成员变量的值
Vector3(Vector3< _Precision > &&v)=default
移动构造函数
Vector3 operator*(_Precision fScale) const
实现向量与标量的乘法操作
Vector3 operator-() const
取向量的相反数
void Move(_Precision fX, _Precision fY, _Precision fZ)
将向量的每个分量(x, y, z)分量按给定的值 fX, fY, fZ 进行平移
_Precision Sqr() const
计算向量长度的平方
_Precision num_type
向量分量的数值类型
Definition Vector3D.h:152
void ScaleY(_Precision f)
将向量的 y 分量按给定的比例因子 f 进行缩放
~Vector3()=default
_Precision DistanceToPlane(const Vector3 &rclBase, const Vector3 &rclNorm) const
计算当前向量到一个平面的距离
Vector3 operator/(_Precision fDiv) const
实现向量与标量的除法操作
bool IsNull() const
检查向量是否为零向量
Vector3(const Vector3< _Precision > &v)=default
拷贝构造函数
bool operator==(const Vector3< _Precision > &rcVct) const
比较两个向量是否相等
float_traits< num_type > traits_type
向量分量数值类型的特质
Definition Vector3D.h:153
static Vector3 calFootOfPerpendicular(const Vector3< _Precision > &A, const Vector3< _Precision > &B, const Vector3< _Precision > &C)
计算过点 C 与 AB 垂直的线的垂足
Definition Vector3D.h:762
void RotateZ(_Precision f)
将向量绕 z 轴旋转给定的角度 f
Vector3 operator&(const Vector3< _Precision > &rcVct) const
实现两个三维向量的叉乘
Vector3 moveAlongLinePt(const Vector3 &rclP1, const double &d) const
计算沿着当前点到rclP1的方向移动距离d的点
Definition Vector3D.h:805
Vector3 & ProjectToLine(const Vector3 &rclPoint, const Vector3 &rclLine)
将当前向量投影到一条由点 rclPoint 和方向向量 rclLine 定义的直线上
void ScaleZ(_Precision f)
将向量的 z 分量按给定的比例因子 f 进行缩放
bool isParallelX() const
判断当前向量是否平行于世界坐标系的X轴(1,0,0)
bool IsOnLineSegment(const Vector3< _Precision > &startVct, const Vector3< _Precision > &endVct) const
判断当前向量(表示一个点)是否位于由 startVct 和 endVct 定义的线段上
_Precision Length() const
计算向量的长度(也称为模或范数)
Vector3 & Normalize()
将向量归一化,即将其长度缩放到 1
_Precision Dot(const Vector3< _Precision > &rcVct) const
实现向量与标量的乘法操作
_Precision magnitude2D() const
计算三维向量的二维幅度
_Precision y
y-coordinate
Definition Vector3D.h:715
Vector3 类,表示三维空间中的向量
Definition Vector3D.h:149
Vector3< double > Vector3d
定义一个基于 double 的三维向量类型
Definition Vector3D.h:909
_Precision Distance(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2)
计算三维空间中两个点之间的欧几里得距离
Definition Vector3D.h:851
DualNumber operator*(DualNumber a, DualNumber b)
将两个对偶数相乘
Definition DualNumber.h:188
Vector3< _Pr1 > toVector(const Vector3< _Pr2 > &v)
将一个类型为 _Pr2 的 Vector3 对象转换为另一个类型为 _Pr1 的 Vector3 对象
Definition Vector3D.h:895
Vector3< float > Vector3f
定义一个基于 float 的三维向量类型
Definition Vector3D.h:904
_Precision DistanceP2(const Vector3< _Precision > &v1, const Vector3< _Precision > &v2)
计算两个向量之间的欧几里得距离的平方
Definition Vector3D.h:866
Definition BaseFigureFactory.h:24
static float_type pi()
提供 π(圆周率)的值
Definition Vector3D.h:117
static float_type epsilon()
提供双精度浮点数精度的最小值
Definition Vector3D.h:127
double float_type
浮点数类型
Definition Vector3D.h:110
static float_type maximum()
提供双精度浮点数的最大值
Definition Vector3D.h:137
static float_type pi()
提供 π(圆周率)的值
Definition Vector3D.h:77
static float_type epsilon()
提供浮点数精度的最小值
Definition Vector3D.h:87
float float_type
浮点数类型
Definition Vector3D.h:70
static float_type maximum()
提供单精度浮点数的最大值
Definition Vector3D.h:97
提供浮点数类型的特性
Definition Vector3D.h:60