1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 **************************************************************************/
27 // File: vogl_vector2d.h
30 #include "vogl_core.h"
38 typedef vogl::vector<T> vector_type;
41 typedef const T &const_reference;
43 typedef const T *const_pointer;
45 inline vector2D(uint width = 0, uint height = 0, const T &def = T())
48 m_vec(width * height),
53 inline vector2D(const vector2D &other)
54 : m_width(other.m_width),
55 m_height(other.m_height),
61 inline vector2D &operator=(const vector2D &rhs)
66 m_width = rhs.m_width;
67 m_height = rhs.m_height;
73 bool try_resize(uint width, uint height, bool preserve = true)
75 if ((width == m_width) && (height == m_height))
79 if (!new_vec.try_resize(width * height))
84 const uint nx = math::minimum(width, m_width);
85 const uint ny = math::minimum(height, m_height);
87 for (uint y = 0; y < ny; y++)
89 const T *pSrc = &m_vec[y * m_width];
90 T *pDst = &new_vec[y * width];
91 if (VOGL_IS_BITWISE_COPYABLE_OR_MOVABLE(T))
92 memcpy(pDst, pSrc, nx * sizeof(T));
95 for (uint x = 0; x < nx; x++)
108 void resize(uint width, uint height, bool preserve = true)
110 if (!try_resize(width, height, preserve))
112 VOGL_FAIL("vector2D::resize: Out of memory");
123 inline uint width() const
127 inline uint height() const
131 inline uint size() const
136 inline uint size_in_bytes() const
138 return m_vec.size() * sizeof(T);
141 const vector_type &get_vec() const
145 vector_type &get_vec()
150 inline const T *get_ptr() const
152 return m_vec.get_ptr();
156 return m_vec.get_ptr();
159 inline const T &operator[](uint i) const
163 inline T &operator[](uint i)
168 inline const T &operator()(uint x, uint y) const
170 VOGL_ASSERT((x < m_width) && (y < m_height));
171 return m_vec[x + y * m_width];
174 inline T &operator()(uint x, uint y)
176 VOGL_ASSERT((x < m_width) && (y < m_height));
177 return m_vec[x + y * m_width];
180 inline const T &at(uint x, uint y) const
182 if ((x >= m_width) || (y >= m_height))
184 return m_vec[x + y * m_width];
187 inline T &at(uint x, uint y)
189 if ((x >= m_width) || (y >= m_height))
191 return m_vec[x + y * m_width];
194 inline const T &at_clamped(int x, int y) const
196 x = math::clamp<int>(x, 0, m_width - 1);
197 y = math::clamp<int>(y, 0, m_height - 1);
198 return m_vec[x + y * m_width];
201 inline T &at_clamped(int x, int y)
203 x = math::clamp<int>(x, 0, m_width - 1);
204 y = math::clamp<int>(y, 0, m_height - 1);
205 return m_vec[x + y * m_width];
208 inline const T &at_wrapped(int x, int y) const
210 x = math::posmod(x, m_width);
211 y = math::posmod(y, m_height);
212 return m_vec[x + y * m_width];
215 inline T &at_wrapped(int x, int y)
217 x = math::posmod(x, m_width);
218 y = math::posmod(y, m_height);
219 return m_vec[x + y * m_width];
222 inline void swap(vector2D &other)
224 m_vec.swap(other.m_vec);
225 std::swap(m_width, other.m_width);
226 std::swap(m_height, other.m_height);
227 std::swap(m_def, other.m_def);
230 inline void set_all(const T &x)
235 inline void extract_block_clamped(vector2D &dst, uint dst_x, uint dst_y, int src_x, int src_y, uint w, uint h) const
237 for (uint y = 0; y < h; y++)
238 for (uint x = 0; x < w; x++)
239 dst.at(dst_x + x, dst_y + y) = at_clamped(src_x + x, src_y + y);
242 inline void extract_block_wrapped(vector2D &dst, uint dst_x, uint dst_y, int src_x, int src_y, uint w, uint h) const
244 for (uint y = 0; y < h; y++)
245 for (uint x = 0; x < w; x++)
246 dst.at(dst_x + x, dst_y + y) = at_wrapped(src_x + x, src_y + y);
249 // Doesn't compare the default object
250 inline bool operator==(const vector2D &rhs) const
252 return (m_width == rhs.m_width) && (m_height == rhs.m_height) && (m_vec == rhs.m_vec);
255 inline bool operator!=(const vector2D &rhs) const
257 return !(*this == rhs);