ResultPoint.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  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. namespace Zxing;
  18. use Zxing\Common\Detector\MathUtils;
  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. class ResultPoint {
  26. private $x;
  27. private $y;
  28. public function __construct($x, $y) {
  29. $this->x = (float)($x);
  30. $this->y = (float)($y);
  31. }
  32. public final function getX() {
  33. return (float)($this->x);
  34. }
  35. public final function getY() {
  36. return (float)($this->y);
  37. }
  38. //@Override
  39. public final function equals($other) {
  40. if ($other instanceof ResultPoint) {
  41. $otherPoint = $other;
  42. return $this->x == $otherPoint->x && $this->y == $otherPoint->y;
  43. }
  44. return false;
  45. }
  46. //@Override
  47. public final function hashCode() {
  48. return 31 * floatToIntBits($this->x) + floatToIntBits($this->y);
  49. }
  50. //@Override
  51. public final function toString() {
  52. $result = '';
  53. $result.= ('(');
  54. $result.=($this->x);
  55. $result.=(',');
  56. $result.=($this->y);
  57. $result.=(')');
  58. return $result;
  59. }
  60. /**
  61. * Orders an array of three ResultPoints in an order [A,B,C] such that AB is less than AC
  62. * and BC is less than AC, and the angle between BC and BA is less than 180 degrees.
  63. *
  64. * @param patterns array of three {@code ResultPoint} to order
  65. */
  66. public static function orderBestPatterns($patterns) {
  67. // Find distances between pattern centers
  68. $zeroOneDistance = self::distance($patterns[0], $patterns[1]);
  69. $oneTwoDistance = self::distance($patterns[1], $patterns[2]);
  70. $zeroTwoDistance = self::distance($patterns[0], $patterns[2]);
  71. $pointA='';
  72. $pointB='';
  73. $pointC='';
  74. // Assume one closest to other two is B; A and C will just be guesses at first
  75. if ($oneTwoDistance >= $zeroOneDistance && $oneTwoDistance >= $zeroTwoDistance) {
  76. $pointB = $patterns[0];
  77. $pointA = $patterns[1];
  78. $pointC = $patterns[2];
  79. } else if ($zeroTwoDistance >= $oneTwoDistance && $zeroTwoDistance >= $zeroOneDistance) {
  80. $pointB = $patterns[1];
  81. $pointA = $patterns[0];
  82. $pointC = $patterns[2];
  83. } else {
  84. $pointB = $patterns[2];
  85. $pointA = $patterns[0];
  86. $pointC = $patterns[1];
  87. }
  88. // Use cross product to figure out whether A and C are correct or flipped.
  89. // This asks whether BC x BA has a positive z component, which is the arrangement
  90. // we want for A, B, C. If it's negative, then we've got it flipped around and
  91. // should swap A and C.
  92. if (self::crossProductZ($pointA, $pointB, $pointC) < 0.0) {
  93. $temp = $pointA;
  94. $pointA = $pointC;
  95. $pointC = $temp;
  96. }
  97. $patterns[0] = $pointA;
  98. $patterns[1] = $pointB;
  99. $patterns[2] = $pointC;
  100. return $patterns;
  101. }
  102. /**
  103. * @param pattern1 first pattern
  104. * @param pattern2 second pattern
  105. * @return distance between two points
  106. */
  107. public static function distance($pattern1, $pattern2) {
  108. return MathUtils::distance($pattern1->x, $pattern1->y, $pattern2->x, $pattern2->y);
  109. }
  110. /**
  111. * Returns the z component of the cross product between vectors BC and BA.
  112. */
  113. private static function crossProductZ($pointA,
  114. $pointB,
  115. $pointC) {
  116. $bX = $pointB->x;
  117. $bY = $pointB->y;
  118. return (($pointC->x - $bX) * ($pointA->y - $bY)) - (($pointC->y - $bY) * ($pointA->x - $bX));
  119. }
  120. }