- 积分
- 40630
- 明经币
- 个
- 注册时间
- 2010-7-10
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2025-9-22 20:31:08
|
显示全部楼层
我以前也写过类似的函数。也是用到了四叉树。
 - //寻找首尾相连的曲线的子函数
- static void findConnected(Point& pt, std::vector<AcDbObjectId>& outIds, std::set<AcDbObjectId> & visitedIds, std::set<Point> & points, QuadTree &qt, double tol = 1e-2)
- {
- visitedIds.insert(pt.id);
- outIds.push_back(pt.id);
- Rectangle recQuery;
- recQuery.x = pt.x - tol;
- recQuery.y = pt.y - tol;
- recQuery.width = tol + tol;
- recQuery.height = recQuery.width;
- std::vector<Point> res;
- qt.query(recQuery, res);
- points.erase(pt);
- Point ptOther(pt.ptOther.x, pt.ptOther.y, pt.id);
- ptOther.ptOther = pt;
- points.erase(ptOther);
- if (!pt.bVisited)
- {
- recQuery.x = pt.ptOther.x - tol;
- recQuery.y = pt.ptOther.y - tol;
- qt.query(recQuery, res);
- }
- qt.remove(pt);
- qt.remove(ptOther);
- for (size_t i = 0; i < res.size(); ++i)
- {
- if (res.id == pt.id) continue;
- std::set<AcDbObjectId>::iterator it = visitedIds.find(res.id);
- if (it == visitedIds.end())
- {
- Point ptNew(res.ptOther.x, res.ptOther.y, res.id);
- ptNew.ptOther = res;
- ptNew.bVisited = true;
- findConnected(ptNew, outIds, visitedIds, points, qt, tol);
- }
- }
- }
- //寻找首尾相连的曲线
- static void findConnected(const std::vector<AcDbObjectId>& objs, std::vector<std::vector<AcDbObjectId>>& out, std::vector<AcDbObjectId> & closedIds)
- {
- Acad::ErrorStatus es;
- AcDbExtents exts;
- std::vector<Point> points;
- for (size_t i = 0; i < objs.size(); ++i)
- {
- AcDbObjectId id = objs;
- AcDbObjectPointer<AcDbCurve> curve(id, AcDb::kForRead);
- es = curve.openStatus();
- if (es != Acad::eOk) continue;
- AcGePoint3d pt1, pt2;
- curve->getStartPoint(pt1);
- curve->getEndPoint(pt2);
- pt1.z = 0; //除掉Z坐标
- pt2.z = 0; //除掉Z坐标
- if (curve->isClosed() || pt1.distanceTo(pt2) <= 1e-3)
- {
- closedIds.push_back(id);
- }
- else
- {
- AcDbExtents ext;
- curve->getGeomExtents(ext);
- exts.addExt(ext);
- points.push_back(Point(pt1.x, pt1.y, id));
- points.push_back(Point(pt2.x, pt2.y, id));
- points[points.size() - 1].ptOther = pt1;
- points[points.size() - 2].ptOther = pt2;
- }
- }
- Rectangle rec;
- AcGePoint3d ptMin = exts.minPoint();
- AcGePoint3d ptMax = exts.maxPoint();
- double width = ptMax.x - ptMin.x;
- double height = ptMax.y - ptMin.y;
- rec.x = ptMin.x - 10;
- rec.y = ptMin.y - 10;
- rec.width = width + 20;
- rec.height = height + 20;
- QuadTree qt(rec);
- for (size_t i = 0; i < points.size(); i++)
- {
- qt.insert(points);
- }
- std::set<Point> setPoints;
- for (size_t i = 0; i < points.size(); ++i)
- {
- setPoints.insert(points);
- }
- std::set<AcDbObjectId> visitedIds;
- while (!setPoints.empty())
- {
- Point pt = *setPoints.begin();
- std::vector<AcDbObjectId> ids;
- findConnected(pt, ids, visitedIds, setPoints, qt);
- out.push_back(ids);
- }
- }
|
评分
-
查看全部评分
|