ResultPoint.js 4.2 KB

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