My Project: dynamics.mod/NewtonDynamics/packages/dMath/dVector.h Source File

My Project

dVector.h
Go to the documentation of this file.
1 /* Copyright (c) <2009> <Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely
10 */
11
12 #ifndef __dVector__
13 #define __dVector__
14
15
16 #include "dMathDefines.h"
17
18
19 // small but very effective 4 dimensional template vector class
20
21 template<class T>
22 class TemplateVector
23 {
24 public:
25 TemplateVector ();
26 TemplateVector (const T *ptr);
27 TemplateVector (T m_x, T m_y, T m_z, T m_w);
28 TemplateVector Scale (T s) const;
29
30 T& operator[] (int i);
31 const T& operator[] (int i) const;
32
33 TemplateVector operator+ (const TemplateVector &A) const;
34 TemplateVector operator- (const TemplateVector &A) const;
35 TemplateVector &operator+= (const TemplateVector &A);
36 TemplateVector &operator-= (const TemplateVector &A);
37
38 // return dot product
39 T operator% (const TemplateVector &A) const;
40
41 // return cross product
42 TemplateVector operator* (const TemplateVector &B) const;
43
44 // component wise multiplication
45 TemplateVector CompProduct (const TemplateVector &A) const;
46
47 T m_x;
48 T m_y;
49 T m_z;
50 T m_w;
51 };
52
53 D_MSC_VECTOR_ALIGMENT
54 class dVector: public TemplateVector<dFloat>
55 {
56 public:
57 dVector();
58 dVector (const TemplateVector<dFloat>& v);
59 dVector (const dFloat *ptr);
60 dVector (dFloat x, dFloat y, dFloat z, dFloat w = 1.0);
61 };
62
63
64 class dBigVector: public TemplateVector<double>
65 {
66 public:
67 dBigVector(){};
68 dBigVector (const TemplateVector<dFloat>& v)
69 :TemplateVector<dFloat64> (v.m_x, v.m_y, v.m_z, v.m_w)
70 {
71 }
72
73 #ifndef _NEWTON_USE_DOUBLE
74 dBigVector (const TemplateVector<dFloat64>& v)
75 :TemplateVector<dFloat64> (v.m_x, v.m_y, v.m_z, v.m_w)
76 {
77 }
78 #endif
79
80 // dBigVector (const dFloat *ptr);
81 dBigVector (dFloat x, dFloat y, dFloat z, dFloat w = 1.0)
82 :TemplateVector<dFloat64> (x, y, z, w)
83 {
84 }
85
86 #ifndef _NEWTON_USE_DOUBLE
87 dBigVector (dFloat64 x, dFloat64 y, dFloat64 z, dFloat64 w = 1.0)
88 :TemplateVector<dFloat64> (x, y, z, w)
89 {
90 }
91 #endif
92 };
93
94
95
96
97 template<class T>
98 TemplateVector<T>::TemplateVector() {}
99
100 template<class T>
101 TemplateVector<T>::TemplateVector(const T *ptr)
102 :m_x (ptr[0]), m_y(ptr[1]), m_z(ptr[2]), m_w(0.0f)
103 {
104 }
105
106 template<class T>
107 TemplateVector<T>::TemplateVector(T x, T y, T z, T w)
108 :m_x (x), m_y(y), m_z(z), m_w(w)
109 {
110 }
111
112
113 template<class T>
114 T& TemplateVector<T>::operator[] (int i)
115 {
116 return (&m_x)[i];
117 }
118
119 template<class T>
120 const T& TemplateVector<T>::operator[] (int i) const
121 {
122 return (&m_x)[i];
123 }
124
125 template<class T>
126 TemplateVector<T> TemplateVector<T>::Scale (T scale) const
127 {
128 return TemplateVector<T> (m_x * scale, m_y * scale, m_z * scale, m_w);
129 }
130
131
132 template<class T>
133 TemplateVector<T> TemplateVector<T>::operator+ (const TemplateVector<T>& B) const
134 {
135 return TemplateVector<T> (m_x + B.m_x, m_y + B.m_y, m_z + B.m_z, m_w);
136 }
137
138 template<class T>
139 TemplateVector<T>& TemplateVector<T>::operator+= (const TemplateVector<T>& A)
140 {
141 m_x += A.m_x;
142 m_y += A.m_y;
143 m_z += A.m_z;
144 return *this;
145 }
146
147 template<class T>
148 TemplateVector<T> TemplateVector<T>::operator- (const TemplateVector<T>& A) const
149 {
150 return TemplateVector<T> (m_x - A.m_x, m_y - A.m_y, m_z - A.m_z, m_w);
151 }
152
153 template<class T>
154 TemplateVector<T>& TemplateVector<T>::operator-= (const TemplateVector<T>& A)
155 {
156 m_x -= A.m_x;
157 m_y -= A.m_y;
158 m_z -= A.m_z;
159 return *this;
160 }
161
162
163 template<class T>
164 T TemplateVector<T>::operator% (const TemplateVector<T>& A) const
165 {
166 return m_x * A.m_x + m_y * A.m_y + m_z * A.m_z;
167 }
168
169
170 template<class T>
171 TemplateVector<T> TemplateVector<T>::operator* (const TemplateVector<T>& B) const
172 {
173 return TemplateVector<T> (m_y * B.m_z - m_z * B.m_y,
174 m_z * B.m_x - m_x * B.m_z,
175 m_x * B.m_y - m_y * B.m_x, m_w);
176 }
177
178
179
180 template<class T>
181 TemplateVector<T> TemplateVector<T>::CompProduct (const TemplateVector<T>& A) const
182 {
183 return TemplateVector<T> (m_x * A.m_x, m_y * A.m_y, m_z * A.m_z, A.m_w);
184 }
185
186
187
188 inline dVector::dVector()
189 :TemplateVector<dFloat>()
190 {
191 }
192
193 inline dVector::dVector (const TemplateVector<dFloat>& v)
194 :TemplateVector<dFloat>(v)
195 {
196 }
197
198 inline dVector::dVector (const dFloat *ptr)
199 :TemplateVector<dFloat>(ptr)
200 {
201 }
202
203 inline dVector::dVector (dFloat x, dFloat y, dFloat z, dFloat w)
204 :TemplateVector<dFloat>(x, y, z, w)
205 {
206 }
207
208 #endif
209
dVector::dVector
dVector()
Definition: dVector.h:188
TemplateVector::operator+
TemplateVector operator+(const TemplateVector &A) const
Definition: dVector.h:133
dBigVector::dBigVector
dBigVector(dFloat x, dFloat y, dFloat z, dFloat w=1.0)
Definition: dVector.h:81
TemplateVector::m_x
T m_x
Definition: dVector.h:47
TemplateVector::m_z
T m_z
Definition: dVector.h:49
dFloat
float dFloat
Definition: dMathDefines.h:27
dMathDefines.h
TemplateVector
Definition: dVector.h:22
dBigVector::dBigVector
dBigVector(dFloat64 x, dFloat64 y, dFloat64 z, dFloat64 w=1.0)
Definition: dVector.h:87
TemplateVector::operator*
TemplateVector operator*(const TemplateVector &B) const
Definition: dVector.h:171
TemplateVector::Scale
TemplateVector Scale(T s) const
Definition: dVector.h:126
TemplateVector::TemplateVector
TemplateVector()
Definition: dVector.h:98
dFloat64
double dFloat64
Definition: dMathDefines.h:32
dBigVector::dBigVector
dBigVector()
Definition: dVector.h:67
TemplateVector::CompProduct
TemplateVector CompProduct(const TemplateVector &A) const
Definition: dVector.h:181
s
sem_t s
Definition: semaphore3.c:78
TemplateVector::m_y
T m_y
Definition: dVector.h:48
D_MSC_VECTOR_ALIGMENT
#define D_MSC_VECTOR_ALIGMENT
Definition: dMathDefines.h:52
TemplateVector::operator-=
TemplateVector & operator-=(const TemplateVector &A)
Definition: dVector.h:154
TemplateVector::operator%
T operator%(const TemplateVector &A) const
Definition: dVector.h:164
dBigVector
Definition: dVector.h:64
dBigVector::dBigVector
dBigVector(const TemplateVector< dFloat > &v)
Definition: dVector.h:68
TemplateVector::operator+=
TemplateVector & operator+=(const TemplateVector &A)
Definition: dVector.h:139
TemplateVector::operator-
TemplateVector operator-(const TemplateVector &A) const
Definition: dVector.h:148
TemplateVector::m_w
T m_w
Definition: dVector.h:50
TemplateVector::operator[]
T & operator[](int i)
Definition: dVector.h:114
dVector
Definition: dVector.h:54
dBigVector::dBigVector
dBigVector(const TemplateVector< dFloat64 > &v)
Definition: dVector.h:74