//定义一个线的结构体
struct LineData
{
Point3d start;
Point3d end;
tag_t curve_tag;
// 有参数构造函数
LineData(const Point3d& startPoint, const Point3d& endPoint, const tag_t& lineTag)
: start(startPoint), end(endPoint), curve_tag(lineTag)
{
}
};
//比较两点是否在公差内相等
bool areEqual(double a, double b, double epsilon)
{
return std::abs(a - b) < epsilon;
}
//曲线1和2是否相连
bool areLinesConnected(const LineData& line1, const LineData& line2,double epsilon)
{
if (areEqual(line1.end.X, line2.start.X, epsilon) && areEqual(line1.end.Y, line2.start.Y, epsilon) && areEqual(line1.end.Z, line2.start.Z, epsilon))
{
return true;
}
else if (areEqual(line1.start.X, line2.end.X, epsilon) && areEqual(line1.start.Y, line2.end.Y, epsilon) && areEqual(line1.start.Z, line2.end.Z, epsilon))
{
return true;
}
else
{
return false;
}
}
//查找函数
vector<vector<tag_t>> findConnectedGroups(const vector<LineData>& lines,double epsilon)
{
vector<vector<tag_t>> connectedGroups;
vector<bool> visited(lines.size(), false);
for (size_t i = 0; i < lines.size(); ++i)
{
if (visited[i])
{
continue;
}
vector<tag_t> group;
vector<size_t> stack;
stack.push_back(i);
visited[i] = true;
while (!stack.empty())
{
size_t index = stack.back();
stack.pop_back();
group.push_back(lines[index].curve_tag);
for (size_t j = 0; j < lines.size(); ++j)
{
if (!visited[j] && areLinesConnected(lines[index], lines[j],epsilon))
{
stack.push_back(j);
visited[j] = true;
}
}
}
connectedGroups.push_back(group);
}
return connectedGroups;
}
//获取曲线的端点
LineData getVerOfCurve(tag_t curveTag)
{
double point[3] = { 0 };
double target[3] = { 0 };
double pnorm[3] = { 0 };
double bnorm[3] = { 0 };
double a, b;
double sta = 0;
double end = 1;
double stratP[3] = { 0 };
double endP[3] = { 0 };
UF_MODL_ask_curve_props(curveTag, sta, stratP, target, pnorm, bnorm, &a, &b);
UF_MODL_ask_curve_props(curveTag, end, endP, target, pnorm, bnorm, &a, &b);
return LineData(Point3d(stratP[0], stratP[1], stratP[2]), Point3d(endP[0], endP[1], endP[2]), curveTag);
}
int main()
{
//拿到需要的曲线
vector<tag_t>tempVec = getViewSilhouette(allBody[i]);
for (int j = 0; j < tempVec.size(); j++)
{
//这里是基于NX的获取曲线端点的方法,其他环境下相应更换
LineData data = getVerOfCurve(tempVec[j]);
allCurveVec.push_back(data);
}
vector<vector<tag_t>> curveGroupVec = findConnectedGroups(allCurveVec,0.1);
}
查找相连曲线组,传入线的端点,进行查询,返回相连的曲线组合,不限于NX,更换环境时,可适当更换端点获取的方法
于 2024-01-10 10:15:15 首次发布