闭合曲线查找算法

闭合曲线查找算法:传入无序曲线组,将能构成闭环的曲线返回

//定义一个线的结构体
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 IsSamePoint(const Point3d& a, const Point3d& b) 
{
    // 可以根据实际情况调整精度
    const double tolerance = 1e-6;
    return (std::abs(a.X - b.X) < tolerance && std::abs(a.Y - b.Y) < tolerance && std::abs(a.Z - b.Z) < tolerance);
}

// 查找闭环
std::vector<std::vector<LineData>> FindClosedCurves(std::vector<LineData>& lines) 
{
    std::vector<std::vector<LineData>> closedCurves;
    std::vector<bool> used(lines.size(), false);
    for (size_t i = 0; i < lines.size(); ++i) {
        if (used[i]) continue;  // 跳过已经处理过的线段

        std::vector<LineData> currentCurve;
        currentCurve.push_back(lines[i]);
        used[i] = true;

        bool curveClosed = false;
        while (!curveClosed) {
            curveClosed = true;
            for (size_t j = 0; j < lines.size(); ++j) {
                if (!used[j]) {
                    if (IsSamePoint(currentCurve.back().end, lines[j].start)) {
                        // 当前线段的终点与另一线段的起点相连
                        currentCurve.push_back(lines[j]);
                        used[j] = true;
                        curveClosed = false;
                        break;
                    }
                    else if (IsSamePoint(currentCurve.back().end, lines[j].end)) 
                    {
                        // 当前线段的终点与另一线段的终点相连
                        // 需要翻转这条线段
                        LineData reversedLine = lines[j];
                        std::swap(reversedLine.start, reversedLine.end);
                        currentCurve.push_back(reversedLine);
                        used[j] = true;
                        curveClosed = false;
                        break;
                    }
                }
            }
        }

        // 检查闭环是否完整(起点等于终点)
        if (currentCurve.size()>2 && IsSamePoint(currentCurve.back().end, currentCurve.front().start))
        {
            closedCurves.push_back(currentCurve);
        }
    }

    return closedCurves;
}


	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);
}
 
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

At_HeadBlock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值