-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmath.h
1072 lines (889 loc) · 24.4 KB
/
xmath.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
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
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef QDENGINE_XMATH_H
#define QDENGINE_XMATH_H
#include "common/scummsys.h"
namespace QDEngine {
class Archive;
class Vect2f;
class Vect2i;
class Vect2s;
class Vect3f;
enum eAxis {
X_AXIS = 0,
Y_AXIS = 1,
Z_AXIS = 2,
W_AXIS = 3
};
///////////////////////////////////////////////////////////////////////////////
// Constants
///////////////////////////////////////////////////////////////////////////////
#undef M_PI
#define M_PI 3.14159265358979323846f
#undef M_PI_2
#define M_PI_2 1.57079632679489661923f
#undef M_PI_4
#define M_PI_4 0.785398163397448309616f
const double DBL_EPS = 1.e-15;
const double DBL_INF = 1.e+100;
const double DBL_COMPARE_TOLERANCE = 1.e-10;
const float FLT_EPS = 1.192092896e-07f; //1.e-7f;
const float FLT_INF = 1.e+30f;
const float FLT_COMPARE_TOLERANCE = 1.e-5f;
const int INT_INF = 0x7fffffff;
inline float invSqrtFast(float x) {
x += 1e-7f; // Добавка, устраняющая деление на 0
float xhalf = 0.5f * x;
int i = *(int *)&x; // get bits for floating value
i = 0x5f375a86 - (i >> 1); // gives initial guess y0
x = *(float *)&i; // convert bits back to float
x = x * (1.5f - xhalf * x * x); // Newton step, repeating increases accuracy
return x;
}
inline float cycle(float f, float size) {
return fmod(fmod(f, size) + size, size);
}
inline float getDist(float v0, float v1, float size) {
float d = fmod(v0 - v1, size);
float ad = (float)fabs(d);
float dd = size - ad;
if (ad <= dd) return d;
return d < 0 ? d + size : d - size;
}
inline float getDeltaAngle(float to, float from) {
return getDist(to, from, 2 * M_PI);
}
inline float cycleAngle(float a) {
return cycle(a, 2 * M_PI);
}
///////////////////////////////////////////////////////////////////////////////
//
// Scalar Functions
//
///////////////////////////////////////////////////////////////////////////////
template <class T>
inline T sqr(const T &x) {
return x*x;
}
#define G2R(x) ((x)*M_PI/180.f)
#define R2G(x) ((x)*180.f/M_PI)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// class Vect2f
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Vect2f {
public:
float x, y;
inline Vect2f() { }
inline Vect2f(float x_, float y_) {
x = x_;
y = y_;
}
typedef float float2[2];
inline Vect2f(const float2 &v) {
x = v[0];
y = v[1];
}
inline Vect2f(const Vect2i &v);
inline Vect2f(const Vect2s &v);
inline Vect2f &set(float x_, float y_) {
x = x_;
y = y_;
return *this;
}
inline Vect2f operator - () const {
return Vect2f(-x, -y);
}
inline int xi() const {
return round(x);
}
inline int yi() const {
return round(y);
}
inline const float &operator[](int i) const {
return *(&x + i);
}
inline float &operator[](int i) {
return *(&x + i);
}
inline Vect2f &operator += (const Vect2f &v) {
x += v.x;
y += v.y;
return *this;
}
inline Vect2f &operator -= (const Vect2f &v) {
x -= v.x;
y -= v.y;
return *this;
}
inline Vect2f &operator *= (const Vect2f &v) {
x *= v.x;
y *= v.y;
return *this;
}
inline Vect2f &operator /= (const Vect2f &v) {
x /= v.x;
y /= v.y;
return *this;
}
inline Vect2f &operator *= (float f) {
x *= f;
y *= f;
return *this;
}
inline Vect2f &operator /= (float f) {
if (f != 0.f) f = 1 / f;
else f = 0.0001f;
x *= f;
y *= f;
return *this;
}
inline Vect2f operator + (const Vect2f &v) const {
return Vect2f(*this) += v;
}
inline Vect2f operator - (const Vect2f &v) const {
return Vect2f(*this) -= v;
}
inline Vect2f operator * (const Vect2f &v) const {
return Vect2f(*this) *= v;
}
inline Vect2f operator / (const Vect2f &v) const {
return Vect2f(*this) /= v;
}
inline Vect2f operator * (float f) const {
return Vect2f(*this) *= f;
}
inline Vect2f operator / (float f) const {
return Vect2f(*this) /= f;
}
inline bool eq(const Vect2f &v, float delta = FLT_COMPARE_TOLERANCE) const {
return fabsf(v.x - x) < delta && fabsf(v.y - y) < delta;
}
inline float dot(const Vect2f &v) const {
return x * v.x + y * v.y;
}
inline friend float dot(const Vect2f &u, const Vect2f &v) {
return u.dot(v);
}
inline float operator % (const Vect2f &v) const {
return x * v.y - y * v.x;
}
inline Vect2f &scaleAdd(const Vect2f &u, float lambda) {
x += lambda * u.x;
y += lambda * u.y;
return *this;
}
inline Vect2f &interpolate(const Vect2f &u, const Vect2f &v, float lambda); // (1-lambda)*u + lambda*v
inline float norm() const {
return sqrtf(x * x + y * y);
}
inline float norm2() const {
return x * x + y * y;
}
inline Vect2f &normalize(float norma) {
float f = norma * invSqrtFast(x * x + y * y);
x *= f;
y *= f;
return *this;
}
inline float distance(const Vect2f &v) const {
return sqrtf(distance2(v));
}
inline float distance2(const Vect2f &v) const {
float dx = x - v.x, dy = y - v.y;
return dx * dx + dy * dy;
}
inline void swap(Vect2f &v) {
Vect2f tmp = v;
v = *this;
*this = tmp;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// class Vect2i
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Vect2i {
public:
int x, y;
inline Vect2i() { }
inline Vect2i(int x_, int y_) {
x = x_;
y = y_;
}
inline Vect2i(float x_, float y_) {
x = round(x_);
y = round(y_);
}
inline Vect2i(const Vect2f &v) {
x = round(v.x);
y = round(v.y);
}
inline Vect2i(const Vect2s &v);
inline void set(int x_, int y_) {
x = x_;
y = y_;
}
inline void set(float x_, float y_) {
x = round(x_);
y = round(y_);
}
inline Vect2i operator - () const {
return Vect2i(-x, -y);
}
inline const int &operator[](int i) const {
return *(&x + i);
}
inline int &operator[](int i) {
return *(&x + i);
}
inline Vect2i &operator += (const Vect2i &v) {
x += v.x;
y += v.y;
return *this;
}
inline Vect2i &operator -= (const Vect2i &v) {
x -= v.x;
y -= v.y;
return *this;
}
inline Vect2i &operator *= (const Vect2i &v) {
x *= v.x;
y *= v.y;
return *this;
}
inline Vect2i &operator /= (const Vect2i &v) {
x /= v.x;
y /= v.y;
return *this;
}
inline Vect2i operator + (const Vect2i &v) const {
return Vect2i(*this) += v;
}
inline Vect2i operator - (const Vect2i &v) const {
return Vect2i(*this) -= v;
}
inline Vect2i operator * (const Vect2i &v) const {
return Vect2i(*this) *= v;
}
inline Vect2i &operator *= (int f) {
x *= f;
y *= f;
return *this;
}
inline Vect2i operator * (int f) const {
return Vect2i(*this) *= f;
}
inline Vect2i &operator >>= (int n) {
x >>= n;
y >>= n;
return *this;
}
inline Vect2i operator >> (int n) const {
return Vect2i(*this) >>= n;
}
inline Vect2i &operator *= (float f) {
x = round(x * f);
y = round(y * f);
return *this;
}
inline Vect2i &operator /= (float f) {
return *this *= 1.f / f;
}
inline Vect2i operator * (float f) const {
return Vect2i(*this) *= f;
}
inline Vect2i operator / (float f) const {
return Vect2i(*this) /= f;
}
inline int dot(const Vect2i &v) const {
return x * v.x + y * v.y;
}
inline friend int dot(const Vect2i &u, const Vect2i &v) {
return u.dot(v);
}
inline int operator % (const Vect2i &v) const {
return x * v.y - y * v.x;
}
inline int norm() const {
return round(sqrtf(float(x * x + y * y)));
}
inline int norm2() const {
return x * x + y * y;
}
inline void normalize(int norma) {
float f = (float)norma * invSqrtFast((float)(x * x + y * y));
x = round(x * f);
y = round(y * f);
}
inline int distance2(const Vect2i &v) const {
return sqr(x - v.x) + sqr(y - v.y);
}
inline int operator == (const Vect2i &v) const {
return x == v.x && y == v.y;
}
inline int operator != (const Vect2i &v) const {
return x != v.x || y != v.y;
}
inline void swap(Vect2i &v) {
Vect2i tmp = v;
v = *this;
*this = tmp;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// class Vect2s
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Vect2s {
public:
int16 x, y;
inline Vect2s() { }
inline Vect2s(int x_, int y_) {
x = x_;
y = y_;
}
inline Vect2s(const Vect2f &v) {
x = round(v.x);
y = round(v.y);
}
inline Vect2s(const Vect2i &v) {
x = v.x;
y = v.y;
}
inline void set(int x_, int y_) {
x = x_;
y = y_;
}
inline Vect2s operator - () const {
return Vect2s(-x, -y);
}
inline const int16 &operator[](int i) const {
return *(&x + i);
}
inline int16 &operator[](int i) {
return *(&x + i);
}
inline Vect2s &operator += (const Vect2s &v) {
x += v.x;
y += v.y;
return *this;
}
inline Vect2s &operator -= (const Vect2s &v) {
x -= v.x;
y -= v.y;
return *this;
}
inline Vect2s &operator *= (const Vect2s &v) {
x *= v.x;
y *= v.y;
return *this;
}
inline Vect2s &operator *= (float f) {
x = round(x * f);
y = round(y * f);
return *this;
}
inline Vect2s &operator /= (float f) {
if (f != 0.f) f = 1 / f;
else f = 0.0001f;
x = round(x * f);
y = round(y * f);
return *this;
}
inline Vect2s operator - (const Vect2s &v) const {
return Vect2s(x - v.x, y - v.y);
}
inline Vect2s operator + (const Vect2s &v) const {
return Vect2s(x + v.x, y + v.y);
}
inline Vect2s operator * (const Vect2s &v) const {
return Vect2s(x * v.x, y * v.y);
}
inline Vect2s operator * (float f) const {
Vect2s tmp(round(x * f), round(y * f));
return tmp;
}
inline Vect2s operator / (float f) const {
if (f != 0.f) f = 1 / f;
else f = 0.0001f;
Vect2s tmp(round(x * f), round(y * f));
return tmp;
}
inline int operator == (const Vect2s &v) const {
return x == v.x && y == v.y;
}
inline int norm() const {
return round(sqrtf((float)(x * x + y * y)));
}
inline int norm2() const {
return x * x + y * y;
}
inline int distance(const Vect2s &v) const {
int dx = v.x - x, dy = v.y - y;
return round(sqrtf((float)(dx * dx + dy * dy)));
}
inline void normalize(int norma) {
float f = (float)norma * invSqrtFast((float)((int)x * x + (int)y * y));
x = round(x * f);
y = round(y * f);
}
inline void swap(Vect2s &v) {
Vect2s tmp = v;
v = *this;
*this = tmp;
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// class Vect3f
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Vect3f {
public:
typedef float float3[3];
float x, y, z;
// constructors //////////////////////////////////////////////////////////////
inline Vect3f() {}
inline Vect3f(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
}
explicit inline Vect3f(const Vect2f &v, float z_ = 0) {
x = v.x;
y = v.y;
z = z_;
}
inline Vect3f(const float3 &v) {
x = v[0];
y = v[1];
z = v[2];
}
inline operator const Vect2f &() const {
return *reinterpret_cast<const Vect2f *>(this);
}
// setters / accessors / translators /////////////////////////////////////////
inline Vect3f &set(float x_, float y_, float z_) {
x = x_;
y = y_;
z = z_;
return *this;
}
inline Vect3f &setSpherical(float psi, float theta, float radius);
// index-based access: 0=x, 1=y, 2=z.
inline const float &operator[](int i) const {
return *(&x + i);
}
inline float &operator[](int i) {
return *(&x + i);
}
// Fortran index-based access: 1=x, 2=y, 3=z.
inline const float &operator()(int i) const {
return *(&x + i - 1);
}
inline float &operator()(int i) {
return *(&x + i - 1);
}
// Convertion to int ///////
inline int xi() const {
return round(x);
}
inline int yi() const {
return round(y);
}
inline int zi() const {
return round(z);
}
// Negate ////////////////////////////////////
inline Vect3f operator- () const;
inline Vect3f &negate(const Vect3f &v);
inline Vect3f &negate();
// Logical operations ////////////////////////////////
inline bool eq(const Vect3f &v, float delta = FLT_COMPARE_TOLERANCE) const;
// Addition and substruction ////////////////////
inline Vect3f &add(const Vect3f &u, const Vect3f &v);
inline Vect3f &add(const Vect3f &v);
inline Vect3f &sub(const Vect3f &u, const Vect3f &v);
inline Vect3f &sub(const Vect3f &v);
inline Vect3f &operator+= (const Vect3f &v) {
return add(v);
}
inline Vect3f &operator-= (const Vect3f &v) {
return sub(v);
}
inline Vect3f operator+ (const Vect3f &v) const {
Vect3f u;
return u.add(*this, v);
}
inline Vect3f operator- (const Vect3f &v) const {
Vect3f u;
return u.sub(*this, v);
}
// Component-wise multiplication and division ////////////////
inline Vect3f &mult(const Vect3f &u, const Vect3f &v);
inline Vect3f &mult(const Vect3f &v);
inline Vect3f &div(const Vect3f &u, const Vect3f &v);
inline Vect3f &div(const Vect3f &v);
inline Vect3f &operator*= (const Vect3f &v) {
return mult(v);
}
inline Vect3f &operator/= (const Vect3f &v) {
return div(v);
}
inline Vect3f operator* (const Vect3f &v) const {
Vect3f u;
return u.mult(*this, v);
}
inline Vect3f operator/ (const Vect3f &v) const {
Vect3f u;
return u.div(*this, v);
}
// Cross product //////////////////////
inline Vect3f &cross(const Vect3f &u, const Vect3f &v);// u x v [!]
inline Vect3f &precross(const Vect3f &v); // v x this [!]
inline Vect3f &postcross(const Vect3f &v); // this x v [!]
inline Vect3f &operator%= (const Vect3f &v) {
return postcross(v); // this x v [!]
}
inline Vect3f operator% (const Vect3f &v) const {
Vect3f u;
return u.cross(*this, v);
}
// Dot product //////////////////////
inline float dot(const Vect3f &other) const;
inline friend float dot(const Vect3f &u, const Vect3f &v) {
return u.dot(v);
}
// Multiplication & division by scalar ///////////
inline Vect3f &scale(const Vect3f &v, float s);
inline Vect3f &scale(float s);
inline Vect3f &operator*= (float s) {
return scale(s);
}
inline Vect3f &operator/= (float s) {
return scale(1 / s);
}
inline Vect3f operator* (float s) const {
Vect3f u;
return u.scale(*this, s);
}
inline Vect3f operator/ (float s) const {
Vect3f u;
return u.scale(*this, 1 / s);
}
inline friend Vect3f operator* (float s, const Vect3f &v) {
Vect3f u;
return u.scale(v, s);
}
// Normalize ///////////////////////////
inline Vect3f &normalize(float r = 1.0f);
inline Vect3f &normalize(const Vect3f &v, float r = 1.0f);
// Operation returning scalar ////////////
inline float norm() const;
inline float norm2() const; // norm^2
inline float distance(const Vect3f &other) const;
inline float distance2(const Vect3f &other) const; // distance^2
inline float psi() const;
inline float theta() const;
inline float min() const;
inline float max() const;
inline float minAbs() const;
inline float maxAbs() const;
inline float sumAbs() const; // |x| + |y| + |z|
// Composite functions ////////////////////////////////
inline Vect3f &crossAdd(const Vect3f &u, const Vect3f &v, const Vect3f &w); // u x v + w [!] this must be distinct from u and v, but not necessarily from w.
inline Vect3f &crossAdd(const Vect3f &u, const Vect3f &v); // u x v + this [!]
inline Vect3f &scaleAdd(const Vect3f &v, const Vect3f &u, float lambda); // v + lambda * u
inline Vect3f &scaleAdd(const Vect3f &u, float lambda);// this + lambda * u
inline Vect3f &interpolate(const Vect3f &u, const Vect3f &v, float lambda); // (1-lambda)*u + lambda*v
// Swap /////////////////////////
inline void swap(Vect3f &other);
inline friend void swap(Vect3f &u, Vect3f &v) {
u.swap(v);
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Miscellaneous functions
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Decomposition ////////////////////////////////
inline void decomposition(const Vect3f &axis, const Vect3f &v, Vect3f &v_normal, Vect3f &v_tangent) {
// axis - axis of decomposition, v_normal - collinear to axis, v_tangent - perpendicular to axis
v_normal.scale(axis, dot(axis, v) / ((axis).norm2()));
v_tangent.sub(v, v_normal);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////
//// DEFINITIONS
////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
// Vect2 definitions
//
///////////////////////////////////////////////////////////////////////////////
inline Vect2i::Vect2i(const Vect2s &v) {
x = v.x;
y = v.y;
}
inline Vect2f::Vect2f(const Vect2i &v) {
x = float(v.x);
y = float(v.y);
}
inline Vect2f::Vect2f(const Vect2s &v) {
x = v.x;
y = v.y;
}
Vect2f &Vect2f::interpolate(const Vect2f &u, const Vect2f &v, float lambda) {
float lambda2 = 1.0f - lambda;
x = lambda2 * u.x + lambda * v.x;
y = lambda2 * u.y + lambda * v.y;
return *this;
}
///////////////////////////////////////////////////////////////////////////////
//
// Vect3f inline definitions
//
///////////////////////////////////////////////////////////////////////////////
// Dot product //////////////////////
//inline double dot(const Vect3d& u, const Vect3f& v) { return u.dot(v); }
//inline float dot(const Vect3f& u, const Vect3d& v) { return u.dot(v); }
bool Vect3f::eq(const Vect3f &other, float delta) const {
return fabs(x - other.x) < delta &&
fabs(y - other.y) < delta &&
fabs(z - other.z) < delta;
}
Vect3f Vect3f::operator- () const {
return Vect3f(-x, -y, -z);
}
// Norm operations /////////
float Vect3f::sumAbs() const {
return (float)(fabs(x) + fabs(y) + fabs(z));
}
// Descart - spherical function //////////////
float Vect3f::psi() const {
return (float)atan2(y, x);
}
float Vect3f::theta() const {
return (float)acos(z / (norm() + FLT_EPS));
}
Vect3f &Vect3f::setSpherical(float psi, float theta, float radius) {
x = radius * (float)sin(theta);
y = x * (float)sin(psi);
x = x * (float)cos(psi);
z = radius * (float)cos(theta);
return *this;
}
float Vect3f::dot(const Vect3f &other) const {
return x * other.x + y * other.y + z * other.z;
}
float Vect3f::norm() const {
return (float)sqrt(x * x + y * y + z * z);
}
float Vect3f::norm2() const {
return (x * x + y * y + z * z);
}
float Vect3f::distance(const Vect3f &other) const {
Vect3f w;
w.sub(other, *this);
return w.norm();
}
float Vect3f::distance2(const Vect3f &other) const {
Vect3f w;
w.sub(other, *this);
return w.norm2();
}
float Vect3f::min() const {
return (x <= y) ? ((x <= z) ? x : z) : ((y <= z) ? y : z);
}
float Vect3f::max() const {
return (x >= y) ? ((x >= z) ? x : z) : ((y >= z) ? y : z);
}
float Vect3f::minAbs() const {
float ax, ay, az;
ax = (float)fabs(x);
ay = (float)fabs(y);
az = (float)fabs(z);
return (ax <= ay) ? ((ax <= az) ? ax : az) : ((ay <= az) ? ay : az);
}
float Vect3f::maxAbs() const {
float ax, ay, az;
ax = (float)fabs(x);
ay = (float)fabs(y);
az = (float)fabs(z);
return (ax >= ay) ? ((ax >= az) ? ax : az) : ((ay >= az) ? ay : az);
}
void Vect3f::swap(Vect3f &other) {
Vect3f tmp;
tmp = *this;
*this = other;
other = tmp;
}
Vect3f &Vect3f::normalize(const Vect3f &v, float r) {
float s = r * invSqrtFast(v.x * v.x + v.y * v.y + v.z * v.z);
x = s * v.x;
y = s * v.y;
z = s * v.z;
return *this;
}
Vect3f &Vect3f::normalize(float r) {
float s = r * invSqrtFast(x * x + y * y + z * z);
x *= s;
y *= s;
z *= s;
return *this;
}
Vect3f &Vect3f::negate(const Vect3f &v) {
x = - v.x;
y = - v.y;
z = - v.z;
return *this;
}
Vect3f &Vect3f::negate() {
x = - x;
y = - y;
z = - z;
return *this;
}
Vect3f &Vect3f::add(const Vect3f &u, const Vect3f &v) {
x = u.x + v.x;
y = u.y + v.y;
z = u.z + v.z;
return *this;
}
Vect3f &Vect3f::add(const Vect3f &v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vect3f &Vect3f::sub(const Vect3f &u, const Vect3f &v) {
x = u.x - v.x;
y = u.y - v.y;
z = u.z - v.z;
return *this;
}
Vect3f &Vect3f::sub(const Vect3f &v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vect3f &Vect3f::mult(const Vect3f &u, const Vect3f &v) {
x = u.x * v.x;
y = u.y * v.y;
z = u.z * v.z;
return *this;
}
Vect3f &Vect3f::mult(const Vect3f &v) {
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
Vect3f &Vect3f::div(const Vect3f &u, const Vect3f &v) {
x = u.x / v.x;
y = u.y / v.y;
z = u.z / v.z;
return *this;
}
Vect3f &Vect3f::div(const Vect3f &v) {
x /= v.x;
y /= v.y;
z /= v.z;
return *this;
}
Vect3f &Vect3f::scale(const Vect3f &v, float s) {
x = s * v.x;
y = s * v.y;
z = s * v.z;
return *this;
}
Vect3f &Vect3f::scale(float s) {
x *= s;
y *= s;
z *= s;
return *this;
}
Vect3f &Vect3f::cross(const Vect3f &u, const Vect3f &v) {
x = u.y * v.z - u.z * v.y;
y = u.z * v.x - u.x * v.z;
z = u.x * v.y - u.y * v.x;