ResultPoint.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*namespace com.google.zxing {*/
  17. import MathUtils from './common/detector/MathUtils';
  18. import Float from './util/Float';
  19. /**
  20. * <p>Encapsulates a point of interest in an image containing a barcode. Typically, this
  21. * would be the location of a finder pattern or the corner of the barcode, for example.</p>
  22. *
  23. * @author Sean Owen
  24. */
  25. var ResultPoint = /** @class */ (function () {
  26. function ResultPoint(x, y) {
  27. this.x = x;
  28. this.y = y;
  29. }
  30. ResultPoint.prototype.getX = function () {
  31. return this.x;
  32. };
  33. ResultPoint.prototype.getY = function () {
  34. return this.y;
  35. };
  36. /*@Override*/
  37. ResultPoint.prototype.equals = function (other) {
  38. if (other instanceof ResultPoint) {
  39. var otherPoint = other;
  40. return this.x === otherPoint.x && this.y === otherPoint.y;
  41. }
  42. return false;
  43. };
  44. /*@Override*/
  45. ResultPoint.prototype.hashCode = function () {
  46. return 31 * Float.floatToIntBits(this.x) + Float.floatToIntBits(this.y);
  47. };
  48. /*@Override*/
  49. ResultPoint.prototype.toString = function () {
  50. return '(' + this.x + ',' + this.y + ')';
  51. };
  52. /**
  53. * Orders an array of three ResultPoints in an order [A,B,C] such that AB is less than AC
  54. * and BC is less than AC, and the angle between BC and BA is less than 180 degrees.
  55. *
  56. * @param patterns array of three {@code ResultPoint} to order
  57. */
  58. ResultPoint.orderBestPatterns = function (patterns) {
  59. // Find distances between pattern centers
  60. var zeroOneDistance = this.distance(patterns[0], patterns[1]);
  61. var oneTwoDistance = this.distance(patterns[1], patterns[2]);
  62. var zeroTwoDistance = this.distance(patterns[0], patterns[2]);
  63. var pointA;
  64. var pointB;
  65. var pointC;
  66. // Assume one closest to other two is B; A and C will just be guesses at first
  67. if (oneTwoDistance >= zeroOneDistance && oneTwoDistance >= zeroTwoDistance) {
  68. pointB = patterns[0];
  69. pointA = patterns[1];
  70. pointC = patterns[2];
  71. }
  72. else if (zeroTwoDistance >= oneTwoDistance && zeroTwoDistance >= zeroOneDistance) {
  73. pointB = patterns[1];
  74. pointA = patterns[0];
  75. pointC = patterns[2];
  76. }
  77. else {
  78. pointB = patterns[2];
  79. pointA = patterns[0];
  80. pointC = patterns[1];
  81. }
  82. // Use cross product to figure out whether A and C are correct or flipped.
  83. // This asks whether BC x BA has a positive z component, which is the arrangement
  84. // we want for A, B, C. If it's negative, then we've got it flipped around and
  85. // should swap A and C.
  86. if (this.crossProductZ(pointA, pointB, pointC) < 0.0) {
  87. var temp = pointA;
  88. pointA = pointC;
  89. pointC = temp;
  90. }
  91. patterns[0] = pointA;
  92. patterns[1] = pointB;
  93. patterns[2] = pointC;
  94. };
  95. /**
  96. * @param pattern1 first pattern
  97. * @param pattern2 second pattern
  98. * @return distance between two points
  99. */
  100. ResultPoint.distance = function (pattern1, pattern2) {
  101. return MathUtils.distance(pattern1.x, pattern1.y, pattern2.x, pattern2.y);
  102. };
  103. /**
  104. * Returns the z component of the cross product between vectors BC and BA.
  105. */
  106. ResultPoint.crossProductZ = function (pointA, pointB, pointC) {
  107. var bX = pointB.x;
  108. var bY = pointB.y;
  109. return ((pointC.x - bX) * (pointA.y - bY)) - ((pointC.y - bY) * (pointA.x - bX));
  110. };
  111. return ResultPoint;
  112. }());
  113. export default ResultPoint;