-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathCurvePolygon.php
210 lines (184 loc) · 5.89 KB
/
CurvePolygon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
declare(strict_types=1);
namespace Brick\Geo;
use ArrayIterator;
use Brick\Geo\Attribute\NoProxy;
use Brick\Geo\Exception\CoordinateSystemException;
use Brick\Geo\Exception\EmptyGeometryException;
use Brick\Geo\Exception\NoSuchGeometryException;
use Brick\Geo\Projector\Projector;
use Override;
/**
* A CurvePolygon is a planar Surface defined by 1 exterior boundary and 0 or more interior boundaries.
*
* A CurvePolygon instance differs from a Polygon instance in that a CurvePolygon instance may contain
* the following circular arc segments: CircularString and CompoundCurve in addition to LineString.
*
* @template-implements \IteratorAggregate<int<0, max>, Curve>
* @final
*/
class CurvePolygon extends Surface implements \Countable, \IteratorAggregate
{
/**
* The rings that compose this CurvePolygon.
*
* The first one represents the exterior ring, and the
* (optional) other ones represent the interior rings (holes) of the CurvePolygon.
*
* An empty CurvePolygon contains no rings.
*
* @var list<Curve>
*/
protected array $rings = [];
/**
* The coordinate system of each of the rings must match the one of the CurvePolygon.
*
* @param CoordinateSystem $cs The coordinate system of the CurvePolygon.
* @param Curve ...$rings The rings that compose the CurvePolygon.
*
* @throws CoordinateSystemException If different coordinate systems are used.
*/
public function __construct(CoordinateSystem $cs, Curve ...$rings)
{
parent::__construct($cs, ! $rings);
if (! $rings) {
return;
}
CoordinateSystem::check($this, ...$rings);
$this->rings = array_values($rings);
}
/**
* Creates a non-empty CurvePolygon composed of the given rings.
*
* @param Curve $exteriorRing The exterior ring.
* @param Curve ...$interiorRings The interior rings, if any.
*
* @throws CoordinateSystemException If the rings use different coordinate systems.
*/
public static function of(Curve $exteriorRing, Curve ...$interiorRings) : CurvePolygon
{
return new static($exteriorRing->coordinateSystem(), $exteriorRing, ...$interiorRings);
}
/**
* Returns the exterior ring of this CurvePolygon.
*
* @throws EmptyGeometryException
*/
public function exteriorRing() : Curve
{
if ($this->isEmpty) {
throw new EmptyGeometryException('An empty CurvePolygon has no exterior ring.');
}
return $this->rings[0];
}
/**
* Returns the number of interior rings in this CurvePolygon.
*/
public function numInteriorRings() : int
{
if ($this->isEmpty) {
return 0;
}
return count($this->rings) - 1;
}
/**
* Returns the specified interior ring N in this CurvePolygon.
*
* @param int $n The ring number, 1-based.
*
* @throws NoSuchGeometryException If there is no interior ring at this index.
*/
public function interiorRingN(int $n) : Curve
{
if ($n === 0 || ! isset($this->rings[$n])) {
throw new NoSuchGeometryException('There is no interior ring in this CurvePolygon at index ' . $n);
}
return $this->rings[$n];
}
/**
* Returns the interior rings in this CurvePolygon.
*
* @return list<Curve>
*/
public function interiorRings() : array
{
return array_slice($this->rings, 1);
}
#[NoProxy, Override]
public function geometryType() : string
{
return 'CurvePolygon';
}
#[NoProxy, Override]
public function geometryTypeBinary() : int
{
return Geometry::CURVEPOLYGON;
}
#[Override]
public function getBoundingBox() : BoundingBox
{
return array_reduce(
$this->rings,
fn (BoundingBox $boundingBox, Curve $ring) => $boundingBox->extendedWithBoundingBox($ring->getBoundingBox()),
BoundingBox::new(),
);
}
#[Override]
public function toArray() : array
{
return array_map(
fn (Curve $ring) => $ring->toArray(),
$this->rings,
);
}
#[Override]
public function project(Projector $projector): CurvePolygon
{
return new CurvePolygon(
$projector->getTargetCoordinateSystem($this->coordinateSystem),
...array_map(
fn (Curve $ring) => $ring->project($projector),
$this->rings,
),
);
}
/**
* Returns the number of rings (exterior + interior) in this CurvePolygon.
*/
#[Override]
public function count() : int
{
return count($this->rings);
}
/**
* Returns an iterator for the rings (exterior + interior) in this CurvePolygon.
*
* @return ArrayIterator<int<0, max>, Curve>
*/
#[Override]
public function getIterator() : ArrayIterator
{
return new ArrayIterator($this->rings);
}
/**
* Returns a copy of this CurvePolygon, with the exterior ring replaced with the given one.
*/
public function withExteriorRing(Curve $exteriorRing) : CurvePolygon
{
return new CurvePolygon($this->coordinateSystem, $exteriorRing, ...$this->interiorRings());
}
/**
* Returns a copy of this CurvePolygon, with the interior rings replaced with the given ones.
*/
public function withInteriorRings(Curve ...$interiorRings) : CurvePolygon
{
return new CurvePolygon($this->coordinateSystem, $this->exteriorRing(), ...$interiorRings);
}
/**
* Returns a copy of this CurvePolygon, with the given interior rings added.
*/
public function withAddedInteriorRings(Curve ...$interiorRings) : CurvePolygon
{
return new CurvePolygon($this->coordinateSystem, $this->exteriorRing(), ...$this->interiorRings(), ...$interiorRings);
}
}