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

#pragma once
#ifndef NV_MATH_BOX_INL
#define NV_MATH_BOX_INL

#include "Box.h"
#include "Vector.inl"

#include <float.h> // FLT_MAX

namespace nv
{
    // Default ctor.
    //inline Box::Box() { };

    // Copy ctor.
    //inline Box::Box(const Box & b) : minCorner(b.minCorner), maxCorner(b.maxCorner) { }

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

    // Assignment operator.
    inline Box & Box::operator=(const Box & b) { minCorner = b.minCorner; maxCorner = b.maxCorner; return *this; }

    // Clear the bounds.
    inline void Box::clearBounds()
    {
        minCorner.set(FLT_MAX, FLT_MAX, FLT_MAX);
        maxCorner.set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
    }

    // min < max
    inline bool Box::isValid() const
    {
        return minCorner.x <= maxCorner.x && minCorner.y <= maxCorner.y && minCorner.z <= maxCorner.z;
    }

    // Build a cube centered on center and with edge = 2*dist
    inline void Box::cube(const Vector3 & center, float dist)
    {
        setCenterExtents(center, Vector3(dist));
    }

    // Build a box, given center and extents.
    inline void Box::setCenterExtents(const Vector3 & center, const Vector3 & extents)
    {
        minCorner = center - extents;
        maxCorner = center + extents;
    }

    // Get box center.
    inline Vector3 Box::center() const
    {
        return (minCorner + maxCorner) * 0.5f;
    }

    // Return extents of the box.
    inline Vector3 Box::extents() const
    {
        return (maxCorner - minCorner) * 0.5f;
    }

    // Return extents of the box.
    inline float Box::extents(uint axis) const
    {
        nvDebugCheck(axis < 3);
        if (axis == 0) return (maxCorner.x - minCorner.x) * 0.5f;
        if (axis == 1) return (maxCorner.y - minCorner.y) * 0.5f;
        if (axis == 2) return (maxCorner.z - minCorner.z) * 0.5f;
        nvUnreachable();
        return 0.0f;
    }

    // Add a point to this box.
    inline void Box::addPointToBounds(const Vector3 & p)
    {
        minCorner = min(minCorner, p);
        maxCorner = max(maxCorner, p);
    }

    // Add a box to this box.
    inline void Box::addBoxToBounds(const Box & b)
    {
        minCorner = min(minCorner, b.minCorner);
        maxCorner = max(maxCorner, b.maxCorner);
    }

    // Add sphere to this box.
    inline void Box::addSphereToBounds(const Vector3 & p, float r) {
        minCorner = min(minCorner, p - Vector3(r));
        maxCorner = min(maxCorner, p + Vector3(r));
    }

    // Translate box.
    inline void Box::translate(const Vector3 & v)
    {
        minCorner += v;
        maxCorner += v;
    }

    // Scale the box.
    inline void Box::scale(float s)
    {
        minCorner *= s;
        maxCorner *= s;
    }

    // Expand the box by a fixed amount.
    inline void Box::expand(float r) {
        minCorner -= Vector3(r,r,r);
        maxCorner += Vector3(r,r,r);
    }

    // Get the area of the box.
    inline float Box::area() const
    {
        const Vector3 d = extents();
        return 8.0f * (d.x*d.y + d.x*d.z + d.y*d.z);
    }	

    // Get the volume of the box.
    inline float Box::volume() const
    {
        Vector3 d = extents();
        return 8.0f * (d.x * d.y * d.z);
    }

    // Return true if the box contains the given point.
    inline bool Box::contains(const Vector3 & p) const
    {
        return 
            minCorner.x < p.x && minCorner.y < p.y && minCorner.z < p.z &&
            maxCorner.x > p.x && maxCorner.y > p.y && maxCorner.z > p.z;
    }

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

        if (i & 4) minCorner.x = center.x;
        else       maxCorner.x = center.x;
        if (i & 2) minCorner.y = center.y;
        else       maxCorner.y = center.y;
        if (i & 1) minCorner.z = center.z;
        else       maxCorner.z = center.z;
    }

} // nv namespace


#endif // NV_MATH_BOX_INL