Intersections2D.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import Cartesian2 from './Cartesian2.js';
  2. import Cartesian3 from './Cartesian3.js';
  3. import Check from './Check.js';
  4. import defined from './defined.js';
  5. import DeveloperError from './DeveloperError.js';
  6. /**
  7. * Contains functions for operating on 2D triangles.
  8. *
  9. * @exports Intersections2D
  10. */
  11. var Intersections2D = {};
  12. /**
  13. * Splits a 2D triangle at given axis-aligned threshold value and returns the resulting
  14. * polygon on a given side of the threshold. The resulting polygon may have 0, 1, 2,
  15. * 3, or 4 vertices.
  16. *
  17. * @param {Number} threshold The threshold coordinate value at which to clip the triangle.
  18. * @param {Boolean} keepAbove true to keep the portion of the triangle above the threshold, or false
  19. * to keep the portion below.
  20. * @param {Number} u0 The coordinate of the first vertex in the triangle, in counter-clockwise order.
  21. * @param {Number} u1 The coordinate of the second vertex in the triangle, in counter-clockwise order.
  22. * @param {Number} u2 The coordinate of the third vertex in the triangle, in counter-clockwise order.
  23. * @param {Number[]} [result] The array into which to copy the result. If this parameter is not supplied,
  24. * a new array is constructed and returned.
  25. * @returns {Number[]} The polygon that results after the clip, specified as a list of
  26. * vertices. The vertices are specified in counter-clockwise order.
  27. * Each vertex is either an index from the existing list (identified as
  28. * a 0, 1, or 2) or -1 indicating a new vertex not in the original triangle.
  29. * For new vertices, the -1 is followed by three additional numbers: the
  30. * index of each of the two original vertices forming the line segment that
  31. * the new vertex lies on, and the fraction of the distance from the first
  32. * vertex to the second one.
  33. *
  34. * @example
  35. * var result = Cesium.Intersections2D.clipTriangleAtAxisAlignedThreshold(0.5, false, 0.2, 0.6, 0.4);
  36. * // result === [2, 0, -1, 1, 0, 0.25, -1, 1, 2, 0.5]
  37. */
  38. Intersections2D.clipTriangleAtAxisAlignedThreshold = function(threshold, keepAbove, u0, u1, u2, result) {
  39. //>>includeStart('debug', pragmas.debug);
  40. if (!defined(threshold)) {
  41. throw new DeveloperError('threshold is required.');
  42. }
  43. if (!defined(keepAbove)) {
  44. throw new DeveloperError('keepAbove is required.');
  45. }
  46. if (!defined(u0)) {
  47. throw new DeveloperError('u0 is required.');
  48. }
  49. if (!defined(u1)) {
  50. throw new DeveloperError('u1 is required.');
  51. }
  52. if (!defined(u2)) {
  53. throw new DeveloperError('u2 is required.');
  54. }
  55. //>>includeEnd('debug');
  56. if (!defined(result)) {
  57. result = [];
  58. } else {
  59. result.length = 0;
  60. }
  61. var u0Behind;
  62. var u1Behind;
  63. var u2Behind;
  64. if (keepAbove) {
  65. u0Behind = u0 < threshold;
  66. u1Behind = u1 < threshold;
  67. u2Behind = u2 < threshold;
  68. } else {
  69. u0Behind = u0 > threshold;
  70. u1Behind = u1 > threshold;
  71. u2Behind = u2 > threshold;
  72. }
  73. var numBehind = u0Behind + u1Behind + u2Behind;
  74. var u01Ratio;
  75. var u02Ratio;
  76. var u12Ratio;
  77. var u10Ratio;
  78. var u20Ratio;
  79. var u21Ratio;
  80. if (numBehind === 1) {
  81. if (u0Behind) {
  82. u01Ratio = (threshold - u0) / (u1 - u0);
  83. u02Ratio = (threshold - u0) / (u2 - u0);
  84. result.push(1);
  85. result.push(2);
  86. if (u02Ratio !== 1.0) {
  87. result.push(-1);
  88. result.push(0);
  89. result.push(2);
  90. result.push(u02Ratio);
  91. }
  92. if (u01Ratio !== 1.0) {
  93. result.push(-1);
  94. result.push(0);
  95. result.push(1);
  96. result.push(u01Ratio);
  97. }
  98. } else if (u1Behind) {
  99. u12Ratio = (threshold - u1) / (u2 - u1);
  100. u10Ratio = (threshold - u1) / (u0 - u1);
  101. result.push(2);
  102. result.push(0);
  103. if (u10Ratio !== 1.0) {
  104. result.push(-1);
  105. result.push(1);
  106. result.push(0);
  107. result.push(u10Ratio);
  108. }
  109. if (u12Ratio !== 1.0) {
  110. result.push(-1);
  111. result.push(1);
  112. result.push(2);
  113. result.push(u12Ratio);
  114. }
  115. } else if (u2Behind) {
  116. u20Ratio = (threshold - u2) / (u0 - u2);
  117. u21Ratio = (threshold - u2) / (u1 - u2);
  118. result.push(0);
  119. result.push(1);
  120. if (u21Ratio !== 1.0) {
  121. result.push(-1);
  122. result.push(2);
  123. result.push(1);
  124. result.push(u21Ratio);
  125. }
  126. if (u20Ratio !== 1.0) {
  127. result.push(-1);
  128. result.push(2);
  129. result.push(0);
  130. result.push(u20Ratio);
  131. }
  132. }
  133. } else if (numBehind === 2) {
  134. if (!u0Behind && u0 !== threshold) {
  135. u10Ratio = (threshold - u1) / (u0 - u1);
  136. u20Ratio = (threshold - u2) / (u0 - u2);
  137. result.push(0);
  138. result.push(-1);
  139. result.push(1);
  140. result.push(0);
  141. result.push(u10Ratio);
  142. result.push(-1);
  143. result.push(2);
  144. result.push(0);
  145. result.push(u20Ratio);
  146. } else if (!u1Behind && u1 !== threshold) {
  147. u21Ratio = (threshold - u2) / (u1 - u2);
  148. u01Ratio = (threshold - u0) / (u1 - u0);
  149. result.push(1);
  150. result.push(-1);
  151. result.push(2);
  152. result.push(1);
  153. result.push(u21Ratio);
  154. result.push(-1);
  155. result.push(0);
  156. result.push(1);
  157. result.push(u01Ratio);
  158. } else if (!u2Behind && u2 !== threshold) {
  159. u02Ratio = (threshold - u0) / (u2 - u0);
  160. u12Ratio = (threshold - u1) / (u2 - u1);
  161. result.push(2);
  162. result.push(-1);
  163. result.push(0);
  164. result.push(2);
  165. result.push(u02Ratio);
  166. result.push(-1);
  167. result.push(1);
  168. result.push(2);
  169. result.push(u12Ratio);
  170. }
  171. } else if (numBehind !== 3) {
  172. // Completely in front of threshold
  173. result.push(0);
  174. result.push(1);
  175. result.push(2);
  176. }
  177. // else Completely behind threshold
  178. return result;
  179. };
  180. /**
  181. * Compute the barycentric coordinates of a 2D position within a 2D triangle.
  182. *
  183. * @param {Number} x The x coordinate of the position for which to find the barycentric coordinates.
  184. * @param {Number} y The y coordinate of the position for which to find the barycentric coordinates.
  185. * @param {Number} x1 The x coordinate of the triangle's first vertex.
  186. * @param {Number} y1 The y coordinate of the triangle's first vertex.
  187. * @param {Number} x2 The x coordinate of the triangle's second vertex.
  188. * @param {Number} y2 The y coordinate of the triangle's second vertex.
  189. * @param {Number} x3 The x coordinate of the triangle's third vertex.
  190. * @param {Number} y3 The y coordinate of the triangle's third vertex.
  191. * @param {Cartesian3} [result] The instance into to which to copy the result. If this parameter
  192. * is undefined, a new instance is created and returned.
  193. * @returns {Cartesian3} The barycentric coordinates of the position within the triangle.
  194. *
  195. * @example
  196. * var result = Cesium.Intersections2D.computeBarycentricCoordinates(0.0, 0.0, 0.0, 1.0, -1, -0.5, 1, -0.5);
  197. * // result === new Cesium.Cartesian3(1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0);
  198. */
  199. Intersections2D.computeBarycentricCoordinates = function(x, y, x1, y1, x2, y2, x3, y3, result) {
  200. //>>includeStart('debug', pragmas.debug);
  201. if (!defined(x)) {
  202. throw new DeveloperError('x is required.');
  203. }
  204. if (!defined(y)) {
  205. throw new DeveloperError('y is required.');
  206. }
  207. if (!defined(x1)) {
  208. throw new DeveloperError('x1 is required.');
  209. }
  210. if (!defined(y1)) {
  211. throw new DeveloperError('y1 is required.');
  212. }
  213. if (!defined(x2)) {
  214. throw new DeveloperError('x2 is required.');
  215. }
  216. if (!defined(y2)) {
  217. throw new DeveloperError('y2 is required.');
  218. }
  219. if (!defined(x3)) {
  220. throw new DeveloperError('x3 is required.');
  221. }
  222. if (!defined(y3)) {
  223. throw new DeveloperError('y3 is required.');
  224. }
  225. //>>includeEnd('debug');
  226. var x1mx3 = x1 - x3;
  227. var x3mx2 = x3 - x2;
  228. var y2my3 = y2 - y3;
  229. var y1my3 = y1 - y3;
  230. var inverseDeterminant = 1.0 / (y2my3 * x1mx3 + x3mx2 * y1my3);
  231. var ymy3 = y - y3;
  232. var xmx3 = x - x3;
  233. var l1 = (y2my3 * xmx3 + x3mx2 * ymy3) * inverseDeterminant;
  234. var l2 = (-y1my3 * xmx3 + x1mx3 * ymy3) * inverseDeterminant;
  235. var l3 = 1.0 - l1 - l2;
  236. if (defined(result)) {
  237. result.x = l1;
  238. result.y = l2;
  239. result.z = l3;
  240. return result;
  241. }
  242. return new Cartesian3(l1, l2, l3);
  243. };
  244. /**
  245. * Compute the intersection between 2 line segments
  246. *
  247. * @param {Number} x00 The x coordinate of the first line's first vertex.
  248. * @param {Number} y00 The y coordinate of the first line's first vertex.
  249. * @param {Number} x01 The x coordinate of the first line's second vertex.
  250. * @param {Number} y01 The y coordinate of the first line's second vertex.
  251. * @param {Number} x10 The x coordinate of the second line's first vertex.
  252. * @param {Number} y10 The y coordinate of the second line's first vertex.
  253. * @param {Number} x11 The x coordinate of the second line's second vertex.
  254. * @param {Number} y11 The y coordinate of the second line's second vertex.
  255. * @param {Cartesian2} [result] The instance into to which to copy the result. If this parameter
  256. * is undefined, a new instance is created and returned.
  257. * @returns {Cartesian2} The intersection point, undefined if there is no intersection point or lines are coincident.
  258. *
  259. * @example
  260. * var result = Cesium.Intersections2D.computeLineSegmentLineSegmentIntersection(0.0, 0.0, 0.0, 2.0, -1, 1, 1, 1);
  261. * // result === new Cesium.Cartesian2(0.0, 1.0);
  262. */
  263. Intersections2D.computeLineSegmentLineSegmentIntersection = function(x00, y00, x01, y01, x10, y10, x11, y11, result) {
  264. //>>includeStart('debug', pragmas.debug);
  265. Check.typeOf.number('x00', x00);
  266. Check.typeOf.number('y00', y00);
  267. Check.typeOf.number('x01', x01);
  268. Check.typeOf.number('y01', y01);
  269. Check.typeOf.number('x10', x10);
  270. Check.typeOf.number('y10', y10);
  271. Check.typeOf.number('x11', x11);
  272. Check.typeOf.number('y11', y11);
  273. //>>includeEnd('debug');
  274. var numerator1A = (x11 - x10) * (y00 - y10) - (y11 - y10) * (x00 - x10);
  275. var numerator1B = (x01 - x00) * (y00 - y10) - (y01 - y00) * (x00 - x10);
  276. var denominator1 = (y11 - y10) * (x01 - x00) - (x11 - x10) * (y01 - y00);
  277. // If denominator = 0, then lines are parallel. If denominator = 0 and both numerators are 0, then coincident
  278. if (denominator1 === 0) {
  279. return;
  280. }
  281. var ua1 = numerator1A / denominator1;
  282. var ub1 = numerator1B / denominator1;
  283. if (ua1 >= 0 && ua1 <= 1 && ub1 >= 0 && ub1 <= 1) {
  284. if (!defined(result)) {
  285. result = new Cartesian2();
  286. }
  287. result.x = x00 + ua1 * (x01 - x00);
  288. result.y = y00 + ua1 * (y01 - y00);
  289. return result;
  290. }
  291. };
  292. export default Intersections2D;