dl
dl_vector2.h
浏览该文件的文档.
1
15#pragma once
16
17#include <cstddef>
18#include <vector>
19#include <cassert>
20#include <span>
21
22#include "dl_type.h"
24#include "math/dl_math.h"
25
26namespace dl
27{
29template<typename T, typename Index = unsigned>
31{
32public:
34 using PointU = Size;
39 _w{ 0 }
40 {}
41
46 {
47 _init(size);
48 }
49
53 Vector2(const Size& size, const T* p)
54 {
55 _init(size);
56
57 for (size_t i = 0; i < _data.size(); ++i)
58 {
59 _data[i] = *(p + i);
60 }
61 }
62
65 Vector2(const Vector2& b)
66 {
67 *this = b;
68 _rebuild_ptr_line((Index)b._ptrLine.size());
69 }
70
75 void Resize(const Size& size)
76 {
77 Vector2 temp{ *this };
78
79 _init(size);
80 Fill({});
81
82 CopyCross(temp);
83 }
84
88 std::span<T> operator[](Index j)
89 {
90 assert(j < _ptrLine.size());
91 return { _ptrLine[j], _w };
92 }
93
96 const std::span<T> operator[](Index j) const
97 {
98 assert(j < _ptrLine.size());
99 return { _ptrLine[j], _w };
100 }
101
106 /*T& operator[](std::size_t j, std::size_t i)
107 {
108 assert(j < _ptrLine.size());
109 assert(i < _w);
110 return _ptrLine[j][i];
111 }*/
112
113 const T& Get(Index j, Index i) const
114 {
115 assert(j < _ptrLine.size());
116 assert(i < _w);
117 return _ptrLine[j][i];
118 }
119 const T& Get(const PointU& xy) const
120 {
121 return Get(xy[1], xy[0]);
122 }
123 T& GetRef(Index j, Index i)
124 {
125 assert(j < _ptrLine.size());
126 assert(i < _w);
127 return _ptrLine[j][i];
128 }
129 T& GetRef(const PointU& xy)
130 {
131 return GetRef(xy[1], xy[0]);
132 }
133
136 T* GetPointer(Index j, Index i)
137 {
138 if (j >= _ptrLine.size()
139 || i >= _w)
140 return nullptr;
141 return &_ptrLine[j][i];
142 }
143 T* GetPointer(const PointU& xy)
144 {
145 return GetPointer(xy[1], xy[0]);
146 }
147 T* GetPointerOffset(const PointU& xy, const Point& offset)
148 {
149 Point xy_out = toPoint(xy) + offset;
150 if (xy_out[0] < 0 || xy_out[1] < 0)
151 return nullptr;
152 return GetPointer((Index)xy_out[1], (Index)xy_out[0]);
153 }
154
155 void Set(Index j, Index i, const T& v)
156 {
157 assert(j < _ptrLine.size());
158 assert(i < _w);
159 _ptrLine[j][i] = v;
160 }
161
165 void Fill(const T& v)
166 {
167 for (auto& iter : _data)
168 iter = v;
169 }
170 void Clear()
171 {
172 _data.clear();
173 _w = 0;
174 _ptrLine.clear();
175 }
176
180 void CopyCross(const Vector2& b)
181 {
182 Index i_max = (std::min)(_w, b._w);
183 Index j_max = (Index)(std::min)(_ptrLine.size(), b._ptrLine.size());
184 for(Index j = 0; j < j_max; ++j)
185 for (Index i = 0; i < i_max; ++i)
186 {
187 GetRef(j, i) = b.Get(j, i);
188 }
189 }
190
194 void PushBack(std::vector<T>& vec)
195 {
196 assert(vec.size() == _w);
197 _data.insert(_data.end(), vec.begin(), vec.end());
198 _rebuild_ptr_line((Index)_ptrLine.size() + 1);
199 }
200
201
202 bool IsEmpty() const { return _data.empty(); }
203
204 const std::byte* GetBuffer() const
205 {
206 return (const std::byte*)_data.data();
207 }
208
210 {
211 return _data.data();
212 }
213
214 const T* GetData() const
215 {
216 return _data.data();
217 }
218
219 Index GetWidth() const
220 {
221 return _w;
222 }
223
224 Index GetHeight() const
225 {
226 return (Index)_ptrLine.size();
227 }
228
229 Size GetSize() const
230 {
231 return { GetWidth(), GetHeight() };
232 }
233
234 // 由于标准库 size与 length等价,不使用length命令
235 Index GetSizeMul() const
236 {
237 return GetWidth() * GetHeight();
238 }
239
241 {
242 return (Index)(_data.size() * sizeof(T));
243 }
244
245 auto begin()
246 {
247 return _data.begin();
248 }
249 auto end()
250 {
251 return _data.end();
252 }
253 void reserve(size_t i)
254 {
255 _data.reserve(i);
256 }
257 void emplace_back(T&& t)
258 {
259 _data.emplace_back(t);
260 }
261 size_t size() const
262 {
263 return _data.size();
264 }
265 // 顺时针旋转90度
267 {
268 Vector2 ret;
269 ret.Resize({ v.GetHeight(), v.GetWidth() });
270
271 for (Index j = 0; j < v.GetHeight(); ++j)
272 {
273 for (Index i = 0; i < v.GetWidth(); ++i)
274 {
275 ret[i][v.GetHeight() - j - 1] = v[j][i];
276 }
277 }
278 return ret;
279 }
280private:
281 std::vector<T> _data;
282 Index _w;// 宽
283 std::vector<T*> _ptrLine; // 行指针
284
285 void _init(const Size& size)
286 {
287 _w = size[0];
288 _data.resize(Accumulate::Mul(size));
289 _rebuild_ptr_line(size[1]);
290 }
291 // 重建行指针
292 void _rebuild_ptr_line(Index h)
293 {
294 _ptrLine.resize(h);
295 for (Index i = 0; i < _ptrLine.size(); ++i)
296 _ptrLine[i] = _data.data() + i * _w;
297 }
298};
299
300
301}
const std::byte * GetBuffer() const
Array< Index, 2 > Size
void emplace_back(T &&t)
void Resize(const Size &size)
调整为指定大小
Index GetHeight() const
void CopyCross(const Vector2 &b)
从b拷贝重复的面积
const T * GetData() const
void PushBack(std::vector< T > &vec)
插入行
Index GetSizeMul() const
T * GetPointerOffset(const PointU &xy, const Point &offset)
T * GetPointer(Index j, Index i)
返回元素地址,失败返回空
Vector2(const Size &size, const T *p)
以指定大小构造,并传入指针赋值初始化
Index GetWidth() const
T & GetRef(Index j, Index i)
const std::span< T > operator[](Index j) const
返回第j行
Vector2()
空构造
const T & Get(Index j, Index i) const
返回第j行 第i个
T & GetRef(const PointU &xy)
void reserve(size_t i)
static Vector2 RotateRightAngle1(const Vector2 &v)
Size GetSize() const
T * GetPointer(const PointU &xy)
Vector2(const Size &size)
以指定大小构造
Vector2(const Vector2 &b)
复制构造 todo
bool IsEmpty() const
const T & Get(const PointU &xy) const
std::span< T > operator[](Index j)
返回第j行
void Set(Index j, Index i, const T &v)
数学相关的通用算法
通用类型
constexpr auto Mul(const C &container)
求积product
constexpr Array< Point::value_type, N > toPoint(const Array< T, N > &a)
转换到Point,已知长度
PointU Size
Point2 Point