This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslVector.H
1639 lines (1248 loc) · 44.2 KB
/
slVector.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
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//
// Simple Spring Mass System
// -- vector lib
//
// Primary Author: James F. O'Brien (obrienj@cc.gatech.edu)
//
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//
// Copyright (c) 2003-2005, Regents of the University of California. All
// rights reserved.
//
// This software is part of the Berkeley Fluid Animation & Simulation
// Toolkit. The name "Berkeley Fluid Animation & Simulation Toolkit" is
// a trademark of the Regents of the University of California.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// Redistributions in binary form as an executable program, or part of
// an executable program must, when run for the first time by a given
// user, prominently display the above copyright notice, this list of
// conditions and the following disclaimer.
//
// Neither the name of the University of California, Berkeley nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//-------------------------------------------------------------------
//-------------------------------------------------------------------
#ifndef SlVECTOR_is_defined
#define SlVECTOR_is_defined
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Classes from this header:
class SlVector2;
class SlVector3;
class SlVector4;
#include <cmath>
#include <cstdlib>
#include <iostream>
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class SlVector3 {
// Class to store a three dimensional vector.
// Expected uses include point in R3, RGB color, etc.
public:
//----------------------------------------------
// Constructors
inline SlVector3();
inline SlVector3(double d);
inline SlVector3(double d0,double d1,double d2);
inline SlVector3(const SlVector3 &da);
inline SlVector3(const double *da);
// da should point to a double[3] that will be copied
//----------------------------------------------
// Index operators
inline double &operator[](unsigned int i) ;
inline double operator[](unsigned int i) const;
inline double &operator()(unsigned int i) ;
inline double operator()(unsigned int i) const;
inline double x() const;
inline double y() const;
inline double z() const;
//----------------------------------------------
// Assignment and set
inline SlVector3 &set(double d);
inline SlVector3 &set(double d0, double d1, double d2);
inline SlVector3 &set(const SlVector3 &da);
inline SlVector3 &set(const double *da);
// da should point to a double[3] that will be copied
inline SlVector3 &operator=(double d);
inline SlVector3 &operator=(const SlVector3 &da);
inline SlVector3 &operator=(const double *da);
//----------------------------------------------
// Comparison operators
inline int operator==(const SlVector3 &da) const;
inline int operator!=(const SlVector3 &da) const;
inline int operator==(double d) const;
inline int operator!=(double d) const;
//----------------------------------------------
// In place arithmetic
inline SlVector3 &operator+=(double d);
inline SlVector3 &operator-=(double d);
inline SlVector3 &operator*=(double d);
inline SlVector3 &operator/=(double d);
inline SlVector3 &operator+=(const SlVector3 &da);
inline SlVector3 &operator-=(const SlVector3 &da);
inline SlVector3 &operator*=(const SlVector3 &da);
inline SlVector3 &operator/=(const SlVector3 &da);
// Componentwise operations
inline SlVector3 &maxSet(const SlVector3 &da);
inline SlVector3 &minSet(const SlVector3 &da);
// Sets data[i] = max(data[i],da[i]) or min
//----------------------------------------------
// Static methods
inline static unsigned int cycleAxis(unsigned int axis, int direction);
//----------------------------------------------
// Define Components
enum Index { X = 0 , Y = 1 , Z = 2 ,
U = 0 , V = 1 , W = 2 ,
R = 0 , G = 1 , B = 2 };
public:
//----------------------------------------------
// Public data members
double data[3];
//----------------------------------------------
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Operators for class SlVector3
inline SlVector3 operator-(const SlVector3 &a);
inline SlVector3 operator+(const SlVector3 &a,const SlVector3 &b);
inline SlVector3 operator-(const SlVector3 &a,const SlVector3 &b);
inline SlVector3 operator*(const SlVector3 &a,const SlVector3 &b);
inline SlVector3 operator/(const SlVector3 &a,const SlVector3 &b);
inline SlVector3 operator+(const SlVector3 &a, double b);
inline SlVector3 operator-(const SlVector3 &a, double b);
inline SlVector3 operator*(const SlVector3 &a, double b);
inline SlVector3 operator/(const SlVector3 &a, double b);
inline SlVector3 operator+(double a, const SlVector3 &b);
inline SlVector3 operator-(double a, const SlVector3 &b);
inline SlVector3 operator*(double a, const SlVector3 &b);
inline SlVector3 operator/(double a, const SlVector3 &b);
std::istream &operator>>(std::istream &strm, SlVector3 &v);
std::ostream &operator<<(std::ostream &strm,const SlVector3 &v);
//-------------------------------------------------------------------
// Norm type functions for SlVector3
inline double l1Norm(const SlVector3 &a);
inline double l2Norm(const SlVector3 &a);
inline double lInfNorm(const SlVector3 &a);
// Computes the l1, l2 or lInfinity norm of a
inline double mag(const SlVector3 &a);
inline double sqrMag(const SlVector3 &a);
// mag is the l2Norm or magnitude of the vector
// sqrMag is mag^2, which is faster to compute
inline double normalize(SlVector3 &a);
// SETS a = a/mag(a)
//-------------------------------------------------------------------
// Other functions for SlVector3
inline unsigned int dominantAxis(const SlVector3 &v);
inline unsigned int subinantAxis(const SlVector3 &v);
inline unsigned int midinantAxis(const SlVector3 &v);
// Returns the index of the component with the largest,
// smallest or middle value. Note: subinantAxis and
// midinantAxis are nore really words, I made them up.
// If multiple comonents have the same value, then the
// results are not unique.
inline double dot(const SlVector3 &a,const SlVector3 &b);
inline SlVector3 cross(const SlVector3 &a,const SlVector3 &b);
// Compute the dot and cros product of a and b.
inline double box(const SlVector3 &a,const SlVector3 &b,const SlVector3 &c);
// Compute the box (aka tripple) product of a, b, and d.
inline SlVector3 abs(const SlVector3 &a);
// returns a vector with r[i] = abs(a[i])
inline double sum(const SlVector3 &a);
// return a[0]+a[1]+a[2]
inline double max(const SlVector3 &a);
inline double min(const SlVector3 &a);
// Returns the max or min component of a.
inline SlVector3 max(const SlVector3 &a,const SlVector3 &b);
inline SlVector3 min(const SlVector3 &a,const SlVector3 &b);
// Computes a NEW vector by taking the max component in the
// x,y,z direction from a and b. ie: r[i] = max(a[i],b[i])
// Note: signed values are used.
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class SlVector4 {
// Class to store a three dimensional vector.
// Expected uses include point in R3, RGB color, etc.
public:
//----------------------------------------------
// Constructors
inline SlVector4();
inline SlVector4(double d0,double d1,double d2, double d3);
inline SlVector4(const SlVector4 &da);
inline SlVector4(const double *da);
// da should point to a double[4] that will be copied
//----------------------------------------------
// Index operators
inline double &operator[](unsigned int i) ;
inline double operator[](unsigned int i) const;
inline double &operator()(unsigned int i) ;
inline double operator()(unsigned int i) const;
//----------------------------------------------
// Assignment and set
inline SlVector4 &set(double d0, double d1, double d2, double d3);
inline SlVector4 &set(const SlVector4 &da);
inline SlVector4 &set(const double *da);
// da should point to a double[3] that will be copied
inline SlVector4 &operator=(const SlVector4 &da);
inline SlVector4 &operator=(const double *da);
//----------------------------------------------
// Comparison operators
inline int operator==(const SlVector4 &da) const;
inline int operator!=(const SlVector4 &da) const;
//----------------------------------------------
// In place arithmetic
inline SlVector4 &operator+=(double d);
inline SlVector4 &operator-=(double d);
inline SlVector4 &operator*=(double d);
inline SlVector4 &operator/=(double d);
inline SlVector4 &operator+=(const SlVector4 &da);
inline SlVector4 &operator-=(const SlVector4 &da);
inline SlVector4 &operator*=(const SlVector4 &da);
inline SlVector4 &operator/=(const SlVector4 &da);
// Componentwise operations
inline SlVector4 &maxSet(const SlVector4 &da);
inline SlVector4 &minSet(const SlVector4 &da);
// Sets data[i] = max(data[i],da[i]) or min
//----------------------------------------------
// Define Components
enum Index { X = 0 , Y = 1 , Z = 2 , W = 3,
R = 0 , G = 1 , B = 2 , A = 3 };
public:
//----------------------------------------------
// Public data members
double data[4];
//----------------------------------------------
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Operators for class SlVector3
inline SlVector4 operator-(const SlVector4 &a);
inline SlVector4 operator+(const SlVector4 &a,const SlVector4 &b);
inline SlVector4 operator-(const SlVector4 &a,const SlVector4 &b);
inline SlVector4 operator*(const SlVector4 &a,const SlVector4 &b);
inline SlVector4 operator/(const SlVector4 &a,const SlVector4 &b);
inline SlVector4 operator+(const SlVector4 &a, double b);
inline SlVector4 operator-(const SlVector4 &a, double b);
inline SlVector4 operator*(const SlVector4 &a, double b);
inline SlVector4 operator/(const SlVector4 &a, double b);
inline SlVector4 operator+(double a, const SlVector4 &b);
inline SlVector4 operator-(double a, const SlVector4 &b);
inline SlVector4 operator*(double a, const SlVector4 &b);
inline SlVector4 operator/(double a, const SlVector4 &b);
std::istream &operator>>(std::istream &strm, SlVector4 &v);
std::ostream &operator<<(std::ostream &strm,const SlVector4 &v);
//-------------------------------------------------------------------
// Norm type functions for SlVector3
inline double l1Norm(const SlVector4 &a);
inline double l2Norm(const SlVector4 &a);
inline double lInfNorm(const SlVector4 &a);
// Computes the l1, l2 or lInfinity norm of a
inline double mag(const SlVector4 &a);
inline double sqrMag(const SlVector4 &a);
// mag is the l2Norm or magnitude of the vector
// sqrMag is mag^2, which is faster to compute
inline double normalize(SlVector4 &a);
// SETS a = a/mag(a)
inline double dot(const SlVector4 &a,const SlVector4 &b);
inline SlVector4 cross(const SlVector4 &a,const SlVector4 &b);
// Compute the dot and cros product of a and b.
inline double box(const SlVector4 &a,const SlVector4 &b,const SlVector4 &c);
// Compute the box (aka tripple) product of a, b, and d.
inline SlVector4 abs(const SlVector4 &a);
// returns a vector with r[i] = abs(a[i])
inline double sum(const SlVector4 &a);
// return a[0]+a[1]+a[2]
inline double max(const SlVector4 &a);
inline double min(const SlVector4 &a);
// Returns the max or min component of a.
inline SlVector4 max(const SlVector4 &a,const SlVector4 &b);
inline SlVector4 min(const SlVector4 &a,const SlVector4 &b);
// Computes a NEW vector by taking the max component in the
// x,y,z direction from a and b. ie: r[i] = max(a[i],b[i])
// Note: signed values are used.
//-------------------------------------------------------------------
//-------------------------------------------------------------------
class SlVector2 {
// Class to store a two dimensional vector.
// Expected uses include point in R2, etc.
public:
//----------------------------------------------
// Constructors
inline SlVector2();
inline SlVector2(double d);
inline SlVector2(double d0,double d1);
inline SlVector2(const SlVector2 &da);
inline SlVector2(const double *da);
// da should point to a double[2] that will be copied
//----------------------------------------------
// Index operators
inline double &operator[](unsigned int i) ;
inline double operator[](unsigned int i) const;
inline double &operator()(unsigned int i) ;
inline double operator()(unsigned int i) const;
//----------------------------------------------
// Assignment and set
inline SlVector2 &set(double d);
inline SlVector2 &set(double d0, double d1);
inline SlVector2 &set(const SlVector2 &da);
inline SlVector2 &set(const double *da);
// da should point to a double[3] that will be copied
inline SlVector2 &operator=(double d);
inline SlVector2 &operator=(const SlVector2 &da);
inline SlVector2 &operator=(const double *da);
//----------------------------------------------
// Comparison operators
inline int operator==(const SlVector2 &da) const;
inline int operator!=(const SlVector2 &da) const;
inline int operator==(double d) const;
inline int operator!=(double d) const;
//----------------------------------------------
// In place arithmetic
inline SlVector2 &operator+=(double d);
inline SlVector2 &operator-=(double d);
inline SlVector2 &operator*=(double d);
inline SlVector2 &operator/=(double d);
inline SlVector2 &operator+=(const SlVector2 &da);
inline SlVector2 &operator-=(const SlVector2 &da);
inline SlVector2 &operator*=(const SlVector2 &da);
inline SlVector2 &operator/=(const SlVector2 &da);
// Componentwise operations
inline SlVector2 &maxSet(const SlVector2 &da);
inline SlVector2 &minSet(const SlVector2 &da);
// Sets data[i] = max(data[i],da[i]) or min
//----------------------------------------------
// Static methods
inline static unsigned int cycleAxis(unsigned int axis, int direction);
//----------------------------------------------
// Define Components
enum Index { X = 0 , Y = 1 ,
U = 0 , V = 1 };
public:
//----------------------------------------------
// Public data members
double data[2];
//----------------------------------------------
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Operators for class SlVector2
inline SlVector2 operator-(const SlVector2 &a);
inline SlVector2 operator+(const SlVector2 &a, const SlVector2 &b);
inline SlVector2 operator-(const SlVector2 &a, const SlVector2 &b);
inline SlVector2 operator*(const SlVector2 &a, const SlVector2 &b);
inline SlVector2 operator/(const SlVector2 &a, const SlVector2 &b);
inline SlVector2 operator+(const SlVector2 &a, double b);
inline SlVector2 operator-(const SlVector2 &a, double b);
inline SlVector2 operator*(const SlVector2 &a, double b);
inline SlVector2 operator/(const SlVector2 &a, double b);
inline SlVector2 operator+(double a, const SlVector2 &b);
inline SlVector2 operator-(double a, const SlVector2 &b);
inline SlVector2 operator*(double a, const SlVector2 &b);
inline SlVector2 operator/(double a, const SlVector2 &b);
std::istream &operator>>(std::istream &strm, SlVector2 &v);
std::ostream &operator<<(std::ostream &strm,const SlVector2 &v);
//-------------------------------------------------------------------
// Norm type functions for SlVector2
inline double l1Norm(const SlVector2 &a);
inline double l2Norm(const SlVector2 &a);
inline double lInfNorm(const SlVector2 &a);
// Computes the l1, l2 or lInfinity norm of a
inline double mag(const SlVector2 &a);
inline double sqrMag(const SlVector2 &a);
// mag is the l2Norm or magnitude of the vector
// sqrMag is mag^2, which is faster to compute
inline double normalize(SlVector2 &a);
// SETS a = a/mag(a)
//-------------------------------------------------------------------
// Other functions for SlVector2
inline unsigned int dominantAxis(const SlVector2 &v);
inline unsigned int subinantAxis(const SlVector2 &v);
// Returns the index of the component with the largest,
// smallest value. Note: subinantAxis and is not really
// a word, I made it up.
// If multiple comonents have the same value, then the
// results are not unique.
inline double dot(const SlVector2 &a,const SlVector2 &b);
inline double cross(const SlVector2 &a,const SlVector2 &b);
// Compute the dot and cros product of a and b.
inline SlVector2 abs(const SlVector2 &a);
// returns a vector with r[i] = abs(a[i])
inline double sum(const SlVector2 &a);
// return a[0]+a[1]
inline double max(const SlVector2 &a);
inline double min(const SlVector2 &a);
// Returns the max or min component of a.
inline SlVector2 max(const SlVector2 &a,const SlVector2 &b);
inline SlVector2 min(const SlVector2 &a,const SlVector2 &b);
// Computes a NEW vector by taking the max component in the
// x,y,z direction from a and b. ie: r[i] = max(a[i],b[i])
// Note: signed values are used.
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Inline implementation below:::
#ifndef DB_CHECK
#ifdef DEBUG
#define DB_CHECK( C ) { if ( ! (C) ) { abort(); } }
#else
#define DB_CHECK( C ) { }
#endif
#endif
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Inline implementation of SlVector3
inline double &SlVector3::operator[](unsigned int i) {
DB_CHECK(i<3);
return data[i];
}
inline double &SlVector3::operator()(unsigned int i) {
DB_CHECK(i<3);
return data[i];
}
inline double SlVector3::operator[](unsigned int i) const {
DB_CHECK(i<3);
return data[i];
}
inline double SlVector3::operator()(unsigned int i) const {
DB_CHECK(i<3);
return data[i];
}
inline double SlVector3::x() const {
return data[0];
}
inline double SlVector3::y() const {
return data[1];
}
inline double SlVector3::z() const {
return data[2];
}
//-------------------------------------------------------------------
inline SlVector3::SlVector3() {
data[0] = data[1] = data[2] = 0.0;
}
inline SlVector3::SlVector3(double d) {
data[0] = data[1] = data[2] = d;
}
inline SlVector3::SlVector3(double d0,double d1,double d2) {
data[0] = d0;
data[1] = d1;
data[2] = d2;
}
inline SlVector3::SlVector3(const SlVector3 &da) {
data[0] = da[0];
data[1] = da[1];
data[2] = da[2];
}
inline SlVector3::SlVector3(const double *da) {
data[0] = da[0];
data[1] = da[1];
data[2] = da[2];
}
//-------------------------------------------------------------------
inline SlVector3 &SlVector3::set(double d) {
data[0] = d;
data[1] = d;
data[2] = d;
return (*this);
}
inline SlVector3 &SlVector3::set(double d0, double d1, double d2) {
data[0] = d0;
data[1] = d1;
data[2] = d2;
return (*this);
}
inline SlVector3 &SlVector3::set(const SlVector3 &da) {
data[0] = da[0];
data[1] = da[1];
data[2] = da[2];
return (*this);
}
inline SlVector3 &SlVector3::set(const double *da) {
data[0] = da[0];
data[1] = da[1];
data[2] = da[2];
return (*this);
}
//-------------------------------------------------------------------
inline SlVector3 &SlVector3::operator=(double d) {
return set(d);
}
inline SlVector3 &SlVector3::operator=(const SlVector3 &da) {
return set(da);
}
inline SlVector3 &SlVector3::operator=(const double *da) {
return set(da);
}
//-------------------------------------------------------------------
inline int SlVector3::operator==(const SlVector3 &da) const {
return ((data[0] == da[0]) &&
(data[1] == da[1]) &&
(data[2] == da[2]));
}
inline int SlVector3::operator!=(const SlVector3 &da) const {
return ((data[0] != da[0]) ||
(data[1] != da[1]) ||
(data[2] != da[2]));
}
inline int SlVector3::operator==(double d) const {
return ((data[0] == d) &&
(data[1] == d) &&
(data[2] == d));
}
inline int SlVector3::operator!=(double d) const {
return ((data[0] != d) ||
(data[1] != d) ||
(data[2] != d));
}
//-------------------------------------------------------------------
inline SlVector3 &SlVector3::operator+=(double d) {
data[0] += d;
data[1] += d;
data[2] += d;
return (*this);
}
inline SlVector3 &SlVector3::operator-=(double d) {
data[0] -= d;
data[1] -= d;
data[2] -= d;
return (*this);
}
inline SlVector3 &SlVector3::operator*=(double d) {
data[0] *= d;
data[1] *= d;
data[2] *= d;
return (*this);
}
inline SlVector3 &SlVector3::operator/=(double d) {
data[0] /= d;
data[1] /= d;
data[2] /= d;
return (*this);
}
//-------------------------------------------------------------------
inline SlVector3 &SlVector3::operator+=(const SlVector3 &da) {
data[0] += da[0];
data[1] += da[1];
data[2] += da[2];
return (*this);
}
inline SlVector3 &SlVector3::operator-=(const SlVector3 &da) {
data[0] -= da[0];
data[1] -= da[1];
data[2] -= da[2];
return (*this);
}
inline SlVector3 &SlVector3::operator*=(const SlVector3 &da) {
data[0] *= da[0];
data[1] *= da[1];
data[2] *= da[2];
return (*this);
}
inline SlVector3 &SlVector3::operator/=(const SlVector3 &da) {
data[0] /= da[0];
data[1] /= da[1];
data[2] /= da[2];
return (*this);
}
//-------------------------------------------------------------------
inline SlVector3 &SlVector3::maxSet(const SlVector3 &da) {
if (da[0] > data[0]) data[0] = da[0];
if (da[1] > data[1]) data[1] = da[1];
if (da[2] > data[2]) data[2] = da[2];
return (*this);
}
inline SlVector3 &SlVector3::minSet(const SlVector3 &da) {
if (da[0] < data[0]) data[0] = da[0];
if (da[1] < data[1]) data[1] = da[1];
if (da[2] < data[2]) data[2] = da[2];
return (*this);
}
//-------------------------------------------------------------------
inline unsigned int SlVector3::cycleAxis(unsigned int axis, int direction) {
switch (axis+direction) {
case 0: case 3: case 6: return 0;
case 1: case 4: case 7: return 1;
case 2: case 5: case 8: return 2;
default: return (axis+direction)%3;
}
}
//-------------------------------------------------------------------
inline SlVector3 operator-(const SlVector3 &a) {
return SlVector3(-a[0],-a[1],-a[2]);
}
//-------------------------------------------------------------------
inline SlVector3 operator+(const SlVector3 &a,const SlVector3 &b) {
return SlVector3(a[0] + b[0], a[1] + b[1], a[2] + b[2]);
}
inline SlVector3 operator-(const SlVector3 &a,const SlVector3 &b) {
return SlVector3(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
}
inline SlVector3 operator*(const SlVector3 &a,const SlVector3 &b){
return SlVector3(a[0] * b[0], a[1] * b[1], a[2] * b[2]);
}
inline SlVector3 operator/(const SlVector3 &a,const SlVector3 &b){
return SlVector3(a[0] / b[0], a[1] / b[1], a[2] / b[2]);
}
//-------------------------------------------------------------------
inline SlVector3 operator+(const SlVector3 &a,double b){
return SlVector3(a[0] + b, a[1] + b, a[2] + b);
}
inline SlVector3 operator-(const SlVector3 &a,double b){
return SlVector3(a[0] - b, a[1] - b, a[2] - b);
}
inline SlVector3 operator*(const SlVector3 &a,double b){
return SlVector3(a[0] * b, a[1] * b, a[2] * b);
}
inline SlVector3 operator/(const SlVector3 &a,double b){
return SlVector3(a[0] / b, a[1] / b, a[2] / b);
}
//-------------------------------------------------------------------
inline SlVector3 operator+(double a,const SlVector3 &b){
return SlVector3(a + b[0], a + b[1], a + b[2]);
}
inline SlVector3 operator-(double a,const SlVector3 &b){
return SlVector3(a - b[0], a - b[1], a - b[2]);
}
inline SlVector3 operator*(double a,const SlVector3 &b){
return SlVector3(a * b[0], a * b[1], a * b[2]);
}
inline SlVector3 operator/(double a,const SlVector3 &b){
return SlVector3(a / b[0], a / b[1], a / b[2]);
}
//-------------------------------------------------------------------
inline double l1Norm(const SlVector3 &a) {
return (((a[0]>0)?a[0]:-a[0])+
((a[1]>0)?a[1]:-a[1])+
((a[2]>0)?a[2]:-a[2]));
}
inline double l2Norm(const SlVector3 &a) {
return sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
}
inline double lInfNorm(const SlVector3 &a) {
return max(abs(a));
}
inline double mag(const SlVector3 &a) {
return sqrt(a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
}
inline double sqrMag(const SlVector3 &a) {
return (a[0] * a[0] + a[1] * a[1] + a[2] * a[2]);
}
inline double normalize(SlVector3 &a) {
double m = mag(a);
if (m != 0) a /= m;
return m;
}
//-------------------------------------------------------------------
inline unsigned int dominantAxis(const SlVector3 &v) {
double x,y,z;
if (v[0]>0) x = v[0]; else x = -v[0];
if (v[1]>0) y = v[1]; else y = -v[1];
if (v[2]>0) z = v[2]; else z = -v[2];
return ( x > y ) ? (( x > z ) ? 0 : 2) : (( y > z ) ? 1 : 2 );
}
inline unsigned int subinantAxis(const SlVector3 &v) {
double x,y,z;
if (v[0]>0) x = v[0]; else x = -v[0];
if (v[1]>0) y = v[1]; else y = -v[1];
if (v[2]>0) z = v[2]; else z = -v[2];
return ( x < y ) ? (( x < z ) ? 0 : 2) : (( y < z ) ? 1 : 2 );
}
inline unsigned int midinantAxis(const SlVector3 &v) {
double x,y,z;
if (v[0]>0) x = v[0]; else x = -v[0];
if (v[1]>0) y = v[1]; else y = -v[1];
if (v[2]>0) z = v[2]; else z = -v[2];
unsigned int d = ( x > y ) ? (( x > z ) ? 0 : 2) : (( y > z ) ? 1 : 2 );
unsigned int s = ( x < y ) ? (( x < z ) ? 0 : 2) : (( y < z ) ? 1 : 2 );
unsigned int m=0;
if (d==0) {
if (s!= 1) m = 1; else m = 2;
}else if (d==1) {
if (s!= 0) m = 0; else m = 2;
}else if (d==2) {
if (s!= 0) m = 0; else m = 1;
}
return m;
}
//-------------------------------------------------------------------
inline double dot(const SlVector3 &a,const SlVector3 &b) {
return (a[0] * b[0] + a[1] * b[1] + a[2] * b[2]);
}
inline SlVector3 cross(const SlVector3 &a,const SlVector3 &b) {
return SlVector3(a[1] * b[2] - b[1] * a[2],
a[2] * b[0] - b[2] * a[0],
a[0] * b[1] - b[0] * a[1]);
}
//-------------------------------------------------------------------
inline double box(const SlVector3 &a,const SlVector3 &b,const SlVector3 &c) {
return dot(cross(a,b),c);
}
//-------------------------------------------------------------------
inline SlVector3 abs(const SlVector3 &a) {
return SlVector3(((a[0]>0)?a[0]:-a[0]),
((a[1]>0)?a[1]:-a[1]),
((a[2]>0)?a[2]:-a[2]));
}
inline double sum(const SlVector3 &a) {
return a[0]+a[1]+a[2];
}
//-------------------------------------------------------------------
inline double max(const SlVector3 &a) {
return ((a[0]>a[1])?((a[0]>a[2])?a[0]:a[2]):(a[1]>a[2])?a[1]:a[2]);
}
inline double min(const SlVector3 &a) {
return ((a[0]<a[1])?((a[0]<a[2])?a[0]:a[2]):(a[1]<a[2])?a[1]:a[2]);
}
//-------------------------------------------------------------------
inline SlVector3 max(const SlVector3 &a,const SlVector3 &b) {
return SlVector3((a[0]>b[0])?a[0]:b[0],
(a[1]>b[1])?a[1]:b[1],
(a[2]>b[2])?a[2]:b[2]);
}
inline SlVector3 min(const SlVector3 &a,const SlVector3 &b) {
return SlVector3((a[0]<b[0])?a[0]:b[0],
(a[1]<b[1])?a[1]:b[1],
(a[2]<b[2])?a[2]:b[2]);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Inline implementation of SlVector4
inline double &SlVector4::operator[](unsigned int i) {
DB_CHECK(i<4);
return data[i];
}
inline double &SlVector4::operator()(unsigned int i) {
DB_CHECK(i<4);
return data[i];
}
inline double SlVector4::operator[](unsigned int i) const {
DB_CHECK(i<4);
return data[i];
}