PointDistanceInfo.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /////////////////////////////////////////
  2. //Copyright (c) 2016, SAGA
  3. //All rights reserved.
  4. //文件名称: PointDistanceInfo.cs
  5. //文件描述: 包含点线面的功能类
  6. //创 建 者: ly
  7. //创建日期: 2016-2-25
  8. //版 本 号:1.0.0.0
  9. ////////////////////////////////////////
  10. using System;
  11. using System.Collections.Generic;
  12. using Autodesk.Revit.DB;
  13. using SAGA.DotNetUtils;
  14. using SAGA.DotNetUtils.Extend;
  15. namespace SAGA.RevitUtils.Extends
  16. {
  17. public class PointDistanceInfo
  18. {
  19. public PointDistanceInfo(XYZ p1, XYZ p2)
  20. {
  21. this.P1 = p1;
  22. this.P2 = p2;
  23. }
  24. public XYZ P1 { get; private set; }
  25. public XYZ P2 { get; private set; }
  26. private double _mDistance = -1;
  27. public double Distance
  28. {
  29. get
  30. {
  31. if (_mDistance < 0)
  32. {
  33. _mDistance = P1.DistanceTo(P2);
  34. }
  35. return _mDistance;
  36. }
  37. }
  38. public static Line CreateLongLine(XYZ[] points)
  39. {
  40. PointDistanceInfo max = Max(points);
  41. if (max.P1.DistanceTo(max.P2).IsLess(0.1d))
  42. return null;
  43. return max.P1.NewLine( max.P2);
  44. }
  45. public static PointDistanceInfo Min(PointDistanceInfo a, PointDistanceInfo b)
  46. {
  47. if (a.Distance == b.Distance)
  48. {
  49. return a;
  50. }
  51. if (a.Distance > b.Distance)
  52. {
  53. return b;
  54. }
  55. return a;
  56. }
  57. public static PointDistanceInfo Max(PointDistanceInfo a, PointDistanceInfo b)
  58. {
  59. if (a.Distance == b.Distance)
  60. {
  61. return a;
  62. }
  63. if (a.Distance > b.Distance)
  64. {
  65. return a;
  66. }
  67. return b;
  68. }
  69. public static PointDistanceInfo Max(PointDistanceInfo[] items)
  70. {
  71. if (items == null || items.Length <= 0)
  72. throw new ArgumentNullException("items");
  73. PointDistanceInfo maxResult = items[0];
  74. for (int i = 1; i < items.Length; i++)
  75. {
  76. maxResult = Max(maxResult, items[i]);
  77. }
  78. return maxResult;
  79. }
  80. public static PointDistanceInfo Min(PointDistanceInfo[] items)
  81. {
  82. if (items == null || items.Length <= 0)
  83. throw new ArgumentNullException("items");
  84. PointDistanceInfo minResult = items[0];
  85. for (int i = 1; i < items.Length; i++)
  86. {
  87. minResult = Min(minResult, items[i]);
  88. }
  89. return minResult;
  90. }
  91. public static PointDistanceInfo Max(XYZ[] ps)
  92. {
  93. if (ps == null || ps.Length < 2)
  94. {
  95. throw new ArgumentException("ps");
  96. }
  97. PointDistanceInfo[] distances = FromPoints(ps);
  98. if (distances.Length <= 0)
  99. throw new ArgumentException("ps");
  100. return Max(distances);
  101. }
  102. public static PointDistanceInfo Min(XYZ[] ps)
  103. {
  104. if (ps == null || ps.Length < 2)
  105. {
  106. throw new ArgumentException("ps");
  107. }
  108. PointDistanceInfo[] distances = FromPoints(ps);
  109. if (distances.Length <= 0)
  110. throw new ArgumentException("ps");
  111. return Min(distances);
  112. }
  113. public static PointDistanceInfo[] FromPoints(XYZ[] points)
  114. {
  115. List<PointDistanceInfo> listDistanceInfos = new List<PointDistanceInfo>();
  116. for (int i = 0; i < points.Length; i++)
  117. {
  118. for (int j = i + 1; j < points.Length; j++)
  119. {
  120. listDistanceInfos.Add(new PointDistanceInfo(points[i], points[j]));
  121. }
  122. }
  123. return listDistanceInfos.ToArray();
  124. }
  125. }
  126. }