Newton Dynamics  4.00
dTemplateVector.h
1 /* Copyright (c) <2003-2019> <Julio Jerez, 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, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef __D_TEMPLATE_VECTOR_H__
23 #define __D_TEMPLATE_VECTOR_H__
24 
25 #include "dCoreStdafx.h"
26 #include "dTypes.h"
27 #include "dClassAlloc.h"
28 
29 //#define dCheckVector(x) (dCheckFloat(x[0]) && dCheckFloat(x[1]) && dCheckFloat(x[2]) && dCheckFloat(x[3]))
30 
31 template<class T>
33 {
34  public:
35  dTemplateVector ()
36  {
37  }
38 
39  dTemplateVector (const T* const ptr)
40  :m_x(ptr[0]), m_y(ptr[1]), m_z(ptr[2]), m_w (ptr[3])
41  {
42  //dAssert (dCheckVector ((*this)));
43  }
44 
46  :m_x(copy.m_x), m_y(copy.m_y), m_z(copy.m_z), m_w (copy.m_w)
47  {
48  // dAssert (dCheckVector ((*this)));
49  }
50 
51  dTemplateVector (T x, T y, T z, T w)
52  :m_x(x), m_y(y), m_z(z), m_w (w)
53  {
54  }
55 
56  T& operator[] (dInt32 i)
57  {
58  dAssert (i < 4);
59  dAssert (i >= 0);
60  return (&m_x)[i];
61  }
62 
63  const T& operator[] (dInt32 i) const
64  {
65  dAssert (i < 4);
66  dAssert (i >= 0);
67  return (&m_x)[i];
68  }
69 
70  T GetScalar() const
71  {
72  return m_x;
73  }
74 
75  dTemplateVector<T> Scale (T scale) const
76  {
77  return dTemplateVector<T> (m_x * scale, m_y * scale, m_z * scale, m_w * scale);
78  }
79 
80  dTemplateVector<T> operator+ (const dTemplateVector<T>& B) const
81  {
82  return dTemplateVector<T> (m_x + B.m_x, m_y + B.m_y, m_z + B.m_z, m_w + B.m_w);
83  }
84 
85  dTemplateVector<T>& operator+= (const dTemplateVector<T>& A)
86  {
87  return (*this = dTemplateVector<T> (m_x + A.m_x, m_y + A.m_y, m_z + A.m_z, m_w + A.m_w));
88  }
89 
90  dTemplateVector<T> operator- (const dTemplateVector<T>& A) const
91  {
92  return dTemplateVector<T> (m_x - A.m_x, m_y - A.m_y, m_z - A.m_z, m_w - A.m_w);
93  }
94 
95  dTemplateVector<T>& operator-= (const dTemplateVector<T>& A)
96  {
97  return (*this = dTemplateVector<T> (m_x - A.m_x, m_y - A.m_y, m_z - A.m_z, m_w - A.m_w));
98  }
99 
100  dTemplateVector<T> operator* (const dTemplateVector<T>& B) const
101  {
102  return dTemplateVector<T>(m_x * B.m_x, m_y * B.m_y, m_z * B.m_z, m_w * B.m_w);
103  }
104 
105  dTemplateVector<T> operator*= (const dTemplateVector<T>& B) const
106  {
107  return (*this = dTemplateVector<T>(m_x * B.m_x, m_y * B.m_y, m_z * B.m_z, m_w * B.m_w));
108  }
109 
110  dTemplateVector<T> AddHorizontal() const
111  {
112  T val(m_x + m_y + m_z + m_w);
113  return dTemplateVector<T>(val, val, val, val);
114  }
115 
116  dTemplateVector<T> MulAdd(const dTemplateVector<T>& A, const dTemplateVector<T>& B) const
117  {
118  return *this + A * B;
119  }
120 
121  dTemplateVector<T> MulSub(const dTemplateVector<T>& A, const dTemplateVector<T>& B) const
122  {
123  return *this - A * B;
124  }
125 
126  // return cross product
127  dTemplateVector<T> CrossProduct (const dTemplateVector<T>& B) const
128  {
129  return dTemplateVector<T> (m_y * B.m_z - m_z * B.m_y,
130  m_z * B.m_x - m_x * B.m_z,
131  m_x * B.m_y - m_y * B.m_x, m_w);
132  }
133 
134  dTemplateVector<T> CrossProduct(const dTemplateVector &A, const dTemplateVector &B) const
135  {
136  T cofactor[3][3];
137  T array[4][4];
138 
139  const dTemplateVector<T>& me = *this;
140  for (dInt32 i = 0; i < 4; i++) {
141  array[0][i] = me[i];
142  array[1][i] = A[i];
143  array[2][i] = B[i];
144  array[3][i] = T(1.0f);
145  }
146 
147  dTemplateVector<T> normal;
148  T sign = T(-1.0f);
149  for (dInt32 i = 0; i < 4; i++)
150  {
151  for (dInt32 j = 0; j < 3; j++)
152  {
153  dInt32 k0 = 0;
154  for (dInt32 k = 0; k < 4; k++)
155  {
156  if (k != i)
157  {
158  cofactor[j][k0] = array[j][k];
159  k0++;
160  }
161  }
162  }
163  T x = cofactor[0][0] * (cofactor[1][1] * cofactor[2][2] - cofactor[1][2] * cofactor[2][1]);
164  T y = cofactor[0][1] * (cofactor[1][2] * cofactor[2][0] - cofactor[1][0] * cofactor[2][2]);
165  T z = cofactor[0][2] * (cofactor[1][0] * cofactor[2][1] - cofactor[1][1] * cofactor[2][0]);
166  T det = x + y + z;
167 
168  normal[i] = sign * det;
169  sign *= T(-1.0f);
170  }
171 
172  return normal;
173  }
174 
175  // return dot 4d dot product
176  dTemplateVector<T> DotProduct (const dTemplateVector &A) const
177  {
178  T val (m_x * A.m_x + m_y * A.m_y + m_z * A.m_z + m_w * A.m_w);
179  return dTemplateVector<T> (val, val, val, val);
180  }
181 
182 
183  T GetMax () const
184  {
185  return dMax(dMax(m_x, m_y), dMax(m_z, m_w));
186  }
187 
188  dTemplateVector<T> GetMax(const dTemplateVector<T>& data) const
189  {
190  return dTemplateVector<T>((m_x > data.m_x) ? m_x : data.m_x, (m_y > data.m_y) ? m_y : data.m_y, (m_z > data.m_z) ? m_z : data.m_z, (m_w > data.m_w) ? m_w : data.m_w);
191  }
192 
193  dTemplateVector<T> GetMin(const dTemplateVector<T>& data) const
194  {
195  return dTemplateVector<T>((m_x < data.m_x) ? m_x : data.m_x, (m_y < data.m_y) ? m_y : data.m_y, (m_z < data.m_z) ? m_z : data.m_z, (m_w < data.m_w) ? m_w : data.m_w);
196  }
197 
198  // check validity of floats
199 #ifdef _DEBUG
200  void Trace (char* const name) const
201  {
202  dTrace (("%s %f %f %f %f\n", name, m_x, m_y, m_z, m_w));
203  }
204 #endif
205 
206  T m_x;
207  T m_y;
208  T m_z;
209  T m_w;
210 };
211 
212 #endif
dClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: dClassAlloc.h:29
dTemplateVector
Definition: dTemplateVector.h:33