forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POPVector.h
executable file
·375 lines (294 loc) · 12.6 KB
/
POPVector.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
Copyright (c) 2014-present, Facebook, Inc.
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree. An additional grant
of patent rights can be found in the PATENTS file in the same directory.
*/
#ifndef __POP__FBVector__
#define __POP__FBVector__
#include <iostream>
#include <vector>
#import <CoreGraphics/CoreGraphics.h>
#import <objc/NSObjCRuntime.h>
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif
#import "POPMath.h"
namespace POP {
/** Fixed two-size vector class */
template <typename T>
struct Vector2
{
private:
typedef T Vector2<T>::* const _data[2];
static const _data _v;
public:
T x;
T y;
// Zero vector
static const Vector2 Zero() { return Vector2(0); }
// Constructors
Vector2() {}
explicit Vector2(T v) { x = v; y = v; };
explicit Vector2(T x0, T y0) : x(x0), y(y0) {};
explicit Vector2(const CGPoint &p) : x(p.x), y (p.y) {}
explicit Vector2(const CGSize &s) : x(s.width), y (s.height) {}
// Copy constructor
template<typename U> explicit Vector2(const Vector2<U> &v) : x(v.x), y(v.y) {}
// Index operators
const T& operator[](size_t i) const { return this->*_v[i]; }
T& operator[](size_t i) { return this->*_v[i]; }
const T& operator()(size_t i) const { return this->*_v[i]; }
T& operator()(size_t i) { return this->*_v[i]; }
// Backing data
T * data() { return &(this->*_v[0]); }
const T * data() const { return &(this->*_v[0]); }
// Size
inline size_t size() const { return 2; }
// Assignment
Vector2 &operator= (T v) { x = v; y = v; return *this;}
template<typename U> Vector2 &operator= (const Vector2<U> &v) { x = v.x; y = v.y; return *this;}
// Negation
Vector2 operator- (void) const { return Vector2<T>(-x, -y); }
// Equality
bool operator== (T v) const { return (x == v && y == v); }
bool operator== (const Vector2 &v) const { return (x == v.x && y == v.y); }
// Inequality
bool operator!= (T v) const {return (x != v || y != v); }
bool operator!= (const Vector2 &v) const { return (x != v.x || y != v.y); }
// Scalar Math
Vector2 operator+ (T v) const { return Vector2(x + v, y + v); }
Vector2 operator- (T v) const { return Vector2(x - v, y - v); }
Vector2 operator* (T v) const { return Vector2(x * v, y * v); }
Vector2 operator/ (T v) const { return Vector2(x / v, y / v); }
Vector2 &operator+= (T v) { x += v; y += v; return *this; };
Vector2 &operator-= (T v) { x -= v; y -= v; return *this; };
Vector2 &operator*= (T v) { x *= v; y *= v; return *this; };
Vector2 &operator/= (T v) { x /= v; y /= v; return *this; };
// Vector Math
Vector2 operator+ (const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
Vector2 operator- (const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
Vector2 &operator+= (const Vector2 &v) { x += v.x; y += v.y; return *this; };
Vector2 &operator-= (const Vector2 &v) { x -= v.x; y -= v.y; return *this; };
// Norms
CGFloat norm() const { return sqrtr(squaredNorm()); }
CGFloat squaredNorm() const { return x * x + y * y; }
// Cast
template<typename U> Vector2<U> cast() const { return Vector2<U>(x, y); }
CGPoint cg_point() const { return CGPointMake(x, y); };
};
template<typename T>
const typename Vector2<T>::_data Vector2<T>::_v = { &Vector2<T>::x, &Vector2<T>::y };
/** Fixed three-size vector class */
template <typename T>
struct Vector3
{
private:
typedef T Vector3<T>::* const _data[3];
static const _data _v;
public:
T x;
T y;
T z;
// Zero vector
static const Vector3 Zero() { return Vector3(0); };
// Constructors
Vector3() {}
explicit Vector3(T v) : x(v), y(v), z(v) {};
explicit Vector3(T x0, T y0, T z0) : x(x0), y(y0), z(z0) {};
// Copy constructor
template<typename U> explicit Vector3(const Vector3<U> &v) : x(v.x), y(v.y), z(v.z) {}
// Index operators
const T& operator[](size_t i) const { return this->*_v[i]; }
T& operator[](size_t i) { return this->*_v[i]; }
const T& operator()(size_t i) const { return this->*_v[i]; }
T& operator()(size_t i) { return this->*_v[i]; }
// Backing data
T * data() { return &(this->*_v[0]); }
const T * data() const { return &(this->*_v[0]); }
// Size
inline size_t size() const { return 3; }
// Assignment
Vector3 &operator= (T v) { x = v; y = v; z = v; return *this;}
template<typename U> Vector3 &operator= (const Vector3<U> &v) { x = v.x; y = v.y; z = v.z; return *this;}
// Negation
Vector3 operator- (void) const { return Vector3<T>(-x, -y, -z); }
// Equality
bool operator== (T v) const { return (x == v && y == v && z = v); }
bool operator== (const Vector3 &v) const { return (x == v.x && y == v.y && z == v.z); }
// Inequality
bool operator!= (T v) const {return (x != v || y != v || z != v); }
bool operator!= (const Vector3 &v) const { return (x != v.x || y != v.y || z != v.z); }
// Scalar Math
Vector3 operator+ (T v) const { return Vector3(x + v, y + v, z + v); }
Vector3 operator- (T v) const { return Vector3(x - v, y - v, z - v); }
Vector3 operator* (T v) const { return Vector3(x * v, y * v, z * v); }
Vector3 operator/ (T v) const { return Vector3(x / v, y / v, z / v); }
Vector3 &operator+= (T v) { x += v; y += v; z += v; return *this; };
Vector3 &operator-= (T v) { x -= v; y -= v; z -= v; return *this; };
Vector3 &operator*= (T v) { x *= v; y *= v; z *= v; return *this; };
Vector3 &operator/= (T v) { x /= v; y /= v; z /= v; return *this; };
// Vector Math
Vector3 operator+ (const Vector3 &v) const { return Vector3(x + v.x, y + v.y, z + v.z); }
Vector3 operator- (const Vector3 &v) const { return Vector3(x - v.x, y - v.y, z - v.z); }
Vector3 &operator+= (const Vector3 &v) { x += v.x; y += v.y; z += v.z; return *this; };
Vector3 &operator-= (const Vector3 &v) { x -= v.x; y -= v.y; z -= v.z; return *this; };
// Norms
CGFloat norm() const { return sqrtr(squaredNorm()); }
CGFloat squaredNorm() const { return x * x + y * y + z * z; }
// Cast
template<typename U> Vector3<U> cast() const { return Vector3<U>(x, y, z); }
};
template<typename T>
const typename Vector3<T>::_data Vector3<T>::_v = { &Vector3<T>::x, &Vector3<T>::y, &Vector3<T>::z };
/** Fixed four-size vector class */
template <typename T>
struct Vector4
{
private:
typedef T Vector4<T>::* const _data[4];
static const _data _v;
public:
T x;
T y;
T z;
T w;
// Zero vector
static const Vector4 Zero() { return Vector4(0); };
// Constructors
Vector4() {}
explicit Vector4(T v) : x(v), y(v), z(v), w(v) {};
explicit Vector4(T x0, T y0, T z0, T w0) : x(x0), y(y0), z(z0), w(w0) {};
// Copy constructor
template<typename U> explicit Vector4(const Vector4<U> &v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
// Index operators
const T& operator[](size_t i) const { return this->*_v[i]; }
T& operator[](size_t i) { return this->*_v[i]; }
const T& operator()(size_t i) const { return this->*_v[i]; }
T& operator()(size_t i) { return this->*_v[i]; }
// Backing data
T * data() { return &(this->*_v[0]); }
const T * data() const { return &(this->*_v[0]); }
// Size
inline size_t size() const { return 4; }
// Assignment
Vector4 &operator= (T v) { x = v; y = v; z = v; w = v; return *this;}
template<typename U> Vector4 &operator= (const Vector4<U> &v) { x = v.x; y = v.y; z = v.z; w = v.w; return *this;}
// Negation
Vector4 operator- (void) const { return Vector4<T>(-x, -y, -z, -w); }
// Equality
bool operator== (T v) const { return (x == v && y == v && z = v, w = v); }
bool operator== (const Vector4 &v) const { return (x == v.x && y == v.y && z == v.z && w == v.w); }
// Inequality
bool operator!= (T v) const {return (x != v || y != v || z != v || w != v); }
bool operator!= (const Vector4 &v) const { return (x != v.x || y != v.y || z != v.z || w != v.w); }
// Scalar Math
Vector4 operator+ (T v) const { return Vector4(x + v, y + v, z + v, w + v); }
Vector4 operator- (T v) const { return Vector4(x - v, y - v, z - v, w - v); }
Vector4 operator* (T v) const { return Vector4(x * v, y * v, z * v, w * v); }
Vector4 operator/ (T v) const { return Vector4(x / v, y / v, z / v, w / v); }
Vector4 &operator+= (T v) { x += v; y += v; z += v; w += v; return *this; };
Vector4 &operator-= (T v) { x -= v; y -= v; z -= v; w -= v; return *this; };
Vector4 &operator*= (T v) { x *= v; y *= v; z *= v; w *= v; return *this; };
Vector4 &operator/= (T v) { x /= v; y /= v; z /= v; w /= v; return *this; };
// Vector Math
Vector4 operator+ (const Vector4 &v) const { return Vector4(x + v.x, y + v.y, z + v.z, w + v.w); }
Vector4 operator- (const Vector4 &v) const { return Vector4(x - v.x, y - v.y, z - v.z, w - v.w); }
Vector4 &operator+= (const Vector4 &v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; };
Vector4 &operator-= (const Vector4 &v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; };
// Norms
CGFloat norm() const { return sqrtr(squaredNorm()); }
CGFloat squaredNorm() const { return x * x + y * y + z * z + w * w; }
// Cast
template<typename U> Vector4<U> cast() const { return Vector4<U>(x, y, z, w); }
};
template<typename T>
const typename Vector4<T>::_data Vector4<T>::_v = { &Vector4<T>::x, &Vector4<T>::y, &Vector4<T>::z, &Vector4<T>::w };
/** Convenience typedefs */
typedef Vector2<float> Vector2f;
typedef Vector2<double> Vector2d;
typedef Vector2<CGFloat> Vector2r;
typedef Vector3<float> Vector3f;
typedef Vector3<double> Vector3d;
typedef Vector3<CGFloat> Vector3r;
typedef Vector4<float> Vector4f;
typedef Vector4<double> Vector4d;
typedef Vector4<CGFloat> Vector4r;
/** Variable-sized vector class */
class Vector
{
size_t _count;
CGFloat *_values;
private:
Vector(size_t);
Vector(const Vector& other);
public:
~Vector();
// Creates a new vector instance of count with values. Initializing a vector of size 0 returns NULL.
static Vector *new_vector(NSUInteger count, const CGFloat *values);
// Creates a new vector given a pointer to another. Can return NULL.
static Vector *new_vector(const Vector * const other);
// Creates a variable size vector given a static vector and count.
static Vector *new_vector(NSUInteger count, Vector4r vec);
// Size of vector
NSUInteger size() const { return _count; }
// Returns array of values
CGFloat *data () { return _values; }
const CGFloat *data () const { return _values; };
// Vector2r support
Vector2r vector2r() const;
// Vector4r support
Vector4r vector4r() const;
// CGFloat support
static Vector *new_cg_float(CGFloat f);
// CGPoint support
CGPoint cg_point() const;
static Vector *new_cg_point(const CGPoint &p);
// CGSize support
CGSize cg_size() const;
static Vector *new_cg_size(const CGSize &s);
// CGRect support
CGRect cg_rect() const;
static Vector *new_cg_rect(const CGRect &r);
#if TARGET_OS_IPHONE
// UIEdgeInsets support
UIEdgeInsets ui_edge_insets() const;
static Vector *new_ui_edge_insets(const UIEdgeInsets &i);
#endif
// CGAffineTransform support
CGAffineTransform cg_affine_transform() const;
static Vector *new_cg_affine_transform(const CGAffineTransform &t);
// CGColorRef support
CGColorRef cg_color() const CF_RETURNS_RETAINED;
static Vector *new_cg_color(CGColorRef color);
// operator overloads
CGFloat &operator[](size_t i) const {
NSCAssert(size() > i, @"unexpected vector size:%lu", (unsigned long)size());
return _values[i];
}
// Returns the mathematical length
CGFloat norm() const;
CGFloat squaredNorm() const;
// Round to nearest sub
void subRound(CGFloat sub);
// Returns string description
NSString * toString() const;
// Operator overloads
template<typename U> Vector& operator= (const Vector4<U>& other) {
size_t count = MIN(_count, other.size());
for (size_t i = 0; i < count; i++) {
_values[i] = other[i];
}
return *this;
}
Vector& operator= (const Vector& other);
void swap(Vector &first, Vector &second);
bool operator==(const Vector &other) const;
bool operator!=(const Vector &other) const;
};
/** Convenience typedefs */
typedef std::shared_ptr<Vector> VectorRef;
typedef std::shared_ptr<const Vector> VectorConstRef;
}
#endif /* defined(__POP__FBVector__) */