-
Notifications
You must be signed in to change notification settings - Fork 5
/
E512W3DUtils.hpp
1672 lines (1526 loc) · 63.5 KB
/
E512W3DUtils.hpp
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include "E512W3DUtilsX.hpp"
const float DEGREE_TO_RADIAN_F = 0.01745329251f;
inline float toRadianF (float d) { return d * DEGREE_TO_RADIAN_F; }
uint16_t color565 (uint16_t r, uint16_t g, uint16_t b) { return ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); }
uint16_t color1555 (uint16_t a, uint16_t r, uint16_t g, uint16_t b) { return a << 15 | ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); }
float lerp (float a, float b, float v) { return a + v * (b - a); }
uint64_t xrnd() {
static uint64_t y = 2463534242;
y = y ^ (y << 13);
y = y ^ (y >> 17);
return y = y ^ (y << 5);
}
// max size uint16_t
// template <class T>
// class E512Array {
// private:
// uint16_t array_size = 0;
// uint16_t array_capacity = 1;
// public:
// T* a;
// E512Array () { this->a = new T[1]; }
// E512Array (uint16_t sz) {
// this->a = new T[sz];
// this->array_capacity = sz;
// }
// E512Array (uint16_t sz, T t) {
// this->a = new T[sz];
// this->array_size = sz;
// this->array_capacity = sz;
// for (int i = 0; i < sz; ++i) { this->a[i] = t; }
// }
// ~E512Array () { delete[] this->a; }
// uint16_t size () { return this->array_size; }
// uint16_t capacity () { return this->array_capacity; }
// void clear () { this->array_size = 0; }
// void resize (uint16_t sz, T c = T()) {
// while (sz < this->array_size) { this->pop_back(); }
// while (sz > this->array_size) { this->push_back(c); }
// }
// void reserve (uint16_t sz) {
// if (sz < this->array_capacity) { return; }
// this->array_capacity = sz;
// T* a = new T[this->array_capacity];
// for (int i = 0; i < min(this->array_size, this->array_capacity); ++i) { a[i] = this->a[i]; }
// delete[] this->a;
// this->a = a;
// this->array_size = min(this->array_size, this->array_capacity);
// }
// void shrink_to_fit () {
// this->array_capacity = this->array_size;
// T* a = new T[this->array_capacity];
// for (int i = 0; i < this->array_size; ++i) { a[i] = this->a[i]; }
// delete[] this->a;
// this->a = a;
// }
// template <class... Args>
// void emplace_back (Args... args) {
// if (this->array_size + 1 > this->array_capacity) {
// this->array_capacity *= 2;
// T* a = new T[this->array_capacity];
// for (int i = 0; i < this->array_size; ++i) { a[i] = this->a[i]; }
// a[this->array_size] = T(args...);
// delete[] this->a;
// this->a = a;
// this->array_size += 1;
// } else {
// this->a[this->array_size] = T(args...);
// this->array_size += 1;
// }
// }
// void push_back (T t) { this->emplace_back(t); }
// void pop_back () {
// if (this->array_size > 0) { this->array_size -= 1; }
// }
// void erase_index (int index) {
// uint16_t tcnt = 0;
// for (uint16_t i = 0; i < this->array_size; ++i) {
// if (i != index) {
// this->a[tcnt] = this->a[i];
// tcnt += 1;
// }
// }
// this->array_size = tcnt;
// }
// void erase_value (T t) {
// uint16_t tcnt = 0;
// for (uint16_t i = 0; i < this->array_size; ++i) {
// if (this->a[i] != t) {
// this->a[tcnt] = this->a[i];
// tcnt += 1;
// }
// }
// this->array_size = tcnt;
// }
// T& front () { return this->a[0]; }
// T& back () { return this->a[this->array_size - 1]; }
// E512Array (const E512Array& t) {
// T* a = new T[t.array_capacity];
// for (int i = 0; i < t.array_size; ++i) { a[i] = t.a[i]; }
// this->array_size = t.array_size;
// this->array_capacity = t.array_capacity;
// this->a = a;
// }
// E512Array& operator = (const E512Array& t) {
// T* a = new T[t.array_capacity];
// for (int i = 0; i < t.array_size; ++i) { a[i] = t.a[i]; }
// this->array_size = t.array_size;
// this->array_capacity = t.array_capacity;
// delete[] this->a;
// this->a = a;
// return *this;
// }
// // indexer
// T& operator [] (uint16_t i) { return this->a[i]; }
// // range based for
// T* begin () { return &this->a[0]; }
// T* end () { return &this->a[this->array_size]; }
// };
// max size uint32_t
template <class T>
class E512Array {
private:
uint32_t array_size = 0;
uint32_t array_capacity = 1;
public:
T* a;
E512Array () { this->a = new T[1]; }
E512Array (uint32_t sz) {
this->a = new T[sz];
this->array_capacity = sz;
}
E512Array (uint32_t sz, T t) {
this->a = new T[sz];
this->array_size = sz;
this->array_capacity = sz;
for (int i = 0; i < sz; ++i) { this->a[i] = t; }
}
~E512Array () { delete[] this->a; }
uint32_t size () { return this->array_size; }
uint32_t capacity () { return this->array_capacity; }
void clear () { this->array_size = 0; }
void resize (uint32_t sz, T c = T()) {
while (sz < this->array_size) { this->pop_back(); }
while (sz > this->array_size) { this->push_back(c); }
}
void reserve (uint32_t sz) {
if (sz < this->array_capacity) { return; }
this->array_capacity = sz;
T* a = new T[this->array_capacity];
for (int i = 0; i < min(this->array_size, this->array_capacity); ++i) { a[i] = this->a[i]; }
delete[] this->a;
this->a = a;
this->array_size = min(this->array_size, this->array_capacity);
}
void shrink_to_fit () {
this->array_capacity = this->array_size;
T* a = new T[this->array_capacity];
for (int i = 0; i < this->array_size; ++i) { a[i] = this->a[i]; }
delete[] this->a;
this->a = a;
}
template <class... Args>
void emplace_back (Args... args) {
if (this->array_size + 1 > this->array_capacity) {
this->array_capacity *= 2;
T* a = new T[this->array_capacity];
for (int i = 0; i < this->array_size; ++i) { a[i] = this->a[i]; }
a[this->array_size] = T(args...);
delete[] this->a;
this->a = a;
this->array_size += 1;
} else {
this->a[this->array_size] = T(args...);
this->array_size += 1;
}
}
void push_back (T t) { this->emplace_back(t); }
void pop_back () {
if (this->array_size > 0) { this->array_size -= 1; }
}
void erase_index (int index) {
uint32_t tcnt = 0;
for (uint32_t i = 0; i < this->array_size; ++i) {
if (i != index) {
this->a[tcnt] = this->a[i];
tcnt += 1;
}
}
this->array_size = tcnt;
}
void erase_value (T t) {
uint32_t tcnt = 0;
for (uint32_t i = 0; i < this->array_size; ++i) {
if (this->a[i] != t) {
this->a[tcnt] = this->a[i];
tcnt += 1;
}
}
this->array_size = tcnt;
}
T& front () { return this->a[0]; }
T& back () { return this->a[this->array_size - 1]; }
E512Array (const E512Array& t) {
T* a = new T[t.array_capacity];
for (int i = 0; i < t.array_size; ++i) { a[i] = t.a[i]; }
this->array_size = t.array_size;
this->array_capacity = t.array_capacity;
this->a = a;
}
E512Array& operator = (const E512Array& t) {
T* a = new T[t.array_capacity];
for (int i = 0; i < t.array_size; ++i) { a[i] = t.a[i]; }
this->array_size = t.array_size;
this->array_capacity = t.array_capacity;
delete[] this->a;
this->a = a;
return *this;
}
// indexer
T& operator [] (uint32_t i) { return this->a[i]; }
// range based for
T* begin () { return &this->a[0]; }
T* end () { return &this->a[this->array_size]; }
};
struct E512Point {
int x, y;
E512Point () { this->x = 0; this->y = 0; };
E512Point (int x, int y) { this->x = x; this->y = y; }
E512Point operator + (const E512Point& t) const { return E512Point(this->x + t.x, this->y + t.y); }
E512Point operator - (const E512Point& t) const { return E512Point(this->x - t.x, this->y - t.y); }
bool operator == (const E512Point& t) const { return this->x == t.x && this->y == t.y; }
bool operator != (const E512Point& t) const { return this->x != t.x || this->y != t.y; }
};
template <class T>
class E512PriorityQueueMin {
private:
E512Array<T> v;
public:
E512PriorityQueueMin () {}
T top () { return v[0]; }
bool empty () { return v.size() == 0; }
void clear () { this->v.clear(); }
void reserve (uint32_t sz) { this->v.reserve(sz); }
template <class... Args> void emplace (Args... args) { this->push(T(args...)); }
void push (T t) {
int n = v.size();
this->v.emplace_back(t);
while (n != 0) {
int i = (n - 1) / 2;
if (this->v[n] < this->v[i]) {
T tmp = this->v[n];
this->v[n] = this->v[i];
this->v[i] = tmp;
}
n = i;
}
}
void pop () {
int n = this->v.size() - 1;
this->v[0] = this->v[n];
this->v.pop_back();
int i = 0;
int j = 1;
while (j < n) {
if (j != n - 1 && this->v[j+1] < this->v[j]) { j += 1; }
if (this->v[j] < this->v[i]) {
T tmp = this->v[j];
this->v[j] = this->v[i];
this->v[i] = tmp;
}
i = j;
j = 2 * i + 1;
}
}
};
template <class T>
class E512PriorityQueueMax {
private:
E512Array<T> v;
public:
E512PriorityQueueMax () {}
T top () { return v[0]; }
bool empty () { return v.size() == 0; }
void clear () { this->v.clear(); }
void reserve (uint32_t sz) { this->v.reserve(sz); }
template <class... Args> void emplace (Args... args) { this->push(T(args...)); }
void push (T t) {
int n = v.size();
this->v.emplace_back(t);
while (n != 0) {
int i = (n - 1) >> 1;
if (this->v[n] > this->v[i]) {
T tmp = this->v[n];
this->v[n] = this->v[i];
this->v[i] = tmp;
}
n = i;
}
}
void pop () {
int n = this->v.size() - 1;
this->v[0] = this->v[n];
this->v.pop_back();
int i = 0;
int j = 1;
while (j < n) {
if (j != n - 1 && this->v[j+1] > this->v[j]) { j += 1; }
if (this->v[j] > this->v[i]) {
T tmp = this->v[j];
this->v[j] = this->v[i];
this->v[i] = tmp;
}
i = j;
j = 2 * i + 1;
}
}
};
struct E512Edge {
public:
int a, b, cost;
E512Edge () {
this->a = 0;
this->b = 0;
this->cost = 1000000;
}
E512Edge (int a, int b, int cost) {
this->a = a;
this->b = b;
this->cost = cost;
}
bool operator == (const E512Edge& t) const { return this->a == t.a && this->b == t.b; }
};
class E512GraphDijkstra {
private:
class Node {
public:
int cost;
int index;
Node () {}
Node (int cost, int index) {
this->cost = cost;
this->index = index;
}
bool operator > (const Node& t) const { return this->cost > t.cost; }
bool operator < (const Node& t) const { return this->cost < t.cost; }
};
int tmp = -1;
E512Array<int> prevs;
E512Array<int> costs;
E512PriorityQueueMin<Node> que;
E512Array< E512Array<E512Point> > edgecost;
public:
int n;
E512Array<int> path;
E512Array<int> rpath;
int pathcost;
E512GraphDijkstra () {}
E512GraphDijkstra (int n, E512Array<E512Edge>& edges, bool undir = false) {
this->pathcost = 0;
this->path.clear();
this->n = n;
this->edgecost = E512Array< E512Array<E512Point> >(this->n, E512Array<E512Point>());
if (undir) {// undirected
for (auto&& i : edges) {
this->edgecost[i.a].emplace_back(i.b, i.cost);
this->edgecost[i.b].emplace_back(i.a, i.cost);
}
} else {// directed
for (auto&& i : edges) {
this->edgecost[i.a].emplace_back(i.b, i.cost);
}
}
}
E512GraphDijkstra (E512Array<E512Edge>& edges, bool undir = false) {
this->pathcost = 0;
this->path.clear();
int n = 0;
for (auto&& i : edges) { n = max(max(i.a, i.b), n); }
this->n = n+1;
this->edgecost = E512Array< E512Array<E512Point> >(this->n, E512Array<E512Point>());
if (undir) {// undirected
for (auto&& i : edges) {
this->edgecost[i.a].emplace_back(i.b, i.cost);
this->edgecost[i.b].emplace_back(i.a, i.cost);
}
} else {// directed
for (auto&& i : edges) {
this->edgecost[i.a].emplace_back(i.b, i.cost);
}
}
}
void costUpdate (E512Array<E512Edge>& edges, bool undir = false) {
for (auto&& i : this->edgecost) { i.clear(); }
if (undir) {// undirected
for (auto&& i : edges) {
this->edgecost[i.a].emplace_back(i.b, i.cost);
this->edgecost[i.b].emplace_back(i.a, i.cost);
}
} else {// directed
for (auto&& i : edges) {
this->edgecost[i.a].emplace_back(i.b, i.cost);
}
}
}
void calcPath (int start_i, int end_i) {
this->path.clear();
this->rpath.clear();
if (this->tmp != start_i) {
this->prevs = E512Array<int>(this->n, -1);
this->costs = E512Array<int>(this->n, 2147483647);
this->que.clear();
this->costs[start_i] = 0;
this->que.emplace(0, start_i);
}
this->tmp = start_i;
while (!this->que.empty()) {
const Node t = this->que.top();
if (t.cost >= this->costs[end_i]) { break; }
this->que.pop();
for (auto&& i : this->edgecost[t.index]) {
const int cost = t.cost + i.y;
if (cost < this->costs[i.x]) {
this->costs[i.x] = cost;
this->prevs[i.x] = t.index;
this->que.emplace(cost, i.x);
}
}
}
if (this->costs[end_i] < 2147483647) {
this->pathcost = this->costs[end_i];
int e = end_i;
while (e > -1) {
this->rpath.emplace_back(e);
e = this->prevs[e];
}
this->path.reserve(this->rpath.size());
for (int i = 0; i < this->rpath.size(); ++i) {
this->path.emplace_back(this->rpath[this->rpath.size()-1-i]);
}
} else {
this->pathcost = -1;
}
}
};
struct Vector2 {
public:
float x, y;
Vector2 () { this->x = 0; this->y = 0; }
Vector2 (float t) { this->x = t; this->y = t; }
Vector2 (float x, float y) { this->x = x; this->y = y; }
//Vector2 (Vector3 v) { this->x = v.x; this->y = v.y; }
static Vector2 normalize (Vector2 v) {
float d = sqrt(v.x * v.x + v.y * v.y);
if (d == 0) { return Vector2(0.0f, 0.0f); }
return v / d;
}
static float distance (const Vector2 a, const Vector2 b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }
Vector2 operator + (const Vector2& t) const { return Vector2(this->x + t.x, this->y + t.y); }
Vector2 operator - (const Vector2& t) const { return Vector2(this->x - t.x, this->y - t.y); }
Vector2 operator * (const Vector2& t) const { return Vector2(this->x * t.x, this->y * t.y); }
Vector2 operator / (const Vector2& t) const { return Vector2(this->x / t.x, this->y / t.y); }
Vector2 operator + (const float& t) const { return Vector2(this->x + t, this->y + t); }
Vector2 operator - (const float& t) const { return Vector2(this->x - t, this->y - t); }
Vector2 operator * (const float& t) const { return Vector2(this->x * t, this->y * t); }
Vector2 operator / (const float& t) const { return Vector2(this->x / t, this->y / t); }
bool operator == (const Vector2& t) const { return this->x == t.x && this->y == t.y; }
};
struct Vector3 {
public:
float x, y, z;
Vector3 () { this->x = 0; this->y = 0; this->z = 0; }
Vector3 (float t) { this->x = t; this->y = t; this->z = t; }
Vector3 (Vector2 t, float z) { this->x = t.x; this->y = t.y; this->z = z; }
Vector3 (float x, Vector2 t) { this->x = x; this->y = t.x; this->z = t.y; }
Vector3 (float x, float y, float z) { this->x = x; this->y = y; this->z = z; }
static Vector3 normalize (Vector3 v) {
float d = sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
if (d == 0) { return Vector3(0.0f, 0.0f, 0.0f); }
return v / d;
}
static Vector3 cross (const Vector3 a, const Vector3 b) { return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
static float dot (Vector3 a, Vector3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
static float distance (const Vector3 a, const Vector3 b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)); }
Vector3 operator + (const Vector3& t) const { return Vector3(this->x + t.x, this->y + t.y, this->z + t.z); }
Vector3 operator - (const Vector3& t) const { return Vector3(this->x - t.x, this->y - t.y, this->z - t.z); }
Vector3 operator * (const Vector3& t) const { return Vector3(this->x * t.x, this->y * t.y, this->z * t.z); }
Vector3 operator / (const Vector3& t) const { return Vector3(this->x / t.x, this->y / t.y, this->z / t.z); }
Vector3 operator + (const float& t) const { return Vector3(this->x + t, this->y + t, this->z + t); }
Vector3 operator - (const float& t) const { return Vector3(this->x - t, this->y - t, this->z - t); }
Vector3 operator * (const float& t) const { return Vector3(this->x * t, this->y * t, this->z * t); }
Vector3 operator / (const float& t) const { return Vector3(this->x / t, this->y / t, this->z / t); }
bool operator == (const Vector3& t) const { return this->x == t.x && this->y == t.y && this->z == t.z; }
};
struct Vector4 {
public:
float x, y, z, w;
Vector4 () { this->x = 0; this->y = 0; this->z = 0; this->w = 0; }
Vector4 (float t) { this->x = t; this->y = t; this->z = t; this->w = t; }
Vector4 (Vector3 v) { this->x = v.x; this->y = v.y; this->z = v.z; this->w = 1; }
Vector4 (float x, float y, float z, float w) { this->x = x; this->y = y; this->z = z; this->w = w; }
Vector3 xyz() { return Vector3(this->x, this->y, this->z); }
Vector4 operator + (const Vector4& t) const { return Vector4(this->x + t.x, this->y + t.y, this->z + t.z, this->w + t.w); }
Vector4 operator * (const float& t) const { return Vector4(this->x * t, this->y * t, this->z * t, this->w * t); }
};
struct Quaternion {
float w = 1.0f;
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
Quaternion () {}
Quaternion (float w, float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
static Quaternion angleAxis (float w, float x, float y, float z) {
Quaternion q;
w *= DEGREE_TO_RADIAN_F;
q.w = cos(w/2);
w = sin(-w/2);
q.x = x * w;
q.y = y * w;
q.z = z * w;
return q;
}
static Quaternion angleAxis (float w, Vector3 v) {
return Quaternion::angleAxis(w, v.x, v.y, v.z);
}
Quaternion mul (Quaternion q, Quaternion p) {
return Quaternion(
-q.x*p.x - q.y*p.y - q.z*p.z + q.w*p.w,
q.w*p.x - q.z*p.y + q.y*p.z + q.x*p.w,
q.z*p.x + q.w*p.y - q.x*p.z + q.y*p.w,
-q.y*p.x + q.x*p.y + q.w*p.z + q.z*p.w
);
}
void mul (Quaternion q) { *this = Quaternion::mul(*this, q); }
Quaternion operator * (const Quaternion& t) const {
return Quaternion(
-this->x*t.x - this->y*t.y - this->z*t.z + this->w*t.w,
this->w*t.x - this->z*t.y + this->y*t.z + this->x*t.w,
this->z*t.x + this->w*t.y - this->x*t.z + this->y*t.w,
-this->y*t.x + this->x*t.y + this->w*t.z + this->z*t.w
);
}
Quaternion &operator *= (const Quaternion& t) {
this->mul(t);
return *this;
}
};
struct Matrix4x4 {
public:
float m[4][4] = {
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f},
{0.0f, 0.0f, 0.0f, 0.0f}
};
Matrix4x4 () {}
Matrix4x4 (float a[]) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
this->m[i][j] = a[i*4+j];
}
}
}
static Matrix4x4 identity () {
float a[] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
return Matrix4x4(a);
}
static Matrix4x4 mul (Matrix4x4 a, Matrix4x4 b) {
Matrix4x4 r;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
r.m[i][j] += a.m[i][k] * b.m[k][j];
}
}
}
return r;
}
static Vector3 mul (Vector3 v, Matrix4x4 m) {
Vector3 r = {
v.x * m.m[0][0] + v.y * m.m[1][0] + v.z * m.m[2][0] + m.m[3][0],
v.x * m.m[0][1] + v.y * m.m[1][1] + v.z * m.m[2][1] + m.m[3][1],
v.x * m.m[0][2] + v.y * m.m[1][2] + v.z * m.m[2][2] + m.m[3][2],
};
return r;
}
static Vector3 muld (Vector3 v, Matrix4x4 m) {
float d = v.x * m.m[0][3] + v.y * m.m[1][3] + v.z * m.m[2][3] + m.m[3][3];
Vector3 r = {
v.x * m.m[0][0] + v.y * m.m[1][0] + v.z * m.m[2][0] + m.m[3][0],
v.x * m.m[0][1] + v.y * m.m[1][1] + v.z * m.m[2][1] + m.m[3][1],
v.x * m.m[0][2] + v.y * m.m[1][2] + v.z * m.m[2][2] + m.m[3][2],
};
if (d > 0) {
r.x /= d;
r.y /= d;
r.z /= d;
}
return r;
}
static Vector4 muld (Vector4 v, Matrix4x4 m) {
float d = v.x * m.m[0][3] + v.y * m.m[1][3] + v.z * m.m[2][3] + m.m[3][3];
Vector4 r = {
v.x * m.m[0][0] + v.y * m.m[1][0] + v.z * m.m[2][0] + m.m[3][0],
v.x * m.m[0][1] + v.y * m.m[1][1] + v.z * m.m[2][1] + m.m[3][1],
v.x * m.m[0][2] + v.y * m.m[1][2] + v.z * m.m[2][2] + m.m[3][2],
v.x * m.m[0][3] + v.y * m.m[1][3] + v.z * m.m[2][3] + m.m[3][3],
};
if (d > 0) {
r.x /= d;
r.y /= d;
r.z /= d;
}
return r;
}
static Vector4 mul (Vector4 v, Matrix4x4 m) {
Vector4 r = {
v.x * m.m[0][0] + v.y * m.m[1][0] + v.z * m.m[2][0] + m.m[3][0],
v.x * m.m[0][1] + v.y * m.m[1][1] + v.z * m.m[2][1] + m.m[3][1],
v.x * m.m[0][2] + v.y * m.m[1][2] + v.z * m.m[2][2] + m.m[3][2],
v.x * m.m[0][3] + v.y * m.m[1][3] + v.z * m.m[2][3] + m.m[3][3],
};
return r;
}
static Matrix4x4 projectionMatrix (float w, float h) {
Matrix4x4 r;
float aspect = w / h;
float cfov = 45.0 * DEGREE_TO_RADIAN_F;
float cnear = 0.5f;
float cfar = 1000.0f;
float y = 1.0f / tan(cfov * 0.5f);
float x = y / aspect;
float z = cfar / (cnear - cfar);
r.m[0][0] = x;
r.m[1][1] = y;
r.m[2][2] = z;
r.m[2][3] = -1.0f;
r.m[3][2] = z * cnear;
return r;
}
static Matrix4x4 orthoMatrix (float w, float h, float size) {
Matrix4x4 r;
float left = -w * size;
float right = w * size;
float top = h * size;
float bottom = -h * size;
float cnear = 0.5f;
float cfar = 1000.0f;
r.m[0][0] = 4.0f / (right - left);
r.m[1][1] = 4.0f / (top - bottom);
r.m[2][2] = -4.0f / (cfar - cnear);
r.m[3][0] = -((right+left)/(right-left));
r.m[3][1] = -((top+bottom)/(top-bottom));
r.m[3][2] = ((cfar+cnear)/(cfar-cnear));
r.m[3][3] = 2.0f;
return r;
}
static Matrix4x4 screenMatrix (float w, float h) {
Matrix4x4 r;
float hcx = w / 2;
float hcy = h / 2;
r.m[0][0] = hcx;
r.m[1][1] = -hcy;
r.m[2][2] = 1.0f;
r.m[3][0] = hcx; r.m[3][1] = hcy; r.m[3][3] = 1.0f;
return r;
}
static Matrix4x4 projscreenMatrix (float w, float h) {
Matrix4x4 r;
r.m[0][0] = 1.0f; r.m[1][1] = 1.0f; r.m[2][2] = 1.0f; r.m[3][3] = 1.0f;
r = Matrix4x4::mul(r, Matrix4x4::projectionMatrix(w, h));
r = Matrix4x4::mul(r, Matrix4x4::screenMatrix(w, h));
return r;
}
static Matrix4x4 orthoscreenMatrix (float w, float h, float size) {
Matrix4x4 r;
r.m[0][0] = 1.0f; r.m[1][1] = 1.0f; r.m[2][2] = 1.0f; r.m[3][3] = 1.0f;
r = Matrix4x4::mul(r, Matrix4x4::orthoMatrix(w, h, size));
r = Matrix4x4::mul(r, Matrix4x4::screenMatrix(w, h));
return r;
}
static Matrix4x4 moveMatrix (Vector3 v) {
Matrix4x4 r = Matrix4x4::identity();
r.m[3][0] = v.x;
r.m[3][1] = v.y;
r.m[3][2] = v.z;
return r;
}
static Matrix4x4 rotMatrix (Vector3 v) {
Matrix4x4 r = Matrix4x4::identity();
Matrix4x4 t;
float x = v.x * DEGREE_TO_RADIAN_F;
float y = v.y * DEGREE_TO_RADIAN_F;
float z = v.z * DEGREE_TO_RADIAN_F;
t = Matrix4x4::identity();
t.m[1][1] = cos(x);
t.m[1][2] = sin(x);
t.m[2][1] = -sin(x);
t.m[2][2] = cos(x);
r = Matrix4x4::mul(r, t);
t = Matrix4x4::identity();
t.m[0][0] = cos(y);
t.m[0][2] = -sin(y);
t.m[2][0] = sin(y);
t.m[2][2] = cos(y);
r = Matrix4x4::mul(r, t);
t = Matrix4x4::identity();
t.m[0][0] = cos(z);
t.m[0][1] = sin(z);
t.m[1][0] = -sin(z);
t.m[1][1] = cos(z);
r = Matrix4x4::mul(r, t);
return r;
}
static Matrix4x4 scaleMatrix (Vector3 v) {
Matrix4x4 r = Matrix4x4::identity();
r.m[0][0] = v.x;
r.m[1][1] = v.y;
r.m[2][2] = v.z;
return r;
}
static Matrix4x4 rotMatrix (Quaternion q) {
Matrix4x4 r = Matrix4x4::identity();
r.m[0][0] = 2*q.w*q.w + 2*q.x*q.x - 1.0;
r.m[0][1] = 2*q.x*q.y - 2*q.z*q.w;
r.m[0][2] = 2*q.x*q.z + 2*q.y*q.w;
r.m[1][0] = 2*q.x*q.y + 2*q.z*q.w;
r.m[1][1] = 2*q.w*q.w + 2*q.y*q.y - 1.0;
r.m[1][2] = 2*q.y*q.z - 2*q.x*q.w;
r.m[2][0] = 2*q.x*q.z - 2*q.y*q.w;
r.m[2][1] = 2*q.y*q.z + 2*q.x*q.w;
r.m[2][2] = 2*q.w*q.w + 2*q.z*q.z - 1.0;
return r;
}
static Matrix4x4 rotMatrixR (Quaternion q) {
q.w = -q.w;
Matrix4x4 r = Matrix4x4::identity();
r.m[0][0] = 2*q.w*q.w + 2*q.x*q.x - 1.0;
r.m[0][1] = 2*q.x*q.y - 2*q.z*q.w;
r.m[0][2] = 2*q.x*q.z + 2*q.y*q.w;
r.m[1][0] = 2*q.x*q.y + 2*q.z*q.w;
r.m[1][1] = 2*q.w*q.w + 2*q.y*q.y - 1.0;
r.m[1][2] = 2*q.y*q.z - 2*q.x*q.w;
r.m[2][0] = 2*q.x*q.z - 2*q.y*q.w;
r.m[2][1] = 2*q.y*q.z + 2*q.x*q.w;
r.m[2][2] = 2*q.w*q.w + 2*q.z*q.z - 1.0;
return r;
}
static Matrix4x4 inverse (Matrix4x4 a) {
Matrix4x4 b;
float d = a.m[0][0] * a.m[1][1] * a.m[2][2] * a.m[3][3] + a.m[0][0] * a.m[1][2] * a.m[2][3] * a.m[3][1] + a.m[0][0] * a.m[1][3] * a.m[2][1] * a.m[3][2]
+ a.m[0][1] * a.m[1][0] * a.m[2][3] * a.m[3][2] + a.m[0][1] * a.m[1][2] * a.m[2][0] * a.m[3][3] + a.m[0][1] * a.m[1][3] * a.m[2][2] * a.m[3][0]
+ a.m[0][2] * a.m[1][0] * a.m[2][1] * a.m[3][3] + a.m[0][2] * a.m[1][1] * a.m[2][3] * a.m[3][0] + a.m[0][2] * a.m[1][3] * a.m[2][0] * a.m[3][1]
+ a.m[0][3] * a.m[1][0] * a.m[2][2] * a.m[3][1] + a.m[0][3] * a.m[1][1] * a.m[2][0] * a.m[3][2] + a.m[0][3] * a.m[1][2] * a.m[2][1] * a.m[3][0]
- a.m[0][0] * a.m[1][1] * a.m[2][3] * a.m[3][2] - a.m[0][0] * a.m[1][2] * a.m[2][1] * a.m[3][3] - a.m[0][0] * a.m[1][3] * a.m[2][2] * a.m[3][1]
- a.m[0][1] * a.m[1][0] * a.m[2][2] * a.m[3][3] - a.m[0][1] * a.m[1][2] * a.m[2][3] * a.m[3][0] - a.m[0][1] * a.m[1][3] * a.m[2][0] * a.m[3][2]
- a.m[0][2] * a.m[1][0] * a.m[2][3] * a.m[3][1] - a.m[0][2] * a.m[1][1] * a.m[2][0] * a.m[3][3] - a.m[0][2] * a.m[1][3] * a.m[2][1] * a.m[3][0]
- a.m[0][3] * a.m[1][0] * a.m[2][1] * a.m[3][2] - a.m[0][3] * a.m[1][1] * a.m[2][2] * a.m[3][0] - a.m[0][3] * a.m[1][2] * a.m[2][0] * a.m[3][1];
if (d == 0.0f) { return a; }
b.m[0][0] = a.m[1][1] * a.m[2][2] * a.m[3][3] + a.m[1][2] * a.m[2][3] * a.m[3][1] + a.m[1][3] * a.m[2][1] * a.m[3][2] - a.m[1][1] * a.m[2][3] * a.m[3][2] - a.m[1][2] * a.m[2][1] * a.m[3][3] - a.m[1][3] * a.m[2][2] * a.m[3][1];
b.m[0][1] = a.m[0][1] * a.m[2][3] * a.m[3][2] + a.m[0][2] * a.m[2][1] * a.m[3][3] + a.m[0][3] * a.m[2][2] * a.m[3][1] - a.m[0][1] * a.m[2][2] * a.m[3][3] - a.m[0][2] * a.m[2][3] * a.m[3][1] - a.m[0][3] * a.m[2][1] * a.m[3][2];
b.m[0][2] = a.m[0][1] * a.m[1][2] * a.m[3][3] + a.m[0][2] * a.m[1][3] * a.m[3][1] + a.m[0][3] * a.m[1][1] * a.m[3][2] - a.m[0][1] * a.m[1][3] * a.m[3][2] - a.m[0][2] * a.m[1][1] * a.m[3][3] - a.m[0][3] * a.m[1][2] * a.m[3][1];
b.m[0][3] = a.m[0][1] * a.m[1][3] * a.m[2][2] + a.m[0][2] * a.m[1][1] * a.m[2][3] + a.m[0][3] * a.m[1][2] * a.m[2][1] - a.m[0][1] * a.m[1][2] * a.m[2][3] - a.m[0][2] * a.m[1][3] * a.m[2][1] - a.m[0][3] * a.m[1][1] * a.m[2][2];
b.m[1][0] = a.m[1][0] * a.m[2][3] * a.m[3][2] + a.m[1][2] * a.m[2][0] * a.m[3][3] + a.m[1][3] * a.m[2][2] * a.m[3][0] - a.m[1][0] * a.m[2][2] * a.m[3][3] - a.m[1][2] * a.m[2][3] * a.m[3][0] - a.m[1][3] * a.m[2][0] * a.m[3][2];
b.m[1][1] = a.m[0][0] * a.m[2][2] * a.m[3][3] + a.m[0][2] * a.m[2][3] * a.m[3][0] + a.m[0][3] * a.m[2][0] * a.m[3][2] - a.m[0][0] * a.m[2][3] * a.m[3][2] - a.m[0][2] * a.m[2][0] * a.m[3][3] - a.m[0][3] * a.m[2][2] * a.m[3][0];
b.m[1][2] = a.m[0][0] * a.m[1][3] * a.m[3][2] + a.m[0][2] * a.m[1][0] * a.m[3][3] + a.m[0][3] * a.m[1][2] * a.m[3][0] - a.m[0][0] * a.m[1][2] * a.m[3][3] - a.m[0][2] * a.m[1][3] * a.m[3][0] - a.m[0][3] * a.m[1][0] * a.m[3][2];
b.m[1][3] = a.m[0][0] * a.m[1][2] * a.m[2][3] + a.m[0][2] * a.m[1][3] * a.m[2][0] + a.m[0][3] * a.m[1][0] * a.m[2][2] - a.m[0][0] * a.m[1][3] * a.m[2][2] - a.m[0][2] * a.m[1][0] * a.m[2][3] - a.m[0][3] * a.m[1][2] * a.m[2][0];
b.m[2][0] = a.m[1][0] * a.m[2][1] * a.m[3][3] + a.m[1][1] * a.m[2][3] * a.m[3][0] + a.m[1][3] * a.m[2][0] * a.m[3][1] - a.m[1][0] * a.m[2][3] * a.m[3][1] - a.m[1][1] * a.m[2][0] * a.m[3][3] - a.m[1][3] * a.m[2][1] * a.m[3][0];
b.m[2][1] = a.m[0][0] * a.m[2][3] * a.m[3][1] + a.m[0][1] * a.m[2][0] * a.m[3][3] + a.m[0][3] * a.m[2][1] * a.m[3][0] - a.m[0][0] * a.m[2][1] * a.m[3][3] - a.m[0][1] * a.m[2][3] * a.m[3][0] - a.m[0][3] * a.m[2][0] * a.m[3][1];
b.m[2][2] = a.m[0][0] * a.m[1][1] * a.m[3][3] + a.m[0][1] * a.m[1][3] * a.m[3][0] + a.m[0][3] * a.m[1][0] * a.m[3][1] - a.m[0][0] * a.m[1][3] * a.m[3][1] - a.m[0][1] * a.m[1][0] * a.m[3][3] - a.m[0][3] * a.m[1][1] * a.m[3][0];
b.m[2][3] = a.m[0][0] * a.m[1][3] * a.m[2][1] + a.m[0][1] * a.m[1][0] * a.m[2][3] + a.m[0][3] * a.m[1][1] * a.m[2][0] - a.m[0][0] * a.m[1][1] * a.m[2][3] - a.m[0][1] * a.m[1][3] * a.m[2][0] - a.m[0][3] * a.m[1][0] * a.m[2][1];
b.m[3][0] = a.m[1][0] * a.m[2][2] * a.m[3][1] + a.m[1][1] * a.m[2][0] * a.m[3][2] + a.m[1][2] * a.m[2][1] * a.m[3][0] - a.m[1][0] * a.m[2][1] * a.m[3][2] - a.m[1][1] * a.m[2][2] * a.m[3][0] - a.m[1][2] * a.m[2][0] * a.m[3][1];
b.m[3][1] = a.m[0][0] * a.m[2][1] * a.m[3][2] + a.m[0][1] * a.m[2][2] * a.m[3][0] + a.m[0][2] * a.m[2][0] * a.m[3][1] - a.m[0][0] * a.m[2][2] * a.m[3][1] - a.m[0][1] * a.m[2][0] * a.m[3][2] - a.m[0][2] * a.m[2][1] * a.m[3][0];
b.m[3][2] = a.m[0][0] * a.m[1][2] * a.m[3][1] + a.m[0][1] * a.m[1][0] * a.m[3][2] + a.m[0][2] * a.m[1][1] * a.m[3][0] - a.m[0][0] * a.m[1][1] * a.m[3][2] - a.m[0][1] * a.m[1][2] * a.m[3][0] - a.m[0][2] * a.m[1][0] * a.m[3][1];
b.m[3][3] = a.m[0][0] * a.m[1][1] * a.m[2][2] + a.m[0][1] * a.m[1][2] * a.m[2][0] + a.m[0][2] * a.m[1][0] * a.m[2][1] - a.m[0][0] * a.m[1][2] * a.m[2][1] - a.m[0][1] * a.m[1][0] * a.m[2][2] - a.m[0][2] * a.m[1][1] * a.m[2][0];
float t[] = {b.m[0][0] / d, b.m[0][1] / d, b.m[0][2] / d, b.m[0][3] / d,
b.m[1][0] / d, b.m[1][1] / d, b.m[1][2] / d, b.m[1][3] / d,
b.m[2][0] / d, b.m[2][1] / d, b.m[2][2] / d, b.m[2][3] / d,
b.m[3][0] / d, b.m[3][1] / d, b.m[3][2] / d, b.m[3][3] / d};
return Matrix4x4(t);
}
};
struct Ray {
public:
Vector3 position = Vector3(0, 0, 0);
Vector3 direction = Vector3(0, -1, 0);
float distance = 0.0f;
Ray () {}
Ray (Vector3 s, Vector3 e) {
this->position = s;
this->direction = Vector3::normalize(e-s);
this->distance = Vector3::distance(s, e);
}
Ray (int x, int y, Matrix4x4 view, Matrix4x4 projscreen) {
Vector4 s(x, y, 0, 1);
Vector4 e(x, y, 1, 1);
Matrix4x4 invm = Matrix4x4::inverse(Matrix4x4::mul(view, projscreen));
s = Matrix4x4::muld(s, invm);
e = Matrix4x4::muld(e, invm);
this->position = Vector3(s.x, s.y, s.z);
this->direction = Vector3::normalize(Vector3(e.x-s.x, e.y-s.y, e.z-s.z));
this->distance = 1000.0f;
}
float raytriangle (Vector3 v1, Vector3 v2, Vector3 v3) {
float dist = -1.0f;
Vector3 e1 = v2 - v1;
Vector3 e2 = v3 - v1;
Vector3 p = Vector3::cross(this->direction, e2);
float d = Vector3::dot(e1, p);
if (d < 0.0001f) { return dist; }
Vector3 t = this->position - v1;
float u = Vector3::dot(t, p);
if (u < 0.0f || u > d) { return dist; }
Vector3 q = Vector3::cross(t, e1);
float v = Vector3::dot(this->direction, q);
if (v < 0.0f || u + v > d) { return dist; }
dist = Vector3::dot(e2, q);
float f = 1.0f / d;
dist *= f;
u *= f;
v *= f;
dist = dist <= this->distance ? dist : -1.0f;
return dist;
}
float raytriangle (Vector3 v1, Vector3 v2, Vector3 v3, float& u, float& v) {
float dist = -1.0f;
Vector3 e1 = v2 - v1;
Vector3 e2 = v3 - v1;
Vector3 p = Vector3::cross(this->direction, e2);
float d = Vector3::dot(e1, p);
if (d < 0.0001f) { return dist; }
Vector3 t = this->position - v1;
u = Vector3::dot(t, p);
if (u < 0.0f || u > d) { return dist; }
Vector3 q = Vector3::cross(t, e1);
v = Vector3::dot(this->direction, q);
if (v < 0.0f || u + v > d) { return dist; }
dist = Vector3::dot(e2, q);
float f = 1.0f / d;
dist *= f;
u *= f;
v *= f;
dist = dist <= this->distance ? dist : -1.0f;
return dist;
}
};
struct RaycastHit {
Vector3 point;
float u, v, distance;
uint32_t triangleindex;
operator bool () const { return this->distance > -1; }
};
struct PerlinNoise {
private:
static Vector2 randomGradient (int ix, int iy) {
static const uint64_t s = (4 * sizeof(uint64_t));
uint64_t a = ix, b = iy;
a *= 3284157443; b ^= (a << s) | (a >> s);
b *= 1911520717; a ^= (b << s) | (b >> s);
a *= 2048419325;
float random = a * (3.14159265f / 2147483648);
return Vector2(cos(random), sin(random));
}
//static float fede (float t) { return 1.0f-3.0f*(t*t)+2.0f*abs(t*t*t); }
static float fede (float t) { return 1.0f-(6.0f*abs(t*t*t*t*t)-15.0f*(t*t*t*t)+10.0f*abs(t*t*t)); }
static float vxy (Vector2 a, float x, float y) { return fede(x) * fede(y) * (a.x*x + a.y*y); }
public:
static float getNoise (float x, float y) {
const int iy = y < 0 ? y-1 : y;
const int ix = x < 0 ? x-1 : x;
const float u = (x - ix);
const float v = (y - iy);
const float w00 = vxy(randomGradient( ix, iy), u, v);
const float w10 = vxy(randomGradient(ix+1, iy), u-1.0f, v);
const float w01 = vxy(randomGradient( ix, iy+1), u, v-1.0f);
const float w11 = vxy(randomGradient(ix+1, iy+1), u-1.0f, v-1.0f);
return lerp(lerp(w00, w10, u), lerp(w01, w11, u), v);
}
};
struct Face {
uint16_t a, b, c;
Face () {};
Face (uint16_t a, uint16_t b, uint16_t c) { this->a = a; this->b = b; this->c = c; }