1 /*
2
3 Copyright (C) 1991-2001 and beyond by Bungie Studios, Inc.
4 and the "Aleph One" developers.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 This license is contained in the file "COPYING",
17 which is included with this source code; it is available online at
18 http://www.gnu.org/licenses/gpl.html
19
20 Vector-operations template functions,
21 by Loren Petrich,
22 March 16, 2000
23
24 This contains template
25 */
26
27 #ifndef VECTOR_OPERATIONS
28 #define VECTOR_OPERATIONS
29
30
31 // Sets V1 = V0
VecCopy(const T0 * V0,T1 * V1)32 template <class T0, class T1> inline void VecCopy(const T0* V0, T1* V1)
33 {
34 V1[0] = T1(V0[0]);
35 V1[1] = T1(V0[1]);
36 V1[2] = T1(V0[2]);
37 }
38
39 // Sets V2 = V0 + V1
VecAdd(const T0 * V0,const T1 * V1,T2 * V2)40 template <class T0, class T1, class T2> inline void VecAdd(const T0* V0, const T1* V1, T2* V2)
41 {
42 V2[0] = T2(V0[0] + V1[0]);
43 V2[1] = T2(V0[1] + V1[1]);
44 V2[2] = T2(V0[2] + V1[2]);
45 }
46
47 // Sets V2 = V0 - V1
VecSub(const T0 * V0,const T1 * V1,T2 * V2)48 template <class T0, class T1, class T2> inline void VecSub(const T0* V0, const T1* V1, T2* V2)
49 {
50 V2[0] = T2(V0[0] - V1[0]);
51 V2[1] = T2(V0[1] - V1[1]);
52 V2[2] = T2(V0[2] - V1[2]);
53 }
54
55 // Sets V0 += V1
VecAddTo(T0 * V0,const T1 * V1)56 template <class T0, class T1> inline void VecAddTo(T0* V0, const T1* V1)
57 {
58 V0[0] += V1[0];
59 V0[1] += V1[1];
60 V0[2] += V1[2];
61 }
62
63 // Sets V0 -= V1
VecSubFrom(T0 * V0,const T1 * V1)64 template <class T0, class T1> inline void VecSubFrom(T0* V0, const T1* V1)
65 {
66 V0[0] -= V1[0];
67 V0[1] -= V1[1];
68 V0[2] -= V1[2];
69 }
70
71 // Sets V1 = V0 * S
VecScalarMult(const T0 * V0,const TS & S,T1 * V1)72 template <class T0, class TS, class T1> inline void VecScalarMult(const T0* V0, const TS& S, T1* V1)
73 {
74 V1[0] = T1(S*V0[0]);
75 V1[1] = T1(S*V0[1]);
76 V1[2] = T1(S*V0[2]);
77 }
78
79 // Sets V1 *= S
VecScalarMultTo(T * V,const TS & S)80 template <class T, class TS> inline void VecScalarMultTo(T* V, const TS& S)
81 {
82 V[0] *= S;
83 V[1] *= S;
84 V[2] *= S;
85 }
86
87 // Returns V0.V1
ScalarProd(const T * V0,const T * V1)88 template <class T> inline T ScalarProd(const T* V0, const T* V1)
89 {return (V0[0]*V1[0] + V0[1]*V1[1] + V0[2]*V1[2]);}
90
91 // Sets V2 = V0 x V1
VectorProd(const T0 * V0,const T1 * V1,T2 * V2)92 template <class T0, class T1, class T2> inline void VectorProd(const T0* V0, const T1* V1, T2* V2)
93 {
94 V2[0] = T2(V0[1]*V1[2] - V0[2]*V1[1]);
95 V2[1] = T2(V0[2]*V1[0] - V0[0]*V1[2]);
96 V2[2] = T2(V0[0]*V1[1] - V0[1]*V1[0]);
97 }
98
99
100 #endif
101