-
Notifications
You must be signed in to change notification settings - Fork 1
/
VL234f.h
2436 lines (1850 loc) · 51.1 KB
/
VL234f.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
// VL234 monolithic header
typedef float Real;
#ifdef VL_DEBUG
#include <assert.h>
#define CL_ASSERT(M_X) assert(M_X);
#define CL_ASSERT_MSG(M_X, M_MSG) assert(M_X);
#define CL_ERROR(M_MSG) assert(false);
#define CL_WARNING(M_X)
#define CL_WARNING_MSG(M_X, M_MSG)
#define CL_EXPECT(M_X) assert(M_X)
#define CL_EXPECT_MSG(M_X, M_MSG) assert(M_X)
#define CL_RANGE(M_X, M_0, M_1) assert((M_X) >= M_0 && (M_X) < M_1)
#define CL_RANGE_MSG(M_X, M_0, M_1, M_MSG) assert((M_X) >= M_0 && (M_X) < M_1)
#define CL_INDEX(M_X, M_N) assert((M_X) >= 0 && (M_X) < M_N)
#define CL_INDEX_MSG(M_X, M_N, M_MSG) assert((M_X) >= 0 && (M_X) < M_N)
#else
#define CL_ASSERT(M_X)
#define CL_ASSERT_MSG(M_X, M_MSG)
#define CL_ERROR(M_X)
#define CL_WARNING(M_X)
#define CL_WARNING_MSG(M_X, M_MSG)
#define CL_EXPECT(M_X) (void) (M_X)
#define CL_EXPECT_MSG(M_X, M_MSG) (void) (M_X)
#define CL_RANGE(M_X, M_0, M_1)
#define CL_RANGE_MSG(M_X, M_0, M_1, M_MSG)
#define CL_INDEX(M_X, M_N)
#define CL_INDEX_MSG(M_X, M_N, M_MSG)
#endif
#ifndef VL_NEW
#define VL_NEW new
#define VL_DELETE delete
#endif
#ifndef VL_RESTRICT
#define VL_RESTRICT
#endif
/*
File: VLConstants.h
Function: Contains various constants for VL.
Copyright: Andrew Willmott
*/
#ifndef VLConstants_H
#define VLConstants_H
#include <cmath>
#include <cstdint>
// --- Mathematical constants -------------------------------------------------
const Real vl_pi = Real(3.14159265358979323846);
const Real vl_halfPi = Real(vl_pi / 2.0);
const Real vl_quarterPi = Real(vl_pi / 4.0);
const Real vl_twoPi = Real(2.0 * vl_pi);
#ifdef HUGE_VAL
const Real vl_inf = Real(HUGE_VAL);
#endif
enum ZeroOrOne { vl_zero = 0, vl_0 = 0, vl_one = 1, vl_I = 1, vl_1 = 1 };
enum Block { vl_Z = 0, vl_B = 1, vl_block = 1 };
enum Axis { vl_x, vl_y, vl_z, vl_w };
typedef Axis vl_axis;
const uint32_t VL_REF_FLAG = uint32_t(1) << (sizeof(uint32_t) * 8 - 1);
const uint32_t VL_REF_MASK = (~VL_REF_FLAG);
enum { VL_SV_END = -1 };
const uint32_t VL_SV_MAX_INDEX = (1 << 30);
const int VL_MAX_STEPS = 10000;
#endif
/*
File: VLMath.h
Function: Various math definitions for VL
Copyright: Andrew Willmott
*/
#ifndef VL_MATH_H
#define VL_MATH_H
#include <stdlib.h>
// --- Inlines ----------------------------------------------------------------
// additions to arithmetic functions
#ifdef VL_HAS_IEEEFP
#include <ieeefp.h>
#define vl_is_finite(X) finite(X)
#elif defined (__GNUC__) && defined(VL_USE_MISC)
#define vl_is_finite(X) finite(X)
#else
#define vl_is_finite(X) (1)
#endif
#ifdef VL_HAS_DRAND
inline double vl_rand()
{ return drand48()); }
inline double vl_srand(Uint seed)
{ srand48(seed); }
#else
inline double vl_rand()
{ return rand() / (RAND_MAX + 1.0); }
inline void vl_srand(uint32_t seed)
{ srand(seed); }
#endif
inline float len(float x)
{ return (fabsf(x)); }
inline double len(double x)
{ return (fabs(x)); }
template<class T_Value> inline T_Value sqr(T_Value x)
{ return x * x; }
template<class T_Value> inline T_Value cube(T_Value x)
{ return x * x * x; }
inline float sqrlen(float r)
{ return sqr(r); }
inline double sqrlen(double r)
{ return sqr(r); }
inline float lerp(float a, float b, float s)
{ return (1.0f - s) * a + s * b; }
inline double lerp(double a, double b, double s)
{ return (1.0 - s) * a + s * b; }
template<class T_Value> inline T_Value lerp(T_Value x, T_Value y, Real s)
{ return x + (y - x) * s; }
template<> inline float lerp<float>(float x, float y, Real s)
{ return float(x + (y - x) * s); }
template<class T> inline T vl_min(T a, T b)
{
return a < b ? a : b;
}
template<class T> inline T vl_max(T a, T b)
{
return a > b ? a : b;
}
inline double sign(double d)
{
if (d < 0)
return -1.0;
else
return +1.0;
}
inline double sinc(double x)
{
if (fabs(x) < 1.0e-6)
return 1.0;
else
return sin(x) / x;
}
inline float sincf(float x)
{
if (fabsf(x) < 1.0e-6f)
return 1.0f;
else
return sinf(x) / x;
}
#ifndef VL_HAVE_SINCOS
inline void sincosf(double phi, double* sinv, double* cosv)
{
*sinv = sin(phi);
*cosv = cos(phi);
}
inline void sincosf(float phi, float* sinv, float* cosv)
{
*sinv = sinf(phi);
*cosv = cosf(phi);
}
#endif
template<class T_Value> inline T_Value clip(T_Value x, T_Value min, T_Value max)
{
if (x <= min)
return min;
else if (x >= max)
return max;
else
return x;
}
// useful routines
inline void SetReal(float& a, double b)
{ a = float(b); }
inline void SetReal(double& a, double b)
{ a = b; }
template <class S, class T> inline void ConvertVec(const S& u, T& v)
{
for (int i = 0; i < u.Elts(); i++)
v[i] = u[i];
}
template <class T> inline void ConvertVec(const T& u, T& v)
{ v = u; }
template <class S, class T> inline void ConvertMat(const S& m, T& n)
{
for (int i = 0; i < m.Rows(); i++)
ConvertVec(m[i], n[i]);
}
template <class T> inline void ConvertMat(const T& m, T& n)
{ n = m; }
#endif
/*
File: VLVec2.h
Function: Defines a length-2 vector.
Copyright: Andrew Willmott
*/
#ifndef VL_Vec2_H
#define VL_Vec2_H
// --- Vec2 Class -------------------------------------------------------------
class Vec2f
{
public:
// Constructors
Vec2f();
Vec2f(float x, float y); // (x, y)
Vec2f(const Vec2f& v); // Copy constructor
Vec2f(ZeroOrOne k); // v[i] = vl_zero
Vec2f(Axis k); // v[k] = 1
explicit Vec2f(float s);
explicit Vec2f(const float v[]);
// Accessor functions
int Elts() const { return 2; }; // Element count
float& operator [] (int i); // Indexing by row
const float& operator [] (int i) const; // Indexing by row
float* Ref(); // Return pointer to data
const float* Ref() const; // Return pointer to data
// Assignment operators
Vec2f& operator = (const Vec2f& a);
Vec2f& operator = (ZeroOrOne k);
Vec2f& operator = (Axis k);
Vec2f& operator += (const Vec2f& a);
Vec2f& operator -= (const Vec2f& a);
Vec2f& operator *= (const Vec2f& a);
Vec2f& operator *= (float s);
Vec2f& operator /= (const Vec2f& a);
Vec2f& operator /= (float s);
// Comparison operators
bool operator == (const Vec2f& a) const; // v == a?
bool operator != (const Vec2f& a) const; // v != a?
// Arithmetic operators
Vec2f operator + (const Vec2f& a) const; // v + a
Vec2f operator - (const Vec2f& a) const; // v - a
Vec2f operator - () const; // -v
Vec2f operator * (const Vec2f& a) const; // v * a (vx * ax, ...)
Vec2f operator * (float s) const; // v * s
Vec2f operator / (const Vec2f& a) const; // v / a (vx / ax, ...)
Vec2f operator / (float s) const; // v / s
// Initialisers
Vec2f& MakeZero(); // Zero vector
Vec2f& MakeUnit(int i, float k = vl_one);// I[i]
Vec2f& MakeBlock(float k = vl_one); // All-k vector
Vec2f& Normalize(); // normalize vector
// Data
float x;
float y;
};
// --- Vec operators ----------------------------------------------------------
Vec2f operator * (float s, const Vec2f& v);// s * v
float dot (const Vec2f& a, const Vec2f& b); // v . a
float len (const Vec2f& v); // || v ||
float sqrlen (const Vec2f& v); // v . v
Vec2f norm (const Vec2f& v); // v / || v ||
Vec2f norm_safe(const Vec2f& v); // v / || v ||, handles || v || = 0
Vec2f cross (const Vec2f& v); // cross prod.
Vec2f inv (const Vec2f& v); // inverse: 1 / v
Vec2f abs (const Vec2f& v); // abs(v_i)
// --- Inlines ----------------------------------------------------------------
inline float& Vec2f::operator [] (int i)
{
CL_RANGE_MSG(i, 0, 2, "(Vec2::[i]) index out of range");
return (&x)[i];
}
inline const float& Vec2f::operator [] (int i) const
{
CL_RANGE_MSG(i, 0, 2, "(Vec2::[i]) index out of range");
return (&x)[i];
}
inline Vec2f::Vec2f()
{
}
inline Vec2f::Vec2f(float x_in, float y_in) :
x(x_in),
y(y_in)
{}
inline Vec2f::Vec2f(const Vec2f& v) :
x(v.x),
y(v.y)
{}
inline Vec2f::Vec2f(float s) :
x(s),
y(s)
{
}
inline Vec2f::Vec2f(const float v[]) :
x(v[0]),
y(v[1])
{
}
inline float* Vec2f::Ref()
{
return &x;
}
inline const float* Vec2f::Ref() const
{
return &x;
}
inline Vec2f& Vec2f::operator = (const Vec2f& v)
{
x = v.x;
y = v.y;
return *this;
}
inline Vec2f& Vec2f::operator += (const Vec2f& v)
{
x += v.x;
y += v.y;
return *this;
}
inline Vec2f& Vec2f::operator -= (const Vec2f& v)
{
x -= v.x;
y -= v.y;
return *this;
}
inline Vec2f& Vec2f::operator *= (const Vec2f& v)
{
x *= v.x;
y *= v.y;
return *this;
}
inline Vec2f& Vec2f::operator *= (float s)
{
x *= s;
y *= s;
return *this;
}
inline Vec2f& Vec2f::operator /= (const Vec2f& v)
{
x /= v.x;
y /= v.y;
return *this;
}
inline Vec2f& Vec2f::operator /= (float s)
{
x /= s;
y /= s;
return *this;
}
inline Vec2f Vec2f::operator + (const Vec2f& a) const
{
Vec2f result;
result.x = x + a.x;
result.y = y + a.y;
return result;
}
inline Vec2f Vec2f::operator - (const Vec2f& a) const
{
Vec2f result;
result.x = x - a.x;
result.y = y - a.y;
return result;
}
inline Vec2f Vec2f::operator - () const
{
Vec2f result;
result.x = -x;
result.y = -y;
return result;
}
inline Vec2f Vec2f::operator * (const Vec2f& a) const
{
Vec2f result;
result.x = x * a.x;
result.y = y * a.y;
return result;
}
inline Vec2f Vec2f::operator * (float s) const
{
Vec2f result;
result.x = x * s;
result.y = y * s;
return result;
}
inline Vec2f operator * (float s, const Vec2f& v)
{
return v * s;
}
inline Vec2f Vec2f::operator / (const Vec2f& a) const
{
Vec2f result;
result.x = x / a.x;
result.y = y / a.y;
return result;
}
inline Vec2f Vec2f::operator / (float s) const
{
Vec2f result;
result.x = x / s;
result.y = y / s;
return result;
}
inline float dot(const Vec2f& a, const Vec2f& b)
{
return a.x * b.x + a.y * b.y;
}
inline Vec2f cross(const Vec2f& a)
{
Vec2f result;
result.x = -a.y;
result.y = a.x;
return result;
}
inline float len(const Vec2f& v)
{
return sqrt(dot(v, v));
}
inline float sqrlen(const Vec2f& v)
{
return dot(v, v);
}
inline Vec2f norm(const Vec2f& v)
{
CL_ASSERT_MSG(sqrlen(v) > float(vl_zero), "normalising length-zero vector");
return v / len(v);
}
inline Vec2f norm_safe(const Vec2f& v)
{
return v / (len(v) + float(1e-8));
}
inline Vec2f inv(const Vec2f& v)
{
return Vec2f(float(1) / v.x, float(1) / v.y);
}
inline Vec2f abs(const Vec2f& v)
{
return Vec2f(abs(v.x), abs(v.y));
}
inline Vec2f& Vec2f::MakeUnit(int i, float k)
{
if (i == 0)
{ x = k; y = vl_zero; }
else if (i == 1)
{ x = vl_zero; y = k; }
else
CL_ERROR("(Vec2::Unit) illegal unit vector");
return *this;
}
inline Vec2f& Vec2f::MakeZero()
{
x = vl_zero; y = vl_zero;
return *this;
}
inline Vec2f& Vec2f::MakeBlock(float k)
{
x = k; y = k;
return *this;
}
inline Vec2f& Vec2f::Normalize()
{
CL_ASSERT_MSG(sqrlen(*this) > float(vl_zero), "normalising length-zero vector");
*this /= len(*this);
return *this;
}
inline Vec2f::Vec2f(ZeroOrOne k)
{
x = float(k);
y = float(k);
}
inline Vec2f::Vec2f(Axis k)
{
MakeUnit(k, vl_one);
}
inline Vec2f& Vec2f::operator = (ZeroOrOne k)
{
x = float(k); y = float(k);
return *this;
}
inline Vec2f& Vec2f::operator = (Axis k)
{
MakeUnit(k, vl_1);
return *this;
}
inline bool Vec2f::operator == (const Vec2f& a) const
{
return x == a.x && y == a.y;
}
inline bool Vec2f::operator != (const Vec2f& a) const
{
return x != a.x || y != a.y;
}
#endif
/*
File: VLVec3.h
Function: Defines a length-3 vector.
Copyright: Andrew Willmott
*/
#ifndef VL_Vec3_H
#define VL_Vec3_H
// --- Vec3 Class -------------------------------------------------------------
class Vec2f;
class Vec3f
{
public:
// Constructors
Vec3f();
Vec3f(float x, float y, float z);// [x, y, z]
Vec3f(const Vec3f& v); // Copy constructor
Vec3f(const Vec2f& v, float w); // Hom. 2D vector
Vec3f(ZeroOrOne k);
Vec3f(Axis a);
explicit Vec3f(float s);
explicit Vec3f(const float v[]);
// Accessor functions
int Elts() const { return 3; }; // Element count
float& operator [] (int i); // Indexing by row
const float& operator [] (int i) const; // Indexing by row
float* Ref(); // Return pointer to data
const float* Ref() const; // Return pointer to data
// Assignment operators
Vec3f& operator = (const Vec3f& a);
Vec3f& operator = (ZeroOrOne k);
Vec3f& operator += (const Vec3f& a);
Vec3f& operator -= (const Vec3f& a);
Vec3f& operator *= (const Vec3f& a);
Vec3f& operator *= (float s);
Vec3f& operator /= (const Vec3f& a);
Vec3f& operator /= (float s);
// Comparison operators
bool operator == (const Vec3f& a) const; // v == a?
bool operator != (const Vec3f& a) const; // v != a?
bool operator < (const Vec3f& a) const; // v < a?
bool operator >= (const Vec3f& a) const; // v >= a?
// Arithmetic operators
Vec3f operator + (const Vec3f& a) const; // v + a
Vec3f operator - (const Vec3f& a) const; // v - a
Vec3f operator - () const; // -v
Vec3f operator * (const Vec3f& a) const; // v * a (vx * ax, ...)
Vec3f operator * (float s) const; // v * s
Vec3f operator / (const Vec3f& a) const; // v / a (vx / ax, ...)
Vec3f operator / (float s) const; // v / s
// Initialisers
Vec3f& MakeZero(); // Zero vector
Vec3f& MakeUnit(int i, float k = vl_one);// I[i]
Vec3f& MakeBlock(float k = vl_one); // All-k vector
Vec3f& Normalize(); // normalize vector
// Conversion
Vec2f& AsVec2();
const Vec2f& AsVec2() const;
// Data
float x;
float y;
float z;
};
// --- Vec operators ----------------------------------------------------------
Vec3f operator * (float s, const Vec3f& v); // s * v
float dot(const Vec3f& a, const Vec3f& b); // v . a
float len (const Vec3f& v); // || v ||
float sqrlen (const Vec3f& v); // v . v
Vec3f norm (const Vec3f& v); // v / || v ||
Vec3f norm_safe(const Vec3f& v); // v / || v ||, handles || v || = 0
Vec3f cross (const Vec3f& a, const Vec3f& b); // a x b
Vec3f cross_x (const Vec3f& v); // v x e_x
Vec3f cross_y (const Vec3f& v); // v x e_y
Vec3f cross_z (const Vec3f& v); // v x e_z
Vec3f inv (const Vec3f& v); // inverse: 1 / v
Vec2f proj (const Vec3f& v); // homogeneous projection
Vec3f abs (const Vec3f& v); // abs(v_i)
// --- Inlines ----------------------------------------------------------------
inline float& Vec3f::operator [] (int i)
{
CL_RANGE_MSG(i, 0, 3, "(Vec3::[i]) index out of range");
return (&x)[i];
}
inline const float& Vec3f::operator [] (int i) const
{
CL_RANGE_MSG(i, 0, 3, "(Vec3::[i]) index out of range");
return (&x)[i];
}
inline Vec3f::Vec3f()
{
}
inline Vec3f::Vec3f(float x_in, float y_in, float z_in) :
x(x_in),
y(y_in),
z(z_in)
{}
inline Vec3f::Vec3f(const Vec3f& v) :
x(v.x),
y(v.y),
z(v.z)
{}
inline Vec3f::Vec3f(const Vec2f& v, float z_in) :
x(v.x),
y(v.y),
z(z_in)
{}
inline Vec3f::Vec3f(float s) :
x(s),
y(s),
z(s)
{}
inline Vec3f::Vec3f(const float v[]) :
x(v[0]),
y(v[1]),
z(v[2])
{}
inline float* Vec3f::Ref()
{
return &x;
}
inline const float* Vec3f::Ref() const
{
return &x;
}
inline Vec3f& Vec3f::operator = (const Vec3f& v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
inline Vec3f& Vec3f::operator += (const Vec3f& v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
inline Vec3f& Vec3f::operator -= (const Vec3f& v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
inline Vec3f& Vec3f::operator *= (const Vec3f& a)
{
x *= a.x;
y *= a.y;
z *= a.z;
return *this;
}
inline Vec3f& Vec3f::operator *= (float s)
{
x *= s;
y *= s;
z *= s;
return *this;
}
inline Vec3f& Vec3f::operator /= (const Vec3f& a)
{
x /= a.x;
y /= a.y;
z /= a.z;
return *this;
}
inline Vec3f& Vec3f::operator /= (float s)
{
x /= s;
y /= s;
z /= s;
return *this;
}
inline Vec3f Vec3f::operator + (const Vec3f& a) const
{
Vec3f result;
result.x = x + a.x;
result.y = y + a.y;
result.z = z + a.z;
return result;
}
inline Vec3f Vec3f::operator - (const Vec3f& a) const
{
Vec3f result;
result.x = x - a.x;
result.y = y - a.y;
result.z = z - a.z;
return result;
}
inline Vec3f Vec3f::operator - () const
{
Vec3f result;
result.x = -x;
result.y = -y;
result.z = -z;
return result;
}
inline Vec3f Vec3f::operator * (const Vec3f& a) const
{
Vec3f result;
result.x = x * a.x;
result.y = y * a.y;
result.z = z * a.z;
return result;
}
inline Vec3f Vec3f::operator * (float s) const
{
Vec3f result;
result.x = x * s;
result.y = y * s;
result.z = z * s;
return result;
}
inline Vec3f Vec3f::operator / (const Vec3f& a) const
{
Vec3f result;
result.x = x / a.x;
result.y = y / a.y;
result.z = z / a.z;
return result;
}
inline Vec3f Vec3f::operator / (float s) const
{
Vec3f result;
result.x = x / s;
result.y = y / s;
result.z = z / s;
return result;
}
inline Vec3f operator * (float s, const Vec3f& v)
{
return v * s;
}
inline Vec3f& Vec3f::MakeUnit(int n, float k)
{
switch (n)
{
case 0:
{ x = k; y = vl_zero; z = vl_zero; } break;
case 1:
{ x = vl_zero; y = k; z = vl_zero; } break;
case 2:
{ x = vl_zero; y = vl_zero; z = k; } break;
default:
CL_ERROR("(Vec3::Unit) illegal unit vector");
}
return *this;
}
inline Vec3f& Vec3f::MakeZero()
{
x = vl_zero; y = vl_zero; z = vl_zero;
return *this;
}
inline Vec3f& Vec3f::MakeBlock(float k)
{
x = k; y = k; z = k;
return *this;
}
inline Vec3f& Vec3f::Normalize()
{
CL_ASSERT_MSG(sqrlen(*this) > vl_zero, "normalising length-zero vector");
*this /= len(*this);
return *this;
}
inline Vec3f::Vec3f(ZeroOrOne k)
{
x = float(k); y = float(k); z = float(k);
}
inline Vec3f& Vec3f::operator = (ZeroOrOne k)
{
x = float(k); y = float(k); z = float(k);
return *this;
}
inline Vec3f::Vec3f(Axis a)
{
MakeUnit(a, vl_one);
}
inline bool Vec3f::operator == (const Vec3f& a) const
{
return x == a.x && y == a.y && z == a.z;
}
inline bool Vec3f::operator != (const Vec3f& a) const
{
return x != a.x || y != a.y || z != a.z;
}
inline bool Vec3f::operator < (const Vec3f& a) const
{
return x < a.x && y < a.y && z < a.z;