- UID
- 10108
- 积分
- 5956
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-9-17
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
一个开源的高效 三角网 库 https://github.com/garykac/triangle.net
一个简单测试,没有使用 USE_Z 生成凸包 和 测试拾取点所在的三角形
- [CommandMethod("tt")]
- public void CmdTinTriangle()
- {
- var doc = Application.DocumentManager.MdiActiveDocument;
- var ed = doc.Editor;
- var filter = new SelectionFilter(new[] { new TypedValue(0, "POINT") });
- var pso = new PromptSelectionOptions() { MessageForAdding = "\nselect points: " };
- var psr = ed.GetSelection(pso, filter);
- if (psr.Status != PromptStatus.OK) return;
- using (var tr = doc.Database.TransactionManager.StartTransaction())
- {
- try
- {
- var polygon = new TriangleNet.Geometry.Polygon();
- foreach (ObjectId id in psr.Value.GetObjectIds())
- {
- var pnt = (DBPoint)tr.GetObject(id, OpenMode.ForRead);
- polygon.Add(new TriangleNet.Geometry.Vertex(pnt.Position.X, pnt.Position.Y));
- }
- var options = new ConstraintOptions { Convex = true };
- var mesh = (TriangleNet.Mesh)polygon.Triangulate(options);
- var btr = (BlockTableRecord)tr.GetObject(doc.Database.CurrentSpaceId, OpenMode.ForWrite);
- var pts = new List<Point2d>();
- foreach (var sg in mesh.Segments)
- {
- if (sg.Label != 1) continue;
- var v1 = mesh.Vertices.First(t => t.ID == sg.P0);
- pts.Add(new Point2d(v1.X, v1.Y));
- }
- var pline = new Polyline();
- for (int i = 0; i < pts.Count; i++)
- {
- pline.AddVertexAt(i,pts[i], 0.0, 0.0, 0.0);
- }
- pline.Closed = true;
- btr.AppendEntity(pline);
- tr.AddNewlyCreatedDBObject(pline, true);
- var ppr = doc.Editor.GetPoint("\ntest point: ");
- if (ppr.Status == PromptStatus.OK)
- {
- var qs = new TriangleNet.Tools.TriangleQuadTree(mesh);
- var tri =(Triangle) qs.Query(ppr.Value.X, ppr.Value.Y);
- var pnts = mesh.Vertices;
- var p1 = pnts.First(t => t.ID == tri.GetVertexID(0));
- var p2 = pnts.First(t => t.ID == tri.GetVertexID(1));
- var p3 = pnts.First(t => t.ID == tri.GetVertexID(2));
- Addpline(tr, btr,
- new List<Point2d>()
- {new Point2d(p1.X, p1.Y), new Point2d(p2.X, p2.Y), new Point2d(p3.X, p3.Y)});
- }
- tr.Commit();
- }
- catch
- {
- tr.Abort();
- }
- }
- }
- private void Addpline(Transaction tr, BlockTableRecord btr, List<Point2d> pts)
- {
- var pline = new Polyline();
- for (var i = 0; i < pts.Count; i++)
- {
- pline.AddVertexAt(i, pts[i], 0.0, 0.0, 0.0);
- }
- pline.Closed = true;
- btr.AppendEntity(pline);
- tr.AddNewlyCreatedDBObject(pline, true);
- }
|
|