Newton Dynamics  4.00
ndTemplateVector.h
1 /* Copyright (c) <2003-2022> <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 __ND_TEMPLATE_VECTOR_H__
23 #define __ND_TEMPLATE_VECTOR_H__
24 
25 #include "ndCoreStdafx.h"
26 #include "ndTypes.h"
27 #include "ndClassAlloc.h"
28 
29 template<class T>
31 {
32  public:
34  {
35  }
36 
37  ndTemplateVector (const T* const ptr)
38  :m_x(ptr[0]), m_y(ptr[1]), m_z(ptr[2]), m_w (ptr[3])
39  {
40  //ndAssert (dCheckVector ((*this)));
41  }
42 
44  :m_x(copy.m_x), m_y(copy.m_y), m_z(copy.m_z), m_w (copy.m_w)
45  {
46  // ndAssert (dCheckVector ((*this)));
47  }
48 
49  ndTemplateVector (T x, T y, T z, T w)
50  :m_x(x), m_y(y), m_z(z), m_w (w)
51  {
52  }
53 
54  T& operator[] (ndInt32 i)
55  {
56  ndAssert (i < 4);
57  ndAssert (i >= 0);
58  return (&m_x)[i];
59  }
60 
61  const T& operator[] (ndInt32 i) const
62  {
63  ndAssert (i < 4);
64  ndAssert (i >= 0);
65  return (&m_x)[i];
66  }
67 
68  T GetScalar() const
69  {
70  return m_x;
71  }
72 
73  ndTemplateVector<T> Scale (T scale) const
74  {
75  return ndTemplateVector<T> (m_x * scale, m_y * scale, m_z * scale, m_w * scale);
76  }
77 
78  ndTemplateVector<T> operator+ (const ndTemplateVector<T>& B) const
79  {
80  return ndTemplateVector<T> (m_x + B.m_x, m_y + B.m_y, m_z + B.m_z, m_w + B.m_w);
81  }
82 
83  ndTemplateVector<T>& operator+= (const ndTemplateVector<T>& A)
84  {
85  return (*this = ndTemplateVector<T> (m_x + A.m_x, m_y + A.m_y, m_z + A.m_z, m_w + A.m_w));
86  }
87 
88  ndTemplateVector<T> operator- (const ndTemplateVector<T>& A) const
89  {
90  return ndTemplateVector<T> (m_x - A.m_x, m_y - A.m_y, m_z - A.m_z, m_w - A.m_w);
91  }
92 
93  ndTemplateVector<T>& operator-= (const ndTemplateVector<T>& A)
94  {
95  return (*this = ndTemplateVector<T> (m_x - A.m_x, m_y - A.m_y, m_z - A.m_z, m_w - A.m_w));
96  }
97 
98  ndTemplateVector<T> operator* (const ndTemplateVector<T>& B) const
99  {
100  return ndTemplateVector<T>(m_x * B.m_x, m_y * B.m_y, m_z * B.m_z, m_w * B.m_w);
101  }
102 
103  ndTemplateVector<T> operator*= (const ndTemplateVector<T>& B) const
104  {
105  return (*this = ndTemplateVector<T>(m_x * B.m_x, m_y * B.m_y, m_z * B.m_z, m_w * B.m_w));
106  }
107 
108  ndTemplateVector<T> AddHorizontal() const
109  {
110  T val(m_x + m_y + m_z + m_w);
111  return ndTemplateVector<T>(val, val, val, val);
112  }
113 
114  ndTemplateVector<T> MulAdd(const ndTemplateVector<T>& A, const ndTemplateVector<T>& B) const
115  {
116  return *this + A * B;
117  }
118 
119  ndTemplateVector<T> MulSub(const ndTemplateVector<T>& A, const ndTemplateVector<T>& B) const
120  {
121  return *this - A * B;
122  }
123 
124  // return cross product
125  ndTemplateVector<T> CrossProduct (const ndTemplateVector<T>& B) const
126  {
127  return ndTemplateVector<T> (m_y * B.m_z - m_z * B.m_y,
128  m_z * B.m_x - m_x * B.m_z,
129  m_x * B.m_y - m_y * B.m_x, m_w);
130  }
131 
132  ndTemplateVector<T> CrossProduct(const ndTemplateVector &A, const ndTemplateVector &B) const
133  {
134  T cofactor[3][3];
135  T array[4][4];
136 
137  const ndTemplateVector<T>& me = *this;
138  for (ndInt32 i = 0; i < 4; ++i) {
139  array[0][i] = me[i];
140  array[1][i] = A[i];
141  array[2][i] = B[i];
142  array[3][i] = T(1.0f);
143  }
144 
145  ndTemplateVector<T> normal;
146  T sign = T(-1.0f);
147  for (ndInt32 i = 0; i < 4; ++i)
148  {
149  for (ndInt32 j = 0; j < 3; ++j)
150  {
151  ndInt32 k0 = 0;
152  for (ndInt32 k = 0; k < 4; ++k)
153  {
154  if (k != i)
155  {
156  cofactor[j][k0] = array[j][k];
157  k0++;
158  }
159  }
160  }
161  T x = cofactor[0][0] * (cofactor[1][1] * cofactor[2][2] - cofactor[1][2] * cofactor[2][1]);
162  T y = cofactor[0][1] * (cofactor[1][2] * cofactor[2][0] - cofactor[1][0] * cofactor[2][2]);
163  T z = cofactor[0][2] * (cofactor[1][0] * cofactor[2][1] - cofactor[1][1] * cofactor[2][0]);
164  T det = x + y + z;
165 
166  normal[i] = sign * det;
167  sign *= T(-1.0f);
168  }
169 
170  return normal;
171  }
172 
173  // return dot 4d dot product
174  ndTemplateVector<T> DotProduct (const ndTemplateVector &A) const
175  {
176  T val (m_x * A.m_x + m_y * A.m_y + m_z * A.m_z + m_w * A.m_w);
177  return ndTemplateVector<T> (val, val, val, val);
178  }
179 
180 
181  T GetMax () const
182  {
183  return ndMax(ndMax(m_x, m_y), ndMax(m_z, m_w));
184  }
185 
186  ndTemplateVector<T> GetMax(const ndTemplateVector<T>& data) const
187  {
188  return ndTemplateVector<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);
189  }
190 
191  ndTemplateVector<T> GetMin(const ndTemplateVector<T>& data) const
192  {
193  return ndTemplateVector<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);
194  }
195 
196  // check validity of floats
197 #ifdef _DEBUG
198  void Trace (char* const name) const
199  {
200  ndTrace (("%s %f %f %f %f\n", name, m_x, m_y, m_z, m_w));
201  }
202 #endif
203 
204  T m_x;
205  T m_y;
206  T m_z;
207  T m_w;
208 };
209 
210 #endif
ndClassAlloc
Base class for providing memory allocation for all other engine classes.
Definition: ndClassAlloc.h:30
ndTemplateVector
Definition: ndTemplateVector.h:31