OrientedBoundingRect.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import Point, { PointLike } from './Point';
  20. import BoundingRect from './BoundingRect';
  21. import { MatrixArray } from './matrix';
  22. const extent = [0, 0];
  23. const extent2 = [0, 0];
  24. const minTv = new Point();
  25. const maxTv = new Point();
  26. class OrientedBoundingRect {
  27. // lt, rt, rb, lb
  28. private _corners: Point[] = [];
  29. private _axes: Point[] = [];
  30. private _origin: number[] = [0, 0];
  31. constructor(rect?: BoundingRect, transform?: MatrixArray) {
  32. for (let i = 0; i < 4; i++) {
  33. this._corners[i] = new Point();
  34. }
  35. for (let i = 0; i < 2; i++) {
  36. this._axes[i] = new Point();
  37. }
  38. if (rect) {
  39. this.fromBoundingRect(rect, transform);
  40. }
  41. }
  42. fromBoundingRect(rect: BoundingRect, transform?: MatrixArray) {
  43. const corners = this._corners;
  44. const axes = this._axes;
  45. const x = rect.x;
  46. const y = rect.y;
  47. const x2 = x + rect.width;
  48. const y2 = y + rect.height;
  49. corners[0].set(x, y);
  50. corners[1].set(x2, y);
  51. corners[2].set(x2, y2);
  52. corners[3].set(x, y2);
  53. if (transform) {
  54. for (let i = 0; i < 4; i++) {
  55. corners[i].transform(transform);
  56. }
  57. }
  58. // Calculate axes
  59. Point.sub(axes[0], corners[1], corners[0]);
  60. Point.sub(axes[1], corners[3], corners[0]);
  61. axes[0].normalize();
  62. axes[1].normalize();
  63. // Calculate projected origin
  64. for (let i = 0; i < 2; i++) {
  65. this._origin[i] = axes[i].dot(corners[0]);
  66. }
  67. }
  68. /**
  69. * If intersect with another OBB
  70. * @param other Bounding rect to be intersected with
  71. * @param mtv Calculated .
  72. * If it's not overlapped. it means needs to move given rect with Maximum Translation Vector to be overlapped.
  73. * Else it means needs to move given rect with Minimum Translation Vector to be not overlapped.
  74. */
  75. intersect(other: OrientedBoundingRect, mtv?: PointLike): boolean {
  76. // OBB collision with SAT method
  77. let overlapped = true;
  78. const noMtv = !mtv;
  79. minTv.set(Infinity, Infinity);
  80. maxTv.set(0, 0);
  81. // Check two axes for both two obb.
  82. if (!this._intersectCheckOneSide(this, other, minTv, maxTv, noMtv, 1)) {
  83. overlapped = false;
  84. if (noMtv) {
  85. // Early return if no need to calculate mtv
  86. return overlapped;
  87. }
  88. }
  89. if (!this._intersectCheckOneSide(other, this, minTv, maxTv, noMtv, -1)) {
  90. overlapped = false;
  91. if (noMtv) {
  92. return overlapped;
  93. }
  94. }
  95. if (!noMtv) {
  96. Point.copy(mtv, overlapped ? minTv : maxTv);
  97. }
  98. return overlapped;
  99. }
  100. private _intersectCheckOneSide(
  101. self: OrientedBoundingRect,
  102. other: OrientedBoundingRect,
  103. minTv: Point,
  104. maxTv: Point,
  105. noMtv: boolean,
  106. inverse: 1 | -1
  107. ): boolean {
  108. let overlapped = true;
  109. for (let i = 0; i < 2; i++) {
  110. const axis = this._axes[i];
  111. this._getProjMinMaxOnAxis(i, self._corners, extent);
  112. this._getProjMinMaxOnAxis(i, other._corners, extent2);
  113. // Not overlap on the any axis.
  114. if (extent[1] < extent2[0] || extent[0] > extent2[1]) {
  115. overlapped = false;
  116. if (noMtv) {
  117. return overlapped;
  118. }
  119. const dist0 = Math.abs(extent2[0] - extent[1]);
  120. const dist1 = Math.abs(extent[0] - extent2[1]);
  121. // Find longest distance of all axes.
  122. if (Math.min(dist0, dist1) > maxTv.len()) {
  123. if (dist0 < dist1) {
  124. Point.scale(maxTv, axis, -dist0 * inverse);
  125. }
  126. else {
  127. Point.scale(maxTv, axis, dist1 * inverse);
  128. }
  129. }
  130. }
  131. else if (minTv) {
  132. const dist0 = Math.abs(extent2[0] - extent[1]);
  133. const dist1 = Math.abs(extent[0] - extent2[1]);
  134. if (Math.min(dist0, dist1) < minTv.len()) {
  135. if (dist0 < dist1) {
  136. Point.scale(minTv, axis, dist0 * inverse);
  137. }
  138. else {
  139. Point.scale(minTv, axis, -dist1 * inverse);
  140. }
  141. }
  142. }
  143. }
  144. return overlapped;
  145. }
  146. private _getProjMinMaxOnAxis(dim: number, corners: Point[], out: number[]) {
  147. const axis = this._axes[dim];
  148. const origin = this._origin;
  149. const proj = corners[0].dot(axis) + origin[dim];
  150. let min = proj;
  151. let max = proj;
  152. for (let i = 1; i < corners.length; i++) {
  153. const proj = corners[i].dot(axis) + origin[dim];
  154. min = Math.min(proj, min);
  155. max = Math.max(proj, max);
  156. }
  157. out[0] = min;
  158. out[1] = max;
  159. }
  160. }
  161. export default OrientedBoundingRect;