You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AppFlow/FITK_Kernel/FITKCore/FITKVec3DAlg.cpp

108 lines
3.6 KiB
C++

#include "FITKVec3DAlg.h"
#include <cmath>
namespace Core
{
FITKPoint FITKCoreAPI Add(const FITKPoint& a, const FITKPoint& b, const double aFac, const double bFac)
{
double c[3] = { 0,0,0 };
c[1] = a.y()*aFac + b.y()*bFac;
c[0] = a.x()*aFac + b.x()*bFac;
c[2] = a.z()*aFac + b.z()*bFac;
// 创建并返回新的FITKPoint对象存储结果
return FITKPoint(c);
}
FITKPoint FITKCoreAPI Subtract(const FITKPoint& a, const FITKPoint& b, const double aFac, const double bFac )
{
double c[3] = { 0,0,0 };
c[0] = a.x()*aFac - b.x()*bFac;
c[1] = a.y()*aFac - b.y()*bFac;
c[2] = a.z()*aFac - b.z()*bFac;
// 创建并返回新的FITKPoint对象存储结果
return FITKPoint(c);
}
/**
* FITKPointFITKPoint
*
* @param a FITKPoint
* @param b FITKPoint
* @return FITKPoint
*/
FITKPoint CrossProduct(const FITKPoint& a, const FITKPoint& b)
{
double c[3] = { 0,0,0 };
// 根据叉积定义计算各个分量
c[0] = a.y() * b.z() - a.z() * b.y();
c[1] = a.z() * b.x() - a.x() * b.z();
c[2] = a.x() * b.y() - a.y() * b.x();
// 创建并返回新的FITKPoint对象存储叉积结果
return FITKPoint(c);
}
/**
* FITKPoint
*
* @param a FITKPoint
* @param b FITKPoint
* @return double
*/
double DotProduct(const FITKPoint & a, const FITKPoint & b)
{
// 根据点积定义计算标量结果
return a.x() * b.x() + a.y() * b.y() + a.z() * b.z();
}
/**
* P线AxisAngleFITKPoint
*
* @param P FITKPoint
* @param Axis 线
* @param Angle double
* @return FITKPoint
*/
FITKPoint RotateAroundAxis(const FITKPoint& P, FITKVec3D& Axis, double Angle)
{
Axis.normalize();
double cosTheta = std::cos(Angle);
double sinTheta = std::sin(Angle);
double kP = DotProduct(Axis, P); // 计算点P在旋转轴上的投影长度
FITKPoint kCrossP = CrossProduct(Axis, P); // 计算点P相对于旋转轴的垂直向量
// 应用罗德里格斯公式计算旋转后的点坐标
FITKPoint pt(
P.x() * cosTheta + kCrossP.x() * sinTheta + Axis.x() * kP * (1.0 - cosTheta),
P.y() * cosTheta + kCrossP.y() * sinTheta + Axis.y() * kP * (1.0 - cosTheta),
P.z() * cosTheta + kCrossP.z() * sinTheta + Axis.z() * kP * (1.0 - cosTheta)
);
return pt; // 返回旋转后的FITKPoint对象
}
double FITKCoreAPI Distance(const FITKPoint& a, const FITKPoint& b)
{
double dx = a.x() - b.x();
double dy = a.y() - b.y();
double dz = a.z() - b.z();
return sqrt(dx*dx + dy * dy + dz * dz);
}
double FITKCoreAPI AngleBetweenVec(const FITKVec3D& v1, const FITKVec3D& v2)
{
//点乘
double dp = DotProduct(v1, v2);
//角度余弦值
double arcAng = dp / (v1.getNorm()*v2.getNorm());
return std::acos(arcAng);
}
}