]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_vec_interval.h
Initial vogl checkin
[vogl] / src / voglcore / vogl_vec_interval.h
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_vec_interval.h
28 #pragma once
29
30 #include "vogl_core.h"
31 #include "vogl_vec.h"
32
33 namespace vogl
34 {
35     enum eInitExpand
36     {
37         cInitExpand
38     };
39
40     template <typename T>
41     class vec_interval
42     {
43     public:
44         enum
45         {
46             N = T::num_elements
47         };
48         typedef typename T::scalar_type scalar_type;
49
50         inline vec_interval()
51         {
52         }
53         inline vec_interval(eInitExpand)
54         {
55             init_expand();
56         }
57         inline vec_interval(const T &v)
58         {
59             m_bounds[0] = v;
60             m_bounds[1] = v;
61         }
62         inline vec_interval(const T &low, const T &high)
63         {
64             m_bounds[0] = low;
65             m_bounds[1] = high;
66         }
67
68         inline void clear()
69         {
70             m_bounds[0].clear();
71             m_bounds[1].clear();
72         }
73
74         inline const T &operator[](uint i) const
75         {
76             VOGL_ASSERT(i < 2);
77             return m_bounds[i];
78         }
79         inline T &operator[](uint i)
80         {
81             VOGL_ASSERT(i < 2);
82             return m_bounds[i];
83         }
84
85         inline T get_range() const
86         {
87             return m_bounds[1] - m_bounds[0];
88         }
89         inline scalar_type get_dimension(uint axis) const
90         {
91             return m_bounds[1][axis] - m_bounds[0][axis];
92         }
93
94         inline void init_expand()
95         {
96             m_bounds[0].set(std::numeric_limits<scalar_type>::max());
97             m_bounds[1].set(std::numeric_limits<scalar_type>::lowest());
98         }
99
100         inline void expand(const T &val)
101         {
102             m_bounds[0] = T::component_min(m_bounds[0], val);
103             m_bounds[1] = T::component_max(m_bounds[1], val);
104         }
105
106         inline bool is_null() const
107         {
108             for (uint i = 0; i < N; i++)
109                 if (m_bounds[0][i] > m_bounds[1][i])
110                     return true;
111             return false;
112         }
113
114     private:
115         T m_bounds[2];
116     };
117
118     typedef vec_interval<vec1F> vec_interval1F;
119     typedef vec_interval<vec2I> vec_interval2I;
120     typedef vec_interval<vec2F> vec_interval2F;
121     typedef vec_interval<vec3F> vec_interval3F;
122     typedef vec_interval<vec4F> vec_interval4F;
123
124     typedef vec_interval2F aabb2F;
125     typedef vec_interval3F aabb3F;
126
127     template <typename T>
128     class scalar_interval
129     {
130     public:
131         typedef T scalar_type;
132
133         inline scalar_interval()
134         {
135         }
136         inline scalar_interval(eInitExpand)
137         {
138             init_expand();
139         }
140         inline scalar_interval(const T v)
141         {
142             m_bounds[0] = v;
143             m_bounds[1] = v;
144         }
145         inline scalar_interval(const T low, const T high)
146         {
147             m_bounds[0] = low;
148             m_bounds[1] = high;
149         }
150
151         inline void clear()
152         {
153             m_bounds[0] = 0;
154             m_bounds[1] = 0;
155         }
156
157         inline const T operator[](uint i) const
158         {
159             VOGL_ASSERT(i < 2);
160             return m_bounds[i];
161         }
162         inline T &operator[](uint i)
163         {
164             VOGL_ASSERT(i < 2);
165             return m_bounds[i];
166         }
167
168         inline T get_range() const
169         {
170             return m_bounds[1] - m_bounds[0];
171         }
172         inline T get_dimension() const
173         {
174             return get_range();
175         }
176
177         inline void init_expand()
178         {
179             m_bounds[0] = std::numeric_limits<scalar_type>::max();
180             m_bounds[1] = std::numeric_limits<scalar_type>::min();
181         }
182
183         inline void expand(const T val)
184         {
185             m_bounds[0] = math::minimum(m_bounds[0], val);
186             m_bounds[1] = math::maximum(m_bounds[1], val);
187         }
188
189         inline bool is_null() const
190         {
191             return m_bounds[0] > m_bounds[1];
192         }
193
194     private:
195         T m_bounds[2];
196     };
197
198     typedef scalar_interval<float> scalar_intervalF;
199     typedef scalar_interval<double> scalar_intervalD;
200
201 } // namespace vogl