-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2.h
357 lines (306 loc) · 9.36 KB
/
Vector2.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
#pragma once
template<typename T>
struct Vector2
{
T x, y;
T & operator[](const int i)
{
return *(&(x) + i);
}
inline Vector2<T>() {}
//inline Vector3d(const Vector3d & rhs) { *this = rhs; }
inline Vector2<T>(const T &_x, const T &_y): x(_x), y(_y) { }
inline T Len() const
{
return sqrt(x * x + y * y);
}
inline T SquareLen() const
{
return x * x + y * y;
}
inline void Normalize() //Normalize itself
{
T l = sqrtf(x * x + y * y);
if (fabs(l) > T(1e-8))
{
T k = T(1.0) / l;
x *= k;
y *= k;
}
}
inline void Invert()
{
x = -x;
y = -y;
}
void Rotate(const T angle)
{
Vector2 self = *this;
Vector2 x = self;
Vector2 y = Vector2(-x.y, x.x);
Vector2 delta = x * cos(angle) + y * sin(angle) - x;
self += delta;
*this = self;
}
inline Vector2<T> GetNorm() const
{
T l = sqrt(x * x + y * y);
if (fabs(l) > T(1e-8))
{
T k = T(1.0) / l;
return Vector2<T>(x * k, y * k);
}else
{
return Vector2<T>(0, 0);
}
}
inline Vector2<T> operator-() const
{
return Vector2<T>(-x, -y);
}
void Decrease(T val)
{
if(SquareLen() > val * val)
{
T len = Len();
T scale = (len - val) / len;
x *= scale;
y *= scale;
}else
{
x = 0.0f;
y = 0.0f;
}
}
inline Vector2<T> &operator *=(const T &val)
{
x *= val;
y *= val;
return *this;
}
inline Vector2<T> &operator /=(const T &val)
{
T inv = T(1.0) / val;
x *= inv;
y *= inv;
return *this;
}
inline Vector2<T> &operator +=(const Vector2<T> &vec)
{
x += vec.x;
y += vec.y;
return *this;
}
inline Vector2<T> &operator-=(const Vector2<T> &vec)
{
x -= vec.x;
y -= vec.y;
return *this;
}
inline Vector2<T> &operator--()
{
x = -x;
y = -y;
return *this;
}
inline Vector2<T> operator+(const Vector2<T> &vec) const
{
return Vector2<T>(x + vec.x, y + vec.y);
}
inline Vector2<T> operator-(const Vector2<T> &vec) const
{
return Vector2<T>(x - vec.x, y - vec.y);
}
inline T operator*(const Vector2<T> &vec) const
{
return x * vec.x + y * vec.y;
}
inline Vector2<T> operator*(const T &val) const
{
return Vector2<T>(x * val, y * val);
}
Vector2<T> GetPerpendicular() const
{
return Vector2<T>(-y, x);
}
template<typename SomeVector>
inline const Vector2<T> operator=(const SomeVector &v)
{
x = v.x;
y = v.y;
return *this;
}
static const Vector2<T> zeroVector()
{
return Vector2<T>(0, 0);
}
static const Vector2<T> zero()
{
return zeroVector();
}
static const Vector2<T> one()
{
return Vector2<T>(T(1.0), T(1.0));
}
static const Vector2<T> xAxis()
{
return Vector2<T>(T(1.0), 0);
}
static const Vector2<T> yAxis()
{
return Vector2<T>(0, T(1.0));
}
};
template<typename T>
inline Vector2<T> operator*(const T &d, const Vector2<T> &V)
{
return Vector2<T>(V.x * d, V.y * d);
}
template<typename T>
inline Vector2<T> operator/(const Vector2<T> &V, const T &d)
{
T invd;
if(fabs(d) > T(1e-8)) invd = T(1.0) / d;
return Vector2<T>(V.x * invd, V.y * invd);
}
template<typename T>
inline T operator^(const Vector2<T> &v1, const Vector2<T> &v2)
{
return v1.x * v2.y - v1.y * v2.x;
}
typedef Vector2<float> Vector2f;
typedef Vector2<double> Vector2d;
const static Vector2f zeroVector2f = Vector2f(0, 0);
const static Vector2f xAxis2f = Vector2f(1, 0);
const static Vector2f yAxis2f = Vector2f(0, 1);
const static Vector2d zeroVector2d = Vector2d(0, 0);
const static Vector2d xAxis2d = Vector2d(1, 0);
const static Vector2d yAxis2d = Vector2d(0, 1);
template<typename T>
bool GetTwoLinesIntersection(const Vector2<T> &p1,const Vector2<T> &p2, const Vector2<T> &t1, const Vector2<T> &t2, Vector2<T> &p0)
{
Vector2<T> v1, v2;
T k1, k2;
v1 = p2 - p1;
v2 = t2 - t1;
T invmul;
T mul = v1 ^ v2;
if(fabs(mul) > T(1e-5))
{
invmul = 1.0f / (v1 ^ v2);
k2 = ((t1 ^ v1) - (p1 ^ v1)) * invmul;/*p1.x * v1.y - p1.y * v1.x + t1.y * v1.x - t1.x * v1.y*/
k1 = ((t1 ^ v2) - (p1 ^ v2)) * invmul;
p0 = p1 + v1 * k1;
// Vector p02 = t1 + v2 * k2;
return((k1 > 0.0f) && (k1 < 1.0f) && (k2 > 0.0f) && (k2 < 1.0f));
}else
{
return 0;
}
p0 = t1 + (t1 - t2); //100% bad point
return 0;
}
template<typename T>
bool ProjectPointToLine(const Vector2<T> &t1, const Vector2<T> &t2, const Vector2<T> &p, Vector2<T> &p0, T &signOfSide)
{
Vector2<T> v1 = p - t1;
Vector2<T> v2 = t2 - t1;
signOfSide = sgn(v1 ^ v2);
p0 = t1 + v2 * ((v1 * v2) / v2.SquareLen());
if((v1 * v2 >= 0.0f) && ((v1 * v2) / (v2.SquareLen()) <= 1.0f))
{
return 1;
}
else
{
return 0;
}
}
template<typename T>
bool ProjectPointToLine(const Vector2<T> &t1, const Vector2<T> &t2, const Vector2<T> &p, Vector2<T> &p0)
{
T signOfSide;
return ProjectPointToLine(t1, t2, p, p0, signOfSide);
}
template <typename T>
T PointToSegmentDistanse(const Vector2<T> &t1, const Vector2<T> &t2, const Vector2<T> &p)
{
Vector2<T> p0;
T signOfSide;
if (ProjectPointToLine(t1, t2, p, p0, signOfSide))
{
return Vector2<T>(p.x - p0.x, p.y - p0.y).Len();
} else
{
return Min(Vector2<T>(p.x - t1.x, p.y - t1.y).Len(),
Vector2<T>(p.x - t2.x, p.y - t2.y).Len());
}
}
template<typename T>
void ProjectPointToLine(const Vector2<T> &point, const Vector2<T> &planePoint, const Vector2<T> &planeNormal, const Vector2<T> &projectionDirection,
Vector2f &projectedPoint)
{
float mult = 1.0f / (projectionDirection * planeNormal);
projectedPoint = point + projectionDirection * ((planePoint * planeNormal) - (point * planeNormal)) * mult;
}
template<typename T>
void ProjectPointToPlane(const Vector2<T> &point, const Vector2<T> &planePoint, const Vector2<T> &planeNormal, const Vector2<T> &projectionDirection,
Vector2f &projectedPoint)
{
ProjectPointToLine(point, planePoint, planeNormal, projectionDirection, projectedPoint);
}
template<typename T>
void ProjectPointToLine_noreturn(const Vector2<T> &t1, const Vector2<T> &t2, const Vector2<T> &p, Vector2<T> &p0, T &signOfSide)
{
Vector2<T> v1 = p - t1;
Vector2<T> v2 = t2 - t1;
signOfSide = sgn(v1 ^ v2);
p0 = t1 + v2 * ((v1 * v2) / v2.SquareLen());
}
template<typename GeomSpace>
bool IsPointInCellEx(const typename GeomSpace::Vector2 points[3], typename GeomSpace::Vector2 testPoint, typename GeomSpace::Scalar eps = 0)
{
typedef typename GeomSpace::Vector2 Vector2;
typedef typename GeomSpace::Scalar Scalar;
Scalar side0 = ((points[1] - points[0]) ^ (testPoint - points[0]));
Scalar side1 = ((points[2] - points[1]) ^ (testPoint - points[1]));
Scalar side2 = ((points[0] - points[2]) ^ (testPoint - points[2]));
if (side0 >= -eps && side1 >= -eps && side2 >= -eps) return 1;
if (side0 <= eps && side1 <= eps && side2 <= eps) return 1;
return 0;
}
template<typename GeomSpace>
bool IsPointInCell(const typename GeomSpace::Vector2 points[3], typename GeomSpace::Vector2 testPoint)
{
typedef typename GeomSpace::Vector2 Vector2;
typedef typename GeomSpace::Scalar Scalar;
//Scalar //eps = 0;//-1e-4;//std::numeric_limits<float>::epsilon();//Scalar(1e-9);
Scalar eps = std::numeric_limits<Scalar>::epsilon();
Scalar side0 = ((points[1] - points[0]) ^ (testPoint - points[0]));
Scalar side1 = ((points[2] - points[1]) ^ (testPoint - points[1]));
Scalar side2 = ((points[0] - points[2]) ^ (testPoint - points[2]));
if (side0 >= -eps && side1 >= -eps && side2 >= -eps) return 1;
if (side0 <= eps && side1 <= eps && side2 <= eps) return 1;
return 0;
/*Scalar eps = std::numeric_limits<float>::epsilon();//Scalar(1e-9);
Scalar side012 = mixed_product(points[1] - points[0], points[2] - points[0], testPoint - points[0]) *
mixed_product(points[1] - points[0], points[2] - points[0], points[3] - points[0]);
if(side012 < -eps) return 0;
Scalar side123 = mixed_product(points[1] - points[2], points[3] - points[2], testPoint - points[2]) *
mixed_product(points[1] - points[2], points[3] - points[2], points[0] - points[2]);
if(side123 < -eps) return 0;
Scalar side230 = mixed_product(points[2] - points[3], points[0] - points[3], testPoint - points[3]) *
mixed_product(points[2] - points[3], points[0] - points[3], points[1] - points[3]);
if(side230 < -eps) return 0;
Scalar side013 = mixed_product(points[0] - points[1], points[3] - points[1], testPoint - points[1]) *
mixed_product(points[0] - points[1], points[3] - points[1], points[2] - points[1]);
if(side013 < -eps) return 0;
return 1;*/
/*Scalar side1 = mixed_product(points[2] - points[0], points[3] - points[0], testPoint - points[0]);
Scalar side2 = mixed_product(points[3] - points[0], points[1] - points[0], testPoint - points[0]);
Scalar side3 = mixed_product(points[3] - points[1], points[2] - points[1], testPoint - points[1]);
if (side0 * side1 < 0) return 0;
if (side1 * side2 < 0) return 0;
if (side2 * side3 < 0) return 0;
return 1;*/
}