summaryrefslogtreecommitdiff
path: root/thirdparty/thekla_atlas/nvmath/Box.h
blob: 19b5f2a3a565a1cabda1d1c8cbd5140fb5be38e4 (plain)
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
// This code is in the public domain -- castanyo@yahoo.es

#pragma once
#ifndef NV_MATH_BOX_H
#define NV_MATH_BOX_H

#include "Vector.h"

#include <float.h> // FLT_MAX

namespace nv
{
    class Vector;
    class Stream;
    class Sphere;

    // Axis Aligned Bounding Box.
    class Box
    {
    public:

        inline Box() {}
        inline Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) {}
        inline Box(const Vector3 & mins, const Vector3 & maxs) : minCorner(mins), maxCorner(maxs) {}

        Box & operator=(const Box & b);

        operator const float * () const { return reinterpret_cast<const float *>(this); }

        // Clear the bounds.
        void clearBounds();

        // min < max
        bool isValid() const;

        // Build a cube centered on center and with edge = 2*dist
        void cube(const Vector3 & center, float dist);

        // Build a box, given center and extents.
        void setCenterExtents(const Vector3 & center, const Vector3 & extents);

        // Get box center.
        Vector3 center() const;

        // Return extents of the box.
        Vector3 extents() const;

        // Return extents of the box.
        float extents(uint axis) const;

        // Add a point to this box.
        void addPointToBounds(const Vector3 & p);

        // Add a box to this box.
        void addBoxToBounds(const Box & b);

        // Add sphere to this box.
        void addSphereToBounds(const Vector3 & p, float r);

        // Translate box.
        void translate(const Vector3 & v);

        // Scale the box.
        void scale(float s);

        // Expand the box by a fixed amount.
        void expand(float r);

        // Get the area of the box.
        float area() const;
 
        // Get the volume of the box.
        float volume() const;

        // Return true if the box contains the given point.
        bool contains(const Vector3 & p) const;

        // Split the given box in 8 octants and assign the ith one to this box.
        void setOctant(const Box & box, const Vector3 & center, int i);


        // Clip the given segment against this box.
        bool clipSegment(const Vector3 & origin, const Vector3 & dir, float * t_near, float * t_far) const;


        friend Stream & operator<< (Stream & s, Box & box);

        const Vector3 & corner(int i) const { return (&minCorner)[i]; }

        Vector3 minCorner;
        Vector3 maxCorner;
    };

    float distanceSquared(const Box &box, const Vector3 &point);
    bool overlap(const Box &box, const Sphere &sphere);

    // p is ray origin, id is inverse ray direction.
    bool intersect(const Box & box, const Vector3 & p, const Vector3 & id, float * t);

} // nv namespace


#endif // NV_MATH_BOX_H