Skip to content

Commit fa64177

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

File tree

5 files changed

+87
-180
lines changed

5 files changed

+87
-180
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/Geometry/BBoxUtils.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public static function generateBBoxFromPolygon(?Polygon $polygon): ?BBox
2020
}
2121

2222
return new BBox(
23-
PolygonUtils::getMinXCoordinate($polygon),
24-
PolygonUtils::getMaxXCoordinate($polygon),
25-
PolygonUtils::getMinYCoordinate($polygon),
26-
PolygonUtils::getMaxYCoordinate($polygon),
23+
$polygon->getMinX(),
24+
$polygon->getMaxX(),
25+
$polygon->getMinY(),
26+
$polygon->getMaxY(),
2727
);
2828
}
2929

@@ -47,10 +47,10 @@ public static function generateBBoxFromPolygons(array $polygons): ?BBox
4747
}
4848

4949
return new BBox(
50-
PolygonUtils::getMinXCoordinate($merged),
51-
PolygonUtils::getMaxXCoordinate($merged),
52-
PolygonUtils::getMinYCoordinate($merged),
53-
PolygonUtils::getMaxYCoordinate($merged),
50+
$merged->getMinX(),
51+
$merged->getMaxX(),
52+
$merged->getMinY(),
53+
$merged->getMaxY(),
5454
);
5555
}
5656

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
*

src/Geometry/PolygonUtils.php

Lines changed: 6 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -31,106 +31,6 @@ public static function getCentroid(array $vertices): Point
3131
return new Point($xSum / $verticesSum, $ySum / $verticesSum);
3232
}
3333

34-
/**
35-
* Retrieves the minimum y coordinate of a Polygon.
36-
*
37-
* @param Polygon $polygon Polygon to get the minimum y coordinate of.
38-
* @return float
39-
* @throws MindeeGeometryException Throws if a minimum y-axis value cannot
40-
* be found, e.g. if the polygon is empty.
41-
*/
42-
public static function getMinYCoordinate(Polygon $polygon): float
43-
{
44-
$min = null;
45-
foreach ($polygon->getCoordinates() as $point) {
46-
if (!isset($min) || $min > $point->getY()) {
47-
$min = $point->getY();
48-
}
49-
}
50-
if (!isset($min)) {
51-
throw new MindeeGeometryException(
52-
'The provided polygon seems to be empty, or the Y coordinates of each point are invalid.'
53-
);
54-
}
55-
56-
return $min;
57-
}
58-
59-
/**
60-
* Retrieves the minimum x coordinate of a Polygon.
61-
*
62-
* @param Polygon $polygon Polygon to get the minimum y coordinate of.
63-
* @return float
64-
* @throws MindeeGeometryException Throws if a minimum x-axis value cannot be
65-
* found, e.g. if the polygon is empty.
66-
*/
67-
public static function getMinXCoordinate(Polygon $polygon): float
68-
{
69-
$min = null;
70-
foreach ($polygon->getCoordinates() as $point) {
71-
if (!isset($min) || $min > $point->getX()) {
72-
$min = $point->getX();
73-
}
74-
}
75-
if (!isset($min)) {
76-
throw new MindeeGeometryException(
77-
'The provided polygon seems to be empty, or the X coordinates of each point are invalid.'
78-
);
79-
}
80-
81-
return $min;
82-
}
83-
84-
/**
85-
* Retrieves the maximum y coordinate of a Polygon.
86-
*
87-
* @param Polygon $polygon Polygon to get the minimum y coordinate of.
88-
* @return float
89-
* @throws MindeeGeometryException Throws if a maximum y-axis value cannot be
90-
* found, e.g. if the polygon is empty.
91-
*/
92-
public static function getMaxYCoordinate(Polygon $polygon): float
93-
{
94-
$min = null;
95-
foreach ($polygon->getCoordinates() as $point) {
96-
if (!isset($min) || $min < $point->getY()) {
97-
$min = $point->getY();
98-
}
99-
}
100-
if (!isset($min)) {
101-
throw new MindeeGeometryException(
102-
'The provided polygon seems to be empty, or the Y coordinates of each point are invalid.'
103-
);
104-
}
105-
106-
return $min;
107-
}
108-
109-
/**
110-
* Retrieves the maximum x coordinate of a Polygon.
111-
*
112-
* @param Polygon $polygon Polygon to get the minimum y coordinate of.
113-
* @return float
114-
* @throws MindeeGeometryException Throws if a maximum x-axis value cannot be
115-
* found, e.g. if the polygon is empty.
116-
*/
117-
public static function getMaxXCoordinate(Polygon $polygon): float
118-
{
119-
$min = null;
120-
foreach ($polygon->getCoordinates() as $point) {
121-
if (!isset($min) || $min < $point->getX()) {
122-
$min = $point->getX();
123-
}
124-
}
125-
if (!isset($min)) {
126-
throw new MindeeGeometryException(
127-
'The provided polygon seems to be empty, or the X coordinates of each point are invalid.'
128-
);
129-
}
130-
131-
return $min;
132-
}
133-
13434
/**
13535
* Compares two polygons on the Y axis. Returns a sort-compliant result (0;-1;1).
13636
*
@@ -140,11 +40,10 @@ public static function getMaxXCoordinate(Polygon $polygon): float
14040
*/
14141
public static function compareOnY(Polygon $polygon1, Polygon $polygon2): int
14242
{
143-
$sort = self::getMinYCoordinate($polygon1) - self::getMinYCoordinate($polygon2);
43+
$sort = ($polygon1->getMinY() - $polygon2->getMinY());
14444
if ($sort == 0) {
14545
return 0;
14646
}
147-
14847
return $sort < 0 ? -1 : 1;
14948
}
15049

@@ -182,10 +81,10 @@ public static function createBoundingBoxFrom(?Polygon $base, ?Polygon $target =
18281
{
18382
$merged = PolygonUtils::merge($base, $target);
18483

185-
$topLeft = new Point(self::getMinXCoordinate($merged), self::getMinYCoordinate($merged));
186-
$topRight = new Point(self::getMaxXCoordinate($merged), self::getMinYCoordinate($merged));
187-
$bottomRight = new Point(self::getMaxXCoordinate($merged), self::getMaxYCoordinate($merged));
188-
$bottomLeft = new Point(self::getMinXCoordinate($merged), self::getMaxYCoordinate($merged));
84+
$topLeft = new Point($merged->getMinX(), $merged->getMinY());
85+
$topRight = new Point($merged->getMaxX(), $merged->getMinY());
86+
$bottomRight = new Point($merged->getMaxX(), $merged->getMaxY());
87+
$bottomLeft = new Point($merged->getMinX(), $merged->getMaxY());
18988

19089
return new Polygon([
19190
$topLeft,
@@ -207,29 +106,7 @@ public static function quadrilateralFromPrediction(array $prediction): Polygon
207106
if (count($prediction) != 4) {
208107
throw new MindeeGeometryException('Prediction must have exactly 4 points.');
209108
}
210-
211-
return new Polygon([
212-
new Point($prediction[0][0], $prediction[0][1]),
213-
new Point($prediction[1][0], $prediction[1][1]),
214-
new Point($prediction[2][0], $prediction[2][1]),
215-
new Point($prediction[3][0], $prediction[3][1]),
216-
]);
217-
}
218-
219-
/**
220-
* Generates a Polygon from a given prediction.
221-
*
222-
* @deprecated construct a new Polygon() instead.
223-
* @param array $prediction Raw prediction array.
224-
* @return Polygon
225-
*/
226-
public static function polygonFromPrediction(array $prediction): Polygon
227-
{
228-
$points = [];
229-
foreach ($prediction as $point) {
230-
$points[] = new Point($point[0], $point[1]);
231-
}
232-
return new Polygon($points);
109+
return new Polygon($prediction);
233110
}
234111

235112
/**
@@ -245,20 +122,6 @@ public static function isPointInX(Point $point, float $minX, float $maxX): bool
245122
return $point->getX() >= $minX && $point->getX() <= $maxX;
246123
}
247124

248-
/**
249-
* Checks whether a point is in a polygon's x-axis range.
250-
*
251-
* @param Point $point Point to check.
252-
* @param Polygon $polygon Polygon.
253-
* @return boolean
254-
*/
255-
public static function isPointInPolygonX(Point $point, Polygon $polygon): bool
256-
{
257-
$minX = MinMaxUtils::getMinMaxX($polygon->getCoordinates())->getMin();
258-
$maxX = MinMaxUtils::getMinMaxX($polygon->getCoordinates())->getMax();
259-
return self::isPointInX($point, $minX, $maxX);
260-
}
261-
262125
/**
263126
* Checks whether a point is located within a coordinate range on the y-axis.
264127
*
@@ -271,18 +134,4 @@ public static function isPointInY(Point $point, float $minY, float $maxY): bool
271134
{
272135
return $point->getY() >= $minY && $point->getY() <= $maxY;
273136
}
274-
275-
/**
276-
* Checks whether a point is in a polygon's y-axis range.
277-
*
278-
* @param Point $point Point to check.
279-
* @param Polygon $polygon Polygon.
280-
* @return boolean
281-
*/
282-
public static function isPointInPolygonY(Point $point, Polygon $polygon): bool
283-
{
284-
$minY = MinMaxUtils::getMinMaxY($polygon->getCoordinates())->getMin();
285-
$maxY = MinMaxUtils::getMinMaxY($polygon->getCoordinates())->getMax();
286-
return self::isPointInY($point, $minY, $maxY);
287-
}
288137
}

tests/Geometry/PolygonUtilsTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,32 @@ public function testGivenAValidPolygonMustGetTheValidCentroid()
3636

3737
public function testGivenAValidPolygonMustGetTheMinX()
3838
{
39-
$this->assertEquals(0.123, PolygonUtils::getMinXCoordinate($this->polygonWhichIsNotRectangle));
39+
$this->assertEquals(0.123, $this->polygonWhichIsNotRectangle->getMinX());
4040
}
4141

4242
public function testGivenAValidPolygonMustGetTheMinY()
4343
{
44-
$this->assertEquals(0.53, PolygonUtils::getMinYCoordinate($this->polygonWhichIsNotRectangle));
44+
$this->assertEquals(0.53, $this->polygonWhichIsNotRectangle->getMinY());
4545
}
4646

4747
public function testGivenAValidPolygonMustGetTheMaxX()
4848
{
49-
$this->assertEquals(0.175, PolygonUtils::getMaxXCoordinate($this->polygonWhichIsNotRectangle));
49+
$this->assertEquals(0.175, $this->polygonWhichIsNotRectangle->getMaxX());
5050
}
5151

5252
public function testGivenAValidPolygonMustGetTheMaxY()
5353
{
54-
$this->assertEquals(0.546, PolygonUtils::getMaxYCoordinate($this->polygonWhichIsNotRectangle));
54+
$this->assertEquals(0.546, $this->polygonWhichIsNotRectangle->getMaxY());
5555
}
5656

5757
public function testMergePolygonsWithTwoNotNullMustGetAValidPolygon()
5858
{
5959
$mergedPolygon = PolygonUtils::merge($this->polygon1, $this->polygon2);
6060

61-
$this->assertEquals(0.442, PolygonUtils::getMinYCoordinate($mergedPolygon));
62-
$this->assertEquals(0.081, PolygonUtils::getMinXCoordinate($mergedPolygon));
63-
$this->assertEquals(0.451, PolygonUtils::getMaxYCoordinate($mergedPolygon));
64-
$this->assertEquals(0.26, PolygonUtils::getMaxXCoordinate($mergedPolygon));
61+
$this->assertEquals(0.442, $mergedPolygon->getMinY());
62+
$this->assertEquals(0.081, $mergedPolygon->getMinX());
63+
$this->assertEquals(0.451, $mergedPolygon->getMaxY());;
64+
$this->assertEquals(0.26, $mergedPolygon->getMaxX());
6565
}
6666

6767
public function testMergeWithNullPolygonMustThrow()
@@ -74,9 +74,9 @@ public function testMergeWith1PolygonAndANullPolygonMustGetPolygon()
7474
{
7575
$mergedPolygon = PolygonUtils::merge($this->polygon1, null);
7676

77-
$this->assertEquals(0.442, PolygonUtils::getMinYCoordinate($mergedPolygon));
78-
$this->assertEquals(0.081, PolygonUtils::getMinXCoordinate($mergedPolygon));
79-
$this->assertEquals(0.451, PolygonUtils::getMaxYCoordinate($mergedPolygon));
80-
$this->assertEquals(0.15, PolygonUtils::getMaxXCoordinate($mergedPolygon));
77+
$this->assertEquals(0.442, $mergedPolygon->getMinY());
78+
$this->assertEquals(0.081, $mergedPolygon->getMinX());
79+
$this->assertEquals(0.451, $mergedPolygon->getMaxY());
80+
$this->assertEquals(0.15, $mergedPolygon->getMaxX());
8181
}
8282
}

0 commit comments

Comments
 (0)