Issues (39)

src/Detector/FinderPatternFinder.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Class FinderPatternFinder
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
 * @phan-file-suppress PhanTypePossiblyInvalidDimOffset
12
 */
13
14
namespace chillerlan\QRCode\Detector;
15
16
use chillerlan\QRCode\Decoder\BitMatrix;
17
use function abs, count, usort;
18
use const PHP_FLOAT_MAX;
19
20
/**
21
 * This class attempts to find finder patterns in a QR Code. Finder patterns are the square
22
 * markers at three corners of a QR Code.
23
 *
24
 * This class is thread-safe but not reentrant. Each thread must allocate its own object.
25
 *
26
 * @author Sean Owen
27
 */
28
final class FinderPatternFinder{
29
30
	private const MIN_SKIP      = 2;
31
	private const MAX_MODULES   = 177; // 1 pixel/module times 3 modules/center
32
	private const CENTER_QUORUM = 2; // support up to version 10 for mobile clients
33
	private BitMatrix $matrix;
34
	/** @var \chillerlan\QRCode\Detector\FinderPattern[] */
35
	private array $possibleCenters;
36
	private bool  $hasSkipped = false;
37
38
	/**
39
	 * Creates a finder that will search the image for three finder patterns.
40
	 *
41
	 * @param BitMatrix $matrix image to search
42
	 */
43
	public function __construct(BitMatrix $matrix){
44
		$this->matrix          = $matrix;
45
		$this->possibleCenters = [];
46
	}
47
48
	/**
49
	 * @return \chillerlan\QRCode\Detector\FinderPattern[]
50
	 */
51
	public function find():array{
52
		$dimension = $this->matrix->getSize();
53
54
		// We are looking for black/white/black/white/black modules in
55
		// 1:1:3:1:1 ratio; this tracks the number of such modules seen so far
56
		// Let's assume that the maximum version QR Code we support takes up 1/4 the height of the
57
		// image, and then account for the center being 3 modules in size. This gives the smallest
58
		// number of pixels the center could be, so skip this often.
59
		$iSkip = (int)((3 * $dimension) / (4 * self::MAX_MODULES));
60
61
		if($iSkip < self::MIN_SKIP){
62
			$iSkip = self::MIN_SKIP;
63
		}
64
65
		$done = false;
66
67
		for($i = ($iSkip - 1); ($i < $dimension) && !$done; $i += $iSkip){
68
			// Get a row of black/white values
69
			$stateCount   = $this->getCrossCheckStateCount();
70
			$currentState = 0;
71
72
			for($j = 0; $j < $dimension; $j++){
73
74
				// Black pixel
75
				if($this->matrix->check($j, $i)){
76
					// Counting white pixels
77
					if(($currentState & 1) === 1){
78
						$currentState++;
79
					}
80
81
					$stateCount[$currentState]++;
82
				}
83
				// White pixel
84
				else{
85
					// Counting black pixels
86
					if(($currentState & 1) === 0){
87
						// A winner?
88
						if($currentState === 4){
89
							// Yes
90
							if($this->foundPatternCross($stateCount)){
91
								$confirmed = $this->handlePossibleCenter($stateCount, $i, $j);
92
93
								if($confirmed){
94
									// Start examining every other line. Checking each line turned out to be too
95
									// expensive and didn't improve performance.
96
									$iSkip = 3;
97
98
									if($this->hasSkipped){
99
										$done = $this->haveMultiplyConfirmedCenters();
100
									}
101
									else{
102
										$rowSkip = $this->findRowSkip();
103
104
										if($rowSkip > $stateCount[2]){
105
											// Skip rows between row of lower confirmed center
106
											// and top of presumed third confirmed center
107
											// but back up a bit to get a full chance of detecting
108
											// it, entire width of center of finder pattern
109
110
											// Skip by rowSkip, but back off by $stateCount[2] (size of last center
111
											// of pattern we saw) to be conservative, and also back off by iSkip which
112
											// is about to be re-added
113
											$i += ($rowSkip - $stateCount[2] - $iSkip);
114
											$j = ($dimension - 1);
115
										}
116
									}
117
								}
118
								else{
119
									$stateCount   = $this->doShiftCounts2($stateCount);
120
									$currentState = 3;
121
122
									continue;
123
								}
124
								// Clear state to start looking again
125
								$currentState = 0;
126
								$stateCount   = $this->getCrossCheckStateCount();
127
							}
128
							// No, shift counts back by two
129
							else{
130
								$stateCount   = $this->doShiftCounts2($stateCount);
131
								$currentState = 3;
132
							}
133
						}
134
						else{
135
							$stateCount[++$currentState]++;
136
						}
137
					}
138
					// Counting white pixels
139
					else{
140
						$stateCount[$currentState]++;
141
					}
142
				}
143
			}
144
145
			if($this->foundPatternCross($stateCount)){
146
				$confirmed = $this->handlePossibleCenter($stateCount, $i, $dimension);
147
148
				if($confirmed){
149
					$iSkip = $stateCount[0];
150
151
					if($this->hasSkipped){
152
						// Found a third one
153
						$done = $this->haveMultiplyConfirmedCenters();
154
					}
155
				}
156
			}
157
		}
158
159
		return $this->orderBestPatterns($this->selectBestPatterns());
160
	}
161
162
	/**
163
	 * @return int[]
164
	 */
165
	private function getCrossCheckStateCount():array{
166
		return [0, 0, 0, 0, 0];
167
	}
168
169
	/**
170
	 * @param int[] $stateCount
171
	 *
172
	 * @return int[]
173
	 */
174
	private function doShiftCounts2(array $stateCount):array{
175
		$stateCount[0] = $stateCount[2];
176
		$stateCount[1] = $stateCount[3];
177
		$stateCount[2] = $stateCount[4];
178
		$stateCount[3] = 1;
179
		$stateCount[4] = 0;
180
181
		return $stateCount;
182
	}
183
184
	/**
185
	 * Given a count of black/white/black/white/black pixels just seen and an end position,
186
	 * figures the location of the center of this run.
187
	 *
188
	 * @param int[] $stateCount
189
	 */
190
	private function centerFromEnd(array $stateCount, int $end):float{
191
		return (float)(($end - $stateCount[4] - $stateCount[3]) - $stateCount[2] / 2);
192
	}
193
194
	/**
195
	 * @param int[] $stateCount
196
	 */
197
	private function foundPatternCross(array $stateCount):bool{
198
		// Allow less than 50% variance from 1-1-3-1-1 proportions
199
		return $this->foundPatternVariance($stateCount, 2.0);
200
	}
201
202
	/**
203
	 * @param int[] $stateCount
204
	 */
205
	private function foundPatternDiagonal(array $stateCount):bool{
206
		// Allow less than 75% variance from 1-1-3-1-1 proportions
207
		return $this->foundPatternVariance($stateCount, 1.333);
208
	}
209
210
	/**
211
	 * @param int[] $stateCount count of black/white/black/white/black pixels just read
212
	 *
213
	 * @return bool true if the proportions of the counts is close enough to the 1/1/3/1/1 ratios
214
	 *              used by finder patterns to be considered a match
215
	 */
216
	private function foundPatternVariance(array $stateCount, float $variance):bool{
217
		$totalModuleSize = 0;
218
219
		for($i = 0; $i < 5; $i++){
220
			$count = $stateCount[$i];
221
222
			if($count === 0){
223
				return false;
224
			}
225
226
			$totalModuleSize += $count;
227
		}
228
229
		if($totalModuleSize < 7){
230
			return false;
231
		}
232
233
		$moduleSize  = ($totalModuleSize / 7.0);
234
		$maxVariance = ($moduleSize / $variance);
235
236
		return
237
			abs($moduleSize - $stateCount[0]) < $maxVariance
238
			&& abs($moduleSize - $stateCount[1]) < $maxVariance
239
			&& abs(3.0 * $moduleSize - $stateCount[2]) < (3 * $maxVariance)
240
			&& abs($moduleSize - $stateCount[3]) < $maxVariance
241
			&& abs($moduleSize - $stateCount[4]) < $maxVariance;
242
	}
243
244
	/**
245
	 * After a vertical and horizontal scan finds a potential finder pattern, this method
246
	 * "cross-cross-cross-checks" by scanning down diagonally through the center of the possible
247
	 * finder pattern to see if the same proportion is detected.
248
	 *
249
	 * @param int $centerI row where a finder pattern was detected
250
	 * @param int $centerJ center of the section that appears to cross a finder pattern
251
	 *
252
	 * @return bool true if proportions are withing expected limits
253
	 */
254
	private function crossCheckDiagonal(int $centerI, int $centerJ):bool{
255
		$stateCount = $this->getCrossCheckStateCount();
256
257
		// Start counting up, left from center finding black center mass
258
		$i = 0;
259
260
		while($centerI >= $i && $centerJ >= $i && $this->matrix->check(($centerJ - $i), ($centerI - $i))){
261
			$stateCount[2]++;
262
			$i++;
263
		}
264
265
		if($stateCount[2] === 0){
266
			return false;
267
		}
268
269
		// Continue up, left finding white space
270
		while($centerI >= $i && $centerJ >= $i && !$this->matrix->check(($centerJ - $i), ($centerI - $i))){
271
			$stateCount[1]++;
272
			$i++;
273
		}
274
275
		if($stateCount[1] === 0){
276
			return false;
277
		}
278
279
		// Continue up, left finding black border
280
		while($centerI >= $i && $centerJ >= $i && $this->matrix->check(($centerJ - $i), ($centerI - $i))){
281
			$stateCount[0]++;
282
			$i++;
283
		}
284
285
		if($stateCount[0] === 0){
286
			return false;
287
		}
288
289
		$dimension = $this->matrix->getSize();
290
291
		// Now also count down, right from center
292
		$i = 1;
293
		while(($centerI + $i) < $dimension && ($centerJ + $i) < $dimension && $this->matrix->check(($centerJ + $i), ($centerI + $i))){
294
			$stateCount[2]++;
295
			$i++;
296
		}
297
298
		while(($centerI + $i) < $dimension && ($centerJ + $i) < $dimension && !$this->matrix->check(($centerJ + $i), ($centerI + $i))){
299
			$stateCount[3]++;
300
			$i++;
301
		}
302
303
		if($stateCount[3] === 0){
304
			return false;
305
		}
306
307
		while(($centerI + $i) < $dimension && ($centerJ + $i) < $dimension && $this->matrix->check(($centerJ + $i), ($centerI + $i))){
308
			$stateCount[4]++;
309
			$i++;
310
		}
311
312
		if($stateCount[4] === 0){
313
			return false;
314
		}
315
316
		return $this->foundPatternDiagonal($stateCount);
317
	}
318
319
	/**
320
	 * After a horizontal scan finds a potential finder pattern, this method
321
	 * "cross-checks" by scanning down vertically through the center of the possible
322
	 * finder pattern to see if the same proportion is detected.
323
	 *
324
	 * @param int $startI   row where a finder pattern was detected
325
	 * @param int $centerJ  center of the section that appears to cross a finder pattern
326
	 * @param int $maxCount maximum reasonable number of modules that should be
327
	 *                      observed in any reading state, based on the results of the horizontal scan
328
	 * @param int $originalStateCountTotal
329
	 *
330
	 * @return float|null vertical center of finder pattern, or null if not found
331
	 * @noinspection DuplicatedCode
332
	 */
333
	private function crossCheckVertical(int $startI, int $centerJ, int $maxCount, int $originalStateCountTotal):?float{
334
		$maxI       = $this->matrix->getSize();
335
		$stateCount = $this->getCrossCheckStateCount();
336
337
		// Start counting up from center
338
		$i = $startI;
339
		while($i >= 0 && $this->matrix->check($centerJ, $i)){
340
			$stateCount[2]++;
341
			$i--;
342
		}
343
344
		if($i < 0){
345
			return null;
346
		}
347
348
		while($i >= 0 && !$this->matrix->check($centerJ, $i) && $stateCount[1] <= $maxCount){
349
			$stateCount[1]++;
350
			$i--;
351
		}
352
353
		// If already too many modules in this state or ran off the edge:
354
		if($i < 0 || $stateCount[1] > $maxCount){
355
			return null;
356
		}
357
358
		while($i >= 0 && $this->matrix->check($centerJ, $i) && $stateCount[0] <= $maxCount){
359
			$stateCount[0]++;
360
			$i--;
361
		}
362
363
		if($stateCount[0] > $maxCount){
364
			return null;
365
		}
366
367
		// Now also count down from center
368
		$i = ($startI + 1);
369
		while($i < $maxI && $this->matrix->check($centerJ, $i)){
370
			$stateCount[2]++;
371
			$i++;
372
		}
373
374
		if($i === $maxI){
375
			return null;
376
		}
377
378
		while($i < $maxI && !$this->matrix->check($centerJ, $i) && $stateCount[3] < $maxCount){
379
			$stateCount[3]++;
380
			$i++;
381
		}
382
383
		if($i === $maxI || $stateCount[3] >= $maxCount){
384
			return null;
385
		}
386
387
		while($i < $maxI && $this->matrix->check($centerJ, $i) && $stateCount[4] < $maxCount){
388
			$stateCount[4]++;
389
			$i++;
390
		}
391
392
		if($stateCount[4] >= $maxCount){
393
			return null;
394
		}
395
396
		// If we found a finder-pattern-like section, but its size is more than 40% different from
397
		// the original, assume it's a false positive
398
		$stateCountTotal = ($stateCount[0] + $stateCount[1] + $stateCount[2] + $stateCount[3] + $stateCount[4]);
399
400
		if((5 * abs($stateCountTotal - $originalStateCountTotal)) >= (2 * $originalStateCountTotal)){
401
			return null;
402
		}
403
404
		if(!$this->foundPatternCross($stateCount)){
405
			return null;
406
		}
407
408
		return $this->centerFromEnd($stateCount, $i);
409
	}
410
411
	/**
412
	 * Like #crossCheckVertical(int, int, int, int), and in fact is basically identical,
413
	 * except it reads horizontally instead of vertically. This is used to cross-cross
414
	 * check a vertical cross-check and locate the real center of the alignment pattern.
415
	 * @noinspection DuplicatedCode
416
	 */
417
	private function crossCheckHorizontal(int $startJ, int $centerI, int $maxCount, int $originalStateCountTotal):?float{
418
		$maxJ       = $this->matrix->getSize();
419
		$stateCount = $this->getCrossCheckStateCount();
420
421
		$j = $startJ;
422
		while($j >= 0 && $this->matrix->check($j, $centerI)){
423
			$stateCount[2]++;
424
			$j--;
425
		}
426
427
		if($j < 0){
428
			return null;
429
		}
430
431
		while($j >= 0 && !$this->matrix->check($j, $centerI) && $stateCount[1] <= $maxCount){
432
			$stateCount[1]++;
433
			$j--;
434
		}
435
436
		if($j < 0 || $stateCount[1] > $maxCount){
437
			return null;
438
		}
439
440
		while($j >= 0 && $this->matrix->check($j, $centerI) && $stateCount[0] <= $maxCount){
441
			$stateCount[0]++;
442
			$j--;
443
		}
444
445
		if($stateCount[0] > $maxCount){
446
			return null;
447
		}
448
449
		$j = ($startJ + 1);
450
		while($j < $maxJ && $this->matrix->check($j, $centerI)){
451
			$stateCount[2]++;
452
			$j++;
453
		}
454
455
		if($j === $maxJ){
456
			return null;
457
		}
458
459
		while($j < $maxJ && !$this->matrix->check($j, $centerI) && $stateCount[3] < $maxCount){
460
			$stateCount[3]++;
461
			$j++;
462
		}
463
464
		if($j === $maxJ || $stateCount[3] >= $maxCount){
465
			return null;
466
		}
467
468
		while($j < $maxJ && $this->matrix->check($j, $centerI) && $stateCount[4] < $maxCount){
469
			$stateCount[4]++;
470
			$j++;
471
		}
472
473
		if($stateCount[4] >= $maxCount){
474
			return null;
475
		}
476
477
		// If we found a finder-pattern-like section, but its size is significantly different from
478
		// the original, assume it's a false positive
479
		$stateCountTotal = ($stateCount[0] + $stateCount[1] + $stateCount[2] + $stateCount[3] + $stateCount[4]);
480
481
		if((5 * abs($stateCountTotal - $originalStateCountTotal)) >= $originalStateCountTotal){
482
			return null;
483
		}
484
485
		if(!$this->foundPatternCross($stateCount)){
486
			return null;
487
		}
488
489
		return $this->centerFromEnd($stateCount, $j);
490
	}
491
492
	/**
493
	 * This is called when a horizontal scan finds a possible alignment pattern. It will
494
	 * cross-check with a vertical scan, and if successful, will, ah, cross-cross-check
495
	 * with another horizontal scan. This is needed primarily to locate the real horizontal
496
	 * center of the pattern in cases of extreme skew.
497
	 * And then we cross-cross-cross check with another diagonal scan.
498
	 *
499
	 * If that succeeds the finder pattern location is added to a list that tracks
500
	 * the number of times each location has been nearly-matched as a finder pattern.
501
	 * Each additional find is more evidence that the location is in fact a finder
502
	 * pattern center
503
	 *
504
	 * @param int[] $stateCount reading state module counts from horizontal scan
505
	 * @param int   $i          row where finder pattern may be found
506
	 * @param int   $j          end of possible finder pattern in row
507
	 *
508
	 * @return bool if a finder pattern candidate was found this time
509
	 */
510
	private function handlePossibleCenter(array $stateCount, int $i, int $j):bool{
511
		$stateCountTotal = ($stateCount[0] + $stateCount[1] + $stateCount[2] + $stateCount[3] + $stateCount[4]);
512
		$centerJ         = $this->centerFromEnd($stateCount, $j);
513
		$centerI         = $this->crossCheckVertical($i, (int)$centerJ, $stateCount[2], $stateCountTotal);
514
515
		if($centerI !== null){
516
			// Re-cross check
517
			$centerJ = $this->crossCheckHorizontal((int)$centerJ, (int)$centerI, $stateCount[2], $stateCountTotal);
518
			if($centerJ !== null && ($this->crossCheckDiagonal((int)$centerI, (int)$centerJ))){
519
				$estimatedModuleSize = ($stateCountTotal / 7.0);
520
				$found               = false;
521
522
				// cautious (was in for fool in which $this->possibleCenters is updated)
523
				$count = count($this->possibleCenters);
524
525
				for($index = 0; $index < $count; $index++){
526
					$center = $this->possibleCenters[$index];
527
					// Look for about the same center and module size:
528
					if($center->aboutEquals($estimatedModuleSize, $centerI, $centerJ)){
529
						$this->possibleCenters[$index] = $center->combineEstimate($centerI, $centerJ, $estimatedModuleSize);
530
						$found                         = true;
531
						break;
532
					}
533
				}
534
535
				if(!$found){
536
					$point                   = new FinderPattern($centerJ, $centerI, $estimatedModuleSize);
537
					$this->possibleCenters[] = $point;
538
				}
539
540
				return true;
541
			}
542
		}
543
544
		return false;
545
	}
546
547
	/**
548
	 * @return int number of rows we could safely skip during scanning, based on the first
549
	 *         two finder patterns that have been located. In some cases their position will
550
	 *         allow us to infer that the third pattern must lie below a certain point farther
551
	 *         down in the image.
552
	 */
553
	private function findRowSkip():int{
554
		$max = count($this->possibleCenters);
555
556
		if($max <= 1){
557
			return 0;
558
		}
559
560
		$firstConfirmedCenter = null;
561
562
		foreach($this->possibleCenters as $center){
563
564
			if($center->getCount() >= self::CENTER_QUORUM){
565
566
				if($firstConfirmedCenter === null){
567
					$firstConfirmedCenter = $center;
568
				}
569
				else{
570
					// We have two confirmed centers
571
					// How far down can we skip before resuming looking for the next
572
					// pattern? In the worst case, only the difference between the
573
					// difference in the x / y coordinates of the two centers.
574
					// This is the case where you find top left last.
575
					$this->hasSkipped = true;
576
577
					return (int)((abs($firstConfirmedCenter->getX() - $center->getX()) -
0 ignored issues
show
The method getX() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

577
					return (int)((abs($firstConfirmedCenter->/** @scrutinizer ignore-call */ getX() - $center->getX()) -

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
578
					              abs($firstConfirmedCenter->getY() - $center->getY())) / 2);
579
				}
580
			}
581
		}
582
583
		return 0;
584
	}
585
586
	/**
587
	 * @return bool true if we have found at least 3 finder patterns that have been detected
588
	 *              at least #CENTER_QUORUM times each, and, the estimated module size of the
589
	 *              candidates is "pretty similar"
590
	 */
591
	private function haveMultiplyConfirmedCenters():bool{
592
		$confirmedCount  = 0;
593
		$totalModuleSize = 0.0;
594
		$max             = count($this->possibleCenters);
595
596
		foreach($this->possibleCenters as $pattern){
597
			if($pattern->getCount() >= self::CENTER_QUORUM){
598
				$confirmedCount++;
599
				$totalModuleSize += $pattern->getEstimatedModuleSize();
600
			}
601
		}
602
603
		if($confirmedCount < 3){
604
			return false;
605
		}
606
		// OK, we have at least 3 confirmed centers, but, it's possible that one is a "false positive"
607
		// and that we need to keep looking. We detect this by asking if the estimated module sizes
608
		// vary too much. We arbitrarily say that when the total deviation from average exceeds
609
		// 5% of the total module size estimates, it's too much.
610
		$average        = ($totalModuleSize / (float)$max);
611
		$totalDeviation = 0.0;
612
613
		foreach($this->possibleCenters as $pattern){
614
			$totalDeviation += abs($pattern->getEstimatedModuleSize() - $average);
615
		}
616
617
		return $totalDeviation <= (0.05 * $totalModuleSize);
618
	}
619
620
	/**
621
	 * @return \chillerlan\QRCode\Detector\FinderPattern[] the 3 best FinderPatterns from our list of candidates. The "best" are
622
	 *         those that have been detected at least #CENTER_QUORUM times, and whose module
623
	 *         size differs from the average among those patterns the least
624
	 * @throws \chillerlan\QRCode\Detector\QRCodeDetectorException if 3 such finder patterns do not exist
625
	 */
626
	private function selectBestPatterns():array{
627
		$startSize = count($this->possibleCenters);
628
629
		if($startSize < 3){
630
			throw new QRCodeDetectorException('could not find enough finder patterns');
631
		}
632
633
		usort(
634
			$this->possibleCenters,
635
			fn(FinderPattern $a, FinderPattern $b) => ($a->getEstimatedModuleSize() <=> $b->getEstimatedModuleSize())
636
		);
637
638
		$distortion   = PHP_FLOAT_MAX;
639
		$bestPatterns = [];
640
641
		for($i = 0; $i < ($startSize - 2); $i++){
642
			$fpi           = $this->possibleCenters[$i];
643
			$minModuleSize = $fpi->getEstimatedModuleSize();
644
645
			for($j = ($i + 1); $j < ($startSize - 1); $j++){
646
				$fpj      = $this->possibleCenters[$j];
647
				$squares0 = $fpi->getSquaredDistance($fpj);
648
649
				for($k = ($j + 1); $k < $startSize; $k++){
650
					$fpk           = $this->possibleCenters[$k];
651
					$maxModuleSize = $fpk->getEstimatedModuleSize();
652
653
					// module size is not similar
654
					if($maxModuleSize > ($minModuleSize * 1.4)){
655
						continue;
656
					}
657
658
					$a = $squares0;
659
					$b = $fpj->getSquaredDistance($fpk);
660
					$c = $fpi->getSquaredDistance($fpk);
661
662
					// sorts ascending - inlined
663
					if($a < $b){
664
						if($b > $c){
665
							if($a < $c){
666
								$temp = $b;
667
								$b    = $c;
668
								$c    = $temp;
669
							}
670
							else{
671
								$temp = $a;
672
								$a    = $c;
673
								$c    = $b;
674
								$b    = $temp;
675
							}
676
						}
677
					}
678
					else{
679
						if($b < $c){
680
							if($a < $c){
681
								$temp = $a;
682
								$a    = $b;
683
								$b    = $temp;
684
							}
685
							else{
686
								$temp = $a;
687
								$a    = $b;
688
								$b    = $c;
689
								$c    = $temp;
690
							}
691
						}
692
						else{
693
							$temp = $a;
694
							$a    = $c;
695
							$c    = $temp;
696
						}
697
					}
698
699
					// a^2 + b^2 = c^2 (Pythagorean theorem), and a = b (isosceles triangle).
700
					// Since any right triangle satisfies the formula c^2 - b^2 - a^2 = 0,
701
					// we need to check both two equal sides separately.
702
					// The value of |c^2 - 2 * b^2| + |c^2 - 2 * a^2| increases as dissimilarity
703
					// from isosceles right triangle.
704
					$d = (abs($c - 2 * $b) + abs($c - 2 * $a));
705
706
					if($d < $distortion){
707
						$distortion   = $d;
708
						$bestPatterns = [$fpi, $fpj, $fpk];
709
					}
710
				}
711
			}
712
		}
713
714
		if($distortion === PHP_FLOAT_MAX){
715
			throw new QRCodeDetectorException('finder patterns may be too distorted');
716
		}
717
718
		return $bestPatterns;
719
	}
720
721
	/**
722
	 * Orders an array of three ResultPoints in an order [A,B,C] such that AB is less than AC
723
	 * and BC is less than AC, and the angle between BC and BA is less than 180 degrees.
724
	 *
725
	 * @param \chillerlan\QRCode\Detector\FinderPattern[] $patterns array of three FinderPattern to order
726
	 *
727
	 * @return \chillerlan\QRCode\Detector\FinderPattern[]
728
	 */
729
	private function orderBestPatterns(array $patterns):array{
730
731
		// Find distances between pattern centers
732
		$zeroOneDistance = $patterns[0]->getDistance($patterns[1]);
733
		$oneTwoDistance  = $patterns[1]->getDistance($patterns[2]);
734
		$zeroTwoDistance = $patterns[0]->getDistance($patterns[2]);
735
736
		// Assume one closest to other two is B; A and C will just be guesses at first
737
		if($oneTwoDistance >= $zeroOneDistance && $oneTwoDistance >= $zeroTwoDistance){
738
			[$pointB, $pointA, $pointC] = $patterns;
739
		}
740
		elseif($zeroTwoDistance >= $oneTwoDistance && $zeroTwoDistance >= $zeroOneDistance){
741
			[$pointA, $pointB, $pointC] = $patterns;
742
		}
743
		else{
744
			[$pointA, $pointC, $pointB] = $patterns;
745
		}
746
747
		// Use cross product to figure out whether A and C are correct or flipped.
748
		// This asks whether BC x BA has a positive z component, which is the arrangement
749
		// we want for A, B, C. If it's negative, then we've got it flipped around and
750
		// should swap A and C.
751
		if($this->crossProductZ($pointA, $pointB, $pointC) < 0.0){
752
			$temp   = $pointA;
753
			$pointA = $pointC;
754
			$pointC = $temp;
755
		}
756
757
		return [$pointA, $pointB, $pointC];
758
	}
759
760
	/**
761
	 * Returns the z component of the cross product between vectors BC and BA.
762
	 */
763
	private function crossProductZ(FinderPattern $pointA, FinderPattern $pointB, FinderPattern $pointC):float{
764
		$bX = $pointB->getX();
765
		$bY = $pointB->getY();
766
767
		return ((($pointC->getX() - $bX) * ($pointA->getY() - $bY)) - (($pointC->getY() - $bY) * ($pointA->getX() - $bX)));
768
	}
769
770
}
771