FinderPattern.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * Class FinderPattern
  4. *
  5. * @created 17.01.2021
  6. * @author ZXing Authors
  7. * @author Smiley <smiley@chillerlan.net>
  8. * @copyright 2021 Smiley
  9. * @license Apache-2.0
  10. */
  11. namespace chillerlan\QRCode\Detector;
  12. use function chillerlan\QRCode\Common\{distance, squaredDistance};
  13. /**
  14. * <p>Encapsulates a finder pattern, which are the three square patterns found in
  15. * the corners of QR Codes. It also encapsulates a count of similar finder patterns,
  16. * as a convenience to the finder's bookkeeping.</p>
  17. *
  18. * @author Sean Owen
  19. */
  20. final class FinderPattern extends ResultPoint{
  21. private int $count;
  22. public function __construct(float $posX, float $posY, float $estimatedModuleSize, int $count = 1){
  23. parent::__construct($posX, $posY, $estimatedModuleSize);
  24. $this->count = $count;
  25. }
  26. public function getCount():int{
  27. return $this->count;
  28. }
  29. /**
  30. * @param \chillerlan\QRCode\Detector\FinderPattern $b second pattern
  31. *
  32. * @return float distance between two points
  33. */
  34. public function distance(FinderPattern $b):float{
  35. return distance($this->getX(), $this->getY(), $b->getX(), $b->getY());
  36. }
  37. /**
  38. * Get square of distance between a and b.
  39. */
  40. public function squaredDistance(FinderPattern $b):float{
  41. return squaredDistance($this->getX(), $this->getY(), $b->getX(), $b->getY());
  42. }
  43. /**
  44. * Combines this object's current estimate of a finder pattern position and module size
  45. * with a new estimate. It returns a new {@code FinderPattern} containing a weighted average
  46. * based on count.
  47. */
  48. public function combineEstimate(float $i, float $j, float $newModuleSize):FinderPattern{
  49. $combinedCount = $this->count + 1;
  50. return new self(
  51. ($this->count * $this->x + $j) / $combinedCount,
  52. ($this->count * $this->y + $i) / $combinedCount,
  53. ($this->count * $this->estimatedModuleSize + $newModuleSize) / $combinedCount,
  54. $combinedCount
  55. );
  56. }
  57. }