| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class FinderPattern |
| 4 |
* |
| 5 |
* @created 17.01.2021 |
| 6 |
* @author ZXing Authors |
| 7 |
* @author Smiley <[email protected]> |
| 8 |
* @copyright 2021 Smiley |
| 9 |
* @license Apache-2.0 |
| 10 |
*/ |
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
namespace chillerlan\QRCode\Detector; |
| 14 |
|
| 15 |
use function sqrt; |
| 16 |
|
| 17 |
/** |
| 18 |
* Encapsulates a finder pattern, which are the three square patterns found in |
| 19 |
* the corners of QR Codes. It also encapsulates a count of similar finder patterns, |
| 20 |
* as a convenience to the finder's bookkeeping. |
| 21 |
* |
| 22 |
* @author Sean Owen |
| 23 |
*/ |
| 24 |
final class FinderPattern extends ResultPoint{ |
| 25 |
|
| 26 |
private int $count; |
| 27 |
|
| 28 |
public function __construct(float $posX, float $posY, float $estimatedModuleSize, int|null $count = null){ |
| 29 |
parent::__construct($posX, $posY, $estimatedModuleSize); |
| 30 |
|
| 31 |
$this->count = ($count ?? 1); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @deprecated 6.0.1 This method will be removed. In v7, use the property "FinderPattern::$count" instead. |
| 36 |
*/ |
| 37 |
public function getCount():int{ |
| 38 |
return $this->count; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param \chillerlan\QRCode\Detector\FinderPattern $b second pattern |
| 43 |
* |
| 44 |
* @return float distance between two points |
| 45 |
*/ |
| 46 |
public function getDistance(FinderPattern $b):float{ |
| 47 |
return self::distance($this->x, $this->y, $b->x, $b->y); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get square of distance between a and b. |
| 52 |
*/ |
| 53 |
public function getSquaredDistance(FinderPattern $b):float{ |
| 54 |
return self::squaredDistance($this->x, $this->y, $b->x, $b->y); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Combines this object's current estimate of a finder pattern position and module size |
| 59 |
* with a new estimate. It returns a new FinderPattern containing a weighted average |
| 60 |
* based on count. |
| 61 |
*/ |
| 62 |
public function combineEstimate(float $i, float $j, float $newModuleSize):static{ |
| 63 |
$combinedCount = ($this->count + 1); |
| 64 |
|
| 65 |
return new self( |
| 66 |
($this->count * $this->x + $j) / $combinedCount, |
| 67 |
($this->count * $this->y + $i) / $combinedCount, |
| 68 |
($this->count * $this->estimatedModuleSize + $newModuleSize) / $combinedCount, |
| 69 |
$combinedCount, |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
private static function squaredDistance(float $aX, float $aY, float $bX, float $bY):float{ |
| 74 |
$xDiff = ($aX - $bX); |
| 75 |
$yDiff = ($aY - $bY); |
| 76 |
|
| 77 |
return ($xDiff * $xDiff + $yDiff * $yDiff); |
| 78 |
} |
| 79 |
|
| 80 |
public static function distance(float $aX, float $aY, float $bX, float $bY):float{ |
| 81 |
return sqrt(self::squaredDistance($aX, $aY, $bX, $bY)); |
| 82 |
} |
| 83 |
|
| 84 |
} |