|
#include <gp_Pnt.hxx>
#include <gp_Dir.hxx>
#include <gp_Ax2.hxx>
#include <Standard.hxx>
#include <iostream>
struct SafeAx2Result {
gp_Ax2 axis;
bool vxAdjusted = false; // 是否自动修正了 VX
};
// 判断两个方向是否平行(带容差)
bool AreDirsParallel(const gp_Dir& d1, const gp_Dir& d2, double angularTol = 1e-6) {
return d1.IsParallel(d2, angularTol);
}
// 选择一个与 Z 方向不平行的备用 VX 方向
gp_Dir GetFallbackVx(const gp_Dir& zDir, double angularTol = 1e-6) {
const gp_Dir fallbackX(1, 0, 0);
const gp_Dir fallbackY(0, 1, 0);
const gp_Dir fallbackZ(0, 0, 1); // 备用备用…
if (!AreDirsParallel(zDir, fallbackX, angularTol)) return fallbackX;
if (!AreDirsParallel(zDir, fallbackY, angularTol)) return fallbackY;
std::cerr << "[Error] Z direction is parallel to fallback directions. Using Z default!" << std::endl;
return fallbackZ; // 实在不行返回 Z 本身(不建议)
}
// 主函数:安全创建 gp_Ax2
SafeAx2Result MakeSafeAx2(const gp_Pnt& origin, const gp_Dir& zDir, const gp_Dir& vxHint, double angularTol = 1e-6)
{
gp_Dir finalVx = vxHint;
bool adjusted = false;
if (AreDirsParallel(zDir, vxHint, angularTol))
{
adjusted = true;
finalVx = GetFallbackVx(zDir, angularTol);
std::cerr << "[Warning] Vx is parallel to Z. Adjusted Vx to fallback direction." << std::endl;
}
SafeAx2Result result;
result.axis = gp_Ax2(origin, zDir, finalVx);
result.vxAdjusted = adjusted;
return result;
}
|
|