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

brick / geo / 13399473854

18 Feb 2025 08:26PM UTC coverage: 83.425%. Remained the same
13399473854

push

github

BenMorel
BoundingBox: readonly + promoted properties

23 of 28 new or added lines in 10 files covered. (82.14%)

8 existing lines in 3 files now uncovered.

1525 of 1828 relevant lines covered (83.42%)

1858.37 hits per line

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

98.33
/src/BoundingBox.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Brick\Geo;
6

7
use Brick\Geo\Exception\CoordinateSystemException;
8
use Brick\Geo\Exception\EmptyGeometryException;
9

10
/**
11
 * Represents a 2D or 3D bounding box calculated from a set of points. M coordinates are ignored.
12
 * This class is immutable.
13
 */
14
final readonly class BoundingBox
15
{
16
    /**
17
     * Private constructor.
18
     * Use the BoundingBox::new() factory method to obtain an instance.
19
     */
20
    private function __construct(
21
        public ?CoordinateSystem $cs,
22
        public ?float $swX,
23
        public ?float $swY,
24
        public ?float $swZ,
25
        public ?float $neX,
26
        public ?float $neY,
27
        public ?float $neZ,
28
    ) {
29
    }
392✔
30

31
    /**
32
     * Creates an empty BoundingBox instance.
33
     */
34
    public static function new(): BoundingBox
35
    {
36
        return new BoundingBox(
392✔
37
            null,
392✔
38
            null,
392✔
39
            null,
392✔
40
            null,
392✔
41
            null,
392✔
42
            null,
392✔
43
            null,
392✔
44
        );
392✔
45
    }
46

47
    /**
48
     * Returns a copy of this BoundingBox extended with the given Point.
49
     *
50
     * @throws CoordinateSystemException
51
     */
52
    public function extendedWithPoint(Point $point) : BoundingBox
53
    {
54
        if ($point->isEmpty()) {
294✔
55
            return $this;
14✔
56
        }
57

58
        $point = $point->withoutM();
280✔
59

60
        if ($this->cs === null) {
280✔
61
            $cs = $point->coordinateSystem();
280✔
62
        } else {
63
            $cs = $this->cs;
224✔
64
            if (! $cs->isEqualTo($point->coordinateSystem())) {
224✔
65
                throw CoordinateSystemException::dimensionalityMix($cs, $point->coordinateSystem());
14✔
66
            }
67
        }
68

69
        $x = $point->x();
280✔
70
        $y = $point->y();
280✔
71
        $z = $point->z();
280✔
72

73
        $swX = ($this->swX === null) ? $x : min($this->swX, $x);
280✔
74
        $swY = ($this->swY === null) ? $y : min($this->swY, $y);
280✔
75

76
        $neX = ($this->neX === null) ? $x : max($this->neX, $x);
280✔
77
        $neY = ($this->neY === null) ? $y : max($this->neY, $y);
280✔
78

79
        if ($z !== null) {
280✔
80
            $swZ = ($this->swZ === null) ? $z : min($this->swZ, $z);
133✔
81
            $neZ = ($this->neZ === null) ? $z : max($this->neZ, $z);
133✔
82
        } else {
83
            $swZ = null;
147✔
84
            $neZ = null;
147✔
85
        }
86

87
        if (
88
            $swX === $this->swX && $swY === $this->swY && $swZ === $this->swZ &&
280✔
89
            $neX === $this->neX && $neY === $this->neY && $neZ === $this->neZ
280✔
90
        ) {
91
            return $this;
154✔
92
        }
93

94
        return new BoundingBox(
280✔
95
            $cs,
280✔
96
            $swX,
280✔
97
            $swY,
280✔
98
            $swZ,
280✔
99
            $neX,
280✔
100
            $neY,
280✔
101
            $neZ,
280✔
102
        );
280✔
103
    }
104

105
    /**
106
     * Returns a copy of this BoundingBox extended with the given BoundingBox.
107
     *
108
     * @throws CoordinateSystemException
109
     */
110
    public function extendedWithBoundingBox(BoundingBox $boundingBox) : BoundingBox
111
    {
112
        if ($boundingBox->isEmpty()) {
168✔
UNCOV
113
            return $this;
×
114
        }
115

116
        return $this
168✔
117
            ->extendedWithPoint($boundingBox->getSouthWest())
168✔
118
            ->extendedWithPoint($boundingBox->getNorthEast());
168✔
119
    }
120

121
    public function isEmpty() : bool
122
    {
123
        return $this->cs === null;
168✔
124
    }
125

126
    /**
127
     * Returns the south-west XY or XYZ point.
128
     *
129
     * @throws EmptyGeometryException
130
     */
131
    public function getSouthWest() : Point
132
    {
133
        if ($this->cs === null) {
189✔
134
            throw new EmptyGeometryException('The bounding box is empty.');
7✔
135
        }
136

137
        assert($this->swX !== null);
138
        assert($this->swY !== null);
139

140
        if ($this->cs->hasZ()) {
182✔
141
            assert($this->swZ !== null);
142
            $coords = [$this->swX, $this->swY, $this->swZ];
91✔
143
        } else {
144
            $coords = [$this->swX, $this->swY];
91✔
145
        }
146

147
        return new Point($this->cs, ...$coords);
182✔
148
    }
149

150
    /**
151
     * Returns the north-east XY or XYZ point.
152
     *
153
     * @throws EmptyGeometryException
154
     */
155
    public function getNorthEast() : Point
156
    {
157
        if ($this->cs === null) {
189✔
158
            throw new EmptyGeometryException('The bounding box is empty.');
7✔
159
        }
160

161
        if ($this->cs->hasZ()) {
182✔
162
            $coords = [$this->neX, $this->neY, $this->neZ];
91✔
163
        } else {
164
            $coords = [$this->neX, $this->neY];
91✔
165
        }
166

167
        /** @var list<float> $coords */
168
        return new Point($this->cs, ...$coords);
182✔
169
    }
170
}
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