BoundingBox.cpp 1.48 KB
Newer Older
1
2
3
4
#include "BoundingBox.h"

#include <stdexcept>

5
6
#include "Error.h"

7
8
namespace elsa
{
Jens Petit's avatar
Jens Petit committed
9
    BoundingBox::BoundingBox(const IndexVector_t& volumeDimensions) : _dim(volumeDimensions.size())
10
11
12
    {
        // sanity check
        if (volumeDimensions.size() < 2 || volumeDimensions.size() > 3)
13
            throw InvalidArgumentError("BoundingBox: can only deal with the 2d/3d cases");
14
15
16
17
18
19
20
21
22
23

        _min.setZero();
        _max = volumeDimensions.template cast<real_t>();

        _voxelCoordToIndexVector[0] = 1;
        _voxelCoordToIndexVector[1] = volumeDimensions[1];
        if (_dim == 3)
            _voxelCoordToIndexVector[2] = volumeDimensions[2] * volumeDimensions[2];
    }

David Frank's avatar
David Frank committed
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
    RealVector_t BoundingBox::center() const { return (_max - _min).array() / 2; }

    void BoundingBox::recomputeBounds()
    {
        RealVector_t min = _min.cwiseMin(_max);
        RealVector_t max = _min.cwiseMax(_max);

        _min = min;
        _max = max;
    }

    bool operator==(const BoundingBox& box1, const BoundingBox& box2)
    {
        return box1._min == box2._min && box1._max == box2._max;
    }

    bool operator!=(const BoundingBox& box1, const BoundingBox& box2) { return !(box1 == box2); }

    std::ostream& operator<<(std::ostream& stream, const BoundingBox& aabb)
    {
        Eigen::IOFormat fmt(4, 0, ", ", ", ", "", "", "[", "]");
        stream << "AABB { min = " << aabb._min.format(fmt) << ", max = " << aabb._max.format(fmt)
               << " }";

        return stream;
    }

51
} // namespace elsa