• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

brick / geo / 13753277563

09 Mar 2025 10:43PM UTC coverage: 49.787% (+2.5%) from 47.295%
13753277563

push

github

BenMorel
Prepare for release

1749 of 3513 relevant lines covered (49.79%)

975.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.76
/src/PolyhedralSurface.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use ArrayIterator;
8
use Brick\Geo\Attribute\NoProxy;
9
use Brick\Geo\Exception\CoordinateSystemException;
10
use Brick\Geo\Exception\NoSuchGeometryException;
11
use Brick\Geo\Exception\UnexpectedGeometryException;
12
use Brick\Geo\Projector\Projector;
13
use Override;
14

15
/**
16
 * A PolyhedralSurface is a contiguous collection of polygons, which share common boundary segments.
17
 *
18
 * For each pair of polygons that "touch", the common boundary shall be expressible as a finite collection
19
 * of LineStrings. Each such LineString shall be part of the boundary of at most 2 Polygon patches.
20
 *
21
 * For any two polygons that share a common boundary, the "top" of the polygon shall be consistent. This means
22
 * that when two linear rings from these two Polygons traverse the common boundary segment, they do so in
23
 * opposite directions. Since the Polyhedral surface is contiguous, all polygons will be thus consistently oriented.
24
 * This means that a non-oriented surface (such as Möbius band) shall not have single surface representations.
25
 * They may be represented by a MultiSurface.
26
 *
27
 * If each such LineString is the boundary of exactly 2 Polygon patches, then the PolyhedralSurface is a simple,
28
 * closed polyhedron and is topologically isomorphic to the surface of a sphere. By the Jordan Surface Theorem
29
 * (Jordan’s Theorem for 2-spheres), such polyhedrons enclose a solid topologically isomorphic to the interior of a
30
 * sphere; the ball. In this case, the "top" of the surface will either point inward or outward of the enclosed
31
 * finite solid. If outward, the surface is the exterior boundary of the enclosed surface. If inward, the surface
32
 * is the interior of the infinite complement of the enclosed solid. A Ball with some number of voids (holes) inside
33
 * can thus be presented as one exterior boundary shell, and some number in interior boundary shells.
34
 *
35
 * @template T of Polygon
36
 * @template-implements \IteratorAggregate<int<0, max>, T>
37
 */
38
class PolyhedralSurface extends Surface implements \Countable, \IteratorAggregate
39
{
40
    /**
41
     * The polygons that compose this PolyhedralSurface.
42
     *
43
     * An empty PolyhedralSurface contains no polygons.
44
     *
45
     * @var list<T>
46
     */
47
    protected array $patches = [];
48

49
    /**
50
     * The coordinate system of each of the patches must match the one of the PolyhedralSurface.
51
     *
52
     * @param CoordinateSystem $cs         The coordinate system of the PolyhedralSurface.
53
     * @param T                ...$patches The patches that compose the PolyhedralSurface.
54
     *
55
     * @throws CoordinateSystemException If different coordinate systems are used.
56
     */
57
    public function __construct(CoordinateSystem $cs, Polygon ...$patches)
58
    {
59
        parent::__construct($cs, ! $patches);
2,870✔
60

61
        if (! $patches) {
2,870✔
62
            return;
1,386✔
63
        }
64

65
        CoordinateSystem::check($this, ...$patches);
1,512✔
66

67
        $patchType = $this->patchType();
1,512✔
68

69
        foreach ($patches as $patch) {
1,512✔
70
            /**
71
             * @psalm-suppress DocblockTypeContradiction We do want to enforce this in code, as not everyone uses static analysis!
72
             * @psalm-suppress MixedArgument It looks like due to this check, Psalm considers that $geometry no longer has a type.
73
             */
74
            if (! $patch instanceof $patchType) {
1,512✔
75
                throw new UnexpectedGeometryException(sprintf(
×
76
                    '%s expects instance of %s, instance of %s given.',
×
77
                    static::class,
×
78
                    $patchType,
×
79
                    $patch::class
×
80
                ));
×
81
            }
82
        }
83

84
        $this->patches = array_values($patches);
1,512✔
85
    }
86

87
    /**
88
     * Creates a non-empty PolyhedralSurface composed of the given patches.
89
     *
90
     * @psalm-suppress UnsafeInstantiation
91
     *
92
     * @param Polygon    $patch1 The first patch.
93
     * @param Polygon ...$patchN The subsequent patches, if any.
94
     *
95
     * @throws CoordinateSystemException If the patches use different coordinate systems.
96
     */
97
    public static function of(Polygon $patch1, Polygon ...$patchN) : PolyhedralSurface
98
    {
99
        return new static($patch1->coordinateSystem(), $patch1, ...$patchN);
7✔
100
    }
101

102
    /**
103
     * Returns the FQCN of the contained patch type.
104
     *
105
     * @return class-string<T>
106
     */
107
    protected function patchType() : string
108
    {
109
        return Polygon::class;
896✔
110
    }
111

112
    public function numPatches() : int
113
    {
114
        return count($this->patches);
56✔
115
    }
116

117
    /**
118
     * Returns the specified patch N in this PolyhedralSurface.
119
     *
120
     * @param int $n The patch number, 1-based.
121
     *
122
     * @return T
123
     *
124
     * @throws NoSuchGeometryException If there is no patch at this index.
125
     */
126
    public function patchN(int $n) : Polygon
127
    {
128
        if (! isset($this->patches[$n - 1])) {
343✔
129
            throw new NoSuchGeometryException('There is no patch in this PolyhedralSurface at index ' . $n);
224✔
130
        }
131

132
        return $this->patches[$n - 1];
119✔
133
    }
134

135
    /**
136
     * Returns the patches that compose this PolyhedralSurface.
137
     *
138
     * @return list<T>
139
     */
140
    public function patches() : array
141
    {
142
        return $this->patches;
×
143
    }
144

145
    #[NoProxy, Override]
146
    public function geometryType() : string
147
    {
148
        return 'PolyhedralSurface';
686✔
149
    }
150

151
    #[NoProxy, Override]
152
    public function geometryTypeBinary() : int
153
    {
154
        return Geometry::POLYHEDRALSURFACE;
343✔
155
    }
156

157
    #[Override]
158
    public function getBoundingBox() : BoundingBox
159
    {
160
        return array_reduce(
×
161
            $this->patches,
×
162
            fn (BoundingBox $boundingBox, Polygon $patch) => $boundingBox->extendedWithBoundingBox($patch->getBoundingBox()),
×
163
            BoundingBox::new(),
×
164
        );
×
165
    }
166

167
    /**
168
     * @return list<list<list<list<float>>>>
169
     */
170
    #[Override]
171
    public function toArray() : array
172
    {
173
        return array_map(
448✔
174
            fn (Polygon $patch) => $patch->toArray(),
448✔
175
            $this->patches,
448✔
176
        );
448✔
177
    }
178

179
    #[Override]
180
    public function project(Projector $projector): PolyhedralSurface
181
    {
182
        return new PolyhedralSurface(
×
183
            $projector->getTargetCoordinateSystem($this->coordinateSystem),
×
184
            ...array_map(
×
185
                fn (Polygon $patch) => $patch->project($projector),
×
186
                $this->patches,
×
187
            ),
×
188
        );
×
189
    }
190

191
    /**
192
     * Returns the number of patches in this PolyhedralSurface.
193
     */
194
    #[Override]
195
    public function count() : int
196
    {
197
        return count($this->patches);
686✔
198
    }
199

200
    /**
201
     * Returns an iterator for the patches in this PolyhedralSurface.
202
     *
203
     * @return ArrayIterator<int<0, max>, T>
204
     */
205
    #[Override]
206
    public function getIterator() : ArrayIterator
207
    {
208
        return new ArrayIterator($this->patches);
1,379✔
209
    }
210

211
    /**
212
     * Returns a copy of this PolyhedralSurface, with the given patches added.
213
     *
214
     * @psalm-suppress UnsafeInstantiation
215
     */
216
    public function withAddedPatches(Polygon ...$patches) : PolyhedralSurface
217
    {
218
        return new static($this->coordinateSystem, ...$this->patches, ...$patches);
105✔
219
    }
220
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc