:octocat: de-static PerspectiveTransform

This commit is contained in:
codemasher
2021-06-04 14:27:00 +02:00
parent b3dfd0f5a2
commit d60a4e0f55
2 changed files with 32 additions and 31 deletions
+1 -1
View File
@@ -343,7 +343,7 @@ final class Detector{
$sourceBottomRightY = $dimMinusThree;
}
return PerspectiveTransform::quadrilateralToQuadrilateral(
return (new PerspectiveTransform)->quadrilateralToQuadrilateral(
3.5, 3.5,
$dimMinusThree, 3.5,
$sourceBottomRightX, $sourceBottomRightY,
+31 -30
View File
@@ -32,11 +32,11 @@ final class PerspectiveTransform{
private float $a32;
private float $a33;
private function __construct(
private function set(
float $a11, float $a21, float $a31,
float $a12, float $a22, float $a32,
float $a13, float $a23, float $a33
){
):PerspectiveTransform{
$this->a11 = $a11;
$this->a12 = $a12;
$this->a13 = $a13;
@@ -46,30 +46,32 @@ final class PerspectiveTransform{
$this->a31 = $a31;
$this->a32 = $a32;
$this->a33 = $a33;
return $this;
}
public static function quadrilateralToQuadrilateral(
public function quadrilateralToQuadrilateral(
float $x0, float $y0, float $x1, float $y1, float $x2, float $y2, float $x3, float $y3,
float $x0p, float $y0p, float $x1p, float $y1p, float $x2p, float $y2p, float $x3p, float $y3p
):PerspectiveTransform{
$qToS = self::quadrilateralToSquare($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3);
$sToQ = self::squareToQuadrilateral($x0p, $y0p, $x1p, $y1p, $x2p, $y2p, $x3p, $y3p);
return $sToQ->times($qToS);
return (new self)
->squareToQuadrilateral($x0p, $y0p, $x1p, $y1p, $x2p, $y2p, $x3p, $y3p)
->times($this->quadrilateralToSquare($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3));
}
public static function quadrilateralToSquare(
private function quadrilateralToSquare(
float $x0, float $y0, float $x1, float $y1,
float $x2, float $y2, float $x3, float $y3
):PerspectiveTransform{
// Here, the adjoint serves as the inverse:
return self::squareToQuadrilateral($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3)->buildAdjoint();
return $this
->squareToQuadrilateral($x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3)
->buildAdjoint();
}
public function buildAdjoint():PerspectiveTransform{
private function buildAdjoint():PerspectiveTransform{
// Adjoint is the transpose of the cofactor matrix:
return new self(
return $this->set(
$this->a22 * $this->a33 - $this->a23 * $this->a32,
$this->a23 * $this->a31 - $this->a21 * $this->a33,
$this->a21 * $this->a32 - $this->a22 * $this->a31,
@@ -82,7 +84,7 @@ final class PerspectiveTransform{
);
}
public static function squareToQuadrilateral(
private function squareToQuadrilateral(
float $x0, float $y0, float $x1, float $y1,
float $x2, float $y2, float $x3, float $y3
):PerspectiveTransform{
@@ -91,27 +93,26 @@ final class PerspectiveTransform{
if($dx3 === 0.0 && $dy3 === 0.0){
// Affine
return new self($x1 - $x0, $x2 - $x1, $x0, $y1 - $y0, $y2 - $y1, $y0, 0.0, 0.0, 1.0);
return $this->set($x1 - $x0, $x2 - $x1, $x0, $y1 - $y0, $y2 - $y1, $y0, 0.0, 0.0, 1.0);
}
else{
$dx1 = $x1 - $x2;
$dx2 = $x3 - $x2;
$dy1 = $y1 - $y2;
$dy2 = $y3 - $y2;
$denominator = $dx1 * $dy2 - $dx2 * $dy1;
$a13 = ($dx3 * $dy2 - $dx2 * $dy3) / $denominator;
$a23 = ($dx1 * $dy3 - $dx3 * $dy1) / $denominator;
return new self(
$x1 - $x0 + $a13 * $x1, $x3 - $x0 + $a23 * $x3, $x0,
$y1 - $y0 + $a13 * $y1, $y3 - $y0 + $a23 * $y3, $y0,
$a13, $a23, 1.0
);
}
$dx1 = $x1 - $x2;
$dx2 = $x3 - $x2;
$dy1 = $y1 - $y2;
$dy2 = $y3 - $y2;
$denominator = $dx1 * $dy2 - $dx2 * $dy1;
$a13 = ($dx3 * $dy2 - $dx2 * $dy3) / $denominator;
$a23 = ($dx1 * $dy3 - $dx3 * $dy1) / $denominator;
return $this->set(
$x1 - $x0 + $a13 * $x1, $x3 - $x0 + $a23 * $x3, $x0,
$y1 - $y0 + $a13 * $y1, $y3 - $y0 + $a23 * $y3, $y0,
$a13, $a23, 1.0
);
}
public function times(PerspectiveTransform $other):PerspectiveTransform{
return new self(
private function times(PerspectiveTransform $other):PerspectiveTransform{
return $this->set(
$this->a11 * $other->a11 + $this->a21 * $other->a12 + $this->a31 * $other->a13,
$this->a11 * $other->a21 + $this->a21 * $other->a22 + $this->a31 * $other->a23,
$this->a11 * $other->a31 + $this->a21 * $other->a32 + $this->a31 * $other->a33,