Polygon.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:Polygon
  3. * 作者:xulisong
  4. * 创建时间: 2019/4/29 17:37:09
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Autodesk.Revit.DB;
  15. using SAGA.RevitUtils.Extends;
  16. namespace SAGA.RevitUtils.Utils
  17. {
  18. public class Polygon : IEnumerable<XYZ>
  19. {
  20. private List<XYZ> m_Points;
  21. public Polygon(List<XYZ> points)
  22. {
  23. if (points.Count < 3)
  24. {
  25. throw new ArgumentException($"{nameof(points)}数量不能小于3");
  26. }
  27. m_Points = (points ?? new List<XYZ>()).Select(xyz=>xyz.NewZ()).ToList();
  28. Points = new ReadOnlyCollection<XYZ>(m_Points);
  29. }
  30. /// <summary>
  31. /// 关联逆时针点(最后一个点和第一个点封闭)
  32. /// </summary>
  33. public ReadOnlyCollection<XYZ> Points { get; private set; }
  34. #region 实现遍历方法
  35. public IEnumerator<XYZ> GetEnumerator()
  36. {
  37. return ((IEnumerable<XYZ>)m_Points).GetEnumerator();
  38. }
  39. IEnumerator IEnumerable.GetEnumerator()
  40. {
  41. return ((IEnumerable<XYZ>)m_Points).GetEnumerator();
  42. }
  43. #endregion
  44. #region 子凸多边形
  45. private ReadOnlyCollection<Polygon> m_ChildrenConvexPolygons;
  46. /// <summary>
  47. /// 分解的子凸多边形
  48. /// </summary>
  49. public ReadOnlyCollection<Polygon> ChildrenConvexPolygons
  50. {
  51. get
  52. {
  53. if (m_ChildrenConvexPolygons == null)
  54. {
  55. var polygons = PolygonUtil.SplitToConvexPolygons(this);
  56. m_ChildrenConvexPolygons = new ReadOnlyCollection<Polygon>(polygons);
  57. }
  58. return m_ChildrenConvexPolygons;
  59. }
  60. }
  61. #endregion
  62. private Outline m_Box;
  63. /// <summary>
  64. /// 多边形包围盒
  65. /// </summary>
  66. public Outline Box
  67. {
  68. get
  69. {
  70. if (m_Box == null)
  71. {
  72. m_Box = OutlineUtil.CreateBox(this.m_Points);
  73. }
  74. return m_Box;
  75. }
  76. }
  77. }
  78. }