Skip to content

Commit f4f82ac

Browse files
committed
more geometry stuff
1 parent 94e1f71 commit f4f82ac

File tree

7 files changed

+114
-214
lines changed

7 files changed

+114
-214
lines changed

docs/code_samples/custom_v1.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//
44
// DEPRECATED API
5-
// Support removed in version 2.0.
5+
// Support removed in version 2.0 of the Mindee PHP library.
66
// Use only with version 1.x of the library.
77
//
88

src/Extraction/ExtractedImage.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,12 @@ public function asInputSource(): BytesInput
8686
*/
8787
private function getEncodedImageFormat(string $saveFormat): string
8888
{
89-
switch (strtolower($saveFormat)) {
90-
case 'png':
91-
return 'png';
92-
case 'bmp':
93-
return 'bmp';
94-
case 'gif':
95-
return 'gif';
96-
case 'webp':
97-
return 'webp';
98-
default:
99-
return 'jpeg';
100-
}
89+
return match (strtolower($saveFormat)) {
90+
'png' => 'png',
91+
'bmp', => 'bmp',
92+
'gif' => 'gif',
93+
'webp' => 'webp',
94+
default => 'jpeg',
95+
};
10196
}
10297
}

src/Extraction/PdfExtractor.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ public function extractInvoices($pageIndexes, bool $strict = false): array
180180
$previousConfidence = $confidence;
181181
$i++;
182182
}
183-
184-
185183
return $this->extractSubDocuments($correctPageIndexes);
186184
}
187185

src/Geometry/BBoxUtils.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ public static function generateBBoxFromPolygon(?Polygon $polygon): ?BBox
1818
if (!$polygon || !$polygon->getCoordinates()) {
1919
return null;
2020
}
21-
2221
return new BBox(
23-
PolygonUtils::getMinXCoordinate($polygon),
24-
PolygonUtils::getMaxXCoordinate($polygon),
25-
PolygonUtils::getMinYCoordinate($polygon),
26-
PolygonUtils::getMaxYCoordinate($polygon),
22+
$polygon->getMinX(),
23+
$polygon->getMaxX(),
24+
$polygon->getMinY(),
25+
$polygon->getMaxY(),
2726
);
2827
}
2928

@@ -38,19 +37,17 @@ public static function generateBBoxFromPolygons(array $polygons): ?BBox
3837
if (!$polygons) {
3938
return null;
4039
}
41-
4240
$merged = $polygons[0];
4341
foreach ($polygons as $polygon) {
44-
if ($merged !== $polygon) {
42+
if ($polygon && $merged !== $polygon) {
4543
$merged = PolygonUtils::merge($merged, $polygon);
4644
}
4745
}
48-
4946
return new BBox(
50-
PolygonUtils::getMinXCoordinate($merged),
51-
PolygonUtils::getMaxXCoordinate($merged),
52-
PolygonUtils::getMinYCoordinate($merged),
53-
PolygonUtils::getMaxYCoordinate($merged),
47+
$merged->getMinX(),
48+
$merged->getMaxX(),
49+
$merged->getMinY(),
50+
$merged->getMaxY(),
5451
);
5552
}
5653

@@ -83,7 +80,6 @@ public static function mergeBBoxes(array $bboxes): ?BBox
8380
$maxY = $bbox->getMaxY();
8481
}
8582
}
86-
8783
return new BBox((float)$minX, (float)$maxX, (float)$minY, (float)$maxY);
8884
}
8985
}

src/Geometry/Polygon.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Mindee\Geometry;
44

5+
use Mindee\Error\MindeeGeometryException;
6+
57
/**
68
* Polygon represented as a set of coordinates (vertices/points).
79
*/
@@ -12,6 +14,16 @@ class Polygon
1214
*/
1315
public ?array $coordinates;
1416

17+
/**
18+
* @var MinMax Min and max Y values of the polygon.
19+
*/
20+
private MinMax $minMaxY;
21+
22+
/**
23+
* @var MinMax Min and max X values of the polygon.
24+
*/
25+
private MinMax $minMaxX;
26+
1527
/**
1628
* @param array|null $coordinates Coordinates of the polygon as a set of Points.
1729
*/
@@ -44,7 +56,10 @@ public function getCentroid(): Point
4456
*/
4557
public function getMinMaxY(): MinMax
4658
{
47-
return MinMaxUtils::getMinMaxY($this->coordinates);
59+
if (!isset($this->minMaxY)) {
60+
$this->minMaxY = MinMaxUtils::getMinMaxY($this->coordinates);
61+
}
62+
return $this->minMaxY;
4863
}
4964

5065
/**
@@ -54,7 +69,10 @@ public function getMinMaxY(): MinMax
5469
*/
5570
public function getMinMaxX(): MinMax
5671
{
57-
return MinMaxUtils::getMinMaxX($this->coordinates);
72+
if (!isset($this->minMaxX)) {
73+
$this->minMaxX = MinMaxUtils::getMinMaxX($this->coordinates);
74+
}
75+
return $this->minMaxX;
5876
}
5977

6078
/**
@@ -81,6 +99,46 @@ public function isPointInX(Point $point): bool
8199
return PolygonUtils::isPointInX($point, $minMax->getMin(), $minMax->getMax());
82100
}
83101

102+
/**
103+
* Retrieves the minimum X coordinate.
104+
*
105+
* @return float
106+
*/
107+
public function getMinX(): float
108+
{
109+
return $this->getMinMaxX()->getMin();
110+
}
111+
112+
/**
113+
* Retrieves the maximum X coordinate.
114+
*
115+
* @return float
116+
*/
117+
public function getMaxX(): float
118+
{
119+
return $this->getMinMaxX()->getMax();
120+
}
121+
122+
/**
123+
* Retrieves the minimum Y coordinate.
124+
*
125+
* @return float
126+
*/
127+
public function getMinY(): float
128+
{
129+
return $this->getMinMaxY()->getMin();
130+
}
131+
132+
/**
133+
* Retrieves the maximum Y coordinate.
134+
*
135+
* @return float
136+
*/
137+
public function getMaxY(): float
138+
{
139+
return $this->getMinMaxY()->getMax();
140+
}
141+
84142
/**
85143
* Checks whether the Polygon has coordinates.
86144
*

0 commit comments

Comments
 (0)